mc13xxx-regulator-core.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. int mc13xxx_get_best_voltage_index(struct regulator_dev *rdev,
  77. int min_uV, int max_uV)
  78. {
  79. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  80. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  81. int reg_id = rdev_get_id(rdev);
  82. int i;
  83. int bestmatch;
  84. int bestindex;
  85. /*
  86. * Locate the minimum voltage fitting the criteria on
  87. * this regulator. The switchable voltages are not
  88. * in strict falling order so we need to check them
  89. * all for the best match.
  90. */
  91. bestmatch = INT_MAX;
  92. bestindex = -1;
  93. for (i = 0; i < mc13xxx_regulators[reg_id].desc.n_voltages; i++) {
  94. if (mc13xxx_regulators[reg_id].voltages[i] >= min_uV &&
  95. mc13xxx_regulators[reg_id].voltages[i] < bestmatch) {
  96. bestmatch = mc13xxx_regulators[reg_id].voltages[i];
  97. bestindex = i;
  98. }
  99. }
  100. if (bestindex < 0 || bestmatch > max_uV) {
  101. dev_warn(&rdev->dev, "no possible value for %d<=x<=%d uV\n",
  102. min_uV, max_uV);
  103. return -EINVAL;
  104. }
  105. return bestindex;
  106. }
  107. static int mc13xxx_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
  108. int max_uV, unsigned *selector)
  109. {
  110. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  111. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  112. int value, id = rdev_get_id(rdev);
  113. int ret;
  114. dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
  115. __func__, id, min_uV, max_uV);
  116. /* Find the best index */
  117. value = mc13xxx_get_best_voltage_index(rdev, min_uV, max_uV);
  118. dev_dbg(rdev_get_dev(rdev), "%s best value: %d\n", __func__, value);
  119. if (value < 0)
  120. return value;
  121. mc13xxx_lock(priv->mc13xxx);
  122. ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].vsel_reg,
  123. mc13xxx_regulators[id].vsel_mask,
  124. value << mc13xxx_regulators[id].vsel_shift);
  125. mc13xxx_unlock(priv->mc13xxx);
  126. return ret;
  127. }
  128. static int mc13xxx_regulator_get_voltage(struct regulator_dev *rdev)
  129. {
  130. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  131. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  132. int ret, id = rdev_get_id(rdev);
  133. unsigned int val;
  134. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  135. mc13xxx_lock(priv->mc13xxx);
  136. ret = mc13xxx_reg_read(priv->mc13xxx,
  137. mc13xxx_regulators[id].vsel_reg, &val);
  138. mc13xxx_unlock(priv->mc13xxx);
  139. if (ret)
  140. return ret;
  141. val = (val & mc13xxx_regulators[id].vsel_mask)
  142. >> mc13xxx_regulators[id].vsel_shift;
  143. dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);
  144. BUG_ON(val < 0 || val > mc13xxx_regulators[id].desc.n_voltages);
  145. return mc13xxx_regulators[id].voltages[val];
  146. }
  147. struct regulator_ops mc13xxx_regulator_ops = {
  148. .enable = mc13xxx_regulator_enable,
  149. .disable = mc13xxx_regulator_disable,
  150. .is_enabled = mc13xxx_regulator_is_enabled,
  151. .list_voltage = mc13xxx_regulator_list_voltage,
  152. .set_voltage = mc13xxx_regulator_set_voltage,
  153. .get_voltage = mc13xxx_regulator_get_voltage,
  154. };
  155. int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
  156. int max_uV, unsigned *selector)
  157. {
  158. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  159. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  160. int id = rdev_get_id(rdev);
  161. dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
  162. __func__, id, min_uV, max_uV);
  163. if (min_uV >= mc13xxx_regulators[id].voltages[0] &&
  164. max_uV <= mc13xxx_regulators[id].voltages[0])
  165. return 0;
  166. else
  167. return -EINVAL;
  168. }
  169. int mc13xxx_fixed_regulator_get_voltage(struct regulator_dev *rdev)
  170. {
  171. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  172. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  173. int id = rdev_get_id(rdev);
  174. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  175. return mc13xxx_regulators[id].voltages[0];
  176. }
  177. struct regulator_ops mc13xxx_fixed_regulator_ops = {
  178. .enable = mc13xxx_regulator_enable,
  179. .disable = mc13xxx_regulator_disable,
  180. .is_enabled = mc13xxx_regulator_is_enabled,
  181. .list_voltage = mc13xxx_regulator_list_voltage,
  182. .set_voltage = mc13xxx_fixed_regulator_set_voltage,
  183. .get_voltage = mc13xxx_fixed_regulator_get_voltage,
  184. };
  185. int mc13xxx_sw_regulator_is_enabled(struct regulator_dev *rdev)
  186. {
  187. return 1;
  188. }
  189. MODULE_LICENSE("GPL v2");
  190. MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>");
  191. MODULE_DESCRIPTION("Regulator Driver for Freescale MC13xxx PMIC");
  192. MODULE_ALIAS("mc13xxx-regulator-core");