anatop-regulator.c 6.0 KB

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