summaryrefslogtreecommitdiff
path: root/drivers/net/wireless/cnss2/power.c
blob: ed145d7049be92eca6c61572dbb73b7b87e548b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 and
 * only version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/delay.h>
#include <linux/of.h>
#include <linux/pinctrl/consumer.h>
#include <linux/regulator/consumer.h>
#include <linux/gpio.h>

#include "main.h"
#include "debug.h"

static struct cnss_vreg_info cnss_vreg_info[] = {
	{NULL, "vdd-wlan-core", 1300000, 1300000, 0, 0},
	{NULL, "vdd-wlan-io", 1800000, 1800000, 0, 0},
	{NULL, "vdd-wlan-xtal-aon", 0, 0, 0, 0},
	{NULL, "vdd-wlan-xtal", 1800000, 1800000, 0, 2},
	{NULL, "vdd-wlan", 0, 0, 0, 0},
	{NULL, "vdd-wlan-sp2t", 2700000, 2700000, 0, 0},
	{NULL, "wlan-ant-switch", 2700000, 2700000, 20000, 0},
	{NULL, "wlan-soc-swreg", 1200000, 1200000, 0, 0},
	{NULL, "vdd-wlan-en", 0, 0, 0, 10},
};

#define CNSS_VREG_INFO_SIZE		ARRAY_SIZE(cnss_vreg_info)
#define MAX_PROP_SIZE			32

#define BOOTSTRAP_GPIO			"qcom,enable-bootstrap-gpio"
#define BOOTSTRAP_ACTIVE		"bootstrap_active"
#define WLAN_EN_GPIO			"wlan-en-gpio"
#define WLAN_EN_ACTIVE			"wlan_en_active"
#define WLAN_EN_SLEEP			"wlan_en_sleep"

#define BOOTSTRAP_DELAY			1000
#define WLAN_ENABLE_DELAY		1000

int cnss_get_vreg(struct cnss_plat_data *plat_priv)
{
	int ret = 0;
	int i;
	struct cnss_vreg_info *vreg_info;
	struct device *dev;
	struct regulator *reg;
	const __be32 *prop;
	char prop_name[MAX_PROP_SIZE];
	int len;

	dev = &plat_priv->plat_dev->dev;

	plat_priv->vreg_info = devm_kzalloc(dev, sizeof(cnss_vreg_info),
					    GFP_KERNEL);
	if (!plat_priv->vreg_info) {
		ret = -ENOMEM;
		goto out;
	}

	memcpy(plat_priv->vreg_info, cnss_vreg_info, sizeof(cnss_vreg_info));

	for (i = 0; i < CNSS_VREG_INFO_SIZE; i++) {
		vreg_info = &plat_priv->vreg_info[i];
		reg = devm_regulator_get_optional(dev, vreg_info->name);
		if (IS_ERR(reg)) {
			ret = PTR_ERR(reg);
			if (ret == -ENODEV)
				continue;
			else if (ret == -EPROBE_DEFER)
				cnss_pr_info("EPROBE_DEFER for regulator: %s\n",
					     vreg_info->name);
			else
				cnss_pr_err("Failed to get regulator %s, err = %d\n",
					    vreg_info->name, ret);
			goto out;
		}

		vreg_info->reg = reg;

		snprintf(prop_name, MAX_PROP_SIZE, "qcom,%s-info",
			 vreg_info->name);

		prop = of_get_property(dev->of_node, prop_name, &len);
		cnss_pr_dbg("Got regulator info, name: %s, len: %d\n",
			    prop_name, len);

		if (!prop || len != (4 * sizeof(__be32))) {
			cnss_pr_dbg("Property %s %s, use default\n", prop_name,
				    prop ? "invalid format" : "doesn't exist");
		} else {
			vreg_info->min_uv = be32_to_cpup(&prop[0]);
			vreg_info->max_uv = be32_to_cpup(&prop[1]);
			vreg_info->load_ua = be32_to_cpup(&prop[2]);
			vreg_info->delay_us = be32_to_cpup(&prop[3]);
		}

		cnss_pr_dbg("Got regulator: %s, min_uv: %u, max_uv: %u, load_ua: %u, delay_us: %u\n",
			    vreg_info->name, vreg_info->min_uv,
			    vreg_info->max_uv, vreg_info->load_ua,
			    vreg_info->delay_us);
	}

	return 0;
out:
	return ret;
}

