anatop-regulator.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (C) 2011 Freescale Semiconductor, Inc. All Rights Reserved.
  3. */
  4. /*
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include <linux/slab.h>
  18. #include <linux/device.h>
  19. #include <linux/module.h>
  20. #include <linux/err.h>
  21. #include <linux/io.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/of.h>
  24. #include <linux/of_address.h>
  25. #include <linux/mfd/anatop.h>
  26. #include <linux/regulator/driver.h>
  27. #include <linux/regulator/of_regulator.h>
  28. struct anatop_regulator {
  29. const char *name;
  30. u32 control_reg;
  31. struct anatop *mfd;
  32. int vol_bit_shift;
  33. int vol_bit_width;
  34. int min_bit_val;
  35. int min_voltage;
  36. int max_voltage;
  37. struct regulator_desc rdesc;
  38. struct regulator_init_data *initdata;
  39. };
  40. static int anatop_set_voltage(struct regulator_dev *reg, int min_uV,
  41. int max_uV, unsigned *selector)
  42. {
  43. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  44. u32 val, sel;
  45. int uv;
  46. uv = min_uV;
  47. dev_dbg(&reg->dev, "%s: uv %d, min %d, max %d\n", __func__,
  48. uv, anatop_reg->min_voltage,
  49. anatop_reg->max_voltage);
  50. if (uv < anatop_reg->min_voltage) {
  51. if (max_uV > anatop_reg->min_voltage)
  52. uv = anatop_reg->min_voltage;
  53. else
  54. return -EINVAL;
  55. }
  56. if (!anatop_reg->control_reg)
  57. return -ENOTSUPP;
  58. sel = DIV_ROUND_UP(uv - anatop_reg->min_voltage, 25000);
  59. if (sel * 25000 + anatop_reg->min_voltage > anatop_reg->max_voltage)
  60. return -EINVAL;
  61. val = anatop_reg->min_bit_val + sel;
  62. *selector = sel;
  63. dev_dbg(&reg->dev, "%s: calculated val %d\n", __func__, val);
  64. anatop_set_bits(anatop_reg->mfd,
  65. anatop_reg->control_reg,
  66. anatop_reg->vol_bit_shift,
  67. anatop_reg->vol_bit_width,
  68. val);
  69. return 0;
  70. }
  71. static int anatop_get_voltage_sel(struct regulator_dev *reg)
  72. {
  73. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  74. u32 val;
  75. if (!anatop_reg->control_reg)
  76. return -ENOTSUPP;
  77. val = anatop_get_bits(anatop_reg->mfd,
  78. anatop_reg->control_reg,
  79. anatop_reg->vol_bit_shift,
  80. anatop_reg->vol_bit_width);
  81. return val - anatop_reg->min_bit_val;
  82. }
  83. static int anatop_list_voltage(struct regulator_dev *reg, unsigned selector)
  84. {
  85. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  86. int uv;
  87. uv = anatop_reg->min_voltage + selector * 25000;
  88. dev_dbg(&reg->dev, "vddio = %d, selector = %u\n", uv, selector);
  89. return uv;
  90. }
  91. static struct regulator_ops anatop_rops = {
  92. .set_voltage = anatop_set_voltage,
  93. .get_voltage_sel = anatop_get_voltage_sel,
  94. .list_voltage = anatop_list_voltage,
  95. };
  96. static int __devinit anatop_regulator_probe(struct platform_device *pdev)
  97. {
  98. struct device *dev = &pdev->dev;
  99. struct device_node *np = dev->of_node;
  100. struct regulator_desc *rdesc;
  101. struct regulator_dev *rdev;
  102. struct anatop_regulator *sreg;
  103. struct regulator_init_data *initdata;
  104. struct anatop *anatopmfd = dev_get_drvdata(pdev->dev.parent);
  105. int ret = 0;
  106. initdata = of_get_regulator_init_data(dev, np);
  107. sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL);
  108. if (!sreg)
  109. return -ENOMEM;
  110. sreg->initdata = initdata;
  111. sreg->name = kstrdup(of_get_property(np, "regulator-name", NULL),
  112. GFP_KERNEL);
  113. rdesc = &sreg->rdesc;
  114. memset(rdesc, 0, sizeof(*rdesc));
  115. rdesc->name = sreg->name;
  116. rdesc->ops = &anatop_rops;
  117. rdesc->type = REGULATOR_VOLTAGE;
  118. rdesc->owner = THIS_MODULE;
  119. sreg->mfd = anatopmfd;
  120. ret = of_property_read_u32(np, "reg", &sreg->control_reg);
  121. if (ret) {
  122. dev_err(dev, "no reg property set\n");
  123. goto anatop_probe_end;
  124. }
  125. ret = of_property_read_u32(np, "anatop-vol-bit-width",
  126. &sreg->vol_bit_width);
  127. if (ret) {
  128. dev_err(dev, "no anatop-vol-bit-width property set\n");
  129. goto anatop_probe_end;
  130. }
  131. ret = of_property_read_u32(np, "anatop-vol-bit-shift",
  132. &sreg->vol_bit_shift);
  133. if (ret) {
  134. dev_err(dev, "no anatop-vol-bit-shift property set\n");
  135. goto anatop_probe_end;
  136. }
  137. ret = of_property_read_u32(np, "anatop-min-bit-val",
  138. &sreg->min_bit_val);
  139. if (ret) {
  140. dev_err(dev, "no anatop-min-bit-val property set\n");
  141. goto anatop_probe_end;
  142. }
  143. ret = of_property_read_u32(np, "anatop-min-voltage",
  144. &sreg->min_voltage);
  145. if (ret) {
  146. dev_err(dev, "no anatop-min-voltage property set\n");
  147. goto anatop_probe_end;
  148. }
  149. ret = of_property_read_u32(np, "anatop-max-voltage",
  150. &sreg->max_voltage);
  151. if (ret) {
  152. dev_err(dev, "no anatop-max-voltage property set\n");
  153. goto anatop_probe_end;
  154. }
  155. rdesc->n_voltages = (sreg->max_voltage - sreg->min_voltage)
  156. / 25000 + 1;
  157. /* register regulator */
  158. rdev = regulator_register(rdesc, dev,
  159. initdata, sreg, pdev->dev.of_node);
  160. if (IS_ERR(rdev)) {
  161. dev_err(dev, "failed to register %s\n",
  162. rdesc->name);
  163. ret = PTR_ERR(rdev);
  164. goto anatop_probe_end;
  165. }
  166. platform_set_drvdata(pdev, rdev);
  167. anatop_probe_end:
  168. if (ret)
  169. kfree(sreg->name);
  170. return ret;
  171. }
  172. static int __devexit anatop_regulator_remove(struct platform_device *pdev)
  173. {
  174. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  175. struct anatop_regulator *sreg = rdev_get_drvdata(rdev);
  176. const char *name = sreg->name;
  177. regulator_unregister(rdev);
  178. kfree(name);
  179. return 0;
  180. }
  181. static struct of_device_id __devinitdata of_anatop_regulator_match_tbl[] = {
  182. { .compatible = "fsl,anatop-regulator", },
  183. { /* end */ }
  184. };
  185. static struct platform_driver anatop_regulator = {
  186. .driver = {
  187. .name = "anatop_regulator",
  188. .owner = THIS_MODULE,
  189. .of_match_table = of_anatop_regulator_match_tbl,
  190. },
  191. .probe = anatop_regulator_probe,
  192. .remove = anatop_regulator_remove,
  193. };
  194. static int __init anatop_regulator_init(void)
  195. {
  196. return platform_driver_register(&anatop_regulator);
  197. }
  198. postcore_initcall(anatop_regulator_init);
  199. static void __exit anatop_regulator_exit(void)
  200. {
  201. platform_driver_unregister(&anatop_regulator);
  202. }
  203. module_exit(anatop_regulator_exit);
  204. MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>, "
  205. "Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
  206. MODULE_DESCRIPTION("ANATOP Regulator driver");
  207. MODULE_LICENSE("GPL v2");