fixed.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. };
  36. /**
  37. * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
  38. * @dev: device requesting for fixed_voltage_config
  39. * @desc: regulator description
  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. const struct regulator_desc *desc)
  48. {
  49. struct fixed_voltage_config *config;
  50. struct device_node *np = dev->of_node;
  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, desc);
  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. of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
  85. config->enable_high = of_property_read_bool(np, "enable-active-high");
  86. config->gpio_is_open_drain = of_property_read_bool(np,
  87. "gpio-open-drain");
  88. if (of_find_property(np, "vin-supply", NULL))
  89. config->input_supply = "vin";
  90. return config;
  91. }
  92. static struct regulator_ops fixed_voltage_ops = {
  93. };
  94. static int reg_fixed_voltage_probe(struct platform_device *pdev)
  95. {
  96. struct fixed_voltage_config *config;
  97. struct fixed_voltage_data *drvdata;
  98. struct regulator_config cfg = { };
  99. int ret;
  100. drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
  101. GFP_KERNEL);
  102. if (!drvdata)
  103. return -ENOMEM;
  104. if (pdev->dev.of_node) {
  105. config = of_get_fixed_voltage_config(&pdev->dev,
  106. &drvdata->desc);
  107. if (IS_ERR(config))
  108. return PTR_ERR(config);
  109. } else {
  110. config = dev_get_platdata(&pdev->dev);
  111. }
  112. if (!config)
  113. return -ENOMEM;
  114. drvdata->desc.name = devm_kstrdup(&pdev->dev,
  115. config->supply_name,
  116. GFP_KERNEL);
  117. if (drvdata->desc.name == NULL) {
  118. dev_err(&pdev->dev, "Failed to allocate supply name\n");
  119. return -ENOMEM;
  120. }
  121. drvdata->desc.type = REGULATOR_VOLTAGE;
  122. drvdata->desc.owner = THIS_MODULE;
  123. drvdata->desc.ops = &fixed_voltage_ops;
  124. drvdata->desc.enable_time = config->startup_delay;
  125. if (config->input_supply) {
  126. drvdata->desc.supply_name = devm_kstrdup(&pdev->dev,
  127. config->input_supply,
  128. GFP_KERNEL);
  129. if (!drvdata->desc.supply_name) {
  130. dev_err(&pdev->dev,
  131. "Failed to allocate input supply\n");
  132. return -ENOMEM;
  133. }
  134. }
  135. if (config->microvolts)
  136. drvdata->desc.n_voltages = 1;
  137. drvdata->desc.fixed_uV = config->microvolts;
  138. if (gpio_is_valid(config->gpio)) {
  139. cfg.ena_gpio = config->gpio;
  140. if (pdev->dev.of_node)
  141. cfg.ena_gpio_initialized = true;
  142. }
  143. cfg.ena_gpio_invert = !config->enable_high;
  144. if (config->enabled_at_boot) {
  145. if (config->enable_high)
  146. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
  147. else
  148. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
  149. } else {
  150. if (config->enable_high)
  151. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_LOW;
  152. else
  153. cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
  154. }
  155. if (config->gpio_is_open_drain)
  156. cfg.ena_gpio_flags |= GPIOF_OPEN_DRAIN;
  157. cfg.dev = &pdev->dev;
  158. cfg.init_data = config->init_data;
  159. cfg.driver_data = drvdata;
  160. cfg.of_node = pdev->dev.of_node;
  161. drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc,
  162. &cfg);
  163. if (IS_ERR(drvdata->dev)) {
  164. ret = PTR_ERR(drvdata->dev);
  165. dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
  166. return ret;
  167. }
  168. platform_set_drvdata(pdev, drvdata);
  169. dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
  170. drvdata->desc.fixed_uV);
  171. return 0;
  172. }
  173. #if defined(CONFIG_OF)
  174. static const struct of_device_id fixed_of_match[] = {
  175. { .compatible = "regulator-fixed", },
  176. {},
  177. };
  178. MODULE_DEVICE_TABLE(of, fixed_of_match);
  179. #endif
  180. static struct platform_driver regulator_fixed_voltage_driver = {
  181. .probe = reg_fixed_voltage_probe,
  182. .driver = {
  183. .name = "reg-fixed-voltage",
  184. .of_match_table = of_match_ptr(fixed_of_match),
  185. },
  186. };
  187. static int __init regulator_fixed_voltage_init(void)
  188. {
  189. return platform_driver_register(&regulator_fixed_voltage_driver);
  190. }
  191. subsys_initcall(regulator_fixed_voltage_init);
  192. static void __exit regulator_fixed_voltage_exit(void)
  193. {
  194. platform_driver_unregister(&regulator_fixed_voltage_driver);
  195. }
  196. module_exit(regulator_fixed_voltage_exit);
  197. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  198. MODULE_DESCRIPTION("Fixed voltage regulator");
  199. MODULE_LICENSE("GPL");
  200. MODULE_ALIAS("platform:reg-fixed-voltage");