qcom_rpm-regulator.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. * Copyright (c) 2014, Sony Mobile Communications AB.
  3. * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 and
  7. * only version 2 as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/of.h>
  17. #include <linux/of_device.h>
  18. #include <linux/regulator/driver.h>
  19. #include <linux/regulator/machine.h>
  20. #include <linux/regulator/of_regulator.h>
  21. #include <linux/mfd/qcom_rpm.h>
  22. #include <dt-bindings/mfd/qcom-rpm.h>
  23. #define MAX_REQUEST_LEN 2
  24. struct request_member {
  25. int word;
  26. unsigned int mask;
  27. int shift;
  28. };
  29. struct rpm_reg_parts {
  30. struct request_member mV; /* used if voltage is in mV */
  31. struct request_member uV; /* used if voltage is in uV */
  32. struct request_member ip; /* peak current in mA */
  33. struct request_member pd; /* pull down enable */
  34. struct request_member ia; /* average current in mA */
  35. struct request_member fm; /* force mode */
  36. struct request_member pm; /* power mode */
  37. struct request_member pc; /* pin control */
  38. struct request_member pf; /* pin function */
  39. struct request_member enable_state; /* NCP and switch */
  40. struct request_member comp_mode; /* NCP */
  41. struct request_member freq; /* frequency: NCP and SMPS */
  42. struct request_member freq_clk_src; /* clock source: SMPS */
  43. struct request_member hpm; /* switch: control OCP and SS */
  44. int request_len;
  45. };
  46. #define FORCE_MODE_IS_2_BITS(reg) \
  47. (((reg)->parts->fm.mask >> (reg)->parts->fm.shift) == 3)
  48. struct qcom_rpm_reg {
  49. struct qcom_rpm *rpm;
  50. struct mutex lock;
  51. struct device *dev;
  52. struct regulator_desc desc;
  53. const struct rpm_reg_parts *parts;
  54. int resource;
  55. u32 val[MAX_REQUEST_LEN];
  56. int uV;
  57. int is_enabled;
  58. bool supports_force_mode_auto;
  59. bool supports_force_mode_bypass;
  60. };
  61. static const struct rpm_reg_parts rpm8660_ldo_parts = {
  62. .request_len = 2,
  63. .mV = { 0, 0x00000FFF, 0 },
  64. .ip = { 0, 0x00FFF000, 12 },
  65. .fm = { 0, 0x03000000, 24 },
  66. .pc = { 0, 0x3C000000, 26 },
  67. .pf = { 0, 0xC0000000, 30 },
  68. .pd = { 1, 0x00000001, 0 },
  69. .ia = { 1, 0x00001FFE, 1 },
  70. };
  71. static const struct rpm_reg_parts rpm8660_smps_parts = {
  72. .request_len = 2,
  73. .mV = { 0, 0x00000FFF, 0 },
  74. .ip = { 0, 0x00FFF000, 12 },
  75. .fm = { 0, 0x03000000, 24 },
  76. .pc = { 0, 0x3C000000, 26 },
  77. .pf = { 0, 0xC0000000, 30 },
  78. .pd = { 1, 0x00000001, 0 },
  79. .ia = { 1, 0x00001FFE, 1 },
  80. .freq = { 1, 0x001FE000, 13 },
  81. .freq_clk_src = { 1, 0x00600000, 21 },
  82. };
  83. static const struct rpm_reg_parts rpm8660_switch_parts = {
  84. .request_len = 1,
  85. .enable_state = { 0, 0x00000001, 0 },
  86. .pd = { 0, 0x00000002, 1 },
  87. .pc = { 0, 0x0000003C, 2 },
  88. .pf = { 0, 0x000000C0, 6 },
  89. .hpm = { 0, 0x00000300, 8 },
  90. };
  91. static const struct rpm_reg_parts rpm8660_ncp_parts = {
  92. .request_len = 1,
  93. .mV = { 0, 0x00000FFF, 0 },
  94. .enable_state = { 0, 0x00001000, 12 },
  95. .comp_mode = { 0, 0x00002000, 13 },
  96. .freq = { 0, 0x003FC000, 14 },
  97. };
  98. static const struct rpm_reg_parts rpm8960_ldo_parts = {
  99. .request_len = 2,
  100. .uV = { 0, 0x007FFFFF, 0 },
  101. .pd = { 0, 0x00800000, 23 },
  102. .pc = { 0, 0x0F000000, 24 },
  103. .pf = { 0, 0xF0000000, 28 },
  104. .ip = { 1, 0x000003FF, 0 },
  105. .ia = { 1, 0x000FFC00, 10 },
  106. .fm = { 1, 0x00700000, 20 },
  107. };
  108. static const struct rpm_reg_parts rpm8960_smps_parts = {
  109. .request_len = 2,
  110. .uV = { 0, 0x007FFFFF, 0 },
  111. .pd = { 0, 0x00800000, 23 },
  112. .pc = { 0, 0x0F000000, 24 },
  113. .pf = { 0, 0xF0000000, 28 },
  114. .ip = { 1, 0x000003FF, 0 },
  115. .ia = { 1, 0x000FFC00, 10 },
  116. .fm = { 1, 0x00700000, 20 },
  117. .pm = { 1, 0x00800000, 23 },
  118. .freq = { 1, 0x1F000000, 24 },
  119. .freq_clk_src = { 1, 0x60000000, 29 },
  120. };
  121. static const struct rpm_reg_parts rpm8960_switch_parts = {
  122. .request_len = 1,
  123. .enable_state = { 0, 0x00000001, 0 },
  124. .pd = { 0, 0x00000002, 1 },
  125. .pc = { 0, 0x0000003C, 2 },
  126. .pf = { 0, 0x000003C0, 6 },
  127. .hpm = { 0, 0x00000C00, 10 },
  128. };
  129. static const struct rpm_reg_parts rpm8960_ncp_parts = {
  130. .request_len = 1,
  131. .uV = { 0, 0x007FFFFF, 0 },
  132. .enable_state = { 0, 0x00800000, 23 },
  133. .comp_mode = { 0, 0x01000000, 24 },
  134. .freq = { 0, 0x3E000000, 25 },
  135. };
  136. /*
  137. * Physically available PMIC regulator voltage ranges
  138. */
  139. static const struct regulator_linear_range pldo_ranges[] = {
  140. REGULATOR_LINEAR_RANGE( 750000, 0, 59, 12500),
  141. REGULATOR_LINEAR_RANGE(1500000, 60, 123, 25000),
  142. REGULATOR_LINEAR_RANGE(3100000, 124, 160, 50000),
  143. };
  144. static const struct regulator_linear_range nldo_ranges[] = {
  145. REGULATOR_LINEAR_RANGE( 750000, 0, 63, 12500),
  146. };
  147. static const struct regulator_linear_range nldo1200_ranges[] = {
  148. REGULATOR_LINEAR_RANGE( 375000, 0, 59, 6250),
  149. REGULATOR_LINEAR_RANGE( 750000, 60, 123, 12500),
  150. };
  151. static const struct regulator_linear_range smps_ranges[] = {
  152. REGULATOR_LINEAR_RANGE( 375000, 0, 29, 12500),
  153. REGULATOR_LINEAR_RANGE( 750000, 30, 89, 12500),
  154. REGULATOR_LINEAR_RANGE(1500000, 90, 153, 25000),
  155. };
  156. static const struct regulator_linear_range ftsmps_ranges[] = {
  157. REGULATOR_LINEAR_RANGE( 350000, 0, 6, 50000),
  158. REGULATOR_LINEAR_RANGE( 700000, 7, 63, 12500),
  159. REGULATOR_LINEAR_RANGE(1500000, 64, 100, 50000),
  160. };
  161. static const struct regulator_linear_range smb208_ranges[] = {
  162. REGULATOR_LINEAR_RANGE( 375000, 0, 29, 12500),
  163. REGULATOR_LINEAR_RANGE( 750000, 30, 89, 12500),
  164. REGULATOR_LINEAR_RANGE(1500000, 90, 153, 25000),
  165. REGULATOR_LINEAR_RANGE(3100000, 154, 234, 25000),
  166. };
  167. static const struct regulator_linear_range ncp_ranges[] = {
  168. REGULATOR_LINEAR_RANGE(1500000, 0, 31, 50000),
  169. };
  170. static int rpm_reg_write(struct qcom_rpm_reg *vreg,
  171. const struct request_member *req,
  172. const int value)
  173. {
  174. if (WARN_ON((value << req->shift) & ~req->mask))
  175. return -EINVAL;
  176. vreg->val[req->word] &= ~req->mask;
  177. vreg->val[req->word] |= value << req->shift;
  178. return qcom_rpm_write(vreg->rpm,
  179. vreg->resource,
  180. vreg->val,
  181. vreg->parts->request_len);
  182. }
  183. static int rpm_reg_set_mV_sel(struct regulator_dev *rdev,
  184. unsigned selector)
  185. {
  186. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  187. const struct rpm_reg_parts *parts = vreg->parts;
  188. const struct request_member *req = &parts->mV;
  189. int ret = 0;
  190. int uV;
  191. if (req->mask == 0)
  192. return -EINVAL;
  193. uV = regulator_list_voltage_linear_range(rdev, selector);
  194. if (uV < 0)
  195. return uV;
  196. mutex_lock(&vreg->lock);
  197. vreg->uV = uV;
  198. if (vreg->is_enabled)
  199. ret = rpm_reg_write(vreg, req, vreg->uV / 1000);
  200. mutex_unlock(&vreg->lock);
  201. return ret;
  202. }
  203. static int rpm_reg_set_uV_sel(struct regulator_dev *rdev,
  204. unsigned selector)
  205. {
  206. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  207. const struct rpm_reg_parts *parts = vreg->parts;
  208. const struct request_member *req = &parts->uV;
  209. int ret = 0;
  210. int uV;
  211. if (req->mask == 0)
  212. return -EINVAL;
  213. uV = regulator_list_voltage_linear_range(rdev, selector);
  214. if (uV < 0)
  215. return uV;
  216. mutex_lock(&vreg->lock);
  217. vreg->uV = uV;
  218. if (vreg->is_enabled)
  219. ret = rpm_reg_write(vreg, req, vreg->uV);
  220. mutex_unlock(&vreg->lock);
  221. return ret;
  222. }
  223. static int rpm_reg_get_voltage(struct regulator_dev *rdev)
  224. {
  225. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  226. return vreg->uV;
  227. }
  228. static int rpm_reg_mV_enable(struct regulator_dev *rdev)
  229. {
  230. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  231. const struct rpm_reg_parts *parts = vreg->parts;
  232. const struct request_member *req = &parts->mV;
  233. int ret;
  234. if (req->mask == 0)
  235. return -EINVAL;
  236. mutex_lock(&vreg->lock);
  237. ret = rpm_reg_write(vreg, req, vreg->uV / 1000);
  238. if (!ret)
  239. vreg->is_enabled = 1;
  240. mutex_unlock(&vreg->lock);
  241. return ret;
  242. }
  243. static int rpm_reg_uV_enable(struct regulator_dev *rdev)
  244. {
  245. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  246. const struct rpm_reg_parts *parts = vreg->parts;
  247. const struct request_member *req = &parts->uV;
  248. int ret;
  249. if (req->mask == 0)
  250. return -EINVAL;
  251. mutex_lock(&vreg->lock);
  252. ret = rpm_reg_write(vreg, req, vreg->uV);
  253. if (!ret)
  254. vreg->is_enabled = 1;
  255. mutex_unlock(&vreg->lock);
  256. return ret;
  257. }
  258. static int rpm_reg_switch_enable(struct regulator_dev *rdev)
  259. {
  260. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  261. const struct rpm_reg_parts *parts = vreg->parts;
  262. const struct request_member *req = &parts->enable_state;
  263. int ret;
  264. if (req->mask == 0)
  265. return -EINVAL;
  266. mutex_lock(&vreg->lock);
  267. ret = rpm_reg_write(vreg, req, 1);
  268. if (!ret)
  269. vreg->is_enabled = 1;
  270. mutex_unlock(&vreg->lock);
  271. return ret;
  272. }
  273. static int rpm_reg_mV_disable(struct regulator_dev *rdev)
  274. {
  275. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  276. const struct rpm_reg_parts *parts = vreg->parts;
  277. const struct request_member *req = &parts->mV;
  278. int ret;
  279. if (req->mask == 0)
  280. return -EINVAL;
  281. mutex_lock(&vreg->lock);
  282. ret = rpm_reg_write(vreg, req, 0);
  283. if (!ret)
  284. vreg->is_enabled = 0;
  285. mutex_unlock(&vreg->lock);
  286. return ret;
  287. }
  288. static int rpm_reg_uV_disable(struct regulator_dev *rdev)
  289. {
  290. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  291. const struct rpm_reg_parts *parts = vreg->parts;
  292. const struct request_member *req = &parts->uV;
  293. int ret;
  294. if (req->mask == 0)
  295. return -EINVAL;
  296. mutex_lock(&vreg->lock);
  297. ret = rpm_reg_write(vreg, req, 0);
  298. if (!ret)
  299. vreg->is_enabled = 0;
  300. mutex_unlock(&vreg->lock);
  301. return ret;
  302. }
  303. static int rpm_reg_switch_disable(struct regulator_dev *rdev)
  304. {
  305. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  306. const struct rpm_reg_parts *parts = vreg->parts;
  307. const struct request_member *req = &parts->enable_state;
  308. int ret;
  309. if (req->mask == 0)
  310. return -EINVAL;
  311. mutex_lock(&vreg->lock);
  312. ret = rpm_reg_write(vreg, req, 0);
  313. if (!ret)
  314. vreg->is_enabled = 0;
  315. mutex_unlock(&vreg->lock);
  316. return ret;
  317. }
  318. static int rpm_reg_is_enabled(struct regulator_dev *rdev)
  319. {
  320. struct qcom_rpm_reg *vreg = rdev_get_drvdata(rdev);
  321. return vreg->is_enabled;
  322. }
  323. static struct regulator_ops uV_ops = {
  324. .list_voltage = regulator_list_voltage_linear_range,
  325. .set_voltage_sel = rpm_reg_set_uV_sel,
  326. .get_voltage = rpm_reg_get_voltage,
  327. .enable = rpm_reg_uV_enable,
  328. .disable = rpm_reg_uV_disable,
  329. .is_enabled = rpm_reg_is_enabled,
  330. };
  331. static struct regulator_ops mV_ops = {
  332. .list_voltage = regulator_list_voltage_linear_range,
  333. .set_voltage_sel = rpm_reg_set_mV_sel,
  334. .get_voltage = rpm_reg_get_voltage,
  335. .enable = rpm_reg_mV_enable,
  336. .disable = rpm_reg_mV_disable,
  337. .is_enabled = rpm_reg_is_enabled,
  338. };
  339. static struct regulator_ops switch_ops = {
  340. .enable = rpm_reg_switch_enable,
  341. .disable = rpm_reg_switch_disable,
  342. .is_enabled = rpm_reg_is_enabled,
  343. };
  344. /*
  345. * PM8058 regulators
  346. */
  347. static const struct qcom_rpm_reg pm8058_pldo = {
  348. .desc.linear_ranges = pldo_ranges,
  349. .desc.n_linear_ranges = ARRAY_SIZE(pldo_ranges),
  350. .desc.n_voltages = 161,
  351. .desc.ops = &mV_ops,
  352. .parts = &rpm8660_ldo_parts,
  353. .supports_force_mode_auto = false,
  354. .supports_force_mode_bypass = false,
  355. };
  356. static const struct qcom_rpm_reg pm8058_nldo = {
  357. .desc.linear_ranges = nldo_ranges,
  358. .desc.n_linear_ranges = ARRAY_SIZE(nldo_ranges),
  359. .desc.n_voltages = 64,
  360. .desc.ops = &mV_ops,
  361. .parts = &rpm8660_ldo_parts,
  362. .supports_force_mode_auto = false,
  363. .supports_force_mode_bypass = false,
  364. };
  365. static const struct qcom_rpm_reg pm8058_smps = {
  366. .desc.linear_ranges = smps_ranges,
  367. .desc.n_linear_ranges = ARRAY_SIZE(smps_ranges),
  368. .desc.n_voltages = 154,
  369. .desc.ops = &mV_ops,
  370. .parts = &rpm8660_smps_parts,
  371. .supports_force_mode_auto = false,
  372. .supports_force_mode_bypass = false,
  373. };
  374. static const struct qcom_rpm_reg pm8058_ncp = {
  375. .desc.linear_ranges = ncp_ranges,
  376. .desc.n_linear_ranges = ARRAY_SIZE(ncp_ranges),
  377. .desc.n_voltages = 32,
  378. .desc.ops = &mV_ops,
  379. .parts = &rpm8660_ncp_parts,
  380. };
  381. static const struct qcom_rpm_reg pm8058_switch = {
  382. .desc.ops = &switch_ops,
  383. .parts = &rpm8660_switch_parts,
  384. };
  385. /*
  386. * PM8901 regulators
  387. */
  388. static const struct qcom_rpm_reg pm8901_pldo = {
  389. .desc.linear_ranges = pldo_ranges,
  390. .desc.n_linear_ranges = ARRAY_SIZE(pldo_ranges),
  391. .desc.n_voltages = 161,
  392. .desc.ops = &mV_ops,
  393. .parts = &rpm8660_ldo_parts,
  394. .supports_force_mode_auto = false,
  395. .supports_force_mode_bypass = true,
  396. };
  397. static const struct qcom_rpm_reg pm8901_nldo = {
  398. .desc.linear_ranges = nldo_ranges,
  399. .desc.n_linear_ranges = ARRAY_SIZE(nldo_ranges),
  400. .desc.n_voltages = 64,
  401. .desc.ops = &mV_ops,
  402. .parts = &rpm8660_ldo_parts,
  403. .supports_force_mode_auto = false,
  404. .supports_force_mode_bypass = true,
  405. };
  406. static const struct qcom_rpm_reg pm8901_ftsmps = {
  407. .desc.linear_ranges = ftsmps_ranges,
  408. .desc.n_linear_ranges = ARRAY_SIZE(ftsmps_ranges),
  409. .desc.n_voltages = 101,
  410. .desc.ops = &mV_ops,
  411. .parts = &rpm8660_smps_parts,
  412. .supports_force_mode_auto = true,
  413. .supports_force_mode_bypass = false,
  414. };
  415. static const struct qcom_rpm_reg pm8901_switch = {
  416. .desc.ops = &switch_ops,
  417. .parts = &rpm8660_switch_parts,
  418. };
  419. /*
  420. * PM8921 regulators
  421. */
  422. static const struct qcom_rpm_reg pm8921_pldo = {
  423. .desc.linear_ranges = pldo_ranges,
  424. .desc.n_linear_ranges = ARRAY_SIZE(pldo_ranges),
  425. .desc.n_voltages = 161,
  426. .desc.ops = &uV_ops,
  427. .parts = &rpm8960_ldo_parts,
  428. .supports_force_mode_auto = false,
  429. .supports_force_mode_bypass = true,
  430. };
  431. static const struct qcom_rpm_reg pm8921_nldo = {
  432. .desc.linear_ranges = nldo_ranges,
  433. .desc.n_linear_ranges = ARRAY_SIZE(nldo_ranges),
  434. .desc.n_voltages = 64,
  435. .desc.ops = &uV_ops,
  436. .parts = &rpm8960_ldo_parts,
  437. .supports_force_mode_auto = false,
  438. .supports_force_mode_bypass = true,
  439. };
  440. static const struct qcom_rpm_reg pm8921_nldo1200 = {
  441. .desc.linear_ranges = nldo1200_ranges,
  442. .desc.n_linear_ranges = ARRAY_SIZE(nldo1200_ranges),
  443. .desc.n_voltages = 124,
  444. .desc.ops = &uV_ops,
  445. .parts = &rpm8960_ldo_parts,
  446. .supports_force_mode_auto = false,
  447. .supports_force_mode_bypass = true,
  448. };
  449. static const struct qcom_rpm_reg pm8921_smps = {
  450. .desc.linear_ranges = smps_ranges,
  451. .desc.n_linear_ranges = ARRAY_SIZE(smps_ranges),
  452. .desc.n_voltages = 154,
  453. .desc.ops = &uV_ops,
  454. .parts = &rpm8960_smps_parts,
  455. .supports_force_mode_auto = true,
  456. .supports_force_mode_bypass = false,
  457. };
  458. static const struct qcom_rpm_reg pm8921_ftsmps = {
  459. .desc.linear_ranges = ftsmps_ranges,
  460. .desc.n_linear_ranges = ARRAY_SIZE(ftsmps_ranges),
  461. .desc.n_voltages = 101,
  462. .desc.ops = &uV_ops,
  463. .parts = &rpm8960_smps_parts,
  464. .supports_force_mode_auto = true,
  465. .supports_force_mode_bypass = false,
  466. };
  467. static const struct qcom_rpm_reg pm8921_ncp = {
  468. .desc.linear_ranges = ncp_ranges,
  469. .desc.n_linear_ranges = ARRAY_SIZE(ncp_ranges),
  470. .desc.n_voltages = 32,
  471. .desc.ops = &uV_ops,
  472. .parts = &rpm8960_ncp_parts,
  473. };
  474. static const struct qcom_rpm_reg pm8921_switch = {
  475. .desc.ops = &switch_ops,
  476. .parts = &rpm8960_switch_parts,
  477. };
  478. static const struct qcom_rpm_reg smb208_smps = {
  479. .desc.linear_ranges = smb208_ranges,
  480. .desc.n_linear_ranges = ARRAY_SIZE(smb208_ranges),
  481. .desc.n_voltages = 235,
  482. .desc.ops = &uV_ops,
  483. .parts = &rpm8960_smps_parts,
  484. .supports_force_mode_auto = false,
  485. .supports_force_mode_bypass = false,
  486. };
  487. static const struct of_device_id rpm_of_match[] = {
  488. { .compatible = "qcom,rpm-pm8058-pldo", .data = &pm8058_pldo },
  489. { .compatible = "qcom,rpm-pm8058-nldo", .data = &pm8058_nldo },
  490. { .compatible = "qcom,rpm-pm8058-smps", .data = &pm8058_smps },
  491. { .compatible = "qcom,rpm-pm8058-ncp", .data = &pm8058_ncp },
  492. { .compatible = "qcom,rpm-pm8058-switch", .data = &pm8058_switch },
  493. { .compatible = "qcom,rpm-pm8901-pldo", .data = &pm8901_pldo },
  494. { .compatible = "qcom,rpm-pm8901-nldo", .data = &pm8901_nldo },
  495. { .compatible = "qcom,rpm-pm8901-ftsmps", .data = &pm8901_ftsmps },
  496. { .compatible = "qcom,rpm-pm8901-switch", .data = &pm8901_switch },
  497. { .compatible = "qcom,rpm-pm8921-pldo", .data = &pm8921_pldo },
  498. { .compatible = "qcom,rpm-pm8921-nldo", .data = &pm8921_nldo },
  499. { .compatible = "qcom,rpm-pm8921-nldo1200", .data = &pm8921_nldo1200 },
  500. { .compatible = "qcom,rpm-pm8921-smps", .data = &pm8921_smps },
  501. { .compatible = "qcom,rpm-pm8921-ftsmps", .data = &pm8921_ftsmps },
  502. { .compatible = "qcom,rpm-pm8921-ncp", .data = &pm8921_ncp },
  503. { .compatible = "qcom,rpm-pm8921-switch", .data = &pm8921_switch },
  504. { .compatible = "qcom,rpm-smb208", .data = &smb208_smps },
  505. { }
  506. };
  507. MODULE_DEVICE_TABLE(of, rpm_of_match);
  508. static int rpm_reg_set(struct qcom_rpm_reg *vreg,
  509. const struct request_member *req,
  510. const int value)
  511. {
  512. if (req->mask == 0 || (value << req->shift) & ~req->mask)
  513. return -EINVAL;
  514. vreg->val[req->word] &= ~req->mask;
  515. vreg->val[req->word] |= value << req->shift;
  516. return 0;
  517. }
  518. static int rpm_reg_of_parse_freq(struct device *dev, struct qcom_rpm_reg *vreg)
  519. {
  520. static const int freq_table[] = {
  521. 19200000, 9600000, 6400000, 4800000, 3840000, 3200000, 2740000,
  522. 2400000, 2130000, 1920000, 1750000, 1600000, 1480000, 1370000,
  523. 1280000, 1200000,
  524. };
  525. const char *key;
  526. u32 freq;
  527. int ret;
  528. int i;
  529. key = "qcom,switch-mode-frequency";
  530. ret = of_property_read_u32(dev->of_node, key, &freq);
  531. if (ret) {
  532. dev_err(dev, "regulator requires %s property\n", key);
  533. return -EINVAL;
  534. }
  535. for (i = 0; i < ARRAY_SIZE(freq_table); i++) {
  536. if (freq == freq_table[i]) {
  537. rpm_reg_set(vreg, &vreg->parts->freq, i + 1);
  538. return 0;
  539. }
  540. }
  541. dev_err(dev, "invalid frequency %d\n", freq);
  542. return -EINVAL;
  543. }
  544. static int rpm_reg_probe(struct platform_device *pdev)
  545. {
  546. struct regulator_init_data *initdata;
  547. const struct qcom_rpm_reg *template;
  548. const struct of_device_id *match;
  549. struct regulator_config config = { };
  550. struct regulator_dev *rdev;
  551. struct qcom_rpm_reg *vreg;
  552. const char *key;
  553. u32 force_mode;
  554. bool pwm;
  555. u32 val;
  556. int ret;
  557. match = of_match_device(rpm_of_match, &pdev->dev);
  558. template = match->data;
  559. vreg = devm_kmalloc(&pdev->dev, sizeof(*vreg), GFP_KERNEL);
  560. if (!vreg) {
  561. dev_err(&pdev->dev, "failed to allocate vreg\n");
  562. return -ENOMEM;
  563. }
  564. memcpy(vreg, template, sizeof(*vreg));
  565. mutex_init(&vreg->lock);
  566. vreg->dev = &pdev->dev;
  567. vreg->desc.id = -1;
  568. vreg->desc.owner = THIS_MODULE;
  569. vreg->desc.type = REGULATOR_VOLTAGE;
  570. vreg->desc.name = pdev->dev.of_node->name;
  571. vreg->rpm = dev_get_drvdata(pdev->dev.parent);
  572. if (!vreg->rpm) {
  573. dev_err(&pdev->dev, "unable to retrieve handle to rpm\n");
  574. return -ENODEV;
  575. }
  576. initdata = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
  577. &vreg->desc);
  578. if (!initdata)
  579. return -EINVAL;
  580. key = "reg";
  581. ret = of_property_read_u32(pdev->dev.of_node, key, &val);
  582. if (ret) {
  583. dev_err(&pdev->dev, "failed to read %s\n", key);
  584. return ret;
  585. }
  586. vreg->resource = val;
  587. if ((vreg->parts->uV.mask || vreg->parts->mV.mask) &&
  588. (!initdata->constraints.min_uV || !initdata->constraints.max_uV)) {
  589. dev_err(&pdev->dev, "no voltage specified for regulator\n");
  590. return -EINVAL;
  591. }
  592. key = "bias-pull-down";
  593. if (of_property_read_bool(pdev->dev.of_node, key)) {
  594. ret = rpm_reg_set(vreg, &vreg->parts->pd, 1);
  595. if (ret) {
  596. dev_err(&pdev->dev, "%s is invalid", key);
  597. return ret;
  598. }
  599. }
  600. if (vreg->parts->freq.mask) {
  601. ret = rpm_reg_of_parse_freq(&pdev->dev, vreg);
  602. if (ret < 0)
  603. return ret;
  604. }
  605. if (vreg->parts->pm.mask) {
  606. key = "qcom,power-mode-hysteretic";
  607. pwm = !of_property_read_bool(pdev->dev.of_node, key);
  608. ret = rpm_reg_set(vreg, &vreg->parts->pm, pwm);
  609. if (ret) {
  610. dev_err(&pdev->dev, "failed to set power mode\n");
  611. return ret;
  612. }
  613. }
  614. if (vreg->parts->fm.mask) {
  615. force_mode = -1;
  616. key = "qcom,force-mode";
  617. ret = of_property_read_u32(pdev->dev.of_node, key, &val);
  618. if (ret == -EINVAL) {
  619. val = QCOM_RPM_FORCE_MODE_NONE;
  620. } else if (ret < 0) {
  621. dev_err(&pdev->dev, "failed to read %s\n", key);
  622. return ret;
  623. }
  624. /*
  625. * If force-mode is encoded as 2 bits then the
  626. * possible register values are:
  627. * NONE, LPM, HPM
  628. * otherwise:
  629. * NONE, LPM, AUTO, HPM, BYPASS
  630. */
  631. switch (val) {
  632. case QCOM_RPM_FORCE_MODE_NONE:
  633. force_mode = 0;
  634. break;
  635. case QCOM_RPM_FORCE_MODE_LPM:
  636. force_mode = 1;
  637. break;
  638. case QCOM_RPM_FORCE_MODE_HPM:
  639. if (FORCE_MODE_IS_2_BITS(vreg))
  640. force_mode = 2;
  641. else
  642. force_mode = 3;
  643. break;
  644. case QCOM_RPM_FORCE_MODE_AUTO:
  645. if (vreg->supports_force_mode_auto)
  646. force_mode = 2;
  647. break;
  648. case QCOM_RPM_FORCE_MODE_BYPASS:
  649. if (vreg->supports_force_mode_bypass)
  650. force_mode = 4;
  651. break;
  652. }
  653. if (force_mode < 0) {
  654. dev_err(&pdev->dev, "invalid force mode\n");
  655. return -EINVAL;
  656. }
  657. ret = rpm_reg_set(vreg, &vreg->parts->fm, force_mode);
  658. if (ret) {
  659. dev_err(&pdev->dev, "failed to set force mode\n");
  660. return ret;
  661. }
  662. }
  663. config.dev = &pdev->dev;
  664. config.init_data = initdata;
  665. config.driver_data = vreg;
  666. config.of_node = pdev->dev.of_node;
  667. rdev = devm_regulator_register(&pdev->dev, &vreg->desc, &config);
  668. if (IS_ERR(rdev)) {
  669. dev_err(&pdev->dev, "can't register regulator\n");
  670. return PTR_ERR(rdev);
  671. }
  672. return 0;
  673. }
  674. static struct platform_driver rpm_reg_driver = {
  675. .probe = rpm_reg_probe,
  676. .driver = {
  677. .name = "qcom_rpm_reg",
  678. .of_match_table = of_match_ptr(rpm_of_match),
  679. },
  680. };
  681. static int __init rpm_reg_init(void)
  682. {
  683. return platform_driver_register(&rpm_reg_driver);
  684. }
  685. subsys_initcall(rpm_reg_init);
  686. static void __exit rpm_reg_exit(void)
  687. {
  688. platform_driver_unregister(&rpm_reg_driver);
  689. }
  690. module_exit(rpm_reg_exit)
  691. MODULE_DESCRIPTION("Qualcomm RPM regulator driver");
  692. MODULE_LICENSE("GPL v2");