fixed.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * fixed.c
  4. *
  5. * Copyright 2008 Wolfson Microelectronics PLC.
  6. *
  7. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  8. *
  9. * Copyright (c) 2009 Nokia Corporation
  10. * Roger Quadros <ext-roger.quadros@nokia.com>
  11. *
  12. * This is useful for systems with mixed controllable and
  13. * non-controllable regulators, as well as for allowing testing on
  14. * systems with no controllable regulators.
  15. */
  16. #include <linux/err.h>
  17. #include <linux/mutex.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regulator/driver.h>
  21. #include <linux/regulator/fixed.h>
  22. #include <linux/gpio/consumer.h>
  23. #include <linux/slab.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/regulator/of_regulator.h>
  27. #include <linux/regulator/machine.h>
  28. #include <linux/clk.h>
  29. struct fixed_voltage_data {
  30. struct regulator_desc desc;
  31. struct regulator_dev *dev;
  32. struct clk *enable_clock;
  33. unsigned int clk_enable_counter;
  34. };
  35. struct fixed_dev_type {
  36. bool has_enable_clock;
  37. };
  38. static int reg_clock_enable(struct regulator_dev *rdev)
  39. {
  40. struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
  41. int ret = 0;
  42. ret = clk_prepare_enable(priv->enable_clock);
  43. if (ret)
  44. return ret;
  45. priv->clk_enable_counter++;
  46. return ret;
  47. }
  48. static int reg_clock_disable(struct regulator_dev *rdev)
  49. {
  50. struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
  51. clk_disable_unprepare(priv->enable_clock);
  52. priv->clk_enable_counter--;
  53. return 0;
  54. }
  55. static int reg_clock_is_enabled(struct regulator_dev *rdev)
  56. {
  57. struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
  58. return priv->clk_enable_counter > 0;
  59. }
  60. /**
  61. * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
  62. * @dev: device requesting for fixed_voltage_config
  63. * @desc: regulator description
  64. *
  65. * Populates fixed_voltage_config structure by extracting data from device
  66. * tree node, returns a pointer to the populated structure of NULL if memory
  67. * alloc fails.
  68. */
  69. static struct fixed_voltage_config *
  70. of_get_fixed_voltage_config(struct device *dev,
  71. const struct regulator_desc *desc)
  72. {
  73. struct fixed_voltage_config *config;
  74. struct device_node *np = dev->of_node;
  75. struct regulator_init_data *init_data;
  76. config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
  77. GFP_KERNEL);
  78. if (!config)
  79. return ERR_PTR(-ENOMEM);
  80. config->init_data = of_get_regulator_init_data(dev, dev->of_node, desc);
  81. if (!config->init_data)
  82. return ERR_PTR(-EINVAL);
  83. init_data = config->init_data;
  84. init_data->constraints.apply_uV = 0;
  85. config->supply_name = init_data->constraints.name;
  86. if (init_data->constraints.min_uV == init_data->constraints.max_uV) {
  87. config->microvolts = init_data->constraints.min_uV;
  88. } else {
  89. dev_err(dev,
  90. "Fixed regulator specified with variable voltages\n");
  91. return ERR_PTR(-EINVAL);
  92. }
  93. if (init_data->constraints.boot_on)
  94. config->enabled_at_boot = true;
  95. of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
  96. of_property_read_u32(np, "off-on-delay-us", &config->off_on_delay);
  97. if (of_find_property(np, "vin-supply", NULL))
  98. config->input_supply = "vin";
  99. return config;
  100. }
  101. static const struct regulator_ops fixed_voltage_ops = {
  102. };
  103. static const struct regulator_ops fixed_voltage_clkenabled_ops = {
  104. .enable = reg_clock_enable,
  105. .disable = reg_clock_disable,
  106. .is_enabled = reg_clock_is_enabled,
  107. };
  108. static int reg_fixed_voltage_probe(struct platform_device *pdev)
  109. {
  110. struct device *dev = &pdev->dev;
  111. struct fixed_voltage_config *config;
  112. struct fixed_voltage_data *drvdata;
  113. const struct fixed_dev_type *drvtype = of_device_get_match_data(dev);
  114. struct regulator_config cfg = { };
  115. enum gpiod_flags gflags;
  116. int ret;
  117. drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
  118. GFP_KERNEL);
  119. if (!drvdata)
  120. return -ENOMEM;
  121. if (pdev->dev.of_node) {
  122. config = of_get_fixed_voltage_config(&pdev->dev,
  123. &drvdata->desc);
  124. if (IS_ERR(config))
  125. return PTR_ERR(config);
  126. } else {
  127. config = dev_get_platdata(&pdev->dev);
  128. }
  129. if (!config)
  130. return -ENOMEM;
  131. drvdata->desc.name = devm_kstrdup(&pdev->dev,
  132. config->supply_name,
  133. GFP_KERNEL);
  134. if (drvdata->desc.name == NULL) {
  135. dev_err(&pdev->dev, "Failed to allocate supply name\n");
  136. return -ENOMEM;
  137. }
  138. drvdata->desc.type = REGULATOR_VOLTAGE;
  139. drvdata->desc.owner = THIS_MODULE;
  140. if (drvtype && drvtype->has_enable_clock) {
  141. drvdata->desc.ops = &fixed_voltage_clkenabled_ops;
  142. drvdata->enable_clock = devm_clk_get(dev, NULL);
  143. if (IS_ERR(drvdata->enable_clock)) {
  144. dev_err(dev, "Can't get enable-clock from devicetree\n");
  145. return -ENOENT;
  146. }
  147. } else {
  148. drvdata->desc.ops = &fixed_voltage_ops;
  149. }
  150. drvdata->desc.enable_time = config->startup_delay;
  151. drvdata->desc.off_on_delay = config->off_on_delay;
  152. if (config->input_supply) {
  153. drvdata->desc.supply_name = devm_kstrdup(&pdev->dev,
  154. config->input_supply,
  155. GFP_KERNEL);
  156. if (!drvdata->desc.supply_name) {
  157. dev_err(&pdev->dev,
  158. "Failed to allocate input supply\n");
  159. return -ENOMEM;
  160. }
  161. }
  162. if (config->microvolts)
  163. drvdata->desc.n_voltages = 1;
  164. drvdata->desc.fixed_uV = config->microvolts;
  165. /*
  166. * The signal will be inverted by the GPIO core if flagged so in the
  167. * descriptor.
  168. */
  169. if (config->enabled_at_boot)
  170. gflags = GPIOD_OUT_HIGH;
  171. else
  172. gflags = GPIOD_OUT_LOW;
  173. /*
  174. * Some fixed regulators share the enable line between two
  175. * regulators which makes it necessary to get a handle on the
  176. * same descriptor for two different consumers. This will get
  177. * the GPIO descriptor, but only the first call will initialize
  178. * it so any flags such as inversion or open drain will only
  179. * be set up by the first caller and assumed identical on the
  180. * next caller.
  181. *
  182. * FIXME: find a better way to deal with this.
  183. */
  184. gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
  185. /*
  186. * Do not use devm* here: the regulator core takes over the
  187. * lifecycle management of the GPIO descriptor.
  188. */
  189. cfg.ena_gpiod = gpiod_get_optional(&pdev->dev, NULL, gflags);
  190. if (IS_ERR(cfg.ena_gpiod))
  191. return PTR_ERR(cfg.ena_gpiod);
  192. cfg.dev = &pdev->dev;
  193. cfg.init_data = config->init_data;
  194. cfg.driver_data = drvdata;
  195. cfg.of_node = pdev->dev.of_node;
  196. drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc,
  197. &cfg);
  198. if (IS_ERR(drvdata->dev)) {
  199. ret = PTR_ERR(drvdata->dev);
  200. dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
  201. return ret;
  202. }
  203. platform_set_drvdata(pdev, drvdata);
  204. dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
  205. drvdata->desc.fixed_uV);
  206. return 0;
  207. }
  208. #if defined(CONFIG_OF)
  209. static const struct fixed_dev_type fixed_voltage_data = {
  210. .has_enable_clock = false,
  211. };
  212. static const struct fixed_dev_type fixed_clkenable_data = {
  213. .has_enable_clock = true,
  214. };
  215. static const struct of_device_id fixed_of_match[] = {
  216. {
  217. .compatible = "regulator-fixed",
  218. .data = &fixed_voltage_data,
  219. },
  220. {
  221. .compatible = "regulator-fixed-clock",
  222. .data = &fixed_clkenable_data,
  223. },
  224. {
  225. },
  226. };
  227. MODULE_DEVICE_TABLE(of, fixed_of_match);
  228. #endif
  229. static struct platform_driver regulator_fixed_voltage_driver = {
  230. .probe = reg_fixed_voltage_probe,
  231. .driver = {
  232. .name = "reg-fixed-voltage",
  233. .of_match_table = of_match_ptr(fixed_of_match),
  234. },
  235. };
  236. static int __init regulator_fixed_voltage_init(void)
  237. {
  238. return platform_driver_register(&regulator_fixed_voltage_driver);
  239. }
  240. subsys_initcall(regulator_fixed_voltage_init);
  241. static void __exit regulator_fixed_voltage_exit(void)
  242. {
  243. platform_driver_unregister(&regulator_fixed_voltage_driver);
  244. }
  245. module_exit(regulator_fixed_voltage_exit);
  246. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  247. MODULE_DESCRIPTION("Fixed voltage regulator");
  248. MODULE_LICENSE("GPL");
  249. MODULE_ALIAS("platform:reg-fixed-voltage");