1 // SPDX-License-Identifier: GPL-2.0
3 // TAS2781 HDA I2C driver
5 // Copyright 2023 Texas Instruments, Inc.
7 // Author: Shenghao Ding <shenghao-ding@ti.com>
9 #include <linux/acpi.h>
10 #include <linux/crc8.h>
11 #include <linux/crc32.h>
12 #include <linux/efi.h>
13 #include <linux/firmware.h>
14 #include <linux/i2c.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/regmap.h>
19 #include <sound/hda_codec.h>
20 #include <sound/soc.h>
21 #include <sound/tas2781.h>
22 #include <sound/tlv.h>
23 #include <sound/tas2781-tlv.h>
25 #include "hda_local.h"
26 #include "hda_auto_parser.h"
27 #include "hda_component.h"
29 #include "hda_generic.h"
31 #define TASDEVICE_SPEAKER_CALIBRATION_SIZE 20
33 /* No standard control callbacks for SNDRV_CTL_ELEM_IFACE_CARD
34 * Define two controls, one is Volume control callbacks, the other is
35 * flag setting control callbacks.
38 /* Volume control callbacks for tas2781 */
39 #define ACARD_SINGLE_RANGE_EXT_TLV(xname, xreg, xshift, xmin, xmax, xinvert, \
40 xhandler_get, xhandler_put, tlv_array) \
41 { .iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = (xname),\
42 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
43 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
44 .tlv.p = (tlv_array), \
45 .info = snd_soc_info_volsw_range, \
46 .get = xhandler_get, .put = xhandler_put, \
47 .private_value = (unsigned long)&(struct soc_mixer_control) \
48 {.reg = xreg, .rreg = xreg, .shift = xshift, \
49 .rshift = xshift, .min = xmin, .max = xmax, \
52 /* Flag control callbacks for tas2781 */
53 #define ACARD_SINGLE_BOOL_EXT(xname, xdata, xhandler_get, xhandler_put) \
54 { .iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = xname, \
55 .info = snd_ctl_boolean_mono_info, \
56 .get = xhandler_get, .put = xhandler_put, \
57 .private_value = xdata }
68 #define TAS2563_MAX_CHANNELS 4
70 #define TAS2563_CAL_POWER TASDEVICE_REG(0, 0x0d, 0x3c)
71 #define TAS2563_CAL_R0 TASDEVICE_REG(0, 0x0f, 0x34)
72 #define TAS2563_CAL_INVR0 TASDEVICE_REG(0, 0x0f, 0x40)
73 #define TAS2563_CAL_R0_LOW TASDEVICE_REG(0, 0x0f, 0x48)
74 #define TAS2563_CAL_TLIM TASDEVICE_REG(0, 0x10, 0x14)
75 #define TAS2563_CAL_N 5
76 #define TAS2563_CAL_DATA_SIZE 4
77 #define TAS2563_CAL_CH_SIZE 20
78 #define TAS2563_CAL_ARRAY_SIZE 80
80 static unsigned int cal_regs[TAS2563_CAL_N] = {
81 TAS2563_CAL_POWER, TAS2563_CAL_R0, TAS2563_CAL_INVR0,
82 TAS2563_CAL_R0_LOW, TAS2563_CAL_TLIM,
88 struct tasdevice_priv *priv;
89 struct snd_kcontrol *dsp_prog_ctl;
90 struct snd_kcontrol *dsp_conf_ctl;
91 struct snd_kcontrol *prof_ctl;
92 struct snd_kcontrol *snd_ctls[2];
95 static int tas2781_get_i2c_res(struct acpi_resource *ares, void *data)
97 struct tasdevice_priv *tas_priv = data;
98 struct acpi_resource_i2c_serialbus *sb;
100 if (i2c_acpi_get_i2c_resource(ares, &sb)) {
101 if (tas_priv->ndev < TASDEVICE_MAX_CHANNELS &&
102 sb->slave_address != tas_priv->global_addr) {
103 tas_priv->tasdevice[tas_priv->ndev].dev_addr =
104 (unsigned int)sb->slave_address;
111 static int tas2781_read_acpi(struct tasdevice_priv *p, const char *hid)
113 struct acpi_device *adev;
114 LIST_HEAD(resources);
117 adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
120 "Failed to find an ACPI device for %s\n", hid);
124 ret = acpi_dev_get_resources(adev, &resources, tas2781_get_i2c_res, p);
128 acpi_dev_free_resource_list(&resources);
129 strscpy(p->dev_name, hid, sizeof(p->dev_name));
135 dev_err(p->dev, "read acpi error, ret: %d\n", ret);
141 static void tas2781_hda_playback_hook(struct device *dev, int action)
143 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
145 dev_dbg(tas_hda->dev, "%s: action = %d\n", __func__, action);
147 case HDA_GEN_PCM_ACT_OPEN:
148 pm_runtime_get_sync(dev);
149 mutex_lock(&tas_hda->priv->codec_lock);
150 tasdevice_tuning_switch(tas_hda->priv, 0);
151 tas_hda->priv->playback_started = true;
152 mutex_unlock(&tas_hda->priv->codec_lock);
154 case HDA_GEN_PCM_ACT_CLOSE:
155 mutex_lock(&tas_hda->priv->codec_lock);
156 tasdevice_tuning_switch(tas_hda->priv, 1);
157 tas_hda->priv->playback_started = false;
158 mutex_unlock(&tas_hda->priv->codec_lock);
160 pm_runtime_mark_last_busy(dev);
161 pm_runtime_put_autosuspend(dev);
168 static int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
169 struct snd_ctl_elem_info *uinfo)
171 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
173 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
175 uinfo->value.integer.min = 0;
176 uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;
181 static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
182 struct snd_ctl_elem_value *ucontrol)
184 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
186 mutex_lock(&tas_priv->codec_lock);
188 ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;
190 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n",
191 __func__, kcontrol->id.name, tas_priv->rcabin.profile_cfg_id);
193 mutex_unlock(&tas_priv->codec_lock);
198 static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
199 struct snd_ctl_elem_value *ucontrol)
201 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
202 int nr_profile = ucontrol->value.integer.value[0];
203 int max = tas_priv->rcabin.ncfgs - 1;
206 val = clamp(nr_profile, 0, max);
208 mutex_lock(&tas_priv->codec_lock);
210 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n",
211 __func__, kcontrol->id.name,
212 tas_priv->rcabin.profile_cfg_id, val);
214 if (tas_priv->rcabin.profile_cfg_id != val) {
215 tas_priv->rcabin.profile_cfg_id = val;
219 mutex_unlock(&tas_priv->codec_lock);
224 static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
225 struct snd_ctl_elem_info *uinfo)
227 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
228 struct tasdevice_fw *tas_fw = tas_priv->fmw;
230 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
232 uinfo->value.integer.min = 0;
233 uinfo->value.integer.max = tas_fw->nr_programs - 1;
238 static int tasdevice_info_config(struct snd_kcontrol *kcontrol,
239 struct snd_ctl_elem_info *uinfo)
241 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
242 struct tasdevice_fw *tas_fw = tas_priv->fmw;
244 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
246 uinfo->value.integer.min = 0;
247 uinfo->value.integer.max = tas_fw->nr_configurations - 1;
252 static int tasdevice_program_get(struct snd_kcontrol *kcontrol,
253 struct snd_ctl_elem_value *ucontrol)
255 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
257 mutex_lock(&tas_priv->codec_lock);
259 ucontrol->value.integer.value[0] = tas_priv->cur_prog;
261 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n",
262 __func__, kcontrol->id.name, tas_priv->cur_prog);
264 mutex_unlock(&tas_priv->codec_lock);
269 static int tasdevice_program_put(struct snd_kcontrol *kcontrol,
270 struct snd_ctl_elem_value *ucontrol)
272 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
273 struct tasdevice_fw *tas_fw = tas_priv->fmw;
274 int nr_program = ucontrol->value.integer.value[0];
275 int max = tas_fw->nr_programs - 1;
278 val = clamp(nr_program, 0, max);
280 mutex_lock(&tas_priv->codec_lock);
282 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n",
283 __func__, kcontrol->id.name, tas_priv->cur_prog, val);
285 if (tas_priv->cur_prog != val) {
286 tas_priv->cur_prog = val;
290 mutex_unlock(&tas_priv->codec_lock);
295 static int tasdevice_config_get(struct snd_kcontrol *kcontrol,
296 struct snd_ctl_elem_value *ucontrol)
298 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
300 mutex_lock(&tas_priv->codec_lock);
302 ucontrol->value.integer.value[0] = tas_priv->cur_conf;
304 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n",
305 __func__, kcontrol->id.name, tas_priv->cur_conf);
307 mutex_unlock(&tas_priv->codec_lock);
312 static int tasdevice_config_put(struct snd_kcontrol *kcontrol,
313 struct snd_ctl_elem_value *ucontrol)
315 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
316 struct tasdevice_fw *tas_fw = tas_priv->fmw;
317 int nr_config = ucontrol->value.integer.value[0];
318 int max = tas_fw->nr_configurations - 1;
321 val = clamp(nr_config, 0, max);
323 mutex_lock(&tas_priv->codec_lock);
325 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n",
326 __func__, kcontrol->id.name, tas_priv->cur_conf, val);
328 if (tas_priv->cur_conf != val) {
329 tas_priv->cur_conf = val;
333 mutex_unlock(&tas_priv->codec_lock);
338 static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
339 struct snd_ctl_elem_value *ucontrol)
341 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
342 struct soc_mixer_control *mc =
343 (struct soc_mixer_control *)kcontrol->private_value;
346 mutex_lock(&tas_priv->codec_lock);
348 ret = tasdevice_amp_getvol(tas_priv, ucontrol, mc);
350 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %ld\n",
351 __func__, kcontrol->id.name, ucontrol->value.integer.value[0]);
353 mutex_unlock(&tas_priv->codec_lock);
358 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
359 struct snd_ctl_elem_value *ucontrol)
361 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
362 struct soc_mixer_control *mc =
363 (struct soc_mixer_control *)kcontrol->private_value;
366 mutex_lock(&tas_priv->codec_lock);
368 dev_dbg(tas_priv->dev, "%s: kcontrol %s: -> %ld\n",
369 __func__, kcontrol->id.name, ucontrol->value.integer.value[0]);
371 /* The check of the given value is in tasdevice_amp_putvol. */
372 ret = tasdevice_amp_putvol(tas_priv, ucontrol, mc);
374 mutex_unlock(&tas_priv->codec_lock);
379 static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,
380 struct snd_ctl_elem_value *ucontrol)
382 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
384 mutex_lock(&tas_priv->codec_lock);
386 ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
387 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n",
388 __func__, kcontrol->id.name, tas_priv->force_fwload_status);
390 mutex_unlock(&tas_priv->codec_lock);
395 static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
396 struct snd_ctl_elem_value *ucontrol)
398 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
399 bool change, val = (bool)ucontrol->value.integer.value[0];
401 mutex_lock(&tas_priv->codec_lock);
403 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n",
404 __func__, kcontrol->id.name,
405 tas_priv->force_fwload_status, val);
407 if (tas_priv->force_fwload_status == val)
411 tas_priv->force_fwload_status = val;
414 mutex_unlock(&tas_priv->codec_lock);
419 static const struct snd_kcontrol_new tas2781_snd_controls[] = {
420 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL,
421 1, 0, 20, 0, tas2781_amp_getvol,
422 tas2781_amp_putvol, amp_vol_tlv),
423 ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
424 tas2781_force_fwload_get, tas2781_force_fwload_put),
427 static const struct snd_kcontrol_new tas2781_prof_ctrl = {
428 .name = "Speaker Profile Id",
429 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
430 .info = tasdevice_info_profile,
431 .get = tasdevice_get_profile_id,
432 .put = tasdevice_set_profile_id,
435 static const struct snd_kcontrol_new tas2781_dsp_prog_ctrl = {
436 .name = "Speaker Program Id",
437 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
438 .info = tasdevice_info_programs,
439 .get = tasdevice_program_get,
440 .put = tasdevice_program_put,
443 static const struct snd_kcontrol_new tas2781_dsp_conf_ctrl = {
444 .name = "Speaker Config Id",
445 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
446 .info = tasdevice_info_config,
447 .get = tasdevice_config_get,
448 .put = tasdevice_config_put,
451 static void tas2563_apply_calib(struct tasdevice_priv *tas_priv)
457 for (int i = 0; i < tas_priv->ndev; i++) {
458 for (int j = 0; j < TAS2563_CAL_N; ++j) {
460 *(uint32_t *)&tas_priv->cali_data.data[offset]);
461 ret = tasdevice_dev_bulk_write(tas_priv, i, cal_regs[j],
462 (unsigned char *)&data, TAS2563_CAL_DATA_SIZE);
464 dev_err(tas_priv->dev,
465 "Error writing calib regs\n");
466 offset += TAS2563_CAL_DATA_SIZE;
471 static int tas2563_save_calibration(struct tasdevice_priv *tas_priv)
473 static efi_guid_t efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc,
474 0x09, 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
476 static efi_char16_t *efi_vars[TAS2563_MAX_CHANNELS][TAS2563_CAL_N] = {
477 { L"Power_1", L"R0_1", L"InvR0_1", L"R0_Low_1", L"TLim_1" },
478 { L"Power_2", L"R0_2", L"InvR0_2", L"R0_Low_2", L"TLim_2" },
479 { L"Power_3", L"R0_3", L"InvR0_3", L"R0_Low_3", L"TLim_3" },
480 { L"Power_4", L"R0_4", L"InvR0_4", L"R0_Low_4", L"TLim_4" },
483 unsigned long max_size = TAS2563_CAL_DATA_SIZE;
484 unsigned int offset = 0;
488 tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev,
489 TAS2563_CAL_ARRAY_SIZE, GFP_KERNEL);
490 if (!tas_priv->cali_data.data)
493 for (int i = 0; i < tas_priv->ndev; ++i) {
494 for (int j = 0; j < TAS2563_CAL_N; ++j) {
495 status = efi.get_variable(efi_vars[i][j],
496 &efi_guid, &attr, &max_size,
497 &tas_priv->cali_data.data[offset]);
498 if (status != EFI_SUCCESS ||
499 max_size != TAS2563_CAL_DATA_SIZE) {
500 dev_warn(tas_priv->dev,
501 "Calibration data read failed %ld\n", status);
504 offset += TAS2563_CAL_DATA_SIZE;
508 tas_priv->cali_data.total_sz = offset;
509 tasdevice_apply_calibration(tas_priv);
514 static void tas2781_apply_calib(struct tasdevice_priv *tas_priv)
516 static const unsigned char page_array[CALIB_MAX] = {
517 0x17, 0x18, 0x18, 0x13, 0x18,
519 static const unsigned char rgno_array[CALIB_MAX] = {
520 0x74, 0x0c, 0x14, 0x70, 0x7c,
525 for (i = 0; i < tas_priv->ndev; i++) {
526 data = tas_priv->cali_data.data +
527 i * TASDEVICE_SPEAKER_CALIBRATION_SIZE;
528 for (j = 0; j < CALIB_MAX; j++) {
529 rc = tasdevice_dev_bulk_write(tas_priv, i,
530 TASDEVICE_REG(0, page_array[j], rgno_array[j]),
533 dev_err(tas_priv->dev,
534 "chn %d calib %d bulk_wr err = %d\n",
540 /* Update the calibration data, including speaker impedance, f0, etc, into algo.
541 * Calibrate data is done by manufacturer in the factory. These data are used
542 * by Algo for calculating the speaker temperature, speaker membrane excursion
543 * and f0 in real time during playback.
545 static int tas2781_save_calibration(struct tasdevice_priv *tas_priv)
547 efi_guid_t efi_guid = EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d,
548 0x93, 0xfe, 0x5a, 0xa3, 0x5d, 0xb3);
549 static efi_char16_t efi_name[] = L"CALI_DATA";
550 struct tm *tm = &tas_priv->tm;
551 unsigned int attr, crc;
552 unsigned int *tmp_val;
556 if (tas_priv->catlog_id == LENOVO)
557 efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc, 0x09,
558 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
560 tas_priv->cali_data.total_sz = 0;
561 /* Get real size of UEFI variable */
562 status = efi.get_variable(efi_name, &efi_guid, &attr,
563 &tas_priv->cali_data.total_sz, tas_priv->cali_data.data);
564 if (status == EFI_BUFFER_TOO_SMALL) {
565 /* Allocate data buffer of data_size bytes */
566 tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev,
567 tas_priv->cali_data.total_sz, GFP_KERNEL);
568 if (!tas_priv->cali_data.data)
570 /* Get variable contents into buffer */
571 status = efi.get_variable(efi_name, &efi_guid, &attr,
572 &tas_priv->cali_data.total_sz,
573 tas_priv->cali_data.data);
575 if (status != EFI_SUCCESS)
578 tmp_val = (unsigned int *)tas_priv->cali_data.data;
580 crc = crc32(~0, tas_priv->cali_data.data, 84) ^ ~0;
581 dev_dbg(tas_priv->dev, "cali crc 0x%08x PK tmp_val 0x%08x\n",
584 if (crc == tmp_val[21]) {
585 time64_to_tm(tmp_val[20], 0, tm);
586 dev_dbg(tas_priv->dev, "%4ld-%2d-%2d, %2d:%2d:%2d\n",
587 tm->tm_year, tm->tm_mon, tm->tm_mday,
588 tm->tm_hour, tm->tm_min, tm->tm_sec);
589 tasdevice_apply_calibration(tas_priv);
591 tas_priv->cali_data.total_sz = 0;
596 static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
598 struct hda_codec *codec = tas_hda->priv->codec;
600 if (tas_hda->dsp_prog_ctl)
601 snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
603 if (tas_hda->dsp_conf_ctl)
604 snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
606 for (int i = ARRAY_SIZE(tas_hda->snd_ctls) - 1; i >= 0; i--)
607 if (tas_hda->snd_ctls[i])
608 snd_ctl_remove(codec->card, tas_hda->snd_ctls[i]);
610 if (tas_hda->prof_ctl)
611 snd_ctl_remove(codec->card, tas_hda->prof_ctl);
614 static void tasdev_fw_ready(const struct firmware *fmw, void *context)
616 struct tasdevice_priv *tas_priv = context;
617 struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
618 struct hda_codec *codec = tas_priv->codec;
621 pm_runtime_get_sync(tas_priv->dev);
622 mutex_lock(&tas_priv->codec_lock);
624 ret = tasdevice_rca_parser(tas_priv, fmw);
628 tas_hda->prof_ctl = snd_ctl_new1(&tas2781_prof_ctrl, tas_priv);
629 ret = snd_ctl_add(codec->card, tas_hda->prof_ctl);
631 dev_err(tas_priv->dev,
632 "Failed to add KControl %s = %d\n",
633 tas2781_prof_ctrl.name, ret);
637 for (i = 0; i < ARRAY_SIZE(tas2781_snd_controls); i++) {
638 tas_hda->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_controls[i],
640 ret = snd_ctl_add(codec->card, tas_hda->snd_ctls[i]);
642 dev_err(tas_priv->dev,
643 "Failed to add KControl %s = %d\n",
644 tas2781_snd_controls[i].name, ret);
649 tasdevice_dsp_remove(tas_priv);
651 tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
652 scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%04X.bin",
653 codec->core.subsystem_id & 0xffff);
654 ret = tasdevice_dsp_parser(tas_priv);
656 dev_err(tas_priv->dev, "dspfw load %s error\n",
657 tas_priv->coef_binaryname);
658 tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
662 tas_hda->dsp_prog_ctl = snd_ctl_new1(&tas2781_dsp_prog_ctrl,
664 ret = snd_ctl_add(codec->card, tas_hda->dsp_prog_ctl);
666 dev_err(tas_priv->dev,
667 "Failed to add KControl %s = %d\n",
668 tas2781_dsp_prog_ctrl.name, ret);
672 tas_hda->dsp_conf_ctl = snd_ctl_new1(&tas2781_dsp_conf_ctrl,
674 ret = snd_ctl_add(codec->card, tas_hda->dsp_conf_ctl);
676 dev_err(tas_priv->dev,
677 "Failed to add KControl %s = %d\n",
678 tas2781_dsp_conf_ctrl.name, ret);
682 tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
683 tasdevice_prmg_load(tas_priv, 0);
684 if (tas_priv->fmw->nr_programs > 0)
685 tas_priv->cur_prog = 0;
686 if (tas_priv->fmw->nr_configurations > 0)
687 tas_priv->cur_conf = 0;
689 /* If calibrated data occurs error, dsp will still works with default
690 * calibrated data inside algo.
692 tasdevice_save_calibration(tas_priv);
694 tasdevice_tuning_switch(tas_hda->priv, 0);
695 tas_hda->priv->playback_started = true;
698 mutex_unlock(&tas_hda->priv->codec_lock);
700 release_firmware(fmw);
701 pm_runtime_mark_last_busy(tas_hda->dev);
702 pm_runtime_put_autosuspend(tas_hda->dev);
705 static int tas2781_hda_bind(struct device *dev, struct device *master,
708 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
709 struct hda_component *comps = master_data;
710 struct hda_codec *codec;
714 if (!comps || tas_hda->priv->index < 0 ||
715 tas_hda->priv->index >= HDA_MAX_COMPONENTS)
718 comps = &comps[tas_hda->priv->index];
722 codec = comps->codec;
723 subid = codec->core.subsystem_id >> 16;
727 tas_hda->priv->catlog_id = LENOVO;
730 tas_hda->priv->catlog_id = OTHERS;
734 pm_runtime_get_sync(dev);
738 strscpy(comps->name, dev_name(dev), sizeof(comps->name));
740 ret = tascodec_init(tas_hda->priv, codec, THIS_MODULE, tasdev_fw_ready);
742 comps->playback_hook = tas2781_hda_playback_hook;
744 pm_runtime_mark_last_busy(dev);
745 pm_runtime_put_autosuspend(dev);
750 static void tas2781_hda_unbind(struct device *dev,
751 struct device *master, void *master_data)
753 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
754 struct hda_component *comps = master_data;
755 comps = &comps[tas_hda->priv->index];
757 if (comps->dev == dev) {
759 memset(comps->name, 0, sizeof(comps->name));
760 comps->playback_hook = NULL;
763 tas2781_hda_remove_controls(tas_hda);
765 tasdevice_config_info_remove(tas_hda->priv);
766 tasdevice_dsp_remove(tas_hda->priv);
768 tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
771 static const struct component_ops tas2781_hda_comp_ops = {
772 .bind = tas2781_hda_bind,
773 .unbind = tas2781_hda_unbind,
776 static void tas2781_hda_remove(struct device *dev)
778 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
780 pm_runtime_get_sync(tas_hda->dev);
781 pm_runtime_disable(tas_hda->dev);
783 component_del(tas_hda->dev, &tas2781_hda_comp_ops);
785 pm_runtime_put_noidle(tas_hda->dev);
787 tasdevice_remove(tas_hda->priv);
790 static int tas2781_hda_i2c_probe(struct i2c_client *clt)
792 struct tas2781_hda *tas_hda;
793 const char *device_name;
797 tas_hda = devm_kzalloc(&clt->dev, sizeof(*tas_hda), GFP_KERNEL);
801 dev_set_drvdata(&clt->dev, tas_hda);
802 tas_hda->dev = &clt->dev;
804 tas_hda->priv = tasdevice_kzalloc(clt);
808 if (strstr(dev_name(&clt->dev), "TIAS2781")) {
809 device_name = "TIAS2781";
810 tas_hda->priv->save_calibration = tas2781_save_calibration;
811 tas_hda->priv->apply_calibration = tas2781_apply_calib;
812 tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
813 } else if (strstr(dev_name(&clt->dev), "INT8866")) {
814 device_name = "INT8866";
815 tas_hda->priv->save_calibration = tas2563_save_calibration;
816 tas_hda->priv->apply_calibration = tas2563_apply_calib;
817 tas_hda->priv->global_addr = TAS2563_GLOBAL_ADDR;
821 tas_hda->priv->irq_info.irq = clt->irq;
822 ret = tas2781_read_acpi(tas_hda->priv, device_name);
824 return dev_err_probe(tas_hda->dev, ret,
825 "Platform not supported\n");
827 ret = tasdevice_init(tas_hda->priv);
831 pm_runtime_set_autosuspend_delay(tas_hda->dev, 3000);
832 pm_runtime_use_autosuspend(tas_hda->dev);
833 pm_runtime_mark_last_busy(tas_hda->dev);
834 pm_runtime_set_active(tas_hda->dev);
835 pm_runtime_enable(tas_hda->dev);
837 tas2781_reset(tas_hda->priv);
839 ret = component_add(tas_hda->dev, &tas2781_hda_comp_ops);
841 dev_err(tas_hda->dev, "Register component failed: %d\n", ret);
842 pm_runtime_disable(tas_hda->dev);
847 tas2781_hda_remove(&clt->dev);
851 static void tas2781_hda_i2c_remove(struct i2c_client *clt)
853 tas2781_hda_remove(&clt->dev);
856 static int tas2781_runtime_suspend(struct device *dev)
858 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
860 dev_dbg(tas_hda->dev, "Runtime Suspend\n");
862 mutex_lock(&tas_hda->priv->codec_lock);
864 /* The driver powers up the amplifiers at module load time.
865 * Stop the playback if it's unused.
867 if (tas_hda->priv->playback_started) {
868 tasdevice_tuning_switch(tas_hda->priv, 1);
869 tas_hda->priv->playback_started = false;
872 mutex_unlock(&tas_hda->priv->codec_lock);
877 static int tas2781_runtime_resume(struct device *dev)
879 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
881 dev_dbg(tas_hda->dev, "Runtime Resume\n");
883 mutex_lock(&tas_hda->priv->codec_lock);
885 tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
887 /* If calibrated data occurs error, dsp will still works with default
888 * calibrated data inside algo.
890 tasdevice_apply_calibration(tas_hda->priv);
892 mutex_unlock(&tas_hda->priv->codec_lock);
897 static int tas2781_system_suspend(struct device *dev)
899 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
901 dev_dbg(tas_hda->priv->dev, "System Suspend\n");
903 mutex_lock(&tas_hda->priv->codec_lock);
905 /* Shutdown chip before system suspend */
906 if (tas_hda->priv->playback_started)
907 tasdevice_tuning_switch(tas_hda->priv, 1);
909 mutex_unlock(&tas_hda->priv->codec_lock);
912 * Reset GPIO may be shared, so cannot reset here.
913 * However beyond this point, amps may be powered down.
918 static int tas2781_system_resume(struct device *dev)
920 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
923 dev_dbg(tas_hda->priv->dev, "System Resume\n");
925 mutex_lock(&tas_hda->priv->codec_lock);
927 for (i = 0; i < tas_hda->priv->ndev; i++) {
928 tas_hda->priv->tasdevice[i].cur_book = -1;
929 tas_hda->priv->tasdevice[i].cur_prog = -1;
930 tas_hda->priv->tasdevice[i].cur_conf = -1;
932 tas2781_reset(tas_hda->priv);
933 tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
935 /* If calibrated data occurs error, dsp will still work with default
936 * calibrated data inside algo.
938 tasdevice_apply_calibration(tas_hda->priv);
940 if (tas_hda->priv->playback_started)
941 tasdevice_tuning_switch(tas_hda->priv, 0);
943 mutex_unlock(&tas_hda->priv->codec_lock);
948 static const struct dev_pm_ops tas2781_hda_pm_ops = {
949 RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
950 SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
953 static const struct i2c_device_id tas2781_hda_i2c_id[] = {
954 { "tas2781-hda", 0 },
958 static const struct acpi_device_id tas2781_acpi_hda_match[] = {
963 MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
965 static struct i2c_driver tas2781_hda_i2c_driver = {
967 .name = "tas2781-hda",
968 .acpi_match_table = tas2781_acpi_hda_match,
969 .pm = &tas2781_hda_pm_ops,
971 .id_table = tas2781_hda_i2c_id,
972 .probe = tas2781_hda_i2c_probe,
973 .remove = tas2781_hda_i2c_remove,
975 module_i2c_driver(tas2781_hda_i2c_driver);
977 MODULE_DESCRIPTION("TAS2781 HDA Driver");
978 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
979 MODULE_LICENSE("GPL");
980 MODULE_IMPORT_NS(SND_SOC_TAS2781_FMWLIB);