mc13xxx-regulator-core.c 7.3 KB

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