12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
24b74ff65SMark Brown /*
34b74ff65SMark Brown * fixed.c
44b74ff65SMark Brown *
54b74ff65SMark Brown * Copyright 2008 Wolfson Microelectronics PLC.
64b74ff65SMark Brown *
74b74ff65SMark Brown * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
84b74ff65SMark Brown *
986d9884bSRoger Quadros * Copyright (c) 2009 Nokia Corporation
1086d9884bSRoger Quadros * Roger Quadros <ext-roger.quadros@nokia.com>
1186d9884bSRoger Quadros *
124b74ff65SMark Brown * This is useful for systems with mixed controllable and
134b74ff65SMark Brown * non-controllable regulators, as well as for allowing testing on
144b74ff65SMark Brown * systems with no controllable regulators.
154b74ff65SMark Brown */
164b74ff65SMark Brown
174b74ff65SMark Brown #include <linux/err.h>
184b74ff65SMark Brown #include <linux/mutex.h>
1965602c32SPaul Gortmaker #include <linux/module.h>
204b74ff65SMark Brown #include <linux/platform_device.h>
21bf3a28cfSDmitry Baryshkov #include <linux/pm_domain.h>
22bf3a28cfSDmitry Baryshkov #include <linux/pm_opp.h>
234b74ff65SMark Brown #include <linux/regulator/driver.h>
244b74ff65SMark Brown #include <linux/regulator/fixed.h>
25efdfeb07SLinus Walleij #include <linux/gpio/consumer.h>
265a0e3ad6STejun Heo #include <linux/slab.h>
27cef49102SRajendra Nayak #include <linux/of.h>
28cef49102SRajendra Nayak #include <linux/regulator/of_regulator.h>
29cef49102SRajendra Nayak #include <linux/regulator/machine.h>
308959e532SPhilippe Schenker #include <linux/clk.h>
318959e532SPhilippe Schenker
324b74ff65SMark Brown
334b74ff65SMark Brown struct fixed_voltage_data {
344b74ff65SMark Brown struct regulator_desc desc;
354b74ff65SMark Brown struct regulator_dev *dev;
368959e532SPhilippe Schenker
378959e532SPhilippe Schenker struct clk *enable_clock;
38bf3a28cfSDmitry Baryshkov unsigned int enable_counter;
39bf3a28cfSDmitry Baryshkov int performance_state;
404b74ff65SMark Brown };
414b74ff65SMark Brown
428959e532SPhilippe Schenker struct fixed_dev_type {
438959e532SPhilippe Schenker bool has_enable_clock;
44bf3a28cfSDmitry Baryshkov bool has_performance_state;
458959e532SPhilippe Schenker };
468959e532SPhilippe Schenker
reg_clock_enable(struct regulator_dev * rdev)478959e532SPhilippe Schenker static int reg_clock_enable(struct regulator_dev *rdev)
488959e532SPhilippe Schenker {
498959e532SPhilippe Schenker struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
508959e532SPhilippe Schenker int ret = 0;
518959e532SPhilippe Schenker
528959e532SPhilippe Schenker ret = clk_prepare_enable(priv->enable_clock);
538959e532SPhilippe Schenker if (ret)
548959e532SPhilippe Schenker return ret;
558959e532SPhilippe Schenker
56bf3a28cfSDmitry Baryshkov priv->enable_counter++;
578959e532SPhilippe Schenker
588959e532SPhilippe Schenker return ret;
598959e532SPhilippe Schenker }
608959e532SPhilippe Schenker
reg_clock_disable(struct regulator_dev * rdev)618959e532SPhilippe Schenker static int reg_clock_disable(struct regulator_dev *rdev)
628959e532SPhilippe Schenker {
638959e532SPhilippe Schenker struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
648959e532SPhilippe Schenker
658959e532SPhilippe Schenker clk_disable_unprepare(priv->enable_clock);
66bf3a28cfSDmitry Baryshkov priv->enable_counter--;
678959e532SPhilippe Schenker
688959e532SPhilippe Schenker return 0;
698959e532SPhilippe Schenker }
708959e532SPhilippe Schenker
reg_domain_enable(struct regulator_dev * rdev)71bf3a28cfSDmitry Baryshkov static int reg_domain_enable(struct regulator_dev *rdev)
72bf3a28cfSDmitry Baryshkov {
73bf3a28cfSDmitry Baryshkov struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
74bf3a28cfSDmitry Baryshkov struct device *dev = rdev->dev.parent;
75bf3a28cfSDmitry Baryshkov int ret;
76bf3a28cfSDmitry Baryshkov
77bf3a28cfSDmitry Baryshkov ret = dev_pm_genpd_set_performance_state(dev, priv->performance_state);
78bf3a28cfSDmitry Baryshkov if (ret)
79bf3a28cfSDmitry Baryshkov return ret;
80bf3a28cfSDmitry Baryshkov
81bf3a28cfSDmitry Baryshkov priv->enable_counter++;
82bf3a28cfSDmitry Baryshkov
83bf3a28cfSDmitry Baryshkov return ret;
84bf3a28cfSDmitry Baryshkov }
85bf3a28cfSDmitry Baryshkov
reg_domain_disable(struct regulator_dev * rdev)86bf3a28cfSDmitry Baryshkov static int reg_domain_disable(struct regulator_dev *rdev)
87bf3a28cfSDmitry Baryshkov {
88bf3a28cfSDmitry Baryshkov struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
89bf3a28cfSDmitry Baryshkov struct device *dev = rdev->dev.parent;
90855bfff9SAxel Lin int ret;
91855bfff9SAxel Lin
92855bfff9SAxel Lin ret = dev_pm_genpd_set_performance_state(dev, 0);
93855bfff9SAxel Lin if (ret)
94855bfff9SAxel Lin return ret;
95bf3a28cfSDmitry Baryshkov
96bf3a28cfSDmitry Baryshkov priv->enable_counter--;
97bf3a28cfSDmitry Baryshkov
98855bfff9SAxel Lin return 0;
99bf3a28cfSDmitry Baryshkov }
100bf3a28cfSDmitry Baryshkov
reg_is_enabled(struct regulator_dev * rdev)101bf3a28cfSDmitry Baryshkov static int reg_is_enabled(struct regulator_dev *rdev)
1028959e532SPhilippe Schenker {
1038959e532SPhilippe Schenker struct fixed_voltage_data *priv = rdev_get_drvdata(rdev);
1048959e532SPhilippe Schenker
105bf3a28cfSDmitry Baryshkov return priv->enable_counter > 0;
1068959e532SPhilippe Schenker }
1078959e532SPhilippe Schenker
108cef49102SRajendra Nayak
109cef49102SRajendra Nayak /**
110cef49102SRajendra Nayak * of_get_fixed_voltage_config - extract fixed_voltage_config structure info
111cef49102SRajendra Nayak * @dev: device requesting for fixed_voltage_config
112072e78b1SJavier Martinez Canillas * @desc: regulator description
113cef49102SRajendra Nayak *
114cef49102SRajendra Nayak * Populates fixed_voltage_config structure by extracting data from device
115cef49102SRajendra Nayak * tree node, returns a pointer to the populated structure of NULL if memory
116cef49102SRajendra Nayak * alloc fails.
117cef49102SRajendra Nayak */
118bc91396bSAxel Lin static struct fixed_voltage_config *
of_get_fixed_voltage_config(struct device * dev,const struct regulator_desc * desc)119072e78b1SJavier Martinez Canillas of_get_fixed_voltage_config(struct device *dev,
120072e78b1SJavier Martinez Canillas const struct regulator_desc *desc)
121cef49102SRajendra Nayak {
122cef49102SRajendra Nayak struct fixed_voltage_config *config;
123cef49102SRajendra Nayak struct device_node *np = dev->of_node;
124cef49102SRajendra Nayak struct regulator_init_data *init_data;
125cef49102SRajendra Nayak
126cef49102SRajendra Nayak config = devm_kzalloc(dev, sizeof(struct fixed_voltage_config),
127cef49102SRajendra Nayak GFP_KERNEL);
128cef49102SRajendra Nayak if (!config)
129f141822bSStephen Warren return ERR_PTR(-ENOMEM);
130cef49102SRajendra Nayak
131072e78b1SJavier Martinez Canillas config->init_data = of_get_regulator_init_data(dev, dev->of_node, desc);
1324b864af1SAxel Lin if (!config->init_data)
133f141822bSStephen Warren return ERR_PTR(-EINVAL);
1344b864af1SAxel Lin
135cef49102SRajendra Nayak init_data = config->init_data;
1360c437c4aSRichard Zhao init_data->constraints.apply_uV = 0;
137cef49102SRajendra Nayak
138cef49102SRajendra Nayak config->supply_name = init_data->constraints.name;
139cef49102SRajendra Nayak if (init_data->constraints.min_uV == init_data->constraints.max_uV) {
140cef49102SRajendra Nayak config->microvolts = init_data->constraints.min_uV;
141cef49102SRajendra Nayak } else {
142cef49102SRajendra Nayak dev_err(dev,
143cef49102SRajendra Nayak "Fixed regulator specified with variable voltages\n");
144f141822bSStephen Warren return ERR_PTR(-EINVAL);
145cef49102SRajendra Nayak }
146cef49102SRajendra Nayak
147cef49102SRajendra Nayak if (init_data->constraints.boot_on)
148cef49102SRajendra Nayak config->enabled_at_boot = true;
149cef49102SRajendra Nayak
1504127f696SSergei Shtylyov of_property_read_u32(np, "startup-delay-us", &config->startup_delay);
151f7907e57SPeng Fan of_property_read_u32(np, "off-on-delay-us", &config->off_on_delay);
152cef49102SRajendra Nayak
1537dda20c9SRob Herring if (of_property_present(np, "vin-supply"))
1546be5bfc3SLaxman Dewangan config->input_supply = "vin";
1556be5bfc3SLaxman Dewangan
156cef49102SRajendra Nayak return config;
157cef49102SRajendra Nayak }
158cef49102SRajendra Nayak
15996ee75ffSRikard Falkeborn static const struct regulator_ops fixed_voltage_ops = {
1609d442061SMark Brown };
1619d442061SMark Brown
16296ee75ffSRikard Falkeborn static const struct regulator_ops fixed_voltage_clkenabled_ops = {
1638959e532SPhilippe Schenker .enable = reg_clock_enable,
1648959e532SPhilippe Schenker .disable = reg_clock_disable,
165bf3a28cfSDmitry Baryshkov .is_enabled = reg_is_enabled,
166bf3a28cfSDmitry Baryshkov };
167bf3a28cfSDmitry Baryshkov
168bf3a28cfSDmitry Baryshkov static const struct regulator_ops fixed_voltage_domain_ops = {
169bf3a28cfSDmitry Baryshkov .enable = reg_domain_enable,
170bf3a28cfSDmitry Baryshkov .disable = reg_domain_disable,
171bf3a28cfSDmitry Baryshkov .is_enabled = reg_is_enabled,
1728959e532SPhilippe Schenker };
1738959e532SPhilippe Schenker
reg_fixed_voltage_probe(struct platform_device * pdev)174a5023574SBill Pemberton static int reg_fixed_voltage_probe(struct platform_device *pdev)
1754b74ff65SMark Brown {
1768959e532SPhilippe Schenker struct device *dev = &pdev->dev;
17722d881c0SAxel Lin struct fixed_voltage_config *config;
1784b74ff65SMark Brown struct fixed_voltage_data *drvdata;
1791d6db22fSAxel Lin const struct fixed_dev_type *drvtype = of_device_get_match_data(dev);
180c172708dSMark Brown struct regulator_config cfg = { };
181efdfeb07SLinus Walleij enum gpiod_flags gflags;
1824b74ff65SMark Brown int ret;
1834b74ff65SMark Brown
184072e78b1SJavier Martinez Canillas drvdata = devm_kzalloc(&pdev->dev, sizeof(struct fixed_voltage_data),
185072e78b1SJavier Martinez Canillas GFP_KERNEL);
186072e78b1SJavier Martinez Canillas if (!drvdata)
187072e78b1SJavier Martinez Canillas return -ENOMEM;
188072e78b1SJavier Martinez Canillas
189f141822bSStephen Warren if (pdev->dev.of_node) {
190072e78b1SJavier Martinez Canillas config = of_get_fixed_voltage_config(&pdev->dev,
191072e78b1SJavier Martinez Canillas &drvdata->desc);
192f141822bSStephen Warren if (IS_ERR(config))
193f141822bSStephen Warren return PTR_ERR(config);
194f141822bSStephen Warren } else {
195dff91d0bSJingoo Han config = dev_get_platdata(&pdev->dev);
196f141822bSStephen Warren }
19722d881c0SAxel Lin
19822d881c0SAxel Lin if (!config)
19922d881c0SAxel Lin return -ENOMEM;
200cef49102SRajendra Nayak
20184d0ffbeSManish Badarkhe drvdata->desc.name = devm_kstrdup(&pdev->dev,
20284d0ffbeSManish Badarkhe config->supply_name,
20384d0ffbeSManish Badarkhe GFP_KERNEL);
2044b74ff65SMark Brown if (drvdata->desc.name == NULL) {
205c53ad7feSMark Brown dev_err(&pdev->dev, "Failed to allocate supply name\n");
20684d0ffbeSManish Badarkhe return -ENOMEM;
2074b74ff65SMark Brown }
2084b74ff65SMark Brown drvdata->desc.type = REGULATOR_VOLTAGE;
2094b74ff65SMark Brown drvdata->desc.owner = THIS_MODULE;
2108959e532SPhilippe Schenker
2111d6db22fSAxel Lin if (drvtype && drvtype->has_enable_clock) {
2128959e532SPhilippe Schenker drvdata->desc.ops = &fixed_voltage_clkenabled_ops;
2138959e532SPhilippe Schenker
2148959e532SPhilippe Schenker drvdata->enable_clock = devm_clk_get(dev, NULL);
2158959e532SPhilippe Schenker if (IS_ERR(drvdata->enable_clock)) {
21609dad81eSColin Ian King dev_err(dev, "Can't get enable-clock from devicetree\n");
217*02bcba0bSChristophe JAILLET return PTR_ERR(drvdata->enable_clock);
2188959e532SPhilippe Schenker }
219bf3a28cfSDmitry Baryshkov } else if (drvtype && drvtype->has_performance_state) {
220bf3a28cfSDmitry Baryshkov drvdata->desc.ops = &fixed_voltage_domain_ops;
221bf3a28cfSDmitry Baryshkov
222bf3a28cfSDmitry Baryshkov drvdata->performance_state = of_get_required_opp_performance_state(dev->of_node, 0);
223bf3a28cfSDmitry Baryshkov if (drvdata->performance_state < 0) {
224bf3a28cfSDmitry Baryshkov dev_err(dev, "Can't get performance state from devicetree\n");
225bf3a28cfSDmitry Baryshkov return drvdata->performance_state;
226bf3a28cfSDmitry Baryshkov }
2278959e532SPhilippe Schenker } else {
22825a53dfbSMark Brown drvdata->desc.ops = &fixed_voltage_ops;
2298959e532SPhilippe Schenker }
2301c37f8a8SSascha Hauer
2313d0f267fSMark Brown drvdata->desc.enable_time = config->startup_delay;
232f7907e57SPeng Fan drvdata->desc.off_on_delay = config->off_on_delay;
2333d0f267fSMark Brown
2346be5bfc3SLaxman Dewangan if (config->input_supply) {
23584d0ffbeSManish Badarkhe drvdata->desc.supply_name = devm_kstrdup(&pdev->dev,
23684d0ffbeSManish Badarkhe config->input_supply,
2376be5bfc3SLaxman Dewangan GFP_KERNEL);
2386c315afeSMark Brown if (!drvdata->desc.supply_name)
23984d0ffbeSManish Badarkhe return -ENOMEM;
2406be5bfc3SLaxman Dewangan }
2416be5bfc3SLaxman Dewangan
2421c37f8a8SSascha Hauer if (config->microvolts)
2439035cefcSMark Brown drvdata->desc.n_voltages = 1;
2444b74ff65SMark Brown
245c368e5fcSLaxman Dewangan drvdata->desc.fixed_uV = config->microvolts;
24686d9884bSRoger Quadros
24701dc79cdSLinus Walleij /*
24801dc79cdSLinus Walleij * The signal will be inverted by the GPIO core if flagged so in the
249d3f37233SKieran Bingham * descriptor.
25001dc79cdSLinus Walleij */
25101dc79cdSLinus Walleij if (config->enabled_at_boot)
252efdfeb07SLinus Walleij gflags = GPIOD_OUT_HIGH;
253609d5f6dSJingoo Han else
254efdfeb07SLinus Walleij gflags = GPIOD_OUT_LOW;
255efdfeb07SLinus Walleij
256b0ce7b29SLinus Walleij /*
257b0ce7b29SLinus Walleij * Some fixed regulators share the enable line between two
258b0ce7b29SLinus Walleij * regulators which makes it necessary to get a handle on the
259b0ce7b29SLinus Walleij * same descriptor for two different consumers. This will get
260b0ce7b29SLinus Walleij * the GPIO descriptor, but only the first call will initialize
261b0ce7b29SLinus Walleij * it so any flags such as inversion or open drain will only
262b0ce7b29SLinus Walleij * be set up by the first caller and assumed identical on the
263b0ce7b29SLinus Walleij * next caller.
264b0ce7b29SLinus Walleij *
265b0ce7b29SLinus Walleij * FIXME: find a better way to deal with this.
266b0ce7b29SLinus Walleij */
267b0ce7b29SLinus Walleij gflags |= GPIOD_FLAGS_BIT_NONEXCLUSIVE;
268b0ce7b29SLinus Walleij
2695e6f3ae5SLinus Walleij /*
2705e6f3ae5SLinus Walleij * Do not use devm* here: the regulator core takes over the
2715e6f3ae5SLinus Walleij * lifecycle management of the GPIO descriptor.
2725e6f3ae5SLinus Walleij */
2735e6f3ae5SLinus Walleij cfg.ena_gpiod = gpiod_get_optional(&pdev->dev, NULL, gflags);
274efdfeb07SLinus Walleij if (IS_ERR(cfg.ena_gpiod))
2757740ab84SRouven Czerwinski return dev_err_probe(&pdev->dev, PTR_ERR(cfg.ena_gpiod),
2767740ab84SRouven Czerwinski "can't get GPIO\n");
2774b74ff65SMark Brown
278c172708dSMark Brown cfg.dev = &pdev->dev;
279c172708dSMark Brown cfg.init_data = config->init_data;
280c172708dSMark Brown cfg.driver_data = drvdata;
281c172708dSMark Brown cfg.of_node = pdev->dev.of_node;
282c172708dSMark Brown
28384d0ffbeSManish Badarkhe drvdata->dev = devm_regulator_register(&pdev->dev, &drvdata->desc,
28484d0ffbeSManish Badarkhe &cfg);
2854b74ff65SMark Brown if (IS_ERR(drvdata->dev)) {
286d0f95e64SChris Morgan ret = dev_err_probe(&pdev->dev, PTR_ERR(drvdata->dev),
287d0f95e64SChris Morgan "Failed to register regulator: %ld\n",
288d0f95e64SChris Morgan PTR_ERR(drvdata->dev));
28984d0ffbeSManish Badarkhe return ret;
2904b74ff65SMark Brown }
2914b74ff65SMark Brown
2924b74ff65SMark Brown platform_set_drvdata(pdev, drvdata);
2934b74ff65SMark Brown
2944b74ff65SMark Brown dev_dbg(&pdev->dev, "%s supplying %duV\n", drvdata->desc.name,
295c368e5fcSLaxman Dewangan drvdata->desc.fixed_uV);
2964b74ff65SMark Brown
2974b74ff65SMark Brown return 0;
2984b74ff65SMark Brown }
2994b74ff65SMark Brown
300cef49102SRajendra Nayak #if defined(CONFIG_OF)
3010f037255SJisheng Zhang static const struct fixed_dev_type fixed_voltage_data = {
3020f037255SJisheng Zhang .has_enable_clock = false,
3030f037255SJisheng Zhang };
3040f037255SJisheng Zhang
3050f037255SJisheng Zhang static const struct fixed_dev_type fixed_clkenable_data = {
3060f037255SJisheng Zhang .has_enable_clock = true,
3070f037255SJisheng Zhang };
3080f037255SJisheng Zhang
309bf3a28cfSDmitry Baryshkov static const struct fixed_dev_type fixed_domain_data = {
310bf3a28cfSDmitry Baryshkov .has_performance_state = true,
311bf3a28cfSDmitry Baryshkov };
312bf3a28cfSDmitry Baryshkov
3133d68dfe3SGreg Kroah-Hartman static const struct of_device_id fixed_of_match[] = {
3148959e532SPhilippe Schenker {
3158959e532SPhilippe Schenker .compatible = "regulator-fixed",
3168959e532SPhilippe Schenker .data = &fixed_voltage_data,
3178959e532SPhilippe Schenker },
3188959e532SPhilippe Schenker {
3198959e532SPhilippe Schenker .compatible = "regulator-fixed-clock",
3208959e532SPhilippe Schenker .data = &fixed_clkenable_data,
3218959e532SPhilippe Schenker },
3228959e532SPhilippe Schenker {
323bf3a28cfSDmitry Baryshkov .compatible = "regulator-fixed-domain",
324bf3a28cfSDmitry Baryshkov .data = &fixed_domain_data,
325bf3a28cfSDmitry Baryshkov },
326bf3a28cfSDmitry Baryshkov {
3278959e532SPhilippe Schenker },
328cef49102SRajendra Nayak };
329cef49102SRajendra Nayak MODULE_DEVICE_TABLE(of, fixed_of_match);
330cef49102SRajendra Nayak #endif
331cef49102SRajendra Nayak
3324b74ff65SMark Brown static struct platform_driver regulator_fixed_voltage_driver = {
3338ab3343dSDmitry Torokhov .probe = reg_fixed_voltage_probe,
3344b74ff65SMark Brown .driver = {
3354b74ff65SMark Brown .name = "reg-fixed-voltage",
336259b93b2SDouglas Anderson .probe_type = PROBE_PREFER_ASYNCHRONOUS,
337abcfaf23SAxel Lin .of_match_table = of_match_ptr(fixed_of_match),
3384b74ff65SMark Brown },
3394b74ff65SMark Brown };
3404b74ff65SMark Brown
regulator_fixed_voltage_init(void)3414b74ff65SMark Brown static int __init regulator_fixed_voltage_init(void)
3424b74ff65SMark Brown {
3434b74ff65SMark Brown return platform_driver_register(®ulator_fixed_voltage_driver);
3444b74ff65SMark Brown }
3455a1b22beSMark Brown subsys_initcall(regulator_fixed_voltage_init);
3464b74ff65SMark Brown
regulator_fixed_voltage_exit(void)3474b74ff65SMark Brown static void __exit regulator_fixed_voltage_exit(void)
3484b74ff65SMark Brown {
3494b74ff65SMark Brown platform_driver_unregister(®ulator_fixed_voltage_driver);
3504b74ff65SMark Brown }
3514b74ff65SMark Brown module_exit(regulator_fixed_voltage_exit);
3524b74ff65SMark Brown
3534b74ff65SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
3544b74ff65SMark Brown MODULE_DESCRIPTION("Fixed voltage regulator");
3554b74ff65SMark Brown MODULE_LICENSE("GPL");
35638c53c89SMark Brown MODULE_ALIAS("platform:reg-fixed-voltage");
357