#ifndef CONFIG_MSM_GVM_QUIN
static int cnss_vreg_on(struct cnss_plat_data *plat_priv)
{
	int ret = 0;
	struct cnss_vreg_info *vreg_info;
	int i;

	if (!plat_priv) {
		cnss_pr_err("plat_priv is NULL!\n");
		return -ENODEV;
	}

	for (i = 0; i < CNSS_VREG_INFO_SIZE; i++) {
		vreg_info = &plat_priv->vreg_info[i];

		if (!vreg_info->reg)
			continue;

		cnss_pr_dbg("Regulator %s is being enabled\n", vreg_info->name);

		if (vreg_info->min_uv != 0 && vreg_info->max_uv != 0) {
			ret = regulator_set_voltage(vreg_info->reg,
						    vreg_info->min_uv,
						    vreg_info->max_uv);

			if (ret) {
				cnss_pr_err("Failed to set voltage for regulator %s, min_uv: %u, max_uv: %u, err = %d\n",
					    vreg_info->name, vreg_info->min_uv,
					    vreg_info->max_uv, ret);
				break;
			}
		}

		if (vreg_info->load_ua) {
			ret = regulator_set_load(vreg_info->reg,
						 vreg_info->load_ua);

			if (ret < 0) {
				cnss_pr_err("Failed to set load for regulator %s, load: %u, err = %d\n",
					    vreg_info->name, vreg_info->load_ua,
					    ret);
				break;
			}
		}

		if (vreg_info->delay_us)
			udelay(vreg_info->delay_us);

		ret = regulator_enable(vreg_info->reg);
		if (ret) {
			cnss_pr_err("Failed to enable regulator %s, err = %d\n",
				    vreg_info->name, ret);
			break;
		}
	}

	if (ret) {
		for (; i >= 0; i--) {
			vreg_info = &plat_priv->vreg_info[i];

			if (!vreg_info->reg)
				continue;

			regulator_disable(vreg_info->reg);
			if (vreg_info->load_ua)
				regulator_set_load(vreg_info->reg, 0);
			if (vreg_info->min_uv != 0 && vreg_info->max_uv != 0)
				regulator_set_voltage(vreg_info->reg, 0,
						      vreg_info->max_uv);
		}

		return ret;
	}

	return 0;
}

static int cnss_vreg_off(struct cnss_plat_data *plat_priv)
{
	int ret = 0;
	struct cnss_vreg_info *vreg_info;
	int i;

	if (!plat_priv) {
		cnss_pr_err("plat_priv is NULL!\n");
		return -ENODEV;
	}

	for (i = CNSS_VREG_INFO_SIZE - 1; i >= 0; i--) {
		vreg_info = &plat_priv->vreg_info[i];

		if (!vreg_info->reg)
			continue;

		cnss_pr_dbg("Regulator %s is being disabled\n",
			    vreg_info->name);

		ret = regulator_disable(vreg_info->reg);
		if (ret)
			cnss_pr_err("Failed to disable regulator %s, err = %d\n",
				    vreg_info->name, ret);

		if (vreg_info->load_ua) {
			ret = regulator_set_load(vreg_info->reg, 0);
			if (ret < 0)
				cnss_pr_err("Failed to set load for regulator %s, err = %d\n",
					    vreg_info->name, ret);
		}

		if (vreg_info->min_uv != 0 && vreg_info->max_uv != 0) {
			ret = regulator_set_voltage(vreg_info->reg, 0,
						    vreg_info->max_uv);
			if (ret)
				cnss_pr_err("Failed to set voltage for regulator %s, err = %d\n",
					    vreg_info->name, ret);
		}
	}

	return ret;
}
#endif /* CONFIG_MSM_GVM_QUIN */

int cnss_get_pinctrl(struct cnss_plat_data *plat_priv)
{
	int ret = 0;
	struct device *dev;
	struct cnss_pinctrl_info *pinctrl_info;

	dev = &plat_priv->plat_dev->dev;
	pinctrl_info = &plat_priv->pinctrl_info;

	pinctrl_info->pinctrl = devm_pinctrl_get(dev);
	if (IS_ERR_OR_NULL(pinctrl_info->pinctrl)) {
		ret = PTR_ERR(pinctrl_info->pinctrl);
		cnss_pr_err("Failed to get pinctrl, err = %d\n", ret);
		goto out;
	}

	if (of_find_property(dev->of_node, BOOTSTRAP_GPIO, NULL)) {
		pinctrl_info->bootstrap_active =
			pinctrl_lookup_state(pinctrl_info->pinctrl,
					     BOOTSTRAP_ACTIVE);
		if (IS_ERR_OR_NULL(pinctrl_info->bootstrap_active)) {
			ret = PTR_ERR(pinctrl_info->bootstrap_active);
			cnss_pr_err("Failed to get bootstrap active state, err = %d\n",
				    ret);
			goto out;
		}
	}

	if (of_find_property(dev->of_node, WLAN_EN_GPIO, NULL)) {
		pinctrl_info->wlan_en_active =
			pinctrl_lookup_state(pinctrl_info->pinctrl,
					     WLAN_EN_ACTIVE);
		if (IS_ERR_OR_NULL(pinctrl_info->wlan_en_active)) {
			ret = PTR_ERR(pinctrl_info->wlan_en_active);
			cnss_pr_err("Failed to get wlan_en active state, err = %d\n",
				    ret);
			goto out;
		}

		pinctrl_info->wlan_en_sleep =
			pinctrl_lookup_state(pinctrl_info->pinctrl,
					     WLAN_EN_SLEEP);
		if (IS_ERR_OR_NULL(pinctrl_info->wlan_en_sleep)) {
			ret = PTR_ERR(pinctrl_info->wlan_en_sleep);
			cnss_pr_err("Failed to get wlan_en sleep state, err = %d\n",
				    ret);
			goto out;
		}
	}

	return 0;
out:
	return ret;
}

