gpio-regulator.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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/of_regulator.h>
  31. #include <linux/regulator/gpio-regulator.h>
  32. #include <linux/gpio.h>
  33. #include <linux/slab.h>
  34. #include <linux/of.h>
  35. #include <linux/of_gpio.h>
  36. struct gpio_regulator_data {
  37. struct regulator_desc desc;
  38. struct regulator_dev *dev;
  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_get_value(struct regulator_dev *dev)
  46. {
  47. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  48. int ptr;
  49. for (ptr = 0; ptr < data->nr_states; ptr++)
  50. if (data->states[ptr].gpios == data->state)
  51. return data->states[ptr].value;
  52. return -EINVAL;
  53. }
  54. static int gpio_regulator_set_voltage(struct regulator_dev *dev,
  55. int min_uV, int max_uV,
  56. unsigned *selector)
  57. {
  58. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  59. int ptr, target = 0, state, best_val = INT_MAX;
  60. for (ptr = 0; ptr < data->nr_states; ptr++)
  61. if (data->states[ptr].value < best_val &&
  62. data->states[ptr].value >= min_uV &&
  63. data->states[ptr].value <= max_uV) {
  64. target = data->states[ptr].gpios;
  65. best_val = data->states[ptr].value;
  66. if (selector)
  67. *selector = ptr;
  68. }
  69. if (best_val == INT_MAX)
  70. return -EINVAL;
  71. for (ptr = 0; ptr < data->nr_gpios; ptr++) {
  72. state = (target & (1 << ptr)) >> ptr;
  73. gpio_set_value(data->gpios[ptr].gpio, state);
  74. }
  75. data->state = target;
  76. return 0;
  77. }
  78. static int gpio_regulator_list_voltage(struct regulator_dev *dev,
  79. unsigned selector)
  80. {
  81. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  82. if (selector >= data->nr_states)
  83. return -EINVAL;
  84. return data->states[selector].value;
  85. }
  86. static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
  87. int min_uA, int max_uA)
  88. {
  89. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  90. int ptr, target = 0, state, best_val = 0;
  91. for (ptr = 0; ptr < data->nr_states; ptr++)
  92. if (data->states[ptr].value > best_val &&
  93. data->states[ptr].value >= min_uA &&
  94. data->states[ptr].value <= max_uA) {
  95. target = data->states[ptr].gpios;
  96. best_val = data->states[ptr].value;
  97. }
  98. if (best_val == 0)
  99. return -EINVAL;
  100. for (ptr = 0; ptr < data->nr_gpios; ptr++) {
  101. state = (target & (1 << ptr)) >> ptr;
  102. gpio_set_value(data->gpios[ptr].gpio, state);
  103. }
  104. data->state = target;
  105. return 0;
  106. }
  107. static struct regulator_ops gpio_regulator_voltage_ops = {
  108. .get_voltage = gpio_regulator_get_value,
  109. .set_voltage = gpio_regulator_set_voltage,
  110. .list_voltage = gpio_regulator_list_voltage,
  111. };
  112. struct gpio_regulator_config *
  113. of_get_gpio_regulator_config(struct device *dev, struct device_node *np)
  114. {
  115. struct gpio_regulator_config *config;
  116. struct property *prop;
  117. const char *regtype;
  118. int proplen, gpio, i;
  119. config = devm_kzalloc(dev,
  120. sizeof(struct gpio_regulator_config),
  121. GFP_KERNEL);
  122. if (!config)
  123. return ERR_PTR(-ENOMEM);
  124. config->init_data = of_get_regulator_init_data(dev, np);
  125. if (!config->init_data)
  126. return ERR_PTR(-EINVAL);
  127. config->supply_name = config->init_data->constraints.name;
  128. if (of_property_read_bool(np, "enable-active-high"))
  129. config->enable_high = true;
  130. if (of_property_read_bool(np, "enable-at-boot"))
  131. config->enabled_at_boot = true;
  132. of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
  133. config->enable_gpio = of_get_named_gpio(np, "enable-gpio", 0);
  134. /* Fetch GPIOs. */
  135. for (i = 0; ; i++)
  136. if (of_get_named_gpio(np, "gpios", i) < 0)
  137. break;
  138. config->nr_gpios = i;
  139. config->gpios = devm_kzalloc(dev,
  140. sizeof(struct gpio) * config->nr_gpios,
  141. GFP_KERNEL);
  142. if (!config->gpios)
  143. return ERR_PTR(-ENOMEM);
  144. for (i = 0; config->nr_gpios; i++) {
  145. gpio = of_get_named_gpio(np, "gpios", i);
  146. if (gpio < 0)
  147. break;
  148. config->gpios[i].gpio = gpio;
  149. }
  150. /* Fetch states. */
  151. prop = of_find_property(np, "states", NULL);
  152. proplen = prop->length / sizeof(int);
  153. config->states = devm_kzalloc(dev,
  154. sizeof(struct gpio_regulator_state)
  155. * (proplen / 2),
  156. GFP_KERNEL);
  157. if (!config->states)
  158. return ERR_PTR(-ENOMEM);
  159. for (i = 0; i < proplen / 2; i++) {
  160. config->states[i].value =
  161. be32_to_cpup((int *)prop->value + (i * 2));
  162. config->states[i].gpios =
  163. be32_to_cpup((int *)prop->value + (i * 2 + 1));
  164. }
  165. config->nr_states = i;
  166. of_property_read_string(np, "regulator-type", &regtype);
  167. if (!strncmp("voltage", regtype, 7))
  168. config->type = REGULATOR_VOLTAGE;
  169. else if (!strncmp("current", regtype, 7))
  170. config->type = REGULATOR_CURRENT;
  171. return config;
  172. }
  173. static struct regulator_ops gpio_regulator_current_ops = {
  174. .get_current_limit = gpio_regulator_get_value,
  175. .set_current_limit = gpio_regulator_set_current_limit,
  176. };
  177. static int __devinit gpio_regulator_probe(struct platform_device *pdev)
  178. {
  179. struct gpio_regulator_config *config = pdev->dev.platform_data;
  180. struct device_node *np = pdev->dev.of_node;
  181. struct gpio_regulator_data *drvdata;
  182. struct regulator_config cfg = { };
  183. int ptr, ret, state;
  184. if (np) {
  185. config = of_get_gpio_regulator_config(&pdev->dev, np);
  186. if (IS_ERR(config))
  187. return PTR_ERR(config);
  188. }
  189. drvdata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_regulator_data),
  190. GFP_KERNEL);
  191. if (drvdata == NULL) {
  192. dev_err(&pdev->dev, "Failed to allocate device data\n");
  193. return -ENOMEM;
  194. }
  195. drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
  196. if (drvdata->desc.name == NULL) {
  197. dev_err(&pdev->dev, "Failed to allocate supply name\n");
  198. ret = -ENOMEM;
  199. goto err;
  200. }
  201. drvdata->gpios = kmemdup(config->gpios,
  202. config->nr_gpios * sizeof(struct gpio),
  203. GFP_KERNEL);
  204. if (drvdata->gpios == NULL) {
  205. dev_err(&pdev->dev, "Failed to allocate gpio data\n");
  206. ret = -ENOMEM;
  207. goto err_name;
  208. }
  209. drvdata->states = kmemdup(config->states,
  210. config->nr_states *
  211. sizeof(struct gpio_regulator_state),
  212. GFP_KERNEL);
  213. if (drvdata->states == NULL) {
  214. dev_err(&pdev->dev, "Failed to allocate state data\n");
  215. ret = -ENOMEM;
  216. goto err_memgpio;
  217. }
  218. drvdata->nr_states = config->nr_states;
  219. drvdata->desc.owner = THIS_MODULE;
  220. drvdata->desc.enable_time = config->startup_delay;
  221. /* handle regulator type*/
  222. switch (config->type) {
  223. case REGULATOR_VOLTAGE:
  224. drvdata->desc.type = REGULATOR_VOLTAGE;
  225. drvdata->desc.ops = &gpio_regulator_voltage_ops;
  226. drvdata->desc.n_voltages = config->nr_states;
  227. break;
  228. case REGULATOR_CURRENT:
  229. drvdata->desc.type = REGULATOR_CURRENT;
  230. drvdata->desc.ops = &gpio_regulator_current_ops;
  231. break;
  232. default:
  233. dev_err(&pdev->dev, "No regulator type set\n");
  234. ret = -EINVAL;
  235. goto err_memgpio;
  236. break;
  237. }
  238. drvdata->nr_gpios = config->nr_gpios;
  239. ret = gpio_request_array(drvdata->gpios, drvdata->nr_gpios);
  240. if (ret) {
  241. dev_err(&pdev->dev,
  242. "Could not obtain regulator setting GPIOs: %d\n", ret);
  243. goto err_memstate;
  244. }
  245. /* build initial state from gpio init data. */
  246. state = 0;
  247. for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
  248. if (config->gpios[ptr].flags & GPIOF_OUT_INIT_HIGH)
  249. state |= (1 << ptr);
  250. }
  251. drvdata->state = state;
  252. cfg.dev = &pdev->dev;
  253. cfg.init_data = config->init_data;
  254. cfg.driver_data = drvdata;
  255. if (config->enable_gpio >= 0)
  256. cfg.ena_gpio = config->enable_gpio;
  257. cfg.ena_gpio_invert = !config->enable_high;
  258. if (config->enabled_at_boot) {
  259. if (config->enable_high)
  260. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
  261. else
  262. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
  263. } else {
  264. if (config->enable_high)
  265. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
  266. else
  267. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
  268. }
  269. drvdata->dev = regulator_register(&drvdata->desc, &cfg);
  270. if (IS_ERR(drvdata->dev)) {
  271. ret = PTR_ERR(drvdata->dev);
  272. dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
  273. goto err_stategpio;
  274. }
  275. platform_set_drvdata(pdev, drvdata);
  276. return 0;
  277. err_stategpio:
  278. gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
  279. err_memstate:
  280. kfree(drvdata->states);
  281. err_memgpio:
  282. kfree(drvdata->gpios);
  283. err_name:
  284. kfree(drvdata->desc.name);
  285. err:
  286. return ret;
  287. }
  288. static int __devexit gpio_regulator_remove(struct platform_device *pdev)
  289. {
  290. struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev);
  291. regulator_unregister(drvdata->dev);
  292. gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
  293. kfree(drvdata->states);
  294. kfree(drvdata->gpios);
  295. kfree(drvdata->desc.name);
  296. return 0;
  297. }
  298. static const struct of_device_id regulator_gpio_of_match[] __devinitconst = {
  299. { .compatible = "regulator-gpio", },
  300. {},
  301. };
  302. static struct platform_driver gpio_regulator_driver = {
  303. .probe = gpio_regulator_probe,
  304. .remove = __devexit_p(gpio_regulator_remove),
  305. .driver = {
  306. .name = "gpio-regulator",
  307. .owner = THIS_MODULE,
  308. .of_match_table = regulator_gpio_of_match,
  309. },
  310. };
  311. static int __init gpio_regulator_init(void)
  312. {
  313. return platform_driver_register(&gpio_regulator_driver);
  314. }
  315. subsys_initcall(gpio_regulator_init);
  316. static void __exit gpio_regulator_exit(void)
  317. {
  318. platform_driver_unregister(&gpio_regulator_driver);
  319. }
  320. module_exit(gpio_regulator_exit);
  321. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  322. MODULE_DESCRIPTION("gpio voltage regulator");
  323. MODULE_LICENSE("GPL");
  324. MODULE_ALIAS("platform:gpio-regulator");