gpio-regulator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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_cansleep(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_cansleep(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. static struct gpio_regulator_config *
  113. of_get_gpio_regulator_config(struct device *dev, struct device_node *np,
  114. const struct regulator_desc *desc)
  115. {
  116. struct gpio_regulator_config *config;
  117. const char *regtype;
  118. int proplen, gpio, i;
  119. int ret;
  120. config = devm_kzalloc(dev,
  121. sizeof(struct gpio_regulator_config),
  122. GFP_KERNEL);
  123. if (!config)
  124. return ERR_PTR(-ENOMEM);
  125. config->init_data = of_get_regulator_init_data(dev, np, desc);
  126. if (!config->init_data)
  127. return ERR_PTR(-EINVAL);
  128. config->supply_name = config->init_data->constraints.name;
  129. if (of_property_read_bool(np, "enable-active-high"))
  130. config->enable_high = true;
  131. if (of_property_read_bool(np, "enable-at-boot"))
  132. config->enabled_at_boot = true;
  133. of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
  134. config->enable_gpio = of_get_named_gpio(np, "enable-gpio", 0);
  135. if (config->enable_gpio == -EPROBE_DEFER)
  136. return ERR_PTR(-EPROBE_DEFER);
  137. /* Fetch GPIOs. - optional property*/
  138. ret = of_gpio_count(np);
  139. if ((ret < 0) && (ret != -ENOENT))
  140. return ERR_PTR(ret);
  141. if (ret > 0) {
  142. config->nr_gpios = ret;
  143. config->gpios = devm_kzalloc(dev,
  144. sizeof(struct gpio) * config->nr_gpios,
  145. GFP_KERNEL);
  146. if (!config->gpios)
  147. return ERR_PTR(-ENOMEM);
  148. proplen = of_property_count_u32_elems(np, "gpios-states");
  149. /* optional property */
  150. if (proplen < 0)
  151. proplen = 0;
  152. if (proplen > 0 && proplen != config->nr_gpios) {
  153. dev_warn(dev, "gpios <-> gpios-states mismatch\n");
  154. proplen = 0;
  155. }
  156. for (i = 0; i < config->nr_gpios; i++) {
  157. gpio = of_get_named_gpio(np, "gpios", i);
  158. if (gpio < 0)
  159. break;
  160. config->gpios[i].gpio = gpio;
  161. if (proplen > 0) {
  162. of_property_read_u32_index(np, "gpios-states",
  163. i, &ret);
  164. if (ret)
  165. config->gpios[i].flags =
  166. GPIOF_OUT_INIT_HIGH;
  167. }
  168. }
  169. }
  170. /* Fetch states. */
  171. proplen = of_property_count_u32_elems(np, "states");
  172. if (proplen < 0) {
  173. dev_err(dev, "No 'states' property found\n");
  174. return ERR_PTR(-EINVAL);
  175. }
  176. config->states = devm_kzalloc(dev,
  177. sizeof(struct gpio_regulator_state)
  178. * (proplen / 2),
  179. GFP_KERNEL);
  180. if (!config->states)
  181. return ERR_PTR(-ENOMEM);
  182. for (i = 0; i < proplen / 2; i++) {
  183. of_property_read_u32_index(np, "states", i * 2,
  184. &config->states[i].value);
  185. of_property_read_u32_index(np, "states", i * 2 + 1,
  186. &config->states[i].gpios);
  187. }
  188. config->nr_states = i;
  189. config->type = REGULATOR_VOLTAGE;
  190. ret = of_property_read_string(np, "regulator-type", &regtype);
  191. if (ret >= 0) {
  192. if (!strncmp("voltage", regtype, 7))
  193. config->type = REGULATOR_VOLTAGE;
  194. else if (!strncmp("current", regtype, 7))
  195. config->type = REGULATOR_CURRENT;
  196. else
  197. dev_warn(dev, "Unknown regulator-type '%s'\n",
  198. regtype);
  199. }
  200. return config;
  201. }
  202. static struct regulator_ops gpio_regulator_current_ops = {
  203. .get_current_limit = gpio_regulator_get_value,
  204. .set_current_limit = gpio_regulator_set_current_limit,
  205. };
  206. static int gpio_regulator_probe(struct platform_device *pdev)
  207. {
  208. struct gpio_regulator_config *config = dev_get_platdata(&pdev->dev);
  209. struct device_node *np = pdev->dev.of_node;
  210. struct gpio_regulator_data *drvdata;
  211. struct regulator_config cfg = { };
  212. int ptr, ret, state;
  213. drvdata = devm_kzalloc(&pdev->dev, sizeof(struct gpio_regulator_data),
  214. GFP_KERNEL);
  215. if (drvdata == NULL)
  216. return -ENOMEM;
  217. if (np) {
  218. config = of_get_gpio_regulator_config(&pdev->dev, np,
  219. &drvdata->desc);
  220. if (IS_ERR(config))
  221. return PTR_ERR(config);
  222. }
  223. drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
  224. if (drvdata->desc.name == NULL) {
  225. dev_err(&pdev->dev, "Failed to allocate supply name\n");
  226. return -ENOMEM;
  227. }
  228. if (config->nr_gpios != 0) {
  229. drvdata->gpios = kmemdup(config->gpios,
  230. config->nr_gpios * sizeof(struct gpio),
  231. GFP_KERNEL);
  232. if (drvdata->gpios == NULL) {
  233. dev_err(&pdev->dev, "Failed to allocate gpio data\n");
  234. ret = -ENOMEM;
  235. goto err_name;
  236. }
  237. drvdata->nr_gpios = config->nr_gpios;
  238. ret = gpio_request_array(drvdata->gpios, drvdata->nr_gpios);
  239. if (ret) {
  240. if (ret != -EPROBE_DEFER)
  241. dev_err(&pdev->dev,
  242. "Could not obtain regulator setting GPIOs: %d\n",
  243. ret);
  244. goto err_memgpio;
  245. }
  246. }
  247. drvdata->states = kmemdup(config->states,
  248. config->nr_states *
  249. sizeof(struct gpio_regulator_state),
  250. GFP_KERNEL);
  251. if (drvdata->states == NULL) {
  252. dev_err(&pdev->dev, "Failed to allocate state data\n");
  253. ret = -ENOMEM;
  254. goto err_stategpio;
  255. }
  256. drvdata->nr_states = config->nr_states;
  257. drvdata->desc.owner = THIS_MODULE;
  258. drvdata->desc.enable_time = config->startup_delay;
  259. /* handle regulator type*/
  260. switch (config->type) {
  261. case REGULATOR_VOLTAGE:
  262. drvdata->desc.type = REGULATOR_VOLTAGE;
  263. drvdata->desc.ops = &gpio_regulator_voltage_ops;
  264. drvdata->desc.n_voltages = config->nr_states;
  265. break;
  266. case REGULATOR_CURRENT:
  267. drvdata->desc.type = REGULATOR_CURRENT;
  268. drvdata->desc.ops = &gpio_regulator_current_ops;
  269. break;
  270. default:
  271. dev_err(&pdev->dev, "No regulator type set\n");
  272. ret = -EINVAL;
  273. goto err_memstate;
  274. }
  275. /* build initial state from gpio init data. */
  276. state = 0;
  277. for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
  278. if (config->gpios[ptr].flags & GPIOF_OUT_INIT_HIGH)
  279. state |= (1 << ptr);
  280. }
  281. drvdata->state = state;
  282. cfg.dev = &pdev->dev;
  283. cfg.init_data = config->init_data;
  284. cfg.driver_data = drvdata;
  285. cfg.of_node = np;
  286. if (gpio_is_valid(config->enable_gpio)) {
  287. cfg.ena_gpio = config->enable_gpio;
  288. cfg.ena_gpio_initialized = true;
  289. }
  290. cfg.ena_gpio_invert = !config->enable_high;
  291. if (config->enabled_at_boot) {
  292. if (config->enable_high)
  293. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
  294. else
  295. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
  296. } else {
  297. if (config->enable_high)
  298. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
  299. else
  300. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
  301. }
  302. drvdata->dev = regulator_register(&drvdata->desc, &cfg);
  303. if (IS_ERR(drvdata->dev)) {
  304. ret = PTR_ERR(drvdata->dev);
  305. dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
  306. goto err_memstate;
  307. }
  308. platform_set_drvdata(pdev, drvdata);
  309. return 0;
  310. err_memstate:
  311. kfree(drvdata->states);
  312. err_stategpio:
  313. gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
  314. err_memgpio:
  315. kfree(drvdata->gpios);
  316. err_name:
  317. kfree(drvdata->desc.name);
  318. return ret;
  319. }
  320. static int gpio_regulator_remove(struct platform_device *pdev)
  321. {
  322. struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev);
  323. regulator_unregister(drvdata->dev);
  324. gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
  325. kfree(drvdata->states);
  326. kfree(drvdata->gpios);
  327. kfree(drvdata->desc.name);
  328. return 0;
  329. }
  330. #if defined(CONFIG_OF)
  331. static const struct of_device_id regulator_gpio_of_match[] = {
  332. { .compatible = "regulator-gpio", },
  333. {},
  334. };
  335. MODULE_DEVICE_TABLE(of, regulator_gpio_of_match);
  336. #endif
  337. static struct platform_driver gpio_regulator_driver = {
  338. .probe = gpio_regulator_probe,
  339. .remove = gpio_regulator_remove,
  340. .driver = {
  341. .name = "gpio-regulator",
  342. .of_match_table = of_match_ptr(regulator_gpio_of_match),
  343. },
  344. };
  345. static int __init gpio_regulator_init(void)
  346. {
  347. return platform_driver_register(&gpio_regulator_driver);
  348. }
  349. subsys_initcall(gpio_regulator_init);
  350. static void __exit gpio_regulator_exit(void)
  351. {
  352. platform_driver_unregister(&gpio_regulator_driver);
  353. }
  354. module_exit(gpio_regulator_exit);
  355. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  356. MODULE_DESCRIPTION("gpio voltage regulator");
  357. MODULE_LICENSE("GPL");
  358. MODULE_ALIAS("platform:gpio-regulator");