gpio-regulator.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * gpio-regulator.c
  4. *
  5. * Copyright 2011 Heiko Stuebner <heiko@sntech.de>
  6. *
  7. * based on fixed.c
  8. *
  9. * Copyright 2008 Wolfson Microelectronics PLC.
  10. *
  11. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  12. *
  13. * Copyright (c) 2009 Nokia Corporation
  14. * Roger Quadros <ext-roger.quadros@nokia.com>
  15. *
  16. * This is useful for systems with mixed controllable and
  17. * non-controllable regulators, as well as for allowing testing on
  18. * systems with no controllable regulators.
  19. */
  20. #include <linux/err.h>
  21. #include <linux/mutex.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/regulator/driver.h>
  25. #include <linux/regulator/machine.h>
  26. #include <linux/regulator/of_regulator.h>
  27. #include <linux/regulator/gpio-regulator.h>
  28. #include <linux/gpio/consumer.h>
  29. #include <linux/slab.h>
  30. #include <linux/of.h>
  31. struct gpio_regulator_data {
  32. struct regulator_desc desc;
  33. struct gpio_desc **gpiods;
  34. int nr_gpios;
  35. struct gpio_regulator_state *states;
  36. int nr_states;
  37. int state;
  38. };
  39. static int gpio_regulator_get_value(struct regulator_dev *dev)
  40. {
  41. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  42. int ptr;
  43. for (ptr = 0; ptr < data->nr_states; ptr++)
  44. if (data->states[ptr].gpios == data->state)
  45. return data->states[ptr].value;
  46. return -EINVAL;
  47. }
  48. static int gpio_regulator_set_voltage(struct regulator_dev *dev,
  49. int min_uV, int max_uV,
  50. unsigned *selector)
  51. {
  52. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  53. int ptr, target = 0, state, best_val = INT_MAX;
  54. for (ptr = 0; ptr < data->nr_states; ptr++)
  55. if (data->states[ptr].value < best_val &&
  56. data->states[ptr].value >= min_uV &&
  57. data->states[ptr].value <= max_uV) {
  58. target = data->states[ptr].gpios;
  59. best_val = data->states[ptr].value;
  60. if (selector)
  61. *selector = ptr;
  62. }
  63. if (best_val == INT_MAX)
  64. return -EINVAL;
  65. for (ptr = 0; ptr < data->nr_gpios; ptr++) {
  66. state = (target & (1 << ptr)) >> ptr;
  67. gpiod_set_value_cansleep(data->gpiods[ptr], state);
  68. }
  69. data->state = target;
  70. return 0;
  71. }
  72. static int gpio_regulator_list_voltage(struct regulator_dev *dev,
  73. unsigned selector)
  74. {
  75. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  76. if (selector >= data->nr_states)
  77. return -EINVAL;
  78. return data->states[selector].value;
  79. }
  80. static int gpio_regulator_set_current_limit(struct regulator_dev *dev,
  81. int min_uA, int max_uA)
  82. {
  83. struct gpio_regulator_data *data = rdev_get_drvdata(dev);
  84. int ptr, target = 0, state, best_val = 0;
  85. for (ptr = 0; ptr < data->nr_states; ptr++)
  86. if (data->states[ptr].value > best_val &&
  87. data->states[ptr].value >= min_uA &&
  88. data->states[ptr].value <= max_uA) {
  89. target = data->states[ptr].gpios;
  90. best_val = data->states[ptr].value;
  91. }
  92. if (best_val == 0)
  93. return -EINVAL;
  94. for (ptr = 0; ptr < data->nr_gpios; ptr++) {
  95. state = (target & (1 << ptr)) >> ptr;
  96. gpiod_set_value_cansleep(data->gpiods[ptr], state);
  97. }
  98. data->state = target;
  99. return 0;
  100. }
  101. static const struct regulator_ops gpio_regulator_voltage_ops = {
  102. .get_voltage = gpio_regulator_get_value,
  103. .set_voltage = gpio_regulator_set_voltage,
  104. .list_voltage = gpio_regulator_list_voltage,
  105. };
  106. static struct gpio_regulator_config *
  107. of_get_gpio_regulator_config(struct device *dev, struct device_node *np,
  108. const struct regulator_desc *desc)
  109. {
  110. struct gpio_regulator_config *config;
  111. const char *regtype;
  112. int proplen, i;
  113. int ngpios;
  114. int ret;
  115. config = devm_kzalloc(dev,
  116. sizeof(struct gpio_regulator_config),
  117. GFP_KERNEL);
  118. if (!config)
  119. return ERR_PTR(-ENOMEM);
  120. config->init_data = of_get_regulator_init_data(dev, np, desc);
  121. if (!config->init_data)
  122. return ERR_PTR(-EINVAL);
  123. config->supply_name = config->init_data->constraints.name;
  124. if (of_property_read_bool(np, "enable-at-boot"))
  125. config->enabled_at_boot = true;
  126. of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
  127. /* Fetch GPIO init levels */
  128. ngpios = gpiod_count(dev, NULL);
  129. if (ngpios > 0) {
  130. config->gflags = devm_kzalloc(dev,
  131. sizeof(enum gpiod_flags)
  132. * ngpios,
  133. GFP_KERNEL);
  134. if (!config->gflags)
  135. return ERR_PTR(-ENOMEM);
  136. for (i = 0; i < ngpios; i++) {
  137. u32 val;
  138. ret = of_property_read_u32_index(np, "gpios-states", i,
  139. &val);
  140. /* Default to high per specification */
  141. if (ret)
  142. config->gflags[i] = GPIOD_OUT_HIGH;
  143. else
  144. config->gflags[i] =
  145. val ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
  146. }
  147. }
  148. config->ngpios = ngpios;
  149. /* Fetch states. */
  150. proplen = of_property_count_u32_elems(np, "states");
  151. if (proplen < 0) {
  152. dev_err(dev, "No 'states' property found\n");
  153. return ERR_PTR(-EINVAL);
  154. }
  155. config->states = devm_kcalloc(dev,
  156. proplen / 2,
  157. sizeof(struct gpio_regulator_state),
  158. GFP_KERNEL);
  159. if (!config->states)
  160. return ERR_PTR(-ENOMEM);
  161. for (i = 0; i < proplen / 2; i++) {
  162. of_property_read_u32_index(np, "states", i * 2,
  163. &config->states[i].value);
  164. of_property_read_u32_index(np, "states", i * 2 + 1,
  165. &config->states[i].gpios);
  166. }
  167. config->nr_states = i;
  168. config->type = REGULATOR_VOLTAGE;
  169. ret = of_property_read_string(np, "regulator-type", &regtype);
  170. if (ret >= 0) {
  171. if (!strncmp("voltage", regtype, 7))
  172. config->type = REGULATOR_VOLTAGE;
  173. else if (!strncmp("current", regtype, 7))
  174. config->type = REGULATOR_CURRENT;
  175. else
  176. dev_warn(dev, "Unknown regulator-type '%s'\n",
  177. regtype);
  178. }
  179. return config;
  180. }
  181. static const struct regulator_ops gpio_regulator_current_ops = {
  182. .get_current_limit = gpio_regulator_get_value,
  183. .set_current_limit = gpio_regulator_set_current_limit,
  184. };
  185. static int gpio_regulator_probe(struct platform_device *pdev)
  186. {
  187. struct device *dev = &pdev->dev;
  188. struct gpio_regulator_config *config = dev_get_platdata(dev);
  189. struct device_node *np = dev->of_node;
  190. struct gpio_regulator_data *drvdata;
  191. struct regulator_config cfg = { };
  192. struct regulator_dev *rdev;
  193. enum gpiod_flags gflags;
  194. int ptr, ret, state, i;
  195. drvdata = devm_kzalloc(dev, sizeof(struct gpio_regulator_data),
  196. GFP_KERNEL);
  197. if (drvdata == NULL)
  198. return -ENOMEM;
  199. if (np) {
  200. config = of_get_gpio_regulator_config(dev, np,
  201. &drvdata->desc);
  202. if (IS_ERR(config))
  203. return PTR_ERR(config);
  204. }
  205. drvdata->desc.name = devm_kstrdup(dev, config->supply_name, GFP_KERNEL);
  206. if (drvdata->desc.name == NULL) {
  207. dev_err(dev, "Failed to allocate supply name\n");
  208. return -ENOMEM;
  209. }
  210. drvdata->gpiods = devm_kzalloc(dev, sizeof(struct gpio_desc *),
  211. GFP_KERNEL);
  212. if (!drvdata->gpiods)
  213. return -ENOMEM;
  214. for (i = 0; i < config->ngpios; i++) {
  215. drvdata->gpiods[i] = devm_gpiod_get_index(dev,
  216. NULL,
  217. i,
  218. config->gflags[i]);
  219. if (IS_ERR(drvdata->gpiods[i]))
  220. return PTR_ERR(drvdata->gpiods[i]);
  221. /* This is good to know */
  222. gpiod_set_consumer_name(drvdata->gpiods[i], drvdata->desc.name);
  223. }
  224. drvdata->nr_gpios = config->ngpios;
  225. drvdata->states = devm_kmemdup(dev,
  226. config->states,
  227. config->nr_states *
  228. sizeof(struct gpio_regulator_state),
  229. GFP_KERNEL);
  230. if (drvdata->states == NULL) {
  231. dev_err(dev, "Failed to allocate state data\n");
  232. return -ENOMEM;
  233. }
  234. drvdata->nr_states = config->nr_states;
  235. drvdata->desc.owner = THIS_MODULE;
  236. drvdata->desc.enable_time = config->startup_delay;
  237. /* handle regulator type*/
  238. switch (config->type) {
  239. case REGULATOR_VOLTAGE:
  240. drvdata->desc.type = REGULATOR_VOLTAGE;
  241. drvdata->desc.ops = &gpio_regulator_voltage_ops;
  242. drvdata->desc.n_voltages = config->nr_states;
  243. break;
  244. case REGULATOR_CURRENT:
  245. drvdata->desc.type = REGULATOR_CURRENT;
  246. drvdata->desc.ops = &gpio_regulator_current_ops;
  247. break;
  248. default:
  249. dev_err(dev, "No regulator type set\n");
  250. return -EINVAL;
  251. }
  252. /* build initial state from gpio init data. */
  253. state = 0;
  254. for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
  255. if (config->gflags[ptr] == GPIOD_OUT_HIGH)
  256. state |= (1 << ptr);
  257. }
  258. drvdata->state = state;
  259. cfg.dev = dev;
  260. cfg.init_data = config->init_data;
  261. cfg.driver_data = drvdata;
  262. cfg.of_node = np;
  263. /*
  264. * The signal will be inverted by the GPIO core if flagged so in the
  265. * decriptor.
  266. */
  267. if (config->enabled_at_boot)
  268. gflags = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
  269. else
  270. gflags = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
  271. cfg.ena_gpiod = gpiod_get_optional(dev, "enable", gflags);
  272. if (IS_ERR(cfg.ena_gpiod))
  273. return PTR_ERR(cfg.ena_gpiod);
  274. rdev = devm_regulator_register(dev, &drvdata->desc, &cfg);
  275. if (IS_ERR(rdev)) {
  276. ret = PTR_ERR(rdev);
  277. dev_err(dev, "Failed to register regulator: %d\n", ret);
  278. return ret;
  279. }
  280. platform_set_drvdata(pdev, drvdata);
  281. return 0;
  282. }
  283. #if defined(CONFIG_OF)
  284. static const struct of_device_id regulator_gpio_of_match[] = {
  285. { .compatible = "regulator-gpio", },
  286. {},
  287. };
  288. MODULE_DEVICE_TABLE(of, regulator_gpio_of_match);
  289. #endif
  290. static struct platform_driver gpio_regulator_driver = {
  291. .probe = gpio_regulator_probe,
  292. .driver = {
  293. .name = "gpio-regulator",
  294. .of_match_table = of_match_ptr(regulator_gpio_of_match),
  295. },
  296. };
  297. static int __init gpio_regulator_init(void)
  298. {
  299. return platform_driver_register(&gpio_regulator_driver);
  300. }
  301. subsys_initcall(gpio_regulator_init);
  302. static void __exit gpio_regulator_exit(void)
  303. {
  304. platform_driver_unregister(&gpio_regulator_driver);
  305. }
  306. module_exit(gpio_regulator_exit);
  307. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  308. MODULE_DESCRIPTION("gpio voltage regulator");
  309. MODULE_LICENSE("GPL");
  310. MODULE_ALIAS("platform:gpio-regulator");