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 <[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-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_range, \
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 	struct tm *tm = &tas_priv->tm;
806 	unsigned long total_sz = 0;
807 	efi_status_t status;
808 
809 	tas_priv->cali_data[0] = 0;
810 	status = efi.get_variable(efi_name, &efi_guid, &attr, &total_sz, data);
811 	if (status == EFI_BUFFER_TOO_SMALL) {
812 		if (total_sz > TASDEVICE_CALIBRATION_DATA_SIZE)
813 			return -ENOMEM;
814 		/* Get variable contents into buffer */
815 		status = efi.get_variable(efi_name, &efi_guid, &attr,
816 			&total_sz, data);
817 	}
818 	if (status != EFI_SUCCESS)
819 		return status;
820 
821 	tmp_val = (unsigned int *)data;
822 	if (tmp_val[0] == 2781) {
823 		/*
824 		 * New features were added in calibrated Data V3:
825 		 *     1. Added calibration registers address define in
826 		 *     a node, marked as Device id == 0x80.
827 		 * New features were added in calibrated Data V2:
828 		 *     1. Added some the fields to store the link_id and
829 		 *     uniqie_id for multi-link solutions
830 		 *     2. Support flexible number of devices instead of
831 		 *     fixed one in V1.
832 		 * Layout of calibrated data V2 in UEFI(total 256 bytes):
833 		 *     ChipID (2781, 4 bytes)
834 		 *     Device-Sum (4 bytes)
835 		 *     TimeStamp of Calibration (4 bytes)
836 		 *     for (i = 0; i < Device-Sum; i++) {
837 		 *             Device #i index_info () {
838 		 *                     SDW link id (2bytes)
839 		 *                     SDW unique_id (2bytes)
840 		 *             } // if Device number is 0x80, mean it's
841 		 *                  calibration registers address.
842 		 *             Calibrated Data of Device #i (20 bytes)
843 		 *     }
844 		 *     CRC (4 bytes)
845 		 *     Reserved (the rest)
846 		 */
847 		crc = crc32(~0, data, (3 + tmp_val[1] * 6) * 4) ^ ~0;
848 
849 		if (crc != tmp_val[3 + tmp_val[1] * 6])
850 			return 0;
851 
852 		time64_to_tm(tmp_val[2], 0, tm);
853 		for (int j = 0; j < tmp_val[1]; j++) {
854 			offset = j * 6 + 3;
855 			if (tmp_val[offset] == tas_priv->index) {
856 				for (int i = 0; i < CALIB_MAX; i++)
857 					tas_priv->cali_data[i] =
858 					tmp_val[offset + i + 1];
859 			} else if (tmp_val[offset] ==
860 				   TASDEVICE_CALIBRATION_REG_ADDRESS) {
861 				for (int i = 0; i < CALIB_MAX; i++) {
862 					buf = &data[(offset + i + 1) * 4];
863 					tas_priv->cali_reg_array[i] =
864 						TASDEVICE_REG(buf[1], buf[2],
865 						      buf[3]);
866 				}
867 			}
868 			tas_priv->apply_calibration(tas_priv);
869 		}
870 	} else {
871 		/*
872 		 * Calibration data is in V1 format.
873 		 * struct cali_data {
874 		 *      char cali_data[20];
875 		 * }
876 		 *
877 		 * struct {
878 		 *      struct cali_data cali_data[4];
879 		 *      int  TimeStamp of Calibration (4 bytes)
880 		 *      int CRC (4 bytes)
881 		 * } ueft;
882 		 */
883 		crc = crc32(~0, data, 84) ^ ~0;
884 		if (crc == tmp_val[21]) {
885 			time64_to_tm(tmp_val[20], 0, tm);
886 			for (int i = 0; i < CALIB_MAX; i++)
887 				tas_priv->cali_data[i] =
888 					tmp_val[tas_priv->index * 5 + i];
889 			tas_priv->apply_calibration(tas_priv);
890 		}
891 	}
892 
893 	return 0;
894 }
895 
tas2781_hda_remove_controls(struct tas2781_hda * tas_hda)896 static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
897 {
898 	struct hda_codec *codec = tas_hda->priv->codec;
899 
900 	snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
901 
902 	snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
903 
904 	for (int i = ARRAY_SIZE(tas_hda->snd_ctls) - 1; i >= 0; i--)
905 		snd_ctl_remove(codec->card, tas_hda->snd_ctls[i]);
906 
907 	snd_ctl_remove(codec->card, tas_hda->prof_ctl);
908 }
909 
tasdev_fw_ready(const struct firmware * fmw,void * context)910 static void tasdev_fw_ready(const struct firmware *fmw, void *context)
911 {
912 	struct tasdevice_priv *tas_priv = context;
913 	struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
914 	struct hda_codec *codec = tas_priv->codec;
915 	int i, j, ret, val;
916 
917 	pm_runtime_get_sync(tas_priv->dev);
918 	guard(mutex)(&tas_priv->codec_lock);
919 
920 	ret = tasdevice_spi_rca_parser(tas_priv, fmw);
921 	if (ret)
922 		goto out;
923 
924 	/* Add control one time only. */
925 	tas_hda->prof_ctl = snd_ctl_new1(&tas2781_prof_ctrl[tas_priv->index],
926 		tas_priv);
927 	ret = snd_ctl_add(codec->card, tas_hda->prof_ctl);
928 	if (ret) {
929 		dev_err(tas_priv->dev, "Failed to add KControl %s = %d\n",
930 			tas2781_prof_ctrl[tas_priv->index].name, ret);
931 		goto out;
932 	}
933 	j = tas_priv->index * ARRAY_SIZE(tas2781_snd_controls) / 2;
934 	for (i = 0; i < 3; i++) {
935 		tas_hda->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_controls[i+j],
936 			tas_priv);
937 		ret = snd_ctl_add(codec->card, tas_hda->snd_ctls[i]);
938 		if (ret) {
939 			dev_err(tas_priv->dev,
940 				"Failed to add KControl %s = %d\n",
941 				tas2781_snd_controls[i+tas_priv->index*3].name,
942 				ret);
943 			goto out;
944 		}
945 	}
946 
947 	tasdevice_spi_dsp_remove(tas_priv);
948 
949 	tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
950 	scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%08X-%01d.bin",
951 		codec->core.subsystem_id, tas_priv->index);
952 	ret = tasdevice_spi_dsp_parser(tas_priv);
953 	if (ret) {
954 		dev_err(tas_priv->dev, "dspfw load %s error\n",
955 			tas_priv->coef_binaryname);
956 		tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
957 		goto out;
958 	}
959 
960 	/* Add control one time only. */
961 	tas_hda->dsp_prog_ctl =
962 		snd_ctl_new1(&tas2781_dsp_prog_ctrl[tas_priv->index],
963 			     tas_priv);
964 	ret = snd_ctl_add(codec->card, tas_hda->dsp_prog_ctl);
965 	if (ret) {
966 		dev_err(tas_priv->dev,
967 			"Failed to add KControl %s = %d\n",
968 			tas2781_dsp_prog_ctrl[tas_priv->index].name, ret);
969 		goto out;
970 	}
971 
972 	tas_hda->dsp_conf_ctl =
973 		snd_ctl_new1(&tas2781_dsp_conf_ctrl[tas_priv->index],
974 			     tas_priv);
975 	ret = snd_ctl_add(codec->card, tas_hda->dsp_conf_ctl);
976 	if (ret) {
977 		dev_err(tas_priv->dev, "Failed to add KControl %s = %d\n",
978 			tas2781_dsp_conf_ctrl[tas_priv->index].name, ret);
979 		goto out;
980 	}
981 
982 	/* Perform AMP reset before firmware download. */
983 	tas_priv->rcabin.profile_cfg_id = TAS2781_PRE_POST_RESET_CFG;
984 	tas2781_spi_reset(tas_priv);
985 	tas_priv->rcabin.profile_cfg_id = 0;
986 
987 	tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
988 	ret = tasdevice_spi_dev_read(tas_priv, TAS2781_REG_CLK_CONFIG, &val);
989 	if (ret < 0)
990 		goto out;
991 
992 	if (val == TAS2781_REG_CLK_CONFIG_RESET)
993 		ret = tasdevice_spi_prmg_load(tas_priv, 0);
994 	if (ret < 0) {
995 		dev_err(tas_priv->dev, "FW download failed = %d\n", ret);
996 		goto out;
997 	}
998 	if (tas_priv->fmw->nr_programs > 0)
999 		tas_priv->cur_prog = 0;
1000 	if (tas_priv->fmw->nr_configurations > 0)
1001 		tas_priv->cur_conf = 0;
1002 
1003 	/*
1004 	 * If calibrated data occurs error, dsp will still works with default
1005 	 * calibrated data inside algo.
1006 	 */
1007 
1008 out:
1009 	if (fmw)
1010 		release_firmware(fmw);
1011 	pm_runtime_mark_last_busy(tas_hda->priv->dev);
1012 	pm_runtime_put_autosuspend(tas_hda->priv->dev);
1013 }
1014 
tas2781_hda_bind(struct device * dev,struct device * master,void * master_data)1015 static int tas2781_hda_bind(struct device *dev, struct device *master,
1016 	void *master_data)
1017 {
1018 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
1019 	struct hda_component_parent *parent = master_data;
1020 	struct hda_component *comp;
1021 	struct hda_codec *codec;
1022 	int ret;
1023 
1024 	comp = hda_component_from_index(parent, tas_hda->priv->index);
1025 	if (!comp)
1026 		return -EINVAL;
1027 
1028 	if (comp->dev)
1029 		return -EBUSY;
1030 
1031 	codec = parent->codec;
1032 
1033 	pm_runtime_get_sync(dev);
1034 
1035 	comp->dev = dev;
1036 
1037 	strscpy(comp->name, dev_name(dev), sizeof(comp->name));
1038 
1039 	ret = tascodec_spi_init(tas_hda->priv, codec, THIS_MODULE,
1040 		tasdev_fw_ready);
1041 	if (!ret)
1042 		comp->playback_hook = tas2781_hda_playback_hook;
1043 
1044 	pm_runtime_mark_last_busy(dev);
1045 	pm_runtime_put_autosuspend(dev);
1046 
1047 	return ret;
1048 }
1049 
tas2781_hda_unbind(struct device * dev,struct device * master,void * master_data)1050 static void tas2781_hda_unbind(struct device *dev, struct device *master,
1051 			       void *master_data)
1052 {
1053 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
1054 	struct hda_component_parent *parent = master_data;
1055 	struct hda_component *comp;
1056 
1057 	comp = hda_component_from_index(parent, tas_hda->priv->index);
1058 	if (comp && (comp->dev == dev)) {
1059 		comp->dev = NULL;
1060 		memset(comp->name, 0, sizeof(comp->name));
1061 		comp->playback_hook = NULL;
1062 	}
1063 
1064 	tas2781_hda_remove_controls(tas_hda);
1065 
1066 	tasdevice_spi_config_info_remove(tas_hda->priv);
1067 	tasdevice_spi_dsp_remove(tas_hda->priv);
1068 
1069 	tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
1070 }
1071 
1072 static const struct component_ops tas2781_hda_comp_ops = {
1073 	.bind = tas2781_hda_bind,
1074 	.unbind = tas2781_hda_unbind,
1075 };
1076 
tas2781_hda_remove(struct device * dev)1077 static void tas2781_hda_remove(struct device *dev)
1078 {
1079 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
1080 
1081 	component_del(tas_hda->priv->dev, &tas2781_hda_comp_ops);
1082 
1083 	pm_runtime_get_sync(tas_hda->priv->dev);
1084 	pm_runtime_disable(tas_hda->priv->dev);
1085 
1086 	pm_runtime_put_noidle(tas_hda->priv->dev);
1087 
1088 	mutex_destroy(&tas_hda->priv->codec_lock);
1089 }
1090 
tas2781_hda_spi_probe(struct spi_device * spi)1091 static int tas2781_hda_spi_probe(struct spi_device *spi)
1092 {
1093 	struct tasdevice_priv *tas_priv;
1094 	struct tas2781_hda *tas_hda;
1095 	const char *device_name;
1096 	int ret = 0;
1097 
1098 	tas_hda = devm_kzalloc(&spi->dev, sizeof(*tas_hda), GFP_KERNEL);
1099 	if (!tas_hda)
1100 		return -ENOMEM;
1101 
1102 	spi->max_speed_hz = TAS2781_SPI_MAX_FREQ;
1103 
1104 	tas_priv = devm_kzalloc(&spi->dev, sizeof(*tas_priv), GFP_KERNEL);
1105 	if (!tas_priv)
1106 		return -ENOMEM;
1107 	tas_priv->dev = &spi->dev;
1108 	tas_hda->priv = tas_priv;
1109 	tas_priv->regmap = devm_regmap_init_spi(spi, &tasdevice_regmap);
1110 	if (IS_ERR(tas_priv->regmap)) {
1111 		ret = PTR_ERR(tas_priv->regmap);
1112 		dev_err(tas_priv->dev, "Failed to allocate regmap: %d\n",
1113 			ret);
1114 		return ret;
1115 	}
1116 	if (strstr(dev_name(&spi->dev), "TXNW2781")) {
1117 		device_name = "TXNW2781";
1118 		tas_priv->save_calibration = tas2781_save_calibration;
1119 		tas_priv->apply_calibration = tas2781_apply_calib;
1120 	} else {
1121 		dev_err(tas_priv->dev, "Unmatched spi dev %s\n",
1122 			dev_name(&spi->dev));
1123 		return -ENODEV;
1124 	}
1125 
1126 	tas_priv->irq = spi->irq;
1127 	dev_set_drvdata(&spi->dev, tas_hda);
1128 	ret = tas2781_read_acpi(tas_hda, device_name,
1129 				spi_get_chipselect(spi, 0));
1130 	if (ret)
1131 		return dev_err_probe(tas_priv->dev, ret,
1132 				     "Platform not supported\n");
1133 
1134 	tasdevice_spi_init(tas_priv);
1135 
1136 	ret = component_add(tas_priv->dev, &tas2781_hda_comp_ops);
1137 	if (ret) {
1138 		dev_err(tas_priv->dev, "Register component fail: %d\n", ret);
1139 		return ret;
1140 	}
1141 
1142 	pm_runtime_set_autosuspend_delay(tas_priv->dev, 3000);
1143 	pm_runtime_use_autosuspend(tas_priv->dev);
1144 	pm_runtime_mark_last_busy(tas_priv->dev);
1145 	pm_runtime_set_active(tas_priv->dev);
1146 	pm_runtime_get_noresume(tas_priv->dev);
1147 	pm_runtime_enable(tas_priv->dev);
1148 
1149 	pm_runtime_put_autosuspend(tas_priv->dev);
1150 
1151 	return 0;
1152 }
1153 
tas2781_hda_spi_remove(struct spi_device * spi)1154 static void tas2781_hda_spi_remove(struct spi_device *spi)
1155 {
1156 	tas2781_hda_remove(&spi->dev);
1157 }
1158 
tas2781_runtime_suspend(struct device * dev)1159 static int tas2781_runtime_suspend(struct device *dev)
1160 {
1161 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
1162 
1163 	guard(mutex)(&tas_hda->priv->codec_lock);
1164 
1165 	if (tas_hda->priv->playback_started)
1166 		tasdevice_spi_tuning_switch(tas_hda->priv, 1);
1167 
1168 	tas_hda->priv->cur_book = -1;
1169 	tas_hda->priv->cur_conf = -1;
1170 
1171 	return 0;
1172 }
1173 
tas2781_runtime_resume(struct device * dev)1174 static int tas2781_runtime_resume(struct device *dev)
1175 {
1176 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
1177 
1178 	guard(mutex)(&tas_hda->priv->codec_lock);
1179 
1180 	if (tas_hda->priv->playback_started)
1181 		tasdevice_spi_tuning_switch(tas_hda->priv, 0);
1182 
1183 	return 0;
1184 }
1185 
tas2781_system_suspend(struct device * dev)1186 static int tas2781_system_suspend(struct device *dev)
1187 {
1188 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
1189 	int ret;
1190 
1191 	ret = pm_runtime_force_suspend(dev);
1192 	if (ret)
1193 		return ret;
1194 
1195 	/* Shutdown chip before system suspend */
1196 	if (tas_hda->priv->playback_started)
1197 		tasdevice_spi_tuning_switch(tas_hda->priv, 1);
1198 
1199 	return 0;
1200 }
1201 
tas2781_system_resume(struct device * dev)1202 static int tas2781_system_resume(struct device *dev)
1203 {
1204 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
1205 	int ret, val;
1206 
1207 	ret = pm_runtime_force_resume(dev);
1208 	if (ret)
1209 		return ret;
1210 
1211 	guard(mutex)(&tas_hda->priv->codec_lock);
1212 	ret = tasdevice_spi_dev_read(tas_hda->priv, TAS2781_REG_CLK_CONFIG,
1213 				     &val);
1214 	if (ret < 0)
1215 		return ret;
1216 
1217 	if (val == TAS2781_REG_CLK_CONFIG_RESET) {
1218 		tas_hda->priv->cur_book = -1;
1219 		tas_hda->priv->cur_conf = -1;
1220 		tas_hda->priv->cur_prog = -1;
1221 
1222 		ret = tasdevice_spi_prmg_load(tas_hda->priv, 0);
1223 		if (ret < 0) {
1224 			dev_err(tas_hda->priv->dev,
1225 				"FW download failed = %d\n", ret);
1226 			return ret;
1227 		}
1228 
1229 		if (tas_hda->priv->playback_started)
1230 			tasdevice_spi_tuning_switch(tas_hda->priv, 0);
1231 	}
1232 
1233 	return ret;
1234 }
1235 
1236 static const struct dev_pm_ops tas2781_hda_pm_ops = {
1237 	RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
1238 	SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
1239 };
1240 
1241 static const struct spi_device_id tas2781_hda_spi_id[] = {
1242 	{ "tas2781-hda", },
1243 	{}
1244 };
1245 
1246 static const struct acpi_device_id tas2781_acpi_hda_match[] = {
1247 	{"TXNW2781", },
1248 	{}
1249 };
1250 MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
1251 
1252 static struct spi_driver tas2781_hda_spi_driver = {
1253 	.driver = {
1254 		.name		= "tas2781-hda",
1255 		.acpi_match_table = tas2781_acpi_hda_match,
1256 		.pm		= &tas2781_hda_pm_ops,
1257 	},
1258 	.id_table	= tas2781_hda_spi_id,
1259 	.probe		= tas2781_hda_spi_probe,
1260 	.remove		= tas2781_hda_spi_remove,
1261 };
1262 module_spi_driver(tas2781_hda_spi_driver);
1263 
1264 MODULE_DESCRIPTION("TAS2781 HDA SPI Driver");
1265 MODULE_AUTHOR("Baojun, Xu, <[email protected]>");
1266 MODULE_LICENSE("GPL");
1267