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 struct device *physdev;
115 LIST_HEAD(resources);
119 adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
122 "Failed to find an ACPI device for %s\n", hid);
126 ret = acpi_dev_get_resources(adev, &resources, tas2781_get_i2c_res, p);
130 acpi_dev_free_resource_list(&resources);
131 strscpy(p->dev_name, hid, sizeof(p->dev_name));
132 physdev = get_device(acpi_get_first_physical_node(adev));
135 /* No side-effect to the playback even if subsystem_id is NULL*/
136 sub = acpi_get_subsystem_id(ACPI_HANDLE(physdev));
140 p->acpi_subsystem_id = sub;
147 dev_err(p->dev, "read acpi error, ret: %d\n", ret);
153 static void tas2781_hda_playback_hook(struct device *dev, int action)
155 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
157 dev_dbg(tas_hda->dev, "%s: action = %d\n", __func__, action);
159 case HDA_GEN_PCM_ACT_OPEN:
160 pm_runtime_get_sync(dev);
161 mutex_lock(&tas_hda->priv->codec_lock);
162 tasdevice_tuning_switch(tas_hda->priv, 0);
163 mutex_unlock(&tas_hda->priv->codec_lock);
165 case HDA_GEN_PCM_ACT_CLOSE:
166 mutex_lock(&tas_hda->priv->codec_lock);
167 tasdevice_tuning_switch(tas_hda->priv, 1);
168 mutex_unlock(&tas_hda->priv->codec_lock);
170 pm_runtime_mark_last_busy(dev);
171 pm_runtime_put_autosuspend(dev);
174 dev_dbg(tas_hda->dev, "Playback action not supported: %d\n",
180 static int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
181 struct snd_ctl_elem_info *uinfo)
183 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
185 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
187 uinfo->value.integer.min = 0;
188 uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;
193 static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
194 struct snd_ctl_elem_value *ucontrol)
196 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
198 ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;
203 static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
204 struct snd_ctl_elem_value *ucontrol)
206 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
207 int nr_profile = ucontrol->value.integer.value[0];
208 int max = tas_priv->rcabin.ncfgs - 1;
211 val = clamp(nr_profile, 0, max);
213 if (tas_priv->rcabin.profile_cfg_id != val) {
214 tas_priv->rcabin.profile_cfg_id = val;
221 static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
222 struct snd_ctl_elem_info *uinfo)
224 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
225 struct tasdevice_fw *tas_fw = tas_priv->fmw;
227 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
229 uinfo->value.integer.min = 0;
230 uinfo->value.integer.max = tas_fw->nr_programs - 1;
235 static int tasdevice_info_config(struct snd_kcontrol *kcontrol,
236 struct snd_ctl_elem_info *uinfo)
238 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
239 struct tasdevice_fw *tas_fw = tas_priv->fmw;
241 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
243 uinfo->value.integer.min = 0;
244 uinfo->value.integer.max = tas_fw->nr_configurations - 1;
249 static int tasdevice_program_get(struct snd_kcontrol *kcontrol,
250 struct snd_ctl_elem_value *ucontrol)
252 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
254 ucontrol->value.integer.value[0] = tas_priv->cur_prog;
259 static int tasdevice_program_put(struct snd_kcontrol *kcontrol,
260 struct snd_ctl_elem_value *ucontrol)
262 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
263 struct tasdevice_fw *tas_fw = tas_priv->fmw;
264 int nr_program = ucontrol->value.integer.value[0];
265 int max = tas_fw->nr_programs - 1;
268 val = clamp(nr_program, 0, max);
270 if (tas_priv->cur_prog != val) {
271 tas_priv->cur_prog = val;
278 static int tasdevice_config_get(struct snd_kcontrol *kcontrol,
279 struct snd_ctl_elem_value *ucontrol)
281 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
283 ucontrol->value.integer.value[0] = tas_priv->cur_conf;
288 static int tasdevice_config_put(struct snd_kcontrol *kcontrol,
289 struct snd_ctl_elem_value *ucontrol)
291 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
292 struct tasdevice_fw *tas_fw = tas_priv->fmw;
293 int nr_config = ucontrol->value.integer.value[0];
294 int max = tas_fw->nr_configurations - 1;
297 val = clamp(nr_config, 0, max);
299 if (tas_priv->cur_conf != val) {
300 tas_priv->cur_conf = val;
308 * tas2781_digital_getvol - get the volum control
309 * @kcontrol: control pointer
310 * @ucontrol: User data
311 * Customer Kcontrol for tas2781 is primarily for regmap booking, paging
312 * depends on internal regmap mechanism.
313 * tas2781 contains book and page two-level register map, especially
314 * book switching will set the register BXXP00R7F, after switching to the
315 * correct book, then leverage the mechanism for paging to access the
318 static int tas2781_digital_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_digital_getvol(tas_priv, ucontrol, mc);
328 static int tas2781_amp_getvol(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 return tasdevice_amp_getvol(tas_priv, ucontrol, mc);
338 static int tas2781_digital_putvol(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;
345 /* The check of the given value is in tasdevice_digital_putvol. */
346 return tasdevice_digital_putvol(tas_priv, ucontrol, mc);
349 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
350 struct snd_ctl_elem_value *ucontrol)
352 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
353 struct soc_mixer_control *mc =
354 (struct soc_mixer_control *)kcontrol->private_value;
356 /* The check of the given value is in tasdevice_amp_putvol. */
357 return tasdevice_amp_putvol(tas_priv, ucontrol, mc);
360 static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,
361 struct snd_ctl_elem_value *ucontrol)
363 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
365 ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
366 dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
367 tas_priv->force_fwload_status ? "ON" : "OFF");
372 static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
373 struct snd_ctl_elem_value *ucontrol)
375 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
376 bool change, val = (bool)ucontrol->value.integer.value[0];
378 if (tas_priv->force_fwload_status == val)
382 tas_priv->force_fwload_status = val;
384 dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
385 tas_priv->force_fwload_status ? "ON" : "OFF");
390 static const struct snd_kcontrol_new tas2781_snd_controls[] = {
391 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL,
392 1, 0, 20, 0, tas2781_amp_getvol,
393 tas2781_amp_putvol, amp_vol_tlv),
394 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Digital Gain", TAS2781_DVC_LVL,
395 0, 0, 200, 1, tas2781_digital_getvol,
396 tas2781_digital_putvol, dvc_tlv),
397 ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
398 tas2781_force_fwload_get, tas2781_force_fwload_put),
401 static const struct snd_kcontrol_new tas2781_prof_ctrl = {
402 .name = "Speaker Profile Id",
403 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
404 .info = tasdevice_info_profile,
405 .get = tasdevice_get_profile_id,
406 .put = tasdevice_set_profile_id,
409 static const struct snd_kcontrol_new tas2781_dsp_prog_ctrl = {
410 .name = "Speaker Program Id",
411 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
412 .info = tasdevice_info_programs,
413 .get = tasdevice_program_get,
414 .put = tasdevice_program_put,
417 static const struct snd_kcontrol_new tas2781_dsp_conf_ctrl = {
418 .name = "Speaker Config Id",
419 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
420 .info = tasdevice_info_config,
421 .get = tasdevice_config_get,
422 .put = tasdevice_config_put,
425 static void tas2563_apply_calib(struct tasdevice_priv *tas_priv)
431 for (int i = 0; i < tas_priv->ndev; i++) {
432 for (int j = 0; j < TAS2563_CAL_N; ++j) {
434 *(uint32_t *)&tas_priv->cali_data.data[offset]);
435 ret = tasdevice_dev_bulk_write(tas_priv, i, cal_regs[j],
436 (unsigned char *)&data, TAS2563_CAL_DATA_SIZE);
438 dev_err(tas_priv->dev,
439 "Error writing calib regs\n");
440 offset += TAS2563_CAL_DATA_SIZE;
445 static int tas2563_save_calibration(struct tasdevice_priv *tas_priv)
447 static efi_guid_t efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc,
448 0x09, 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
450 static efi_char16_t *efi_vars[TAS2563_MAX_CHANNELS][TAS2563_CAL_N] = {
451 { L"Power_1", L"R0_1", L"InvR0_1", L"R0_Low_1", L"TLim_1" },
452 { L"Power_2", L"R0_2", L"InvR0_2", L"R0_Low_2", L"TLim_2" },
453 { L"Power_3", L"R0_3", L"InvR0_3", L"R0_Low_3", L"TLim_3" },
454 { L"Power_4", L"R0_4", L"InvR0_4", L"R0_Low_4", L"TLim_4" },
457 unsigned long max_size = TAS2563_CAL_DATA_SIZE;
458 unsigned int offset = 0;
462 tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev,
463 TAS2563_CAL_ARRAY_SIZE, GFP_KERNEL);
464 if (!tas_priv->cali_data.data)
467 for (int i = 0; i < tas_priv->ndev; ++i) {
468 for (int j = 0; j < TAS2563_CAL_N; ++j) {
469 status = efi.get_variable(efi_vars[i][j],
470 &efi_guid, &attr, &max_size,
471 &tas_priv->cali_data.data[offset]);
472 if (status != EFI_SUCCESS ||
473 max_size != TAS2563_CAL_DATA_SIZE) {
474 dev_warn(tas_priv->dev,
475 "Calibration data read failed %ld\n", status);
478 offset += TAS2563_CAL_DATA_SIZE;
482 tas_priv->cali_data.total_sz = offset;
483 tasdevice_apply_calibration(tas_priv);
488 static void tas2781_apply_calib(struct tasdevice_priv *tas_priv)
490 static const unsigned char page_array[CALIB_MAX] = {
491 0x17, 0x18, 0x18, 0x0d, 0x18
493 static const unsigned char rgno_array[CALIB_MAX] = {
494 0x74, 0x0c, 0x14, 0x3c, 0x7c
499 for (i = 0; i < tas_priv->ndev; i++) {
500 data = tas_priv->cali_data.data +
501 i * TASDEVICE_SPEAKER_CALIBRATION_SIZE;
502 for (j = 0; j < CALIB_MAX; j++) {
503 rc = tasdevice_dev_bulk_write(tas_priv, i,
504 TASDEVICE_REG(0, page_array[j], rgno_array[j]),
507 dev_err(tas_priv->dev,
508 "chn %d calib %d bulk_wr err = %d\n",
514 /* Update the calibration data, including speaker impedance, f0, etc, into algo.
515 * Calibrate data is done by manufacturer in the factory. These data are used
516 * by Algo for calculating the speaker temperature, speaker membrane excursion
517 * and f0 in real time during playback.
519 static int tas2781_save_calibration(struct tasdevice_priv *tas_priv)
521 efi_guid_t efi_guid = EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d,
522 0x93, 0xfe, 0x5a, 0xa3, 0x5d, 0xb3);
523 static efi_char16_t efi_name[] = L"CALI_DATA";
524 struct tm *tm = &tas_priv->tm;
525 unsigned int attr, crc;
526 unsigned int *tmp_val;
530 if (tas_priv->catlog_id == LENOVO)
531 efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc, 0x09,
532 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
534 tas_priv->cali_data.total_sz = 0;
535 /* Get real size of UEFI variable */
536 status = efi.get_variable(efi_name, &efi_guid, &attr,
537 &tas_priv->cali_data.total_sz, tas_priv->cali_data.data);
538 if (status == EFI_BUFFER_TOO_SMALL) {
539 /* Allocate data buffer of data_size bytes */
540 tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev,
541 tas_priv->cali_data.total_sz, GFP_KERNEL);
542 if (!tas_priv->cali_data.data)
544 /* Get variable contents into buffer */
545 status = efi.get_variable(efi_name, &efi_guid, &attr,
546 &tas_priv->cali_data.total_sz,
547 tas_priv->cali_data.data);
549 if (status != EFI_SUCCESS)
552 tmp_val = (unsigned int *)tas_priv->cali_data.data;
554 crc = crc32(~0, tas_priv->cali_data.data, 84) ^ ~0;
555 dev_dbg(tas_priv->dev, "cali crc 0x%08x PK tmp_val 0x%08x\n",
558 if (crc == tmp_val[21]) {
559 time64_to_tm(tmp_val[20], 0, tm);
560 dev_dbg(tas_priv->dev, "%4ld-%2d-%2d, %2d:%2d:%2d\n",
561 tm->tm_year, tm->tm_mon, tm->tm_mday,
562 tm->tm_hour, tm->tm_min, tm->tm_sec);
563 tasdevice_apply_calibration(tas_priv);
565 tas_priv->cali_data.total_sz = 0;
570 static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
572 struct hda_codec *codec = tas_hda->priv->codec;
574 if (tas_hda->dsp_prog_ctl)
575 snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
577 if (tas_hda->dsp_conf_ctl)
578 snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
580 for (int i = ARRAY_SIZE(tas_hda->snd_ctls) - 1; i >= 0; i--)
581 if (tas_hda->snd_ctls[i])
582 snd_ctl_remove(codec->card, tas_hda->snd_ctls[i]);
584 if (tas_hda->prof_ctl)
585 snd_ctl_remove(codec->card, tas_hda->prof_ctl);
588 static void tasdev_fw_ready(const struct firmware *fmw, void *context)
590 struct tasdevice_priv *tas_priv = context;
591 struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
592 struct hda_codec *codec = tas_priv->codec;
595 pm_runtime_get_sync(tas_priv->dev);
596 mutex_lock(&tas_priv->codec_lock);
598 ret = tasdevice_rca_parser(tas_priv, fmw);
602 tas_hda->prof_ctl = snd_ctl_new1(&tas2781_prof_ctrl, tas_priv);
603 ret = snd_ctl_add(codec->card, tas_hda->prof_ctl);
605 dev_err(tas_priv->dev,
606 "Failed to add KControl %s = %d\n",
607 tas2781_prof_ctrl.name, ret);
611 for (i = 0; i < ARRAY_SIZE(tas2781_snd_controls); i++) {
612 tas_hda->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_controls[i],
614 ret = snd_ctl_add(codec->card, tas_hda->snd_ctls[i]);
616 dev_err(tas_priv->dev,
617 "Failed to add KControl %s = %d\n",
618 tas2781_snd_controls[i].name, ret);
623 tasdevice_dsp_remove(tas_priv);
625 tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
626 scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%04X.bin",
627 codec->core.subsystem_id & 0xffff);
628 ret = tasdevice_dsp_parser(tas_priv);
630 dev_err(tas_priv->dev, "dspfw load %s error\n",
631 tas_priv->coef_binaryname);
632 tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
636 tas_hda->dsp_prog_ctl = snd_ctl_new1(&tas2781_dsp_prog_ctrl,
638 ret = snd_ctl_add(codec->card, tas_hda->dsp_prog_ctl);
640 dev_err(tas_priv->dev,
641 "Failed to add KControl %s = %d\n",
642 tas2781_dsp_prog_ctrl.name, ret);
646 tas_hda->dsp_conf_ctl = snd_ctl_new1(&tas2781_dsp_conf_ctrl,
648 ret = snd_ctl_add(codec->card, tas_hda->dsp_conf_ctl);
650 dev_err(tas_priv->dev,
651 "Failed to add KControl %s = %d\n",
652 tas2781_dsp_conf_ctrl.name, ret);
656 tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
657 tasdevice_prmg_load(tas_priv, 0);
658 if (tas_priv->fmw->nr_programs > 0)
659 tas_priv->cur_prog = 0;
660 if (tas_priv->fmw->nr_configurations > 0)
661 tas_priv->cur_conf = 0;
663 /* If calibrated data occurs error, dsp will still works with default
664 * calibrated data inside algo.
666 tasdevice_save_calibration(tas_priv);
668 tasdevice_tuning_switch(tas_hda->priv, 0);
671 mutex_unlock(&tas_hda->priv->codec_lock);
673 release_firmware(fmw);
674 pm_runtime_mark_last_busy(tas_hda->dev);
675 pm_runtime_put_autosuspend(tas_hda->dev);
678 static int tas2781_hda_bind(struct device *dev, struct device *master,
681 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
682 struct hda_component *comps = master_data;
683 struct hda_codec *codec;
687 if (!comps || tas_hda->priv->index < 0 ||
688 tas_hda->priv->index >= HDA_MAX_COMPONENTS)
691 comps = &comps[tas_hda->priv->index];
695 codec = comps->codec;
696 subid = codec->core.subsystem_id >> 16;
700 tas_hda->priv->catlog_id = LENOVO;
703 tas_hda->priv->catlog_id = OTHERS;
707 pm_runtime_get_sync(dev);
711 strscpy(comps->name, dev_name(dev), sizeof(comps->name));
713 ret = tascodec_init(tas_hda->priv, codec, tasdev_fw_ready);
715 comps->playback_hook = tas2781_hda_playback_hook;
717 pm_runtime_mark_last_busy(dev);
718 pm_runtime_put_autosuspend(dev);
723 static void tas2781_hda_unbind(struct device *dev,
724 struct device *master, void *master_data)
726 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
727 struct hda_component *comps = master_data;
728 comps = &comps[tas_hda->priv->index];
730 if (comps->dev == dev) {
732 memset(comps->name, 0, sizeof(comps->name));
733 comps->playback_hook = NULL;
736 tas2781_hda_remove_controls(tas_hda);
738 tasdevice_config_info_remove(tas_hda->priv);
739 tasdevice_dsp_remove(tas_hda->priv);
741 tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
744 static const struct component_ops tas2781_hda_comp_ops = {
745 .bind = tas2781_hda_bind,
746 .unbind = tas2781_hda_unbind,
749 static void tas2781_hda_remove(struct device *dev)
751 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
753 pm_runtime_get_sync(tas_hda->dev);
754 pm_runtime_disable(tas_hda->dev);
756 component_del(tas_hda->dev, &tas2781_hda_comp_ops);
758 pm_runtime_put_noidle(tas_hda->dev);
760 tasdevice_remove(tas_hda->priv);
763 static int tas2781_hda_i2c_probe(struct i2c_client *clt)
765 struct tas2781_hda *tas_hda;
766 const char *device_name;
770 tas_hda = devm_kzalloc(&clt->dev, sizeof(*tas_hda), GFP_KERNEL);
774 dev_set_drvdata(&clt->dev, tas_hda);
775 tas_hda->dev = &clt->dev;
777 tas_hda->priv = tasdevice_kzalloc(clt);
781 if (strstr(dev_name(&clt->dev), "TIAS2781")) {
782 device_name = "TIAS2781";
783 tas_hda->priv->save_calibration = tas2781_save_calibration;
784 tas_hda->priv->apply_calibration = tas2781_apply_calib;
785 tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
786 } else if (strstr(dev_name(&clt->dev), "INT8866")) {
787 device_name = "INT8866";
788 tas_hda->priv->save_calibration = tas2563_save_calibration;
789 tas_hda->priv->apply_calibration = tas2563_apply_calib;
790 tas_hda->priv->global_addr = TAS2563_GLOBAL_ADDR;
794 tas_hda->priv->irq_info.irq = clt->irq;
795 ret = tas2781_read_acpi(tas_hda->priv, device_name);
797 return dev_err_probe(tas_hda->dev, ret,
798 "Platform not supported\n");
800 ret = tasdevice_init(tas_hda->priv);
804 pm_runtime_set_autosuspend_delay(tas_hda->dev, 3000);
805 pm_runtime_use_autosuspend(tas_hda->dev);
806 pm_runtime_mark_last_busy(tas_hda->dev);
807 pm_runtime_set_active(tas_hda->dev);
808 pm_runtime_get_noresume(tas_hda->dev);
809 pm_runtime_enable(tas_hda->dev);
811 pm_runtime_put_autosuspend(tas_hda->dev);
813 tas2781_reset(tas_hda->priv);
815 ret = component_add(tas_hda->dev, &tas2781_hda_comp_ops);
817 dev_err(tas_hda->dev, "Register component failed: %d\n", ret);
818 pm_runtime_disable(tas_hda->dev);
823 tas2781_hda_remove(&clt->dev);
827 static void tas2781_hda_i2c_remove(struct i2c_client *clt)
829 tas2781_hda_remove(&clt->dev);
832 static int tas2781_runtime_suspend(struct device *dev)
834 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
837 dev_dbg(tas_hda->dev, "Runtime Suspend\n");
839 mutex_lock(&tas_hda->priv->codec_lock);
841 if (tas_hda->priv->playback_started) {
842 tasdevice_tuning_switch(tas_hda->priv, 1);
843 tas_hda->priv->playback_started = false;
846 for (i = 0; i < tas_hda->priv->ndev; i++) {
847 tas_hda->priv->tasdevice[i].cur_book = -1;
848 tas_hda->priv->tasdevice[i].cur_prog = -1;
849 tas_hda->priv->tasdevice[i].cur_conf = -1;
852 mutex_unlock(&tas_hda->priv->codec_lock);
857 static int tas2781_runtime_resume(struct device *dev)
859 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
861 dev_dbg(tas_hda->dev, "Runtime Resume\n");
863 mutex_lock(&tas_hda->priv->codec_lock);
865 tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
867 /* If calibrated data occurs error, dsp will still works with default
868 * calibrated data inside algo.
870 tasdevice_apply_calibration(tas_hda->priv);
872 mutex_unlock(&tas_hda->priv->codec_lock);
877 static int tas2781_system_suspend(struct device *dev)
879 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
882 dev_dbg(tas_hda->priv->dev, "System Suspend\n");
884 ret = pm_runtime_force_suspend(dev);
888 /* Shutdown chip before system suspend */
889 tasdevice_tuning_switch(tas_hda->priv, 1);
892 * Reset GPIO may be shared, so cannot reset here.
893 * However beyond this point, amps may be powered down.
898 static int tas2781_system_resume(struct device *dev)
900 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
903 dev_info(tas_hda->priv->dev, "System Resume\n");
905 ret = pm_runtime_force_resume(dev);
909 mutex_lock(&tas_hda->priv->codec_lock);
911 for (i = 0; i < tas_hda->priv->ndev; i++) {
912 tas_hda->priv->tasdevice[i].cur_book = -1;
913 tas_hda->priv->tasdevice[i].cur_prog = -1;
914 tas_hda->priv->tasdevice[i].cur_conf = -1;
916 tas2781_reset(tas_hda->priv);
917 tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
919 /* If calibrated data occurs error, dsp will still work with default
920 * calibrated data inside algo.
922 tasdevice_apply_calibration(tas_hda->priv);
923 mutex_unlock(&tas_hda->priv->codec_lock);
928 static const struct dev_pm_ops tas2781_hda_pm_ops = {
929 RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
930 SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
933 static const struct i2c_device_id tas2781_hda_i2c_id[] = {
934 { "tas2781-hda", 0 },
938 static const struct acpi_device_id tas2781_acpi_hda_match[] = {
943 MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
945 static struct i2c_driver tas2781_hda_i2c_driver = {
947 .name = "tas2781-hda",
948 .acpi_match_table = tas2781_acpi_hda_match,
949 .pm = &tas2781_hda_pm_ops,
951 .id_table = tas2781_hda_i2c_id,
952 .probe = tas2781_hda_i2c_probe,
953 .remove = tas2781_hda_i2c_remove,
955 module_i2c_driver(tas2781_hda_i2c_driver);
957 MODULE_DESCRIPTION("TAS2781 HDA Driver");
958 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
959 MODULE_LICENSE("GPL");
960 MODULE_IMPORT_NS(SND_SOC_TAS2781_FMWLIB);