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;
25 const struct pwrseq_target_data **targets;
28 struct pwrseq_qcom_wcn_ctx {
29 struct pwrseq_device *pwrseq;
30 struct device_node *of_node;
31 const struct pwrseq_qcom_wcn_pdata *pdata;
32 struct regulator_bulk_data *regs;
33 struct gpio_desc *bt_gpio;
34 struct gpio_desc *wlan_gpio;
35 struct gpio_desc *xo_clk_gpio;
37 unsigned long last_gpio_enable_jf;
40 static void pwrseq_qcom_wcn_ensure_gpio_delay(struct pwrseq_qcom_wcn_ctx *ctx)
42 unsigned long diff_jiffies;
43 unsigned int diff_msecs;
45 if (!ctx->pdata->gpio_enable_delay_ms)
48 diff_jiffies = jiffies - ctx->last_gpio_enable_jf;
49 diff_msecs = jiffies_to_msecs(diff_jiffies);
51 if (diff_msecs < ctx->pdata->gpio_enable_delay_ms)
52 msleep(ctx->pdata->gpio_enable_delay_ms - diff_msecs);
55 static int pwrseq_qcom_wcn_vregs_enable(struct pwrseq_device *pwrseq)
57 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
59 return regulator_bulk_enable(ctx->pdata->num_vregs, ctx->regs);
62 static int pwrseq_qcom_wcn_vregs_disable(struct pwrseq_device *pwrseq)
64 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
66 return regulator_bulk_disable(ctx->pdata->num_vregs, ctx->regs);
69 static const struct pwrseq_unit_data pwrseq_qcom_wcn_vregs_unit_data = {
70 .name = "regulators-enable",
71 .enable = pwrseq_qcom_wcn_vregs_enable,
72 .disable = pwrseq_qcom_wcn_vregs_disable,
75 static int pwrseq_qcom_wcn_clk_enable(struct pwrseq_device *pwrseq)
77 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
79 return clk_prepare_enable(ctx->clk);
82 static int pwrseq_qcom_wcn_clk_disable(struct pwrseq_device *pwrseq)
84 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
86 clk_disable_unprepare(ctx->clk);
91 static const struct pwrseq_unit_data pwrseq_qcom_wcn_clk_unit_data = {
92 .name = "clock-enable",
93 .enable = pwrseq_qcom_wcn_clk_enable,
94 .disable = pwrseq_qcom_wcn_clk_disable,
97 static const struct pwrseq_unit_data *pwrseq_qcom_wcn_unit_deps[] = {
98 &pwrseq_qcom_wcn_vregs_unit_data,
99 &pwrseq_qcom_wcn_clk_unit_data,
103 static int pwrseq_qcom_wcn6855_clk_assert(struct pwrseq_device *pwrseq)
105 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
107 if (!ctx->xo_clk_gpio)
112 gpiod_set_value_cansleep(ctx->xo_clk_gpio, 1);
113 usleep_range(100, 200);
118 static const struct pwrseq_unit_data pwrseq_qcom_wcn6855_xo_clk_assert = {
119 .name = "xo-clk-assert",
120 .enable = pwrseq_qcom_wcn6855_clk_assert,
123 static const struct pwrseq_unit_data *pwrseq_qcom_wcn6855_unit_deps[] = {
124 &pwrseq_qcom_wcn_vregs_unit_data,
125 &pwrseq_qcom_wcn_clk_unit_data,
126 &pwrseq_qcom_wcn6855_xo_clk_assert,
130 static int pwrseq_qcom_wcn_bt_enable(struct pwrseq_device *pwrseq)
132 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
134 pwrseq_qcom_wcn_ensure_gpio_delay(ctx);
135 gpiod_set_value_cansleep(ctx->bt_gpio, 1);
136 ctx->last_gpio_enable_jf = jiffies;
141 static int pwrseq_qcom_wcn_bt_disable(struct pwrseq_device *pwrseq)
143 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
145 gpiod_set_value_cansleep(ctx->bt_gpio, 0);
150 static const struct pwrseq_unit_data pwrseq_qcom_wcn_bt_unit_data = {
151 .name = "bluetooth-enable",
152 .deps = pwrseq_qcom_wcn_unit_deps,
153 .enable = pwrseq_qcom_wcn_bt_enable,
154 .disable = pwrseq_qcom_wcn_bt_disable,
157 static const struct pwrseq_unit_data pwrseq_qcom_wcn6855_bt_unit_data = {
158 .name = "wlan-enable",
159 .deps = pwrseq_qcom_wcn6855_unit_deps,
160 .enable = pwrseq_qcom_wcn_bt_enable,
161 .disable = pwrseq_qcom_wcn_bt_disable,
164 static int pwrseq_qcom_wcn_wlan_enable(struct pwrseq_device *pwrseq)
166 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
168 pwrseq_qcom_wcn_ensure_gpio_delay(ctx);
169 gpiod_set_value_cansleep(ctx->wlan_gpio, 1);
170 ctx->last_gpio_enable_jf = jiffies;
175 static int pwrseq_qcom_wcn_wlan_disable(struct pwrseq_device *pwrseq)
177 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
179 gpiod_set_value_cansleep(ctx->wlan_gpio, 0);
184 static const struct pwrseq_unit_data pwrseq_qcom_wcn_wlan_unit_data = {
185 .name = "wlan-enable",
186 .deps = pwrseq_qcom_wcn_unit_deps,
187 .enable = pwrseq_qcom_wcn_wlan_enable,
188 .disable = pwrseq_qcom_wcn_wlan_disable,
191 static const struct pwrseq_unit_data pwrseq_qcom_wcn6855_wlan_unit_data = {
192 .name = "wlan-enable",
193 .deps = pwrseq_qcom_wcn6855_unit_deps,
194 .enable = pwrseq_qcom_wcn_wlan_enable,
195 .disable = pwrseq_qcom_wcn_wlan_disable,
198 static int pwrseq_qcom_wcn_pwup_delay(struct pwrseq_device *pwrseq)
200 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
202 if (ctx->pdata->pwup_delay_ms)
203 msleep(ctx->pdata->pwup_delay_ms);
208 static int pwrseq_qcom_wcn6855_xo_clk_deassert(struct pwrseq_device *pwrseq)
210 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
212 if (ctx->xo_clk_gpio) {
213 usleep_range(2000, 5000);
214 gpiod_set_value_cansleep(ctx->xo_clk_gpio, 0);
217 return pwrseq_qcom_wcn_pwup_delay(pwrseq);
220 static const struct pwrseq_target_data pwrseq_qcom_wcn_bt_target_data = {
222 .unit = &pwrseq_qcom_wcn_bt_unit_data,
223 .post_enable = pwrseq_qcom_wcn_pwup_delay,
226 static const struct pwrseq_target_data pwrseq_qcom_wcn_wlan_target_data = {
228 .unit = &pwrseq_qcom_wcn_wlan_unit_data,
229 .post_enable = pwrseq_qcom_wcn_pwup_delay,
232 static const struct pwrseq_target_data pwrseq_qcom_wcn6855_bt_target_data = {
234 .unit = &pwrseq_qcom_wcn6855_bt_unit_data,
235 .post_enable = pwrseq_qcom_wcn6855_xo_clk_deassert,
238 static const struct pwrseq_target_data pwrseq_qcom_wcn6855_wlan_target_data = {
240 .unit = &pwrseq_qcom_wcn6855_wlan_unit_data,
241 .post_enable = pwrseq_qcom_wcn6855_xo_clk_deassert,
244 static const struct pwrseq_target_data *pwrseq_qcom_wcn_targets[] = {
245 &pwrseq_qcom_wcn_bt_target_data,
246 &pwrseq_qcom_wcn_wlan_target_data,
250 static const struct pwrseq_target_data *pwrseq_qcom_wcn6855_targets[] = {
251 &pwrseq_qcom_wcn6855_bt_target_data,
252 &pwrseq_qcom_wcn6855_wlan_target_data,
256 static const char *const pwrseq_qca6390_vregs[] = {
267 static const struct pwrseq_qcom_wcn_pdata pwrseq_qca6390_of_data = {
268 .vregs = pwrseq_qca6390_vregs,
269 .num_vregs = ARRAY_SIZE(pwrseq_qca6390_vregs),
271 .gpio_enable_delay_ms = 100,
272 .targets = pwrseq_qcom_wcn_targets,
275 static const char *const pwrseq_wcn6750_vregs[] = {
285 static const struct pwrseq_qcom_wcn_pdata pwrseq_wcn6750_of_data = {
286 .vregs = pwrseq_wcn6750_vregs,
287 .num_vregs = ARRAY_SIZE(pwrseq_wcn6750_vregs),
289 .gpio_enable_delay_ms = 5,
290 .targets = pwrseq_qcom_wcn_targets,
293 static const char *const pwrseq_wcn6855_vregs[] = {
306 static const struct pwrseq_qcom_wcn_pdata pwrseq_wcn6855_of_data = {
307 .vregs = pwrseq_wcn6855_vregs,
308 .num_vregs = ARRAY_SIZE(pwrseq_wcn6855_vregs),
310 .gpio_enable_delay_ms = 5,
311 .targets = pwrseq_qcom_wcn6855_targets,
314 static const char *const pwrseq_wcn7850_vregs[] = {
324 static const struct pwrseq_qcom_wcn_pdata pwrseq_wcn7850_of_data = {
325 .vregs = pwrseq_wcn7850_vregs,
326 .num_vregs = ARRAY_SIZE(pwrseq_wcn7850_vregs),
328 .targets = pwrseq_qcom_wcn_targets,
331 static int pwrseq_qcom_wcn_match(struct pwrseq_device *pwrseq,
334 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
335 struct device_node *dev_node = dev->of_node;
338 * The PMU supplies power to the Bluetooth and WLAN modules. both
339 * consume the PMU AON output so check the presence of the
340 * 'vddaon-supply' property and whether it leads us to the right
343 if (!of_property_present(dev_node, "vddaon-supply"))
346 struct device_node *reg_node __free(device_node) =
347 of_parse_phandle(dev_node, "vddaon-supply", 0);
352 * `reg_node` is the PMU AON regulator, its parent is the `regulators`
353 * node and finally its grandparent is the PMU device node that we're
356 if (!reg_node->parent || !reg_node->parent->parent ||
357 reg_node->parent->parent != ctx->of_node)
363 static int pwrseq_qcom_wcn_probe(struct platform_device *pdev)
365 struct device *dev = &pdev->dev;
366 struct pwrseq_qcom_wcn_ctx *ctx;
367 struct pwrseq_config config;
370 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
374 ctx->of_node = dev->of_node;
376 ctx->pdata = of_device_get_match_data(dev);
378 return dev_err_probe(dev, -ENODEV,
379 "Failed to obtain platform data\n");
381 ctx->regs = devm_kcalloc(dev, ctx->pdata->num_vregs,
382 sizeof(*ctx->regs), GFP_KERNEL);
386 for (i = 0; i < ctx->pdata->num_vregs; i++)
387 ctx->regs[i].supply = ctx->pdata->vregs[i];
389 ret = devm_regulator_bulk_get(dev, ctx->pdata->num_vregs, ctx->regs);
391 return dev_err_probe(dev, ret,
392 "Failed to get all regulators\n");
394 ctx->bt_gpio = devm_gpiod_get_optional(dev, "bt-enable", GPIOD_OUT_LOW);
395 if (IS_ERR(ctx->bt_gpio))
396 return dev_err_probe(dev, PTR_ERR(ctx->bt_gpio),
397 "Failed to get the Bluetooth enable GPIO\n");
400 * FIXME: This should actually be GPIOD_OUT_LOW, but doing so would
401 * cause the WLAN power to be toggled, resulting in PCIe link down.
402 * Since the PCIe controller driver is not handling link down currently,
403 * the device becomes unusable. So we need to keep this workaround until
404 * the link down handling is implemented in the controller driver.
406 ctx->wlan_gpio = devm_gpiod_get_optional(dev, "wlan-enable",
408 if (IS_ERR(ctx->wlan_gpio))
409 return dev_err_probe(dev, PTR_ERR(ctx->wlan_gpio),
410 "Failed to get the WLAN enable GPIO\n");
412 ctx->xo_clk_gpio = devm_gpiod_get_optional(dev, "xo-clk",
414 if (IS_ERR(ctx->xo_clk_gpio))
415 return dev_err_probe(dev, PTR_ERR(ctx->xo_clk_gpio),
416 "Failed to get the XO_CLK GPIO\n");
419 * Set direction to output but keep the current value in order to not
420 * disable the WLAN module accidentally if it's already powered on.
422 gpiod_direction_output(ctx->wlan_gpio,
423 gpiod_get_value_cansleep(ctx->wlan_gpio));
425 ctx->clk = devm_clk_get_optional(dev, NULL);
426 if (IS_ERR(ctx->clk))
427 return dev_err_probe(dev, PTR_ERR(ctx->clk),
428 "Failed to get the reference clock\n");
430 memset(&config, 0, sizeof(config));
433 config.owner = THIS_MODULE;
434 config.drvdata = ctx;
435 config.match = pwrseq_qcom_wcn_match;
436 config.targets = ctx->pdata->targets;
438 ctx->pwrseq = devm_pwrseq_device_register(dev, &config);
439 if (IS_ERR(ctx->pwrseq))
440 return dev_err_probe(dev, PTR_ERR(ctx->pwrseq),
441 "Failed to register the power sequencer\n");
446 static const struct of_device_id pwrseq_qcom_wcn_of_match[] = {
448 .compatible = "qcom,qca6390-pmu",
449 .data = &pwrseq_qca6390_of_data,
452 .compatible = "qcom,wcn6855-pmu",
453 .data = &pwrseq_wcn6855_of_data,
456 .compatible = "qcom,wcn7850-pmu",
457 .data = &pwrseq_wcn7850_of_data,
460 .compatible = "qcom,wcn6750-pmu",
461 .data = &pwrseq_wcn6750_of_data,
465 MODULE_DEVICE_TABLE(of, pwrseq_qcom_wcn_of_match);
467 static struct platform_driver pwrseq_qcom_wcn_driver = {
469 .name = "pwrseq-qcom_wcn",
470 .of_match_table = pwrseq_qcom_wcn_of_match,
472 .probe = pwrseq_qcom_wcn_probe,
474 module_platform_driver(pwrseq_qcom_wcn_driver);
477 MODULE_DESCRIPTION("Qualcomm WCN PMU power sequencing driver");
478 MODULE_LICENSE("GPL");