xref: /openbmc/linux/sound/soc/codecs/tas2781-i2c.c (revision 80d0624d)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // ALSA SoC Texas Instruments TAS2781 Audio Smart Amplifier
4 //
5 // Copyright (C) 2022 - 2024 Texas Instruments Incorporated
6 // https://www.ti.com
7 //
8 // The TAS2781 driver implements a flexible and configurable
9 // algo coefficient setting for one, two, or even multiple
10 // TAS2781 chips.
11 //
12 // Author: Shenghao Ding <shenghao-ding@ti.com>
13 // Author: Kevin Lu <kevin-lu@ti.com>
14 //
15 
16 #include <linux/crc8.h>
17 #include <linux/firmware.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/i2c.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/of_gpio.h>
25 #include <linux/of_irq.h>
26 #include <linux/regmap.h>
27 #include <linux/slab.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/tas2781.h>
31 #include <sound/tlv.h>
32 #include <sound/tas2781-tlv.h>
33 
34 static const struct i2c_device_id tasdevice_id[] = {
35 	{ "tas2781", TAS2781 },
36 	{}
37 };
38 MODULE_DEVICE_TABLE(i2c, tasdevice_id);
39 
40 #ifdef CONFIG_OF
41 static const struct of_device_id tasdevice_of_match[] = {
42 	{ .compatible = "ti,tas2781" },
43 	{},
44 };
45 MODULE_DEVICE_TABLE(of, tasdevice_of_match);
46 #endif
47 
48 /**
49  * tas2781_digital_getvol - get the volum control
50  * @kcontrol: control pointer
51  * @ucontrol: User data
52  * Customer Kcontrol for tas2781 is primarily for regmap booking, paging
53  * depends on internal regmap mechanism.
54  * tas2781 contains book and page two-level register map, especially
55  * book switching will set the register BXXP00R7F, after switching to the
56  * correct book, then leverage the mechanism for paging to access the
57  * register.
58  */
59 static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,
60 	struct snd_ctl_elem_value *ucontrol)
61 {
62 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
63 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
64 	struct soc_mixer_control *mc =
65 		(struct soc_mixer_control *)kcontrol->private_value;
66 
67 	return tasdevice_digital_getvol(tas_priv, ucontrol, mc);
68 }
69 
70 static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,
71 	struct snd_ctl_elem_value *ucontrol)
72 {
73 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
74 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
75 	struct soc_mixer_control *mc =
76 		(struct soc_mixer_control *)kcontrol->private_value;
77 
78 	return tasdevice_digital_putvol(tas_priv, ucontrol, mc);
79 }
80 
81 static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
82 	struct snd_ctl_elem_value *ucontrol)
83 {
84 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
85 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
86 	struct soc_mixer_control *mc =
87 		(struct soc_mixer_control *)kcontrol->private_value;
88 
89 	return tasdevice_amp_getvol(tas_priv, ucontrol, mc);
90 }
91 
92 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
93 	struct snd_ctl_elem_value *ucontrol)
94 {
95 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
96 	struct tasdevice_priv *tas_priv =
97 		snd_soc_component_get_drvdata(codec);
98 	struct soc_mixer_control *mc =
99 		(struct soc_mixer_control *)kcontrol->private_value;
100 
101 	return tasdevice_amp_putvol(tas_priv, ucontrol, mc);
102 }
103 
104 static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,
105 	struct snd_ctl_elem_value *ucontrol)
106 {
107 	struct snd_soc_component *component =
108 		snd_soc_kcontrol_component(kcontrol);
109 	struct tasdevice_priv *tas_priv =
110 		snd_soc_component_get_drvdata(component);
111 
112 	ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
113 	dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
114 			tas_priv->force_fwload_status ? "ON" : "OFF");
115 
116 	return 0;
117 }
118 
119 static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
120 	struct snd_ctl_elem_value *ucontrol)
121 {
122 	struct snd_soc_component *component =
123 		snd_soc_kcontrol_component(kcontrol);
124 	struct tasdevice_priv *tas_priv =
125 		snd_soc_component_get_drvdata(component);
126 	bool change, val = (bool)ucontrol->value.integer.value[0];
127 
128 	if (tas_priv->force_fwload_status == val)
129 		change = false;
130 	else {
131 		change = true;
132 		tas_priv->force_fwload_status = val;
133 	}
134 	dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
135 		tas_priv->force_fwload_status ? "ON" : "OFF");
136 
137 	return change;
138 }
139 
140 static const struct snd_kcontrol_new tas2781_snd_controls[] = {
141 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL,
142 		1, 0, 20, 0, tas2781_amp_getvol,
143 		tas2781_amp_putvol, amp_vol_tlv),
144 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Gain", TAS2781_DVC_LVL,
145 		0, 0, 200, 1, tas2781_digital_getvol,
146 		tas2781_digital_putvol, dvc_tlv),
147 	SOC_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
148 		tas2781_force_fwload_get, tas2781_force_fwload_put),
149 };
150 
151 static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
152 		struct snd_ctl_elem_value *ucontrol)
153 {
154 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
155 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
156 	int ret = 0;
157 
158 	if (tas_priv->rcabin.profile_cfg_id !=
159 		ucontrol->value.integer.value[0]) {
160 		tas_priv->rcabin.profile_cfg_id =
161 			ucontrol->value.integer.value[0];
162 		ret = 1;
163 	}
164 
165 	return ret;
166 }
167 
168 static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
169 			struct snd_ctl_elem_info *uinfo)
170 {
171 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
172 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
173 	struct tasdevice_fw *tas_fw = tas_priv->fmw;
174 
175 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
176 	uinfo->count = 1;
177 	uinfo->value.integer.min = 0;
178 	uinfo->value.integer.max = (int)tas_fw->nr_programs;
179 
180 	return 0;
181 }
182 
183 static int tasdevice_info_configurations(
184 	struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
185 {
186 	struct snd_soc_component *codec =
187 		snd_soc_kcontrol_component(kcontrol);
188 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
189 	struct tasdevice_fw *tas_fw = tas_priv->fmw;
190 
191 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
192 	uinfo->count = 1;
193 	uinfo->value.integer.min = 0;
194 	uinfo->value.integer.max = (int)tas_fw->nr_configurations - 1;
195 
196 	return 0;
197 }
198 
199 static int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
200 			struct snd_ctl_elem_info *uinfo)
201 {
202 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
203 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
204 
205 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
206 	uinfo->count = 1;
207 	uinfo->value.integer.min = 0;
208 	uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;
209 
210 	return 0;
211 }
212 
213 static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
214 			struct snd_ctl_elem_value *ucontrol)
215 {
216 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
217 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
218 
219 	ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;
220 
221 	return 0;
222 }
223 
224 static int tasdevice_create_control(struct tasdevice_priv *tas_priv)
225 {
226 	struct snd_kcontrol_new *prof_ctrls;
227 	int nr_controls = 1;
228 	int mix_index = 0;
229 	int ret;
230 	char *name;
231 
232 	prof_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,
233 		sizeof(prof_ctrls[0]), GFP_KERNEL);
234 	if (!prof_ctrls) {
235 		ret = -ENOMEM;
236 		goto out;
237 	}
238 
239 	/* Create a mixer item for selecting the active profile */
240 	name = devm_kzalloc(tas_priv->dev, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
241 		GFP_KERNEL);
242 	if (!name) {
243 		ret = -ENOMEM;
244 		goto out;
245 	}
246 	scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "Speaker Profile Id");
247 	prof_ctrls[mix_index].name = name;
248 	prof_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
249 	prof_ctrls[mix_index].info = tasdevice_info_profile;
250 	prof_ctrls[mix_index].get = tasdevice_get_profile_id;
251 	prof_ctrls[mix_index].put = tasdevice_set_profile_id;
252 	mix_index++;
253 
254 	ret = snd_soc_add_component_controls(tas_priv->codec,
255 		prof_ctrls, nr_controls < mix_index ? nr_controls : mix_index);
256 
257 out:
258 	return ret;
259 }
260 
261 static int tasdevice_program_get(struct snd_kcontrol *kcontrol,
262 	struct snd_ctl_elem_value *ucontrol)
263 {
264 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
265 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
266 
267 	ucontrol->value.integer.value[0] = tas_priv->cur_prog;
268 
269 	return 0;
270 }
271 
272 static int tasdevice_program_put(struct snd_kcontrol *kcontrol,
273 	struct snd_ctl_elem_value *ucontrol)
274 {
275 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
276 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
277 	unsigned int nr_program = ucontrol->value.integer.value[0];
278 	int ret = 0;
279 
280 	if (tas_priv->cur_prog != nr_program) {
281 		tas_priv->cur_prog = nr_program;
282 		ret = 1;
283 	}
284 
285 	return ret;
286 }
287 
288 static int tasdevice_configuration_get(struct snd_kcontrol *kcontrol,
289 	struct snd_ctl_elem_value *ucontrol)
290 {
291 
292 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
293 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
294 
295 	ucontrol->value.integer.value[0] = tas_priv->cur_conf;
296 
297 	return 0;
298 }
299 
300 static int tasdevice_configuration_put(
301 	struct snd_kcontrol *kcontrol,
302 	struct snd_ctl_elem_value *ucontrol)
303 {
304 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
305 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
306 	unsigned int nr_configuration = ucontrol->value.integer.value[0];
307 	int ret = 0;
308 
309 	if (tas_priv->cur_conf != nr_configuration) {
310 		tas_priv->cur_conf = nr_configuration;
311 		ret = 1;
312 	}
313 
314 	return ret;
315 }
316 
317 static int tasdevice_dsp_create_ctrls(
318 	struct tasdevice_priv *tas_priv)
319 {
320 	struct snd_kcontrol_new *dsp_ctrls;
321 	char *prog_name, *conf_name;
322 	int nr_controls = 2;
323 	int mix_index = 0;
324 	int ret;
325 
326 	/* Alloc kcontrol via devm_kzalloc, which don't manually
327 	 * free the kcontrol
328 	 */
329 	dsp_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,
330 		sizeof(dsp_ctrls[0]), GFP_KERNEL);
331 	if (!dsp_ctrls) {
332 		ret = -ENOMEM;
333 		goto out;
334 	}
335 
336 	/* Create a mixer item for selecting the active profile */
337 	prog_name = devm_kzalloc(tas_priv->dev,
338 		SNDRV_CTL_ELEM_ID_NAME_MAXLEN, GFP_KERNEL);
339 	conf_name = devm_kzalloc(tas_priv->dev, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
340 		GFP_KERNEL);
341 	if (!prog_name || !conf_name) {
342 		ret = -ENOMEM;
343 		goto out;
344 	}
345 
346 	scnprintf(prog_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
347 		"Speaker Program Id");
348 	dsp_ctrls[mix_index].name = prog_name;
349 	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
350 	dsp_ctrls[mix_index].info = tasdevice_info_programs;
351 	dsp_ctrls[mix_index].get = tasdevice_program_get;
352 	dsp_ctrls[mix_index].put = tasdevice_program_put;
353 	mix_index++;
354 
355 	scnprintf(conf_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
356 		"Speaker Config Id");
357 	dsp_ctrls[mix_index].name = conf_name;
358 	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
359 	dsp_ctrls[mix_index].info = tasdevice_info_configurations;
360 	dsp_ctrls[mix_index].get = tasdevice_configuration_get;
361 	dsp_ctrls[mix_index].put = tasdevice_configuration_put;
362 	mix_index++;
363 
364 	ret = snd_soc_add_component_controls(tas_priv->codec, dsp_ctrls,
365 		nr_controls < mix_index ? nr_controls : mix_index);
366 
367 out:
368 	return ret;
369 }
370 
371 static void tasdevice_fw_ready(const struct firmware *fmw,
372 	void *context)
373 {
374 	struct tasdevice_priv *tas_priv = context;
375 	int ret = 0;
376 	int i;
377 
378 	mutex_lock(&tas_priv->codec_lock);
379 
380 	ret = tasdevice_rca_parser(tas_priv, fmw);
381 	if (ret) {
382 		tasdevice_config_info_remove(tas_priv);
383 		goto out;
384 	}
385 	tasdevice_create_control(tas_priv);
386 
387 	tasdevice_dsp_remove(tas_priv);
388 	tasdevice_calbin_remove(tas_priv);
389 	/*
390 	 * The baseline is the RCA-only case, and then the code attempts to
391 	 * load DSP firmware but in case of failures just keep going, i.e.
392 	 * failing to load DSP firmware is NOT an error.
393 	 */
394 	tas_priv->fw_state = TASDEVICE_RCA_FW_OK;
395 	scnprintf(tas_priv->coef_binaryname, 64, "%s_coef.bin",
396 		tas_priv->dev_name);
397 	ret = tasdevice_dsp_parser(tas_priv);
398 	if (ret) {
399 		dev_err(tas_priv->dev, "dspfw load %s error\n",
400 			tas_priv->coef_binaryname);
401 		goto out;
402 	}
403 
404 	/*
405 	 * If no dsp-related kcontrol created, the dsp resource will be freed.
406 	 */
407 	ret = tasdevice_dsp_create_ctrls(tas_priv);
408 	if (ret) {
409 		dev_err(tas_priv->dev, "dsp controls error\n");
410 		goto out;
411 	}
412 
413 	tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
414 
415 	/* If calibrated data occurs error, dsp will still works with default
416 	 * calibrated data inside algo.
417 	 */
418 	for (i = 0; i < tas_priv->ndev; i++) {
419 		scnprintf(tas_priv->cal_binaryname[i], 64, "%s_cal_0x%02x.bin",
420 			tas_priv->dev_name, tas_priv->tasdevice[i].dev_addr);
421 		ret = tas2781_load_calibration(tas_priv,
422 			tas_priv->cal_binaryname[i], i);
423 		if (ret != 0)
424 			dev_err(tas_priv->dev,
425 				"%s: load %s error, default will effect\n",
426 				__func__, tas_priv->cal_binaryname[i]);
427 	}
428 
429 	tasdevice_prmg_load(tas_priv, 0);
430 	tas_priv->cur_prog = 0;
431 out:
432 	if (tas_priv->fw_state == TASDEVICE_RCA_FW_OK) {
433 		/* If DSP FW fail, DSP kcontrol won't be created. */
434 		tasdevice_dsp_remove(tas_priv);
435 	}
436 	mutex_unlock(&tas_priv->codec_lock);
437 	if (fmw)
438 		release_firmware(fmw);
439 }
440 
441 static int tasdevice_dapm_event(struct snd_soc_dapm_widget *w,
442 			struct snd_kcontrol *kcontrol, int event)
443 {
444 	struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm);
445 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
446 	int state = 0;
447 
448 	/* Codec Lock Hold */
449 	mutex_lock(&tas_priv->codec_lock);
450 	if (event == SND_SOC_DAPM_PRE_PMD)
451 		state = 1;
452 	tasdevice_tuning_switch(tas_priv, state);
453 	/* Codec Lock Release*/
454 	mutex_unlock(&tas_priv->codec_lock);
455 
456 	return 0;
457 }
458 
459 static const struct snd_soc_dapm_widget tasdevice_dapm_widgets[] = {
460 	SND_SOC_DAPM_AIF_IN("ASI", "ASI Playback", 0, SND_SOC_NOPM, 0, 0),
461 	SND_SOC_DAPM_AIF_OUT_E("ASI OUT", "ASI Capture", 0, SND_SOC_NOPM,
462 		0, 0, tasdevice_dapm_event,
463 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
464 	SND_SOC_DAPM_SPK("SPK", tasdevice_dapm_event),
465 	SND_SOC_DAPM_OUTPUT("OUT"),
466 	SND_SOC_DAPM_INPUT("DMIC")
467 };
468 
469 static const struct snd_soc_dapm_route tasdevice_audio_map[] = {
470 	{"SPK", NULL, "ASI"},
471 	{"OUT", NULL, "SPK"},
472 	{"ASI OUT", NULL, "DMIC"}
473 };
474 
475 static int tasdevice_startup(struct snd_pcm_substream *substream,
476 						struct snd_soc_dai *dai)
477 {
478 	struct snd_soc_component *codec = dai->component;
479 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
480 
481 	switch (tas_priv->fw_state) {
482 	case TASDEVICE_RCA_FW_OK:
483 	case TASDEVICE_DSP_FW_ALL_OK:
484 		return 0;
485 	default:
486 		return -EINVAL;
487 	}
488 }
489 
490 static int tasdevice_hw_params(struct snd_pcm_substream *substream,
491 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
492 {
493 	struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(dai);
494 	unsigned int slot_width;
495 	unsigned int fsrate;
496 	int bclk_rate;
497 	int rc = 0;
498 
499 	fsrate = params_rate(params);
500 	switch (fsrate) {
501 	case 48000:
502 	case 44100:
503 		break;
504 	default:
505 		dev_err(tas_priv->dev, "%s: incorrect sample rate = %u\n",
506 			__func__, fsrate);
507 		rc = -EINVAL;
508 		goto out;
509 	}
510 
511 	slot_width = params_width(params);
512 	switch (slot_width) {
513 	case 16:
514 	case 20:
515 	case 24:
516 	case 32:
517 		break;
518 	default:
519 		dev_err(tas_priv->dev, "%s: incorrect slot width = %u\n",
520 			__func__, slot_width);
521 		rc = -EINVAL;
522 		goto out;
523 	}
524 
525 	bclk_rate = snd_soc_params_to_bclk(params);
526 	if (bclk_rate < 0) {
527 		dev_err(tas_priv->dev, "%s: incorrect bclk rate = %d\n",
528 			__func__, bclk_rate);
529 		rc = bclk_rate;
530 		goto out;
531 	}
532 
533 out:
534 	return rc;
535 }
536 
537 static int tasdevice_set_dai_sysclk(struct snd_soc_dai *codec_dai,
538 	int clk_id, unsigned int freq, int dir)
539 {
540 	struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(codec_dai);
541 
542 	tas_priv->sysclk = freq;
543 
544 	return 0;
545 }
546 
547 static const struct snd_soc_dai_ops tasdevice_dai_ops = {
548 	.startup = tasdevice_startup,
549 	.hw_params = tasdevice_hw_params,
550 	.set_sysclk = tasdevice_set_dai_sysclk,
551 };
552 
553 static struct snd_soc_dai_driver tasdevice_dai_driver[] = {
554 	{
555 		.name = "tas2781_codec",
556 		.id = 0,
557 		.playback = {
558 			.stream_name = "Playback",
559 			.channels_min = 1,
560 			.channels_max = 4,
561 			.rates	 = TASDEVICE_RATES,
562 			.formats	= TASDEVICE_FORMATS,
563 		},
564 		.capture = {
565 			.stream_name = "Capture",
566 			.channels_min = 1,
567 			.channels_max = 4,
568 			.rates	 = TASDEVICE_RATES,
569 			.formats	= TASDEVICE_FORMATS,
570 		},
571 		.ops = &tasdevice_dai_ops,
572 		.symmetric_rate = 1,
573 	},
574 };
575 
576 static int tasdevice_codec_probe(struct snd_soc_component *codec)
577 {
578 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
579 
580 	return tascodec_init(tas_priv, codec, THIS_MODULE, tasdevice_fw_ready);
581 }
582 
583 static void tasdevice_deinit(void *context)
584 {
585 	struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) context;
586 
587 	tasdevice_config_info_remove(tas_priv);
588 	tasdevice_dsp_remove(tas_priv);
589 	tasdevice_calbin_remove(tas_priv);
590 	tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
591 }
592 
593 static void tasdevice_codec_remove(
594 	struct snd_soc_component *codec)
595 {
596 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
597 
598 	tasdevice_deinit(tas_priv);
599 }
600 
601 static const struct snd_soc_component_driver
602 	soc_codec_driver_tasdevice = {
603 	.probe			= tasdevice_codec_probe,
604 	.remove			= tasdevice_codec_remove,
605 	.controls		= tas2781_snd_controls,
606 	.num_controls		= ARRAY_SIZE(tas2781_snd_controls),
607 	.dapm_widgets		= tasdevice_dapm_widgets,
608 	.num_dapm_widgets	= ARRAY_SIZE(tasdevice_dapm_widgets),
609 	.dapm_routes		= tasdevice_audio_map,
610 	.num_dapm_routes	= ARRAY_SIZE(tasdevice_audio_map),
611 	.idle_bias_on		= 1,
612 	.endianness		= 1,
613 };
614 
615 static void tasdevice_parse_dt(struct tasdevice_priv *tas_priv)
616 {
617 	struct i2c_client *client = (struct i2c_client *)tas_priv->client;
618 	unsigned int dev_addrs[TASDEVICE_MAX_CHANNELS];
619 	int rc, i, ndev = 0;
620 
621 	if (tas_priv->isacpi) {
622 		ndev = device_property_read_u32_array(&client->dev,
623 			"ti,audio-slots", NULL, 0);
624 		if (ndev <= 0) {
625 			ndev = 1;
626 			dev_addrs[0] = client->addr;
627 		} else {
628 			ndev = (ndev < ARRAY_SIZE(dev_addrs))
629 				? ndev : ARRAY_SIZE(dev_addrs);
630 			ndev = device_property_read_u32_array(&client->dev,
631 				"ti,audio-slots", dev_addrs, ndev);
632 		}
633 
634 		tas_priv->irq_info.irq_gpio =
635 			acpi_dev_gpio_irq_get(ACPI_COMPANION(&client->dev), 0);
636 	} else {
637 		struct device_node *np = tas_priv->dev->of_node;
638 #ifdef CONFIG_OF
639 		const __be32 *reg, *reg_end;
640 		int len, sw, aw;
641 
642 		aw = of_n_addr_cells(np);
643 		sw = of_n_size_cells(np);
644 		if (sw == 0) {
645 			reg = (const __be32 *)of_get_property(np,
646 				"reg", &len);
647 			reg_end = reg + len/sizeof(*reg);
648 			ndev = 0;
649 			do {
650 				dev_addrs[ndev] = of_read_number(reg, aw);
651 				reg += aw;
652 				ndev++;
653 			} while (reg < reg_end);
654 		} else {
655 			ndev = 1;
656 			dev_addrs[0] = client->addr;
657 		}
658 #else
659 		ndev = 1;
660 		dev_addrs[0] = client->addr;
661 #endif
662 		tas_priv->irq_info.irq_gpio = of_irq_get(np, 0);
663 	}
664 	tas_priv->ndev = ndev;
665 	for (i = 0; i < ndev; i++)
666 		tas_priv->tasdevice[i].dev_addr = dev_addrs[i];
667 
668 	tas_priv->reset = devm_gpiod_get_optional(&client->dev,
669 			"reset-gpios", GPIOD_OUT_HIGH);
670 	if (IS_ERR(tas_priv->reset))
671 		dev_err(tas_priv->dev, "%s Can't get reset GPIO\n",
672 			__func__);
673 
674 	strcpy(tas_priv->dev_name, tasdevice_id[tas_priv->chip_id].name);
675 
676 	if (gpio_is_valid(tas_priv->irq_info.irq_gpio)) {
677 		rc = gpio_request(tas_priv->irq_info.irq_gpio,
678 				"AUDEV-IRQ");
679 		if (!rc) {
680 			gpio_direction_input(
681 				tas_priv->irq_info.irq_gpio);
682 
683 			tas_priv->irq_info.irq =
684 				gpio_to_irq(tas_priv->irq_info.irq_gpio);
685 		} else
686 			dev_err(tas_priv->dev, "%s: GPIO %d request error\n",
687 				__func__, tas_priv->irq_info.irq_gpio);
688 	} else
689 		dev_err(tas_priv->dev,
690 			"Looking up irq-gpio property failed %d\n",
691 			tas_priv->irq_info.irq_gpio);
692 }
693 
694 static int tasdevice_i2c_probe(struct i2c_client *i2c)
695 {
696 	const struct i2c_device_id *id = i2c_match_id(tasdevice_id, i2c);
697 	const struct acpi_device_id *acpi_id;
698 	struct tasdevice_priv *tas_priv;
699 	int ret;
700 
701 	tas_priv = tasdevice_kzalloc(i2c);
702 	if (!tas_priv)
703 		return -ENOMEM;
704 
705 	dev_set_drvdata(&i2c->dev, tas_priv);
706 
707 	if (ACPI_HANDLE(&i2c->dev)) {
708 		acpi_id = acpi_match_device(i2c->dev.driver->acpi_match_table,
709 				&i2c->dev);
710 		if (!acpi_id) {
711 			dev_err(&i2c->dev, "No driver data\n");
712 			ret = -EINVAL;
713 			goto err;
714 		}
715 		tas_priv->chip_id = acpi_id->driver_data;
716 		tas_priv->isacpi = true;
717 	} else {
718 		tas_priv->chip_id = id ? id->driver_data : 0;
719 		tas_priv->isacpi = false;
720 	}
721 
722 	tasdevice_parse_dt(tas_priv);
723 
724 	ret = tasdevice_init(tas_priv);
725 	if (ret)
726 		goto err;
727 
728 	ret = devm_snd_soc_register_component(tas_priv->dev,
729 		&soc_codec_driver_tasdevice,
730 		tasdevice_dai_driver, ARRAY_SIZE(tasdevice_dai_driver));
731 	if (ret) {
732 		dev_err(tas_priv->dev, "%s: codec register error:0x%08x\n",
733 			__func__, ret);
734 		goto err;
735 	}
736 err:
737 	if (ret < 0)
738 		tasdevice_remove(tas_priv);
739 	return ret;
740 }
741 
742 static void tasdevice_i2c_remove(struct i2c_client *client)
743 {
744 	struct tasdevice_priv *tas_priv = i2c_get_clientdata(client);
745 
746 	tasdevice_remove(tas_priv);
747 }
748 
749 #ifdef CONFIG_ACPI
750 static const struct acpi_device_id tasdevice_acpi_match[] = {
751 	{ "TAS2781", TAS2781 },
752 	{},
753 };
754 
755 MODULE_DEVICE_TABLE(acpi, tasdevice_acpi_match);
756 #endif
757 
758 static struct i2c_driver tasdevice_i2c_driver = {
759 	.driver = {
760 		.name = "tas2781-codec",
761 		.of_match_table = of_match_ptr(tasdevice_of_match),
762 #ifdef CONFIG_ACPI
763 		.acpi_match_table = ACPI_PTR(tasdevice_acpi_match),
764 #endif
765 	},
766 	.probe	= tasdevice_i2c_probe,
767 	.remove = tasdevice_i2c_remove,
768 	.id_table = tasdevice_id,
769 };
770 
771 module_i2c_driver(tasdevice_i2c_driver);
772 
773 MODULE_AUTHOR("Shenghao Ding <shenghao-ding@ti.com>");
774 MODULE_AUTHOR("Kevin Lu <kevin-lu@ti.com>");
775 MODULE_DESCRIPTION("ASoC TAS2781 Driver");
776 MODULE_LICENSE("GPL");
777 MODULE_IMPORT_NS(SND_SOC_TAS2781_FMWLIB);
778