xref: /linux/sound/soc/codecs/aw88395/aw88395.c (revision 23313771c7b99b3b8dba169bc71dae619d41ab56)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // aw88395.c --  ALSA SoC AW88395 codec support
4 //
5 // Copyright (c) 2022-2023 AWINIC Technology CO., LTD
6 //
7 // Author: Bruce zhao <zhaolei@awinic.com>
8 // Author: Weidong Wang <wangweidong.a@awinic.com>
9 //
10 
11 #include <linux/gpio/consumer.h>
12 #include <linux/i2c.h>
13 #include <linux/firmware.h>
14 #include <linux/regmap.h>
15 #include <sound/soc.h>
16 #include "aw88395.h"
17 #include "aw88395_device.h"
18 #include "aw88395_lib.h"
19 #include "aw88395_reg.h"
20 
21 static const struct regmap_config aw88395_remap_config = {
22 	.val_bits = 16,
23 	.reg_bits = 8,
24 	.max_register = AW88395_REG_MAX - 1,
25 	.reg_format_endian = REGMAP_ENDIAN_LITTLE,
26 	.val_format_endian = REGMAP_ENDIAN_BIG,
27 };
28 
29 static void aw88395_start_pa(struct aw88395 *aw88395)
30 {
31 	int ret, i;
32 
33 	for (i = 0; i < AW88395_START_RETRIES; i++) {
34 		ret = aw88395_dev_start(aw88395->aw_pa);
35 		if (ret) {
36 			dev_err(aw88395->aw_pa->dev, "aw88395 device start failed. retry = %d", i);
37 			ret = aw88395_dev_fw_update(aw88395->aw_pa, AW88395_DSP_FW_UPDATE_ON, true);
38 			if (ret < 0) {
39 				dev_err(aw88395->aw_pa->dev, "fw update failed");
40 				continue;
41 			}
42 		} else {
43 			dev_info(aw88395->aw_pa->dev, "start success\n");
44 			break;
45 		}
46 	}
47 }
48 
49 static void aw88395_startup_work(struct work_struct *work)
50 {
51 	struct aw88395 *aw88395 =
52 		container_of(work, struct aw88395, start_work.work);
53 
54 	mutex_lock(&aw88395->lock);
55 	aw88395_start_pa(aw88395);
56 	mutex_unlock(&aw88395->lock);
57 }
58 
59 static void aw88395_start(struct aw88395 *aw88395, bool sync_start)
60 {
61 	int ret;
62 
63 	if (aw88395->aw_pa->fw_status != AW88395_DEV_FW_OK)
64 		return;
65 
66 	if (aw88395->aw_pa->status == AW88395_DEV_PW_ON)
67 		return;
68 
69 	ret = aw88395_dev_fw_update(aw88395->aw_pa, AW88395_DSP_FW_UPDATE_OFF, true);
70 	if (ret < 0) {
71 		dev_err(aw88395->aw_pa->dev, "fw update failed.");
72 		return;
73 	}
74 
75 	if (sync_start == AW88395_SYNC_START)
76 		aw88395_start_pa(aw88395);
77 	else
78 		queue_delayed_work(system_wq,
79 			&aw88395->start_work,
80 			AW88395_START_WORK_DELAY_MS);
81 }
82 
83 static struct snd_soc_dai_driver aw88395_dai[] = {
84 	{
85 		.name = "aw88395-aif",
86 		.id = 1,
87 		.playback = {
88 			.stream_name = "Speaker_Playback",
89 			.channels_min = 1,
90 			.channels_max = 2,
91 			.rates = AW88395_RATES,
92 			.formats = AW88395_FORMATS,
93 		},
94 		.capture = {
95 			.stream_name = "Speaker_Capture",
96 			.channels_min = 1,
97 			.channels_max = 2,
98 			.rates = AW88395_RATES,
99 			.formats = AW88395_FORMATS,
100 		},
101 	},
102 };
103 
104 static int aw88395_get_fade_in_time(struct snd_kcontrol *kcontrol,
105 	struct snd_ctl_elem_value *ucontrol)
106 {
107 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
108 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(component);
109 	struct aw_device *aw_dev = aw88395->aw_pa;
110 
111 	ucontrol->value.integer.value[0] = aw_dev->fade_in_time;
112 
113 	return 0;
114 }
115 
116 static int aw88395_set_fade_in_time(struct snd_kcontrol *kcontrol,
117 	struct snd_ctl_elem_value *ucontrol)
118 {
119 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
120 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(component);
121 	struct soc_mixer_control *mc =
122 		(struct soc_mixer_control *)kcontrol->private_value;
123 	struct aw_device *aw_dev = aw88395->aw_pa;
124 	int time;
125 
126 	time = ucontrol->value.integer.value[0];
127 
128 	if (time < mc->min || time > mc->max)
129 		return -EINVAL;
130 
131 	if (time != aw_dev->fade_in_time) {
132 		aw_dev->fade_in_time = time;
133 		return 1;
134 	}
135 
136 	return 0;
137 }
138 
139 static int aw88395_get_fade_out_time(struct snd_kcontrol *kcontrol,
140 	struct snd_ctl_elem_value *ucontrol)
141 {
142 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
143 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(component);
144 	struct aw_device *aw_dev = aw88395->aw_pa;
145 
146 	ucontrol->value.integer.value[0] = aw_dev->fade_out_time;
147 
148 	return 0;
149 }
150 
151 static int aw88395_set_fade_out_time(struct snd_kcontrol *kcontrol,
152 	struct snd_ctl_elem_value *ucontrol)
153 {
154 	struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
155 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(component);
156 	struct soc_mixer_control *mc =
157 		(struct soc_mixer_control *)kcontrol->private_value;
158 	struct aw_device *aw_dev = aw88395->aw_pa;
159 	int time;
160 
161 	time = ucontrol->value.integer.value[0];
162 	if (time < mc->min || time > mc->max)
163 		return -EINVAL;
164 
165 	if (time != aw_dev->fade_out_time) {
166 		aw_dev->fade_out_time = time;
167 		return 1;
168 	}
169 
170 	return 0;
171 }
172 
173 static int aw88395_profile_info(struct snd_kcontrol *kcontrol,
174 			 struct snd_ctl_elem_info *uinfo)
175 {
176 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
177 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
178 	char *prof_name;
179 	int count, ret;
180 
181 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
182 	uinfo->count = 1;
183 
184 	count = aw88395_dev_get_profile_count(aw88395->aw_pa);
185 	if (count <= 0) {
186 		uinfo->value.enumerated.items = 0;
187 		return 0;
188 	}
189 
190 	uinfo->value.enumerated.items = count;
191 
192 	if (uinfo->value.enumerated.item >= count)
193 		uinfo->value.enumerated.item = count - 1;
194 
195 	count = uinfo->value.enumerated.item;
196 
197 	ret = aw88395_dev_get_prof_name(aw88395->aw_pa, count, &prof_name);
198 	if (ret) {
199 		strscpy(uinfo->value.enumerated.name, "null");
200 		return 0;
201 	}
202 
203 	strscpy(uinfo->value.enumerated.name, prof_name);
204 
205 	return 0;
206 }
207 
208 static int aw88395_profile_get(struct snd_kcontrol *kcontrol,
209 			struct snd_ctl_elem_value *ucontrol)
210 {
211 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
212 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
213 
214 	ucontrol->value.integer.value[0] = aw88395_dev_get_profile_index(aw88395->aw_pa);
215 
216 	return 0;
217 }
218 
219 static int aw88395_profile_set(struct snd_kcontrol *kcontrol,
220 		struct snd_ctl_elem_value *ucontrol)
221 {
222 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
223 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
224 	int ret;
225 
226 	/* pa stop or stopping just set profile */
227 	mutex_lock(&aw88395->lock);
228 	ret = aw88395_dev_set_profile_index(aw88395->aw_pa, ucontrol->value.integer.value[0]);
229 	if (ret < 0) {
230 		dev_dbg(codec->dev, "profile index does not change");
231 		mutex_unlock(&aw88395->lock);
232 		return 0;
233 	}
234 
235 	if (aw88395->aw_pa->status) {
236 		aw88395_dev_stop(aw88395->aw_pa);
237 		aw88395_start(aw88395, AW88395_SYNC_START);
238 	}
239 
240 	mutex_unlock(&aw88395->lock);
241 
242 	return 1;
243 }
244 
245 static int aw88395_volume_get(struct snd_kcontrol *kcontrol,
246 				struct snd_ctl_elem_value *ucontrol)
247 {
248 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
249 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
250 	struct aw_volume_desc *vol_desc = &aw88395->aw_pa->volume_desc;
251 
252 	ucontrol->value.integer.value[0] = vol_desc->ctl_volume;
253 
254 	return 0;
255 }
256 
257 static int aw88395_volume_set(struct snd_kcontrol *kcontrol,
258 				struct snd_ctl_elem_value *ucontrol)
259 {
260 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
261 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
262 	struct aw_volume_desc *vol_desc = &aw88395->aw_pa->volume_desc;
263 	struct soc_mixer_control *mc =
264 		(struct soc_mixer_control *)kcontrol->private_value;
265 	int value;
266 
267 	value = ucontrol->value.integer.value[0];
268 	if (value < mc->min || value > mc->max)
269 		return -EINVAL;
270 
271 	if (vol_desc->ctl_volume != value) {
272 		vol_desc->ctl_volume = value;
273 		aw88395_dev_set_volume(aw88395->aw_pa, vol_desc->ctl_volume);
274 
275 		return 1;
276 	}
277 
278 	return 0;
279 }
280 
281 static int aw88395_get_fade_step(struct snd_kcontrol *kcontrol,
282 				struct snd_ctl_elem_value *ucontrol)
283 {
284 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
285 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
286 
287 	ucontrol->value.integer.value[0] = aw88395->aw_pa->fade_step;
288 
289 	return 0;
290 }
291 
292 static int aw88395_set_fade_step(struct snd_kcontrol *kcontrol,
293 				struct snd_ctl_elem_value *ucontrol)
294 {
295 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
296 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
297 	struct soc_mixer_control *mc =
298 		(struct soc_mixer_control *)kcontrol->private_value;
299 	int value;
300 
301 	value = ucontrol->value.integer.value[0];
302 	if (value < mc->min || value > mc->max)
303 		return -EINVAL;
304 
305 	if (aw88395->aw_pa->fade_step != value) {
306 		aw88395->aw_pa->fade_step = value;
307 		return 1;
308 	}
309 
310 	return 0;
311 }
312 
313 static int aw88395_re_get(struct snd_kcontrol *kcontrol,
314 				struct snd_ctl_elem_value *ucontrol)
315 {
316 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
317 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
318 	struct aw_device *aw_dev = aw88395->aw_pa;
319 
320 	ucontrol->value.integer.value[0] = aw_dev->cali_desc.cali_re;
321 
322 	return 0;
323 }
324 
325 static int aw88395_re_set(struct snd_kcontrol *kcontrol,
326 				struct snd_ctl_elem_value *ucontrol)
327 {
328 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
329 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
330 	struct soc_mixer_control *mc =
331 		(struct soc_mixer_control *)kcontrol->private_value;
332 	struct aw_device *aw_dev = aw88395->aw_pa;
333 	int value;
334 
335 	value = ucontrol->value.integer.value[0];
336 	if (value < mc->min || value > mc->max)
337 		return -EINVAL;
338 
339 	if (aw_dev->cali_desc.cali_re != value) {
340 		aw_dev->cali_desc.cali_re = value;
341 		return 1;
342 	}
343 
344 	return 0;
345 }
346 
347 static const struct snd_kcontrol_new aw88395_controls[] = {
348 	SOC_SINGLE_EXT("PCM Playback Volume", AW88395_SYSCTRL2_REG,
349 		6, AW88395_MUTE_VOL, 0, aw88395_volume_get,
350 		aw88395_volume_set),
351 	SOC_SINGLE_EXT("Fade Step", 0, 0, AW88395_MUTE_VOL, 0,
352 		aw88395_get_fade_step, aw88395_set_fade_step),
353 	SOC_SINGLE_EXT("Volume Ramp Up Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
354 		aw88395_get_fade_in_time, aw88395_set_fade_in_time),
355 	SOC_SINGLE_EXT("Volume Ramp Down Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
356 		aw88395_get_fade_out_time, aw88395_set_fade_out_time),
357 	SOC_SINGLE_EXT("Calib", 0, 0, AW88395_CALI_RE_MAX, 0,
358 		aw88395_re_get, aw88395_re_set),
359 	AW88395_PROFILE_EXT("Profile Set", aw88395_profile_info,
360 		aw88395_profile_get, aw88395_profile_set),
361 };
362 
363 static int aw88395_playback_event(struct snd_soc_dapm_widget *w,
364 				struct snd_kcontrol *k, int event)
365 {
366 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
367 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(component);
368 
369 	mutex_lock(&aw88395->lock);
370 	switch (event) {
371 	case SND_SOC_DAPM_PRE_PMU:
372 		aw88395_start(aw88395, AW88395_ASYNC_START);
373 		break;
374 	case SND_SOC_DAPM_POST_PMD:
375 		aw88395_dev_stop(aw88395->aw_pa);
376 		break;
377 	default:
378 		break;
379 	}
380 	mutex_unlock(&aw88395->lock);
381 
382 	return 0;
383 }
384 
385 static const struct snd_soc_dapm_widget aw88395_dapm_widgets[] = {
386 	 /* playback */
387 	SND_SOC_DAPM_AIF_IN_E("AIF_RX", "Speaker_Playback", 0, 0, 0, 0,
388 					aw88395_playback_event,
389 					SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
390 	SND_SOC_DAPM_OUTPUT("DAC Output"),
391 
392 	/* capture */
393 	SND_SOC_DAPM_AIF_OUT("AIF_TX", "Speaker_Capture", 0, SND_SOC_NOPM, 0, 0),
394 	SND_SOC_DAPM_INPUT("ADC Input"),
395 };
396 
397 static const struct snd_soc_dapm_route aw88395_audio_map[] = {
398 	{"DAC Output", NULL, "AIF_RX"},
399 	{"AIF_TX", NULL, "ADC Input"},
400 };
401 
402 static int aw88395_codec_probe(struct snd_soc_component *component)
403 {
404 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
405 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(component);
406 	int ret;
407 
408 	INIT_DELAYED_WORK(&aw88395->start_work, aw88395_startup_work);
409 
410 	/* add widgets */
411 	ret = snd_soc_dapm_new_controls(dapm, aw88395_dapm_widgets,
412 							ARRAY_SIZE(aw88395_dapm_widgets));
413 	if (ret < 0)
414 		return ret;
415 
416 	/* add route */
417 	ret = snd_soc_dapm_add_routes(dapm, aw88395_audio_map,
418 							ARRAY_SIZE(aw88395_audio_map));
419 	if (ret < 0)
420 		return ret;
421 
422 	ret = snd_soc_add_component_controls(component, aw88395_controls,
423 							ARRAY_SIZE(aw88395_controls));
424 
425 	return ret;
426 }
427 
428 static void aw88395_codec_remove(struct snd_soc_component *aw_codec)
429 {
430 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(aw_codec);
431 
432 	cancel_delayed_work_sync(&aw88395->start_work);
433 }
434 
435 static const struct snd_soc_component_driver soc_codec_dev_aw88395 = {
436 	.probe = aw88395_codec_probe,
437 	.remove = aw88395_codec_remove,
438 };
439 
440 static struct aw88395 *aw88395_malloc_init(struct i2c_client *i2c)
441 {
442 	struct aw88395 *aw88395 = devm_kzalloc(&i2c->dev,
443 			sizeof(struct aw88395), GFP_KERNEL);
444 	if (!aw88395)
445 		return NULL;
446 
447 	mutex_init(&aw88395->lock);
448 
449 	return aw88395;
450 }
451 
452 static void aw88395_hw_reset(struct aw88395 *aw88395)
453 {
454 	if (aw88395->reset_gpio) {
455 		gpiod_set_value_cansleep(aw88395->reset_gpio, 0);
456 		usleep_range(AW88395_1000_US, AW88395_1000_US + 10);
457 		gpiod_set_value_cansleep(aw88395->reset_gpio, 1);
458 		usleep_range(AW88395_1000_US, AW88395_1000_US + 10);
459 	} else {
460 		dev_err(aw88395->aw_pa->dev, "%s failed", __func__);
461 	}
462 }
463 
464 static int aw88395_request_firmware_file(struct aw88395 *aw88395)
465 {
466 	const struct firmware *cont = NULL;
467 	int ret;
468 
469 	aw88395->aw_pa->fw_status = AW88395_DEV_FW_FAILED;
470 
471 	ret = request_firmware(&cont, AW88395_ACF_FILE, aw88395->aw_pa->dev);
472 	if ((ret < 0) || (!cont)) {
473 		dev_err(aw88395->aw_pa->dev, "load [%s] failed!", AW88395_ACF_FILE);
474 		return ret;
475 	}
476 
477 	dev_info(aw88395->aw_pa->dev, "loaded %s - size: %zu\n",
478 			AW88395_ACF_FILE, cont ? cont->size : 0);
479 
480 	aw88395->aw_cfg = devm_kzalloc(aw88395->aw_pa->dev, cont->size + sizeof(int), GFP_KERNEL);
481 	if (!aw88395->aw_cfg) {
482 		release_firmware(cont);
483 		return -ENOMEM;
484 	}
485 	aw88395->aw_cfg->len = (int)cont->size;
486 	memcpy(aw88395->aw_cfg->data, cont->data, cont->size);
487 	release_firmware(cont);
488 
489 	ret = aw88395_dev_load_acf_check(aw88395->aw_pa, aw88395->aw_cfg);
490 	if (ret < 0) {
491 		dev_err(aw88395->aw_pa->dev, "Load [%s] failed ....!", AW88395_ACF_FILE);
492 		return ret;
493 	}
494 
495 	dev_dbg(aw88395->aw_pa->dev, "%s : bin load success\n", __func__);
496 
497 	mutex_lock(&aw88395->lock);
498 	/* aw device init */
499 	ret = aw88395_dev_init(aw88395->aw_pa, aw88395->aw_cfg);
500 	if (ret < 0)
501 		dev_err(aw88395->aw_pa->dev, "dev init failed");
502 	mutex_unlock(&aw88395->lock);
503 
504 	return ret;
505 }
506 
507 static int aw88395_i2c_probe(struct i2c_client *i2c)
508 {
509 	struct aw88395 *aw88395;
510 	int ret;
511 
512 	if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C)) {
513 		dev_err(&i2c->dev, "check_functionality failed");
514 		return -EIO;
515 	}
516 
517 	aw88395 = aw88395_malloc_init(i2c);
518 	if (!aw88395) {
519 		dev_err(&i2c->dev, "malloc aw88395 failed");
520 		return -ENOMEM;
521 	}
522 	i2c_set_clientdata(i2c, aw88395);
523 
524 	aw88395->reset_gpio = devm_gpiod_get_optional(&i2c->dev, "reset", GPIOD_OUT_LOW);
525 	if (IS_ERR(aw88395->reset_gpio))
526 		dev_info(&i2c->dev, "reset gpio not defined\n");
527 
528 	/* hardware reset */
529 	aw88395_hw_reset(aw88395);
530 
531 	aw88395->regmap = devm_regmap_init_i2c(i2c, &aw88395_remap_config);
532 	if (IS_ERR(aw88395->regmap)) {
533 		ret = PTR_ERR(aw88395->regmap);
534 		dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
535 		return ret;
536 	}
537 
538 	/* aw pa init */
539 	ret = aw88395_init(&aw88395->aw_pa, i2c, aw88395->regmap);
540 	if (ret < 0)
541 		return ret;
542 
543 	ret = aw88395_request_firmware_file(aw88395);
544 	if (ret < 0) {
545 		dev_err(&i2c->dev, "%s failed\n", __func__);
546 		return ret;
547 	}
548 
549 	ret = devm_snd_soc_register_component(&i2c->dev,
550 			&soc_codec_dev_aw88395,
551 			aw88395_dai, ARRAY_SIZE(aw88395_dai));
552 	if (ret < 0) {
553 		dev_err(&i2c->dev, "failed to register aw88395: %d", ret);
554 		return ret;
555 	}
556 
557 	return 0;
558 }
559 
560 static const struct i2c_device_id aw88395_i2c_id[] = {
561 	{ AW88395_I2C_NAME },
562 	{ }
563 };
564 MODULE_DEVICE_TABLE(i2c, aw88395_i2c_id);
565 
566 static struct i2c_driver aw88395_i2c_driver = {
567 	.driver = {
568 		.name = AW88395_I2C_NAME,
569 	},
570 	.probe = aw88395_i2c_probe,
571 	.id_table = aw88395_i2c_id,
572 };
573 module_i2c_driver(aw88395_i2c_driver);
574 
575 MODULE_DESCRIPTION("ASoC AW88395 Smart PA Driver");
576 MODULE_LICENSE("GPL v2");
577