mc13xxx-regulator-core.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Regulator Driver for Freescale MC13xxx PMIC
  3. *
  4. * Copyright 2010 Yong Shen <yong.shen@linaro.org>
  5. *
  6. * Based on mc13783 regulator driver :
  7. * Copyright (C) 2008 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
  8. * Copyright 2009 Alberto Panizzo <maramaopercheseimorto@gmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * Regs infos taken from mc13xxx drivers from freescale and mc13xxx.pdf file
  15. * from freescale
  16. */
  17. #include <linux/mfd/mc13xxx.h>
  18. #include <linux/regulator/machine.h>
  19. #include <linux/regulator/driver.h>
  20. #include <linux/regulator/of_regulator.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/init.h>
  25. #include <linux/err.h>
  26. #include <linux/module.h>
  27. #include <linux/of.h>
  28. #include "mc13xxx.h"
  29. static int mc13xxx_regulator_enable(struct regulator_dev *rdev)
  30. {
  31. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  32. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  33. int id = rdev_get_id(rdev);
  34. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  35. return mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
  36. mc13xxx_regulators[id].enable_bit,
  37. mc13xxx_regulators[id].enable_bit);
  38. }
  39. static int mc13xxx_regulator_disable(struct regulator_dev *rdev)
  40. {
  41. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  42. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  43. int id = rdev_get_id(rdev);
  44. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  45. return mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
  46. mc13xxx_regulators[id].enable_bit, 0);
  47. }
  48. static int mc13xxx_regulator_is_enabled(struct regulator_dev *rdev)
  49. {
  50. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  51. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  52. int ret, id = rdev_get_id(rdev);
  53. unsigned int val;
  54. ret = mc13xxx_reg_read(priv->mc13xxx, mc13xxx_regulators[id].reg, &val);
  55. if (ret)
  56. return ret;
  57. return (val & mc13xxx_regulators[id].enable_bit) != 0;
  58. }
  59. static int mc13xxx_regulator_set_voltage_sel(struct regulator_dev *rdev,
  60. unsigned selector)
  61. {
  62. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  63. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  64. int id = rdev_get_id(rdev);
  65. return mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].vsel_reg,
  66. mc13xxx_regulators[id].vsel_mask,
  67. selector << mc13xxx_regulators[id].vsel_shift);
  68. }
  69. static int mc13xxx_regulator_get_voltage(struct regulator_dev *rdev)
  70. {
  71. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  72. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  73. int ret, id = rdev_get_id(rdev);
  74. unsigned int val;
  75. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  76. ret = mc13xxx_reg_read(priv->mc13xxx,
  77. mc13xxx_regulators[id].vsel_reg, &val);
  78. if (ret)
  79. return ret;
  80. val = (val & mc13xxx_regulators[id].vsel_mask)
  81. >> mc13xxx_regulators[id].vsel_shift;
  82. dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);
  83. BUG_ON(val >= mc13xxx_regulators[id].desc.n_voltages);
  84. return rdev->desc->volt_table[val];
  85. }
  86. struct regulator_ops mc13xxx_regulator_ops = {
  87. .enable = mc13xxx_regulator_enable,
  88. .disable = mc13xxx_regulator_disable,
  89. .is_enabled = mc13xxx_regulator_is_enabled,
  90. .list_voltage = regulator_list_voltage_table,
  91. .set_voltage_sel = mc13xxx_regulator_set_voltage_sel,
  92. .get_voltage = mc13xxx_regulator_get_voltage,
  93. };
  94. EXPORT_SYMBOL_GPL(mc13xxx_regulator_ops);
  95. int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
  96. int max_uV, unsigned *selector)
  97. {
  98. int id = rdev_get_id(rdev);
  99. dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
  100. __func__, id, min_uV, max_uV);
  101. if (min_uV <= rdev->desc->volt_table[0] &&
  102. rdev->desc->volt_table[0] <= max_uV) {
  103. *selector = 0;
  104. return 0;
  105. } else {
  106. return -EINVAL;
  107. }
  108. }
  109. EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_set_voltage);
  110. struct regulator_ops mc13xxx_fixed_regulator_ops = {
  111. .enable = mc13xxx_regulator_enable,
  112. .disable = mc13xxx_regulator_disable,
  113. .is_enabled = mc13xxx_regulator_is_enabled,
  114. .list_voltage = regulator_list_voltage_table,
  115. .set_voltage = mc13xxx_fixed_regulator_set_voltage,
  116. };
  117. EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_ops);
  118. #ifdef CONFIG_OF
  119. int mc13xxx_get_num_regulators_dt(struct platform_device *pdev)
  120. {
  121. struct device_node *parent;
  122. int num;
  123. if (!pdev->dev.parent->of_node)
  124. return -ENODEV;
  125. parent = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
  126. if (!parent)
  127. return -ENODEV;
  128. num = of_get_child_count(parent);
  129. of_node_put(parent);
  130. return num;
  131. }
  132. EXPORT_SYMBOL_GPL(mc13xxx_get_num_regulators_dt);
  133. struct mc13xxx_regulator_init_data *mc13xxx_parse_regulators_dt(
  134. struct platform_device *pdev, struct mc13xxx_regulator *regulators,
  135. int num_regulators)
  136. {
  137. struct mc13xxx_regulator_priv *priv = platform_get_drvdata(pdev);
  138. struct mc13xxx_regulator_init_data *data, *p;
  139. struct device_node *parent, *child;
  140. int i, parsed = 0;
  141. if (!pdev->dev.parent->of_node)
  142. return NULL;
  143. parent = of_get_child_by_name(pdev->dev.parent->of_node, "regulators");
  144. if (!parent)
  145. return NULL;
  146. data = devm_kzalloc(&pdev->dev, sizeof(*data) * priv->num_regulators,
  147. GFP_KERNEL);
  148. if (!data) {
  149. of_node_put(parent);
  150. return NULL;
  151. }
  152. p = data;
  153. for_each_child_of_node(parent, child) {
  154. int found = 0;
  155. for (i = 0; i < num_regulators; i++) {
  156. if (!regulators[i].desc.name)
  157. continue;
  158. if (!of_node_cmp(child->name,
  159. regulators[i].desc.name)) {
  160. p->id = i;
  161. p->init_data = of_get_regulator_init_data(
  162. &pdev->dev, child,
  163. &regulators[i].desc);
  164. p->node = child;
  165. p++;
  166. parsed++;
  167. found = 1;
  168. break;
  169. }
  170. }
  171. if (!found)
  172. dev_warn(&pdev->dev,
  173. "Unknown regulator: %s\n", child->name);
  174. }
  175. of_node_put(parent);
  176. priv->num_regulators = parsed;
  177. return data;
  178. }
  179. EXPORT_SYMBOL_GPL(mc13xxx_parse_regulators_dt);
  180. #endif
  181. MODULE_LICENSE("GPL v2");
  182. MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>");
  183. MODULE_DESCRIPTION("Regulator Driver for Freescale MC13xxx PMIC");
  184. MODULE_ALIAS("mc13xxx-regulator-core");