qcom_rpm-regulator.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014, Sony Mobile Communications AB.
  4. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/platform_device.h>
  8. #include <linux/of.h>
  9. #include <linux/of_device.h>
  10. #include <linux/regulator/driver.h>
  11. #include <linux/regulator/machine.h>
  12. #include <linux/regulator/of_regulator.h>
  13. #include <linux/mfd/qcom_rpm.h>
  14. #include <dt-bindings/mfd/qcom-rpm.h>
  15. #define MAX_REQUEST_LEN 2
  16. struct request_member {
  17. int word;
  18. unsigned int mask;
  19. int shift;
  20. };
  21. struct rpm_reg_parts {
  22. struct request_member mV; /* used if voltage is in mV */
  23. struct request_member uV; /* used if voltage is in uV */
  24. struct request_member ip; /* peak current in mA */
  25. struct request_member pd; /* pull down enable */
  26. struct request_member ia; /* average current in mA */
  27. struct request_member fm; /* force mode */
  28. struct request_member pm; /* power mode */
  29. struct request_member pc; /* pin control */
  30. struct request_member pf; /* pin function */
  31. struct request_member enable_state; /* NCP and switch */
  32. struct request_member comp_mode; /* NCP */
  33. struct request_member freq; /* frequency: NCP and SMPS */
  34. struct request_member freq_clk_src; /* clock source: SMPS */
  35. struct request_member hpm; /* switch: control OCP and SS */
  36. int request_len;
  37. };
  38. #define FORCE_MODE_IS_2_BITS(reg) \
  39. (((reg)->parts->fm.mask >> (reg)->parts->fm.shift) == 3)
  40. struct qcom_rpm_reg {
  41. struct qcom_rpm *rpm;
  42. struct mutex lock;
  43. struct device *dev;
  44. struct regulator_desc desc;
  45. const struct rpm_reg_parts *parts;
  46. int resource;
  47. u32 val[MAX_REQUEST_LEN];
  48. int uV;
  49. int is_enabled;
  50. bool supports_force_mode_auto;
  51. bool supports_force_mode_bypass;
  52. };
  53. static const struct rpm_reg_parts rpm8660_ldo_parts = {
  54. .request_len = 2,
  55. .mV = { 0, 0x00000FFF, 0 },
  56. .ip = { 0, 0x00FFF000, 12 },
  57. .fm = { 0, 0x03000000, 24 },
  58. .pc = { 0, 0x3C000000, 26 },
  59. .pf = { 0, 0xC0000000, 30 },
  60. .pd = { 1, 0x00000001, 0 },
  61. .ia = { 1, 0x00001FFE, 1 },
  62. };
  63. static const struct rpm_reg_parts rpm8660_smps_parts = {
  64. .request_len = 2,
  65. .mV = { 0, 0x00000FFF, 0 },
  66. .ip = { 0, 0x00FFF000, 12 },
  67. .fm = { 0, 0x03000000, 24 },
  68. .pc = { 0, 0x3C000000, 26 },
  69. .pf = { 0, 0xC0000000, 30 },
  70. .pd = { 1, 0x00000001, 0 },
  71. .ia = { 1, 0x00001FFE, 1 },
  72. .freq = { 1, 0x001FE000, 13 },
  73. .freq_clk_src = { 1, 0x00600000, 21 },
  74. };
  75. static const struct rpm_reg_parts rpm8660_switch_parts = {
  76. .request_len = 1,
  77. .enable_state = { 0, 0x00000001, 0 },
  78. .pd = { 0, 0x00000002, 1 },
  79. .pc = { 0, 0x0000003C, 2 },
  80. .pf = { 0, 0x000000C0, 6 },
  81. .hpm = { 0, 0x00000300, 8 },
  82. };
  83. static const struct rpm_reg_parts rpm8660_ncp_parts = {
  84. .request_len = 1,
  85. .mV = { 0, 0x00000FFF, 0 },
  86. .enable_state = { 0, 0x00001000, 12 },
  87. .comp_mode = { 0, 0x00002000, 13 },
  88. .freq = { 0, 0x003FC000, 14 },
  89. };
  90. static const struct rpm_reg_parts rpm8960_ldo_parts = {
  91. .request_len = 2,
  92. .uV = { 0, 0x007FFFFF, 0 },
  93. .pd = { 0, 0x00800000, 23 },
  94. .pc = { 0, 0x0F000000, 24 },
  95. .pf = { 0, 0xF0000000, 28 },
  96. .ip = { 1, 0x000003FF, 0 },
  97. .ia = { 1, 0x000FFC00, 10 },
  98. .fm = { 1, 0x00700000, 20 },
  99. };
  100. static const struct rpm_reg_parts rpm8960_smps_parts = {
  101. .request_len = 2,
  102. .uV = { 0, 0x007FFFFF, 0 },
  103. .pd = { 0, 0x00800000, 23 },
  104. .pc = { 0, 0x0F000000, 24 },
  105. .pf = { 0, 0xF0000000, 28 },
  106. .ip = { 1, 0x000003FF, 0 },
  107. .ia = { 1, 0x000FFC00, 10 },
  108. .fm = { 1, 0x00700000, 20 },
  109. .pm = { 1, 0x00800000, 23 },
  110. .freq = { 1, 0x1F000000, 24 },
  111. .freq_clk_src = { 1, 0x60000000, 29 },
  112. };
  113. static const struct rpm_reg_parts rpm8960_switch_parts = {
  114. .request_len = 1,
  115. .enable_state = { 0, 0x00000001, 0 },
  116. .pd = { 0, 0x00000002, 1 },
  117. .pc = { 0, 0x0000003C, 2 },
  118. .pf = { 0, 0x000003C0, 6 },
  119. .hpm = { 0, 0x00000C00, 10 },
  120. };
  121. static const struct rpm_reg_parts rpm8960_ncp_parts = {
  122. .request_len = 1,
  123. .uV = { 0, 0x007FFFFF, 0 },
  124. .enable_state = { 0, 0x00800000, 23 },
  125. .comp_mode = { 0, 0x01000000, 24 },
  126. .freq = { 0, 0x3E000000, 25 },
  127. };
  128. /*
  129. * Physically available PMIC regulator voltage ranges
  130. */
  131. static const struct regulator_linear_range pldo_ranges[] = {
  132. REGULATOR_LINEAR_RANGE( 750000, 0, 59, 12500),
  133. REGULATOR_LINEAR_RANGE(1500000, 60, 123, 25000),
  134. REGULATOR_LINEAR_RANGE(3100000, 124, 160, 50000),
  135. };
  136. static const struct regulator_linear_range nldo_ranges[] = {
  137. REGULATOR_LINEAR_RANGE( 750000, 0, 63, 12500),
  138. };
  139. static const struct regulator_linear_range nldo1200_ranges[] = {
  140. REGULATOR_LINEAR_RANGE( 375000, 0, 59, 6250),
  141. REGULATOR_LINEAR_RANGE( 750000, 60, 123, 12500),
  142. };
  143. static const struct regulator_linear_range smps_ranges[] = {
  144. REGULATOR_LINEAR_RANGE( 375000, 0, 29, 12500),
  145. REGULATOR_LINEAR_RANGE( 750000, 30, 89, 12500),
  146. REGULATOR_LINEAR_RANGE(1500000, 90, 153, 25000),
  147. };
  148. static const struct regulator_linear_range ftsmps_ranges[] = {
  149. REGULATOR_LINEAR_RANGE( 350000, 0, 6, 50000),
  150. REGULATOR_LINEAR_RANGE( 700000, 7, 63, 12500),
  151. REGULATOR_LINEAR_RANGE(1500000, 64, 100, 50000),
  152. };
  153. static const struct regulator_linear_range smb208_ranges[] = {
  154. REGULATOR_LINEAR_RANGE( 375000, 0, 29, 12500),
  155. REGULATOR_LINEAR_RANGE( 750000, 30, 89, 12500),
  156. REGULATOR_LINEAR_RANGE(1500000, 90, 153, 25000),
  157. REGULATOR_LINEAR_RANGE(3100000, 154, 234, 25000),
  158. };
  159. static const struct regulator_linear_range ncp_ranges[] = {
  160. REGULATOR_LINEAR_RANGE(1500000, 0, 31, 50000),
  161. };
  162. static int rpm_reg_write(struct qcom_rpm_reg *vreg,
  163. const struct request_member *req,
  164. const int value)
  165. {
  166. if (WARN_ON((value << req->shift) & ~req->mask))
  167. return -EINVAL;
  168. vreg->val[req->word] &= ~req->mask;
  169. vreg->val[req->word] |= value << req->shift;
  170. return qcom_rpm_write(vreg->rpm,
  171. QCOM_RPM_ACTIVE_STATE,
  172. vreg->resource,
  173. vreg->val,
  174. vreg->parts->request_len);
  175. }
  176. static int rpm_reg_set_mV_sel(struct regulator_dev *rdev,
  177. unsigned selector)
  178. {
  179. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  180. const struct rpm_reg_parts *parts = vreg->parts;
  181. const struct request_member *req = &parts->mV;
  182. int ret = 0;
  183. int uV;
  184. if (req->mask == 0)
  185. return -EINVAL;
  186. uV = regulator_list_voltage_linear_range(rdev, selector);
  187. if (uV < 0)
  188. return uV;
  189. mutex_lock(&vreg->lock);
  190. if (vreg->is_enabled)
  191. ret = rpm_reg_write(vreg, req, uV / 1000);
  192. if (!ret)
  193. vreg->uV = uV;
  194. mutex_unlock(&vreg->lock);
  195. return ret;
  196. }
  197. static int rpm_reg_set_uV_sel(struct regulator_dev *rdev,
  198. unsigned selector)
  199. {
  200. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  201. const struct rpm_reg_parts *parts = vreg->parts;
  202. const struct request_member *req = &parts->uV;
  203. int ret = 0;
  204. int uV;
  205. if (req->mask == 0)
  206. return -EINVAL;
  207. uV = regulator_list_voltage_linear_range(rdev, selector);
  208. if (uV < 0)
  209. return uV;
  210. mutex_lock(&vreg->lock);
  211. if (vreg->is_enabled)
  212. ret = rpm_reg_write(vreg, req, uV);
  213. if (!ret)
  214. vreg->uV = uV;
  215. mutex_unlock(&vreg->lock);
  216. return ret;
  217. }
  218. static int rpm_reg_get_voltage(struct regulator_dev *rdev)
  219. {
  220. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  221. return vreg->uV;
  222. }
  223. static int rpm_reg_mV_enable(struct regulator_dev *rdev)
  224. {
  225. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  226. const struct rpm_reg_parts *parts = vreg->parts;
  227. const struct request_member *req = &parts->mV;
  228. int ret;
  229. if (req->mask == 0)
  230. return -EINVAL;
  231. mutex_lock(&vreg->lock);
  232. ret = rpm_reg_write(vreg, req, vreg->uV / 1000);
  233. if (!ret)
  234. vreg->is_enabled = 1;
  235. mutex_unlock(&vreg->lock);
  236. return ret;
  237. }
  238. static int rpm_reg_uV_enable(struct regulator_dev *rdev)
  239. {
  240. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  241. const struct rpm_reg_parts *parts = vreg->parts;
  242. const struct request_member *req = &parts->uV;
  243. int ret;
  244. if (req->mask == 0)
  245. return -EINVAL;
  246. mutex_lock(&vreg->lock);
  247. ret = rpm_reg_write(vreg, req, vreg->uV);
  248. if (!ret)
  249. vreg->is_enabled = 1;
  250. mutex_unlock(&vreg->lock);
  251. return ret;
  252. }
  253. static int rpm_reg_switch_enable(struct regulator_dev *rdev)
  254. {
  255. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  256. const struct rpm_reg_parts *parts = vreg->parts;
  257. const struct request_member *req = &parts->enable_state;
  258. int ret;
  259. if (req->mask == 0)
  260. return -EINVAL;
  261. mutex_lock(&vreg->lock);
  262. ret = rpm_reg_write(vreg, req, 1);
  263. if (!ret)
  264. vreg->is_enabled = 1;
  265. mutex_unlock(&vreg->lock);
  266. return ret;
  267. }
  268. static int rpm_reg_mV_disable(struct regulator_dev *rdev)
  269. {
  270. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  271. const struct rpm_reg_parts *parts = vreg->parts;
  272. const struct request_member *req = &parts->mV;
  273. int ret;
  274. if (req->mask == 0)
  275. return -EINVAL;
  276. mutex_lock(&vreg->lock);
  277. ret = rpm_reg_write(vreg, req, 0);
  278. if (!ret)
  279. vreg->is_enabled = 0;
  280. mutex_unlock(&vreg->lock);
  281. return ret;
  282. }
  283. static int rpm_reg_uV_disable(struct regulator_dev *rdev)
  284. {
  285. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  286. const struct rpm_reg_parts *parts = vreg->parts;
  287. const struct request_member *req = &parts->uV;
  288. int ret;
  289. if (req->mask == 0)
  290. return -EINVAL;
  291. mutex_lock(&vreg->lock);
  292. ret = rpm_reg_write(vreg, req, 0);
  293. if (!ret)
  294. vreg->is_enabled = 0;
  295. mutex_unlock(&vreg->lock);
  296. return ret;
  297. }
  298. static int rpm_reg_switch_disable(struct regulator_dev *rdev)
  299. {
  300. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  301. const struct rpm_reg_parts *parts = vreg->parts;
  302. const struct request_member *req = &parts->enable_state;
  303. int ret;
  304. if (req->mask == 0)
  305. return -EINVAL;
  306. mutex_lock(&vreg->lock);
  307. ret = rpm_reg_write(vreg, req, 0);
  308. if (!ret)
  309. vreg->is_enabled = 0;
  310. mutex_unlock(&vreg->lock);
  311. return ret;
  312. }
  313. static int rpm_reg_is_enabled(struct regulator_dev *rdev)
  314. {
  315. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  316. return vreg->is_enabled;
  317. }
  318. static int rpm_reg_set_load(struct regulator_dev *rdev, int load_uA)
  319. {
  320. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  321. const struct rpm_reg_parts *parts = vreg->parts;
  322. const struct request_member *req = &parts->ia;
  323. int load_mA = load_uA / 1000;
  324. int max_mA = req->mask >> req->shift;
  325. int ret;
  326. if (req->mask == 0)
  327. return -EINVAL;
  328. if (load_mA > max_mA)
  329. load_mA = max_mA;
  330. mutex_lock(&vreg->lock);
  331. ret = rpm_reg_write(vreg, req, load_mA);
  332. mutex_unlock(&vreg->lock);
  333. return ret;
  334. }
  335. static struct regulator_ops uV_ops = {
  336. .list_voltage = regulator_list_voltage_linear_range,
  337. .set_voltage_sel = rpm_reg_set_uV_sel,
  338. .get_voltage = rpm_reg_get_voltage,
  339. .enable = rpm_reg_uV_enable,
  340. .disable = rpm_reg_uV_disable,
  341. .is_enabled = rpm_reg_is_enabled,
  342. .set_load = rpm_reg_set_load,
  343. };
  344. static struct regulator_ops mV_ops = {
  345. .list_voltage = regulator_list_voltage_linear_range,
  346. .set_voltage_sel = rpm_reg_set_mV_sel,
  347. .get_voltage = rpm_reg_get_voltage,
  348. .enable = rpm_reg_mV_enable,
  349. .disable = rpm_reg_mV_disable,
  350. .is_enabled = rpm_reg_is_enabled,
  351. .set_load = rpm_reg_set_load,
  352. };
  353. static struct regulator_ops switch_ops = {
  354. .enable = rpm_reg_switch_enable,
  355. .disable = rpm_reg_switch_disable,
  356. .is_enabled = rpm_reg_is_enabled,
  357. };
  358. /*
  359. * PM8018 regulators
  360. */
  361. static const struct qcom_rpm_reg pm8018_pldo = {
  362. .desc.linear_ranges = pldo_ranges,
  363. .desc.n_linear_ranges = ARRAY_SIZE(pldo_ranges),
  364. .desc.n_voltages = 161,
  365. .desc.ops = &uV_ops,
  366. .parts = &rpm8960_ldo_parts,
  367. .supports_force_mode_auto = false,
  368. .supports_force_mode_bypass = false,
  369. };
  370. static const struct qcom_rpm_reg pm8018_nldo = {
  371. .desc.linear_ranges = nldo_ranges,
  372. .desc.n_linear_ranges = ARRAY_SIZE(nldo_ranges),
  373. .desc.n_voltages = 64,
  374. .desc.ops = &uV_ops,
  375. .parts = &rpm8960_ldo_parts,
  376. .supports_force_mode_auto = false,
  377. .supports_force_mode_bypass = false,
  378. };
  379. static const struct qcom_rpm_reg pm8018_smps = {
  380. .desc.linear_ranges = smps_ranges,
  381. .desc.n_linear_ranges = ARRAY_SIZE(smps_ranges),
  382. .desc.n_voltages = 154,
  383. .desc.ops = &uV_ops,
  384. .parts = &rpm8960_smps_parts,
  385. .supports_force_mode_auto = false,
  386. .supports_force_mode_bypass = false,
  387. };
  388. static const struct qcom_rpm_reg pm8018_switch = {
  389. .desc.ops = &switch_ops,
  390. .parts = &rpm8960_switch_parts,
  391. };
  392. /*
  393. * PM8058 regulators
  394. */
  395. static const struct qcom_rpm_reg pm8058_pldo = {
  396. .desc.linear_ranges = pldo_ranges,
  397. .desc.n_linear_ranges = ARRAY_SIZE(pldo_ranges),
  398. .desc.n_voltages = 161,
  399. .desc.ops = &mV_ops,
  400. .parts = &rpm8660_ldo_parts,
  401. .supports_force_mode_auto = false,
  402. .supports_force_mode_bypass = false,
  403. };
  404. static const struct qcom_rpm_reg pm8058_nldo = {
  405. .desc.linear_ranges = nldo_ranges,
  406. .desc.n_linear_ranges = ARRAY_SIZE(nldo_ranges),
  407. .desc.n_voltages = 64,
  408. .desc.ops = &mV_ops,
  409. .parts = &rpm8660_ldo_parts,
  410. .supports_force_mode_auto = false,
  411. .supports_force_mode_bypass = false,
  412. };
  413. static const struct qcom_rpm_reg pm8058_smps = {
  414. .desc.linear_ranges = smps_ranges,
  415. .desc.n_linear_ranges = ARRAY_SIZE(smps_ranges),
  416. .desc.n_voltages = 154,
  417. .desc.ops = &mV_ops,
  418. .parts = &rpm8660_smps_parts,
  419. .supports_force_mode_auto = false,
  420. .supports_force_mode_bypass = false,
  421. };
  422. static const struct qcom_rpm_reg pm8058_ncp = {
  423. .desc.linear_ranges = ncp_ranges,
  424. .desc.n_linear_ranges = ARRAY_SIZE(ncp_ranges),
  425. .desc.n_voltages = 32,
  426. .desc.ops = &mV_ops,
  427. .parts = &rpm8660_ncp_parts,
  428. };
  429. static const struct qcom_rpm_reg pm8058_switch = {
  430. .desc.ops = &switch_ops,
  431. .parts = &rpm8660_switch_parts,
  432. };
  433. /*
  434. * PM8901 regulators
  435. */
  436. static const struct qcom_rpm_reg pm8901_pldo = {
  437. .desc.linear_ranges = pldo_ranges,
  438. .desc.n_linear_ranges = ARRAY_SIZE(pldo_ranges),
  439. .desc.n_voltages = 161,
  440. .desc.ops = &mV_ops,
  441. .parts = &rpm8660_ldo_parts,
  442. .supports_force_mode_auto = false,
  443. .supports_force_mode_bypass = true,
  444. };
  445. static const struct qcom_rpm_reg pm8901_nldo = {
  446. .desc.linear_ranges = nldo_ranges,
  447. .desc.n_linear_ranges = ARRAY_SIZE(nldo_ranges),
  448. .desc.n_voltages = 64,
  449. .desc.ops = &mV_ops,
  450. .parts = &rpm8660_ldo_parts,
  451. .supports_force_mode_auto = false,
  452. .supports_force_mode_bypass = true,
  453. };
  454. static const struct qcom_rpm_reg pm8901_ftsmps = {
  455. .desc.linear_ranges = ftsmps_ranges,
  456. .desc.n_linear_ranges = ARRAY_SIZE(ftsmps_ranges),
  457. .desc.n_voltages = 101,
  458. .desc.ops = &mV_ops,
  459. .parts = &rpm8660_smps_parts,
  460. .supports_force_mode_auto = true,
  461. .supports_force_mode_bypass = false,
  462. };
  463. static const struct qcom_rpm_reg pm8901_switch = {
  464. .desc.ops = &switch_ops,
  465. .parts = &rpm8660_switch_parts,
  466. };
  467. /*
  468. * PM8921 regulators
  469. */
  470. static const struct qcom_rpm_reg pm8921_pldo = {
  471. .desc.linear_ranges = pldo_ranges,
  472. .desc.n_linear_ranges = ARRAY_SIZE(pldo_ranges),
  473. .desc.n_voltages = 161,
  474. .desc.ops = &uV_ops,
  475. .parts = &rpm8960_ldo_parts,
  476. .supports_force_mode_auto = false,
  477. .supports_force_mode_bypass = true,
  478. };
  479. static const struct qcom_rpm_reg pm8921_nldo = {
  480. .desc.linear_ranges = nldo_ranges,
  481. .desc.n_linear_ranges = ARRAY_SIZE(nldo_ranges),
  482. .desc.n_voltages = 64,
  483. .desc.ops = &uV_ops,
  484. .parts = &rpm8960_ldo_parts,
  485. .supports_force_mode_auto = false,
  486. .supports_force_mode_bypass = true,
  487. };
  488. static const struct qcom_rpm_reg pm8921_nldo1200 = {
  489. .desc.linear_ranges = nldo1200_ranges,
  490. .desc.n_linear_ranges = ARRAY_SIZE(nldo1200_ranges),
  491. .desc.n_voltages = 124,
  492. .desc.ops = &uV_ops,
  493. .parts = &rpm8960_ldo_parts,
  494. .supports_force_mode_auto = false,
  495. .supports_force_mode_bypass = true,
  496. };
  497. static const struct qcom_rpm_reg pm8921_smps = {
  498. .desc.linear_ranges = smps_ranges,
  499. .desc.n_linear_ranges = ARRAY_SIZE(smps_ranges),
  500. .desc.n_voltages = 154,
  501. .desc.ops = &uV_ops,
  502. .parts = &rpm8960_smps_parts,
  503. .supports_force_mode_auto = true,
  504. .supports_force_mode_bypass = false,
  505. };
  506. static const struct qcom_rpm_reg pm8921_ftsmps = {
  507. .desc.linear_ranges = ftsmps_ranges,
  508. .desc.n_linear_ranges = ARRAY_SIZE(ftsmps_ranges),
  509. .desc.n_voltages = 101,
  510. .desc.ops = &uV_ops,
  511. .parts = &rpm8960_smps_parts,
  512. .supports_force_mode_auto = true,
  513. .supports_force_mode_bypass = false,
  514. };
  515. static const struct qcom_rpm_reg pm8921_ncp = {
  516. .desc.linear_ranges = ncp_ranges,
  517. .desc.n_linear_ranges = ARRAY_SIZE(ncp_ranges),
  518. .desc.n_voltages = 32,
  519. .desc.ops = &uV_ops,
  520. .parts = &rpm8960_ncp_parts,
  521. };
  522. static const struct qcom_rpm_reg pm8921_switch = {
  523. .desc.ops = &switch_ops,
  524. .parts = &rpm8960_switch_parts,
  525. };
  526. static const struct qcom_rpm_reg smb208_smps = {
  527. .desc.linear_ranges = smb208_ranges,
  528. .desc.n_linear_ranges = ARRAY_SIZE(smb208_ranges),
  529. .desc.n_voltages = 235,
  530. .desc.ops = &uV_ops,
  531. .parts = &rpm8960_smps_parts,
  532. .supports_force_mode_auto = false,
  533. .supports_force_mode_bypass = false,
  534. };
  535. static int rpm_reg_set(struct qcom_rpm_reg *vreg,
  536. const struct request_member *req,
  537. const int value)
  538. {
  539. if (req->mask == 0 || (value << req->shift) & ~req->mask)
  540. return -EINVAL;
  541. vreg->val[req->word] &= ~req->mask;
  542. vreg->val[req->word] |= value << req->shift;
  543. return 0;
  544. }
  545. static int rpm_reg_of_parse_freq(struct device *dev,
  546. struct device_node *node,
  547. struct qcom_rpm_reg *vreg)
  548. {
  549. static const int freq_table[] = {
  550. 19200000, 9600000, 6400000, 4800000, 3840000, 3200000, 2740000,
  551. 2400000, 2130000, 1920000, 1750000, 1600000, 1480000, 1370000,
  552. 1280000, 1200000,
  553. };
  554. const char *key;
  555. u32 freq;
  556. int ret;
  557. int i;
  558. key = "qcom,switch-mode-frequency";
  559. ret = of_property_read_u32(node, key, &freq);
  560. if (ret) {
  561. dev_err(dev, "regulator requires %s property\n", key);
  562. return -EINVAL;
  563. }
  564. for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
  565. if (freq == freq_table[i]) {
  566. rpm_reg_set(vreg, &vreg->parts->freq, i + 1);
  567. return 0;
  568. }
  569. }
  570. dev_err(dev, "invalid frequency %d\n", freq);
  571. return -EINVAL;
  572. }
  573. static int rpm_reg_of_parse(struct device_node *node,
  574. const struct regulator_desc *desc,
  575. struct regulator_config *config)
  576. {
  577. struct qcom_rpm_reg *vreg = config->driver_data;
  578. struct device *dev = config->dev;
  579. const char *key;
  580. u32 force_mode;
  581. bool pwm;
  582. u32 val;
  583. int ret;
  584. key = "bias-pull-down";
  585. if (of_property_read_bool(node, key)) {
  586. ret = rpm_reg_set(vreg, &vreg->parts->pd, 1);
  587. if (ret) {
  588. dev_err(dev, "%s is invalid", key);
  589. return ret;
  590. }
  591. }
  592. if (vreg->parts->freq.mask) {
  593. ret = rpm_reg_of_parse_freq(dev, node, vreg);
  594. if (ret < 0)
  595. return ret;
  596. }
  597. if (vreg->parts->pm.mask) {
  598. key = "qcom,power-mode-hysteretic";
  599. pwm = !of_property_read_bool(node, key);
  600. ret = rpm_reg_set(vreg, &vreg->parts->pm, pwm);
  601. if (ret) {
  602. dev_err(dev, "failed to set power mode\n");
  603. return ret;
  604. }
  605. }
  606. if (vreg->parts->fm.mask) {
  607. force_mode = -1;
  608. key = "qcom,force-mode";
  609. ret = of_property_read_u32(node, key, &val);
  610. if (ret == -EINVAL) {
  611. val = QCOM_RPM_FORCE_MODE_NONE;
  612. } else if (ret < 0) {
  613. dev_err(dev, "failed to read %s\n", key);
  614. return ret;
  615. }
  616. /*
  617. * If force-mode is encoded as 2 bits then the
  618. * possible register values are:
  619. * NONE, LPM, HPM
  620. * otherwise:
  621. * NONE, LPM, AUTO, HPM, BYPASS
  622. */
  623. switch (val) {
  624. case QCOM_RPM_FORCE_MODE_NONE:
  625. force_mode = 0;
  626. break;
  627. case QCOM_RPM_FORCE_MODE_LPM:
  628. force_mode = 1;
  629. break;
  630. case QCOM_RPM_FORCE_MODE_HPM:
  631. if (FORCE_MODE_IS_2_BITS(vreg))
  632. force_mode = 2;
  633. else
  634. force_mode = 3;
  635. break;
  636. case QCOM_RPM_FORCE_MODE_AUTO:
  637. if (vreg->supports_force_mode_auto)
  638. force_mode = 2;
  639. break;
  640. case QCOM_RPM_FORCE_MODE_BYPASS:
  641. if (vreg->supports_force_mode_bypass)
  642. force_mode = 4;
  643. break;
  644. }
  645. if (force_mode == -1) {
  646. dev_err(dev, "invalid force mode\n");
  647. return -EINVAL;
  648. }
  649. ret = rpm_reg_set(vreg, &vreg->parts->fm, force_mode);
  650. if (ret) {
  651. dev_err(dev, "failed to set force mode\n");
  652. return ret;
  653. }
  654. }
  655. return 0;
  656. }
  657. struct rpm_regulator_data {
  658. const char *name;
  659. int resource;
  660. const struct qcom_rpm_reg *template;
  661. const char *supply;
  662. };
  663. static const struct rpm_regulator_data rpm_pm8018_regulators[] = {
  664. { "s1", QCOM_RPM_PM8018_SMPS1, &pm8018_smps, "vdd_s1" },
  665. { "s2", QCOM_RPM_PM8018_SMPS2, &pm8018_smps, "vdd_s2" },
  666. { "s3", QCOM_RPM_PM8018_SMPS3, &pm8018_smps, "vdd_s3" },
  667. { "s4", QCOM_RPM_PM8018_SMPS4, &pm8018_smps, "vdd_s4" },
  668. { "s5", QCOM_RPM_PM8018_SMPS5, &pm8018_smps, "vdd_s5" },
  669. { "l2", QCOM_RPM_PM8018_LDO2, &pm8018_pldo, "vdd_l2" },
  670. { "l3", QCOM_RPM_PM8018_LDO3, &pm8018_pldo, "vdd_l3" },
  671. { "l4", QCOM_RPM_PM8018_LDO4, &pm8018_pldo, "vdd_l4" },
  672. { "l5", QCOM_RPM_PM8018_LDO5, &pm8018_pldo, "vdd_l5" },
  673. { "l6", QCOM_RPM_PM8018_LDO6, &pm8018_pldo, "vdd_l7" },
  674. { "l7", QCOM_RPM_PM8018_LDO7, &pm8018_pldo, "vdd_l7" },
  675. { "l8", QCOM_RPM_PM8018_LDO8, &pm8018_nldo, "vdd_l8" },
  676. { "l9", QCOM_RPM_PM8018_LDO9, &pm8921_nldo1200,
  677. "vdd_l9_l10_l11_l12" },
  678. { "l10", QCOM_RPM_PM8018_LDO10, &pm8018_nldo, "vdd_l9_l10_l11_l12" },
  679. { "l11", QCOM_RPM_PM8018_LDO11, &pm8018_nldo, "vdd_l9_l10_l11_l12" },
  680. { "l12", QCOM_RPM_PM8018_LDO12, &pm8018_nldo, "vdd_l9_l10_l11_l12" },
  681. { "l14", QCOM_RPM_PM8018_LDO14, &pm8018_pldo, "vdd_l14" },
  682. { "lvs1", QCOM_RPM_PM8018_LVS1, &pm8018_switch, "lvs1_in" },
  683. { }
  684. };
  685. static const struct rpm_regulator_data rpm_pm8058_regulators[] = {
  686. { "l0", QCOM_RPM_PM8058_LDO0, &pm8058_nldo, "vdd_l0_l1_lvs" },
  687. { "l1", QCOM_RPM_PM8058_LDO1, &pm8058_nldo, "vdd_l0_l1_lvs" },
  688. { "l2", QCOM_RPM_PM8058_LDO2, &pm8058_pldo, "vdd_l2_l11_l12" },
  689. { "l3", QCOM_RPM_PM8058_LDO3, &pm8058_pldo, "vdd_l3_l4_l5" },
  690. { "l4", QCOM_RPM_PM8058_LDO4, &pm8058_pldo, "vdd_l3_l4_l5" },
  691. { "l5", QCOM_RPM_PM8058_LDO5, &pm8058_pldo, "vdd_l3_l4_l5" },
  692. { "l6", QCOM_RPM_PM8058_LDO6, &pm8058_pldo, "vdd_l6_l7" },
  693. { "l7", QCOM_RPM_PM8058_LDO7, &pm8058_pldo, "vdd_l6_l7" },
  694. { "l8", QCOM_RPM_PM8058_LDO8, &pm8058_pldo, "vdd_l8" },
  695. { "l9", QCOM_RPM_PM8058_LDO9, &pm8058_pldo, "vdd_l9" },
  696. { "l10", QCOM_RPM_PM8058_LDO10, &pm8058_pldo, "vdd_l10" },
  697. { "l11", QCOM_RPM_PM8058_LDO11, &pm8058_pldo, "vdd_l2_l11_l12" },
  698. { "l12", QCOM_RPM_PM8058_LDO12, &pm8058_pldo, "vdd_l2_l11_l12" },
  699. { "l13", QCOM_RPM_PM8058_LDO13, &pm8058_pldo, "vdd_l13_l16" },
  700. { "l14", QCOM_RPM_PM8058_LDO14, &pm8058_pldo, "vdd_l14_l15" },
  701. { "l15", QCOM_RPM_PM8058_LDO15, &pm8058_pldo, "vdd_l14_l15" },
  702. { "l16", QCOM_RPM_PM8058_LDO16, &pm8058_pldo, "vdd_l13_l16" },
  703. { "l17", QCOM_RPM_PM8058_LDO17, &pm8058_pldo, "vdd_l17_l18" },
  704. { "l18", QCOM_RPM_PM8058_LDO18, &pm8058_pldo, "vdd_l17_l18" },
  705. { "l19", QCOM_RPM_PM8058_LDO19, &pm8058_pldo, "vdd_l19_l20" },
  706. { "l20", QCOM_RPM_PM8058_LDO20, &pm8058_pldo, "vdd_l19_l20" },
  707. { "l21", QCOM_RPM_PM8058_LDO21, &pm8058_nldo, "vdd_l21" },
  708. { "l22", QCOM_RPM_PM8058_LDO22, &pm8058_nldo, "vdd_l22" },
  709. { "l23", QCOM_RPM_PM8058_LDO23, &pm8058_nldo, "vdd_l23_l24_l25" },
  710. { "l24", QCOM_RPM_PM8058_LDO24, &pm8058_nldo, "vdd_l23_l24_l25" },
  711. { "l25", QCOM_RPM_PM8058_LDO25, &pm8058_nldo, "vdd_l23_l24_l25" },
  712. { "s0", QCOM_RPM_PM8058_SMPS0, &pm8058_smps, "vdd_s0" },
  713. { "s1", QCOM_RPM_PM8058_SMPS1, &pm8058_smps, "vdd_s1" },
  714. { "s2", QCOM_RPM_PM8058_SMPS2, &pm8058_smps, "vdd_s2" },
  715. { "s3", QCOM_RPM_PM8058_SMPS3, &pm8058_smps, "vdd_s3" },
  716. { "s4", QCOM_RPM_PM8058_SMPS4, &pm8058_smps, "vdd_s4" },
  717. { "lvs0", QCOM_RPM_PM8058_LVS0, &pm8058_switch, "vdd_l0_l1_lvs" },
  718. { "lvs1", QCOM_RPM_PM8058_LVS1, &pm8058_switch, "vdd_l0_l1_lvs" },
  719. { "ncp", QCOM_RPM_PM8058_NCP, &pm8058_ncp, "vdd_ncp" },
  720. { }
  721. };
  722. static const struct rpm_regulator_data rpm_pm8901_regulators[] = {
  723. { "l0", QCOM_RPM_PM8901_LDO0, &pm8901_nldo, "vdd_l0" },
  724. { "l1", QCOM_RPM_PM8901_LDO1, &pm8901_pldo, "vdd_l1" },
  725. { "l2", QCOM_RPM_PM8901_LDO2, &pm8901_pldo, "vdd_l2" },
  726. { "l3", QCOM_RPM_PM8901_LDO3, &pm8901_pldo, "vdd_l3" },
  727. { "l4", QCOM_RPM_PM8901_LDO4, &pm8901_pldo, "vdd_l4" },
  728. { "l5", QCOM_RPM_PM8901_LDO5, &pm8901_pldo, "vdd_l5" },
  729. { "l6", QCOM_RPM_PM8901_LDO6, &pm8901_pldo, "vdd_l6" },
  730. { "s0", QCOM_RPM_PM8901_SMPS0, &pm8901_ftsmps, "vdd_s0" },
  731. { "s1", QCOM_RPM_PM8901_SMPS1, &pm8901_ftsmps, "vdd_s1" },
  732. { "s2", QCOM_RPM_PM8901_SMPS2, &pm8901_ftsmps, "vdd_s2" },
  733. { "s3", QCOM_RPM_PM8901_SMPS3, &pm8901_ftsmps, "vdd_s3" },
  734. { "s4", QCOM_RPM_PM8901_SMPS4, &pm8901_ftsmps, "vdd_s4" },
  735. { "lvs0", QCOM_RPM_PM8901_LVS0, &pm8901_switch, "lvs0_in" },
  736. { "lvs1", QCOM_RPM_PM8901_LVS1, &pm8901_switch, "lvs1_in" },
  737. { "lvs2", QCOM_RPM_PM8901_LVS2, &pm8901_switch, "lvs2_in" },
  738. { "lvs3", QCOM_RPM_PM8901_LVS3, &pm8901_switch, "lvs3_in" },
  739. { "mvs", QCOM_RPM_PM8901_MVS, &pm8901_switch, "mvs_in" },
  740. { }
  741. };
  742. static const struct rpm_regulator_data rpm_pm8921_regulators[] = {
  743. { "s1", QCOM_RPM_PM8921_SMPS1, &pm8921_smps, "vdd_s1" },
  744. { "s2", QCOM_RPM_PM8921_SMPS2, &pm8921_smps, "vdd_s2" },
  745. { "s3", QCOM_RPM_PM8921_SMPS3, &pm8921_smps },
  746. { "s4", QCOM_RPM_PM8921_SMPS4, &pm8921_smps, "vdd_s4" },
  747. { "s7", QCOM_RPM_PM8921_SMPS7, &pm8921_smps, "vdd_s7" },
  748. { "s8", QCOM_RPM_PM8921_SMPS8, &pm8921_smps, "vdd_s8" },
  749. { "l1", QCOM_RPM_PM8921_LDO1, &pm8921_nldo, "vdd_l1_l2_l12_l18" },
  750. { "l2", QCOM_RPM_PM8921_LDO2, &pm8921_nldo, "vdd_l1_l2_l12_l18" },
  751. { "l3", QCOM_RPM_PM8921_LDO3, &pm8921_pldo, "vdd_l3_l15_l17" },
  752. { "l4", QCOM_RPM_PM8921_LDO4, &pm8921_pldo, "vdd_l4_l14" },
  753. { "l5", QCOM_RPM_PM8921_LDO5, &pm8921_pldo, "vdd_l5_l8_l16" },
  754. { "l6", QCOM_RPM_PM8921_LDO6, &pm8921_pldo, "vdd_l6_l7" },
  755. { "l7", QCOM_RPM_PM8921_LDO7, &pm8921_pldo, "vdd_l6_l7" },
  756. { "l8", QCOM_RPM_PM8921_LDO8, &pm8921_pldo, "vdd_l5_l8_l16" },
  757. { "l9", QCOM_RPM_PM8921_LDO9, &pm8921_pldo, "vdd_l9_l11" },
  758. { "l10", QCOM_RPM_PM8921_LDO10, &pm8921_pldo, "vdd_l10_l22" },
  759. { "l11", QCOM_RPM_PM8921_LDO11, &pm8921_pldo, "vdd_l9_l11" },
  760. { "l12", QCOM_RPM_PM8921_LDO12, &pm8921_nldo, "vdd_l1_l2_l12_l18" },
  761. { "l14", QCOM_RPM_PM8921_LDO14, &pm8921_pldo, "vdd_l4_l14" },
  762. { "l15", QCOM_RPM_PM8921_LDO15, &pm8921_pldo, "vdd_l3_l15_l17" },
  763. { "l16", QCOM_RPM_PM8921_LDO16, &pm8921_pldo, "vdd_l5_l8_l16" },
  764. { "l17", QCOM_RPM_PM8921_LDO17, &pm8921_pldo, "vdd_l3_l15_l17" },
  765. { "l18", QCOM_RPM_PM8921_LDO18, &pm8921_nldo, "vdd_l1_l2_l12_l18" },
  766. { "l21", QCOM_RPM_PM8921_LDO21, &pm8921_pldo, "vdd_l21_l23_l29" },
  767. { "l22", QCOM_RPM_PM8921_LDO22, &pm8921_pldo, "vdd_l10_l22" },
  768. { "l23", QCOM_RPM_PM8921_LDO23, &pm8921_pldo, "vdd_l21_l23_l29" },
  769. { "l24", QCOM_RPM_PM8921_LDO24, &pm8921_nldo1200, "vdd_l24" },
  770. { "l25", QCOM_RPM_PM8921_LDO25, &pm8921_nldo1200, "vdd_l25" },
  771. { "l26", QCOM_RPM_PM8921_LDO26, &pm8921_nldo1200, "vdd_l26" },
  772. { "l27", QCOM_RPM_PM8921_LDO27, &pm8921_nldo1200, "vdd_l27" },
  773. { "l28", QCOM_RPM_PM8921_LDO28, &pm8921_nldo1200, "vdd_l28" },
  774. { "l29", QCOM_RPM_PM8921_LDO29, &pm8921_pldo, "vdd_l21_l23_l29" },
  775. { "lvs1", QCOM_RPM_PM8921_LVS1, &pm8921_switch, "vin_lvs1_3_6" },
  776. { "lvs2", QCOM_RPM_PM8921_LVS2, &pm8921_switch, "vin_lvs2" },
  777. { "lvs3", QCOM_RPM_PM8921_LVS3, &pm8921_switch, "vin_lvs1_3_6" },
  778. { "lvs4", QCOM_RPM_PM8921_LVS4, &pm8921_switch, "vin_lvs4_5_7" },
  779. { "lvs5", QCOM_RPM_PM8921_LVS5, &pm8921_switch, "vin_lvs4_5_7" },
  780. { "lvs6", QCOM_RPM_PM8921_LVS6, &pm8921_switch, "vin_lvs1_3_6" },
  781. { "lvs7", QCOM_RPM_PM8921_LVS7, &pm8921_switch, "vin_lvs4_5_7" },
  782. { "usb-switch", QCOM_RPM_USB_OTG_SWITCH, &pm8921_switch, "vin_5vs" },
  783. { "hdmi-switch", QCOM_RPM_HDMI_SWITCH, &pm8921_switch, "vin_5vs" },
  784. { "ncp", QCOM_RPM_PM8921_NCP, &pm8921_ncp, "vdd_ncp" },
  785. { }
  786. };
  787. static const struct of_device_id rpm_of_match[] = {
  788. { .compatible = "qcom,rpm-pm8018-regulators",
  789. .data = &rpm_pm8018_regulators },
  790. { .compatible = "qcom,rpm-pm8058-regulators", .data = &rpm_pm8058_regulators },
  791. { .compatible = "qcom,rpm-pm8901-regulators", .data = &rpm_pm8901_regulators },
  792. { .compatible = "qcom,rpm-pm8921-regulators", .data = &rpm_pm8921_regulators },
  793. { }
  794. };
  795. MODULE_DEVICE_TABLE(of, rpm_of_match);
  796. static int rpm_reg_probe(struct platform_device *pdev)
  797. {
  798. const struct rpm_regulator_data *reg;
  799. const struct of_device_id *match;
  800. struct regulator_config config = { };
  801. struct regulator_dev *rdev;
  802. struct qcom_rpm_reg *vreg;
  803. struct qcom_rpm *rpm;
  804. rpm = dev_get_drvdata(pdev->dev.parent);
  805. if (!rpm) {
  806. dev_err(&pdev->dev, "unable to retrieve handle to rpm\n");
  807. return -ENODEV;
  808. }
  809. match = of_match_device(rpm_of_match, &pdev->dev);
  810. if (!match) {
  811. dev_err(&pdev->dev, "failed to match device\n");
  812. return -ENODEV;
  813. }
  814. for (reg = match->data; reg->name; reg++) {
  815. vreg = devm_kmalloc(&pdev->dev, sizeof(*vreg), GFP_KERNEL);
  816. if (!vreg)
  817. return -ENOMEM;
  818. memcpy(vreg, reg->template, sizeof(*vreg));
  819. mutex_init(&vreg->lock);
  820. vreg->dev = &pdev->dev;
  821. vreg->resource = reg->resource;
  822. vreg->rpm = rpm;
  823. vreg->desc.id = -1;
  824. vreg->desc.owner = THIS_MODULE;
  825. vreg->desc.type = REGULATOR_VOLTAGE;
  826. vreg->desc.name = reg->name;
  827. vreg->desc.supply_name = reg->supply;
  828. vreg->desc.of_match = reg->name;
  829. vreg->desc.of_parse_cb = rpm_reg_of_parse;
  830. config.dev = &pdev->dev;
  831. config.driver_data = vreg;
  832. rdev = devm_regulator_register(&pdev->dev, &vreg->desc, &config);
  833. if (IS_ERR(rdev)) {
  834. dev_err(&pdev->dev, "failed to register %s\n", reg->name);
  835. return PTR_ERR(rdev);
  836. }
  837. }
  838. return 0;
  839. }
  840. static struct platform_driver rpm_reg_driver = {
  841. .probe = rpm_reg_probe,
  842. .driver = {
  843. .name = "qcom_rpm_reg",
  844. .of_match_table = of_match_ptr(rpm_of_match),
  845. },
  846. };
  847. static int __init rpm_reg_init(void)
  848. {
  849. return platform_driver_register(&rpm_reg_driver);
  850. }
  851. subsys_initcall(rpm_reg_init);
  852. static void __exit rpm_reg_exit(void)
  853. {
  854. platform_driver_unregister(&rpm_reg_driver);
  855. }
  856. module_exit(rpm_reg_exit)
  857. MODULE_DESCRIPTION("Qualcomm RPM regulator driver");
  858. MODULE_LICENSE("GPL v2");