tps6586x-regulator.c 11 KB

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