]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - rpmsg/rpmsg.git/blob - sound/soc/codecs/sigmadsp.c
Merge tag 'devicetree-fixes-for-4.19-3' of git://git.kernel.org/pub/scm/linux/kernel...
[rpmsg/rpmsg.git] / sound / soc / codecs / sigmadsp.c
1 /*
2  * Load Analog Devices SigmaStudio firmware files
3  *
4  * Copyright 2009-2014 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
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>
17 #include <sound/control.h>
18 #include <sound/soc.h>
20 #include "sigmadsp.h"
22 #define SIGMA_MAGIC "ADISIGM"
24 #define SIGMA_FW_CHUNK_TYPE_DATA 0
25 #define SIGMA_FW_CHUNK_TYPE_CONTROL 1
26 #define SIGMA_FW_CHUNK_TYPE_SAMPLERATES 2
28 struct sigmadsp_control {
29         struct list_head head;
30         uint32_t samplerates;
31         unsigned int addr;
32         unsigned int num_bytes;
33         const char *name;
34         struct snd_kcontrol *kcontrol;
35         bool cached;
36         uint8_t cache[];
37 };
39 struct sigmadsp_data {
40         struct list_head head;
41         uint32_t samplerates;
42         unsigned int addr;
43         unsigned int length;
44         uint8_t data[];
45 };
47 struct sigma_fw_chunk {
48         __le32 length;
49         __le32 tag;
50         __le32 samplerates;
51 } __packed;
53 struct sigma_fw_chunk_data {
54         struct sigma_fw_chunk chunk;
55         __le16 addr;
56         uint8_t data[];
57 } __packed;
59 struct sigma_fw_chunk_control {
60         struct sigma_fw_chunk chunk;
61         __le16 type;
62         __le16 addr;
63         __le16 num_bytes;
64         const char name[];
65 } __packed;
67 struct sigma_fw_chunk_samplerate {
68         struct sigma_fw_chunk chunk;
69         __le32 samplerates[];
70 } __packed;
72 struct sigma_firmware_header {
73         unsigned char magic[7];
74         u8 version;
75         __le32 crc;
76 } __packed;
78 enum {
79         SIGMA_ACTION_WRITEXBYTES = 0,
80         SIGMA_ACTION_WRITESINGLE,
81         SIGMA_ACTION_WRITESAFELOAD,
82         SIGMA_ACTION_END,
83 };
85 struct sigma_action {
86         u8 instr;
87         u8 len_hi;
88         __le16 len;
89         __be16 addr;
90         unsigned char payload[];
91 } __packed;
93 static int sigmadsp_write(struct sigmadsp *sigmadsp, unsigned int addr,
94         const uint8_t data[], size_t len)
95 {
96         return sigmadsp->write(sigmadsp->control_data, addr, data, len);
97 }
99 static int sigmadsp_read(struct sigmadsp *sigmadsp, unsigned int addr,
100         uint8_t data[], size_t len)
102         return sigmadsp->read(sigmadsp->control_data, addr, data, len);
105 static int sigmadsp_ctrl_info(struct snd_kcontrol *kcontrol,
106         struct snd_ctl_elem_info *info)
108         struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
110         info->type = SNDRV_CTL_ELEM_TYPE_BYTES;
111         info->count = ctrl->num_bytes;
113         return 0;
116 static int sigmadsp_ctrl_write(struct sigmadsp *sigmadsp,
117         struct sigmadsp_control *ctrl, void *data)
119         /* safeload loads up to 20 bytes in a atomic operation */
120         if (ctrl->num_bytes <= 20 && sigmadsp->ops && sigmadsp->ops->safeload)
121                 return sigmadsp->ops->safeload(sigmadsp, ctrl->addr, data,
122                         ctrl->num_bytes);
123         else
124                 return sigmadsp_write(sigmadsp, ctrl->addr, data,
125                         ctrl->num_bytes);
128 static int sigmadsp_ctrl_put(struct snd_kcontrol *kcontrol,
129         struct snd_ctl_elem_value *ucontrol)
131         struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
132         struct sigmadsp *sigmadsp = snd_kcontrol_chip(kcontrol);
133         uint8_t *data;
134         int ret = 0;
136         mutex_lock(&sigmadsp->lock);
138         data = ucontrol->value.bytes.data;
140         if (!(kcontrol->vd[0].access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
141                 ret = sigmadsp_ctrl_write(sigmadsp, ctrl, data);
143         if (ret == 0) {
144                 memcpy(ctrl->cache, data, ctrl->num_bytes);
145                 ctrl->cached = true;
146         }
148         mutex_unlock(&sigmadsp->lock);
150         return ret;
153 static int sigmadsp_ctrl_get(struct snd_kcontrol *kcontrol,
154         struct snd_ctl_elem_value *ucontrol)
156         struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
157         struct sigmadsp *sigmadsp = snd_kcontrol_chip(kcontrol);
158         int ret = 0;
160         mutex_lock(&sigmadsp->lock);
162         if (!ctrl->cached) {
163                 ret = sigmadsp_read(sigmadsp, ctrl->addr, ctrl->cache,
164                         ctrl->num_bytes);
165         }
167         if (ret == 0) {
168                 ctrl->cached = true;
169                 memcpy(ucontrol->value.bytes.data, ctrl->cache,
170                         ctrl->num_bytes);
171         }
173         mutex_unlock(&sigmadsp->lock);
175         return ret;
178 static void sigmadsp_control_free(struct snd_kcontrol *kcontrol)
180         struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
182         ctrl->kcontrol = NULL;
185 static bool sigma_fw_validate_control_name(const char *name, unsigned int len)
187         unsigned int i;
189         for (i = 0; i < len; i++) {
190                 /* Normal ASCII characters are valid */
191                 if (name[i] < ' ' || name[i] > '~')
192                         return false;
193         }
195         return true;
198 static int sigma_fw_load_control(struct sigmadsp *sigmadsp,
199         const struct sigma_fw_chunk *chunk, unsigned int length)
201         const struct sigma_fw_chunk_control *ctrl_chunk;
202         struct sigmadsp_control *ctrl;
203         unsigned int num_bytes;
204         size_t name_len;
205         char *name;
206         int ret;
208         if (length <= sizeof(*ctrl_chunk))
209                 return -EINVAL;
211         ctrl_chunk = (const struct sigma_fw_chunk_control *)chunk;
213         name_len = length - sizeof(*ctrl_chunk);
214         if (name_len >= SNDRV_CTL_ELEM_ID_NAME_MAXLEN)
215                 name_len = SNDRV_CTL_ELEM_ID_NAME_MAXLEN - 1;
217         /* Make sure there are no non-displayable characaters in the string */
218         if (!sigma_fw_validate_control_name(ctrl_chunk->name, name_len))
219                 return -EINVAL;
221         num_bytes = le16_to_cpu(ctrl_chunk->num_bytes);
222         ctrl = kzalloc(sizeof(*ctrl) + num_bytes, GFP_KERNEL);
223         if (!ctrl)
224                 return -ENOMEM;
226         name = kzalloc(name_len + 1, GFP_KERNEL);
227         if (!name) {
228                 ret = -ENOMEM;
229                 goto err_free_ctrl;
230         }
231         memcpy(name, ctrl_chunk->name, name_len);
232         name[name_len] = '\0';
233         ctrl->name = name;
235         ctrl->addr = le16_to_cpu(ctrl_chunk->addr);
236         ctrl->num_bytes = num_bytes;
237         ctrl->samplerates = le32_to_cpu(chunk->samplerates);
239         list_add_tail(&ctrl->head, &sigmadsp->ctrl_list);
241         return 0;
243 err_free_ctrl:
244         kfree(ctrl);
246         return ret;
249 static int sigma_fw_load_data(struct sigmadsp *sigmadsp,
250         const struct sigma_fw_chunk *chunk, unsigned int length)
252         const struct sigma_fw_chunk_data *data_chunk;
253         struct sigmadsp_data *data;
255         if (length <= sizeof(*data_chunk))
256                 return -EINVAL;
258         data_chunk = (struct sigma_fw_chunk_data *)chunk;
260         length -= sizeof(*data_chunk);
262         data = kzalloc(sizeof(*data) + length, GFP_KERNEL);
263         if (!data)
264                 return -ENOMEM;
266         data->addr = le16_to_cpu(data_chunk->addr);
267         data->length = length;
268         data->samplerates = le32_to_cpu(chunk->samplerates);
269         memcpy(data->data, data_chunk->data, length);
270         list_add_tail(&data->head, &sigmadsp->data_list);
272         return 0;
275 static int sigma_fw_load_samplerates(struct sigmadsp *sigmadsp,
276         const struct sigma_fw_chunk *chunk, unsigned int length)
278         const struct sigma_fw_chunk_samplerate *rate_chunk;
279         unsigned int num_rates;
280         unsigned int *rates;
281         unsigned int i;
283         rate_chunk = (const struct sigma_fw_chunk_samplerate *)chunk;
285         num_rates = (length - sizeof(*rate_chunk)) / sizeof(__le32);
287         if (num_rates > 32 || num_rates == 0)
288                 return -EINVAL;
290         /* We only allow one samplerates block per file */
291         if (sigmadsp->rate_constraints.count)
292                 return -EINVAL;
294         rates = kcalloc(num_rates, sizeof(*rates), GFP_KERNEL);
295         if (!rates)
296                 return -ENOMEM;
298         for (i = 0; i < num_rates; i++)
299                 rates[i] = le32_to_cpu(rate_chunk->samplerates[i]);
301         sigmadsp->rate_constraints.count = num_rates;
302         sigmadsp->rate_constraints.list = rates;
304         return 0;
307 static int sigmadsp_fw_load_v2(struct sigmadsp *sigmadsp,
308         const struct firmware *fw)
310         struct sigma_fw_chunk *chunk;
311         unsigned int length, pos;
312         int ret;
314         /*
315          * Make sure that there is at least one chunk to avoid integer
316          * underflows later on. Empty firmware is still valid though.
317          */
318         if (fw->size < sizeof(*chunk) + sizeof(struct sigma_firmware_header))
319                 return 0;
321         pos = sizeof(struct sigma_firmware_header);
323         while (pos < fw->size - sizeof(*chunk)) {
324                 chunk = (struct sigma_fw_chunk *)(fw->data + pos);
326                 length = le32_to_cpu(chunk->length);
328                 if (length > fw->size - pos || length < sizeof(*chunk))
329                         return -EINVAL;
331                 switch (le32_to_cpu(chunk->tag)) {
332                 case SIGMA_FW_CHUNK_TYPE_DATA:
333                         ret = sigma_fw_load_data(sigmadsp, chunk, length);
334                         break;
335                 case SIGMA_FW_CHUNK_TYPE_CONTROL:
336                         ret = sigma_fw_load_control(sigmadsp, chunk, length);
337                         break;
338                 case SIGMA_FW_CHUNK_TYPE_SAMPLERATES:
339                         ret = sigma_fw_load_samplerates(sigmadsp, chunk, length);
340                         break;
341                 default:
342                         dev_warn(sigmadsp->dev, "Unknown chunk type: %d\n",
343                                 chunk->tag);
344                         ret = 0;
345                         break;
346                 }
348                 if (ret)
349                         return ret;
351                 /*
352                  * This can not overflow since if length is larger than the
353                  * maximum firmware size (0x4000000) we'll error out earilier.
354                  */
355                 pos += ALIGN(length, sizeof(__le32));
356         }
358         return 0;
361 static inline u32 sigma_action_len(struct sigma_action *sa)
363         return (sa->len_hi << 16) | le16_to_cpu(sa->len);
366 static size_t sigma_action_size(struct sigma_action *sa)
368         size_t payload = 0;
370         switch (sa->instr) {
371         case SIGMA_ACTION_WRITEXBYTES:
372         case SIGMA_ACTION_WRITESINGLE:
373         case SIGMA_ACTION_WRITESAFELOAD:
374                 payload = sigma_action_len(sa);
375                 break;
376         default:
377                 break;
378         }
380         payload = ALIGN(payload, 2);
382         return payload + sizeof(struct sigma_action);
385 /*
386  * Returns a negative error value in case of an error, 0 if processing of
387  * the firmware should be stopped after this action, 1 otherwise.
388  */
389 static int process_sigma_action(struct sigmadsp *sigmadsp,
390         struct sigma_action *sa)
392         size_t len = sigma_action_len(sa);
393         struct sigmadsp_data *data;
395         pr_debug("%s: instr:%i addr:%#x len:%zu\n", __func__,
396                 sa->instr, sa->addr, len);
398         switch (sa->instr) {
399         case SIGMA_ACTION_WRITEXBYTES:
400         case SIGMA_ACTION_WRITESINGLE:
401         case SIGMA_ACTION_WRITESAFELOAD:
402                 if (len < 3)
403                         return -EINVAL;
405                 data = kzalloc(sizeof(*data) + len - 2, GFP_KERNEL);
406                 if (!data)
407                         return -ENOMEM;
409                 data->addr = be16_to_cpu(sa->addr);
410                 data->length = len - 2;
411                 memcpy(data->data, sa->payload, data->length);
412                 list_add_tail(&data->head, &sigmadsp->data_list);
413                 break;
414         case SIGMA_ACTION_END:
415                 return 0;
416         default:
417                 return -EINVAL;
418         }
420         return 1;
423 static int sigmadsp_fw_load_v1(struct sigmadsp *sigmadsp,
424         const struct firmware *fw)
426         struct sigma_action *sa;
427         size_t size, pos;
428         int ret;
430         pos = sizeof(struct sigma_firmware_header);
432         while (pos + sizeof(*sa) <= fw->size) {
433                 sa = (struct sigma_action *)(fw->data + pos);
435                 size = sigma_action_size(sa);
436                 pos += size;
437                 if (pos > fw->size || size == 0)
438                         break;
440                 ret = process_sigma_action(sigmadsp, sa);
442                 pr_debug("%s: action returned %i\n", __func__, ret);
444                 if (ret <= 0)
445                         return ret;
446         }
448         if (pos != fw->size)
449                 return -EINVAL;
451         return 0;
454 static void sigmadsp_firmware_release(struct sigmadsp *sigmadsp)
456         struct sigmadsp_control *ctrl, *_ctrl;
457         struct sigmadsp_data *data, *_data;
459         list_for_each_entry_safe(ctrl, _ctrl, &sigmadsp->ctrl_list, head) {
460                 kfree(ctrl->name);
461                 kfree(ctrl);
462         }
464         list_for_each_entry_safe(data, _data, &sigmadsp->data_list, head)
465                 kfree(data);
467         INIT_LIST_HEAD(&sigmadsp->ctrl_list);
468         INIT_LIST_HEAD(&sigmadsp->data_list);
471 static void devm_sigmadsp_release(struct device *dev, void *res)
473         sigmadsp_firmware_release((struct sigmadsp *)res);
476 static int sigmadsp_firmware_load(struct sigmadsp *sigmadsp, const char *name)
478         const struct sigma_firmware_header *ssfw_head;
479         const struct firmware *fw;
480         int ret;
481         u32 crc;
483         /* first load the blob */
484         ret = request_firmware(&fw, name, sigmadsp->dev);
485         if (ret) {
486                 pr_debug("%s: request_firmware() failed with %i\n", __func__, ret);
487                 goto done;
488         }
490         /* then verify the header */
491         ret = -EINVAL;
493         /*
494          * Reject too small or unreasonable large files. The upper limit has been
495          * chosen a bit arbitrarily, but it should be enough for all practical
496          * purposes and having the limit makes it easier to avoid integer
497          * overflows later in the loading process.
498          */
499         if (fw->size < sizeof(*ssfw_head) || fw->size >= 0x4000000) {
500                 dev_err(sigmadsp->dev, "Failed to load firmware: Invalid size\n");
501                 goto done;
502         }
504         ssfw_head = (void *)fw->data;
505         if (memcmp(ssfw_head->magic, SIGMA_MAGIC, ARRAY_SIZE(ssfw_head->magic))) {
506                 dev_err(sigmadsp->dev, "Failed to load firmware: Invalid magic\n");
507                 goto done;
508         }
510         crc = crc32(0, fw->data + sizeof(*ssfw_head),
511                         fw->size - sizeof(*ssfw_head));
512         pr_debug("%s: crc=%x\n", __func__, crc);
513         if (crc != le32_to_cpu(ssfw_head->crc)) {
514                 dev_err(sigmadsp->dev, "Failed to load firmware: Wrong crc checksum: expected %x got %x\n",
515                         le32_to_cpu(ssfw_head->crc), crc);
516                 goto done;
517         }
519         switch (ssfw_head->version) {
520         case 1:
521                 ret = sigmadsp_fw_load_v1(sigmadsp, fw);
522                 break;
523         case 2:
524                 ret = sigmadsp_fw_load_v2(sigmadsp, fw);
525                 break;
526         default:
527                 dev_err(sigmadsp->dev,
528                         "Failed to load firmware: Invalid version %d. Supported firmware versions: 1, 2\n",
529                         ssfw_head->version);
530                 ret = -EINVAL;
531                 break;
532         }
534         if (ret)
535                 sigmadsp_firmware_release(sigmadsp);
537 done:
538         release_firmware(fw);
540         return ret;
543 static int sigmadsp_init(struct sigmadsp *sigmadsp, struct device *dev,
544         const struct sigmadsp_ops *ops, const char *firmware_name)
546         sigmadsp->ops = ops;
547         sigmadsp->dev = dev;
549         INIT_LIST_HEAD(&sigmadsp->ctrl_list);
550         INIT_LIST_HEAD(&sigmadsp->data_list);
551         mutex_init(&sigmadsp->lock);
553         return sigmadsp_firmware_load(sigmadsp, firmware_name);
556 /**
557  * devm_sigmadsp_init() - Initialize SigmaDSP instance
558  * @dev: The parent device
559  * @ops: The sigmadsp_ops to use for this instance
560  * @firmware_name: Name of the firmware file to load
561  *
562  * Allocates a SigmaDSP instance and loads the specified firmware file.
563  *
564  * Returns a pointer to a struct sigmadsp on success, or a PTR_ERR() on error.
565  */
566 struct sigmadsp *devm_sigmadsp_init(struct device *dev,
567         const struct sigmadsp_ops *ops, const char *firmware_name)
569         struct sigmadsp *sigmadsp;
570         int ret;
572         sigmadsp = devres_alloc(devm_sigmadsp_release, sizeof(*sigmadsp),
573                 GFP_KERNEL);
574         if (!sigmadsp)
575                 return ERR_PTR(-ENOMEM);
577         ret = sigmadsp_init(sigmadsp, dev, ops, firmware_name);
578         if (ret) {
579                 devres_free(sigmadsp);
580                 return ERR_PTR(ret);
581         }
583         devres_add(dev, sigmadsp);
585         return sigmadsp;
587 EXPORT_SYMBOL_GPL(devm_sigmadsp_init);
589 static int sigmadsp_rate_to_index(struct sigmadsp *sigmadsp, unsigned int rate)
591         unsigned int i;
593         for (i = 0; i < sigmadsp->rate_constraints.count; i++) {
594                 if (sigmadsp->rate_constraints.list[i] == rate)
595                         return i;
596         }
598         return -EINVAL;
601 static unsigned int sigmadsp_get_samplerate_mask(struct sigmadsp *sigmadsp,
602         unsigned int samplerate)
604         int samplerate_index;
606         if (samplerate == 0)
607                 return 0;
609         if (sigmadsp->rate_constraints.count) {
610                 samplerate_index = sigmadsp_rate_to_index(sigmadsp, samplerate);
611                 if (samplerate_index < 0)
612                         return 0;
614                 return BIT(samplerate_index);
615         } else {
616                 return ~0;
617         }
620 static bool sigmadsp_samplerate_valid(unsigned int supported,
621         unsigned int requested)
623         /* All samplerates are supported */
624         if (!supported)
625                 return true;
627         return supported & requested;
630 static int sigmadsp_alloc_control(struct sigmadsp *sigmadsp,
631         struct sigmadsp_control *ctrl, unsigned int samplerate_mask)
633         struct snd_kcontrol_new template;
634         struct snd_kcontrol *kcontrol;
636         memset(&template, 0, sizeof(template));
637         template.iface = SNDRV_CTL_ELEM_IFACE_MIXER;
638         template.name = ctrl->name;
639         template.info = sigmadsp_ctrl_info;
640         template.get = sigmadsp_ctrl_get;
641         template.put = sigmadsp_ctrl_put;
642         template.private_value = (unsigned long)ctrl;
643         template.access = SNDRV_CTL_ELEM_ACCESS_READWRITE;
644         if (!sigmadsp_samplerate_valid(ctrl->samplerates, samplerate_mask))
645                 template.access |= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
647         kcontrol = snd_ctl_new1(&template, sigmadsp);
648         if (!kcontrol)
649                 return -ENOMEM;
651         kcontrol->private_free = sigmadsp_control_free;
652         ctrl->kcontrol = kcontrol;
654         return snd_ctl_add(sigmadsp->component->card->snd_card, kcontrol);
657 static void sigmadsp_activate_ctrl(struct sigmadsp *sigmadsp,
658         struct sigmadsp_control *ctrl, unsigned int samplerate_mask)
660         struct snd_card *card = sigmadsp->component->card->snd_card;
661         struct snd_kcontrol_volatile *vd;
662         struct snd_ctl_elem_id id;
663         bool active;
664         bool changed = false;
666         active = sigmadsp_samplerate_valid(ctrl->samplerates, samplerate_mask);
668         down_write(&card->controls_rwsem);
669         if (!ctrl->kcontrol) {
670                 up_write(&card->controls_rwsem);
671                 return;
672         }
674         id = ctrl->kcontrol->id;
675         vd = &ctrl->kcontrol->vd[0];
676         if (active == (bool)(vd->access & SNDRV_CTL_ELEM_ACCESS_INACTIVE)) {
677                 vd->access ^= SNDRV_CTL_ELEM_ACCESS_INACTIVE;
678                 changed = true;
679         }
680         up_write(&card->controls_rwsem);
682         if (active && changed) {
683                 mutex_lock(&sigmadsp->lock);
684                 if (ctrl->cached)
685                         sigmadsp_ctrl_write(sigmadsp, ctrl, ctrl->cache);
686                 mutex_unlock(&sigmadsp->lock);
687         }
689         if (changed)
690                 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, &id);
693 /**
694  * sigmadsp_attach() - Attach a sigmadsp instance to a ASoC component
695  * @sigmadsp: The sigmadsp instance to attach
696  * @component: The component to attach to
697  *
698  * Typically called in the components probe callback.
699  *
700  * Note, once this function has been called the firmware must not be released
701  * until after the ALSA snd_card that the component belongs to has been
702  * disconnected, even if sigmadsp_attach() returns an error.
703  */
704 int sigmadsp_attach(struct sigmadsp *sigmadsp,
705         struct snd_soc_component *component)
707         struct sigmadsp_control *ctrl;
708         unsigned int samplerate_mask;
709         int ret;
711         sigmadsp->component = component;
713         samplerate_mask = sigmadsp_get_samplerate_mask(sigmadsp,
714                 sigmadsp->current_samplerate);
716         list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head) {
717                 ret = sigmadsp_alloc_control(sigmadsp, ctrl, samplerate_mask);
718                 if (ret)
719                         return ret;
720         }
722         return 0;
724 EXPORT_SYMBOL_GPL(sigmadsp_attach);
726 /**
727  * sigmadsp_setup() - Setup the DSP for the specified samplerate
728  * @sigmadsp: The sigmadsp instance to configure
729  * @samplerate: The samplerate the DSP should be configured for
730  *
731  * Loads the appropriate firmware program and parameter memory (if not already
732  * loaded) and enables the controls for the specified samplerate. Any control
733  * parameter changes that have been made previously will be restored.
734  *
735  * Returns 0 on success, a negative error code otherwise.
736  */
737 int sigmadsp_setup(struct sigmadsp *sigmadsp, unsigned int samplerate)
739         struct sigmadsp_control *ctrl;
740         unsigned int samplerate_mask;
741         struct sigmadsp_data *data;
742         int ret;
744         if (sigmadsp->current_samplerate == samplerate)
745                 return 0;
747         samplerate_mask = sigmadsp_get_samplerate_mask(sigmadsp, samplerate);
748         if (samplerate_mask == 0)
749                 return -EINVAL;
751         list_for_each_entry(data, &sigmadsp->data_list, head) {
752                 if (!sigmadsp_samplerate_valid(data->samplerates,
753                     samplerate_mask))
754                         continue;
755                 ret = sigmadsp_write(sigmadsp, data->addr, data->data,
756                         data->length);
757                 if (ret)
758                         goto err;
759         }
761         list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head)
762                 sigmadsp_activate_ctrl(sigmadsp, ctrl, samplerate_mask);
764         sigmadsp->current_samplerate = samplerate;
766         return 0;
767 err:
768         sigmadsp_reset(sigmadsp);
770         return ret;
772 EXPORT_SYMBOL_GPL(sigmadsp_setup);
774 /**
775  * sigmadsp_reset() - Notify the sigmadsp instance that the DSP has been reset
776  * @sigmadsp: The sigmadsp instance to reset
777  *
778  * Should be called whenever the DSP has been reset and parameter and program
779  * memory need to be re-loaded.
780  */
781 void sigmadsp_reset(struct sigmadsp *sigmadsp)
783         struct sigmadsp_control *ctrl;
785         list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head)
786                 sigmadsp_activate_ctrl(sigmadsp, ctrl, false);
788         sigmadsp->current_samplerate = 0;
790 EXPORT_SYMBOL_GPL(sigmadsp_reset);
792 /**
793  * sigmadsp_restrict_params() - Applies DSP firmware specific constraints
794  * @sigmadsp: The sigmadsp instance
795  * @substream: The substream to restrict
796  *
797  * Applies samplerate constraints that may be required by the firmware Should
798  * typically be called from the CODEC/component drivers startup callback.
799  *
800  * Returns 0 on success, a negative error code otherwise.
801  */
802 int sigmadsp_restrict_params(struct sigmadsp *sigmadsp,
803         struct snd_pcm_substream *substream)
805         if (sigmadsp->rate_constraints.count == 0)
806                 return 0;
808         return snd_pcm_hw_constraint_list(substream->runtime, 0,
809                 SNDRV_PCM_HW_PARAM_RATE, &sigmadsp->rate_constraints);
811 EXPORT_SYMBOL_GPL(sigmadsp_restrict_params);
813 MODULE_LICENSE("GPL");