Debian Codegraph @

sound/hda/codecs/side-codecs/tas2781_hda_spi.c

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