gpio-regulator.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. int enable_gpio;
  37. bool enable_high;
  38. bool is_enabled;
  39. struct gpio *gpios;
  40. int nr_gpios;
  41. struct gpio_regulator_state *states;
  42. int nr_states;
  43. int state;
  44. };
  45. static int gpio_regulator_is_enabled(struct regulator_dev *dev)
  46. {
  47. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  48. return data->is_enabled;
  49. }
  50. static int gpio_regulator_enable(struct regulator_dev *dev)
  51. {
  52. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  53. if (gpio_is_valid(data->enable_gpio)) {
  54. gpio_set_value_cansleep(data->enable_gpio, data->enable_high);
  55. data->is_enabled = true;
  56. }
  57. return 0;
  58. }
  59. static int gpio_regulator_disable(struct regulator_dev *dev)
  60. {
  61. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  62. if (gpio_is_valid(data->enable_gpio)) {
  63. gpio_set_value_cansleep(data->enable_gpio, !data->enable_high);
  64. data->is_enabled = false;
  65. }
  66. return 0;
  67. }
  68. static int gpio_regulator_get_value(struct regulator_dev *dev)
  69. {
  70. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  71. int ptr;
  72. for (ptr = 0; ptr < data->nr_states; ptr++)
  73. if (data->states[ptr].gpios == data->state)
  74. return data->states[ptr].value;
  75. return -EINVAL;
  76. }
  77. static int gpio_regulator_set_value(struct regulator_dev *dev,
  78. int min, int max, unsigned *selector)
  79. {
  80. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  81. int ptr, target = 0, state, best_val = INT_MAX;
  82. for (ptr = 0; ptr < data->nr_states; ptr++)
  83. if (data->states[ptr].value < best_val &&
  84. data->states[ptr].value >= min &&
  85. data->states[ptr].value <= max) {
  86. target = data->states[ptr].gpios;
  87. best_val = data->states[ptr].value;
  88. if (selector)
  89. *selector = ptr;
  90. }
  91. if (best_val == INT_MAX)
  92. return -EINVAL;
  93. for (ptr = 0; ptr < data->nr_gpios; ptr++) {
  94. state = (target & (1 << ptr)) >> ptr;
  95. gpio_set_value(data->gpios[ptr].gpio, state);
  96. }
  97. data->state = target;
  98. return 0;
  99. }
  100. static int gpio_regulator_set_voltage(struct regulator_dev *dev,
  101. int min_uV, int max_uV,
  102. unsigned *selector)
  103. {
  104. return gpio_regulator_set_value(dev, min_uV, max_uV, selector);
  105. }
  106. static int gpio_regulator_list_voltage(struct regulator_dev *dev,
  107. unsigned selector)
  108. {
  109. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  110. if (selector >= data->nr_states)
  111. return -EINVAL;
  112. return data->states[selector].value;
  113. }
  114. static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
  115. int min_uA, int max_uA)
  116. {
  117. return gpio_regulator_set_value(dev, min_uA, max_uA, NULL);
  118. }
  119. static struct regulator_ops gpio_regulator_voltage_ops = {
  120. .is_enabled = gpio_regulator_is_enabled,
  121. .enable = gpio_regulator_enable,
  122. .disable = gpio_regulator_disable,
  123. .get_voltage = gpio_regulator_get_value,
  124. .set_voltage = gpio_regulator_set_voltage,
  125. .list_voltage = gpio_regulator_list_voltage,
  126. };
  127. static struct regulator_ops gpio_regulator_current_ops = {
  128. .is_enabled = gpio_regulator_is_enabled,
  129. .enable = gpio_regulator_enable,
  130. .disable = gpio_regulator_disable,
  131. .get_current_limit = gpio_regulator_get_value,
  132. .set_current_limit = gpio_regulator_set_current_limit,
  133. };
  134. static int __devinit gpio_regulator_probe(struct platform_device *pdev)
  135. {
  136. struct gpio_regulator_config *config = pdev->dev.platform_data;
  137. struct gpio_regulator_data *drvdata;
  138. struct regulator_config cfg = { };
  139. int ptr, ret, state;
  140. drvdata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_regulator_data),
  141. GFP_KERNEL);
  142. if (drvdata == NULL) {
  143. dev_err(&pdev->dev, "Failed to allocate device data\n");
  144. return -ENOMEM;
  145. }
  146. drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
  147. if (drvdata->desc.name == NULL) {
  148. dev_err(&pdev->dev, "Failed to allocate supply name\n");
  149. ret = -ENOMEM;
  150. goto err;
  151. }
  152. drvdata->gpios = kmemdup(config->gpios,
  153. config->nr_gpios * sizeof(struct gpio),
  154. GFP_KERNEL);
  155. if (drvdata->gpios == NULL) {
  156. dev_err(&pdev->dev, "Failed to allocate gpio data\n");
  157. ret = -ENOMEM;
  158. goto err_name;
  159. }
  160. drvdata->states = kmemdup(config->states,
  161. config->nr_states *
  162. sizeof(struct gpio_regulator_state),
  163. GFP_KERNEL);
  164. if (drvdata->states == NULL) {
  165. dev_err(&pdev->dev, "Failed to allocate state data\n");
  166. ret = -ENOMEM;
  167. goto err_memgpio;
  168. }
  169. drvdata->nr_states = config->nr_states;
  170. drvdata->desc.owner = THIS_MODULE;
  171. drvdata->desc.enable_time = config->startup_delay;
  172. /* handle regulator type*/
  173. switch (config->type) {
  174. case REGULATOR_VOLTAGE:
  175. drvdata->desc.type = REGULATOR_VOLTAGE;
  176. drvdata->desc.ops = &gpio_regulator_voltage_ops;
  177. drvdata->desc.n_voltages = config->nr_states;
  178. break;
  179. case REGULATOR_CURRENT:
  180. drvdata->desc.type = REGULATOR_CURRENT;
  181. drvdata->desc.ops = &gpio_regulator_current_ops;
  182. break;
  183. default:
  184. dev_err(&pdev->dev, "No regulator type set\n");
  185. ret = -EINVAL;
  186. goto err_memgpio;
  187. break;
  188. }
  189. drvdata->enable_gpio = config->enable_gpio;
  190. if (gpio_is_valid(config->enable_gpio)) {
  191. drvdata->enable_high = config->enable_high;
  192. ret = gpio_request(config->enable_gpio, config->supply_name);
  193. if (ret) {
  194. dev_err(&pdev->dev,
  195. "Could not obtain regulator enable GPIO %d: %d\n",
  196. config->enable_gpio, ret);
  197. goto err_memstate;
  198. }
  199. /* set output direction without changing state
  200. * to prevent glitch
  201. */
  202. if (config->enabled_at_boot) {
  203. drvdata->is_enabled = true;
  204. ret = gpio_direction_output(config->enable_gpio,
  205. config->enable_high);
  206. } else {
  207. drvdata->is_enabled = false;
  208. ret = gpio_direction_output(config->enable_gpio,
  209. !config->enable_high);
  210. }
  211. if (ret) {
  212. dev_err(&pdev->dev,
  213. "Could not configure regulator enable GPIO %d direction: %d\n",
  214. config->enable_gpio, ret);
  215. goto err_enablegpio;
  216. }
  217. } else {
  218. /* Regulator without GPIO control is considered
  219. * always enabled
  220. */
  221. drvdata->is_enabled = true;
  222. }
  223. drvdata->nr_gpios = config->nr_gpios;
  224. ret = gpio_request_array(drvdata->gpios, drvdata->nr_gpios);
  225. if (ret) {
  226. dev_err(&pdev->dev,
  227. "Could not obtain regulator setting GPIOs: %d\n", ret);
  228. goto err_enablegpio;
  229. }
  230. /* build initial state from gpio init data. */
  231. state = 0;
  232. for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
  233. if (config->gpios[ptr].flags & GPIOF_OUT_INIT_HIGH)
  234. state |= (1 << ptr);
  235. }
  236. drvdata->state = state;
  237. cfg.dev = &pdev->dev;
  238. cfg.init_data = config->init_data;
  239. cfg.driver_data = drvdata;
  240. drvdata->dev = regulator_register(&drvdata->desc, &cfg);
  241. if (IS_ERR(drvdata->dev)) {
  242. ret = PTR_ERR(drvdata->dev);
  243. dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
  244. goto err_stategpio;
  245. }
  246. platform_set_drvdata(pdev, drvdata);
  247. return 0;
  248. err_stategpio:
  249. gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
  250. err_enablegpio:
  251. if (gpio_is_valid(config->enable_gpio))
  252. gpio_free(config->enable_gpio);
  253. err_memstate:
  254. kfree(drvdata->states);
  255. err_memgpio:
  256. kfree(drvdata->gpios);
  257. err_name:
  258. kfree(drvdata->desc.name);
  259. err:
  260. return ret;
  261. }
  262. static int __devexit gpio_regulator_remove(struct platform_device *pdev)
  263. {
  264. struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev);
  265. regulator_unregister(drvdata->dev);
  266. gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
  267. kfree(drvdata->states);
  268. kfree(drvdata->gpios);
  269. if (gpio_is_valid(drvdata->enable_gpio))
  270. gpio_free(drvdata->enable_gpio);
  271. kfree(drvdata->desc.name);
  272. return 0;
  273. }
  274. static struct platform_driver gpio_regulator_driver = {
  275. .probe = gpio_regulator_probe,
  276. .remove = __devexit_p(gpio_regulator_remove),
  277. .driver = {
  278. .name = "gpio-regulator",
  279. .owner = THIS_MODULE,
  280. },
  281. };
  282. static int __init gpio_regulator_init(void)
  283. {
  284. return platform_driver_register(&gpio_regulator_driver);
  285. }
  286. subsys_initcall(gpio_regulator_init);
  287. static void __exit gpio_regulator_exit(void)
  288. {
  289. platform_driver_unregister(&gpio_regulator_driver);
  290. }
  291. module_exit(gpio_regulator_exit);
  292. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  293. MODULE_DESCRIPTION("gpio voltage regulator");
  294. MODULE_LICENSE("GPL");
  295. MODULE_ALIAS("platform:gpio-regulator");