xref: /openbmc/linux/sound/soc/codecs/tas2781-i2c.c (revision 7000e5f3)
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_address.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  */
tas2781_digital_getvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tas2781_digital_putvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tas2781_amp_getvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tas2781_amp_putvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tas2781_force_fwload_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tas2781_force_fwload_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tasdevice_set_profile_id(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tasdevice_info_programs(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)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 
tasdevice_info_configurations(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)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 
tasdevice_info_profile(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)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 
tasdevice_get_profile_id(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tasdevice_create_control(struct tasdevice_priv * tas_priv)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 
tasdevice_program_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tasdevice_program_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tasdevice_configuration_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tasdevice_configuration_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tasdevice_dsp_create_ctrls(struct tasdevice_priv * tas_priv)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 
tasdevice_fw_ready(const struct firmware * fmw,void * context)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 
tasdevice_dapm_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)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 
tasdevice_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)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 
tasdevice_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)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 
tasdevice_set_dai_sysclk(struct snd_soc_dai * codec_dai,int clk_id,unsigned int freq,int dir)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 
tasdevice_codec_probe(struct snd_soc_component * codec)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 
tasdevice_deinit(void * context)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 
tasdevice_codec_remove(struct snd_soc_component * codec)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 
tasdevice_parse_dt(struct tasdevice_priv * tas_priv)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 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 =
635 			acpi_dev_gpio_irq_get(ACPI_COMPANION(&client->dev), 0);
636 	} else if (IS_ENABLED(CONFIG_OF)) {
637 		struct device_node *np = tas_priv->dev->of_node;
638 		u64 addr;
639 
640 		for (i = 0; i < TASDEVICE_MAX_CHANNELS; i++) {
641 			if (of_property_read_reg(np, i, &addr, NULL))
642 				break;
643 			dev_addrs[ndev++] = addr;
644 		}
645 
646 		tas_priv->irq = of_irq_get(np, 0);
647 	} else {
648 		ndev = 1;
649 		dev_addrs[0] = client->addr;
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", 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 
tasdevice_i2c_probe(struct i2c_client * i2c)664 static int tasdevice_i2c_probe(struct i2c_client *i2c)
665 {
666 	const struct i2c_device_id *id = i2c_match_id(tasdevice_id, i2c);
667 	const struct acpi_device_id *acpi_id;
668 	struct tasdevice_priv *tas_priv;
669 	int ret;
670 
671 	tas_priv = tasdevice_kzalloc(i2c);
672 	if (!tas_priv)
673 		return -ENOMEM;
674 
675 	dev_set_drvdata(&i2c->dev, tas_priv);
676 
677 	if (ACPI_HANDLE(&i2c->dev)) {
678 		acpi_id = acpi_match_device(i2c->dev.driver->acpi_match_table,
679 				&i2c->dev);
680 		if (!acpi_id) {
681 			dev_err(&i2c->dev, "No driver data\n");
682 			ret = -EINVAL;
683 			goto err;
684 		}
685 		tas_priv->chip_id = acpi_id->driver_data;
686 		tas_priv->isacpi = true;
687 	} else {
688 		tas_priv->chip_id = id ? id->driver_data : 0;
689 		tas_priv->isacpi = false;
690 	}
691 
692 	tasdevice_parse_dt(tas_priv);
693 
694 	ret = tasdevice_init(tas_priv);
695 	if (ret)
696 		goto err;
697 
698 	ret = devm_snd_soc_register_component(tas_priv->dev,
699 		&soc_codec_driver_tasdevice,
700 		tasdevice_dai_driver, ARRAY_SIZE(tasdevice_dai_driver));
701 	if (ret) {
702 		dev_err(tas_priv->dev, "%s: codec register error:0x%08x\n",
703 			__func__, ret);
704 		goto err;
705 	}
706 err:
707 	if (ret < 0)
708 		tasdevice_remove(tas_priv);
709 	return ret;
710 }
711 
tasdevice_i2c_remove(struct i2c_client * client)712 static void tasdevice_i2c_remove(struct i2c_client *client)
713 {
714 	struct tasdevice_priv *tas_priv = i2c_get_clientdata(client);
715 
716 	tasdevice_remove(tas_priv);
717 }
718 
719 #ifdef CONFIG_ACPI
720 static const struct acpi_device_id tasdevice_acpi_match[] = {
721 	{ "TAS2781", TAS2781 },
722 	{},
723 };
724 
725 MODULE_DEVICE_TABLE(acpi, tasdevice_acpi_match);
726 #endif
727 
728 static struct i2c_driver tasdevice_i2c_driver = {
729 	.driver = {
730 		.name = "tas2781-codec",
731 		.of_match_table = of_match_ptr(tasdevice_of_match),
732 #ifdef CONFIG_ACPI
733 		.acpi_match_table = ACPI_PTR(tasdevice_acpi_match),
734 #endif
735 	},
736 	.probe	= tasdevice_i2c_probe,
737 	.remove = tasdevice_i2c_remove,
738 	.id_table = tasdevice_id,
739 };
740 
741 module_i2c_driver(tasdevice_i2c_driver);
742 
743 MODULE_AUTHOR("Shenghao Ding <shenghao-ding@ti.com>");
744 MODULE_AUTHOR("Kevin Lu <kevin-lu@ti.com>");
745 MODULE_DESCRIPTION("ASoC TAS2781 Driver");
746 MODULE_LICENSE("GPL");
747 MODULE_IMPORT_NS(SND_SOC_TAS2781_FMWLIB);
748