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