1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // TAS2781 HDA SPI driver
4 //
5 // Copyright 2024 Texas Instruments, Inc.
6 //
7 // Author: Baojun Xu <baojun.xu@ti.com>
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-dsp.h>
31 #include <sound/tlv.h>
32 #include <sound/tas2781-tlv.h>
33
34 #include "tas2781-spi.h"
35
36 #include "hda_local.h"
37 #include "hda_auto_parser.h"
38 #include "hda_component.h"
39 #include "hda_jack.h"
40 #include "hda_generic.h"
41
42 /*
43 * No standard control callbacks for SNDRV_CTL_ELEM_IFACE_CARD
44 * Define two controls, one is Volume control callbacks, the other is
45 * flag setting control callbacks.
46 */
47
48 /* Volume control callbacks for tas2781 */
49 #define ACARD_SINGLE_RANGE_EXT_TLV(xname, xreg, xshift, xmin, xmax, xinvert, \
50 xhandler_get, xhandler_put, tlv_array) { \
51 .iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = (xname), \
52 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
53 SNDRV_CTL_ELEM_ACCESS_READWRITE, \
54 .tlv.p = (tlv_array), \
55 .info = snd_soc_info_volsw, \
56 .get = xhandler_get, .put = xhandler_put, \
57 .private_value = (unsigned long)&(struct soc_mixer_control) { \
58 .reg = xreg, .rreg = xreg, \
59 .shift = xshift, .rshift = xshift,\
60 .min = xmin, .max = xmax, .invert = xinvert, \
61 } \
62 }
63
64 /* Flag control callbacks for tas2781 */
65 #define ACARD_SINGLE_BOOL_EXT(xname, xdata, xhandler_get, xhandler_put) { \
66 .iface = SNDRV_CTL_ELEM_IFACE_CARD, \
67 .name = xname, \
68 .info = snd_ctl_boolean_mono_info, \
69 .get = xhandler_get, \
70 .put = xhandler_put, \
71 .private_value = xdata, \
72 }
73
74 struct tas2781_hda {
75 struct tasdevice_priv *priv;
76 struct acpi_device *dacpi;
77 struct snd_kcontrol *dsp_prog_ctl;
78 struct snd_kcontrol *dsp_conf_ctl;
79 struct snd_kcontrol *snd_ctls[3];
80 struct snd_kcontrol *prof_ctl;
81 };
82
83 static const struct regmap_range_cfg tasdevice_ranges[] = {
84 {
85 .range_min = 0,
86 .range_max = TASDEVICE_MAX_SIZE,
87 .selector_reg = TASDEVICE_PAGE_SELECT,
88 .selector_mask = GENMASK(7, 0),
89 .selector_shift = 0,
90 .window_start = 0,
91 .window_len = TASDEVICE_MAX_PAGE,
92 },
93 };
94
95 static const struct regmap_config tasdevice_regmap = {
96 .reg_bits = 8,
97 .val_bits = 8,
98 .zero_flag_mask = true,
99 .cache_type = REGCACHE_NONE,
100 .ranges = tasdevice_ranges,
101 .num_ranges = ARRAY_SIZE(tasdevice_ranges),
102 .max_register = TASDEVICE_MAX_SIZE,
103 };
104
tasdevice_spi_switch_book(struct tasdevice_priv * tas_priv,int reg)105 static int tasdevice_spi_switch_book(struct tasdevice_priv *tas_priv, int reg)
106 {
107 struct regmap *map = tas_priv->regmap;
108
109 if (tas_priv->cur_book != TASDEVICE_BOOK_ID(reg)) {
110 int ret = regmap_write(map, TASDEVICE_BOOKCTL_REG,
111 TASDEVICE_BOOK_ID(reg));
112 if (ret < 0) {
113 dev_err(tas_priv->dev, "Switch Book E=%d\n", ret);
114 return ret;
115 }
116 tas_priv->cur_book = TASDEVICE_BOOK_ID(reg);
117 }
118 return 0;
119 }
120
tasdevice_spi_dev_read(struct tasdevice_priv * tas_priv,unsigned int reg,unsigned int * val)121 int tasdevice_spi_dev_read(struct tasdevice_priv *tas_priv,
122 unsigned int reg,
123 unsigned int *val)
124 {
125 struct regmap *map = tas_priv->regmap;
126 int ret;
127
128 ret = tasdevice_spi_switch_book(tas_priv, reg);
129 if (ret < 0)
130 return ret;
131
132 /*
133 * In our TAS2781 SPI mode, if read from other book (not book 0),
134 * or read from page number larger than 1 in book 0, one more byte
135 * read is needed, and first byte is a dummy byte, need to be ignored.
136 */
137 if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) {
138 unsigned char data[2];
139
140 ret = regmap_bulk_read(map, TASDEVICE_PAGE_REG(reg) | 1,
141 data, sizeof(data));
142 *val = data[1];
143 } else {
144 ret = regmap_read(map, TASDEVICE_PAGE_REG(reg) | 1, val);
145 }
146 if (ret < 0)
147 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
148
149 return ret;
150 }
151
tasdevice_spi_dev_write(struct tasdevice_priv * tas_priv,unsigned int reg,unsigned int value)152 int tasdevice_spi_dev_write(struct tasdevice_priv *tas_priv,
153 unsigned int reg,
154 unsigned int value)
155 {
156 struct regmap *map = tas_priv->regmap;
157 int ret;
158
159 ret = tasdevice_spi_switch_book(tas_priv, reg);
160 if (ret < 0)
161 return ret;
162
163 ret = regmap_write(map, TASDEVICE_PAGE_REG(reg), value);
164 if (ret < 0)
165 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
166
167 return ret;
168 }
169
tasdevice_spi_dev_bulk_write(struct tasdevice_priv * tas_priv,unsigned int reg,unsigned char * data,unsigned int len)170 int tasdevice_spi_dev_bulk_write(struct tasdevice_priv *tas_priv,
171 unsigned int reg,
172 unsigned char *data,
173 unsigned int len)
174 {
175 struct regmap *map = tas_priv->regmap;
176 int ret;
177
178 ret = tasdevice_spi_switch_book(tas_priv, reg);
179 if (ret < 0)
180 return ret;
181
182 ret = regmap_bulk_write(map, TASDEVICE_PAGE_REG(reg), data, len);
183 if (ret < 0)
184 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
185
186 return ret;
187 }
188
tasdevice_spi_dev_bulk_read(struct tasdevice_priv * tas_priv,unsigned int reg,unsigned char * data,unsigned int len)189 int tasdevice_spi_dev_bulk_read(struct tasdevice_priv *tas_priv,
190 unsigned int reg,
191 unsigned char *data,
192 unsigned int len)
193 {
194 struct regmap *map = tas_priv->regmap;
195 int ret;
196
197 ret = tasdevice_spi_switch_book(tas_priv, reg);
198 if (ret < 0)
199 return ret;
200
201 if (len > TASDEVICE_MAX_PAGE)
202 len = TASDEVICE_MAX_PAGE;
203 /*
204 * In our TAS2781 SPI mode, if read from other book (not book 0),
205 * or read from page number larger than 1 in book 0, one more byte
206 * read is needed, and first byte is a dummy byte, need to be ignored.
207 */
208 if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) {
209 unsigned char buf[TASDEVICE_MAX_PAGE+1];
210
211 ret = regmap_bulk_read(map, TASDEVICE_PAGE_REG(reg) | 1, buf,
212 len + 1);
213 memcpy(data, buf + 1, len);
214 } else {
215 ret = regmap_bulk_read(map, TASDEVICE_PAGE_REG(reg) | 1, data,
216 len);
217 }
218 if (ret < 0)
219 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
220
221 return ret;
222 }
223
tasdevice_spi_dev_update_bits(struct tasdevice_priv * tas_priv,unsigned int reg,unsigned int mask,unsigned int value)224 int tasdevice_spi_dev_update_bits(struct tasdevice_priv *tas_priv,
225 unsigned int reg,
226 unsigned int mask,
227 unsigned int value)
228 {
229 struct regmap *map = tas_priv->regmap;
230 int ret, val;
231
232 /*
233 * In our TAS2781 SPI mode, read/write was masked in last bit of
234 * address, it cause regmap_update_bits() not work as expected.
235 */
236 ret = tasdevice_spi_dev_read(tas_priv, reg, &val);
237 if (ret < 0) {
238 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
239 return ret;
240 }
241 ret = regmap_write(map, TASDEVICE_PAGE_REG(reg),
242 (val & ~mask) | (mask & value));
243 if (ret < 0)
244 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
245
246 return ret;
247 }
248
tas2781_spi_reset(struct tasdevice_priv * tas_dev)249 static void tas2781_spi_reset(struct tasdevice_priv *tas_dev)
250 {
251 int ret;
252
253 if (tas_dev->reset) {
254 gpiod_set_value_cansleep(tas_dev->reset, 0);
255 fsleep(800);
256 gpiod_set_value_cansleep(tas_dev->reset, 1);
257 }
258 ret = tasdevice_spi_dev_write(tas_dev, TAS2781_REG_SWRESET,
259 TAS2781_REG_SWRESET_RESET);
260 if (ret < 0)
261 dev_err(tas_dev->dev, "dev sw-reset fail, %d\n", ret);
262 fsleep(1000);
263 }
264
tascodec_spi_init(struct tasdevice_priv * tas_priv,void * codec,struct module * module,void (* cont)(const struct firmware * fw,void * context))265 static int tascodec_spi_init(struct tasdevice_priv *tas_priv,
266 void *codec, struct module *module,
267 void (*cont)(const struct firmware *fw, void *context))
268 {
269 int ret;
270
271 /*
272 * Codec Lock Hold to ensure that codec_probe and firmware parsing and
273 * loading do not simultaneously execute.
274 */
275 guard(mutex)(&tas_priv->codec_lock);
276
277 scnprintf(tas_priv->rca_binaryname,
278 sizeof(tas_priv->rca_binaryname), "%sRCA%d.bin",
279 tas_priv->dev_name, tas_priv->index);
280 crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL);
281 tas_priv->codec = codec;
282 ret = request_firmware_nowait(module, FW_ACTION_UEVENT,
283 tas_priv->rca_binaryname, tas_priv->dev, GFP_KERNEL, tas_priv,
284 cont);
285 if (ret)
286 dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n",
287 ret);
288
289 return ret;
290 }
291
tasdevice_spi_init(struct tasdevice_priv * tas_priv)292 static void tasdevice_spi_init(struct tasdevice_priv *tas_priv)
293 {
294 tas_priv->cur_prog = -1;
295 tas_priv->cur_conf = -1;
296
297 tas_priv->cur_book = -1;
298 tas_priv->cur_prog = -1;
299 tas_priv->cur_conf = -1;
300
301 /* Store default registers address for calibration data. */
302 tas_priv->cali_reg_array[0] = TASDEVICE_REG(0, 0x17, 0x74);
303 tas_priv->cali_reg_array[1] = TASDEVICE_REG(0, 0x18, 0x0c);
304 tas_priv->cali_reg_array[2] = TASDEVICE_REG(0, 0x18, 0x14);
305 tas_priv->cali_reg_array[3] = TASDEVICE_REG(0, 0x13, 0x70);
306 tas_priv->cali_reg_array[4] = TASDEVICE_REG(0, 0x18, 0x7c);
307
308 mutex_init(&tas_priv->codec_lock);
309 }
310
tasdevice_spi_amp_putvol(struct tasdevice_priv * tas_priv,struct snd_ctl_elem_value * ucontrol,struct soc_mixer_control * mc)311 static int tasdevice_spi_amp_putvol(struct tasdevice_priv *tas_priv,
312 struct snd_ctl_elem_value *ucontrol,
313 struct soc_mixer_control *mc)
314 {
315 unsigned int invert = mc->invert;
316 unsigned char mask;
317 int max = mc->max;
318 int val, ret;
319
320 mask = rounddown_pow_of_two(max);
321 mask <<= mc->shift;
322 val = clamp(invert ? max - ucontrol->value.integer.value[0] :
323 ucontrol->value.integer.value[0], 0, max);
324 ret = tasdevice_spi_dev_update_bits(tas_priv,
325 mc->reg, mask, (unsigned int)(val << mc->shift));
326 if (ret)
327 dev_err(tas_priv->dev, "set AMP vol error in dev %d\n",
328 tas_priv->index);
329
330 return ret;
331 }
332
tasdevice_spi_amp_getvol(struct tasdevice_priv * tas_priv,struct snd_ctl_elem_value * ucontrol,struct soc_mixer_control * mc)333 static int tasdevice_spi_amp_getvol(struct tasdevice_priv *tas_priv,
334 struct snd_ctl_elem_value *ucontrol,
335 struct soc_mixer_control *mc)
336 {
337 unsigned int invert = mc->invert;
338 unsigned char mask = 0;
339 int max = mc->max;
340 int ret, val;
341
342 /* Read the primary device */
343 ret = tasdevice_spi_dev_read(tas_priv, mc->reg, &val);
344 if (ret) {
345 dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__);
346 return ret;
347 }
348
349 mask = rounddown_pow_of_two(max);
350 mask <<= mc->shift;
351 val = (val & mask) >> mc->shift;
352 val = clamp(invert ? max - val : val, 0, max);
353 ucontrol->value.integer.value[0] = val;
354
355 return ret;
356 }
357
tasdevice_spi_digital_putvol(struct tasdevice_priv * tas_priv,struct snd_ctl_elem_value * ucontrol,struct soc_mixer_control * mc)358 static int tasdevice_spi_digital_putvol(struct tasdevice_priv *tas_priv,
359 struct snd_ctl_elem_value *ucontrol,
360 struct soc_mixer_control *mc)
361 {
362 unsigned int invert = mc->invert;
363 int max = mc->max;
364 int val, ret;
365
366 val = clamp(invert ? max - ucontrol->value.integer.value[0] :
367 ucontrol->value.integer.value[0], 0, max);
368 ret = tasdevice_spi_dev_write(tas_priv, mc->reg, (unsigned int)val);
369 if (ret)
370 dev_err(tas_priv->dev, "set digital vol err in dev %d\n",
371 tas_priv->index);
372
373 return ret;
374 }
375
tasdevice_spi_digital_getvol(struct tasdevice_priv * tas_priv,struct snd_ctl_elem_value * ucontrol,struct soc_mixer_control * mc)376 static int tasdevice_spi_digital_getvol(struct tasdevice_priv *tas_priv,
377 struct snd_ctl_elem_value *ucontrol,
378 struct soc_mixer_control *mc)
379 {
380 unsigned int invert = mc->invert;
381 int max = mc->max;
382 int ret, val;
383
384 /* Read the primary device as the whole */
385 ret = tasdevice_spi_dev_read(tas_priv, mc->reg, &val);
386 if (ret) {
387 dev_err(tas_priv->dev, "%s, get digital vol err\n", __func__);
388 return ret;
389 }
390
391 val = clamp(invert ? max - val : val, 0, max);
392 ucontrol->value.integer.value[0] = val;
393
394 return ret;
395 }
396
tas2781_read_acpi(struct tas2781_hda * tas_hda,const char * hid,int id)397 static int tas2781_read_acpi(struct tas2781_hda *tas_hda,
398 const char *hid,
399 int id)
400 {
401 struct tasdevice_priv *p = tas_hda->priv;
402 struct acpi_device *adev;
403 struct device *physdev;
404 u32 values[HDA_MAX_COMPONENTS];
405 const char *property;
406 size_t nval;
407 int ret, i;
408
409 adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
410 if (!adev) {
411 dev_err(p->dev, "Failed to find ACPI device: %s\n", hid);
412 return -ENODEV;
413 }
414
415 strscpy(p->dev_name, hid, sizeof(p->dev_name));
416 tas_hda->dacpi = adev;
417 physdev = get_device(acpi_get_first_physical_node(adev));
418 acpi_dev_put(adev);
419
420 property = "ti,dev-index";
421 ret = device_property_count_u32(physdev, property);
422 if (ret <= 0 || ret > ARRAY_SIZE(values)) {
423 ret = -EINVAL;
424 goto err;
425 }
426 nval = ret;
427
428 ret = device_property_read_u32_array(physdev, property, values, nval);
429 if (ret)
430 goto err;
431
432 p->index = U8_MAX;
433 for (i = 0; i < nval; i++) {
434 if (values[i] == id) {
435 p->index = i;
436 break;
437 }
438 }
439 if (p->index == U8_MAX) {
440 dev_dbg(p->dev, "No index found in %s\n", property);
441 ret = -ENODEV;
442 goto err;
443 }
444
445 if (p->index == 0) {
446 /* All of amps share same RESET pin. */
447 p->reset = devm_gpiod_get_index_optional(physdev, "reset",
448 p->index, GPIOD_OUT_LOW);
449 if (IS_ERR(p->reset)) {
450 ret = PTR_ERR(p->reset);
451 dev_err_probe(p->dev, ret, "Failed on reset GPIO\n");
452 goto err;
453 }
454 }
455 put_device(physdev);
456
457 return 0;
458 err:
459 dev_err(p->dev, "read acpi error, ret: %d\n", ret);
460 put_device(physdev);
461 acpi_dev_put(adev);
462
463 return ret;
464 }
465
tas2781_hda_playback_hook(struct device * dev,int action)466 static void tas2781_hda_playback_hook(struct device *dev, int action)
467 {
468 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
469
470 if (action == HDA_GEN_PCM_ACT_OPEN) {
471 pm_runtime_get_sync(dev);
472 guard(mutex)(&tas_hda->priv->codec_lock);
473 tasdevice_spi_tuning_switch(tas_hda->priv, 0);
474 } else if (action == HDA_GEN_PCM_ACT_CLOSE) {
475 guard(mutex)(&tas_hda->priv->codec_lock);
476 tasdevice_spi_tuning_switch(tas_hda->priv, 1);
477 pm_runtime_mark_last_busy(dev);
478 pm_runtime_put_autosuspend(dev);
479 }
480 }
481
tasdevice_info_profile(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)482 static int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
483 struct snd_ctl_elem_info *uinfo)
484 {
485 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
486
487 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
488 uinfo->count = 1;
489 uinfo->value.integer.min = 0;
490 uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;
491
492 return 0;
493 }
494
tasdevice_get_profile_id(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)495 static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
496 struct snd_ctl_elem_value *ucontrol)
497 {
498 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
499
500 ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;
501
502 return 0;
503 }
504
tasdevice_set_profile_id(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)505 static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
506 struct snd_ctl_elem_value *ucontrol)
507 {
508 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
509 int max = tas_priv->rcabin.ncfgs - 1;
510 int val;
511
512 val = clamp(ucontrol->value.integer.value[0], 0, max);
513 if (tas_priv->rcabin.profile_cfg_id != val) {
514 tas_priv->rcabin.profile_cfg_id = val;
515 return 1;
516 }
517
518 return 0;
519 }
520
tasdevice_info_programs(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)521 static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
522 struct snd_ctl_elem_info *uinfo)
523 {
524 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
525
526 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
527 uinfo->count = 1;
528 uinfo->value.integer.min = 0;
529 uinfo->value.integer.max = tas_priv->fmw->nr_programs - 1;
530
531 return 0;
532 }
533
tasdevice_info_config(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)534 static int tasdevice_info_config(struct snd_kcontrol *kcontrol,
535 struct snd_ctl_elem_info *uinfo)
536 {
537 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
538
539 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
540 uinfo->count = 1;
541 uinfo->value.integer.min = 0;
542 uinfo->value.integer.max = tas_priv->fmw->nr_configurations - 1;
543
544 return 0;
545 }
546
tasdevice_program_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)547 static int tasdevice_program_get(struct snd_kcontrol *kcontrol,
548 struct snd_ctl_elem_value *ucontrol)
549 {
550 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
551
552 ucontrol->value.integer.value[0] = tas_priv->cur_prog;
553
554 return 0;
555 }
556
tasdevice_program_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)557 static int tasdevice_program_put(struct snd_kcontrol *kcontrol,
558 struct snd_ctl_elem_value *ucontrol)
559 {
560 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
561 int nr_program = ucontrol->value.integer.value[0];
562 int max = tas_priv->fmw->nr_programs - 1;
563 int val;
564
565 val = clamp(nr_program, 0, max);
566
567 if (tas_priv->cur_prog != val) {
568 tas_priv->cur_prog = val;
569 return 1;
570 }
571
572 return 0;
573 }
574
tasdevice_config_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)575 static int tasdevice_config_get(struct snd_kcontrol *kcontrol,
576 struct snd_ctl_elem_value *ucontrol)
577 {
578 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
579
580 ucontrol->value.integer.value[0] = tas_priv->cur_conf;
581
582 return 0;
583 }
584
tasdevice_config_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)585 static int tasdevice_config_put(struct snd_kcontrol *kcontrol,
586 struct snd_ctl_elem_value *ucontrol)
587 {
588 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
589 int max = tas_priv->fmw->nr_configurations - 1;
590 int val;
591
592 val = clamp(ucontrol->value.integer.value[0], 0, max);
593
594 if (tas_priv->cur_conf != val) {
595 tas_priv->cur_conf = val;
596 return 1;
597 }
598
599 return 0;
600 }
601
602 /*
603 * tas2781_digital_getvol - get the volum control
604 * @kcontrol: control pointer
605 * @ucontrol: User data
606 *
607 * Customer Kcontrol for tas2781 is primarily for regmap booking, paging
608 * depends on internal regmap mechanism.
609 * tas2781 contains book and page two-level register map, especially
610 * book switching will set the register BXXP00R7F, after switching to the
611 * correct book, then leverage the mechanism for paging to access the
612 * register.
613 *
614 * Return 0 if succeeded.
615 */
tas2781_digital_getvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)616 static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,
617 struct snd_ctl_elem_value *ucontrol)
618 {
619 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
620 struct soc_mixer_control *mc =
621 (struct soc_mixer_control *)kcontrol->private_value;
622
623 return tasdevice_spi_digital_getvol(tas_priv, ucontrol, mc);
624 }
625
tas2781_amp_getvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)626 static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
627 struct snd_ctl_elem_value *ucontrol)
628 {
629 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
630 struct soc_mixer_control *mc =
631 (struct soc_mixer_control *)kcontrol->private_value;
632
633 return tasdevice_spi_amp_getvol(tas_priv, ucontrol, mc);
634 }
635
tas2781_digital_putvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)636 static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,
637 struct snd_ctl_elem_value *ucontrol)
638 {
639 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
640 struct soc_mixer_control *mc =
641 (struct soc_mixer_control *)kcontrol->private_value;
642
643 /* The check of the given value is in tasdevice_digital_putvol. */
644 return tasdevice_spi_digital_putvol(tas_priv, ucontrol, mc);
645 }
646
tas2781_amp_putvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)647 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
648 struct snd_ctl_elem_value *ucontrol)
649 {
650 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
651 struct soc_mixer_control *mc =
652 (struct soc_mixer_control *)kcontrol->private_value;
653
654 /* The check of the given value is in tasdevice_amp_putvol. */
655 return tasdevice_spi_amp_putvol(tas_priv, ucontrol, mc);
656 }
657
tas2781_force_fwload_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)658 static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,
659 struct snd_ctl_elem_value *ucontrol)
660 {
661 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
662
663 ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
664 dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
665 str_on_off(tas_priv->force_fwload_status));
666
667 return 0;
668 }
669
tas2781_force_fwload_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)670 static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
671 struct snd_ctl_elem_value *ucontrol)
672 {
673 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
674 bool change, val = (bool)ucontrol->value.integer.value[0];
675
676 if (tas_priv->force_fwload_status == val) {
677 change = false;
678 } else {
679 change = true;
680 tas_priv->force_fwload_status = val;
681 }
682 dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
683 str_on_off(tas_priv->force_fwload_status));
684
685 return change;
686 }
687
688 static const struct snd_kcontrol_new tas2781_snd_controls[] = {
689 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain 0", TAS2781_AMP_LEVEL,
690 1, 0, 20, 0, tas2781_amp_getvol,
691 tas2781_amp_putvol, amp_vol_tlv),
692 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Digital Gain 0", TAS2781_DVC_LVL,
693 0, 0, 200, 1, tas2781_digital_getvol,
694 tas2781_digital_putvol, dvc_tlv),
695 ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load 0", 0,
696 tas2781_force_fwload_get, tas2781_force_fwload_put),
697 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain 1", TAS2781_AMP_LEVEL,
698 1, 0, 20, 0, tas2781_amp_getvol,
699 tas2781_amp_putvol, amp_vol_tlv),
700 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Digital Gain 1", TAS2781_DVC_LVL,
701 0, 0, 200, 1, tas2781_digital_getvol,
702 tas2781_digital_putvol, dvc_tlv),
703 ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load 1", 0,
704 tas2781_force_fwload_get, tas2781_force_fwload_put),
705 };
706
707 static const struct snd_kcontrol_new tas2781_prof_ctrl[] = {
708 {
709 .name = "Speaker Profile Id - 0",
710 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
711 .info = tasdevice_info_profile,
712 .get = tasdevice_get_profile_id,
713 .put = tasdevice_set_profile_id,
714 },
715 {
716 .name = "Speaker Profile Id - 1",
717 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
718 .info = tasdevice_info_profile,
719 .get = tasdevice_get_profile_id,
720 .put = tasdevice_set_profile_id,
721 },
722 };
723 static const struct snd_kcontrol_new tas2781_dsp_prog_ctrl[] = {
724 {
725 .name = "Speaker Program Id 0",
726 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
727 .info = tasdevice_info_programs,
728 .get = tasdevice_program_get,
729 .put = tasdevice_program_put,
730 },
731 {
732 .name = "Speaker Program Id 1",
733 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
734 .info = tasdevice_info_programs,
735 .get = tasdevice_program_get,
736 .put = tasdevice_program_put,
737 },
738 };
739
740 static const struct snd_kcontrol_new tas2781_dsp_conf_ctrl[] = {
741 {
742 .name = "Speaker Config Id 0",
743 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
744 .info = tasdevice_info_config,
745 .get = tasdevice_config_get,
746 .put = tasdevice_config_put,
747 },
748 {
749 .name = "Speaker Config Id 1",
750 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
751 .info = tasdevice_info_config,
752 .get = tasdevice_config_get,
753 .put = tasdevice_config_put,
754 },
755 };
756
tas2781_apply_calib(struct tasdevice_priv * tas_priv)757 static void tas2781_apply_calib(struct tasdevice_priv *tas_priv)
758 {
759 int i, rc;
760
761 /*
762 * If no calibration data exist in tasdevice_priv *tas_priv,
763 * calibration apply will be ignored, and use default values
764 * in firmware binary, which was loaded during firmware download.
765 */
766 if (tas_priv->cali_data[0] == 0)
767 return;
768 /*
769 * Calibration data was saved in tasdevice_priv *tas_priv as:
770 * unsigned int cali_data[CALIB_MAX];
771 * and every data (in 4 bytes) will be saved in register which in
772 * book 0, and page number in page_array[], offset was saved in
773 * rgno_array[].
774 */
775 for (i = 0; i < CALIB_MAX; i++) {
776 rc = tasdevice_spi_dev_bulk_write(tas_priv,
777 tas_priv->cali_reg_array[i],
778 (unsigned char *)&tas_priv->cali_data[i], 4);
779 if (rc < 0)
780 dev_err(tas_priv->dev,
781 "chn %d calib %d bulk_wr err = %d\n",
782 tas_priv->index, i, rc);
783 }
784 }
785
786 /*
787 * Update the calibration data, including speaker impedance, f0, etc,
788 * into algo. Calibrate data is done by manufacturer in the factory.
789 * These data are used by Algo for calculating the speaker temperature,
790 * speaker membrane excursion and f0 in real time during playback.
791 * Calibration data format in EFI is V2, since 2024.
792 */
tas2781_save_calibration(struct tasdevice_priv * tas_priv)793 static int tas2781_save_calibration(struct tasdevice_priv *tas_priv)
794 {
795 /*
796 * GUID was used for data access in BIOS, it was provided by board
797 * manufactory, like HP: "{02f9af02-7734-4233-b43d-93fe5aa35db3}"
798 */
799 efi_guid_t efi_guid =
800 EFI_GUID(0x02f9af02, 0x7734, 0x4233,
801 0xb4, 0x3d, 0x93, 0xfe, 0x5a, 0xa3, 0x5d, 0xb3);
802 static efi_char16_t efi_name[] = TASDEVICE_CALIBRATION_DATA_NAME;
803 unsigned char data[TASDEVICE_CALIBRATION_DATA_SIZE], *buf;
804 unsigned int attr, crc, offset, *tmp_val;
805 unsigned long total_sz = 0;
806 efi_status_t status;
807
808 tas_priv->cali_data[0] = 0;
809 status = efi.get_variable(efi_name, &efi_guid, &attr, &total_sz, data);
810 if (status == EFI_BUFFER_TOO_SMALL) {
811 if (total_sz > TASDEVICE_CALIBRATION_DATA_SIZE)
812 return -ENOMEM;
813 /* Get variable contents into buffer */
814 status = efi.get_variable(efi_name, &efi_guid, &attr,
815 &total_sz, data);
816 }
817 if (status != EFI_SUCCESS)
818 return status;
819
820 tmp_val = (unsigned int *)data;
821 if (tmp_val[0] == 2781) {
822 /*
823 * New features were added in calibrated Data V3:
824 * 1. Added calibration registers address define in
825 * a node, marked as Device id == 0x80.
826 * New features were added in calibrated Data V2:
827 * 1. Added some the fields to store the link_id and
828 * uniqie_id for multi-link solutions
829 * 2. Support flexible number of devices instead of
830 * fixed one in V1.
831 * Layout of calibrated data V2 in UEFI(total 256 bytes):
832 * ChipID (2781, 4 bytes)
833 * Device-Sum (4 bytes)
834 * TimeStamp of Calibration (4 bytes)
835 * for (i = 0; i < Device-Sum; i++) {
836 * Device #i index_info () {
837 * SDW link id (2bytes)
838 * SDW unique_id (2bytes)
839 * } // if Device number is 0x80, mean it's
840 * calibration registers address.
841 * Calibrated Data of Device #i (20 bytes)
842 * }
843 * CRC (4 bytes)
844 * Reserved (the rest)
845 */
846 crc = crc32(~0, data, (3 + tmp_val[1] * 6) * 4) ^ ~0;
847
848 if (crc != tmp_val[3 + tmp_val[1] * 6])
849 return 0;
850
851 for (int j = 0; j < tmp_val[1]; j++) {
852 offset = j * 6 + 3;
853 if (tmp_val[offset] == tas_priv->index) {
854 for (int i = 0; i < CALIB_MAX; i++)
855 tas_priv->cali_data[i] =
856 tmp_val[offset + i + 1];
857 } else if (tmp_val[offset] ==
858 TASDEVICE_CALIBRATION_REG_ADDRESS) {
859 for (int i = 0; i < CALIB_MAX; i++) {
860 buf = &data[(offset + i + 1) * 4];
861 tas_priv->cali_reg_array[i] =
862 TASDEVICE_REG(buf[1], buf[2],
863 buf[3]);
864 }
865 }
866 tas_priv->apply_calibration(tas_priv);
867 }
868 } else {
869 /*
870 * Calibration data is in V1 format.
871 * struct cali_data {
872 * char cali_data[20];
873 * }
874 *
875 * struct {
876 * struct cali_data cali_data[4];
877 * int TimeStamp of Calibration (4 bytes)
878 * int CRC (4 bytes)
879 * } ueft;
880 */
881 crc = crc32(~0, data, 84) ^ ~0;
882 if (crc == tmp_val[21]) {
883 for (int i = 0; i < CALIB_MAX; i++)
884 tas_priv->cali_data[i] =
885 tmp_val[tas_priv->index * 5 + i];
886 tas_priv->apply_calibration(tas_priv);
887 }
888 }
889
890 return 0;
891 }
892
tas2781_hda_remove_controls(struct tas2781_hda * tas_hda)893 static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
894 {
895 struct hda_codec *codec = tas_hda->priv->codec;
896
897 snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
898
899 snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
900
901 for (int i = ARRAY_SIZE(tas_hda->snd_ctls) - 1; i >= 0; i--)
902 snd_ctl_remove(codec->card, tas_hda->snd_ctls[i]);
903
904 snd_ctl_remove(codec->card, tas_hda->prof_ctl);
905 }
906
tasdev_fw_ready(const struct firmware * fmw,void * context)907 static void tasdev_fw_ready(const struct firmware *fmw, void *context)
908 {
909 struct tasdevice_priv *tas_priv = context;
910 struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
911 struct hda_codec *codec = tas_priv->codec;
912 int i, j, ret, val;
913
914 pm_runtime_get_sync(tas_priv->dev);
915 guard(mutex)(&tas_priv->codec_lock);
916
917 ret = tasdevice_spi_rca_parser(tas_priv, fmw);
918 if (ret)
919 goto out;
920
921 /* Add control one time only. */
922 tas_hda->prof_ctl = snd_ctl_new1(&tas2781_prof_ctrl[tas_priv->index],
923 tas_priv);
924 ret = snd_ctl_add(codec->card, tas_hda->prof_ctl);
925 if (ret) {
926 dev_err(tas_priv->dev, "Failed to add KControl %s = %d\n",
927 tas2781_prof_ctrl[tas_priv->index].name, ret);
928 goto out;
929 }
930 j = tas_priv->index * ARRAY_SIZE(tas2781_snd_controls) / 2;
931 for (i = 0; i < 3; i++) {
932 tas_hda->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_controls[i+j],
933 tas_priv);
934 ret = snd_ctl_add(codec->card, tas_hda->snd_ctls[i]);
935 if (ret) {
936 dev_err(tas_priv->dev,
937 "Failed to add KControl %s = %d\n",
938 tas2781_snd_controls[i+tas_priv->index*3].name,
939 ret);
940 goto out;
941 }
942 }
943
944 tasdevice_spi_dsp_remove(tas_priv);
945
946 tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
947 scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%08X-%01d.bin",
948 codec->core.subsystem_id, tas_priv->index);
949 ret = tasdevice_spi_dsp_parser(tas_priv);
950 if (ret) {
951 dev_err(tas_priv->dev, "dspfw load %s error\n",
952 tas_priv->coef_binaryname);
953 tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
954 goto out;
955 }
956
957 /* Add control one time only. */
958 tas_hda->dsp_prog_ctl =
959 snd_ctl_new1(&tas2781_dsp_prog_ctrl[tas_priv->index],
960 tas_priv);
961 ret = snd_ctl_add(codec->card, tas_hda->dsp_prog_ctl);
962 if (ret) {
963 dev_err(tas_priv->dev,
964 "Failed to add KControl %s = %d\n",
965 tas2781_dsp_prog_ctrl[tas_priv->index].name, ret);
966 goto out;
967 }
968
969 tas_hda->dsp_conf_ctl =
970 snd_ctl_new1(&tas2781_dsp_conf_ctrl[tas_priv->index],
971 tas_priv);
972 ret = snd_ctl_add(codec->card, tas_hda->dsp_conf_ctl);
973 if (ret) {
974 dev_err(tas_priv->dev, "Failed to add KControl %s = %d\n",
975 tas2781_dsp_conf_ctrl[tas_priv->index].name, ret);
976 goto out;
977 }
978
979 /* Perform AMP reset before firmware download. */
980 tas_priv->rcabin.profile_cfg_id = TAS2781_PRE_POST_RESET_CFG;
981 tas2781_spi_reset(tas_priv);
982 tas_priv->rcabin.profile_cfg_id = 0;
983
984 tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
985 ret = tasdevice_spi_dev_read(tas_priv, TAS2781_REG_CLK_CONFIG, &val);
986 if (ret < 0)
987 goto out;
988
989 if (val == TAS2781_REG_CLK_CONFIG_RESET)
990 ret = tasdevice_spi_prmg_load(tas_priv, 0);
991 if (ret < 0) {
992 dev_err(tas_priv->dev, "FW download failed = %d\n", ret);
993 goto out;
994 }
995 if (tas_priv->fmw->nr_programs > 0)
996 tas_priv->cur_prog = 0;
997 if (tas_priv->fmw->nr_configurations > 0)
998 tas_priv->cur_conf = 0;
999
1000 /*
1001 * If calibrated data occurs error, dsp will still works with default
1002 * calibrated data inside algo.
1003 */
1004
1005 out:
1006 release_firmware(fmw);
1007 pm_runtime_mark_last_busy(tas_hda->priv->dev);
1008 pm_runtime_put_autosuspend(tas_hda->priv->dev);
1009 }
1010
tas2781_hda_bind(struct device * dev,struct device * master,void * master_data)1011 static int tas2781_hda_bind(struct device *dev, struct device *master,
1012 void *master_data)
1013 {
1014 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
1015 struct hda_component_parent *parent = master_data;
1016 struct hda_component *comp;
1017 struct hda_codec *codec;
1018 int ret;
1019
1020 comp = hda_component_from_index(parent, tas_hda->priv->index);
1021 if (!comp)
1022 return -EINVAL;
1023
1024 if (comp->dev)
1025 return -EBUSY;
1026
1027 codec = parent->codec;
1028
1029 pm_runtime_get_sync(dev);
1030
1031 comp->dev = dev;
1032
1033 strscpy(comp->name, dev_name(dev), sizeof(comp->name));
1034
1035 ret = tascodec_spi_init(tas_hda->priv, codec, THIS_MODULE,
1036 tasdev_fw_ready);
1037 if (!ret)
1038 comp->playback_hook = tas2781_hda_playback_hook;
1039
1040 pm_runtime_mark_last_busy(dev);
1041 pm_runtime_put_autosuspend(dev);
1042
1043 return ret;
1044 }
1045
tas2781_hda_unbind(struct device * dev,struct device * master,void * master_data)1046 static void tas2781_hda_unbind(struct device *dev, struct device *master,
1047 void *master_data)
1048 {
1049 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
1050 struct hda_component_parent *parent = master_data;
1051 struct hda_component *comp;
1052
1053 comp = hda_component_from_index(parent, tas_hda->priv->index);
1054 if (comp && (comp->dev == dev)) {
1055 comp->dev = NULL;
1056 memset(comp->name, 0, sizeof(comp->name));
1057 comp->playback_hook = NULL;
1058 }
1059
1060 tas2781_hda_remove_controls(tas_hda);
1061
1062 tasdevice_spi_config_info_remove(tas_hda->priv);
1063 tasdevice_spi_dsp_remove(tas_hda->priv);
1064
1065 tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
1066 }
1067
1068 static const struct component_ops tas2781_hda_comp_ops = {
1069 .bind = tas2781_hda_bind,
1070 .unbind = tas2781_hda_unbind,
1071 };
1072
tas2781_hda_remove(struct device * dev)1073 static void tas2781_hda_remove(struct device *dev)
1074 {
1075 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
1076
1077 component_del(tas_hda->priv->dev, &tas2781_hda_comp_ops);
1078
1079 pm_runtime_get_sync(tas_hda->priv->dev);
1080 pm_runtime_disable(tas_hda->priv->dev);
1081
1082 pm_runtime_put_noidle(tas_hda->priv->dev);
1083
1084 mutex_destroy(&tas_hda->priv->codec_lock);
1085 }
1086
tas2781_hda_spi_probe(struct spi_device * spi)1087 static int tas2781_hda_spi_probe(struct spi_device *spi)
1088 {
1089 struct tasdevice_priv *tas_priv;
1090 struct tas2781_hda *tas_hda;
1091 const char *device_name;
1092 int ret = 0;
1093
1094 tas_hda = devm_kzalloc(&spi->dev, sizeof(*tas_hda), GFP_KERNEL);
1095 if (!tas_hda)
1096 return -ENOMEM;
1097
1098 spi->max_speed_hz = TAS2781_SPI_MAX_FREQ;
1099
1100 tas_priv = devm_kzalloc(&spi->dev, sizeof(*tas_priv), GFP_KERNEL);
1101 if (!tas_priv)
1102 return -ENOMEM;
1103 tas_priv->dev = &spi->dev;
1104 tas_hda->priv = tas_priv;
1105 tas_priv->regmap = devm_regmap_init_spi(spi, &tasdevice_regmap);
1106 if (IS_ERR(tas_priv->regmap)) {
1107 ret = PTR_ERR(tas_priv->regmap);
1108 dev_err(tas_priv->dev, "Failed to allocate regmap: %d\n",
1109 ret);
1110 return ret;
1111 }
1112 if (strstr(dev_name(&spi->dev), "TXNW2781")) {
1113 device_name = "TXNW2781";
1114 tas_priv->save_calibration = tas2781_save_calibration;
1115 tas_priv->apply_calibration = tas2781_apply_calib;
1116 } else {
1117 dev_err(tas_priv->dev, "Unmatched spi dev %s\n",
1118 dev_name(&spi->dev));
1119 return -ENODEV;
1120 }
1121
1122 tas_priv->irq = spi->irq;
1123 dev_set_drvdata(&spi->dev, tas_hda);
1124 ret = tas2781_read_acpi(tas_hda, device_name,
1125 spi_get_chipselect(spi, 0));
1126 if (ret)
1127 return dev_err_probe(tas_priv->dev, ret,
1128 "Platform not supported\n");
1129
1130 tasdevice_spi_init(tas_priv);
1131
1132 ret = component_add(tas_priv->dev, &tas2781_hda_comp_ops);
1133 if (ret) {
1134 dev_err(tas_priv->dev, "Register component fail: %d\n", ret);
1135 return ret;
1136 }
1137
1138 pm_runtime_set_autosuspend_delay(tas_priv->dev, 3000);
1139 pm_runtime_use_autosuspend(tas_priv->dev);
1140 pm_runtime_mark_last_busy(tas_priv->dev);
1141 pm_runtime_set_active(tas_priv->dev);
1142 pm_runtime_get_noresume(tas_priv->dev);
1143 pm_runtime_enable(tas_priv->dev);
1144
1145 pm_runtime_put_autosuspend(tas_priv->dev);
1146
1147 return 0;
1148 }
1149
tas2781_hda_spi_remove(struct spi_device * spi)1150 static void tas2781_hda_spi_remove(struct spi_device *spi)
1151 {
1152 tas2781_hda_remove(&spi->dev);
1153 }
1154
tas2781_runtime_suspend(struct device * dev)1155 static int tas2781_runtime_suspend(struct device *dev)
1156 {
1157 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
1158
1159 guard(mutex)(&tas_hda->priv->codec_lock);
1160
1161 if (tas_hda->priv->playback_started)
1162 tasdevice_spi_tuning_switch(tas_hda->priv, 1);
1163
1164 tas_hda->priv->cur_book = -1;
1165 tas_hda->priv->cur_conf = -1;
1166
1167 return 0;
1168 }
1169
tas2781_runtime_resume(struct device * dev)1170 static int tas2781_runtime_resume(struct device *dev)
1171 {
1172 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
1173
1174 guard(mutex)(&tas_hda->priv->codec_lock);
1175
1176 if (tas_hda->priv->playback_started)
1177 tasdevice_spi_tuning_switch(tas_hda->priv, 0);
1178
1179 return 0;
1180 }
1181
tas2781_system_suspend(struct device * dev)1182 static int tas2781_system_suspend(struct device *dev)
1183 {
1184 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
1185 int ret;
1186
1187 ret = pm_runtime_force_suspend(dev);
1188 if (ret)
1189 return ret;
1190
1191 /* Shutdown chip before system suspend */
1192 if (tas_hda->priv->playback_started)
1193 tasdevice_spi_tuning_switch(tas_hda->priv, 1);
1194
1195 return 0;
1196 }
1197
tas2781_system_resume(struct device * dev)1198 static int tas2781_system_resume(struct device *dev)
1199 {
1200 struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
1201 int ret, val;
1202
1203 ret = pm_runtime_force_resume(dev);
1204 if (ret)
1205 return ret;
1206
1207 guard(mutex)(&tas_hda->priv->codec_lock);
1208 ret = tasdevice_spi_dev_read(tas_hda->priv, TAS2781_REG_CLK_CONFIG,
1209 &val);
1210 if (ret < 0)
1211 return ret;
1212
1213 if (val == TAS2781_REG_CLK_CONFIG_RESET) {
1214 tas_hda->priv->cur_book = -1;
1215 tas_hda->priv->cur_conf = -1;
1216 tas_hda->priv->cur_prog = -1;
1217
1218 ret = tasdevice_spi_prmg_load(tas_hda->priv, 0);
1219 if (ret < 0) {
1220 dev_err(tas_hda->priv->dev,
1221 "FW download failed = %d\n", ret);
1222 return ret;
1223 }
1224
1225 if (tas_hda->priv->playback_started)
1226 tasdevice_spi_tuning_switch(tas_hda->priv, 0);
1227 }
1228
1229 return ret;
1230 }
1231
1232 static const struct dev_pm_ops tas2781_hda_pm_ops = {
1233 RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
1234 SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
1235 };
1236
1237 static const struct spi_device_id tas2781_hda_spi_id[] = {
1238 { "tas2781-hda", },
1239 {}
1240 };
1241
1242 static const struct acpi_device_id tas2781_acpi_hda_match[] = {
1243 {"TXNW2781", },
1244 {}
1245 };
1246 MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
1247
1248 static struct spi_driver tas2781_hda_spi_driver = {
1249 .driver = {
1250 .name = "tas2781-hda",
1251 .acpi_match_table = tas2781_acpi_hda_match,
1252 .pm = &tas2781_hda_pm_ops,
1253 },
1254 .id_table = tas2781_hda_spi_id,
1255 .probe = tas2781_hda_spi_probe,
1256 .remove = tas2781_hda_spi_remove,
1257 };
1258 module_spi_driver(tas2781_hda_spi_driver);
1259
1260 MODULE_DESCRIPTION("TAS2781 HDA SPI Driver");
1261 MODULE_AUTHOR("Baojun, Xu, <baojun.xug@ti.com>");
1262 MODULE_LICENSE("GPL");
1263