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