gpio-regulator.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 (config->init_data->constraints.boot_on)
  125. config->enabled_at_boot = true;
  126. /*
  127. * Do not use: undocumented device tree property.
  128. * This is kept around solely for device tree ABI stability.
  129. */
  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. /* Fetch GPIO init levels */
  134. ngpios = gpiod_count(dev, NULL);
  135. if (ngpios > 0) {
  136. config->gflags = devm_kzalloc(dev,
  137. sizeof(enum gpiod_flags)
  138. * ngpios,
  139. GFP_KERNEL);
  140. if (!config->gflags)
  141. return ERR_PTR(-ENOMEM);
  142. for (i = 0; i < ngpios; i++) {
  143. u32 val;
  144. ret = of_property_read_u32_index(np, "gpios-states", i,
  145. &val);
  146. /* Default to high per specification */
  147. if (ret)
  148. config->gflags[i] = GPIOD_OUT_HIGH;
  149. else
  150. config->gflags[i] =
  151. val ? GPIOD_OUT_HIGH : GPIOD_OUT_LOW;
  152. }
  153. }
  154. config->ngpios = ngpios;
  155. /* Fetch states. */
  156. proplen = of_property_count_u32_elems(np, "states");
  157. if (proplen < 0) {
  158. dev_err(dev, "No 'states' property found\n");
  159. return ERR_PTR(-EINVAL);
  160. }
  161. config->states = devm_kcalloc(dev,
  162. proplen / 2,
  163. sizeof(struct gpio_regulator_state),
  164. GFP_KERNEL);
  165. if (!config->states)
  166. return ERR_PTR(-ENOMEM);
  167. for (i = 0; i < proplen / 2; i++) {
  168. of_property_read_u32_index(np, "states", i * 2,
  169. &config->states[i].value);
  170. of_property_read_u32_index(np, "states", i * 2 + 1,
  171. &config->states[i].gpios);
  172. }
  173. config->nr_states = i;
  174. config->type = REGULATOR_VOLTAGE;
  175. ret = of_property_read_string(np, "regulator-type", &regtype);
  176. if (ret >= 0) {
  177. if (!strncmp("voltage", regtype, 7))
  178. config->type = REGULATOR_VOLTAGE;
  179. else if (!strncmp("current", regtype, 7))
  180. config->type = REGULATOR_CURRENT;
  181. else
  182. dev_warn(dev, "Unknown regulator-type '%s'\n",
  183. regtype);
  184. }
  185. return config;
  186. }
  187. static const struct regulator_ops gpio_regulator_current_ops = {
  188. .get_current_limit = gpio_regulator_get_value,
  189. .set_current_limit = gpio_regulator_set_current_limit,
  190. };
  191. static int gpio_regulator_probe(struct platform_device *pdev)
  192. {
  193. struct device *dev = &pdev->dev;
  194. struct gpio_regulator_config *config = dev_get_platdata(dev);
  195. struct device_node *np = dev->of_node;
  196. struct gpio_regulator_data *drvdata;
  197. struct regulator_config cfg = { };
  198. struct regulator_dev *rdev;
  199. enum gpiod_flags gflags;
  200. int ptr, ret, state, i;
  201. drvdata = devm_kzalloc(dev, sizeof(struct gpio_regulator_data),
  202. GFP_KERNEL);
  203. if (drvdata == NULL)
  204. return -ENOMEM;
  205. if (np) {
  206. config = of_get_gpio_regulator_config(dev, np,
  207. &drvdata->desc);
  208. if (IS_ERR(config))
  209. return PTR_ERR(config);
  210. }
  211. drvdata->desc.name = devm_kstrdup(dev, config->supply_name, GFP_KERNEL);
  212. if (drvdata->desc.name == NULL) {
  213. dev_err(dev, "Failed to allocate supply name\n");
  214. return -ENOMEM;
  215. }
  216. drvdata->gpiods = devm_kzalloc(dev, sizeof(struct gpio_desc *),
  217. GFP_KERNEL);
  218. if (!drvdata->gpiods)
  219. return -ENOMEM;
  220. for (i = 0; i < config->ngpios; i++) {
  221. drvdata->gpiods[i] = devm_gpiod_get_index(dev,
  222. NULL,
  223. i,
  224. config->gflags[i]);
  225. if (IS_ERR(drvdata->gpiods[i]))
  226. return PTR_ERR(drvdata->gpiods[i]);
  227. /* This is good to know */
  228. gpiod_set_consumer_name(drvdata->gpiods[i], drvdata->desc.name);
  229. }
  230. drvdata->nr_gpios = config->ngpios;
  231. drvdata->states = devm_kmemdup(dev,
  232. config->states,
  233. config->nr_states *
  234. sizeof(struct gpio_regulator_state),
  235. GFP_KERNEL);
  236. if (drvdata->states == NULL) {
  237. dev_err(dev, "Failed to allocate state data\n");
  238. return -ENOMEM;
  239. }
  240. drvdata->nr_states = config->nr_states;
  241. drvdata->desc.owner = THIS_MODULE;
  242. drvdata->desc.enable_time = config->startup_delay;
  243. /* handle regulator type*/
  244. switch (config->type) {
  245. case REGULATOR_VOLTAGE:
  246. drvdata->desc.type = REGULATOR_VOLTAGE;
  247. drvdata->desc.ops = &gpio_regulator_voltage_ops;
  248. drvdata->desc.n_voltages = config->nr_states;
  249. break;
  250. case REGULATOR_CURRENT:
  251. drvdata->desc.type = REGULATOR_CURRENT;
  252. drvdata->desc.ops = &gpio_regulator_current_ops;
  253. break;
  254. default:
  255. dev_err(dev, "No regulator type set\n");
  256. return -EINVAL;
  257. }
  258. /* build initial state from gpio init data. */
  259. state = 0;
  260. for (ptr = 0; ptr < drvdata->nr_gpios; ptr++) {
  261. if (config->gflags[ptr] == GPIOD_OUT_HIGH)
  262. state |= (1 << ptr);
  263. }
  264. drvdata->state = state;
  265. cfg.dev = dev;
  266. cfg.init_data = config->init_data;
  267. cfg.driver_data = drvdata;
  268. cfg.of_node = np;
  269. /*
  270. * The signal will be inverted by the GPIO core if flagged so in the
  271. * descriptor.
  272. */
  273. if (config->enabled_at_boot)
  274. gflags = GPIOD_OUT_HIGH | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
  275. else
  276. gflags = GPIOD_OUT_LOW | GPIOD_FLAGS_BIT_NONEXCLUSIVE;
  277. cfg.ena_gpiod = gpiod_get_optional(dev, "enable", gflags);
  278. if (IS_ERR(cfg.ena_gpiod))
  279. return PTR_ERR(cfg.ena_gpiod);
  280. rdev = devm_regulator_register(dev, &drvdata->desc, &cfg);
  281. if (IS_ERR(rdev)) {
  282. ret = PTR_ERR(rdev);
  283. dev_err(dev, "Failed to register regulator: %d\n", ret);
  284. return ret;
  285. }
  286. platform_set_drvdata(pdev, drvdata);
  287. return 0;
  288. }
  289. #if defined(CONFIG_OF)
  290. static const struct of_device_id regulator_gpio_of_match[] = {
  291. { .compatible = "regulator-gpio", },
  292. {},
  293. };
  294. MODULE_DEVICE_TABLE(of, regulator_gpio_of_match);
  295. #endif
  296. static struct platform_driver gpio_regulator_driver = {
  297. .probe = gpio_regulator_probe,
  298. .driver = {
  299. .name = "gpio-regulator",
  300. .of_match_table = of_match_ptr(regulator_gpio_of_match),
  301. },
  302. };
  303. static int __init gpio_regulator_init(void)
  304. {
  305. return platform_driver_register(&gpio_regulator_driver);
  306. }
  307. subsys_initcall(gpio_regulator_init);
  308. static void __exit gpio_regulator_exit(void)
  309. {
  310. platform_driver_unregister(&gpio_regulator_driver);
  311. }
  312. module_exit(gpio_regulator_exit);
  313. MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
  314. MODULE_DESCRIPTION("gpio voltage regulator");
  315. MODULE_LICENSE("GPL");
  316. MODULE_ALIAS("platform:gpio-regulator");