pcap-regulator.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * PCAP2 Regulator Driver
  3. *
  4. * Copyright (c) 2009 Daniel Ribeiro <drwyrm@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/err.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regulator/driver.h>
  17. #include <linux/regulator/machine.h>
  18. #include <linux/mfd/ezx-pcap.h>
  19. static const u16 V1_table[] = {
  20. 2775, 1275, 1600, 1725, 1825, 1925, 2075, 2275,
  21. };
  22. static const u16 V2_table[] = {
  23. 2500, 2775,
  24. };
  25. static const u16 V3_table[] = {
  26. 1075, 1275, 1550, 1725, 1876, 1950, 2075, 2275,
  27. };
  28. static const u16 V4_table[] = {
  29. 1275, 1550, 1725, 1875, 1950, 2075, 2275, 2775,
  30. };
  31. static const u16 V5_table[] = {
  32. 1875, 2275, 2475, 2775,
  33. };
  34. static const u16 V6_table[] = {
  35. 2475, 2775,
  36. };
  37. static const u16 V7_table[] = {
  38. 1875, 2775,
  39. };
  40. #define V8_table V4_table
  41. static const u16 V9_table[] = {
  42. 1575, 1875, 2475, 2775,
  43. };
  44. static const u16 V10_table[] = {
  45. 5000,
  46. };
  47. static const u16 VAUX1_table[] = {
  48. 1875, 2475, 2775, 3000,
  49. };
  50. #define VAUX2_table VAUX1_table
  51. static const u16 VAUX3_table[] = {
  52. 1200, 1200, 1200, 1200, 1400, 1600, 1800, 2000,
  53. 2200, 2400, 2600, 2800, 3000, 3200, 3400, 3600,
  54. };
  55. static const u16 VAUX4_table[] = {
  56. 1800, 1800, 3000, 5000,
  57. };
  58. static const u16 VSIM_table[] = {
  59. 1875, 3000,
  60. };
  61. static const u16 VSIM2_table[] = {
  62. 1875,
  63. };
  64. static const u16 VVIB_table[] = {
  65. 1300, 1800, 2000, 3000,
  66. };
  67. static const u16 SW1_table[] = {
  68. 900, 950, 1000, 1050, 1100, 1150, 1200, 1250,
  69. 1300, 1350, 1400, 1450, 1500, 1600, 1875, 2250,
  70. };
  71. #define SW2_table SW1_table
  72. static const u16 SW3_table[] = {
  73. 4000, 4500, 5000, 5500,
  74. };
  75. struct pcap_regulator {
  76. const u8 reg;
  77. const u8 en;
  78. const u8 index;
  79. const u8 stby;
  80. const u8 lowpwr;
  81. const u8 n_voltages;
  82. const u16 *voltage_table;
  83. };
  84. #define NA 0xff
  85. #define VREG_INFO(_vreg, _reg, _en, _index, _stby, _lowpwr) \
  86. [_vreg] = { \
  87. .reg = _reg, \
  88. .en = _en, \
  89. .index = _index, \
  90. .stby = _stby, \
  91. .lowpwr = _lowpwr, \
  92. .n_voltages = ARRAY_SIZE(_vreg##_table), \
  93. .voltage_table = _vreg##_table, \
  94. }
  95. static struct pcap_regulator vreg_table[] = {
  96. VREG_INFO(V1, PCAP_REG_VREG1, 1, 2, 18, 0),
  97. VREG_INFO(V2, PCAP_REG_VREG1, 5, 6, 19, 22),
  98. VREG_INFO(V3, PCAP_REG_VREG1, 7, 8, 20, 23),
  99. VREG_INFO(V4, PCAP_REG_VREG1, 11, 12, 21, 24),
  100. /* V5 STBY and LOWPWR are on PCAP_REG_VREG2 */
  101. VREG_INFO(V5, PCAP_REG_VREG1, 15, 16, 12, 19),
  102. VREG_INFO(V6, PCAP_REG_VREG2, 1, 2, 14, 20),
  103. VREG_INFO(V7, PCAP_REG_VREG2, 3, 4, 15, 21),
  104. VREG_INFO(V8, PCAP_REG_VREG2, 5, 6, 16, 22),
  105. VREG_INFO(V9, PCAP_REG_VREG2, 9, 10, 17, 23),
  106. VREG_INFO(V10, PCAP_REG_VREG2, 10, NA, 18, 24),
  107. VREG_INFO(VAUX1, PCAP_REG_AUXVREG, 1, 2, 22, 23),
  108. /* VAUX2 ... VSIM2 STBY and LOWPWR are on PCAP_REG_LOWPWR */
  109. VREG_INFO(VAUX2, PCAP_REG_AUXVREG, 4, 5, 0, 1),
  110. VREG_INFO(VAUX3, PCAP_REG_AUXVREG, 7, 8, 2, 3),
  111. VREG_INFO(VAUX4, PCAP_REG_AUXVREG, 12, 13, 4, 5),
  112. VREG_INFO(VSIM, PCAP_REG_AUXVREG, 17, 18, NA, 6),
  113. VREG_INFO(VSIM2, PCAP_REG_AUXVREG, 16, NA, NA, 7),
  114. VREG_INFO(VVIB, PCAP_REG_AUXVREG, 19, 20, NA, NA),
  115. VREG_INFO(SW1, PCAP_REG_SWCTRL, 1, 2, NA, NA),
  116. VREG_INFO(SW2, PCAP_REG_SWCTRL, 6, 7, NA, NA),
  117. /* SW3 STBY is on PCAP_REG_AUXVREG */
  118. VREG_INFO(SW3, PCAP_REG_SWCTRL, 11, 12, 24, NA),
  119. /* SWxS used to control SWx voltage on standby */
  120. /* VREG_INFO(SW1S, PCAP_REG_LOWPWR, NA, 12, NA, NA),
  121. VREG_INFO(SW2S, PCAP_REG_LOWPWR, NA, 20, NA, NA), */
  122. };
  123. static int pcap_regulator_set_voltage(struct regulator_dev *rdev,
  124. int min_uV, int max_uV)
  125. {
  126. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  127. void *pcap = rdev_get_drvdata(rdev);
  128. int uV;
  129. u8 i;
  130. /* the regulator doesn't support voltage switching */
  131. if (vreg->n_voltages == 1)
  132. return -EINVAL;
  133. for (i = 0; i < vreg->n_voltages; i++) {
  134. /* For V1 the first is not the best match */
  135. if (i == 0 && rdev_get_id(rdev) == V1)
  136. i = 1;
  137. else if (i + 1 == vreg->n_voltages && rdev_get_id(rdev) == V1)
  138. i = 0;
  139. uV = vreg->voltage_table[i] * 1000;
  140. if (min_uV <= uV && uV <= max_uV)
  141. return ezx_pcap_set_bits(pcap, vreg->reg,
  142. (vreg->n_voltages - 1) << vreg->index,
  143. i << vreg->index);
  144. if (i == 0 && rdev_get_id(rdev) == V1)
  145. i = vreg->n_voltages - 1;
  146. }
  147. /* the requested voltage range is not supported by this regulator */
  148. return -EINVAL;
  149. }
  150. static int pcap_regulator_get_voltage(struct regulator_dev *rdev)
  151. {
  152. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  153. void *pcap = rdev_get_drvdata(rdev);
  154. u32 tmp;
  155. int mV;
  156. if (vreg->n_voltages == 1)
  157. return vreg->voltage_table[0] * 1000;
  158. ezx_pcap_read(pcap, vreg->reg, &tmp);
  159. tmp = ((tmp >> vreg->index) & (vreg->n_voltages - 1));
  160. mV = vreg->voltage_table[tmp];
  161. return mV * 1000;
  162. }
  163. static int pcap_regulator_enable(struct regulator_dev *rdev)
  164. {
  165. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  166. void *pcap = rdev_get_drvdata(rdev);
  167. if (vreg->en == NA)
  168. return -EINVAL;
  169. return ezx_pcap_set_bits(pcap, vreg->reg, 1 << vreg->en, 1 << vreg->en);
  170. }
  171. static int pcap_regulator_disable(struct regulator_dev *rdev)
  172. {
  173. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  174. void *pcap = rdev_get_drvdata(rdev);
  175. if (vreg->en == NA)
  176. return -EINVAL;
  177. return ezx_pcap_set_bits(pcap, vreg->reg, 1 << vreg->en, 0);
  178. }
  179. static int pcap_regulator_is_enabled(struct regulator_dev *rdev)
  180. {
  181. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  182. void *pcap = rdev_get_drvdata(rdev);
  183. u32 tmp;
  184. if (vreg->en == NA)
  185. return -EINVAL;
  186. ezx_pcap_read(pcap, vreg->reg, &tmp);
  187. return (tmp >> vreg->en) & 1;
  188. }
  189. static int pcap_regulator_list_voltage(struct regulator_dev *rdev,
  190. unsigned int index)
  191. {
  192. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  193. return vreg->voltage_table[index] * 1000;
  194. }
  195. static struct regulator_ops pcap_regulator_ops = {
  196. .list_voltage = pcap_regulator_list_voltage,
  197. .set_voltage = pcap_regulator_set_voltage,
  198. .get_voltage = pcap_regulator_get_voltage,
  199. .enable = pcap_regulator_enable,
  200. .disable = pcap_regulator_disable,
  201. .is_enabled = pcap_regulator_is_enabled,
  202. };
  203. #define VREG(_vreg) \
  204. [_vreg] = { \
  205. .name = #_vreg, \
  206. .id = _vreg, \
  207. .n_voltages = ARRAY_SIZE(_vreg##_table), \
  208. .ops = &pcap_regulator_ops, \
  209. .type = REGULATOR_VOLTAGE, \
  210. .owner = THIS_MODULE, \
  211. }
  212. static struct regulator_desc pcap_regulators[] = {
  213. VREG(V1), VREG(V2), VREG(V3), VREG(V4), VREG(V5), VREG(V6), VREG(V7),
  214. VREG(V8), VREG(V9), VREG(V10), VREG(VAUX1), VREG(VAUX2), VREG(VAUX3),
  215. VREG(VAUX4), VREG(VSIM), VREG(VSIM2), VREG(VVIB), VREG(SW1), VREG(SW2),
  216. };
  217. static int __devinit pcap_regulator_probe(struct platform_device *pdev)
  218. {
  219. struct regulator_dev *rdev;
  220. void *pcap = dev_get_drvdata(pdev->dev.parent);
  221. rdev = regulator_register(&pcap_regulators[pdev->id], &pdev->dev,
  222. pdev->dev.platform_data, pcap);
  223. if (IS_ERR(rdev))
  224. return PTR_ERR(rdev);
  225. platform_set_drvdata(pdev, rdev);
  226. return 0;
  227. }
  228. static int __devexit pcap_regulator_remove(struct platform_device *pdev)
  229. {
  230. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  231. regulator_unregister(rdev);
  232. return 0;
  233. }
  234. static struct platform_driver pcap_regulator_driver = {
  235. .driver = {
  236. .name = "pcap-regulator",
  237. },
  238. .probe = pcap_regulator_probe,
  239. .remove = __devexit_p(pcap_regulator_remove),
  240. };
  241. static int __init pcap_regulator_init(void)
  242. {
  243. return platform_driver_register(&pcap_regulator_driver);
  244. }
  245. static void __exit pcap_regulator_exit(void)
  246. {
  247. platform_driver_unregister(&pcap_regulator_driver);
  248. }
  249. subsys_initcall(pcap_regulator_init);
  250. module_exit(pcap_regulator_exit);
  251. MODULE_AUTHOR("Daniel Ribeiro <drwyrm@gmail.com>");
  252. MODULE_DESCRIPTION("PCAP2 Regulator Driver");
  253. MODULE_LICENSE("GPL");