anatop-regulator.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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, mask;
  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. mask = ((1 << anatop_reg->vol_bit_width) - 1) <<
  65. anatop_reg->vol_bit_shift;
  66. val <<= anatop_reg->vol_bit_shift;
  67. anatop_write_reg(anatop_reg->mfd, anatop_reg->control_reg, val, mask);
  68. return 0;
  69. }
  70. static int anatop_get_voltage_sel(struct regulator_dev *reg)
  71. {
  72. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  73. u32 val;
  74. if (!anatop_reg->control_reg)
  75. return -ENOTSUPP;
  76. val = anatop_read_reg(anatop_reg->mfd, anatop_reg->control_reg);
  77. val = (val & ((1 << anatop_reg->vol_bit_width) - 1)) >>
  78. anatop_reg->vol_bit_shift;
  79. return val - anatop_reg->min_bit_val;
  80. }
  81. static int anatop_list_voltage(struct regulator_dev *reg, unsigned selector)
  82. {
  83. struct anatop_regulator *anatop_reg = rdev_get_drvdata(reg);
  84. int uv;
  85. uv = anatop_reg->min_voltage + selector * 25000;
  86. dev_dbg(&reg->dev, "vddio = %d, selector = %u\n", uv, selector);
  87. return uv;
  88. }
  89. static struct regulator_ops anatop_rops = {
  90. .set_voltage = anatop_set_voltage,
  91. .get_voltage_sel = anatop_get_voltage_sel,
  92. .list_voltage = anatop_list_voltage,
  93. };
  94. static int __devinit anatop_regulator_probe(struct platform_device *pdev)
  95. {
  96. struct device *dev = &pdev->dev;
  97. struct device_node *np = dev->of_node;
  98. struct regulator_desc *rdesc;
  99. struct regulator_dev *rdev;
  100. struct anatop_regulator *sreg;
  101. struct regulator_init_data *initdata;
  102. struct anatop *anatopmfd = dev_get_drvdata(pdev->dev.parent);
  103. int ret = 0;
  104. initdata = of_get_regulator_init_data(dev, np);
  105. sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL);
  106. if (!sreg)
  107. return -ENOMEM;
  108. sreg->initdata = initdata;
  109. sreg->name = kstrdup(of_get_property(np, "regulator-name", NULL),
  110. GFP_KERNEL);
  111. rdesc = &sreg->rdesc;
  112. memset(rdesc, 0, sizeof(*rdesc));
  113. rdesc->name = sreg->name;
  114. rdesc->ops = &anatop_rops;
  115. rdesc->type = REGULATOR_VOLTAGE;
  116. rdesc->owner = THIS_MODULE;
  117. sreg->mfd = anatopmfd;
  118. ret = of_property_read_u32(np, "anatop-reg-offset",
  119. &sreg->control_reg);
  120. if (ret) {
  121. dev_err(dev, "no anatop-reg-offset property set\n");
  122. goto anatop_probe_end;
  123. }
  124. ret = of_property_read_u32(np, "anatop-vol-bit-width",
  125. &sreg->vol_bit_width);
  126. if (ret) {
  127. dev_err(dev, "no anatop-vol-bit-width property set\n");
  128. goto anatop_probe_end;
  129. }
  130. ret = of_property_read_u32(np, "anatop-vol-bit-shift",
  131. &sreg->vol_bit_shift);
  132. if (ret) {
  133. dev_err(dev, "no anatop-vol-bit-shift property set\n");
  134. goto anatop_probe_end;
  135. }
  136. ret = of_property_read_u32(np, "anatop-min-bit-val",
  137. &sreg->min_bit_val);
  138. if (ret) {
  139. dev_err(dev, "no anatop-min-bit-val property set\n");
  140. goto anatop_probe_end;
  141. }
  142. ret = of_property_read_u32(np, "anatop-min-voltage",
  143. &sreg->min_voltage);
  144. if (ret) {
  145. dev_err(dev, "no anatop-min-voltage property set\n");
  146. goto anatop_probe_end;
  147. }
  148. ret = of_property_read_u32(np, "anatop-max-voltage",
  149. &sreg->max_voltage);
  150. if (ret) {
  151. dev_err(dev, "no anatop-max-voltage property set\n");
  152. goto anatop_probe_end;
  153. }
  154. rdesc->n_voltages = (sreg->max_voltage - sreg->min_voltage)
  155. / 25000 + 1;
  156. /* register regulator */
  157. rdev = regulator_register(rdesc, dev,
  158. initdata, sreg, pdev->dev.of_node);
  159. if (IS_ERR(rdev)) {
  160. dev_err(dev, "failed to register %s\n",
  161. rdesc->name);
  162. ret = PTR_ERR(rdev);
  163. goto anatop_probe_end;
  164. }
  165. platform_set_drvdata(pdev, rdev);
  166. anatop_probe_end:
  167. if (ret)
  168. kfree(sreg->name);
  169. return ret;
  170. }
  171. static int __devexit anatop_regulator_remove(struct platform_device *pdev)
  172. {
  173. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  174. struct anatop_regulator *sreg = rdev_get_drvdata(rdev);
  175. const char *name = sreg->name;
  176. regulator_unregister(rdev);
  177. kfree(name);
  178. return 0;
  179. }
  180. static struct of_device_id __devinitdata of_anatop_regulator_match_tbl[] = {
  181. { .compatible = "fsl,anatop-regulator", },
  182. { /* end */ }
  183. };
  184. static struct platform_driver anatop_regulator_driver = {
  185. .driver = {
  186. .name = "anatop_regulator",
  187. .owner = THIS_MODULE,
  188. .of_match_table = of_anatop_regulator_match_tbl,
  189. },
  190. .probe = anatop_regulator_probe,
  191. .remove = anatop_regulator_remove,
  192. };
  193. static int __init anatop_regulator_init(void)
  194. {
  195. return platform_driver_register(&anatop_regulator_driver);
  196. }
  197. postcore_initcall(anatop_regulator_init);
  198. static void __exit anatop_regulator_exit(void)
  199. {
  200. platform_driver_unregister(&anatop_regulator_driver);
  201. }
  202. module_exit(anatop_regulator_exit);
  203. MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>, "
  204. "Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
  205. MODULE_DESCRIPTION("ANATOP Regulator driver");
  206. MODULE_LICENSE("GPL v2");