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