pcap-regulator.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. unsigned *selector)
  126. {
  127. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  128. void *pcap = rdev_get_drvdata(rdev);
  129. int uV;
  130. u8 i;
  131. /* the regulator doesn't support voltage switching */
  132. if (vreg->n_voltages == 1)
  133. return -EINVAL;
  134. for (i = 0; i < vreg->n_voltages; i++) {
  135. /* For V1 the first is not the best match */
  136. if (i == 0 && rdev_get_id(rdev) == V1)
  137. i = 1;
  138. else if (i + 1 == vreg->n_voltages && rdev_get_id(rdev) == V1)
  139. i = 0;
  140. uV = vreg->voltage_table[i] * 1000;
  141. if (min_uV <= uV && uV <= max_uV) {
  142. *selector = i;
  143. return ezx_pcap_set_bits(pcap, vreg->reg,
  144. (vreg->n_voltages - 1) << vreg->index,
  145. i << vreg->index);
  146. }
  147. if (i == 0 && rdev_get_id(rdev) == V1)
  148. i = vreg->n_voltages - 1;
  149. }
  150. /* the requested voltage range is not supported by this regulator */
  151. return -EINVAL;
  152. }
  153. static int pcap_regulator_get_voltage(struct regulator_dev *rdev)
  154. {
  155. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  156. void *pcap = rdev_get_drvdata(rdev);
  157. u32 tmp;
  158. int mV;
  159. if (vreg->n_voltages == 1)
  160. return vreg->voltage_table[0] * 1000;
  161. ezx_pcap_read(pcap, vreg->reg, &tmp);
  162. tmp = ((tmp >> vreg->index) & (vreg->n_voltages - 1));
  163. mV = vreg->voltage_table[tmp];
  164. return mV * 1000;
  165. }
  166. static int pcap_regulator_enable(struct regulator_dev *rdev)
  167. {
  168. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  169. void *pcap = rdev_get_drvdata(rdev);
  170. if (vreg->en == NA)
  171. return -EINVAL;
  172. return ezx_pcap_set_bits(pcap, vreg->reg, 1 << vreg->en, 1 << vreg->en);
  173. }
  174. static int pcap_regulator_disable(struct regulator_dev *rdev)
  175. {
  176. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  177. void *pcap = rdev_get_drvdata(rdev);
  178. if (vreg->en == NA)
  179. return -EINVAL;
  180. return ezx_pcap_set_bits(pcap, vreg->reg, 1 << vreg->en, 0);
  181. }
  182. static int pcap_regulator_is_enabled(struct regulator_dev *rdev)
  183. {
  184. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  185. void *pcap = rdev_get_drvdata(rdev);
  186. u32 tmp;
  187. if (vreg->en == NA)
  188. return -EINVAL;
  189. ezx_pcap_read(pcap, vreg->reg, &tmp);
  190. return (tmp >> vreg->en) & 1;
  191. }
  192. static int pcap_regulator_list_voltage(struct regulator_dev *rdev,
  193. unsigned int index)
  194. {
  195. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  196. return vreg->voltage_table[index] * 1000;
  197. }
  198. static struct regulator_ops pcap_regulator_ops = {
  199. .list_voltage = pcap_regulator_list_voltage,
  200. .set_voltage = pcap_regulator_set_voltage,
  201. .get_voltage = pcap_regulator_get_voltage,
  202. .enable = pcap_regulator_enable,
  203. .disable = pcap_regulator_disable,
  204. .is_enabled = pcap_regulator_is_enabled,
  205. };
  206. #define VREG(_vreg) \
  207. [_vreg] = { \
  208. .name = #_vreg, \
  209. .id = _vreg, \
  210. .n_voltages = ARRAY_SIZE(_vreg##_table), \
  211. .ops = &pcap_regulator_ops, \
  212. .type = REGULATOR_VOLTAGE, \
  213. .owner = THIS_MODULE, \
  214. }
  215. static struct regulator_desc pcap_regulators[] = {
  216. VREG(V1), VREG(V2), VREG(V3), VREG(V4), VREG(V5), VREG(V6), VREG(V7),
  217. VREG(V8), VREG(V9), VREG(V10), VREG(VAUX1), VREG(VAUX2), VREG(VAUX3),
  218. VREG(VAUX4), VREG(VSIM), VREG(VSIM2), VREG(VVIB), VREG(SW1), VREG(SW2),
  219. };
  220. static int __devinit pcap_regulator_probe(struct platform_device *pdev)
  221. {
  222. struct regulator_dev *rdev;
  223. void *pcap = dev_get_drvdata(pdev->dev.parent);
  224. rdev = regulator_register(&pcap_regulators[pdev->id], &pdev->dev,
  225. pdev->dev.platform_data, pcap, NULL);
  226. if (IS_ERR(rdev))
  227. return PTR_ERR(rdev);
  228. platform_set_drvdata(pdev, rdev);
  229. return 0;
  230. }
  231. static int __devexit pcap_regulator_remove(struct platform_device *pdev)
  232. {
  233. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  234. regulator_unregister(rdev);
  235. platform_set_drvdata(pdev, NULL);
  236. return 0;
  237. }
  238. static struct platform_driver pcap_regulator_driver = {
  239. .driver = {
  240. .name = "pcap-regulator",
  241. .owner = THIS_MODULE,
  242. },
  243. .probe = pcap_regulator_probe,
  244. .remove = __devexit_p(pcap_regulator_remove),
  245. };
  246. static int __init pcap_regulator_init(void)
  247. {
  248. return platform_driver_register(&pcap_regulator_driver);
  249. }
  250. static void __exit pcap_regulator_exit(void)
  251. {
  252. platform_driver_unregister(&pcap_regulator_driver);
  253. }
  254. subsys_initcall(pcap_regulator_init);
  255. module_exit(pcap_regulator_exit);
  256. MODULE_AUTHOR("Daniel Ribeiro <drwyrm@gmail.com>");
  257. MODULE_DESCRIPTION("PCAP2 Regulator Driver");
  258. MODULE_LICENSE("GPL");