pcf50633-regulator.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. volt_bits = ldo_voltage_bits(millivolts);
  121. break;
  122. default:
  123. return -EINVAL;
  124. }
  125. *selector = volt_bits;
  126. return pcf50633_reg_write(pcf, regnr, volt_bits);
  127. }
  128. static int pcf50633_regulator_voltage_value(enum pcf50633_regulator_id id,
  129. u8 bits)
  130. {
  131. int millivolts;
  132. switch (id) {
  133. case PCF50633_REGULATOR_AUTO:
  134. millivolts = auto_voltage_value(bits);
  135. break;
  136. case PCF50633_REGULATOR_DOWN1:
  137. millivolts = down_voltage_value(bits);
  138. break;
  139. case PCF50633_REGULATOR_DOWN2:
  140. millivolts = down_voltage_value(bits);
  141. break;
  142. case PCF50633_REGULATOR_LDO1:
  143. case PCF50633_REGULATOR_LDO2:
  144. case PCF50633_REGULATOR_LDO3:
  145. case PCF50633_REGULATOR_LDO4:
  146. case PCF50633_REGULATOR_LDO5:
  147. case PCF50633_REGULATOR_LDO6:
  148. case PCF50633_REGULATOR_HCLDO:
  149. millivolts = ldo_voltage_value(bits);
  150. break;
  151. default:
  152. return -EINVAL;
  153. }
  154. return millivolts * 1000;
  155. }
  156. static int pcf50633_regulator_get_voltage(struct regulator_dev *rdev)
  157. {
  158. struct pcf50633 *pcf;
  159. int regulator_id;
  160. u8 volt_bits, regnr;
  161. pcf = rdev_get_drvdata(rdev);
  162. regulator_id = rdev_get_id(rdev);
  163. if (regulator_id >= PCF50633_NUM_REGULATORS)
  164. return -EINVAL;
  165. regnr = pcf50633_regulator_registers[regulator_id];
  166. volt_bits = pcf50633_reg_read(pcf, regnr);
  167. return pcf50633_regulator_voltage_value(regulator_id, volt_bits);
  168. }
  169. static int pcf50633_regulator_list_voltage(struct regulator_dev *rdev,
  170. unsigned int index)
  171. {
  172. struct pcf50633 *pcf;
  173. int regulator_id;
  174. pcf = rdev_get_drvdata(rdev);
  175. regulator_id = rdev_get_id(rdev);
  176. switch (regulator_id) {
  177. case PCF50633_REGULATOR_AUTO:
  178. index += 0x2f;
  179. break;
  180. case PCF50633_REGULATOR_HCLDO:
  181. index += 0x01;
  182. break;
  183. default:
  184. break;
  185. }
  186. return pcf50633_regulator_voltage_value(regulator_id, index);
  187. }
  188. static int pcf50633_regulator_enable(struct regulator_dev *rdev)
  189. {
  190. struct pcf50633 *pcf = rdev_get_drvdata(rdev);
  191. int regulator_id;
  192. u8 regnr;
  193. regulator_id = rdev_get_id(rdev);
  194. if (regulator_id >= PCF50633_NUM_REGULATORS)
  195. return -EINVAL;
  196. /* The *ENA register is always one after the *OUT register */
  197. regnr = pcf50633_regulator_registers[regulator_id] + 1;
  198. return pcf50633_reg_set_bit_mask(pcf, regnr, PCF50633_REGULATOR_ON,
  199. PCF50633_REGULATOR_ON);
  200. }
  201. static int pcf50633_regulator_disable(struct regulator_dev *rdev)
  202. {
  203. struct pcf50633 *pcf = rdev_get_drvdata(rdev);
  204. int regulator_id;
  205. u8 regnr;
  206. regulator_id = rdev_get_id(rdev);
  207. if (regulator_id >= PCF50633_NUM_REGULATORS)
  208. return -EINVAL;
  209. /* the *ENA register is always one after the *OUT register */
  210. regnr = pcf50633_regulator_registers[regulator_id] + 1;
  211. return pcf50633_reg_set_bit_mask(pcf, regnr,
  212. PCF50633_REGULATOR_ON, 0);
  213. }
  214. static int pcf50633_regulator_is_enabled(struct regulator_dev *rdev)
  215. {
  216. struct pcf50633 *pcf = rdev_get_drvdata(rdev);
  217. int regulator_id = rdev_get_id(rdev);
  218. u8 regnr;
  219. regulator_id = rdev_get_id(rdev);
  220. if (regulator_id >= PCF50633_NUM_REGULATORS)
  221. return -EINVAL;
  222. /* the *ENA register is always one after the *OUT register */
  223. regnr = pcf50633_regulator_registers[regulator_id] + 1;
  224. return pcf50633_reg_read(pcf, regnr) & PCF50633_REGULATOR_ON;
  225. }
  226. static struct regulator_ops pcf50633_regulator_ops = {
  227. .set_voltage = pcf50633_regulator_set_voltage,
  228. .get_voltage = pcf50633_regulator_get_voltage,
  229. .list_voltage = pcf50633_regulator_list_voltage,
  230. .enable = pcf50633_regulator_enable,
  231. .disable = pcf50633_regulator_disable,
  232. .is_enabled = pcf50633_regulator_is_enabled,
  233. };
  234. static struct regulator_desc regulators[] = {
  235. [PCF50633_REGULATOR_AUTO] =
  236. PCF50633_REGULATOR("auto", PCF50633_REGULATOR_AUTO, 80),
  237. [PCF50633_REGULATOR_DOWN1] =
  238. PCF50633_REGULATOR("down1", PCF50633_REGULATOR_DOWN1, 95),
  239. [PCF50633_REGULATOR_DOWN2] =
  240. PCF50633_REGULATOR("down2", PCF50633_REGULATOR_DOWN2, 95),
  241. [PCF50633_REGULATOR_LDO1] =
  242. PCF50633_REGULATOR("ldo1", PCF50633_REGULATOR_LDO1, 27),
  243. [PCF50633_REGULATOR_LDO2] =
  244. PCF50633_REGULATOR("ldo2", PCF50633_REGULATOR_LDO2, 27),
  245. [PCF50633_REGULATOR_LDO3] =
  246. PCF50633_REGULATOR("ldo3", PCF50633_REGULATOR_LDO3, 27),
  247. [PCF50633_REGULATOR_LDO4] =
  248. PCF50633_REGULATOR("ldo4", PCF50633_REGULATOR_LDO4, 27),
  249. [PCF50633_REGULATOR_LDO5] =
  250. PCF50633_REGULATOR("ldo5", PCF50633_REGULATOR_LDO5, 27),
  251. [PCF50633_REGULATOR_LDO6] =
  252. PCF50633_REGULATOR("ldo6", PCF50633_REGULATOR_LDO6, 27),
  253. [PCF50633_REGULATOR_HCLDO] =
  254. PCF50633_REGULATOR("hcldo", PCF50633_REGULATOR_HCLDO, 26),
  255. [PCF50633_REGULATOR_MEMLDO] =
  256. PCF50633_REGULATOR("memldo", PCF50633_REGULATOR_MEMLDO, 0),
  257. };
  258. static int __devinit pcf50633_regulator_probe(struct platform_device *pdev)
  259. {
  260. struct regulator_dev *rdev;
  261. struct pcf50633 *pcf;
  262. /* Already set by core driver */
  263. pcf = dev_to_pcf50633(pdev->dev.parent);
  264. rdev = regulator_register(&regulators[pdev->id], &pdev->dev,
  265. pdev->dev.platform_data, pcf);
  266. if (IS_ERR(rdev))
  267. return PTR_ERR(rdev);
  268. platform_set_drvdata(pdev, rdev);
  269. if (pcf->pdata->regulator_registered)
  270. pcf->pdata->regulator_registered(pcf, pdev->id);
  271. return 0;
  272. }
  273. static int __devexit pcf50633_regulator_remove(struct platform_device *pdev)
  274. {
  275. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  276. platform_set_drvdata(pdev, NULL);
  277. regulator_unregister(rdev);
  278. return 0;
  279. }
  280. static struct platform_driver pcf50633_regulator_driver = {
  281. .driver = {
  282. .name = "pcf50633-regltr",
  283. },
  284. .probe = pcf50633_regulator_probe,
  285. .remove = __devexit_p(pcf50633_regulator_remove),
  286. };
  287. static int __init pcf50633_regulator_init(void)
  288. {
  289. return platform_driver_register(&pcf50633_regulator_driver);
  290. }
  291. subsys_initcall(pcf50633_regulator_init);
  292. static void __exit pcf50633_regulator_exit(void)
  293. {
  294. platform_driver_unregister(&pcf50633_regulator_driver);
  295. }
  296. module_exit(pcf50633_regulator_exit);
  297. MODULE_AUTHOR("Balaji Rao <balajirrao@openmoko.org>");
  298. MODULE_DESCRIPTION("PCF50633 regulator driver");
  299. MODULE_LICENSE("GPL");
  300. MODULE_ALIAS("platform:pcf50633-regulator");