xref: /openbmc/linux/sound/soc/codecs/tas2781-i2c.c (revision 6db6b729)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // ALSA SoC Texas Instruments TAS2781 Audio Smart Amplifier
4 //
5 // Copyright (C) 2022 - 2023 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 		goto out;
383 	tasdevice_create_control(tas_priv);
384 
385 	tasdevice_dsp_remove(tas_priv);
386 	tasdevice_calbin_remove(tas_priv);
387 	tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
388 	scnprintf(tas_priv->coef_binaryname, 64, "%s_coef.bin",
389 		tas_priv->dev_name);
390 	ret = tasdevice_dsp_parser(tas_priv);
391 	if (ret) {
392 		dev_err(tas_priv->dev, "dspfw load %s error\n",
393 			tas_priv->coef_binaryname);
394 		tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
395 		goto out;
396 	}
397 	tasdevice_dsp_create_ctrls(tas_priv);
398 
399 	tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
400 
401 	/* If calibrated data occurs error, dsp will still works with default
402 	 * calibrated data inside algo.
403 	 */
404 	for (i = 0; i < tas_priv->ndev; i++) {
405 		scnprintf(tas_priv->cal_binaryname[i], 64, "%s_cal_0x%02x.bin",
406 			tas_priv->dev_name, tas_priv->tasdevice[i].dev_addr);
407 		ret = tas2781_load_calibration(tas_priv,
408 			tas_priv->cal_binaryname[i], i);
409 		if (ret != 0)
410 			dev_err(tas_priv->dev,
411 				"%s: load %s error, default will effect\n",
412 				__func__, tas_priv->cal_binaryname[i]);
413 	}
414 
415 	tasdevice_prmg_calibdata_load(tas_priv, 0);
416 	tas_priv->cur_prog = 0;
417 out:
418 	if (tas_priv->fw_state == TASDEVICE_DSP_FW_FAIL) {
419 		/*If DSP FW fail, kcontrol won't be created */
420 		tasdevice_config_info_remove(tas_priv);
421 		tasdevice_dsp_remove(tas_priv);
422 	}
423 	mutex_unlock(&tas_priv->codec_lock);
424 	if (fmw)
425 		release_firmware(fmw);
426 }
427 
428 static int tasdevice_dapm_event(struct snd_soc_dapm_widget *w,
429 			struct snd_kcontrol *kcontrol, int event)
430 {
431 	struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm);
432 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
433 	int state = 0;
434 
435 	/* Codec Lock Hold */
436 	mutex_lock(&tas_priv->codec_lock);
437 	if (event == SND_SOC_DAPM_PRE_PMD)
438 		state = 1;
439 	tasdevice_tuning_switch(tas_priv, state);
440 	/* Codec Lock Release*/
441 	mutex_unlock(&tas_priv->codec_lock);
442 
443 	return 0;
444 }
445 
446 static const struct snd_soc_dapm_widget tasdevice_dapm_widgets[] = {
447 	SND_SOC_DAPM_AIF_IN("ASI", "ASI Playback", 0, SND_SOC_NOPM, 0, 0),
448 	SND_SOC_DAPM_AIF_OUT_E("ASI OUT", "ASI Capture", 0, SND_SOC_NOPM,
449 		0, 0, tasdevice_dapm_event,
450 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
451 	SND_SOC_DAPM_SPK("SPK", tasdevice_dapm_event),
452 	SND_SOC_DAPM_OUTPUT("OUT"),
453 	SND_SOC_DAPM_INPUT("DMIC")
454 };
455 
456 static const struct snd_soc_dapm_route tasdevice_audio_map[] = {
457 	{"SPK", NULL, "ASI"},
458 	{"OUT", NULL, "SPK"},
459 	{"ASI OUT", NULL, "DMIC"}
460 };
461 
462 static int tasdevice_startup(struct snd_pcm_substream *substream,
463 						struct snd_soc_dai *dai)
464 {
465 	struct snd_soc_component *codec = dai->component;
466 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
467 	int ret = 0;
468 
469 	if (tas_priv->fw_state != TASDEVICE_DSP_FW_ALL_OK) {
470 		dev_err(tas_priv->dev, "DSP bin file not loaded\n");
471 		ret = -EINVAL;
472 	}
473 
474 	return ret;
475 }
476 
477 static int tasdevice_hw_params(struct snd_pcm_substream *substream,
478 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
479 {
480 	struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(dai);
481 	unsigned int slot_width;
482 	unsigned int fsrate;
483 	int bclk_rate;
484 	int rc = 0;
485 
486 	fsrate = params_rate(params);
487 	switch (fsrate) {
488 	case 48000:
489 	case 44100:
490 		break;
491 	default:
492 		dev_err(tas_priv->dev, "%s: incorrect sample rate = %u\n",
493 			__func__, fsrate);
494 		rc = -EINVAL;
495 		goto out;
496 	}
497 
498 	slot_width = params_width(params);
499 	switch (slot_width) {
500 	case 16:
501 	case 20:
502 	case 24:
503 	case 32:
504 		break;
505 	default:
506 		dev_err(tas_priv->dev, "%s: incorrect slot width = %u\n",
507 			__func__, slot_width);
508 		rc = -EINVAL;
509 		goto out;
510 	}
511 
512 	bclk_rate = snd_soc_params_to_bclk(params);
513 	if (bclk_rate < 0) {
514 		dev_err(tas_priv->dev, "%s: incorrect bclk rate = %d\n",
515 			__func__, bclk_rate);
516 		rc = bclk_rate;
517 		goto out;
518 	}
519 
520 out:
521 	return rc;
522 }
523 
524 static int tasdevice_set_dai_sysclk(struct snd_soc_dai *codec_dai,
525 	int clk_id, unsigned int freq, int dir)
526 {
527 	struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(codec_dai);
528 
529 	tas_priv->sysclk = freq;
530 
531 	return 0;
532 }
533 
534 static const struct snd_soc_dai_ops tasdevice_dai_ops = {
535 	.startup = tasdevice_startup,
536 	.hw_params = tasdevice_hw_params,
537 	.set_sysclk = tasdevice_set_dai_sysclk,
538 };
539 
540 static struct snd_soc_dai_driver tasdevice_dai_driver[] = {
541 	{
542 		.name = "tas2781_codec",
543 		.id = 0,
544 		.playback = {
545 			.stream_name = "Playback",
546 			.channels_min = 1,
547 			.channels_max = 4,
548 			.rates	 = TASDEVICE_RATES,
549 			.formats	= TASDEVICE_FORMATS,
550 		},
551 		.capture = {
552 			.stream_name = "Capture",
553 			.channels_min = 1,
554 			.channels_max = 4,
555 			.rates	 = TASDEVICE_RATES,
556 			.formats	= TASDEVICE_FORMATS,
557 		},
558 		.ops = &tasdevice_dai_ops,
559 		.symmetric_rate = 1,
560 	},
561 };
562 
563 static int tasdevice_codec_probe(struct snd_soc_component *codec)
564 {
565 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
566 
567 	return tascodec_init(tas_priv, codec, tasdevice_fw_ready);
568 }
569 
570 static void tasdevice_deinit(void *context)
571 {
572 	struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) context;
573 
574 	tasdevice_config_info_remove(tas_priv);
575 	tasdevice_dsp_remove(tas_priv);
576 	tasdevice_calbin_remove(tas_priv);
577 	tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
578 }
579 
580 static void tasdevice_codec_remove(
581 	struct snd_soc_component *codec)
582 {
583 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
584 
585 	tasdevice_deinit(tas_priv);
586 }
587 
588 static const struct snd_soc_component_driver
589 	soc_codec_driver_tasdevice = {
590 	.probe			= tasdevice_codec_probe,
591 	.remove			= tasdevice_codec_remove,
592 	.controls		= tas2781_snd_controls,
593 	.num_controls		= ARRAY_SIZE(tas2781_snd_controls),
594 	.dapm_widgets		= tasdevice_dapm_widgets,
595 	.num_dapm_widgets	= ARRAY_SIZE(tasdevice_dapm_widgets),
596 	.dapm_routes		= tasdevice_audio_map,
597 	.num_dapm_routes	= ARRAY_SIZE(tasdevice_audio_map),
598 	.idle_bias_on		= 1,
599 	.endianness		= 1,
600 };
601 
602 static void tasdevice_parse_dt(struct tasdevice_priv *tas_priv)
603 {
604 	struct i2c_client *client = (struct i2c_client *)tas_priv->client;
605 	unsigned int dev_addrs[TASDEVICE_MAX_CHANNELS];
606 	int rc, i, ndev = 0;
607 
608 	if (tas_priv->isacpi) {
609 		ndev = device_property_read_u32_array(&client->dev,
610 			"ti,audio-slots", NULL, 0);
611 		if (ndev <= 0) {
612 			ndev = 1;
613 			dev_addrs[0] = client->addr;
614 		} else {
615 			ndev = (ndev < ARRAY_SIZE(dev_addrs))
616 				? ndev : ARRAY_SIZE(dev_addrs);
617 			ndev = device_property_read_u32_array(&client->dev,
618 				"ti,audio-slots", dev_addrs, ndev);
619 		}
620 
621 		tas_priv->irq_info.irq_gpio =
622 			acpi_dev_gpio_irq_get(ACPI_COMPANION(&client->dev), 0);
623 	} else {
624 		struct device_node *np = tas_priv->dev->of_node;
625 #ifdef CONFIG_OF
626 		const __be32 *reg, *reg_end;
627 		int len, sw, aw;
628 
629 		aw = of_n_addr_cells(np);
630 		sw = of_n_size_cells(np);
631 		if (sw == 0) {
632 			reg = (const __be32 *)of_get_property(np,
633 				"reg", &len);
634 			reg_end = reg + len/sizeof(*reg);
635 			ndev = 0;
636 			do {
637 				dev_addrs[ndev] = of_read_number(reg, aw);
638 				reg += aw;
639 				ndev++;
640 			} while (reg < reg_end);
641 		} else {
642 			ndev = 1;
643 			dev_addrs[0] = client->addr;
644 		}
645 #else
646 		ndev = 1;
647 		dev_addrs[0] = client->addr;
648 #endif
649 		tas_priv->irq_info.irq_gpio = of_irq_get(np, 0);
650 	}
651 	tas_priv->ndev = ndev;
652 	for (i = 0; i < ndev; i++)
653 		tas_priv->tasdevice[i].dev_addr = dev_addrs[i];
654 
655 	tas_priv->reset = devm_gpiod_get_optional(&client->dev,
656 			"reset-gpios", GPIOD_OUT_HIGH);
657 	if (IS_ERR(tas_priv->reset))
658 		dev_err(tas_priv->dev, "%s Can't get reset GPIO\n",
659 			__func__);
660 
661 	strcpy(tas_priv->dev_name, tasdevice_id[tas_priv->chip_id].name);
662 
663 	if (gpio_is_valid(tas_priv->irq_info.irq_gpio)) {
664 		rc = gpio_request(tas_priv->irq_info.irq_gpio,
665 				"AUDEV-IRQ");
666 		if (!rc) {
667 			gpio_direction_input(
668 				tas_priv->irq_info.irq_gpio);
669 
670 			tas_priv->irq_info.irq =
671 				gpio_to_irq(tas_priv->irq_info.irq_gpio);
672 		} else
673 			dev_err(tas_priv->dev, "%s: GPIO %d request error\n",
674 				__func__, tas_priv->irq_info.irq_gpio);
675 	} else
676 		dev_err(tas_priv->dev,
677 			"Looking up irq-gpio property failed %d\n",
678 			tas_priv->irq_info.irq_gpio);
679 }
680 
681 static int tasdevice_i2c_probe(struct i2c_client *i2c)
682 {
683 	const struct i2c_device_id *id = i2c_match_id(tasdevice_id, i2c);
684 	const struct acpi_device_id *acpi_id;
685 	struct tasdevice_priv *tas_priv;
686 	int ret;
687 
688 	tas_priv = tasdevice_kzalloc(i2c);
689 	if (!tas_priv)
690 		return -ENOMEM;
691 
692 	if (ACPI_HANDLE(&i2c->dev)) {
693 		acpi_id = acpi_match_device(i2c->dev.driver->acpi_match_table,
694 				&i2c->dev);
695 		if (!acpi_id) {
696 			dev_err(&i2c->dev, "No driver data\n");
697 			ret = -EINVAL;
698 			goto err;
699 		}
700 		tas_priv->chip_id = acpi_id->driver_data;
701 		tas_priv->isacpi = true;
702 	} else {
703 		tas_priv->chip_id = id ? id->driver_data : 0;
704 		tas_priv->isacpi = false;
705 	}
706 
707 	tasdevice_parse_dt(tas_priv);
708 
709 	ret = tasdevice_init(tas_priv);
710 	if (ret)
711 		goto err;
712 
713 	ret = devm_snd_soc_register_component(tas_priv->dev,
714 		&soc_codec_driver_tasdevice,
715 		tasdevice_dai_driver, ARRAY_SIZE(tasdevice_dai_driver));
716 	if (ret) {
717 		dev_err(tas_priv->dev, "%s: codec register error:0x%08x\n",
718 			__func__, ret);
719 		goto err;
720 	}
721 err:
722 	if (ret < 0)
723 		tasdevice_remove(tas_priv);
724 	return ret;
725 }
726 
727 static void tasdevice_i2c_remove(struct i2c_client *client)
728 {
729 	struct tasdevice_priv *tas_priv = i2c_get_clientdata(client);
730 
731 	tasdevice_remove(tas_priv);
732 }
733 
734 #ifdef CONFIG_ACPI
735 static const struct acpi_device_id tasdevice_acpi_match[] = {
736 	{ "TAS2781", TAS2781 },
737 	{},
738 };
739 
740 MODULE_DEVICE_TABLE(acpi, tasdevice_acpi_match);
741 #endif
742 
743 static struct i2c_driver tasdevice_i2c_driver = {
744 	.driver = {
745 		.name = "tas2781-codec",
746 		.of_match_table = of_match_ptr(tasdevice_of_match),
747 #ifdef CONFIG_ACPI
748 		.acpi_match_table = ACPI_PTR(tasdevice_acpi_match),
749 #endif
750 	},
751 	.probe	= tasdevice_i2c_probe,
752 	.remove = tasdevice_i2c_remove,
753 	.id_table = tasdevice_id,
754 };
755 
756 module_i2c_driver(tasdevice_i2c_driver);
757 
758 MODULE_AUTHOR("Shenghao Ding <shenghao-ding@ti.com>");
759 MODULE_AUTHOR("Kevin Lu <kevin-lu@ti.com>");
760 MODULE_DESCRIPTION("ASoC TAS2781 Driver");
761 MODULE_LICENSE("GPL");
762 MODULE_IMPORT_NS(SND_SOC_TAS2781_FMWLIB);
763