twl6030-regulator.c 20 KB

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