tps6586x-regulator.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * Regulator driver for TI TPS6586x
  3. *
  4. * Copyright (C) 2010 Compulab Ltd.
  5. * Author: Mike Rapoport <mike@compulab.co.il>
  6. *
  7. * Based on da903x
  8. * Copyright (C) 2006-2008 Marvell International Ltd.
  9. * Copyright (C) 2008 Compulab Ltd.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/err.h>
  19. #include <linux/slab.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/regulator/driver.h>
  22. #include <linux/regulator/machine.h>
  23. #include <linux/mfd/tps6586x.h>
  24. /* supply control and voltage setting */
  25. #define TPS6586X_SUPPLYENA 0x10
  26. #define TPS6586X_SUPPLYENB 0x11
  27. #define TPS6586X_SUPPLYENC 0x12
  28. #define TPS6586X_SUPPLYEND 0x13
  29. #define TPS6586X_SUPPLYENE 0x14
  30. #define TPS6586X_VCC1 0x20
  31. #define TPS6586X_VCC2 0x21
  32. #define TPS6586X_SM1V1 0x23
  33. #define TPS6586X_SM1V2 0x24
  34. #define TPS6586X_SM1SL 0x25
  35. #define TPS6586X_SM0V1 0x26
  36. #define TPS6586X_SM0V2 0x27
  37. #define TPS6586X_SM0SL 0x28
  38. #define TPS6586X_LDO2AV1 0x29
  39. #define TPS6586X_LDO2AV2 0x2A
  40. #define TPS6586X_LDO2BV1 0x2F
  41. #define TPS6586X_LDO2BV2 0x30
  42. #define TPS6586X_LDO4V1 0x32
  43. #define TPS6586X_LDO4V2 0x33
  44. /* converter settings */
  45. #define TPS6586X_SUPPLYV1 0x41
  46. #define TPS6586X_SUPPLYV2 0x42
  47. #define TPS6586X_SUPPLYV3 0x43
  48. #define TPS6586X_SUPPLYV4 0x44
  49. #define TPS6586X_SUPPLYV5 0x45
  50. #define TPS6586X_SUPPLYV6 0x46
  51. #define TPS6586X_SMODE1 0x47
  52. #define TPS6586X_SMODE2 0x48
  53. struct tps6586x_regulator {
  54. struct regulator_desc desc;
  55. int volt_reg;
  56. int volt_shift;
  57. int volt_nbits;
  58. int enable_bit[2];
  59. int enable_reg[2];
  60. int *voltages;
  61. /* for DVM regulators */
  62. int go_reg;
  63. int go_bit;
  64. };
  65. static inline struct device *to_tps6586x_dev(struct regulator_dev *rdev)
  66. {
  67. return rdev_get_dev(rdev)->parent->parent;
  68. }
  69. static int tps6586x_ldo_list_voltage(struct regulator_dev *rdev,
  70. unsigned selector)
  71. {
  72. struct tps6586x_regulator *info = rdev_get_drvdata(rdev);
  73. int rid = rdev_get_id(rdev);
  74. /* LDO0 has minimal voltage 1.2V rather than 1.25V */
  75. if ((rid == TPS6586X_ID_LDO_0) && (selector == 0))
  76. return (info->voltages[0] - 50) * 1000;
  77. return info->voltages[selector] * 1000;
  78. }
  79. static int __tps6586x_ldo_set_voltage(struct device *parent,
  80. struct tps6586x_regulator *ri,
  81. int min_uV, int max_uV,
  82. unsigned *selector)
  83. {
  84. int val, uV;
  85. uint8_t mask;
  86. for (val = 0; val < ri->desc.n_voltages; val++) {
  87. uV = ri->voltages[val] * 1000;
  88. /* LDO0 has minimal voltage 1.2 rather than 1.25 */
  89. if (ri->desc.id == TPS6586X_ID_LDO_0 && val == 0)
  90. uV -= 50 * 1000;
  91. /* use the first in-range value */
  92. if (min_uV <= uV && uV <= max_uV) {
  93. *selector = val;
  94. val <<= ri->volt_shift;
  95. mask = ((1 << ri->volt_nbits) - 1) << ri->volt_shift;
  96. return tps6586x_update(parent, ri->volt_reg, val, mask);
  97. }
  98. }
  99. return -EINVAL;
  100. }
  101. static int tps6586x_ldo_set_voltage(struct regulator_dev *rdev,
  102. int min_uV, int max_uV, unsigned *selector)
  103. {
  104. struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
  105. struct device *parent = to_tps6586x_dev(rdev);
  106. return __tps6586x_ldo_set_voltage(parent, ri, min_uV, max_uV,
  107. selector);
  108. }
  109. static int tps6586x_ldo_get_voltage(struct regulator_dev *rdev)
  110. {
  111. struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
  112. struct device *parent = to_tps6586x_dev(rdev);
  113. uint8_t val, mask;
  114. int ret;
  115. ret = tps6586x_read(parent, ri->volt_reg, &val);
  116. if (ret)
  117. return ret;
  118. mask = ((1 << ri->volt_nbits) - 1) << ri->volt_shift;
  119. val = (val & mask) >> ri->volt_shift;
  120. if (val >= ri->desc.n_voltages)
  121. BUG();
  122. return ri->voltages[val] * 1000;
  123. }
  124. static int tps6586x_dvm_set_voltage(struct regulator_dev *rdev,
  125. int min_uV, int max_uV, unsigned *selector)
  126. {
  127. struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
  128. struct device *parent = to_tps6586x_dev(rdev);
  129. int ret;
  130. ret = __tps6586x_ldo_set_voltage(parent, ri, min_uV, max_uV,
  131. selector);
  132. if (ret)
  133. return ret;
  134. return tps6586x_set_bits(parent, ri->go_reg, 1 << ri->go_bit);
  135. }
  136. static int tps6586x_regulator_enable(struct regulator_dev *rdev)
  137. {
  138. struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
  139. struct device *parent = to_tps6586x_dev(rdev);
  140. return tps6586x_set_bits(parent, ri->enable_reg[0],
  141. 1 << ri->enable_bit[0]);
  142. }
  143. static int tps6586x_regulator_disable(struct regulator_dev *rdev)
  144. {
  145. struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
  146. struct device *parent = to_tps6586x_dev(rdev);
  147. return tps6586x_clr_bits(parent, ri->enable_reg[0],
  148. 1 << ri->enable_bit[0]);
  149. }
  150. static int tps6586x_regulator_is_enabled(struct regulator_dev *rdev)
  151. {
  152. struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
  153. struct device *parent = to_tps6586x_dev(rdev);
  154. uint8_t reg_val;
  155. int ret;
  156. ret = tps6586x_read(parent, ri->enable_reg[0], &reg_val);
  157. if (ret)
  158. return ret;
  159. return !!(reg_val & (1 << ri->enable_bit[0]));
  160. }
  161. static struct regulator_ops tps6586x_regulator_ldo_ops = {
  162. .list_voltage = tps6586x_ldo_list_voltage,
  163. .get_voltage = tps6586x_ldo_get_voltage,
  164. .set_voltage = tps6586x_ldo_set_voltage,
  165. .is_enabled = tps6586x_regulator_is_enabled,
  166. .enable = tps6586x_regulator_enable,
  167. .disable = tps6586x_regulator_disable,
  168. };
  169. static struct regulator_ops tps6586x_regulator_dvm_ops = {
  170. .list_voltage = tps6586x_ldo_list_voltage,
  171. .get_voltage = tps6586x_ldo_get_voltage,
  172. .set_voltage = tps6586x_dvm_set_voltage,
  173. .is_enabled = tps6586x_regulator_is_enabled,
  174. .enable = tps6586x_regulator_enable,
  175. .disable = tps6586x_regulator_disable,
  176. };
  177. static int tps6586x_ldo_voltages[] = {
  178. 1250, 1500, 1800, 2500, 2700, 2850, 3100, 3300,
  179. };
  180. static int tps6586x_ldo4_voltages[] = {
  181. 1700, 1725, 1750, 1775, 1800, 1825, 1850, 1875,
  182. 1900, 1925, 1950, 1975, 2000, 2025, 2050, 2075,
  183. 2100, 2125, 2150, 2175, 2200, 2225, 2250, 2275,
  184. 2300, 2325, 2350, 2375, 2400, 2425, 2450, 2475,
  185. };
  186. static int tps6586x_sm2_voltages[] = {
  187. 3000, 3050, 3100, 3150, 3200, 3250, 3300, 3350,
  188. 3400, 3450, 3500, 3550, 3600, 3650, 3700, 3750,
  189. 3800, 3850, 3900, 3950, 4000, 4050, 4100, 4150,
  190. 4200, 4250, 4300, 4350, 4400, 4450, 4500, 4550,
  191. };
  192. static int tps6586x_dvm_voltages[] = {
  193. 725, 750, 775, 800, 825, 850, 875, 900,
  194. 925, 950, 975, 1000, 1025, 1050, 1075, 1100,
  195. 1125, 1150, 1175, 1200, 1225, 1250, 1275, 1300,
  196. 1325, 1350, 1375, 1400, 1425, 1450, 1475, 1500,
  197. };
  198. #define TPS6586X_REGULATOR(_id, vdata, _ops, vreg, shift, nbits, \
  199. ereg0, ebit0, ereg1, ebit1) \
  200. .desc = { \
  201. .name = "REG-" #_id, \
  202. .ops = &tps6586x_regulator_##_ops, \
  203. .type = REGULATOR_VOLTAGE, \
  204. .id = TPS6586X_ID_##_id, \
  205. .n_voltages = ARRAY_SIZE(tps6586x_##vdata##_voltages), \
  206. .owner = THIS_MODULE, \
  207. }, \
  208. .volt_reg = TPS6586X_##vreg, \
  209. .volt_shift = (shift), \
  210. .volt_nbits = (nbits), \
  211. .enable_reg[0] = TPS6586X_SUPPLY##ereg0, \
  212. .enable_bit[0] = (ebit0), \
  213. .enable_reg[1] = TPS6586X_SUPPLY##ereg1, \
  214. .enable_bit[1] = (ebit1), \
  215. .voltages = tps6586x_##vdata##_voltages,
  216. #define TPS6586X_REGULATOR_DVM_GOREG(goreg, gobit) \
  217. .go_reg = TPS6586X_##goreg, \
  218. .go_bit = (gobit),
  219. #define TPS6586X_LDO(_id, vdata, vreg, shift, nbits, \
  220. ereg0, ebit0, ereg1, ebit1) \
  221. { \
  222. TPS6586X_REGULATOR(_id, vdata, ldo_ops, vreg, shift, nbits, \
  223. ereg0, ebit0, ereg1, ebit1) \
  224. }
  225. #define TPS6586X_DVM(_id, vdata, vreg, shift, nbits, \
  226. ereg0, ebit0, ereg1, ebit1, goreg, gobit) \
  227. { \
  228. TPS6586X_REGULATOR(_id, vdata, dvm_ops, vreg, shift, nbits, \
  229. ereg0, ebit0, ereg1, ebit1) \
  230. TPS6586X_REGULATOR_DVM_GOREG(goreg, gobit) \
  231. }
  232. static struct tps6586x_regulator tps6586x_regulator[] = {
  233. TPS6586X_LDO(LDO_0, ldo, SUPPLYV1, 5, 3, ENC, 0, END, 0),
  234. TPS6586X_LDO(LDO_3, ldo, SUPPLYV4, 0, 3, ENC, 2, END, 2),
  235. TPS6586X_LDO(LDO_5, ldo, SUPPLYV6, 0, 3, ENE, 6, ENE, 6),
  236. TPS6586X_LDO(LDO_6, ldo, SUPPLYV3, 0, 3, ENC, 4, END, 4),
  237. TPS6586X_LDO(LDO_7, ldo, SUPPLYV3, 3, 3, ENC, 5, END, 5),
  238. TPS6586X_LDO(LDO_8, ldo, SUPPLYV2, 5, 3, ENC, 6, END, 6),
  239. TPS6586X_LDO(LDO_9, ldo, SUPPLYV6, 3, 3, ENE, 7, ENE, 7),
  240. TPS6586X_LDO(LDO_RTC, ldo, SUPPLYV4, 3, 3, V4, 7, V4, 7),
  241. TPS6586X_LDO(LDO_1, dvm, SUPPLYV1, 0, 5, ENC, 1, END, 1),
  242. TPS6586X_LDO(SM_2, sm2, SUPPLYV2, 0, 5, ENC, 7, END, 7),
  243. TPS6586X_DVM(LDO_2, dvm, LDO2BV1, 0, 5, ENA, 3, ENB, 3, VCC2, 6),
  244. TPS6586X_DVM(LDO_4, ldo4, LDO4V1, 0, 5, ENC, 3, END, 3, VCC1, 6),
  245. TPS6586X_DVM(SM_0, dvm, SM0V1, 0, 5, ENA, 1, ENB, 1, VCC1, 2),
  246. TPS6586X_DVM(SM_1, dvm, SM1V1, 0, 5, ENA, 0, ENB, 0, VCC1, 0),
  247. };
  248. /*
  249. * TPS6586X has 2 enable bits that are OR'ed to determine the actual
  250. * regulator state. Clearing one of this bits allows switching
  251. * regulator on and of with single register write.
  252. */
  253. static inline int tps6586x_regulator_preinit(struct device *parent,
  254. struct tps6586x_regulator *ri)
  255. {
  256. uint8_t val1, val2;
  257. int ret;
  258. if (ri->enable_reg[0] == ri->enable_reg[1] &&
  259. ri->enable_bit[0] == ri->enable_bit[1])
  260. return 0;
  261. ret = tps6586x_read(parent, ri->enable_reg[0], &val1);
  262. if (ret)
  263. return ret;
  264. ret = tps6586x_read(parent, ri->enable_reg[1], &val2);
  265. if (ret)
  266. return ret;
  267. if (!(val2 & (1 << ri->enable_bit[1])))
  268. return 0;
  269. /*
  270. * The regulator is on, but it's enabled with the bit we don't
  271. * want to use, so we switch the enable bits
  272. */
  273. if (!(val1 & (1 << ri->enable_bit[0]))) {
  274. ret = tps6586x_set_bits(parent, ri->enable_reg[0],
  275. 1 << ri->enable_bit[0]);
  276. if (ret)
  277. return ret;
  278. }
  279. return tps6586x_clr_bits(parent, ri->enable_reg[1],
  280. 1 << ri->enable_bit[1]);
  281. }
  282. static int tps6586x_regulator_set_slew_rate(struct platform_device *pdev)
  283. {
  284. struct device *parent = pdev->dev.parent;
  285. struct regulator_init_data *p = pdev->dev.platform_data;
  286. struct tps6586x_settings *setting = p->driver_data;
  287. uint8_t reg;
  288. if (setting == NULL)
  289. return 0;
  290. if (!(setting->slew_rate & TPS6586X_SLEW_RATE_SET))
  291. return 0;
  292. /* only SM0 and SM1 can have the slew rate settings */
  293. switch (pdev->id) {
  294. case TPS6586X_ID_SM_0:
  295. reg = TPS6586X_SM0SL;
  296. break;
  297. case TPS6586X_ID_SM_1:
  298. reg = TPS6586X_SM1SL;
  299. break;
  300. default:
  301. dev_warn(&pdev->dev, "Only SM0/SM1 can set slew rate\n");
  302. return -EINVAL;
  303. }
  304. return tps6586x_write(parent, reg,
  305. setting->slew_rate & TPS6586X_SLEW_RATE_MASK);
  306. }
  307. static inline struct tps6586x_regulator *find_regulator_info(int id)
  308. {
  309. struct tps6586x_regulator *ri;
  310. int i;
  311. for (i = 0; i < ARRAY_SIZE(tps6586x_regulator); i++) {
  312. ri = &tps6586x_regulator[i];
  313. if (ri->desc.id == id)
  314. return ri;
  315. }
  316. return NULL;
  317. }
  318. static int __devinit tps6586x_regulator_probe(struct platform_device *pdev)
  319. {
  320. struct tps6586x_regulator *ri = NULL;
  321. struct regulator_dev *rdev;
  322. int id = pdev->id;
  323. int err;
  324. dev_dbg(&pdev->dev, "Probing regulator %d\n", id);
  325. ri = find_regulator_info(id);
  326. if (ri == NULL) {
  327. dev_err(&pdev->dev, "invalid regulator ID specified\n");
  328. return -EINVAL;
  329. }
  330. err = tps6586x_regulator_preinit(pdev->dev.parent, ri);
  331. if (err)
  332. return err;
  333. rdev = regulator_register(&ri->desc, &pdev->dev,
  334. pdev->dev.platform_data, ri, NULL);
  335. if (IS_ERR(rdev)) {
  336. dev_err(&pdev->dev, "failed to register regulator %s\n",
  337. ri->desc.name);
  338. return PTR_ERR(rdev);
  339. }
  340. platform_set_drvdata(pdev, rdev);
  341. return tps6586x_regulator_set_slew_rate(pdev);
  342. }
  343. static int __devexit tps6586x_regulator_remove(struct platform_device *pdev)
  344. {
  345. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  346. regulator_unregister(rdev);
  347. return 0;
  348. }
  349. static struct platform_driver tps6586x_regulator_driver = {
  350. .driver = {
  351. .name = "tps6586x-regulator",
  352. .owner = THIS_MODULE,
  353. },
  354. .probe = tps6586x_regulator_probe,
  355. .remove = __devexit_p(tps6586x_regulator_remove),
  356. };
  357. static int __init tps6586x_regulator_init(void)
  358. {
  359. return platform_driver_register(&tps6586x_regulator_driver);
  360. }
  361. subsys_initcall(tps6586x_regulator_init);
  362. static void __exit tps6586x_regulator_exit(void)
  363. {
  364. platform_driver_unregister(&tps6586x_regulator_driver);
  365. }
  366. module_exit(tps6586x_regulator_exit);
  367. MODULE_LICENSE("GPL");
  368. MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
  369. MODULE_DESCRIPTION("Regulator Driver for TI TPS6586X PMIC");
  370. MODULE_ALIAS("platform:tps6586x-regulator");