twl6030-regulator.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /*
  2. * Split TWL6030 logic from twl-regulator.c:
  3. * Copyright (C) 2008 David Brownell
  4. *
  5. * Copyright (C) 2016 Nicolae Rosia <nicolae.rosia@gmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/string.h>
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <linux/err.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/of.h>
  19. #include <linux/of_device.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/regulator/machine.h>
  22. #include <linux/regulator/of_regulator.h>
  23. #include <linux/mfd/twl.h>
  24. #include <linux/delay.h>
  25. struct twlreg_info {
  26. /* start of regulator's PM_RECEIVER control register bank */
  27. u8 base;
  28. /* twl resource ID, for resource control state machine */
  29. u8 id;
  30. /* chip constraints on regulator behavior */
  31. u16 min_mV;
  32. u8 flags;
  33. /* used by regulator core */
  34. struct regulator_desc desc;
  35. /* chip specific features */
  36. unsigned long features;
  37. /* data passed from board for external get/set voltage */
  38. void *data;
  39. };
  40. /* LDO control registers ... offset is from the base of its register bank.
  41. * The first three registers of all power resource banks help hardware to
  42. * manage the various resource groups.
  43. */
  44. /* Common offset in TWL4030/6030 */
  45. #define VREG_GRP 0
  46. /* TWL6030 register offsets */
  47. #define VREG_TRANS 1
  48. #define VREG_STATE 2
  49. #define VREG_VOLTAGE 3
  50. #define VREG_VOLTAGE_SMPS 4
  51. /* TWL6030 Misc register offsets */
  52. #define VREG_BC_ALL 1
  53. #define VREG_BC_REF 2
  54. #define VREG_BC_PROC 3
  55. #define VREG_BC_CLK_RST 4
  56. /* TWL6030 LDO register values for CFG_STATE */
  57. #define TWL6030_CFG_STATE_OFF 0x00
  58. #define TWL6030_CFG_STATE_ON 0x01
  59. #define TWL6030_CFG_STATE_OFF2 0x02
  60. #define TWL6030_CFG_STATE_SLEEP 0x03
  61. #define TWL6030_CFG_STATE_GRP_SHIFT 5
  62. #define TWL6030_CFG_STATE_APP_SHIFT 2
  63. #define TWL6030_CFG_STATE_APP_MASK (0x03 << TWL6030_CFG_STATE_APP_SHIFT)
  64. #define TWL6030_CFG_STATE_APP(v) (((v) & TWL6030_CFG_STATE_APP_MASK) >>\
  65. TWL6030_CFG_STATE_APP_SHIFT)
  66. /* Flags for SMPS Voltage reading */
  67. #define SMPS_OFFSET_EN BIT(0)
  68. #define SMPS_EXTENDED_EN BIT(1)
  69. /* twl6032 SMPS EPROM values */
  70. #define TWL6030_SMPS_OFFSET 0xB0
  71. #define TWL6030_SMPS_MULT 0xB3
  72. #define SMPS_MULTOFFSET_SMPS4 BIT(0)
  73. #define SMPS_MULTOFFSET_VIO BIT(1)
  74. #define SMPS_MULTOFFSET_SMPS3 BIT(6)
  75. static inline int
  76. twlreg_read(struct twlreg_info *info, unsigned slave_subgp, unsigned offset)
  77. {
  78. u8 value;
  79. int status;
  80. status = twl_i2c_read_u8(slave_subgp,
  81. &value, info->base + offset);
  82. return (status < 0) ? status : value;
  83. }
  84. static inline int
  85. twlreg_write(struct twlreg_info *info, unsigned slave_subgp, unsigned offset,
  86. u8 value)
  87. {
  88. return twl_i2c_write_u8(slave_subgp,
  89. value, info->base + offset);
  90. }
  91. /* generic power resource operations, which work on all regulators */
  92. static int twlreg_grp(struct regulator_dev *rdev)
  93. {
  94. return twlreg_read(rdev_get_drvdata(rdev), TWL_MODULE_PM_RECEIVER,
  95. VREG_GRP);
  96. }
  97. /*
  98. * Enable/disable regulators by joining/leaving the P1 (processor) group.
  99. * We assume nobody else is updating the DEV_GRP registers.
  100. */
  101. /* definition for 6030 family */
  102. #define P3_GRP_6030 BIT(2) /* secondary processor, modem, etc */
  103. #define P2_GRP_6030 BIT(1) /* "peripherals" */
  104. #define P1_GRP_6030 BIT(0) /* CPU/Linux */
  105. static int twl6030reg_is_enabled(struct regulator_dev *rdev)
  106. {
  107. struct twlreg_info *info = rdev_get_drvdata(rdev);
  108. int grp = 0, val;
  109. if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS))) {
  110. grp = twlreg_grp(rdev);
  111. if (grp < 0)
  112. return grp;
  113. grp &= P1_GRP_6030;
  114. } else {
  115. grp = 1;
  116. }
  117. val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
  118. val = TWL6030_CFG_STATE_APP(val);
  119. return grp && (val == TWL6030_CFG_STATE_ON);
  120. }
  121. #define PB_I2C_BUSY BIT(0)
  122. #define PB_I2C_BWEN BIT(1)
  123. static int twl6030reg_enable(struct regulator_dev *rdev)
  124. {
  125. struct twlreg_info *info = rdev_get_drvdata(rdev);
  126. int grp = 0;
  127. int ret;
  128. if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
  129. grp = twlreg_grp(rdev);
  130. if (grp < 0)
  131. return grp;
  132. ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
  133. grp << TWL6030_CFG_STATE_GRP_SHIFT |
  134. TWL6030_CFG_STATE_ON);
  135. return ret;
  136. }
  137. static int twl6030reg_disable(struct regulator_dev *rdev)
  138. {
  139. struct twlreg_info *info = rdev_get_drvdata(rdev);
  140. int grp = 0;
  141. int ret;
  142. if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
  143. grp = P1_GRP_6030 | P2_GRP_6030 | P3_GRP_6030;
  144. /* For 6030, set the off state for all grps enabled */
  145. ret = twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE,
  146. (grp) << TWL6030_CFG_STATE_GRP_SHIFT |
  147. TWL6030_CFG_STATE_OFF);
  148. return ret;
  149. }
  150. static int twl6030reg_get_status(struct regulator_dev *rdev)
  151. {
  152. struct twlreg_info *info = rdev_get_drvdata(rdev);
  153. int val;
  154. val = twlreg_grp(rdev);
  155. if (val < 0)
  156. return val;
  157. val = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_STATE);
  158. switch (TWL6030_CFG_STATE_APP(val)) {
  159. case TWL6030_CFG_STATE_ON:
  160. return REGULATOR_STATUS_NORMAL;
  161. case TWL6030_CFG_STATE_SLEEP:
  162. return REGULATOR_STATUS_STANDBY;
  163. case TWL6030_CFG_STATE_OFF:
  164. case TWL6030_CFG_STATE_OFF2:
  165. default:
  166. break;
  167. }
  168. return REGULATOR_STATUS_OFF;
  169. }
  170. static int twl6030reg_set_mode(struct regulator_dev *rdev, unsigned mode)
  171. {
  172. struct twlreg_info *info = rdev_get_drvdata(rdev);
  173. int grp = 0;
  174. int val;
  175. if (!(twl_class_is_6030() && (info->features & TWL6032_SUBCLASS)))
  176. grp = twlreg_grp(rdev);
  177. if (grp < 0)
  178. return grp;
  179. /* Compose the state register settings */
  180. val = grp << TWL6030_CFG_STATE_GRP_SHIFT;
  181. /* We can only set the mode through state machine commands... */
  182. switch (mode) {
  183. case REGULATOR_MODE_NORMAL:
  184. val |= TWL6030_CFG_STATE_ON;
  185. break;
  186. case REGULATOR_MODE_STANDBY:
  187. val |= TWL6030_CFG_STATE_SLEEP;
  188. break;
  189. default:
  190. return -EINVAL;
  191. }
  192. return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_STATE, val);
  193. }
  194. static int twl6030coresmps_set_voltage(struct regulator_dev *rdev, int min_uV,
  195. int max_uV, unsigned *selector)
  196. {
  197. return -ENODEV;
  198. }
  199. static int twl6030coresmps_get_voltage(struct regulator_dev *rdev)
  200. {
  201. return -ENODEV;
  202. }
  203. static struct regulator_ops twl6030coresmps_ops = {
  204. .set_voltage = twl6030coresmps_set_voltage,
  205. .get_voltage = twl6030coresmps_get_voltage,
  206. };
  207. static int twl6030ldo_list_voltage(struct regulator_dev *rdev, unsigned sel)
  208. {
  209. struct twlreg_info *info = rdev_get_drvdata(rdev);
  210. switch (sel) {
  211. case 0:
  212. return 0;
  213. case 1 ... 24:
  214. /* Linear mapping from 00000001 to 00011000:
  215. * Absolute voltage value = 1.0 V + 0.1 V × (sel – 00000001)
  216. */
  217. return (info->min_mV + 100 * (sel - 1)) * 1000;
  218. case 25 ... 30:
  219. return -EINVAL;
  220. case 31:
  221. return 2750000;
  222. default:
  223. return -EINVAL;
  224. }
  225. }
  226. static int
  227. twl6030ldo_set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
  228. {
  229. struct twlreg_info *info = rdev_get_drvdata(rdev);
  230. return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE,
  231. selector);
  232. }
  233. static int twl6030ldo_get_voltage_sel(struct regulator_dev *rdev)
  234. {
  235. struct twlreg_info *info = rdev_get_drvdata(rdev);
  236. int vsel = twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE);
  237. return vsel;
  238. }
  239. static struct regulator_ops twl6030ldo_ops = {
  240. .list_voltage = twl6030ldo_list_voltage,
  241. .set_voltage_sel = twl6030ldo_set_voltage_sel,
  242. .get_voltage_sel = twl6030ldo_get_voltage_sel,
  243. .enable = twl6030reg_enable,
  244. .disable = twl6030reg_disable,
  245. .is_enabled = twl6030reg_is_enabled,
  246. .set_mode = twl6030reg_set_mode,
  247. .get_status = twl6030reg_get_status,
  248. };
  249. static struct regulator_ops twl6030fixed_ops = {
  250. .list_voltage = regulator_list_voltage_linear,
  251. .enable = twl6030reg_enable,
  252. .disable = twl6030reg_disable,
  253. .is_enabled = twl6030reg_is_enabled,
  254. .set_mode = twl6030reg_set_mode,
  255. .get_status = twl6030reg_get_status,
  256. };
  257. /*
  258. * SMPS status and control
  259. */
  260. static int twl6030smps_list_voltage(struct regulator_dev *rdev, unsigned index)
  261. {
  262. struct twlreg_info *info = rdev_get_drvdata(rdev);
  263. int voltage = 0;
  264. switch (info->flags) {
  265. case SMPS_OFFSET_EN:
  266. voltage = 100000;
  267. /* fall through */
  268. case 0:
  269. switch (index) {
  270. case 0:
  271. voltage = 0;
  272. break;
  273. case 58:
  274. voltage = 1350 * 1000;
  275. break;
  276. case 59:
  277. voltage = 1500 * 1000;
  278. break;
  279. case 60:
  280. voltage = 1800 * 1000;
  281. break;
  282. case 61:
  283. voltage = 1900 * 1000;
  284. break;
  285. case 62:
  286. voltage = 2100 * 1000;
  287. break;
  288. default:
  289. voltage += (600000 + (12500 * (index - 1)));
  290. }
  291. break;
  292. case SMPS_EXTENDED_EN:
  293. switch (index) {
  294. case 0:
  295. voltage = 0;
  296. break;
  297. case 58:
  298. voltage = 2084 * 1000;
  299. break;
  300. case 59:
  301. voltage = 2315 * 1000;
  302. break;
  303. case 60:
  304. voltage = 2778 * 1000;
  305. break;
  306. case 61:
  307. voltage = 2932 * 1000;
  308. break;
  309. case 62:
  310. voltage = 3241 * 1000;
  311. break;
  312. default:
  313. voltage = (1852000 + (38600 * (index - 1)));
  314. }
  315. break;
  316. case SMPS_OFFSET_EN | SMPS_EXTENDED_EN:
  317. switch (index) {
  318. case 0:
  319. voltage = 0;
  320. break;
  321. case 58:
  322. voltage = 4167 * 1000;
  323. break;
  324. case 59:
  325. voltage = 2315 * 1000;
  326. break;
  327. case 60:
  328. voltage = 2778 * 1000;
  329. break;
  330. case 61:
  331. voltage = 2932 * 1000;
  332. break;
  333. case 62:
  334. voltage = 3241 * 1000;
  335. break;
  336. default:
  337. voltage = (2161000 + (38600 * (index - 1)));
  338. }
  339. break;
  340. }
  341. return voltage;
  342. }
  343. static int twl6030smps_map_voltage(struct regulator_dev *rdev, int min_uV,
  344. int max_uV)
  345. {
  346. struct twlreg_info *info = rdev_get_drvdata(rdev);
  347. int vsel = 0;
  348. switch (info->flags) {
  349. case 0:
  350. if (min_uV == 0)
  351. vsel = 0;
  352. else if ((min_uV >= 600000) && (min_uV <= 1300000)) {
  353. vsel = DIV_ROUND_UP(min_uV - 600000, 12500);
  354. vsel++;
  355. }
  356. /* Values 1..57 for vsel are linear and can be calculated
  357. * values 58..62 are non linear.
  358. */
  359. else if ((min_uV > 1900000) && (min_uV <= 2100000))
  360. vsel = 62;
  361. else if ((min_uV > 1800000) && (min_uV <= 1900000))
  362. vsel = 61;
  363. else if ((min_uV > 1500000) && (min_uV <= 1800000))
  364. vsel = 60;
  365. else if ((min_uV > 1350000) && (min_uV <= 1500000))
  366. vsel = 59;
  367. else if ((min_uV > 1300000) && (min_uV <= 1350000))
  368. vsel = 58;
  369. else
  370. return -EINVAL;
  371. break;
  372. case SMPS_OFFSET_EN:
  373. if (min_uV == 0)
  374. vsel = 0;
  375. else if ((min_uV >= 700000) && (min_uV <= 1420000)) {
  376. vsel = DIV_ROUND_UP(min_uV - 700000, 12500);
  377. vsel++;
  378. }
  379. /* Values 1..57 for vsel are linear and can be calculated
  380. * values 58..62 are non linear.
  381. */
  382. else if ((min_uV > 1900000) && (min_uV <= 2100000))
  383. vsel = 62;
  384. else if ((min_uV > 1800000) && (min_uV <= 1900000))
  385. vsel = 61;
  386. else if ((min_uV > 1500000) && (min_uV <= 1800000))
  387. vsel = 60;
  388. else if ((min_uV > 1350000) && (min_uV <= 1500000))
  389. vsel = 59;
  390. else
  391. return -EINVAL;
  392. break;
  393. case SMPS_EXTENDED_EN:
  394. if (min_uV == 0) {
  395. vsel = 0;
  396. } else if ((min_uV >= 1852000) && (max_uV <= 4013600)) {
  397. vsel = DIV_ROUND_UP(min_uV - 1852000, 38600);
  398. vsel++;
  399. }
  400. break;
  401. case SMPS_OFFSET_EN|SMPS_EXTENDED_EN:
  402. if (min_uV == 0) {
  403. vsel = 0;
  404. } else if ((min_uV >= 2161000) && (min_uV <= 4321000)) {
  405. vsel = DIV_ROUND_UP(min_uV - 2161000, 38600);
  406. vsel++;
  407. }
  408. break;
  409. }
  410. return vsel;
  411. }
  412. static int twl6030smps_set_voltage_sel(struct regulator_dev *rdev,
  413. unsigned int selector)
  414. {
  415. struct twlreg_info *info = rdev_get_drvdata(rdev);
  416. return twlreg_write(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS,
  417. selector);
  418. }
  419. static int twl6030smps_get_voltage_sel(struct regulator_dev *rdev)
  420. {
  421. struct twlreg_info *info = rdev_get_drvdata(rdev);
  422. return twlreg_read(info, TWL_MODULE_PM_RECEIVER, VREG_VOLTAGE_SMPS);
  423. }
  424. static struct regulator_ops twlsmps_ops = {
  425. .list_voltage = twl6030smps_list_voltage,
  426. .map_voltage = twl6030smps_map_voltage,
  427. .set_voltage_sel = twl6030smps_set_voltage_sel,
  428. .get_voltage_sel = twl6030smps_get_voltage_sel,
  429. .enable = twl6030reg_enable,
  430. .disable = twl6030reg_disable,
  431. .is_enabled = twl6030reg_is_enabled,
  432. .set_mode = twl6030reg_set_mode,
  433. .get_status = twl6030reg_get_status,
  434. };
  435. /*----------------------------------------------------------------------*/
  436. #define TWL6030_ADJUSTABLE_SMPS(label) \
  437. static const struct twlreg_info TWL6030_INFO_##label = { \
  438. .desc = { \
  439. .name = #label, \
  440. .id = TWL6030_REG_##label, \
  441. .ops = &twl6030coresmps_ops, \
  442. .type = REGULATOR_VOLTAGE, \
  443. .owner = THIS_MODULE, \
  444. }, \
  445. }
  446. #define TWL6030_ADJUSTABLE_LDO(label, offset, min_mVolts) \
  447. static const struct twlreg_info TWL6030_INFO_##label = { \
  448. .base = offset, \
  449. .min_mV = min_mVolts, \
  450. .desc = { \
  451. .name = #label, \
  452. .id = TWL6030_REG_##label, \
  453. .n_voltages = 32, \
  454. .ops = &twl6030ldo_ops, \
  455. .type = REGULATOR_VOLTAGE, \
  456. .owner = THIS_MODULE, \
  457. }, \
  458. }
  459. #define TWL6032_ADJUSTABLE_LDO(label, offset, min_mVolts) \
  460. static const struct twlreg_info TWL6032_INFO_##label = { \
  461. .base = offset, \
  462. .min_mV = min_mVolts, \
  463. .desc = { \
  464. .name = #label, \
  465. .id = TWL6032_REG_##label, \
  466. .n_voltages = 32, \
  467. .ops = &twl6030ldo_ops, \
  468. .type = REGULATOR_VOLTAGE, \
  469. .owner = THIS_MODULE, \
  470. }, \
  471. }
  472. #define TWL6030_FIXED_LDO(label, offset, mVolts, turnon_delay) \
  473. static const struct twlreg_info TWLFIXED_INFO_##label = { \
  474. .base = offset, \
  475. .id = 0, \
  476. .min_mV = mVolts, \
  477. .desc = { \
  478. .name = #label, \
  479. .id = TWL6030##_REG_##label, \
  480. .n_voltages = 1, \
  481. .ops = &twl6030fixed_ops, \
  482. .type = REGULATOR_VOLTAGE, \
  483. .owner = THIS_MODULE, \
  484. .min_uV = mVolts * 1000, \
  485. .enable_time = turnon_delay, \
  486. .of_map_mode = NULL, \
  487. }, \
  488. }
  489. #define TWL6032_ADJUSTABLE_SMPS(label, offset) \
  490. static const struct twlreg_info TWLSMPS_INFO_##label = { \
  491. .base = offset, \
  492. .min_mV = 600, \
  493. .desc = { \
  494. .name = #label, \
  495. .id = TWL6032_REG_##label, \
  496. .n_voltages = 63, \
  497. .ops = &twlsmps_ops, \
  498. .type = REGULATOR_VOLTAGE, \
  499. .owner = THIS_MODULE, \
  500. }, \
  501. }
  502. /* VUSBCP is managed *only* by the USB subchip */
  503. /* 6030 REG with base as PMC Slave Misc : 0x0030 */
  504. /* Turnon-delay and remap configuration values for 6030 are not
  505. verified since the specification is not public */
  506. TWL6030_ADJUSTABLE_SMPS(VDD1);
  507. TWL6030_ADJUSTABLE_SMPS(VDD2);
  508. TWL6030_ADJUSTABLE_SMPS(VDD3);
  509. TWL6030_ADJUSTABLE_LDO(VAUX1_6030, 0x54, 1000);
  510. TWL6030_ADJUSTABLE_LDO(VAUX2_6030, 0x58, 1000);
  511. TWL6030_ADJUSTABLE_LDO(VAUX3_6030, 0x5c, 1000);
  512. TWL6030_ADJUSTABLE_LDO(VMMC, 0x68, 1000);
  513. TWL6030_ADJUSTABLE_LDO(VPP, 0x6c, 1000);
  514. TWL6030_ADJUSTABLE_LDO(VUSIM, 0x74, 1000);
  515. /* 6025 are renamed compared to 6030 versions */
  516. TWL6032_ADJUSTABLE_LDO(LDO2, 0x54, 1000);
  517. TWL6032_ADJUSTABLE_LDO(LDO4, 0x58, 1000);
  518. TWL6032_ADJUSTABLE_LDO(LDO3, 0x5c, 1000);
  519. TWL6032_ADJUSTABLE_LDO(LDO5, 0x68, 1000);
  520. TWL6032_ADJUSTABLE_LDO(LDO1, 0x6c, 1000);
  521. TWL6032_ADJUSTABLE_LDO(LDO7, 0x74, 1000);
  522. TWL6032_ADJUSTABLE_LDO(LDO6, 0x60, 1000);
  523. TWL6032_ADJUSTABLE_LDO(LDOLN, 0x64, 1000);
  524. TWL6032_ADJUSTABLE_LDO(LDOUSB, 0x70, 1000);
  525. TWL6030_FIXED_LDO(VANA, 0x50, 2100, 0);
  526. TWL6030_FIXED_LDO(VCXIO, 0x60, 1800, 0);
  527. TWL6030_FIXED_LDO(VDAC, 0x64, 1800, 0);
  528. TWL6030_FIXED_LDO(VUSB, 0x70, 3300, 0);
  529. TWL6030_FIXED_LDO(V1V8, 0x16, 1800, 0);
  530. TWL6030_FIXED_LDO(V2V1, 0x1c, 2100, 0);
  531. TWL6032_ADJUSTABLE_SMPS(SMPS3, 0x34);
  532. TWL6032_ADJUSTABLE_SMPS(SMPS4, 0x10);
  533. TWL6032_ADJUSTABLE_SMPS(VIO, 0x16);
  534. static u8 twl_get_smps_offset(void)
  535. {
  536. u8 value;
  537. twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
  538. TWL6030_SMPS_OFFSET);
  539. return value;
  540. }
  541. static u8 twl_get_smps_mult(void)
  542. {
  543. u8 value;
  544. twl_i2c_read_u8(TWL_MODULE_PM_RECEIVER, &value,
  545. TWL6030_SMPS_MULT);
  546. return value;
  547. }
  548. #define TWL_OF_MATCH(comp, family, label) \
  549. { \
  550. .compatible = comp, \
  551. .data = &family##_INFO_##label, \
  552. }
  553. #define TWL6030_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL6030, label)
  554. #define TWL6032_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWL6032, label)
  555. #define TWLFIXED_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWLFIXED, label)
  556. #define TWLSMPS_OF_MATCH(comp, label) TWL_OF_MATCH(comp, TWLSMPS, label)
  557. static const struct of_device_id twl_of_match[] = {
  558. TWL6030_OF_MATCH("ti,twl6030-vdd1", VDD1),
  559. TWL6030_OF_MATCH("ti,twl6030-vdd2", VDD2),
  560. TWL6030_OF_MATCH("ti,twl6030-vdd3", VDD3),
  561. TWL6030_OF_MATCH("ti,twl6030-vaux1", VAUX1_6030),
  562. TWL6030_OF_MATCH("ti,twl6030-vaux2", VAUX2_6030),
  563. TWL6030_OF_MATCH("ti,twl6030-vaux3", VAUX3_6030),
  564. TWL6030_OF_MATCH("ti,twl6030-vmmc", VMMC),
  565. TWL6030_OF_MATCH("ti,twl6030-vpp", VPP),
  566. TWL6030_OF_MATCH("ti,twl6030-vusim", VUSIM),
  567. TWL6032_OF_MATCH("ti,twl6032-ldo2", LDO2),
  568. TWL6032_OF_MATCH("ti,twl6032-ldo4", LDO4),
  569. TWL6032_OF_MATCH("ti,twl6032-ldo3", LDO3),
  570. TWL6032_OF_MATCH("ti,twl6032-ldo5", LDO5),
  571. TWL6032_OF_MATCH("ti,twl6032-ldo1", LDO1),
  572. TWL6032_OF_MATCH("ti,twl6032-ldo7", LDO7),
  573. TWL6032_OF_MATCH("ti,twl6032-ldo6", LDO6),
  574. TWL6032_OF_MATCH("ti,twl6032-ldoln", LDOLN),
  575. TWL6032_OF_MATCH("ti,twl6032-ldousb", LDOUSB),
  576. TWLFIXED_OF_MATCH("ti,twl6030-vana", VANA),
  577. TWLFIXED_OF_MATCH("ti,twl6030-vcxio", VCXIO),
  578. TWLFIXED_OF_MATCH("ti,twl6030-vdac", VDAC),
  579. TWLFIXED_OF_MATCH("ti,twl6030-vusb", VUSB),
  580. TWLFIXED_OF_MATCH("ti,twl6030-v1v8", V1V8),
  581. TWLFIXED_OF_MATCH("ti,twl6030-v2v1", V2V1),
  582. TWLSMPS_OF_MATCH("ti,twl6032-smps3", SMPS3),
  583. TWLSMPS_OF_MATCH("ti,twl6032-smps4", SMPS4),
  584. TWLSMPS_OF_MATCH("ti,twl6032-vio", VIO),
  585. {},
  586. };
  587. MODULE_DEVICE_TABLE(of, twl_of_match);
  588. static int twlreg_probe(struct platform_device *pdev)
  589. {
  590. int id;
  591. struct twlreg_info *info;
  592. const struct twlreg_info *template;
  593. struct regulator_init_data *initdata;
  594. struct regulation_constraints *c;
  595. struct regulator_dev *rdev;
  596. const struct of_device_id *match;
  597. struct regulator_config config = { };
  598. match = of_match_device(twl_of_match, &pdev->dev);
  599. if (!match)
  600. return -ENODEV;
  601. template = match->data;
  602. if (!template)
  603. return -ENODEV;
  604. id = template->desc.id;
  605. initdata = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node,
  606. &template->desc);
  607. if (!initdata)
  608. return -EINVAL;
  609. info = devm_kmemdup(&pdev->dev, template, sizeof(*info), GFP_KERNEL);
  610. if (!info)
  611. return -ENOMEM;
  612. /* Constrain board-specific capabilities according to what
  613. * this driver and the chip itself can actually do.
  614. */
  615. c = &initdata->constraints;
  616. c->valid_modes_mask &= REGULATOR_MODE_NORMAL | REGULATOR_MODE_STANDBY;
  617. c->valid_ops_mask &= REGULATOR_CHANGE_VOLTAGE
  618. | REGULATOR_CHANGE_MODE
  619. | REGULATOR_CHANGE_STATUS;
  620. switch (id) {
  621. case TWL6032_REG_SMPS3:
  622. if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS3)
  623. info->flags |= SMPS_EXTENDED_EN;
  624. if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS3)
  625. info->flags |= SMPS_OFFSET_EN;
  626. break;
  627. case TWL6032_REG_SMPS4:
  628. if (twl_get_smps_mult() & SMPS_MULTOFFSET_SMPS4)
  629. info->flags |= SMPS_EXTENDED_EN;
  630. if (twl_get_smps_offset() & SMPS_MULTOFFSET_SMPS4)
  631. info->flags |= SMPS_OFFSET_EN;
  632. break;
  633. case TWL6032_REG_VIO:
  634. if (twl_get_smps_mult() & SMPS_MULTOFFSET_VIO)
  635. info->flags |= SMPS_EXTENDED_EN;
  636. if (twl_get_smps_offset() & SMPS_MULTOFFSET_VIO)
  637. info->flags |= SMPS_OFFSET_EN;
  638. break;
  639. }
  640. config.dev = &pdev->dev;
  641. config.init_data = initdata;
  642. config.driver_data = info;
  643. config.of_node = pdev->dev.of_node;
  644. rdev = devm_regulator_register(&pdev->dev, &info->desc, &config);
  645. if (IS_ERR(rdev)) {
  646. dev_err(&pdev->dev, "can't register %s, %ld\n",
  647. info->desc.name, PTR_ERR(rdev));
  648. return PTR_ERR(rdev);
  649. }
  650. platform_set_drvdata(pdev, rdev);
  651. /* NOTE: many regulators support short-circuit IRQs (presentable
  652. * as REGULATOR_OVER_CURRENT notifications?) configured via:
  653. * - SC_CONFIG
  654. * - SC_DETECT1 (vintana2, vmmc1/2, vaux1/2/3/4)
  655. * - SC_DETECT2 (vusb, vdac, vio, vdd1/2, vpll2)
  656. * - IT_CONFIG
  657. */
  658. return 0;
  659. }
  660. MODULE_ALIAS("platform:twl6030_reg");
  661. static struct platform_driver twlreg_driver = {
  662. .probe = twlreg_probe,
  663. /* NOTE: short name, to work around driver model truncation of
  664. * "twl_regulator.12" (and friends) to "twl_regulator.1".
  665. */
  666. .driver = {
  667. .name = "twl6030_reg",
  668. .of_match_table = of_match_ptr(twl_of_match),
  669. },
  670. };
  671. static int __init twlreg_init(void)
  672. {
  673. return platform_driver_register(&twlreg_driver);
  674. }
  675. subsys_initcall(twlreg_init);
  676. static void __exit twlreg_exit(void)
  677. {
  678. platform_driver_unregister(&twlreg_driver);
  679. }
  680. module_exit(twlreg_exit)
  681. MODULE_DESCRIPTION("TWL6030 regulator driver");
  682. MODULE_LICENSE("GPL");