mc13xxx-regulator-core.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. int ret;
  35. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  36. mc13xxx_lock(priv->mc13xxx);
  37. ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
  38. mc13xxx_regulators[id].enable_bit,
  39. mc13xxx_regulators[id].enable_bit);
  40. mc13xxx_unlock(priv->mc13xxx);
  41. return ret;
  42. }
  43. static int mc13xxx_regulator_disable(struct regulator_dev *rdev)
  44. {
  45. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  46. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  47. int id = rdev_get_id(rdev);
  48. int ret;
  49. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  50. mc13xxx_lock(priv->mc13xxx);
  51. ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].reg,
  52. mc13xxx_regulators[id].enable_bit, 0);
  53. mc13xxx_unlock(priv->mc13xxx);
  54. return ret;
  55. }
  56. static int mc13xxx_regulator_is_enabled(struct regulator_dev *rdev)
  57. {
  58. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  59. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  60. int ret, id = rdev_get_id(rdev);
  61. unsigned int val;
  62. mc13xxx_lock(priv->mc13xxx);
  63. ret = mc13xxx_reg_read(priv->mc13xxx, mc13xxx_regulators[id].reg, &val);
  64. mc13xxx_unlock(priv->mc13xxx);
  65. if (ret)
  66. return ret;
  67. return (val & mc13xxx_regulators[id].enable_bit) != 0;
  68. }
  69. int mc13xxx_regulator_list_voltage(struct regulator_dev *rdev,
  70. unsigned selector)
  71. {
  72. int id = rdev_get_id(rdev);
  73. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  74. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  75. if (selector >= mc13xxx_regulators[id].desc.n_voltages)
  76. return -EINVAL;
  77. return mc13xxx_regulators[id].voltages[selector];
  78. }
  79. EXPORT_SYMBOL_GPL(mc13xxx_regulator_list_voltage);
  80. int mc13xxx_get_best_voltage_index(struct regulator_dev *rdev,
  81. int min_uV, int max_uV)
  82. {
  83. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  84. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  85. int reg_id = rdev_get_id(rdev);
  86. int i;
  87. int bestmatch;
  88. int bestindex;
  89. /*
  90. * Locate the minimum voltage fitting the criteria on
  91. * this regulator. The switchable voltages are not
  92. * in strict falling order so we need to check them
  93. * all for the best match.
  94. */
  95. bestmatch = INT_MAX;
  96. bestindex = -1;
  97. for (i = 0; i < mc13xxx_regulators[reg_id].desc.n_voltages; i++) {
  98. if (mc13xxx_regulators[reg_id].voltages[i] >= min_uV &&
  99. mc13xxx_regulators[reg_id].voltages[i] < bestmatch) {
  100. bestmatch = mc13xxx_regulators[reg_id].voltages[i];
  101. bestindex = i;
  102. }
  103. }
  104. if (bestindex < 0 || bestmatch > max_uV) {
  105. dev_warn(&rdev->dev, "no possible value for %d<=x<=%d uV\n",
  106. min_uV, max_uV);
  107. return -EINVAL;
  108. }
  109. return bestindex;
  110. }
  111. EXPORT_SYMBOL_GPL(mc13xxx_get_best_voltage_index);
  112. static int mc13xxx_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
  113. int max_uV, unsigned *selector)
  114. {
  115. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  116. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  117. int value, id = rdev_get_id(rdev);
  118. int ret;
  119. dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
  120. __func__, id, min_uV, max_uV);
  121. /* Find the best index */
  122. value = mc13xxx_get_best_voltage_index(rdev, min_uV, max_uV);
  123. dev_dbg(rdev_get_dev(rdev), "%s best value: %d\n", __func__, value);
  124. if (value < 0)
  125. return value;
  126. mc13xxx_lock(priv->mc13xxx);
  127. ret = mc13xxx_reg_rmw(priv->mc13xxx, mc13xxx_regulators[id].vsel_reg,
  128. mc13xxx_regulators[id].vsel_mask,
  129. value << mc13xxx_regulators[id].vsel_shift);
  130. mc13xxx_unlock(priv->mc13xxx);
  131. return ret;
  132. }
  133. static int mc13xxx_regulator_get_voltage(struct regulator_dev *rdev)
  134. {
  135. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  136. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  137. int ret, id = rdev_get_id(rdev);
  138. unsigned int val;
  139. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  140. mc13xxx_lock(priv->mc13xxx);
  141. ret = mc13xxx_reg_read(priv->mc13xxx,
  142. mc13xxx_regulators[id].vsel_reg, &val);
  143. mc13xxx_unlock(priv->mc13xxx);
  144. if (ret)
  145. return ret;
  146. val = (val & mc13xxx_regulators[id].vsel_mask)
  147. >> mc13xxx_regulators[id].vsel_shift;
  148. dev_dbg(rdev_get_dev(rdev), "%s id: %d val: %d\n", __func__, id, val);
  149. BUG_ON(val >= mc13xxx_regulators[id].desc.n_voltages);
  150. return mc13xxx_regulators[id].voltages[val];
  151. }
  152. struct regulator_ops mc13xxx_regulator_ops = {
  153. .enable = mc13xxx_regulator_enable,
  154. .disable = mc13xxx_regulator_disable,
  155. .is_enabled = mc13xxx_regulator_is_enabled,
  156. .list_voltage = mc13xxx_regulator_list_voltage,
  157. .set_voltage = mc13xxx_regulator_set_voltage,
  158. .get_voltage = mc13xxx_regulator_get_voltage,
  159. };
  160. EXPORT_SYMBOL_GPL(mc13xxx_regulator_ops);
  161. int mc13xxx_fixed_regulator_set_voltage(struct regulator_dev *rdev, int min_uV,
  162. int max_uV, unsigned *selector)
  163. {
  164. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  165. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  166. int id = rdev_get_id(rdev);
  167. dev_dbg(rdev_get_dev(rdev), "%s id: %d min_uV: %d max_uV: %d\n",
  168. __func__, id, min_uV, max_uV);
  169. if (min_uV >= mc13xxx_regulators[id].voltages[0] &&
  170. max_uV <= mc13xxx_regulators[id].voltages[0])
  171. return 0;
  172. else
  173. return -EINVAL;
  174. }
  175. EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_set_voltage);
  176. int mc13xxx_fixed_regulator_get_voltage(struct regulator_dev *rdev)
  177. {
  178. struct mc13xxx_regulator_priv *priv = rdev_get_drvdata(rdev);
  179. struct mc13xxx_regulator *mc13xxx_regulators = priv->mc13xxx_regulators;
  180. int id = rdev_get_id(rdev);
  181. dev_dbg(rdev_get_dev(rdev), "%s id: %d\n", __func__, id);
  182. return mc13xxx_regulators[id].voltages[0];
  183. }
  184. EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_get_voltage);
  185. struct regulator_ops mc13xxx_fixed_regulator_ops = {
  186. .enable = mc13xxx_regulator_enable,
  187. .disable = mc13xxx_regulator_disable,
  188. .is_enabled = mc13xxx_regulator_is_enabled,
  189. .list_voltage = mc13xxx_regulator_list_voltage,
  190. .set_voltage = mc13xxx_fixed_regulator_set_voltage,
  191. .get_voltage = mc13xxx_fixed_regulator_get_voltage,
  192. };
  193. EXPORT_SYMBOL_GPL(mc13xxx_fixed_regulator_ops);
  194. int mc13xxx_sw_regulator_is_enabled(struct regulator_dev *rdev)
  195. {
  196. return 1;
  197. }
  198. EXPORT_SYMBOL_GPL(mc13xxx_sw_regulator_is_enabled);
  199. #ifdef CONFIG_OF
  200. int __devinit mc13xxx_get_num_regulators_dt(struct platform_device *pdev)
  201. {
  202. struct device_node *parent, *child;
  203. int num = 0;
  204. of_node_get(pdev->dev.parent->of_node);
  205. parent = of_find_node_by_name(pdev->dev.parent->of_node, "regulators");
  206. if (!parent)
  207. return -ENODEV;
  208. for_each_child_of_node(parent, child)
  209. num++;
  210. return num;
  211. }
  212. EXPORT_SYMBOL_GPL(mc13xxx_get_num_regulators_dt);
  213. struct mc13xxx_regulator_init_data * __devinit mc13xxx_parse_regulators_dt(
  214. struct platform_device *pdev, struct mc13xxx_regulator *regulators,
  215. int num_regulators)
  216. {
  217. struct mc13xxx_regulator_priv *priv = platform_get_drvdata(pdev);
  218. struct mc13xxx_regulator_init_data *data, *p;
  219. struct device_node *parent, *child;
  220. int i;
  221. of_node_get(pdev->dev.parent->of_node);
  222. parent = of_find_node_by_name(pdev->dev.parent->of_node, "regulators");
  223. if (!parent)
  224. return NULL;
  225. data = devm_kzalloc(&pdev->dev, sizeof(*data) * priv->num_regulators,
  226. GFP_KERNEL);
  227. if (!data)
  228. return NULL;
  229. p = data;
  230. for_each_child_of_node(parent, child) {
  231. for (i = 0; i < num_regulators; i++) {
  232. if (!of_node_cmp(child->name,
  233. regulators[i].desc.name)) {
  234. p->id = i;
  235. p->init_data = of_get_regulator_init_data(
  236. &pdev->dev, child);
  237. p->node = child;
  238. p++;
  239. break;
  240. }
  241. }
  242. }
  243. return data;
  244. }
  245. EXPORT_SYMBOL_GPL(mc13xxx_parse_regulators_dt);
  246. #endif
  247. MODULE_LICENSE("GPL v2");
  248. MODULE_AUTHOR("Yong Shen <yong.shen@linaro.org>");
  249. MODULE_DESCRIPTION("Regulator Driver for Freescale MC13xxx PMIC");
  250. MODULE_ALIAS("mc13xxx-regulator-core");