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[3];
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);
164 dev_dbg(tas_hda->dev, "Playback action not supported: %d\n",
170 static int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
171 struct snd_ctl_elem_info *uinfo)
173 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
175 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
177 uinfo->value.integer.min = 0;
178 uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;
183 static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
184 struct snd_ctl_elem_value *ucontrol)
186 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
188 ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;
193 static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
194 struct snd_ctl_elem_value *ucontrol)
196 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
197 int nr_profile = ucontrol->value.integer.value[0];
198 int max = tas_priv->rcabin.ncfgs - 1;
201 val = clamp(nr_profile, 0, max);
203 if (tas_priv->rcabin.profile_cfg_id != val) {
204 tas_priv->rcabin.profile_cfg_id = val;
211 static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
212 struct snd_ctl_elem_info *uinfo)
214 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
215 struct tasdevice_fw *tas_fw = tas_priv->fmw;
217 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
219 uinfo->value.integer.min = 0;
220 uinfo->value.integer.max = tas_fw->nr_programs - 1;
225 static int tasdevice_info_config(struct snd_kcontrol *kcontrol,
226 struct snd_ctl_elem_info *uinfo)
228 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
229 struct tasdevice_fw *tas_fw = tas_priv->fmw;
231 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
233 uinfo->value.integer.min = 0;
234 uinfo->value.integer.max = tas_fw->nr_configurations - 1;
239 static int tasdevice_program_get(struct snd_kcontrol *kcontrol,
240 struct snd_ctl_elem_value *ucontrol)
242 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
244 ucontrol->value.integer.value[0] = tas_priv->cur_prog;
249 static int tasdevice_program_put(struct snd_kcontrol *kcontrol,
250 struct snd_ctl_elem_value *ucontrol)
252 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
253 struct tasdevice_fw *tas_fw = tas_priv->fmw;
254 int nr_program = ucontrol->value.integer.value[0];
255 int max = tas_fw->nr_programs - 1;
258 val = clamp(nr_program, 0, max);
260 if (tas_priv->cur_prog != val) {
261 tas_priv->cur_prog = val;
268 static int tasdevice_config_get(struct snd_kcontrol *kcontrol,
269 struct snd_ctl_elem_value *ucontrol)
271 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
273 ucontrol->value.integer.value[0] = tas_priv->cur_conf;
278 static int tasdevice_config_put(struct snd_kcontrol *kcontrol,
279 struct snd_ctl_elem_value *ucontrol)
281 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
282 struct tasdevice_fw *tas_fw = tas_priv->fmw;
283 int nr_config = ucontrol->value.integer.value[0];
284 int max = tas_fw->nr_configurations - 1;
287 val = clamp(nr_config, 0, max);
289 if (tas_priv->cur_conf != val) {
290 tas_priv->cur_conf = val;
298 * tas2781_digital_getvol - get the volum control
299 * @kcontrol: control pointer
300 * @ucontrol: User data
301 * Customer Kcontrol for tas2781 is primarily for regmap booking, paging
302 * depends on internal regmap mechanism.
303 * tas2781 contains book and page two-level register map, especially
304 * book switching will set the register BXXP00R7F, after switching to the
305 * correct book, then leverage the mechanism for paging to access the
308 static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,
309 struct snd_ctl_elem_value *ucontrol)
311 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
312 struct soc_mixer_control *mc =
313 (struct soc_mixer_control *)kcontrol->private_value;
315 return tasdevice_digital_getvol(tas_priv, ucontrol, mc);
318 static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
319 struct snd_ctl_elem_value *ucontrol)
321 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
322 struct soc_mixer_control *mc =
323 (struct soc_mixer_control *)kcontrol->private_value;
325 return tasdevice_amp_getvol(tas_priv, ucontrol, mc);
328 static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,
329 struct snd_ctl_elem_value *ucontrol)
331 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
332 struct soc_mixer_control *mc =
333 (struct soc_mixer_control *)kcontrol->private_value;
335 /* The check of the given value is in tasdevice_digital_putvol. */
336 return tasdevice_digital_putvol(tas_priv, ucontrol, mc);
339 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
340 struct snd_ctl_elem_value *ucontrol)
342 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
343 struct soc_mixer_control *mc =
344 (struct soc_mixer_control *)kcontrol->private_value;
346 /* The check of the given value is in tasdevice_amp_putvol. */
347 return tasdevice_amp_putvol(tas_priv, ucontrol, mc);
350 static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,
351 struct snd_ctl_elem_value *ucontrol)
353 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
355 ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
356 dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
357 tas_priv->force_fwload_status ? "ON" : "OFF");
362 static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
363 struct snd_ctl_elem_value *ucontrol)
365 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
366 bool change, val = (bool)ucontrol->value.integer.value[0];
368 if (tas_priv->force_fwload_status == val)
372 tas_priv->force_fwload_status = val;
374 dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
375 tas_priv->force_fwload_status ? "ON" : "OFF");
380 static const struct snd_kcontrol_new tas2781_snd_controls[] = {
381 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL,
382 1, 0, 20, 0, tas2781_amp_getvol,
383 tas2781_amp_putvol, amp_vol_tlv),
384 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Digital Gain", TAS2781_DVC_LVL,
385 0, 0, 200, 1, tas2781_digital_getvol,
386 tas2781_digital_putvol, dvc_tlv),
387 ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
388 tas2781_force_fwload_get, tas2781_force_fwload_put),
391 static const struct snd_kcontrol_new tas2781_prof_ctrl = {
392 .name = "Speaker Profile Id",
393 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
394 .info = tasdevice_info_profile,
395 .get = tasdevice_get_profile_id,
396 .put = tasdevice_set_profile_id,
399 static const struct snd_kcontrol_new tas2781_dsp_prog_ctrl = {
400 .name = "Speaker Program Id",
401 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
402 .info = tasdevice_info_programs,
403 .get = tasdevice_program_get,
404 .put = tasdevice_program_put,
407 static const struct snd_kcontrol_new tas2781_dsp_conf_ctrl = {
408 .name = "Speaker Config Id",
409 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
410 .info = tasdevice_info_config,
411 .get = tasdevice_config_get,
412 .put = tasdevice_config_put,
415 static void tas2563_apply_calib(struct tasdevice_priv *tas_priv)
421 for (int i = 0; i < tas_priv->ndev; i++) {
422 for (int j = 0; j < TAS2563_CAL_N; ++j) {
424 *(uint32_t *)&tas_priv->cali_data.data[offset]);
425 ret = tasdevice_dev_bulk_write(tas_priv, i, cal_regs[j],
426 (unsigned char *)&data, TAS2563_CAL_DATA_SIZE);
428 dev_err(tas_priv->dev,
429 "Error writing calib regs\n");
430 offset += TAS2563_CAL_DATA_SIZE;
435 static int tas2563_save_calibration(struct tasdevice_priv *tas_priv)
437 static efi_guid_t efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc,
438 0x09, 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
440 static efi_char16_t *efi_vars[TAS2563_MAX_CHANNELS][TAS2563_CAL_N] = {
441 { L"Power_1", L"R0_1", L"InvR0_1", L"R0_Low_1", L"TLim_1" },
442 { L"Power_2", L"R0_2", L"InvR0_2", L"R0_Low_2", L"TLim_2" },
443 { L"Power_3", L"R0_3", L"InvR0_3", L"R0_Low_3", L"TLim_3" },
444 { L"Power_4", L"R0_4", L"InvR0_4", L"R0_Low_4", L"TLim_4" },
447 unsigned long max_size = TAS2563_CAL_DATA_SIZE;
448 unsigned int offset = 0;
452 tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev,
453 TAS2563_CAL_ARRAY_SIZE, GFP_KERNEL);
454 if (!tas_priv->cali_data.data)
457 for (int i = 0; i < tas_priv->ndev; ++i) {
458 for (int j = 0; j < TAS2563_CAL_N; ++j) {
459 status = efi.get_variable(efi_vars[i][j],
460 &efi_guid, &attr, &max_size,
461 &tas_priv->cali_data.data[offset]);
462 if (status != EFI_SUCCESS ||
463 max_size != TAS2563_CAL_DATA_SIZE) {
464 dev_warn(tas_priv->dev,
465 "Calibration data read failed %ld\n", status);
468 offset += TAS2563_CAL_DATA_SIZE;
472 tas_priv->cali_data.total_sz = offset;
473 tasdevice_apply_calibration(tas_priv);
478 static void tas2781_apply_calib(struct tasdevice_priv *tas_priv)
480 static const unsigned char page_array[CALIB_MAX] = {
481 0x17, 0x18, 0x18, 0x0d, 0x18
483 static const unsigned char rgno_array[CALIB_MAX] = {
484 0x74, 0x0c, 0x14, 0x3c, 0x7c
489 for (i = 0; i < tas_priv->ndev; i++) {
490 data = tas_priv->cali_data.data +
491 i * TASDEVICE_SPEAKER_CALIBRATION_SIZE;
492 for (j = 0; j < CALIB_MAX; j++) {
493 rc = tasdevice_dev_bulk_write(tas_priv, i,
494 TASDEVICE_REG(0, page_array[j], rgno_array[j]),
497 dev_err(tas_priv->dev,
498 "chn %d calib %d bulk_wr err = %d\n",
504 /* Update the calibration data, including speaker impedance, f0, etc, into algo.
505 * Calibrate data is done by manufacturer in the factory. These data are used
506 * by Algo for calculating the speaker temperature, speaker membrane excursion
507 * and f0 in real time during playback.
509 static int tas2781_save_calibration(struct tasdevice_priv *tas_priv)
511 efi_guid_t efi_guid = EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d,
512 0x93, 0xfe, 0x5a, 0xa3, 0x5d, 0xb3);
513 static efi_char16_t efi_name[] = L"CALI_DATA";
514 struct tm *tm = &tas_priv->tm;
515 unsigned int attr, crc;
516 unsigned int *tmp_val;
520 if (tas_priv->catlog_id == LENOVO)
521 efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc, 0x09,
522 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
524 tas_priv->cali_data.total_sz = 0;
525 /* Get real size of UEFI variable */
526 status = efi.get_variable(efi_name, &efi_guid, &attr,
527 &tas_priv->cali_data.total_sz, tas_priv->cali_data.data);
528 if (status == EFI_BUFFER_TOO_SMALL) {
529 /* Allocate data buffer of data_size bytes */
530 tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev,
531 tas_priv->cali_data.total_sz, GFP_KERNEL);
532 if (!tas_priv->cali_data.data)
534 /* Get variable contents into buffer */
535 status = efi.get_variable(efi_name, &efi_guid, &attr,
536 &tas_priv->cali_data.total_sz,
537 tas_priv->cali_data.data);
539 if (status != EFI_SUCCESS)
542 tmp_val = (unsigned int *)tas_priv->cali_data.data;
544 crc = crc32(~0, tas_priv->cali_data.data, 84) ^ ~0;
545 dev_dbg(tas_priv->dev, "cali crc 0x%08x PK tmp_val 0x%08x\n",
548 if (crc == tmp_val[21]) {
549 time64_to_tm(tmp_val[20], 0, tm);
550 dev_dbg(tas_priv->dev, "%4ld-%2d-%2d, %2d:%2d:%2d\n",
551 tm->tm_year, tm->tm_mon, tm->tm_mday,
552 tm->tm_hour, tm->tm_min, tm->tm_sec);
553 tasdevice_apply_calibration(tas_priv);
555 tas_priv->cali_data.total_sz = 0;
560 static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
562 struct hda_codec *codec = tas_hda->priv->codec;
564 if (tas_hda->dsp_prog_ctl)
565 snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
567 if (tas_hda->dsp_conf_ctl)
568 snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
570 for (int i = ARRAY_SIZE(tas_hda->snd_ctls) - 1; i >= 0; i--)
571 if (tas_hda->snd_ctls[i])
572 snd_ctl_remove(codec->card, tas_hda->snd_ctls[i]);
574 if (tas_hda->prof_ctl)
575 snd_ctl_remove(codec->card, tas_hda->prof_ctl);
578 static void tasdev_fw_ready(const struct firmware *fmw, void *context)
580 struct tasdevice_priv *tas_priv = context;
581 struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
582 struct hda_codec *codec = tas_priv->codec;
585 pm_runtime_get_sync(tas_priv->dev);
586 mutex_lock(&tas_priv->codec_lock);
588 ret = tasdevice_rca_parser(tas_priv, fmw);
592 tas_hda->prof_ctl = snd_ctl_new1(&tas2781_prof_ctrl, tas_priv);
593 ret = snd_ctl_add(codec->card, tas_hda->prof_ctl);
595 dev_err(tas_priv->dev,
596 "Failed to add KControl %s = %d\n",
597 tas2781_prof_ctrl.name, ret);
601 for (i = 0; i < ARRAY_SIZE(tas2781_snd_controls); i++) {
602 tas_hda->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_controls[i],
604 ret = snd_ctl_add(codec->card, tas_hda->snd_ctls[i]);
606 dev_err(tas_priv->dev,
607 "Failed to add KControl %s = %d\n",
608 tas2781_snd_controls[i].name, ret);
613 tasdevice_dsp_remove(tas_priv);
615 tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
616 scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%04X.bin",
617 codec->core.subsystem_id & 0xffff);
618 ret = tasdevice_dsp_parser(tas_priv);
620 dev_err(tas_priv->dev, "dspfw load %s error\n",
621 tas_priv->coef_binaryname);
622 tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
626 tas_hda->dsp_prog_ctl = snd_ctl_new1(&tas2781_dsp_prog_ctrl,
628 ret = snd_ctl_add(codec->card, tas_hda->dsp_prog_ctl);
630 dev_err(tas_priv->dev,
631 "Failed to add KControl %s = %d\n",
632 tas2781_dsp_prog_ctrl.name, ret);
636 tas_hda->dsp_conf_ctl = snd_ctl_new1(&tas2781_dsp_conf_ctrl,
638 ret = snd_ctl_add(codec->card, tas_hda->dsp_conf_ctl);
640 dev_err(tas_priv->dev,
641 "Failed to add KControl %s = %d\n",
642 tas2781_dsp_conf_ctrl.name, ret);
646 tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
647 tasdevice_prmg_load(tas_priv, 0);
648 if (tas_priv->fmw->nr_programs > 0)
649 tas_priv->cur_prog = 0;
650 if (tas_priv->fmw->nr_configurations > 0)
651 tas_priv->cur_conf = 0;
653 /* If calibrated data occurs error, dsp will still works with default
654 * calibrated data inside algo.
656 tasdevice_save_calibration(tas_priv);
658 tasdevice_tuning_switch(tas_hda->priv, 0);
659 tas_hda->priv->playback_started = true;
662 mutex_unlock(&tas_hda->priv->codec_lock);
664 release_firmware(fmw);
665 pm_runtime_mark_last_busy(tas_hda->dev);
666 pm_runtime_put_autosuspend(tas_hda->dev);
669 static int tas2781_hda_bind(struct device *dev, struct device *master,
672 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
673 struct hda_component *comps = master_data;
674 struct hda_codec *codec;
678 if (!comps || tas_hda->priv->index < 0 ||
679 tas_hda->priv->index >= HDA_MAX_COMPONENTS)
682 comps = &comps[tas_hda->priv->index];
686 codec = comps->codec;
687 subid = codec->core.subsystem_id >> 16;
691 tas_hda->priv->catlog_id = LENOVO;
694 tas_hda->priv->catlog_id = OTHERS;
698 pm_runtime_get_sync(dev);
702 strscpy(comps->name, dev_name(dev), sizeof(comps->name));
704 ret = tascodec_init(tas_hda->priv, codec, THIS_MODULE, tasdev_fw_ready);
706 comps->playback_hook = tas2781_hda_playback_hook;
708 pm_runtime_mark_last_busy(dev);
709 pm_runtime_put_autosuspend(dev);
714 static void tas2781_hda_unbind(struct device *dev,
715 struct device *master, void *master_data)
717 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
718 struct hda_component *comps = master_data;
719 comps = &comps[tas_hda->priv->index];
721 if (comps->dev == dev) {
723 memset(comps->name, 0, sizeof(comps->name));
724 comps->playback_hook = NULL;
727 tas2781_hda_remove_controls(tas_hda);
729 tasdevice_config_info_remove(tas_hda->priv);
730 tasdevice_dsp_remove(tas_hda->priv);
732 tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
735 static const struct component_ops tas2781_hda_comp_ops = {
736 .bind = tas2781_hda_bind,
737 .unbind = tas2781_hda_unbind,
740 static void tas2781_hda_remove(struct device *dev)
742 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
744 pm_runtime_get_sync(tas_hda->dev);
745 pm_runtime_disable(tas_hda->dev);
747 component_del(tas_hda->dev, &tas2781_hda_comp_ops);
749 pm_runtime_put_noidle(tas_hda->dev);
751 tasdevice_remove(tas_hda->priv);
754 static int tas2781_hda_i2c_probe(struct i2c_client *clt)
756 struct tas2781_hda *tas_hda;
757 const char *device_name;
761 tas_hda = devm_kzalloc(&clt->dev, sizeof(*tas_hda), GFP_KERNEL);
765 dev_set_drvdata(&clt->dev, tas_hda);
766 tas_hda->dev = &clt->dev;
768 tas_hda->priv = tasdevice_kzalloc(clt);
772 if (strstr(dev_name(&clt->dev), "TIAS2781")) {
773 device_name = "TIAS2781";
774 tas_hda->priv->save_calibration = tas2781_save_calibration;
775 tas_hda->priv->apply_calibration = tas2781_apply_calib;
776 tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
777 } else if (strstr(dev_name(&clt->dev), "INT8866")) {
778 device_name = "INT8866";
779 tas_hda->priv->save_calibration = tas2563_save_calibration;
780 tas_hda->priv->apply_calibration = tas2563_apply_calib;
781 tas_hda->priv->global_addr = TAS2563_GLOBAL_ADDR;
785 tas_hda->priv->irq_info.irq = clt->irq;
786 ret = tas2781_read_acpi(tas_hda->priv, device_name);
788 return dev_err_probe(tas_hda->dev, ret,
789 "Platform not supported\n");
791 ret = tasdevice_init(tas_hda->priv);
795 pm_runtime_set_autosuspend_delay(tas_hda->dev, 3000);
796 pm_runtime_use_autosuspend(tas_hda->dev);
797 pm_runtime_mark_last_busy(tas_hda->dev);
798 pm_runtime_set_active(tas_hda->dev);
799 pm_runtime_enable(tas_hda->dev);
801 tas2781_reset(tas_hda->priv);
803 ret = component_add(tas_hda->dev, &tas2781_hda_comp_ops);
805 dev_err(tas_hda->dev, "Register component failed: %d\n", ret);
806 pm_runtime_disable(tas_hda->dev);
811 tas2781_hda_remove(&clt->dev);
815 static void tas2781_hda_i2c_remove(struct i2c_client *clt)
817 tas2781_hda_remove(&clt->dev);
820 static int tas2781_runtime_suspend(struct device *dev)
822 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
824 dev_dbg(tas_hda->dev, "Runtime Suspend\n");
826 mutex_lock(&tas_hda->priv->codec_lock);
828 /* The driver powers up the amplifiers at module load time.
829 * Stop the playback if it's unused.
831 if (tas_hda->priv->playback_started) {
832 tasdevice_tuning_switch(tas_hda->priv, 1);
833 tas_hda->priv->playback_started = false;
836 mutex_unlock(&tas_hda->priv->codec_lock);
841 static int tas2781_runtime_resume(struct device *dev)
843 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
845 dev_dbg(tas_hda->dev, "Runtime Resume\n");
847 mutex_lock(&tas_hda->priv->codec_lock);
849 tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
851 /* If calibrated data occurs error, dsp will still works with default
852 * calibrated data inside algo.
854 tasdevice_apply_calibration(tas_hda->priv);
856 mutex_unlock(&tas_hda->priv->codec_lock);
861 static int tas2781_system_suspend(struct device *dev)
863 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
865 dev_dbg(tas_hda->priv->dev, "System Suspend\n");
867 mutex_lock(&tas_hda->priv->codec_lock);
869 /* Shutdown chip before system suspend */
870 if (tas_hda->priv->playback_started)
871 tasdevice_tuning_switch(tas_hda->priv, 1);
873 mutex_unlock(&tas_hda->priv->codec_lock);
876 * Reset GPIO may be shared, so cannot reset here.
877 * However beyond this point, amps may be powered down.
882 static int tas2781_system_resume(struct device *dev)
884 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
887 dev_dbg(tas_hda->priv->dev, "System Resume\n");
889 mutex_lock(&tas_hda->priv->codec_lock);
891 for (i = 0; i < tas_hda->priv->ndev; i++) {
892 tas_hda->priv->tasdevice[i].cur_book = -1;
893 tas_hda->priv->tasdevice[i].cur_prog = -1;
894 tas_hda->priv->tasdevice[i].cur_conf = -1;
896 tas2781_reset(tas_hda->priv);
897 tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
899 /* If calibrated data occurs error, dsp will still work with default
900 * calibrated data inside algo.
902 tasdevice_apply_calibration(tas_hda->priv);
904 if (tas_hda->priv->playback_started)
905 tasdevice_tuning_switch(tas_hda->priv, 0);
907 mutex_unlock(&tas_hda->priv->codec_lock);
912 static const struct dev_pm_ops tas2781_hda_pm_ops = {
913 RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
914 SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
917 static const struct i2c_device_id tas2781_hda_i2c_id[] = {
918 { "tas2781-hda", 0 },
922 static const struct acpi_device_id tas2781_acpi_hda_match[] = {
927 MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
929 static struct i2c_driver tas2781_hda_i2c_driver = {
931 .name = "tas2781-hda",
932 .acpi_match_table = tas2781_acpi_hda_match,
933 .pm = &tas2781_hda_pm_ops,
935 .id_table = tas2781_hda_i2c_id,
936 .probe = tas2781_hda_i2c_probe,
937 .remove = tas2781_hda_i2c_remove,
939 module_i2c_driver(tas2781_hda_i2c_driver);
941 MODULE_DESCRIPTION("TAS2781 HDA Driver");
942 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
943 MODULE_LICENSE("GPL");
944 MODULE_IMPORT_NS(SND_SOC_TAS2781_FMWLIB);