1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2024 Linaro Ltd.
7 #include <linux/delay.h>
8 #include <linux/device.h>
9 #include <linux/gpio/consumer.h>
10 #include <linux/jiffies.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/pwrseq/provider.h>
17 #include <linux/string.h>
18 #include <linux/types.h>
20 struct pwrseq_qcom_wcn_pdata {
21 const char *const *vregs;
23 unsigned int pwup_delay_ms;
24 unsigned int gpio_enable_delay_ms;
27 struct pwrseq_qcom_wcn_ctx {
28 struct pwrseq_device *pwrseq;
29 struct device_node *of_node;
30 const struct pwrseq_qcom_wcn_pdata *pdata;
31 struct regulator_bulk_data *regs;
32 struct gpio_desc *bt_gpio;
33 struct gpio_desc *wlan_gpio;
35 unsigned long last_gpio_enable_jf;
38 static void pwrseq_qcom_wcn_ensure_gpio_delay(struct pwrseq_qcom_wcn_ctx *ctx)
40 unsigned long diff_jiffies;
41 unsigned int diff_msecs;
43 if (!ctx->pdata->gpio_enable_delay_ms)
46 diff_jiffies = jiffies - ctx->last_gpio_enable_jf;
47 diff_msecs = jiffies_to_msecs(diff_jiffies);
49 if (diff_msecs < ctx->pdata->gpio_enable_delay_ms)
50 msleep(ctx->pdata->gpio_enable_delay_ms - diff_msecs);
53 static int pwrseq_qcom_wcn_vregs_enable(struct pwrseq_device *pwrseq)
55 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
57 return regulator_bulk_enable(ctx->pdata->num_vregs, ctx->regs);
60 static int pwrseq_qcom_wcn_vregs_disable(struct pwrseq_device *pwrseq)
62 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
64 return regulator_bulk_disable(ctx->pdata->num_vregs, ctx->regs);
67 static const struct pwrseq_unit_data pwrseq_qcom_wcn_vregs_unit_data = {
68 .name = "regulators-enable",
69 .enable = pwrseq_qcom_wcn_vregs_enable,
70 .disable = pwrseq_qcom_wcn_vregs_disable,
73 static int pwrseq_qcom_wcn_clk_enable(struct pwrseq_device *pwrseq)
75 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
77 return clk_prepare_enable(ctx->clk);
80 static int pwrseq_qcom_wcn_clk_disable(struct pwrseq_device *pwrseq)
82 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
84 clk_disable_unprepare(ctx->clk);
89 static const struct pwrseq_unit_data pwrseq_qcom_wcn_clk_unit_data = {
90 .name = "clock-enable",
91 .enable = pwrseq_qcom_wcn_clk_enable,
92 .disable = pwrseq_qcom_wcn_clk_disable,
95 static const struct pwrseq_unit_data *pwrseq_qcom_wcn_unit_deps[] = {
96 &pwrseq_qcom_wcn_vregs_unit_data,
97 &pwrseq_qcom_wcn_clk_unit_data,
101 static int pwrseq_qcom_wcn_bt_enable(struct pwrseq_device *pwrseq)
103 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
105 pwrseq_qcom_wcn_ensure_gpio_delay(ctx);
106 gpiod_set_value_cansleep(ctx->bt_gpio, 1);
107 ctx->last_gpio_enable_jf = jiffies;
112 static int pwrseq_qcom_wcn_bt_disable(struct pwrseq_device *pwrseq)
114 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
116 gpiod_set_value_cansleep(ctx->bt_gpio, 0);
121 static const struct pwrseq_unit_data pwrseq_qcom_wcn_bt_unit_data = {
122 .name = "bluetooth-enable",
123 .deps = pwrseq_qcom_wcn_unit_deps,
124 .enable = pwrseq_qcom_wcn_bt_enable,
125 .disable = pwrseq_qcom_wcn_bt_disable,
128 static int pwrseq_qcom_wcn_wlan_enable(struct pwrseq_device *pwrseq)
130 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
132 pwrseq_qcom_wcn_ensure_gpio_delay(ctx);
133 gpiod_set_value_cansleep(ctx->wlan_gpio, 1);
134 ctx->last_gpio_enable_jf = jiffies;
139 static int pwrseq_qcom_wcn_wlan_disable(struct pwrseq_device *pwrseq)
141 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
143 gpiod_set_value_cansleep(ctx->wlan_gpio, 0);
148 static const struct pwrseq_unit_data pwrseq_qcom_wcn_wlan_unit_data = {
149 .name = "wlan-enable",
150 .deps = pwrseq_qcom_wcn_unit_deps,
151 .enable = pwrseq_qcom_wcn_wlan_enable,
152 .disable = pwrseq_qcom_wcn_wlan_disable,
155 static int pwrseq_qcom_wcn_pwup_delay(struct pwrseq_device *pwrseq)
157 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
159 if (ctx->pdata->pwup_delay_ms)
160 msleep(ctx->pdata->pwup_delay_ms);
165 static const struct pwrseq_target_data pwrseq_qcom_wcn_bt_target_data = {
167 .unit = &pwrseq_qcom_wcn_bt_unit_data,
168 .post_enable = pwrseq_qcom_wcn_pwup_delay,
171 static const struct pwrseq_target_data pwrseq_qcom_wcn_wlan_target_data = {
173 .unit = &pwrseq_qcom_wcn_wlan_unit_data,
174 .post_enable = pwrseq_qcom_wcn_pwup_delay,
177 static const struct pwrseq_target_data *pwrseq_qcom_wcn_targets[] = {
178 &pwrseq_qcom_wcn_bt_target_data,
179 &pwrseq_qcom_wcn_wlan_target_data,
183 static const char *const pwrseq_qca6390_vregs[] = {
194 static const struct pwrseq_qcom_wcn_pdata pwrseq_qca6390_of_data = {
195 .vregs = pwrseq_qca6390_vregs,
196 .num_vregs = ARRAY_SIZE(pwrseq_qca6390_vregs),
198 .gpio_enable_delay_ms = 100,
201 static const char *const pwrseq_wcn7850_vregs[] = {
211 static const struct pwrseq_qcom_wcn_pdata pwrseq_wcn7850_of_data = {
212 .vregs = pwrseq_wcn7850_vregs,
213 .num_vregs = ARRAY_SIZE(pwrseq_wcn7850_vregs),
217 static int pwrseq_qcom_wcn_match(struct pwrseq_device *pwrseq,
220 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
221 struct device_node *dev_node = dev->of_node;
224 * The PMU supplies power to the Bluetooth and WLAN modules. both
225 * consume the PMU AON output so check the presence of the
226 * 'vddaon-supply' property and whether it leads us to the right
229 if (!of_property_present(dev_node, "vddaon-supply"))
232 struct device_node *reg_node __free(device_node) =
233 of_parse_phandle(dev_node, "vddaon-supply", 0);
238 * `reg_node` is the PMU AON regulator, its parent is the `regulators`
239 * node and finally its grandparent is the PMU device node that we're
242 if (!reg_node->parent || !reg_node->parent->parent ||
243 reg_node->parent->parent != ctx->of_node)
249 static int pwrseq_qcom_wcn_probe(struct platform_device *pdev)
251 struct device *dev = &pdev->dev;
252 struct pwrseq_qcom_wcn_ctx *ctx;
253 struct pwrseq_config config;
256 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
260 ctx->of_node = dev->of_node;
262 ctx->pdata = of_device_get_match_data(dev);
264 return dev_err_probe(dev, -ENODEV,
265 "Failed to obtain platform data\n");
267 ctx->regs = devm_kcalloc(dev, ctx->pdata->num_vregs,
268 sizeof(*ctx->regs), GFP_KERNEL);
272 for (i = 0; i < ctx->pdata->num_vregs; i++)
273 ctx->regs[i].supply = ctx->pdata->vregs[i];
275 ret = devm_regulator_bulk_get(dev, ctx->pdata->num_vregs, ctx->regs);
277 return dev_err_probe(dev, ret,
278 "Failed to get all regulators\n");
280 ctx->bt_gpio = devm_gpiod_get_optional(dev, "bt-enable", GPIOD_OUT_LOW);
281 if (IS_ERR(ctx->bt_gpio))
282 return dev_err_probe(dev, PTR_ERR(ctx->bt_gpio),
283 "Failed to get the Bluetooth enable GPIO\n");
285 ctx->wlan_gpio = devm_gpiod_get_optional(dev, "wlan-enable",
287 if (IS_ERR(ctx->wlan_gpio))
288 return dev_err_probe(dev, PTR_ERR(ctx->wlan_gpio),
289 "Failed to get the WLAN enable GPIO\n");
291 ctx->clk = devm_clk_get_optional(dev, NULL);
292 if (IS_ERR(ctx->clk))
293 return dev_err_probe(dev, PTR_ERR(ctx->clk),
294 "Failed to get the reference clock\n");
296 memset(&config, 0, sizeof(config));
299 config.owner = THIS_MODULE;
300 config.drvdata = ctx;
301 config.match = pwrseq_qcom_wcn_match;
302 config.targets = pwrseq_qcom_wcn_targets;
304 ctx->pwrseq = devm_pwrseq_device_register(dev, &config);
305 if (IS_ERR(ctx->pwrseq))
306 return dev_err_probe(dev, PTR_ERR(ctx->pwrseq),
307 "Failed to register the power sequencer\n");
312 static const struct of_device_id pwrseq_qcom_wcn_of_match[] = {
314 .compatible = "qcom,qca6390-pmu",
315 .data = &pwrseq_qca6390_of_data,
318 .compatible = "qcom,wcn7850-pmu",
319 .data = &pwrseq_wcn7850_of_data,
323 MODULE_DEVICE_TABLE(of, pwrseq_qcom_wcn_of_match);
325 static struct platform_driver pwrseq_qcom_wcn_driver = {
327 .name = "pwrseq-qcom_wcn",
328 .of_match_table = pwrseq_qcom_wcn_of_match,
330 .probe = pwrseq_qcom_wcn_probe,
332 module_platform_driver(pwrseq_qcom_wcn_driver);
335 MODULE_DESCRIPTION("Qualcomm WCN PMU power sequencing driver");
336 MODULE_LICENSE("GPL");