fixed.c 6.0 KB

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