tps6586x-regulator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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_list_voltage(struct regulator_dev *rdev, unsigned selector)
  70. {
  71. struct tps6586x_regulator *info = rdev_get_drvdata(rdev);
  72. int rid = rdev_get_id(rdev);
  73. /* LDO0 has minimal voltage 1.2V rather than 1.25V */
  74. if ((rid == TPS6586X_ID_LDO_0) && (selector == 0))
  75. return (info->voltages[0] - 50) * 1000;
  76. return info->voltages[selector] * 1000;
  77. }
  78. static int tps6586x_set_voltage_sel(struct regulator_dev *rdev,
  79. unsigned selector)
  80. {
  81. struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
  82. struct device *parent = to_tps6586x_dev(rdev);
  83. int ret, val, rid = rdev_get_id(rdev);
  84. uint8_t mask;
  85. val = selector << ri->volt_shift;
  86. mask = ((1 << ri->volt_nbits) - 1) << ri->volt_shift;
  87. ret = tps6586x_update(parent, ri->volt_reg, val, mask);
  88. if (ret)
  89. return ret;
  90. /* Update go bit for DVM regulators */
  91. switch (rid) {
  92. case TPS6586X_ID_LDO_2:
  93. case TPS6586X_ID_LDO_4:
  94. case TPS6586X_ID_SM_0:
  95. case TPS6586X_ID_SM_1:
  96. ret = tps6586x_set_bits(parent, ri->go_reg, 1 << ri->go_bit);
  97. break;
  98. }
  99. return ret;
  100. }
  101. static int tps6586x_get_voltage_sel(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 val;
  115. }
  116. static int tps6586x_regulator_enable(struct regulator_dev *rdev)
  117. {
  118. struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
  119. struct device *parent = to_tps6586x_dev(rdev);
  120. return tps6586x_set_bits(parent, ri->enable_reg[0],
  121. 1 << ri->enable_bit[0]);
  122. }
  123. static int tps6586x_regulator_disable(struct regulator_dev *rdev)
  124. {
  125. struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
  126. struct device *parent = to_tps6586x_dev(rdev);
  127. return tps6586x_clr_bits(parent, ri->enable_reg[0],
  128. 1 << ri->enable_bit[0]);
  129. }
  130. static int tps6586x_regulator_is_enabled(struct regulator_dev *rdev)
  131. {
  132. struct tps6586x_regulator *ri = rdev_get_drvdata(rdev);
  133. struct device *parent = to_tps6586x_dev(rdev);
  134. uint8_t reg_val;
  135. int ret;
  136. ret = tps6586x_read(parent, ri->enable_reg[0], &reg_val);
  137. if (ret)
  138. return ret;
  139. return !!(reg_val & (1 << ri->enable_bit[0]));
  140. }
  141. static struct regulator_ops tps6586x_regulator_ops = {
  142. .list_voltage = tps6586x_list_voltage,
  143. .get_voltage_sel = tps6586x_get_voltage_sel,
  144. .set_voltage_sel = tps6586x_set_voltage_sel,
  145. .is_enabled = tps6586x_regulator_is_enabled,
  146. .enable = tps6586x_regulator_enable,
  147. .disable = tps6586x_regulator_disable,
  148. };
  149. static int tps6586x_ldo_voltages[] = {
  150. 1250, 1500, 1800, 2500, 2700, 2850, 3100, 3300,
  151. };
  152. static int tps6586x_ldo4_voltages[] = {
  153. 1700, 1725, 1750, 1775, 1800, 1825, 1850, 1875,
  154. 1900, 1925, 1950, 1975, 2000, 2025, 2050, 2075,
  155. 2100, 2125, 2150, 2175, 2200, 2225, 2250, 2275,
  156. 2300, 2325, 2350, 2375, 2400, 2425, 2450, 2475,
  157. };
  158. static int tps6586x_sm2_voltages[] = {
  159. 3000, 3050, 3100, 3150, 3200, 3250, 3300, 3350,
  160. 3400, 3450, 3500, 3550, 3600, 3650, 3700, 3750,
  161. 3800, 3850, 3900, 3950, 4000, 4050, 4100, 4150,
  162. 4200, 4250, 4300, 4350, 4400, 4450, 4500, 4550,
  163. };
  164. static int tps6586x_dvm_voltages[] = {
  165. 725, 750, 775, 800, 825, 850, 875, 900,
  166. 925, 950, 975, 1000, 1025, 1050, 1075, 1100,
  167. 1125, 1150, 1175, 1200, 1225, 1250, 1275, 1300,
  168. 1325, 1350, 1375, 1400, 1425, 1450, 1475, 1500,
  169. };
  170. #define TPS6586X_REGULATOR(_id, vdata, vreg, shift, nbits, \
  171. ereg0, ebit0, ereg1, ebit1) \
  172. .desc = { \
  173. .name = "REG-" #_id, \
  174. .ops = &tps6586x_regulator_ops, \
  175. .type = REGULATOR_VOLTAGE, \
  176. .id = TPS6586X_ID_##_id, \
  177. .n_voltages = ARRAY_SIZE(tps6586x_##vdata##_voltages), \
  178. .owner = THIS_MODULE, \
  179. }, \
  180. .volt_reg = TPS6586X_##vreg, \
  181. .volt_shift = (shift), \
  182. .volt_nbits = (nbits), \
  183. .enable_reg[0] = TPS6586X_SUPPLY##ereg0, \
  184. .enable_bit[0] = (ebit0), \
  185. .enable_reg[1] = TPS6586X_SUPPLY##ereg1, \
  186. .enable_bit[1] = (ebit1), \
  187. .voltages = tps6586x_##vdata##_voltages,
  188. #define TPS6586X_REGULATOR_DVM_GOREG(goreg, gobit) \
  189. .go_reg = TPS6586X_##goreg, \
  190. .go_bit = (gobit),
  191. #define TPS6586X_LDO(_id, vdata, vreg, shift, nbits, \
  192. ereg0, ebit0, ereg1, ebit1) \
  193. { \
  194. TPS6586X_REGULATOR(_id, vdata, vreg, shift, nbits, \
  195. ereg0, ebit0, ereg1, ebit1) \
  196. }
  197. #define TPS6586X_DVM(_id, vdata, vreg, shift, nbits, \
  198. ereg0, ebit0, ereg1, ebit1, goreg, gobit) \
  199. { \
  200. TPS6586X_REGULATOR(_id, vdata, vreg, shift, nbits, \
  201. ereg0, ebit0, ereg1, ebit1) \
  202. TPS6586X_REGULATOR_DVM_GOREG(goreg, gobit) \
  203. }
  204. static struct tps6586x_regulator tps6586x_regulator[] = {
  205. TPS6586X_LDO(LDO_0, ldo, SUPPLYV1, 5, 3, ENC, 0, END, 0),
  206. TPS6586X_LDO(LDO_3, ldo, SUPPLYV4, 0, 3, ENC, 2, END, 2),
  207. TPS6586X_LDO(LDO_5, ldo, SUPPLYV6, 0, 3, ENE, 6, ENE, 6),
  208. TPS6586X_LDO(LDO_6, ldo, SUPPLYV3, 0, 3, ENC, 4, END, 4),
  209. TPS6586X_LDO(LDO_7, ldo, SUPPLYV3, 3, 3, ENC, 5, END, 5),
  210. TPS6586X_LDO(LDO_8, ldo, SUPPLYV2, 5, 3, ENC, 6, END, 6),
  211. TPS6586X_LDO(LDO_9, ldo, SUPPLYV6, 3, 3, ENE, 7, ENE, 7),
  212. TPS6586X_LDO(LDO_RTC, ldo, SUPPLYV4, 3, 3, V4, 7, V4, 7),
  213. TPS6586X_LDO(LDO_1, dvm, SUPPLYV1, 0, 5, ENC, 1, END, 1),
  214. TPS6586X_LDO(SM_2, sm2, SUPPLYV2, 0, 5, ENC, 7, END, 7),
  215. TPS6586X_DVM(LDO_2, dvm, LDO2BV1, 0, 5, ENA, 3, ENB, 3, VCC2, 6),
  216. TPS6586X_DVM(LDO_4, ldo4, LDO4V1, 0, 5, ENC, 3, END, 3, VCC1, 6),
  217. TPS6586X_DVM(SM_0, dvm, SM0V1, 0, 5, ENA, 1, ENB, 1, VCC1, 2),
  218. TPS6586X_DVM(SM_1, dvm, SM1V1, 0, 5, ENA, 0, ENB, 0, VCC1, 0),
  219. };
  220. /*
  221. * TPS6586X has 2 enable bits that are OR'ed to determine the actual
  222. * regulator state. Clearing one of this bits allows switching
  223. * regulator on and of with single register write.
  224. */
  225. static inline int tps6586x_regulator_preinit(struct device *parent,
  226. struct tps6586x_regulator *ri)
  227. {
  228. uint8_t val1, val2;
  229. int ret;
  230. if (ri->enable_reg[0] == ri->enable_reg[1] &&
  231. ri->enable_bit[0] == ri->enable_bit[1])
  232. return 0;
  233. ret = tps6586x_read(parent, ri->enable_reg[0], &val1);
  234. if (ret)
  235. return ret;
  236. ret = tps6586x_read(parent, ri->enable_reg[1], &val2);
  237. if (ret)
  238. return ret;
  239. if (!(val2 & (1 << ri->enable_bit[1])))
  240. return 0;
  241. /*
  242. * The regulator is on, but it's enabled with the bit we don't
  243. * want to use, so we switch the enable bits
  244. */
  245. if (!(val1 & (1 << ri->enable_bit[0]))) {
  246. ret = tps6586x_set_bits(parent, ri->enable_reg[0],
  247. 1 << ri->enable_bit[0]);
  248. if (ret)
  249. return ret;
  250. }
  251. return tps6586x_clr_bits(parent, ri->enable_reg[1],
  252. 1 << ri->enable_bit[1]);
  253. }
  254. static int tps6586x_regulator_set_slew_rate(struct platform_device *pdev)
  255. {
  256. struct device *parent = pdev->dev.parent;
  257. struct regulator_init_data *p = pdev->dev.platform_data;
  258. struct tps6586x_settings *setting = p->driver_data;
  259. uint8_t reg;
  260. if (setting == NULL)
  261. return 0;
  262. if (!(setting->slew_rate & TPS6586X_SLEW_RATE_SET))
  263. return 0;
  264. /* only SM0 and SM1 can have the slew rate settings */
  265. switch (pdev->id) {
  266. case TPS6586X_ID_SM_0:
  267. reg = TPS6586X_SM0SL;
  268. break;
  269. case TPS6586X_ID_SM_1:
  270. reg = TPS6586X_SM1SL;
  271. break;
  272. default:
  273. dev_warn(&pdev->dev, "Only SM0/SM1 can set slew rate\n");
  274. return -EINVAL;
  275. }
  276. return tps6586x_write(parent, reg,
  277. setting->slew_rate & TPS6586X_SLEW_RATE_MASK);
  278. }
  279. static inline struct tps6586x_regulator *find_regulator_info(int id)
  280. {
  281. struct tps6586x_regulator *ri;
  282. int i;
  283. for (i = 0; i < ARRAY_SIZE(tps6586x_regulator); i++) {
  284. ri = &tps6586x_regulator[i];
  285. if (ri->desc.id == id)
  286. return ri;
  287. }
  288. return NULL;
  289. }
  290. static int __devinit tps6586x_regulator_probe(struct platform_device *pdev)
  291. {
  292. struct tps6586x_regulator *ri = NULL;
  293. struct regulator_config config = { };
  294. struct regulator_dev *rdev;
  295. int id = pdev->id;
  296. int err;
  297. dev_dbg(&pdev->dev, "Probing regulator %d\n", id);
  298. ri = find_regulator_info(id);
  299. if (ri == NULL) {
  300. dev_err(&pdev->dev, "invalid regulator ID specified\n");
  301. return -EINVAL;
  302. }
  303. err = tps6586x_regulator_preinit(pdev->dev.parent, ri);
  304. if (err)
  305. return err;
  306. config.dev = &pdev->dev;
  307. config.of_node = pdev->dev.of_node;
  308. config.init_data = pdev->dev.platform_data;
  309. config.driver_data = ri;
  310. rdev = regulator_register(&ri->desc, &config);
  311. if (IS_ERR(rdev)) {
  312. dev_err(&pdev->dev, "failed to register regulator %s\n",
  313. ri->desc.name);
  314. return PTR_ERR(rdev);
  315. }
  316. platform_set_drvdata(pdev, rdev);
  317. return tps6586x_regulator_set_slew_rate(pdev);
  318. }
  319. static int __devexit tps6586x_regulator_remove(struct platform_device *pdev)
  320. {
  321. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  322. regulator_unregister(rdev);
  323. return 0;
  324. }
  325. static struct platform_driver tps6586x_regulator_driver = {
  326. .driver = {
  327. .name = "tps6586x-regulator",
  328. .owner = THIS_MODULE,
  329. },
  330. .probe = tps6586x_regulator_probe,
  331. .remove = __devexit_p(tps6586x_regulator_remove),
  332. };
  333. static int __init tps6586x_regulator_init(void)
  334. {
  335. return platform_driver_register(&tps6586x_regulator_driver);
  336. }
  337. subsys_initcall(tps6586x_regulator_init);
  338. static void __exit tps6586x_regulator_exit(void)
  339. {
  340. platform_driver_unregister(&tps6586x_regulator_driver);
  341. }
  342. module_exit(tps6586x_regulator_exit);
  343. MODULE_LICENSE("GPL");
  344. MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
  345. MODULE_DESCRIPTION("Regulator Driver for TI TPS6586X PMIC");
  346. MODULE_ALIAS("platform:tps6586x-regulator");