pcf50633-regulator.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 = _id, \
  28. .ops = &pcf50633_regulator_ops, \
  29. .n_voltages = _n, \
  30. .type = REGULATOR_VOLTAGE, \
  31. .owner = THIS_MODULE, \
  32. }
  33. static const u8 pcf50633_regulator_registers[PCF50633_NUM_REGULATORS] = {
  34. [PCF50633_REGULATOR_AUTO] = PCF50633_REG_AUTOOUT,
  35. [PCF50633_REGULATOR_DOWN1] = PCF50633_REG_DOWN1OUT,
  36. [PCF50633_REGULATOR_DOWN2] = PCF50633_REG_DOWN2OUT,
  37. [PCF50633_REGULATOR_MEMLDO] = PCF50633_REG_MEMLDOOUT,
  38. [PCF50633_REGULATOR_LDO1] = PCF50633_REG_LDO1OUT,
  39. [PCF50633_REGULATOR_LDO2] = PCF50633_REG_LDO2OUT,
  40. [PCF50633_REGULATOR_LDO3] = PCF50633_REG_LDO3OUT,
  41. [PCF50633_REGULATOR_LDO4] = PCF50633_REG_LDO4OUT,
  42. [PCF50633_REGULATOR_LDO5] = PCF50633_REG_LDO5OUT,
  43. [PCF50633_REGULATOR_LDO6] = PCF50633_REG_LDO6OUT,
  44. [PCF50633_REGULATOR_HCLDO] = PCF50633_REG_HCLDOOUT,
  45. };
  46. /* Bits from voltage value */
  47. static u8 auto_voltage_bits(unsigned int millivolts)
  48. {
  49. if (millivolts < 1800)
  50. return 0;
  51. if (millivolts > 3800)
  52. return 0xff;
  53. millivolts -= 625;
  54. return millivolts / 25;
  55. }
  56. static u8 down_voltage_bits(unsigned int millivolts)
  57. {
  58. if (millivolts < 625)
  59. return 0;
  60. else if (millivolts > 3000)
  61. return 0xff;
  62. millivolts -= 625;
  63. return millivolts / 25;
  64. }
  65. static u8 ldo_voltage_bits(unsigned int millivolts)
  66. {
  67. if (millivolts < 900)
  68. return 0;
  69. else if (millivolts > 3600)
  70. return 0x1f;
  71. millivolts -= 900;
  72. return millivolts / 100;
  73. }
  74. /* Obtain voltage value from bits */
  75. static unsigned int auto_voltage_value(u8 bits)
  76. {
  77. if (bits < 0x2f)
  78. return 0;
  79. return 625 + (bits * 25);
  80. }
  81. static unsigned int down_voltage_value(u8 bits)
  82. {
  83. return 625 + (bits * 25);
  84. }
  85. static unsigned int ldo_voltage_value(u8 bits)
  86. {
  87. bits &= 0x1f;
  88. return 900 + (bits * 100);
  89. }
  90. static int pcf50633_regulator_set_voltage(struct regulator_dev *rdev,
  91. int min_uV, int max_uV,
  92. unsigned *selector)
  93. {
  94. struct pcf50633 *pcf;
  95. int regulator_id, millivolts;
  96. u8 volt_bits, regnr;
  97. pcf = rdev_get_drvdata(rdev);
  98. regulator_id = rdev_get_id(rdev);
  99. if (regulator_id >= PCF50633_NUM_REGULATORS)
  100. return -EINVAL;
  101. millivolts = min_uV / 1000;
  102. regnr = pcf50633_regulator_registers[regulator_id];
  103. switch (regulator_id) {
  104. case PCF50633_REGULATOR_AUTO:
  105. volt_bits = auto_voltage_bits(millivolts);
  106. break;
  107. case PCF50633_REGULATOR_DOWN1:
  108. volt_bits = down_voltage_bits(millivolts);
  109. break;
  110. case PCF50633_REGULATOR_DOWN2:
  111. volt_bits = down_voltage_bits(millivolts);
  112. break;
  113. case PCF50633_REGULATOR_LDO1:
  114. case PCF50633_REGULATOR_LDO2:
  115. case PCF50633_REGULATOR_LDO3:
  116. case PCF50633_REGULATOR_LDO4:
  117. case PCF50633_REGULATOR_LDO5:
  118. case PCF50633_REGULATOR_LDO6:
  119. case PCF50633_REGULATOR_HCLDO:
  120. case PCF50633_REGULATOR_MEMLDO:
  121. volt_bits = ldo_voltage_bits(millivolts);
  122. break;
  123. default:
  124. return -EINVAL;
  125. }
  126. *selector = volt_bits;
  127. return pcf50633_reg_write(pcf, regnr, volt_bits);
  128. }
  129. static int pcf50633_regulator_voltage_value(enum pcf50633_regulator_id id,
  130. u8 bits)
  131. {
  132. int millivolts;
  133. switch (id) {
  134. case PCF50633_REGULATOR_AUTO:
  135. millivolts = auto_voltage_value(bits);
  136. break;
  137. case PCF50633_REGULATOR_DOWN1:
  138. millivolts = down_voltage_value(bits);
  139. break;
  140. case PCF50633_REGULATOR_DOWN2:
  141. millivolts = down_voltage_value(bits);
  142. break;
  143. case PCF50633_REGULATOR_LDO1:
  144. case PCF50633_REGULATOR_LDO2:
  145. case PCF50633_REGULATOR_LDO3:
  146. case PCF50633_REGULATOR_LDO4:
  147. case PCF50633_REGULATOR_LDO5:
  148. case PCF50633_REGULATOR_LDO6:
  149. case PCF50633_REGULATOR_HCLDO:
  150. case PCF50633_REGULATOR_MEMLDO:
  151. millivolts = ldo_voltage_value(bits);
  152. break;
  153. default:
  154. return -EINVAL;
  155. }
  156. return millivolts * 1000;
  157. }
  158. static int pcf50633_regulator_get_voltage(struct regulator_dev *rdev)
  159. {
  160. struct pcf50633 *pcf;
  161. int regulator_id;
  162. u8 volt_bits, regnr;
  163. pcf = rdev_get_drvdata(rdev);
  164. regulator_id = rdev_get_id(rdev);
  165. if (regulator_id >= PCF50633_NUM_REGULATORS)
  166. return -EINVAL;
  167. regnr = pcf50633_regulator_registers[regulator_id];
  168. volt_bits = pcf50633_reg_read(pcf, regnr);
  169. return pcf50633_regulator_voltage_value(regulator_id, volt_bits);
  170. }
  171. static int pcf50633_regulator_list_voltage(struct regulator_dev *rdev,
  172. unsigned int index)
  173. {
  174. struct pcf50633 *pcf;
  175. int regulator_id;
  176. pcf = rdev_get_drvdata(rdev);
  177. regulator_id = rdev_get_id(rdev);
  178. switch (regulator_id) {
  179. case PCF50633_REGULATOR_AUTO:
  180. index += 0x2f;
  181. break;
  182. default:
  183. break;
  184. }
  185. return pcf50633_regulator_voltage_value(regulator_id, index);
  186. }
  187. static int pcf50633_regulator_enable(struct regulator_dev *rdev)
  188. {
  189. struct pcf50633 *pcf = rdev_get_drvdata(rdev);
  190. int regulator_id;
  191. u8 regnr;
  192. regulator_id = rdev_get_id(rdev);
  193. if (regulator_id >= PCF50633_NUM_REGULATORS)
  194. return -EINVAL;
  195. /* The *ENA register is always one after the *OUT register */
  196. regnr = pcf50633_regulator_registers[regulator_id] + 1;
  197. return pcf50633_reg_set_bit_mask(pcf, regnr, PCF50633_REGULATOR_ON,
  198. PCF50633_REGULATOR_ON);
  199. }
  200. static int pcf50633_regulator_disable(struct regulator_dev *rdev)
  201. {
  202. struct pcf50633 *pcf = rdev_get_drvdata(rdev);
  203. int regulator_id;
  204. u8 regnr;
  205. regulator_id = rdev_get_id(rdev);
  206. if (regulator_id >= PCF50633_NUM_REGULATORS)
  207. return -EINVAL;
  208. /* the *ENA register is always one after the *OUT register */
  209. regnr = pcf50633_regulator_registers[regulator_id] + 1;
  210. return pcf50633_reg_set_bit_mask(pcf, regnr,
  211. PCF50633_REGULATOR_ON, 0);
  212. }
  213. static int pcf50633_regulator_is_enabled(struct regulator_dev *rdev)
  214. {
  215. struct pcf50633 *pcf = rdev_get_drvdata(rdev);
  216. int regulator_id = rdev_get_id(rdev);
  217. u8 regnr;
  218. regulator_id = rdev_get_id(rdev);
  219. if (regulator_id >= PCF50633_NUM_REGULATORS)
  220. return -EINVAL;
  221. /* the *ENA register is always one after the *OUT register */
  222. regnr = pcf50633_regulator_registers[regulator_id] + 1;
  223. return pcf50633_reg_read(pcf, regnr) & PCF50633_REGULATOR_ON;
  224. }
  225. static struct regulator_ops pcf50633_regulator_ops = {
  226. .set_voltage = pcf50633_regulator_set_voltage,
  227. .get_voltage = pcf50633_regulator_get_voltage,
  228. .list_voltage = pcf50633_regulator_list_voltage,
  229. .enable = pcf50633_regulator_enable,
  230. .disable = pcf50633_regulator_disable,
  231. .is_enabled = pcf50633_regulator_is_enabled,
  232. };
  233. static struct regulator_desc regulators[] = {
  234. [PCF50633_REGULATOR_AUTO] =
  235. PCF50633_REGULATOR("auto", PCF50633_REGULATOR_AUTO, 81),
  236. [PCF50633_REGULATOR_DOWN1] =
  237. PCF50633_REGULATOR("down1", PCF50633_REGULATOR_DOWN1, 96),
  238. [PCF50633_REGULATOR_DOWN2] =
  239. PCF50633_REGULATOR("down2", PCF50633_REGULATOR_DOWN2, 96),
  240. [PCF50633_REGULATOR_LDO1] =
  241. PCF50633_REGULATOR("ldo1", PCF50633_REGULATOR_LDO1, 28),
  242. [PCF50633_REGULATOR_LDO2] =
  243. PCF50633_REGULATOR("ldo2", PCF50633_REGULATOR_LDO2, 28),
  244. [PCF50633_REGULATOR_LDO3] =
  245. PCF50633_REGULATOR("ldo3", PCF50633_REGULATOR_LDO3, 28),
  246. [PCF50633_REGULATOR_LDO4] =
  247. PCF50633_REGULATOR("ldo4", PCF50633_REGULATOR_LDO4, 28),
  248. [PCF50633_REGULATOR_LDO5] =
  249. PCF50633_REGULATOR("ldo5", PCF50633_REGULATOR_LDO5, 28),
  250. [PCF50633_REGULATOR_LDO6] =
  251. PCF50633_REGULATOR("ldo6", PCF50633_REGULATOR_LDO6, 28),
  252. [PCF50633_REGULATOR_HCLDO] =
  253. PCF50633_REGULATOR("hcldo", PCF50633_REGULATOR_HCLDO, 28),
  254. [PCF50633_REGULATOR_MEMLDO] =
  255. PCF50633_REGULATOR("memldo", PCF50633_REGULATOR_MEMLDO, 28),
  256. };
  257. static int __devinit pcf50633_regulator_probe(struct platform_device *pdev)
  258. {
  259. struct regulator_dev *rdev;
  260. struct pcf50633 *pcf;
  261. /* Already set by core driver */
  262. pcf = dev_to_pcf50633(pdev->dev.parent);
  263. rdev = regulator_register(&regulators[pdev->id], &pdev->dev,
  264. pdev->dev.platform_data, pcf, NULL);
  265. if (IS_ERR(rdev))
  266. return PTR_ERR(rdev);
  267. platform_set_drvdata(pdev, rdev);
  268. if (pcf->pdata->regulator_registered)
  269. pcf->pdata->regulator_registered(pcf, pdev->id);
  270. return 0;
  271. }
  272. static int __devexit pcf50633_regulator_remove(struct platform_device *pdev)
  273. {
  274. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  275. platform_set_drvdata(pdev, NULL);
  276. regulator_unregister(rdev);
  277. return 0;
  278. }
  279. static struct platform_driver pcf50633_regulator_driver = {
  280. .driver = {
  281. .name = "pcf50633-regltr",
  282. },
  283. .probe = pcf50633_regulator_probe,
  284. .remove = __devexit_p(pcf50633_regulator_remove),
  285. };
  286. static int __init pcf50633_regulator_init(void)
  287. {
  288. return platform_driver_register(&pcf50633_regulator_driver);
  289. }
  290. subsys_initcall(pcf50633_regulator_init);
  291. static void __exit pcf50633_regulator_exit(void)
  292. {
  293. platform_driver_unregister(&pcf50633_regulator_driver);
  294. }
  295. module_exit(pcf50633_regulator_exit);
  296. MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
  297. MODULE_DESCRIPTION("PCF50633 regulator driver");
  298. MODULE_LICENSE("GPL");
  299. MODULE_ALIAS("platform:pcf50633-regulator");