1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Load Analog Devices SigmaStudio firmware files
5 * Copyright 2009-2014 Analog Devices Inc.
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>
16 #include <sound/control.h>
17 #include <sound/soc.h>
21 #define SIGMA_MAGIC "ADISIGM"
23 #define SIGMA_FW_CHUNK_TYPE_DATA 0
24 #define SIGMA_FW_CHUNK_TYPE_CONTROL 1
25 #define SIGMA_FW_CHUNK_TYPE_SAMPLERATES 2
27 #define READBACK_CTRL_NAME "ReadBack"
29 struct sigmadsp_control {
30 struct list_head head;
33 unsigned int num_bytes;
35 struct snd_kcontrol *kcontrol;
41 struct sigmadsp_data {
42 struct list_head head;
49 struct sigma_fw_chunk {
55 struct sigma_fw_chunk_data {
56 struct sigma_fw_chunk chunk;
61 struct sigma_fw_chunk_control {
62 struct sigma_fw_chunk chunk;
69 struct sigma_fw_chunk_samplerate {
70 struct sigma_fw_chunk chunk;
74 struct sigma_firmware_header {
75 unsigned char magic[7];
81 SIGMA_ACTION_WRITEXBYTES = 0,
82 SIGMA_ACTION_WRITESINGLE,
83 SIGMA_ACTION_WRITESAFELOAD,
92 unsigned char payload[];
95 static int sigmadsp_write(struct sigmadsp *sigmadsp, unsigned int addr,
96 const uint8_t data[], size_t len)
98 return sigmadsp->write(sigmadsp->control_data, addr, data, len);
101 static int sigmadsp_read(struct sigmadsp *sigmadsp, unsigned int addr,
102 uint8_t data[], size_t len)
104 return sigmadsp->read(sigmadsp->control_data, addr, data, len);
107 static int sigmadsp_ctrl_info(struct snd_kcontrol *kcontrol,
108 struct snd_ctl_elem_info *info)
110 struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
112 info->type = SNDRV_CTL_ELEM_TYPE_BYTES;
113 info->count = ctrl->num_bytes;
118 static int sigmadsp_ctrl_write(struct sigmadsp *sigmadsp,
119 struct sigmadsp_control *ctrl, void *data)
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,
126 return sigmadsp_write(sigmadsp, ctrl->addr, data,
130 static int sigmadsp_ctrl_put(struct snd_kcontrol *kcontrol,
131 struct snd_ctl_elem_value *ucontrol)
133 struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
134 struct sigmadsp *sigmadsp = snd_kcontrol_chip(kcontrol);
138 mutex_lock(&sigmadsp->lock);
140 data = ucontrol->value.bytes.data;
142 if (!(kcontrol->vd[0].access & SNDRV_CTL_ELEM_ACCESS_INACTIVE))
143 ret = sigmadsp_ctrl_write(sigmadsp, ctrl, data);
146 memcpy(ctrl->cache, data, ctrl->num_bytes);
147 if (!ctrl->is_readback)
151 mutex_unlock(&sigmadsp->lock);
156 static int sigmadsp_ctrl_get(struct snd_kcontrol *kcontrol,
157 struct snd_ctl_elem_value *ucontrol)
159 struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
160 struct sigmadsp *sigmadsp = snd_kcontrol_chip(kcontrol);
163 mutex_lock(&sigmadsp->lock);
166 ret = sigmadsp_read(sigmadsp, ctrl->addr, ctrl->cache,
171 if (!ctrl->is_readback)
173 memcpy(ucontrol->value.bytes.data, ctrl->cache,
177 mutex_unlock(&sigmadsp->lock);
182 static void sigmadsp_control_free(struct snd_kcontrol *kcontrol)
184 struct sigmadsp_control *ctrl = (void *)kcontrol->private_value;
186 ctrl->kcontrol = NULL;
189 static bool sigma_fw_validate_control_name(const char *name, unsigned int len)
193 for (i = 0; i < len; i++) {
194 /* Normal ASCII characters are valid */
195 if (name[i] < ' ' || name[i] > '~')
202 static int sigma_fw_load_control(struct sigmadsp *sigmadsp,
203 const struct sigma_fw_chunk *chunk, unsigned int length)
205 const struct sigma_fw_chunk_control *ctrl_chunk;
206 struct sigmadsp_control *ctrl;
207 unsigned int num_bytes;
212 if (length <= sizeof(*ctrl_chunk))
215 ctrl_chunk = (const struct sigma_fw_chunk_control *)chunk;
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;
221 /* Make sure there are no non-displayable characaters in the string */
222 if (!sigma_fw_validate_control_name(ctrl_chunk->name, name_len))
225 num_bytes = le16_to_cpu(ctrl_chunk->num_bytes);
226 ctrl = kzalloc(sizeof(*ctrl) + num_bytes, GFP_KERNEL);
230 name = kzalloc(name_len + 1, GFP_KERNEL);
235 memcpy(name, ctrl_chunk->name, name_len);
236 name[name_len] = '\0';
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.
244 if (ctrl->name && strncmp(ctrl->name, READBACK_CTRL_NAME,
245 (sizeof(READBACK_CTRL_NAME) - 1)) == 0)
246 ctrl->is_readback = true;
248 ctrl->addr = le16_to_cpu(ctrl_chunk->addr);
249 ctrl->num_bytes = num_bytes;
250 ctrl->samplerates = le32_to_cpu(chunk->samplerates);
252 list_add_tail(&ctrl->head, &sigmadsp->ctrl_list);
262 static int sigma_fw_load_data(struct sigmadsp *sigmadsp,
263 const struct sigma_fw_chunk *chunk, unsigned int length)
265 const struct sigma_fw_chunk_data *data_chunk;
266 struct sigmadsp_data *data;
268 if (length <= sizeof(*data_chunk))
271 data_chunk = (struct sigma_fw_chunk_data *)chunk;
273 length -= sizeof(*data_chunk);
275 data = kzalloc(sizeof(*data) + length, GFP_KERNEL);
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);
288 static int sigma_fw_load_samplerates(struct sigmadsp *sigmadsp,
289 const struct sigma_fw_chunk *chunk, unsigned int length)
291 const struct sigma_fw_chunk_samplerate *rate_chunk;
292 unsigned int num_rates;
296 rate_chunk = (const struct sigma_fw_chunk_samplerate *)chunk;
298 num_rates = (length - sizeof(*rate_chunk)) / sizeof(__le32);
300 if (num_rates > 32 || num_rates == 0)
303 /* We only allow one samplerates block per file */
304 if (sigmadsp->rate_constraints.count)
307 rates = kcalloc(num_rates, sizeof(*rates), GFP_KERNEL);
311 for (i = 0; i < num_rates; i++)
312 rates[i] = le32_to_cpu(rate_chunk->samplerates[i]);
314 sigmadsp->rate_constraints.count = num_rates;
315 sigmadsp->rate_constraints.list = rates;
320 static int sigmadsp_fw_load_v2(struct sigmadsp *sigmadsp,
321 const struct firmware *fw)
323 struct sigma_fw_chunk *chunk;
324 unsigned int length, pos;
328 * Make sure that there is at least one chunk to avoid integer
329 * underflows later on. Empty firmware is still valid though.
331 if (fw->size < sizeof(*chunk) + sizeof(struct sigma_firmware_header))
334 pos = sizeof(struct sigma_firmware_header);
336 while (pos < fw->size - sizeof(*chunk)) {
337 chunk = (struct sigma_fw_chunk *)(fw->data + pos);
339 length = le32_to_cpu(chunk->length);
341 if (length > fw->size - pos || length < sizeof(*chunk))
344 switch (le32_to_cpu(chunk->tag)) {
345 case SIGMA_FW_CHUNK_TYPE_DATA:
346 ret = sigma_fw_load_data(sigmadsp, chunk, length);
348 case SIGMA_FW_CHUNK_TYPE_CONTROL:
349 ret = sigma_fw_load_control(sigmadsp, chunk, length);
351 case SIGMA_FW_CHUNK_TYPE_SAMPLERATES:
352 ret = sigma_fw_load_samplerates(sigmadsp, chunk, length);
355 dev_warn(sigmadsp->dev, "Unknown chunk type: %d\n",
365 * This can not overflow since if length is larger than the
366 * maximum firmware size (0x4000000) we'll error out earilier.
368 pos += ALIGN(length, sizeof(__le32));
374 static inline u32 sigma_action_len(struct sigma_action *sa)
376 return (sa->len_hi << 16) | le16_to_cpu(sa->len);
379 static size_t sigma_action_size(struct sigma_action *sa)
384 case SIGMA_ACTION_WRITEXBYTES:
385 case SIGMA_ACTION_WRITESINGLE:
386 case SIGMA_ACTION_WRITESAFELOAD:
387 payload = sigma_action_len(sa);
393 payload = ALIGN(payload, 2);
395 return payload + sizeof(struct sigma_action);
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.
402 static int process_sigma_action(struct sigmadsp *sigmadsp,
403 struct sigma_action *sa)
405 size_t len = sigma_action_len(sa);
406 struct sigmadsp_data *data;
408 pr_debug("%s: instr:%i addr:%#x len:%zu\n", __func__,
409 sa->instr, sa->addr, len);
412 case SIGMA_ACTION_WRITEXBYTES:
413 case SIGMA_ACTION_WRITESINGLE:
414 case SIGMA_ACTION_WRITESAFELOAD:
418 data = kzalloc(sizeof(*data) + len - 2, GFP_KERNEL);
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);
427 case SIGMA_ACTION_END:
436 static int sigmadsp_fw_load_v1(struct sigmadsp *sigmadsp,
437 const struct firmware *fw)
439 struct sigma_action *sa;
443 pos = sizeof(struct sigma_firmware_header);
445 while (pos + sizeof(*sa) <= fw->size) {
446 sa = (struct sigma_action *)(fw->data + pos);
448 size = sigma_action_size(sa);
450 if (pos > fw->size || size == 0)
453 ret = process_sigma_action(sigmadsp, sa);
455 pr_debug("%s: action returned %i\n", __func__, ret);
467 static void sigmadsp_firmware_release(struct sigmadsp *sigmadsp)
469 struct sigmadsp_control *ctrl, *_ctrl;
470 struct sigmadsp_data *data, *_data;
472 list_for_each_entry_safe(ctrl, _ctrl, &sigmadsp->ctrl_list, head) {
477 list_for_each_entry_safe(data, _data, &sigmadsp->data_list, head)
480 INIT_LIST_HEAD(&sigmadsp->ctrl_list);
481 INIT_LIST_HEAD(&sigmadsp->data_list);
484 static void devm_sigmadsp_release(struct device *dev, void *res)
486 sigmadsp_firmware_release((struct sigmadsp *)res);
489 static int sigmadsp_firmware_load(struct sigmadsp *sigmadsp, const char *name)
491 const struct sigma_firmware_header *ssfw_head;
492 const struct firmware *fw;
496 /* first load the blob */
497 ret = request_firmware(&fw, name, sigmadsp->dev);
499 pr_debug("%s: request_firmware() failed with %i\n", __func__, ret);
503 /* then verify the header */
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.
512 if (fw->size < sizeof(*ssfw_head) || fw->size >= 0x4000000) {
513 dev_err(sigmadsp->dev, "Failed to load firmware: Invalid size\n");
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");
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);
532 switch (ssfw_head->version) {
534 ret = sigmadsp_fw_load_v1(sigmadsp, fw);
537 ret = sigmadsp_fw_load_v2(sigmadsp, fw);
540 dev_err(sigmadsp->dev,
541 "Failed to load firmware: Invalid version %d. Supported firmware versions: 1, 2\n",
548 sigmadsp_firmware_release(sigmadsp);
551 release_firmware(fw);
556 static int sigmadsp_init(struct sigmadsp *sigmadsp, struct device *dev,
557 const struct sigmadsp_ops *ops, const char *firmware_name)
562 INIT_LIST_HEAD(&sigmadsp->ctrl_list);
563 INIT_LIST_HEAD(&sigmadsp->data_list);
564 mutex_init(&sigmadsp->lock);
566 return sigmadsp_firmware_load(sigmadsp, firmware_name);
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
575 * Allocates a SigmaDSP instance and loads the specified firmware file.
577 * Returns a pointer to a struct sigmadsp on success, or a PTR_ERR() on error.
579 struct sigmadsp *devm_sigmadsp_init(struct device *dev,
580 const struct sigmadsp_ops *ops, const char *firmware_name)
582 struct sigmadsp *sigmadsp;
585 sigmadsp = devres_alloc(devm_sigmadsp_release, sizeof(*sigmadsp),
588 return ERR_PTR(-ENOMEM);
590 ret = sigmadsp_init(sigmadsp, dev, ops, firmware_name);
592 devres_free(sigmadsp);
596 devres_add(dev, sigmadsp);
600 EXPORT_SYMBOL_GPL(devm_sigmadsp_init);
602 static int sigmadsp_rate_to_index(struct sigmadsp *sigmadsp, unsigned int rate)
606 for (i = 0; i < sigmadsp->rate_constraints.count; i++) {
607 if (sigmadsp->rate_constraints.list[i] == rate)
614 static unsigned int sigmadsp_get_samplerate_mask(struct sigmadsp *sigmadsp,
615 unsigned int samplerate)
617 int samplerate_index;
622 if (sigmadsp->rate_constraints.count) {
623 samplerate_index = sigmadsp_rate_to_index(sigmadsp, samplerate);
624 if (samplerate_index < 0)
627 return BIT(samplerate_index);
633 static bool sigmadsp_samplerate_valid(unsigned int supported,
634 unsigned int requested)
636 /* All samplerates are supported */
640 return supported & requested;
643 static int sigmadsp_alloc_control(struct sigmadsp *sigmadsp,
644 struct sigmadsp_control *ctrl, unsigned int samplerate_mask)
646 struct snd_kcontrol_new template;
647 struct snd_kcontrol *kcontrol;
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;
660 kcontrol = snd_ctl_new1(&template, sigmadsp);
664 kcontrol->private_free = sigmadsp_control_free;
665 ctrl->kcontrol = kcontrol;
667 return snd_ctl_add(sigmadsp->component->card->snd_card, kcontrol);
670 static void sigmadsp_activate_ctrl(struct sigmadsp *sigmadsp,
671 struct sigmadsp_control *ctrl, unsigned int samplerate_mask)
673 struct snd_card *card = sigmadsp->component->card->snd_card;
674 struct snd_kcontrol_volatile *vd;
675 struct snd_ctl_elem_id id;
677 bool changed = false;
679 active = sigmadsp_samplerate_valid(ctrl->samplerates, samplerate_mask);
681 down_write(&card->controls_rwsem);
682 if (!ctrl->kcontrol) {
683 up_write(&card->controls_rwsem);
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;
693 up_write(&card->controls_rwsem);
695 if (active && changed) {
696 mutex_lock(&sigmadsp->lock);
698 sigmadsp_ctrl_write(sigmadsp, ctrl, ctrl->cache);
699 mutex_unlock(&sigmadsp->lock);
703 snd_ctl_notify(card, SNDRV_CTL_EVENT_MASK_INFO, &id);
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
711 * Typically called in the components probe callback.
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.
717 int sigmadsp_attach(struct sigmadsp *sigmadsp,
718 struct snd_soc_component *component)
720 struct sigmadsp_control *ctrl;
721 unsigned int samplerate_mask;
724 sigmadsp->component = component;
726 samplerate_mask = sigmadsp_get_samplerate_mask(sigmadsp,
727 sigmadsp->current_samplerate);
729 list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head) {
730 ret = sigmadsp_alloc_control(sigmadsp, ctrl, samplerate_mask);
737 EXPORT_SYMBOL_GPL(sigmadsp_attach);
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
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.
748 * Returns 0 on success, a negative error code otherwise.
750 int sigmadsp_setup(struct sigmadsp *sigmadsp, unsigned int samplerate)
752 struct sigmadsp_control *ctrl;
753 unsigned int samplerate_mask;
754 struct sigmadsp_data *data;
757 if (sigmadsp->current_samplerate == samplerate)
760 samplerate_mask = sigmadsp_get_samplerate_mask(sigmadsp, samplerate);
761 if (samplerate_mask == 0)
764 list_for_each_entry(data, &sigmadsp->data_list, head) {
765 if (!sigmadsp_samplerate_valid(data->samplerates,
768 ret = sigmadsp_write(sigmadsp, data->addr, data->data,
774 list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head)
775 sigmadsp_activate_ctrl(sigmadsp, ctrl, samplerate_mask);
777 sigmadsp->current_samplerate = samplerate;
781 sigmadsp_reset(sigmadsp);
785 EXPORT_SYMBOL_GPL(sigmadsp_setup);
788 * sigmadsp_reset() - Notify the sigmadsp instance that the DSP has been reset
789 * @sigmadsp: The sigmadsp instance to reset
791 * Should be called whenever the DSP has been reset and parameter and program
792 * memory need to be re-loaded.
794 void sigmadsp_reset(struct sigmadsp *sigmadsp)
796 struct sigmadsp_control *ctrl;
798 list_for_each_entry(ctrl, &sigmadsp->ctrl_list, head)
799 sigmadsp_activate_ctrl(sigmadsp, ctrl, false);
801 sigmadsp->current_samplerate = 0;
803 EXPORT_SYMBOL_GPL(sigmadsp_reset);
806 * sigmadsp_restrict_params() - Applies DSP firmware specific constraints
807 * @sigmadsp: The sigmadsp instance
808 * @substream: The substream to restrict
810 * Applies samplerate constraints that may be required by the firmware Should
811 * typically be called from the CODEC/component drivers startup callback.
813 * Returns 0 on success, a negative error code otherwise.
815 int sigmadsp_restrict_params(struct sigmadsp *sigmadsp,
816 struct snd_pcm_substream *substream)
818 if (sigmadsp->rate_constraints.count == 0)
821 return snd_pcm_hw_constraint_list(substream->runtime, 0,
822 SNDRV_PCM_HW_PARAM_RATE, &sigmadsp->rate_constraints);
824 EXPORT_SYMBOL_GPL(sigmadsp_restrict_params);
826 MODULE_LICENSE("GPL");