pcf50633-regulator.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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_map_voltage(struct regulator_dev *rdev,
  85. int min_uV, int max_uV)
  86. {
  87. struct pcf50633 *pcf;
  88. int regulator_id, millivolts;
  89. u8 volt_bits;
  90. pcf = rdev_get_drvdata(rdev);
  91. regulator_id = rdev_get_id(rdev);
  92. if (regulator_id >= PCF50633_NUM_REGULATORS)
  93. return -EINVAL;
  94. millivolts = min_uV / 1000;
  95. switch (regulator_id) {
  96. case PCF50633_REGULATOR_AUTO:
  97. volt_bits = auto_voltage_bits(millivolts);
  98. break;
  99. case PCF50633_REGULATOR_DOWN1:
  100. case PCF50633_REGULATOR_DOWN2:
  101. volt_bits = down_voltage_bits(millivolts);
  102. break;
  103. case PCF50633_REGULATOR_LDO1:
  104. case PCF50633_REGULATOR_LDO2:
  105. case PCF50633_REGULATOR_LDO3:
  106. case PCF50633_REGULATOR_LDO4:
  107. case PCF50633_REGULATOR_LDO5:
  108. case PCF50633_REGULATOR_LDO6:
  109. case PCF50633_REGULATOR_HCLDO:
  110. case PCF50633_REGULATOR_MEMLDO:
  111. volt_bits = ldo_voltage_bits(millivolts);
  112. break;
  113. default:
  114. return -EINVAL;
  115. }
  116. return volt_bits;
  117. }
  118. static int pcf50633_regulator_list_voltage(struct regulator_dev *rdev,
  119. unsigned int index)
  120. {
  121. int regulator_id = rdev_get_id(rdev);
  122. int millivolts;
  123. switch (regulator_id) {
  124. case PCF50633_REGULATOR_AUTO:
  125. millivolts = auto_voltage_value(index);
  126. break;
  127. case PCF50633_REGULATOR_DOWN1:
  128. case PCF50633_REGULATOR_DOWN2:
  129. millivolts = down_voltage_value(index);
  130. break;
  131. case PCF50633_REGULATOR_LDO1:
  132. case PCF50633_REGULATOR_LDO2:
  133. case PCF50633_REGULATOR_LDO3:
  134. case PCF50633_REGULATOR_LDO4:
  135. case PCF50633_REGULATOR_LDO5:
  136. case PCF50633_REGULATOR_LDO6:
  137. case PCF50633_REGULATOR_HCLDO:
  138. case PCF50633_REGULATOR_MEMLDO:
  139. millivolts = ldo_voltage_value(index);
  140. break;
  141. default:
  142. return -EINVAL;
  143. }
  144. return millivolts * 1000;
  145. }
  146. static struct regulator_ops pcf50633_regulator_ops = {
  147. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  148. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  149. .list_voltage = pcf50633_regulator_list_voltage,
  150. .map_voltage = pcf50633_regulator_map_voltage,
  151. .enable = regulator_enable_regmap,
  152. .disable = regulator_disable_regmap,
  153. .is_enabled = regulator_is_enabled_regmap,
  154. };
  155. static const struct regulator_desc regulators[] = {
  156. [PCF50633_REGULATOR_AUTO] = PCF50633_REGULATOR("auto", AUTO, 128),
  157. [PCF50633_REGULATOR_DOWN1] = PCF50633_REGULATOR("down1", DOWN1, 96),
  158. [PCF50633_REGULATOR_DOWN2] = PCF50633_REGULATOR("down2", DOWN2, 96),
  159. [PCF50633_REGULATOR_LDO1] = PCF50633_REGULATOR("ldo1", LDO1, 28),
  160. [PCF50633_REGULATOR_LDO2] = PCF50633_REGULATOR("ldo2", LDO2, 28),
  161. [PCF50633_REGULATOR_LDO3] = PCF50633_REGULATOR("ldo3", LDO3, 28),
  162. [PCF50633_REGULATOR_LDO4] = PCF50633_REGULATOR("ldo4", LDO4, 28),
  163. [PCF50633_REGULATOR_LDO5] = PCF50633_REGULATOR("ldo5", LDO5, 28),
  164. [PCF50633_REGULATOR_LDO6] = PCF50633_REGULATOR("ldo6", LDO6, 28),
  165. [PCF50633_REGULATOR_HCLDO] = PCF50633_REGULATOR("hcldo", HCLDO, 28),
  166. [PCF50633_REGULATOR_MEMLDO] = PCF50633_REGULATOR("memldo", MEMLDO, 28),
  167. };
  168. static int __devinit pcf50633_regulator_probe(struct platform_device *pdev)
  169. {
  170. struct regulator_dev *rdev;
  171. struct pcf50633 *pcf;
  172. struct regulator_config config = { };
  173. /* Already set by core driver */
  174. pcf = dev_to_pcf50633(pdev->dev.parent);
  175. config.dev = &pdev->dev;
  176. config.init_data = pdev->dev.platform_data;
  177. config.driver_data = pcf;
  178. config.regmap = pcf->regmap;
  179. rdev = regulator_register(&regulators[pdev->id], &config);
  180. if (IS_ERR(rdev))
  181. return PTR_ERR(rdev);
  182. platform_set_drvdata(pdev, rdev);
  183. if (pcf->pdata->regulator_registered)
  184. pcf->pdata->regulator_registered(pcf, pdev->id);
  185. return 0;
  186. }
  187. static int __devexit pcf50633_regulator_remove(struct platform_device *pdev)
  188. {
  189. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  190. platform_set_drvdata(pdev, NULL);
  191. regulator_unregister(rdev);
  192. return 0;
  193. }
  194. static struct platform_driver pcf50633_regulator_driver = {
  195. .driver = {
  196. .name = "pcf50633-regltr",
  197. },
  198. .probe = pcf50633_regulator_probe,
  199. .remove = __devexit_p(pcf50633_regulator_remove),
  200. };
  201. static int __init pcf50633_regulator_init(void)
  202. {
  203. return platform_driver_register(&pcf50633_regulator_driver);
  204. }
  205. subsys_initcall(pcf50633_regulator_init);
  206. static void __exit pcf50633_regulator_exit(void)
  207. {
  208. platform_driver_unregister(&pcf50633_regulator_driver);
  209. }
  210. module_exit(pcf50633_regulator_exit);
  211. MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
  212. MODULE_DESCRIPTION("PCF50633 regulator driver");
  213. MODULE_LICENSE("GPL");
  214. MODULE_ALIAS("platform:pcf50633-regulator");