pcap-regulator.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * PCAP2 Regulator Driver
  4. *
  5. * Copyright (c) 2009 Daniel Ribeiro <drwyrm@gmail.com>
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/err.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regulator/driver.h>
  13. #include <linux/regulator/machine.h>
  14. #include <linux/mfd/ezx-pcap.h>
  15. static const unsigned int V1_table[] = {
  16. 2775000, 1275000, 1600000, 1725000, 1825000, 1925000, 2075000, 2275000,
  17. };
  18. static const unsigned int V2_table[] = {
  19. 2500000, 2775000,
  20. };
  21. static const unsigned int V3_table[] = {
  22. 1075000, 1275000, 1550000, 1725000, 1876000, 1950000, 2075000, 2275000,
  23. };
  24. static const unsigned int V4_table[] = {
  25. 1275000, 1550000, 1725000, 1875000, 1950000, 2075000, 2275000, 2775000,
  26. };
  27. static const unsigned int V5_table[] = {
  28. 1875000, 2275000, 2475000, 2775000,
  29. };
  30. static const unsigned int V6_table[] = {
  31. 2475000, 2775000,
  32. };
  33. static const unsigned int V7_table[] = {
  34. 1875000, 2775000,
  35. };
  36. #define V8_table V4_table
  37. static const unsigned int V9_table[] = {
  38. 1575000, 1875000, 2475000, 2775000,
  39. };
  40. static const unsigned int V10_table[] = {
  41. 5000000,
  42. };
  43. static const unsigned int VAUX1_table[] = {
  44. 1875000, 2475000, 2775000, 3000000,
  45. };
  46. #define VAUX2_table VAUX1_table
  47. static const unsigned int VAUX3_table[] = {
  48. 1200000, 1200000, 1200000, 1200000, 1400000, 1600000, 1800000, 2000000,
  49. 2200000, 2400000, 2600000, 2800000, 3000000, 3200000, 3400000, 3600000,
  50. };
  51. static const unsigned int VAUX4_table[] = {
  52. 1800000, 1800000, 3000000, 5000000,
  53. };
  54. static const unsigned int VSIM_table[] = {
  55. 1875000, 3000000,
  56. };
  57. static const unsigned int VSIM2_table[] = {
  58. 1875000,
  59. };
  60. static const unsigned int VVIB_table[] = {
  61. 1300000, 1800000, 2000000, 3000000,
  62. };
  63. static const unsigned int SW1_table[] = {
  64. 900000, 950000, 1000000, 1050000, 1100000, 1150000, 1200000, 1250000,
  65. 1300000, 1350000, 1400000, 1450000, 1500000, 1600000, 1875000, 2250000,
  66. };
  67. #define SW2_table SW1_table
  68. struct pcap_regulator {
  69. const u8 reg;
  70. const u8 en;
  71. const u8 index;
  72. const u8 stby;
  73. const u8 lowpwr;
  74. };
  75. #define NA 0xff
  76. #define VREG_INFO(_vreg, _reg, _en, _index, _stby, _lowpwr) \
  77. [_vreg] = { \
  78. .reg = _reg, \
  79. .en = _en, \
  80. .index = _index, \
  81. .stby = _stby, \
  82. .lowpwr = _lowpwr, \
  83. }
  84. static struct pcap_regulator vreg_table[] = {
  85. VREG_INFO(V1, PCAP_REG_VREG1, 1, 2, 18, 0),
  86. VREG_INFO(V2, PCAP_REG_VREG1, 5, 6, 19, 22),
  87. VREG_INFO(V3, PCAP_REG_VREG1, 7, 8, 20, 23),
  88. VREG_INFO(V4, PCAP_REG_VREG1, 11, 12, 21, 24),
  89. /* V5 STBY and LOWPWR are on PCAP_REG_VREG2 */
  90. VREG_INFO(V5, PCAP_REG_VREG1, 15, 16, 12, 19),
  91. VREG_INFO(V6, PCAP_REG_VREG2, 1, 2, 14, 20),
  92. VREG_INFO(V7, PCAP_REG_VREG2, 3, 4, 15, 21),
  93. VREG_INFO(V8, PCAP_REG_VREG2, 5, 6, 16, 22),
  94. VREG_INFO(V9, PCAP_REG_VREG2, 9, 10, 17, 23),
  95. VREG_INFO(V10, PCAP_REG_VREG2, 10, NA, 18, 24),
  96. VREG_INFO(VAUX1, PCAP_REG_AUXVREG, 1, 2, 22, 23),
  97. /* VAUX2 ... VSIM2 STBY and LOWPWR are on PCAP_REG_LOWPWR */
  98. VREG_INFO(VAUX2, PCAP_REG_AUXVREG, 4, 5, 0, 1),
  99. VREG_INFO(VAUX3, PCAP_REG_AUXVREG, 7, 8, 2, 3),
  100. VREG_INFO(VAUX4, PCAP_REG_AUXVREG, 12, 13, 4, 5),
  101. VREG_INFO(VSIM, PCAP_REG_AUXVREG, 17, 18, NA, 6),
  102. VREG_INFO(VSIM2, PCAP_REG_AUXVREG, 16, NA, NA, 7),
  103. VREG_INFO(VVIB, PCAP_REG_AUXVREG, 19, 20, NA, NA),
  104. VREG_INFO(SW1, PCAP_REG_SWCTRL, 1, 2, NA, NA),
  105. VREG_INFO(SW2, PCAP_REG_SWCTRL, 6, 7, NA, NA),
  106. /* SW3 STBY is on PCAP_REG_AUXVREG */
  107. VREG_INFO(SW3, PCAP_REG_SWCTRL, 11, 12, 24, NA),
  108. /* SWxS used to control SWx voltage on standby */
  109. /* VREG_INFO(SW1S, PCAP_REG_LOWPWR, NA, 12, NA, NA),
  110. VREG_INFO(SW2S, PCAP_REG_LOWPWR, NA, 20, NA, NA), */
  111. };
  112. static int pcap_regulator_set_voltage_sel(struct regulator_dev *rdev,
  113. unsigned selector)
  114. {
  115. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  116. void *pcap = rdev_get_drvdata(rdev);
  117. /* the regulator doesn't support voltage switching */
  118. if (rdev->desc->n_voltages == 1)
  119. return -EINVAL;
  120. return ezx_pcap_set_bits(pcap, vreg->reg,
  121. (rdev->desc->n_voltages - 1) << vreg->index,
  122. selector << vreg->index);
  123. }
  124. static int pcap_regulator_get_voltage_sel(struct regulator_dev *rdev)
  125. {
  126. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  127. void *pcap = rdev_get_drvdata(rdev);
  128. u32 tmp;
  129. if (rdev->desc->n_voltages == 1)
  130. return 0;
  131. ezx_pcap_read(pcap, vreg->reg, &tmp);
  132. tmp = ((tmp >> vreg->index) & (rdev->desc->n_voltages - 1));
  133. return tmp;
  134. }
  135. static int pcap_regulator_enable(struct regulator_dev *rdev)
  136. {
  137. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  138. void *pcap = rdev_get_drvdata(rdev);
  139. if (vreg->en == NA)
  140. return -EINVAL;
  141. return ezx_pcap_set_bits(pcap, vreg->reg, 1 << vreg->en, 1 << vreg->en);
  142. }
  143. static int pcap_regulator_disable(struct regulator_dev *rdev)
  144. {
  145. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  146. void *pcap = rdev_get_drvdata(rdev);
  147. if (vreg->en == NA)
  148. return -EINVAL;
  149. return ezx_pcap_set_bits(pcap, vreg->reg, 1 << vreg->en, 0);
  150. }
  151. static int pcap_regulator_is_enabled(struct regulator_dev *rdev)
  152. {
  153. struct pcap_regulator *vreg = &vreg_table[rdev_get_id(rdev)];
  154. void *pcap = rdev_get_drvdata(rdev);
  155. u32 tmp;
  156. if (vreg->en == NA)
  157. return -EINVAL;
  158. ezx_pcap_read(pcap, vreg->reg, &tmp);
  159. return (tmp >> vreg->en) & 1;
  160. }
  161. static const struct regulator_ops pcap_regulator_ops = {
  162. .list_voltage = regulator_list_voltage_table,
  163. .set_voltage_sel = pcap_regulator_set_voltage_sel,
  164. .get_voltage_sel = pcap_regulator_get_voltage_sel,
  165. .enable = pcap_regulator_enable,
  166. .disable = pcap_regulator_disable,
  167. .is_enabled = pcap_regulator_is_enabled,
  168. };
  169. #define VREG(_vreg) \
  170. [_vreg] = { \
  171. .name = #_vreg, \
  172. .id = _vreg, \
  173. .n_voltages = ARRAY_SIZE(_vreg##_table), \
  174. .volt_table = _vreg##_table, \
  175. .ops = &pcap_regulator_ops, \
  176. .type = REGULATOR_VOLTAGE, \
  177. .owner = THIS_MODULE, \
  178. }
  179. static const struct regulator_desc pcap_regulators[] = {
  180. VREG(V1), VREG(V2), VREG(V3), VREG(V4), VREG(V5), VREG(V6), VREG(V7),
  181. VREG(V8), VREG(V9), VREG(V10), VREG(VAUX1), VREG(VAUX2), VREG(VAUX3),
  182. VREG(VAUX4), VREG(VSIM), VREG(VSIM2), VREG(VVIB), VREG(SW1), VREG(SW2),
  183. };
  184. static int pcap_regulator_probe(struct platform_device *pdev)
  185. {
  186. struct regulator_dev *rdev;
  187. void *pcap = dev_get_drvdata(pdev->dev.parent);
  188. struct regulator_config config = { };
  189. config.dev = &pdev->dev;
  190. config.init_data = dev_get_platdata(&pdev->dev);
  191. config.driver_data = pcap;
  192. rdev = devm_regulator_register(&pdev->dev, &pcap_regulators[pdev->id],
  193. &config);
  194. if (IS_ERR(rdev))
  195. return PTR_ERR(rdev);
  196. platform_set_drvdata(pdev, rdev);
  197. return 0;
  198. }
  199. static struct platform_driver pcap_regulator_driver = {
  200. .driver = {
  201. .name = "pcap-regulator",
  202. },
  203. .probe = pcap_regulator_probe,
  204. };
  205. static int __init pcap_regulator_init(void)
  206. {
  207. return platform_driver_register(&pcap_regulator_driver);
  208. }
  209. static void __exit pcap_regulator_exit(void)
  210. {
  211. platform_driver_unregister(&pcap_regulator_driver);
  212. }
  213. subsys_initcall(pcap_regulator_init);
  214. module_exit(pcap_regulator_exit);
  215. MODULE_AUTHOR("Daniel Ribeiro <drwyrm@gmail.com>");
  216. MODULE_DESCRIPTION("PCAP2 Regulator Driver");
  217. MODULE_LICENSE("GPL");