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