twl4030-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * twl4030-regulator.c -- support regulators in twl4030 family chips
  3. *
  4. * Copyright (C) 2008 David Brownell
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/err.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/regulator/driver.h>
  16. #include <linux/regulator/machine.h>
  17. #include <linux/i2c/twl4030.h>
  18. /*
  19. * The TWL4030/TW5030/TPS659x0 family chips include power management, a
  20. * USB OTG transceiver, an RTC, ADC, PWM, and lots more. Some versions
  21. * include an audio codec, battery charger, and more voltage regulators.
  22. * These chips are often used in OMAP-based systems.
  23. *
  24. * This driver implements software-based resource control for various
  25. * voltage regulators. This is usually augmented with state machine
  26. * based control.
  27. */
  28. struct twlreg_info {
  29. /* start of regulator's PM_RECEIVER control register bank */
  30. u8 base;
  31. /* twl4030 resource ID, for resource control state machine */
  32. u8 id;
  33. /* voltage in mV = table[VSEL]; table_len must be a power-of-two */
  34. u8 table_len;
  35. const u16 *table;
  36. /* chip constraints on regulator behavior */
  37. u16 min_mV;
  38. /* used by regulator core */
  39. struct regulator_desc desc;
  40. };
  41. /* LDO control registers ... offset is from the base of its register bank.
  42. * The first three registers of all power resource banks help hardware to
  43. * manage the various resource groups.
  44. */
  45. #define VREG_GRP 0
  46. #define VREG_TYPE 1
  47. #define VREG_REMAP 2
  48. #define VREG_DEDICATED 3 /* LDO control */
  49. static inline int
  50. twl4030reg_read(struct twlreg_info *info, unsigned offset)
  51. {
  52. u8 value;
  53. int status;
  54. status = twl4030_i2c_read_u8(TWL4030_MODULE_PM_RECEIVER,
  55. &value, info->base + offset);
  56. return (status < 0) ? status : value;
  57. }
  58. static inline int
  59. twl4030reg_write(struct twlreg_info *info, unsigned offset, u8 value)
  60. {
  61. return twl4030_i2c_write_u8(TWL4030_MODULE_PM_RECEIVER,
  62. value, info->base + offset);
  63. }
  64. /*----------------------------------------------------------------------*/
  65. /* generic power resource operations, which work on all regulators */
  66. static int twl4030reg_grp(struct regulator_dev *rdev)
  67. {
  68. return twl4030reg_read(rdev_get_drvdata(rdev), VREG_GRP);
  69. }
  70. /*
  71. * Enable/disable regulators by joining/leaving the P1 (processor) group.
  72. * We assume nobody else is updating the DEV_GRP registers.
  73. */
  74. #define P3_GRP BIT(7) /* "peripherals" */
  75. #define P2_GRP BIT(6) /* secondary processor, modem, etc */
  76. #define P1_GRP BIT(5) /* CPU/Linux */
  77. static int twl4030reg_is_enabled(struct regulator_dev *rdev)
  78. {
  79. int state = twl4030reg_grp(rdev);
  80. if (state < 0)
  81. return state;
  82. return (state & P1_GRP) != 0;
  83. }
  84. static int twl4030reg_enable(struct regulator_dev *rdev)
  85. {
  86. struct twlreg_info *info = rdev_get_drvdata(rdev);
  87. int grp;
  88. grp = twl4030reg_read(info, VREG_GRP);
  89. if (grp < 0)
  90. return grp;
  91. grp |= P1_GRP;
  92. return twl4030reg_write(info, VREG_GRP, grp);
  93. }
  94. static int twl4030reg_disable(struct regulator_dev *rdev)
  95. {
  96. struct twlreg_info *info = rdev_get_drvdata(rdev);
  97. int grp;
  98. grp = twl4030reg_read(info, VREG_GRP);
  99. if (grp < 0)
  100. return grp;
  101. grp &= ~P1_GRP;
  102. return twl4030reg_write(info, VREG_GRP, grp);
  103. }
  104. static int twl4030reg_get_status(struct regulator_dev *rdev)
  105. {
  106. int state = twl4030reg_grp(rdev);
  107. if (state < 0)
  108. return state;
  109. state &= 0x0f;
  110. /* assume state != WARM_RESET; we'd not be running... */
  111. if (!state)
  112. return REGULATOR_STATUS_OFF;
  113. return (state & BIT(3))
  114. ? REGULATOR_STATUS_NORMAL
  115. : REGULATOR_STATUS_STANDBY;
  116. }
  117. static int twl4030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
  118. {
  119. struct twlreg_info *info = rdev_get_drvdata(rdev);
  120. unsigned message;
  121. int status;
  122. /* We can only set the mode through state machine commands... */
  123. switch (mode) {
  124. case REGULATOR_MODE_NORMAL:
  125. message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_ACTIVE);
  126. break;
  127. case REGULATOR_MODE_STANDBY:
  128. message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_SLEEP);
  129. break;
  130. default:
  131. return -EINVAL;
  132. }
  133. /* Ensure the resource is associated with some group */
  134. status = twl4030reg_grp(rdev);
  135. if (status < 0)
  136. return status;
  137. if (!(status & (P3_GRP | P2_GRP | P1_GRP)))
  138. return -EACCES;
  139. status = twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
  140. message >> 8, 0x15 /* PB_WORD_MSB */ );
  141. if (status >= 0)
  142. return status;
  143. return twl4030_i2c_write_u8(TWL4030_MODULE_PM_MASTER,
  144. message, 0x16 /* PB_WORD_LSB */ );
  145. }
  146. /*----------------------------------------------------------------------*/
  147. /*
  148. * Support for adjustable-voltage LDOs uses a four bit (or less) voltage
  149. * select field in its control register. We use tables indexed by VSEL
  150. * to record voltages in milliVolts. (Accuracy is about three percent.)
  151. *
  152. * Note that VSEL values for VAUX2 changed in twl5030 and newer silicon;
  153. * currently handled by listing two slightly different VAUX2 regulators,
  154. * only one of which will be configured.
  155. *
  156. * VSEL values documented as "TI cannot support these values" are flagged
  157. * in these tables as UNSUP() values; we normally won't assign them.
  158. */
  159. #ifdef CONFIG_TWL4030_ALLOW_UNSUPPORTED
  160. #define UNSUP_MASK 0x0000
  161. #else
  162. #define UNSUP_MASK 0x8000
  163. #endif
  164. #define UNSUP(x) (UNSUP_MASK | (x))
  165. #define IS_UNSUP(x) (UNSUP_MASK & (x))
  166. #define LDO_MV(x) (~UNSUP_MASK & (x))
  167. static const u16 VAUX1_VSEL_table[] = {
  168. UNSUP(1500), UNSUP(1800), 2500, 2800,
  169. 3000, 3000, 3000, 3000,
  170. };
  171. static const u16 VAUX2_4030_VSEL_table[] = {
  172. UNSUP(1000), UNSUP(1000), UNSUP(1200), 1300,
  173. 1500, 1800, UNSUP(1850), 2500,
  174. UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
  175. UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
  176. };
  177. static const u16 VAUX2_VSEL_table[] = {
  178. 1700, 1700, 1900, 1300,
  179. 1500, 1800, 2000, 2500,
  180. 2100, 2800, 2200, 2300,
  181. 2400, 2400, 2400, 2400,
  182. };
  183. static const u16 VAUX3_VSEL_table[] = {
  184. 1500, 1800, 2500, 2800,
  185. UNSUP(3000), UNSUP(3000), UNSUP(3000), UNSUP(3000),
  186. };
  187. static const u16 VAUX4_VSEL_table[] = {
  188. 700, 1000, 1200, UNSUP(1300),
  189. 1500, 1800, UNSUP(1850), 2500,
  190. };
  191. static const u16 VMMC1_VSEL_table[] = {
  192. 1850, 2850, 3000, 3150,
  193. };
  194. static const u16 VMMC2_VSEL_table[] = {
  195. UNSUP(1000), UNSUP(1000), UNSUP(1200), UNSUP(1300),
  196. UNSUP(1500), UNSUP(1800), 1850, UNSUP(2500),
  197. 2600, 2800, 2850, 3000,
  198. 3150, 3150, 3150, 3150,
  199. };
  200. static const u16 VPLL1_VSEL_table[] = {
  201. 1000, 1200, 1300, 1800,
  202. UNSUP(2800), UNSUP(3000), UNSUP(3000), UNSUP(3000),
  203. };
  204. static const u16 VPLL2_VSEL_table[] = {
  205. 700, 1000, 1200, 1300,
  206. UNSUP(1500), 1800, UNSUP(1850), UNSUP(2500),
  207. UNSUP(2600), UNSUP(2800), UNSUP(2850), UNSUP(3000),
  208. UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
  209. };
  210. static const u16 VSIM_VSEL_table[] = {
  211. UNSUP(1000), UNSUP(1200), UNSUP(1300), 1800,
  212. 2800, 3000, 3000, 3000,
  213. };
  214. static const u16 VDAC_VSEL_table[] = {
  215. 1200, 1300, 1800, 1800,
  216. };
  217. static int twl4030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
  218. {
  219. struct twlreg_info *info = rdev_get_drvdata(rdev);
  220. int mV = info->table[index];
  221. return IS_UNSUP(mV) ? 0 : (LDO_MV(mV) * 1000);
  222. }
  223. static int
  224. twl4030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV)
  225. {
  226. struct twlreg_info *info = rdev_get_drvdata(rdev);
  227. int vsel;
  228. for (vsel = 0; vsel < info->table_len; vsel++) {
  229. int mV = info->table[vsel];
  230. int uV;
  231. if (IS_UNSUP(mV))
  232. continue;
  233. uV = LDO_MV(mV) * 1000;
  234. /* REVISIT for VAUX2, first match may not be best/lowest */
  235. /* use the first in-range value */
  236. if (min_uV <= uV && uV <= max_uV)
  237. return twl4030reg_write(info, VREG_DEDICATED, vsel);
  238. }
  239. return -EDOM;
  240. }
  241. static int twl4030ldo_get_voltage(struct regulator_dev *rdev)
  242. {
  243. struct twlreg_info *info = rdev_get_drvdata(rdev);
  244. int vsel = twl4030reg_read(info, VREG_DEDICATED);
  245. if (vsel < 0)
  246. return vsel;
  247. vsel &= info->table_len - 1;
  248. return LDO_MV(info->table[vsel]) * 1000;
  249. }
  250. static struct regulator_ops twl4030ldo_ops = {
  251. .list_voltage = twl4030ldo_list_voltage,
  252. .set_voltage = twl4030ldo_set_voltage,
  253. .get_voltage = twl4030ldo_get_voltage,
  254. .enable = twl4030reg_enable,
  255. .disable = twl4030reg_disable,
  256. .is_enabled = twl4030reg_is_enabled,
  257. .set_mode = twl4030reg_set_mode,
  258. .get_status = twl4030reg_get_status,
  259. };
  260. /*----------------------------------------------------------------------*/
  261. /*
  262. * Fixed voltage LDOs don't have a VSEL field to update.
  263. */
  264. static int twl4030fixed_list_voltage(struct regulator_dev *rdev, unsigned index)
  265. {
  266. struct twlreg_info *info = rdev_get_drvdata(rdev);
  267. return info->min_mV * 1000;
  268. }
  269. static int twl4030fixed_get_voltage(struct regulator_dev *rdev)
  270. {
  271. struct twlreg_info *info = rdev_get_drvdata(rdev);
  272. return info->min_mV * 1000;
  273. }
  274. static struct regulator_ops twl4030fixed_ops = {
  275. .list_voltage = twl4030fixed_list_voltage,
  276. .get_voltage = twl4030fixed_get_voltage,
  277. .enable = twl4030reg_enable,
  278. .disable = twl4030reg_disable,
  279. .is_enabled = twl4030reg_is_enabled,
  280. .set_mode = twl4030reg_set_mode,
  281. .get_status = twl4030reg_get_status,
  282. };
  283. /*----------------------------------------------------------------------*/
  284. #define TWL_ADJUSTABLE_LDO(label, offset, num) { \
  285. .base = offset, \
  286. .id = num, \
  287. .table_len = ARRAY_SIZE(label##_VSEL_table), \
  288. .table = label##_VSEL_table, \
  289. .desc = { \
  290. .name = #label, \
  291. .id = TWL4030_REG_##label, \
  292. .n_voltages = ARRAY_SIZE(label##_VSEL_table), \
  293. .ops = &twl4030ldo_ops, \
  294. .type = REGULATOR_VOLTAGE, \
  295. .owner = THIS_MODULE, \
  296. }, \
  297. }
  298. #define TWL_FIXED_LDO(label, offset, mVolts, num) { \
  299. .base = offset, \
  300. .id = num, \
  301. .min_mV = mVolts, \
  302. .desc = { \
  303. .name = #label, \
  304. .id = TWL4030_REG_##label, \
  305. .n_voltages = 1, \
  306. .ops = &twl4030fixed_ops, \
  307. .type = REGULATOR_VOLTAGE, \
  308. .owner = THIS_MODULE, \
  309. }, \
  310. }
  311. /*
  312. * We list regulators here if systems need some level of
  313. * software control over them after boot.
  314. */
  315. static struct twlreg_info twl4030_regs[] = {
  316. TWL_ADJUSTABLE_LDO(VAUX1, 0x17, 1),
  317. TWL_ADJUSTABLE_LDO(VAUX2_4030, 0x1b, 2),
  318. TWL_ADJUSTABLE_LDO(VAUX2, 0x1b, 2),
  319. TWL_ADJUSTABLE_LDO(VAUX3, 0x1f, 3),
  320. TWL_ADJUSTABLE_LDO(VAUX4, 0x23, 4),
  321. TWL_ADJUSTABLE_LDO(VMMC1, 0x27, 5),
  322. TWL_ADJUSTABLE_LDO(VMMC2, 0x2b, 6),
  323. /*
  324. TWL_ADJUSTABLE_LDO(VPLL1, 0x2f, 7),
  325. TWL_ADJUSTABLE_LDO(VPLL2, 0x33, 8),
  326. */
  327. TWL_ADJUSTABLE_LDO(VSIM, 0x37, 9),
  328. TWL_ADJUSTABLE_LDO(VDAC, 0x3b, 10),
  329. /*
  330. TWL_ADJUSTABLE_LDO(VINTANA1, 0x3f, 11),
  331. TWL_ADJUSTABLE_LDO(VINTANA2, 0x43, 12),
  332. TWL_ADJUSTABLE_LDO(VINTDIG, 0x47, 13),
  333. TWL_SMPS(VIO, 0x4b, 14),
  334. TWL_SMPS(VDD1, 0x55, 15),
  335. TWL_SMPS(VDD2, 0x63, 16),
  336. */
  337. TWL_FIXED_LDO(VUSB1V5, 0x71, 1500, 17),
  338. TWL_FIXED_LDO(VUSB1V8, 0x74, 1800, 18),
  339. TWL_FIXED_LDO(VUSB3V1, 0x77, 3100, 19),
  340. /* VUSBCP is managed *only* by the USB subchip */
  341. };
  342. static int twl4030reg_probe(struct platform_device *pdev)
  343. {
  344. int i;
  345. struct twlreg_info *info;
  346. struct regulator_init_data *initdata;
  347. struct regulation_constraints *c;
  348. struct regulator_dev *rdev;
  349. for (i = 0, info = NULL; i < ARRAY_SIZE(twl4030_regs); i++) {
  350. if (twl4030_regs[i].desc.id != pdev->id)
  351. continue;
  352. info = twl4030_regs + i;
  353. break;
  354. }
  355. if (!info)
  356. return -ENODEV;
  357. initdata = pdev->dev.platform_data;
  358. if (!initdata)
  359. return -EINVAL;
  360. /* Constrain board-specific capabilities according to what
  361. * this driver and the chip itself can actually do.
  362. */
  363. c = &initdata->constraints;
  364. c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
  365. c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
  366. | REGULATOR_CHANGE_MODE
  367. | REGULATOR_CHANGE_STATUS;
  368. rdev = regulator_register(&info->desc, &pdev->dev, initdata, info);
  369. if (IS_ERR(rdev)) {
  370. dev_err(&pdev->dev, "can't register %s, %ld\n",
  371. info->desc.name, PTR_ERR(rdev));
  372. return PTR_ERR(rdev);
  373. }
  374. platform_set_drvdata(pdev, rdev);
  375. /* NOTE: many regulators support short-circuit IRQs (presentable
  376. * as REGULATOR_OVER_CURRENT notifications?) configured via:
  377. * - SC_CONFIG
  378. * - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
  379. * - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
  380. * - IT_CONFIG
  381. */
  382. return 0;
  383. }
  384. static int __devexit twl4030reg_remove(struct platform_device *pdev)
  385. {
  386. regulator_unregister(platform_get_drvdata(pdev));
  387. return 0;
  388. }
  389. MODULE_ALIAS("platform:twl4030_reg");
  390. static struct platform_driver twl4030reg_driver = {
  391. .probe = twl4030reg_probe,
  392. .remove = __devexit_p(twl4030reg_remove),
  393. /* NOTE: short name, to work around driver model truncation of
  394. * "twl4030_regulator.12" (and friends) to "twl4030_regulator.1".
  395. */
  396. .driver.name = "twl4030_reg",
  397. .driver.owner = THIS_MODULE,
  398. };
  399. static int __init twl4030reg_init(void)
  400. {
  401. return platform_driver_register(&twl4030reg_driver);
  402. }
  403. subsys_initcall(twl4030reg_init);
  404. static void __exit twl4030reg_exit(void)
  405. {
  406. platform_driver_unregister(&twl4030reg_driver);
  407. }
  408. module_exit(twl4030reg_exit)
  409. MODULE_DESCRIPTION("TWL4030 regulator driver");
  410. MODULE_LICENSE("GPL");