2 * Load Analog Devices SigmaStudio firmware files
4 * Copyright 2009-2014 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
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>
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
;
32 unsigned int num_bytes
;
34 struct snd_kcontrol
*kcontrol
;
39 struct sigmadsp_data
{
40 struct list_head head
;
47 struct sigma_fw_chunk
{
53 struct sigma_fw_chunk_data
{
54 struct sigma_fw_chunk chunk
;
59 struct sigma_fw_chunk_control
{
60 struct sigma_fw_chunk chunk
;
67 struct sigma_fw_chunk_samplerate
{
68 struct sigma_fw_chunk chunk
;
72 struct sigma_firmware_header
{
73 unsigned char magic
[7];
79 SIGMA_ACTION_WRITEXBYTES
= 0,
80 SIGMA_ACTION_WRITESINGLE
,
81 SIGMA_ACTION_WRITESAFELOAD
,
90 unsigned char payload
[];
93 static int sigmadsp_write(struct sigmadsp
*sigmadsp
, unsigned int addr
,
94 const uint8_t data
[], size_t len
)
96 return sigmadsp
->write(sigmadsp
->control_data
, addr
, data
, len
);
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
;
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
,
124 return sigmadsp_write(sigmadsp
, ctrl
->addr
, data
,
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
);
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
);
144 memcpy(ctrl
->cache
, data
, ctrl
->num_bytes
);
148 mutex_unlock(&sigmadsp
->lock
);
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
);
160 mutex_lock(&sigmadsp
->lock
);
163 ret
= sigmadsp_read(sigmadsp
, ctrl
->addr
, ctrl
->cache
,
169 memcpy(ucontrol
->value
.bytes
.data
, ctrl
->cache
,
173 mutex_unlock(&sigmadsp
->lock
);
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
)
189 for (i
= 0; i
< len
; i
++) {
190 /* Normal ASCII characters are valid */
191 if (name
[i
] < ' ' || name
[i
] > '~')
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
;
208 if (length
<= sizeof(*ctrl_chunk
))
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
))
221 num_bytes
= le16_to_cpu(ctrl_chunk
->num_bytes
);
222 ctrl
= kzalloc(sizeof(*ctrl
) + num_bytes
, GFP_KERNEL
);
226 name
= kzalloc(name_len
+ 1, GFP_KERNEL
);
231 memcpy(name
, ctrl_chunk
->name
, name_len
);
232 name
[name_len
] = '\0';
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
);
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
))
258 data_chunk
= (struct sigma_fw_chunk_data
*)chunk
;
260 length
-= sizeof(*data_chunk
);
262 data
= kzalloc(sizeof(*data
) + length
, GFP_KERNEL
);
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
);
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
;
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)
290 /* We only allow one samplerates block per file */
291 if (sigmadsp
->rate_constraints
.count
)
294 rates
= kcalloc(num_rates
, sizeof(*rates
), GFP_KERNEL
);
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
;
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
;
315 * Make sure that there is at least one chunk to avoid integer
316 * underflows later on. Empty firmware is still valid though.
318 if (fw
->size
< sizeof(*chunk
) + sizeof(struct sigma_firmware_header
))
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
))
331 switch (le32_to_cpu(chunk
->tag
)) {
332 case SIGMA_FW_CHUNK_TYPE_DATA
:
333 ret
= sigma_fw_load_data(sigmadsp
, chunk
, length
);
335 case SIGMA_FW_CHUNK_TYPE_CONTROL
:
336 ret
= sigma_fw_load_control(sigmadsp
, chunk
, length
);
338 case SIGMA_FW_CHUNK_TYPE_SAMPLERATES
:
339 ret
= sigma_fw_load_samplerates(sigmadsp
, chunk
, length
);
342 dev_warn(sigmadsp
->dev
, "Unknown chunk type: %d\n",
352 * This can not overflow since if length is larger than the
353 * maximum firmware size (0x4000000) we'll error out earilier.
355 pos
+= ALIGN(length
, sizeof(__le32
));
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
)
371 case SIGMA_ACTION_WRITEXBYTES
:
372 case SIGMA_ACTION_WRITESINGLE
:
373 case SIGMA_ACTION_WRITESAFELOAD
:
374 payload
= sigma_action_len(sa
);
380 payload
= ALIGN(payload
, 2);
382 return payload
+ sizeof(struct sigma_action
);
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.
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
);
399 case SIGMA_ACTION_WRITEXBYTES
:
400 case SIGMA_ACTION_WRITESINGLE
:
401 case SIGMA_ACTION_WRITESAFELOAD
:
405 data
= kzalloc(sizeof(*data
) + len
- 2, GFP_KERNEL
);
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
);
414 case SIGMA_ACTION_END
:
423 static int sigmadsp_fw_load_v1(struct sigmadsp
*sigmadsp
,
424 const struct firmware
*fw
)
426 struct sigma_action
*sa
;
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
);
437 if (pos
> fw
->size
|| size
== 0)
440 ret
= process_sigma_action(sigmadsp
, sa
);
442 pr_debug("%s: action returned %i\n", __func__
, ret
);
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
) {
464 list_for_each_entry_safe(data
, _data
, &sigmadsp
->data_list
, head
)
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
;
483 /* first load the blob */
484 ret
= request_firmware(&fw
, name
, sigmadsp
->dev
);
486 pr_debug("%s: request_firmware() failed with %i\n", __func__
, ret
);
490 /* then verify the header */
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.
499 if (fw
->size
< sizeof(*ssfw_head
) || fw
->size
>= 0x4000000) {
500 dev_err(sigmadsp
->dev
, "Failed to load firmware: Invalid size\n");
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");
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
);
519 switch (ssfw_head
->version
) {
521 ret
= sigmadsp_fw_load_v1(sigmadsp
, fw
);
524 ret
= sigmadsp_fw_load_v2(sigmadsp
, fw
);
527 dev_err(sigmadsp
->dev
,
528 "Failed to load firmware: Invalid version %d. Supported firmware versions: 1, 2\n",
535 sigmadsp_firmware_release(sigmadsp
);
538 release_firmware(fw
);
543 static int sigmadsp_init(struct sigmadsp
*sigmadsp
, struct device
*dev
,
544 const struct sigmadsp_ops
*ops
, const char *firmware_name
)
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
);
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
562 * Allocates a SigmaDSP instance and loads the specified firmware file.
564 * Returns a pointer to a struct sigmadsp on success, or a PTR_ERR() on error.
566 struct sigmadsp
*devm_sigmadsp_init(struct device
*dev
,
567 const struct sigmadsp_ops
*ops
, const char *firmware_name
)
569 struct sigmadsp
*sigmadsp
;
572 sigmadsp
= devres_alloc(devm_sigmadsp_release
, sizeof(*sigmadsp
),
575 return ERR_PTR(-ENOMEM
);
577 ret
= sigmadsp_init(sigmadsp
, dev
, ops
, firmware_name
);
579 devres_free(sigmadsp
);
583 devres_add(dev
, sigmadsp
);
587 EXPORT_SYMBOL_GPL(devm_sigmadsp_init
);
589 static int sigmadsp_rate_to_index(struct sigmadsp
*sigmadsp
, unsigned int rate
)
593 for (i
= 0; i
< sigmadsp
->rate_constraints
.count
; i
++) {
594 if (sigmadsp
->rate_constraints
.list
[i
] == rate
)
601 static unsigned int sigmadsp_get_samplerate_mask(struct sigmadsp
*sigmadsp
,
602 unsigned int samplerate
)
604 int samplerate_index
;
609 if (sigmadsp
->rate_constraints
.count
) {
610 samplerate_index
= sigmadsp_rate_to_index(sigmadsp
, samplerate
);
611 if (samplerate_index
< 0)
614 return BIT(samplerate_index
);
620 static bool sigmadsp_samplerate_valid(unsigned int supported
,
621 unsigned int requested
)
623 /* All samplerates are supported */
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
);
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
;
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
);
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
;
680 up_write(&card
->controls_rwsem
);
682 if (active
&& changed
) {
683 mutex_lock(&sigmadsp
->lock
);
685 sigmadsp_ctrl_write(sigmadsp
, ctrl
, ctrl
->cache
);
686 mutex_unlock(&sigmadsp
->lock
);
690 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_INFO
, &id
);
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
698 * Typically called in the components probe callback.
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.
704 int sigmadsp_attach(struct sigmadsp
*sigmadsp
,
705 struct snd_soc_component
*component
)
707 struct sigmadsp_control
*ctrl
;
708 unsigned int samplerate_mask
;
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
);
724 EXPORT_SYMBOL_GPL(sigmadsp_attach
);
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
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.
735 * Returns 0 on success, a negative error code otherwise.
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
;
744 if (sigmadsp
->current_samplerate
== samplerate
)
747 samplerate_mask
= sigmadsp_get_samplerate_mask(sigmadsp
, samplerate
);
748 if (samplerate_mask
== 0)
751 list_for_each_entry(data
, &sigmadsp
->data_list
, head
) {
752 if (!sigmadsp_samplerate_valid(data
->samplerates
,
755 ret
= sigmadsp_write(sigmadsp
, data
->addr
, data
->data
,
761 list_for_each_entry(ctrl
, &sigmadsp
->ctrl_list
, head
)
762 sigmadsp_activate_ctrl(sigmadsp
, ctrl
, samplerate_mask
);
764 sigmadsp
->current_samplerate
= samplerate
;
768 sigmadsp_reset(sigmadsp
);
772 EXPORT_SYMBOL_GPL(sigmadsp_setup
);
775 * sigmadsp_reset() - Notify the sigmadsp instance that the DSP has been reset
776 * @sigmadsp: The sigmadsp instance to reset
778 * Should be called whenever the DSP has been reset and parameter and program
779 * memory need to be re-loaded.
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
);
793 * sigmadsp_restrict_params() - Applies DSP firmware specific constraints
794 * @sigmadsp: The sigmadsp instance
795 * @substream: The substream to restrict
797 * Applies samplerate constraints that may be required by the firmware Should
798 * typically be called from the CODEC/component drivers startup callback.
800 * Returns 0 on success, a negative error code otherwise.
802 int sigmadsp_restrict_params(struct sigmadsp
*sigmadsp
,
803 struct snd_pcm_substream
*substream
)
805 if (sigmadsp
->rate_constraints
.count
== 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");