twl6030-regulator.c 20 KB

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