cpcap-regulator.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * Motorola CPCAP PMIC regulator driver
  3. *
  4. * Based on cpcap-regulator.c from Motorola Linux kernel tree
  5. * Copyright (C) 2009-2011 Motorola, Inc.
  6. *
  7. * Rewritten for mainline kernel to use device tree and regmap
  8. * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation version 2.
  13. *
  14. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  15. * kind, whether express or implied; without even the implied warranty
  16. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/err.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/of_platform.h>
  23. #include <linux/regmap.h>
  24. #include <linux/regulator/driver.h>
  25. #include <linux/regulator/machine.h>
  26. #include <linux/regulator/of_regulator.h>
  27. #include <linux/mfd/motorola-cpcap.h>
  28. /*
  29. * Resource assignment register bits. These seem to control the state
  30. * idle modes adn are used at least for omap4.
  31. */
  32. /* CPCAP_REG_ASSIGN2 bits - Resource Assignment 2 */
  33. #define CPCAP_BIT_VSDIO_SEL BIT(15)
  34. #define CPCAP_BIT_VDIG_SEL BIT(14)
  35. #define CPCAP_BIT_VCAM_SEL BIT(13)
  36. #define CPCAP_BIT_SW6_SEL BIT(12)
  37. #define CPCAP_BIT_SW5_SEL BIT(11)
  38. #define CPCAP_BIT_SW4_SEL BIT(10)
  39. #define CPCAP_BIT_SW3_SEL BIT(9)
  40. #define CPCAP_BIT_SW2_SEL BIT(8)
  41. #define CPCAP_BIT_SW1_SEL BIT(7)
  42. /* CPCAP_REG_ASSIGN3 bits - Resource Assignment 3 */
  43. #define CPCAP_BIT_VUSBINT2_SEL BIT(15)
  44. #define CPCAP_BIT_VUSBINT1_SEL BIT(14)
  45. #define CPCAP_BIT_VVIB_SEL BIT(13)
  46. #define CPCAP_BIT_VWLAN1_SEL BIT(12)
  47. #define CPCAP_BIT_VRF1_SEL BIT(11)
  48. #define CPCAP_BIT_VHVIO_SEL BIT(10)
  49. #define CPCAP_BIT_VDAC_SEL BIT(9)
  50. #define CPCAP_BIT_VUSB_SEL BIT(8)
  51. #define CPCAP_BIT_VSIM_SEL BIT(7)
  52. #define CPCAP_BIT_VRFREF_SEL BIT(6)
  53. #define CPCAP_BIT_VPLL_SEL BIT(5)
  54. #define CPCAP_BIT_VFUSE_SEL BIT(4)
  55. #define CPCAP_BIT_VCSI_SEL BIT(3)
  56. #define CPCAP_BIT_SPARE_14_2 BIT(2)
  57. #define CPCAP_BIT_VWLAN2_SEL BIT(1)
  58. #define CPCAP_BIT_VRF2_SEL BIT(0)
  59. /* CPCAP_REG_ASSIGN4 bits - Resource Assignment 4 */
  60. #define CPCAP_BIT_VAUDIO_SEL BIT(0)
  61. /*
  62. * Enable register bits. At least CPCAP_BIT_AUDIO_LOW_PWR is generic,
  63. * and not limited to audio regulator. Let's use the Motorola kernel
  64. * naming for now until we have a better understanding of the other
  65. * enable register bits. No idea why BIT(3) is not defined.
  66. */
  67. #define CPCAP_BIT_AUDIO_LOW_PWR BIT(6)
  68. #define CPCAP_BIT_AUD_LOWPWR_SPEED BIT(5)
  69. #define CPCAP_BIT_VAUDIOPRISTBY BIT(4)
  70. #define CPCAP_BIT_VAUDIO_MODE1 BIT(2)
  71. #define CPCAP_BIT_VAUDIO_MODE0 BIT(1)
  72. #define CPCAP_BIT_V_AUDIO_EN BIT(0)
  73. /*
  74. * Off mode configuration bit. Used currently only by SW5 on omap4. There's
  75. * the following comment in Motorola Linux kernel tree for it:
  76. *
  77. * When set in the regulator mode, the regulator assignment will be changed
  78. * to secondary when the regulator is disabled. The mode will be set back to
  79. * primary when the regulator is turned on.
  80. */
  81. #define CPCAP_REG_OFF_MODE_SEC BIT(15)
  82. /**
  83. * SoC specific configuraion for CPCAP regulator. There are at least three
  84. * different SoCs each with their own parameters: omap3, omap4 and tegra2.
  85. *
  86. * The assign_reg and assign_mask seem to allow toggling between primary
  87. * and secondary mode that at least omap4 uses for off mode.
  88. */
  89. struct cpcap_regulator {
  90. struct regulator_desc rdesc;
  91. const u16 assign_reg;
  92. const u16 assign_mask;
  93. const u16 vsel_shift;
  94. };
  95. #define CPCAP_REG(_ID, reg, assignment_reg, assignment_mask, val_tbl, \
  96. mode_mask, volt_mask, volt_shft, \
  97. mode_val, off_val, volt_trans_time) { \
  98. .rdesc = { \
  99. .name = #_ID, \
  100. .of_match = of_match_ptr(#_ID), \
  101. .ops = &cpcap_regulator_ops, \
  102. .regulators_node = of_match_ptr("regulators"), \
  103. .type = REGULATOR_VOLTAGE, \
  104. .id = CPCAP_##_ID, \
  105. .owner = THIS_MODULE, \
  106. .n_voltages = ARRAY_SIZE(val_tbl), \
  107. .volt_table = (val_tbl), \
  108. .vsel_reg = (reg), \
  109. .vsel_mask = (volt_mask), \
  110. .enable_reg = (reg), \
  111. .enable_mask = (mode_mask), \
  112. .enable_val = (mode_val), \
  113. .disable_val = (off_val), \
  114. .ramp_delay = (volt_trans_time), \
  115. }, \
  116. .assign_reg = (assignment_reg), \
  117. .assign_mask = (assignment_mask), \
  118. .vsel_shift = (volt_shft), \
  119. }
  120. struct cpcap_ddata {
  121. struct regmap *reg;
  122. struct device *dev;
  123. const struct cpcap_regulator *soc;
  124. };
  125. enum cpcap_regulator_id {
  126. CPCAP_SW1,
  127. CPCAP_SW2,
  128. CPCAP_SW3,
  129. CPCAP_SW4,
  130. CPCAP_SW5,
  131. CPCAP_SW6,
  132. CPCAP_VCAM,
  133. CPCAP_VCSI,
  134. CPCAP_VDAC,
  135. CPCAP_VDIG,
  136. CPCAP_VFUSE,
  137. CPCAP_VHVIO,
  138. CPCAP_VSDIO,
  139. CPCAP_VPLL,
  140. CPCAP_VRF1,
  141. CPCAP_VRF2,
  142. CPCAP_VRFREF,
  143. CPCAP_VWLAN1,
  144. CPCAP_VWLAN2,
  145. CPCAP_VSIM,
  146. CPCAP_VSIMCARD,
  147. CPCAP_VVIB,
  148. CPCAP_VUSB,
  149. CPCAP_VAUDIO,
  150. CPCAP_NR_REGULATORS,
  151. };
  152. /*
  153. * We need to also configure regulator idle mode for SoC off mode if
  154. * CPCAP_REG_OFF_MODE_SEC is set.
  155. */
  156. static int cpcap_regulator_enable(struct regulator_dev *rdev)
  157. {
  158. struct cpcap_regulator *regulator = rdev_get_drvdata(rdev);
  159. int error, ignore;
  160. error = regulator_enable_regmap(rdev);
  161. if (error)
  162. return error;
  163. if (rdev->desc->enable_val & CPCAP_REG_OFF_MODE_SEC) {
  164. error = regmap_update_bits(rdev->regmap, regulator->assign_reg,
  165. regulator->assign_mask,
  166. regulator->assign_mask);
  167. if (error)
  168. ignore = regulator_disable_regmap(rdev);
  169. }
  170. return error;
  171. }
  172. /*
  173. * We need to also configure regulator idle mode for SoC off mode if
  174. * CPCAP_REG_OFF_MODE_SEC is set.
  175. */
  176. static int cpcap_regulator_disable(struct regulator_dev *rdev)
  177. {
  178. struct cpcap_regulator *regulator = rdev_get_drvdata(rdev);
  179. int error, ignore;
  180. if (rdev->desc->enable_val & CPCAP_REG_OFF_MODE_SEC) {
  181. error = regmap_update_bits(rdev->regmap, regulator->assign_reg,
  182. regulator->assign_mask, 0);
  183. if (error)
  184. return error;
  185. }
  186. error = regulator_disable_regmap(rdev);
  187. if (error && (rdev->desc->enable_val & CPCAP_REG_OFF_MODE_SEC)) {
  188. ignore = regmap_update_bits(rdev->regmap, regulator->assign_reg,
  189. regulator->assign_mask,
  190. regulator->assign_mask);
  191. }
  192. return error;
  193. }
  194. static unsigned int cpcap_regulator_get_mode(struct regulator_dev *rdev)
  195. {
  196. int value;
  197. regmap_read(rdev->regmap, rdev->desc->enable_reg, &value);
  198. if (!(value & CPCAP_BIT_AUDIO_LOW_PWR))
  199. return REGULATOR_MODE_STANDBY;
  200. return REGULATOR_MODE_NORMAL;
  201. }
  202. static int cpcap_regulator_set_mode(struct regulator_dev *rdev,
  203. unsigned int mode)
  204. {
  205. int value;
  206. switch (mode) {
  207. case REGULATOR_MODE_NORMAL:
  208. value = CPCAP_BIT_AUDIO_LOW_PWR;
  209. break;
  210. case REGULATOR_MODE_STANDBY:
  211. value = 0;
  212. break;
  213. default:
  214. return -EINVAL;
  215. }
  216. return regmap_update_bits(rdev->regmap, rdev->desc->enable_reg,
  217. CPCAP_BIT_AUDIO_LOW_PWR, value);
  218. }
  219. static struct regulator_ops cpcap_regulator_ops = {
  220. .enable = cpcap_regulator_enable,
  221. .disable = cpcap_regulator_disable,
  222. .is_enabled = regulator_is_enabled_regmap,
  223. .list_voltage = regulator_list_voltage_table,
  224. .map_voltage = regulator_map_voltage_iterate,
  225. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  226. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  227. .get_mode = cpcap_regulator_get_mode,
  228. .set_mode = cpcap_regulator_set_mode,
  229. };
  230. static const unsigned int unknown_val_tbl[] = { 0, };
  231. static const unsigned int sw5_val_tbl[] = { 0, 5050000, };
  232. static const unsigned int vcam_val_tbl[] = { 2600000, 2700000, 2800000,
  233. 2900000, };
  234. static const unsigned int vcsi_val_tbl[] = { 1200000, 1800000, };
  235. static const unsigned int vdac_val_tbl[] = { 1200000, 1500000, 1800000,
  236. 2500000,};
  237. static const unsigned int vdig_val_tbl[] = { 1200000, 1350000, 1500000,
  238. 1875000, };
  239. static const unsigned int vfuse_val_tbl[] = { 1500000, 1600000, 1700000,
  240. 1800000, 1900000, 2000000,
  241. 2100000, 2200000, 2300000,
  242. 2400000, 2500000, 2600000,
  243. 2700000, 3150000, };
  244. static const unsigned int vhvio_val_tbl[] = { 2775000, };
  245. static const unsigned int vsdio_val_tbl[] = { 1500000, 1600000, 1800000,
  246. 2600000, 2700000, 2800000,
  247. 2900000, 3000000, };
  248. static const unsigned int vpll_val_tbl[] = { 1200000, 1300000, 1400000,
  249. 1800000, };
  250. /* Quirk: 2775000 is before 2500000 for vrf1 regulator */
  251. static const unsigned int vrf1_val_tbl[] = { 2775000, 2500000, };
  252. static const unsigned int vrf2_val_tbl[] = { 0, 2775000, };
  253. static const unsigned int vrfref_val_tbl[] = { 2500000, 2775000, };
  254. static const unsigned int vwlan1_val_tbl[] = { 1800000, 1900000, };
  255. static const unsigned int vwlan2_val_tbl[] = { 2775000, 3000000, 3300000,
  256. 3300000, };
  257. static const unsigned int vsim_val_tbl[] = { 1800000, 2900000, };
  258. static const unsigned int vsimcard_val_tbl[] = { 1800000, 2900000, };
  259. static const unsigned int vvib_val_tbl[] = { 1300000, 1800000, 2000000,
  260. 3000000, };
  261. static const unsigned int vusb_val_tbl[] = { 0, 3300000, };
  262. static const unsigned int vaudio_val_tbl[] = { 0, 2775000, };
  263. /**
  264. * SoC specific configuration for omap4. The data below is comes from Motorola
  265. * Linux kernel tree. It's basically the values of cpcap_regltr_data,
  266. * cpcap_regulator_mode_values and cpcap_regulator_off_mode_values, see
  267. * CPCAP_REG macro above.
  268. *
  269. * SW1 to SW4 and SW6 seems to be unused for mapphone. Note that VSIM and
  270. * VSIMCARD have a shared resource assignment bit.
  271. */
  272. static struct cpcap_regulator omap4_regulators[] = {
  273. CPCAP_REG(SW1, CPCAP_REG_S1C1, CPCAP_REG_ASSIGN2,
  274. CPCAP_BIT_SW1_SEL, unknown_val_tbl,
  275. 0, 0, 0, 0, 0, 0),
  276. CPCAP_REG(SW2, CPCAP_REG_S2C1, CPCAP_REG_ASSIGN2,
  277. CPCAP_BIT_SW2_SEL, unknown_val_tbl,
  278. 0, 0, 0, 0, 0, 0),
  279. CPCAP_REG(SW3, CPCAP_REG_S3C, CPCAP_REG_ASSIGN2,
  280. CPCAP_BIT_SW3_SEL, unknown_val_tbl,
  281. 0, 0, 0, 0, 0, 0),
  282. CPCAP_REG(SW4, CPCAP_REG_S4C1, CPCAP_REG_ASSIGN2,
  283. CPCAP_BIT_SW4_SEL, unknown_val_tbl,
  284. 0, 0, 0, 0, 0, 0),
  285. CPCAP_REG(SW5, CPCAP_REG_S5C, CPCAP_REG_ASSIGN2,
  286. CPCAP_BIT_SW5_SEL, sw5_val_tbl,
  287. 0x28, 0, 0, 0x20 | CPCAP_REG_OFF_MODE_SEC, 0, 0),
  288. CPCAP_REG(SW6, CPCAP_REG_S6C, CPCAP_REG_ASSIGN2,
  289. CPCAP_BIT_SW6_SEL, unknown_val_tbl,
  290. 0, 0, 0, 0, 0, 0),
  291. CPCAP_REG(VCAM, CPCAP_REG_VCAMC, CPCAP_REG_ASSIGN2,
  292. CPCAP_BIT_VCAM_SEL, vcam_val_tbl,
  293. 0x87, 0x30, 4, 0x3, 0, 420),
  294. CPCAP_REG(VCSI, CPCAP_REG_VCSIC, CPCAP_REG_ASSIGN3,
  295. CPCAP_BIT_VCSI_SEL, vcsi_val_tbl,
  296. 0x47, 0x10, 4, 0x43, 0x41, 350),
  297. CPCAP_REG(VDAC, CPCAP_REG_VDACC, CPCAP_REG_ASSIGN3,
  298. CPCAP_BIT_VDAC_SEL, vdac_val_tbl,
  299. 0x87, 0x30, 4, 0x3, 0, 420),
  300. CPCAP_REG(VDIG, CPCAP_REG_VDIGC, CPCAP_REG_ASSIGN2,
  301. CPCAP_BIT_VDIG_SEL, vdig_val_tbl,
  302. 0x87, 0x30, 4, 0x82, 0, 420),
  303. CPCAP_REG(VFUSE, CPCAP_REG_VFUSEC, CPCAP_REG_ASSIGN3,
  304. CPCAP_BIT_VFUSE_SEL, vfuse_val_tbl,
  305. 0x80, 0xf, 0, 0x80, 0, 420),
  306. CPCAP_REG(VHVIO, CPCAP_REG_VHVIOC, CPCAP_REG_ASSIGN3,
  307. CPCAP_BIT_VHVIO_SEL, vhvio_val_tbl,
  308. 0x17, 0, 0, 0, 0x12, 0),
  309. CPCAP_REG(VSDIO, CPCAP_REG_VSDIOC, CPCAP_REG_ASSIGN2,
  310. CPCAP_BIT_VSDIO_SEL, vsdio_val_tbl,
  311. 0x87, 0x38, 3, 0x82, 0, 420),
  312. CPCAP_REG(VPLL, CPCAP_REG_VPLLC, CPCAP_REG_ASSIGN3,
  313. CPCAP_BIT_VPLL_SEL, vpll_val_tbl,
  314. 0x43, 0x18, 3, 0x2, 0, 420),
  315. CPCAP_REG(VRF1, CPCAP_REG_VRF1C, CPCAP_REG_ASSIGN3,
  316. CPCAP_BIT_VRF1_SEL, vrf1_val_tbl,
  317. 0xac, 0x2, 1, 0x4, 0, 10),
  318. CPCAP_REG(VRF2, CPCAP_REG_VRF2C, CPCAP_REG_ASSIGN3,
  319. CPCAP_BIT_VRF2_SEL, vrf2_val_tbl,
  320. 0x23, 0x8, 3, 0, 0, 10),
  321. CPCAP_REG(VRFREF, CPCAP_REG_VRFREFC, CPCAP_REG_ASSIGN3,
  322. CPCAP_BIT_VRFREF_SEL, vrfref_val_tbl,
  323. 0x23, 0x8, 3, 0, 0, 420),
  324. CPCAP_REG(VWLAN1, CPCAP_REG_VWLAN1C, CPCAP_REG_ASSIGN3,
  325. CPCAP_BIT_VWLAN1_SEL, vwlan1_val_tbl,
  326. 0x47, 0x10, 4, 0, 0, 420),
  327. CPCAP_REG(VWLAN2, CPCAP_REG_VWLAN2C, CPCAP_REG_ASSIGN3,
  328. CPCAP_BIT_VWLAN2_SEL, vwlan2_val_tbl,
  329. 0x20c, 0xc0, 6, 0x20c, 0, 420),
  330. CPCAP_REG(VSIM, CPCAP_REG_VSIMC, CPCAP_REG_ASSIGN3,
  331. 0xffff, vsim_val_tbl,
  332. 0x23, 0x8, 3, 0x3, 0, 420),
  333. CPCAP_REG(VSIMCARD, CPCAP_REG_VSIMC, CPCAP_REG_ASSIGN3,
  334. 0xffff, vsimcard_val_tbl,
  335. 0x1e80, 0x8, 3, 0x1e00, 0, 420),
  336. CPCAP_REG(VVIB, CPCAP_REG_VVIBC, CPCAP_REG_ASSIGN3,
  337. CPCAP_BIT_VVIB_SEL, vvib_val_tbl,
  338. 0x1, 0xc, 2, 0x1, 0, 500),
  339. CPCAP_REG(VUSB, CPCAP_REG_VUSBC, CPCAP_REG_ASSIGN3,
  340. CPCAP_BIT_VUSB_SEL, vusb_val_tbl,
  341. 0x11c, 0x40, 6, 0xc, 0, 0),
  342. CPCAP_REG(VAUDIO, CPCAP_REG_VAUDIOC, CPCAP_REG_ASSIGN4,
  343. CPCAP_BIT_VAUDIO_SEL, vaudio_val_tbl,
  344. 0x16, 0x1, 0, 0x4, 0, 0),
  345. { /* sentinel */ },
  346. };
  347. static const struct of_device_id cpcap_regulator_id_table[] = {
  348. {
  349. .compatible = "motorola,cpcap-regulator",
  350. },
  351. {
  352. .compatible = "motorola,mapphone-cpcap-regulator",
  353. .data = omap4_regulators,
  354. },
  355. {},
  356. };
  357. MODULE_DEVICE_TABLE(of, cpcap_regulator_id_table);
  358. static int cpcap_regulator_probe(struct platform_device *pdev)
  359. {
  360. struct cpcap_ddata *ddata;
  361. const struct of_device_id *match;
  362. struct regulator_config config;
  363. struct regulator_init_data init_data;
  364. int i;
  365. match = of_match_device(of_match_ptr(cpcap_regulator_id_table),
  366. &pdev->dev);
  367. if (!match)
  368. return -EINVAL;
  369. if (!match->data) {
  370. dev_err(&pdev->dev, "no configuration data found\n");
  371. return -ENODEV;
  372. }
  373. ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
  374. if (!ddata)
  375. return -ENOMEM;
  376. ddata->reg = dev_get_regmap(pdev->dev.parent, NULL);
  377. if (!ddata->reg)
  378. return -ENODEV;
  379. ddata->dev = &pdev->dev;
  380. ddata->soc = match->data;
  381. platform_set_drvdata(pdev, ddata);
  382. memset(&config, 0, sizeof(config));
  383. memset(&init_data, 0, sizeof(init_data));
  384. config.dev = &pdev->dev;
  385. config.regmap = ddata->reg;
  386. config.init_data = &init_data;
  387. for (i = 0; i < CPCAP_NR_REGULATORS; i++) {
  388. const struct cpcap_regulator *regulator = &ddata->soc[i];
  389. struct regulator_dev *rdev;
  390. if (!regulator->rdesc.name)
  391. break;
  392. if (regulator->rdesc.volt_table == unknown_val_tbl)
  393. continue;
  394. config.driver_data = (void *)regulator;
  395. rdev = devm_regulator_register(&pdev->dev,
  396. &regulator->rdesc,
  397. &config);
  398. if (IS_ERR(rdev)) {
  399. dev_err(&pdev->dev, "failed to register regulator %s\n",
  400. regulator->rdesc.name);
  401. return PTR_ERR(rdev);
  402. }
  403. }
  404. return 0;
  405. }
  406. static struct platform_driver cpcap_regulator_driver = {
  407. .probe = cpcap_regulator_probe,
  408. .driver = {
  409. .name = "cpcap-regulator",
  410. .of_match_table = of_match_ptr(cpcap_regulator_id_table),
  411. },
  412. };
  413. module_platform_driver(cpcap_regulator_driver);
  414. MODULE_ALIAS("platform:cpcap-regulator");
  415. MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
  416. MODULE_DESCRIPTION("CPCAP regulator driver");
  417. MODULE_LICENSE("GPL v2");