1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2024 Linaro Ltd.
4 */
5
6 #include <linux/clk.h>
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>
13 #include <linux/of.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>
19
20 struct pwrseq_qcom_wcn_pdata {
21 const char *const *vregs;
22 size_t num_vregs;
23 unsigned int pwup_delay_ms;
24 unsigned int gpio_enable_delay_ms;
25 const struct pwrseq_target_data **targets;
26 };
27
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;
36 struct clk *clk;
37 unsigned long last_gpio_enable_jf;
38 };
39
pwrseq_qcom_wcn_ensure_gpio_delay(struct pwrseq_qcom_wcn_ctx * ctx)40 static void pwrseq_qcom_wcn_ensure_gpio_delay(struct pwrseq_qcom_wcn_ctx *ctx)
41 {
42 unsigned long diff_jiffies;
43 unsigned int diff_msecs;
44
45 if (!ctx->pdata->gpio_enable_delay_ms)
46 return;
47
48 diff_jiffies = jiffies - ctx->last_gpio_enable_jf;
49 diff_msecs = jiffies_to_msecs(diff_jiffies);
50
51 if (diff_msecs < ctx->pdata->gpio_enable_delay_ms)
52 msleep(ctx->pdata->gpio_enable_delay_ms - diff_msecs);
53 }
54
pwrseq_qcom_wcn_vregs_enable(struct pwrseq_device * pwrseq)55 static int pwrseq_qcom_wcn_vregs_enable(struct pwrseq_device *pwrseq)
56 {
57 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
58
59 return regulator_bulk_enable(ctx->pdata->num_vregs, ctx->regs);
60 }
61
pwrseq_qcom_wcn_vregs_disable(struct pwrseq_device * pwrseq)62 static int pwrseq_qcom_wcn_vregs_disable(struct pwrseq_device *pwrseq)
63 {
64 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
65
66 return regulator_bulk_disable(ctx->pdata->num_vregs, ctx->regs);
67 }
68
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,
73 };
74
pwrseq_qcom_wcn_clk_enable(struct pwrseq_device * pwrseq)75 static int pwrseq_qcom_wcn_clk_enable(struct pwrseq_device *pwrseq)
76 {
77 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
78
79 return clk_prepare_enable(ctx->clk);
80 }
81
pwrseq_qcom_wcn_clk_disable(struct pwrseq_device * pwrseq)82 static int pwrseq_qcom_wcn_clk_disable(struct pwrseq_device *pwrseq)
83 {
84 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
85
86 clk_disable_unprepare(ctx->clk);
87
88 return 0;
89 }
90
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,
95 };
96
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,
100 NULL
101 };
102
pwrseq_qcom_wcn6855_clk_assert(struct pwrseq_device * pwrseq)103 static int pwrseq_qcom_wcn6855_clk_assert(struct pwrseq_device *pwrseq)
104 {
105 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
106
107 if (!ctx->xo_clk_gpio)
108 return 0;
109
110 msleep(1);
111
112 gpiod_set_value_cansleep(ctx->xo_clk_gpio, 1);
113 usleep_range(100, 200);
114
115 return 0;
116 }
117
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,
121 };
122
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,
127 NULL
128 };
129
pwrseq_qcom_wcn_bt_enable(struct pwrseq_device * pwrseq)130 static int pwrseq_qcom_wcn_bt_enable(struct pwrseq_device *pwrseq)
131 {
132 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
133
134 pwrseq_qcom_wcn_ensure_gpio_delay(ctx);
135 gpiod_set_value_cansleep(ctx->bt_gpio, 1);
136 ctx->last_gpio_enable_jf = jiffies;
137
138 return 0;
139 }
140
pwrseq_qcom_wcn_bt_disable(struct pwrseq_device * pwrseq)141 static int pwrseq_qcom_wcn_bt_disable(struct pwrseq_device *pwrseq)
142 {
143 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
144
145 gpiod_set_value_cansleep(ctx->bt_gpio, 0);
146
147 return 0;
148 }
149
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,
155 };
156
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,
162 };
163
pwrseq_qcom_wcn_wlan_enable(struct pwrseq_device * pwrseq)164 static int pwrseq_qcom_wcn_wlan_enable(struct pwrseq_device *pwrseq)
165 {
166 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
167
168 pwrseq_qcom_wcn_ensure_gpio_delay(ctx);
169 gpiod_set_value_cansleep(ctx->wlan_gpio, 1);
170 ctx->last_gpio_enable_jf = jiffies;
171
172 return 0;
173 }
174
pwrseq_qcom_wcn_wlan_disable(struct pwrseq_device * pwrseq)175 static int pwrseq_qcom_wcn_wlan_disable(struct pwrseq_device *pwrseq)
176 {
177 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
178
179 gpiod_set_value_cansleep(ctx->wlan_gpio, 0);
180
181 return 0;
182 }
183
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,
189 };
190
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,
196 };
197
pwrseq_qcom_wcn_pwup_delay(struct pwrseq_device * pwrseq)198 static int pwrseq_qcom_wcn_pwup_delay(struct pwrseq_device *pwrseq)
199 {
200 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
201
202 if (ctx->pdata->pwup_delay_ms)
203 msleep(ctx->pdata->pwup_delay_ms);
204
205 return 0;
206 }
207
pwrseq_qcom_wcn6855_xo_clk_deassert(struct pwrseq_device * pwrseq)208 static int pwrseq_qcom_wcn6855_xo_clk_deassert(struct pwrseq_device *pwrseq)
209 {
210 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
211
212 if (ctx->xo_clk_gpio) {
213 usleep_range(2000, 5000);
214 gpiod_set_value_cansleep(ctx->xo_clk_gpio, 0);
215 }
216
217 return pwrseq_qcom_wcn_pwup_delay(pwrseq);
218 }
219
220 static const struct pwrseq_target_data pwrseq_qcom_wcn_bt_target_data = {
221 .name = "bluetooth",
222 .unit = &pwrseq_qcom_wcn_bt_unit_data,
223 .post_enable = pwrseq_qcom_wcn_pwup_delay,
224 };
225
226 static const struct pwrseq_target_data pwrseq_qcom_wcn_wlan_target_data = {
227 .name = "wlan",
228 .unit = &pwrseq_qcom_wcn_wlan_unit_data,
229 .post_enable = pwrseq_qcom_wcn_pwup_delay,
230 };
231
232 static const struct pwrseq_target_data pwrseq_qcom_wcn6855_bt_target_data = {
233 .name = "bluetooth",
234 .unit = &pwrseq_qcom_wcn6855_bt_unit_data,
235 .post_enable = pwrseq_qcom_wcn6855_xo_clk_deassert,
236 };
237
238 static const struct pwrseq_target_data pwrseq_qcom_wcn6855_wlan_target_data = {
239 .name = "wlan",
240 .unit = &pwrseq_qcom_wcn6855_wlan_unit_data,
241 .post_enable = pwrseq_qcom_wcn6855_xo_clk_deassert,
242 };
243
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,
247 NULL
248 };
249
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,
253 NULL
254 };
255
256 static const char *const pwrseq_qca6390_vregs[] = {
257 "vddio",
258 "vddaon",
259 "vddpmu",
260 "vddrfa0p95",
261 "vddrfa1p3",
262 "vddrfa1p9",
263 "vddpcie1p3",
264 "vddpcie1p9",
265 };
266
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),
270 .pwup_delay_ms = 60,
271 .gpio_enable_delay_ms = 100,
272 .targets = pwrseq_qcom_wcn_targets,
273 };
274
275 static const char *const pwrseq_wcn6855_vregs[] = {
276 "vddio",
277 "vddaon",
278 "vddpmu",
279 "vddpmumx",
280 "vddpmucx",
281 "vddrfa0p95",
282 "vddrfa1p3",
283 "vddrfa1p9",
284 "vddpcie1p3",
285 "vddpcie1p9",
286 };
287
288 static const struct pwrseq_qcom_wcn_pdata pwrseq_wcn6855_of_data = {
289 .vregs = pwrseq_wcn6855_vregs,
290 .num_vregs = ARRAY_SIZE(pwrseq_wcn6855_vregs),
291 .pwup_delay_ms = 50,
292 .gpio_enable_delay_ms = 5,
293 .targets = pwrseq_qcom_wcn6855_targets,
294 };
295
296 static const char *const pwrseq_wcn7850_vregs[] = {
297 "vdd",
298 "vddio",
299 "vddio1p2",
300 "vddaon",
301 "vdddig",
302 "vddrfa1p2",
303 "vddrfa1p8",
304 };
305
306 static const struct pwrseq_qcom_wcn_pdata pwrseq_wcn7850_of_data = {
307 .vregs = pwrseq_wcn7850_vregs,
308 .num_vregs = ARRAY_SIZE(pwrseq_wcn7850_vregs),
309 .pwup_delay_ms = 50,
310 .targets = pwrseq_qcom_wcn_targets,
311 };
312
pwrseq_qcom_wcn_match(struct pwrseq_device * pwrseq,struct device * dev)313 static int pwrseq_qcom_wcn_match(struct pwrseq_device *pwrseq,
314 struct device *dev)
315 {
316 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
317 struct device_node *dev_node = dev->of_node;
318
319 /*
320 * The PMU supplies power to the Bluetooth and WLAN modules. both
321 * consume the PMU AON output so check the presence of the
322 * 'vddaon-supply' property and whether it leads us to the right
323 * device.
324 */
325 if (!of_property_present(dev_node, "vddaon-supply"))
326 return 0;
327
328 struct device_node *reg_node __free(device_node) =
329 of_parse_phandle(dev_node, "vddaon-supply", 0);
330 if (!reg_node)
331 return 0;
332
333 /*
334 * `reg_node` is the PMU AON regulator, its parent is the `regulators`
335 * node and finally its grandparent is the PMU device node that we're
336 * looking for.
337 */
338 if (!reg_node->parent || !reg_node->parent->parent ||
339 reg_node->parent->parent != ctx->of_node)
340 return 0;
341
342 return 1;
343 }
344
pwrseq_qcom_wcn_probe(struct platform_device * pdev)345 static int pwrseq_qcom_wcn_probe(struct platform_device *pdev)
346 {
347 struct device *dev = &pdev->dev;
348 struct pwrseq_qcom_wcn_ctx *ctx;
349 struct pwrseq_config config;
350 int i, ret;
351
352 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
353 if (!ctx)
354 return -ENOMEM;
355
356 ctx->of_node = dev->of_node;
357
358 ctx->pdata = of_device_get_match_data(dev);
359 if (!ctx->pdata)
360 return dev_err_probe(dev, -ENODEV,
361 "Failed to obtain platform data\n");
362
363 ctx->regs = devm_kcalloc(dev, ctx->pdata->num_vregs,
364 sizeof(*ctx->regs), GFP_KERNEL);
365 if (!ctx->regs)
366 return -ENOMEM;
367
368 for (i = 0; i < ctx->pdata->num_vregs; i++)
369 ctx->regs[i].supply = ctx->pdata->vregs[i];
370
371 ret = devm_regulator_bulk_get(dev, ctx->pdata->num_vregs, ctx->regs);
372 if (ret < 0)
373 return dev_err_probe(dev, ret,
374 "Failed to get all regulators\n");
375
376 ctx->bt_gpio = devm_gpiod_get_optional(dev, "bt-enable", GPIOD_OUT_LOW);
377 if (IS_ERR(ctx->bt_gpio))
378 return dev_err_probe(dev, PTR_ERR(ctx->bt_gpio),
379 "Failed to get the Bluetooth enable GPIO\n");
380
381 ctx->wlan_gpio = devm_gpiod_get_optional(dev, "wlan-enable",
382 GPIOD_ASIS);
383 if (IS_ERR(ctx->wlan_gpio))
384 return dev_err_probe(dev, PTR_ERR(ctx->wlan_gpio),
385 "Failed to get the WLAN enable GPIO\n");
386
387 ctx->xo_clk_gpio = devm_gpiod_get_optional(dev, "xo-clk",
388 GPIOD_OUT_LOW);
389 if (IS_ERR(ctx->xo_clk_gpio))
390 return dev_err_probe(dev, PTR_ERR(ctx->xo_clk_gpio),
391 "Failed to get the XO_CLK GPIO\n");
392
393 /*
394 * Set direction to output but keep the current value in order to not
395 * disable the WLAN module accidentally if it's already powered on.
396 */
397 gpiod_direction_output(ctx->wlan_gpio,
398 gpiod_get_value_cansleep(ctx->wlan_gpio));
399
400 ctx->clk = devm_clk_get_optional(dev, NULL);
401 if (IS_ERR(ctx->clk))
402 return dev_err_probe(dev, PTR_ERR(ctx->clk),
403 "Failed to get the reference clock\n");
404
405 memset(&config, 0, sizeof(config));
406
407 config.parent = dev;
408 config.owner = THIS_MODULE;
409 config.drvdata = ctx;
410 config.match = pwrseq_qcom_wcn_match;
411 config.targets = ctx->pdata->targets;
412
413 ctx->pwrseq = devm_pwrseq_device_register(dev, &config);
414 if (IS_ERR(ctx->pwrseq))
415 return dev_err_probe(dev, PTR_ERR(ctx->pwrseq),
416 "Failed to register the power sequencer\n");
417
418 return 0;
419 }
420
421 static const struct of_device_id pwrseq_qcom_wcn_of_match[] = {
422 {
423 .compatible = "qcom,qca6390-pmu",
424 .data = &pwrseq_qca6390_of_data,
425 },
426 {
427 .compatible = "qcom,wcn6855-pmu",
428 .data = &pwrseq_wcn6855_of_data,
429 },
430 {
431 .compatible = "qcom,wcn7850-pmu",
432 .data = &pwrseq_wcn7850_of_data,
433 },
434 { }
435 };
436 MODULE_DEVICE_TABLE(of, pwrseq_qcom_wcn_of_match);
437
438 static struct platform_driver pwrseq_qcom_wcn_driver = {
439 .driver = {
440 .name = "pwrseq-qcom_wcn",
441 .of_match_table = pwrseq_qcom_wcn_of_match,
442 },
443 .probe = pwrseq_qcom_wcn_probe,
444 };
445 module_platform_driver(pwrseq_qcom_wcn_driver);
446
447 MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
448 MODULE_DESCRIPTION("Qualcomm WCN PMU power sequencing driver");
449 MODULE_LICENSE("GPL");
450