Loading...
Note: File does not exist in v3.15.
1// SPDX-License-Identifier: GPL-2.0
2//
3// TAS2781 HDA I2C driver
4//
5// Copyright 2023 - 2024 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/tlv.h>
26#include <sound/tas2781-tlv.h>
27
28#include "hda_local.h"
29#include "hda_auto_parser.h"
30#include "hda_component.h"
31#include "hda_jack.h"
32#include "hda_generic.h"
33
34#define TASDEVICE_SPEAKER_CALIBRATION_SIZE 20
35
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.
39 */
40
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, \
53 .invert = xinvert} }
54
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 }
61
62enum calib_data {
63 R0_VAL = 0,
64 INV_R0,
65 R0LOW,
66 POWER,
67 TLIM,
68 CALIB_MAX
69};
70
71#define TAS2563_MAX_CHANNELS 4
72
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
82
83static 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,
86};
87
88
89struct tas2781_hda {
90 struct device *dev;
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];
96};
97
98static int tas2781_get_i2c_res(struct acpi_resource *ares, void *data)
99{
100 struct tasdevice_priv *tas_priv = data;
101 struct acpi_resource_i2c_serialbus *sb;
102
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;
108 tas_priv->ndev++;
109 }
110 }
111 return 1;
112}
113
114static const struct acpi_gpio_params speakerid_gpios = { 0, 0, false };
115
116static const struct acpi_gpio_mapping tas2781_speaker_id_gpios[] = {
117 { "speakerid-gpios", &speakerid_gpios, 1 },
118 { }
119};
120
121static int tas2781_read_acpi(struct tasdevice_priv *p, const char *hid)
122{
123 struct acpi_device *adev;
124 struct device *physdev;
125 LIST_HEAD(resources);
126 const char *sub;
127 uint32_t subid;
128 int ret;
129
130 adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
131 if (!adev) {
132 dev_err(p->dev,
133 "Failed to find an ACPI device for %s\n", hid);
134 return -ENODEV;
135 }
136
137 physdev = get_device(acpi_get_first_physical_node(adev));
138 ret = acpi_dev_get_resources(adev, &resources, tas2781_get_i2c_res, p);
139 if (ret < 0) {
140 dev_err(p->dev, "Failed to get ACPI resource.\n");
141 goto err;
142 }
143 sub = acpi_get_subsystem_id(ACPI_HANDLE(physdev));
144 if (IS_ERR(sub)) {
145 /* No subsys id in older tas2563 projects. */
146 if (!strncmp(hid, "INT8866", sizeof("INT8866")))
147 goto end_2563;
148 dev_err(p->dev, "Failed to get SUBSYS ID.\n");
149 ret = PTR_ERR(sub);
150 goto err;
151 }
152 /* Speaker id was needed for ASUS projects. */
153 ret = kstrtou32(sub, 16, &subid);
154 if (!ret && upper_16_bits(subid) == PCI_VENDOR_ID_ASUSTEK) {
155 ret = devm_acpi_dev_add_driver_gpios(p->dev,
156 tas2781_speaker_id_gpios);
157 if (ret < 0)
158 dev_err(p->dev, "Failed to add driver gpio %d.\n",
159 ret);
160 p->speaker_id = devm_gpiod_get(p->dev, "speakerid", GPIOD_IN);
161 if (IS_ERR(p->speaker_id)) {
162 dev_err(p->dev, "Failed to get Speaker id.\n");
163 ret = PTR_ERR(p->speaker_id);
164 goto err;
165 }
166 } else {
167 p->speaker_id = NULL;
168 }
169
170end_2563:
171 acpi_dev_free_resource_list(&resources);
172 strscpy(p->dev_name, hid, sizeof(p->dev_name));
173 put_device(physdev);
174 acpi_dev_put(adev);
175
176 return 0;
177
178err:
179 dev_err(p->dev, "read acpi error, ret: %d\n", ret);
180 put_device(physdev);
181 acpi_dev_put(adev);
182
183 return ret;
184}
185
186static void tas2781_hda_playback_hook(struct device *dev, int action)
187{
188 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
189
190 dev_dbg(tas_hda->dev, "%s: action = %d\n", __func__, action);
191 switch (action) {
192 case HDA_GEN_PCM_ACT_OPEN:
193 pm_runtime_get_sync(dev);
194 mutex_lock(&tas_hda->priv->codec_lock);
195 tasdevice_tuning_switch(tas_hda->priv, 0);
196 tas_hda->priv->playback_started = true;
197 mutex_unlock(&tas_hda->priv->codec_lock);
198 break;
199 case HDA_GEN_PCM_ACT_CLOSE:
200 mutex_lock(&tas_hda->priv->codec_lock);
201 tasdevice_tuning_switch(tas_hda->priv, 1);
202 tas_hda->priv->playback_started = false;
203 mutex_unlock(&tas_hda->priv->codec_lock);
204
205 pm_runtime_mark_last_busy(dev);
206 pm_runtime_put_autosuspend(dev);
207 break;
208 default:
209 break;
210 }
211}
212
213static int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
214 struct snd_ctl_elem_info *uinfo)
215{
216 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
217
218 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
219 uinfo->count = 1;
220 uinfo->value.integer.min = 0;
221 uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;
222
223 return 0;
224}
225
226static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
227 struct snd_ctl_elem_value *ucontrol)
228{
229 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
230
231 mutex_lock(&tas_priv->codec_lock);
232
233 ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;
234
235 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n",
236 __func__, kcontrol->id.name, tas_priv->rcabin.profile_cfg_id);
237
238 mutex_unlock(&tas_priv->codec_lock);
239
240 return 0;
241}
242
243static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
244 struct snd_ctl_elem_value *ucontrol)
245{
246 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
247 int nr_profile = ucontrol->value.integer.value[0];
248 int max = tas_priv->rcabin.ncfgs - 1;
249 int val, ret = 0;
250
251 val = clamp(nr_profile, 0, max);
252
253 mutex_lock(&tas_priv->codec_lock);
254
255 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n",
256 __func__, kcontrol->id.name,
257 tas_priv->rcabin.profile_cfg_id, val);
258
259 if (tas_priv->rcabin.profile_cfg_id != val) {
260 tas_priv->rcabin.profile_cfg_id = val;
261 ret = 1;
262 }
263
264 mutex_unlock(&tas_priv->codec_lock);
265
266 return ret;
267}
268
269static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
270 struct snd_ctl_elem_info *uinfo)
271{
272 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
273 struct tasdevice_fw *tas_fw = tas_priv->fmw;
274
275 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
276 uinfo->count = 1;
277 uinfo->value.integer.min = 0;
278 uinfo->value.integer.max = tas_fw->nr_programs - 1;
279
280 return 0;
281}
282
283static int tasdevice_info_config(struct snd_kcontrol *kcontrol,
284 struct snd_ctl_elem_info *uinfo)
285{
286 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
287 struct tasdevice_fw *tas_fw = tas_priv->fmw;
288
289 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
290 uinfo->count = 1;
291 uinfo->value.integer.min = 0;
292 uinfo->value.integer.max = tas_fw->nr_configurations - 1;
293
294 return 0;
295}
296
297static int tasdevice_program_get(struct snd_kcontrol *kcontrol,
298 struct snd_ctl_elem_value *ucontrol)
299{
300 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
301
302 mutex_lock(&tas_priv->codec_lock);
303
304 ucontrol->value.integer.value[0] = tas_priv->cur_prog;
305
306 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n",
307 __func__, kcontrol->id.name, tas_priv->cur_prog);
308
309 mutex_unlock(&tas_priv->codec_lock);
310
311 return 0;
312}
313
314static int tasdevice_program_put(struct snd_kcontrol *kcontrol,
315 struct snd_ctl_elem_value *ucontrol)
316{
317 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
318 struct tasdevice_fw *tas_fw = tas_priv->fmw;
319 int nr_program = ucontrol->value.integer.value[0];
320 int max = tas_fw->nr_programs - 1;
321 int val, ret = 0;
322
323 val = clamp(nr_program, 0, max);
324
325 mutex_lock(&tas_priv->codec_lock);
326
327 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n",
328 __func__, kcontrol->id.name, tas_priv->cur_prog, val);
329
330 if (tas_priv->cur_prog != val) {
331 tas_priv->cur_prog = val;
332 ret = 1;
333 }
334
335 mutex_unlock(&tas_priv->codec_lock);
336
337 return ret;
338}
339
340static int tasdevice_config_get(struct snd_kcontrol *kcontrol,
341 struct snd_ctl_elem_value *ucontrol)
342{
343 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
344
345 mutex_lock(&tas_priv->codec_lock);
346
347 ucontrol->value.integer.value[0] = tas_priv->cur_conf;
348
349 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n",
350 __func__, kcontrol->id.name, tas_priv->cur_conf);
351
352 mutex_unlock(&tas_priv->codec_lock);
353
354 return 0;
355}
356
357static int tasdevice_config_put(struct snd_kcontrol *kcontrol,
358 struct snd_ctl_elem_value *ucontrol)
359{
360 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
361 struct tasdevice_fw *tas_fw = tas_priv->fmw;
362 int nr_config = ucontrol->value.integer.value[0];
363 int max = tas_fw->nr_configurations - 1;
364 int val, ret = 0;
365
366 val = clamp(nr_config, 0, max);
367
368 mutex_lock(&tas_priv->codec_lock);
369
370 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n",
371 __func__, kcontrol->id.name, tas_priv->cur_conf, val);
372
373 if (tas_priv->cur_conf != val) {
374 tas_priv->cur_conf = val;
375 ret = 1;
376 }
377
378 mutex_unlock(&tas_priv->codec_lock);
379
380 return ret;
381}
382
383static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
384 struct snd_ctl_elem_value *ucontrol)
385{
386 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
387 struct soc_mixer_control *mc =
388 (struct soc_mixer_control *)kcontrol->private_value;
389 int ret;
390
391 mutex_lock(&tas_priv->codec_lock);
392
393 ret = tasdevice_amp_getvol(tas_priv, ucontrol, mc);
394
395 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %ld\n",
396 __func__, kcontrol->id.name, ucontrol->value.integer.value[0]);
397
398 mutex_unlock(&tas_priv->codec_lock);
399
400 return ret;
401}
402
403static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
404 struct snd_ctl_elem_value *ucontrol)
405{
406 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
407 struct soc_mixer_control *mc =
408 (struct soc_mixer_control *)kcontrol->private_value;
409 int ret;
410
411 mutex_lock(&tas_priv->codec_lock);
412
413 dev_dbg(tas_priv->dev, "%s: kcontrol %s: -> %ld\n",
414 __func__, kcontrol->id.name, ucontrol->value.integer.value[0]);
415
416 /* The check of the given value is in tasdevice_amp_putvol. */
417 ret = tasdevice_amp_putvol(tas_priv, ucontrol, mc);
418
419 mutex_unlock(&tas_priv->codec_lock);
420
421 return ret;
422}
423
424static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,
425 struct snd_ctl_elem_value *ucontrol)
426{
427 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
428
429 mutex_lock(&tas_priv->codec_lock);
430
431 ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
432 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n",
433 __func__, kcontrol->id.name, tas_priv->force_fwload_status);
434
435 mutex_unlock(&tas_priv->codec_lock);
436
437 return 0;
438}
439
440static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
441 struct snd_ctl_elem_value *ucontrol)
442{
443 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
444 bool change, val = (bool)ucontrol->value.integer.value[0];
445
446 mutex_lock(&tas_priv->codec_lock);
447
448 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n",
449 __func__, kcontrol->id.name,
450 tas_priv->force_fwload_status, val);
451
452 if (tas_priv->force_fwload_status == val)
453 change = false;
454 else {
455 change = true;
456 tas_priv->force_fwload_status = val;
457 }
458
459 mutex_unlock(&tas_priv->codec_lock);
460
461 return change;
462}
463
464static const struct snd_kcontrol_new tas2781_snd_controls[] = {
465 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL,
466 1, 0, 20, 0, tas2781_amp_getvol,
467 tas2781_amp_putvol, amp_vol_tlv),
468 ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
469 tas2781_force_fwload_get, tas2781_force_fwload_put),
470};
471
472static const struct snd_kcontrol_new tas2781_prof_ctrl = {
473 .name = "Speaker Profile Id",
474 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
475 .info = tasdevice_info_profile,
476 .get = tasdevice_get_profile_id,
477 .put = tasdevice_set_profile_id,
478};
479
480static const struct snd_kcontrol_new tas2781_dsp_prog_ctrl = {
481 .name = "Speaker Program Id",
482 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
483 .info = tasdevice_info_programs,
484 .get = tasdevice_program_get,
485 .put = tasdevice_program_put,
486};
487
488static const struct snd_kcontrol_new tas2781_dsp_conf_ctrl = {
489 .name = "Speaker Config Id",
490 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
491 .info = tasdevice_info_config,
492 .get = tasdevice_config_get,
493 .put = tasdevice_config_put,
494};
495
496static void tas2563_apply_calib(struct tasdevice_priv *tas_priv)
497{
498 int offset = 0;
499 __be32 data;
500 int ret;
501
502 for (int i = 0; i < tas_priv->ndev; i++) {
503 for (int j = 0; j < TAS2563_CAL_N; ++j) {
504 data = cpu_to_be32(
505 *(uint32_t *)&tas_priv->cali_data.data[offset]);
506 ret = tasdevice_dev_bulk_write(tas_priv, i, cal_regs[j],
507 (unsigned char *)&data, TAS2563_CAL_DATA_SIZE);
508 if (ret)
509 dev_err(tas_priv->dev,
510 "Error writing calib regs\n");
511 offset += TAS2563_CAL_DATA_SIZE;
512 }
513 }
514}
515
516static int tas2563_save_calibration(struct tasdevice_priv *tas_priv)
517{
518 static efi_guid_t efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc,
519 0x09, 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
520
521 static efi_char16_t *efi_vars[TAS2563_MAX_CHANNELS][TAS2563_CAL_N] = {
522 { L"Power_1", L"R0_1", L"InvR0_1", L"R0_Low_1", L"TLim_1" },
523 { L"Power_2", L"R0_2", L"InvR0_2", L"R0_Low_2", L"TLim_2" },
524 { L"Power_3", L"R0_3", L"InvR0_3", L"R0_Low_3", L"TLim_3" },
525 { L"Power_4", L"R0_4", L"InvR0_4", L"R0_Low_4", L"TLim_4" },
526 };
527
528 unsigned long max_size = TAS2563_CAL_DATA_SIZE;
529 unsigned int offset = 0;
530 efi_status_t status;
531 unsigned int attr;
532
533 tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev,
534 TAS2563_CAL_ARRAY_SIZE, GFP_KERNEL);
535 if (!tas_priv->cali_data.data)
536 return -ENOMEM;
537
538 for (int i = 0; i < tas_priv->ndev; ++i) {
539 for (int j = 0; j < TAS2563_CAL_N; ++j) {
540 status = efi.get_variable(efi_vars[i][j],
541 &efi_guid, &attr, &max_size,
542 &tas_priv->cali_data.data[offset]);
543 if (status != EFI_SUCCESS ||
544 max_size != TAS2563_CAL_DATA_SIZE) {
545 dev_warn(tas_priv->dev,
546 "Calibration data read failed %ld\n", status);
547 return -EINVAL;
548 }
549 offset += TAS2563_CAL_DATA_SIZE;
550 }
551 }
552
553 tas_priv->cali_data.total_sz = offset;
554 tasdevice_apply_calibration(tas_priv);
555
556 return 0;
557}
558
559static void tas2781_apply_calib(struct tasdevice_priv *tas_priv)
560{
561 static const unsigned char page_array[CALIB_MAX] = {
562 0x17, 0x18, 0x18, 0x13, 0x18,
563 };
564 static const unsigned char rgno_array[CALIB_MAX] = {
565 0x74, 0x0c, 0x14, 0x70, 0x7c,
566 };
567 int offset = 0;
568 int i, j, rc;
569 __be32 data;
570
571 for (i = 0; i < tas_priv->ndev; i++) {
572 for (j = 0; j < CALIB_MAX; j++) {
573 data = cpu_to_be32(
574 *(uint32_t *)&tas_priv->cali_data.data[offset]);
575 rc = tasdevice_dev_bulk_write(tas_priv, i,
576 TASDEVICE_REG(0, page_array[j], rgno_array[j]),
577 (unsigned char *)&data, 4);
578 if (rc < 0)
579 dev_err(tas_priv->dev,
580 "chn %d calib %d bulk_wr err = %d\n",
581 i, j, rc);
582 offset += 4;
583 }
584 }
585}
586
587/* Update the calibration data, including speaker impedance, f0, etc, into algo.
588 * Calibrate data is done by manufacturer in the factory. These data are used
589 * by Algo for calculating the speaker temperature, speaker membrane excursion
590 * and f0 in real time during playback.
591 */
592static int tas2781_save_calibration(struct tasdevice_priv *tas_priv)
593{
594 efi_guid_t efi_guid = EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d,
595 0x93, 0xfe, 0x5a, 0xa3, 0x5d, 0xb3);
596 static efi_char16_t efi_name[] = L"CALI_DATA";
597 struct tm *tm = &tas_priv->tm;
598 unsigned int attr, crc;
599 unsigned int *tmp_val;
600 efi_status_t status;
601
602 /* Lenovo devices */
603 if (tas_priv->catlog_id == LENOVO)
604 efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc, 0x09,
605 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92);
606
607 tas_priv->cali_data.total_sz = 0;
608 /* Get real size of UEFI variable */
609 status = efi.get_variable(efi_name, &efi_guid, &attr,
610 &tas_priv->cali_data.total_sz, tas_priv->cali_data.data);
611 if (status == EFI_BUFFER_TOO_SMALL) {
612 /* Allocate data buffer of data_size bytes */
613 tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev,
614 tas_priv->cali_data.total_sz, GFP_KERNEL);
615 if (!tas_priv->cali_data.data)
616 return -ENOMEM;
617 /* Get variable contents into buffer */
618 status = efi.get_variable(efi_name, &efi_guid, &attr,
619 &tas_priv->cali_data.total_sz,
620 tas_priv->cali_data.data);
621 }
622 if (status != EFI_SUCCESS)
623 return -EINVAL;
624
625 tmp_val = (unsigned int *)tas_priv->cali_data.data;
626
627 crc = crc32(~0, tas_priv->cali_data.data, 84) ^ ~0;
628 dev_dbg(tas_priv->dev, "cali crc 0x%08x PK tmp_val 0x%08x\n",
629 crc, tmp_val[21]);
630
631 if (crc == tmp_val[21]) {
632 time64_to_tm(tmp_val[20], 0, tm);
633 dev_dbg(tas_priv->dev, "%4ld-%2d-%2d, %2d:%2d:%2d\n",
634 tm->tm_year, tm->tm_mon, tm->tm_mday,
635 tm->tm_hour, tm->tm_min, tm->tm_sec);
636 tasdevice_apply_calibration(tas_priv);
637 } else
638 tas_priv->cali_data.total_sz = 0;
639
640 return 0;
641}
642
643static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
644{
645 struct hda_codec *codec = tas_hda->priv->codec;
646
647 snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
648 snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
649
650 for (int i = ARRAY_SIZE(tas_hda->snd_ctls) - 1; i >= 0; i--)
651 snd_ctl_remove(codec->card, tas_hda->snd_ctls[i]);
652
653 snd_ctl_remove(codec->card, tas_hda->prof_ctl);
654}
655
656static void tasdev_fw_ready(const struct firmware *fmw, void *context)
657{
658 struct tasdevice_priv *tas_priv = context;
659 struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
660 struct hda_codec *codec = tas_priv->codec;
661 int i, ret, spk_id;
662
663 pm_runtime_get_sync(tas_priv->dev);
664 mutex_lock(&tas_priv->codec_lock);
665
666 ret = tasdevice_rca_parser(tas_priv, fmw);
667 if (ret)
668 goto out;
669
670 tas_hda->prof_ctl = snd_ctl_new1(&tas2781_prof_ctrl, tas_priv);
671 ret = snd_ctl_add(codec->card, tas_hda->prof_ctl);
672 if (ret) {
673 dev_err(tas_priv->dev,
674 "Failed to add KControl %s = %d\n",
675 tas2781_prof_ctrl.name, ret);
676 goto out;
677 }
678
679 for (i = 0; i < ARRAY_SIZE(tas2781_snd_controls); i++) {
680 tas_hda->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_controls[i],
681 tas_priv);
682 ret = snd_ctl_add(codec->card, tas_hda->snd_ctls[i]);
683 if (ret) {
684 dev_err(tas_priv->dev,
685 "Failed to add KControl %s = %d\n",
686 tas2781_snd_controls[i].name, ret);
687 goto out;
688 }
689 }
690
691 tasdevice_dsp_remove(tas_priv);
692
693 tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
694 if (tas_priv->speaker_id != NULL) {
695 // Speaker id need to be checked for ASUS only.
696 spk_id = gpiod_get_value(tas_priv->speaker_id);
697 if (spk_id < 0) {
698 // Speaker id is not valid, use default.
699 dev_dbg(tas_priv->dev, "Wrong spk_id = %d\n", spk_id);
700 spk_id = 0;
701 }
702 snprintf(tas_priv->coef_binaryname,
703 sizeof(tas_priv->coef_binaryname),
704 "TAS2XXX%04X%d.bin",
705 lower_16_bits(codec->core.subsystem_id),
706 spk_id);
707 } else {
708 snprintf(tas_priv->coef_binaryname,
709 sizeof(tas_priv->coef_binaryname),
710 "TAS2XXX%04X.bin",
711 lower_16_bits(codec->core.subsystem_id));
712 }
713 ret = tasdevice_dsp_parser(tas_priv);
714 if (ret) {
715 dev_err(tas_priv->dev, "dspfw load %s error\n",
716 tas_priv->coef_binaryname);
717 tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
718 goto out;
719 }
720
721 tas_hda->dsp_prog_ctl = snd_ctl_new1(&tas2781_dsp_prog_ctrl,
722 tas_priv);
723 ret = snd_ctl_add(codec->card, tas_hda->dsp_prog_ctl);
724 if (ret) {
725 dev_err(tas_priv->dev,
726 "Failed to add KControl %s = %d\n",
727 tas2781_dsp_prog_ctrl.name, ret);
728 goto out;
729 }
730
731 tas_hda->dsp_conf_ctl = snd_ctl_new1(&tas2781_dsp_conf_ctrl,
732 tas_priv);
733 ret = snd_ctl_add(codec->card, tas_hda->dsp_conf_ctl);
734 if (ret) {
735 dev_err(tas_priv->dev,
736 "Failed to add KControl %s = %d\n",
737 tas2781_dsp_conf_ctrl.name, ret);
738 goto out;
739 }
740
741 tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
742 tasdevice_prmg_load(tas_priv, 0);
743 if (tas_priv->fmw->nr_programs > 0)
744 tas_priv->cur_prog = 0;
745 if (tas_priv->fmw->nr_configurations > 0)
746 tas_priv->cur_conf = 0;
747
748 /* If calibrated data occurs error, dsp will still works with default
749 * calibrated data inside algo.
750 */
751 tasdevice_save_calibration(tas_priv);
752
753 tasdevice_tuning_switch(tas_hda->priv, 0);
754 tas_hda->priv->playback_started = true;
755
756out:
757 mutex_unlock(&tas_hda->priv->codec_lock);
758 if (fmw)
759 release_firmware(fmw);
760 pm_runtime_mark_last_busy(tas_hda->dev);
761 pm_runtime_put_autosuspend(tas_hda->dev);
762}
763
764static int tas2781_hda_bind(struct device *dev, struct device *master,
765 void *master_data)
766{
767 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
768 struct hda_component_parent *parent = master_data;
769 struct hda_component *comp;
770 struct hda_codec *codec;
771 unsigned int subid;
772 int ret;
773
774 comp = hda_component_from_index(parent, tas_hda->priv->index);
775 if (!comp)
776 return -EINVAL;
777
778 if (comp->dev)
779 return -EBUSY;
780
781 codec = parent->codec;
782 subid = codec->core.subsystem_id >> 16;
783
784 switch (subid) {
785 case 0x17aa:
786 tas_hda->priv->catlog_id = LENOVO;
787 break;
788 default:
789 tas_hda->priv->catlog_id = OTHERS;
790 break;
791 }
792
793 pm_runtime_get_sync(dev);
794
795 comp->dev = dev;
796
797 strscpy(comp->name, dev_name(dev), sizeof(comp->name));
798
799 ret = tascodec_init(tas_hda->priv, codec, THIS_MODULE, tasdev_fw_ready);
800 if (!ret)
801 comp->playback_hook = tas2781_hda_playback_hook;
802
803 pm_runtime_mark_last_busy(dev);
804 pm_runtime_put_autosuspend(dev);
805
806 return ret;
807}
808
809static void tas2781_hda_unbind(struct device *dev,
810 struct device *master, void *master_data)
811{
812 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
813 struct hda_component_parent *parent = master_data;
814 struct hda_component *comp;
815
816 comp = hda_component_from_index(parent, tas_hda->priv->index);
817 if (comp && (comp->dev == dev)) {
818 comp->dev = NULL;
819 memset(comp->name, 0, sizeof(comp->name));
820 comp->playback_hook = NULL;
821 }
822
823 tas2781_hda_remove_controls(tas_hda);
824
825 tasdevice_config_info_remove(tas_hda->priv);
826 tasdevice_dsp_remove(tas_hda->priv);
827
828 tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
829}
830
831static const struct component_ops tas2781_hda_comp_ops = {
832 .bind = tas2781_hda_bind,
833 .unbind = tas2781_hda_unbind,
834};
835
836static void tas2781_hda_remove(struct device *dev)
837{
838 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
839
840 component_del(tas_hda->dev, &tas2781_hda_comp_ops);
841
842 pm_runtime_get_sync(tas_hda->dev);
843 pm_runtime_disable(tas_hda->dev);
844
845 pm_runtime_put_noidle(tas_hda->dev);
846
847 tasdevice_remove(tas_hda->priv);
848}
849
850static int tas2781_hda_i2c_probe(struct i2c_client *clt)
851{
852 struct tas2781_hda *tas_hda;
853 const char *device_name;
854 int ret;
855
856
857 tas_hda = devm_kzalloc(&clt->dev, sizeof(*tas_hda), GFP_KERNEL);
858 if (!tas_hda)
859 return -ENOMEM;
860
861 dev_set_drvdata(&clt->dev, tas_hda);
862 tas_hda->dev = &clt->dev;
863
864 tas_hda->priv = tasdevice_kzalloc(clt);
865 if (!tas_hda->priv)
866 return -ENOMEM;
867
868 if (strstr(dev_name(&clt->dev), "TIAS2781")) {
869 device_name = "TIAS2781";
870 tas_hda->priv->save_calibration = tas2781_save_calibration;
871 tas_hda->priv->apply_calibration = tas2781_apply_calib;
872 tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
873 } else if (strstr(dev_name(&clt->dev), "INT8866")) {
874 device_name = "INT8866";
875 tas_hda->priv->save_calibration = tas2563_save_calibration;
876 tas_hda->priv->apply_calibration = tas2563_apply_calib;
877 tas_hda->priv->global_addr = TAS2563_GLOBAL_ADDR;
878 } else
879 return -ENODEV;
880
881 tas_hda->priv->irq = clt->irq;
882 ret = tas2781_read_acpi(tas_hda->priv, device_name);
883 if (ret)
884 return dev_err_probe(tas_hda->dev, ret,
885 "Platform not supported\n");
886
887 ret = tasdevice_init(tas_hda->priv);
888 if (ret)
889 goto err;
890
891 pm_runtime_set_autosuspend_delay(tas_hda->dev, 3000);
892 pm_runtime_use_autosuspend(tas_hda->dev);
893 pm_runtime_mark_last_busy(tas_hda->dev);
894 pm_runtime_set_active(tas_hda->dev);
895 pm_runtime_enable(tas_hda->dev);
896
897 tasdevice_reset(tas_hda->priv);
898
899 ret = component_add(tas_hda->dev, &tas2781_hda_comp_ops);
900 if (ret) {
901 dev_err(tas_hda->dev, "Register component failed: %d\n", ret);
902 pm_runtime_disable(tas_hda->dev);
903 }
904
905err:
906 if (ret)
907 tas2781_hda_remove(&clt->dev);
908 return ret;
909}
910
911static void tas2781_hda_i2c_remove(struct i2c_client *clt)
912{
913 tas2781_hda_remove(&clt->dev);
914}
915
916static int tas2781_runtime_suspend(struct device *dev)
917{
918 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
919
920 dev_dbg(tas_hda->dev, "Runtime Suspend\n");
921
922 mutex_lock(&tas_hda->priv->codec_lock);
923
924 /* The driver powers up the amplifiers at module load time.
925 * Stop the playback if it's unused.
926 */
927 if (tas_hda->priv->playback_started) {
928 tasdevice_tuning_switch(tas_hda->priv, 1);
929 tas_hda->priv->playback_started = false;
930 }
931
932 mutex_unlock(&tas_hda->priv->codec_lock);
933
934 return 0;
935}
936
937static int tas2781_runtime_resume(struct device *dev)
938{
939 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
940
941 dev_dbg(tas_hda->dev, "Runtime Resume\n");
942
943 mutex_lock(&tas_hda->priv->codec_lock);
944
945 tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
946
947 /* If calibrated data occurs error, dsp will still works with default
948 * calibrated data inside algo.
949 */
950 tasdevice_apply_calibration(tas_hda->priv);
951
952 mutex_unlock(&tas_hda->priv->codec_lock);
953
954 return 0;
955}
956
957static int tas2781_system_suspend(struct device *dev)
958{
959 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
960
961 dev_dbg(tas_hda->priv->dev, "System Suspend\n");
962
963 mutex_lock(&tas_hda->priv->codec_lock);
964
965 /* Shutdown chip before system suspend */
966 if (tas_hda->priv->playback_started)
967 tasdevice_tuning_switch(tas_hda->priv, 1);
968
969 mutex_unlock(&tas_hda->priv->codec_lock);
970
971 /*
972 * Reset GPIO may be shared, so cannot reset here.
973 * However beyond this point, amps may be powered down.
974 */
975 return 0;
976}
977
978static int tas2781_system_resume(struct device *dev)
979{
980 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
981 int i;
982
983 dev_dbg(tas_hda->priv->dev, "System Resume\n");
984
985 mutex_lock(&tas_hda->priv->codec_lock);
986
987 for (i = 0; i < tas_hda->priv->ndev; i++) {
988 tas_hda->priv->tasdevice[i].cur_book = -1;
989 tas_hda->priv->tasdevice[i].cur_prog = -1;
990 tas_hda->priv->tasdevice[i].cur_conf = -1;
991 }
992 tasdevice_reset(tas_hda->priv);
993 tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
994
995 /* If calibrated data occurs error, dsp will still work with default
996 * calibrated data inside algo.
997 */
998 tasdevice_apply_calibration(tas_hda->priv);
999
1000 if (tas_hda->priv->playback_started)
1001 tasdevice_tuning_switch(tas_hda->priv, 0);
1002
1003 mutex_unlock(&tas_hda->priv->codec_lock);
1004
1005 return 0;
1006}
1007
1008static const struct dev_pm_ops tas2781_hda_pm_ops = {
1009 RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
1010 SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
1011};
1012
1013static const struct i2c_device_id tas2781_hda_i2c_id[] = {
1014 { "tas2781-hda" },
1015 {}
1016};
1017
1018static const struct acpi_device_id tas2781_acpi_hda_match[] = {
1019 {"TIAS2781", 0 },
1020 {"INT8866", 0 },
1021 {}
1022};
1023MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
1024
1025static struct i2c_driver tas2781_hda_i2c_driver = {
1026 .driver = {
1027 .name = "tas2781-hda",
1028 .acpi_match_table = tas2781_acpi_hda_match,
1029 .pm = &tas2781_hda_pm_ops,
1030 },
1031 .id_table = tas2781_hda_i2c_id,
1032 .probe = tas2781_hda_i2c_probe,
1033 .remove = tas2781_hda_i2c_remove,
1034};
1035module_i2c_driver(tas2781_hda_i2c_driver);
1036
1037MODULE_DESCRIPTION("TAS2781 HDA Driver");
1038MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
1039MODULE_LICENSE("GPL");
1040MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");