xref: /linux/sound/soc/codecs/sigmadsp.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Load Analog Devices SigmaStudio firmware files
4  *
5  * Copyright 2009-2014 Analog Devices Inc.
6  */
7 
8 #include <linux/cleanup.h>
9 #include <linux/crc32.h>
10 #include <linux/firmware.h>
11 #include <linux/kernel.h>
12 #include <linux/i2c.h>
13 #include <linux/regmap.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 
17 #include <sound/control.h>
18 #include <sound/soc.h>
19 
20 #include "sigmadsp.h"
21 
22 #define SIGMA_MAGIC "ADISIGM"
23 
24 #define SIGMA_FW_CHUNK_TYPE_DATA 0
25 #define SIGMA_FW_CHUNK_TYPE_CONTROL 1
26 #define SIGMA_FW_CHUNK_TYPE_SAMPLERATES 2
27 
28 #define READBACK_CTRL_NAME "ReadBack"
29 
30 struct sigmadsp_control {
31 	struct list_head head;
32 	uint32_t samplerates;
33 	unsigned int addr;
34 	unsigned int num_bytes;
35 	const char *name;
36 	struct snd_kcontrol *kcontrol;
37 	bool is_readback;
38 	bool cached;
39 	u8 cache[] __counted_by(num_bytes);
40 };
41 
42 struct sigmadsp_data {
43 	struct list_head head;
44 	uint32_t samplerates;
45 	unsigned int addr;
46 	unsigned int length;
47 	uint8_t data[] __counted_by(length);
48 };
49 
50 struct sigma_fw_chunk {
51 	__le32 length;
52 	__le32 tag;
53 	__le32 samplerates;
54 } __packed;
55 
56 struct sigma_fw_chunk_data {
57 	struct sigma_fw_chunk chunk;
58 	__le16 addr;
59 	uint8_t data[];
60 } __packed;
61 
62 struct sigma_fw_chunk_control {
63 	struct sigma_fw_chunk chunk;
64 	__le16 type;
65 	__le16 addr;
66 	__le16 num_bytes;
67 	const char name[];
68 } __packed;
69 
70 struct sigma_fw_chunk_samplerate {
71 	struct sigma_fw_chunk chunk;
72 	__le32 samplerates[];
73 } __packed;
74 
75 struct sigma_firmware_header {
76 	unsigned char magic[7];
77 	u8 version;
78 	__le32 crc;
79 } __packed;
80 
81 enum {
82 	SIGMA_ACTION_WRITEXBYTES = 0,
83 	SIGMA_ACTION_WRITESINGLE,
84 	SIGMA_ACTION_WRITESAFELOAD,
85 	SIGMA_ACTION_END,
86 };
87 
88 struct sigma_action {
89 	u8 instr;
90 	u8 len_hi;
91 	__le16 len;
92 	__be16 addr;
93 	unsigned char payload[];
94 } __packed;
95 
96 static int sigmadsp_write(struct sigmadsp *sigmadsp, unsigned int addr,
97 	const uint8_t data[], size_t len)
98 {
99 	return sigmadsp->write(sigmadsp->control_data, addr, data, len);
100 }
101 
102 static int sigmadsp_read(struct sigmadsp *sigmadsp, unsigned int addr,
103 	uint8_t data[], size_t len)
104 {
105 	return sigmadsp->read(sigmadsp->control_data, addr, data, len);
106 }
107 
108 static int sigmadsp_ctrl_info(struct snd_kcontrol *kcontrol,
109 	struct snd_ctl_elem_info *info)
110 {
111 	struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
112 
113 	info->type = SNDRV_CTL_ELEM_TYPE_BYTES;
114 	info->count = ctrl->num_bytes;
115 
116 	return 0;
117 }
118 
119 static int sigmadsp_ctrl_write(struct sigmadsp *sigmadsp,
120 	struct sigmadsp_control *ctrl, void *data)
121 {
122 	/* safeload loads up to 20 bytes in a atomic operation */
123 	if (ctrl->num_bytes <= 20 && sigmadsp->ops && sigmadsp->ops->safeload)
124 		return sigmadsp->ops->safeload(sigmadsp, ctrl->addr, data,
125 			ctrl->num_bytes);
126 	else
127 		return sigmadsp_write(sigmadsp, ctrl->addr, data,
128 			ctrl->num_bytes);
129 }
130 
131 static int sigmadsp_ctrl_put(struct snd_kcontrol *kcontrol,
132 	struct snd_ctl_elem_value *ucontrol)
133 {
134 	struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
135 	struct sigmadsp *sigmadsp = snd_kcontrol_chip(kcontrol);
136 	uint8_t *data;
137 	int ret = 0;
138 
139 	guard(mutex)(&sigmadsp->lock);
140 
141 	data = ucontrol->value.bytes.data;
142 
143 	if (!(kcontrol->vd[0].access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
144 		ret = sigmadsp_ctrl_write(sigmadsp, ctrl, data);
145 
146 	if (ret == 0) {
147 		memcpy(ctrl->cache, data, ctrl->num_bytes);
148 		if (!ctrl->is_readback)
149 			ctrl->cached = true;
150 	}
151 
152 	return ret;
153 }
154 
155 static int sigmadsp_ctrl_get(struct snd_kcontrol *kcontrol,
156 	struct snd_ctl_elem_value *ucontrol)
157 {
158 	struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
159 	struct sigmadsp *sigmadsp = snd_kcontrol_chip(kcontrol);
160 	int ret = 0;
161 
162 	guard(mutex)(&sigmadsp->lock);
163 
164 	if (!ctrl->cached) {
165 		ret = sigmadsp_read(sigmadsp, ctrl->addr, ctrl->cache,
166 			ctrl->num_bytes);
167 	}
168 
169 	if (ret == 0) {
170 		if (!ctrl->is_readback)
171 			ctrl->cached = true;
172 		memcpy(ucontrol->value.bytes.data, ctrl->cache,
173 			ctrl->num_bytes);
174 	}
175 
176 	return ret;
177 }
178 
179 static void sigmadsp_control_free(struct snd_kcontrol *kcontrol)
180 {
181 	struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
182 
183 	ctrl->kcontrol = NULL;
184 }
185 
186 static bool sigma_fw_validate_control_name(const char *name, unsigned int len)
187 {
188 	unsigned int i;
189 
190 	for (i = 0; i < len; i++) {
191 		/* Normal ASCII characters are valid */
192 		if (name[i] < ' ' || name[i] > '~')
193 			return false;
194 	}
195 
196 	return true;
197 }
198 
199 static int sigma_fw_load_control(struct sigmadsp *sigmadsp,
200 	const struct sigma_fw_chunk *chunk, unsigned int length)
201 {
202 	const struct sigma_fw_chunk_control *ctrl_chunk;
203 	struct sigmadsp_control *ctrl;
204 	unsigned int num_bytes;
205 	size_t name_len;
206 	char *name;
207 	int ret;
208 
209 	if (length <= sizeof(*ctrl_chunk))
210 		return -EINVAL;
211 
212 	ctrl_chunk = (const struct sigma_fw_chunk_control *)chunk;
213 
214 	name_len = length - sizeof(*ctrl_chunk);
215 	if (name_len >= SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
216 		name_len = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - 1;
217 
218 	/* Make sure there are no non-displayable characaters in the string */
219 	if (!sigma_fw_validate_control_name(ctrl_chunk->name, name_len))
220 		return -EINVAL;
221 
222 	num_bytes = le16_to_cpu(ctrl_chunk->num_bytes);
223 	ctrl = kzalloc_flex(*ctrl, cache, num_bytes);
224 	if (!ctrl)
225 		return -ENOMEM;
226 
227 	name = kmemdup_nul(ctrl_chunk->name, name_len, GFP_KERNEL);
228 	if (!name) {
229 		ret = -ENOMEM;
230 		goto err_free_ctrl;
231 	}
232 	ctrl->name = name;
233 
234 	/*
235 	 * Readbacks doesn't work with non-volatile controls, since the
236 	 * firmware updates the control value without driver interaction. Mark
237 	 * the readbacks to ensure that the values are not cached.
238 	 */
239 	if (ctrl->name && strncmp(ctrl->name, READBACK_CTRL_NAME,
240 				  (sizeof(READBACK_CTRL_NAME) - 1)) == 0)
241 		ctrl->is_readback = true;
242 
243 	ctrl->addr = le16_to_cpu(ctrl_chunk->addr);
244 	ctrl->num_bytes = num_bytes;
245 	ctrl->samplerates = le32_to_cpu(chunk->samplerates);
246 
247 	list_add_tail(&ctrl->head, &sigmadsp->ctrl_list);
248 
249 	return 0;
250 
251 err_free_ctrl:
252 	kfree(ctrl);
253 
254 	return ret;
255 }
256 
257 static int sigma_fw_load_data(struct sigmadsp *sigmadsp,
258 	const struct sigma_fw_chunk *chunk, unsigned int length)
259 {
260 	const struct sigma_fw_chunk_data *data_chunk;
261 	struct sigmadsp_data *data;
262 
263 	if (length <= sizeof(*data_chunk))
264 		return -EINVAL;
265 
266 	data_chunk = (struct sigma_fw_chunk_data *)chunk;
267 
268 	length -= sizeof(*data_chunk);
269 
270 	data = kzalloc_flex(*data, data, length);
271 	if (!data)
272 		return -ENOMEM;
273 
274 	data->addr = le16_to_cpu(data_chunk->addr);
275 	data->length = length;
276 	data->samplerates = le32_to_cpu(chunk->samplerates);
277 	memcpy(data->data, data_chunk->data, length);
278 	list_add_tail(&data->head, &sigmadsp->data_list);
279 
280 	return 0;
281 }
282 
283 static int sigma_fw_load_samplerates(struct sigmadsp *sigmadsp,
284 	const struct sigma_fw_chunk *chunk, unsigned int length)
285 {
286 	const struct sigma_fw_chunk_samplerate *rate_chunk;
287 	unsigned int num_rates;
288 	unsigned int *rates;
289 	unsigned int i;
290 
291 	rate_chunk = (const struct sigma_fw_chunk_samplerate *)chunk;
292 
293 	num_rates = (length - sizeof(*rate_chunk)) / sizeof(__le32);
294 
295 	if (num_rates > 32 || num_rates == 0)
296 		return -EINVAL;
297 
298 	/* We only allow one samplerates block per file */
299 	if (sigmadsp->rate_constraints.count)
300 		return -EINVAL;
301 
302 	rates = kcalloc(num_rates, sizeof(*rates), GFP_KERNEL);
303 	if (!rates)
304 		return -ENOMEM;
305 
306 	for (i = 0; i < num_rates; i++)
307 		rates[i] = le32_to_cpu(rate_chunk->samplerates[i]);
308 
309 	sigmadsp->rate_constraints.count = num_rates;
310 	sigmadsp->rate_constraints.list = rates;
311 
312 	return 0;
313 }
314 
315 static int sigmadsp_fw_load_v2(struct sigmadsp *sigmadsp,
316 	const struct firmware *fw)
317 {
318 	struct sigma_fw_chunk *chunk;
319 	unsigned int length, pos;
320 	int ret;
321 
322 	/*
323 	 * Make sure that there is at least one chunk to avoid integer
324 	 * underflows later on. Empty firmware is still valid though.
325 	 */
326 	if (fw->size < sizeof(*chunk) + sizeof(struct sigma_firmware_header))
327 		return 0;
328 
329 	pos = sizeof(struct sigma_firmware_header);
330 
331 	while (pos < fw->size - sizeof(*chunk)) {
332 		chunk = (struct sigma_fw_chunk *)(fw->data + pos);
333 
334 		length = le32_to_cpu(chunk->length);
335 
336 		if (length > fw->size - pos || length < sizeof(*chunk))
337 			return -EINVAL;
338 
339 		switch (le32_to_cpu(chunk->tag)) {
340 		case SIGMA_FW_CHUNK_TYPE_DATA:
341 			ret = sigma_fw_load_data(sigmadsp, chunk, length);
342 			break;
343 		case SIGMA_FW_CHUNK_TYPE_CONTROL:
344 			ret = sigma_fw_load_control(sigmadsp, chunk, length);
345 			break;
346 		case SIGMA_FW_CHUNK_TYPE_SAMPLERATES:
347 			ret = sigma_fw_load_samplerates(sigmadsp, chunk, length);
348 			break;
349 		default:
350 			dev_warn(sigmadsp->dev, "Unknown chunk type: %d\n",
351 				chunk->tag);
352 			ret = 0;
353 			break;
354 		}
355 
356 		if (ret)
357 			return ret;
358 
359 		/*
360 		 * This can not overflow since if length is larger than the
361 		 * maximum firmware size (0x4000000) we'll error out earilier.
362 		 */
363 		pos += ALIGN(length, sizeof(__le32));
364 	}
365 
366 	return 0;
367 }
368 
369 static inline u32 sigma_action_len(struct sigma_action *sa)
370 {
371 	return (sa->len_hi << 16) | le16_to_cpu(sa->len);
372 }
373 
374 static size_t sigma_action_size(struct sigma_action *sa)
375 {
376 	size_t payload = 0;
377 
378 	switch (sa->instr) {
379 	case SIGMA_ACTION_WRITEXBYTES:
380 	case SIGMA_ACTION_WRITESINGLE:
381 	case SIGMA_ACTION_WRITESAFELOAD:
382 		payload = sigma_action_len(sa);
383 		break;
384 	default:
385 		break;
386 	}
387 
388 	payload = ALIGN(payload, 2);
389 
390 	return payload + sizeof(struct sigma_action);
391 }
392 
393 /*
394  * Returns a negative error value in case of an error, 0 if processing of
395  * the firmware should be stopped after this action, 1 otherwise.
396  */
397 static int process_sigma_action(struct sigmadsp *sigmadsp,
398 	struct sigma_action *sa)
399 {
400 	size_t len = sigma_action_len(sa);
401 	struct sigmadsp_data *data;
402 
403 	pr_debug("%s: instr:%i addr:%#x len:%zu\n", __func__,
404 		sa->instr, sa->addr, len);
405 
406 	switch (sa->instr) {
407 	case SIGMA_ACTION_WRITEXBYTES:
408 	case SIGMA_ACTION_WRITESINGLE:
409 	case SIGMA_ACTION_WRITESAFELOAD:
410 		if (len < 3)
411 			return -EINVAL;
412 
413 		data = kzalloc_flex(*data, data, size_sub(len, 2));
414 		if (!data)
415 			return -ENOMEM;
416 
417 		data->addr = be16_to_cpu(sa->addr);
418 		data->length = len - 2;
419 		memcpy(data->data, sa->payload, data->length);
420 		list_add_tail(&data->head, &sigmadsp->data_list);
421 		break;
422 	case SIGMA_ACTION_END:
423 		return 0;
424 	default:
425 		return -EINVAL;
426 	}
427 
428 	return 1;
429 }
430 
431 static int sigmadsp_fw_load_v1(struct sigmadsp *sigmadsp,
432 	const struct firmware *fw)
433 {
434 	struct sigma_action *sa;
435 	size_t size, pos;
436 	int ret;
437 
438 	pos = sizeof(struct sigma_firmware_header);
439 
440 	while (pos + sizeof(*sa) <= fw->size) {
441 		sa = (struct sigma_action *)(fw->data + pos);
442 
443 		size = sigma_action_size(sa);
444 		pos += size;
445 		if (pos > fw->size || size == 0)
446 			break;
447 
448 		ret = process_sigma_action(sigmadsp, sa);
449 
450 		pr_debug("%s: action returned %i\n", __func__, ret);
451 
452 		if (ret <= 0)
453 			return ret;
454 	}
455 
456 	if (pos != fw->size)
457 		return -EINVAL;
458 
459 	return 0;
460 }
461 
462 static void sigmadsp_firmware_release(struct sigmadsp *sigmadsp)
463 {
464 	struct sigmadsp_control *ctrl, *_ctrl;
465 	struct sigmadsp_data *data, *_data;
466 
467 	list_for_each_entry_safe(ctrl, _ctrl, &sigmadsp->ctrl_list, head) {
468 		kfree(ctrl->name);
469 		kfree(ctrl);
470 	}
471 
472 	list_for_each_entry_safe(data, _data, &sigmadsp->data_list, head)
473 		kfree(data);
474 
475 	INIT_LIST_HEAD(&sigmadsp->ctrl_list);
476 	INIT_LIST_HEAD(&sigmadsp->data_list);
477 }
478 
479 static void devm_sigmadsp_release(struct device *dev, void *res)
480 {
481 	sigmadsp_firmware_release((struct sigmadsp *)res);
482 }
483 
484 static int sigmadsp_firmware_load(struct sigmadsp *sigmadsp, const char *name)
485 {
486 	const struct sigma_firmware_header *ssfw_head;
487 	const struct firmware *fw __free(firmware) = NULL;
488 	int ret;
489 	u32 crc;
490 
491 	/* first load the blob */
492 	ret = request_firmware(&fw, name, sigmadsp->dev);
493 	if (ret) {
494 		pr_debug("%s: request_firmware() failed with %i\n", __func__, ret);
495 		return ret;
496 	}
497 
498 	/* then verify the header */
499 	ret = -EINVAL;
500 
501 	/*
502 	 * Reject too small or unreasonable large files. The upper limit has been
503 	 * chosen a bit arbitrarily, but it should be enough for all practical
504 	 * purposes and having the limit makes it easier to avoid integer
505 	 * overflows later in the loading process.
506 	 */
507 	if (fw->size < sizeof(*ssfw_head) || fw->size >= 0x4000000) {
508 		dev_err(sigmadsp->dev, "Failed to load firmware: Invalid size\n");
509 		return -EINVAL;
510 	}
511 
512 	ssfw_head = (void *)fw->data;
513 	if (memcmp(ssfw_head->magic, SIGMA_MAGIC, ARRAY_SIZE(ssfw_head->magic))) {
514 		dev_err(sigmadsp->dev, "Failed to load firmware: Invalid magic\n");
515 		return -EINVAL;
516 	}
517 
518 	crc = crc32(0, fw->data + sizeof(*ssfw_head),
519 			fw->size - sizeof(*ssfw_head));
520 	pr_debug("%s: crc=%x\n", __func__, crc);
521 	if (crc != le32_to_cpu(ssfw_head->crc)) {
522 		dev_err(sigmadsp->dev, "Failed to load firmware: Wrong crc checksum: expected %x got %x\n",
523 			le32_to_cpu(ssfw_head->crc), crc);
524 		return -EINVAL;
525 	}
526 
527 	switch (ssfw_head->version) {
528 	case 1:
529 		ret = sigmadsp_fw_load_v1(sigmadsp, fw);
530 		break;
531 	case 2:
532 		ret = sigmadsp_fw_load_v2(sigmadsp, fw);
533 		break;
534 	default:
535 		dev_err(sigmadsp->dev,
536 			"Failed to load firmware: Invalid version %d. Supported firmware versions: 1, 2\n",
537 			ssfw_head->version);
538 		ret = -EINVAL;
539 		break;
540 	}
541 
542 	if (ret)
543 		sigmadsp_firmware_release(sigmadsp);
544 
545 	return ret;
546 }
547 
548 static int sigmadsp_init(struct sigmadsp *sigmadsp, struct device *dev,
549 	const struct sigmadsp_ops *ops, const char *firmware_name)
550 {
551 	sigmadsp->ops = ops;
552 	sigmadsp->dev = dev;
553 
554 	INIT_LIST_HEAD(&sigmadsp->ctrl_list);
555 	INIT_LIST_HEAD(&sigmadsp->data_list);
556 	mutex_init(&sigmadsp->lock);
557 
558 	return sigmadsp_firmware_load(sigmadsp, firmware_name);
559 }
560 
561 /**
562  * devm_sigmadsp_init() - Initialize SigmaDSP instance
563  * @dev: The parent device
564  * @ops: The sigmadsp_ops to use for this instance
565  * @firmware_name: Name of the firmware file to load
566  *
567  * Allocates a SigmaDSP instance and loads the specified firmware file.
568  *
569  * Returns a pointer to a struct sigmadsp on success, or a PTR_ERR() on error.
570  */
571 struct sigmadsp *devm_sigmadsp_init(struct device *dev,
572 	const struct sigmadsp_ops *ops, const char *firmware_name)
573 {
574 	struct sigmadsp *sigmadsp;
575 	int ret;
576 
577 	sigmadsp = devres_alloc(devm_sigmadsp_release, sizeof(*sigmadsp),
578 		GFP_KERNEL);
579 	if (!sigmadsp)
580 		return ERR_PTR(-ENOMEM);
581 
582 	ret = sigmadsp_init(sigmadsp, dev, ops, firmware_name);
583 	if (ret) {
584 		devres_free(sigmadsp);
585 		return ERR_PTR(ret);
586 	}
587 
588 	devres_add(dev, sigmadsp);
589 
590 	return sigmadsp;
591 }
592 EXPORT_SYMBOL_GPL(devm_sigmadsp_init);
593 
594 static int sigmadsp_rate_to_index(struct sigmadsp *sigmadsp, unsigned int rate)
595 {
596 	unsigned int i;
597 
598 	for (i = 0; i < sigmadsp->rate_constraints.count; i++) {
599 		if (sigmadsp->rate_constraints.list[i] == rate)
600 			return i;
601 	}
602 
603 	return -EINVAL;
604 }
605 
606 static unsigned int sigmadsp_get_samplerate_mask(struct sigmadsp *sigmadsp,
607 	unsigned int samplerate)
608 {
609 	int samplerate_index;
610 
611 	if (samplerate == 0)
612 		return 0;
613 
614 	if (sigmadsp->rate_constraints.count) {
615 		samplerate_index = sigmadsp_rate_to_index(sigmadsp, samplerate);
616 		if (samplerate_index < 0)
617 			return 0;
618 
619 		return BIT(samplerate_index);
620 	} else {
621 		return ~0;
622 	}
623 }
624 
625 static bool sigmadsp_samplerate_valid(unsigned int supported,
626 	unsigned int requested)
627 {
628 	/* All samplerates are supported */
629 	if (!supported)
630 		return true;
631 
632 	return supported & requested;
633 }
634 
635 static int sigmadsp_alloc_control(struct sigmadsp *sigmadsp,
636 	struct sigmadsp_control *ctrl, unsigned int samplerate_mask)
637 {
638 	struct snd_kcontrol_new template;
639 	struct snd_kcontrol *kcontrol;
640 
641 	memset(&template, 0, sizeof(template));
642 	template.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
643 	template.name = ctrl->name;
644 	template.info = sigmadsp_ctrl_info;
645 	template.get = sigmadsp_ctrl_get;
646 	template.put = sigmadsp_ctrl_put;
647 	template.private_value = (unsigned long)ctrl;
648 	template.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
649 	if (!sigmadsp_samplerate_valid(ctrl->samplerates, samplerate_mask))
650 		template.access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
651 
652 	kcontrol = snd_ctl_new1(&template, sigmadsp);
653 	if (!kcontrol)
654 		return -ENOMEM;
655 
656 	kcontrol->private_free = sigmadsp_control_free;
657 	ctrl->kcontrol = kcontrol;
658 
659 	return snd_ctl_add(sigmadsp->component->card->snd_card, kcontrol);
660 }
661 
662 static void sigmadsp_activate_ctrl(struct sigmadsp *sigmadsp,
663 	struct sigmadsp_control *ctrl, unsigned int samplerate_mask)
664 {
665 	struct snd_card *card = sigmadsp->component->card->snd_card;
666 	bool active;
667 	int changed;
668 
669 	active = sigmadsp_samplerate_valid(ctrl->samplerates, samplerate_mask);
670 	if (!ctrl->kcontrol)
671 		return;
672 	changed = snd_ctl_activate_id(card, &ctrl->kcontrol->id, active);
673 	if (active && changed > 0) {
674 		scoped_guard(mutex, &sigmadsp->lock) {
675 			if (ctrl->cached)
676 				sigmadsp_ctrl_write(sigmadsp, ctrl, ctrl->cache);
677 		}
678 	}
679 }
680 
681 /**
682  * sigmadsp_attach() - Attach a sigmadsp instance to a ASoC component
683  * @sigmadsp: The sigmadsp instance to attach
684  * @component: The component to attach to
685  *
686  * Typically called in the components probe callback.
687  *
688  * Note, once this function has been called the firmware must not be released
689  * until after the ALSA snd_card that the component belongs to has been
690  * disconnected, even if sigmadsp_attach() returns an error.
691  */
692 int sigmadsp_attach(struct sigmadsp *sigmadsp,
693 	struct snd_soc_component *component)
694 {
695 	struct sigmadsp_control *ctrl;
696 	unsigned int samplerate_mask;
697 	int ret;
698 
699 	sigmadsp->component = component;
700 
701 	samplerate_mask = sigmadsp_get_samplerate_mask(sigmadsp,
702 		sigmadsp->current_samplerate);
703 
704 	list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head) {
705 		ret = sigmadsp_alloc_control(sigmadsp, ctrl, samplerate_mask);
706 		if (ret)
707 			return ret;
708 	}
709 
710 	return 0;
711 }
712 EXPORT_SYMBOL_GPL(sigmadsp_attach);
713 
714 /**
715  * sigmadsp_setup() - Setup the DSP for the specified samplerate
716  * @sigmadsp: The sigmadsp instance to configure
717  * @samplerate: The samplerate the DSP should be configured for
718  *
719  * Loads the appropriate firmware program and parameter memory (if not already
720  * loaded) and enables the controls for the specified samplerate. Any control
721  * parameter changes that have been made previously will be restored.
722  *
723  * Returns 0 on success, a negative error code otherwise.
724  */
725 int sigmadsp_setup(struct sigmadsp *sigmadsp, unsigned int samplerate)
726 {
727 	struct sigmadsp_control *ctrl;
728 	unsigned int samplerate_mask;
729 	struct sigmadsp_data *data;
730 	int ret;
731 
732 	if (sigmadsp->current_samplerate == samplerate)
733 		return 0;
734 
735 	samplerate_mask = sigmadsp_get_samplerate_mask(sigmadsp, samplerate);
736 	if (samplerate_mask == 0)
737 		return -EINVAL;
738 
739 	list_for_each_entry(data, &sigmadsp->data_list, head) {
740 		if (!sigmadsp_samplerate_valid(data->samplerates,
741 		    samplerate_mask))
742 			continue;
743 		ret = sigmadsp_write(sigmadsp, data->addr, data->data,
744 			data->length);
745 		if (ret)
746 			goto err;
747 	}
748 
749 	list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head)
750 		sigmadsp_activate_ctrl(sigmadsp, ctrl, samplerate_mask);
751 
752 	sigmadsp->current_samplerate = samplerate;
753 
754 	return 0;
755 err:
756 	sigmadsp_reset(sigmadsp);
757 
758 	return ret;
759 }
760 EXPORT_SYMBOL_GPL(sigmadsp_setup);
761 
762 /**
763  * sigmadsp_reset() - Notify the sigmadsp instance that the DSP has been reset
764  * @sigmadsp: The sigmadsp instance to reset
765  *
766  * Should be called whenever the DSP has been reset and parameter and program
767  * memory need to be re-loaded.
768  */
769 void sigmadsp_reset(struct sigmadsp *sigmadsp)
770 {
771 	struct sigmadsp_control *ctrl;
772 
773 	list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head)
774 		sigmadsp_activate_ctrl(sigmadsp, ctrl, false);
775 
776 	sigmadsp->current_samplerate = 0;
777 }
778 EXPORT_SYMBOL_GPL(sigmadsp_reset);
779 
780 /**
781  * sigmadsp_restrict_params() - Applies DSP firmware specific constraints
782  * @sigmadsp: The sigmadsp instance
783  * @substream: The substream to restrict
784  *
785  * Applies samplerate constraints that may be required by the firmware Should
786  * typically be called from the CODEC/component drivers startup callback.
787  *
788  * Returns 0 on success, a negative error code otherwise.
789  */
790 int sigmadsp_restrict_params(struct sigmadsp *sigmadsp,
791 	struct snd_pcm_substream *substream)
792 {
793 	if (sigmadsp->rate_constraints.count == 0)
794 		return 0;
795 
796 	return snd_pcm_hw_constraint_list(substream->runtime, 0,
797 		SNDRV_PCM_HW_PARAM_RATE, &sigmadsp->rate_constraints);
798 }
799 EXPORT_SYMBOL_GPL(sigmadsp_restrict_params);
800 
801 MODULE_DESCRIPTION("Analog Devices SigmaStudio firmware helpers");
802 MODULE_LICENSE("GPL");
803