anatop-regulator.c 6.1 KB

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