1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // TAS2781 HDA I2C driver
4 //
5 // Copyright 2023 - 2025 Texas Instruments, Inc.
6 //
7 // Author: Shenghao Ding <shenghao-ding@ti.com>
8 // Current maintainer: Baojun Xu <baojun.xu@ti.com>
9
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/tas2781-comlib-i2c.h>
26 #include <sound/tlv.h>
27 #include <sound/tas2770-tlv.h>
28 #include <sound/tas2781-tlv.h>
29 #include <sound/tas5825-tlv.h>
30
31 #include "hda_local.h"
32 #include "hda_auto_parser.h"
33 #include "hda_component.h"
34 #include "hda_jack.h"
35 #include "../generic.h"
36 #include "tas2781_hda.h"
37
38 #define TAS2563_CAL_VAR_NAME_MAX 16
39 #define TAS2563_CAL_ARRAY_SIZE 80
40 #define TAS2563_CAL_DATA_SIZE 4
41 #define TAS2563_MAX_CHANNELS 4
42 #define TAS2563_CAL_CH_SIZE 20
43
44 #define TAS2563_CAL_R0_LOW TASDEVICE_REG(0, 0x0f, 0x48)
45 #define TAS2563_CAL_POWER TASDEVICE_REG(0, 0x0d, 0x3c)
46 #define TAS2563_CAL_INVR0 TASDEVICE_REG(0, 0x0f, 0x40)
47 #define TAS2563_CAL_TLIM TASDEVICE_REG(0, 0x10, 0x14)
48 #define TAS2563_CAL_R0 TASDEVICE_REG(0, 0x0f, 0x34)
49
50 enum device_chip_id {
51 HDA_TAS2563,
52 HDA_TAS2770,
53 HDA_TAS2781,
54 HDA_TAS5825,
55 HDA_OTHERS
56 };
57
58 struct tas2781_hda_i2c_priv {
59 struct snd_kcontrol *snd_ctls[2];
60 int (*save_calibration)(struct tas2781_hda *h);
61
62 int hda_chip_id;
63 };
64
tas2781_get_i2c_res(struct acpi_resource * ares,void * data)65 static int tas2781_get_i2c_res(struct acpi_resource *ares, void *data)
66 {
67 struct tasdevice_priv *tas_priv = data;
68 struct acpi_resource_i2c_serialbus *sb;
69
70 if (i2c_acpi_get_i2c_resource(ares, &sb)) {
71 if (tas_priv->ndev < TASDEVICE_MAX_CHANNELS &&
72 sb->slave_address != tas_priv->global_addr) {
73 tas_priv->tasdevice[tas_priv->ndev].dev_addr =
74 (unsigned int)sb->slave_address;
75 tas_priv->ndev++;
76 }
77 }
78 return 1;
79 }
80
81 static const struct acpi_gpio_params speakerid_gpios = { 0, 0, false };
82
83 static const struct acpi_gpio_mapping tas2781_speaker_id_gpios[] = {
84 { "speakerid-gpios", &speakerid_gpios, 1 },
85 { }
86 };
87
tas2781_read_acpi(struct tasdevice_priv * p,const char * hid)88 static int tas2781_read_acpi(struct tasdevice_priv *p, const char *hid)
89 {
90 struct gpio_desc *speaker_id;
91 struct acpi_device *adev;
92 struct device *physdev;
93 LIST_HEAD(resources);
94 const char *sub;
95 uint32_t subid;
96 int ret;
97
98 adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
99 if (!adev) {
100 dev_err(p->dev,
101 "Failed to find an ACPI device for %s\n", hid);
102 return -ENODEV;
103 }
104
105 physdev = get_device(acpi_get_first_physical_node(adev));
106 ret = acpi_dev_get_resources(adev, &resources, tas2781_get_i2c_res, p);
107 if (ret < 0) {
108 dev_err(p->dev, "Failed to get ACPI resource.\n");
109 goto err;
110 }
111 sub = acpi_get_subsystem_id(ACPI_HANDLE(physdev));
112 if (IS_ERR(sub)) {
113 /* No subsys id in older tas2563 projects. */
114 if (!strncmp(hid, "INT8866", sizeof("INT8866")))
115 goto end_2563;
116 dev_err(p->dev, "Failed to get SUBSYS ID.\n");
117 ret = PTR_ERR(sub);
118 goto err;
119 }
120 /* Speaker id was needed for ASUS projects. */
121 ret = kstrtou32(sub, 16, &subid);
122 if (!ret && upper_16_bits(subid) == PCI_VENDOR_ID_ASUSTEK) {
123 ret = acpi_dev_add_driver_gpios(adev, tas2781_speaker_id_gpios);
124 if (ret < 0) {
125 dev_err(p->dev, "Failed to add driver gpio %d.\n",
126 ret);
127 p->speaker_id = -1;
128 goto end_2563;
129 }
130
131 speaker_id = fwnode_gpiod_get_index(acpi_fwnode_handle(adev),
132 "speakerid", 0, GPIOD_IN, NULL);
133 if (!IS_ERR(speaker_id)) {
134 p->speaker_id = gpiod_get_value_cansleep(speaker_id);
135 dev_dbg(p->dev, "Got speaker id gpio from ACPI: %d.\n",
136 p->speaker_id);
137 gpiod_put(speaker_id);
138 } else {
139 p->speaker_id = -1;
140 ret = PTR_ERR(speaker_id);
141 dev_err(p->dev, "Get speaker id gpio failed %d.\n",
142 ret);
143 }
144
145 acpi_dev_remove_driver_gpios(adev);
146 } else {
147 p->speaker_id = -1;
148 }
149
150 end_2563:
151 acpi_dev_free_resource_list(&resources);
152 strscpy(p->dev_name, hid, sizeof(p->dev_name));
153 put_device(physdev);
154 acpi_dev_put(adev);
155
156 return 0;
157
158 err:
159 dev_err(p->dev, "read acpi error, ret: %d\n", ret);
160 put_device(physdev);
161 acpi_dev_put(adev);
162
163 return ret;
164 }
165
tas2781_hda_playback_hook(struct device * dev,int action)166 static void tas2781_hda_playback_hook(struct device *dev, int action)
167 {
168 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
169
170 dev_dbg(tas_hda->dev, "%s: action = %d\n", __func__, action);
171 switch (action) {
172 case HDA_GEN_PCM_ACT_OPEN:
173 pm_runtime_get_sync(dev);
174 scoped_guard(mutex, &tas_hda->priv->codec_lock) {
175 tasdevice_tuning_switch(tas_hda->priv, 0);
176 tas_hda->priv->playback_started = true;
177 }
178 break;
179 case HDA_GEN_PCM_ACT_CLOSE:
180 scoped_guard(mutex, &tas_hda->priv->codec_lock) {
181 tasdevice_tuning_switch(tas_hda->priv, 1);
182 tas_hda->priv->playback_started = false;
183 }
184
185 pm_runtime_put_autosuspend(dev);
186 break;
187 default:
188 break;
189 }
190 }
191
tas2781_amp_getvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)192 static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
193 struct snd_ctl_elem_value *ucontrol)
194 {
195 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
196 struct soc_mixer_control *mc =
197 (struct soc_mixer_control *)kcontrol->private_value;
198 int ret;
199
200 guard(mutex)(&tas_priv->codec_lock);
201
202 ret = tasdevice_amp_getvol(tas_priv, ucontrol, mc);
203
204 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %ld\n",
205 __func__, kcontrol->id.name, ucontrol->value.integer.value[0]);
206
207 return ret;
208 }
209
tas2781_amp_putvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)210 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
211 struct snd_ctl_elem_value *ucontrol)
212 {
213 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
214 struct soc_mixer_control *mc =
215 (struct soc_mixer_control *)kcontrol->private_value;
216
217 guard(mutex)(&tas_priv->codec_lock);
218
219 dev_dbg(tas_priv->dev, "%s: kcontrol %s: -> %ld\n",
220 __func__, kcontrol->id.name, ucontrol->value.integer.value[0]);
221
222 /* The check of the given value is in tasdevice_amp_putvol. */
223 return tasdevice_amp_putvol(tas_priv, ucontrol, mc);
224 }
225
tas2781_force_fwload_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)226 static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,
227 struct snd_ctl_elem_value *ucontrol)
228 {
229 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
230
231 guard(mutex)(&tas_priv->codec_lock);
232
233 ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
234 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n",
235 __func__, kcontrol->id.name, tas_priv->force_fwload_status);
236
237 return 0;
238 }
239
tas2781_force_fwload_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)240 static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
241 struct snd_ctl_elem_value *ucontrol)
242 {
243 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
244 bool change, val = (bool)ucontrol->value.integer.value[0];
245
246 guard(mutex)(&tas_priv->codec_lock);
247
248 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n",
249 __func__, kcontrol->id.name,
250 tas_priv->force_fwload_status, val);
251
252 if (tas_priv->force_fwload_status == val)
253 change = false;
254 else {
255 change = true;
256 tas_priv->force_fwload_status = val;
257 }
258
259 return change;
260 }
261
262 static const struct snd_kcontrol_new tas2770_snd_controls[] = {
263 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS2770_AMP_LEVEL,
264 0, 0, 20, 0, tas2781_amp_getvol,
265 tas2781_amp_putvol, tas2770_amp_tlv),
266 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2770_DVC_LEVEL,
267 0, 0, 200, 1, tas2781_amp_getvol,
268 tas2781_amp_putvol, tas2770_dvc_tlv),
269 };
270
271 static const struct snd_kcontrol_new tas2781_snd_controls[] = {
272 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS2781_AMP_LEVEL,
273 1, 0, 20, 0, tas2781_amp_getvol,
274 tas2781_amp_putvol, tas2781_amp_tlv),
275 ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
276 tas2781_force_fwload_get, tas2781_force_fwload_put),
277 };
278
279 static const struct snd_kcontrol_new tas5825_snd_controls[] = {
280 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS5825_AMP_LEVEL,
281 0, 0, 31, 1, tas2781_amp_getvol,
282 tas2781_amp_putvol, tas5825_amp_tlv),
283 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS5825_DVC_LEVEL,
284 0, 0, 254, 1, tas2781_amp_getvol,
285 tas2781_amp_putvol, tas5825_dvc_tlv),
286 ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
287 tas2781_force_fwload_get, tas2781_force_fwload_put),
288 };
289
290 static const struct snd_kcontrol_new tasdevice_prof_ctrl = {
291 .name = "Speaker Profile Id",
292 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
293 .info = tasdevice_info_profile,
294 .get = tasdevice_get_profile_id,
295 .put = tasdevice_set_profile_id,
296 };
297
298 static const struct snd_kcontrol_new tasdevice_dsp_prog_ctrl = {
299 .name = "Speaker Program Id",
300 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
301 .info = tasdevice_info_programs,
302 .get = tasdevice_program_get,
303 .put = tasdevice_program_put,
304 };
305
306 static const struct snd_kcontrol_new tasdevice_dsp_conf_ctrl = {
307 .name = "Speaker Config Id",
308 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
309 .info = tasdevice_info_config,
310 .get = tasdevice_config_get,
311 .put = tasdevice_config_put,
312 };
313
tas2563_save_calibration(struct tas2781_hda * h)314 static int tas2563_save_calibration(struct tas2781_hda *h)
315 {
316 efi_guid_t efi_guid = tasdev_fct_efi_guid[LENOVO];
317 char *vars[TASDEV_CALIB_N] = {
318 "R0_%d", "R0_Low_%d", "InvR0_%d", "Power_%d", "TLim_%d"
319 };
320 efi_char16_t efi_name[TAS2563_CAL_VAR_NAME_MAX];
321 unsigned long max_size = TAS2563_CAL_DATA_SIZE;
322 unsigned char var8[TAS2563_CAL_VAR_NAME_MAX];
323 struct tasdevice_priv *p = h->priv;
324 struct calidata *cd = &p->cali_data;
325 struct cali_reg *r = &cd->cali_reg_array;
326 unsigned int offset = 0;
327 unsigned char *data;
328 __be32 bedata;
329 efi_status_t status;
330 unsigned int attr;
331 int ret, i, j, k;
332
333 if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) {
334 dev_err(p->dev, "%s: NO EFI FOUND!\n", __func__);
335 return -EINVAL;
336 }
337
338 cd->cali_dat_sz_per_dev = TAS2563_CAL_DATA_SIZE * TASDEV_CALIB_N;
339
340 /* extra byte for each device is the device number */
341 cd->total_sz = (cd->cali_dat_sz_per_dev + 1) * p->ndev;
342 data = cd->data = devm_kzalloc(p->dev, cd->total_sz,
343 GFP_KERNEL);
344 if (!data)
345 return -ENOMEM;
346
347 for (i = 0; i < p->ndev; ++i) {
348 data[offset] = i;
349 offset++;
350 for (j = 0; j < TASDEV_CALIB_N; ++j) {
351 /* EFI name for calibration started with 1, not 0 */
352 ret = snprintf(var8, sizeof(var8), vars[j], i + 1);
353 if (ret < 0 || ret >= sizeof(var8) - 1) {
354 dev_err(p->dev, "%s: Read %s failed\n",
355 __func__, var8);
356 return -EINVAL;
357 }
358 /*
359 * Our variable names are ASCII by construction, but
360 * EFI names are wide chars. Convert and zero-pad.
361 */
362 memset(efi_name, 0, sizeof(efi_name));
363 for (k = 0; k < sizeof(var8) && var8[k]; k++)
364 efi_name[k] = var8[k];
365 status = efi.get_variable(efi_name,
366 &efi_guid, &attr, &max_size,
367 &data[offset]);
368 if (status != EFI_SUCCESS ||
369 max_size != TAS2563_CAL_DATA_SIZE) {
370 dev_warn(p->dev,
371 "Dev %d: Caldat[%d] read failed %ld\n",
372 i, j, status);
373 return -EINVAL;
374 }
375 bedata = cpu_to_be32(*(uint32_t *)&data[offset]);
376 memcpy(&data[offset], &bedata, sizeof(bedata));
377 offset += TAS2563_CAL_DATA_SIZE;
378 }
379 }
380
381 if (cd->total_sz != offset) {
382 dev_err(p->dev, "%s: tot_size(%lu) and offset(%u) mismatch\n",
383 __func__, cd->total_sz, offset);
384 return -EINVAL;
385 }
386
387 r->r0_reg = TAS2563_CAL_R0;
388 r->invr0_reg = TAS2563_CAL_INVR0;
389 r->r0_low_reg = TAS2563_CAL_R0_LOW;
390 r->pow_reg = TAS2563_CAL_POWER;
391 r->tlimit_reg = TAS2563_CAL_TLIM;
392
393 /*
394 * TAS2781_FMWLIB supports two solutions of calibrated data. One is
395 * from the driver itself: driver reads the calibrated files directly
396 * during probe; The other from user space: during init of audio hal,
397 * the audio hal will pass the calibrated data via kcontrol interface.
398 * Driver will store this data in "struct calidata" for use. For hda
399 * device, calibrated data are usunally saved into UEFI. So Hda side
400 * codec driver use the mixture of these two solutions, driver reads
401 * the data from UEFI, then store this data in "struct calidata" for
402 * use.
403 */
404 p->is_user_space_calidata = true;
405
406 return 0;
407 }
408
tas2781_hda_remove_controls(struct tas2781_hda * tas_hda)409 static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
410 {
411 struct tas2781_hda_i2c_priv *hda_priv = tas_hda->hda_priv;
412 struct hda_codec *codec = tas_hda->priv->codec;
413
414 snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
415 snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
416
417 for (int i = ARRAY_SIZE(hda_priv->snd_ctls) - 1; i >= 0; i--)
418 snd_ctl_remove(codec->card, hda_priv->snd_ctls[i]);
419
420 snd_ctl_remove(codec->card, tas_hda->prof_ctl);
421 }
422
tasdev_add_kcontrols(struct tasdevice_priv * tas_priv,struct snd_kcontrol ** ctls,struct hda_codec * codec,const struct snd_kcontrol_new * tas_snd_ctrls,int num_ctls)423 static void tasdev_add_kcontrols(struct tasdevice_priv *tas_priv,
424 struct snd_kcontrol **ctls, struct hda_codec *codec,
425 const struct snd_kcontrol_new *tas_snd_ctrls, int num_ctls)
426 {
427 int i, ret;
428
429 for (i = 0; i < num_ctls; i++) {
430 ctls[i] = snd_ctl_new1(
431 &tas_snd_ctrls[i], tas_priv);
432 ret = snd_ctl_add(codec->card, ctls[i]);
433 if (ret) {
434 dev_err(tas_priv->dev,
435 "Failed to add KControl %s = %d\n",
436 tas_snd_ctrls[i].name, ret);
437 break;
438 }
439 }
440 }
441
tasdevice_dspfw_init(void * context)442 static void tasdevice_dspfw_init(void *context)
443 {
444 struct tasdevice_priv *tas_priv = context;
445 struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
446 struct tas2781_hda_i2c_priv *hda_priv = tas_hda->hda_priv;
447 struct hda_codec *codec = tas_priv->codec;
448 int ret;
449
450 tasdevice_dsp_remove(tas_priv);
451 tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
452 if (tas_priv->speaker_id >= 0) {
453 snprintf(tas_priv->coef_binaryname,
454 sizeof(tas_priv->coef_binaryname),
455 "TAS2XXX%04X%d.bin",
456 lower_16_bits(codec->core.subsystem_id),
457 tas_priv->speaker_id);
458 } else {
459 snprintf(tas_priv->coef_binaryname,
460 sizeof(tas_priv->coef_binaryname),
461 "TAS2XXX%04X.bin",
462 lower_16_bits(codec->core.subsystem_id));
463 }
464 ret = tasdevice_dsp_parser(tas_priv);
465 if (ret) {
466 dev_err(tas_priv->dev, "dspfw load %s error\n",
467 tas_priv->coef_binaryname);
468 tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
469 return;
470 }
471 tasdev_add_kcontrols(tas_priv, &tas_hda->dsp_prog_ctl, codec,
472 &tasdevice_dsp_prog_ctrl, 1);
473 tasdev_add_kcontrols(tas_priv, &tas_hda->dsp_conf_ctl, codec,
474 &tasdevice_dsp_conf_ctrl, 1);
475
476 tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
477 tasdevice_prmg_load(tas_priv, 0);
478 if (tas_priv->fmw->nr_programs > 0)
479 tas_priv->cur_prog = 0;
480 if (tas_priv->fmw->nr_configurations > 0)
481 tas_priv->cur_conf = 0;
482
483 /* Init common setting for different audio profiles */
484 if (tas_priv->rcabin.init_profile_id >= 0)
485 tasdevice_select_cfg_blk(tas_priv,
486 tas_priv->rcabin.init_profile_id,
487 TASDEVICE_BIN_BLK_PRE_POWER_UP);
488
489 /* If calibrated data occurs error, dsp will still works with default
490 * calibrated data inside algo.
491 */
492 hda_priv->save_calibration(tas_hda);
493 }
494
tasdev_fw_ready(const struct firmware * fmw,void * context)495 static void tasdev_fw_ready(const struct firmware *fmw, void *context)
496 {
497 struct tasdevice_priv *tas_priv = context;
498 struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
499 struct tas2781_hda_i2c_priv *hda_priv = tas_hda->hda_priv;
500 struct hda_codec *codec = tas_priv->codec;
501 int ret;
502
503 pm_runtime_get_sync(tas_priv->dev);
504 mutex_lock(&tas_priv->codec_lock);
505
506 ret = tasdevice_rca_parser(tas_priv, fmw);
507 if (ret)
508 goto out;
509
510 tas_priv->fw_state = TASDEVICE_RCA_FW_OK;
511 tasdev_add_kcontrols(tas_priv, &tas_hda->prof_ctl, codec,
512 &tasdevice_prof_ctrl, 1);
513
514 switch (hda_priv->hda_chip_id) {
515 case HDA_TAS2770:
516 tasdev_add_kcontrols(tas_priv, hda_priv->snd_ctls, codec,
517 &tas2770_snd_controls[0],
518 ARRAY_SIZE(tas2770_snd_controls));
519 break;
520 case HDA_TAS2781:
521 tasdev_add_kcontrols(tas_priv, hda_priv->snd_ctls, codec,
522 &tas2781_snd_controls[0],
523 ARRAY_SIZE(tas2781_snd_controls));
524 tasdevice_dspfw_init(context);
525 break;
526 case HDA_TAS5825:
527 tasdev_add_kcontrols(tas_priv, hda_priv->snd_ctls, codec,
528 &tas5825_snd_controls[0],
529 ARRAY_SIZE(tas5825_snd_controls));
530 tasdevice_dspfw_init(context);
531 break;
532 case HDA_TAS2563:
533 tasdevice_dspfw_init(context);
534 break;
535 default:
536 break;
537 }
538
539 out:
540 mutex_unlock(&tas_hda->priv->codec_lock);
541 release_firmware(fmw);
542 pm_runtime_put_autosuspend(tas_hda->dev);
543 }
544
tas2781_hda_bind(struct device * dev,struct device * master,void * master_data)545 static int tas2781_hda_bind(struct device *dev, struct device *master,
546 void *master_data)
547 {
548 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
549 struct hda_component_parent *parent = master_data;
550 struct hda_component *comp;
551 struct hda_codec *codec;
552 unsigned int subid;
553 int ret;
554
555 comp = hda_component_from_index(parent, tas_hda->priv->index);
556 if (!comp)
557 return -EINVAL;
558
559 if (comp->dev)
560 return -EBUSY;
561
562 codec = parent->codec;
563 subid = codec->core.subsystem_id >> 16;
564
565 switch (subid) {
566 case 0x1028:
567 tas_hda->catlog_id = DELL;
568 break;
569 default:
570 tas_hda->catlog_id = LENOVO;
571 break;
572 }
573
574 pm_runtime_get_sync(dev);
575
576 comp->dev = dev;
577
578 strscpy(comp->name, dev_name(dev), sizeof(comp->name));
579
580 ret = tascodec_init(tas_hda->priv, codec, THIS_MODULE, tasdev_fw_ready);
581 if (!ret)
582 comp->playback_hook = tas2781_hda_playback_hook;
583
584 pm_runtime_put_autosuspend(dev);
585
586 return ret;
587 }
588
tas2781_hda_unbind(struct device * dev,struct device * master,void * master_data)589 static void tas2781_hda_unbind(struct device *dev,
590 struct device *master, void *master_data)
591 {
592 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
593 struct hda_component_parent *parent = master_data;
594 struct hda_component *comp;
595
596 comp = hda_component_from_index(parent, tas_hda->priv->index);
597 if (comp && (comp->dev == dev)) {
598 comp->dev = NULL;
599 memset(comp->name, 0, sizeof(comp->name));
600 comp->playback_hook = NULL;
601 }
602
603 tas2781_hda_remove_controls(tas_hda);
604
605 tasdevice_config_info_remove(tas_hda->priv);
606 tasdevice_dsp_remove(tas_hda->priv);
607
608 tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
609 }
610
611 static const struct component_ops tas2781_hda_comp_ops = {
612 .bind = tas2781_hda_bind,
613 .unbind = tas2781_hda_unbind,
614 };
615
tas2781_hda_i2c_probe(struct i2c_client * clt)616 static int tas2781_hda_i2c_probe(struct i2c_client *clt)
617 {
618 struct tas2781_hda_i2c_priv *hda_priv;
619 struct tas2781_hda *tas_hda;
620 const char *device_name;
621 int ret;
622
623 tas_hda = devm_kzalloc(&clt->dev, sizeof(*tas_hda), GFP_KERNEL);
624 if (!tas_hda)
625 return -ENOMEM;
626
627 hda_priv = devm_kzalloc(&clt->dev, sizeof(*hda_priv), GFP_KERNEL);
628 if (!hda_priv)
629 return -ENOMEM;
630
631 tas_hda->hda_priv = hda_priv;
632
633 dev_set_drvdata(&clt->dev, tas_hda);
634 tas_hda->dev = &clt->dev;
635
636 tas_hda->priv = tasdevice_kzalloc(clt);
637 if (!tas_hda->priv)
638 return -ENOMEM;
639
640 if (strstr(dev_name(&clt->dev), "TIAS2781")) {
641 /*
642 * TAS2781, integrated on-chip DSP with
643 * global I2C address supported.
644 */
645 device_name = "TIAS2781";
646 hda_priv->hda_chip_id = HDA_TAS2781;
647 hda_priv->save_calibration = tas2781_save_calibration;
648 tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
649 } else if (strstarts(dev_name(&clt->dev), "i2c-TXNW2770")) {
650 /*
651 * TAS2770, has no on-chip DSP, so no calibration data
652 * required; has no global I2C address supported.
653 */
654 device_name = "TXNW2770";
655 hda_priv->hda_chip_id = HDA_TAS2770;
656 } else if (strstarts(dev_name(&clt->dev),
657 "i2c-TXNW2781:00-tas2781-hda.0")) {
658 device_name = "TXNW2781";
659 hda_priv->hda_chip_id = HDA_TAS2781;
660 hda_priv->save_calibration = tas2781_save_calibration;
661 tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
662 } else if (strstr(dev_name(&clt->dev), "INT8866")) {
663 /*
664 * TAS2563, integrated on-chip DSP with
665 * global I2C address supported.
666 */
667 device_name = "INT8866";
668 hda_priv->hda_chip_id = HDA_TAS2563;
669 hda_priv->save_calibration = tas2563_save_calibration;
670 tas_hda->priv->global_addr = TAS2563_GLOBAL_ADDR;
671 } else if (strstarts(dev_name(&clt->dev), "i2c-TXNW5825")) {
672 /*
673 * TAS5825, integrated on-chip DSP without
674 * global I2C address and calibration supported.
675 */
676 device_name = "TXNW5825";
677 hda_priv->hda_chip_id = HDA_TAS5825;
678 tas_hda->priv->chip_id = TAS5825;
679 } else {
680 return -ENODEV;
681 }
682
683 tas_hda->priv->irq = clt->irq;
684 ret = tas2781_read_acpi(tas_hda->priv, device_name);
685 if (ret)
686 return dev_err_probe(tas_hda->dev, ret,
687 "Platform not supported\n");
688
689 ret = tasdevice_init(tas_hda->priv);
690 if (ret)
691 goto err;
692
693 pm_runtime_set_autosuspend_delay(tas_hda->dev, 3000);
694 pm_runtime_use_autosuspend(tas_hda->dev);
695 pm_runtime_mark_last_busy(tas_hda->dev);
696 pm_runtime_set_active(tas_hda->dev);
697 pm_runtime_enable(tas_hda->dev);
698
699 tasdevice_reset(tas_hda->priv);
700
701 ret = component_add(tas_hda->dev, &tas2781_hda_comp_ops);
702 if (ret) {
703 dev_err(tas_hda->dev, "Register component failed: %d\n", ret);
704 pm_runtime_disable(tas_hda->dev);
705 }
706
707 err:
708 if (ret)
709 tas2781_hda_remove(&clt->dev, &tas2781_hda_comp_ops);
710 return ret;
711 }
712
tas2781_hda_i2c_remove(struct i2c_client * clt)713 static void tas2781_hda_i2c_remove(struct i2c_client *clt)
714 {
715 tas2781_hda_remove(&clt->dev, &tas2781_hda_comp_ops);
716 }
717
tas2781_runtime_suspend(struct device * dev)718 static int tas2781_runtime_suspend(struct device *dev)
719 {
720 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
721
722 dev_dbg(tas_hda->dev, "Runtime Suspend\n");
723
724 guard(mutex)(&tas_hda->priv->codec_lock);
725
726 /* The driver powers up the amplifiers at module load time.
727 * Stop the playback if it's unused.
728 */
729 if (tas_hda->priv->playback_started) {
730 tasdevice_tuning_switch(tas_hda->priv, 1);
731 tas_hda->priv->playback_started = false;
732 }
733
734 return 0;
735 }
736
tas2781_runtime_resume(struct device * dev)737 static int tas2781_runtime_resume(struct device *dev)
738 {
739 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
740
741 dev_dbg(tas_hda->dev, "Runtime Resume\n");
742
743 guard(mutex)(&tas_hda->priv->codec_lock);
744
745 tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
746
747 return 0;
748 }
749
tas2781_system_suspend(struct device * dev)750 static int tas2781_system_suspend(struct device *dev)
751 {
752 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
753
754 dev_dbg(tas_hda->priv->dev, "System Suspend\n");
755
756 guard(mutex)(&tas_hda->priv->codec_lock);
757
758 /* Shutdown chip before system suspend */
759 if (tas_hda->priv->playback_started)
760 tasdevice_tuning_switch(tas_hda->priv, 1);
761
762 /*
763 * Reset GPIO may be shared, so cannot reset here.
764 * However beyond this point, amps may be powered down.
765 */
766 return 0;
767 }
768
tas2781_system_resume(struct device * dev)769 static int tas2781_system_resume(struct device *dev)
770 {
771 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
772 int i;
773
774 dev_dbg(tas_hda->priv->dev, "System Resume\n");
775
776 guard(mutex)(&tas_hda->priv->codec_lock);
777
778 for (i = 0; i < tas_hda->priv->ndev; i++) {
779 tas_hda->priv->tasdevice[i].cur_book = -1;
780 tas_hda->priv->tasdevice[i].cur_prog = -1;
781 tas_hda->priv->tasdevice[i].cur_conf = -1;
782 }
783 tasdevice_reset(tas_hda->priv);
784 tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
785
786 /* Init common setting for different audio profiles */
787 if (tas_hda->priv->rcabin.init_profile_id >= 0)
788 tasdevice_select_cfg_blk(tas_hda->priv,
789 tas_hda->priv->rcabin.init_profile_id,
790 TASDEVICE_BIN_BLK_PRE_POWER_UP);
791
792 if (tas_hda->priv->playback_started)
793 tasdevice_tuning_switch(tas_hda->priv, 0);
794
795 return 0;
796 }
797
798 static const struct dev_pm_ops tas2781_hda_pm_ops = {
799 RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
800 SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
801 };
802
803 static const struct i2c_device_id tas2781_hda_i2c_id[] = {
804 { "tas2781-hda" },
805 {}
806 };
807
808 static const struct acpi_device_id tas2781_acpi_hda_match[] = {
809 {"INT8866", 0 },
810 {"TIAS2781", 0 },
811 {"TXNW2770", 0 },
812 {"TXNW2781", 0 },
813 {"TXNW5825", 0 },
814 {}
815 };
816 MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
817
818 static struct i2c_driver tas2781_hda_i2c_driver = {
819 .driver = {
820 .name = "tas2781-hda",
821 .acpi_match_table = tas2781_acpi_hda_match,
822 .pm = &tas2781_hda_pm_ops,
823 },
824 .id_table = tas2781_hda_i2c_id,
825 .probe = tas2781_hda_i2c_probe,
826 .remove = tas2781_hda_i2c_remove,
827 };
828 module_i2c_driver(tas2781_hda_i2c_driver);
829
830 MODULE_DESCRIPTION("TAS2781 HDA Driver");
831 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
832 MODULE_LICENSE("GPL");
833 MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");
834 MODULE_IMPORT_NS("SND_HDA_SCODEC_TAS2781");
835