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