anatop-regulator.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. struct regulator_config config = { };
  104. int ret = 0;
  105. initdata = of_get_regulator_init_data(dev, np);
  106. sreg = devm_kzalloc(dev, sizeof(*sreg), GFP_KERNEL);
  107. if (!sreg)
  108. return -ENOMEM;
  109. sreg->initdata = initdata;
  110. sreg->name = kstrdup(of_get_property(np, "regulator-name", NULL),
  111. GFP_KERNEL);
  112. rdesc = &sreg->rdesc;
  113. memset(rdesc, 0, sizeof(*rdesc));
  114. rdesc->name = sreg->name;
  115. rdesc->ops = &anatop_rops;
  116. rdesc->type = REGULATOR_VOLTAGE;
  117. rdesc->owner = THIS_MODULE;
  118. sreg->mfd = anatopmfd;
  119. ret = of_property_read_u32(np, "anatop-reg-offset",
  120. &sreg->control_reg);
  121. if (ret) {
  122. dev_err(dev, "no anatop-reg-offset 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. config.dev = &pdev->dev;
  158. config.init_data = initdata;
  159. config.driver_data = sreg;
  160. config.of_node = pdev->dev.of_node;
  161. /* register regulator */
  162. rdev = regulator_register(rdesc, &config);
  163. if (IS_ERR(rdev)) {
  164. dev_err(dev, "failed to register %s\n",
  165. rdesc->name);
  166. ret = PTR_ERR(rdev);
  167. goto anatop_probe_end;
  168. }
  169. platform_set_drvdata(pdev, rdev);
  170. anatop_probe_end:
  171. if (ret)
  172. kfree(sreg->name);
  173. return ret;
  174. }
  175. static int __devexit anatop_regulator_remove(struct platform_device *pdev)
  176. {
  177. struct regulator_dev *rdev = platform_get_drvdata(pdev);
  178. struct anatop_regulator *sreg = rdev_get_drvdata(rdev);
  179. const char *name = sreg->name;
  180. regulator_unregister(rdev);
  181. kfree(name);
  182. return 0;
  183. }
  184. static struct of_device_id __devinitdata of_anatop_regulator_match_tbl[] = {
  185. { .compatible = "fsl,anatop-regulator", },
  186. { /* end */ }
  187. };
  188. static struct platform_driver anatop_regulator_driver = {
  189. .driver = {
  190. .name = "anatop_regulator",
  191. .owner = THIS_MODULE,
  192. .of_match_table = of_anatop_regulator_match_tbl,
  193. },
  194. .probe = anatop_regulator_probe,
  195. .remove = anatop_regulator_remove,
  196. };
  197. static int __init anatop_regulator_init(void)
  198. {
  199. return platform_driver_register(&anatop_regulator_driver);
  200. }
  201. postcore_initcall(anatop_regulator_init);
  202. static void __exit anatop_regulator_exit(void)
  203. {
  204. platform_driver_unregister(&anatop_regulator_driver);
  205. }
  206. module_exit(anatop_regulator_exit);
  207. MODULE_AUTHOR("Nancy Chen <Nancy.Chen@freescale.com>, "
  208. "Ying-Chun Liu (PaulLiu) <paul.liu@linaro.org>");
  209. MODULE_DESCRIPTION("ANATOP Regulator driver");
  210. MODULE_LICENSE("GPL v2");