1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
3 // Copyright (c) 2017-2018, Linaro Limited
5 #include <linux/slab.h>
7 #include <linux/kernel.h>
8 #include <linux/delay.h>
10 #include "wcd-clsh-v2.h"
12 struct wcd_clsh_ctrl
{
19 struct snd_soc_component
*comp
;
22 /* Class-H registers for codecs from and above WCD9335 */
23 #define WCD9XXX_A_CDC_RX0_RX_PATH_CFG0 WCD9335_REG(0xB, 0x42)
24 #define WCD9XXX_A_CDC_RX_PATH_CLSH_EN_MASK BIT(6)
25 #define WCD9XXX_A_CDC_RX_PATH_CLSH_ENABLE BIT(6)
26 #define WCD9XXX_A_CDC_RX_PATH_CLSH_DISABLE 0
27 #define WCD9XXX_A_CDC_RX1_RX_PATH_CFG0 WCD9335_REG(0xB, 0x56)
28 #define WCD9XXX_A_CDC_RX2_RX_PATH_CFG0 WCD9335_REG(0xB, 0x6A)
29 #define WCD9XXX_A_CDC_CLSH_K1_MSB WCD9335_REG(0xC, 0x08)
30 #define WCD9XXX_A_CDC_CLSH_K1_MSB_COEF_MASK GENMASK(3, 0)
31 #define WCD9XXX_A_CDC_CLSH_K1_LSB WCD9335_REG(0xC, 0x09)
32 #define WCD9XXX_A_CDC_CLSH_K1_LSB_COEF_MASK GENMASK(7, 0)
33 #define WCD9XXX_A_ANA_RX_SUPPLIES WCD9335_REG(0x6, 0x08)
34 #define WCD9XXX_A_ANA_RX_REGULATOR_MODE_MASK BIT(1)
35 #define WCD9XXX_A_ANA_RX_REGULATOR_MODE_CLS_H 0
36 #define WCD9XXX_A_ANA_RX_REGULATOR_MODE_CLS_AB BIT(1)
37 #define WCD9XXX_A_ANA_RX_VNEG_PWR_LVL_MASK BIT(2)
38 #define WCD9XXX_A_ANA_RX_VNEG_PWR_LVL_UHQA BIT(2)
39 #define WCD9XXX_A_ANA_RX_VNEG_PWR_LVL_DEFAULT 0
40 #define WCD9XXX_A_ANA_RX_VPOS_PWR_LVL_MASK BIT(3)
41 #define WCD9XXX_A_ANA_RX_VPOS_PWR_LVL_UHQA BIT(3)
42 #define WCD9XXX_A_ANA_RX_VPOS_PWR_LVL_DEFAULT 0
43 #define WCD9XXX_A_ANA_RX_VNEG_EN_MASK BIT(6)
44 #define WCD9XXX_A_ANA_RX_VNEG_EN_SHIFT 6
45 #define WCD9XXX_A_ANA_RX_VNEG_ENABLE BIT(6)
46 #define WCD9XXX_A_ANA_RX_VNEG_DISABLE 0
47 #define WCD9XXX_A_ANA_RX_VPOS_EN_MASK BIT(7)
48 #define WCD9XXX_A_ANA_RX_VPOS_EN_SHIFT 7
49 #define WCD9XXX_A_ANA_RX_VPOS_ENABLE BIT(7)
50 #define WCD9XXX_A_ANA_RX_VPOS_DISABLE 0
51 #define WCD9XXX_A_ANA_HPH WCD9335_REG(0x6, 0x09)
52 #define WCD9XXX_A_ANA_HPH_PWR_LEVEL_MASK GENMASK(3, 2)
53 #define WCD9XXX_A_ANA_HPH_PWR_LEVEL_UHQA 0x08
54 #define WCD9XXX_A_ANA_HPH_PWR_LEVEL_LP 0x04
55 #define WCD9XXX_A_ANA_HPH_PWR_LEVEL_NORMAL 0x0
56 #define WCD9XXX_A_CDC_CLSH_CRC WCD9335_REG(0xC, 0x01)
57 #define WCD9XXX_A_CDC_CLSH_CRC_CLK_EN_MASK BIT(0)
58 #define WCD9XXX_A_CDC_CLSH_CRC_CLK_ENABLE BIT(0)
59 #define WCD9XXX_A_CDC_CLSH_CRC_CLK_DISABLE 0
60 #define WCD9XXX_FLYBACK_EN WCD9335_REG(0x6, 0xA4)
61 #define WCD9XXX_FLYBACK_EN_DELAY_SEL_MASK GENMASK(6, 5)
62 #define WCD9XXX_FLYBACK_EN_DELAY_26P25_US 0x40
63 #define WCD9XXX_FLYBACK_EN_RESET_BY_EXT_MASK BIT(4)
64 #define WCD9XXX_FLYBACK_EN_PWDN_WITHOUT_DELAY BIT(4)
65 #define WCD9XXX_FLYBACK_EN_PWDN_WITH_DELAY 0
66 #define WCD9XXX_RX_BIAS_FLYB_BUFF WCD9335_REG(0x6, 0xC7)
67 #define WCD9XXX_RX_BIAS_FLYB_VNEG_5_UA_MASK GENMASK(7, 4)
68 #define WCD9XXX_RX_BIAS_FLYB_VPOS_5_UA_MASK GENMASK(3, 0)
69 #define WCD9XXX_HPH_L_EN WCD9335_REG(0x6, 0xD3)
70 #define WCD9XXX_HPH_CONST_SEL_L_MASK GENMASK(7, 3)
71 #define WCD9XXX_HPH_CONST_SEL_BYPASS 0
72 #define WCD9XXX_HPH_CONST_SEL_LP_PATH 0x40
73 #define WCD9XXX_HPH_CONST_SEL_HQ_PATH 0x80
74 #define WCD9XXX_HPH_R_EN WCD9335_REG(0x6, 0xD6)
75 #define WCD9XXX_HPH_REFBUFF_UHQA_CTL WCD9335_REG(0x6, 0xDD)
76 #define WCD9XXX_HPH_REFBUFF_UHQA_GAIN_MASK GENMASK(2, 0)
77 #define WCD9XXX_CLASSH_CTRL_VCL_2 WCD9335_REG(0x6, 0x9B)
78 #define WCD9XXX_CLASSH_CTRL_VCL_2_VREF_FILT_1_MASK GENMASK(5, 4)
79 #define WCD9XXX_CLASSH_CTRL_VCL_VREF_FILT_R_50KOHM 0x20
80 #define WCD9XXX_CLASSH_CTRL_VCL_VREF_FILT_R_0KOHM 0x0
81 #define WCD9XXX_CDC_RX1_RX_PATH_CTL WCD9335_REG(0xB, 0x55)
82 #define WCD9XXX_CDC_RX2_RX_PATH_CTL WCD9335_REG(0xB, 0x69)
83 #define WCD9XXX_CDC_CLK_RST_CTRL_MCLK_CONTROL WCD9335_REG(0xD, 0x41)
84 #define WCD9XXX_CDC_CLK_RST_CTRL_MCLK_EN_MASK BIT(0)
85 #define WCD9XXX_CDC_CLK_RST_CTRL_MCLK_11P3_EN_MASK BIT(1)
86 #define WCD9XXX_CLASSH_CTRL_CCL_1 WCD9335_REG(0x6, 0x9C)
87 #define WCD9XXX_CLASSH_CTRL_CCL_1_DELTA_IPEAK_MASK GENMASK(7, 4)
88 #define WCD9XXX_CLASSH_CTRL_CCL_1_DELTA_IPEAK_50MA 0x50
89 #define WCD9XXX_CLASSH_CTRL_CCL_1_DELTA_IPEAK_30MA 0x30
91 #define WCD9XXX_BASE_ADDRESS 0x3000
92 #define WCD9XXX_ANA_RX_SUPPLIES (WCD9XXX_BASE_ADDRESS+0x008)
93 #define WCD9XXX_ANA_HPH (WCD9XXX_BASE_ADDRESS+0x009)
94 #define WCD9XXX_CLASSH_MODE_2 (WCD9XXX_BASE_ADDRESS+0x098)
95 #define WCD9XXX_CLASSH_MODE_3 (WCD9XXX_BASE_ADDRESS+0x099)
96 #define WCD9XXX_FLYBACK_VNEG_CTRL_1 (WCD9XXX_BASE_ADDRESS+0x0A5)
97 #define WCD9XXX_FLYBACK_VNEG_CTRL_4 (WCD9XXX_BASE_ADDRESS+0x0A8)
98 #define WCD9XXX_FLYBACK_VNEGDAC_CTRL_2 (WCD9XXX_BASE_ADDRESS+0x0AF)
99 #define WCD9XXX_RX_BIAS_HPH_LOWPOWER (WCD9XXX_BASE_ADDRESS+0x0BF)
100 #define WCD9XXX_V3_RX_BIAS_FLYB_BUFF (WCD9XXX_BASE_ADDRESS+0x0C7)
101 #define WCD9XXX_HPH_PA_CTL1 (WCD9XXX_BASE_ADDRESS+0x0D1)
102 #define WCD9XXX_HPH_NEW_INT_PA_MISC2 (WCD9XXX_BASE_ADDRESS+0x138)
104 #define CLSH_REQ_ENABLE true
105 #define CLSH_REQ_DISABLE false
106 #define WCD_USLEEP_RANGE 50
119 static inline void wcd_enable_clsh_block(struct wcd_clsh_ctrl
*ctrl
,
122 struct snd_soc_component
*comp
= ctrl
->comp
;
124 if ((enable
&& ++ctrl
->clsh_users
== 1) ||
125 (!enable
&& --ctrl
->clsh_users
== 0))
126 snd_soc_component_update_bits(comp
, WCD9XXX_A_CDC_CLSH_CRC
,
127 WCD9XXX_A_CDC_CLSH_CRC_CLK_EN_MASK
,
129 if (ctrl
->clsh_users
< 0)
130 ctrl
->clsh_users
= 0;
133 static inline void wcd_clsh_set_buck_mode(struct snd_soc_component
*comp
,
137 if (mode
== CLS_H_HIFI
)
138 snd_soc_component_update_bits(comp
, WCD9XXX_A_ANA_RX_SUPPLIES
,
139 WCD9XXX_A_ANA_RX_VPOS_PWR_LVL_MASK
,
140 WCD9XXX_A_ANA_RX_VPOS_PWR_LVL_UHQA
);
142 snd_soc_component_update_bits(comp
, WCD9XXX_A_ANA_RX_SUPPLIES
,
143 WCD9XXX_A_ANA_RX_VPOS_PWR_LVL_MASK
,
144 WCD9XXX_A_ANA_RX_VPOS_PWR_LVL_DEFAULT
);
147 static void wcd_clsh_v3_set_buck_mode(struct snd_soc_component
*component
,
150 if (mode
== CLS_H_HIFI
|| mode
== CLS_H_LOHIFI
||
151 mode
== CLS_AB_HIFI
|| mode
== CLS_AB_LOHIFI
)
152 snd_soc_component_update_bits(component
,
153 WCD9XXX_ANA_RX_SUPPLIES
,
154 0x08, 0x08); /* set to HIFI */
156 snd_soc_component_update_bits(component
,
157 WCD9XXX_ANA_RX_SUPPLIES
,
158 0x08, 0x00); /* set to default */
161 static inline void wcd_clsh_set_flyback_mode(struct snd_soc_component
*comp
,
165 if (mode
== CLS_H_HIFI
)
166 snd_soc_component_update_bits(comp
, WCD9XXX_A_ANA_RX_SUPPLIES
,
167 WCD9XXX_A_ANA_RX_VNEG_PWR_LVL_MASK
,
168 WCD9XXX_A_ANA_RX_VNEG_PWR_LVL_UHQA
);
170 snd_soc_component_update_bits(comp
, WCD9XXX_A_ANA_RX_SUPPLIES
,
171 WCD9XXX_A_ANA_RX_VNEG_PWR_LVL_MASK
,
172 WCD9XXX_A_ANA_RX_VNEG_PWR_LVL_DEFAULT
);
175 static void wcd_clsh_buck_ctrl(struct wcd_clsh_ctrl
*ctrl
,
179 struct snd_soc_component
*comp
= ctrl
->comp
;
181 /* enable/disable buck */
182 if ((enable
&& (++ctrl
->buck_users
== 1)) ||
183 (!enable
&& (--ctrl
->buck_users
== 0)))
184 snd_soc_component_update_bits(comp
, WCD9XXX_A_ANA_RX_SUPPLIES
,
185 WCD9XXX_A_ANA_RX_VPOS_EN_MASK
,
186 enable
<< WCD9XXX_A_ANA_RX_VPOS_EN_SHIFT
);
188 * 500us sleep is required after buck enable/disable
189 * as per HW requirement
191 usleep_range(500, 500 + WCD_USLEEP_RANGE
);
194 static void wcd_clsh_v3_buck_ctrl(struct snd_soc_component
*component
,
195 struct wcd_clsh_ctrl
*ctrl
,
199 /* enable/disable buck */
200 if ((enable
&& (++ctrl
->buck_users
== 1)) ||
201 (!enable
&& (--ctrl
->buck_users
== 0))) {
202 snd_soc_component_update_bits(component
,
203 WCD9XXX_ANA_RX_SUPPLIES
,
204 (1 << 7), (enable
<< 7));
206 * 500us sleep is required after buck enable/disable
207 * as per HW requirement
209 usleep_range(500, 510);
210 if (mode
== CLS_H_LOHIFI
|| mode
== CLS_H_ULP
||
211 mode
== CLS_H_HIFI
|| mode
== CLS_H_LP
)
212 snd_soc_component_update_bits(component
,
213 WCD9XXX_CLASSH_MODE_3
,
216 snd_soc_component_update_bits(component
,
217 WCD9XXX_CLASSH_MODE_2
,
219 /* 500usec delay is needed as per HW requirement */
220 usleep_range(500, 500 + WCD_USLEEP_RANGE
);
224 static void wcd_clsh_flyback_ctrl(struct wcd_clsh_ctrl
*ctrl
,
228 struct snd_soc_component
*comp
= ctrl
->comp
;
230 /* enable/disable flyback */
231 if ((enable
&& (++ctrl
->flyback_users
== 1)) ||
232 (!enable
&& (--ctrl
->flyback_users
== 0))) {
233 snd_soc_component_update_bits(comp
, WCD9XXX_A_ANA_RX_SUPPLIES
,
234 WCD9XXX_A_ANA_RX_VNEG_EN_MASK
,
235 enable
<< WCD9XXX_A_ANA_RX_VNEG_EN_SHIFT
);
236 /* 100usec delay is needed as per HW requirement */
237 usleep_range(100, 110);
240 * 500us sleep is required after flyback enable/disable
241 * as per HW requirement
243 usleep_range(500, 500 + WCD_USLEEP_RANGE
);
246 static void wcd_clsh_set_gain_path(struct wcd_clsh_ctrl
*ctrl
, int mode
)
248 struct snd_soc_component
*comp
= ctrl
->comp
;
254 val
= WCD9XXX_HPH_CONST_SEL_BYPASS
;
257 val
= WCD9XXX_HPH_CONST_SEL_HQ_PATH
;
260 val
= WCD9XXX_HPH_CONST_SEL_LP_PATH
;
264 snd_soc_component_update_bits(comp
, WCD9XXX_HPH_L_EN
,
265 WCD9XXX_HPH_CONST_SEL_L_MASK
,
268 snd_soc_component_update_bits(comp
, WCD9XXX_HPH_R_EN
,
269 WCD9XXX_HPH_CONST_SEL_L_MASK
,
273 static void wcd_clsh_v2_set_hph_mode(struct snd_soc_component
*comp
, int mode
)
275 int val
= 0, gain
= 0, res_val
;
276 int ipeak
= WCD9XXX_CLASSH_CTRL_CCL_1_DELTA_IPEAK_50MA
;
278 res_val
= WCD9XXX_CLASSH_CTRL_VCL_VREF_FILT_R_0KOHM
;
281 res_val
= WCD9XXX_CLASSH_CTRL_VCL_VREF_FILT_R_50KOHM
;
282 val
= WCD9XXX_A_ANA_HPH_PWR_LEVEL_NORMAL
;
284 ipeak
= WCD9XXX_CLASSH_CTRL_CCL_1_DELTA_IPEAK_50MA
;
287 val
= WCD9XXX_A_ANA_HPH_PWR_LEVEL_NORMAL
;
289 ipeak
= WCD9XXX_CLASSH_CTRL_CCL_1_DELTA_IPEAK_50MA
;
292 val
= WCD9XXX_A_ANA_HPH_PWR_LEVEL_UHQA
;
293 gain
= DAC_GAIN_M0P2DB
;
294 ipeak
= WCD9XXX_CLASSH_CTRL_CCL_1_DELTA_IPEAK_50MA
;
297 val
= WCD9XXX_A_ANA_HPH_PWR_LEVEL_LP
;
298 ipeak
= WCD9XXX_CLASSH_CTRL_CCL_1_DELTA_IPEAK_30MA
;
302 snd_soc_component_update_bits(comp
, WCD9XXX_A_ANA_HPH
,
303 WCD9XXX_A_ANA_HPH_PWR_LEVEL_MASK
, val
);
304 snd_soc_component_update_bits(comp
, WCD9XXX_CLASSH_CTRL_VCL_2
,
305 WCD9XXX_CLASSH_CTRL_VCL_2_VREF_FILT_1_MASK
,
307 if (mode
!= CLS_H_LP
)
308 snd_soc_component_update_bits(comp
,
309 WCD9XXX_HPH_REFBUFF_UHQA_CTL
,
310 WCD9XXX_HPH_REFBUFF_UHQA_GAIN_MASK
,
312 snd_soc_component_update_bits(comp
, WCD9XXX_CLASSH_CTRL_CCL_1
,
313 WCD9XXX_CLASSH_CTRL_CCL_1_DELTA_IPEAK_MASK
,
317 static void wcd_clsh_v3_set_hph_mode(struct snd_soc_component
*component
,
341 dev_err(component
->dev
, "%s:Invalid mode %d\n", __func__
, mode
);
345 snd_soc_component_update_bits(component
, WCD9XXX_ANA_HPH
, 0x0C, val
);
348 void wcd_clsh_set_hph_mode(struct wcd_clsh_ctrl
*ctrl
, int mode
)
350 struct snd_soc_component
*comp
= ctrl
->comp
;
352 if (ctrl
->codec_version
>= WCD937X
)
353 wcd_clsh_v3_set_hph_mode(comp
, mode
);
355 wcd_clsh_v2_set_hph_mode(comp
, mode
);
358 EXPORT_SYMBOL_GPL(wcd_clsh_set_hph_mode
);
360 static void wcd_clsh_set_flyback_current(struct snd_soc_component
*comp
,
364 snd_soc_component_update_bits(comp
, WCD9XXX_RX_BIAS_FLYB_BUFF
,
365 WCD9XXX_RX_BIAS_FLYB_VPOS_5_UA_MASK
, 0x0A);
366 snd_soc_component_update_bits(comp
, WCD9XXX_RX_BIAS_FLYB_BUFF
,
367 WCD9XXX_RX_BIAS_FLYB_VNEG_5_UA_MASK
, 0x0A);
368 /* Sleep needed to avoid click and pop as per HW requirement */
369 usleep_range(100, 110);
372 static void wcd_clsh_set_buck_regulator_mode(struct snd_soc_component
*comp
,
376 snd_soc_component_update_bits(comp
, WCD9XXX_A_ANA_RX_SUPPLIES
,
377 WCD9XXX_A_ANA_RX_REGULATOR_MODE_MASK
,
378 WCD9XXX_A_ANA_RX_REGULATOR_MODE_CLS_AB
);
380 snd_soc_component_update_bits(comp
, WCD9XXX_A_ANA_RX_SUPPLIES
,
381 WCD9XXX_A_ANA_RX_REGULATOR_MODE_MASK
,
382 WCD9XXX_A_ANA_RX_REGULATOR_MODE_CLS_H
);
385 static void wcd_clsh_v3_set_buck_regulator_mode(struct snd_soc_component
*component
,
388 snd_soc_component_update_bits(component
, WCD9XXX_ANA_RX_SUPPLIES
,
392 static void wcd_clsh_v3_set_flyback_mode(struct snd_soc_component
*component
,
395 if (mode
== CLS_H_HIFI
|| mode
== CLS_H_LOHIFI
||
396 mode
== CLS_AB_HIFI
|| mode
== CLS_AB_LOHIFI
) {
397 snd_soc_component_update_bits(component
,
398 WCD9XXX_ANA_RX_SUPPLIES
,
400 snd_soc_component_update_bits(component
,
401 WCD9XXX_FLYBACK_VNEG_CTRL_4
,
404 snd_soc_component_update_bits(component
,
405 WCD9XXX_ANA_RX_SUPPLIES
,
406 0x04, 0x00); /* set to Default */
407 snd_soc_component_update_bits(component
,
408 WCD9XXX_FLYBACK_VNEG_CTRL_4
,
413 static void wcd_clsh_v3_force_iq_ctl(struct snd_soc_component
*component
,
414 int mode
, bool enable
)
417 snd_soc_component_update_bits(component
,
418 WCD9XXX_FLYBACK_VNEGDAC_CTRL_2
,
420 /* 100usec delay is needed as per HW requirement */
421 usleep_range(100, 110);
422 snd_soc_component_update_bits(component
,
423 WCD9XXX_CLASSH_MODE_3
,
425 snd_soc_component_update_bits(component
,
426 WCD9XXX_CLASSH_MODE_2
,
428 if (mode
== CLS_H_LOHIFI
|| mode
== CLS_AB_LOHIFI
) {
429 snd_soc_component_update_bits(component
,
430 WCD9XXX_HPH_NEW_INT_PA_MISC2
,
432 snd_soc_component_update_bits(component
,
433 WCD9XXX_RX_BIAS_HPH_LOWPOWER
,
435 snd_soc_component_update_bits(component
,
440 snd_soc_component_update_bits(component
,
441 WCD9XXX_HPH_NEW_INT_PA_MISC2
,
443 snd_soc_component_update_bits(component
,
444 WCD9XXX_RX_BIAS_HPH_LOWPOWER
,
446 snd_soc_component_update_bits(component
,
452 static void wcd_clsh_v3_flyback_ctrl(struct snd_soc_component
*component
,
453 struct wcd_clsh_ctrl
*ctrl
,
457 /* enable/disable flyback */
458 if ((enable
&& (++ctrl
->flyback_users
== 1)) ||
459 (!enable
&& (--ctrl
->flyback_users
== 0))) {
460 snd_soc_component_update_bits(component
,
461 WCD9XXX_FLYBACK_VNEG_CTRL_1
,
463 snd_soc_component_update_bits(component
,
464 WCD9XXX_ANA_RX_SUPPLIES
,
465 (1 << 6), (enable
<< 6));
467 * 100us sleep is required after flyback enable/disable
468 * as per HW requirement
470 usleep_range(100, 110);
471 snd_soc_component_update_bits(component
,
472 WCD9XXX_FLYBACK_VNEGDAC_CTRL_2
,
474 /* 500usec delay is needed as per HW requirement */
475 usleep_range(500, 500 + WCD_USLEEP_RANGE
);
479 static void wcd_clsh_v3_set_flyback_current(struct snd_soc_component
*component
,
482 snd_soc_component_update_bits(component
, WCD9XXX_V3_RX_BIAS_FLYB_BUFF
,
484 snd_soc_component_update_bits(component
, WCD9XXX_V3_RX_BIAS_FLYB_BUFF
,
486 /* Sleep needed to avoid click and pop as per HW requirement */
487 usleep_range(100, 110);
490 static void wcd_clsh_v3_state_aux(struct wcd_clsh_ctrl
*ctrl
, int req_state
,
491 bool is_enable
, int mode
)
493 struct snd_soc_component
*component
= ctrl
->comp
;
496 wcd_clsh_v3_set_buck_mode(component
, mode
);
497 wcd_clsh_v3_set_flyback_mode(component
, mode
);
498 wcd_clsh_v3_flyback_ctrl(component
, ctrl
, mode
, true);
499 wcd_clsh_v3_set_flyback_current(component
, mode
);
500 wcd_clsh_v3_buck_ctrl(component
, ctrl
, mode
, true);
502 wcd_clsh_v3_buck_ctrl(component
, ctrl
, mode
, false);
503 wcd_clsh_v3_flyback_ctrl(component
, ctrl
, mode
, false);
504 wcd_clsh_v3_set_flyback_mode(component
, CLS_H_NORMAL
);
505 wcd_clsh_v3_set_buck_mode(component
, CLS_H_NORMAL
);
509 static void wcd_clsh_state_lo(struct wcd_clsh_ctrl
*ctrl
, int req_state
,
510 bool is_enable
, int mode
)
512 struct snd_soc_component
*comp
= ctrl
->comp
;
514 if (mode
!= CLS_AB
) {
515 dev_err(comp
->dev
, "%s: LO cannot be in this mode: %d\n",
521 wcd_clsh_set_buck_regulator_mode(comp
, mode
);
522 wcd_clsh_set_buck_mode(comp
, mode
);
523 wcd_clsh_set_flyback_mode(comp
, mode
);
524 wcd_clsh_flyback_ctrl(ctrl
, mode
, true);
525 wcd_clsh_set_flyback_current(comp
, mode
);
526 wcd_clsh_buck_ctrl(ctrl
, mode
, true);
528 wcd_clsh_buck_ctrl(ctrl
, mode
, false);
529 wcd_clsh_flyback_ctrl(ctrl
, mode
, false);
530 wcd_clsh_set_flyback_mode(comp
, CLS_H_NORMAL
);
531 wcd_clsh_set_buck_mode(comp
, CLS_H_NORMAL
);
532 wcd_clsh_set_buck_regulator_mode(comp
, CLS_H_NORMAL
);
536 static void wcd_clsh_v3_state_hph_r(struct wcd_clsh_ctrl
*ctrl
, int req_state
,
537 bool is_enable
, int mode
)
539 struct snd_soc_component
*component
= ctrl
->comp
;
541 if (mode
== CLS_H_NORMAL
) {
542 dev_dbg(component
->dev
, "%s: Normal mode not applicable for hph_r\n",
548 wcd_clsh_v3_set_buck_regulator_mode(component
, mode
);
549 wcd_clsh_v3_set_flyback_mode(component
, mode
);
550 wcd_clsh_v3_force_iq_ctl(component
, mode
, true);
551 wcd_clsh_v3_flyback_ctrl(component
, ctrl
, mode
, true);
552 wcd_clsh_v3_set_flyback_current(component
, mode
);
553 wcd_clsh_v3_set_buck_mode(component
, mode
);
554 wcd_clsh_v3_buck_ctrl(component
, ctrl
, mode
, true);
555 wcd_clsh_v3_set_hph_mode(component
, mode
);
557 wcd_clsh_v3_set_hph_mode(component
, CLS_H_NORMAL
);
559 /* buck and flyback set to default mode and disable */
560 wcd_clsh_v3_flyback_ctrl(component
, ctrl
, CLS_H_NORMAL
, false);
561 wcd_clsh_v3_buck_ctrl(component
, ctrl
, CLS_H_NORMAL
, false);
562 wcd_clsh_v3_force_iq_ctl(component
, CLS_H_NORMAL
, false);
563 wcd_clsh_v3_set_flyback_mode(component
, CLS_H_NORMAL
);
564 wcd_clsh_v3_set_buck_mode(component
, CLS_H_NORMAL
);
568 static void wcd_clsh_state_hph_r(struct wcd_clsh_ctrl
*ctrl
, int req_state
,
569 bool is_enable
, int mode
)
571 struct snd_soc_component
*comp
= ctrl
->comp
;
573 if (mode
== CLS_H_NORMAL
) {
574 dev_err(comp
->dev
, "%s: Normal mode not applicable for hph_r\n",
580 if (mode
!= CLS_AB
) {
581 wcd_enable_clsh_block(ctrl
, true);
583 * These K1 values depend on the Headphone Impedance
584 * For now it is assumed to be 16 ohm
586 snd_soc_component_update_bits(comp
,
587 WCD9XXX_A_CDC_CLSH_K1_MSB
,
588 WCD9XXX_A_CDC_CLSH_K1_MSB_COEF_MASK
,
590 snd_soc_component_update_bits(comp
,
591 WCD9XXX_A_CDC_CLSH_K1_LSB
,
592 WCD9XXX_A_CDC_CLSH_K1_LSB_COEF_MASK
,
594 snd_soc_component_update_bits(comp
,
595 WCD9XXX_A_CDC_RX2_RX_PATH_CFG0
,
596 WCD9XXX_A_CDC_RX_PATH_CLSH_EN_MASK
,
597 WCD9XXX_A_CDC_RX_PATH_CLSH_ENABLE
);
599 wcd_clsh_set_buck_regulator_mode(comp
, mode
);
600 wcd_clsh_set_flyback_mode(comp
, mode
);
601 wcd_clsh_flyback_ctrl(ctrl
, mode
, true);
602 wcd_clsh_set_flyback_current(comp
, mode
);
603 wcd_clsh_set_buck_mode(comp
, mode
);
604 wcd_clsh_buck_ctrl(ctrl
, mode
, true);
605 wcd_clsh_v2_set_hph_mode(comp
, mode
);
606 wcd_clsh_set_gain_path(ctrl
, mode
);
608 wcd_clsh_v2_set_hph_mode(comp
, CLS_H_NORMAL
);
610 if (mode
!= CLS_AB
) {
611 snd_soc_component_update_bits(comp
,
612 WCD9XXX_A_CDC_RX2_RX_PATH_CFG0
,
613 WCD9XXX_A_CDC_RX_PATH_CLSH_EN_MASK
,
614 WCD9XXX_A_CDC_RX_PATH_CLSH_DISABLE
);
615 wcd_enable_clsh_block(ctrl
, false);
617 /* buck and flyback set to default mode and disable */
618 wcd_clsh_buck_ctrl(ctrl
, CLS_H_NORMAL
, false);
619 wcd_clsh_flyback_ctrl(ctrl
, CLS_H_NORMAL
, false);
620 wcd_clsh_set_flyback_mode(comp
, CLS_H_NORMAL
);
621 wcd_clsh_set_buck_mode(comp
, CLS_H_NORMAL
);
622 wcd_clsh_set_buck_regulator_mode(comp
, CLS_H_NORMAL
);
626 static void wcd_clsh_v3_state_hph_l(struct wcd_clsh_ctrl
*ctrl
, int req_state
,
627 bool is_enable
, int mode
)
629 struct snd_soc_component
*component
= ctrl
->comp
;
631 if (mode
== CLS_H_NORMAL
) {
632 dev_dbg(component
->dev
, "%s: Normal mode not applicable for hph_l\n",
638 wcd_clsh_v3_set_buck_regulator_mode(component
, mode
);
639 wcd_clsh_v3_set_flyback_mode(component
, mode
);
640 wcd_clsh_v3_force_iq_ctl(component
, mode
, true);
641 wcd_clsh_v3_flyback_ctrl(component
, ctrl
, mode
, true);
642 wcd_clsh_v3_set_flyback_current(component
, mode
);
643 wcd_clsh_v3_set_buck_mode(component
, mode
);
644 wcd_clsh_v3_buck_ctrl(component
, ctrl
, mode
, true);
645 wcd_clsh_v3_set_hph_mode(component
, mode
);
647 wcd_clsh_v3_set_hph_mode(component
, CLS_H_NORMAL
);
649 /* set buck and flyback to Default Mode */
650 wcd_clsh_v3_flyback_ctrl(component
, ctrl
, CLS_H_NORMAL
, false);
651 wcd_clsh_v3_buck_ctrl(component
, ctrl
, CLS_H_NORMAL
, false);
652 wcd_clsh_v3_force_iq_ctl(component
, CLS_H_NORMAL
, false);
653 wcd_clsh_v3_set_flyback_mode(component
, CLS_H_NORMAL
);
654 wcd_clsh_v3_set_buck_mode(component
, CLS_H_NORMAL
);
658 static void wcd_clsh_state_hph_l(struct wcd_clsh_ctrl
*ctrl
, int req_state
,
659 bool is_enable
, int mode
)
661 struct snd_soc_component
*comp
= ctrl
->comp
;
663 if (mode
== CLS_H_NORMAL
) {
664 dev_err(comp
->dev
, "%s: Normal mode not applicable for hph_l\n",
670 if (mode
!= CLS_AB
) {
671 wcd_enable_clsh_block(ctrl
, true);
673 * These K1 values depend on the Headphone Impedance
674 * For now it is assumed to be 16 ohm
676 snd_soc_component_update_bits(comp
,
677 WCD9XXX_A_CDC_CLSH_K1_MSB
,
678 WCD9XXX_A_CDC_CLSH_K1_MSB_COEF_MASK
,
680 snd_soc_component_update_bits(comp
,
681 WCD9XXX_A_CDC_CLSH_K1_LSB
,
682 WCD9XXX_A_CDC_CLSH_K1_LSB_COEF_MASK
,
684 snd_soc_component_update_bits(comp
,
685 WCD9XXX_A_CDC_RX1_RX_PATH_CFG0
,
686 WCD9XXX_A_CDC_RX_PATH_CLSH_EN_MASK
,
687 WCD9XXX_A_CDC_RX_PATH_CLSH_ENABLE
);
689 wcd_clsh_set_buck_regulator_mode(comp
, mode
);
690 wcd_clsh_set_flyback_mode(comp
, mode
);
691 wcd_clsh_flyback_ctrl(ctrl
, mode
, true);
692 wcd_clsh_set_flyback_current(comp
, mode
);
693 wcd_clsh_set_buck_mode(comp
, mode
);
694 wcd_clsh_buck_ctrl(ctrl
, mode
, true);
695 wcd_clsh_v2_set_hph_mode(comp
, mode
);
696 wcd_clsh_set_gain_path(ctrl
, mode
);
698 wcd_clsh_v2_set_hph_mode(comp
, CLS_H_NORMAL
);
700 if (mode
!= CLS_AB
) {
701 snd_soc_component_update_bits(comp
,
702 WCD9XXX_A_CDC_RX1_RX_PATH_CFG0
,
703 WCD9XXX_A_CDC_RX_PATH_CLSH_EN_MASK
,
704 WCD9XXX_A_CDC_RX_PATH_CLSH_DISABLE
);
705 wcd_enable_clsh_block(ctrl
, false);
707 /* set buck and flyback to Default Mode */
708 wcd_clsh_buck_ctrl(ctrl
, CLS_H_NORMAL
, false);
709 wcd_clsh_flyback_ctrl(ctrl
, CLS_H_NORMAL
, false);
710 wcd_clsh_set_flyback_mode(comp
, CLS_H_NORMAL
);
711 wcd_clsh_set_buck_mode(comp
, CLS_H_NORMAL
);
712 wcd_clsh_set_buck_regulator_mode(comp
, CLS_H_NORMAL
);
716 static void wcd_clsh_v3_state_ear(struct wcd_clsh_ctrl
*ctrl
, int req_state
,
717 bool is_enable
, int mode
)
719 struct snd_soc_component
*component
= ctrl
->comp
;
722 wcd_clsh_v3_set_buck_regulator_mode(component
, mode
);
723 wcd_clsh_v3_set_flyback_mode(component
, mode
);
724 wcd_clsh_v3_force_iq_ctl(component
, mode
, true);
725 wcd_clsh_v3_flyback_ctrl(component
, ctrl
, mode
, true);
726 wcd_clsh_v3_set_flyback_current(component
, mode
);
727 wcd_clsh_v3_set_buck_mode(component
, mode
);
728 wcd_clsh_v3_buck_ctrl(component
, ctrl
, mode
, true);
729 wcd_clsh_v3_set_hph_mode(component
, mode
);
731 wcd_clsh_v3_set_hph_mode(component
, CLS_H_NORMAL
);
733 /* set buck and flyback to Default Mode */
734 wcd_clsh_v3_flyback_ctrl(component
, ctrl
, CLS_H_NORMAL
, false);
735 wcd_clsh_v3_buck_ctrl(component
, ctrl
, CLS_H_NORMAL
, false);
736 wcd_clsh_v3_force_iq_ctl(component
, CLS_H_NORMAL
, false);
737 wcd_clsh_v3_set_flyback_mode(component
, CLS_H_NORMAL
);
738 wcd_clsh_v3_set_buck_mode(component
, CLS_H_NORMAL
);
742 static void wcd_clsh_state_ear(struct wcd_clsh_ctrl
*ctrl
, int req_state
,
743 bool is_enable
, int mode
)
745 struct snd_soc_component
*comp
= ctrl
->comp
;
747 if (mode
!= CLS_H_NORMAL
) {
748 dev_err(comp
->dev
, "%s: mode: %d cannot be used for EAR\n",
754 wcd_enable_clsh_block(ctrl
, true);
755 snd_soc_component_update_bits(comp
,
756 WCD9XXX_A_CDC_RX0_RX_PATH_CFG0
,
757 WCD9XXX_A_CDC_RX_PATH_CLSH_EN_MASK
,
758 WCD9XXX_A_CDC_RX_PATH_CLSH_ENABLE
);
759 wcd_clsh_set_buck_mode(comp
, mode
);
760 wcd_clsh_set_flyback_mode(comp
, mode
);
761 wcd_clsh_flyback_ctrl(ctrl
, mode
, true);
762 wcd_clsh_set_flyback_current(comp
, mode
);
763 wcd_clsh_buck_ctrl(ctrl
, mode
, true);
765 snd_soc_component_update_bits(comp
,
766 WCD9XXX_A_CDC_RX0_RX_PATH_CFG0
,
767 WCD9XXX_A_CDC_RX_PATH_CLSH_EN_MASK
,
768 WCD9XXX_A_CDC_RX_PATH_CLSH_DISABLE
);
769 wcd_enable_clsh_block(ctrl
, false);
770 wcd_clsh_buck_ctrl(ctrl
, mode
, false);
771 wcd_clsh_flyback_ctrl(ctrl
, mode
, false);
772 wcd_clsh_set_flyback_mode(comp
, CLS_H_NORMAL
);
773 wcd_clsh_set_buck_mode(comp
, CLS_H_NORMAL
);
777 static int _wcd_clsh_ctrl_set_state(struct wcd_clsh_ctrl
*ctrl
, int req_state
,
778 bool is_enable
, int mode
)
781 case WCD_CLSH_STATE_EAR
:
782 if (ctrl
->codec_version
>= WCD937X
)
783 wcd_clsh_v3_state_ear(ctrl
, req_state
, is_enable
, mode
);
785 wcd_clsh_state_ear(ctrl
, req_state
, is_enable
, mode
);
787 case WCD_CLSH_STATE_HPHL
:
788 if (ctrl
->codec_version
>= WCD937X
)
789 wcd_clsh_v3_state_hph_l(ctrl
, req_state
, is_enable
, mode
);
791 wcd_clsh_state_hph_l(ctrl
, req_state
, is_enable
, mode
);
793 case WCD_CLSH_STATE_HPHR
:
794 if (ctrl
->codec_version
>= WCD937X
)
795 wcd_clsh_v3_state_hph_r(ctrl
, req_state
, is_enable
, mode
);
797 wcd_clsh_state_hph_r(ctrl
, req_state
, is_enable
, mode
);
799 case WCD_CLSH_STATE_LO
:
800 if (ctrl
->codec_version
< WCD937X
)
801 wcd_clsh_state_lo(ctrl
, req_state
, is_enable
, mode
);
803 case WCD_CLSH_STATE_AUX
:
804 if (ctrl
->codec_version
>= WCD937X
)
805 wcd_clsh_v3_state_aux(ctrl
, req_state
, is_enable
, mode
);
815 * Function: wcd_clsh_is_state_valid
818 * Provides information on valid states of Class H configuration
820 static bool wcd_clsh_is_state_valid(int state
)
823 case WCD_CLSH_STATE_IDLE
:
824 case WCD_CLSH_STATE_EAR
:
825 case WCD_CLSH_STATE_HPHL
:
826 case WCD_CLSH_STATE_HPHR
:
827 case WCD_CLSH_STATE_LO
:
828 case WCD_CLSH_STATE_AUX
:
836 * Function: wcd_clsh_fsm
837 * Params: ctrl, req_state, req_type, clsh_event
839 * This function handles PRE DAC and POST DAC conditions of different devices
840 * and updates class H configuration of different combination of devices
841 * based on validity of their states. ctrl will contain current
842 * class h state information
844 int wcd_clsh_ctrl_set_state(struct wcd_clsh_ctrl
*ctrl
,
845 enum wcd_clsh_event clsh_event
,
847 enum wcd_clsh_mode mode
)
849 struct snd_soc_component
*comp
= ctrl
->comp
;
851 if (nstate
== ctrl
->state
)
854 if (!wcd_clsh_is_state_valid(nstate
)) {
855 dev_err(comp
->dev
, "Class-H not a valid new state:\n");
859 switch (clsh_event
) {
860 case WCD_CLSH_EVENT_PRE_DAC
:
861 _wcd_clsh_ctrl_set_state(ctrl
, nstate
, CLSH_REQ_ENABLE
, mode
);
863 case WCD_CLSH_EVENT_POST_PA
:
864 _wcd_clsh_ctrl_set_state(ctrl
, nstate
, CLSH_REQ_DISABLE
, mode
);
868 ctrl
->state
= nstate
;
873 EXPORT_SYMBOL_GPL(wcd_clsh_ctrl_set_state
);
875 int wcd_clsh_ctrl_get_state(struct wcd_clsh_ctrl
*ctrl
)
879 EXPORT_SYMBOL_GPL(wcd_clsh_ctrl_get_state
);
881 struct wcd_clsh_ctrl
*wcd_clsh_ctrl_alloc(struct snd_soc_component
*comp
,
884 struct wcd_clsh_ctrl
*ctrl
;
886 ctrl
= kzalloc(sizeof(*ctrl
), GFP_KERNEL
);
888 return ERR_PTR(-ENOMEM
);
890 ctrl
->state
= WCD_CLSH_STATE_IDLE
;
892 ctrl
->codec_version
= version
;
896 EXPORT_SYMBOL_GPL(wcd_clsh_ctrl_alloc
);
898 void wcd_clsh_ctrl_free(struct wcd_clsh_ctrl
*ctrl
)
902 EXPORT_SYMBOL_GPL(wcd_clsh_ctrl_free
);
904 MODULE_DESCRIPTION("WCD93XX Class-H driver");
905 MODULE_LICENSE("GPL");