1 // SPDX-License-Identifier: GPL-2.0-only
3 // aw88261.c -- AW88261 ALSA SoC Audio driver
5 // Copyright (c) 2023 awinic Technology CO., LTD
11 #include <linux/i2c.h>
12 #include <linux/firmware.h>
13 #include <linux/regmap.h>
14 #include <sound/soc.h>
16 #include "aw88395/aw88395_data_type.h"
17 #include "aw88395/aw88395_device.h"
19 static const struct regmap_config aw88261_remap_config = {
22 .max_register = AW88261_REG_MAX,
23 .reg_format_endian = REGMAP_ENDIAN_LITTLE,
24 .val_format_endian = REGMAP_ENDIAN_BIG,
27 static void aw88261_dev_set_volume(struct aw_device *aw_dev, unsigned int value)
29 struct aw_volume_desc *vol_desc = &aw_dev->volume_desc;
30 unsigned int real_value, volume;
31 unsigned int reg_value;
33 volume = min((value + vol_desc->init_volume), (unsigned int)AW88261_MUTE_VOL);
34 real_value = DB_TO_REG_VAL(volume);
36 regmap_read(aw_dev->regmap, AW88261_SYSCTRL2_REG, ®_value);
38 real_value = (real_value | (reg_value & AW88261_VOL_START_MASK));
40 dev_dbg(aw_dev->dev, "value 0x%x , real_value:0x%x", value, real_value);
42 regmap_write(aw_dev->regmap, AW88261_SYSCTRL2_REG, real_value);
45 static void aw88261_dev_fade_in(struct aw_device *aw_dev)
47 struct aw_volume_desc *desc = &aw_dev->volume_desc;
48 int fade_in_vol = desc->ctl_volume;
49 int fade_step = aw_dev->fade_step;
52 if (fade_step == 0 || aw_dev->fade_in_time == 0) {
53 aw88261_dev_set_volume(aw_dev, fade_in_vol);
57 for (i = AW88261_MUTE_VOL; i >= fade_in_vol; i -= fade_step) {
58 aw88261_dev_set_volume(aw_dev, i);
59 usleep_range(aw_dev->fade_in_time,
60 aw_dev->fade_in_time + 10);
64 aw88261_dev_set_volume(aw_dev, fade_in_vol);
67 static void aw88261_dev_fade_out(struct aw_device *aw_dev)
69 struct aw_volume_desc *desc = &aw_dev->volume_desc;
70 int fade_step = aw_dev->fade_step;
73 if (fade_step == 0 || aw_dev->fade_out_time == 0) {
74 aw88261_dev_set_volume(aw_dev, AW88261_MUTE_VOL);
78 for (i = desc->ctl_volume; i <= AW88261_MUTE_VOL; i += fade_step) {
79 aw88261_dev_set_volume(aw_dev, i);
80 usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10);
83 if (i != AW88261_MUTE_VOL) {
84 aw88261_dev_set_volume(aw_dev, AW88261_MUTE_VOL);
85 usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10);
89 static void aw88261_dev_i2s_tx_enable(struct aw_device *aw_dev, bool flag)
92 regmap_update_bits(aw_dev->regmap, AW88261_I2SCFG1_REG,
93 ~AW88261_I2STXEN_MASK, AW88261_I2STXEN_ENABLE_VALUE);
95 regmap_update_bits(aw_dev->regmap, AW88261_I2SCFG1_REG,
96 ~AW88261_I2STXEN_MASK, AW88261_I2STXEN_DISABLE_VALUE);
99 static void aw88261_dev_pwd(struct aw_device *aw_dev, bool pwd)
102 regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
103 ~AW88261_PWDN_MASK, AW88261_PWDN_POWER_DOWN_VALUE);
105 regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
106 ~AW88261_PWDN_MASK, AW88261_PWDN_WORKING_VALUE);
109 static void aw88261_dev_amppd(struct aw_device *aw_dev, bool amppd)
112 regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
113 ~AW88261_AMPPD_MASK, AW88261_AMPPD_POWER_DOWN_VALUE);
115 regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
116 ~AW88261_AMPPD_MASK, AW88261_AMPPD_WORKING_VALUE);
119 static void aw88261_dev_mute(struct aw_device *aw_dev, bool is_mute)
122 aw88261_dev_fade_out(aw_dev);
123 regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
124 ~AW88261_HMUTE_MASK, AW88261_HMUTE_ENABLE_VALUE);
126 regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
127 ~AW88261_HMUTE_MASK, AW88261_HMUTE_DISABLE_VALUE);
128 aw88261_dev_fade_in(aw_dev);
132 static void aw88261_dev_clear_int_status(struct aw_device *aw_dev)
134 unsigned int int_status;
136 /* read int status and clear */
137 regmap_read(aw_dev->regmap, AW88261_SYSINT_REG, &int_status);
138 /* make sure int status is clear */
139 regmap_read(aw_dev->regmap, AW88261_SYSINT_REG, &int_status);
141 dev_dbg(aw_dev->dev, "read interrupt reg = 0x%04x", int_status);
144 static int aw88261_dev_get_iis_status(struct aw_device *aw_dev)
146 unsigned int reg_val;
149 ret = regmap_read(aw_dev->regmap, AW88261_SYSST_REG, ®_val);
152 if ((reg_val & AW88261_BIT_PLL_CHECK) != AW88261_BIT_PLL_CHECK) {
153 dev_err(aw_dev->dev, "check pll lock fail,reg_val:0x%04x", reg_val);
160 static int aw88261_dev_check_mode1_pll(struct aw_device *aw_dev)
164 for (i = 0; i < AW88261_DEV_SYSST_CHECK_MAX; i++) {
165 ret = aw88261_dev_get_iis_status(aw_dev);
167 dev_err(aw_dev->dev, "mode1 iis signal check error");
168 usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
177 static int aw88261_dev_check_mode2_pll(struct aw_device *aw_dev)
179 unsigned int reg_val;
182 ret = regmap_read(aw_dev->regmap, AW88261_PLLCTRL1_REG, ®_val);
186 reg_val &= (~AW88261_CCO_MUX_MASK);
187 if (reg_val == AW88261_CCO_MUX_DIVIDED_VALUE) {
188 dev_dbg(aw_dev->dev, "CCO_MUX is already divider");
193 ret = regmap_update_bits(aw_dev->regmap, AW88261_PLLCTRL1_REG,
194 ~AW88261_CCO_MUX_MASK, AW88261_CCO_MUX_DIVIDED_VALUE);
198 for (i = 0; i < AW88261_DEV_SYSST_CHECK_MAX; i++) {
199 ret = aw88261_dev_get_iis_status(aw_dev);
201 dev_err(aw_dev->dev, "mode2 iis signal check error");
202 usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
209 ret = regmap_update_bits(aw_dev->regmap, AW88261_PLLCTRL1_REG,
210 ~AW88261_CCO_MUX_MASK, AW88261_CCO_MUX_BYPASS_VALUE);
212 usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
213 for (i = 0; i < AW88261_DEV_SYSST_CHECK_MAX; i++) {
214 ret = aw88261_dev_check_mode1_pll(aw_dev);
216 dev_err(aw_dev->dev, "mode2 switch to mode1, iis signal check error");
217 usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
227 static int aw88261_dev_check_syspll(struct aw_device *aw_dev)
231 ret = aw88261_dev_check_mode1_pll(aw_dev);
233 dev_dbg(aw_dev->dev, "mode1 check iis failed try switch to mode2 check");
234 ret = aw88261_dev_check_mode2_pll(aw_dev);
236 dev_err(aw_dev->dev, "mode2 check iis failed");
244 static int aw88261_dev_check_sysst(struct aw_device *aw_dev)
246 unsigned int check_val;
247 unsigned int reg_val;
250 for (i = 0; i < AW88261_DEV_SYSST_CHECK_MAX; i++) {
251 ret = regmap_read(aw_dev->regmap, AW88261_SYSST_REG, ®_val);
255 check_val = reg_val & (~AW88261_BIT_SYSST_CHECK_MASK)
256 & AW88261_BIT_SYSST_CHECK;
257 if (check_val != AW88261_BIT_SYSST_CHECK) {
258 dev_err(aw_dev->dev, "check sysst fail, reg_val=0x%04x, check:0x%x",
259 reg_val, AW88261_BIT_SYSST_CHECK);
260 usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
269 static void aw88261_dev_uls_hmute(struct aw_device *aw_dev, bool uls_hmute)
272 regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
273 ~AW88261_ULS_HMUTE_MASK,
274 AW88261_ULS_HMUTE_ENABLE_VALUE);
276 regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
277 ~AW88261_ULS_HMUTE_MASK,
278 AW88261_ULS_HMUTE_DISABLE_VALUE);
281 static void aw88261_reg_force_set(struct aw88261 *aw88261)
283 if (aw88261->frcset_en == AW88261_FRCSET_ENABLE) {
285 regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL3_REG,
286 AW88261_FORCE_PWM_MASK, AW88261_FORCE_PWM_FORCEMINUS_PWM_VALUE);
287 /* set BOOST_OS_WIDTH */
288 regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL5_REG,
289 AW88261_BST_OS_WIDTH_MASK, AW88261_BST_OS_WIDTH_50NS_VALUE);
290 /* set BURST_LOOPR */
291 regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL6_REG,
292 AW88261_BST_LOOPR_MASK, AW88261_BST_LOOPR_340K_VALUE);
294 regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL7_REG,
295 AW88261_RSQN_DLY_MASK, AW88261_RSQN_DLY_35NS_VALUE);
296 /* set BURST_SSMODE */
297 regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL8_REG,
298 AW88261_BURST_SSMODE_MASK, AW88261_BURST_SSMODE_FAST_VALUE);
300 regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL9_REG,
301 AW88261_BST_BURST_MASK, AW88261_BST_BURST_30MA_VALUE);
303 dev_dbg(aw88261->aw_pa->dev, "needn't set reg value");
307 static int aw88261_dev_get_icalk(struct aw_device *aw_dev, int16_t *icalk)
309 u16 reg_icalk, reg_icalkl;
310 unsigned int reg_val;
313 ret = regmap_read(aw_dev->regmap, AW88261_EFRH4_REG, ®_val);
317 reg_icalk = reg_val & (~AW88261_EF_ISN_GESLP_H_MASK);
319 ret = regmap_read(aw_dev->regmap, AW88261_EFRL4_REG, ®_val);
323 reg_icalkl = reg_val & (~AW88261_EF_ISN_GESLP_L_MASK);
325 reg_icalk = (reg_icalk >> AW88261_ICALK_SHIFT) & (reg_icalkl >> AW88261_ICALKL_SHIFT);
327 if (reg_icalk & (~AW88261_EF_ISN_GESLP_SIGN_MASK))
328 reg_icalk = reg_icalk | ~AW88261_EF_ISN_GESLP_NEG;
330 *icalk = (int16_t)reg_icalk;
335 static int aw88261_dev_get_vcalk(struct aw_device *aw_dev, int16_t *vcalk)
337 u16 reg_vcalk, reg_vcalkl;
338 unsigned int reg_val;
341 ret = regmap_read(aw_dev->regmap, AW88261_EFRH3_REG, ®_val);
345 reg_vcalk = (u16)reg_val & (~AW88261_EF_VSN_GESLP_H_MASK);
347 ret = regmap_read(aw_dev->regmap, AW88261_EFRL3_REG, ®_val);
351 reg_vcalkl = (u16)reg_val & (~AW88261_EF_VSN_GESLP_L_MASK);
353 reg_vcalk = (reg_vcalk >> AW88261_VCALK_SHIFT) & (reg_vcalkl >> AW88261_VCALKL_SHIFT);
355 if (reg_vcalk & AW88261_EF_VSN_GESLP_SIGN_MASK)
356 reg_vcalk = reg_vcalk | (~AW88261_EF_VSN_GESLP_NEG);
357 *vcalk = (int16_t)reg_vcalk;
362 static int aw88261_dev_set_vcalb(struct aw_device *aw_dev)
364 int16_t icalk_val, vcalk_val;
365 int icalk, vcalk, vcalb;
369 ret = aw88261_dev_get_icalk(aw_dev, &icalk_val);
373 ret = aw88261_dev_get_vcalk(aw_dev, &vcalk_val);
377 icalk = AW88261_CABL_BASE_VALUE + AW88261_ICABLK_FACTOR * icalk_val;
378 vcalk = AW88261_CABL_BASE_VALUE + AW88261_VCABLK_FACTOR * vcalk_val;
382 vcalb = AW88261_VCAL_FACTOR * icalk / vcalk;
383 reg_val = (unsigned int)vcalb;
385 dev_dbg(aw_dev->dev, "icalk=%d, vcalk=%d, vcalb=%d, reg_val=0x%04x",
386 icalk, vcalk, vcalb, reg_val);
387 ret = regmap_write(aw_dev->regmap, AW88261_VSNTM1_REG, reg_val);
392 static int aw88261_dev_reg_update(struct aw88261 *aw88261,
393 unsigned char *data, unsigned int len)
395 struct aw_device *aw_dev = aw88261->aw_pa;
396 struct aw_volume_desc *vol_desc = &aw_dev->volume_desc;
397 unsigned int read_val, efcheck_val, read_vol;
398 int data_len, i, ret;
404 dev_err(aw_dev->dev, "reg data is null or len is 0");
408 reg_data = (int16_t *)data;
411 if (data_len & 0x1) {
412 dev_err(aw_dev->dev, "data len:%d unsupported", data_len);
416 for (i = 0; i < data_len; i += 2) {
417 reg_addr = reg_data[i];
418 reg_val = reg_data[i + 1];
420 if (reg_addr == AW88261_SYSCTRL_REG) {
421 aw88261->amppd_st = reg_val & (~AW88261_AMPPD_MASK);
422 ret = regmap_read(aw_dev->regmap, reg_addr, &read_val);
426 read_val &= (~AW88261_AMPPD_MASK) | (~AW88261_PWDN_MASK) |
427 (~AW88261_HMUTE_MASK);
428 reg_val &= (AW88261_AMPPD_MASK | AW88261_PWDN_MASK | AW88261_HMUTE_MASK);
431 /* enable uls hmute */
432 reg_val &= AW88261_ULS_HMUTE_MASK;
433 reg_val |= AW88261_ULS_HMUTE_ENABLE_VALUE;
436 if (reg_addr == AW88261_DBGCTRL_REG) {
437 efcheck_val = reg_val & (~AW88261_EF_DBMD_MASK);
438 if (efcheck_val == AW88261_OR_VALUE)
439 aw88261->efuse_check = AW88261_EF_OR_CHECK;
441 aw88261->efuse_check = AW88261_EF_AND_CHECK;
445 if (reg_addr == AW88261_I2SCTRL3_REG) {
447 reg_val &= AW88261_I2STXEN_MASK;
448 reg_val |= AW88261_I2STXEN_DISABLE_VALUE;
451 if (reg_addr == AW88261_SYSCTRL2_REG) {
452 read_vol = (reg_val & (~AW88261_VOL_MASK)) >>
453 AW88261_VOL_START_BIT;
454 aw_dev->volume_desc.init_volume =
455 REG_VAL_TO_DB(read_vol);
458 if (reg_addr == AW88261_VSNTM1_REG)
461 ret = regmap_write(aw_dev->regmap, reg_addr, reg_val);
466 ret = aw88261_dev_set_vcalb(aw_dev);
470 if (aw_dev->prof_cur != aw_dev->prof_index)
471 vol_desc->ctl_volume = 0;
473 /* keep min volume */
474 aw88261_dev_set_volume(aw_dev, vol_desc->mute_volume);
479 static int aw88261_dev_get_prof_name(struct aw_device *aw_dev, int index, char **prof_name)
481 struct aw_prof_info *prof_info = &aw_dev->prof_info;
482 struct aw_prof_desc *prof_desc;
484 if ((index >= aw_dev->prof_info.count) || (index < 0)) {
485 dev_err(aw_dev->dev, "index[%d] overflow count[%d]",
486 index, aw_dev->prof_info.count);
490 prof_desc = &aw_dev->prof_info.prof_desc[index];
492 *prof_name = prof_info->prof_name_list[prof_desc->id];
497 static int aw88261_dev_get_prof_data(struct aw_device *aw_dev, int index,
498 struct aw_prof_desc **prof_desc)
500 if ((index >= aw_dev->prof_info.count) || (index < 0)) {
501 dev_err(aw_dev->dev, "%s: index[%d] overflow count[%d]\n",
502 __func__, index, aw_dev->prof_info.count);
506 *prof_desc = &aw_dev->prof_info.prof_desc[index];
511 static int aw88261_dev_fw_update(struct aw88261 *aw88261)
513 struct aw_device *aw_dev = aw88261->aw_pa;
514 struct aw_prof_desc *prof_index_desc;
515 struct aw_sec_data_desc *sec_desc;
519 ret = aw88261_dev_get_prof_name(aw_dev, aw_dev->prof_index, &prof_name);
521 dev_err(aw_dev->dev, "get prof name failed");
525 dev_dbg(aw_dev->dev, "start update %s", prof_name);
527 ret = aw88261_dev_get_prof_data(aw_dev, aw_dev->prof_index, &prof_index_desc);
532 sec_desc = prof_index_desc->sec_desc;
533 ret = aw88261_dev_reg_update(aw88261, sec_desc[AW88395_DATA_TYPE_REG].data,
534 sec_desc[AW88395_DATA_TYPE_REG].len);
536 dev_err(aw_dev->dev, "update reg failed");
540 aw_dev->prof_cur = aw_dev->prof_index;
545 static int aw88261_dev_start(struct aw88261 *aw88261)
547 struct aw_device *aw_dev = aw88261->aw_pa;
550 if (aw_dev->status == AW88261_DEV_PW_ON) {
551 dev_info(aw_dev->dev, "already power on");
556 aw88261_dev_pwd(aw_dev, false);
557 usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
559 ret = aw88261_dev_check_syspll(aw_dev);
561 dev_err(aw_dev->dev, "pll check failed cannot start");
566 aw88261_dev_amppd(aw_dev, false);
567 usleep_range(AW88261_1000_US, AW88261_1000_US + 50);
569 /* check i2s status */
570 ret = aw88261_dev_check_sysst(aw_dev);
572 dev_err(aw_dev->dev, "sysst check failed");
573 goto sysst_check_fail;
576 /* enable tx feedback */
577 aw88261_dev_i2s_tx_enable(aw_dev, true);
579 if (aw88261->amppd_st)
580 aw88261_dev_amppd(aw_dev, true);
582 aw88261_reg_force_set(aw88261);
585 aw88261_dev_uls_hmute(aw_dev, false);
588 if (!aw88261->mute_st)
589 aw88261_dev_mute(aw_dev, false);
591 /* clear inturrupt */
592 aw88261_dev_clear_int_status(aw_dev);
593 aw_dev->status = AW88261_DEV_PW_ON;
598 aw88261_dev_i2s_tx_enable(aw_dev, false);
599 aw88261_dev_clear_int_status(aw_dev);
600 aw88261_dev_amppd(aw_dev, true);
602 aw88261_dev_pwd(aw_dev, true);
603 aw_dev->status = AW88261_DEV_PW_OFF;
608 static int aw88261_dev_stop(struct aw_device *aw_dev)
610 if (aw_dev->status == AW88261_DEV_PW_OFF) {
611 dev_info(aw_dev->dev, "already power off");
615 aw_dev->status = AW88261_DEV_PW_OFF;
617 /* clear inturrupt */
618 aw88261_dev_clear_int_status(aw_dev);
620 aw88261_dev_uls_hmute(aw_dev, true);
622 aw88261_dev_mute(aw_dev, true);
624 /* close tx feedback */
625 aw88261_dev_i2s_tx_enable(aw_dev, false);
626 usleep_range(AW88261_1000_US, AW88261_1000_US + 100);
629 aw88261_dev_amppd(aw_dev, true);
632 aw88261_dev_pwd(aw_dev, true);
637 static int aw88261_reg_update(struct aw88261 *aw88261, bool force)
639 struct aw_device *aw_dev = aw88261->aw_pa;
643 ret = regmap_write(aw_dev->regmap,
644 AW88261_ID_REG, AW88261_SOFT_RESET_VALUE);
648 ret = aw88261_dev_fw_update(aw88261);
652 if (aw_dev->prof_cur != aw_dev->prof_index) {
653 ret = aw88261_dev_fw_update(aw88261);
661 aw_dev->prof_cur = aw_dev->prof_index;
666 static void aw88261_start_pa(struct aw88261 *aw88261)
670 for (i = 0; i < AW88261_START_RETRIES; i++) {
671 ret = aw88261_reg_update(aw88261, aw88261->phase_sync);
673 dev_err(aw88261->aw_pa->dev, "fw update failed, cnt:%d\n", i);
676 ret = aw88261_dev_start(aw88261);
678 dev_err(aw88261->aw_pa->dev, "aw88261 device start failed. retry = %d", i);
681 dev_info(aw88261->aw_pa->dev, "start success\n");
687 static void aw88261_startup_work(struct work_struct *work)
689 struct aw88261 *aw88261 =
690 container_of(work, struct aw88261, start_work.work);
692 mutex_lock(&aw88261->lock);
693 aw88261_start_pa(aw88261);
694 mutex_unlock(&aw88261->lock);
697 static void aw88261_start(struct aw88261 *aw88261, bool sync_start)
699 if (aw88261->aw_pa->fw_status != AW88261_DEV_FW_OK)
702 if (aw88261->aw_pa->status == AW88261_DEV_PW_ON)
705 if (sync_start == AW88261_SYNC_START)
706 aw88261_start_pa(aw88261);
708 queue_delayed_work(system_wq,
709 &aw88261->start_work,
710 AW88261_START_WORK_DELAY_MS);
713 static struct snd_soc_dai_driver aw88261_dai[] = {
715 .name = "aw88261-aif",
718 .stream_name = "Speaker_Playback",
721 .rates = AW88261_RATES,
722 .formats = AW88261_FORMATS,
725 .stream_name = "Speaker_Capture",
728 .rates = AW88261_RATES,
729 .formats = AW88261_FORMATS,
734 static int aw88261_get_fade_in_time(struct snd_kcontrol *kcontrol,
735 struct snd_ctl_elem_value *ucontrol)
737 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
738 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
739 struct aw_device *aw_dev = aw88261->aw_pa;
741 ucontrol->value.integer.value[0] = aw_dev->fade_in_time;
746 static int aw88261_set_fade_in_time(struct snd_kcontrol *kcontrol,
747 struct snd_ctl_elem_value *ucontrol)
749 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
750 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
751 struct soc_mixer_control *mc =
752 (struct soc_mixer_control *)kcontrol->private_value;
753 struct aw_device *aw_dev = aw88261->aw_pa;
756 time = ucontrol->value.integer.value[0];
758 if (time < mc->min || time > mc->max)
761 if (time != aw_dev->fade_in_time) {
762 aw_dev->fade_in_time = time;
769 static int aw88261_get_fade_out_time(struct snd_kcontrol *kcontrol,
770 struct snd_ctl_elem_value *ucontrol)
772 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
773 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
774 struct aw_device *aw_dev = aw88261->aw_pa;
776 ucontrol->value.integer.value[0] = aw_dev->fade_out_time;
781 static int aw88261_set_fade_out_time(struct snd_kcontrol *kcontrol,
782 struct snd_ctl_elem_value *ucontrol)
784 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
785 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
786 struct soc_mixer_control *mc =
787 (struct soc_mixer_control *)kcontrol->private_value;
788 struct aw_device *aw_dev = aw88261->aw_pa;
791 time = ucontrol->value.integer.value[0];
792 if (time < mc->min || time > mc->max)
795 if (time != aw_dev->fade_out_time) {
796 aw_dev->fade_out_time = time;
803 static int aw88261_dev_set_profile_index(struct aw_device *aw_dev, int index)
805 /* check the index whether is valid */
806 if ((index >= aw_dev->prof_info.count) || (index < 0))
808 /* check the index whether change */
809 if (aw_dev->prof_index == index)
812 aw_dev->prof_index = index;
817 static int aw88261_profile_info(struct snd_kcontrol *kcontrol,
818 struct snd_ctl_elem_info *uinfo)
820 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
821 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
822 char *prof_name, *name;
825 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
828 count = aw88261->aw_pa->prof_info.count;
830 uinfo->value.enumerated.items = 0;
834 uinfo->value.enumerated.items = count;
836 if (uinfo->value.enumerated.item >= count)
837 uinfo->value.enumerated.item = count - 1;
839 name = uinfo->value.enumerated.name;
840 count = uinfo->value.enumerated.item;
842 ret = aw88261_dev_get_prof_name(aw88261->aw_pa, count, &prof_name);
844 strscpy(uinfo->value.enumerated.name, "null",
849 strscpy(name, prof_name, sizeof(uinfo->value.enumerated.name));
854 static int aw88261_profile_get(struct snd_kcontrol *kcontrol,
855 struct snd_ctl_elem_value *ucontrol)
857 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
858 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
860 ucontrol->value.integer.value[0] = aw88261->aw_pa->prof_index;
865 static int aw88261_profile_set(struct snd_kcontrol *kcontrol,
866 struct snd_ctl_elem_value *ucontrol)
868 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
869 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
872 /* pa stop or stopping just set profile */
873 mutex_lock(&aw88261->lock);
874 ret = aw88261_dev_set_profile_index(aw88261->aw_pa, ucontrol->value.integer.value[0]);
876 dev_dbg(codec->dev, "profile index does not change");
877 mutex_unlock(&aw88261->lock);
881 if (aw88261->aw_pa->status) {
882 aw88261_dev_stop(aw88261->aw_pa);
883 aw88261_start(aw88261, AW88261_SYNC_START);
886 mutex_unlock(&aw88261->lock);
891 static int aw88261_volume_get(struct snd_kcontrol *kcontrol,
892 struct snd_ctl_elem_value *ucontrol)
894 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
895 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
896 struct aw_volume_desc *vol_desc = &aw88261->aw_pa->volume_desc;
898 ucontrol->value.integer.value[0] = vol_desc->ctl_volume;
903 static int aw88261_volume_set(struct snd_kcontrol *kcontrol,
904 struct snd_ctl_elem_value *ucontrol)
906 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
907 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
908 struct aw_volume_desc *vol_desc = &aw88261->aw_pa->volume_desc;
909 struct soc_mixer_control *mc =
910 (struct soc_mixer_control *)kcontrol->private_value;
913 value = ucontrol->value.integer.value[0];
915 if (value < mc->min || value > mc->max)
918 if (vol_desc->ctl_volume != value) {
919 vol_desc->ctl_volume = value;
920 aw88261_dev_set_volume(aw88261->aw_pa, vol_desc->ctl_volume);
928 static int aw88261_get_fade_step(struct snd_kcontrol *kcontrol,
929 struct snd_ctl_elem_value *ucontrol)
931 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
932 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
934 ucontrol->value.integer.value[0] = aw88261->aw_pa->fade_step;
939 static int aw88261_set_fade_step(struct snd_kcontrol *kcontrol,
940 struct snd_ctl_elem_value *ucontrol)
942 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
943 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
944 struct soc_mixer_control *mc =
945 (struct soc_mixer_control *)kcontrol->private_value;
948 value = ucontrol->value.integer.value[0];
949 if (value < mc->min || value > mc->max)
952 if (aw88261->aw_pa->fade_step != value) {
953 aw88261->aw_pa->fade_step = value;
960 static const struct snd_kcontrol_new aw88261_controls[] = {
961 SOC_SINGLE_EXT("PCM Playback Volume", AW88261_SYSCTRL2_REG,
962 6, AW88261_MUTE_VOL, 0, aw88261_volume_get,
964 SOC_SINGLE_EXT("Fade Step", 0, 0, AW88261_MUTE_VOL, 0,
965 aw88261_get_fade_step, aw88261_set_fade_step),
966 SOC_SINGLE_EXT("Volume Ramp Up Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
967 aw88261_get_fade_in_time, aw88261_set_fade_in_time),
968 SOC_SINGLE_EXT("Volume Ramp Down Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
969 aw88261_get_fade_out_time, aw88261_set_fade_out_time),
970 AW88261_PROFILE_EXT("Profile Set", aw88261_profile_info,
971 aw88261_profile_get, aw88261_profile_set),
974 static int aw88261_playback_event(struct snd_soc_dapm_widget *w,
975 struct snd_kcontrol *k, int event)
977 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
978 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
980 mutex_lock(&aw88261->lock);
982 case SND_SOC_DAPM_PRE_PMU:
983 aw88261_start(aw88261, AW88261_ASYNC_START);
985 case SND_SOC_DAPM_POST_PMD:
986 aw88261_dev_stop(aw88261->aw_pa);
991 mutex_unlock(&aw88261->lock);
996 static const struct snd_soc_dapm_widget aw88261_dapm_widgets[] = {
998 SND_SOC_DAPM_AIF_IN_E("AIF_RX", "Speaker_Playback", 0, 0, 0, 0,
999 aw88261_playback_event,
1000 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1001 SND_SOC_DAPM_OUTPUT("DAC Output"),
1004 SND_SOC_DAPM_AIF_OUT("AIF_TX", "Speaker_Capture", 0, SND_SOC_NOPM, 0, 0),
1005 SND_SOC_DAPM_INPUT("ADC Input"),
1008 static const struct snd_soc_dapm_route aw88261_audio_map[] = {
1009 {"DAC Output", NULL, "AIF_RX"},
1010 {"AIF_TX", NULL, "ADC Input"},
1013 static int aw88261_frcset_check(struct aw88261 *aw88261)
1015 unsigned int reg_val;
1016 u16 temh, teml, tem;
1019 ret = regmap_read(aw88261->regmap, AW88261_EFRH3_REG, ®_val);
1022 temh = ((u16)reg_val & (~AW88261_TEMH_MASK));
1024 ret = regmap_read(aw88261->regmap, AW88261_EFRL3_REG, ®_val);
1027 teml = ((u16)reg_val & (~AW88261_TEML_MASK));
1029 if (aw88261->efuse_check == AW88261_EF_OR_CHECK)
1030 tem = (temh | teml);
1032 tem = (temh & teml);
1034 if (tem == AW88261_DEFAULT_CFG)
1035 aw88261->frcset_en = AW88261_FRCSET_ENABLE;
1037 aw88261->frcset_en = AW88261_FRCSET_DISABLE;
1039 dev_dbg(aw88261->aw_pa->dev, "tem is 0x%04x, frcset_en is %d",
1040 tem, aw88261->frcset_en);
1045 static int aw88261_dev_init(struct aw88261 *aw88261, struct aw_container *aw_cfg)
1047 struct aw_device *aw_dev = aw88261->aw_pa;
1050 ret = aw88395_dev_cfg_load(aw_dev, aw_cfg);
1052 dev_err(aw_dev->dev, "aw_dev acf parse failed");
1056 ret = regmap_write(aw_dev->regmap, AW88261_ID_REG, AW88261_SOFT_RESET_VALUE);
1060 aw_dev->fade_in_time = AW88261_500_US;
1061 aw_dev->fade_out_time = AW88261_500_US;
1062 aw_dev->prof_cur = AW88261_INIT_PROFILE;
1063 aw_dev->prof_index = AW88261_INIT_PROFILE;
1065 ret = aw88261_dev_fw_update(aw88261);
1067 dev_err(aw_dev->dev, "fw update failed ret = %d\n", ret);
1071 ret = aw88261_frcset_check(aw88261);
1073 dev_err(aw_dev->dev, "aw88261_frcset_check ret = %d\n", ret);
1077 aw88261_dev_clear_int_status(aw_dev);
1079 aw88261_dev_uls_hmute(aw_dev, true);
1081 aw88261_dev_mute(aw_dev, true);
1083 aw88261_dev_i2s_tx_enable(aw_dev, false);
1085 usleep_range(AW88261_1000_US, AW88261_1000_US + 100);
1087 aw88261_dev_amppd(aw_dev, true);
1089 aw88261_dev_pwd(aw_dev, true);
1094 static int aw88261_request_firmware_file(struct aw88261 *aw88261)
1096 const struct firmware *cont = NULL;
1099 aw88261->aw_pa->fw_status = AW88261_DEV_FW_FAILED;
1101 ret = request_firmware(&cont, AW88261_ACF_FILE, aw88261->aw_pa->dev);
1103 return dev_err_probe(aw88261->aw_pa->dev, ret,
1104 "load [%s] failed!", AW88261_ACF_FILE);
1106 dev_info(aw88261->aw_pa->dev, "loaded %s - size: %zu\n",
1107 AW88261_ACF_FILE, cont ? cont->size : 0);
1109 aw88261->aw_cfg = devm_kzalloc(aw88261->aw_pa->dev, cont->size + sizeof(int), GFP_KERNEL);
1110 if (!aw88261->aw_cfg) {
1111 release_firmware(cont);
1114 aw88261->aw_cfg->len = (int)cont->size;
1115 memcpy(aw88261->aw_cfg->data, cont->data, cont->size);
1116 release_firmware(cont);
1118 ret = aw88395_dev_load_acf_check(aw88261->aw_pa, aw88261->aw_cfg);
1120 dev_err(aw88261->aw_pa->dev, "load [%s] failed !", AW88261_ACF_FILE);
1124 mutex_lock(&aw88261->lock);
1125 /* aw device init */
1126 ret = aw88261_dev_init(aw88261, aw88261->aw_cfg);
1128 dev_err(aw88261->aw_pa->dev, "dev init failed");
1129 mutex_unlock(&aw88261->lock);
1134 static int aw88261_codec_probe(struct snd_soc_component *component)
1136 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
1137 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
1140 INIT_DELAYED_WORK(&aw88261->start_work, aw88261_startup_work);
1142 ret = aw88261_request_firmware_file(aw88261);
1144 return dev_err_probe(aw88261->aw_pa->dev, ret,
1145 "aw88261_request_firmware_file failed\n");
1148 ret = snd_soc_dapm_new_controls(dapm, aw88261_dapm_widgets,
1149 ARRAY_SIZE(aw88261_dapm_widgets));
1154 ret = snd_soc_dapm_add_routes(dapm, aw88261_audio_map,
1155 ARRAY_SIZE(aw88261_audio_map));
1159 ret = snd_soc_add_component_controls(component, aw88261_controls,
1160 ARRAY_SIZE(aw88261_controls));
1165 static void aw88261_codec_remove(struct snd_soc_component *aw_codec)
1167 struct aw88261 *aw88261 = snd_soc_component_get_drvdata(aw_codec);
1169 cancel_delayed_work_sync(&aw88261->start_work);
1172 static const struct snd_soc_component_driver soc_codec_dev_aw88261 = {
1173 .probe = aw88261_codec_probe,
1174 .remove = aw88261_codec_remove,
1177 static void aw88261_parse_channel_dt(struct aw88261 *aw88261)
1179 struct aw_device *aw_dev = aw88261->aw_pa;
1180 struct device_node *np = aw_dev->dev->of_node;
1181 u32 channel_value = AW88261_DEV_DEFAULT_CH;
1183 of_property_read_u32(np, "awinic,audio-channel", &channel_value);
1184 aw88261->phase_sync = of_property_read_bool(np, "awinic,sync-flag");
1186 aw_dev->channel = channel_value;
1189 static int aw88261_init(struct aw88261 **aw88261, struct i2c_client *i2c, struct regmap *regmap)
1191 struct aw_device *aw_dev;
1192 unsigned int chip_id;
1196 ret = regmap_read(regmap, AW88261_ID_REG, &chip_id);
1198 dev_err(&i2c->dev, "%s read chipid error. ret = %d", __func__, ret);
1201 if (chip_id != AW88261_CHIP_ID) {
1202 dev_err(&i2c->dev, "unsupported device");
1206 dev_info(&i2c->dev, "chip id = %x\n", chip_id);
1208 aw_dev = devm_kzalloc(&i2c->dev, sizeof(*aw_dev), GFP_KERNEL);
1212 (*aw88261)->aw_pa = aw_dev;
1214 aw_dev->regmap = regmap;
1215 aw_dev->dev = &i2c->dev;
1216 aw_dev->chip_id = AW88261_CHIP_ID;
1218 aw_dev->prof_info.prof_desc = NULL;
1219 aw_dev->prof_info.count = 0;
1220 aw_dev->prof_info.prof_type = AW88395_DEV_NONE_TYPE_ID;
1221 aw_dev->channel = 0;
1222 aw_dev->fw_status = AW88261_DEV_FW_FAILED;
1223 aw_dev->fade_step = AW88261_VOLUME_STEP_DB;
1224 aw_dev->volume_desc.ctl_volume = AW88261_VOL_DEFAULT_VALUE;
1225 aw_dev->volume_desc.mute_volume = AW88261_MUTE_VOL;
1226 aw88261_parse_channel_dt(*aw88261);
1231 static int aw88261_i2c_probe(struct i2c_client *i2c)
1233 struct aw88261 *aw88261;
1236 ret = i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C);
1238 return dev_err_probe(&i2c->dev, -ENXIO, "check_functionality failed");
1240 aw88261 = devm_kzalloc(&i2c->dev, sizeof(*aw88261), GFP_KERNEL);
1244 mutex_init(&aw88261->lock);
1246 i2c_set_clientdata(i2c, aw88261);
1248 aw88261->regmap = devm_regmap_init_i2c(i2c, &aw88261_remap_config);
1249 if (IS_ERR(aw88261->regmap)) {
1250 ret = PTR_ERR(aw88261->regmap);
1251 return dev_err_probe(&i2c->dev, ret, "failed to init regmap: %d\n", ret);
1255 ret = aw88261_init(&aw88261, i2c, aw88261->regmap);
1259 ret = devm_snd_soc_register_component(&i2c->dev,
1260 &soc_codec_dev_aw88261,
1261 aw88261_dai, ARRAY_SIZE(aw88261_dai));
1263 dev_err(&i2c->dev, "failed to register aw88261: %d", ret);
1268 static const struct i2c_device_id aw88261_i2c_id[] = {
1269 { AW88261_I2C_NAME },
1272 MODULE_DEVICE_TABLE(i2c, aw88261_i2c_id);
1274 static struct i2c_driver aw88261_i2c_driver = {
1276 .name = AW88261_I2C_NAME,
1278 .probe = aw88261_i2c_probe,
1279 .id_table = aw88261_i2c_id,
1281 module_i2c_driver(aw88261_i2c_driver);
1283 MODULE_DESCRIPTION("ASoC AW88261 Smart PA Driver");
1284 MODULE_LICENSE("GPL v2");