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 struct sigmadsp_control
{
28 struct list_head head
;
31 unsigned int num_bytes
;
33 struct snd_kcontrol
*kcontrol
;
38 struct sigmadsp_data
{
39 struct list_head head
;
46 struct sigma_fw_chunk
{
52 struct sigma_fw_chunk_data
{
53 struct sigma_fw_chunk chunk
;
58 struct sigma_fw_chunk_control
{
59 struct sigma_fw_chunk chunk
;
66 struct sigma_fw_chunk_samplerate
{
67 struct sigma_fw_chunk chunk
;
71 struct sigma_firmware_header
{
72 unsigned char magic
[7];
78 SIGMA_ACTION_WRITEXBYTES
= 0,
79 SIGMA_ACTION_WRITESINGLE
,
80 SIGMA_ACTION_WRITESAFELOAD
,
89 unsigned char payload
[];
92 static int sigmadsp_write(struct sigmadsp
*sigmadsp
, unsigned int addr
,
93 const uint8_t data
[], size_t len
)
95 return sigmadsp
->write(sigmadsp
->control_data
, addr
, data
, len
);
98 static int sigmadsp_read(struct sigmadsp
*sigmadsp
, unsigned int addr
,
99 uint8_t data
[], size_t len
)
101 return sigmadsp
->read(sigmadsp
->control_data
, addr
, data
, len
);
104 static int sigmadsp_ctrl_info(struct snd_kcontrol
*kcontrol
,
105 struct snd_ctl_elem_info
*info
)
107 struct sigmadsp_control
*ctrl
= (void *)kcontrol
->private_value
;
109 info
->type
= SNDRV_CTL_ELEM_TYPE_BYTES
;
110 info
->count
= ctrl
->num_bytes
;
115 static int sigmadsp_ctrl_write(struct sigmadsp
*sigmadsp
,
116 struct sigmadsp_control
*ctrl
, void *data
)
118 /* safeload loads up to 20 bytes in a atomic operation */
119 if (ctrl
->num_bytes
<= 20 && sigmadsp
->ops
&& sigmadsp
->ops
->safeload
)
120 return sigmadsp
->ops
->safeload(sigmadsp
, ctrl
->addr
, data
,
123 return sigmadsp_write(sigmadsp
, ctrl
->addr
, data
,
127 static int sigmadsp_ctrl_put(struct snd_kcontrol
*kcontrol
,
128 struct snd_ctl_elem_value
*ucontrol
)
130 struct sigmadsp_control
*ctrl
= (void *)kcontrol
->private_value
;
131 struct sigmadsp
*sigmadsp
= snd_kcontrol_chip(kcontrol
);
135 mutex_lock(&sigmadsp
->lock
);
137 data
= ucontrol
->value
.bytes
.data
;
139 if (!(kcontrol
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_INACTIVE
))
140 ret
= sigmadsp_ctrl_write(sigmadsp
, ctrl
, data
);
143 memcpy(ctrl
->cache
, data
, ctrl
->num_bytes
);
147 mutex_unlock(&sigmadsp
->lock
);
152 static int sigmadsp_ctrl_get(struct snd_kcontrol
*kcontrol
,
153 struct snd_ctl_elem_value
*ucontrol
)
155 struct sigmadsp_control
*ctrl
= (void *)kcontrol
->private_value
;
156 struct sigmadsp
*sigmadsp
= snd_kcontrol_chip(kcontrol
);
159 mutex_lock(&sigmadsp
->lock
);
162 ret
= sigmadsp_read(sigmadsp
, ctrl
->addr
, ctrl
->cache
,
168 memcpy(ucontrol
->value
.bytes
.data
, ctrl
->cache
,
172 mutex_unlock(&sigmadsp
->lock
);
177 static void sigmadsp_control_free(struct snd_kcontrol
*kcontrol
)
179 struct sigmadsp_control
*ctrl
= (void *)kcontrol
->private_value
;
181 ctrl
->kcontrol
= NULL
;
184 static bool sigma_fw_validate_control_name(const char *name
, unsigned int len
)
188 for (i
= 0; i
< len
; i
++) {
189 /* Normal ASCII characters are valid */
190 if (name
[i
] < ' ' || name
[i
] > '~')
197 static int sigma_fw_load_control(struct sigmadsp
*sigmadsp
,
198 const struct sigma_fw_chunk
*chunk
, unsigned int length
)
200 const struct sigma_fw_chunk_control
*ctrl_chunk
;
201 struct sigmadsp_control
*ctrl
;
202 unsigned int num_bytes
;
207 if (length
<= sizeof(*ctrl_chunk
))
210 ctrl_chunk
= (const struct sigma_fw_chunk_control
*)chunk
;
212 name_len
= length
- sizeof(*ctrl_chunk
);
213 if (name_len
>= SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
214 name_len
= SNDRV_CTL_ELEM_ID_NAME_MAXLEN
- 1;
216 /* Make sure there are no non-displayable characaters in the string */
217 if (!sigma_fw_validate_control_name(ctrl_chunk
->name
, name_len
))
220 num_bytes
= le16_to_cpu(ctrl_chunk
->num_bytes
);
221 ctrl
= kzalloc(sizeof(*ctrl
) + num_bytes
, GFP_KERNEL
);
225 name
= kzalloc(name_len
+ 1, GFP_KERNEL
);
230 memcpy(name
, ctrl_chunk
->name
, name_len
);
231 name
[name_len
] = '\0';
234 ctrl
->addr
= le16_to_cpu(ctrl_chunk
->addr
);
235 ctrl
->num_bytes
= num_bytes
;
236 ctrl
->samplerates
= le32_to_cpu(chunk
->samplerates
);
238 list_add_tail(&ctrl
->head
, &sigmadsp
->ctrl_list
);
248 static int sigma_fw_load_data(struct sigmadsp
*sigmadsp
,
249 const struct sigma_fw_chunk
*chunk
, unsigned int length
)
251 const struct sigma_fw_chunk_data
*data_chunk
;
252 struct sigmadsp_data
*data
;
254 if (length
<= sizeof(*data_chunk
))
257 data_chunk
= (struct sigma_fw_chunk_data
*)chunk
;
259 length
-= sizeof(*data_chunk
);
261 data
= kzalloc(sizeof(*data
) + length
, GFP_KERNEL
);
265 data
->addr
= le16_to_cpu(data_chunk
->addr
);
266 data
->length
= length
;
267 data
->samplerates
= le32_to_cpu(chunk
->samplerates
);
268 memcpy(data
->data
, data_chunk
->data
, length
);
269 list_add_tail(&data
->head
, &sigmadsp
->data_list
);
274 static int sigma_fw_load_samplerates(struct sigmadsp
*sigmadsp
,
275 const struct sigma_fw_chunk
*chunk
, unsigned int length
)
277 const struct sigma_fw_chunk_samplerate
*rate_chunk
;
278 unsigned int num_rates
;
282 rate_chunk
= (const struct sigma_fw_chunk_samplerate
*)chunk
;
284 num_rates
= (length
- sizeof(*rate_chunk
)) / sizeof(__le32
);
286 if (num_rates
> 32 || num_rates
== 0)
289 /* We only allow one samplerates block per file */
290 if (sigmadsp
->rate_constraints
.count
)
293 rates
= kcalloc(num_rates
, sizeof(*rates
), GFP_KERNEL
);
297 for (i
= 0; i
< num_rates
; i
++)
298 rates
[i
] = le32_to_cpu(rate_chunk
->samplerates
[i
]);
300 sigmadsp
->rate_constraints
.count
= num_rates
;
301 sigmadsp
->rate_constraints
.list
= rates
;
306 static int sigmadsp_fw_load_v2(struct sigmadsp
*sigmadsp
,
307 const struct firmware
*fw
)
309 struct sigma_fw_chunk
*chunk
;
310 unsigned int length
, pos
;
314 * Make sure that there is at least one chunk to avoid integer
315 * underflows later on. Empty firmware is still valid though.
317 if (fw
->size
< sizeof(*chunk
) + sizeof(struct sigma_firmware_header
))
320 pos
= sizeof(struct sigma_firmware_header
);
322 while (pos
< fw
->size
- sizeof(*chunk
)) {
323 chunk
= (struct sigma_fw_chunk
*)(fw
->data
+ pos
);
325 length
= le32_to_cpu(chunk
->length
);
327 if (length
> fw
->size
- pos
|| length
< sizeof(*chunk
))
330 switch (le32_to_cpu(chunk
->tag
)) {
331 case SIGMA_FW_CHUNK_TYPE_DATA
:
332 ret
= sigma_fw_load_data(sigmadsp
, chunk
, length
);
334 case SIGMA_FW_CHUNK_TYPE_CONTROL
:
335 ret
= sigma_fw_load_control(sigmadsp
, chunk
, length
);
337 case SIGMA_FW_CHUNK_TYPE_SAMPLERATES
:
338 ret
= sigma_fw_load_samplerates(sigmadsp
, chunk
, length
);
341 dev_warn(sigmadsp
->dev
, "Unknown chunk type: %d\n",
351 * This can not overflow since if length is larger than the
352 * maximum firmware size (0x4000000) we'll error out earilier.
354 pos
+= ALIGN(length
, sizeof(__le32
));
360 static inline u32
sigma_action_len(struct sigma_action
*sa
)
362 return (sa
->len_hi
<< 16) | le16_to_cpu(sa
->len
);
365 static size_t sigma_action_size(struct sigma_action
*sa
)
370 case SIGMA_ACTION_WRITEXBYTES
:
371 case SIGMA_ACTION_WRITESINGLE
:
372 case SIGMA_ACTION_WRITESAFELOAD
:
373 payload
= sigma_action_len(sa
);
379 payload
= ALIGN(payload
, 2);
381 return payload
+ sizeof(struct sigma_action
);
385 * Returns a negative error value in case of an error, 0 if processing of
386 * the firmware should be stopped after this action, 1 otherwise.
388 static int process_sigma_action(struct sigmadsp
*sigmadsp
,
389 struct sigma_action
*sa
)
391 size_t len
= sigma_action_len(sa
);
392 struct sigmadsp_data
*data
;
394 pr_debug("%s: instr:%i addr:%#x len:%zu\n", __func__
,
395 sa
->instr
, sa
->addr
, len
);
398 case SIGMA_ACTION_WRITEXBYTES
:
399 case SIGMA_ACTION_WRITESINGLE
:
400 case SIGMA_ACTION_WRITESAFELOAD
:
404 data
= kzalloc(sizeof(*data
) + len
- 2, GFP_KERNEL
);
408 data
->addr
= be16_to_cpu(sa
->addr
);
409 data
->length
= len
- 2;
410 memcpy(data
->data
, sa
->payload
, data
->length
);
411 list_add_tail(&data
->head
, &sigmadsp
->data_list
);
413 case SIGMA_ACTION_END
:
422 static int sigmadsp_fw_load_v1(struct sigmadsp
*sigmadsp
,
423 const struct firmware
*fw
)
425 struct sigma_action
*sa
;
429 pos
= sizeof(struct sigma_firmware_header
);
431 while (pos
+ sizeof(*sa
) <= fw
->size
) {
432 sa
= (struct sigma_action
*)(fw
->data
+ pos
);
434 size
= sigma_action_size(sa
);
436 if (pos
> fw
->size
|| size
== 0)
439 ret
= process_sigma_action(sigmadsp
, sa
);
441 pr_debug("%s: action returned %i\n", __func__
, ret
);
453 static void sigmadsp_firmware_release(struct sigmadsp
*sigmadsp
)
455 struct sigmadsp_control
*ctrl
, *_ctrl
;
456 struct sigmadsp_data
*data
, *_data
;
458 list_for_each_entry_safe(ctrl
, _ctrl
, &sigmadsp
->ctrl_list
, head
) {
463 list_for_each_entry_safe(data
, _data
, &sigmadsp
->data_list
, head
)
466 INIT_LIST_HEAD(&sigmadsp
->ctrl_list
);
467 INIT_LIST_HEAD(&sigmadsp
->data_list
);
470 static void devm_sigmadsp_release(struct device
*dev
, void *res
)
472 sigmadsp_firmware_release((struct sigmadsp
*)res
);
475 static int sigmadsp_firmware_load(struct sigmadsp
*sigmadsp
, const char *name
)
477 const struct sigma_firmware_header
*ssfw_head
;
478 const struct firmware
*fw
;
482 /* first load the blob */
483 ret
= request_firmware(&fw
, name
, sigmadsp
->dev
);
485 pr_debug("%s: request_firmware() failed with %i\n", __func__
, ret
);
489 /* then verify the header */
493 * Reject too small or unreasonable large files. The upper limit has been
494 * chosen a bit arbitrarily, but it should be enough for all practical
495 * purposes and having the limit makes it easier to avoid integer
496 * overflows later in the loading process.
498 if (fw
->size
< sizeof(*ssfw_head
) || fw
->size
>= 0x4000000) {
499 dev_err(sigmadsp
->dev
, "Failed to load firmware: Invalid size\n");
503 ssfw_head
= (void *)fw
->data
;
504 if (memcmp(ssfw_head
->magic
, SIGMA_MAGIC
, ARRAY_SIZE(ssfw_head
->magic
))) {
505 dev_err(sigmadsp
->dev
, "Failed to load firmware: Invalid magic\n");
509 crc
= crc32(0, fw
->data
+ sizeof(*ssfw_head
),
510 fw
->size
- sizeof(*ssfw_head
));
511 pr_debug("%s: crc=%x\n", __func__
, crc
);
512 if (crc
!= le32_to_cpu(ssfw_head
->crc
)) {
513 dev_err(sigmadsp
->dev
, "Failed to load firmware: Wrong crc checksum: expected %x got %x\n",
514 le32_to_cpu(ssfw_head
->crc
), crc
);
518 switch (ssfw_head
->version
) {
520 ret
= sigmadsp_fw_load_v1(sigmadsp
, fw
);
523 ret
= sigmadsp_fw_load_v2(sigmadsp
, fw
);
526 dev_err(sigmadsp
->dev
,
527 "Failed to load firmware: Invalid version %d. Supported firmware versions: 1, 2\n",
534 sigmadsp_firmware_release(sigmadsp
);
537 release_firmware(fw
);
542 static int sigmadsp_init(struct sigmadsp
*sigmadsp
, struct device
*dev
,
543 const struct sigmadsp_ops
*ops
, const char *firmware_name
)
548 INIT_LIST_HEAD(&sigmadsp
->ctrl_list
);
549 INIT_LIST_HEAD(&sigmadsp
->data_list
);
550 mutex_init(&sigmadsp
->lock
);
552 return sigmadsp_firmware_load(sigmadsp
, firmware_name
);
556 * devm_sigmadsp_init() - Initialize SigmaDSP instance
557 * @dev: The parent device
558 * @ops: The sigmadsp_ops to use for this instance
559 * @firmware_name: Name of the firmware file to load
561 * Allocates a SigmaDSP instance and loads the specified firmware file.
563 * Returns a pointer to a struct sigmadsp on success, or a PTR_ERR() on error.
565 struct sigmadsp
*devm_sigmadsp_init(struct device
*dev
,
566 const struct sigmadsp_ops
*ops
, const char *firmware_name
)
568 struct sigmadsp
*sigmadsp
;
571 sigmadsp
= devres_alloc(devm_sigmadsp_release
, sizeof(*sigmadsp
),
574 return ERR_PTR(-ENOMEM
);
576 ret
= sigmadsp_init(sigmadsp
, dev
, ops
, firmware_name
);
578 devres_free(sigmadsp
);
582 devres_add(dev
, sigmadsp
);
586 EXPORT_SYMBOL_GPL(devm_sigmadsp_init
);
588 static int sigmadsp_rate_to_index(struct sigmadsp
*sigmadsp
, unsigned int rate
)
592 for (i
= 0; i
< sigmadsp
->rate_constraints
.count
; i
++) {
593 if (sigmadsp
->rate_constraints
.list
[i
] == rate
)
600 static unsigned int sigmadsp_get_samplerate_mask(struct sigmadsp
*sigmadsp
,
601 unsigned int samplerate
)
603 int samplerate_index
;
608 if (sigmadsp
->rate_constraints
.count
) {
609 samplerate_index
= sigmadsp_rate_to_index(sigmadsp
, samplerate
);
610 if (samplerate_index
< 0)
613 return BIT(samplerate_index
);
619 static bool sigmadsp_samplerate_valid(unsigned int supported
,
620 unsigned int requested
)
622 /* All samplerates are supported */
626 return supported
& requested
;
629 static int sigmadsp_alloc_control(struct sigmadsp
*sigmadsp
,
630 struct sigmadsp_control
*ctrl
, unsigned int samplerate_mask
)
632 struct snd_kcontrol_new
template;
633 struct snd_kcontrol
*kcontrol
;
635 memset(&template, 0, sizeof(template));
636 template.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
637 template.name
= ctrl
->name
;
638 template.info
= sigmadsp_ctrl_info
;
639 template.get
= sigmadsp_ctrl_get
;
640 template.put
= sigmadsp_ctrl_put
;
641 template.private_value
= (unsigned long)ctrl
;
642 template.access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
;
643 if (!sigmadsp_samplerate_valid(ctrl
->samplerates
, samplerate_mask
))
644 template.access
|= SNDRV_CTL_ELEM_ACCESS_INACTIVE
;
646 kcontrol
= snd_ctl_new1(&template, sigmadsp
);
650 kcontrol
->private_free
= sigmadsp_control_free
;
651 ctrl
->kcontrol
= kcontrol
;
653 return snd_ctl_add(sigmadsp
->component
->card
->snd_card
, kcontrol
);
656 static void sigmadsp_activate_ctrl(struct sigmadsp
*sigmadsp
,
657 struct sigmadsp_control
*ctrl
, unsigned int samplerate_mask
)
659 struct snd_card
*card
= sigmadsp
->component
->card
->snd_card
;
660 struct snd_kcontrol_volatile
*vd
;
661 struct snd_ctl_elem_id id
;
663 bool changed
= false;
665 active
= sigmadsp_samplerate_valid(ctrl
->samplerates
, samplerate_mask
);
667 down_write(&card
->controls_rwsem
);
668 if (!ctrl
->kcontrol
) {
669 up_write(&card
->controls_rwsem
);
673 id
= ctrl
->kcontrol
->id
;
674 vd
= &ctrl
->kcontrol
->vd
[0];
675 if (active
== (bool)(vd
->access
& SNDRV_CTL_ELEM_ACCESS_INACTIVE
)) {
676 vd
->access
^= SNDRV_CTL_ELEM_ACCESS_INACTIVE
;
679 up_write(&card
->controls_rwsem
);
681 if (active
&& changed
) {
682 mutex_lock(&sigmadsp
->lock
);
684 sigmadsp_ctrl_write(sigmadsp
, ctrl
, ctrl
->cache
);
685 mutex_unlock(&sigmadsp
->lock
);
689 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_INFO
, &id
);
693 * sigmadsp_attach() - Attach a sigmadsp instance to a ASoC component
694 * @sigmadsp: The sigmadsp instance to attach
695 * @component: The component to attach to
697 * Typically called in the components probe callback.
699 * Note, once this function has been called the firmware must not be released
700 * until after the ALSA snd_card that the component belongs to has been
701 * disconnected, even if sigmadsp_attach() returns an error.
703 int sigmadsp_attach(struct sigmadsp
*sigmadsp
,
704 struct snd_soc_component
*component
)
706 struct sigmadsp_control
*ctrl
;
707 unsigned int samplerate_mask
;
710 sigmadsp
->component
= component
;
712 samplerate_mask
= sigmadsp_get_samplerate_mask(sigmadsp
,
713 sigmadsp
->current_samplerate
);
715 list_for_each_entry(ctrl
, &sigmadsp
->ctrl_list
, head
) {
716 ret
= sigmadsp_alloc_control(sigmadsp
, ctrl
, samplerate_mask
);
723 EXPORT_SYMBOL_GPL(sigmadsp_attach
);
726 * sigmadsp_setup() - Setup the DSP for the specified samplerate
727 * @sigmadsp: The sigmadsp instance to configure
728 * @samplerate: The samplerate the DSP should be configured for
730 * Loads the appropriate firmware program and parameter memory (if not already
731 * loaded) and enables the controls for the specified samplerate. Any control
732 * parameter changes that have been made previously will be restored.
734 * Returns 0 on success, a negative error code otherwise.
736 int sigmadsp_setup(struct sigmadsp
*sigmadsp
, unsigned int samplerate
)
738 struct sigmadsp_control
*ctrl
;
739 unsigned int samplerate_mask
;
740 struct sigmadsp_data
*data
;
743 if (sigmadsp
->current_samplerate
== samplerate
)
746 samplerate_mask
= sigmadsp_get_samplerate_mask(sigmadsp
, samplerate
);
747 if (samplerate_mask
== 0)
750 list_for_each_entry(data
, &sigmadsp
->data_list
, head
) {
751 if (!sigmadsp_samplerate_valid(data
->samplerates
,
754 ret
= sigmadsp_write(sigmadsp
, data
->addr
, data
->data
,
760 list_for_each_entry(ctrl
, &sigmadsp
->ctrl_list
, head
)
761 sigmadsp_activate_ctrl(sigmadsp
, ctrl
, samplerate_mask
);
763 sigmadsp
->current_samplerate
= samplerate
;
767 sigmadsp_reset(sigmadsp
);
771 EXPORT_SYMBOL_GPL(sigmadsp_setup
);
774 * sigmadsp_reset() - Notify the sigmadsp instance that the DSP has been reset
775 * @sigmadsp: The sigmadsp instance to reset
777 * Should be called whenever the DSP has been reset and parameter and program
778 * memory need to be re-loaded.
780 void sigmadsp_reset(struct sigmadsp
*sigmadsp
)
782 struct sigmadsp_control
*ctrl
;
784 list_for_each_entry(ctrl
, &sigmadsp
->ctrl_list
, head
)
785 sigmadsp_activate_ctrl(sigmadsp
, ctrl
, false);
787 sigmadsp
->current_samplerate
= 0;
789 EXPORT_SYMBOL_GPL(sigmadsp_reset
);
792 * sigmadsp_restrict_params() - Applies DSP firmware specific constraints
793 * @sigmadsp: The sigmadsp instance
794 * @substream: The substream to restrict
796 * Applies samplerate constraints that may be required by the firmware Should
797 * typically be called from the CODEC/component drivers startup callback.
799 * Returns 0 on success, a negative error code otherwise.
801 int sigmadsp_restrict_params(struct sigmadsp
*sigmadsp
,
802 struct snd_pcm_substream
*substream
)
804 if (sigmadsp
->rate_constraints
.count
== 0)
807 return snd_pcm_hw_constraint_list(substream
->runtime
, 0,
808 SNDRV_PCM_HW_PARAM_RATE
, &sigmadsp
->rate_constraints
);
810 EXPORT_SYMBOL_GPL(sigmadsp_restrict_params
);
812 MODULE_LICENSE("GPL");