xref: /openbmc/linux/sound/soc/codecs/aw88395/aw88395.c (revision 185c8f33)
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/i2c.h>
12 #include <linux/firmware.h>
13 #include <linux/of_gpio.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 	const char *prof_name;
179 	char *name;
180 	int count;
181 
182 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
183 	uinfo->count = 1;
184 
185 	count = aw88395_dev_get_profile_count(aw88395->aw_pa);
186 	if (count <= 0) {
187 		uinfo->value.enumerated.items = 0;
188 		return 0;
189 	}
190 
191 	uinfo->value.enumerated.items = count;
192 
193 	if (uinfo->value.enumerated.item >= count)
194 		uinfo->value.enumerated.item = count - 1;
195 
196 	name = uinfo->value.enumerated.name;
197 	count = uinfo->value.enumerated.item;
198 
199 	prof_name = aw88395_dev_get_prof_name(aw88395->aw_pa, count);
200 	if (!prof_name) {
201 		strscpy(uinfo->value.enumerated.name, "null",
202 						strlen("null") + 1);
203 		return 0;
204 	}
205 
206 	strscpy(name, prof_name, sizeof(uinfo->value.enumerated.name));
207 
208 	return 0;
209 }
210 
211 static int aw88395_profile_get(struct snd_kcontrol *kcontrol,
212 			struct snd_ctl_elem_value *ucontrol)
213 {
214 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
215 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
216 
217 	ucontrol->value.integer.value[0] = aw88395_dev_get_profile_index(aw88395->aw_pa);
218 
219 	return 0;
220 }
221 
222 static int aw88395_profile_set(struct snd_kcontrol *kcontrol,
223 		struct snd_ctl_elem_value *ucontrol)
224 {
225 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
226 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
227 	int ret;
228 
229 	/* pa stop or stopping just set profile */
230 	mutex_lock(&aw88395->lock);
231 	ret = aw88395_dev_set_profile_index(aw88395->aw_pa, ucontrol->value.integer.value[0]);
232 	if (ret < 0) {
233 		dev_dbg(codec->dev, "profile index does not change");
234 		mutex_unlock(&aw88395->lock);
235 		return 0;
236 	}
237 
238 	if (aw88395->aw_pa->status) {
239 		aw88395_dev_stop(aw88395->aw_pa);
240 		aw88395_start(aw88395, AW88395_SYNC_START);
241 	}
242 
243 	mutex_unlock(&aw88395->lock);
244 
245 	return 1;
246 }
247 
248 static int aw88395_volume_get(struct snd_kcontrol *kcontrol,
249 				struct snd_ctl_elem_value *ucontrol)
250 {
251 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
252 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
253 	struct aw_volume_desc *vol_desc = &aw88395->aw_pa->volume_desc;
254 
255 	ucontrol->value.integer.value[0] = vol_desc->ctl_volume;
256 
257 	return 0;
258 }
259 
260 static int aw88395_volume_set(struct snd_kcontrol *kcontrol,
261 				struct snd_ctl_elem_value *ucontrol)
262 {
263 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
264 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
265 	struct aw_volume_desc *vol_desc = &aw88395->aw_pa->volume_desc;
266 	struct soc_mixer_control *mc =
267 		(struct soc_mixer_control *)kcontrol->private_value;
268 	int value;
269 
270 	value = ucontrol->value.integer.value[0];
271 	if (value < mc->min || value > mc->max)
272 		return -EINVAL;
273 
274 	if (vol_desc->ctl_volume != value) {
275 		vol_desc->ctl_volume = value;
276 		aw88395_dev_set_volume(aw88395->aw_pa, vol_desc->ctl_volume);
277 
278 		return 1;
279 	}
280 
281 	return 0;
282 }
283 
284 static int aw88395_get_fade_step(struct snd_kcontrol *kcontrol,
285 				struct snd_ctl_elem_value *ucontrol)
286 {
287 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
288 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
289 
290 	ucontrol->value.integer.value[0] = aw88395->aw_pa->fade_step;
291 
292 	return 0;
293 }
294 
295 static int aw88395_set_fade_step(struct snd_kcontrol *kcontrol,
296 				struct snd_ctl_elem_value *ucontrol)
297 {
298 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
299 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
300 	struct soc_mixer_control *mc =
301 		(struct soc_mixer_control *)kcontrol->private_value;
302 	int value;
303 
304 	value = ucontrol->value.integer.value[0];
305 	if (value < mc->min || value > mc->max)
306 		return -EINVAL;
307 
308 	if (aw88395->aw_pa->fade_step != value) {
309 		aw88395->aw_pa->fade_step = value;
310 		return 1;
311 	}
312 
313 	return 0;
314 }
315 
316 static int aw88395_re_get(struct snd_kcontrol *kcontrol,
317 				struct snd_ctl_elem_value *ucontrol)
318 {
319 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
320 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
321 	struct aw_device *aw_dev = aw88395->aw_pa;
322 
323 	ucontrol->value.integer.value[0] = aw_dev->cali_desc.cali_re;
324 
325 	return 0;
326 }
327 
328 static int aw88395_re_set(struct snd_kcontrol *kcontrol,
329 				struct snd_ctl_elem_value *ucontrol)
330 {
331 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
332 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(codec);
333 	struct soc_mixer_control *mc =
334 		(struct soc_mixer_control *)kcontrol->private_value;
335 	struct aw_device *aw_dev = aw88395->aw_pa;
336 	int value;
337 
338 	value = ucontrol->value.integer.value[0];
339 	if (value < mc->min || value > mc->max)
340 		return -EINVAL;
341 
342 	if (aw_dev->cali_desc.cali_re != value) {
343 		aw_dev->cali_desc.cali_re = value;
344 		return 1;
345 	}
346 
347 	return 0;
348 }
349 
350 static const struct snd_kcontrol_new aw88395_controls[] = {
351 	SOC_SINGLE_EXT("PCM Playback Volume", AW88395_SYSCTRL2_REG,
352 		6, AW88395_MUTE_VOL, 0, aw88395_volume_get,
353 		aw88395_volume_set),
354 	SOC_SINGLE_EXT("Fade Step", 0, 0, AW88395_MUTE_VOL, 0,
355 		aw88395_get_fade_step, aw88395_set_fade_step),
356 	SOC_SINGLE_EXT("Volume Ramp Up Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
357 		aw88395_get_fade_in_time, aw88395_set_fade_in_time),
358 	SOC_SINGLE_EXT("Volume Ramp Down Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
359 		aw88395_get_fade_out_time, aw88395_set_fade_out_time),
360 	SOC_SINGLE_EXT("Calib", 0, 0, 100, 0,
361 		aw88395_re_get, aw88395_re_set),
362 	AW88395_PROFILE_EXT("Profile Set", aw88395_profile_info,
363 		aw88395_profile_get, aw88395_profile_set),
364 };
365 
366 static int aw88395_playback_event(struct snd_soc_dapm_widget *w,
367 				struct snd_kcontrol *k, int event)
368 {
369 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
370 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(component);
371 
372 	mutex_lock(&aw88395->lock);
373 	switch (event) {
374 	case SND_SOC_DAPM_PRE_PMU:
375 		aw88395_start(aw88395, AW88395_ASYNC_START);
376 		break;
377 	case SND_SOC_DAPM_POST_PMD:
378 		aw88395_dev_stop(aw88395->aw_pa);
379 		break;
380 	default:
381 		break;
382 	}
383 	mutex_unlock(&aw88395->lock);
384 
385 	return 0;
386 }
387 
388 static const struct snd_soc_dapm_widget aw88395_dapm_widgets[] = {
389 	 /* playback */
390 	SND_SOC_DAPM_AIF_IN_E("AIF_RX", "Speaker_Playback", 0, 0, 0, 0,
391 					aw88395_playback_event,
392 					SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
393 	SND_SOC_DAPM_OUTPUT("DAC Output"),
394 
395 	/* capture */
396 	SND_SOC_DAPM_AIF_OUT("AIF_TX", "Speaker_Capture", 0, SND_SOC_NOPM, 0, 0),
397 	SND_SOC_DAPM_INPUT("ADC Input"),
398 };
399 
400 static const struct snd_soc_dapm_route aw88395_audio_map[] = {
401 	{"DAC Output", NULL, "AIF_RX"},
402 	{"AIF_TX", NULL, "ADC Input"},
403 };
404 
405 static int aw88395_codec_probe(struct snd_soc_component *component)
406 {
407 	struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component);
408 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(component);
409 	int ret;
410 
411 	INIT_DELAYED_WORK(&aw88395->start_work, aw88395_startup_work);
412 
413 	/* add widgets */
414 	ret = snd_soc_dapm_new_controls(dapm, aw88395_dapm_widgets,
415 							ARRAY_SIZE(aw88395_dapm_widgets));
416 	if (ret < 0)
417 		return ret;
418 
419 	/* add route */
420 	ret = snd_soc_dapm_add_routes(dapm, aw88395_audio_map,
421 							ARRAY_SIZE(aw88395_audio_map));
422 	if (ret < 0)
423 		return ret;
424 
425 	ret = snd_soc_add_component_controls(component, aw88395_controls,
426 							ARRAY_SIZE(aw88395_controls));
427 
428 	return ret;
429 }
430 
431 static void aw88395_codec_remove(struct snd_soc_component *aw_codec)
432 {
433 	struct aw88395 *aw88395 = snd_soc_component_get_drvdata(aw_codec);
434 
435 	cancel_delayed_work_sync(&aw88395->start_work);
436 }
437 
438 static const struct snd_soc_component_driver soc_codec_dev_aw88395 = {
439 	.probe = aw88395_codec_probe,
440 	.remove = aw88395_codec_remove,
441 };
442 
443 static struct aw88395 *aw88395_malloc_init(struct i2c_client *i2c)
444 {
445 	struct aw88395 *aw88395 = devm_kzalloc(&i2c->dev,
446 			sizeof(struct aw88395), GFP_KERNEL);
447 	if (!aw88395)
448 		return NULL;
449 
450 	mutex_init(&aw88395->lock);
451 
452 	return aw88395;
453 }
454 
455 static void aw88395_hw_reset(struct aw88395 *aw88395)
456 {
457 	if (aw88395->reset_gpio) {
458 		gpiod_set_value_cansleep(aw88395->reset_gpio, 0);
459 		usleep_range(AW88395_1000_US, AW88395_1000_US + 10);
460 		gpiod_set_value_cansleep(aw88395->reset_gpio, 1);
461 		usleep_range(AW88395_1000_US, AW88395_1000_US + 10);
462 	} else {
463 		dev_err(aw88395->aw_pa->dev, "%s failed", __func__);
464 	}
465 }
466 
467 static int aw88395_request_firmware_file(struct aw88395 *aw88395)
468 {
469 	const struct firmware *cont = NULL;
470 	int ret;
471 
472 	aw88395->aw_pa->fw_status = AW88395_DEV_FW_FAILED;
473 
474 	ret = request_firmware(&cont, AW88395_ACF_FILE, aw88395->aw_pa->dev);
475 	if ((ret < 0) || (!cont)) {
476 		dev_err(aw88395->aw_pa->dev, "load [%s] failed!", AW88395_ACF_FILE);
477 		return ret;
478 	}
479 
480 	dev_info(aw88395->aw_pa->dev, "loaded %s - size: %zu\n",
481 			AW88395_ACF_FILE, cont ? cont->size : 0);
482 
483 	aw88395->aw_cfg = devm_kzalloc(aw88395->aw_pa->dev, cont->size + sizeof(int), GFP_KERNEL);
484 	if (!aw88395->aw_cfg) {
485 		release_firmware(cont);
486 		return -ENOMEM;
487 	}
488 	aw88395->aw_cfg->len = (int)cont->size;
489 	memcpy(aw88395->aw_cfg->data, cont->data, cont->size);
490 	release_firmware(cont);
491 
492 	ret = aw88395_dev_load_acf_check(aw88395->aw_pa, aw88395->aw_cfg);
493 	if (ret < 0) {
494 		dev_err(aw88395->aw_pa->dev, "Load [%s] failed ....!", AW88395_ACF_FILE);
495 		return ret;
496 	}
497 
498 	dev_dbg(aw88395->aw_pa->dev, "%s : bin load success\n", __func__);
499 
500 	mutex_lock(&aw88395->lock);
501 	/* aw device init */
502 	ret = aw88395_dev_init(aw88395->aw_pa, aw88395->aw_cfg);
503 	if (ret < 0)
504 		dev_err(aw88395->aw_pa->dev, "dev init failed");
505 	mutex_unlock(&aw88395->lock);
506 
507 	return ret;
508 }
509 
510 static int aw88395_i2c_probe(struct i2c_client *i2c)
511 {
512 	struct aw88395 *aw88395;
513 	int ret;
514 
515 	if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C)) {
516 		dev_err(&i2c->dev, "check_functionality failed");
517 		return -EIO;
518 	}
519 
520 	aw88395 = aw88395_malloc_init(i2c);
521 	if (!aw88395) {
522 		dev_err(&i2c->dev, "malloc aw88395 failed");
523 		return -ENOMEM;
524 	}
525 	i2c_set_clientdata(i2c, aw88395);
526 
527 	aw88395->reset_gpio = devm_gpiod_get_optional(&i2c->dev, "reset", GPIOD_OUT_LOW);
528 	if (IS_ERR(aw88395->reset_gpio))
529 		dev_info(&i2c->dev, "reset gpio not defined\n");
530 
531 	/* hardware reset */
532 	aw88395_hw_reset(aw88395);
533 
534 	aw88395->regmap = devm_regmap_init_i2c(i2c, &aw88395_remap_config);
535 	if (IS_ERR(aw88395->regmap)) {
536 		ret = PTR_ERR(aw88395->regmap);
537 		dev_err(&i2c->dev, "Failed to init regmap: %d\n", ret);
538 		return ret;
539 	}
540 
541 	/* aw pa init */
542 	ret = aw88395_init(&aw88395->aw_pa, i2c, aw88395->regmap);
543 	if (ret < 0)
544 		return ret;
545 
546 	ret = aw88395_request_firmware_file(aw88395);
547 	if (ret < 0) {
548 		dev_err(&i2c->dev, "%s failed\n", __func__);
549 		return ret;
550 	}
551 
552 	ret = devm_snd_soc_register_component(&i2c->dev,
553 			&soc_codec_dev_aw88395,
554 			aw88395_dai, ARRAY_SIZE(aw88395_dai));
555 	if (ret < 0) {
556 		dev_err(&i2c->dev, "failed to register aw88395: %d", ret);
557 		return ret;
558 	}
559 
560 	return 0;
561 }
562 
563 static const struct i2c_device_id aw88395_i2c_id[] = {
564 	{ AW88395_I2C_NAME, 0 },
565 	{ }
566 };
567 MODULE_DEVICE_TABLE(i2c, aw88395_i2c_id);
568 
569 static struct i2c_driver aw88395_i2c_driver = {
570 	.driver = {
571 		.name = AW88395_I2C_NAME,
572 	},
573 	.probe = aw88395_i2c_probe,
574 	.id_table = aw88395_i2c_id,
575 };
576 module_i2c_driver(aw88395_i2c_driver);
577 
578 MODULE_DESCRIPTION("ASoC AW88395 Smart PA Driver");
579 MODULE_LICENSE("GPL v2");
580