1*5113495bSYour Name // SPDX-License-Identifier: GPL-2.0-only
2*5113495bSYour Name /*
3*5113495bSYour Name * Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
4*5113495bSYour Name * Copyright (c) 2023-2024 Qualcomm Innovation Center, Inc. All rights reserved.
5*5113495bSYour Name */
6*5113495bSYour Name #include <linux/clk.h>
7*5113495bSYour Name #include <linux/delay.h>
8*5113495bSYour Name #include <linux/of.h>
9*5113495bSYour Name #include <linux/pinctrl/consumer.h>
10*5113495bSYour Name #include <linux/regulator/consumer.h>
11*5113495bSYour Name #include <soc/qcom/cmd-db.h>
12*5113495bSYour Name #include "main.h"
13*5113495bSYour Name #include "qmi.h"
14*5113495bSYour Name #include "debug.h"
15*5113495bSYour Name #include "power.h"
16*5113495bSYour Name #if IS_ENABLED(CONFIG_MSM_QMP)
17*5113495bSYour Name #include <linux/soc/qcom/qcom_aoss.h>
18*5113495bSYour Name #endif
19*5113495bSYour Name
20*5113495bSYour Name static struct icnss_vreg_cfg icnss_wcn6750_vreg_list[] = {
21*5113495bSYour Name {"vdd-cx-mx", 824000, 952000, 0, 0, 0, false, true},
22*5113495bSYour Name {"vdd-1.8-xo", 1872000, 1872000, 0, 0, 0, false, true},
23*5113495bSYour Name {"vdd-1.3-rfa", 1256000, 1352000, 0, 0, 0, false, true},
24*5113495bSYour Name {"vdd-ipa-2p2", 2200000, 2200000, 0, 0, 0, false, true},
25*5113495bSYour Name };
26*5113495bSYour Name
27*5113495bSYour Name static struct icnss_vreg_cfg icnss_adrestea_vreg_list[] = {
28*5113495bSYour Name {"vdd-cx-mx", 752000, 752000, 0, 0, 0, false, true},
29*5113495bSYour Name {"vdd-1.8-xo", 1800000, 1800000, 0, 0, 0, false, true},
30*5113495bSYour Name {"vdd-1.3-rfa", 1304000, 1304000, 0, 0, 0, false, true},
31*5113495bSYour Name {"vdd-3.3-ch1", 3312000, 3312000, 0, 0, 0, false, false},
32*5113495bSYour Name {"vdd-3.3-ch0", 3312000, 3312000, 0, 0, 0, false, true},
33*5113495bSYour Name };
34*5113495bSYour Name
35*5113495bSYour Name static struct icnss_battery_level icnss_battery_level[] = {
36*5113495bSYour Name {70, 3300000},
37*5113495bSYour Name {60, 3200000},
38*5113495bSYour Name {50, 3100000},
39*5113495bSYour Name {25, 3000000},
40*5113495bSYour Name {0, 2850000},
41*5113495bSYour Name };
42*5113495bSYour Name
43*5113495bSYour Name static struct icnss_vreg_cfg icnss_wcn6450_vreg_list[] = {
44*5113495bSYour Name {"vdd-cx-mx", 824000, 952000, 0, 0, 0, false, true},
45*5113495bSYour Name {"vdd-1.8-xo", 1872000, 1872000, 0, 0, 0, false, true},
46*5113495bSYour Name {"vdd-1.3-rfa", 1256000, 1352000, 0, 0, 0, false, true},
47*5113495bSYour Name {"vdd-aon", 1256000, 1352000, 0, 0, 0, false, true},
48*5113495bSYour Name };
49*5113495bSYour Name
50*5113495bSYour Name static struct icnss_clk_cfg icnss_clk_list[] = {
51*5113495bSYour Name {"rf_clk", 0, 0},
52*5113495bSYour Name };
53*5113495bSYour Name
54*5113495bSYour Name static struct icnss_clk_cfg icnss_adrestea_clk_list[] = {
55*5113495bSYour Name {"cxo_ref_clk_pin", 0, 0},
56*5113495bSYour Name };
57*5113495bSYour Name
58*5113495bSYour Name #define ICNSS_VREG_LIST_SIZE ARRAY_SIZE(icnss_wcn6750_vreg_list)
59*5113495bSYour Name #define ICNSS_VREG_ADRESTEA_LIST_SIZE ARRAY_SIZE(icnss_adrestea_vreg_list)
60*5113495bSYour Name #define ICNSS_VREG_EVROS_LIST_SIZE ARRAY_SIZE(icnss_wcn6450_vreg_list)
61*5113495bSYour Name #define ICNSS_CLK_LIST_SIZE ARRAY_SIZE(icnss_clk_list)
62*5113495bSYour Name #define ICNSS_CLK_ADRESTEA_LIST_SIZE ARRAY_SIZE(icnss_adrestea_clk_list)
63*5113495bSYour Name
64*5113495bSYour Name #define ICNSS_CHAIN1_REGULATOR "vdd-3.3-ch1"
65*5113495bSYour Name #define MAX_PROP_SIZE 32
66*5113495bSYour Name
67*5113495bSYour Name #define BT_CXMX_VOLTAGE_MV 950
68*5113495bSYour Name #define ICNSS_MBOX_MSG_MAX_LEN 64
69*5113495bSYour Name #define ICNSS_MBOX_TIMEOUT_MS 1000
70*5113495bSYour Name
71*5113495bSYour Name #define ICNSS_BATTERY_LEVEL_COUNT ARRAY_SIZE(icnss_battery_level)
72*5113495bSYour Name #define ICNSS_MAX_BATTERY_LEVEL 100
73*5113495bSYour Name
74*5113495bSYour Name /**
75*5113495bSYour Name * enum icnss_vreg_param: Voltage regulator TCS param
76*5113495bSYour Name * @ICNSS_VREG_VOLTAGE: Provides voltage level to be configured in TCS
77*5113495bSYour Name * @ICNSS_VREG_MODE: Regulator mode
78*5113495bSYour Name * @ICNSS_VREG_ENABLE: Set Voltage regulator enable config in TCS
79*5113495bSYour Name */
80*5113495bSYour Name enum icnss_vreg_param {
81*5113495bSYour Name ICNSS_VREG_VOLTAGE,
82*5113495bSYour Name ICNSS_VREG_MODE,
83*5113495bSYour Name ICNSS_VREG_ENABLE,
84*5113495bSYour Name };
85*5113495bSYour Name
86*5113495bSYour Name /**
87*5113495bSYour Name * enum icnss_tcs_seq: TCS sequence ID for trigger
88*5113495bSYour Name * ICNSS_TCS_UP_SEQ: TCS Sequence based on up trigger / Wake TCS
89*5113495bSYour Name * ICNSS_TCS_DOWN_SEQ: TCS Sequence based on down trigger / Sleep TCS
90*5113495bSYour Name * ICNSS_TCS_ALL_SEQ: Update for both up and down triggers
91*5113495bSYour Name */
92*5113495bSYour Name enum icnss_tcs_seq {
93*5113495bSYour Name ICNSS_TCS_UP_SEQ,
94*5113495bSYour Name ICNSS_TCS_DOWN_SEQ,
95*5113495bSYour Name ICNSS_TCS_ALL_SEQ,
96*5113495bSYour Name };
97*5113495bSYour Name
icnss_get_vreg_single(struct icnss_priv * priv,struct icnss_vreg_info * vreg)98*5113495bSYour Name static int icnss_get_vreg_single(struct icnss_priv *priv,
99*5113495bSYour Name struct icnss_vreg_info *vreg)
100*5113495bSYour Name {
101*5113495bSYour Name int ret = 0;
102*5113495bSYour Name struct device *dev = NULL;
103*5113495bSYour Name struct regulator *reg = NULL;
104*5113495bSYour Name const __be32 *prop = NULL;
105*5113495bSYour Name char prop_name[MAX_PROP_SIZE] = {0};
106*5113495bSYour Name int len = 0;
107*5113495bSYour Name int i;
108*5113495bSYour Name
109*5113495bSYour Name dev = &priv->pdev->dev;
110*5113495bSYour Name
111*5113495bSYour Name reg = devm_regulator_get_optional(dev, vreg->cfg.name);
112*5113495bSYour Name if (IS_ERR(reg)) {
113*5113495bSYour Name ret = PTR_ERR(reg);
114*5113495bSYour Name if (ret == -ENODEV) {
115*5113495bSYour Name return ret;
116*5113495bSYour Name } else if (ret == -EPROBE_DEFER) {
117*5113495bSYour Name icnss_pr_info("EPROBE_DEFER for regulator: %s\n",
118*5113495bSYour Name vreg->cfg.name);
119*5113495bSYour Name goto out;
120*5113495bSYour Name } else if (priv->device_id == ADRASTEA_DEVICE_ID) {
121*5113495bSYour Name if (vreg->cfg.required) {
122*5113495bSYour Name icnss_pr_err("Regulator %s doesn't exist: %d\n",
123*5113495bSYour Name vreg->cfg.name, ret);
124*5113495bSYour Name goto out;
125*5113495bSYour Name } else {
126*5113495bSYour Name icnss_pr_dbg("Optional regulator %s doesn't exist: %d\n",
127*5113495bSYour Name vreg->cfg.name, ret);
128*5113495bSYour Name goto done;
129*5113495bSYour Name }
130*5113495bSYour Name } else {
131*5113495bSYour Name icnss_pr_err("Failed to get regulator %s, err = %d\n",
132*5113495bSYour Name vreg->cfg.name, ret);
133*5113495bSYour Name goto out;
134*5113495bSYour Name }
135*5113495bSYour Name }
136*5113495bSYour Name
137*5113495bSYour Name vreg->reg = reg;
138*5113495bSYour Name
139*5113495bSYour Name snprintf(prop_name, MAX_PROP_SIZE, "qcom,%s-config",
140*5113495bSYour Name vreg->cfg.name);
141*5113495bSYour Name
142*5113495bSYour Name prop = of_get_property(dev->of_node, prop_name, &len);
143*5113495bSYour Name
144*5113495bSYour Name icnss_pr_dbg("Got regulator config, prop: %s, len: %d\n",
145*5113495bSYour Name prop_name, len);
146*5113495bSYour Name
147*5113495bSYour Name if (!prop || len < (2 * sizeof(__be32))) {
148*5113495bSYour Name icnss_pr_dbg("Property %s %s, use default\n", prop_name,
149*5113495bSYour Name prop ? "invalid format" : "doesn't exist");
150*5113495bSYour Name goto done;
151*5113495bSYour Name }
152*5113495bSYour Name
153*5113495bSYour Name for (i = 0; (i * sizeof(__be32)) < len; i++) {
154*5113495bSYour Name switch (i) {
155*5113495bSYour Name case 0:
156*5113495bSYour Name vreg->cfg.min_uv = be32_to_cpup(&prop[0]);
157*5113495bSYour Name break;
158*5113495bSYour Name case 1:
159*5113495bSYour Name vreg->cfg.max_uv = be32_to_cpup(&prop[1]);
160*5113495bSYour Name break;
161*5113495bSYour Name case 2:
162*5113495bSYour Name vreg->cfg.load_ua = be32_to_cpup(&prop[2]);
163*5113495bSYour Name break;
164*5113495bSYour Name case 3:
165*5113495bSYour Name vreg->cfg.delay_us = be32_to_cpup(&prop[3]);
166*5113495bSYour Name break;
167*5113495bSYour Name case 4:
168*5113495bSYour Name if (priv->device_id == WCN6750_DEVICE_ID)
169*5113495bSYour Name vreg->cfg.need_unvote = be32_to_cpup(&prop[4]);
170*5113495bSYour Name else
171*5113495bSYour Name vreg->cfg.need_unvote = 0;
172*5113495bSYour Name break;
173*5113495bSYour Name default:
174*5113495bSYour Name icnss_pr_dbg("Property %s, ignoring value at %d\n",
175*5113495bSYour Name prop_name, i);
176*5113495bSYour Name break;
177*5113495bSYour Name }
178*5113495bSYour Name }
179*5113495bSYour Name
180*5113495bSYour Name done:
181*5113495bSYour Name icnss_pr_dbg("Got regulator: %s, min_uv: %u, max_uv: %u, load_ua: %u, delay_us: %u, need_unvote: %u\n",
182*5113495bSYour Name vreg->cfg.name, vreg->cfg.min_uv,
183*5113495bSYour Name vreg->cfg.max_uv, vreg->cfg.load_ua,
184*5113495bSYour Name vreg->cfg.delay_us, vreg->cfg.need_unvote);
185*5113495bSYour Name
186*5113495bSYour Name return 0;
187*5113495bSYour Name
188*5113495bSYour Name out:
189*5113495bSYour Name return ret;
190*5113495bSYour Name }
191*5113495bSYour Name
icnss_vreg_on_single(struct icnss_vreg_info * vreg)192*5113495bSYour Name static int icnss_vreg_on_single(struct icnss_vreg_info *vreg)
193*5113495bSYour Name {
194*5113495bSYour Name int ret = 0;
195*5113495bSYour Name
196*5113495bSYour Name if (vreg->enabled) {
197*5113495bSYour Name icnss_pr_dbg("Regulator %s is already enabled\n",
198*5113495bSYour Name vreg->cfg.name);
199*5113495bSYour Name return 0;
200*5113495bSYour Name }
201*5113495bSYour Name
202*5113495bSYour Name icnss_pr_dbg("Regulator %s is being enabled\n", vreg->cfg.name);
203*5113495bSYour Name
204*5113495bSYour Name if (vreg->cfg.min_uv != 0 && vreg->cfg.max_uv != 0) {
205*5113495bSYour Name ret = regulator_set_voltage(vreg->reg,
206*5113495bSYour Name vreg->cfg.min_uv,
207*5113495bSYour Name vreg->cfg.max_uv);
208*5113495bSYour Name
209*5113495bSYour Name if (ret) {
210*5113495bSYour Name icnss_pr_err("Failed to set voltage for regulator %s, min_uv: %u, max_uv: %u, err = %d\n",
211*5113495bSYour Name vreg->cfg.name, vreg->cfg.min_uv,
212*5113495bSYour Name vreg->cfg.max_uv, ret);
213*5113495bSYour Name goto out;
214*5113495bSYour Name }
215*5113495bSYour Name }
216*5113495bSYour Name
217*5113495bSYour Name if (vreg->cfg.load_ua) {
218*5113495bSYour Name ret = regulator_set_load(vreg->reg,
219*5113495bSYour Name vreg->cfg.load_ua);
220*5113495bSYour Name
221*5113495bSYour Name if (ret < 0) {
222*5113495bSYour Name icnss_pr_err("Failed to set load for regulator %s, load: %u, err = %d\n",
223*5113495bSYour Name vreg->cfg.name, vreg->cfg.load_ua,
224*5113495bSYour Name ret);
225*5113495bSYour Name goto out;
226*5113495bSYour Name }
227*5113495bSYour Name }
228*5113495bSYour Name
229*5113495bSYour Name if (vreg->cfg.delay_us)
230*5113495bSYour Name udelay(vreg->cfg.delay_us);
231*5113495bSYour Name
232*5113495bSYour Name ret = regulator_enable(vreg->reg);
233*5113495bSYour Name if (ret) {
234*5113495bSYour Name icnss_pr_err("Failed to enable regulator %s, err = %d\n",
235*5113495bSYour Name vreg->cfg.name, ret);
236*5113495bSYour Name goto out;
237*5113495bSYour Name }
238*5113495bSYour Name
239*5113495bSYour Name vreg->enabled = true;
240*5113495bSYour Name
241*5113495bSYour Name out:
242*5113495bSYour Name return ret;
243*5113495bSYour Name }
244*5113495bSYour Name
icnss_vreg_unvote_single(struct icnss_vreg_info * vreg)245*5113495bSYour Name static int icnss_vreg_unvote_single(struct icnss_vreg_info *vreg)
246*5113495bSYour Name {
247*5113495bSYour Name int ret = 0;
248*5113495bSYour Name
249*5113495bSYour Name if (!vreg->enabled) {
250*5113495bSYour Name icnss_pr_dbg("Regulator %s is already disabled\n",
251*5113495bSYour Name vreg->cfg.name);
252*5113495bSYour Name return 0;
253*5113495bSYour Name }
254*5113495bSYour Name
255*5113495bSYour Name icnss_pr_dbg("Removing vote for Regulator %s\n", vreg->cfg.name);
256*5113495bSYour Name
257*5113495bSYour Name if (vreg->cfg.load_ua) {
258*5113495bSYour Name ret = regulator_set_load(vreg->reg, 0);
259*5113495bSYour Name if (ret < 0)
260*5113495bSYour Name icnss_pr_err("Failed to set load for regulator %s, err = %d\n",
261*5113495bSYour Name vreg->cfg.name, ret);
262*5113495bSYour Name }
263*5113495bSYour Name
264*5113495bSYour Name if (vreg->cfg.min_uv != 0 && vreg->cfg.max_uv != 0) {
265*5113495bSYour Name ret = regulator_set_voltage(vreg->reg, 0,
266*5113495bSYour Name vreg->cfg.max_uv);
267*5113495bSYour Name if (ret)
268*5113495bSYour Name icnss_pr_err("Failed to set voltage for regulator %s, err = %d\n",
269*5113495bSYour Name vreg->cfg.name, ret);
270*5113495bSYour Name }
271*5113495bSYour Name
272*5113495bSYour Name return ret;
273*5113495bSYour Name }
274*5113495bSYour Name
icnss_vreg_off_single(struct icnss_vreg_info * vreg)275*5113495bSYour Name static int icnss_vreg_off_single(struct icnss_vreg_info *vreg)
276*5113495bSYour Name {
277*5113495bSYour Name int ret = 0;
278*5113495bSYour Name
279*5113495bSYour Name if (!vreg->enabled) {
280*5113495bSYour Name icnss_pr_dbg("Regulator %s is already disabled\n",
281*5113495bSYour Name vreg->cfg.name);
282*5113495bSYour Name return 0;
283*5113495bSYour Name }
284*5113495bSYour Name
285*5113495bSYour Name icnss_pr_dbg("Regulator %s is being disabled\n",
286*5113495bSYour Name vreg->cfg.name);
287*5113495bSYour Name
288*5113495bSYour Name ret = regulator_disable(vreg->reg);
289*5113495bSYour Name if (ret)
290*5113495bSYour Name icnss_pr_err("Failed to disable regulator %s, err = %d\n",
291*5113495bSYour Name vreg->cfg.name, ret);
292*5113495bSYour Name
293*5113495bSYour Name if (vreg->cfg.load_ua) {
294*5113495bSYour Name ret = regulator_set_load(vreg->reg, 0);
295*5113495bSYour Name if (ret < 0)
296*5113495bSYour Name icnss_pr_err("Failed to set load for regulator %s, err = %d\n",
297*5113495bSYour Name vreg->cfg.name, ret);
298*5113495bSYour Name }
299*5113495bSYour Name
300*5113495bSYour Name if (vreg->cfg.min_uv != 0 && vreg->cfg.max_uv != 0) {
301*5113495bSYour Name ret = regulator_set_voltage(vreg->reg, 0,
302*5113495bSYour Name vreg->cfg.max_uv);
303*5113495bSYour Name if (ret)
304*5113495bSYour Name icnss_pr_err("Failed to set voltage for regulator %s, err = %d\n",
305*5113495bSYour Name vreg->cfg.name, ret);
306*5113495bSYour Name }
307*5113495bSYour Name vreg->enabled = false;
308*5113495bSYour Name
309*5113495bSYour Name return ret;
310*5113495bSYour Name }
311*5113495bSYour Name
get_vreg_list(u32 * vreg_list_size,unsigned long device_id)312*5113495bSYour Name static struct icnss_vreg_cfg *get_vreg_list(u32 *vreg_list_size,
313*5113495bSYour Name unsigned long device_id)
314*5113495bSYour Name {
315*5113495bSYour Name switch (device_id) {
316*5113495bSYour Name case WCN6750_DEVICE_ID:
317*5113495bSYour Name *vreg_list_size = ICNSS_VREG_LIST_SIZE;
318*5113495bSYour Name return icnss_wcn6750_vreg_list;
319*5113495bSYour Name
320*5113495bSYour Name case ADRASTEA_DEVICE_ID:
321*5113495bSYour Name *vreg_list_size = ICNSS_VREG_ADRESTEA_LIST_SIZE;
322*5113495bSYour Name return icnss_adrestea_vreg_list;
323*5113495bSYour Name
324*5113495bSYour Name case WCN6450_DEVICE_ID:
325*5113495bSYour Name *vreg_list_size = ICNSS_VREG_EVROS_LIST_SIZE;
326*5113495bSYour Name return icnss_wcn6450_vreg_list;
327*5113495bSYour Name
328*5113495bSYour Name default:
329*5113495bSYour Name icnss_pr_err("Unsupported device_id 0x%x\n", device_id);
330*5113495bSYour Name *vreg_list_size = 0;
331*5113495bSYour Name return NULL;
332*5113495bSYour Name }
333*5113495bSYour Name }
334*5113495bSYour Name
icnss_get_vreg(struct icnss_priv * priv)335*5113495bSYour Name int icnss_get_vreg(struct icnss_priv *priv)
336*5113495bSYour Name {
337*5113495bSYour Name int ret = 0;
338*5113495bSYour Name int i;
339*5113495bSYour Name struct icnss_vreg_info *vreg;
340*5113495bSYour Name struct icnss_vreg_cfg *vreg_cfg = NULL;
341*5113495bSYour Name struct list_head *vreg_list = &priv->vreg_list;
342*5113495bSYour Name struct device *dev = &priv->pdev->dev;
343*5113495bSYour Name u32 vreg_list_size = 0;
344*5113495bSYour Name
345*5113495bSYour Name vreg_cfg = get_vreg_list(&vreg_list_size, priv->device_id);
346*5113495bSYour Name if (!vreg_cfg)
347*5113495bSYour Name return -EINVAL;
348*5113495bSYour Name
349*5113495bSYour Name for (i = 0; i < vreg_list_size; i++) {
350*5113495bSYour Name vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
351*5113495bSYour Name if (!vreg)
352*5113495bSYour Name return -ENOMEM;
353*5113495bSYour Name
354*5113495bSYour Name memcpy(&vreg->cfg, &vreg_cfg[i], sizeof(vreg->cfg));
355*5113495bSYour Name ret = icnss_get_vreg_single(priv, vreg);
356*5113495bSYour Name if (ret != 0) {
357*5113495bSYour Name if (ret == -ENODEV)
358*5113495bSYour Name continue;
359*5113495bSYour Name else
360*5113495bSYour Name return ret;
361*5113495bSYour Name }
362*5113495bSYour Name list_add_tail(&vreg->list, vreg_list);
363*5113495bSYour Name }
364*5113495bSYour Name
365*5113495bSYour Name return 0;
366*5113495bSYour Name }
367*5113495bSYour Name
icnss_put_vreg(struct icnss_priv * priv)368*5113495bSYour Name void icnss_put_vreg(struct icnss_priv *priv)
369*5113495bSYour Name {
370*5113495bSYour Name struct list_head *vreg_list = &priv->vreg_list;
371*5113495bSYour Name struct icnss_vreg_info *vreg = NULL;
372*5113495bSYour Name
373*5113495bSYour Name while (!list_empty(vreg_list)) {
374*5113495bSYour Name vreg = list_first_entry(vreg_list,
375*5113495bSYour Name struct icnss_vreg_info, list);
376*5113495bSYour Name list_del(&vreg->list);
377*5113495bSYour Name }
378*5113495bSYour Name }
379*5113495bSYour Name
icnss_vreg_on(struct icnss_priv * priv)380*5113495bSYour Name static int icnss_vreg_on(struct icnss_priv *priv)
381*5113495bSYour Name {
382*5113495bSYour Name struct list_head *vreg_list = &priv->vreg_list;
383*5113495bSYour Name struct icnss_vreg_info *vreg = NULL;
384*5113495bSYour Name int ret = 0;
385*5113495bSYour Name
386*5113495bSYour Name list_for_each_entry(vreg, vreg_list, list) {
387*5113495bSYour Name if (IS_ERR_OR_NULL(vreg->reg) || !vreg->cfg.is_supported)
388*5113495bSYour Name continue;
389*5113495bSYour Name
390*5113495bSYour Name ret = icnss_vreg_on_single(vreg);
391*5113495bSYour Name if (ret)
392*5113495bSYour Name break;
393*5113495bSYour Name }
394*5113495bSYour Name
395*5113495bSYour Name if (!ret)
396*5113495bSYour Name return 0;
397*5113495bSYour Name
398*5113495bSYour Name list_for_each_entry_continue_reverse(vreg, vreg_list, list) {
399*5113495bSYour Name if (IS_ERR_OR_NULL(vreg->reg) || !vreg->enabled)
400*5113495bSYour Name continue;
401*5113495bSYour Name
402*5113495bSYour Name icnss_vreg_off_single(vreg);
403*5113495bSYour Name }
404*5113495bSYour Name
405*5113495bSYour Name return ret;
406*5113495bSYour Name }
407*5113495bSYour Name
icnss_vreg_off(struct icnss_priv * priv)408*5113495bSYour Name static int icnss_vreg_off(struct icnss_priv *priv)
409*5113495bSYour Name {
410*5113495bSYour Name struct list_head *vreg_list = &priv->vreg_list;
411*5113495bSYour Name struct icnss_vreg_info *vreg = NULL;
412*5113495bSYour Name
413*5113495bSYour Name list_for_each_entry_reverse(vreg, vreg_list, list) {
414*5113495bSYour Name if (IS_ERR_OR_NULL(vreg->reg))
415*5113495bSYour Name continue;
416*5113495bSYour Name
417*5113495bSYour Name icnss_vreg_off_single(vreg);
418*5113495bSYour Name }
419*5113495bSYour Name
420*5113495bSYour Name return 0;
421*5113495bSYour Name }
422*5113495bSYour Name
icnss_vreg_unvote(struct icnss_priv * priv)423*5113495bSYour Name int icnss_vreg_unvote(struct icnss_priv *priv)
424*5113495bSYour Name {
425*5113495bSYour Name struct list_head *vreg_list = &priv->vreg_list;
426*5113495bSYour Name struct icnss_vreg_info *vreg = NULL;
427*5113495bSYour Name
428*5113495bSYour Name list_for_each_entry_reverse(vreg, vreg_list, list) {
429*5113495bSYour Name if (IS_ERR_OR_NULL(vreg->reg))
430*5113495bSYour Name continue;
431*5113495bSYour Name
432*5113495bSYour Name if (vreg->cfg.need_unvote)
433*5113495bSYour Name icnss_vreg_unvote_single(vreg);
434*5113495bSYour Name }
435*5113495bSYour Name
436*5113495bSYour Name return 0;
437*5113495bSYour Name }
438*5113495bSYour Name
icnss_get_clk_single(struct icnss_priv * priv,struct icnss_clk_info * clk_info)439*5113495bSYour Name int icnss_get_clk_single(struct icnss_priv *priv,
440*5113495bSYour Name struct icnss_clk_info *clk_info)
441*5113495bSYour Name {
442*5113495bSYour Name struct device *dev = &priv->pdev->dev;
443*5113495bSYour Name struct clk *clk;
444*5113495bSYour Name int ret;
445*5113495bSYour Name
446*5113495bSYour Name clk = devm_clk_get(dev, clk_info->cfg.name);
447*5113495bSYour Name if (IS_ERR(clk)) {
448*5113495bSYour Name ret = PTR_ERR(clk);
449*5113495bSYour Name if (clk_info->cfg.required)
450*5113495bSYour Name icnss_pr_err("Failed to get clock %s, err = %d\n",
451*5113495bSYour Name clk_info->cfg.name, ret);
452*5113495bSYour Name else
453*5113495bSYour Name icnss_pr_dbg("Failed to get optional clock %s, err = %d\n",
454*5113495bSYour Name clk_info->cfg.name, ret);
455*5113495bSYour Name return ret;
456*5113495bSYour Name }
457*5113495bSYour Name
458*5113495bSYour Name clk_info->clk = clk;
459*5113495bSYour Name icnss_pr_dbg("Got clock: %s, freq: %u\n",
460*5113495bSYour Name clk_info->cfg.name, clk_info->cfg.freq);
461*5113495bSYour Name
462*5113495bSYour Name return 0;
463*5113495bSYour Name }
464*5113495bSYour Name
icnss_clk_on_single(struct icnss_clk_info * clk_info)465*5113495bSYour Name static int icnss_clk_on_single(struct icnss_clk_info *clk_info)
466*5113495bSYour Name {
467*5113495bSYour Name int ret;
468*5113495bSYour Name
469*5113495bSYour Name if (clk_info->enabled) {
470*5113495bSYour Name icnss_pr_dbg("Clock %s is already enabled\n",
471*5113495bSYour Name clk_info->cfg.name);
472*5113495bSYour Name return 0;
473*5113495bSYour Name }
474*5113495bSYour Name
475*5113495bSYour Name icnss_pr_dbg("Clock %s is being enabled\n", clk_info->cfg.name);
476*5113495bSYour Name
477*5113495bSYour Name if (clk_info->cfg.freq) {
478*5113495bSYour Name ret = clk_set_rate(clk_info->clk, clk_info->cfg.freq);
479*5113495bSYour Name if (ret) {
480*5113495bSYour Name icnss_pr_err("Failed to set frequency %u for clock %s, err = %d\n",
481*5113495bSYour Name clk_info->cfg.freq, clk_info->cfg.name,
482*5113495bSYour Name ret);
483*5113495bSYour Name return ret;
484*5113495bSYour Name }
485*5113495bSYour Name }
486*5113495bSYour Name
487*5113495bSYour Name ret = clk_prepare_enable(clk_info->clk);
488*5113495bSYour Name if (ret) {
489*5113495bSYour Name icnss_pr_err("Failed to enable clock %s, err = %d\n",
490*5113495bSYour Name clk_info->cfg.name, ret);
491*5113495bSYour Name return ret;
492*5113495bSYour Name }
493*5113495bSYour Name
494*5113495bSYour Name clk_info->enabled = true;
495*5113495bSYour Name
496*5113495bSYour Name return 0;
497*5113495bSYour Name }
498*5113495bSYour Name
icnss_clk_off_single(struct icnss_clk_info * clk_info)499*5113495bSYour Name static int icnss_clk_off_single(struct icnss_clk_info *clk_info)
500*5113495bSYour Name {
501*5113495bSYour Name if (!clk_info->enabled) {
502*5113495bSYour Name icnss_pr_dbg("Clock %s is already disabled\n",
503*5113495bSYour Name clk_info->cfg.name);
504*5113495bSYour Name return 0;
505*5113495bSYour Name }
506*5113495bSYour Name
507*5113495bSYour Name icnss_pr_dbg("Clock %s is being disabled\n", clk_info->cfg.name);
508*5113495bSYour Name
509*5113495bSYour Name clk_disable_unprepare(clk_info->clk);
510*5113495bSYour Name clk_info->enabled = false;
511*5113495bSYour Name
512*5113495bSYour Name return 0;
513*5113495bSYour Name }
514*5113495bSYour Name
icnss_get_clk(struct icnss_priv * priv)515*5113495bSYour Name int icnss_get_clk(struct icnss_priv *priv)
516*5113495bSYour Name {
517*5113495bSYour Name struct device *dev;
518*5113495bSYour Name struct list_head *clk_list;
519*5113495bSYour Name struct icnss_clk_info *clk_info;
520*5113495bSYour Name struct icnss_clk_cfg *clk_cfg;
521*5113495bSYour Name int ret, i;
522*5113495bSYour Name u32 clk_list_size = 0;
523*5113495bSYour Name
524*5113495bSYour Name if (!priv)
525*5113495bSYour Name return -ENODEV;
526*5113495bSYour Name
527*5113495bSYour Name dev = &priv->pdev->dev;
528*5113495bSYour Name clk_list = &priv->clk_list;
529*5113495bSYour Name
530*5113495bSYour Name if (priv->device_id == ADRASTEA_DEVICE_ID) {
531*5113495bSYour Name clk_cfg = icnss_adrestea_clk_list;
532*5113495bSYour Name clk_list_size = ICNSS_CLK_ADRESTEA_LIST_SIZE;
533*5113495bSYour Name } else if (priv->device_id == WCN6750_DEVICE_ID ||
534*5113495bSYour Name priv->device_id == WCN6450_DEVICE_ID) {
535*5113495bSYour Name clk_cfg = icnss_clk_list;
536*5113495bSYour Name clk_list_size = ICNSS_CLK_LIST_SIZE;
537*5113495bSYour Name }
538*5113495bSYour Name
539*5113495bSYour Name if (!list_empty(clk_list)) {
540*5113495bSYour Name icnss_pr_dbg("Clocks have already been updated\n");
541*5113495bSYour Name return 0;
542*5113495bSYour Name }
543*5113495bSYour Name
544*5113495bSYour Name for (i = 0; i < clk_list_size; i++) {
545*5113495bSYour Name clk_info = devm_kzalloc(dev, sizeof(*clk_info), GFP_KERNEL);
546*5113495bSYour Name if (!clk_info) {
547*5113495bSYour Name ret = -ENOMEM;
548*5113495bSYour Name goto cleanup;
549*5113495bSYour Name }
550*5113495bSYour Name
551*5113495bSYour Name memcpy(&clk_info->cfg, &clk_cfg[i],
552*5113495bSYour Name sizeof(clk_info->cfg));
553*5113495bSYour Name ret = icnss_get_clk_single(priv, clk_info);
554*5113495bSYour Name if (ret != 0) {
555*5113495bSYour Name if (clk_info->cfg.required)
556*5113495bSYour Name goto cleanup;
557*5113495bSYour Name else
558*5113495bSYour Name continue;
559*5113495bSYour Name }
560*5113495bSYour Name list_add_tail(&clk_info->list, clk_list);
561*5113495bSYour Name }
562*5113495bSYour Name
563*5113495bSYour Name return 0;
564*5113495bSYour Name
565*5113495bSYour Name cleanup:
566*5113495bSYour Name while (!list_empty(clk_list)) {
567*5113495bSYour Name clk_info = list_first_entry(clk_list, struct icnss_clk_info,
568*5113495bSYour Name list);
569*5113495bSYour Name list_del(&clk_info->list);
570*5113495bSYour Name }
571*5113495bSYour Name
572*5113495bSYour Name return ret;
573*5113495bSYour Name }
574*5113495bSYour Name
icnss_put_clk(struct icnss_priv * priv)575*5113495bSYour Name void icnss_put_clk(struct icnss_priv *priv)
576*5113495bSYour Name {
577*5113495bSYour Name struct device *dev;
578*5113495bSYour Name struct list_head *clk_list;
579*5113495bSYour Name struct icnss_clk_info *clk_info;
580*5113495bSYour Name
581*5113495bSYour Name if (!priv)
582*5113495bSYour Name return;
583*5113495bSYour Name
584*5113495bSYour Name dev = &priv->pdev->dev;
585*5113495bSYour Name clk_list = &priv->clk_list;
586*5113495bSYour Name
587*5113495bSYour Name while (!list_empty(clk_list)) {
588*5113495bSYour Name clk_info = list_first_entry(clk_list, struct icnss_clk_info,
589*5113495bSYour Name list);
590*5113495bSYour Name list_del(&clk_info->list);
591*5113495bSYour Name }
592*5113495bSYour Name }
593*5113495bSYour Name
icnss_clk_on(struct list_head * clk_list)594*5113495bSYour Name static int icnss_clk_on(struct list_head *clk_list)
595*5113495bSYour Name {
596*5113495bSYour Name struct icnss_clk_info *clk_info;
597*5113495bSYour Name int ret = 0;
598*5113495bSYour Name
599*5113495bSYour Name list_for_each_entry(clk_info, clk_list, list) {
600*5113495bSYour Name if (IS_ERR_OR_NULL(clk_info->clk))
601*5113495bSYour Name continue;
602*5113495bSYour Name ret = icnss_clk_on_single(clk_info);
603*5113495bSYour Name if (ret)
604*5113495bSYour Name break;
605*5113495bSYour Name }
606*5113495bSYour Name
607*5113495bSYour Name if (!ret)
608*5113495bSYour Name return 0;
609*5113495bSYour Name
610*5113495bSYour Name list_for_each_entry_continue_reverse(clk_info, clk_list, list) {
611*5113495bSYour Name if (IS_ERR_OR_NULL(clk_info->clk))
612*5113495bSYour Name continue;
613*5113495bSYour Name
614*5113495bSYour Name icnss_clk_off_single(clk_info);
615*5113495bSYour Name }
616*5113495bSYour Name
617*5113495bSYour Name return ret;
618*5113495bSYour Name }
619*5113495bSYour Name
icnss_clk_off(struct list_head * clk_list)620*5113495bSYour Name static int icnss_clk_off(struct list_head *clk_list)
621*5113495bSYour Name {
622*5113495bSYour Name struct icnss_clk_info *clk_info;
623*5113495bSYour Name
624*5113495bSYour Name list_for_each_entry_reverse(clk_info, clk_list, list) {
625*5113495bSYour Name if (IS_ERR_OR_NULL(clk_info->clk))
626*5113495bSYour Name continue;
627*5113495bSYour Name
628*5113495bSYour Name icnss_clk_off_single(clk_info);
629*5113495bSYour Name }
630*5113495bSYour Name
631*5113495bSYour Name return 0;
632*5113495bSYour Name }
633*5113495bSYour Name
icnss_hw_power_on(struct icnss_priv * priv)634*5113495bSYour Name int icnss_hw_power_on(struct icnss_priv *priv)
635*5113495bSYour Name {
636*5113495bSYour Name int ret = 0;
637*5113495bSYour Name
638*5113495bSYour Name icnss_pr_dbg("HW Power on: state: 0x%lx\n", priv->state);
639*5113495bSYour Name
640*5113495bSYour Name spin_lock(&priv->on_off_lock);
641*5113495bSYour Name if (test_bit(ICNSS_POWER_ON, &priv->state)) {
642*5113495bSYour Name spin_unlock(&priv->on_off_lock);
643*5113495bSYour Name return ret;
644*5113495bSYour Name }
645*5113495bSYour Name set_bit(ICNSS_POWER_ON, &priv->state);
646*5113495bSYour Name spin_unlock(&priv->on_off_lock);
647*5113495bSYour Name
648*5113495bSYour Name ret = icnss_vreg_on(priv);
649*5113495bSYour Name if (ret) {
650*5113495bSYour Name icnss_pr_err("Failed to turn on vreg, err = %d\n", ret);
651*5113495bSYour Name goto out;
652*5113495bSYour Name }
653*5113495bSYour Name
654*5113495bSYour Name ret = icnss_clk_on(&priv->clk_list);
655*5113495bSYour Name if (ret)
656*5113495bSYour Name goto vreg_off;
657*5113495bSYour Name
658*5113495bSYour Name return ret;
659*5113495bSYour Name
660*5113495bSYour Name vreg_off:
661*5113495bSYour Name icnss_vreg_off(priv);
662*5113495bSYour Name out:
663*5113495bSYour Name clear_bit(ICNSS_POWER_ON, &priv->state);
664*5113495bSYour Name return ret;
665*5113495bSYour Name }
666*5113495bSYour Name
icnss_hw_power_off(struct icnss_priv * priv)667*5113495bSYour Name int icnss_hw_power_off(struct icnss_priv *priv)
668*5113495bSYour Name {
669*5113495bSYour Name int ret = 0;
670*5113495bSYour Name
671*5113495bSYour Name if (test_bit(HW_ALWAYS_ON, &priv->ctrl_params.quirks))
672*5113495bSYour Name return 0;
673*5113495bSYour Name
674*5113495bSYour Name if (test_bit(ICNSS_FW_DOWN, &priv->state))
675*5113495bSYour Name return 0;
676*5113495bSYour Name
677*5113495bSYour Name icnss_pr_dbg("HW Power off: 0x%lx\n", priv->state);
678*5113495bSYour Name
679*5113495bSYour Name spin_lock(&priv->on_off_lock);
680*5113495bSYour Name if (!test_bit(ICNSS_POWER_ON, &priv->state)) {
681*5113495bSYour Name spin_unlock(&priv->on_off_lock);
682*5113495bSYour Name return ret;
683*5113495bSYour Name }
684*5113495bSYour Name clear_bit(ICNSS_POWER_ON, &priv->state);
685*5113495bSYour Name spin_unlock(&priv->on_off_lock);
686*5113495bSYour Name
687*5113495bSYour Name icnss_clk_off(&priv->clk_list);
688*5113495bSYour Name
689*5113495bSYour Name ret = icnss_vreg_off(priv);
690*5113495bSYour Name
691*5113495bSYour Name return ret;
692*5113495bSYour Name }
693*5113495bSYour Name
icnss_power_on(struct device * dev)694*5113495bSYour Name int icnss_power_on(struct device *dev)
695*5113495bSYour Name {
696*5113495bSYour Name struct icnss_priv *priv = dev_get_drvdata(dev);
697*5113495bSYour Name
698*5113495bSYour Name if (!priv) {
699*5113495bSYour Name icnss_pr_err("Invalid drvdata: dev %pK, data %pK\n",
700*5113495bSYour Name dev, priv);
701*5113495bSYour Name return -EINVAL;
702*5113495bSYour Name }
703*5113495bSYour Name
704*5113495bSYour Name icnss_pr_dbg("Power On: 0x%lx\n", priv->state);
705*5113495bSYour Name
706*5113495bSYour Name return icnss_hw_power_on(priv);
707*5113495bSYour Name }
708*5113495bSYour Name EXPORT_SYMBOL(icnss_power_on);
709*5113495bSYour Name
icnss_power_off(struct device * dev)710*5113495bSYour Name int icnss_power_off(struct device *dev)
711*5113495bSYour Name {
712*5113495bSYour Name struct icnss_priv *priv = dev_get_drvdata(dev);
713*5113495bSYour Name
714*5113495bSYour Name if (!priv) {
715*5113495bSYour Name icnss_pr_err("Invalid drvdata: dev %pK, data %pK\n",
716*5113495bSYour Name dev, priv);
717*5113495bSYour Name return -EINVAL;
718*5113495bSYour Name }
719*5113495bSYour Name
720*5113495bSYour Name icnss_pr_dbg("Power Off: 0x%lx\n", priv->state);
721*5113495bSYour Name
722*5113495bSYour Name return icnss_hw_power_off(priv);
723*5113495bSYour Name }
724*5113495bSYour Name EXPORT_SYMBOL(icnss_power_off);
725*5113495bSYour Name
icnss_power_on_chain1_reg(struct icnss_priv * priv)726*5113495bSYour Name int icnss_power_on_chain1_reg(struct icnss_priv *priv)
727*5113495bSYour Name {
728*5113495bSYour Name struct list_head *vreg_list = &priv->vreg_list;
729*5113495bSYour Name struct icnss_vreg_info *vreg = NULL;
730*5113495bSYour Name int ret = 0;
731*5113495bSYour Name
732*5113495bSYour Name list_for_each_entry(vreg, vreg_list, list) {
733*5113495bSYour Name if (!strcmp(ICNSS_CHAIN1_REGULATOR, vreg->cfg.name) && priv->is_chain1_supported) {
734*5113495bSYour Name vreg->cfg.is_supported = true;
735*5113495bSYour Name ret = icnss_vreg_on_single(vreg);
736*5113495bSYour Name break;
737*5113495bSYour Name }
738*5113495bSYour Name }
739*5113495bSYour Name
740*5113495bSYour Name /* Setting chain1 supported to false as chain1 regulator cfg already updated */
741*5113495bSYour Name priv->is_chain1_supported = false;
742*5113495bSYour Name
743*5113495bSYour Name return ret;
744*5113495bSYour Name }
745*5113495bSYour Name
icnss_put_resources(struct icnss_priv * priv)746*5113495bSYour Name void icnss_put_resources(struct icnss_priv *priv)
747*5113495bSYour Name {
748*5113495bSYour Name icnss_put_clk(priv);
749*5113495bSYour Name icnss_put_vreg(priv);
750*5113495bSYour Name }
751*5113495bSYour Name
752*5113495bSYour Name
753*5113495bSYour Name #if IS_ENABLED(CONFIG_MSM_QMP)
754*5113495bSYour Name /**
755*5113495bSYour Name * icnss_aop_interface_init: Initialize AOP interface: either mbox channel or direct QMP
756*5113495bSYour Name * @priv: Pointer to icnss platform data
757*5113495bSYour Name *
758*5113495bSYour Name * Device tree file should have either mbox or qmp configured, but not both.
759*5113495bSYour Name * Based on device tree configuration setup mbox channel or QMP
760*5113495bSYour Name *
761*5113495bSYour Name * Return: 0 for success, otherwise error code
762*5113495bSYour Name */
icnss_aop_interface_init(struct icnss_priv * priv)763*5113495bSYour Name int icnss_aop_interface_init(struct icnss_priv *priv)
764*5113495bSYour Name {
765*5113495bSYour Name struct mbox_client *mbox = &priv->mbox_client_data;
766*5113495bSYour Name struct mbox_chan *chan;
767*5113495bSYour Name int ret = 0, ol_cpr = 0;
768*5113495bSYour Name
769*5113495bSYour Name ol_cpr = of_property_read_string(priv->pdev->dev.of_node,
770*5113495bSYour Name "qcom,vreg_ol_cpr",
771*5113495bSYour Name &priv->cpr_info.vreg_ol_cpr);
772*5113495bSYour Name
773*5113495bSYour Name if (ol_cpr && !priv->pdc_init_table) {
774*5113495bSYour Name icnss_pr_dbg("Vreg for OL CPR and pdc_init table not configured\n");
775*5113495bSYour Name return -EINVAL;
776*5113495bSYour Name }
777*5113495bSYour Name
778*5113495bSYour Name mbox->dev = &priv->pdev->dev;
779*5113495bSYour Name mbox->tx_block = true;
780*5113495bSYour Name mbox->tx_tout = ICNSS_MBOX_TIMEOUT_MS;
781*5113495bSYour Name mbox->knows_txdone = false;
782*5113495bSYour Name
783*5113495bSYour Name priv->mbox_chan = NULL;
784*5113495bSYour Name priv->qmp = NULL;
785*5113495bSYour Name priv->use_direct_qmp = false;
786*5113495bSYour Name /* First try to get mbox channel, if it fails then try qmp_get
787*5113495bSYour Name * In device tree file there should be either mboxes or qmp,
788*5113495bSYour Name * cannot have both properties at the same time.
789*5113495bSYour Name */
790*5113495bSYour Name chan = mbox_request_channel(mbox, 0);
791*5113495bSYour Name if (IS_ERR(chan)) {
792*5113495bSYour Name ret = PTR_ERR(chan);
793*5113495bSYour Name icnss_pr_dbg("Failed to get mbox channel with err %d\n", ret);
794*5113495bSYour Name priv->qmp = qmp_get(&priv->pdev->dev);
795*5113495bSYour Name if (IS_ERR(priv->qmp)) {
796*5113495bSYour Name icnss_pr_err("Failed to get qmp\n");
797*5113495bSYour Name return PTR_ERR(priv->qmp);
798*5113495bSYour Name } else {
799*5113495bSYour Name priv->use_direct_qmp = true;
800*5113495bSYour Name icnss_pr_dbg("QMP initialized\n");
801*5113495bSYour Name }
802*5113495bSYour Name } else {
803*5113495bSYour Name priv->mbox_chan = chan;
804*5113495bSYour Name icnss_pr_dbg("Mbox channel initialized\n");
805*5113495bSYour Name }
806*5113495bSYour Name ret = icnss_aop_pdc_reconfig(priv);
807*5113495bSYour Name if (ret)
808*5113495bSYour Name icnss_pr_err("Failed to reconfig WLAN PDC, err = %d\n", ret);
809*5113495bSYour Name
810*5113495bSYour Name return ret;
811*5113495bSYour Name }
812*5113495bSYour Name
813*5113495bSYour Name /**
814*5113495bSYour Name * icnss_aop_interface_deinit: Cleanup AOP interface
815*5113495bSYour Name * @priv: Pointer to icnss platform data
816*5113495bSYour Name *
817*5113495bSYour Name * Cleanup mbox channel or QMP whichever was configured during initialization.
818*5113495bSYour Name *
819*5113495bSYour Name * Return: None
820*5113495bSYour Name */
icnss_aop_interface_deinit(struct icnss_priv * priv)821*5113495bSYour Name void icnss_aop_interface_deinit(struct icnss_priv *priv)
822*5113495bSYour Name {
823*5113495bSYour Name if (!IS_ERR_OR_NULL(priv->mbox_chan))
824*5113495bSYour Name mbox_free_channel(priv->mbox_chan);
825*5113495bSYour Name
826*5113495bSYour Name if (!IS_ERR_OR_NULL(priv->qmp)) {
827*5113495bSYour Name qmp_put(priv->qmp);
828*5113495bSYour Name priv->use_direct_qmp = false;
829*5113495bSYour Name }
830*5113495bSYour Name }
831*5113495bSYour Name
icnss_aop_set_vreg_param(struct icnss_priv * priv,const char * vreg_name,enum icnss_vreg_param param,enum icnss_tcs_seq seq,int val)832*5113495bSYour Name static int icnss_aop_set_vreg_param(struct icnss_priv *priv,
833*5113495bSYour Name const char *vreg_name,
834*5113495bSYour Name enum icnss_vreg_param param,
835*5113495bSYour Name enum icnss_tcs_seq seq, int val)
836*5113495bSYour Name {
837*5113495bSYour Name struct qmp_pkt pkt;
838*5113495bSYour Name char mbox_msg[ICNSS_MBOX_MSG_MAX_LEN];
839*5113495bSYour Name static const char * const vreg_param_str[] = {"v", "m", "e"};
840*5113495bSYour Name static const char *const tcs_seq_str[] = {"upval", "dwnval", "enable"};
841*5113495bSYour Name int ret = 0;
842*5113495bSYour Name
843*5113495bSYour Name if (param > ICNSS_VREG_ENABLE || seq > ICNSS_TCS_ALL_SEQ || !vreg_name)
844*5113495bSYour Name return -EINVAL;
845*5113495bSYour Name
846*5113495bSYour Name snprintf(mbox_msg, ICNSS_MBOX_MSG_MAX_LEN,
847*5113495bSYour Name "{class: wlan_pdc, res: %s.%s, %s: %d}", vreg_name,
848*5113495bSYour Name vreg_param_str[param], tcs_seq_str[seq], val);
849*5113495bSYour Name if (priv->use_direct_qmp) {
850*5113495bSYour Name icnss_pr_dbg("Sending AOP QMP msg: %s\n", mbox_msg);
851*5113495bSYour Name ret = qmp_send(priv->qmp, mbox_msg, ICNSS_MBOX_MSG_MAX_LEN);
852*5113495bSYour Name if (ret < 0)
853*5113495bSYour Name icnss_pr_err("Failed to send AOP QMP msg: %s\n", mbox_msg);
854*5113495bSYour Name else
855*5113495bSYour Name ret = 0;
856*5113495bSYour Name } else {
857*5113495bSYour Name icnss_pr_dbg("Sending AOP Mbox msg: %s\n", mbox_msg);
858*5113495bSYour Name pkt.size = ICNSS_MBOX_MSG_MAX_LEN;
859*5113495bSYour Name pkt.data = mbox_msg;
860*5113495bSYour Name
861*5113495bSYour Name ret = mbox_send_message(priv->mbox_chan, &pkt);
862*5113495bSYour Name if (ret < 0)
863*5113495bSYour Name icnss_pr_err("Failed to send AOP mbox msg: %s,ret: %d\n",
864*5113495bSYour Name mbox_msg, ret);
865*5113495bSYour Name else
866*5113495bSYour Name ret = 0;
867*5113495bSYour Name }
868*5113495bSYour Name
869*5113495bSYour Name return ret;
870*5113495bSYour Name }
871*5113495bSYour Name
872*5113495bSYour Name /* icnss_aop_pdc_reconfig: Send AOP msg to configure PDC table for WLAN device
873*5113495bSYour Name * @priv: Pointer to icnss platform data
874*5113495bSYour Name *
875*5113495bSYour Name * Send AOP QMP or Mbox msg to configure PDC table for WLAN device
876*5113495bSYour Name *
877*5113495bSYour Name * Return: 0 for success, otherwise error code
878*5113495bSYour Name */
icnss_aop_pdc_reconfig(struct icnss_priv * priv)879*5113495bSYour Name int icnss_aop_pdc_reconfig(struct icnss_priv *priv)
880*5113495bSYour Name {
881*5113495bSYour Name u32 i;
882*5113495bSYour Name int ret;
883*5113495bSYour Name char *mbox_msg;
884*5113495bSYour Name struct qmp_pkt pkt;
885*5113495bSYour Name
886*5113495bSYour Name if (priv->pdc_init_table_len <= 0 || !priv->pdc_init_table)
887*5113495bSYour Name return 0;
888*5113495bSYour Name
889*5113495bSYour Name icnss_pr_dbg("Setting PDC defaults for device ID: (0x%x)\n",
890*5113495bSYour Name priv->device_id);
891*5113495bSYour Name for (i = 0; i < priv->pdc_init_table_len; i++) {
892*5113495bSYour Name mbox_msg = (char *)priv->pdc_init_table[i];
893*5113495bSYour Name if (priv->use_direct_qmp) {
894*5113495bSYour Name icnss_pr_dbg("Sending AOP QMP msg: %s\n", mbox_msg);
895*5113495bSYour Name ret = qmp_send(priv->qmp, mbox_msg,
896*5113495bSYour Name ICNSS_MBOX_MSG_MAX_LEN);
897*5113495bSYour Name if (ret < 0)
898*5113495bSYour Name icnss_pr_err("Failed to send AOP QMP msg: %s\n",
899*5113495bSYour Name mbox_msg);
900*5113495bSYour Name else
901*5113495bSYour Name ret = 0;
902*5113495bSYour Name } else {
903*5113495bSYour Name icnss_pr_dbg("Sending AOP Mbox msg: %s\n", mbox_msg);
904*5113495bSYour Name pkt.size = ICNSS_MBOX_MSG_MAX_LEN;
905*5113495bSYour Name pkt.data = mbox_msg;
906*5113495bSYour Name
907*5113495bSYour Name ret = mbox_send_message(priv->mbox_chan, &pkt);
908*5113495bSYour Name if (ret < 0)
909*5113495bSYour Name icnss_pr_err("Failed to send AOP mbox msg: %s,ret: %d\n",
910*5113495bSYour Name mbox_msg, ret);
911*5113495bSYour Name else
912*5113495bSYour Name ret = 0;
913*5113495bSYour Name }
914*5113495bSYour Name }
915*5113495bSYour Name return ret;
916*5113495bSYour Name }
917*5113495bSYour Name
918*5113495bSYour Name #else
icnss_aop_interface_init(struct icnss_priv * priv)919*5113495bSYour Name int icnss_aop_interface_init(struct icnss_priv *priv)
920*5113495bSYour Name {
921*5113495bSYour Name return 0;
922*5113495bSYour Name }
923*5113495bSYour Name
icnss_aop_interface_deinit(struct icnss_priv * priv)924*5113495bSYour Name void icnss_aop_interface_deinit(struct icnss_priv *priv)
925*5113495bSYour Name {
926*5113495bSYour Name }
927*5113495bSYour Name
icnss_aop_set_vreg_param(struct icnss_priv * priv,const char * vreg_name,enum icnss_vreg_param param,enum icnss_tcs_seq seq,int val)928*5113495bSYour Name static int icnss_aop_set_vreg_param(struct icnss_priv *priv,
929*5113495bSYour Name const char *vreg_name,
930*5113495bSYour Name enum icnss_vreg_param param,
931*5113495bSYour Name enum icnss_tcs_seq seq, int val)
932*5113495bSYour Name {
933*5113495bSYour Name return 0;
934*5113495bSYour Name }
935*5113495bSYour Name
icnss_aop_pdc_reconfig(struct icnss_priv * priv)936*5113495bSYour Name int icnss_aop_pdc_reconfig(struct icnss_priv *priv)
937*5113495bSYour Name {
938*5113495bSYour Name return 0;
939*5113495bSYour Name }
940*5113495bSYour Name
941*5113495bSYour Name #endif
942*5113495bSYour Name
icnss_power_misc_params_init(struct icnss_priv * priv)943*5113495bSYour Name void icnss_power_misc_params_init(struct icnss_priv *priv)
944*5113495bSYour Name {
945*5113495bSYour Name struct device *dev = &priv->pdev->dev;
946*5113495bSYour Name int ret;
947*5113495bSYour Name
948*5113495bSYour Name /* common DT Entries */
949*5113495bSYour Name priv->pdc_init_table_len =
950*5113495bSYour Name of_property_count_strings(dev->of_node,
951*5113495bSYour Name "qcom,pdc_init_table");
952*5113495bSYour Name if (priv->pdc_init_table_len > 0) {
953*5113495bSYour Name priv->pdc_init_table =
954*5113495bSYour Name kcalloc(priv->pdc_init_table_len,
955*5113495bSYour Name sizeof(char *), GFP_KERNEL);
956*5113495bSYour Name if (priv->pdc_init_table) {
957*5113495bSYour Name ret = of_property_read_string_array(dev->of_node,
958*5113495bSYour Name "qcom,pdc_init_table",
959*5113495bSYour Name priv->pdc_init_table,
960*5113495bSYour Name priv->pdc_init_table_len);
961*5113495bSYour Name if (ret < 0)
962*5113495bSYour Name icnss_pr_err("Failed to get PDC Init Table\n");
963*5113495bSYour Name } else {
964*5113495bSYour Name icnss_pr_err("Failed to alloc PDC Init Table mem\n");
965*5113495bSYour Name }
966*5113495bSYour Name } else {
967*5113495bSYour Name icnss_pr_dbg("PDC Init Table not configured\n");
968*5113495bSYour Name }
969*5113495bSYour Name }
970*5113495bSYour Name
icnss_update_cpr_info(struct icnss_priv * priv)971*5113495bSYour Name int icnss_update_cpr_info(struct icnss_priv *priv)
972*5113495bSYour Name {
973*5113495bSYour Name struct icnss_cpr_info *cpr_info = &priv->cpr_info;
974*5113495bSYour Name
975*5113495bSYour Name if (!cpr_info->vreg_ol_cpr || (!priv->mbox_chan && !priv->use_direct_qmp)) {
976*5113495bSYour Name icnss_pr_dbg("Mbox channel / QMP / OL CPR Vreg not configured\n");
977*5113495bSYour Name return 0;
978*5113495bSYour Name }
979*5113495bSYour Name
980*5113495bSYour Name if (cpr_info->voltage == 0) {
981*5113495bSYour Name icnss_pr_err("Voltage %dmV is not valid\n", cpr_info->voltage);
982*5113495bSYour Name return -EINVAL;
983*5113495bSYour Name }
984*5113495bSYour Name
985*5113495bSYour Name cpr_info->voltage = cpr_info->voltage > BT_CXMX_VOLTAGE_MV ?
986*5113495bSYour Name cpr_info->voltage : BT_CXMX_VOLTAGE_MV;
987*5113495bSYour Name
988*5113495bSYour Name return icnss_aop_set_vreg_param(priv,
989*5113495bSYour Name cpr_info->vreg_ol_cpr,
990*5113495bSYour Name ICNSS_VREG_VOLTAGE,
991*5113495bSYour Name ICNSS_TCS_UP_SEQ,
992*5113495bSYour Name cpr_info->voltage);
993*5113495bSYour Name }
994*5113495bSYour Name
icnss_get_battery_level(struct icnss_priv * priv)995*5113495bSYour Name static int icnss_get_battery_level(struct icnss_priv *priv)
996*5113495bSYour Name {
997*5113495bSYour Name int err = 0, battery_percentage = 0;
998*5113495bSYour Name union power_supply_propval psp = {0,};
999*5113495bSYour Name
1000*5113495bSYour Name if (!priv->batt_psy)
1001*5113495bSYour Name priv->batt_psy = power_supply_get_by_name("battery");
1002*5113495bSYour Name
1003*5113495bSYour Name if (priv->batt_psy) {
1004*5113495bSYour Name err = power_supply_get_property(priv->batt_psy,
1005*5113495bSYour Name POWER_SUPPLY_PROP_CAPACITY,
1006*5113495bSYour Name &psp);
1007*5113495bSYour Name if (err) {
1008*5113495bSYour Name icnss_pr_err("battery percentage read error:%d\n", err);
1009*5113495bSYour Name goto out;
1010*5113495bSYour Name }
1011*5113495bSYour Name battery_percentage = psp.intval;
1012*5113495bSYour Name }
1013*5113495bSYour Name
1014*5113495bSYour Name icnss_pr_info("Battery Percentage: %d\n", battery_percentage);
1015*5113495bSYour Name out:
1016*5113495bSYour Name return battery_percentage;
1017*5113495bSYour Name }
1018*5113495bSYour Name
icnss_update_soc_level(struct work_struct * work)1019*5113495bSYour Name static void icnss_update_soc_level(struct work_struct *work)
1020*5113495bSYour Name {
1021*5113495bSYour Name int battery_percentage = 0, current_updated_voltage = 0, err = 0;
1022*5113495bSYour Name int level_count;
1023*5113495bSYour Name struct icnss_priv *priv = container_of(work, struct icnss_priv, soc_update_work);
1024*5113495bSYour Name
1025*5113495bSYour Name battery_percentage = icnss_get_battery_level(priv);
1026*5113495bSYour Name if (!battery_percentage ||
1027*5113495bSYour Name battery_percentage > ICNSS_MAX_BATTERY_LEVEL) {
1028*5113495bSYour Name icnss_pr_err("Battery percentage read failure\n");
1029*5113495bSYour Name return;
1030*5113495bSYour Name }
1031*5113495bSYour Name
1032*5113495bSYour Name for (level_count = 0; level_count < ICNSS_BATTERY_LEVEL_COUNT;
1033*5113495bSYour Name level_count++) {
1034*5113495bSYour Name if (battery_percentage >=
1035*5113495bSYour Name icnss_battery_level[level_count].lower_battery_threshold) {
1036*5113495bSYour Name current_updated_voltage =
1037*5113495bSYour Name icnss_battery_level[level_count].ldo_voltage;
1038*5113495bSYour Name break;
1039*5113495bSYour Name }
1040*5113495bSYour Name }
1041*5113495bSYour Name
1042*5113495bSYour Name if (level_count != ICNSS_BATTERY_LEVEL_COUNT &&
1043*5113495bSYour Name priv->last_updated_voltage != current_updated_voltage) {
1044*5113495bSYour Name err = icnss_send_vbatt_update(priv, current_updated_voltage);
1045*5113495bSYour Name if (err < 0) {
1046*5113495bSYour Name icnss_pr_err("Unable to update ldo voltage");
1047*5113495bSYour Name return;
1048*5113495bSYour Name }
1049*5113495bSYour Name priv->last_updated_voltage = current_updated_voltage;
1050*5113495bSYour Name }
1051*5113495bSYour Name }
1052*5113495bSYour Name
icnss_battery_supply_callback(struct notifier_block * nb,unsigned long event,void * data)1053*5113495bSYour Name static int icnss_battery_supply_callback(struct notifier_block *nb,
1054*5113495bSYour Name unsigned long event, void *data)
1055*5113495bSYour Name {
1056*5113495bSYour Name struct power_supply *psy = data;
1057*5113495bSYour Name struct icnss_priv *priv = container_of(nb, struct icnss_priv,
1058*5113495bSYour Name psf_nb);
1059*5113495bSYour Name if (strcmp(psy->desc->name, "battery"))
1060*5113495bSYour Name return NOTIFY_OK;
1061*5113495bSYour Name
1062*5113495bSYour Name if (test_bit(ICNSS_WLFW_CONNECTED, &priv->state) &&
1063*5113495bSYour Name !test_bit(ICNSS_FW_DOWN, &priv->state))
1064*5113495bSYour Name queue_work(priv->soc_update_wq, &priv->soc_update_work);
1065*5113495bSYour Name
1066*5113495bSYour Name return NOTIFY_OK;
1067*5113495bSYour Name }
1068*5113495bSYour Name
icnss_get_psf_info(struct icnss_priv * priv)1069*5113495bSYour Name int icnss_get_psf_info(struct icnss_priv *priv)
1070*5113495bSYour Name {
1071*5113495bSYour Name int ret = 0;
1072*5113495bSYour Name
1073*5113495bSYour Name priv->soc_update_wq = alloc_workqueue("icnss_soc_update",
1074*5113495bSYour Name WQ_UNBOUND, 1);
1075*5113495bSYour Name if (!priv->soc_update_wq) {
1076*5113495bSYour Name icnss_pr_err("Workqueue creation failed for soc update\n");
1077*5113495bSYour Name ret = -EFAULT;
1078*5113495bSYour Name goto out;
1079*5113495bSYour Name }
1080*5113495bSYour Name
1081*5113495bSYour Name priv->psf_nb.notifier_call = icnss_battery_supply_callback;
1082*5113495bSYour Name ret = power_supply_reg_notifier(&priv->psf_nb);
1083*5113495bSYour Name if (ret < 0) {
1084*5113495bSYour Name icnss_pr_err("Power supply framework registration err: %d\n",
1085*5113495bSYour Name ret);
1086*5113495bSYour Name goto err_psf_registration;
1087*5113495bSYour Name }
1088*5113495bSYour Name
1089*5113495bSYour Name INIT_WORK(&priv->soc_update_work, icnss_update_soc_level);
1090*5113495bSYour Name
1091*5113495bSYour Name return 0;
1092*5113495bSYour Name
1093*5113495bSYour Name err_psf_registration:
1094*5113495bSYour Name destroy_workqueue(priv->soc_update_wq);
1095*5113495bSYour Name out:
1096*5113495bSYour Name return ret;
1097*5113495bSYour Name }
1098