Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/sound/hda/codecs/side-codecs/tas2781_hda_spi.c
26481 views
1
// SPDX-License-Identifier: GPL-2.0
2
//
3
// TAS2781 HDA SPI driver
4
//
5
// Copyright 2024 - 2025 Texas Instruments, Inc.
6
//
7
// Author: Baojun Xu <[email protected]>
8
9
#include <linux/acpi.h>
10
#include <linux/array_size.h>
11
#include <linux/bits.h>
12
#include <linux/cleanup.h>
13
#include <linux/crc8.h>
14
#include <linux/crc32.h>
15
#include <linux/efi.h>
16
#include <linux/firmware.h>
17
#include <linux/mod_devicetable.h>
18
#include <linux/module.h>
19
#include <linux/mutex.h>
20
#include <linux/pm_runtime.h>
21
#include <linux/property.h>
22
#include <linux/regmap.h>
23
#include <linux/spi/spi.h>
24
#include <linux/time.h>
25
#include <linux/types.h>
26
#include <linux/units.h>
27
28
#include <sound/hda_codec.h>
29
#include <sound/soc.h>
30
#include <sound/tas2781.h>
31
#include <sound/tlv.h>
32
#include <sound/tas2781-tlv.h>
33
34
#include "hda_local.h"
35
#include "hda_auto_parser.h"
36
#include "hda_component.h"
37
#include "hda_jack.h"
38
#include "../generic.h"
39
#include "tas2781_hda.h"
40
41
#define TASDEVICE_RANGE_MAX_SIZE (256 * 128)
42
#define TASDEVICE_WIN_LEN 128
43
#define TAS2781_SPI_MAX_FREQ (4 * HZ_PER_MHZ)
44
/* Flag of calibration registers address. */
45
#define TASDEVICE_CALIBRATION_REG_ADDRESS BIT(7)
46
#define TASDEV_UEFI_CALI_REG_ADDR_FLG BIT(7)
47
48
/* System Reset Check Register */
49
#define TAS2781_REG_CLK_CONFIG TASDEVICE_REG(0x0, 0x0, 0x5c)
50
#define TAS2781_REG_CLK_CONFIG_RESET 0x19
51
52
struct tas2781_hda_spi_priv {
53
struct snd_kcontrol *snd_ctls[3];
54
};
55
56
static const struct regmap_range_cfg tasdevice_ranges[] = {
57
{
58
.range_min = 0,
59
.range_max = TASDEVICE_RANGE_MAX_SIZE,
60
.selector_reg = TASDEVICE_PAGE_SELECT,
61
.selector_mask = GENMASK(7, 0),
62
.selector_shift = 0,
63
.window_start = 0,
64
.window_len = TASDEVICE_WIN_LEN,
65
},
66
};
67
68
static const struct regmap_config tasdevice_regmap = {
69
.reg_bits = 8,
70
.val_bits = 8,
71
.zero_flag_mask = true,
72
.read_flag_mask = 0x01,
73
.reg_shift = -1,
74
.cache_type = REGCACHE_NONE,
75
.ranges = tasdevice_ranges,
76
.num_ranges = ARRAY_SIZE(tasdevice_ranges),
77
.max_register = TASDEVICE_RANGE_MAX_SIZE,
78
};
79
80
static int tasdevice_spi_dev_read(struct tasdevice_priv *tas_priv,
81
unsigned short chn, unsigned int reg, unsigned int *val)
82
{
83
int ret;
84
85
/*
86
* In our TAS2781 SPI mode, if read from other book (not book 0),
87
* or read from page number larger than 1 in book 0, one more byte
88
* read is needed, and first byte is a dummy byte, need to be ignored.
89
*/
90
if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) {
91
unsigned char data[2];
92
93
ret = tasdevice_dev_bulk_read(tas_priv, chn, reg,
94
data, sizeof(data));
95
*val = data[1];
96
} else {
97
ret = tasdevice_dev_read(tas_priv, chn, reg, val);
98
}
99
if (ret < 0)
100
dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
101
102
return ret;
103
}
104
105
static int tasdevice_spi_dev_bulk_read(struct tasdevice_priv *tas_priv,
106
unsigned short chn, unsigned int reg, unsigned char *data,
107
unsigned int len)
108
{
109
int ret;
110
111
/*
112
* In our TAS2781 SPI mode, if read from other book (not book 0),
113
* or read from page number larger than 1 in book 0, one more byte
114
* read is needed, and first byte is a dummy byte, need to be ignored.
115
*/
116
if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) {
117
unsigned char buf[TASDEVICE_WIN_LEN + 1];
118
119
ret = tasdevice_dev_bulk_read(tas_priv, chn, reg,
120
buf, len + 1);
121
memcpy(data, buf + 1, len);
122
} else {
123
ret = tasdevice_dev_bulk_read(tas_priv, chn, reg, data, len);
124
}
125
if (ret < 0)
126
dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
127
128
return ret;
129
}
130
131
static int tasdevice_spi_dev_update_bits(struct tasdevice_priv *tas_priv,
132
unsigned short chn, unsigned int reg, unsigned int mask,
133
unsigned int value)
134
{
135
int ret, val;
136
137
/*
138
* In our TAS2781 SPI mode, read/write was masked in last bit of
139
* address, it cause regmap_update_bits() not work as expected.
140
*/
141
ret = tasdevice_dev_read(tas_priv, chn, reg, &val);
142
if (ret < 0) {
143
dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
144
return ret;
145
}
146
147
ret = tasdevice_dev_write(tas_priv, chn, TASDEVICE_PAGE_REG(reg),
148
(val & ~mask) | (mask & value));
149
if (ret < 0)
150
dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
151
152
return ret;
153
}
154
155
static int tasdevice_spi_change_chn_book(struct tasdevice_priv *p,
156
unsigned short chn, int book)
157
{
158
int ret = 0;
159
160
if (chn == p->index) {
161
struct tasdevice *tasdev = &p->tasdevice[chn];
162
struct regmap *map = p->regmap;
163
164
if (tasdev->cur_book != book) {
165
ret = regmap_write(map, TASDEVICE_BOOKCTL_REG, book);
166
if (ret < 0)
167
dev_err(p->dev, "%s, E=%d\n", __func__, ret);
168
else
169
tasdev->cur_book = book;
170
}
171
} else {
172
ret = -EXDEV;
173
dev_dbg(p->dev, "Not error, %s ignore channel(%d)\n",
174
__func__, chn);
175
}
176
177
return ret;
178
}
179
180
static void tas2781_spi_reset(struct tasdevice_priv *tas_dev)
181
{
182
int ret;
183
184
if (tas_dev->reset) {
185
gpiod_set_value_cansleep(tas_dev->reset, 0);
186
fsleep(800);
187
gpiod_set_value_cansleep(tas_dev->reset, 1);
188
} else {
189
ret = tasdevice_dev_write(tas_dev, tas_dev->index,
190
TASDEVICE_REG_SWRESET, TASDEVICE_REG_SWRESET_RESET);
191
if (ret < 0) {
192
dev_err(tas_dev->dev, "dev sw-reset fail, %d\n", ret);
193
return;
194
}
195
fsleep(1000);
196
}
197
}
198
199
static int tascodec_spi_init(struct tasdevice_priv *tas_priv,
200
void *codec, struct module *module,
201
void (*cont)(const struct firmware *fw, void *context))
202
{
203
int ret;
204
205
/*
206
* Codec Lock Hold to ensure that codec_probe and firmware parsing and
207
* loading do not simultaneously execute.
208
*/
209
guard(mutex)(&tas_priv->codec_lock);
210
211
scnprintf(tas_priv->rca_binaryname,
212
sizeof(tas_priv->rca_binaryname), "%sRCA%d.bin",
213
tas_priv->dev_name, tas_priv->ndev);
214
crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL);
215
tas_priv->codec = codec;
216
ret = request_firmware_nowait(module, FW_ACTION_UEVENT,
217
tas_priv->rca_binaryname, tas_priv->dev, GFP_KERNEL, tas_priv,
218
cont);
219
if (ret)
220
dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n",
221
ret);
222
223
return ret;
224
}
225
226
static void tasdevice_spi_init(struct tasdevice_priv *tas_priv)
227
{
228
tas_priv->tasdevice[tas_priv->index].cur_book = -1;
229
tas_priv->tasdevice[tas_priv->index].cur_conf = -1;
230
tas_priv->tasdevice[tas_priv->index].cur_prog = -1;
231
232
tas_priv->isspi = true;
233
234
tas_priv->update_bits = tasdevice_spi_dev_update_bits;
235
tas_priv->change_chn_book = tasdevice_spi_change_chn_book;
236
tas_priv->dev_read = tasdevice_spi_dev_read;
237
tas_priv->dev_bulk_read = tasdevice_spi_dev_bulk_read;
238
239
mutex_init(&tas_priv->codec_lock);
240
}
241
242
static int tasdevice_spi_amp_putvol(struct tasdevice_priv *tas_priv,
243
struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
244
{
245
unsigned int invert = mc->invert;
246
unsigned char mask;
247
int max = mc->max;
248
int val, ret;
249
250
mask = rounddown_pow_of_two(max);
251
mask <<= mc->shift;
252
val = clamp(invert ? max - ucontrol->value.integer.value[0] :
253
ucontrol->value.integer.value[0], 0, max);
254
255
ret = tasdevice_spi_dev_update_bits(tas_priv, tas_priv->index,
256
mc->reg, mask, (unsigned int)(val << mc->shift));
257
if (ret)
258
dev_err(tas_priv->dev, "set AMP vol error in dev %d\n",
259
tas_priv->index);
260
261
return ret;
262
}
263
264
static int tasdevice_spi_amp_getvol(struct tasdevice_priv *tas_priv,
265
struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
266
{
267
unsigned int invert = mc->invert;
268
unsigned char mask = 0;
269
int max = mc->max;
270
int ret, val;
271
272
ret = tasdevice_spi_dev_read(tas_priv, tas_priv->index, mc->reg, &val);
273
if (ret) {
274
dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__);
275
return ret;
276
}
277
278
mask = rounddown_pow_of_two(max);
279
mask <<= mc->shift;
280
val = (val & mask) >> mc->shift;
281
val = clamp(invert ? max - val : val, 0, max);
282
ucontrol->value.integer.value[0] = val;
283
284
return ret;
285
}
286
287
static int tasdevice_spi_digital_putvol(struct tasdevice_priv *p,
288
struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
289
{
290
unsigned int invert = mc->invert;
291
int max = mc->max;
292
int val, ret;
293
294
val = clamp(invert ? max - ucontrol->value.integer.value[0] :
295
ucontrol->value.integer.value[0], 0, max);
296
ret = tasdevice_dev_write(p, p->index, mc->reg, (unsigned int)val);
297
if (ret)
298
dev_err(p->dev, "set digital vol err in dev %d\n", p->index);
299
300
return ret;
301
}
302
303
static int tasdevice_spi_digital_getvol(struct tasdevice_priv *p,
304
struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
305
{
306
unsigned int invert = mc->invert;
307
int max = mc->max;
308
int ret, val;
309
310
ret = tasdevice_spi_dev_read(p, p->index, mc->reg, &val);
311
if (ret) {
312
dev_err(p->dev, "%s, get digital vol err\n", __func__);
313
return ret;
314
}
315
316
val = clamp(invert ? max - val : val, 0, max);
317
ucontrol->value.integer.value[0] = val;
318
319
return ret;
320
}
321
322
static int tas2781_read_acpi(struct tas2781_hda *tas_hda,
323
const char *hid, int id)
324
{
325
struct tasdevice_priv *p = tas_hda->priv;
326
struct acpi_device *adev;
327
struct device *physdev;
328
u32 values[HDA_MAX_COMPONENTS];
329
const char *property;
330
size_t nval;
331
int ret, i;
332
333
adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
334
if (!adev) {
335
dev_err(p->dev, "Failed to find ACPI device: %s\n", hid);
336
return -ENODEV;
337
}
338
339
strscpy(p->dev_name, hid, sizeof(p->dev_name));
340
physdev = get_device(acpi_get_first_physical_node(adev));
341
acpi_dev_put(adev);
342
343
property = "ti,dev-index";
344
ret = device_property_count_u32(physdev, property);
345
if (ret <= 0 || ret > ARRAY_SIZE(values)) {
346
ret = -EINVAL;
347
goto err;
348
}
349
p->ndev = nval = ret;
350
351
ret = device_property_read_u32_array(physdev, property, values, nval);
352
if (ret)
353
goto err;
354
355
p->index = U8_MAX;
356
for (i = 0; i < nval; i++) {
357
if (values[i] == id) {
358
p->index = i;
359
break;
360
}
361
}
362
if (p->index == U8_MAX) {
363
dev_dbg(p->dev, "No index found in %s\n", property);
364
ret = -ENODEV;
365
goto err;
366
}
367
368
if (p->index == 0) {
369
/* All of amps share same RESET pin. */
370
p->reset = devm_gpiod_get_index_optional(physdev, "reset",
371
p->index, GPIOD_OUT_LOW);
372
if (IS_ERR(p->reset)) {
373
ret = PTR_ERR(p->reset);
374
dev_err_probe(p->dev, ret, "Failed on reset GPIO\n");
375
goto err;
376
}
377
}
378
put_device(physdev);
379
380
return 0;
381
err:
382
dev_err(p->dev, "read acpi error, ret: %d\n", ret);
383
put_device(physdev);
384
acpi_dev_put(adev);
385
386
return ret;
387
}
388
389
static void tas2781_hda_playback_hook(struct device *dev, int action)
390
{
391
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
392
struct tasdevice_priv *tas_priv = tas_hda->priv;
393
394
if (action == HDA_GEN_PCM_ACT_OPEN) {
395
pm_runtime_get_sync(dev);
396
guard(mutex)(&tas_priv->codec_lock);
397
if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK)
398
tasdevice_tuning_switch(tas_hda->priv, 0);
399
} else if (action == HDA_GEN_PCM_ACT_CLOSE) {
400
guard(mutex)(&tas_priv->codec_lock);
401
if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK)
402
tasdevice_tuning_switch(tas_priv, 1);
403
pm_runtime_put_autosuspend(dev);
404
}
405
}
406
407
/*
408
* tas2781_digital_getvol - get the volum control
409
* @kcontrol: control pointer
410
* @ucontrol: User data
411
*
412
* Customer Kcontrol for tas2781 is primarily for regmap booking, paging
413
* depends on internal regmap mechanism.
414
* tas2781 contains book and page two-level register map, especially
415
* book switching will set the register BXXP00R7F, after switching to the
416
* correct book, then leverage the mechanism for paging to access the
417
* register.
418
*
419
* Return 0 if succeeded.
420
*/
421
static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,
422
struct snd_ctl_elem_value *ucontrol)
423
{
424
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
425
struct soc_mixer_control *mc =
426
(struct soc_mixer_control *)kcontrol->private_value;
427
428
guard(mutex)(&tas_priv->codec_lock);
429
return tasdevice_spi_digital_getvol(tas_priv, ucontrol, mc);
430
}
431
432
static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
433
struct snd_ctl_elem_value *ucontrol)
434
{
435
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
436
struct soc_mixer_control *mc =
437
(struct soc_mixer_control *)kcontrol->private_value;
438
439
guard(mutex)(&tas_priv->codec_lock);
440
return tasdevice_spi_amp_getvol(tas_priv, ucontrol, mc);
441
}
442
443
static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,
444
struct snd_ctl_elem_value *ucontrol)
445
{
446
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
447
struct soc_mixer_control *mc =
448
(struct soc_mixer_control *)kcontrol->private_value;
449
450
guard(mutex)(&tas_priv->codec_lock);
451
return tasdevice_spi_digital_putvol(tas_priv, ucontrol, mc);
452
}
453
454
static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
455
struct snd_ctl_elem_value *ucontrol)
456
{
457
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
458
struct soc_mixer_control *mc =
459
(struct soc_mixer_control *)kcontrol->private_value;
460
461
guard(mutex)(&tas_priv->codec_lock);
462
return tasdevice_spi_amp_putvol(tas_priv, ucontrol, mc);
463
}
464
465
static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,
466
struct snd_ctl_elem_value *ucontrol)
467
{
468
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
469
470
ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
471
dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
472
str_on_off(tas_priv->force_fwload_status));
473
474
return 0;
475
}
476
477
static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
478
struct snd_ctl_elem_value *ucontrol)
479
{
480
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
481
bool change, val = (bool)ucontrol->value.integer.value[0];
482
483
if (tas_priv->force_fwload_status == val) {
484
change = false;
485
} else {
486
change = true;
487
tas_priv->force_fwload_status = val;
488
}
489
dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
490
str_on_off(tas_priv->force_fwload_status));
491
492
return change;
493
}
494
495
static struct snd_kcontrol_new tas2781_snd_ctls[] = {
496
ACARD_SINGLE_RANGE_EXT_TLV(NULL, TAS2781_AMP_LEVEL, 1, 0, 20, 0,
497
tas2781_amp_getvol, tas2781_amp_putvol,
498
tas2781_amp_tlv),
499
ACARD_SINGLE_RANGE_EXT_TLV(NULL, TAS2781_DVC_LVL, 0, 0, 200, 1,
500
tas2781_digital_getvol, tas2781_digital_putvol,
501
tas2781_dvc_tlv),
502
ACARD_SINGLE_BOOL_EXT(NULL, 0, tas2781_force_fwload_get,
503
tas2781_force_fwload_put),
504
};
505
506
static struct snd_kcontrol_new tas2781_prof_ctl = {
507
.iface = SNDRV_CTL_ELEM_IFACE_CARD,
508
.info = tasdevice_info_profile,
509
.get = tasdevice_get_profile_id,
510
.put = tasdevice_set_profile_id,
511
};
512
513
static struct snd_kcontrol_new tas2781_dsp_ctls[] = {
514
/* Speaker Program */
515
{
516
.iface = SNDRV_CTL_ELEM_IFACE_CARD,
517
.info = tasdevice_info_programs,
518
.get = tasdevice_program_get,
519
.put = tasdevice_program_put,
520
},
521
/* Speaker Config */
522
{
523
.iface = SNDRV_CTL_ELEM_IFACE_CARD,
524
.info = tasdevice_info_config,
525
.get = tasdevice_config_get,
526
.put = tasdevice_config_put,
527
},
528
};
529
530
static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
531
{
532
struct hda_codec *codec = tas_hda->priv->codec;
533
struct tas2781_hda_spi_priv *h_priv = tas_hda->hda_priv;
534
535
snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
536
537
snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
538
539
for (int i = ARRAY_SIZE(h_priv->snd_ctls) - 1; i >= 0; i--)
540
snd_ctl_remove(codec->card, h_priv->snd_ctls[i]);
541
542
snd_ctl_remove(codec->card, tas_hda->prof_ctl);
543
}
544
545
static int tas2781_hda_spi_prf_ctl(struct tas2781_hda *h)
546
{
547
struct tasdevice_priv *p = h->priv;
548
struct hda_codec *c = p->codec;
549
char name[64];
550
int rc;
551
552
snprintf(name, sizeof(name), "Speaker-%d Profile Id", p->index);
553
tas2781_prof_ctl.name = name;
554
h->prof_ctl = snd_ctl_new1(&tas2781_prof_ctl, p);
555
rc = snd_ctl_add(c->card, h->prof_ctl);
556
if (rc)
557
dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
558
tas2781_prof_ctl.name, rc);
559
return rc;
560
}
561
562
static int tas2781_hda_spi_snd_ctls(struct tas2781_hda *h)
563
{
564
struct tas2781_hda_spi_priv *h_priv = h->hda_priv;
565
struct tasdevice_priv *p = h->priv;
566
struct hda_codec *c = p->codec;
567
char name[64];
568
int i = 0;
569
int rc;
570
571
snprintf(name, sizeof(name), "Speaker-%d Analog Volume", p->index);
572
tas2781_snd_ctls[i].name = name;
573
h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);
574
rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);
575
if (rc) {
576
dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
577
tas2781_snd_ctls[i].name, rc);
578
return rc;
579
}
580
i++;
581
snprintf(name, sizeof(name), "Speaker-%d Digital Volume", p->index);
582
tas2781_snd_ctls[i].name = name;
583
h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);
584
rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);
585
if (rc) {
586
dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
587
tas2781_snd_ctls[i].name, rc);
588
return rc;
589
}
590
i++;
591
snprintf(name, sizeof(name), "Froce Speaker-%d FW Load", p->index);
592
tas2781_snd_ctls[i].name = name;
593
h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);
594
rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);
595
if (rc) {
596
dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
597
tas2781_snd_ctls[i].name, rc);
598
}
599
return rc;
600
}
601
602
static int tas2781_hda_spi_dsp_ctls(struct tas2781_hda *h)
603
{
604
struct tasdevice_priv *p = h->priv;
605
struct hda_codec *c = p->codec;
606
char name[64];
607
int i = 0;
608
int rc;
609
610
snprintf(name, sizeof(name), "Speaker-%d Program Id", p->index);
611
tas2781_dsp_ctls[i].name = name;
612
h->dsp_prog_ctl = snd_ctl_new1(&tas2781_dsp_ctls[i], p);
613
rc = snd_ctl_add(c->card, h->dsp_prog_ctl);
614
if (rc) {
615
dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
616
tas2781_dsp_ctls[i].name, rc);
617
return rc;
618
}
619
i++;
620
snprintf(name, sizeof(name), "Speaker-%d Config Id", p->index);
621
tas2781_dsp_ctls[i].name = name;
622
h->dsp_conf_ctl = snd_ctl_new1(&tas2781_dsp_ctls[i], p);
623
rc = snd_ctl_add(c->card, h->dsp_conf_ctl);
624
if (rc) {
625
dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
626
tas2781_dsp_ctls[i].name, rc);
627
}
628
629
return rc;
630
}
631
632
static void tasdev_fw_ready(const struct firmware *fmw, void *context)
633
{
634
struct tasdevice_priv *tas_priv = context;
635
struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
636
struct hda_codec *codec = tas_priv->codec;
637
int ret, val;
638
639
pm_runtime_get_sync(tas_priv->dev);
640
guard(mutex)(&tas_priv->codec_lock);
641
642
ret = tasdevice_rca_parser(tas_priv, fmw);
643
if (ret)
644
goto out;
645
646
/* Add control one time only. */
647
ret = tas2781_hda_spi_prf_ctl(tas_hda);
648
if (ret)
649
goto out;
650
651
ret = tas2781_hda_spi_snd_ctls(tas_hda);
652
if (ret)
653
goto out;
654
655
tasdevice_dsp_remove(tas_priv);
656
657
tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
658
scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%04X-%01d.bin",
659
lower_16_bits(codec->core.subsystem_id), tas_priv->index);
660
ret = tasdevice_dsp_parser(tas_priv);
661
if (ret) {
662
dev_err(tas_priv->dev, "dspfw load %s error\n",
663
tas_priv->coef_binaryname);
664
tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
665
goto out;
666
}
667
668
ret = tas2781_hda_spi_dsp_ctls(tas_hda);
669
if (ret)
670
goto out;
671
/* Perform AMP reset before firmware download. */
672
tas2781_spi_reset(tas_priv);
673
tas_priv->rcabin.profile_cfg_id = 0;
674
675
tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
676
ret = tasdevice_spi_dev_read(tas_priv, tas_priv->index,
677
TAS2781_REG_CLK_CONFIG, &val);
678
if (ret < 0)
679
goto out;
680
681
if (val == TAS2781_REG_CLK_CONFIG_RESET) {
682
ret = tasdevice_prmg_load(tas_priv, 0);
683
if (ret < 0) {
684
dev_err(tas_priv->dev, "FW download failed = %d\n",
685
ret);
686
goto out;
687
}
688
tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
689
}
690
if (tas_priv->fmw->nr_programs > 0)
691
tas_priv->tasdevice[tas_priv->index].cur_prog = 0;
692
if (tas_priv->fmw->nr_configurations > 0)
693
tas_priv->tasdevice[tas_priv->index].cur_conf = 0;
694
695
/*
696
* If calibrated data occurs error, dsp will still works with default
697
* calibrated data inside algo.
698
*/
699
tas2781_save_calibration(tas_hda);
700
out:
701
release_firmware(fmw);
702
pm_runtime_put_autosuspend(tas_hda->priv->dev);
703
}
704
705
static int tas2781_hda_bind(struct device *dev, struct device *master,
706
void *master_data)
707
{
708
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
709
struct hda_component_parent *parent = master_data;
710
struct hda_component *comp;
711
struct hda_codec *codec;
712
int ret;
713
714
comp = hda_component_from_index(parent, tas_hda->priv->index);
715
if (!comp)
716
return -EINVAL;
717
718
if (comp->dev)
719
return -EBUSY;
720
721
codec = parent->codec;
722
723
pm_runtime_get_sync(dev);
724
725
comp->dev = dev;
726
727
strscpy(comp->name, dev_name(dev), sizeof(comp->name));
728
729
ret = tascodec_spi_init(tas_hda->priv, codec, THIS_MODULE,
730
tasdev_fw_ready);
731
if (!ret)
732
comp->playback_hook = tas2781_hda_playback_hook;
733
734
pm_runtime_put_autosuspend(dev);
735
736
return ret;
737
}
738
739
static void tas2781_hda_unbind(struct device *dev, struct device *master,
740
void *master_data)
741
{
742
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
743
struct hda_component_parent *parent = master_data;
744
struct tasdevice_priv *tas_priv = tas_hda->priv;
745
struct hda_component *comp;
746
747
comp = hda_component_from_index(parent, tas_priv->index);
748
if (comp && (comp->dev == dev)) {
749
comp->dev = NULL;
750
memset(comp->name, 0, sizeof(comp->name));
751
comp->playback_hook = NULL;
752
}
753
754
tas2781_hda_remove_controls(tas_hda);
755
756
tasdevice_config_info_remove(tas_priv);
757
tasdevice_dsp_remove(tas_priv);
758
759
tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
760
}
761
762
static const struct component_ops tas2781_hda_comp_ops = {
763
.bind = tas2781_hda_bind,
764
.unbind = tas2781_hda_unbind,
765
};
766
767
static int tas2781_hda_spi_probe(struct spi_device *spi)
768
{
769
struct tas2781_hda_spi_priv *hda_priv;
770
struct tasdevice_priv *tas_priv;
771
struct tas2781_hda *tas_hda;
772
const char *device_name;
773
int ret = 0;
774
775
tas_hda = devm_kzalloc(&spi->dev, sizeof(*tas_hda), GFP_KERNEL);
776
if (!tas_hda)
777
return -ENOMEM;
778
779
hda_priv = devm_kzalloc(&spi->dev, sizeof(*hda_priv), GFP_KERNEL);
780
if (!hda_priv)
781
return -ENOMEM;
782
783
tas_hda->hda_priv = hda_priv;
784
spi->max_speed_hz = TAS2781_SPI_MAX_FREQ;
785
786
tas_priv = devm_kzalloc(&spi->dev, sizeof(*tas_priv), GFP_KERNEL);
787
if (!tas_priv)
788
return -ENOMEM;
789
tas_priv->dev = &spi->dev;
790
tas_hda->priv = tas_priv;
791
tas_priv->regmap = devm_regmap_init_spi(spi, &tasdevice_regmap);
792
if (IS_ERR(tas_priv->regmap)) {
793
ret = PTR_ERR(tas_priv->regmap);
794
dev_err(tas_priv->dev, "Failed to allocate regmap: %d\n",
795
ret);
796
return ret;
797
}
798
if (strstr(dev_name(&spi->dev), "TXNW2781")) {
799
device_name = "TXNW2781";
800
} else {
801
dev_err(tas_priv->dev, "Unmatched spi dev %s\n",
802
dev_name(&spi->dev));
803
return -ENODEV;
804
}
805
806
tas_priv->irq = spi->irq;
807
dev_set_drvdata(&spi->dev, tas_hda);
808
ret = tas2781_read_acpi(tas_hda, device_name,
809
spi_get_chipselect(spi, 0));
810
if (ret)
811
return dev_err_probe(tas_priv->dev, ret,
812
"Platform not supported\n");
813
814
tasdevice_spi_init(tas_priv);
815
816
pm_runtime_set_autosuspend_delay(tas_priv->dev, 3000);
817
pm_runtime_use_autosuspend(tas_priv->dev);
818
pm_runtime_set_active(tas_priv->dev);
819
pm_runtime_get_noresume(tas_priv->dev);
820
pm_runtime_enable(tas_priv->dev);
821
822
pm_runtime_put_autosuspend(tas_priv->dev);
823
824
ret = component_add(tas_priv->dev, &tas2781_hda_comp_ops);
825
if (ret) {
826
dev_err(tas_priv->dev, "Register component fail: %d\n", ret);
827
pm_runtime_disable(tas_priv->dev);
828
tas2781_hda_remove(&spi->dev, &tas2781_hda_comp_ops);
829
}
830
831
return ret;
832
}
833
834
static void tas2781_hda_spi_remove(struct spi_device *spi)
835
{
836
tas2781_hda_remove(&spi->dev, &tas2781_hda_comp_ops);
837
}
838
839
static int tas2781_runtime_suspend(struct device *dev)
840
{
841
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
842
struct tasdevice_priv *tas_priv = tas_hda->priv;
843
844
guard(mutex)(&tas_priv->codec_lock);
845
846
if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK
847
&& tas_priv->playback_started)
848
tasdevice_tuning_switch(tas_priv, 1);
849
850
tas_priv->tasdevice[tas_priv->index].cur_book = -1;
851
tas_priv->tasdevice[tas_priv->index].cur_conf = -1;
852
853
return 0;
854
}
855
856
static int tas2781_runtime_resume(struct device *dev)
857
{
858
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
859
struct tasdevice_priv *tas_priv = tas_hda->priv;
860
861
guard(mutex)(&tas_priv->codec_lock);
862
863
if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK
864
&& tas_priv->playback_started)
865
tasdevice_tuning_switch(tas_priv, 0);
866
867
return 0;
868
}
869
870
static int tas2781_system_suspend(struct device *dev)
871
{
872
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
873
struct tasdevice_priv *tas_priv = tas_hda->priv;
874
int ret;
875
876
ret = pm_runtime_force_suspend(dev);
877
if (ret)
878
return ret;
879
880
/* Shutdown chip before system suspend */
881
if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK
882
&& tas_priv->playback_started)
883
tasdevice_tuning_switch(tas_priv, 1);
884
885
return 0;
886
}
887
888
static int tas2781_system_resume(struct device *dev)
889
{
890
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
891
struct tasdevice_priv *tas_priv = tas_hda->priv;
892
int ret, val;
893
894
ret = pm_runtime_force_resume(dev);
895
if (ret)
896
return ret;
897
898
guard(mutex)(&tas_priv->codec_lock);
899
ret = tas_priv->dev_read(tas_priv, tas_priv->index,
900
TAS2781_REG_CLK_CONFIG, &val);
901
if (ret < 0)
902
return ret;
903
904
if (val == TAS2781_REG_CLK_CONFIG_RESET) {
905
tas_priv->tasdevice[tas_priv->index].cur_book = -1;
906
tas_priv->tasdevice[tas_priv->index].cur_conf = -1;
907
tas_priv->tasdevice[tas_priv->index].cur_prog = -1;
908
909
ret = tasdevice_prmg_load(tas_priv, 0);
910
if (ret < 0) {
911
dev_err(tas_priv->dev,
912
"FW download failed = %d\n", ret);
913
return ret;
914
}
915
tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
916
917
if (tas_priv->playback_started)
918
tasdevice_tuning_switch(tas_priv, 0);
919
}
920
921
return ret;
922
}
923
924
static const struct dev_pm_ops tas2781_hda_pm_ops = {
925
RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
926
SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
927
};
928
929
static const struct spi_device_id tas2781_hda_spi_id[] = {
930
{ "tas2781-hda", },
931
{}
932
};
933
934
static const struct acpi_device_id tas2781_acpi_hda_match[] = {
935
{"TXNW2781", },
936
{}
937
};
938
MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
939
940
static struct spi_driver tas2781_hda_spi_driver = {
941
.driver = {
942
.name = "tas2781-hda",
943
.acpi_match_table = tas2781_acpi_hda_match,
944
.pm = &tas2781_hda_pm_ops,
945
},
946
.id_table = tas2781_hda_spi_id,
947
.probe = tas2781_hda_spi_probe,
948
.remove = tas2781_hda_spi_remove,
949
};
950
module_spi_driver(tas2781_hda_spi_driver);
951
952
MODULE_DESCRIPTION("TAS2781 HDA SPI Driver");
953
MODULE_AUTHOR("Baojun, Xu, <[email protected]>");
954
MODULE_LICENSE("GPL");
955
MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");
956
MODULE_IMPORT_NS("SND_HDA_SCODEC_TAS2781");
957
958