twl-regulator.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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/string.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/err.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/regulator/driver.h>
  20. #include <linux/regulator/machine.h>
  21. #include <linux/regulator/of_regulator.h>
  22. #include <linux/i2c/twl.h>
  23. #include <linux/delay.h>
  24. /*
  25. * The TWL4030/TW5030/TPS659x0/TWL6030 family chips include power management, a
  26. * USB OTG transceiver, an RTC, ADC, PWM, and lots more. Some versions
  27. * include an audio codec, battery charger, and more voltage regulators.
  28. * These chips are often used in OMAP-based systems.
  29. *
  30. * This driver implements software-based resource control for various
  31. * voltage regulators. This is usually augmented with state machine
  32. * based control.
  33. */
  34. struct twlreg_info {
  35. /* start of regulator's PM_RECEIVER control register bank */
  36. u8 base;
  37. /* twl resource ID, for resource control state machine */
  38. u8 id;
  39. /* voltage in mV = table[VSEL]; table_len must be a power-of-two */
  40. u8 table_len;
  41. const u16 *table;
  42. /* State REMAP default configuration */
  43. u8 remap;
  44. /* chip constraints on regulator behavior */
  45. u16 min_mV;
  46. u16 max_mV;
  47. u8 flags;
  48. /* used by regulator core */
  49. struct regulator_desc desc;
  50. /* chip specific features */
  51. unsigned long features;
  52. /*
  53. * optional override functions for voltage set/get
  54. * these are currently only used for SMPS regulators
  55. */
  56. int (*get_voltage)(void *data);
  57. int (*set_voltage)(void *data, int target_uV);
  58. /* data passed from board for external get/set voltage */
  59. void *data;
  60. };
  61. /* LDO control registers ... offset is from the base of its register bank.
  62. * The first three registers of all power resource banks help hardware to
  63. * manage the various resource groups.
  64. */
  65. /* Common offset in TWL4030/6030 */
  66. #define VREG_GRP 0
  67. /* TWL4030 register offsets */
  68. #define VREG_TYPE 1
  69. #define VREG_REMAP 2
  70. #define VREG_DEDICATED 3 /* LDO control */
  71. #define VREG_VOLTAGE_SMPS_4030 9
  72. /* TWL6030 register offsets */
  73. #define VREG_TRANS 1
  74. #define VREG_STATE 2
  75. #define VREG_VOLTAGE 3
  76. #define VREG_VOLTAGE_SMPS 4
  77. /* TWL6030 Misc register offsets */
  78. #define VREG_BC_ALL 1
  79. #define VREG_BC_REF 2
  80. #define VREG_BC_PROC 3
  81. #define VREG_BC_CLK_RST 4
  82. /* TWL6030 LDO register values for CFG_STATE */
  83. #define TWL6030_CFG_STATE_OFF 0x00
  84. #define TWL6030_CFG_STATE_ON 0x01
  85. #define TWL6030_CFG_STATE_OFF2 0x02
  86. #define TWL6030_CFG_STATE_SLEEP 0x03
  87. #define TWL6030_CFG_STATE_GRP_SHIFT 5
  88. #define TWL6030_CFG_STATE_APP_SHIFT 2
  89. #define TWL6030_CFG_STATE_APP_MASK (0x03 << TWL6030_CFG_STATE_APP_SHIFT)
  90. #define TWL6030_CFG_STATE_APP(v) (((v) & TWL6030_CFG_STATE_APP_MASK) >>\
  91. TWL6030_CFG_STATE_APP_SHIFT)
  92. /* Flags for SMPS Voltage reading */
  93. #define SMPS_OFFSET_EN BIT(0)
  94. #define SMPS_EXTENDED_EN BIT(1)
  95. /* twl6032 SMPS EPROM values */
  96. #define TWL6030_SMPS_OFFSET 0xB0
  97. #define TWL6030_SMPS_MULT 0xB3
  98. #define SMPS_MULTOFFSET_SMPS4 BIT(0)
  99. #define SMPS_MULTOFFSET_VIO BIT(1)
  100. #define SMPS_MULTOFFSET_SMPS3 BIT(6)
  101. static inline int
  102. twlreg_read(struct twlreg_info *info, unsigned slave_subgp, unsigned offset)
  103. {
  104. u8 value;
  105. int status;
  106. status = twl_i2c_read_u8(slave_subgp,
  107. &value, info->base + offset);
  108. return (status < 0) ? status : value;
  109. }
  110. static inline int
  111. twlreg_write(struct twlreg_info *info, unsigned slave_subgp, unsigned offset,
  112. u8 value)
  113. {
  114. return twl_i2c_write_u8(slave_subgp,
  115. value, info->base + offset);
  116. }
  117. /*----------------------------------------------------------------------*/
  118. /* generic power resource operations, which work on all regulators */
  119. static int twlreg_grp(struct regulator_dev *rdev)
  120. {
  121. return twlreg_read(rdev_get_drvdata(rdev), TWL_MODULE_PM_RECEIVER,
  122. VREG_GRP);
  123. }
  124. /*
  125. * Enable/disable regulators by joining/leaving the P1 (processor) group.
  126. * We assume nobody else is updating the DEV_GRP registers.
  127. */
  128. /* definition for 4030 family */
  129. #define P3_GRP_4030 BIT(7) /* "peripherals" */
  130. #define P2_GRP_4030 BIT(6) /* secondary processor, modem, etc */
  131. #define P1_GRP_4030 BIT(5) /* CPU/Linux */
  132. /* definition for 6030 family */
  133. #define P3_GRP_6030 BIT(2) /* secondary processor, modem, etc */
  134. #define P2_GRP_6030 BIT(1) /* "peripherals" */
  135. #define P1_GRP_6030 BIT(0) /* CPU/Linux */
  136. static int twl4030reg_is_enabled(struct regulator_dev *rdev)
  137. {
  138. int state = twlreg_grp(rdev);
  139. if (state < 0)
  140. return state;
  141. return state & P1_GRP_4030;
  142. }
  143. static int twl6030reg_is_enabled(struct regulator_dev *rdev)
  144. {
  145. struct twlreg_info *info = rdev_get_drvdata(rdev);
  146. int grp = 0, val;
  147. if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS))) {
  148. grp = twlreg_grp(rdev);
  149. if (grp < 0)
  150. return grp;
  151. grp &= P1_GRP_6030;
  152. } else {
  153. grp = 1;
  154. }
  155. val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
  156. val = TWL6030_CFG_STATE_APP(val);
  157. return grp && (val == TWL6030_CFG_STATE_ON);
  158. }
  159. #define PB_I2C_BUSY BIT(0)
  160. #define PB_I2C_BWEN BIT(1)
  161. /* Wait until buffer empty/ready to send a word on power bus. */
  162. static int twl4030_wait_pb_ready(void)
  163. {
  164. int ret;
  165. int timeout = 10;
  166. u8 val;
  167. do {
  168. ret = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val,
  169. TWL4030_PM_MASTER_PB_CFG);
  170. if (ret < 0)
  171. return ret;
  172. if (!(val & PB_I2C_BUSY))
  173. return 0;
  174. mdelay(1);
  175. timeout--;
  176. } while (timeout);
  177. return -ETIMEDOUT;
  178. }
  179. /* Send a word over the powerbus */
  180. static int twl4030_send_pb_msg(unsigned msg)
  181. {
  182. u8 val;
  183. int ret;
  184. /* save powerbus configuration */
  185. ret = twl_i2c_read_u8(TWL_MODULE_PM_MASTER, &val,
  186. TWL4030_PM_MASTER_PB_CFG);
  187. if (ret < 0)
  188. return ret;
  189. /* Enable i2c access to powerbus */
  190. ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val | PB_I2C_BWEN,
  191. TWL4030_PM_MASTER_PB_CFG);
  192. if (ret < 0)
  193. return ret;
  194. ret = twl4030_wait_pb_ready();
  195. if (ret < 0)
  196. return ret;
  197. ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, msg >> 8,
  198. TWL4030_PM_MASTER_PB_WORD_MSB);
  199. if (ret < 0)
  200. return ret;
  201. ret = twl_i2c_write_u8(TWL_MODULE_PM_MASTER, msg & 0xff,
  202. TWL4030_PM_MASTER_PB_WORD_LSB);
  203. if (ret < 0)
  204. return ret;
  205. ret = twl4030_wait_pb_ready();
  206. if (ret < 0)
  207. return ret;
  208. /* Restore powerbus configuration */
  209. return twl_i2c_write_u8(TWL_MODULE_PM_MASTER, val,
  210. TWL4030_PM_MASTER_PB_CFG);
  211. }
  212. static int twl4030reg_enable(struct regulator_dev *rdev)
  213. {
  214. struct twlreg_info *info = rdev_get_drvdata(rdev);
  215. int grp;
  216. int ret;
  217. grp = twlreg_grp(rdev);
  218. if (grp < 0)
  219. return grp;
  220. grp |= P1_GRP_4030;
  221. ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
  222. return ret;
  223. }
  224. static int twl6030reg_enable(struct regulator_dev *rdev)
  225. {
  226. struct twlreg_info *info = rdev_get_drvdata(rdev);
  227. int grp = 0;
  228. int ret;
  229. if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
  230. grp = twlreg_grp(rdev);
  231. if (grp < 0)
  232. return grp;
  233. ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
  234. grp << TWL6030_CFG_STATE_GRP_SHIFT |
  235. TWL6030_CFG_STATE_ON);
  236. return ret;
  237. }
  238. static int twl4030reg_disable(struct regulator_dev *rdev)
  239. {
  240. struct twlreg_info *info = rdev_get_drvdata(rdev);
  241. int grp;
  242. int ret;
  243. grp = twlreg_grp(rdev);
  244. if (grp < 0)
  245. return grp;
  246. grp &= ~(P1_GRP_4030 | P2_GRP_4030 | P3_GRP_4030);
  247. ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_GRP, grp);
  248. return ret;
  249. }
  250. static int twl6030reg_disable(struct regulator_dev *rdev)
  251. {
  252. struct twlreg_info *info = rdev_get_drvdata(rdev);
  253. int grp = 0;
  254. int ret;
  255. if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
  256. grp = P1_GRP_6030 | P2_GRP_6030 | P3_GRP_6030;
  257. /* For 6030, set the off state for all grps enabled */
  258. ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
  259. (grp) << TWL6030_CFG_STATE_GRP_SHIFT |
  260. TWL6030_CFG_STATE_OFF);
  261. return ret;
  262. }
  263. static int twl4030reg_get_status(struct regulator_dev *rdev)
  264. {
  265. int state = twlreg_grp(rdev);
  266. if (state < 0)
  267. return state;
  268. state &= 0x0f;
  269. /* assume state != WARM_RESET; we'd not be running... */
  270. if (!state)
  271. return REGULATOR_STATUS_OFF;
  272. return (state & BIT(3))
  273. ? REGULATOR_STATUS_NORMAL
  274. : REGULATOR_STATUS_STANDBY;
  275. }
  276. static int twl6030reg_get_status(struct regulator_dev *rdev)
  277. {
  278. struct twlreg_info *info = rdev_get_drvdata(rdev);
  279. int val;
  280. val = twlreg_grp(rdev);
  281. if (val < 0)
  282. return val;
  283. val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
  284. switch (TWL6030_CFG_STATE_APP(val)) {
  285. case TWL6030_CFG_STATE_ON:
  286. return REGULATOR_STATUS_NORMAL;
  287. case TWL6030_CFG_STATE_SLEEP:
  288. return REGULATOR_STATUS_STANDBY;
  289. case TWL6030_CFG_STATE_OFF:
  290. case TWL6030_CFG_STATE_OFF2:
  291. default:
  292. break;
  293. }
  294. return REGULATOR_STATUS_OFF;
  295. }
  296. static int twl4030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
  297. {
  298. struct twlreg_info *info = rdev_get_drvdata(rdev);
  299. unsigned message;
  300. /* We can only set the mode through state machine commands... */
  301. switch (mode) {
  302. case REGULATOR_MODE_NORMAL:
  303. message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_ACTIVE);
  304. break;
  305. case REGULATOR_MODE_STANDBY:
  306. message = MSG_SINGULAR(DEV_GRP_P1, info->id, RES_STATE_SLEEP);
  307. break;
  308. default:
  309. return -EINVAL;
  310. }
  311. return twl4030_send_pb_msg(message);
  312. }
  313. static inline unsigned int twl4030reg_map_mode(unsigned int mode)
  314. {
  315. switch (mode) {
  316. case RES_STATE_ACTIVE:
  317. return REGULATOR_MODE_NORMAL;
  318. case RES_STATE_SLEEP:
  319. return REGULATOR_MODE_STANDBY;
  320. default:
  321. return -EINVAL;
  322. }
  323. }
  324. static int twl6030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
  325. {
  326. struct twlreg_info *info = rdev_get_drvdata(rdev);
  327. int grp = 0;
  328. int val;
  329. if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
  330. grp = twlreg_grp(rdev);
  331. if (grp < 0)
  332. return grp;
  333. /* Compose the state register settings */
  334. val = grp << TWL6030_CFG_STATE_GRP_SHIFT;
  335. /* We can only set the mode through state machine commands... */
  336. switch (mode) {
  337. case REGULATOR_MODE_NORMAL:
  338. val |= TWL6030_CFG_STATE_ON;
  339. break;
  340. case REGULATOR_MODE_STANDBY:
  341. val |= TWL6030_CFG_STATE_SLEEP;
  342. break;
  343. default:
  344. return -EINVAL;
  345. }
  346. return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE, val);
  347. }
  348. /*----------------------------------------------------------------------*/
  349. /*
  350. * Support for adjustable-voltage LDOs uses a four bit (or less) voltage
  351. * select field in its control register. We use tables indexed by VSEL
  352. * to record voltages in milliVolts. (Accuracy is about three percent.)
  353. *
  354. * Note that VSEL values for VAUX2 changed in twl5030 and newer silicon;
  355. * currently handled by listing two slightly different VAUX2 regulators,
  356. * only one of which will be configured.
  357. *
  358. * VSEL values documented as "TI cannot support these values" are flagged
  359. * in these tables as UNSUP() values; we normally won't assign them.
  360. *
  361. * VAUX3 at 3V is incorrectly listed in some TI manuals as unsupported.
  362. * TI are revising the twl5030/tps659x0 specs to support that 3.0V setting.
  363. */
  364. #define UNSUP_MASK 0x8000
  365. #define UNSUP(x) (UNSUP_MASK | (x))
  366. #define IS_UNSUP(info, x) \
  367. ((UNSUP_MASK & (x)) && \
  368. !((info)->features & TWL4030_ALLOW_UNSUPPORTED))
  369. #define LDO_MV(x) (~UNSUP_MASK & (x))
  370. static const u16 VAUX1_VSEL_table[] = {
  371. UNSUP(1500), UNSUP(1800), 2500, 2800,
  372. 3000, 3000, 3000, 3000,
  373. };
  374. static const u16 VAUX2_4030_VSEL_table[] = {
  375. UNSUP(1000), UNSUP(1000), UNSUP(1200), 1300,
  376. 1500, 1800, UNSUP(1850), 2500,
  377. UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
  378. UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
  379. };
  380. static const u16 VAUX2_VSEL_table[] = {
  381. 1700, 1700, 1900, 1300,
  382. 1500, 1800, 2000, 2500,
  383. 2100, 2800, 2200, 2300,
  384. 2400, 2400, 2400, 2400,
  385. };
  386. static const u16 VAUX3_VSEL_table[] = {
  387. 1500, 1800, 2500, 2800,
  388. 3000, 3000, 3000, 3000,
  389. };
  390. static const u16 VAUX4_VSEL_table[] = {
  391. 700, 1000, 1200, UNSUP(1300),
  392. 1500, 1800, UNSUP(1850), 2500,
  393. UNSUP(2600), 2800, UNSUP(2850), UNSUP(3000),
  394. UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
  395. };
  396. static const u16 VMMC1_VSEL_table[] = {
  397. 1850, 2850, 3000, 3150,
  398. };
  399. static const u16 VMMC2_VSEL_table[] = {
  400. UNSUP(1000), UNSUP(1000), UNSUP(1200), UNSUP(1300),
  401. UNSUP(1500), UNSUP(1800), 1850, UNSUP(2500),
  402. 2600, 2800, 2850, 3000,
  403. 3150, 3150, 3150, 3150,
  404. };
  405. static const u16 VPLL1_VSEL_table[] = {
  406. 1000, 1200, 1300, 1800,
  407. UNSUP(2800), UNSUP(3000), UNSUP(3000), UNSUP(3000),
  408. };
  409. static const u16 VPLL2_VSEL_table[] = {
  410. 700, 1000, 1200, 1300,
  411. UNSUP(1500), 1800, UNSUP(1850), UNSUP(2500),
  412. UNSUP(2600), UNSUP(2800), UNSUP(2850), UNSUP(3000),
  413. UNSUP(3150), UNSUP(3150), UNSUP(3150), UNSUP(3150),
  414. };
  415. static const u16 VSIM_VSEL_table[] = {
  416. UNSUP(1000), UNSUP(1200), UNSUP(1300), 1800,
  417. 2800, 3000, 3000, 3000,
  418. };
  419. static const u16 VDAC_VSEL_table[] = {
  420. 1200, 1300, 1800, 1800,
  421. };
  422. static const u16 VIO_VSEL_table[] = {
  423. 1800, 1850,
  424. };
  425. static const u16 VINTANA2_VSEL_table[] = {
  426. 2500, 2750,
  427. };
  428. static int twl4030ldo_list_voltage(struct regulator_dev *rdev, unsigned index)
  429. {
  430. struct twlreg_info *info = rdev_get_drvdata(rdev);
  431. int mV = info->table[index];
  432. return IS_UNSUP(info, mV) ? 0 : (LDO_MV(mV) * 1000);
  433. }
  434. static int
  435. twl4030ldo_set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
  436. {
  437. struct twlreg_info *info = rdev_get_drvdata(rdev);
  438. return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE,
  439. selector);
  440. }
  441. static int twl4030ldo_get_voltage_sel(struct regulator_dev *rdev)
  442. {
  443. struct twlreg_info *info = rdev_get_drvdata(rdev);
  444. int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE);
  445. if (vsel < 0)
  446. return vsel;
  447. vsel &= info->table_len - 1;
  448. return vsel;
  449. }
  450. static struct regulator_ops twl4030ldo_ops = {
  451. .list_voltage = twl4030ldo_list_voltage,
  452. .set_voltage_sel = twl4030ldo_set_voltage_sel,
  453. .get_voltage_sel = twl4030ldo_get_voltage_sel,
  454. .enable = twl4030reg_enable,
  455. .disable = twl4030reg_disable,
  456. .is_enabled = twl4030reg_is_enabled,
  457. .set_mode = twl4030reg_set_mode,
  458. .get_status = twl4030reg_get_status,
  459. };
  460. static int
  461. twl4030smps_set_voltage(struct regulator_dev *rdev, int min_uV, int max_uV,
  462. unsigned *selector)
  463. {
  464. struct twlreg_info *info = rdev_get_drvdata(rdev);
  465. int vsel = DIV_ROUND_UP(min_uV - 600000, 12500);
  466. if (info->set_voltage) {
  467. return info->set_voltage(info->data, min_uV);
  468. } else {
  469. twlreg_write(info, TWL_MODULE_PM_RECEIVER,
  470. VREG_VOLTAGE_SMPS_4030, vsel);
  471. }
  472. return 0;
  473. }
  474. static int twl4030smps_get_voltage(struct regulator_dev *rdev)
  475. {
  476. struct twlreg_info *info = rdev_get_drvdata(rdev);
  477. int vsel;
  478. if (info->get_voltage)
  479. return info->get_voltage(info->data);
  480. vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER,
  481. VREG_VOLTAGE_SMPS_4030);
  482. return vsel * 12500 + 600000;
  483. }
  484. static struct regulator_ops twl4030smps_ops = {
  485. .set_voltage = twl4030smps_set_voltage,
  486. .get_voltage = twl4030smps_get_voltage,
  487. };
  488. static int twl6030coresmps_set_voltage(struct regulator_dev *rdev, int min_uV,
  489. int max_uV, unsigned *selector)
  490. {
  491. struct twlreg_info *info = rdev_get_drvdata(rdev);
  492. if (info->set_voltage)
  493. return info->set_voltage(info->data, min_uV);
  494. return -ENODEV;
  495. }
  496. static int twl6030coresmps_get_voltage(struct regulator_dev *rdev)
  497. {
  498. struct twlreg_info *info = rdev_get_drvdata(rdev);
  499. if (info->get_voltage)
  500. return info->get_voltage(info->data);
  501. return -ENODEV;
  502. }
  503. static struct regulator_ops twl6030coresmps_ops = {
  504. .set_voltage = twl6030coresmps_set_voltage,
  505. .get_voltage = twl6030coresmps_get_voltage,
  506. };
  507. static int twl6030ldo_list_voltage(struct regulator_dev *rdev, unsigned sel)
  508. {
  509. struct twlreg_info *info = rdev_get_drvdata(rdev);
  510. switch (sel) {
  511. case 0:
  512. return 0;
  513. case 1 ... 24:
  514. /* Linear mapping from 00000001 to 00011000:
  515. * Absolute voltage value = 1.0 V + 0.1 V × (sel – 00000001)
  516. */
  517. return (info->min_mV + 100 * (sel - 1)) * 1000;
  518. case 25 ... 30:
  519. return -EINVAL;
  520. case 31:
  521. return 2750000;
  522. default:
  523. return -EINVAL;
  524. }
  525. }
  526. static int
  527. twl6030ldo_set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
  528. {
  529. struct twlreg_info *info = rdev_get_drvdata(rdev);
  530. return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE,
  531. selector);
  532. }
  533. static int twl6030ldo_get_voltage_sel(struct regulator_dev *rdev)
  534. {
  535. struct twlreg_info *info = rdev_get_drvdata(rdev);
  536. int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE);
  537. return vsel;
  538. }
  539. static struct regulator_ops twl6030ldo_ops = {
  540. .list_voltage = twl6030ldo_list_voltage,
  541. .set_voltage_sel = twl6030ldo_set_voltage_sel,
  542. .get_voltage_sel = twl6030ldo_get_voltage_sel,
  543. .enable = twl6030reg_enable,
  544. .disable = twl6030reg_disable,
  545. .is_enabled = twl6030reg_is_enabled,
  546. .set_mode = twl6030reg_set_mode,
  547. .get_status = twl6030reg_get_status,
  548. };
  549. /*----------------------------------------------------------------------*/
  550. static struct regulator_ops twl4030fixed_ops = {
  551. .list_voltage = regulator_list_voltage_linear,
  552. .enable = twl4030reg_enable,
  553. .disable = twl4030reg_disable,
  554. .is_enabled = twl4030reg_is_enabled,
  555. .set_mode = twl4030reg_set_mode,
  556. .get_status = twl4030reg_get_status,
  557. };
  558. static struct regulator_ops twl6030fixed_ops = {
  559. .list_voltage = regulator_list_voltage_linear,
  560. .enable = twl6030reg_enable,
  561. .disable = twl6030reg_disable,
  562. .is_enabled = twl6030reg_is_enabled,
  563. .set_mode = twl6030reg_set_mode,
  564. .get_status = twl6030reg_get_status,
  565. };
  566. /*
  567. * SMPS status and control
  568. */
  569. static int twl6030smps_list_voltage(struct regulator_dev *rdev, unsigned index)
  570. {
  571. struct twlreg_info *info = rdev_get_drvdata(rdev);
  572. int voltage = 0;
  573. switch (info->flags) {
  574. case SMPS_OFFSET_EN:
  575. voltage = 100000;
  576. /* fall through */
  577. case 0:
  578. switch (index) {
  579. case 0:
  580. voltage = 0;
  581. break;
  582. case 58:
  583. voltage = 1350 * 1000;
  584. break;
  585. case 59:
  586. voltage = 1500 * 1000;
  587. break;
  588. case 60:
  589. voltage = 1800 * 1000;
  590. break;
  591. case 61:
  592. voltage = 1900 * 1000;
  593. break;
  594. case 62:
  595. voltage = 2100 * 1000;
  596. break;
  597. default:
  598. voltage += (600000 + (12500 * (index - 1)));
  599. }
  600. break;
  601. case SMPS_EXTENDED_EN:
  602. switch (index) {
  603. case 0:
  604. voltage = 0;
  605. break;
  606. case 58:
  607. voltage = 2084 * 1000;
  608. break;
  609. case 59:
  610. voltage = 2315 * 1000;
  611. break;
  612. case 60:
  613. voltage = 2778 * 1000;
  614. break;
  615. case 61:
  616. voltage = 2932 * 1000;
  617. break;
  618. case 62:
  619. voltage = 3241 * 1000;
  620. break;
  621. default:
  622. voltage = (1852000 + (38600 * (index - 1)));
  623. }
  624. break;
  625. case SMPS_OFFSET_EN | SMPS_EXTENDED_EN:
  626. switch (index) {
  627. case 0:
  628. voltage = 0;
  629. break;
  630. case 58:
  631. voltage = 4167 * 1000;
  632. break;
  633. case 59:
  634. voltage = 2315 * 1000;
  635. break;
  636. case 60:
  637. voltage = 2778 * 1000;
  638. break;
  639. case 61:
  640. voltage = 2932 * 1000;
  641. break;
  642. case 62:
  643. voltage = 3241 * 1000;
  644. break;
  645. default:
  646. voltage = (2161000 + (38600 * (index - 1)));
  647. }
  648. break;
  649. }
  650. return voltage;
  651. }
  652. static int twl6030smps_map_voltage(struct regulator_dev *rdev, int min_uV,
  653. int max_uV)
  654. {
  655. struct twlreg_info *info = rdev_get_drvdata(rdev);
  656. int vsel = 0;
  657. switch (info->flags) {
  658. case 0:
  659. if (min_uV == 0)
  660. vsel = 0;
  661. else if ((min_uV >= 600000) && (min_uV <= 1300000)) {
  662. vsel = DIV_ROUND_UP(min_uV - 600000, 12500);
  663. vsel++;
  664. }
  665. /* Values 1..57 for vsel are linear and can be calculated
  666. * values 58..62 are non linear.
  667. */
  668. else if ((min_uV > 1900000) && (min_uV <= 2100000))
  669. vsel = 62;
  670. else if ((min_uV > 1800000) && (min_uV <= 1900000))
  671. vsel = 61;
  672. else if ((min_uV > 1500000) && (min_uV <= 1800000))
  673. vsel = 60;
  674. else if ((min_uV > 1350000) && (min_uV <= 1500000))
  675. vsel = 59;
  676. else if ((min_uV > 1300000) && (min_uV <= 1350000))
  677. vsel = 58;
  678. else
  679. return -EINVAL;
  680. break;
  681. case SMPS_OFFSET_EN:
  682. if (min_uV == 0)
  683. vsel = 0;
  684. else if ((min_uV >= 700000) && (min_uV <= 1420000)) {
  685. vsel = DIV_ROUND_UP(min_uV - 700000, 12500);
  686. vsel++;
  687. }
  688. /* Values 1..57 for vsel are linear and can be calculated
  689. * values 58..62 are non linear.
  690. */
  691. else if ((min_uV > 1900000) && (min_uV <= 2100000))
  692. vsel = 62;
  693. else if ((min_uV > 1800000) && (min_uV <= 1900000))
  694. vsel = 61;
  695. else if ((min_uV > 1350000) && (min_uV <= 1800000))
  696. vsel = 60;
  697. else if ((min_uV > 1350000) && (min_uV <= 1500000))
  698. vsel = 59;
  699. else if ((min_uV > 1300000) && (min_uV <= 1350000))
  700. vsel = 58;
  701. else
  702. return -EINVAL;
  703. break;
  704. case SMPS_EXTENDED_EN:
  705. if (min_uV == 0) {
  706. vsel = 0;
  707. } else if ((min_uV >= 1852000) && (max_uV <= 4013600)) {
  708. vsel = DIV_ROUND_UP(min_uV - 1852000, 38600);
  709. vsel++;
  710. }
  711. break;
  712. case SMPS_OFFSET_EN|SMPS_EXTENDED_EN:
  713. if (min_uV == 0) {
  714. vsel = 0;
  715. } else if ((min_uV >= 2161000) && (min_uV <= 4321000)) {
  716. vsel = DIV_ROUND_UP(min_uV - 2161000, 38600);
  717. vsel++;
  718. }
  719. break;
  720. }
  721. return vsel;
  722. }
  723. static int twl6030smps_set_voltage_sel(struct regulator_dev *rdev,
  724. unsigned int selector)
  725. {
  726. struct twlreg_info *info = rdev_get_drvdata(rdev);
  727. return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS,
  728. selector);
  729. }
  730. static int twl6030smps_get_voltage_sel(struct regulator_dev *rdev)
  731. {
  732. struct twlreg_info *info = rdev_get_drvdata(rdev);
  733. return twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS);
  734. }
  735. static struct regulator_ops twlsmps_ops = {
  736. .list_voltage = twl6030smps_list_voltage,
  737. .map_voltage = twl6030smps_map_voltage,
  738. .set_voltage_sel = twl6030smps_set_voltage_sel,
  739. .get_voltage_sel = twl6030smps_get_voltage_sel,
  740. .enable = twl6030reg_enable,
  741. .disable = twl6030reg_disable,
  742. .is_enabled = twl6030reg_is_enabled,
  743. .set_mode = twl6030reg_set_mode,
  744. .get_status = twl6030reg_get_status,
  745. };
  746. /*----------------------------------------------------------------------*/
  747. #define TWL4030_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
  748. remap_conf) \
  749. TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, \
  750. remap_conf, TWL4030, twl4030fixed_ops, \
  751. twl4030reg_map_mode)
  752. #define TWL6030_FIXED_LDO(label, offset, mVolts, turnon_delay) \
  753. TWL_FIXED_LDO(label, offset, mVolts, 0x0, turnon_delay, \
  754. 0x0, TWL6030, twl6030fixed_ops, NULL)
  755. #define TWL4030_ADJUSTABLE_LDO(label, offset, num, turnon_delay, remap_conf) \
  756. static const struct twlreg_info TWL4030_INFO_##label = { \
  757. .base = offset, \
  758. .id = num, \
  759. .table_len = ARRAY_SIZE(label##_VSEL_table), \
  760. .table = label##_VSEL_table, \
  761. .remap = remap_conf, \
  762. .desc = { \
  763. .name = #label, \
  764. .id = TWL4030_REG_##label, \
  765. .n_voltages = ARRAY_SIZE(label##_VSEL_table), \
  766. .ops = &twl4030ldo_ops, \
  767. .type = REGULATOR_VOLTAGE, \
  768. .owner = THIS_MODULE, \
  769. .enable_time = turnon_delay, \
  770. .of_map_mode = twl4030reg_map_mode, \
  771. }, \
  772. }
  773. #define TWL4030_ADJUSTABLE_SMPS(label, offset, num, turnon_delay, remap_conf) \
  774. static const struct twlreg_info TWL4030_INFO_##label = { \
  775. .base = offset, \
  776. .id = num, \
  777. .remap = remap_conf, \
  778. .desc = { \
  779. .name = #label, \
  780. .id = TWL4030_REG_##label, \
  781. .ops = &twl4030smps_ops, \
  782. .type = REGULATOR_VOLTAGE, \
  783. .owner = THIS_MODULE, \
  784. .enable_time = turnon_delay, \
  785. .of_map_mode = twl4030reg_map_mode, \
  786. }, \
  787. }
  788. #define TWL6030_ADJUSTABLE_SMPS(label) \
  789. static const struct twlreg_info TWL6030_INFO_##label = { \
  790. .desc = { \
  791. .name = #label, \
  792. .id = TWL6030_REG_##label, \
  793. .ops = &twl6030coresmps_ops, \
  794. .type = REGULATOR_VOLTAGE, \
  795. .owner = THIS_MODULE, \
  796. }, \
  797. }
  798. #define TWL6030_ADJUSTABLE_LDO(label, offset, min_mVolts, max_mVolts) \
  799. static const struct twlreg_info TWL6030_INFO_##label = { \
  800. .base = offset, \
  801. .min_mV = min_mVolts, \
  802. .max_mV = max_mVolts, \
  803. .desc = { \
  804. .name = #label, \
  805. .id = TWL6030_REG_##label, \
  806. .n_voltages = 32, \
  807. .ops = &twl6030ldo_ops, \
  808. .type = REGULATOR_VOLTAGE, \
  809. .owner = THIS_MODULE, \
  810. }, \
  811. }
  812. #define TWL6032_ADJUSTABLE_LDO(label, offset, min_mVolts, max_mVolts) \
  813. static const struct twlreg_info TWL6032_INFO_##label = { \
  814. .base = offset, \
  815. .min_mV = min_mVolts, \
  816. .max_mV = max_mVolts, \
  817. .desc = { \
  818. .name = #label, \
  819. .id = TWL6032_REG_##label, \
  820. .n_voltages = 32, \
  821. .ops = &twl6030ldo_ops, \
  822. .type = REGULATOR_VOLTAGE, \
  823. .owner = THIS_MODULE, \
  824. }, \
  825. }
  826. #define TWL_FIXED_LDO(label, offset, mVolts, num, turnon_delay, remap_conf, \
  827. family, operations, map_mode) \
  828. static const struct twlreg_info TWLFIXED_INFO_##label = { \
  829. .base = offset, \
  830. .id = num, \
  831. .min_mV = mVolts, \
  832. .remap = remap_conf, \
  833. .desc = { \
  834. .name = #label, \
  835. .id = family##_REG_##label, \
  836. .n_voltages = 1, \
  837. .ops = &operations, \
  838. .type = REGULATOR_VOLTAGE, \
  839. .owner = THIS_MODULE, \
  840. .min_uV = mVolts * 1000, \
  841. .enable_time = turnon_delay, \
  842. .of_map_mode = map_mode, \
  843. }, \
  844. }
  845. #define TWL6032_ADJUSTABLE_SMPS(label, offset) \
  846. static const struct twlreg_info TWLSMPS_INFO_##label = { \
  847. .base = offset, \
  848. .min_mV = 600, \
  849. .max_mV = 2100, \
  850. .desc = { \
  851. .name = #label, \
  852. .id = TWL6032_REG_##label, \
  853. .n_voltages = 63, \
  854. .ops = &twlsmps_ops, \
  855. .type = REGULATOR_VOLTAGE, \
  856. .owner = THIS_MODULE, \
  857. }, \
  858. }
  859. /*
  860. * We list regulators here if systems need some level of
  861. * software control over them after boot.
  862. */
  863. TWL4030_ADJUSTABLE_LDO(VAUX1, 0x17, 1, 100, 0x08);
  864. TWL4030_ADJUSTABLE_LDO(VAUX2_4030, 0x1b, 2, 100, 0x08);
  865. TWL4030_ADJUSTABLE_LDO(VAUX2, 0x1b, 2, 100, 0x08);
  866. TWL4030_ADJUSTABLE_LDO(VAUX3, 0x1f, 3, 100, 0x08);
  867. TWL4030_ADJUSTABLE_LDO(VAUX4, 0x23, 4, 100, 0x08);
  868. TWL4030_ADJUSTABLE_LDO(VMMC1, 0x27, 5, 100, 0x08);
  869. TWL4030_ADJUSTABLE_LDO(VMMC2, 0x2b, 6, 100, 0x08);
  870. TWL4030_ADJUSTABLE_LDO(VPLL1, 0x2f, 7, 100, 0x00);
  871. TWL4030_ADJUSTABLE_LDO(VPLL2, 0x33, 8, 100, 0x08);
  872. TWL4030_ADJUSTABLE_LDO(VSIM, 0x37, 9, 100, 0x00);
  873. TWL4030_ADJUSTABLE_LDO(VDAC, 0x3b, 10, 100, 0x08);
  874. TWL4030_ADJUSTABLE_LDO(VINTANA2, 0x43, 12, 100, 0x08);
  875. TWL4030_ADJUSTABLE_LDO(VIO, 0x4b, 14, 1000, 0x08);
  876. TWL4030_ADJUSTABLE_SMPS(VDD1, 0x55, 15, 1000, 0x08);
  877. TWL4030_ADJUSTABLE_SMPS(VDD2, 0x63, 16, 1000, 0x08);
  878. /* VUSBCP is managed *only* by the USB subchip */
  879. /* 6030 REG with base as PMC Slave Misc : 0x0030 */
  880. /* Turnon-delay and remap configuration values for 6030 are not
  881. verified since the specification is not public */
  882. TWL6030_ADJUSTABLE_SMPS(VDD1);
  883. TWL6030_ADJUSTABLE_SMPS(VDD2);
  884. TWL6030_ADJUSTABLE_SMPS(VDD3);
  885. TWL6030_ADJUSTABLE_LDO(VAUX1_6030, 0x54, 1000, 3300);
  886. TWL6030_ADJUSTABLE_LDO(VAUX2_6030, 0x58, 1000, 3300);
  887. TWL6030_ADJUSTABLE_LDO(VAUX3_6030, 0x5c, 1000, 3300);
  888. TWL6030_ADJUSTABLE_LDO(VMMC, 0x68, 1000, 3300);
  889. TWL6030_ADJUSTABLE_LDO(VPP, 0x6c, 1000, 3300);
  890. TWL6030_ADJUSTABLE_LDO(VUSIM, 0x74, 1000, 3300);
  891. /* 6025 are renamed compared to 6030 versions */
  892. TWL6032_ADJUSTABLE_LDO(LDO2, 0x54, 1000, 3300);
  893. TWL6032_ADJUSTABLE_LDO(LDO4, 0x58, 1000, 3300);
  894. TWL6032_ADJUSTABLE_LDO(LDO3, 0x5c, 1000, 3300);
  895. TWL6032_ADJUSTABLE_LDO(LDO5, 0x68, 1000, 3300);
  896. TWL6032_ADJUSTABLE_LDO(LDO1, 0x6c, 1000, 3300);
  897. TWL6032_ADJUSTABLE_LDO(LDO7, 0x74, 1000, 3300);
  898. TWL6032_ADJUSTABLE_LDO(LDO6, 0x60, 1000, 3300);
  899. TWL6032_ADJUSTABLE_LDO(LDOLN, 0x64, 1000, 3300);
  900. TWL6032_ADJUSTABLE_LDO(LDOUSB, 0x70, 1000, 3300);
  901. TWL4030_FIXED_LDO(VINTANA1, 0x3f, 1500, 11, 100, 0x08);
  902. TWL4030_FIXED_LDO(VINTDIG, 0x47, 1500, 13, 100, 0x08);
  903. TWL4030_FIXED_LDO(VUSB1V5, 0x71, 1500, 17, 100, 0x08);
  904. TWL4030_FIXED_LDO(VUSB1V8, 0x74, 1800, 18, 100, 0x08);
  905. TWL4030_FIXED_LDO(VUSB3V1, 0x77, 3100, 19, 150, 0x08);
  906. TWL6030_FIXED_LDO(VANA, 0x50, 2100, 0);
  907. TWL6030_FIXED_LDO(VCXIO, 0x60, 1800, 0);
  908. TWL6030_FIXED_LDO(VDAC, 0x64, 1800, 0);
  909. TWL6030_FIXED_LDO(VUSB, 0x70, 3300, 0);
  910. TWL6030_FIXED_LDO(V1V8, 0x16, 1800, 0);
  911. TWL6030_FIXED_LDO(V2V1, 0x1c, 2100, 0);
  912. TWL6032_ADJUSTABLE_SMPS(SMPS3, 0x34);
  913. TWL6032_ADJUSTABLE_SMPS(SMPS4, 0x10);
  914. TWL6032_ADJUSTABLE_SMPS(VIO, 0x16);
  915. static u8 twl_get_smps_offset(void)
  916. {
  917. u8 value;
  918. twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
  919. TWL6030_SMPS_OFFSET);
  920. return value;
  921. }
  922. static u8 twl_get_smps_mult(void)
  923. {
  924. u8 value;
  925. twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
  926. TWL6030_SMPS_MULT);
  927. return value;
  928. }
  929. #define TWL_OF_MATCH(comp, family, label) \
  930. { \
  931. .compatible = comp, \
  932. .data = &family##_INFO_##label, \
  933. }
  934. #define TWL4030_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL4030, label)
  935. #define TWL6030_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL6030, label)
  936. #define TWL6032_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL6032, label)
  937. #define TWLFIXED_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWLFIXED, label)
  938. #define TWLSMPS_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWLSMPS, label)
  939. static const struct of_device_id twl_of_match[] = {
  940. TWL4030_OF_MATCH("ti,twl4030-vaux1", VAUX1),
  941. TWL4030_OF_MATCH("ti,twl4030-vaux2", VAUX2_4030),
  942. TWL4030_OF_MATCH("ti,twl5030-vaux2", VAUX2),
  943. TWL4030_OF_MATCH("ti,twl4030-vaux3", VAUX3),
  944. TWL4030_OF_MATCH("ti,twl4030-vaux4", VAUX4),
  945. TWL4030_OF_MATCH("ti,twl4030-vmmc1", VMMC1),
  946. TWL4030_OF_MATCH("ti,twl4030-vmmc2", VMMC2),
  947. TWL4030_OF_MATCH("ti,twl4030-vpll1", VPLL1),
  948. TWL4030_OF_MATCH("ti,twl4030-vpll2", VPLL2),
  949. TWL4030_OF_MATCH("ti,twl4030-vsim", VSIM),
  950. TWL4030_OF_MATCH("ti,twl4030-vdac", VDAC),
  951. TWL4030_OF_MATCH("ti,twl4030-vintana2", VINTANA2),
  952. TWL4030_OF_MATCH("ti,twl4030-vio", VIO),
  953. TWL4030_OF_MATCH("ti,twl4030-vdd1", VDD1),
  954. TWL4030_OF_MATCH("ti,twl4030-vdd2", VDD2),
  955. TWL6030_OF_MATCH("ti,twl6030-vdd1", VDD1),
  956. TWL6030_OF_MATCH("ti,twl6030-vdd2", VDD2),
  957. TWL6030_OF_MATCH("ti,twl6030-vdd3", VDD3),
  958. TWL6030_OF_MATCH("ti,twl6030-vaux1", VAUX1_6030),
  959. TWL6030_OF_MATCH("ti,twl6030-vaux2", VAUX2_6030),
  960. TWL6030_OF_MATCH("ti,twl6030-vaux3", VAUX3_6030),
  961. TWL6030_OF_MATCH("ti,twl6030-vmmc", VMMC),
  962. TWL6030_OF_MATCH("ti,twl6030-vpp", VPP),
  963. TWL6030_OF_MATCH("ti,twl6030-vusim", VUSIM),
  964. TWL6032_OF_MATCH("ti,twl6032-ldo2", LDO2),
  965. TWL6032_OF_MATCH("ti,twl6032-ldo4", LDO4),
  966. TWL6032_OF_MATCH("ti,twl6032-ldo3", LDO3),
  967. TWL6032_OF_MATCH("ti,twl6032-ldo5", LDO5),
  968. TWL6032_OF_MATCH("ti,twl6032-ldo1", LDO1),
  969. TWL6032_OF_MATCH("ti,twl6032-ldo7", LDO7),
  970. TWL6032_OF_MATCH("ti,twl6032-ldo6", LDO6),
  971. TWL6032_OF_MATCH("ti,twl6032-ldoln", LDOLN),
  972. TWL6032_OF_MATCH("ti,twl6032-ldousb", LDOUSB),
  973. TWLFIXED_OF_MATCH("ti,twl4030-vintana1", VINTANA1),
  974. TWLFIXED_OF_MATCH("ti,twl4030-vintdig", VINTDIG),
  975. TWLFIXED_OF_MATCH("ti,twl4030-vusb1v5", VUSB1V5),
  976. TWLFIXED_OF_MATCH("ti,twl4030-vusb1v8", VUSB1V8),
  977. TWLFIXED_OF_MATCH("ti,twl4030-vusb3v1", VUSB3V1),
  978. TWLFIXED_OF_MATCH("ti,twl6030-vana", VANA),
  979. TWLFIXED_OF_MATCH("ti,twl6030-vcxio", VCXIO),
  980. TWLFIXED_OF_MATCH("ti,twl6030-vdac", VDAC),
  981. TWLFIXED_OF_MATCH("ti,twl6030-vusb", VUSB),
  982. TWLFIXED_OF_MATCH("ti,twl6030-v1v8", V1V8),
  983. TWLFIXED_OF_MATCH("ti,twl6030-v2v1", V2V1),
  984. TWLSMPS_OF_MATCH("ti,twl6032-smps3", SMPS3),
  985. TWLSMPS_OF_MATCH("ti,twl6032-smps4", SMPS4),
  986. TWLSMPS_OF_MATCH("ti,twl6032-vio", VIO),
  987. {},
  988. };
  989. MODULE_DEVICE_TABLE(of, twl_of_match);
  990. static int twlreg_probe(struct platform_device *pdev)
  991. {
  992. int i, id;
  993. struct twlreg_info *info;
  994. const struct twlreg_info *template;
  995. struct regulator_init_data *initdata;
  996. struct regulation_constraints *c;
  997. struct regulator_dev *rdev;
  998. struct twl_regulator_driver_data *drvdata;
  999. const struct of_device_id *match;
  1000. struct regulator_config config = { };
  1001. match = of_match_device(twl_of_match, &pdev->dev);
  1002. if (match) {
  1003. template = match->data;
  1004. id = template->desc.id;
  1005. initdata = of_get_regulator_init_data(&pdev->dev,
  1006. pdev->dev.of_node,
  1007. &template->desc);
  1008. drvdata = NULL;
  1009. } else {
  1010. id = pdev->id;
  1011. initdata = dev_get_platdata(&pdev->dev);
  1012. for (i = 0, template = NULL; i < ARRAY_SIZE(twl_of_match); i++) {
  1013. template = twl_of_match[i].data;
  1014. if (template && template->desc.id == id)
  1015. break;
  1016. }
  1017. if (i == ARRAY_SIZE(twl_of_match))
  1018. return -ENODEV;
  1019. drvdata = initdata->driver_data;
  1020. if (!drvdata)
  1021. return -EINVAL;
  1022. }
  1023. if (!template)
  1024. return -ENODEV;
  1025. if (!initdata)
  1026. return -EINVAL;
  1027. info = devm_kmemdup(&pdev->dev, template, sizeof(*info), GFP_KERNEL);
  1028. if (!info)
  1029. return -ENOMEM;
  1030. if (drvdata) {
  1031. /* copy the driver data into regulator data */
  1032. info->features = drvdata->features;
  1033. info->data = drvdata->data;
  1034. info->set_voltage = drvdata->set_voltage;
  1035. info->get_voltage = drvdata->get_voltage;
  1036. }
  1037. /* Constrain board-specific capabilities according to what
  1038. * this driver and the chip itself can actually do.
  1039. */
  1040. c = &initdata->constraints;
  1041. c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
  1042. c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
  1043. | REGULATOR_CHANGE_MODE
  1044. | REGULATOR_CHANGE_STATUS;
  1045. switch (id) {
  1046. case TWL4030_REG_VIO:
  1047. case TWL4030_REG_VDD1:
  1048. case TWL4030_REG_VDD2:
  1049. case TWL4030_REG_VPLL1:
  1050. case TWL4030_REG_VINTANA1:
  1051. case TWL4030_REG_VINTANA2:
  1052. case TWL4030_REG_VINTDIG:
  1053. c->always_on = true;
  1054. break;
  1055. default:
  1056. break;
  1057. }
  1058. switch (id) {
  1059. case TWL6032_REG_SMPS3:
  1060. if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS3)
  1061. info->flags |= SMPS_EXTENDED_EN;
  1062. if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS3)
  1063. info->flags |= SMPS_OFFSET_EN;
  1064. break;
  1065. case TWL6032_REG_SMPS4:
  1066. if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS4)
  1067. info->flags |= SMPS_EXTENDED_EN;
  1068. if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS4)
  1069. info->flags |= SMPS_OFFSET_EN;
  1070. break;
  1071. case TWL6032_REG_VIO:
  1072. if (twl_get_smps_mult() & SMPS_MULTOFFSET_VIO)
  1073. info->flags |= SMPS_EXTENDED_EN;
  1074. if (twl_get_smps_offset() & SMPS_MULTOFFSET_VIO)
  1075. info->flags |= SMPS_OFFSET_EN;
  1076. break;
  1077. }
  1078. config.dev = &pdev->dev;
  1079. config.init_data = initdata;
  1080. config.driver_data = info;
  1081. config.of_node = pdev->dev.of_node;
  1082. rdev = devm_regulator_register(&pdev->dev, &info->desc, &config);
  1083. if (IS_ERR(rdev)) {
  1084. dev_err(&pdev->dev, "can't register %s, %ld\n",
  1085. info->desc.name, PTR_ERR(rdev));
  1086. return PTR_ERR(rdev);
  1087. }
  1088. platform_set_drvdata(pdev, rdev);
  1089. if (twl_class_is_4030())
  1090. twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_REMAP,
  1091. info->remap);
  1092. /* NOTE: many regulators support short-circuit IRQs (presentable
  1093. * as REGULATOR_OVER_CURRENT notifications?) configured via:
  1094. * - SC_CONFIG
  1095. * - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
  1096. * - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
  1097. * - IT_CONFIG
  1098. */
  1099. return 0;
  1100. }
  1101. MODULE_ALIAS("platform:twl_reg");
  1102. static struct platform_driver twlreg_driver = {
  1103. .probe = twlreg_probe,
  1104. /* NOTE: short name, to work around driver model truncation of
  1105. * "twl_regulator.12" (and friends) to "twl_regulator.1".
  1106. */
  1107. .driver = {
  1108. .name = "twl_reg",
  1109. .of_match_table = of_match_ptr(twl_of_match),
  1110. },
  1111. };
  1112. static int __init twlreg_init(void)
  1113. {
  1114. return platform_driver_register(&twlreg_driver);
  1115. }
  1116. subsys_initcall(twlreg_init);
  1117. static void __exit twlreg_exit(void)
  1118. {
  1119. platform_driver_unregister(&twlreg_driver);
  1120. }
  1121. module_exit(twlreg_exit)
  1122. MODULE_DESCRIPTION("TWL regulator driver");
  1123. MODULE_LICENSE("GPL");