fixed.c 7.5 KB

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