xref: /linux/drivers/power/sequencing/pwrseq-qcom-wcn.c (revision 42b16d3ac371a2fac9b6f08fd75f23f34ba3955a)
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 };
26 
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;
34 	struct clk *clk;
35 	unsigned long last_gpio_enable_jf;
36 };
37 
pwrseq_qcom_wcn_ensure_gpio_delay(struct pwrseq_qcom_wcn_ctx * ctx)38 static void pwrseq_qcom_wcn_ensure_gpio_delay(struct pwrseq_qcom_wcn_ctx *ctx)
39 {
40 	unsigned long diff_jiffies;
41 	unsigned int diff_msecs;
42 
43 	if (!ctx->pdata->gpio_enable_delay_ms)
44 		return;
45 
46 	diff_jiffies = jiffies - ctx->last_gpio_enable_jf;
47 	diff_msecs = jiffies_to_msecs(diff_jiffies);
48 
49 	if (diff_msecs < ctx->pdata->gpio_enable_delay_ms)
50 		msleep(ctx->pdata->gpio_enable_delay_ms - diff_msecs);
51 }
52 
pwrseq_qcom_wcn_vregs_enable(struct pwrseq_device * pwrseq)53 static int pwrseq_qcom_wcn_vregs_enable(struct pwrseq_device *pwrseq)
54 {
55 	struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
56 
57 	return regulator_bulk_enable(ctx->pdata->num_vregs, ctx->regs);
58 }
59 
pwrseq_qcom_wcn_vregs_disable(struct pwrseq_device * pwrseq)60 static int pwrseq_qcom_wcn_vregs_disable(struct pwrseq_device *pwrseq)
61 {
62 	struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
63 
64 	return regulator_bulk_disable(ctx->pdata->num_vregs, ctx->regs);
65 }
66 
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,
71 };
72 
pwrseq_qcom_wcn_clk_enable(struct pwrseq_device * pwrseq)73 static int pwrseq_qcom_wcn_clk_enable(struct pwrseq_device *pwrseq)
74 {
75 	struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
76 
77 	return clk_prepare_enable(ctx->clk);
78 }
79 
pwrseq_qcom_wcn_clk_disable(struct pwrseq_device * pwrseq)80 static int pwrseq_qcom_wcn_clk_disable(struct pwrseq_device *pwrseq)
81 {
82 	struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
83 
84 	clk_disable_unprepare(ctx->clk);
85 
86 	return 0;
87 }
88 
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,
93 };
94 
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,
98 	NULL
99 };
100 
pwrseq_qcom_wcn_bt_enable(struct pwrseq_device * pwrseq)101 static int pwrseq_qcom_wcn_bt_enable(struct pwrseq_device *pwrseq)
102 {
103 	struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
104 
105 	pwrseq_qcom_wcn_ensure_gpio_delay(ctx);
106 	gpiod_set_value_cansleep(ctx->bt_gpio, 1);
107 	ctx->last_gpio_enable_jf = jiffies;
108 
109 	return 0;
110 }
111 
pwrseq_qcom_wcn_bt_disable(struct pwrseq_device * pwrseq)112 static int pwrseq_qcom_wcn_bt_disable(struct pwrseq_device *pwrseq)
113 {
114 	struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
115 
116 	gpiod_set_value_cansleep(ctx->bt_gpio, 0);
117 
118 	return 0;
119 }
120 
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,
126 };
127 
pwrseq_qcom_wcn_wlan_enable(struct pwrseq_device * pwrseq)128 static int pwrseq_qcom_wcn_wlan_enable(struct pwrseq_device *pwrseq)
129 {
130 	struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
131 
132 	pwrseq_qcom_wcn_ensure_gpio_delay(ctx);
133 	gpiod_set_value_cansleep(ctx->wlan_gpio, 1);
134 	ctx->last_gpio_enable_jf = jiffies;
135 
136 	return 0;
137 }
138 
pwrseq_qcom_wcn_wlan_disable(struct pwrseq_device * pwrseq)139 static int pwrseq_qcom_wcn_wlan_disable(struct pwrseq_device *pwrseq)
140 {
141 	struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
142 
143 	gpiod_set_value_cansleep(ctx->wlan_gpio, 0);
144 
145 	return 0;
146 }
147 
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,
153 };
154 
pwrseq_qcom_wcn_pwup_delay(struct pwrseq_device * pwrseq)155 static int pwrseq_qcom_wcn_pwup_delay(struct pwrseq_device *pwrseq)
156 {
157 	struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
158 
159 	if (ctx->pdata->pwup_delay_ms)
160 		msleep(ctx->pdata->pwup_delay_ms);
161 
162 	return 0;
163 }
164 
165 static const struct pwrseq_target_data pwrseq_qcom_wcn_bt_target_data = {
166 	.name = "bluetooth",
167 	.unit = &pwrseq_qcom_wcn_bt_unit_data,
168 	.post_enable = pwrseq_qcom_wcn_pwup_delay,
169 };
170 
171 static const struct pwrseq_target_data pwrseq_qcom_wcn_wlan_target_data = {
172 	.name = "wlan",
173 	.unit = &pwrseq_qcom_wcn_wlan_unit_data,
174 	.post_enable = pwrseq_qcom_wcn_pwup_delay,
175 };
176 
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,
180 	NULL
181 };
182 
183 static const char *const pwrseq_qca6390_vregs[] = {
184 	"vddio",
185 	"vddaon",
186 	"vddpmu",
187 	"vddrfa0p95",
188 	"vddrfa1p3",
189 	"vddrfa1p9",
190 	"vddpcie1p3",
191 	"vddpcie1p9",
192 };
193 
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),
197 	.pwup_delay_ms = 60,
198 	.gpio_enable_delay_ms = 100,
199 };
200 
201 static const struct pwrseq_qcom_wcn_pdata pwrseq_wcn6855_of_data = {
202 	.vregs = pwrseq_qca6390_vregs,
203 	.num_vregs = ARRAY_SIZE(pwrseq_qca6390_vregs),
204 	.pwup_delay_ms = 50,
205 	.gpio_enable_delay_ms = 5,
206 };
207 
208 static const char *const pwrseq_wcn7850_vregs[] = {
209 	"vdd",
210 	"vddio",
211 	"vddio1p2",
212 	"vddaon",
213 	"vdddig",
214 	"vddrfa1p2",
215 	"vddrfa1p8",
216 };
217 
218 static const struct pwrseq_qcom_wcn_pdata pwrseq_wcn7850_of_data = {
219 	.vregs = pwrseq_wcn7850_vregs,
220 	.num_vregs = ARRAY_SIZE(pwrseq_wcn7850_vregs),
221 	.pwup_delay_ms = 50,
222 };
223 
pwrseq_qcom_wcn_match(struct pwrseq_device * pwrseq,struct device * dev)224 static int pwrseq_qcom_wcn_match(struct pwrseq_device *pwrseq,
225 				 struct device *dev)
226 {
227 	struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq);
228 	struct device_node *dev_node = dev->of_node;
229 
230 	/*
231 	 * The PMU supplies power to the Bluetooth and WLAN modules. both
232 	 * consume the PMU AON output so check the presence of the
233 	 * 'vddaon-supply' property and whether it leads us to the right
234 	 * device.
235 	 */
236 	if (!of_property_present(dev_node, "vddaon-supply"))
237 		return 0;
238 
239 	struct device_node *reg_node __free(device_node) =
240 			of_parse_phandle(dev_node, "vddaon-supply", 0);
241 	if (!reg_node)
242 		return 0;
243 
244 	/*
245 	 * `reg_node` is the PMU AON regulator, its parent is the `regulators`
246 	 * node and finally its grandparent is the PMU device node that we're
247 	 * looking for.
248 	 */
249 	if (!reg_node->parent || !reg_node->parent->parent ||
250 	    reg_node->parent->parent != ctx->of_node)
251 		return 0;
252 
253 	return 1;
254 }
255 
pwrseq_qcom_wcn_probe(struct platform_device * pdev)256 static int pwrseq_qcom_wcn_probe(struct platform_device *pdev)
257 {
258 	struct device *dev = &pdev->dev;
259 	struct pwrseq_qcom_wcn_ctx *ctx;
260 	struct pwrseq_config config;
261 	int i, ret;
262 
263 	ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL);
264 	if (!ctx)
265 		return -ENOMEM;
266 
267 	ctx->of_node = dev->of_node;
268 
269 	ctx->pdata = of_device_get_match_data(dev);
270 	if (!ctx->pdata)
271 		return dev_err_probe(dev, -ENODEV,
272 				     "Failed to obtain platform data\n");
273 
274 	ctx->regs = devm_kcalloc(dev, ctx->pdata->num_vregs,
275 				 sizeof(*ctx->regs), GFP_KERNEL);
276 	if (!ctx->regs)
277 		return -ENOMEM;
278 
279 	for (i = 0; i < ctx->pdata->num_vregs; i++)
280 		ctx->regs[i].supply = ctx->pdata->vregs[i];
281 
282 	ret = devm_regulator_bulk_get(dev, ctx->pdata->num_vregs, ctx->regs);
283 	if (ret < 0)
284 		return dev_err_probe(dev, ret,
285 				     "Failed to get all regulators\n");
286 
287 	ctx->bt_gpio = devm_gpiod_get_optional(dev, "bt-enable", GPIOD_OUT_LOW);
288 	if (IS_ERR(ctx->bt_gpio))
289 		return dev_err_probe(dev, PTR_ERR(ctx->bt_gpio),
290 				     "Failed to get the Bluetooth enable GPIO\n");
291 
292 	ctx->wlan_gpio = devm_gpiod_get_optional(dev, "wlan-enable",
293 						 GPIOD_ASIS);
294 	if (IS_ERR(ctx->wlan_gpio))
295 		return dev_err_probe(dev, PTR_ERR(ctx->wlan_gpio),
296 				     "Failed to get the WLAN enable GPIO\n");
297 
298 	/*
299 	 * Set direction to output but keep the current value in order to not
300 	 * disable the WLAN module accidentally if it's already powered on.
301 	 */
302 	gpiod_direction_output(ctx->wlan_gpio,
303 			       gpiod_get_value_cansleep(ctx->wlan_gpio));
304 
305 	ctx->clk = devm_clk_get_optional(dev, NULL);
306 	if (IS_ERR(ctx->clk))
307 		return dev_err_probe(dev, PTR_ERR(ctx->clk),
308 				     "Failed to get the reference clock\n");
309 
310 	memset(&config, 0, sizeof(config));
311 
312 	config.parent = dev;
313 	config.owner = THIS_MODULE;
314 	config.drvdata = ctx;
315 	config.match = pwrseq_qcom_wcn_match;
316 	config.targets = pwrseq_qcom_wcn_targets;
317 
318 	ctx->pwrseq = devm_pwrseq_device_register(dev, &config);
319 	if (IS_ERR(ctx->pwrseq))
320 		return dev_err_probe(dev, PTR_ERR(ctx->pwrseq),
321 				     "Failed to register the power sequencer\n");
322 
323 	return 0;
324 }
325 
326 static const struct of_device_id pwrseq_qcom_wcn_of_match[] = {
327 	{
328 		.compatible = "qcom,qca6390-pmu",
329 		.data = &pwrseq_qca6390_of_data,
330 	},
331 	{
332 		.compatible = "qcom,wcn6855-pmu",
333 		.data = &pwrseq_wcn6855_of_data,
334 	},
335 	{
336 		.compatible = "qcom,wcn7850-pmu",
337 		.data = &pwrseq_wcn7850_of_data,
338 	},
339 	{ }
340 };
341 MODULE_DEVICE_TABLE(of, pwrseq_qcom_wcn_of_match);
342 
343 static struct platform_driver pwrseq_qcom_wcn_driver = {
344 	.driver = {
345 		.name = "pwrseq-qcom_wcn",
346 		.of_match_table = pwrseq_qcom_wcn_of_match,
347 	},
348 	.probe = pwrseq_qcom_wcn_probe,
349 };
350 module_platform_driver(pwrseq_qcom_wcn_driver);
351 
352 MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>");
353 MODULE_DESCRIPTION("Qualcomm WCN PMU power sequencing driver");
354 MODULE_LICENSE("GPL");
355