gpio-regulator.c 8.9 KB

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