twl4030-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. * VAUX3 at 3V is incorrectly listed in some TI manuals as unsupported.
  160. * TI are revising the twl5030/tps659x0 specs to support that 3.0V setting.
  161. */
  162. #ifdef CONFIG_TWL4030_ALLOW_UNSUPPORTED
  163. #define UNSUP_MASK 0x0000
  164. #else
  165. #define UNSUP_MASK 0x8000
  166. #endif
  167. #define UNSUP(x) (UNSUP_MASK | (x))
  168. #define IS_UNSUP(x) (UNSUP_MASK & (x))
  169. #define LDO_MV(x) (~UNSUP_MASK & (x))
  170. static const u16 VAUX1_VSEL_table[] = {
  171. UNSUP(1500), UNSUP(1800), 2500, 2800,
  172. 3000, 3000, 3000, 3000,
  173. };
  174. static const u16 VAUX2_4030_VSEL_table[] = {
  175. UNSUP(1000), UNSUP(1000), UNSUP(1200), 1300,
  176. 1500, 1800, UNSUP(1850), 2500,
  177. UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
  178. UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
  179. };
  180. static const u16 VAUX2_VSEL_table[] = {
  181. 1700, 1700, 1900, 1300,
  182. 1500, 1800, 2000, 2500,
  183. 2100, 2800, 2200, 2300,
  184. 2400, 2400, 2400, 2400,
  185. };
  186. static const u16 VAUX3_VSEL_table[] = {
  187. 1500, 1800, 2500, 2800,
  188. 3000, 3000, 3000, 3000,
  189. };
  190. static const u16 VAUX4_VSEL_table[] = {
  191. 700, 1000, 1200, UNSUP(1300),
  192. 1500, 1800, UNSUP(1850), 2500,
  193. UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
  194. UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
  195. };
  196. static const u16 VMMC1_VSEL_table[] = {
  197. 1850, 2850, 3000, 3150,
  198. };
  199. static const u16 VMMC2_VSEL_table[] = {
  200. UNSUP(1000), UNSUP(1000), UNSUP(1200), UNSUP(1300),
  201. UNSUP(1500), UNSUP(1800), 1850, UNSUP(2500),
  202. 2600, 2800, 2850, 3000,
  203. 3150, 3150, 3150, 3150,
  204. };
  205. static const u16 VPLL1_VSEL_table[] = {
  206. 1000, 1200, 1300, 1800,
  207. UNSUP(2800), UNSUP(3000), UNSUP(3000), UNSUP(3000),
  208. };
  209. static const u16 VPLL2_VSEL_table[] = {
  210. 700, 1000, 1200, 1300,
  211. UNSUP(1500), 1800, UNSUP(1850), UNSUP(2500),
  212. UNSUP(2600), UNSUP(2800), UNSUP(2850), UNSUP(3000),
  213. UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
  214. };
  215. static const u16 VSIM_VSEL_table[] = {
  216. UNSUP(1000), UNSUP(1200), UNSUP(1300), 1800,
  217. 2800, 3000, 3000, 3000,
  218. };
  219. static const u16 VDAC_VSEL_table[] = {
  220. 1200, 1300, 1800, 1800,
  221. };
  222. static int twl4030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
  223. {
  224. struct twlreg_info *info = rdev_get_drvdata(rdev);
  225. int mV = info->table[index];
  226. return IS_UNSUP(mV) ? 0 : (LDO_MV(mV) * 1000);
  227. }
  228. static int
  229. twl4030ldo_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV)
  230. {
  231. struct twlreg_info *info = rdev_get_drvdata(rdev);
  232. int vsel;
  233. for (vsel = 0; vsel < info->table_len; vsel++) {
  234. int mV = info->table[vsel];
  235. int uV;
  236. if (IS_UNSUP(mV))
  237. continue;
  238. uV = LDO_MV(mV) * 1000;
  239. /* REVISIT for VAUX2, first match may not be best/lowest */
  240. /* use the first in-range value */
  241. if (min_uV <= uV && uV <= max_uV)
  242. return twl4030reg_write(info, VREG_DEDICATED, vsel);
  243. }
  244. return -EDOM;
  245. }
  246. static int twl4030ldo_get_voltage(struct regulator_dev *rdev)
  247. {
  248. struct twlreg_info *info = rdev_get_drvdata(rdev);
  249. int vsel = twl4030reg_read(info, VREG_DEDICATED);
  250. if (vsel < 0)
  251. return vsel;
  252. vsel &= info->table_len - 1;
  253. return LDO_MV(info->table[vsel]) * 1000;
  254. }
  255. static struct regulator_ops twl4030ldo_ops = {
  256. .list_voltage = twl4030ldo_list_voltage,
  257. .set_voltage = twl4030ldo_set_voltage,
  258. .get_voltage = twl4030ldo_get_voltage,
  259. .enable = twl4030reg_enable,
  260. .disable = twl4030reg_disable,
  261. .is_enabled = twl4030reg_is_enabled,
  262. .set_mode = twl4030reg_set_mode,
  263. .get_status = twl4030reg_get_status,
  264. };
  265. /*----------------------------------------------------------------------*/
  266. /*
  267. * Fixed voltage LDOs don't have a VSEL field to update.
  268. */
  269. static int twl4030fixed_list_voltage(struct regulator_dev *rdev, unsigned index)
  270. {
  271. struct twlreg_info *info = rdev_get_drvdata(rdev);
  272. return info->min_mV * 1000;
  273. }
  274. static int twl4030fixed_get_voltage(struct regulator_dev *rdev)
  275. {
  276. struct twlreg_info *info = rdev_get_drvdata(rdev);
  277. return info->min_mV * 1000;
  278. }
  279. static struct regulator_ops twl4030fixed_ops = {
  280. .list_voltage = twl4030fixed_list_voltage,
  281. .get_voltage = twl4030fixed_get_voltage,
  282. .enable = twl4030reg_enable,
  283. .disable = twl4030reg_disable,
  284. .is_enabled = twl4030reg_is_enabled,
  285. .set_mode = twl4030reg_set_mode,
  286. .get_status = twl4030reg_get_status,
  287. };
  288. /*----------------------------------------------------------------------*/
  289. #define TWL_ADJUSTABLE_LDO(label, offset, num) { \
  290. .base = offset, \
  291. .id = num, \
  292. .table_len = ARRAY_SIZE(label##_VSEL_table), \
  293. .table = label##_VSEL_table, \
  294. .desc = { \
  295. .name = #label, \
  296. .id = TWL4030_REG_##label, \
  297. .n_voltages = ARRAY_SIZE(label##_VSEL_table), \
  298. .ops = &twl4030ldo_ops, \
  299. .type = REGULATOR_VOLTAGE, \
  300. .owner = THIS_MODULE, \
  301. }, \
  302. }
  303. #define TWL_FIXED_LDO(label, offset, mVolts, num) { \
  304. .base = offset, \
  305. .id = num, \
  306. .min_mV = mVolts, \
  307. .desc = { \
  308. .name = #label, \
  309. .id = TWL4030_REG_##label, \
  310. .n_voltages = 1, \
  311. .ops = &twl4030fixed_ops, \
  312. .type = REGULATOR_VOLTAGE, \
  313. .owner = THIS_MODULE, \
  314. }, \
  315. }
  316. /*
  317. * We list regulators here if systems need some level of
  318. * software control over them after boot.
  319. */
  320. static struct twlreg_info twl4030_regs[] = {
  321. TWL_ADJUSTABLE_LDO(VAUX1, 0x17, 1),
  322. TWL_ADJUSTABLE_LDO(VAUX2_4030, 0x1b, 2),
  323. TWL_ADJUSTABLE_LDO(VAUX2, 0x1b, 2),
  324. TWL_ADJUSTABLE_LDO(VAUX3, 0x1f, 3),
  325. TWL_ADJUSTABLE_LDO(VAUX4, 0x23, 4),
  326. TWL_ADJUSTABLE_LDO(VMMC1, 0x27, 5),
  327. TWL_ADJUSTABLE_LDO(VMMC2, 0x2b, 6),
  328. /*
  329. TWL_ADJUSTABLE_LDO(VPLL1, 0x2f, 7),
  330. */
  331. TWL_ADJUSTABLE_LDO(VPLL2, 0x33, 8),
  332. TWL_ADJUSTABLE_LDO(VSIM, 0x37, 9),
  333. TWL_ADJUSTABLE_LDO(VDAC, 0x3b, 10),
  334. /*
  335. TWL_ADJUSTABLE_LDO(VINTANA1, 0x3f, 11),
  336. TWL_ADJUSTABLE_LDO(VINTANA2, 0x43, 12),
  337. TWL_ADJUSTABLE_LDO(VINTDIG, 0x47, 13),
  338. TWL_SMPS(VIO, 0x4b, 14),
  339. TWL_SMPS(VDD1, 0x55, 15),
  340. TWL_SMPS(VDD2, 0x63, 16),
  341. */
  342. TWL_FIXED_LDO(VUSB1V5, 0x71, 1500, 17),
  343. TWL_FIXED_LDO(VUSB1V8, 0x74, 1800, 18),
  344. TWL_FIXED_LDO(VUSB3V1, 0x77, 3100, 19),
  345. /* VUSBCP is managed *only* by the USB subchip */
  346. };
  347. static int twl4030reg_probe(struct platform_device *pdev)
  348. {
  349. int i;
  350. struct twlreg_info *info;
  351. struct regulator_init_data *initdata;
  352. struct regulation_constraints *c;
  353. struct regulator_dev *rdev;
  354. for (i = 0, info = NULL; i < ARRAY_SIZE(twl4030_regs); i++) {
  355. if (twl4030_regs[i].desc.id != pdev->id)
  356. continue;
  357. info = twl4030_regs + i;
  358. break;
  359. }
  360. if (!info)
  361. return -ENODEV;
  362. initdata = pdev->dev.platform_data;
  363. if (!initdata)
  364. return -EINVAL;
  365. /* Constrain board-specific capabilities according to what
  366. * this driver and the chip itself can actually do.
  367. */
  368. c = &initdata->constraints;
  369. c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
  370. c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
  371. | REGULATOR_CHANGE_MODE
  372. | REGULATOR_CHANGE_STATUS;
  373. rdev = regulator_register(&info->desc, &pdev->dev, initdata, info);
  374. if (IS_ERR(rdev)) {
  375. dev_err(&pdev->dev, "can't register %s, %ld\n",
  376. info->desc.name, PTR_ERR(rdev));
  377. return PTR_ERR(rdev);
  378. }
  379. platform_set_drvdata(pdev, rdev);
  380. /* NOTE: many regulators support short-circuit IRQs (presentable
  381. * as REGULATOR_OVER_CURRENT notifications?) configured via:
  382. * - SC_CONFIG
  383. * - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
  384. * - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
  385. * - IT_CONFIG
  386. */
  387. return 0;
  388. }
  389. static int __devexit twl4030reg_remove(struct platform_device *pdev)
  390. {
  391. regulator_unregister(platform_get_drvdata(pdev));
  392. return 0;
  393. }
  394. MODULE_ALIAS("platform:twl4030_reg");
  395. static struct platform_driver twl4030reg_driver = {
  396. .probe = twl4030reg_probe,
  397. .remove = __devexit_p(twl4030reg_remove),
  398. /* NOTE: short name, to work around driver model truncation of
  399. * "twl4030_regulator.12" (and friends) to "twl4030_regulator.1".
  400. */
  401. .driver.name = "twl4030_reg",
  402. .driver.owner = THIS_MODULE,
  403. };
  404. static int __init twl4030reg_init(void)
  405. {
  406. return platform_driver_register(&twl4030reg_driver);
  407. }
  408. subsys_initcall(twl4030reg_init);
  409. static void __exit twl4030reg_exit(void)
  410. {
  411. platform_driver_unregister(&twl4030reg_driver);
  412. }
  413. module_exit(twl4030reg_exit)
  414. MODULE_DESCRIPTION("TWL4030 regulator driver");
  415. MODULE_LICENSE("GPL");