fixed.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * fixed.c
  3. *
  4. * Copyright 2008 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. *
  8. * Copyright (c) 2009 Nokia Corporation
  9. * Roger Quadros <ext-roger.quadros@nokia.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of the
  14. * License, or (at your option) any later version.
  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/fixed.h>
  26. #include <linux/gpio.h>
  27. #include <linux/slab.h>
  28. #include <linux/of.h>
  29. #include <linux/of_gpio.h>
  30. #include <linux/regulator/of_regulator.h>
  31. #include <linux/regulator/machine.h>
  32. #include <linux/acpi.h>
  33. #include <linux/property.h>
  34. #include <linux/gpio/consumer.h>
  35. struct fixed_voltage_data {
  36. struct regulator_desc desc;
  37. struct regulator_dev *dev;
  38. };
  39. /**
  40. * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
  41. * @dev: device requesting for fixed_voltage_config
  42. * @desc: regulator description
  43. *
  44. * Populates fixed_voltage_config structure by extracting data from device
  45. * tree node, returns a pointer to the populated structure of NULL if memory
  46. * alloc fails.
  47. */
  48. static struct fixed_voltage_config *
  49. of_get_fixed_voltage_config(struct device *dev,
  50. const struct regulator_desc *desc)
  51. {
  52. struct fixed_voltage_config *config;
  53. struct device_node *np = dev->of_node;
  54. struct regulator_init_data *init_data;
  55. config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
  56. GFP_KERNEL);
  57. if (!config)
  58. return ERR_PTR(-ENOMEM);
  59. config->init_data = of_get_regulator_init_data(dev, dev->of_node, desc);
  60. if (!config->init_data)
  61. return ERR_PTR(-EINVAL);
  62. init_data = config->init_data;
  63. init_data->constraints.apply_uV = 0;
  64. config->supply_name = init_data->constraints.name;
  65. if (init_data->constraints.min_uV == init_data->constraints.max_uV) {
  66. config->microvolts = init_data->constraints.min_uV;
  67. } else {
  68. dev_err(dev,
  69. "Fixed regulator specified with variable voltages\n");
  70. return ERR_PTR(-EINVAL);
  71. }
  72. if (init_data->constraints.boot_on)
  73. config->enabled_at_boot = true;
  74. config->gpio = of_get_named_gpio(np, "gpio", 0);
  75. if ((config->gpio < 0) && (config->gpio != -ENOENT))
  76. return ERR_PTR(config->gpio);
  77. of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
  78. config->enable_high = of_property_read_bool(np, "enable-active-high");
  79. config->gpio_is_open_drain = of_property_read_bool(np,
  80. "gpio-open-drain");
  81. if (of_find_property(np, "vin-supply", NULL))
  82. config->input_supply = "vin";
  83. return config;
  84. }
  85. /**
  86. * acpi_get_fixed_voltage_config - extract fixed_voltage_config structure info
  87. * @dev: device requesting for fixed_voltage_config
  88. * @desc: regulator description
  89. *
  90. * Populates fixed_voltage_config structure by extracting data through ACPI
  91. * interface, returns a pointer to the populated structure of NULL if memory
  92. * alloc fails.
  93. */
  94. static struct fixed_voltage_config *
  95. acpi_get_fixed_voltage_config(struct device *dev,
  96. const struct regulator_desc *desc)
  97. {
  98. struct fixed_voltage_config *config;
  99. const char *supply_name;
  100. struct gpio_desc *gpiod;
  101. int ret;
  102. config = devm_kzalloc(dev, sizeof(*config), GFP_KERNEL);
  103. if (!config)
  104. return ERR_PTR(-ENOMEM);
  105. ret = device_property_read_string(dev, "supply-name", &supply_name);
  106. if (!ret)
  107. config->supply_name = supply_name;
  108. gpiod = gpiod_get(dev, "gpio", GPIOD_ASIS);
  109. if (IS_ERR(gpiod))
  110. return ERR_PTR(-ENODEV);
  111. config->gpio = desc_to_gpio(gpiod);
  112. config->enable_high = device_property_read_bool(dev,
  113. "enable-active-high");
  114. gpiod_put(gpiod);
  115. return config;
  116. }
  117. static struct regulator_ops fixed_voltage_ops = {
  118. };
  119. static int reg_fixed_voltage_probe(struct platform_device *pdev)
  120. {
  121. struct fixed_voltage_config *config;
  122. struct fixed_voltage_data *drvdata;
  123. struct regulator_config cfg = { };
  124. int ret;
  125. drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
  126. GFP_KERNEL);
  127. if (!drvdata)
  128. return -ENOMEM;
  129. if (pdev->dev.of_node) {
  130. config = of_get_fixed_voltage_config(&pdev->dev,
  131. &drvdata->desc);
  132. if (IS_ERR(config))
  133. return PTR_ERR(config);
  134. } else if (ACPI_HANDLE(&pdev->dev)) {
  135. config = acpi_get_fixed_voltage_config(&pdev->dev,
  136. &drvdata->desc);
  137. if (IS_ERR(config))
  138. return PTR_ERR(config);
  139. } else {
  140. config = dev_get_platdata(&pdev->dev);
  141. }
  142. if (!config)
  143. return -ENOMEM;
  144. drvdata->desc.name = devm_kstrdup(&pdev->dev,
  145. config->supply_name,
  146. GFP_KERNEL);
  147. if (drvdata->desc.name == NULL) {
  148. dev_err(&pdev->dev, "Failed to allocate supply name\n");
  149. return -ENOMEM;
  150. }
  151. drvdata->desc.type = REGULATOR_VOLTAGE;
  152. drvdata->desc.owner = THIS_MODULE;
  153. drvdata->desc.ops = &fixed_voltage_ops;
  154. drvdata->desc.enable_time = config->startup_delay;
  155. if (config->input_supply) {
  156. drvdata->desc.supply_name = devm_kstrdup(&pdev->dev,
  157. config->input_supply,
  158. GFP_KERNEL);
  159. if (!drvdata->desc.supply_name) {
  160. dev_err(&pdev->dev,
  161. "Failed to allocate input supply\n");
  162. return -ENOMEM;
  163. }
  164. }
  165. if (config->microvolts)
  166. drvdata->desc.n_voltages = 1;
  167. drvdata->desc.fixed_uV = config->microvolts;
  168. if (gpio_is_valid(config->gpio)) {
  169. cfg.ena_gpio = config->gpio;
  170. if (pdev->dev.of_node)
  171. cfg.ena_gpio_initialized = true;
  172. }
  173. cfg.ena_gpio_invert = !config->enable_high;
  174. if (config->enabled_at_boot) {
  175. if (config->enable_high)
  176. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
  177. else
  178. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
  179. } else {
  180. if (config->enable_high)
  181. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
  182. else
  183. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
  184. }
  185. if (config->gpio_is_open_drain)
  186. cfg.ena_gpio_flags |= GPIOF_OPEN_DRAIN;
  187. cfg.dev = &pdev->dev;
  188. cfg.init_data = config->init_data;
  189. cfg.driver_data = drvdata;
  190. cfg.of_node = pdev->dev.of_node;
  191. drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc,
  192. &cfg);
  193. if (IS_ERR(drvdata->dev)) {
  194. ret = PTR_ERR(drvdata->dev);
  195. dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
  196. return ret;
  197. }
  198. platform_set_drvdata(pdev, drvdata);
  199. dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
  200. drvdata->desc.fixed_uV);
  201. return 0;
  202. }
  203. #if defined(CONFIG_OF)
  204. static const struct of_device_id fixed_of_match[] = {
  205. { .compatible = "regulator-fixed", },
  206. {},
  207. };
  208. MODULE_DEVICE_TABLE(of, fixed_of_match);
  209. #endif
  210. static struct platform_driver regulator_fixed_voltage_driver = {
  211. .probe = reg_fixed_voltage_probe,
  212. .driver = {
  213. .name = "reg-fixed-voltage",
  214. .of_match_table = of_match_ptr(fixed_of_match),
  215. },
  216. };
  217. static int __init regulator_fixed_voltage_init(void)
  218. {
  219. return platform_driver_register(&regulator_fixed_voltage_driver);
  220. }
  221. subsys_initcall(regulator_fixed_voltage_init);
  222. static void __exit regulator_fixed_voltage_exit(void)
  223. {
  224. platform_driver_unregister(&regulator_fixed_voltage_driver);
  225. }
  226. module_exit(regulator_fixed_voltage_exit);
  227. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  228. MODULE_DESCRIPTION("Fixed voltage regulator");
  229. MODULE_LICENSE("GPL");
  230. MODULE_ALIAS("platform:reg-fixed-voltage");