fixed.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. struct fixed_voltage_data {
  33. struct regulator_desc desc;
  34. struct regulator_dev *dev;
  35. int microvolts;
  36. };
  37. /**
  38. * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
  39. * @dev: device requesting for fixed_voltage_config
  40. *
  41. * Populates fixed_voltage_config structure by extracting data from device
  42. * tree node, returns a pointer to the populated structure of NULL if memory
  43. * alloc fails.
  44. */
  45. static struct fixed_voltage_config *
  46. of_get_fixed_voltage_config(struct device *dev)
  47. {
  48. struct fixed_voltage_config *config;
  49. struct device_node *np = dev->of_node;
  50. const __be32 *delay;
  51. struct regulator_init_data *init_data;
  52. config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
  53. GFP_KERNEL);
  54. if (!config)
  55. return ERR_PTR(-ENOMEM);
  56. config->init_data = of_get_regulator_init_data(dev, dev->of_node);
  57. if (!config->init_data)
  58. return ERR_PTR(-EINVAL);
  59. init_data = config->init_data;
  60. init_data->constraints.apply_uV = 0;
  61. config->supply_name = init_data->constraints.name;
  62. if (init_data->constraints.min_uV == init_data->constraints.max_uV) {
  63. config->microvolts = init_data->constraints.min_uV;
  64. } else {
  65. dev_err(dev,
  66. "Fixed regulator specified with variable voltages\n");
  67. return ERR_PTR(-EINVAL);
  68. }
  69. if (init_data->constraints.boot_on)
  70. config->enabled_at_boot = true;
  71. config->gpio = of_get_named_gpio(np, "gpio", 0);
  72. /*
  73. * of_get_named_gpio() currently returns ENODEV rather than
  74. * EPROBE_DEFER. This code attempts to be compatible with both
  75. * for now; the ENODEV check can be removed once the API is fixed.
  76. * of_get_named_gpio() doesn't differentiate between a missing
  77. * property (which would be fine here, since the GPIO is optional)
  78. * and some other error. Patches have been posted for both issues.
  79. * Once they are check in, we should replace this with:
  80. * if (config->gpio < 0 && config->gpio != -ENOENT)
  81. */
  82. if ((config->gpio == -ENODEV) || (config->gpio == -EPROBE_DEFER))
  83. return ERR_PTR(-EPROBE_DEFER);
  84. delay = of_get_property(np, "startup-delay-us", NULL);
  85. if (delay)
  86. config->startup_delay = be32_to_cpu(*delay);
  87. if (of_find_property(np, "enable-active-high", NULL))
  88. config->enable_high = true;
  89. if (of_find_property(np, "gpio-open-drain", NULL))
  90. config->gpio_is_open_drain = true;
  91. if (of_find_property(np, "vin-supply", NULL))
  92. config->input_supply = "vin";
  93. return config;
  94. }
  95. static int fixed_voltage_get_voltage(struct regulator_dev *dev)
  96. {
  97. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  98. if (data->microvolts)
  99. return data->microvolts;
  100. else
  101. return -EINVAL;
  102. }
  103. static int fixed_voltage_list_voltage(struct regulator_dev *dev,
  104. unsigned selector)
  105. {
  106. struct fixed_voltage_data *data = rdev_get_drvdata(dev);
  107. if (selector != 0)
  108. return -EINVAL;
  109. return data->microvolts;
  110. }
  111. static struct regulator_ops fixed_voltage_ops = {
  112. .get_voltage = fixed_voltage_get_voltage,
  113. .list_voltage = fixed_voltage_list_voltage,
  114. };
  115. static int reg_fixed_voltage_probe(struct platform_device *pdev)
  116. {
  117. struct fixed_voltage_config *config;
  118. struct fixed_voltage_data *drvdata;
  119. struct regulator_config cfg = { };
  120. int ret;
  121. if (pdev->dev.of_node) {
  122. config = of_get_fixed_voltage_config(&pdev->dev);
  123. if (IS_ERR(config))
  124. return PTR_ERR(config);
  125. } else {
  126. config = pdev->dev.platform_data;
  127. }
  128. if (!config)
  129. return -ENOMEM;
  130. drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
  131. GFP_KERNEL);
  132. if (drvdata == NULL) {
  133. dev_err(&pdev->dev, "Failed to allocate device data\n");
  134. ret = -ENOMEM;
  135. goto err;
  136. }
  137. drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
  138. if (drvdata->desc.name == NULL) {
  139. dev_err(&pdev->dev, "Failed to allocate supply name\n");
  140. ret = -ENOMEM;
  141. goto err;
  142. }
  143. drvdata->desc.type = REGULATOR_VOLTAGE;
  144. drvdata->desc.owner = THIS_MODULE;
  145. drvdata->desc.ops = &fixed_voltage_ops;
  146. drvdata->desc.enable_time = config->startup_delay;
  147. if (config->input_supply) {
  148. drvdata->desc.supply_name = kstrdup(config->input_supply,
  149. GFP_KERNEL);
  150. if (!drvdata->desc.supply_name) {
  151. dev_err(&pdev->dev,
  152. "Failed to allocate input supply\n");
  153. ret = -ENOMEM;
  154. goto err_name;
  155. }
  156. }
  157. if (config->microvolts)
  158. drvdata->desc.n_voltages = 1;
  159. drvdata->microvolts = config->microvolts;
  160. if (config->gpio >= 0)
  161. cfg.ena_gpio = config->gpio;
  162. cfg.ena_gpio_invert = !config->enable_high;
  163. if (config->enabled_at_boot) {
  164. if (config->enable_high) {
  165. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
  166. } else {
  167. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
  168. }
  169. } else {
  170. if (config->enable_high) {
  171. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
  172. } else {
  173. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
  174. }
  175. }
  176. if (config->gpio_is_open_drain)
  177. cfg.ena_gpio_flags |= GPIOF_OPEN_DRAIN;
  178. cfg.dev = &pdev->dev;
  179. cfg.init_data = config->init_data;
  180. cfg.driver_data = drvdata;
  181. cfg.of_node = pdev->dev.of_node;
  182. drvdata->dev = regulator_register(&drvdata->desc, &cfg);
  183. if (IS_ERR(drvdata->dev)) {
  184. ret = PTR_ERR(drvdata->dev);
  185. dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
  186. goto err_input;
  187. }
  188. platform_set_drvdata(pdev, drvdata);
  189. dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
  190. drvdata->microvolts);
  191. return 0;
  192. err_input:
  193. kfree(drvdata->desc.supply_name);
  194. err_name:
  195. kfree(drvdata->desc.name);
  196. err:
  197. return ret;
  198. }
  199. static int reg_fixed_voltage_remove(struct platform_device *pdev)
  200. {
  201. struct fixed_voltage_data *drvdata = platform_get_drvdata(pdev);
  202. regulator_unregister(drvdata->dev);
  203. kfree(drvdata->desc.supply_name);
  204. kfree(drvdata->desc.name);
  205. return 0;
  206. }
  207. #if defined(CONFIG_OF)
  208. static const struct of_device_id fixed_of_match[] = {
  209. { .compatible = "regulator-fixed", },
  210. {},
  211. };
  212. MODULE_DEVICE_TABLE(of, fixed_of_match);
  213. #endif
  214. static struct platform_driver regulator_fixed_voltage_driver = {
  215. .probe = reg_fixed_voltage_probe,
  216. .remove = reg_fixed_voltage_remove,
  217. .driver = {
  218. .name = "reg-fixed-voltage",
  219. .owner = THIS_MODULE,
  220. .of_match_table = of_match_ptr(fixed_of_match),
  221. },
  222. };
  223. static int __init regulator_fixed_voltage_init(void)
  224. {
  225. return platform_driver_register(&regulator_fixed_voltage_driver);
  226. }
  227. subsys_initcall(regulator_fixed_voltage_init);
  228. static void __exit regulator_fixed_voltage_exit(void)
  229. {
  230. platform_driver_unregister(&regulator_fixed_voltage_driver);
  231. }
  232. module_exit(regulator_fixed_voltage_exit);
  233. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  234. MODULE_DESCRIPTION("Fixed voltage regulator");
  235. MODULE_LICENSE("GPL");
  236. MODULE_ALIAS("platform:reg-fixed-voltage");