1 // SPDX-License-Identifier: GPL-2.0
3 // TAS2781 HDA I2C driver
5 // Copyright 2023 - 2024 Texas Instruments, Inc.
7 // Author: Shenghao Ding <shenghao-ding@ti.com>
8 // Current maintainer: Baojun Xu <baojun.xu@ti.com>
10 #include <linux/unaligned.h>
11 #include <linux/acpi.h>
12 #include <linux/crc8.h>
13 #include <linux/crc32.h>
14 #include <linux/efi.h>
15 #include <linux/firmware.h>
16 #include <linux/i2c.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
19 #include <linux/pci_ids.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regmap.h>
22 #include <sound/hda_codec.h>
23 #include <sound/soc.h>
24 #include <sound/tas2781.h>
25 #include <sound/tlv.h>
26 #include <sound/tas2781-tlv.h>
28 #include "hda_local.h"
29 #include "hda_auto_parser.h"
30 #include "hda_component.h"
32 #include "hda_generic.h"
34 #define TASDEVICE_SPEAKER_CALIBRATION_SIZE 20
36 /* No standard control callbacks for SNDRV_CTL_ELEM_IFACE_CARD
37 * Define two controls, one is Volume control callbacks, the other is
38 * flag setting control callbacks.
41 /* Volume control callbacks for tas2781 */
42 #define ACARD_SINGLE_RANGE_EXT_TLV(xname, xreg, xshift, xmin, xmax, xinvert, \
43 xhandler_get, xhandler_put, tlv_array) \
44 { .iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = (xname),\
45 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
46 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
47 .tlv.p = (tlv_array), \
48 .info = snd_soc_info_volsw_range, \
49 .get = xhandler_get, .put = xhandler_put, \
50 .private_value = (unsigned long)&(struct soc_mixer_control) \
51 {.reg = xreg, .rreg = xreg, .shift = xshift, \
52 .rshift = xshift, .min = xmin, .max = xmax, \
55 /* Flag control callbacks for tas2781 */
56 #define ACARD_SINGLE_BOOL_EXT(xname, xdata, xhandler_get, xhandler_put) \
57 { .iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = xname, \
58 .info = snd_ctl_boolean_mono_info, \
59 .get = xhandler_get, .put = xhandler_put, \
60 .private_value = xdata }
71 #define TAS2563_MAX_CHANNELS 4
73 #define TAS2563_CAL_POWER TASDEVICE_REG(0, 0x0d, 0x3c)
74 #define TAS2563_CAL_R0 TASDEVICE_REG(0, 0x0f, 0x34)
75 #define TAS2563_CAL_INVR0 TASDEVICE_REG(0, 0x0f, 0x40)
76 #define TAS2563_CAL_R0_LOW TASDEVICE_REG(0, 0x0f, 0x48)
77 #define TAS2563_CAL_TLIM TASDEVICE_REG(0, 0x10, 0x14)
78 #define TAS2563_CAL_N 5
79 #define TAS2563_CAL_DATA_SIZE 4
80 #define TAS2563_CAL_CH_SIZE 20
81 #define TAS2563_CAL_ARRAY_SIZE 80
83 static unsigned int cal_regs
[TAS2563_CAL_N
] = {
84 TAS2563_CAL_POWER
, TAS2563_CAL_R0
, TAS2563_CAL_INVR0
,
85 TAS2563_CAL_R0_LOW
, TAS2563_CAL_TLIM
,
91 struct tasdevice_priv
*priv
;
92 struct snd_kcontrol
*dsp_prog_ctl
;
93 struct snd_kcontrol
*dsp_conf_ctl
;
94 struct snd_kcontrol
*prof_ctl
;
95 struct snd_kcontrol
*snd_ctls
[2];
98 static int tas2781_get_i2c_res(struct acpi_resource
*ares
, void *data
)
100 struct tasdevice_priv
*tas_priv
= data
;
101 struct acpi_resource_i2c_serialbus
*sb
;
103 if (i2c_acpi_get_i2c_resource(ares
, &sb
)) {
104 if (tas_priv
->ndev
< TASDEVICE_MAX_CHANNELS
&&
105 sb
->slave_address
!= tas_priv
->global_addr
) {
106 tas_priv
->tasdevice
[tas_priv
->ndev
].dev_addr
=
107 (unsigned int)sb
->slave_address
;
114 static const struct acpi_gpio_params speakerid_gpios
= { 0, 0, false };
116 static const struct acpi_gpio_mapping tas2781_speaker_id_gpios
[] = {
117 { "speakerid-gpios", &speakerid_gpios
, 1 },
121 static int tas2781_read_acpi(struct tasdevice_priv
*p
, const char *hid
)
123 struct acpi_device
*adev
;
124 struct device
*physdev
;
125 LIST_HEAD(resources
);
130 adev
= acpi_dev_get_first_match_dev(hid
, NULL
, -1);
133 "Failed to find an ACPI device for %s\n", hid
);
137 physdev
= get_device(acpi_get_first_physical_node(adev
));
138 ret
= acpi_dev_get_resources(adev
, &resources
, tas2781_get_i2c_res
, p
);
140 dev_err(p
->dev
, "Failed to get ACPI resource.\n");
143 sub
= acpi_get_subsystem_id(ACPI_HANDLE(physdev
));
145 dev_err(p
->dev
, "Failed to get SUBSYS ID.\n");
148 /* Speaker id was needed for ASUS projects. */
149 ret
= kstrtou32(sub
, 16, &subid
);
150 if (!ret
&& upper_16_bits(subid
) == PCI_VENDOR_ID_ASUSTEK
) {
151 ret
= devm_acpi_dev_add_driver_gpios(p
->dev
,
152 tas2781_speaker_id_gpios
);
154 dev_err(p
->dev
, "Failed to add driver gpio %d.\n",
156 p
->speaker_id
= devm_gpiod_get(p
->dev
, "speakerid", GPIOD_IN
);
157 if (IS_ERR(p
->speaker_id
)) {
158 dev_err(p
->dev
, "Failed to get Speaker id.\n");
159 ret
= PTR_ERR(p
->speaker_id
);
163 p
->speaker_id
= NULL
;
166 acpi_dev_free_resource_list(&resources
);
167 strscpy(p
->dev_name
, hid
, sizeof(p
->dev_name
));
174 dev_err(p
->dev
, "read acpi error, ret: %d\n", ret
);
181 static void tas2781_hda_playback_hook(struct device
*dev
, int action
)
183 struct tas2781_hda
*tas_hda
= dev_get_drvdata(dev
);
185 dev_dbg(tas_hda
->dev
, "%s: action = %d\n", __func__
, action
);
187 case HDA_GEN_PCM_ACT_OPEN
:
188 pm_runtime_get_sync(dev
);
189 mutex_lock(&tas_hda
->priv
->codec_lock
);
190 tasdevice_tuning_switch(tas_hda
->priv
, 0);
191 tas_hda
->priv
->playback_started
= true;
192 mutex_unlock(&tas_hda
->priv
->codec_lock
);
194 case HDA_GEN_PCM_ACT_CLOSE
:
195 mutex_lock(&tas_hda
->priv
->codec_lock
);
196 tasdevice_tuning_switch(tas_hda
->priv
, 1);
197 tas_hda
->priv
->playback_started
= false;
198 mutex_unlock(&tas_hda
->priv
->codec_lock
);
200 pm_runtime_mark_last_busy(dev
);
201 pm_runtime_put_autosuspend(dev
);
208 static int tasdevice_info_profile(struct snd_kcontrol
*kcontrol
,
209 struct snd_ctl_elem_info
*uinfo
)
211 struct tasdevice_priv
*tas_priv
= snd_kcontrol_chip(kcontrol
);
213 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
215 uinfo
->value
.integer
.min
= 0;
216 uinfo
->value
.integer
.max
= tas_priv
->rcabin
.ncfgs
- 1;
221 static int tasdevice_get_profile_id(struct snd_kcontrol
*kcontrol
,
222 struct snd_ctl_elem_value
*ucontrol
)
224 struct tasdevice_priv
*tas_priv
= snd_kcontrol_chip(kcontrol
);
226 mutex_lock(&tas_priv
->codec_lock
);
228 ucontrol
->value
.integer
.value
[0] = tas_priv
->rcabin
.profile_cfg_id
;
230 dev_dbg(tas_priv
->dev
, "%s: kcontrol %s: %d\n",
231 __func__
, kcontrol
->id
.name
, tas_priv
->rcabin
.profile_cfg_id
);
233 mutex_unlock(&tas_priv
->codec_lock
);
238 static int tasdevice_set_profile_id(struct snd_kcontrol
*kcontrol
,
239 struct snd_ctl_elem_value
*ucontrol
)
241 struct tasdevice_priv
*tas_priv
= snd_kcontrol_chip(kcontrol
);
242 int nr_profile
= ucontrol
->value
.integer
.value
[0];
243 int max
= tas_priv
->rcabin
.ncfgs
- 1;
246 val
= clamp(nr_profile
, 0, max
);
248 mutex_lock(&tas_priv
->codec_lock
);
250 dev_dbg(tas_priv
->dev
, "%s: kcontrol %s: %d -> %d\n",
251 __func__
, kcontrol
->id
.name
,
252 tas_priv
->rcabin
.profile_cfg_id
, val
);
254 if (tas_priv
->rcabin
.profile_cfg_id
!= val
) {
255 tas_priv
->rcabin
.profile_cfg_id
= val
;
259 mutex_unlock(&tas_priv
->codec_lock
);
264 static int tasdevice_info_programs(struct snd_kcontrol
*kcontrol
,
265 struct snd_ctl_elem_info
*uinfo
)
267 struct tasdevice_priv
*tas_priv
= snd_kcontrol_chip(kcontrol
);
268 struct tasdevice_fw
*tas_fw
= tas_priv
->fmw
;
270 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
272 uinfo
->value
.integer
.min
= 0;
273 uinfo
->value
.integer
.max
= tas_fw
->nr_programs
- 1;
278 static int tasdevice_info_config(struct snd_kcontrol
*kcontrol
,
279 struct snd_ctl_elem_info
*uinfo
)
281 struct tasdevice_priv
*tas_priv
= snd_kcontrol_chip(kcontrol
);
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
= tas_fw
->nr_configurations
- 1;
292 static int tasdevice_program_get(struct snd_kcontrol
*kcontrol
,
293 struct snd_ctl_elem_value
*ucontrol
)
295 struct tasdevice_priv
*tas_priv
= snd_kcontrol_chip(kcontrol
);
297 mutex_lock(&tas_priv
->codec_lock
);
299 ucontrol
->value
.integer
.value
[0] = tas_priv
->cur_prog
;
301 dev_dbg(tas_priv
->dev
, "%s: kcontrol %s: %d\n",
302 __func__
, kcontrol
->id
.name
, tas_priv
->cur_prog
);
304 mutex_unlock(&tas_priv
->codec_lock
);
309 static int tasdevice_program_put(struct snd_kcontrol
*kcontrol
,
310 struct snd_ctl_elem_value
*ucontrol
)
312 struct tasdevice_priv
*tas_priv
= snd_kcontrol_chip(kcontrol
);
313 struct tasdevice_fw
*tas_fw
= tas_priv
->fmw
;
314 int nr_program
= ucontrol
->value
.integer
.value
[0];
315 int max
= tas_fw
->nr_programs
- 1;
318 val
= clamp(nr_program
, 0, max
);
320 mutex_lock(&tas_priv
->codec_lock
);
322 dev_dbg(tas_priv
->dev
, "%s: kcontrol %s: %d -> %d\n",
323 __func__
, kcontrol
->id
.name
, tas_priv
->cur_prog
, val
);
325 if (tas_priv
->cur_prog
!= val
) {
326 tas_priv
->cur_prog
= val
;
330 mutex_unlock(&tas_priv
->codec_lock
);
335 static int tasdevice_config_get(struct snd_kcontrol
*kcontrol
,
336 struct snd_ctl_elem_value
*ucontrol
)
338 struct tasdevice_priv
*tas_priv
= snd_kcontrol_chip(kcontrol
);
340 mutex_lock(&tas_priv
->codec_lock
);
342 ucontrol
->value
.integer
.value
[0] = tas_priv
->cur_conf
;
344 dev_dbg(tas_priv
->dev
, "%s: kcontrol %s: %d\n",
345 __func__
, kcontrol
->id
.name
, tas_priv
->cur_conf
);
347 mutex_unlock(&tas_priv
->codec_lock
);
352 static int tasdevice_config_put(struct snd_kcontrol
*kcontrol
,
353 struct snd_ctl_elem_value
*ucontrol
)
355 struct tasdevice_priv
*tas_priv
= snd_kcontrol_chip(kcontrol
);
356 struct tasdevice_fw
*tas_fw
= tas_priv
->fmw
;
357 int nr_config
= ucontrol
->value
.integer
.value
[0];
358 int max
= tas_fw
->nr_configurations
- 1;
361 val
= clamp(nr_config
, 0, max
);
363 mutex_lock(&tas_priv
->codec_lock
);
365 dev_dbg(tas_priv
->dev
, "%s: kcontrol %s: %d -> %d\n",
366 __func__
, kcontrol
->id
.name
, tas_priv
->cur_conf
, val
);
368 if (tas_priv
->cur_conf
!= val
) {
369 tas_priv
->cur_conf
= val
;
373 mutex_unlock(&tas_priv
->codec_lock
);
378 static int tas2781_amp_getvol(struct snd_kcontrol
*kcontrol
,
379 struct snd_ctl_elem_value
*ucontrol
)
381 struct tasdevice_priv
*tas_priv
= snd_kcontrol_chip(kcontrol
);
382 struct soc_mixer_control
*mc
=
383 (struct soc_mixer_control
*)kcontrol
->private_value
;
386 mutex_lock(&tas_priv
->codec_lock
);
388 ret
= tasdevice_amp_getvol(tas_priv
, ucontrol
, mc
);
390 dev_dbg(tas_priv
->dev
, "%s: kcontrol %s: %ld\n",
391 __func__
, kcontrol
->id
.name
, ucontrol
->value
.integer
.value
[0]);
393 mutex_unlock(&tas_priv
->codec_lock
);
398 static int tas2781_amp_putvol(struct snd_kcontrol
*kcontrol
,
399 struct snd_ctl_elem_value
*ucontrol
)
401 struct tasdevice_priv
*tas_priv
= snd_kcontrol_chip(kcontrol
);
402 struct soc_mixer_control
*mc
=
403 (struct soc_mixer_control
*)kcontrol
->private_value
;
406 mutex_lock(&tas_priv
->codec_lock
);
408 dev_dbg(tas_priv
->dev
, "%s: kcontrol %s: -> %ld\n",
409 __func__
, kcontrol
->id
.name
, ucontrol
->value
.integer
.value
[0]);
411 /* The check of the given value is in tasdevice_amp_putvol. */
412 ret
= tasdevice_amp_putvol(tas_priv
, ucontrol
, mc
);
414 mutex_unlock(&tas_priv
->codec_lock
);
419 static int tas2781_force_fwload_get(struct snd_kcontrol
*kcontrol
,
420 struct snd_ctl_elem_value
*ucontrol
)
422 struct tasdevice_priv
*tas_priv
= snd_kcontrol_chip(kcontrol
);
424 mutex_lock(&tas_priv
->codec_lock
);
426 ucontrol
->value
.integer
.value
[0] = (int)tas_priv
->force_fwload_status
;
427 dev_dbg(tas_priv
->dev
, "%s: kcontrol %s: %d\n",
428 __func__
, kcontrol
->id
.name
, tas_priv
->force_fwload_status
);
430 mutex_unlock(&tas_priv
->codec_lock
);
435 static int tas2781_force_fwload_put(struct snd_kcontrol
*kcontrol
,
436 struct snd_ctl_elem_value
*ucontrol
)
438 struct tasdevice_priv
*tas_priv
= snd_kcontrol_chip(kcontrol
);
439 bool change
, val
= (bool)ucontrol
->value
.integer
.value
[0];
441 mutex_lock(&tas_priv
->codec_lock
);
443 dev_dbg(tas_priv
->dev
, "%s: kcontrol %s: %d -> %d\n",
444 __func__
, kcontrol
->id
.name
,
445 tas_priv
->force_fwload_status
, val
);
447 if (tas_priv
->force_fwload_status
== val
)
451 tas_priv
->force_fwload_status
= val
;
454 mutex_unlock(&tas_priv
->codec_lock
);
459 static const struct snd_kcontrol_new tas2781_snd_controls
[] = {
460 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL
,
461 1, 0, 20, 0, tas2781_amp_getvol
,
462 tas2781_amp_putvol
, amp_vol_tlv
),
463 ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
464 tas2781_force_fwload_get
, tas2781_force_fwload_put
),
467 static const struct snd_kcontrol_new tas2781_prof_ctrl
= {
468 .name
= "Speaker Profile Id",
469 .iface
= SNDRV_CTL_ELEM_IFACE_CARD
,
470 .info
= tasdevice_info_profile
,
471 .get
= tasdevice_get_profile_id
,
472 .put
= tasdevice_set_profile_id
,
475 static const struct snd_kcontrol_new tas2781_dsp_prog_ctrl
= {
476 .name
= "Speaker Program Id",
477 .iface
= SNDRV_CTL_ELEM_IFACE_CARD
,
478 .info
= tasdevice_info_programs
,
479 .get
= tasdevice_program_get
,
480 .put
= tasdevice_program_put
,
483 static const struct snd_kcontrol_new tas2781_dsp_conf_ctrl
= {
484 .name
= "Speaker Config Id",
485 .iface
= SNDRV_CTL_ELEM_IFACE_CARD
,
486 .info
= tasdevice_info_config
,
487 .get
= tasdevice_config_get
,
488 .put
= tasdevice_config_put
,
491 static void tas2563_apply_calib(struct tasdevice_priv
*tas_priv
)
497 for (int i
= 0; i
< tas_priv
->ndev
; i
++) {
498 for (int j
= 0; j
< TAS2563_CAL_N
; ++j
) {
500 *(uint32_t *)&tas_priv
->cali_data
.data
[offset
]);
501 ret
= tasdevice_dev_bulk_write(tas_priv
, i
, cal_regs
[j
],
502 (unsigned char *)&data
, TAS2563_CAL_DATA_SIZE
);
504 dev_err(tas_priv
->dev
,
505 "Error writing calib regs\n");
506 offset
+= TAS2563_CAL_DATA_SIZE
;
511 static int tas2563_save_calibration(struct tasdevice_priv
*tas_priv
)
513 static efi_guid_t efi_guid
= EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc,
514 0x09, 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
516 static efi_char16_t
*efi_vars
[TAS2563_MAX_CHANNELS
][TAS2563_CAL_N
] = {
517 { L
"Power_1", L
"R0_1", L
"InvR0_1", L
"R0_Low_1", L
"TLim_1" },
518 { L
"Power_2", L
"R0_2", L
"InvR0_2", L
"R0_Low_2", L
"TLim_2" },
519 { L
"Power_3", L
"R0_3", L
"InvR0_3", L
"R0_Low_3", L
"TLim_3" },
520 { L
"Power_4", L
"R0_4", L
"InvR0_4", L
"R0_Low_4", L
"TLim_4" },
523 unsigned long max_size
= TAS2563_CAL_DATA_SIZE
;
524 unsigned int offset
= 0;
528 tas_priv
->cali_data
.data
= devm_kzalloc(tas_priv
->dev
,
529 TAS2563_CAL_ARRAY_SIZE
, GFP_KERNEL
);
530 if (!tas_priv
->cali_data
.data
)
533 for (int i
= 0; i
< tas_priv
->ndev
; ++i
) {
534 for (int j
= 0; j
< TAS2563_CAL_N
; ++j
) {
535 status
= efi
.get_variable(efi_vars
[i
][j
],
536 &efi_guid
, &attr
, &max_size
,
537 &tas_priv
->cali_data
.data
[offset
]);
538 if (status
!= EFI_SUCCESS
||
539 max_size
!= TAS2563_CAL_DATA_SIZE
) {
540 dev_warn(tas_priv
->dev
,
541 "Calibration data read failed %ld\n", status
);
544 offset
+= TAS2563_CAL_DATA_SIZE
;
548 tas_priv
->cali_data
.total_sz
= offset
;
549 tasdevice_apply_calibration(tas_priv
);
554 static void tas2781_apply_calib(struct tasdevice_priv
*tas_priv
)
556 static const unsigned char page_array
[CALIB_MAX
] = {
557 0x17, 0x18, 0x18, 0x13, 0x18,
559 static const unsigned char rgno_array
[CALIB_MAX
] = {
560 0x74, 0x0c, 0x14, 0x70, 0x7c,
566 for (i
= 0; i
< tas_priv
->ndev
; i
++) {
567 for (j
= 0; j
< CALIB_MAX
; j
++) {
569 *(uint32_t *)&tas_priv
->cali_data
.data
[offset
]);
570 rc
= tasdevice_dev_bulk_write(tas_priv
, i
,
571 TASDEVICE_REG(0, page_array
[j
], rgno_array
[j
]),
572 (unsigned char *)&data
, 4);
574 dev_err(tas_priv
->dev
,
575 "chn %d calib %d bulk_wr err = %d\n",
582 /* Update the calibration data, including speaker impedance, f0, etc, into algo.
583 * Calibrate data is done by manufacturer in the factory. These data are used
584 * by Algo for calculating the speaker temperature, speaker membrane excursion
585 * and f0 in real time during playback.
587 static int tas2781_save_calibration(struct tasdevice_priv
*tas_priv
)
589 efi_guid_t efi_guid
= EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d,
590 0x93, 0xfe, 0x5a, 0xa3, 0x5d, 0xb3);
591 static efi_char16_t efi_name
[] = L
"CALI_DATA";
592 struct tm
*tm
= &tas_priv
->tm
;
593 unsigned int attr
, crc
;
594 unsigned int *tmp_val
;
598 if (tas_priv
->catlog_id
== LENOVO
)
599 efi_guid
= EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc, 0x09,
600 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
602 tas_priv
->cali_data
.total_sz
= 0;
603 /* Get real size of UEFI variable */
604 status
= efi
.get_variable(efi_name
, &efi_guid
, &attr
,
605 &tas_priv
->cali_data
.total_sz
, tas_priv
->cali_data
.data
);
606 if (status
== EFI_BUFFER_TOO_SMALL
) {
607 /* Allocate data buffer of data_size bytes */
608 tas_priv
->cali_data
.data
= devm_kzalloc(tas_priv
->dev
,
609 tas_priv
->cali_data
.total_sz
, GFP_KERNEL
);
610 if (!tas_priv
->cali_data
.data
)
612 /* Get variable contents into buffer */
613 status
= efi
.get_variable(efi_name
, &efi_guid
, &attr
,
614 &tas_priv
->cali_data
.total_sz
,
615 tas_priv
->cali_data
.data
);
617 if (status
!= EFI_SUCCESS
)
620 tmp_val
= (unsigned int *)tas_priv
->cali_data
.data
;
622 crc
= crc32(~0, tas_priv
->cali_data
.data
, 84) ^ ~0;
623 dev_dbg(tas_priv
->dev
, "cali crc 0x%08x PK tmp_val 0x%08x\n",
626 if (crc
== tmp_val
[21]) {
627 time64_to_tm(tmp_val
[20], 0, tm
);
628 dev_dbg(tas_priv
->dev
, "%4ld-%2d-%2d, %2d:%2d:%2d\n",
629 tm
->tm_year
, tm
->tm_mon
, tm
->tm_mday
,
630 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
);
631 tasdevice_apply_calibration(tas_priv
);
633 tas_priv
->cali_data
.total_sz
= 0;
638 static void tas2781_hda_remove_controls(struct tas2781_hda
*tas_hda
)
640 struct hda_codec
*codec
= tas_hda
->priv
->codec
;
642 snd_ctl_remove(codec
->card
, tas_hda
->dsp_prog_ctl
);
643 snd_ctl_remove(codec
->card
, tas_hda
->dsp_conf_ctl
);
645 for (int i
= ARRAY_SIZE(tas_hda
->snd_ctls
) - 1; i
>= 0; i
--)
646 snd_ctl_remove(codec
->card
, tas_hda
->snd_ctls
[i
]);
648 snd_ctl_remove(codec
->card
, tas_hda
->prof_ctl
);
651 static void tasdev_fw_ready(const struct firmware
*fmw
, void *context
)
653 struct tasdevice_priv
*tas_priv
= context
;
654 struct tas2781_hda
*tas_hda
= dev_get_drvdata(tas_priv
->dev
);
655 struct hda_codec
*codec
= tas_priv
->codec
;
658 pm_runtime_get_sync(tas_priv
->dev
);
659 mutex_lock(&tas_priv
->codec_lock
);
661 ret
= tasdevice_rca_parser(tas_priv
, fmw
);
665 tas_hda
->prof_ctl
= snd_ctl_new1(&tas2781_prof_ctrl
, tas_priv
);
666 ret
= snd_ctl_add(codec
->card
, tas_hda
->prof_ctl
);
668 dev_err(tas_priv
->dev
,
669 "Failed to add KControl %s = %d\n",
670 tas2781_prof_ctrl
.name
, ret
);
674 for (i
= 0; i
< ARRAY_SIZE(tas2781_snd_controls
); i
++) {
675 tas_hda
->snd_ctls
[i
] = snd_ctl_new1(&tas2781_snd_controls
[i
],
677 ret
= snd_ctl_add(codec
->card
, tas_hda
->snd_ctls
[i
]);
679 dev_err(tas_priv
->dev
,
680 "Failed to add KControl %s = %d\n",
681 tas2781_snd_controls
[i
].name
, ret
);
686 tasdevice_dsp_remove(tas_priv
);
688 tas_priv
->fw_state
= TASDEVICE_DSP_FW_PENDING
;
689 if (tas_priv
->speaker_id
!= NULL
) {
690 // Speaker id need to be checked for ASUS only.
691 spk_id
= gpiod_get_value(tas_priv
->speaker_id
);
693 // Speaker id is not valid, use default.
694 dev_dbg(tas_priv
->dev
, "Wrong spk_id = %d\n", spk_id
);
697 snprintf(tas_priv
->coef_binaryname
,
698 sizeof(tas_priv
->coef_binaryname
),
700 lower_16_bits(codec
->core
.subsystem_id
),
703 snprintf(tas_priv
->coef_binaryname
,
704 sizeof(tas_priv
->coef_binaryname
),
706 lower_16_bits(codec
->core
.subsystem_id
));
708 ret
= tasdevice_dsp_parser(tas_priv
);
710 dev_err(tas_priv
->dev
, "dspfw load %s error\n",
711 tas_priv
->coef_binaryname
);
712 tas_priv
->fw_state
= TASDEVICE_DSP_FW_FAIL
;
716 tas_hda
->dsp_prog_ctl
= snd_ctl_new1(&tas2781_dsp_prog_ctrl
,
718 ret
= snd_ctl_add(codec
->card
, tas_hda
->dsp_prog_ctl
);
720 dev_err(tas_priv
->dev
,
721 "Failed to add KControl %s = %d\n",
722 tas2781_dsp_prog_ctrl
.name
, ret
);
726 tas_hda
->dsp_conf_ctl
= snd_ctl_new1(&tas2781_dsp_conf_ctrl
,
728 ret
= snd_ctl_add(codec
->card
, tas_hda
->dsp_conf_ctl
);
730 dev_err(tas_priv
->dev
,
731 "Failed to add KControl %s = %d\n",
732 tas2781_dsp_conf_ctrl
.name
, ret
);
736 tas_priv
->fw_state
= TASDEVICE_DSP_FW_ALL_OK
;
737 tasdevice_prmg_load(tas_priv
, 0);
738 if (tas_priv
->fmw
->nr_programs
> 0)
739 tas_priv
->cur_prog
= 0;
740 if (tas_priv
->fmw
->nr_configurations
> 0)
741 tas_priv
->cur_conf
= 0;
743 /* If calibrated data occurs error, dsp will still works with default
744 * calibrated data inside algo.
746 tasdevice_save_calibration(tas_priv
);
748 tasdevice_tuning_switch(tas_hda
->priv
, 0);
749 tas_hda
->priv
->playback_started
= true;
752 mutex_unlock(&tas_hda
->priv
->codec_lock
);
754 release_firmware(fmw
);
755 pm_runtime_mark_last_busy(tas_hda
->dev
);
756 pm_runtime_put_autosuspend(tas_hda
->dev
);
759 static int tas2781_hda_bind(struct device
*dev
, struct device
*master
,
762 struct tas2781_hda
*tas_hda
= dev_get_drvdata(dev
);
763 struct hda_component_parent
*parent
= master_data
;
764 struct hda_component
*comp
;
765 struct hda_codec
*codec
;
769 comp
= hda_component_from_index(parent
, tas_hda
->priv
->index
);
776 codec
= parent
->codec
;
777 subid
= codec
->core
.subsystem_id
>> 16;
781 tas_hda
->priv
->catlog_id
= LENOVO
;
784 tas_hda
->priv
->catlog_id
= OTHERS
;
788 pm_runtime_get_sync(dev
);
792 strscpy(comp
->name
, dev_name(dev
), sizeof(comp
->name
));
794 ret
= tascodec_init(tas_hda
->priv
, codec
, THIS_MODULE
, tasdev_fw_ready
);
796 comp
->playback_hook
= tas2781_hda_playback_hook
;
798 pm_runtime_mark_last_busy(dev
);
799 pm_runtime_put_autosuspend(dev
);
804 static void tas2781_hda_unbind(struct device
*dev
,
805 struct device
*master
, void *master_data
)
807 struct tas2781_hda
*tas_hda
= dev_get_drvdata(dev
);
808 struct hda_component_parent
*parent
= master_data
;
809 struct hda_component
*comp
;
811 comp
= hda_component_from_index(parent
, tas_hda
->priv
->index
);
812 if (comp
&& (comp
->dev
== dev
)) {
814 memset(comp
->name
, 0, sizeof(comp
->name
));
815 comp
->playback_hook
= NULL
;
818 tas2781_hda_remove_controls(tas_hda
);
820 tasdevice_config_info_remove(tas_hda
->priv
);
821 tasdevice_dsp_remove(tas_hda
->priv
);
823 tas_hda
->priv
->fw_state
= TASDEVICE_DSP_FW_PENDING
;
826 static const struct component_ops tas2781_hda_comp_ops
= {
827 .bind
= tas2781_hda_bind
,
828 .unbind
= tas2781_hda_unbind
,
831 static void tas2781_hda_remove(struct device
*dev
)
833 struct tas2781_hda
*tas_hda
= dev_get_drvdata(dev
);
835 component_del(tas_hda
->dev
, &tas2781_hda_comp_ops
);
837 pm_runtime_get_sync(tas_hda
->dev
);
838 pm_runtime_disable(tas_hda
->dev
);
840 pm_runtime_put_noidle(tas_hda
->dev
);
842 tasdevice_remove(tas_hda
->priv
);
845 static int tas2781_hda_i2c_probe(struct i2c_client
*clt
)
847 struct tas2781_hda
*tas_hda
;
848 const char *device_name
;
852 tas_hda
= devm_kzalloc(&clt
->dev
, sizeof(*tas_hda
), GFP_KERNEL
);
856 dev_set_drvdata(&clt
->dev
, tas_hda
);
857 tas_hda
->dev
= &clt
->dev
;
859 tas_hda
->priv
= tasdevice_kzalloc(clt
);
863 if (strstr(dev_name(&clt
->dev
), "TIAS2781")) {
864 device_name
= "TIAS2781";
865 tas_hda
->priv
->save_calibration
= tas2781_save_calibration
;
866 tas_hda
->priv
->apply_calibration
= tas2781_apply_calib
;
867 tas_hda
->priv
->global_addr
= TAS2781_GLOBAL_ADDR
;
868 } else if (strstr(dev_name(&clt
->dev
), "INT8866")) {
869 device_name
= "INT8866";
870 tas_hda
->priv
->save_calibration
= tas2563_save_calibration
;
871 tas_hda
->priv
->apply_calibration
= tas2563_apply_calib
;
872 tas_hda
->priv
->global_addr
= TAS2563_GLOBAL_ADDR
;
876 tas_hda
->priv
->irq
= clt
->irq
;
877 ret
= tas2781_read_acpi(tas_hda
->priv
, device_name
);
879 return dev_err_probe(tas_hda
->dev
, ret
,
880 "Platform not supported\n");
882 ret
= tasdevice_init(tas_hda
->priv
);
886 pm_runtime_set_autosuspend_delay(tas_hda
->dev
, 3000);
887 pm_runtime_use_autosuspend(tas_hda
->dev
);
888 pm_runtime_mark_last_busy(tas_hda
->dev
);
889 pm_runtime_set_active(tas_hda
->dev
);
890 pm_runtime_enable(tas_hda
->dev
);
892 tasdevice_reset(tas_hda
->priv
);
894 ret
= component_add(tas_hda
->dev
, &tas2781_hda_comp_ops
);
896 dev_err(tas_hda
->dev
, "Register component failed: %d\n", ret
);
897 pm_runtime_disable(tas_hda
->dev
);
902 tas2781_hda_remove(&clt
->dev
);
906 static void tas2781_hda_i2c_remove(struct i2c_client
*clt
)
908 tas2781_hda_remove(&clt
->dev
);
911 static int tas2781_runtime_suspend(struct device
*dev
)
913 struct tas2781_hda
*tas_hda
= dev_get_drvdata(dev
);
915 dev_dbg(tas_hda
->dev
, "Runtime Suspend\n");
917 mutex_lock(&tas_hda
->priv
->codec_lock
);
919 /* The driver powers up the amplifiers at module load time.
920 * Stop the playback if it's unused.
922 if (tas_hda
->priv
->playback_started
) {
923 tasdevice_tuning_switch(tas_hda
->priv
, 1);
924 tas_hda
->priv
->playback_started
= false;
927 mutex_unlock(&tas_hda
->priv
->codec_lock
);
932 static int tas2781_runtime_resume(struct device
*dev
)
934 struct tas2781_hda
*tas_hda
= dev_get_drvdata(dev
);
936 dev_dbg(tas_hda
->dev
, "Runtime Resume\n");
938 mutex_lock(&tas_hda
->priv
->codec_lock
);
940 tasdevice_prmg_load(tas_hda
->priv
, tas_hda
->priv
->cur_prog
);
942 /* If calibrated data occurs error, dsp will still works with default
943 * calibrated data inside algo.
945 tasdevice_apply_calibration(tas_hda
->priv
);
947 mutex_unlock(&tas_hda
->priv
->codec_lock
);
952 static int tas2781_system_suspend(struct device
*dev
)
954 struct tas2781_hda
*tas_hda
= dev_get_drvdata(dev
);
956 dev_dbg(tas_hda
->priv
->dev
, "System Suspend\n");
958 mutex_lock(&tas_hda
->priv
->codec_lock
);
960 /* Shutdown chip before system suspend */
961 if (tas_hda
->priv
->playback_started
)
962 tasdevice_tuning_switch(tas_hda
->priv
, 1);
964 mutex_unlock(&tas_hda
->priv
->codec_lock
);
967 * Reset GPIO may be shared, so cannot reset here.
968 * However beyond this point, amps may be powered down.
973 static int tas2781_system_resume(struct device
*dev
)
975 struct tas2781_hda
*tas_hda
= dev_get_drvdata(dev
);
978 dev_dbg(tas_hda
->priv
->dev
, "System Resume\n");
980 mutex_lock(&tas_hda
->priv
->codec_lock
);
982 for (i
= 0; i
< tas_hda
->priv
->ndev
; i
++) {
983 tas_hda
->priv
->tasdevice
[i
].cur_book
= -1;
984 tas_hda
->priv
->tasdevice
[i
].cur_prog
= -1;
985 tas_hda
->priv
->tasdevice
[i
].cur_conf
= -1;
987 tasdevice_reset(tas_hda
->priv
);
988 tasdevice_prmg_load(tas_hda
->priv
, tas_hda
->priv
->cur_prog
);
990 /* If calibrated data occurs error, dsp will still work with default
991 * calibrated data inside algo.
993 tasdevice_apply_calibration(tas_hda
->priv
);
995 if (tas_hda
->priv
->playback_started
)
996 tasdevice_tuning_switch(tas_hda
->priv
, 0);
998 mutex_unlock(&tas_hda
->priv
->codec_lock
);
1003 static const struct dev_pm_ops tas2781_hda_pm_ops
= {
1004 RUNTIME_PM_OPS(tas2781_runtime_suspend
, tas2781_runtime_resume
, NULL
)
1005 SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend
, tas2781_system_resume
)
1008 static const struct i2c_device_id tas2781_hda_i2c_id
[] = {
1013 static const struct acpi_device_id tas2781_acpi_hda_match
[] = {
1018 MODULE_DEVICE_TABLE(acpi
, tas2781_acpi_hda_match
);
1020 static struct i2c_driver tas2781_hda_i2c_driver
= {
1022 .name
= "tas2781-hda",
1023 .acpi_match_table
= tas2781_acpi_hda_match
,
1024 .pm
= &tas2781_hda_pm_ops
,
1026 .id_table
= tas2781_hda_i2c_id
,
1027 .probe
= tas2781_hda_i2c_probe
,
1028 .remove
= tas2781_hda_i2c_remove
,
1030 module_i2c_driver(tas2781_hda_i2c_driver
);
1032 MODULE_DESCRIPTION("TAS2781 HDA Driver");
1033 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
1034 MODULE_LICENSE("GPL");
1035 MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");