anatop-regulator.c 6.2 KB

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