mc13xxx-regulator-core.c 6.1 KB

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