mc13xxx-regulator-core.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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/platform_device.h>
  21. #include <linux/kernel.h>
  22. #include <linux/slab.h>
  23. #include <linux/init.h>
  24. #include <linux/err.h>
  25. #include "mc13xxx.h"
  26. static int mc13xxx_regulator_enable(struct regulator_dev *rdev)
  27. {
  28. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  29. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  30. int id = rdev_get_id(rdev);
  31. int ret;
  32. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  33. mc13xxx_lock(priv->mc13xxx);
  34. ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
  35. mc13xxx_regulators[id].enable_bit,
  36. mc13xxx_regulators[id].enable_bit);
  37. mc13xxx_unlock(priv->mc13xxx);
  38. return ret;
  39. }
  40. static int mc13xxx_regulator_disable(struct regulator_dev *rdev)
  41. {
  42. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  43. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  44. int id = rdev_get_id(rdev);
  45. int ret;
  46. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  47. mc13xxx_lock(priv->mc13xxx);
  48. ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
  49. mc13xxx_regulators[id].enable_bit, 0);
  50. mc13xxx_unlock(priv->mc13xxx);
  51. return ret;
  52. }
  53. static int mc13xxx_regulator_is_enabled(struct regulator_dev *rdev)
  54. {
  55. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  56. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  57. int ret, id = rdev_get_id(rdev);
  58. unsigned int val;
  59. mc13xxx_lock(priv->mc13xxx);
  60. ret = mc13xxx_reg_read(priv->mc13xxx, mc13xxx_regulators[id].reg, &val);
  61. mc13xxx_unlock(priv->mc13xxx);
  62. if (ret)
  63. return ret;
  64. return (val & mc13xxx_regulators[id].enable_bit) != 0;
  65. }
  66. int mc13xxx_regulator_list_voltage(struct regulator_dev *rdev,
  67. unsigned selector)
  68. {
  69. int id = rdev_get_id(rdev);
  70. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  71. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  72. if (selector >= mc13xxx_regulators[id].desc.n_voltages)
  73. return -EINVAL;
  74. return mc13xxx_regulators[id].voltages[selector];
  75. }
  76. EXPORT_SYMBOL_GPL(mc13xxx_regulator_list_voltage);
  77. int mc13xxx_get_best_voltage_index(struct regulator_dev *rdev,
  78. int min_uV, int max_uV)
  79. {
  80. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  81. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  82. int reg_id = rdev_get_id(rdev);
  83. int i;
  84. int bestmatch;
  85. int bestindex;
  86. /*
  87. * Locate the minimum voltage fitting the criteria on
  88. * this regulator. The switchable voltages are not
  89. * in strict falling order so we need to check them
  90. * all for the best match.
  91. */
  92. bestmatch = INT_MAX;
  93. bestindex = -1;
  94. for (i = 0; i < mc13xxx_regulators[reg_id].desc.n_voltages; i++) {
  95. if (mc13xxx_regulators[reg_id].voltages[i] >= min_uV &&
  96. mc13xxx_regulators[reg_id].voltages[i] < bestmatch) {
  97. bestmatch = mc13xxx_regulators[reg_id].voltages[i];
  98. bestindex = i;
  99. }
  100. }
  101. if (bestindex < 0 || bestmatch > max_uV) {
  102. dev_warn(&rdev->dev, "no possible value for %d<=x<=%d uV\n",
  103. min_uV, max_uV);
  104. return -EINVAL;
  105. }
  106. return bestindex;
  107. }
  108. EXPORT_SYMBOL_GPL(mc13xxx_get_best_voltage_index);
  109. static int mc13xxx_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
  110. int max_uV, unsigned *selector)
  111. {
  112. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  113. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  114. int value, id = rdev_get_id(rdev);
  115. int ret;
  116. dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
  117. __func__, id, min_uV, max_uV);
  118. /* Find the best index */
  119. value = mc13xxx_get_best_voltage_index(rdev, min_uV, max_uV);
  120. dev_dbg(rdev_get_dev(rdev), "%s best value: %d\n", __func__, value);
  121. if (value < 0)
  122. return value;
  123. mc13xxx_lock(priv->mc13xxx);
  124. ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].vsel_reg,
  125. mc13xxx_regulators[id].vsel_mask,
  126. value << mc13xxx_regulators[id].vsel_shift);
  127. mc13xxx_unlock(priv->mc13xxx);
  128. return ret;
  129. }
  130. static int mc13xxx_regulator_get_voltage(struct regulator_dev *rdev)
  131. {
  132. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  133. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  134. int ret, id = rdev_get_id(rdev);
  135. unsigned int val;
  136. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  137. mc13xxx_lock(priv->mc13xxx);
  138. ret = mc13xxx_reg_read(priv->mc13xxx,
  139. mc13xxx_regulators[id].vsel_reg, &val);
  140. mc13xxx_unlock(priv->mc13xxx);
  141. if (ret)
  142. return ret;
  143. val = (val & mc13xxx_regulators[id].vsel_mask)
  144. >> mc13xxx_regulators[id].vsel_shift;
  145. dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);
  146. BUG_ON(val > mc13xxx_regulators[id].desc.n_voltages);
  147. return mc13xxx_regulators[id].voltages[val];
  148. }
  149. struct regulator_ops mc13xxx_regulator_ops = {
  150. .enable = mc13xxx_regulator_enable,
  151. .disable = mc13xxx_regulator_disable,
  152. .is_enabled = mc13xxx_regulator_is_enabled,
  153. .list_voltage = mc13xxx_regulator_list_voltage,
  154. .set_voltage = mc13xxx_regulator_set_voltage,
  155. .get_voltage = mc13xxx_regulator_get_voltage,
  156. };
  157. EXPORT_SYMBOL_GPL(mc13xxx_regulator_ops);
  158. int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
  159. int max_uV, unsigned *selector)
  160. {
  161. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  162. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  163. int id = rdev_get_id(rdev);
  164. dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
  165. __func__, id, min_uV, max_uV);
  166. if (min_uV >= mc13xxx_regulators[id].voltages[0] &&
  167. max_uV <= mc13xxx_regulators[id].voltages[0])
  168. return 0;
  169. else
  170. return -EINVAL;
  171. }
  172. EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_set_voltage);
  173. int mc13xxx_fixed_regulator_get_voltage(struct regulator_dev *rdev)
  174. {
  175. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  176. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  177. int id = rdev_get_id(rdev);
  178. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  179. return mc13xxx_regulators[id].voltages[0];
  180. }
  181. EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_get_voltage);
  182. struct regulator_ops mc13xxx_fixed_regulator_ops = {
  183. .enable = mc13xxx_regulator_enable,
  184. .disable = mc13xxx_regulator_disable,
  185. .is_enabled = mc13xxx_regulator_is_enabled,
  186. .list_voltage = mc13xxx_regulator_list_voltage,
  187. .set_voltage = mc13xxx_fixed_regulator_set_voltage,
  188. .get_voltage = mc13xxx_fixed_regulator_get_voltage,
  189. };
  190. EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_ops);
  191. int mc13xxx_sw_regulator_is_enabled(struct regulator_dev *rdev)
  192. {
  193. return 1;
  194. }
  195. EXPORT_SYMBOL_GPL(mc13xxx_sw_regulator_is_enabled);
  196. MODULE_LICENSE("GPL v2");
  197. MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>");
  198. MODULE_DESCRIPTION("Regulator Driver for Freescale MC13xxx PMIC");
  199. MODULE_ALIAS("mc13xxx-regulator-core");