1 // SPDX-License-Identifier: GPL-2.0-only
3 // aw88081.c -- AW88081 ALSA SoC Audio driver
5 // Copyright (c) 2024 awinic Technology CO., LTD
7 // Author: Weidong Wang <wangweidong.a@awinic.com>
10 #include <linux/firmware.h>
11 #include <linux/i2c.h>
12 #include <linux/regmap.h>
13 #include <sound/soc.h>
15 #include "aw88395/aw88395_device.h"
18 struct aw_device
*aw_pa
;
20 struct delayed_work start_work
;
21 struct regmap
*regmap
;
22 struct aw_container
*aw_cfg
;
27 static const struct regmap_config aw88081_regmap_config
= {
30 .max_register
= AW88081_REG_MAX
,
31 .reg_format_endian
= REGMAP_ENDIAN_LITTLE
,
32 .val_format_endian
= REGMAP_ENDIAN_BIG
,
35 static int aw88081_dev_get_iis_status(struct aw_device
*aw_dev
)
40 ret
= regmap_read(aw_dev
->regmap
, AW88081_SYSST_REG
, ®_val
);
43 if ((reg_val
& AW88081_BIT_PLL_CHECK
) != AW88081_BIT_PLL_CHECK
) {
44 dev_err(aw_dev
->dev
, "check pll lock fail,reg_val:0x%04x", reg_val
);
51 static int aw88081_dev_check_mode1_pll(struct aw_device
*aw_dev
)
55 for (i
= 0; i
< AW88081_DEV_SYSST_CHECK_MAX
; i
++) {
56 ret
= aw88081_dev_get_iis_status(aw_dev
);
58 dev_err(aw_dev
->dev
, "mode1 iis signal check error");
59 usleep_range(AW88081_2000_US
, AW88081_2000_US
+ 10);
68 static int aw88081_dev_check_mode2_pll(struct aw_device
*aw_dev
)
73 ret
= regmap_read(aw_dev
->regmap
, AW88081_PLLCTRL1_REG
, ®_val
);
77 reg_val
&= (~AW88081_CCO_MUX_MASK
);
78 if (reg_val
== AW88081_CCO_MUX_DIVIDED_VALUE
) {
79 dev_dbg(aw_dev
->dev
, "CCO_MUX is already divider");
84 ret
= regmap_update_bits(aw_dev
->regmap
, AW88081_PLLCTRL1_REG
,
85 ~AW88081_CCO_MUX_MASK
, AW88081_CCO_MUX_DIVIDED_VALUE
);
89 for (i
= 0; i
< AW88081_DEV_SYSST_CHECK_MAX
; i
++) {
90 ret
= aw88081_dev_get_iis_status(aw_dev
);
92 dev_err(aw_dev
->dev
, "mode2 iis check error");
93 usleep_range(AW88081_2000_US
, AW88081_2000_US
+ 10);
100 ret
= regmap_update_bits(aw_dev
->regmap
, AW88081_PLLCTRL1_REG
,
101 ~AW88081_CCO_MUX_MASK
, AW88081_CCO_MUX_BYPASS_VALUE
);
103 usleep_range(AW88081_2000_US
, AW88081_2000_US
+ 10);
104 for (i
= 0; i
< AW88081_DEV_SYSST_CHECK_MAX
; i
++) {
105 ret
= aw88081_dev_check_mode1_pll(aw_dev
);
107 dev_err(aw_dev
->dev
, "mode2 switch to mode1, iis check error");
108 usleep_range(AW88081_2000_US
, AW88081_2000_US
+ 10);
118 static int aw88081_dev_check_syspll(struct aw_device
*aw_dev
)
122 ret
= aw88081_dev_check_mode1_pll(aw_dev
);
124 dev_dbg(aw_dev
->dev
, "mode1 check iis failed try switch to mode2 check");
125 ret
= aw88081_dev_check_mode2_pll(aw_dev
);
127 dev_err(aw_dev
->dev
, "mode2 check iis failed");
135 static int aw88081_dev_check_sysst(struct aw_device
*aw_dev
)
137 unsigned int check_val
;
138 unsigned int reg_val
;
142 ret
= regmap_read(aw_dev
->regmap
, AW88081_PWMCTRL4_REG
, ®_val
);
146 if (reg_val
& (~AW88081_NOISE_GATE_EN_MASK
))
147 check_val
= AW88081_NO_SWS_SYSST_CHECK
;
149 check_val
= AW88081_SWS_SYSST_CHECK
;
151 for (i
= 0; i
< AW88081_DEV_SYSST_CHECK_MAX
; i
++) {
152 ret
= regmap_read(aw_dev
->regmap
, AW88081_SYSST_REG
, ®_val
);
156 value
= reg_val
& (~AW88081_BIT_SYSST_CHECK_MASK
) & check_val
;
157 if (value
!= check_val
) {
158 dev_err(aw_dev
->dev
, "check sysst fail, reg_val=0x%04x, check:0x%x",
160 usleep_range(AW88081_2000_US
, AW88081_2000_US
+ 10);
169 static void aw88081_dev_i2s_tx_enable(struct aw_device
*aw_dev
, bool flag
)
172 regmap_update_bits(aw_dev
->regmap
, AW88081_I2SCTRL3_REG
,
173 ~AW88081_I2STXEN_MASK
, AW88081_I2STXEN_ENABLE_VALUE
);
175 regmap_update_bits(aw_dev
->regmap
, AW88081_I2SCTRL3_REG
,
176 ~AW88081_I2STXEN_MASK
, AW88081_I2STXEN_DISABLE_VALUE
);
179 static void aw88081_dev_pwd(struct aw_device
*aw_dev
, bool pwd
)
182 regmap_update_bits(aw_dev
->regmap
, AW88081_SYSCTRL_REG
,
183 ~AW88081_PWDN_MASK
, AW88081_PWDN_POWER_DOWN_VALUE
);
185 regmap_update_bits(aw_dev
->regmap
, AW88081_SYSCTRL_REG
,
186 ~AW88081_PWDN_MASK
, AW88081_PWDN_WORKING_VALUE
);
189 static void aw88081_dev_amppd(struct aw_device
*aw_dev
, bool amppd
)
192 regmap_update_bits(aw_dev
->regmap
, AW88081_SYSCTRL_REG
,
193 ~AW88081_EN_PA_MASK
, AW88081_EN_PA_POWER_DOWN_VALUE
);
195 regmap_update_bits(aw_dev
->regmap
, AW88081_SYSCTRL_REG
,
196 ~AW88081_EN_PA_MASK
, AW88081_EN_PA_WORKING_VALUE
);
199 static void aw88081_dev_clear_int_status(struct aw_device
*aw_dev
)
201 unsigned int int_status
;
203 /* read int status and clear */
204 regmap_read(aw_dev
->regmap
, AW88081_SYSINT_REG
, &int_status
);
205 /* make sure int status is clear */
206 regmap_read(aw_dev
->regmap
, AW88081_SYSINT_REG
, &int_status
);
208 dev_dbg(aw_dev
->dev
, "read interrupt reg = 0x%04x", int_status
);
211 static void aw88081_dev_set_volume(struct aw_device
*aw_dev
, unsigned int value
)
213 struct aw_volume_desc
*vol_desc
= &aw_dev
->volume_desc
;
216 volume
= min((value
+ vol_desc
->init_volume
), (unsigned int)AW88081_MUTE_VOL
);
218 regmap_update_bits(aw_dev
->regmap
, AW88081_SYSCTRL2_REG
, ~AW88081_VOL_MASK
, volume
);
221 static void aw88081_dev_fade_in(struct aw_device
*aw_dev
)
223 struct aw_volume_desc
*desc
= &aw_dev
->volume_desc
;
224 int fade_in_vol
= desc
->ctl_volume
;
225 int fade_step
= aw_dev
->fade_step
;
228 if (fade_step
== 0 || aw_dev
->fade_in_time
== 0) {
229 aw88081_dev_set_volume(aw_dev
, fade_in_vol
);
233 for (i
= AW88081_MUTE_VOL
; i
>= fade_in_vol
; i
-= fade_step
) {
234 aw88081_dev_set_volume(aw_dev
, i
);
235 usleep_range(aw_dev
->fade_in_time
, aw_dev
->fade_in_time
+ 10);
238 if (i
!= fade_in_vol
)
239 aw88081_dev_set_volume(aw_dev
, fade_in_vol
);
242 static void aw88081_dev_fade_out(struct aw_device
*aw_dev
)
244 struct aw_volume_desc
*desc
= &aw_dev
->volume_desc
;
245 int fade_step
= aw_dev
->fade_step
;
248 if (fade_step
== 0 || aw_dev
->fade_out_time
== 0) {
249 aw88081_dev_set_volume(aw_dev
, AW88081_MUTE_VOL
);
253 for (i
= desc
->ctl_volume
; i
<= AW88081_MUTE_VOL
; i
+= fade_step
) {
254 aw88081_dev_set_volume(aw_dev
, i
);
255 usleep_range(aw_dev
->fade_out_time
, aw_dev
->fade_out_time
+ 10);
258 if (i
!= AW88081_MUTE_VOL
)
259 aw88081_dev_set_volume(aw_dev
, AW88081_MUTE_VOL
);
262 static void aw88081_dev_mute(struct aw_device
*aw_dev
, bool is_mute
)
265 aw88081_dev_fade_out(aw_dev
);
266 regmap_update_bits(aw_dev
->regmap
, AW88081_SYSCTRL_REG
,
267 ~AW88081_HMUTE_MASK
, AW88081_HMUTE_ENABLE_VALUE
);
269 regmap_update_bits(aw_dev
->regmap
, AW88081_SYSCTRL_REG
,
270 ~AW88081_HMUTE_MASK
, AW88081_HMUTE_DISABLE_VALUE
);
271 aw88081_dev_fade_in(aw_dev
);
275 static void aw88081_dev_uls_hmute(struct aw_device
*aw_dev
, bool uls_hmute
)
278 regmap_update_bits(aw_dev
->regmap
, AW88081_SYSCTRL_REG
,
279 ~AW88081_ULS_HMUTE_MASK
,
280 AW88081_ULS_HMUTE_ENABLE_VALUE
);
282 regmap_update_bits(aw_dev
->regmap
, AW88081_SYSCTRL_REG
,
283 ~AW88081_ULS_HMUTE_MASK
,
284 AW88081_ULS_HMUTE_DISABLE_VALUE
);
287 static int aw88081_dev_reg_update(struct aw88081
*aw88081
,
288 unsigned char *data
, unsigned int len
)
290 struct aw_device
*aw_dev
= aw88081
->aw_pa
;
291 struct aw_volume_desc
*vol_desc
= &aw_dev
->volume_desc
;
292 unsigned int read_vol
;
293 int data_len
, i
, ret
;
299 dev_err(aw_dev
->dev
, "reg data is null or len is 0");
303 reg_data
= (int16_t *)data
;
306 if (data_len
& 0x1) {
307 dev_err(aw_dev
->dev
, "data len:%d unsupported", data_len
);
311 for (i
= 0; i
< data_len
; i
+= 2) {
312 reg_addr
= reg_data
[i
];
313 reg_val
= reg_data
[i
+ 1];
315 if (reg_addr
== AW88081_SYSCTRL_REG
) {
316 reg_val
&= ~(~AW88081_EN_PA_MASK
|
318 ~AW88081_HMUTE_MASK
|
319 ~AW88081_ULS_HMUTE_MASK
);
321 reg_val
|= AW88081_EN_PA_POWER_DOWN_VALUE
|
322 AW88081_PWDN_POWER_DOWN_VALUE
|
323 AW88081_HMUTE_ENABLE_VALUE
|
324 AW88081_ULS_HMUTE_ENABLE_VALUE
;
327 if (reg_addr
== AW88081_SYSCTRL2_REG
) {
328 read_vol
= (reg_val
& (~AW88081_VOL_MASK
)) >>
329 AW88081_VOL_START_BIT
;
330 aw_dev
->volume_desc
.init_volume
= read_vol
;
334 if (reg_addr
== AW88081_I2SCTRL3_REG
) {
336 reg_val
&= AW88081_I2STXEN_MASK
;
337 reg_val
|= AW88081_I2STXEN_DISABLE_VALUE
;
340 ret
= regmap_write(aw_dev
->regmap
, reg_addr
, reg_val
);
345 if (aw_dev
->prof_cur
!= aw_dev
->prof_index
)
346 vol_desc
->ctl_volume
= 0;
348 /* keep min volume */
349 aw88081_dev_set_volume(aw_dev
, vol_desc
->mute_volume
);
354 static int aw88081_dev_get_prof_name(struct aw_device
*aw_dev
, int index
, char **prof_name
)
356 struct aw_prof_info
*prof_info
= &aw_dev
->prof_info
;
357 struct aw_prof_desc
*prof_desc
;
359 if ((index
>= aw_dev
->prof_info
.count
) || (index
< 0)) {
360 dev_err(aw_dev
->dev
, "index[%d] overflow count[%d]",
361 index
, aw_dev
->prof_info
.count
);
365 prof_desc
= &aw_dev
->prof_info
.prof_desc
[index
];
367 *prof_name
= prof_info
->prof_name_list
[prof_desc
->id
];
372 static int aw88081_dev_get_prof_data(struct aw_device
*aw_dev
, int index
,
373 struct aw_prof_desc
**prof_desc
)
375 if ((index
>= aw_dev
->prof_info
.count
) || (index
< 0)) {
376 dev_err(aw_dev
->dev
, "%s: index[%d] overflow count[%d]\n",
377 __func__
, index
, aw_dev
->prof_info
.count
);
381 *prof_desc
= &aw_dev
->prof_info
.prof_desc
[index
];
386 static int aw88081_dev_fw_update(struct aw88081
*aw88081
)
388 struct aw_device
*aw_dev
= aw88081
->aw_pa
;
389 struct aw_prof_desc
*prof_index_desc
;
390 struct aw_sec_data_desc
*sec_desc
;
394 ret
= aw88081_dev_get_prof_name(aw_dev
, aw_dev
->prof_index
, &prof_name
);
396 dev_err(aw_dev
->dev
, "get prof name failed");
400 dev_dbg(aw_dev
->dev
, "start update %s", prof_name
);
402 ret
= aw88081_dev_get_prof_data(aw_dev
, aw_dev
->prof_index
, &prof_index_desc
);
407 sec_desc
= prof_index_desc
->sec_desc
;
408 ret
= aw88081_dev_reg_update(aw88081
, sec_desc
[AW88395_DATA_TYPE_REG
].data
,
409 sec_desc
[AW88395_DATA_TYPE_REG
].len
);
411 dev_err(aw_dev
->dev
, "update reg failed");
415 aw_dev
->prof_cur
= aw_dev
->prof_index
;
420 static int aw88081_dev_start(struct aw88081
*aw88081
)
422 struct aw_device
*aw_dev
= aw88081
->aw_pa
;
425 if (aw_dev
->status
== AW88081_DEV_PW_ON
) {
426 dev_dbg(aw_dev
->dev
, "already power on");
431 aw88081_dev_pwd(aw_dev
, false);
432 usleep_range(AW88081_2000_US
, AW88081_2000_US
+ 10);
434 ret
= aw88081_dev_check_syspll(aw_dev
);
436 dev_err(aw_dev
->dev
, "pll check failed cannot start");
441 aw88081_dev_amppd(aw_dev
, false);
442 usleep_range(AW88081_1000_US
, AW88081_1000_US
+ 50);
444 /* check i2s status */
445 ret
= aw88081_dev_check_sysst(aw_dev
);
447 dev_err(aw_dev
->dev
, "sysst check failed");
448 goto sysst_check_fail
;
451 /* enable tx feedback */
452 aw88081_dev_i2s_tx_enable(aw_dev
, true);
455 aw88081_dev_uls_hmute(aw_dev
, false);
458 aw88081_dev_mute(aw_dev
, false);
460 /* clear inturrupt */
461 aw88081_dev_clear_int_status(aw_dev
);
462 aw_dev
->status
= AW88081_DEV_PW_ON
;
467 aw88081_dev_i2s_tx_enable(aw_dev
, false);
468 aw88081_dev_clear_int_status(aw_dev
);
469 aw88081_dev_amppd(aw_dev
, true);
471 aw88081_dev_pwd(aw_dev
, true);
472 aw_dev
->status
= AW88081_DEV_PW_OFF
;
477 static int aw88081_dev_stop(struct aw_device
*aw_dev
)
479 if (aw_dev
->status
== AW88081_DEV_PW_OFF
) {
480 dev_dbg(aw_dev
->dev
, "already power off");
484 aw_dev
->status
= AW88081_DEV_PW_OFF
;
486 /* clear inturrupt */
487 aw88081_dev_clear_int_status(aw_dev
);
489 aw88081_dev_uls_hmute(aw_dev
, true);
491 aw88081_dev_mute(aw_dev
, true);
493 /* close tx feedback */
494 aw88081_dev_i2s_tx_enable(aw_dev
, false);
495 usleep_range(AW88081_1000_US
, AW88081_1000_US
+ 100);
498 aw88081_dev_amppd(aw_dev
, true);
501 aw88081_dev_pwd(aw_dev
, true);
506 static int aw88081_reg_update(struct aw88081
*aw88081
, bool force
)
508 struct aw_device
*aw_dev
= aw88081
->aw_pa
;
512 ret
= regmap_write(aw_dev
->regmap
,
513 AW88081_ID_REG
, AW88081_SOFT_RESET_VALUE
);
517 ret
= aw88081_dev_fw_update(aw88081
);
521 if (aw_dev
->prof_cur
!= aw_dev
->prof_index
) {
522 ret
= aw88081_dev_fw_update(aw88081
);
528 aw_dev
->prof_cur
= aw_dev
->prof_index
;
533 static void aw88081_start_pa(struct aw88081
*aw88081
)
537 for (i
= 0; i
< AW88081_START_RETRIES
; i
++) {
538 ret
= aw88081_reg_update(aw88081
, aw88081
->phase_sync
);
540 dev_err(aw88081
->aw_pa
->dev
, "fw update failed, cnt:%d\n", i
);
543 ret
= aw88081_dev_start(aw88081
);
545 dev_err(aw88081
->aw_pa
->dev
, "aw88081 device start failed. retry = %d", i
);
548 dev_dbg(aw88081
->aw_pa
->dev
, "start success\n");
554 static void aw88081_startup_work(struct work_struct
*work
)
556 struct aw88081
*aw88081
=
557 container_of(work
, struct aw88081
, start_work
.work
);
559 mutex_lock(&aw88081
->lock
);
560 aw88081_start_pa(aw88081
);
561 mutex_unlock(&aw88081
->lock
);
564 static void aw88081_start(struct aw88081
*aw88081
, bool sync_start
)
566 if (aw88081
->aw_pa
->fw_status
!= AW88081_DEV_FW_OK
)
569 if (aw88081
->aw_pa
->status
== AW88081_DEV_PW_ON
)
572 if (sync_start
== AW88081_SYNC_START
)
573 aw88081_start_pa(aw88081
);
575 queue_delayed_work(system_wq
,
576 &aw88081
->start_work
,
577 AW88081_START_WORK_DELAY_MS
);
580 static struct snd_soc_dai_driver aw88081_dai
[] = {
582 .name
= "aw88081-aif",
585 .stream_name
= "Speaker_Playback",
588 .rates
= AW88081_RATES
,
589 .formats
= AW88081_FORMATS
,
592 .stream_name
= "Speaker_Capture",
595 .rates
= AW88081_RATES
,
596 .formats
= AW88081_FORMATS
,
601 static int aw88081_get_fade_in_time(struct snd_kcontrol
*kcontrol
,
602 struct snd_ctl_elem_value
*ucontrol
)
604 struct snd_soc_component
*component
= snd_soc_kcontrol_component(kcontrol
);
605 struct aw88081
*aw88081
= snd_soc_component_get_drvdata(component
);
606 struct aw_device
*aw_dev
= aw88081
->aw_pa
;
608 ucontrol
->value
.integer
.value
[0] = aw_dev
->fade_in_time
;
613 static int aw88081_set_fade_in_time(struct snd_kcontrol
*kcontrol
,
614 struct snd_ctl_elem_value
*ucontrol
)
616 struct snd_soc_component
*component
= snd_soc_kcontrol_component(kcontrol
);
617 struct aw88081
*aw88081
= snd_soc_component_get_drvdata(component
);
618 struct soc_mixer_control
*mc
=
619 (struct soc_mixer_control
*)kcontrol
->private_value
;
620 struct aw_device
*aw_dev
= aw88081
->aw_pa
;
623 time
= ucontrol
->value
.integer
.value
[0];
625 if (time
< mc
->min
|| time
> mc
->max
)
628 if (time
!= aw_dev
->fade_in_time
) {
629 aw_dev
->fade_in_time
= time
;
636 static int aw88081_get_fade_out_time(struct snd_kcontrol
*kcontrol
,
637 struct snd_ctl_elem_value
*ucontrol
)
639 struct snd_soc_component
*component
= snd_soc_kcontrol_component(kcontrol
);
640 struct aw88081
*aw88081
= snd_soc_component_get_drvdata(component
);
641 struct aw_device
*aw_dev
= aw88081
->aw_pa
;
643 ucontrol
->value
.integer
.value
[0] = aw_dev
->fade_out_time
;
648 static int aw88081_set_fade_out_time(struct snd_kcontrol
*kcontrol
,
649 struct snd_ctl_elem_value
*ucontrol
)
651 struct snd_soc_component
*component
= snd_soc_kcontrol_component(kcontrol
);
652 struct aw88081
*aw88081
= snd_soc_component_get_drvdata(component
);
653 struct soc_mixer_control
*mc
=
654 (struct soc_mixer_control
*)kcontrol
->private_value
;
655 struct aw_device
*aw_dev
= aw88081
->aw_pa
;
658 time
= ucontrol
->value
.integer
.value
[0];
659 if (time
< mc
->min
|| time
> mc
->max
)
662 if (time
!= aw_dev
->fade_out_time
) {
663 aw_dev
->fade_out_time
= time
;
670 static int aw88081_dev_set_profile_index(struct aw_device
*aw_dev
, int index
)
672 /* check the index whether is valid */
673 if ((index
>= aw_dev
->prof_info
.count
) || (index
< 0))
675 /* check the index whether change */
676 if (aw_dev
->prof_index
== index
)
679 aw_dev
->prof_index
= index
;
684 static int aw88081_profile_info(struct snd_kcontrol
*kcontrol
,
685 struct snd_ctl_elem_info
*uinfo
)
687 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
688 struct aw88081
*aw88081
= snd_soc_component_get_drvdata(codec
);
692 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_ENUMERATED
;
695 count
= aw88081
->aw_pa
->prof_info
.count
;
697 uinfo
->value
.enumerated
.items
= 0;
701 uinfo
->value
.enumerated
.items
= count
;
703 if (uinfo
->value
.enumerated
.item
>= count
)
704 uinfo
->value
.enumerated
.item
= count
- 1;
706 count
= uinfo
->value
.enumerated
.item
;
708 ret
= aw88081_dev_get_prof_name(aw88081
->aw_pa
, count
, &prof_name
);
710 strscpy(uinfo
->value
.enumerated
.name
, "null",
711 sizeof(uinfo
->value
.enumerated
.name
));
715 strscpy(uinfo
->value
.enumerated
.name
, prof_name
, sizeof(uinfo
->value
.enumerated
.name
));
720 static int aw88081_profile_get(struct snd_kcontrol
*kcontrol
,
721 struct snd_ctl_elem_value
*ucontrol
)
723 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
724 struct aw88081
*aw88081
= snd_soc_component_get_drvdata(codec
);
726 ucontrol
->value
.integer
.value
[0] = aw88081
->aw_pa
->prof_index
;
731 static int aw88081_profile_set(struct snd_kcontrol
*kcontrol
,
732 struct snd_ctl_elem_value
*ucontrol
)
734 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
735 struct aw88081
*aw88081
= snd_soc_component_get_drvdata(codec
);
738 /* pa stop or stopping just set profile */
739 mutex_lock(&aw88081
->lock
);
740 ret
= aw88081_dev_set_profile_index(aw88081
->aw_pa
, ucontrol
->value
.integer
.value
[0]);
742 dev_dbg(codec
->dev
, "profile index does not change");
743 mutex_unlock(&aw88081
->lock
);
747 if (aw88081
->aw_pa
->status
) {
748 aw88081_dev_stop(aw88081
->aw_pa
);
749 aw88081_start(aw88081
, AW88081_SYNC_START
);
752 mutex_unlock(&aw88081
->lock
);
757 static int aw88081_volume_get(struct snd_kcontrol
*kcontrol
,
758 struct snd_ctl_elem_value
*ucontrol
)
760 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
761 struct aw88081
*aw88081
= snd_soc_component_get_drvdata(codec
);
762 struct aw_volume_desc
*vol_desc
= &aw88081
->aw_pa
->volume_desc
;
764 ucontrol
->value
.integer
.value
[0] = vol_desc
->ctl_volume
;
769 static int aw88081_volume_set(struct snd_kcontrol
*kcontrol
,
770 struct snd_ctl_elem_value
*ucontrol
)
772 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
773 struct aw88081
*aw88081
= snd_soc_component_get_drvdata(codec
);
774 struct aw_volume_desc
*vol_desc
= &aw88081
->aw_pa
->volume_desc
;
775 struct soc_mixer_control
*mc
=
776 (struct soc_mixer_control
*)kcontrol
->private_value
;
779 value
= ucontrol
->value
.integer
.value
[0];
781 if (value
< mc
->min
|| value
> mc
->max
)
784 if (vol_desc
->ctl_volume
!= value
) {
785 vol_desc
->ctl_volume
= value
;
786 aw88081_dev_set_volume(aw88081
->aw_pa
, vol_desc
->ctl_volume
);
793 static int aw88081_get_fade_step(struct snd_kcontrol
*kcontrol
,
794 struct snd_ctl_elem_value
*ucontrol
)
796 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
797 struct aw88081
*aw88081
= snd_soc_component_get_drvdata(codec
);
799 ucontrol
->value
.integer
.value
[0] = aw88081
->aw_pa
->fade_step
;
804 static int aw88081_set_fade_step(struct snd_kcontrol
*kcontrol
,
805 struct snd_ctl_elem_value
*ucontrol
)
807 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
808 struct aw88081
*aw88081
= snd_soc_component_get_drvdata(codec
);
809 struct soc_mixer_control
*mc
=
810 (struct soc_mixer_control
*)kcontrol
->private_value
;
813 value
= ucontrol
->value
.integer
.value
[0];
814 if (value
< mc
->min
|| value
> mc
->max
)
817 if (aw88081
->aw_pa
->fade_step
!= value
) {
818 aw88081
->aw_pa
->fade_step
= value
;
825 static const struct snd_kcontrol_new aw88081_controls
[] = {
826 SOC_SINGLE_EXT("PCM Playback Volume", AW88081_SYSCTRL2_REG
,
827 0, AW88081_MUTE_VOL
, 0, aw88081_volume_get
,
829 SOC_SINGLE_EXT("Fade Step", 0, 0, AW88081_MUTE_VOL
, 0,
830 aw88081_get_fade_step
, aw88081_set_fade_step
),
831 SOC_SINGLE_EXT("Volume Ramp Up Step", 0, 0, FADE_TIME_MAX
, 0,
832 aw88081_get_fade_in_time
, aw88081_set_fade_in_time
),
833 SOC_SINGLE_EXT("Volume Ramp Down Step", 0, 0, FADE_TIME_MAX
, 0,
834 aw88081_get_fade_out_time
, aw88081_set_fade_out_time
),
835 AW88081_PROFILE_EXT("Profile Set", aw88081_profile_info
,
836 aw88081_profile_get
, aw88081_profile_set
),
839 static void aw88081_parse_channel_dt(struct aw88081
*aw88081
)
841 struct aw_device
*aw_dev
= aw88081
->aw_pa
;
842 struct device_node
*np
= aw_dev
->dev
->of_node
;
843 u32 channel_value
= AW88081_DEV_DEFAULT_CH
;
845 of_property_read_u32(np
, "awinic,audio-channel", &channel_value
);
846 aw88081
->phase_sync
= of_property_read_bool(np
, "awinic,sync-flag");
848 aw_dev
->channel
= channel_value
;
851 static int aw88081_init(struct aw88081
*aw88081
, struct i2c_client
*i2c
, struct regmap
*regmap
)
853 struct aw_device
*aw_dev
;
854 unsigned int chip_id
;
858 ret
= regmap_read(regmap
, AW88081_ID_REG
, &chip_id
);
860 dev_err(&i2c
->dev
, "%s read chipid error. ret = %d", __func__
, ret
);
863 if (chip_id
!= AW88081_CHIP_ID
) {
864 dev_err(&i2c
->dev
, "unsupported device");
868 dev_dbg(&i2c
->dev
, "chip id = %x\n", chip_id
);
870 aw_dev
= devm_kzalloc(&i2c
->dev
, sizeof(*aw_dev
), GFP_KERNEL
);
874 aw88081
->aw_pa
= aw_dev
;
876 aw_dev
->regmap
= regmap
;
877 aw_dev
->dev
= &i2c
->dev
;
878 aw_dev
->chip_id
= AW88081_CHIP_ID
;
880 aw_dev
->prof_info
.prof_desc
= NULL
;
881 aw_dev
->prof_info
.prof_type
= AW88395_DEV_NONE_TYPE_ID
;
882 aw_dev
->fade_step
= AW88081_VOLUME_STEP_DB
;
883 aw_dev
->volume_desc
.mute_volume
= AW88081_MUTE_VOL
;
884 aw88081_parse_channel_dt(aw88081
);
889 static int aw88081_dev_init(struct aw88081
*aw88081
, struct aw_container
*aw_cfg
)
891 struct aw_device
*aw_dev
= aw88081
->aw_pa
;
894 ret
= aw88395_dev_cfg_load(aw_dev
, aw_cfg
);
896 dev_err(aw_dev
->dev
, "aw_dev acf parse failed");
900 ret
= regmap_write(aw_dev
->regmap
, AW88081_ID_REG
, AW88081_SOFT_RESET_VALUE
);
904 aw_dev
->fade_in_time
= AW88081_500_US
;
905 aw_dev
->fade_out_time
= AW88081_500_US
;
906 aw_dev
->prof_cur
= AW88081_INIT_PROFILE
;
907 aw_dev
->prof_index
= AW88081_INIT_PROFILE
;
909 ret
= aw88081_dev_fw_update(aw88081
);
911 dev_err(aw_dev
->dev
, "fw update failed ret = %d\n", ret
);
915 aw88081_dev_clear_int_status(aw_dev
);
917 aw88081_dev_uls_hmute(aw_dev
, true);
919 aw88081_dev_mute(aw_dev
, true);
921 usleep_range(AW88081_5000_US
, AW88081_5000_US
+ 10);
923 aw88081_dev_i2s_tx_enable(aw_dev
, false);
925 usleep_range(AW88081_1000_US
, AW88081_1000_US
+ 100);
927 aw88081_dev_amppd(aw_dev
, true);
929 aw88081_dev_pwd(aw_dev
, true);
934 static int aw88081_request_firmware_file(struct aw88081
*aw88081
)
936 const struct firmware
*cont
= NULL
;
939 aw88081
->aw_pa
->fw_status
= AW88081_DEV_FW_FAILED
;
941 ret
= request_firmware(&cont
, AW88081_ACF_FILE
, aw88081
->aw_pa
->dev
);
945 dev_dbg(aw88081
->aw_pa
->dev
, "loaded %s - size: %zu\n",
946 AW88081_ACF_FILE
, cont
? cont
->size
: 0);
948 aw88081
->aw_cfg
= devm_kzalloc(aw88081
->aw_pa
->dev
, cont
->size
+ sizeof(int), GFP_KERNEL
);
949 if (!aw88081
->aw_cfg
) {
950 release_firmware(cont
);
953 aw88081
->aw_cfg
->len
= (int)cont
->size
;
954 memcpy(aw88081
->aw_cfg
->data
, cont
->data
, cont
->size
);
955 release_firmware(cont
);
957 ret
= aw88395_dev_load_acf_check(aw88081
->aw_pa
, aw88081
->aw_cfg
);
961 mutex_lock(&aw88081
->lock
);
962 ret
= aw88081_dev_init(aw88081
, aw88081
->aw_cfg
);
963 mutex_unlock(&aw88081
->lock
);
968 static int aw88081_playback_event(struct snd_soc_dapm_widget
*w
,
969 struct snd_kcontrol
*k
, int event
)
971 struct snd_soc_component
*component
= snd_soc_dapm_to_component(w
->dapm
);
972 struct aw88081
*aw88081
= snd_soc_component_get_drvdata(component
);
974 mutex_lock(&aw88081
->lock
);
976 case SND_SOC_DAPM_PRE_PMU
:
977 aw88081_start(aw88081
, AW88081_ASYNC_START
);
979 case SND_SOC_DAPM_POST_PMD
:
980 aw88081_dev_stop(aw88081
->aw_pa
);
985 mutex_unlock(&aw88081
->lock
);
990 static const struct snd_soc_dapm_widget aw88081_dapm_widgets
[] = {
992 SND_SOC_DAPM_AIF_IN_E("AIF_RX", "Speaker_Playback", 0, SND_SOC_NOPM
, 0, 0,
993 aw88081_playback_event
,
994 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMD
),
995 SND_SOC_DAPM_OUTPUT("DAC Output"),
998 SND_SOC_DAPM_AIF_OUT("AIF_TX", "Speaker_Capture", 0, SND_SOC_NOPM
, 0, 0),
999 SND_SOC_DAPM_INPUT("ADC Input"),
1002 static const struct snd_soc_dapm_route aw88081_audio_map
[] = {
1003 {"DAC Output", NULL
, "AIF_RX"},
1004 {"AIF_TX", NULL
, "ADC Input"},
1007 static int aw88081_codec_probe(struct snd_soc_component
*component
)
1009 struct aw88081
*aw88081
= snd_soc_component_get_drvdata(component
);
1012 INIT_DELAYED_WORK(&aw88081
->start_work
, aw88081_startup_work
);
1014 ret
= aw88081_request_firmware_file(aw88081
);
1016 dev_err(aw88081
->aw_pa
->dev
, "%s: request firmware failed\n", __func__
);
1021 static void aw88081_codec_remove(struct snd_soc_component
*aw_codec
)
1023 struct aw88081
*aw88081
= snd_soc_component_get_drvdata(aw_codec
);
1025 cancel_delayed_work_sync(&aw88081
->start_work
);
1028 static const struct snd_soc_component_driver soc_codec_dev_aw88081
= {
1029 .probe
= aw88081_codec_probe
,
1030 .remove
= aw88081_codec_remove
,
1031 .dapm_widgets
= aw88081_dapm_widgets
,
1032 .num_dapm_widgets
= ARRAY_SIZE(aw88081_dapm_widgets
),
1033 .dapm_routes
= aw88081_audio_map
,
1034 .num_dapm_routes
= ARRAY_SIZE(aw88081_audio_map
),
1035 .controls
= aw88081_controls
,
1036 .num_controls
= ARRAY_SIZE(aw88081_controls
),
1039 static int aw88081_i2c_probe(struct i2c_client
*i2c
)
1041 struct aw88081
*aw88081
;
1044 ret
= i2c_check_functionality(i2c
->adapter
, I2C_FUNC_I2C
);
1046 return dev_err_probe(&i2c
->dev
, -ENXIO
, "check_functionality failed");
1048 aw88081
= devm_kzalloc(&i2c
->dev
, sizeof(*aw88081
), GFP_KERNEL
);
1052 mutex_init(&aw88081
->lock
);
1054 i2c_set_clientdata(i2c
, aw88081
);
1056 aw88081
->regmap
= devm_regmap_init_i2c(i2c
, &aw88081_regmap_config
);
1057 if (IS_ERR(aw88081
->regmap
))
1058 return dev_err_probe(&i2c
->dev
, PTR_ERR(aw88081
->regmap
),
1059 "failed to init regmap\n");
1062 ret
= aw88081_init(aw88081
, i2c
, aw88081
->regmap
);
1066 return devm_snd_soc_register_component(&i2c
->dev
,
1067 &soc_codec_dev_aw88081
,
1068 aw88081_dai
, ARRAY_SIZE(aw88081_dai
));
1071 static const struct i2c_device_id aw88081_i2c_id
[] = {
1072 { AW88081_I2C_NAME
},
1075 MODULE_DEVICE_TABLE(i2c
, aw88081_i2c_id
);
1077 static struct i2c_driver aw88081_i2c_driver
= {
1079 .name
= AW88081_I2C_NAME
,
1081 .probe
= aw88081_i2c_probe
,
1082 .id_table
= aw88081_i2c_id
,
1084 module_i2c_driver(aw88081_i2c_driver
);
1086 MODULE_DESCRIPTION("ASoC AW88081 Smart PA Driver");
1087 MODULE_LICENSE("GPL v2");