anatop-regulator.c 6.3 KB

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