printf: Remove unused 'bprintf'
[drm/drm-misc.git] / sound / soc / codecs / aw88081.c
blob58b8e002d76f08f70017c6b5d01431b7214746b0
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // aw88081.c -- AW88081 ALSA SoC Audio driver
4 //
5 // Copyright (c) 2024 awinic Technology CO., LTD
6 //
7 // Author: Weidong Wang <wangweidong.a@awinic.com>
8 //
10 #include <linux/firmware.h>
11 #include <linux/i2c.h>
12 #include <linux/regmap.h>
13 #include <sound/soc.h>
14 #include "aw88081.h"
15 #include "aw88395/aw88395_device.h"
17 struct aw88081 {
18 struct aw_device *aw_pa;
19 struct mutex lock;
20 struct delayed_work start_work;
21 struct regmap *regmap;
22 struct aw_container *aw_cfg;
24 bool phase_sync;
27 static const struct regmap_config aw88081_regmap_config = {
28 .val_bits = 16,
29 .reg_bits = 8,
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)
37 unsigned int reg_val;
38 int ret;
40 ret = regmap_read(aw_dev->regmap, AW88081_SYSST_REG, &reg_val);
41 if (ret)
42 return ret;
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);
45 return -EINVAL;
48 return 0;
51 static int aw88081_dev_check_mode1_pll(struct aw_device *aw_dev)
53 int ret, i;
55 for (i = 0; i < AW88081_DEV_SYSST_CHECK_MAX; i++) {
56 ret = aw88081_dev_get_iis_status(aw_dev);
57 if (ret) {
58 dev_err(aw_dev->dev, "mode1 iis signal check error");
59 usleep_range(AW88081_2000_US, AW88081_2000_US + 10);
60 } else {
61 return 0;
65 return -EPERM;
68 static int aw88081_dev_check_mode2_pll(struct aw_device *aw_dev)
70 unsigned int reg_val;
71 int ret, i;
73 ret = regmap_read(aw_dev->regmap, AW88081_PLLCTRL1_REG, &reg_val);
74 if (ret)
75 return ret;
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");
80 return -EPERM;
83 /* change mode2 */
84 ret = regmap_update_bits(aw_dev->regmap, AW88081_PLLCTRL1_REG,
85 ~AW88081_CCO_MUX_MASK, AW88081_CCO_MUX_DIVIDED_VALUE);
86 if (ret)
87 return ret;
89 for (i = 0; i < AW88081_DEV_SYSST_CHECK_MAX; i++) {
90 ret = aw88081_dev_get_iis_status(aw_dev);
91 if (ret) {
92 dev_err(aw_dev->dev, "mode2 iis check error");
93 usleep_range(AW88081_2000_US, AW88081_2000_US + 10);
94 } else {
95 break;
99 /* change mode1 */
100 ret = regmap_update_bits(aw_dev->regmap, AW88081_PLLCTRL1_REG,
101 ~AW88081_CCO_MUX_MASK, AW88081_CCO_MUX_BYPASS_VALUE);
102 if (ret == 0) {
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);
106 if (ret) {
107 dev_err(aw_dev->dev, "mode2 switch to mode1, iis check error");
108 usleep_range(AW88081_2000_US, AW88081_2000_US + 10);
109 } else {
110 break;
115 return ret;
118 static int aw88081_dev_check_syspll(struct aw_device *aw_dev)
120 int ret;
122 ret = aw88081_dev_check_mode1_pll(aw_dev);
123 if (ret) {
124 dev_dbg(aw_dev->dev, "mode1 check iis failed try switch to mode2 check");
125 ret = aw88081_dev_check_mode2_pll(aw_dev);
126 if (ret) {
127 dev_err(aw_dev->dev, "mode2 check iis failed");
128 return ret;
132 return 0;
135 static int aw88081_dev_check_sysst(struct aw_device *aw_dev)
137 unsigned int check_val;
138 unsigned int reg_val;
139 unsigned int value;
140 int ret, i;
142 ret = regmap_read(aw_dev->regmap, AW88081_PWMCTRL4_REG, &reg_val);
143 if (ret)
144 return ret;
146 if (reg_val & (~AW88081_NOISE_GATE_EN_MASK))
147 check_val = AW88081_NO_SWS_SYSST_CHECK;
148 else
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, &reg_val);
153 if (ret)
154 return ret;
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",
159 reg_val, check_val);
160 usleep_range(AW88081_2000_US, AW88081_2000_US + 10);
161 } else {
162 return 0;
166 return -EPERM;
169 static void aw88081_dev_i2s_tx_enable(struct aw_device *aw_dev, bool flag)
171 if (flag)
172 regmap_update_bits(aw_dev->regmap, AW88081_I2SCTRL3_REG,
173 ~AW88081_I2STXEN_MASK, AW88081_I2STXEN_ENABLE_VALUE);
174 else
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)
181 if (pwd)
182 regmap_update_bits(aw_dev->regmap, AW88081_SYSCTRL_REG,
183 ~AW88081_PWDN_MASK, AW88081_PWDN_POWER_DOWN_VALUE);
184 else
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)
191 if (amppd)
192 regmap_update_bits(aw_dev->regmap, AW88081_SYSCTRL_REG,
193 ~AW88081_EN_PA_MASK, AW88081_EN_PA_POWER_DOWN_VALUE);
194 else
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;
214 unsigned int volume;
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;
226 int i;
228 if (fade_step == 0 || aw_dev->fade_in_time == 0) {
229 aw88081_dev_set_volume(aw_dev, fade_in_vol);
230 return;
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;
246 int i;
248 if (fade_step == 0 || aw_dev->fade_out_time == 0) {
249 aw88081_dev_set_volume(aw_dev, AW88081_MUTE_VOL);
250 return;
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)
264 if (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);
268 } else {
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)
277 if (uls_hmute)
278 regmap_update_bits(aw_dev->regmap, AW88081_SYSCTRL_REG,
279 ~AW88081_ULS_HMUTE_MASK,
280 AW88081_ULS_HMUTE_ENABLE_VALUE);
281 else
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;
294 int16_t *reg_data;
295 u16 reg_val;
296 u8 reg_addr;
298 if (!len || !data) {
299 dev_err(aw_dev->dev, "reg data is null or len is 0");
300 return -EINVAL;
303 reg_data = (int16_t *)data;
304 data_len = len >> 1;
306 if (data_len & 0x1) {
307 dev_err(aw_dev->dev, "data len:%d unsupported", data_len);
308 return -EINVAL;
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 |
317 ~AW88081_PWDN_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;
333 /* i2stxen */
334 if (reg_addr == AW88081_I2SCTRL3_REG) {
335 /* close tx */
336 reg_val &= AW88081_I2STXEN_MASK;
337 reg_val |= AW88081_I2STXEN_DISABLE_VALUE;
340 ret = regmap_write(aw_dev->regmap, reg_addr, reg_val);
341 if (ret)
342 return ret;
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);
351 return 0;
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);
362 return -EINVAL;
365 prof_desc = &aw_dev->prof_info.prof_desc[index];
367 *prof_name = prof_info->prof_name_list[prof_desc->id];
369 return 0;
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);
378 return -EINVAL;
381 *prof_desc = &aw_dev->prof_info.prof_desc[index];
383 return 0;
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;
391 char *prof_name;
392 int ret;
394 ret = aw88081_dev_get_prof_name(aw_dev, aw_dev->prof_index, &prof_name);
395 if (ret) {
396 dev_err(aw_dev->dev, "get prof name failed");
397 return -EINVAL;
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);
403 if (ret)
404 return ret;
406 /* update reg */
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);
410 if (ret) {
411 dev_err(aw_dev->dev, "update reg failed");
412 return ret;
415 aw_dev->prof_cur = aw_dev->prof_index;
417 return 0;
420 static int aw88081_dev_start(struct aw88081 *aw88081)
422 struct aw_device *aw_dev = aw88081->aw_pa;
423 int ret;
425 if (aw_dev->status == AW88081_DEV_PW_ON) {
426 dev_dbg(aw_dev->dev, "already power on");
427 return 0;
430 /* 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);
435 if (ret) {
436 dev_err(aw_dev->dev, "pll check failed cannot start");
437 goto pll_check_fail;
440 /* amppd on */
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);
446 if (ret) {
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);
454 /* close uls mute */
455 aw88081_dev_uls_hmute(aw_dev, false);
457 /* close mute */
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;
464 return 0;
466 sysst_check_fail:
467 aw88081_dev_i2s_tx_enable(aw_dev, false);
468 aw88081_dev_clear_int_status(aw_dev);
469 aw88081_dev_amppd(aw_dev, true);
470 pll_check_fail:
471 aw88081_dev_pwd(aw_dev, true);
472 aw_dev->status = AW88081_DEV_PW_OFF;
474 return ret;
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");
481 return 0;
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);
490 /* set mute */
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);
497 /* enable amppd */
498 aw88081_dev_amppd(aw_dev, true);
500 /* set power down */
501 aw88081_dev_pwd(aw_dev, true);
503 return 0;
506 static int aw88081_reg_update(struct aw88081 *aw88081, bool force)
508 struct aw_device *aw_dev = aw88081->aw_pa;
509 int ret;
511 if (force) {
512 ret = regmap_write(aw_dev->regmap,
513 AW88081_ID_REG, AW88081_SOFT_RESET_VALUE);
514 if (ret)
515 return ret;
517 ret = aw88081_dev_fw_update(aw88081);
518 if (ret)
519 return ret;
520 } else {
521 if (aw_dev->prof_cur != aw_dev->prof_index) {
522 ret = aw88081_dev_fw_update(aw88081);
523 if (ret)
524 return ret;
528 aw_dev->prof_cur = aw_dev->prof_index;
530 return 0;
533 static void aw88081_start_pa(struct aw88081 *aw88081)
535 int ret, i;
537 for (i = 0; i < AW88081_START_RETRIES; i++) {
538 ret = aw88081_reg_update(aw88081, aw88081->phase_sync);
539 if (ret) {
540 dev_err(aw88081->aw_pa->dev, "fw update failed, cnt:%d\n", i);
541 continue;
543 ret = aw88081_dev_start(aw88081);
544 if (ret) {
545 dev_err(aw88081->aw_pa->dev, "aw88081 device start failed. retry = %d", i);
546 continue;
547 } else {
548 dev_dbg(aw88081->aw_pa->dev, "start success\n");
549 break;
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)
567 return;
569 if (aw88081->aw_pa->status == AW88081_DEV_PW_ON)
570 return;
572 if (sync_start == AW88081_SYNC_START)
573 aw88081_start_pa(aw88081);
574 else
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",
583 .id = 1,
584 .playback = {
585 .stream_name = "Speaker_Playback",
586 .channels_min = 1,
587 .channels_max = 2,
588 .rates = AW88081_RATES,
589 .formats = AW88081_FORMATS,
591 .capture = {
592 .stream_name = "Speaker_Capture",
593 .channels_min = 1,
594 .channels_max = 2,
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;
610 return 0;
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;
621 int time;
623 time = ucontrol->value.integer.value[0];
625 if (time < mc->min || time > mc->max)
626 return -EINVAL;
628 if (time != aw_dev->fade_in_time) {
629 aw_dev->fade_in_time = time;
630 return 1;
633 return 0;
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;
645 return 0;
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;
656 int time;
658 time = ucontrol->value.integer.value[0];
659 if (time < mc->min || time > mc->max)
660 return -EINVAL;
662 if (time != aw_dev->fade_out_time) {
663 aw_dev->fade_out_time = time;
664 return 1;
667 return 0;
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))
674 return -EINVAL;
675 /* check the index whether change */
676 if (aw_dev->prof_index == index)
677 return -EPERM;
679 aw_dev->prof_index = index;
681 return 0;
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);
689 char *prof_name;
690 int count, ret;
692 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
693 uinfo->count = 1;
695 count = aw88081->aw_pa->prof_info.count;
696 if (count <= 0) {
697 uinfo->value.enumerated.items = 0;
698 return 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);
709 if (ret) {
710 strscpy(uinfo->value.enumerated.name, "null",
711 sizeof(uinfo->value.enumerated.name));
712 return 0;
715 strscpy(uinfo->value.enumerated.name, prof_name, sizeof(uinfo->value.enumerated.name));
717 return 0;
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;
728 return 0;
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);
736 int ret;
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]);
741 if (ret) {
742 dev_dbg(codec->dev, "profile index does not change");
743 mutex_unlock(&aw88081->lock);
744 return 0;
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);
754 return 1;
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;
766 return 0;
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;
777 int value;
779 value = ucontrol->value.integer.value[0];
781 if (value < mc->min || value > mc->max)
782 return -EINVAL;
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);
787 return 1;
790 return 0;
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;
801 return 0;
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;
811 int value;
813 value = ucontrol->value.integer.value[0];
814 if (value < mc->min || value > mc->max)
815 return -EINVAL;
817 if (aw88081->aw_pa->fade_step != value) {
818 aw88081->aw_pa->fade_step = value;
819 return 1;
822 return 0;
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,
828 aw88081_volume_set),
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;
855 int ret;
857 /* read chip id */
858 ret = regmap_read(regmap, AW88081_ID_REG, &chip_id);
859 if (ret) {
860 dev_err(&i2c->dev, "%s read chipid error. ret = %d", __func__, ret);
861 return ret;
863 if (chip_id != AW88081_CHIP_ID) {
864 dev_err(&i2c->dev, "unsupported device");
865 return -ENXIO;
868 dev_dbg(&i2c->dev, "chip id = %x\n", chip_id);
870 aw_dev = devm_kzalloc(&i2c->dev, sizeof(*aw_dev), GFP_KERNEL);
871 if (!aw_dev)
872 return -ENOMEM;
874 aw88081->aw_pa = aw_dev;
875 aw_dev->i2c = i2c;
876 aw_dev->regmap = regmap;
877 aw_dev->dev = &i2c->dev;
878 aw_dev->chip_id = AW88081_CHIP_ID;
879 aw_dev->acf = NULL;
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);
886 return 0;
889 static int aw88081_dev_init(struct aw88081 *aw88081, struct aw_container *aw_cfg)
891 struct aw_device *aw_dev = aw88081->aw_pa;
892 int ret;
894 ret = aw88395_dev_cfg_load(aw_dev, aw_cfg);
895 if (ret) {
896 dev_err(aw_dev->dev, "aw_dev acf parse failed");
897 return -EINVAL;
900 ret = regmap_write(aw_dev->regmap, AW88081_ID_REG, AW88081_SOFT_RESET_VALUE);
901 if (ret)
902 return ret;
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);
910 if (ret) {
911 dev_err(aw_dev->dev, "fw update failed ret = %d\n", ret);
912 return 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);
931 return 0;
934 static int aw88081_request_firmware_file(struct aw88081 *aw88081)
936 const struct firmware *cont = NULL;
937 int ret;
939 aw88081->aw_pa->fw_status = AW88081_DEV_FW_FAILED;
941 ret = request_firmware(&cont, AW88081_ACF_FILE, aw88081->aw_pa->dev);
942 if (ret)
943 return ret;
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);
951 return -ENOMEM;
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);
958 if (ret)
959 return ret;
961 mutex_lock(&aw88081->lock);
962 ret = aw88081_dev_init(aw88081, aw88081->aw_cfg);
963 mutex_unlock(&aw88081->lock);
965 return ret;
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);
975 switch (event) {
976 case SND_SOC_DAPM_PRE_PMU:
977 aw88081_start(aw88081, AW88081_ASYNC_START);
978 break;
979 case SND_SOC_DAPM_POST_PMD:
980 aw88081_dev_stop(aw88081->aw_pa);
981 break;
982 default:
983 break;
985 mutex_unlock(&aw88081->lock);
987 return 0;
990 static const struct snd_soc_dapm_widget aw88081_dapm_widgets[] = {
991 /* playback */
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"),
997 /* capture */
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);
1010 int ret;
1012 INIT_DELAYED_WORK(&aw88081->start_work, aw88081_startup_work);
1014 ret = aw88081_request_firmware_file(aw88081);
1015 if (ret)
1016 dev_err(aw88081->aw_pa->dev, "%s: request firmware failed\n", __func__);
1018 return ret;
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;
1042 int ret;
1044 ret = i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C);
1045 if (!ret)
1046 return dev_err_probe(&i2c->dev, -ENXIO, "check_functionality failed");
1048 aw88081 = devm_kzalloc(&i2c->dev, sizeof(*aw88081), GFP_KERNEL);
1049 if (!aw88081)
1050 return -ENOMEM;
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");
1061 /* aw pa init */
1062 ret = aw88081_init(aw88081, i2c, aw88081->regmap);
1063 if (ret)
1064 return ret;
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 = {
1078 .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");