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
> 4 && ctrl
->num_bytes
<= 20 && sigmadsp
->ops
&&
121 sigmadsp
->ops
->safeload
)
122 return sigmadsp
->ops
->safeload(sigmadsp
, ctrl
->addr
, data
,
125 return sigmadsp_write(sigmadsp
, ctrl
->addr
, data
,
129 static int sigmadsp_ctrl_put(struct snd_kcontrol
*kcontrol
,
130 struct snd_ctl_elem_value
*ucontrol
)
132 struct sigmadsp_control
*ctrl
= (void *)kcontrol
->private_value
;
133 struct sigmadsp
*sigmadsp
= snd_kcontrol_chip(kcontrol
);
137 mutex_lock(&sigmadsp
->lock
);
139 data
= ucontrol
->value
.bytes
.data
;
141 if (!(kcontrol
->vd
[0].access
& SNDRV_CTL_ELEM_ACCESS_INACTIVE
))
142 ret
= sigmadsp_ctrl_write(sigmadsp
, ctrl
, data
);
145 memcpy(ctrl
->cache
, data
, ctrl
->num_bytes
);
149 mutex_unlock(&sigmadsp
->lock
);
154 static int sigmadsp_ctrl_get(struct snd_kcontrol
*kcontrol
,
155 struct snd_ctl_elem_value
*ucontrol
)
157 struct sigmadsp_control
*ctrl
= (void *)kcontrol
->private_value
;
158 struct sigmadsp
*sigmadsp
= snd_kcontrol_chip(kcontrol
);
161 mutex_lock(&sigmadsp
->lock
);
164 ret
= sigmadsp_read(sigmadsp
, ctrl
->addr
, ctrl
->cache
,
170 memcpy(ucontrol
->value
.bytes
.data
, ctrl
->cache
,
174 mutex_unlock(&sigmadsp
->lock
);
179 static void sigmadsp_control_free(struct snd_kcontrol
*kcontrol
)
181 struct sigmadsp_control
*ctrl
= (void *)kcontrol
->private_value
;
183 ctrl
->kcontrol
= NULL
;
186 static bool sigma_fw_validate_control_name(const char *name
, unsigned int len
)
190 for (i
= 0; i
< len
; i
++) {
191 /* Normal ASCII characters are valid */
192 if (name
[i
] < ' ' || name
[i
] > '~')
199 static int sigma_fw_load_control(struct sigmadsp
*sigmadsp
,
200 const struct sigma_fw_chunk
*chunk
, unsigned int length
)
202 const struct sigma_fw_chunk_control
*ctrl_chunk
;
203 struct sigmadsp_control
*ctrl
;
204 unsigned int num_bytes
;
209 if (length
<= sizeof(*ctrl_chunk
))
212 ctrl_chunk
= (const struct sigma_fw_chunk_control
*)chunk
;
214 name_len
= length
- sizeof(*ctrl_chunk
);
215 if (name_len
>= SNDRV_CTL_ELEM_ID_NAME_MAXLEN
)
216 name_len
= SNDRV_CTL_ELEM_ID_NAME_MAXLEN
- 1;
218 /* Make sure there are no non-displayable characaters in the string */
219 if (!sigma_fw_validate_control_name(ctrl_chunk
->name
, name_len
))
222 num_bytes
= le16_to_cpu(ctrl_chunk
->num_bytes
);
223 ctrl
= kzalloc(sizeof(*ctrl
) + num_bytes
, GFP_KERNEL
);
227 name
= kzalloc(name_len
+ 1, GFP_KERNEL
);
232 memcpy(name
, ctrl_chunk
->name
, name_len
);
233 name
[name_len
] = '\0';
236 ctrl
->addr
= le16_to_cpu(ctrl_chunk
->addr
);
237 ctrl
->num_bytes
= num_bytes
;
238 ctrl
->samplerates
= le32_to_cpu(chunk
->samplerates
);
240 list_add_tail(&ctrl
->head
, &sigmadsp
->ctrl_list
);
250 static int sigma_fw_load_data(struct sigmadsp
*sigmadsp
,
251 const struct sigma_fw_chunk
*chunk
, unsigned int length
)
253 const struct sigma_fw_chunk_data
*data_chunk
;
254 struct sigmadsp_data
*data
;
256 if (length
<= sizeof(*data_chunk
))
259 data_chunk
= (struct sigma_fw_chunk_data
*)chunk
;
261 length
-= sizeof(*data_chunk
);
263 data
= kzalloc(sizeof(*data
) + length
, GFP_KERNEL
);
267 data
->addr
= le16_to_cpu(data_chunk
->addr
);
268 data
->length
= length
;
269 data
->samplerates
= le32_to_cpu(chunk
->samplerates
);
270 memcpy(data
->data
, data_chunk
->data
, length
);
271 list_add_tail(&data
->head
, &sigmadsp
->data_list
);
276 static int sigma_fw_load_samplerates(struct sigmadsp
*sigmadsp
,
277 const struct sigma_fw_chunk
*chunk
, unsigned int length
)
279 const struct sigma_fw_chunk_samplerate
*rate_chunk
;
280 unsigned int num_rates
;
284 rate_chunk
= (const struct sigma_fw_chunk_samplerate
*)chunk
;
286 num_rates
= (length
- sizeof(*rate_chunk
)) / sizeof(__le32
);
288 if (num_rates
> 32 || num_rates
== 0)
291 /* We only allow one samplerates block per file */
292 if (sigmadsp
->rate_constraints
.count
)
295 rates
= kcalloc(num_rates
, sizeof(*rates
), GFP_KERNEL
);
299 for (i
= 0; i
< num_rates
; i
++)
300 rates
[i
] = le32_to_cpu(rate_chunk
->samplerates
[i
]);
302 sigmadsp
->rate_constraints
.count
= num_rates
;
303 sigmadsp
->rate_constraints
.list
= rates
;
308 static int sigmadsp_fw_load_v2(struct sigmadsp
*sigmadsp
,
309 const struct firmware
*fw
)
311 struct sigma_fw_chunk
*chunk
;
312 unsigned int length
, pos
;
316 * Make sure that there is at least one chunk to avoid integer
317 * underflows later on. Empty firmware is still valid though.
319 if (fw
->size
< sizeof(*chunk
) + sizeof(struct sigma_firmware_header
))
322 pos
= sizeof(struct sigma_firmware_header
);
324 while (pos
< fw
->size
- sizeof(*chunk
)) {
325 chunk
= (struct sigma_fw_chunk
*)(fw
->data
+ pos
);
327 length
= le32_to_cpu(chunk
->length
);
329 if (length
> fw
->size
- pos
|| length
< sizeof(*chunk
))
332 switch (le32_to_cpu(chunk
->tag
)) {
333 case SIGMA_FW_CHUNK_TYPE_DATA
:
334 ret
= sigma_fw_load_data(sigmadsp
, chunk
, length
);
336 case SIGMA_FW_CHUNK_TYPE_CONTROL
:
337 ret
= sigma_fw_load_control(sigmadsp
, chunk
, length
);
339 case SIGMA_FW_CHUNK_TYPE_SAMPLERATES
:
340 ret
= sigma_fw_load_samplerates(sigmadsp
, chunk
, length
);
343 dev_warn(sigmadsp
->dev
, "Unknown chunk type: %d\n",
353 * This can not overflow since if length is larger than the
354 * maximum firmware size (0x4000000) we'll error out earilier.
356 pos
+= ALIGN(length
, sizeof(__le32
));
362 static inline u32
sigma_action_len(struct sigma_action
*sa
)
364 return (sa
->len_hi
<< 16) | le16_to_cpu(sa
->len
);
367 static size_t sigma_action_size(struct sigma_action
*sa
)
372 case SIGMA_ACTION_WRITEXBYTES
:
373 case SIGMA_ACTION_WRITESINGLE
:
374 case SIGMA_ACTION_WRITESAFELOAD
:
375 payload
= sigma_action_len(sa
);
381 payload
= ALIGN(payload
, 2);
383 return payload
+ sizeof(struct sigma_action
);
387 * Returns a negative error value in case of an error, 0 if processing of
388 * the firmware should be stopped after this action, 1 otherwise.
390 static int process_sigma_action(struct sigmadsp
*sigmadsp
,
391 struct sigma_action
*sa
)
393 size_t len
= sigma_action_len(sa
);
394 struct sigmadsp_data
*data
;
396 pr_debug("%s: instr:%i addr:%#x len:%zu\n", __func__
,
397 sa
->instr
, sa
->addr
, len
);
400 case SIGMA_ACTION_WRITEXBYTES
:
401 case SIGMA_ACTION_WRITESINGLE
:
402 case SIGMA_ACTION_WRITESAFELOAD
:
406 data
= kzalloc(sizeof(*data
) + len
- 2, GFP_KERNEL
);
410 data
->addr
= be16_to_cpu(sa
->addr
);
411 data
->length
= len
- 2;
412 memcpy(data
->data
, sa
->payload
, data
->length
);
413 list_add_tail(&data
->head
, &sigmadsp
->data_list
);
415 case SIGMA_ACTION_END
:
424 static int sigmadsp_fw_load_v1(struct sigmadsp
*sigmadsp
,
425 const struct firmware
*fw
)
427 struct sigma_action
*sa
;
431 pos
= sizeof(struct sigma_firmware_header
);
433 while (pos
+ sizeof(*sa
) <= fw
->size
) {
434 sa
= (struct sigma_action
*)(fw
->data
+ pos
);
436 size
= sigma_action_size(sa
);
438 if (pos
> fw
->size
|| size
== 0)
441 ret
= process_sigma_action(sigmadsp
, sa
);
443 pr_debug("%s: action returned %i\n", __func__
, ret
);
455 static void sigmadsp_firmware_release(struct sigmadsp
*sigmadsp
)
457 struct sigmadsp_control
*ctrl
, *_ctrl
;
458 struct sigmadsp_data
*data
, *_data
;
460 list_for_each_entry_safe(ctrl
, _ctrl
, &sigmadsp
->ctrl_list
, head
) {
465 list_for_each_entry_safe(data
, _data
, &sigmadsp
->data_list
, head
)
468 INIT_LIST_HEAD(&sigmadsp
->ctrl_list
);
469 INIT_LIST_HEAD(&sigmadsp
->data_list
);
472 static void devm_sigmadsp_release(struct device
*dev
, void *res
)
474 sigmadsp_firmware_release((struct sigmadsp
*)res
);
477 static int sigmadsp_firmware_load(struct sigmadsp
*sigmadsp
, const char *name
)
479 const struct sigma_firmware_header
*ssfw_head
;
480 const struct firmware
*fw
;
484 /* first load the blob */
485 ret
= request_firmware(&fw
, name
, sigmadsp
->dev
);
487 pr_debug("%s: request_firmware() failed with %i\n", __func__
, ret
);
491 /* then verify the header */
495 * Reject too small or unreasonable large files. The upper limit has been
496 * chosen a bit arbitrarily, but it should be enough for all practical
497 * purposes and having the limit makes it easier to avoid integer
498 * overflows later in the loading process.
500 if (fw
->size
< sizeof(*ssfw_head
) || fw
->size
>= 0x4000000) {
501 dev_err(sigmadsp
->dev
, "Failed to load firmware: Invalid size\n");
505 ssfw_head
= (void *)fw
->data
;
506 if (memcmp(ssfw_head
->magic
, SIGMA_MAGIC
, ARRAY_SIZE(ssfw_head
->magic
))) {
507 dev_err(sigmadsp
->dev
, "Failed to load firmware: Invalid magic\n");
511 crc
= crc32(0, fw
->data
+ sizeof(*ssfw_head
),
512 fw
->size
- sizeof(*ssfw_head
));
513 pr_debug("%s: crc=%x\n", __func__
, crc
);
514 if (crc
!= le32_to_cpu(ssfw_head
->crc
)) {
515 dev_err(sigmadsp
->dev
, "Failed to load firmware: Wrong crc checksum: expected %x got %x\n",
516 le32_to_cpu(ssfw_head
->crc
), crc
);
520 switch (ssfw_head
->version
) {
522 ret
= sigmadsp_fw_load_v1(sigmadsp
, fw
);
525 ret
= sigmadsp_fw_load_v2(sigmadsp
, fw
);
528 dev_err(sigmadsp
->dev
,
529 "Failed to load firmware: Invalid version %d. Supported firmware versions: 1, 2\n",
536 sigmadsp_firmware_release(sigmadsp
);
539 release_firmware(fw
);
544 static int sigmadsp_init(struct sigmadsp
*sigmadsp
, struct device
*dev
,
545 const struct sigmadsp_ops
*ops
, const char *firmware_name
)
550 INIT_LIST_HEAD(&sigmadsp
->ctrl_list
);
551 INIT_LIST_HEAD(&sigmadsp
->data_list
);
552 mutex_init(&sigmadsp
->lock
);
554 return sigmadsp_firmware_load(sigmadsp
, firmware_name
);
558 * devm_sigmadsp_init() - Initialize SigmaDSP instance
559 * @dev: The parent device
560 * @ops: The sigmadsp_ops to use for this instance
561 * @firmware_name: Name of the firmware file to load
563 * Allocates a SigmaDSP instance and loads the specified firmware file.
565 * Returns a pointer to a struct sigmadsp on success, or a PTR_ERR() on error.
567 struct sigmadsp
*devm_sigmadsp_init(struct device
*dev
,
568 const struct sigmadsp_ops
*ops
, const char *firmware_name
)
570 struct sigmadsp
*sigmadsp
;
573 sigmadsp
= devres_alloc(devm_sigmadsp_release
, sizeof(*sigmadsp
),
576 return ERR_PTR(-ENOMEM
);
578 ret
= sigmadsp_init(sigmadsp
, dev
, ops
, firmware_name
);
580 devres_free(sigmadsp
);
584 devres_add(dev
, sigmadsp
);
588 EXPORT_SYMBOL_GPL(devm_sigmadsp_init
);
590 static int sigmadsp_rate_to_index(struct sigmadsp
*sigmadsp
, unsigned int rate
)
594 for (i
= 0; i
< sigmadsp
->rate_constraints
.count
; i
++) {
595 if (sigmadsp
->rate_constraints
.list
[i
] == rate
)
602 static unsigned int sigmadsp_get_samplerate_mask(struct sigmadsp
*sigmadsp
,
603 unsigned int samplerate
)
605 int samplerate_index
;
610 if (sigmadsp
->rate_constraints
.count
) {
611 samplerate_index
= sigmadsp_rate_to_index(sigmadsp
, samplerate
);
612 if (samplerate_index
< 0)
615 return BIT(samplerate_index
);
621 static bool sigmadsp_samplerate_valid(unsigned int supported
,
622 unsigned int requested
)
624 /* All samplerates are supported */
628 return supported
& requested
;
631 static int sigmadsp_alloc_control(struct sigmadsp
*sigmadsp
,
632 struct sigmadsp_control
*ctrl
, unsigned int samplerate_mask
)
634 struct snd_kcontrol_new
template;
635 struct snd_kcontrol
*kcontrol
;
637 memset(&template, 0, sizeof(template));
638 template.iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
639 template.name
= ctrl
->name
;
640 template.info
= sigmadsp_ctrl_info
;
641 template.get
= sigmadsp_ctrl_get
;
642 template.put
= sigmadsp_ctrl_put
;
643 template.private_value
= (unsigned long)ctrl
;
644 template.access
= SNDRV_CTL_ELEM_ACCESS_READWRITE
;
645 if (!sigmadsp_samplerate_valid(ctrl
->samplerates
, samplerate_mask
))
646 template.access
|= SNDRV_CTL_ELEM_ACCESS_INACTIVE
;
648 kcontrol
= snd_ctl_new1(&template, sigmadsp
);
652 kcontrol
->private_free
= sigmadsp_control_free
;
653 ctrl
->kcontrol
= kcontrol
;
655 return snd_ctl_add(sigmadsp
->component
->card
->snd_card
, kcontrol
);
658 static void sigmadsp_activate_ctrl(struct sigmadsp
*sigmadsp
,
659 struct sigmadsp_control
*ctrl
, unsigned int samplerate_mask
)
661 struct snd_card
*card
= sigmadsp
->component
->card
->snd_card
;
662 struct snd_kcontrol_volatile
*vd
;
663 struct snd_ctl_elem_id id
;
665 bool changed
= false;
667 active
= sigmadsp_samplerate_valid(ctrl
->samplerates
, samplerate_mask
);
669 down_write(&card
->controls_rwsem
);
670 if (!ctrl
->kcontrol
) {
671 up_write(&card
->controls_rwsem
);
675 id
= ctrl
->kcontrol
->id
;
676 vd
= &ctrl
->kcontrol
->vd
[0];
677 if (active
== (bool)(vd
->access
& SNDRV_CTL_ELEM_ACCESS_INACTIVE
)) {
678 vd
->access
^= SNDRV_CTL_ELEM_ACCESS_INACTIVE
;
681 up_write(&card
->controls_rwsem
);
683 if (active
&& changed
) {
684 mutex_lock(&sigmadsp
->lock
);
686 sigmadsp_ctrl_write(sigmadsp
, ctrl
, ctrl
->cache
);
687 mutex_unlock(&sigmadsp
->lock
);
691 snd_ctl_notify(card
, SNDRV_CTL_EVENT_MASK_INFO
, &id
);
695 * sigmadsp_attach() - Attach a sigmadsp instance to a ASoC component
696 * @sigmadsp: The sigmadsp instance to attach
697 * @component: The component to attach to
699 * Typically called in the components probe callback.
701 * Note, once this function has been called the firmware must not be released
702 * until after the ALSA snd_card that the component belongs to has been
703 * disconnected, even if sigmadsp_attach() returns an error.
705 int sigmadsp_attach(struct sigmadsp
*sigmadsp
,
706 struct snd_soc_component
*component
)
708 struct sigmadsp_control
*ctrl
;
709 unsigned int samplerate_mask
;
712 sigmadsp
->component
= component
;
714 samplerate_mask
= sigmadsp_get_samplerate_mask(sigmadsp
,
715 sigmadsp
->current_samplerate
);
717 list_for_each_entry(ctrl
, &sigmadsp
->ctrl_list
, head
) {
718 ret
= sigmadsp_alloc_control(sigmadsp
, ctrl
, samplerate_mask
);
725 EXPORT_SYMBOL_GPL(sigmadsp_attach
);
728 * sigmadsp_setup() - Setup the DSP for the specified samplerate
729 * @sigmadsp: The sigmadsp instance to configure
730 * @samplerate: The samplerate the DSP should be configured for
732 * Loads the appropriate firmware program and parameter memory (if not already
733 * loaded) and enables the controls for the specified samplerate. Any control
734 * parameter changes that have been made previously will be restored.
736 * Returns 0 on success, a negative error code otherwise.
738 int sigmadsp_setup(struct sigmadsp
*sigmadsp
, unsigned int samplerate
)
740 struct sigmadsp_control
*ctrl
;
741 unsigned int samplerate_mask
;
742 struct sigmadsp_data
*data
;
745 if (sigmadsp
->current_samplerate
== samplerate
)
748 samplerate_mask
= sigmadsp_get_samplerate_mask(sigmadsp
, samplerate
);
749 if (samplerate_mask
== 0)
752 list_for_each_entry(data
, &sigmadsp
->data_list
, head
) {
753 if (!sigmadsp_samplerate_valid(data
->samplerates
,
756 ret
= sigmadsp_write(sigmadsp
, data
->addr
, data
->data
,
762 list_for_each_entry(ctrl
, &sigmadsp
->ctrl_list
, head
)
763 sigmadsp_activate_ctrl(sigmadsp
, ctrl
, samplerate_mask
);
765 sigmadsp
->current_samplerate
= samplerate
;
769 sigmadsp_reset(sigmadsp
);
773 EXPORT_SYMBOL_GPL(sigmadsp_setup
);
776 * sigmadsp_reset() - Notify the sigmadsp instance that the DSP has been reset
777 * @sigmadsp: The sigmadsp instance to reset
779 * Should be called whenever the DSP has been reset and parameter and program
780 * memory need to be re-loaded.
782 void sigmadsp_reset(struct sigmadsp
*sigmadsp
)
784 struct sigmadsp_control
*ctrl
;
786 list_for_each_entry(ctrl
, &sigmadsp
->ctrl_list
, head
)
787 sigmadsp_activate_ctrl(sigmadsp
, ctrl
, false);
789 sigmadsp
->current_samplerate
= 0;
791 EXPORT_SYMBOL_GPL(sigmadsp_reset
);
794 * sigmadsp_restrict_params() - Applies DSP firmware specific constraints
795 * @sigmadsp: The sigmadsp instance
796 * @substream: The substream to restrict
798 * Applies samplerate constraints that may be required by the firmware Should
799 * typically be called from the CODEC/component drivers startup callback.
801 * Returns 0 on success, a negative error code otherwise.
803 int sigmadsp_restrict_params(struct sigmadsp
*sigmadsp
,
804 struct snd_pcm_substream
*substream
)
806 if (sigmadsp
->rate_constraints
.count
== 0)
809 return snd_pcm_hw_constraint_list(substream
->runtime
, 0,
810 SNDRV_PCM_HW_PARAM_RATE
, &sigmadsp
->rate_constraints
);
812 EXPORT_SYMBOL_GPL(sigmadsp_restrict_params
);
814 MODULE_LICENSE("GPL");