xref: /linux/sound/soc/codecs/sigmadsp.c (revision f3caa0b02455409eec4673ddd8df72d8bcad4e98)
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;
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 		goto done;
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 		goto done;
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 		goto done;
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 		goto done;
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 done:
546 	release_firmware(fw);
547 
548 	return ret;
549 }
550 
551 static int sigmadsp_init(struct sigmadsp *sigmadsp, struct device *dev,
552 	const struct sigmadsp_ops *ops, const char *firmware_name)
553 {
554 	sigmadsp->ops = ops;
555 	sigmadsp->dev = dev;
556 
557 	INIT_LIST_HEAD(&sigmadsp->ctrl_list);
558 	INIT_LIST_HEAD(&sigmadsp->data_list);
559 	mutex_init(&sigmadsp->lock);
560 
561 	return sigmadsp_firmware_load(sigmadsp, firmware_name);
562 }
563 
564 /**
565  * devm_sigmadsp_init() - Initialize SigmaDSP instance
566  * @dev: The parent device
567  * @ops: The sigmadsp_ops to use for this instance
568  * @firmware_name: Name of the firmware file to load
569  *
570  * Allocates a SigmaDSP instance and loads the specified firmware file.
571  *
572  * Returns a pointer to a struct sigmadsp on success, or a PTR_ERR() on error.
573  */
574 struct sigmadsp *devm_sigmadsp_init(struct device *dev,
575 	const struct sigmadsp_ops *ops, const char *firmware_name)
576 {
577 	struct sigmadsp *sigmadsp;
578 	int ret;
579 
580 	sigmadsp = devres_alloc(devm_sigmadsp_release, sizeof(*sigmadsp),
581 		GFP_KERNEL);
582 	if (!sigmadsp)
583 		return ERR_PTR(-ENOMEM);
584 
585 	ret = sigmadsp_init(sigmadsp, dev, ops, firmware_name);
586 	if (ret) {
587 		devres_free(sigmadsp);
588 		return ERR_PTR(ret);
589 	}
590 
591 	devres_add(dev, sigmadsp);
592 
593 	return sigmadsp;
594 }
595 EXPORT_SYMBOL_GPL(devm_sigmadsp_init);
596 
597 static int sigmadsp_rate_to_index(struct sigmadsp *sigmadsp, unsigned int rate)
598 {
599 	unsigned int i;
600 
601 	for (i = 0; i < sigmadsp->rate_constraints.count; i++) {
602 		if (sigmadsp->rate_constraints.list[i] == rate)
603 			return i;
604 	}
605 
606 	return -EINVAL;
607 }
608 
609 static unsigned int sigmadsp_get_samplerate_mask(struct sigmadsp *sigmadsp,
610 	unsigned int samplerate)
611 {
612 	int samplerate_index;
613 
614 	if (samplerate == 0)
615 		return 0;
616 
617 	if (sigmadsp->rate_constraints.count) {
618 		samplerate_index = sigmadsp_rate_to_index(sigmadsp, samplerate);
619 		if (samplerate_index < 0)
620 			return 0;
621 
622 		return BIT(samplerate_index);
623 	} else {
624 		return ~0;
625 	}
626 }
627 
628 static bool sigmadsp_samplerate_valid(unsigned int supported,
629 	unsigned int requested)
630 {
631 	/* All samplerates are supported */
632 	if (!supported)
633 		return true;
634 
635 	return supported & requested;
636 }
637 
638 static int sigmadsp_alloc_control(struct sigmadsp *sigmadsp,
639 	struct sigmadsp_control *ctrl, unsigned int samplerate_mask)
640 {
641 	struct snd_kcontrol_new template;
642 	struct snd_kcontrol *kcontrol;
643 
644 	memset(&template, 0, sizeof(template));
645 	template.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
646 	template.name = ctrl->name;
647 	template.info = sigmadsp_ctrl_info;
648 	template.get = sigmadsp_ctrl_get;
649 	template.put = sigmadsp_ctrl_put;
650 	template.private_value = (unsigned long)ctrl;
651 	template.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
652 	if (!sigmadsp_samplerate_valid(ctrl->samplerates, samplerate_mask))
653 		template.access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
654 
655 	kcontrol = snd_ctl_new1(&template, sigmadsp);
656 	if (!kcontrol)
657 		return -ENOMEM;
658 
659 	kcontrol->private_free = sigmadsp_control_free;
660 	ctrl->kcontrol = kcontrol;
661 
662 	return snd_ctl_add(sigmadsp->component->card->snd_card, kcontrol);
663 }
664 
665 static void sigmadsp_activate_ctrl(struct sigmadsp *sigmadsp,
666 	struct sigmadsp_control *ctrl, unsigned int samplerate_mask)
667 {
668 	struct snd_card *card = sigmadsp->component->card->snd_card;
669 	bool active;
670 	int changed;
671 
672 	active = sigmadsp_samplerate_valid(ctrl->samplerates, samplerate_mask);
673 	if (!ctrl->kcontrol)
674 		return;
675 	changed = snd_ctl_activate_id(card, &ctrl->kcontrol->id, active);
676 	if (active && changed > 0) {
677 		scoped_guard(mutex, &sigmadsp->lock) {
678 			if (ctrl->cached)
679 				sigmadsp_ctrl_write(sigmadsp, ctrl, ctrl->cache);
680 		}
681 	}
682 }
683 
684 /**
685  * sigmadsp_attach() - Attach a sigmadsp instance to a ASoC component
686  * @sigmadsp: The sigmadsp instance to attach
687  * @component: The component to attach to
688  *
689  * Typically called in the components probe callback.
690  *
691  * Note, once this function has been called the firmware must not be released
692  * until after the ALSA snd_card that the component belongs to has been
693  * disconnected, even if sigmadsp_attach() returns an error.
694  */
695 int sigmadsp_attach(struct sigmadsp *sigmadsp,
696 	struct snd_soc_component *component)
697 {
698 	struct sigmadsp_control *ctrl;
699 	unsigned int samplerate_mask;
700 	int ret;
701 
702 	sigmadsp->component = component;
703 
704 	samplerate_mask = sigmadsp_get_samplerate_mask(sigmadsp,
705 		sigmadsp->current_samplerate);
706 
707 	list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head) {
708 		ret = sigmadsp_alloc_control(sigmadsp, ctrl, samplerate_mask);
709 		if (ret)
710 			return ret;
711 	}
712 
713 	return 0;
714 }
715 EXPORT_SYMBOL_GPL(sigmadsp_attach);
716 
717 /**
718  * sigmadsp_setup() - Setup the DSP for the specified samplerate
719  * @sigmadsp: The sigmadsp instance to configure
720  * @samplerate: The samplerate the DSP should be configured for
721  *
722  * Loads the appropriate firmware program and parameter memory (if not already
723  * loaded) and enables the controls for the specified samplerate. Any control
724  * parameter changes that have been made previously will be restored.
725  *
726  * Returns 0 on success, a negative error code otherwise.
727  */
728 int sigmadsp_setup(struct sigmadsp *sigmadsp, unsigned int samplerate)
729 {
730 	struct sigmadsp_control *ctrl;
731 	unsigned int samplerate_mask;
732 	struct sigmadsp_data *data;
733 	int ret;
734 
735 	if (sigmadsp->current_samplerate == samplerate)
736 		return 0;
737 
738 	samplerate_mask = sigmadsp_get_samplerate_mask(sigmadsp, samplerate);
739 	if (samplerate_mask == 0)
740 		return -EINVAL;
741 
742 	list_for_each_entry(data, &sigmadsp->data_list, head) {
743 		if (!sigmadsp_samplerate_valid(data->samplerates,
744 		    samplerate_mask))
745 			continue;
746 		ret = sigmadsp_write(sigmadsp, data->addr, data->data,
747 			data->length);
748 		if (ret)
749 			goto err;
750 	}
751 
752 	list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head)
753 		sigmadsp_activate_ctrl(sigmadsp, ctrl, samplerate_mask);
754 
755 	sigmadsp->current_samplerate = samplerate;
756 
757 	return 0;
758 err:
759 	sigmadsp_reset(sigmadsp);
760 
761 	return ret;
762 }
763 EXPORT_SYMBOL_GPL(sigmadsp_setup);
764 
765 /**
766  * sigmadsp_reset() - Notify the sigmadsp instance that the DSP has been reset
767  * @sigmadsp: The sigmadsp instance to reset
768  *
769  * Should be called whenever the DSP has been reset and parameter and program
770  * memory need to be re-loaded.
771  */
772 void sigmadsp_reset(struct sigmadsp *sigmadsp)
773 {
774 	struct sigmadsp_control *ctrl;
775 
776 	list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head)
777 		sigmadsp_activate_ctrl(sigmadsp, ctrl, false);
778 
779 	sigmadsp->current_samplerate = 0;
780 }
781 EXPORT_SYMBOL_GPL(sigmadsp_reset);
782 
783 /**
784  * sigmadsp_restrict_params() - Applies DSP firmware specific constraints
785  * @sigmadsp: The sigmadsp instance
786  * @substream: The substream to restrict
787  *
788  * Applies samplerate constraints that may be required by the firmware Should
789  * typically be called from the CODEC/component drivers startup callback.
790  *
791  * Returns 0 on success, a negative error code otherwise.
792  */
793 int sigmadsp_restrict_params(struct sigmadsp *sigmadsp,
794 	struct snd_pcm_substream *substream)
795 {
796 	if (sigmadsp->rate_constraints.count == 0)
797 		return 0;
798 
799 	return snd_pcm_hw_constraint_list(substream->runtime, 0,
800 		SNDRV_PCM_HW_PARAM_RATE, &sigmadsp->rate_constraints);
801 }
802 EXPORT_SYMBOL_GPL(sigmadsp_restrict_params);
803 
804 MODULE_DESCRIPTION("Analog Devices SigmaStudio firmware helpers");
805 MODULE_LICENSE("GPL");
806