fixed.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 <[email protected]>
  8. *
  9. * Copyright (c) 2009 Nokia Corporation
  10. * Roger Quadros <[email protected]>
  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/pm_domain.h>
  21. #include <linux/pm_opp.h>
  22. #include <linux/regulator/driver.h>
  23. #include <linux/regulator/fixed.h>
  24. #include <linux/gpio/consumer.h>
  25. #include <linux/slab.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/regulator/of_regulator.h>
  29. #ifdef QTI_FIXED_REGULATOR
  30. #include <linux/regulator/debug-regulator.h>
  31. #include <linux/regulator/proxy-consumer.h>
  32. #endif
  33. #include <linux/regulator/machine.h>
  34. #include <linux/clk.h>
  35. struct fixed_voltage_data {
  36. struct regulator_desc desc;
  37. struct regulator_dev *dev;
  38. struct clk *enable_clock;
  39. unsigned int enable_counter;
  40. int performance_state;
  41. };
  42. struct fixed_dev_type {
  43. bool has_enable_clock;
  44. bool has_performance_state;
  45. };
  46. static int reg_clock_enable(struct regulator_dev *rdev)
  47. {
  48. struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
  49. int ret = 0;
  50. ret = clk_prepare_enable(priv->enable_clock);
  51. if (ret)
  52. return ret;
  53. priv->enable_counter++;
  54. return ret;
  55. }
  56. static int reg_clock_disable(struct regulator_dev *rdev)
  57. {
  58. struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
  59. clk_disable_unprepare(priv->enable_clock);
  60. priv->enable_counter--;
  61. return 0;
  62. }
  63. static int reg_domain_enable(struct regulator_dev *rdev)
  64. {
  65. struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
  66. struct device *dev = rdev->dev.parent;
  67. int ret;
  68. ret = dev_pm_genpd_set_performance_state(dev, priv->performance_state);
  69. if (ret)
  70. return ret;
  71. priv->enable_counter++;
  72. return ret;
  73. }
  74. static int reg_domain_disable(struct regulator_dev *rdev)
  75. {
  76. struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
  77. struct device *dev = rdev->dev.parent;
  78. int ret;
  79. ret = dev_pm_genpd_set_performance_state(dev, 0);
  80. if (ret)
  81. return ret;
  82. priv->enable_counter--;
  83. return 0;
  84. }
  85. static int reg_is_enabled(struct regulator_dev *rdev)
  86. {
  87. struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
  88. return priv->enable_counter > 0;
  89. }
  90. /**
  91. * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
  92. * @dev: device requesting for fixed_voltage_config
  93. * @desc: regulator description
  94. *
  95. * Populates fixed_voltage_config structure by extracting data from device
  96. * tree node, returns a pointer to the populated structure of NULL if memory
  97. * alloc fails.
  98. */
  99. static struct fixed_voltage_config *
  100. of_get_fixed_voltage_config(struct device *dev,
  101. const struct regulator_desc *desc)
  102. {
  103. struct fixed_voltage_config *config;
  104. struct device_node *np = dev->of_node;
  105. struct regulator_init_data *init_data;
  106. config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
  107. GFP_KERNEL);
  108. if (!config)
  109. return ERR_PTR(-ENOMEM);
  110. config->init_data = of_get_regulator_init_data(dev, dev->of_node, desc);
  111. if (!config->init_data)
  112. return ERR_PTR(-EINVAL);
  113. init_data = config->init_data;
  114. init_data->constraints.apply_uV = 0;
  115. config->supply_name = init_data->constraints.name;
  116. if (init_data->constraints.min_uV == init_data->constraints.max_uV) {
  117. config->microvolts = init_data->constraints.min_uV;
  118. } else {
  119. dev_err(dev,
  120. "Fixed regulator specified with variable voltages\n");
  121. return ERR_PTR(-EINVAL);
  122. }
  123. if (init_data->constraints.boot_on)
  124. config->enabled_at_boot = true;
  125. of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
  126. of_property_read_u32(np, "off-on-delay-us", &config->off_on_delay);
  127. if (of_find_property(np, "vin-supply", NULL))
  128. config->input_supply = "vin";
  129. return config;
  130. }
  131. static const struct regulator_ops fixed_voltage_ops = {
  132. };
  133. static const struct regulator_ops fixed_voltage_clkenabled_ops = {
  134. .enable = reg_clock_enable,
  135. .disable = reg_clock_disable,
  136. .is_enabled = reg_is_enabled,
  137. };
  138. static const struct regulator_ops fixed_voltage_domain_ops = {
  139. .enable = reg_domain_enable,
  140. .disable = reg_domain_disable,
  141. .is_enabled = reg_is_enabled,
  142. };
  143. #ifdef QTI_FIXED_REGULATOR
  144. static void qti_reg_fixed_voltage_init(struct device *dev,
  145. struct regulator_dev *rdev)
  146. {
  147. int ret;
  148. ret = devm_regulator_proxy_consumer_register(dev, dev->of_node);
  149. if (ret)
  150. dev_err(dev, "failed to register proxy consumer, ret=%d\n",
  151. ret);
  152. ret = devm_regulator_debug_register(dev, rdev);
  153. if (ret)
  154. dev_err(dev, "failed to register debug regulator, ret=%d\n",
  155. ret);
  156. }
  157. #endif
  158. static int reg_fixed_voltage_probe(struct platform_device *pdev)
  159. {
  160. struct device *dev = &pdev->dev;
  161. struct fixed_voltage_config *config;
  162. struct fixed_voltage_data *drvdata;
  163. const struct fixed_dev_type *drvtype = of_device_get_match_data(dev);
  164. struct regulator_config cfg = { };
  165. enum gpiod_flags gflags;
  166. int ret;
  167. drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
  168. GFP_KERNEL);
  169. if (!drvdata)
  170. return -ENOMEM;
  171. if (pdev->dev.of_node) {
  172. config = of_get_fixed_voltage_config(&pdev->dev,
  173. &drvdata->desc);
  174. if (IS_ERR(config))
  175. return PTR_ERR(config);
  176. } else {
  177. config = dev_get_platdata(&pdev->dev);
  178. }
  179. if (!config)
  180. return -ENOMEM;
  181. drvdata->desc.name = devm_kstrdup(&pdev->dev,
  182. config->supply_name,
  183. GFP_KERNEL);
  184. if (drvdata->desc.name == NULL) {
  185. dev_err(&pdev->dev, "Failed to allocate supply name\n");
  186. return -ENOMEM;
  187. }
  188. drvdata->desc.type = REGULATOR_VOLTAGE;
  189. drvdata->desc.owner = THIS_MODULE;
  190. if (drvtype && drvtype->has_enable_clock) {
  191. drvdata->desc.ops = &fixed_voltage_clkenabled_ops;
  192. drvdata->enable_clock = devm_clk_get(dev, NULL);
  193. if (IS_ERR(drvdata->enable_clock)) {
  194. dev_err(dev, "Can't get enable-clock from devicetree\n");
  195. return PTR_ERR(drvdata->enable_clock);
  196. }
  197. } else if (drvtype && drvtype->has_performance_state) {
  198. drvdata->desc.ops = &fixed_voltage_domain_ops;
  199. drvdata->performance_state = of_get_required_opp_performance_state(dev->of_node, 0);
  200. if (drvdata->performance_state < 0) {
  201. dev_err(dev, "Can't get performance state from devicetree\n");
  202. return drvdata->performance_state;
  203. }
  204. } else {
  205. drvdata->desc.ops = &fixed_voltage_ops;
  206. }
  207. drvdata->desc.enable_time = config->startup_delay;
  208. drvdata->desc.off_on_delay = config->off_on_delay;
  209. if (config->input_supply) {
  210. drvdata->desc.supply_name = devm_kstrdup(&pdev->dev,
  211. config->input_supply,
  212. GFP_KERNEL);
  213. if (!drvdata->desc.supply_name)
  214. return -ENOMEM;
  215. }
  216. if (config->microvolts)
  217. drvdata->desc.n_voltages = 1;
  218. drvdata->desc.fixed_uV = config->microvolts;
  219. /*
  220. * The signal will be inverted by the GPIO core if flagged so in the
  221. * descriptor.
  222. */
  223. if (config->enabled_at_boot)
  224. gflags = GPIOD_OUT_HIGH;
  225. else
  226. gflags = GPIOD_OUT_LOW;
  227. /*
  228. * Some fixed regulators share the enable line between two
  229. * regulators which makes it necessary to get a handle on the
  230. * same descriptor for two different consumers. This will get
  231. * the GPIO descriptor, but only the first call will initialize
  232. * it so any flags such as inversion or open drain will only
  233. * be set up by the first caller and assumed identical on the
  234. * next caller.
  235. *
  236. * FIXME: find a better way to deal with this.
  237. */
  238. gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
  239. /*
  240. * Do not use devm* here: the regulator core takes over the
  241. * lifecycle management of the GPIO descriptor.
  242. */
  243. cfg.ena_gpiod = gpiod_get_optional(&pdev->dev, NULL, gflags);
  244. if (IS_ERR(cfg.ena_gpiod))
  245. return dev_err_probe(&pdev->dev, PTR_ERR(cfg.ena_gpiod),
  246. "can't get GPIO\n");
  247. cfg.dev = &pdev->dev;
  248. cfg.init_data = config->init_data;
  249. cfg.driver_data = drvdata;
  250. cfg.of_node = pdev->dev.of_node;
  251. drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc,
  252. &cfg);
  253. if (IS_ERR(drvdata->dev)) {
  254. ret = dev_err_probe(&pdev->dev, PTR_ERR(drvdata->dev),
  255. "Failed to register regulator: %ld\n",
  256. PTR_ERR(drvdata->dev));
  257. return ret;
  258. }
  259. platform_set_drvdata(pdev, drvdata);
  260. #ifdef QTI_FIXED_REGULATOR
  261. qti_reg_fixed_voltage_init(dev, drvdata->dev);
  262. #endif
  263. dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
  264. drvdata->desc.fixed_uV);
  265. return 0;
  266. }
  267. #if defined(CONFIG_OF)
  268. static const struct fixed_dev_type fixed_voltage_data = {
  269. .has_enable_clock = false,
  270. };
  271. static const struct fixed_dev_type fixed_clkenable_data = {
  272. .has_enable_clock = true,
  273. };
  274. static const struct fixed_dev_type fixed_domain_data = {
  275. .has_performance_state = true,
  276. };
  277. static const struct of_device_id fixed_of_match[] = {
  278. #ifdef QTI_FIXED_REGULATOR
  279. {
  280. .compatible = "qti-regulator-fixed",
  281. .data = &fixed_voltage_data,
  282. },
  283. #endif
  284. {
  285. .compatible = "regulator-fixed",
  286. .data = &fixed_voltage_data,
  287. },
  288. {
  289. .compatible = "regulator-fixed-clock",
  290. .data = &fixed_clkenable_data,
  291. },
  292. {
  293. .compatible = "regulator-fixed-domain",
  294. .data = &fixed_domain_data,
  295. },
  296. {
  297. },
  298. };
  299. MODULE_DEVICE_TABLE(of, fixed_of_match);
  300. #endif
  301. static struct platform_driver regulator_fixed_voltage_driver = {
  302. .probe = reg_fixed_voltage_probe,
  303. .driver = {
  304. #ifdef QTI_FIXED_REGULATOR
  305. .name = "qti-reg-fixed-voltage",
  306. .sync_state = regulator_proxy_consumer_sync_state,
  307. #else
  308. .name = "reg-fixed-voltage",
  309. #endif
  310. .of_match_table = of_match_ptr(fixed_of_match),
  311. },
  312. };
  313. static int __init regulator_fixed_voltage_init(void)
  314. {
  315. return platform_driver_register(&regulator_fixed_voltage_driver);
  316. }
  317. subsys_initcall(regulator_fixed_voltage_init);
  318. static void __exit regulator_fixed_voltage_exit(void)
  319. {
  320. platform_driver_unregister(&regulator_fixed_voltage_driver);
  321. }
  322. module_exit(regulator_fixed_voltage_exit);
  323. MODULE_AUTHOR("Mark Brown <[email protected]>");
  324. MODULE_DESCRIPTION("Fixed voltage regulator");
  325. MODULE_LICENSE("GPL");
  326. MODULE_ALIAS("platform:reg-fixed-voltage");