anatop-regulator.c 6.0 KB

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