anatop-regulator.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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, "anatop-reg-offset",
  121. &sreg->control_reg);
  122. if (ret) {
  123. dev_err(dev, "no anatop-reg-offset property set\n");
  124. goto anatop_probe_end;
  125. }
  126. ret = of_property_read_u32(np, "anatop-vol-bit-width",
  127. &sreg->vol_bit_width);
  128. if (ret) {
  129. dev_err(dev, "no anatop-vol-bit-width property set\n");
  130. goto anatop_probe_end;
  131. }
  132. ret = of_property_read_u32(np, "anatop-vol-bit-shift",
  133. &sreg->vol_bit_shift);
  134. if (ret) {
  135. dev_err(dev, "no anatop-vol-bit-shift property set\n");
  136. goto anatop_probe_end;
  137. }
  138. ret = of_property_read_u32(np, "anatop-min-bit-val",
  139. &sreg->min_bit_val);
  140. if (ret) {
  141. dev_err(dev, "no anatop-min-bit-val property set\n");
  142. goto anatop_probe_end;
  143. }
  144. ret = of_property_read_u32(np, "anatop-min-voltage",
  145. &sreg->min_voltage);
  146. if (ret) {
  147. dev_err(dev, "no anatop-min-voltage property set\n");
  148. goto anatop_probe_end;
  149. }
  150. ret = of_property_read_u32(np, "anatop-max-voltage",
  151. &sreg->max_voltage);
  152. if (ret) {
  153. dev_err(dev, "no anatop-max-voltage property set\n");
  154. goto anatop_probe_end;
  155. }
  156. rdesc->n_voltages = (sreg->max_voltage - sreg->min_voltage)
  157. / 25000 + 1;
  158. /* register regulator */
  159. rdev = regulator_register(rdesc, dev,
  160. initdata, sreg, pdev->dev.of_node);
  161. if (IS_ERR(rdev)) {
  162. dev_err(dev, "failed to register %s\n",
  163. rdesc->name);
  164. ret = PTR_ERR(rdev);
  165. goto anatop_probe_end;
  166. }
  167. platform_set_drvdata(pdev, rdev);
  168. anatop_probe_end:
  169. if (ret)
  170. kfree(sreg->name);
  171. return ret;
  172. }
  173. static int __devexit anatop_regulator_remove(struct platform_device *pdev)
  174. {
  175. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  176. struct anatop_regulator *sreg = rdev_get_drvdata(rdev);
  177. const char *name = sreg->name;
  178. regulator_unregister(rdev);
  179. kfree(name);
  180. return 0;
  181. }
  182. static struct of_device_id __devinitdata of_anatop_regulator_match_tbl[] = {
  183. { .compatible = "fsl,anatop-regulator", },
  184. { /* end */ }
  185. };
  186. static struct platform_driver anatop_regulator_driver = {
  187. .driver = {
  188. .name = "anatop_regulator",
  189. .owner = THIS_MODULE,
  190. .of_match_table = of_anatop_regulator_match_tbl,
  191. },
  192. .probe = anatop_regulator_probe,
  193. .remove = anatop_regulator_remove,
  194. };
  195. static int __init anatop_regulator_init(void)
  196. {
  197. return platform_driver_register(&anatop_regulator_driver);
  198. }
  199. postcore_initcall(anatop_regulator_init);
  200. static void __exit anatop_regulator_exit(void)
  201. {
  202. platform_driver_unregister(&anatop_regulator_driver);
  203. }
  204. module_exit(anatop_regulator_exit);
  205. MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>, "
  206. "Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
  207. MODULE_DESCRIPTION("ANATOP Regulator driver");
  208. MODULE_LICENSE("GPL v2");