1 // SPDX-License-Identifier: GPL-2.0
3 // ALSA SoC Texas Instruments TAS2563/TAS2781 Audio Smart Amplifier
5 // Copyright (C) 2022 - 2024 Texas Instruments Incorporated
8 // The TAS2563/TAS2781 driver implements a flexible and configurable
9 // algo coefficient setting for one, two, or even multiple
10 // TAS2563/TAS2781 chips.
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>
24 #include <linux/of_address.h>
25 #include <linux/of_gpio.h>
26 #include <linux/of_irq.h>
27 #include <linux/regmap.h>
28 #include <linux/slab.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include <sound/tas2781.h>
32 #include <sound/tlv.h>
33 #include <sound/tas2781-tlv.h>
34 #include <asm/unaligned.h>
36 static const struct i2c_device_id tasdevice_id[] = {
37 { "tas2563", TAS2563 },
38 { "tas2781", TAS2781 },
41 MODULE_DEVICE_TABLE(i2c, tasdevice_id);
44 static const struct of_device_id tasdevice_of_match[] = {
45 { .compatible = "ti,tas2563" },
46 { .compatible = "ti,tas2781" },
49 MODULE_DEVICE_TABLE(of, tasdevice_of_match);
53 * tas2781_digital_getvol - get the volum control
54 * @kcontrol: control pointer
55 * @ucontrol: User data
56 * Customer Kcontrol for tas2781 is primarily for regmap booking, paging
57 * depends on internal regmap mechanism.
58 * tas2781 contains book and page two-level register map, especially
59 * book switching will set the register BXXP00R7F, after switching to the
60 * correct book, then leverage the mechanism for paging to access the
63 static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,
64 struct snd_ctl_elem_value *ucontrol)
66 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
67 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
68 struct soc_mixer_control *mc =
69 (struct soc_mixer_control *)kcontrol->private_value;
71 return tasdevice_digital_getvol(tas_priv, ucontrol, mc);
74 static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,
75 struct snd_ctl_elem_value *ucontrol)
77 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
78 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
79 struct soc_mixer_control *mc =
80 (struct soc_mixer_control *)kcontrol->private_value;
82 return tasdevice_digital_putvol(tas_priv, ucontrol, mc);
85 static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
86 struct snd_ctl_elem_value *ucontrol)
88 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
89 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
90 struct soc_mixer_control *mc =
91 (struct soc_mixer_control *)kcontrol->private_value;
93 return tasdevice_amp_getvol(tas_priv, ucontrol, mc);
96 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
97 struct snd_ctl_elem_value *ucontrol)
99 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
100 struct tasdevice_priv *tas_priv =
101 snd_soc_component_get_drvdata(codec);
102 struct soc_mixer_control *mc =
103 (struct soc_mixer_control *)kcontrol->private_value;
105 return tasdevice_amp_putvol(tas_priv, ucontrol, mc);
108 static int tasdev_force_fwload_get(struct snd_kcontrol *kcontrol,
109 struct snd_ctl_elem_value *ucontrol)
111 struct snd_soc_component *component =
112 snd_soc_kcontrol_component(kcontrol);
113 struct tasdevice_priv *tas_priv =
114 snd_soc_component_get_drvdata(component);
116 ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
117 dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
118 tas_priv->force_fwload_status ? "ON" : "OFF");
123 static int tasdev_force_fwload_put(struct snd_kcontrol *kcontrol,
124 struct snd_ctl_elem_value *ucontrol)
126 struct snd_soc_component *component =
127 snd_soc_kcontrol_component(kcontrol);
128 struct tasdevice_priv *tas_priv =
129 snd_soc_component_get_drvdata(component);
130 bool change, val = (bool)ucontrol->value.integer.value[0];
132 if (tas_priv->force_fwload_status == val)
136 tas_priv->force_fwload_status = val;
138 dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
139 tas_priv->force_fwload_status ? "ON" : "OFF");
144 static int tas2563_digital_gain_get(
145 struct snd_kcontrol *kcontrol,
146 struct snd_ctl_elem_value *ucontrol)
148 struct soc_mixer_control *mc =
149 (struct soc_mixer_control *)kcontrol->private_value;
150 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
151 struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);
152 unsigned int l = 0, r = mc->max;
153 unsigned int target, ar_mid, mid, ar_l, ar_r;
154 unsigned int reg = mc->reg;
155 unsigned char data[4];
158 mutex_lock(&tas_dev->codec_lock);
159 /* Read the primary device */
160 ret = tasdevice_dev_bulk_read(tas_dev, 0, reg, data, 4);
162 dev_err(tas_dev->dev, "%s, get AMP vol error\n", __func__);
166 target = get_unaligned_be32(&data[0]);
170 ar_mid = get_unaligned_be32(tas2563_dvc_table[mid]);
177 ar_l = get_unaligned_be32(tas2563_dvc_table[l]);
178 ar_r = get_unaligned_be32(tas2563_dvc_table[r]);
180 /* find out the member same as or closer to the current volume */
181 ucontrol->value.integer.value[0] =
182 abs(target - ar_l) <= abs(target - ar_r) ? l : r;
184 mutex_unlock(&tas_dev->codec_lock);
188 static int tas2563_digital_gain_put(
189 struct snd_kcontrol *kcontrol,
190 struct snd_ctl_elem_value *ucontrol)
192 struct soc_mixer_control *mc =
193 (struct soc_mixer_control *)kcontrol->private_value;
194 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
195 struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);
196 int vol = ucontrol->value.integer.value[0];
197 int status = 0, max = mc->max, rc = 1;
199 unsigned int reg = mc->reg;
200 unsigned int volrd, volwr;
201 unsigned char data[4];
203 vol = clamp(vol, 0, max);
204 mutex_lock(&tas_dev->codec_lock);
205 /* Read the primary device */
206 ret = tasdevice_dev_bulk_read(tas_dev, 0, reg, data, 4);
208 dev_err(tas_dev->dev, "%s, get AMP vol error\n", __func__);
213 volrd = get_unaligned_be32(&data[0]);
214 volwr = get_unaligned_be32(tas2563_dvc_table[vol]);
216 if (volrd == volwr) {
221 for (i = 0; i < tas_dev->ndev; i++) {
222 ret = tasdevice_dev_bulk_write(tas_dev, i, reg,
223 (unsigned char *)tas2563_dvc_table[vol], 4);
225 dev_err(tas_dev->dev,
226 "%s, set digital vol error in dev %d\n",
235 mutex_unlock(&tas_dev->codec_lock);
239 static const struct snd_kcontrol_new tasdevice_snd_controls[] = {
240 SOC_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
241 tasdev_force_fwload_get, tasdev_force_fwload_put),
244 static const struct snd_kcontrol_new tas2781_snd_controls[] = {
245 SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL,
246 1, 0, 20, 0, tas2781_amp_getvol,
247 tas2781_amp_putvol, amp_vol_tlv),
248 SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Gain", TAS2781_DVC_LVL,
249 0, 0, 200, 1, tas2781_digital_getvol,
250 tas2781_digital_putvol, dvc_tlv),
253 static const struct snd_kcontrol_new tas2563_snd_controls[] = {
254 SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2563_DVC_LVL, 0,
255 0, ARRAY_SIZE(tas2563_dvc_table) - 1, 0,
256 tas2563_digital_gain_get, tas2563_digital_gain_put,
260 static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
261 struct snd_ctl_elem_value *ucontrol)
263 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
264 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
267 if (tas_priv->rcabin.profile_cfg_id !=
268 ucontrol->value.integer.value[0]) {
269 tas_priv->rcabin.profile_cfg_id =
270 ucontrol->value.integer.value[0];
277 static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
278 struct snd_ctl_elem_info *uinfo)
280 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
281 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
282 struct tasdevice_fw *tas_fw = tas_priv->fmw;
284 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
286 uinfo->value.integer.min = 0;
287 uinfo->value.integer.max = (int)tas_fw->nr_programs;
292 static int tasdevice_info_configurations(
293 struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
295 struct snd_soc_component *codec =
296 snd_soc_kcontrol_component(kcontrol);
297 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
298 struct tasdevice_fw *tas_fw = tas_priv->fmw;
300 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
302 uinfo->value.integer.min = 0;
303 uinfo->value.integer.max = (int)tas_fw->nr_configurations - 1;
308 static int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
309 struct snd_ctl_elem_info *uinfo)
311 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
312 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
314 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
316 uinfo->value.integer.min = 0;
317 uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;
322 static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
323 struct snd_ctl_elem_value *ucontrol)
325 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
326 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
328 ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;
333 static int tasdevice_create_control(struct tasdevice_priv *tas_priv)
335 struct snd_kcontrol_new *prof_ctrls;
341 prof_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,
342 sizeof(prof_ctrls[0]), GFP_KERNEL);
348 /* Create a mixer item for selecting the active profile */
349 name = devm_kzalloc(tas_priv->dev, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
355 scnprintf(name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "Speaker Profile Id");
356 prof_ctrls[mix_index].name = name;
357 prof_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
358 prof_ctrls[mix_index].info = tasdevice_info_profile;
359 prof_ctrls[mix_index].get = tasdevice_get_profile_id;
360 prof_ctrls[mix_index].put = tasdevice_set_profile_id;
363 ret = snd_soc_add_component_controls(tas_priv->codec,
364 prof_ctrls, nr_controls < mix_index ? nr_controls : mix_index);
370 static int tasdevice_program_get(struct snd_kcontrol *kcontrol,
371 struct snd_ctl_elem_value *ucontrol)
373 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
374 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
376 ucontrol->value.integer.value[0] = tas_priv->cur_prog;
381 static int tasdevice_program_put(struct snd_kcontrol *kcontrol,
382 struct snd_ctl_elem_value *ucontrol)
384 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
385 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
386 unsigned int nr_program = ucontrol->value.integer.value[0];
389 if (tas_priv->cur_prog != nr_program) {
390 tas_priv->cur_prog = nr_program;
397 static int tasdevice_configuration_get(struct snd_kcontrol *kcontrol,
398 struct snd_ctl_elem_value *ucontrol)
401 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
402 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
404 ucontrol->value.integer.value[0] = tas_priv->cur_conf;
409 static int tasdevice_configuration_put(
410 struct snd_kcontrol *kcontrol,
411 struct snd_ctl_elem_value *ucontrol)
413 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
414 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
415 unsigned int nr_configuration = ucontrol->value.integer.value[0];
418 if (tas_priv->cur_conf != nr_configuration) {
419 tas_priv->cur_conf = nr_configuration;
426 static int tasdevice_dsp_create_ctrls(
427 struct tasdevice_priv *tas_priv)
429 struct snd_kcontrol_new *dsp_ctrls;
430 char *prog_name, *conf_name;
435 /* Alloc kcontrol via devm_kzalloc, which don't manually
438 dsp_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,
439 sizeof(dsp_ctrls[0]), GFP_KERNEL);
445 /* Create a mixer item for selecting the active profile */
446 prog_name = devm_kzalloc(tas_priv->dev,
447 SNDRV_CTL_ELEM_ID_NAME_MAXLEN, GFP_KERNEL);
448 conf_name = devm_kzalloc(tas_priv->dev, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
450 if (!prog_name || !conf_name) {
455 scnprintf(prog_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
456 "Speaker Program Id");
457 dsp_ctrls[mix_index].name = prog_name;
458 dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
459 dsp_ctrls[mix_index].info = tasdevice_info_programs;
460 dsp_ctrls[mix_index].get = tasdevice_program_get;
461 dsp_ctrls[mix_index].put = tasdevice_program_put;
464 scnprintf(conf_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN,
465 "Speaker Config Id");
466 dsp_ctrls[mix_index].name = conf_name;
467 dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
468 dsp_ctrls[mix_index].info = tasdevice_info_configurations;
469 dsp_ctrls[mix_index].get = tasdevice_configuration_get;
470 dsp_ctrls[mix_index].put = tasdevice_configuration_put;
473 ret = snd_soc_add_component_controls(tas_priv->codec, dsp_ctrls,
474 nr_controls < mix_index ? nr_controls : mix_index);
480 static void tasdevice_fw_ready(const struct firmware *fmw,
483 struct tasdevice_priv *tas_priv = context;
487 mutex_lock(&tas_priv->codec_lock);
489 ret = tasdevice_rca_parser(tas_priv, fmw);
491 tasdevice_config_info_remove(tas_priv);
494 tasdevice_create_control(tas_priv);
496 tasdevice_dsp_remove(tas_priv);
497 tasdevice_calbin_remove(tas_priv);
499 * The baseline is the RCA-only case, and then the code attempts to
500 * load DSP firmware but in case of failures just keep going, i.e.
501 * failing to load DSP firmware is NOT an error.
503 tas_priv->fw_state = TASDEVICE_RCA_FW_OK;
504 if (tas_priv->name_prefix)
505 scnprintf(tas_priv->rca_binaryname, 64, "%s-%s_coef.bin",
506 tas_priv->name_prefix, tas_priv->dev_name);
508 scnprintf(tas_priv->coef_binaryname, 64, "%s_coef.bin",
510 ret = tasdevice_dsp_parser(tas_priv);
512 dev_err(tas_priv->dev, "dspfw load %s error\n",
513 tas_priv->coef_binaryname);
518 * If no dsp-related kcontrol created, the dsp resource will be freed.
520 ret = tasdevice_dsp_create_ctrls(tas_priv);
522 dev_err(tas_priv->dev, "dsp controls error\n");
526 tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
528 /* If calibrated data occurs error, dsp will still works with default
529 * calibrated data inside algo.
531 for (i = 0; i < tas_priv->ndev; i++) {
532 if (tas_priv->name_prefix)
533 scnprintf(tas_priv->cal_binaryname[i], 64,
534 "%s-%s_cal_0x%02x.bin", tas_priv->name_prefix,
536 tas_priv->tasdevice[i].dev_addr);
538 scnprintf(tas_priv->cal_binaryname[i], 64,
539 "%s_cal_0x%02x.bin", tas_priv->dev_name,
540 tas_priv->tasdevice[i].dev_addr);
541 ret = tas2781_load_calibration(tas_priv,
542 tas_priv->cal_binaryname[i], i);
544 dev_err(tas_priv->dev,
545 "%s: load %s error, default will effect\n",
546 __func__, tas_priv->cal_binaryname[i]);
549 tasdevice_prmg_load(tas_priv, 0);
550 tas_priv->cur_prog = 0;
552 if (tas_priv->fw_state == TASDEVICE_RCA_FW_OK) {
553 /* If DSP FW fail, DSP kcontrol won't be created. */
554 tasdevice_dsp_remove(tas_priv);
556 mutex_unlock(&tas_priv->codec_lock);
558 release_firmware(fmw);
561 static int tasdevice_dapm_event(struct snd_soc_dapm_widget *w,
562 struct snd_kcontrol *kcontrol, int event)
564 struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm);
565 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
568 /* Codec Lock Hold */
569 mutex_lock(&tas_priv->codec_lock);
570 if (event == SND_SOC_DAPM_PRE_PMD)
572 tasdevice_tuning_switch(tas_priv, state);
573 /* Codec Lock Release*/
574 mutex_unlock(&tas_priv->codec_lock);
579 static const struct snd_soc_dapm_widget tasdevice_dapm_widgets[] = {
580 SND_SOC_DAPM_AIF_IN("ASI", "ASI Playback", 0, SND_SOC_NOPM, 0, 0),
581 SND_SOC_DAPM_AIF_OUT_E("ASI OUT", "ASI Capture", 0, SND_SOC_NOPM,
582 0, 0, tasdevice_dapm_event,
583 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
584 SND_SOC_DAPM_SPK("SPK", tasdevice_dapm_event),
585 SND_SOC_DAPM_OUTPUT("OUT"),
586 SND_SOC_DAPM_INPUT("DMIC")
589 static const struct snd_soc_dapm_route tasdevice_audio_map[] = {
590 {"SPK", NULL, "ASI"},
591 {"OUT", NULL, "SPK"},
592 {"ASI OUT", NULL, "DMIC"}
595 static int tasdevice_startup(struct snd_pcm_substream *substream,
596 struct snd_soc_dai *dai)
598 struct snd_soc_component *codec = dai->component;
599 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
601 switch (tas_priv->fw_state) {
602 case TASDEVICE_RCA_FW_OK:
603 case TASDEVICE_DSP_FW_ALL_OK:
610 static int tasdevice_hw_params(struct snd_pcm_substream *substream,
611 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
613 struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(dai);
614 unsigned int slot_width;
619 fsrate = params_rate(params);
625 dev_err(tas_priv->dev, "%s: incorrect sample rate = %u\n",
631 slot_width = params_width(params);
632 switch (slot_width) {
639 dev_err(tas_priv->dev, "%s: incorrect slot width = %u\n",
640 __func__, slot_width);
645 bclk_rate = snd_soc_params_to_bclk(params);
647 dev_err(tas_priv->dev, "%s: incorrect bclk rate = %d\n",
648 __func__, bclk_rate);
657 static int tasdevice_set_dai_sysclk(struct snd_soc_dai *codec_dai,
658 int clk_id, unsigned int freq, int dir)
660 struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(codec_dai);
662 tas_priv->sysclk = freq;
667 static const struct snd_soc_dai_ops tasdevice_dai_ops = {
668 .startup = tasdevice_startup,
669 .hw_params = tasdevice_hw_params,
670 .set_sysclk = tasdevice_set_dai_sysclk,
673 static struct snd_soc_dai_driver tasdevice_dai_driver[] = {
675 .name = "tas2781_codec",
678 .stream_name = "Playback",
681 .rates = TASDEVICE_RATES,
682 .formats = TASDEVICE_FORMATS,
685 .stream_name = "Capture",
688 .rates = TASDEVICE_RATES,
689 .formats = TASDEVICE_FORMATS,
691 .ops = &tasdevice_dai_ops,
696 static int tasdevice_codec_probe(struct snd_soc_component *codec)
698 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
699 struct snd_kcontrol_new *p;
703 switch (tas_priv->chip_id) {
705 p = (struct snd_kcontrol_new *)tas2781_snd_controls;
706 size = ARRAY_SIZE(tas2781_snd_controls);
709 p = (struct snd_kcontrol_new *)tas2563_snd_controls;
710 size = ARRAY_SIZE(tas2563_snd_controls);
713 rc = snd_soc_add_component_controls(codec, p, size);
715 dev_err(tas_priv->dev, "%s: Add control err rc = %d",
720 tas_priv->name_prefix = codec->name_prefix;
721 return tascodec_init(tas_priv, codec, THIS_MODULE, tasdevice_fw_ready);
724 static void tasdevice_deinit(void *context)
726 struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) context;
728 tasdevice_config_info_remove(tas_priv);
729 tasdevice_dsp_remove(tas_priv);
730 tasdevice_calbin_remove(tas_priv);
731 tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
734 static void tasdevice_codec_remove(
735 struct snd_soc_component *codec)
737 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
739 tasdevice_deinit(tas_priv);
742 static const struct snd_soc_component_driver
743 soc_codec_driver_tasdevice = {
744 .probe = tasdevice_codec_probe,
745 .remove = tasdevice_codec_remove,
746 .controls = tasdevice_snd_controls,
747 .num_controls = ARRAY_SIZE(tasdevice_snd_controls),
748 .dapm_widgets = tasdevice_dapm_widgets,
749 .num_dapm_widgets = ARRAY_SIZE(tasdevice_dapm_widgets),
750 .dapm_routes = tasdevice_audio_map,
751 .num_dapm_routes = ARRAY_SIZE(tasdevice_audio_map),
756 static void tasdevice_parse_dt(struct tasdevice_priv *tas_priv)
758 struct i2c_client *client = (struct i2c_client *)tas_priv->client;
759 unsigned int dev_addrs[TASDEVICE_MAX_CHANNELS];
762 if (tas_priv->isacpi) {
763 ndev = device_property_read_u32_array(&client->dev,
764 "ti,audio-slots", NULL, 0);
767 dev_addrs[0] = client->addr;
769 ndev = (ndev < ARRAY_SIZE(dev_addrs))
770 ? ndev : ARRAY_SIZE(dev_addrs);
771 ndev = device_property_read_u32_array(&client->dev,
772 "ti,audio-slots", dev_addrs, ndev);
775 tas_priv->irq_info.irq_gpio =
776 acpi_dev_gpio_irq_get(ACPI_COMPANION(&client->dev), 0);
777 } else if (IS_ENABLED(CONFIG_OF)) {
778 struct device_node *np = tas_priv->dev->of_node;
781 for (i = 0; i < TASDEVICE_MAX_CHANNELS; i++) {
782 if (of_property_read_reg(np, i, &addr, NULL))
784 dev_addrs[ndev++] = addr;
787 tas_priv->irq_info.irq_gpio = of_irq_get(np, 0);
790 dev_addrs[0] = client->addr;
792 tas_priv->ndev = ndev;
793 for (i = 0; i < ndev; i++)
794 tas_priv->tasdevice[i].dev_addr = dev_addrs[i];
796 tas_priv->reset = devm_gpiod_get_optional(&client->dev,
797 "reset-gpios", GPIOD_OUT_HIGH);
798 if (IS_ERR(tas_priv->reset))
799 dev_err(tas_priv->dev, "%s Can't get reset GPIO\n",
802 strcpy(tas_priv->dev_name, tasdevice_id[tas_priv->chip_id].name);
804 if (gpio_is_valid(tas_priv->irq_info.irq_gpio)) {
805 rc = gpio_request(tas_priv->irq_info.irq_gpio,
808 gpio_direction_input(
809 tas_priv->irq_info.irq_gpio);
811 tas_priv->irq_info.irq =
812 gpio_to_irq(tas_priv->irq_info.irq_gpio);
814 dev_err(tas_priv->dev, "%s: GPIO %d request error\n",
815 __func__, tas_priv->irq_info.irq_gpio);
817 dev_err(tas_priv->dev,
818 "Looking up irq-gpio property failed %d\n",
819 tas_priv->irq_info.irq_gpio);
822 static int tasdevice_i2c_probe(struct i2c_client *i2c)
824 const struct i2c_device_id *id = i2c_match_id(tasdevice_id, i2c);
825 const struct acpi_device_id *acpi_id;
826 struct tasdevice_priv *tas_priv;
829 tas_priv = tasdevice_kzalloc(i2c);
833 dev_set_drvdata(&i2c->dev, tas_priv);
835 if (ACPI_HANDLE(&i2c->dev)) {
836 acpi_id = acpi_match_device(i2c->dev.driver->acpi_match_table,
839 dev_err(&i2c->dev, "No driver data\n");
843 tas_priv->chip_id = acpi_id->driver_data;
844 tas_priv->isacpi = true;
846 tas_priv->chip_id = id ? id->driver_data : 0;
847 tas_priv->isacpi = false;
850 tasdevice_parse_dt(tas_priv);
852 ret = tasdevice_init(tas_priv);
856 tasdevice_reset(tas_priv);
858 ret = devm_snd_soc_register_component(tas_priv->dev,
859 &soc_codec_driver_tasdevice,
860 tasdevice_dai_driver, ARRAY_SIZE(tasdevice_dai_driver));
862 dev_err(tas_priv->dev, "%s: codec register error:0x%08x\n",
868 tasdevice_remove(tas_priv);
872 static void tasdevice_i2c_remove(struct i2c_client *client)
874 struct tasdevice_priv *tas_priv = i2c_get_clientdata(client);
876 tasdevice_remove(tas_priv);
880 static const struct acpi_device_id tasdevice_acpi_match[] = {
881 { "TAS2781", TAS2781 },
885 MODULE_DEVICE_TABLE(acpi, tasdevice_acpi_match);
888 static struct i2c_driver tasdevice_i2c_driver = {
890 .name = "tasdev-codec",
891 .of_match_table = of_match_ptr(tasdevice_of_match),
893 .acpi_match_table = ACPI_PTR(tasdevice_acpi_match),
896 .probe = tasdevice_i2c_probe,
897 .remove = tasdevice_i2c_remove,
898 .id_table = tasdevice_id,
901 module_i2c_driver(tasdevice_i2c_driver);
905 MODULE_DESCRIPTION("ASoC TAS2781 Driver");
906 MODULE_LICENSE("GPL");
907 MODULE_IMPORT_NS(SND_SOC_TAS2781_FMWLIB);