gpio-regulator.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * gpio-regulator.c
  3. *
  4. * Copyright 2011 Heiko Stuebner <heiko@sntech.de>
  5. *
  6. * based on fixed.c
  7. *
  8. * Copyright 2008 Wolfson Microelectronics PLC.
  9. *
  10. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  11. *
  12. * Copyright (c) 2009 Nokia Corporation
  13. * Roger Quadros <ext-roger.quadros@nokia.com>
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License as
  17. * published by the Free Software Foundation; either version 2 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This is useful for systems with mixed controllable and
  21. * non-controllable regulators, as well as for allowing testing on
  22. * systems with no controllable regulators.
  23. */
  24. #include <linux/err.h>
  25. #include <linux/mutex.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/regulator/driver.h>
  29. #include <linux/regulator/machine.h>
  30. #include <linux/regulator/gpio-regulator.h>
  31. #include <linux/gpio.h>
  32. #include <linux/slab.h>
  33. struct gpio_regulator_data {
  34. struct regulator_desc desc;
  35. struct regulator_dev *dev;
  36. struct gpio *gpios;
  37. int nr_gpios;
  38. struct gpio_regulator_state *states;
  39. int nr_states;
  40. int state;
  41. };
  42. static int gpio_regulator_get_value(struct regulator_dev *dev)
  43. {
  44. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  45. int ptr;
  46. for (ptr = 0; ptr < data->nr_states; ptr++)
  47. if (data->states[ptr].gpios == data->state)
  48. return data->states[ptr].value;
  49. return -EINVAL;
  50. }
  51. static int gpio_regulator_set_voltage(struct regulator_dev *dev,
  52. int min_uV, int max_uV,
  53. unsigned *selector)
  54. {
  55. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  56. int ptr, target = 0, state, best_val = INT_MAX;
  57. for (ptr = 0; ptr < data->nr_states; ptr++)
  58. if (data->states[ptr].value < best_val &&
  59. data->states[ptr].value >= min_uV &&
  60. data->states[ptr].value <= max_uV) {
  61. target = data->states[ptr].gpios;
  62. best_val = data->states[ptr].value;
  63. if (selector)
  64. *selector = ptr;
  65. }
  66. if (best_val == INT_MAX)
  67. return -EINVAL;
  68. for (ptr = 0; ptr < data->nr_gpios; ptr++) {
  69. state = (target & (1 << ptr)) >> ptr;
  70. gpio_set_value(data->gpios[ptr].gpio, state);
  71. }
  72. data->state = target;
  73. return 0;
  74. }
  75. static int gpio_regulator_list_voltage(struct regulator_dev *dev,
  76. unsigned selector)
  77. {
  78. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  79. if (selector >= data->nr_states)
  80. return -EINVAL;
  81. return data->states[selector].value;
  82. }
  83. static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
  84. int min_uA, int max_uA)
  85. {
  86. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  87. int ptr, target = 0, state, best_val = 0;
  88. for (ptr = 0; ptr < data->nr_states; ptr++)
  89. if (data->states[ptr].value > best_val &&
  90. data->states[ptr].value >= min_uA &&
  91. data->states[ptr].value <= max_uA) {
  92. target = data->states[ptr].gpios;
  93. best_val = data->states[ptr].value;
  94. }
  95. if (best_val == 0)
  96. return -EINVAL;
  97. for (ptr = 0; ptr < data->nr_gpios; ptr++) {
  98. state = (target & (1 << ptr)) >> ptr;
  99. gpio_set_value(data->gpios[ptr].gpio, state);
  100. }
  101. data->state = target;
  102. return 0;
  103. }
  104. static struct regulator_ops gpio_regulator_voltage_ops = {
  105. .get_voltage = gpio_regulator_get_value,
  106. .set_voltage = gpio_regulator_set_voltage,
  107. .list_voltage = gpio_regulator_list_voltage,
  108. };
  109. static struct regulator_ops gpio_regulator_current_ops = {
  110. .get_current_limit = gpio_regulator_get_value,
  111. .set_current_limit = gpio_regulator_set_current_limit,
  112. };
  113. static int __devinit gpio_regulator_probe(struct platform_device *pdev)
  114. {
  115. struct gpio_regulator_config *config = pdev->dev.platform_data;
  116. struct gpio_regulator_data *drvdata;
  117. struct regulator_config cfg = { };
  118. int ptr, ret, state;
  119. drvdata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_regulator_data),
  120. GFP_KERNEL);
  121. if (drvdata == NULL) {
  122. dev_err(&pdev->dev, "Failed to allocate device data\n");
  123. return -ENOMEM;
  124. }
  125. drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
  126. if (drvdata->desc.name == NULL) {
  127. dev_err(&pdev->dev, "Failed to allocate supply name\n");
  128. ret = -ENOMEM;
  129. goto err;
  130. }
  131. drvdata->gpios = kmemdup(config->gpios,
  132. config->nr_gpios * sizeof(struct gpio),
  133. GFP_KERNEL);
  134. if (drvdata->gpios == NULL) {
  135. dev_err(&pdev->dev, "Failed to allocate gpio data\n");
  136. ret = -ENOMEM;
  137. goto err_name;
  138. }
  139. drvdata->states = kmemdup(config->states,
  140. config->nr_states *
  141. sizeof(struct gpio_regulator_state),
  142. GFP_KERNEL);
  143. if (drvdata->states == NULL) {
  144. dev_err(&pdev->dev, "Failed to allocate state data\n");
  145. ret = -ENOMEM;
  146. goto err_memgpio;
  147. }
  148. drvdata->nr_states = config->nr_states;
  149. drvdata->desc.owner = THIS_MODULE;
  150. drvdata->desc.enable_time = config->startup_delay;
  151. /* handle regulator type*/
  152. switch (config->type) {
  153. case REGULATOR_VOLTAGE:
  154. drvdata->desc.type = REGULATOR_VOLTAGE;
  155. drvdata->desc.ops = &gpio_regulator_voltage_ops;
  156. drvdata->desc.n_voltages = config->nr_states;
  157. break;
  158. case REGULATOR_CURRENT:
  159. drvdata->desc.type = REGULATOR_CURRENT;
  160. drvdata->desc.ops = &gpio_regulator_current_ops;
  161. break;
  162. default:
  163. dev_err(&pdev->dev, "No regulator type set\n");
  164. ret = -EINVAL;
  165. goto err_memgpio;
  166. break;
  167. }
  168. drvdata->nr_gpios = config->nr_gpios;
  169. ret = gpio_request_array(drvdata->gpios, drvdata->nr_gpios);
  170. if (ret) {
  171. dev_err(&pdev->dev,
  172. "Could not obtain regulator setting GPIOs: %d\n", ret);
  173. goto err_memstate;
  174. }
  175. /* build initial state from gpio init data. */
  176. state = 0;
  177. for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
  178. if (config->gpios[ptr].flags & GPIOF_OUT_INIT_HIGH)
  179. state |= (1 << ptr);
  180. }
  181. drvdata->state = state;
  182. cfg.dev = &pdev->dev;
  183. cfg.init_data = config->init_data;
  184. cfg.driver_data = drvdata;
  185. if (config->enable_gpio >= 0)
  186. cfg.ena_gpio = config->enable_gpio;
  187. cfg.ena_gpio_invert = !config->enable_high;
  188. if (config->enabled_at_boot) {
  189. if (config->enable_high)
  190. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
  191. else
  192. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
  193. } else {
  194. if (config->enable_high)
  195. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
  196. else
  197. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
  198. }
  199. drvdata->dev = regulator_register(&drvdata->desc, &cfg);
  200. if (IS_ERR(drvdata->dev)) {
  201. ret = PTR_ERR(drvdata->dev);
  202. dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
  203. goto err_stategpio;
  204. }
  205. platform_set_drvdata(pdev, drvdata);
  206. return 0;
  207. err_stategpio:
  208. gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
  209. err_memstate:
  210. kfree(drvdata->states);
  211. err_memgpio:
  212. kfree(drvdata->gpios);
  213. err_name:
  214. kfree(drvdata->desc.name);
  215. err:
  216. return ret;
  217. }
  218. static int __devexit gpio_regulator_remove(struct platform_device *pdev)
  219. {
  220. struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev);
  221. regulator_unregister(drvdata->dev);
  222. gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
  223. kfree(drvdata->states);
  224. kfree(drvdata->gpios);
  225. kfree(drvdata->desc.name);
  226. return 0;
  227. }
  228. static struct platform_driver gpio_regulator_driver = {
  229. .probe = gpio_regulator_probe,
  230. .remove = __devexit_p(gpio_regulator_remove),
  231. .driver = {
  232. .name = "gpio-regulator",
  233. .owner = THIS_MODULE,
  234. },
  235. };
  236. static int __init gpio_regulator_init(void)
  237. {
  238. return platform_driver_register(&gpio_regulator_driver);
  239. }
  240. subsys_initcall(gpio_regulator_init);
  241. static void __exit gpio_regulator_exit(void)
  242. {
  243. platform_driver_unregister(&gpio_regulator_driver);
  244. }
  245. module_exit(gpio_regulator_exit);
  246. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  247. MODULE_DESCRIPTION("gpio voltage regulator");
  248. MODULE_LICENSE("GPL");
  249. MODULE_ALIAS("platform:gpio-regulator");