#ifndef CONFIG_MSM_GVM_QUIN
static int cnss_select_pinctrl_state(struct cnss_plat_data *plat_priv,
				     bool state)
{
	int ret = 0;
	struct cnss_pinctrl_info *pinctrl_info;

	if (!plat_priv) {
		cnss_pr_err("plat_priv is NULL!\n");
		ret = -ENODEV;
		goto out;
	}

	pinctrl_info = &plat_priv->pinctrl_info;

	if (state) {
		if (!IS_ERR_OR_NULL(pinctrl_info->bootstrap_active)) {
			ret = pinctrl_select_state(pinctrl_info->pinctrl,
						   pinctrl_info->
						   bootstrap_active);
			if (ret) {
				cnss_pr_err("Failed to select bootstrap active state, err = %d\n",
					    ret);
				goto out;
			}
			udelay(BOOTSTRAP_DELAY);
		}

		if (!IS_ERR_OR_NULL(pinctrl_info->wlan_en_active)) {
			ret = pinctrl_select_state(pinctrl_info->pinctrl,
						   pinctrl_info->
						   wlan_en_active);
			if (ret) {
				cnss_pr_err("Failed to select wlan_en active state, err = %d\n",
					    ret);
				goto out;
			}
			udelay(WLAN_ENABLE_DELAY);
		}
	} else {
		if (!IS_ERR_OR_NULL(pinctrl_info->wlan_en_sleep)) {
			ret = pinctrl_select_state(pinctrl_info->pinctrl,
						   pinctrl_info->wlan_en_sleep);
			if (ret) {
				cnss_pr_err("Failed to select wlan_en sleep state, err = %d\n",
					    ret);
				goto out;
			}
		}
	}

	return 0;
out:
	return ret;
}

int cnss_power_on_device(struct cnss_plat_data *plat_priv)
{
	int ret = 0;

	ret = cnss_vreg_on(plat_priv);
	if (ret) {
		cnss_pr_err("Failed to turn on vreg, err = %d\n", ret);
		goto out;
	}

	ret = cnss_select_pinctrl_state(plat_priv, true);
	if (ret) {
		cnss_pr_err("Failed to select pinctrl state, err = %d\n", ret);
		goto vreg_off;
	}

	return 0;
vreg_off:
	cnss_vreg_off(plat_priv);
out:
	return ret;
}

void cnss_power_off_device(struct cnss_plat_data *plat_priv)
{
	cnss_select_pinctrl_state(plat_priv, false);
	cnss_vreg_off(plat_priv);
}
#endif /* CONFIG_MSM_GVM_QUIN */

void cnss_set_pin_connect_status(struct cnss_plat_data *plat_priv)
{
	unsigned long pin_status = 0;

	set_bit(CNSS_WLAN_EN, &pin_status);
	set_bit(CNSS_PCIE_TXN, &pin_status);
	set_bit(CNSS_PCIE_TXP, &pin_status);
	set_bit(CNSS_PCIE_RXN, &pin_status);
	set_bit(CNSS_PCIE_RXP, &pin_status);
	set_bit(CNSS_PCIE_REFCLKN, &pin_status);
	set_bit(CNSS_PCIE_REFCLKP, &pin_status);
	set_bit(CNSS_PCIE_RST, &pin_status);

	plat_priv->pin_result.host_pin_result = pin_status;
}

static irqreturn_t wlan_wakeup_interrupt(int irq, void *dev_id)
{
	return IRQ_HANDLED;
}

void cnss_set_wlan_chip_to_host_wakeup(unsigned int wakeup_gpio_num)
{
	int ret = 0;
	int wakeup_irq_num;

	ret = gpio_request(wakeup_gpio_num, "qcom_wlan_wakeup");
	if (ret)
		cnss_pr_err("wakeup gpio request failed\n");

	ret = gpio_direction_input(wakeup_gpio_num);
	if (ret) {
		cnss_pr_err("wake gpio set dir output failed\n");
		goto free_gpio;
	}

	wakeup_irq_num = gpio_to_irq(wakeup_gpio_num);
	if (wakeup_irq_num < 0) {
		cnss_pr_err("wake gpio_to_irq err %d\n", wakeup_irq_num);
		goto free_gpio;
	}
	ret = request_irq(wakeup_irq_num, wlan_wakeup_interrupt,
			  IRQF_TRIGGER_FALLING, "qcom_wlan_wakeup_irq", NULL);
	if (ret) {
		cnss_pr_err("request_irq err %d\n", ret);
		goto free_gpio;
	}

	ret = enable_irq_wake(wakeup_irq_num);
	if (!ret) {
		cnss_pr_err("enable irq wakeup success %d\n", ret);
	} else {
		cnss_pr_err("enable irq wakeup FAILURE %d\n", ret);
		goto irq_free;
	}
	ret = gpio_get_value(wakeup_gpio_num);
	cnss_pr_err("gpio get val ret = %d wakeup_gpio_num %d\n", ret,
		    wakeup_gpio_num);
	return;
irq_free:
	free_irq(wakeup_irq_num, NULL);
free_gpio:
	gpio_free(wakeup_gpio_num);
}