twl-regulator.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * twl-regulator.c -- support regulators in twl4030/twl6030 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/twl.h>
  18. /*
  19. * The TWL4030/TW5030/TPS659x0/TWL6030 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. /* twl 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. twlreg_read(struct twlreg_info *info, unsigned offset)
  51. {
  52. u8 value;
  53. int status;
  54. status = twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER,
  55. &value, info->base + offset);
  56. return (status < 0) ? status : value;
  57. }
  58. static inline int
  59. twlreg_write(struct twlreg_info *info, unsigned offset, u8 value)
  60. {
  61. return twl_i2c_write_u8(TWL_MODULE_PM_RECEIVER,
  62. value, info->base + offset);
  63. }
  64. /*----------------------------------------------------------------------*/
  65. /* generic power resource operations, which work on all regulators */
  66. static int twlreg_grp(struct regulator_dev *rdev)
  67. {
  68. return twlreg_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 twlreg_is_enabled(struct regulator_dev *rdev)
  78. {
  79. int state = twlreg_grp(rdev);
  80. if (state < 0)
  81. return state;
  82. return (state & P1_GRP) != 0;
  83. }
  84. static int twlreg_enable(struct regulator_dev *rdev)
  85. {
  86. struct twlreg_info *info = rdev_get_drvdata(rdev);
  87. int grp;
  88. grp = twlreg_read(info, VREG_GRP);
  89. if (grp < 0)
  90. return grp;
  91. grp |= P1_GRP;
  92. return twlreg_write(info, VREG_GRP, grp);
  93. }
  94. static int twlreg_disable(struct regulator_dev *rdev)
  95. {
  96. struct twlreg_info *info = rdev_get_drvdata(rdev);
  97. int grp;
  98. grp = twlreg_read(info, VREG_GRP);
  99. if (grp < 0)
  100. return grp;
  101. grp &= ~P1_GRP;
  102. return twlreg_write(info, VREG_GRP, grp);
  103. }
  104. static int twlreg_get_status(struct regulator_dev *rdev)
  105. {
  106. int state = twlreg_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 twlreg_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 = twlreg_grp(rdev);
  135. if (status < 0)
  136. return status;
  137. if (!(status & (P3_GRP | P2_GRP | P1_GRP)))
  138. return -EACCES;
  139. status = twl_i2c_write_u8(TWL_MODULE_PM_MASTER,
  140. message >> 8, 0x15 /* PB_WORD_MSB */ );
  141. if (status >= 0)
  142. return status;
  143. return twl_i2c_write_u8(TWL_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 twlldo_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. twlldo_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 twlreg_write(info, VREG_DEDICATED, vsel);
  243. }
  244. return -EDOM;
  245. }
  246. static int twlldo_get_voltage(struct regulator_dev *rdev)
  247. {
  248. struct twlreg_info *info = rdev_get_drvdata(rdev);
  249. int vsel = twlreg_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 twlldo_ops = {
  256. .list_voltage = twlldo_list_voltage,
  257. .set_voltage = twlldo_set_voltage,
  258. .get_voltage = twlldo_get_voltage,
  259. .enable = twlreg_enable,
  260. .disable = twlreg_disable,
  261. .is_enabled = twlreg_is_enabled,
  262. .set_mode = twlreg_set_mode,
  263. .get_status = twlreg_get_status,
  264. };
  265. /*----------------------------------------------------------------------*/
  266. /*
  267. * Fixed voltage LDOs don't have a VSEL field to update.
  268. */
  269. static int twlfixed_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 twlfixed_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 twlfixed_ops = {
  280. .list_voltage = twlfixed_list_voltage,
  281. .get_voltage = twlfixed_get_voltage,
  282. .enable = twlreg_enable,
  283. .disable = twlreg_disable,
  284. .is_enabled = twlreg_is_enabled,
  285. .set_mode = twlreg_set_mode,
  286. .get_status = twlreg_get_status,
  287. };
  288. /*----------------------------------------------------------------------*/
  289. #define TWL4030_ADJUSTABLE_LDO(label, offset, num) \
  290. TWL_ADJUSTABLE_LDO(label, offset, num, TWL4030)
  291. #define TWL4030_FIXED_LDO(label, offset, mVolts, num) \
  292. TWL_FIXED_LDO(label, offset, mVolts, num, TWL4030)
  293. #define TWL_ADJUSTABLE_LDO(label, offset, num, family) { \
  294. .base = offset, \
  295. .id = num, \
  296. .table_len = ARRAY_SIZE(label##_VSEL_table), \
  297. .table = label##_VSEL_table, \
  298. .desc = { \
  299. .name = #label, \
  300. .id = family##_REG_##label, \
  301. .n_voltages = ARRAY_SIZE(label##_VSEL_table), \
  302. .ops = &twlldo_ops, \
  303. .type = REGULATOR_VOLTAGE, \
  304. .owner = THIS_MODULE, \
  305. }, \
  306. }
  307. #define TWL_FIXED_LDO(label, offset, mVolts, num, family) { \
  308. .base = offset, \
  309. .id = num, \
  310. .min_mV = mVolts, \
  311. .desc = { \
  312. .name = #label, \
  313. .id = family##_REG_##label, \
  314. .n_voltages = 1, \
  315. .ops = &twlfixed_ops, \
  316. .type = REGULATOR_VOLTAGE, \
  317. .owner = THIS_MODULE, \
  318. }, \
  319. }
  320. /*
  321. * We list regulators here if systems need some level of
  322. * software control over them after boot.
  323. */
  324. static struct twlreg_info twl_regs[] = {
  325. TWL4030_ADJUSTABLE_LDO(VAUX1, 0x17, 1),
  326. TWL4030_ADJUSTABLE_LDO(VAUX2_4030, 0x1b, 2),
  327. TWL4030_ADJUSTABLE_LDO(VAUX2, 0x1b, 2),
  328. TWL4030_ADJUSTABLE_LDO(VAUX3, 0x1f, 3),
  329. TWL4030_ADJUSTABLE_LDO(VAUX4, 0x23, 4),
  330. TWL4030_ADJUSTABLE_LDO(VMMC1, 0x27, 5),
  331. TWL4030_ADJUSTABLE_LDO(VMMC2, 0x2b, 6),
  332. /*
  333. TWL4030_ADJUSTABLE_LDO(VPLL1, 0x2f, 7),
  334. */
  335. TWL4030_ADJUSTABLE_LDO(VPLL2, 0x33, 8),
  336. TWL4030_ADJUSTABLE_LDO(VSIM, 0x37, 9),
  337. TWL4030_ADJUSTABLE_LDO(VDAC, 0x3b, 10),
  338. /*
  339. TWL4030_ADJUSTABLE_LDO(VINTANA1, 0x3f, 11),
  340. TWL4030_ADJUSTABLE_LDO(VINTANA2, 0x43, 12),
  341. TWL4030_ADJUSTABLE_LDO(VINTDIG, 0x47, 13),
  342. TWL4030_SMPS(VIO, 0x4b, 14),
  343. TWL4030_SMPS(VDD1, 0x55, 15),
  344. TWL4030_SMPS(VDD2, 0x63, 16),
  345. */
  346. TWL4030_FIXED_LDO(VUSB1V5, 0x71, 1500, 17),
  347. TWL4030_FIXED_LDO(VUSB1V8, 0x74, 1800, 18),
  348. TWL4030_FIXED_LDO(VUSB3V1, 0x77, 3100, 19),
  349. /* VUSBCP is managed *only* by the USB subchip */
  350. };
  351. static int twlreg_probe(struct platform_device *pdev)
  352. {
  353. int i;
  354. struct twlreg_info *info;
  355. struct regulator_init_data *initdata;
  356. struct regulation_constraints *c;
  357. struct regulator_dev *rdev;
  358. for (i = 0, info = NULL; i < ARRAY_SIZE(twl_regs); i++) {
  359. if (twl_regs[i].desc.id != pdev->id)
  360. continue;
  361. info = twl_regs + i;
  362. break;
  363. }
  364. if (!info)
  365. return -ENODEV;
  366. initdata = pdev->dev.platform_data;
  367. if (!initdata)
  368. return -EINVAL;
  369. /* Constrain board-specific capabilities according to what
  370. * this driver and the chip itself can actually do.
  371. */
  372. c = &initdata->constraints;
  373. c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
  374. c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
  375. | REGULATOR_CHANGE_MODE
  376. | REGULATOR_CHANGE_STATUS;
  377. rdev = regulator_register(&info->desc, &pdev->dev, initdata, info);
  378. if (IS_ERR(rdev)) {
  379. dev_err(&pdev->dev, "can't register %s, %ld\n",
  380. info->desc.name, PTR_ERR(rdev));
  381. return PTR_ERR(rdev);
  382. }
  383. platform_set_drvdata(pdev, rdev);
  384. /* NOTE: many regulators support short-circuit IRQs (presentable
  385. * as REGULATOR_OVER_CURRENT notifications?) configured via:
  386. * - SC_CONFIG
  387. * - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
  388. * - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
  389. * - IT_CONFIG
  390. */
  391. return 0;
  392. }
  393. static int __devexit twlreg_remove(struct platform_device *pdev)
  394. {
  395. regulator_unregister(platform_get_drvdata(pdev));
  396. return 0;
  397. }
  398. MODULE_ALIAS("platform:twl_reg");
  399. static struct platform_driver twlreg_driver = {
  400. .probe = twlreg_probe,
  401. .remove = __devexit_p(twlreg_remove),
  402. /* NOTE: short name, to work around driver model truncation of
  403. * "twl_regulator.12" (and friends) to "twl_regulator.1".
  404. */
  405. .driver.name = "twl_reg",
  406. .driver.owner = THIS_MODULE,
  407. };
  408. static int __init twlreg_init(void)
  409. {
  410. return platform_driver_register(&twlreg_driver);
  411. }
  412. subsys_initcall(twlreg_init);
  413. static void __exit twlreg_exit(void)
  414. {
  415. platform_driver_unregister(&twlreg_driver);
  416. }
  417. module_exit(twlreg_exit)
  418. MODULE_DESCRIPTION("TWL regulator driver");
  419. MODULE_LICENSE("GPL");