pcf50633-regulator.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* NXP PCF50633 PMIC Driver
  2. *
  3. * (C) 2006-2008 by Openmoko, Inc.
  4. * Author: Balaji Rao <balajirrao@openmoko.org>
  5. * All rights reserved.
  6. *
  7. * Broken down from monstrous PCF50633 driver mainly by
  8. * Harald Welte and Andy Green and Werner Almesberger
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/device.h>
  20. #include <linux/err.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/mfd/pcf50633/core.h>
  23. #include <linux/mfd/pcf50633/pmic.h>
  24. #define PCF50633_REGULATOR(_name, _id, _n) \
  25. { \
  26. .name = _name, \
  27. .id = PCF50633_REGULATOR_##_id, \
  28. .ops = &pcf50633_regulator_ops, \
  29. .n_voltages = _n, \
  30. .type = REGULATOR_VOLTAGE, \
  31. .owner = THIS_MODULE, \
  32. .vsel_reg = PCF50633_REG_##_id##OUT, \
  33. .vsel_mask = 0xff, \
  34. .enable_reg = PCF50633_REG_##_id##OUT + 1, \
  35. .enable_mask = PCF50633_REGULATOR_ON, \
  36. }
  37. /* Bits from voltage value */
  38. static u8 auto_voltage_bits(unsigned int millivolts)
  39. {
  40. if (millivolts < 1800)
  41. return 0x2f;
  42. if (millivolts > 3800)
  43. return 0xff;
  44. millivolts -= 625;
  45. return millivolts / 25;
  46. }
  47. static u8 down_voltage_bits(unsigned int millivolts)
  48. {
  49. if (millivolts < 625)
  50. return 0;
  51. else if (millivolts > 3000)
  52. return 0xff;
  53. millivolts -= 625;
  54. return millivolts / 25;
  55. }
  56. static u8 ldo_voltage_bits(unsigned int millivolts)
  57. {
  58. if (millivolts < 900)
  59. return 0;
  60. else if (millivolts > 3600)
  61. return 0x1f;
  62. millivolts -= 900;
  63. return millivolts / 100;
  64. }
  65. /* Obtain voltage value from bits */
  66. static unsigned int auto_voltage_value(u8 bits)
  67. {
  68. /* AUTOOUT: 00000000 to 00101110 are reserved.
  69. * Return 0 for bits in reserved range, which means this selector code
  70. * can't be used on this system */
  71. if (bits < 0x2f)
  72. return 0;
  73. return 625 + (bits * 25);
  74. }
  75. static unsigned int down_voltage_value(u8 bits)
  76. {
  77. return 625 + (bits * 25);
  78. }
  79. static unsigned int ldo_voltage_value(u8 bits)
  80. {
  81. bits &= 0x1f;
  82. return 900 + (bits * 100);
  83. }
  84. static int pcf50633_regulator_set_voltage(struct regulator_dev *rdev,
  85. int min_uV, int max_uV,
  86. unsigned *selector)
  87. {
  88. struct pcf50633 *pcf;
  89. int regulator_id, millivolts;
  90. u8 volt_bits, regnr;
  91. pcf = rdev_get_drvdata(rdev);
  92. regulator_id = rdev_get_id(rdev);
  93. if (regulator_id >= PCF50633_NUM_REGULATORS)
  94. return -EINVAL;
  95. millivolts = min_uV / 1000;
  96. regnr = rdev->desc->vsel_reg;
  97. switch (regulator_id) {
  98. case PCF50633_REGULATOR_AUTO:
  99. volt_bits = auto_voltage_bits(millivolts);
  100. break;
  101. case PCF50633_REGULATOR_DOWN1:
  102. volt_bits = down_voltage_bits(millivolts);
  103. break;
  104. case PCF50633_REGULATOR_DOWN2:
  105. volt_bits = down_voltage_bits(millivolts);
  106. break;
  107. case PCF50633_REGULATOR_LDO1:
  108. case PCF50633_REGULATOR_LDO2:
  109. case PCF50633_REGULATOR_LDO3:
  110. case PCF50633_REGULATOR_LDO4:
  111. case PCF50633_REGULATOR_LDO5:
  112. case PCF50633_REGULATOR_LDO6:
  113. case PCF50633_REGULATOR_HCLDO:
  114. case PCF50633_REGULATOR_MEMLDO:
  115. volt_bits = ldo_voltage_bits(millivolts);
  116. break;
  117. default:
  118. return -EINVAL;
  119. }
  120. *selector = volt_bits;
  121. return pcf50633_reg_write(pcf, regnr, volt_bits);
  122. }
  123. static int pcf50633_regulator_list_voltage(struct regulator_dev *rdev,
  124. unsigned int index)
  125. {
  126. int regulator_id = rdev_get_id(rdev);
  127. int millivolts;
  128. switch (regulator_id) {
  129. case PCF50633_REGULATOR_AUTO:
  130. millivolts = auto_voltage_value(index);
  131. break;
  132. case PCF50633_REGULATOR_DOWN1:
  133. millivolts = down_voltage_value(index);
  134. break;
  135. case PCF50633_REGULATOR_DOWN2:
  136. millivolts = down_voltage_value(index);
  137. break;
  138. case PCF50633_REGULATOR_LDO1:
  139. case PCF50633_REGULATOR_LDO2:
  140. case PCF50633_REGULATOR_LDO3:
  141. case PCF50633_REGULATOR_LDO4:
  142. case PCF50633_REGULATOR_LDO5:
  143. case PCF50633_REGULATOR_LDO6:
  144. case PCF50633_REGULATOR_HCLDO:
  145. case PCF50633_REGULATOR_MEMLDO:
  146. millivolts = ldo_voltage_value(index);
  147. break;
  148. default:
  149. return -EINVAL;
  150. }
  151. return millivolts * 1000;
  152. }
  153. static struct regulator_ops pcf50633_regulator_ops = {
  154. .set_voltage = pcf50633_regulator_set_voltage,
  155. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  156. .list_voltage = pcf50633_regulator_list_voltage,
  157. .enable = regulator_enable_regmap,
  158. .disable = regulator_disable_regmap,
  159. .is_enabled = regulator_is_enabled_regmap,
  160. };
  161. static const struct regulator_desc regulators[] = {
  162. [PCF50633_REGULATOR_AUTO] = PCF50633_REGULATOR("auto", AUTO, 128),
  163. [PCF50633_REGULATOR_DOWN1] = PCF50633_REGULATOR("down1", DOWN1, 96),
  164. [PCF50633_REGULATOR_DOWN2] = PCF50633_REGULATOR("down2", DOWN2, 96),
  165. [PCF50633_REGULATOR_LDO1] = PCF50633_REGULATOR("ldo1", LDO1, 28),
  166. [PCF50633_REGULATOR_LDO2] = PCF50633_REGULATOR("ldo2", LDO2, 28),
  167. [PCF50633_REGULATOR_LDO3] = PCF50633_REGULATOR("ldo3", LDO3, 28),
  168. [PCF50633_REGULATOR_LDO4] = PCF50633_REGULATOR("ldo4", LDO4, 28),
  169. [PCF50633_REGULATOR_LDO5] = PCF50633_REGULATOR("ldo5", LDO5, 28),
  170. [PCF50633_REGULATOR_LDO6] = PCF50633_REGULATOR("ldo6", LDO6, 28),
  171. [PCF50633_REGULATOR_HCLDO] = PCF50633_REGULATOR("hcldo", HCLDO, 28),
  172. [PCF50633_REGULATOR_MEMLDO] = PCF50633_REGULATOR("memldo", MEMLDO, 28),
  173. };
  174. static int __devinit pcf50633_regulator_probe(struct platform_device *pdev)
  175. {
  176. struct regulator_dev *rdev;
  177. struct pcf50633 *pcf;
  178. struct regulator_config config = { };
  179. /* Already set by core driver */
  180. pcf = dev_to_pcf50633(pdev->dev.parent);
  181. config.dev = &pdev->dev;
  182. config.init_data = pdev->dev.platform_data;
  183. config.driver_data = pcf;
  184. config.regmap = pcf->regmap;
  185. rdev = regulator_register(&regulators[pdev->id], &config);
  186. if (IS_ERR(rdev))
  187. return PTR_ERR(rdev);
  188. platform_set_drvdata(pdev, rdev);
  189. if (pcf->pdata->regulator_registered)
  190. pcf->pdata->regulator_registered(pcf, pdev->id);
  191. return 0;
  192. }
  193. static int __devexit pcf50633_regulator_remove(struct platform_device *pdev)
  194. {
  195. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  196. platform_set_drvdata(pdev, NULL);
  197. regulator_unregister(rdev);
  198. return 0;
  199. }
  200. static struct platform_driver pcf50633_regulator_driver = {
  201. .driver = {
  202. .name = "pcf50633-regltr",
  203. },
  204. .probe = pcf50633_regulator_probe,
  205. .remove = __devexit_p(pcf50633_regulator_remove),
  206. };
  207. static int __init pcf50633_regulator_init(void)
  208. {
  209. return platform_driver_register(&pcf50633_regulator_driver);
  210. }
  211. subsys_initcall(pcf50633_regulator_init);
  212. static void __exit pcf50633_regulator_exit(void)
  213. {
  214. platform_driver_unregister(&pcf50633_regulator_driver);
  215. }
  216. module_exit(pcf50633_regulator_exit);
  217. MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
  218. MODULE_DESCRIPTION("PCF50633 regulator driver");
  219. MODULE_LICENSE("GPL");
  220. MODULE_ALIAS("platform:pcf50633-regulator");