fixed.c 10 KB

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