gpio-regulator.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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_value(struct regulator_dev *dev,
  52. int min, int max, unsigned *selector)
  53. {
  54. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  55. int ptr, target = 0, state, best_val = INT_MAX;
  56. for (ptr = 0; ptr < data->nr_states; ptr++)
  57. if (data->states[ptr].value < best_val &&
  58. data->states[ptr].value >= min &&
  59. data->states[ptr].value <= max) {
  60. target = data->states[ptr].gpios;
  61. best_val = data->states[ptr].value;
  62. if (selector)
  63. *selector = ptr;
  64. }
  65. if (best_val == INT_MAX)
  66. return -EINVAL;
  67. for (ptr = 0; ptr < data->nr_gpios; ptr++) {
  68. state = (target & (1 << ptr)) >> ptr;
  69. gpio_set_value(data->gpios[ptr].gpio, state);
  70. }
  71. data->state = target;
  72. return 0;
  73. }
  74. static int gpio_regulator_set_voltage(struct regulator_dev *dev,
  75. int min_uV, int max_uV,
  76. unsigned *selector)
  77. {
  78. return gpio_regulator_set_value(dev, min_uV, max_uV, selector);
  79. }
  80. static int gpio_regulator_list_voltage(struct regulator_dev *dev,
  81. unsigned selector)
  82. {
  83. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  84. if (selector >= data->nr_states)
  85. return -EINVAL;
  86. return data->states[selector].value;
  87. }
  88. static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
  89. int min_uA, int max_uA)
  90. {
  91. return gpio_regulator_set_value(dev, min_uA, max_uA, NULL);
  92. }
  93. static struct regulator_ops gpio_regulator_voltage_ops = {
  94. .get_voltage = gpio_regulator_get_value,
  95. .set_voltage = gpio_regulator_set_voltage,
  96. .list_voltage = gpio_regulator_list_voltage,
  97. };
  98. static struct regulator_ops gpio_regulator_current_ops = {
  99. .get_current_limit = gpio_regulator_get_value,
  100. .set_current_limit = gpio_regulator_set_current_limit,
  101. };
  102. static int __devinit gpio_regulator_probe(struct platform_device *pdev)
  103. {
  104. struct gpio_regulator_config *config = pdev->dev.platform_data;
  105. struct gpio_regulator_data *drvdata;
  106. struct regulator_config cfg = { };
  107. int ptr, ret, state;
  108. drvdata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_regulator_data),
  109. GFP_KERNEL);
  110. if (drvdata == NULL) {
  111. dev_err(&pdev->dev, "Failed to allocate device data\n");
  112. return -ENOMEM;
  113. }
  114. drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
  115. if (drvdata->desc.name == NULL) {
  116. dev_err(&pdev->dev, "Failed to allocate supply name\n");
  117. ret = -ENOMEM;
  118. goto err;
  119. }
  120. drvdata->gpios = kmemdup(config->gpios,
  121. config->nr_gpios * sizeof(struct gpio),
  122. GFP_KERNEL);
  123. if (drvdata->gpios == NULL) {
  124. dev_err(&pdev->dev, "Failed to allocate gpio data\n");
  125. ret = -ENOMEM;
  126. goto err_name;
  127. }
  128. drvdata->states = kmemdup(config->states,
  129. config->nr_states *
  130. sizeof(struct gpio_regulator_state),
  131. GFP_KERNEL);
  132. if (drvdata->states == NULL) {
  133. dev_err(&pdev->dev, "Failed to allocate state data\n");
  134. ret = -ENOMEM;
  135. goto err_memgpio;
  136. }
  137. drvdata->nr_states = config->nr_states;
  138. drvdata->desc.owner = THIS_MODULE;
  139. drvdata->desc.enable_time = config->startup_delay;
  140. /* handle regulator type*/
  141. switch (config->type) {
  142. case REGULATOR_VOLTAGE:
  143. drvdata->desc.type = REGULATOR_VOLTAGE;
  144. drvdata->desc.ops = &gpio_regulator_voltage_ops;
  145. drvdata->desc.n_voltages = config->nr_states;
  146. break;
  147. case REGULATOR_CURRENT:
  148. drvdata->desc.type = REGULATOR_CURRENT;
  149. drvdata->desc.ops = &gpio_regulator_current_ops;
  150. break;
  151. default:
  152. dev_err(&pdev->dev, "No regulator type set\n");
  153. ret = -EINVAL;
  154. goto err_memgpio;
  155. break;
  156. }
  157. drvdata->nr_gpios = config->nr_gpios;
  158. ret = gpio_request_array(drvdata->gpios, drvdata->nr_gpios);
  159. if (ret) {
  160. dev_err(&pdev->dev,
  161. "Could not obtain regulator setting GPIOs: %d\n", ret);
  162. goto err_memstate;
  163. }
  164. /* build initial state from gpio init data. */
  165. state = 0;
  166. for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
  167. if (config->gpios[ptr].flags & GPIOF_OUT_INIT_HIGH)
  168. state |= (1 << ptr);
  169. }
  170. drvdata->state = state;
  171. cfg.dev = &pdev->dev;
  172. cfg.init_data = config->init_data;
  173. cfg.driver_data = drvdata;
  174. if (config->enable_gpio >= 0)
  175. cfg.ena_gpio = config->enable_gpio;
  176. cfg.ena_gpio_invert = !config->enable_high;
  177. if (config->enabled_at_boot) {
  178. if (config->enable_high)
  179. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
  180. else
  181. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
  182. } else {
  183. if (config->enable_high)
  184. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
  185. else
  186. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
  187. }
  188. drvdata->dev = regulator_register(&drvdata->desc, &cfg);
  189. if (IS_ERR(drvdata->dev)) {
  190. ret = PTR_ERR(drvdata->dev);
  191. dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
  192. goto err_stategpio;
  193. }
  194. platform_set_drvdata(pdev, drvdata);
  195. return 0;
  196. err_stategpio:
  197. gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
  198. err_memstate:
  199. kfree(drvdata->states);
  200. err_memgpio:
  201. kfree(drvdata->gpios);
  202. err_name:
  203. kfree(drvdata->desc.name);
  204. err:
  205. return ret;
  206. }
  207. static int __devexit gpio_regulator_remove(struct platform_device *pdev)
  208. {
  209. struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev);
  210. regulator_unregister(drvdata->dev);
  211. gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
  212. kfree(drvdata->states);
  213. kfree(drvdata->gpios);
  214. kfree(drvdata->desc.name);
  215. return 0;
  216. }
  217. static struct platform_driver gpio_regulator_driver = {
  218. .probe = gpio_regulator_probe,
  219. .remove = __devexit_p(gpio_regulator_remove),
  220. .driver = {
  221. .name = "gpio-regulator",
  222. .owner = THIS_MODULE,
  223. },
  224. };
  225. static int __init gpio_regulator_init(void)
  226. {
  227. return platform_driver_register(&gpio_regulator_driver);
  228. }
  229. subsys_initcall(gpio_regulator_init);
  230. static void __exit gpio_regulator_exit(void)
  231. {
  232. platform_driver_unregister(&gpio_regulator_driver);
  233. }
  234. module_exit(gpio_regulator_exit);
  235. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  236. MODULE_DESCRIPTION("gpio voltage regulator");
  237. MODULE_LICENSE("GPL");
  238. MODULE_ALIAS("platform:gpio-regulator");