2 * Load Analog Devices SigmaStudio firmware files
4 * Copyright 2009-2011 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/crc32.h>
10 #include <linux/delay.h>
11 #include <linux/firmware.h>
12 #include <linux/kernel.h>
13 #include <linux/i2c.h>
14 #include <linux/regmap.h>
15 #include <linux/module.h>
19 #define SIGMA_MAGIC "ADISIGM"
21 struct sigma_firmware_header
{
22 unsigned char magic
[7];
28 SIGMA_ACTION_WRITEXBYTES
= 0,
29 SIGMA_ACTION_WRITESINGLE
,
30 SIGMA_ACTION_WRITESAFELOAD
,
42 unsigned char payload
[];
45 struct sigma_firmware
{
46 const struct firmware
*fw
;
50 int (*write
)(void *control_data
, const struct sigma_action
*sa
,
54 static inline u32
sigma_action_len(struct sigma_action
*sa
)
56 return (sa
->len_hi
<< 16) | le16_to_cpu(sa
->len
);
59 static size_t sigma_action_size(struct sigma_action
*sa
)
64 case SIGMA_ACTION_WRITEXBYTES
:
65 case SIGMA_ACTION_WRITESINGLE
:
66 case SIGMA_ACTION_WRITESAFELOAD
:
67 payload
= sigma_action_len(sa
);
73 payload
= ALIGN(payload
, 2);
75 return payload
+ sizeof(struct sigma_action
);
79 * Returns a negative error value in case of an error, 0 if processing of
80 * the firmware should be stopped after this action, 1 otherwise.
83 process_sigma_action(struct sigma_firmware
*ssfw
, struct sigma_action
*sa
)
85 size_t len
= sigma_action_len(sa
);
88 pr_debug("%s: instr:%i addr:%#x len:%zu\n", __func__
,
89 sa
->instr
, sa
->addr
, len
);
92 case SIGMA_ACTION_WRITEXBYTES
:
93 case SIGMA_ACTION_WRITESINGLE
:
94 case SIGMA_ACTION_WRITESAFELOAD
:
95 ret
= ssfw
->write(ssfw
->control_data
, sa
, len
);
99 case SIGMA_ACTION_DELAY
:
103 case SIGMA_ACTION_END
:
113 process_sigma_actions(struct sigma_firmware
*ssfw
)
115 struct sigma_action
*sa
;
119 while (ssfw
->pos
+ sizeof(*sa
) <= ssfw
->fw
->size
) {
120 sa
= (struct sigma_action
*)(ssfw
->fw
->data
+ ssfw
->pos
);
122 size
= sigma_action_size(sa
);
124 if (ssfw
->pos
> ssfw
->fw
->size
|| size
== 0)
127 ret
= process_sigma_action(ssfw
, sa
);
129 pr_debug("%s: action returned %i\n", __func__
, ret
);
135 if (ssfw
->pos
!= ssfw
->fw
->size
)
141 static int _process_sigma_firmware(struct device
*dev
,
142 struct sigma_firmware
*ssfw
, const char *name
)
145 struct sigma_firmware_header
*ssfw_head
;
146 const struct firmware
*fw
;
149 pr_debug("%s: loading firmware %s\n", __func__
, name
);
151 /* first load the blob */
152 ret
= request_firmware(&fw
, name
, dev
);
154 pr_debug("%s: request_firmware() failed with %i\n", __func__
, ret
);
159 /* then verify the header */
163 * Reject too small or unreasonable large files. The upper limit has been
164 * chosen a bit arbitrarily, but it should be enough for all practical
165 * purposes and having the limit makes it easier to avoid integer
166 * overflows later in the loading process.
168 if (fw
->size
< sizeof(*ssfw_head
) || fw
->size
>= 0x4000000) {
169 dev_err(dev
, "Failed to load firmware: Invalid size\n");
173 ssfw_head
= (void *)fw
->data
;
174 if (memcmp(ssfw_head
->magic
, SIGMA_MAGIC
, ARRAY_SIZE(ssfw_head
->magic
))) {
175 dev_err(dev
, "Failed to load firmware: Invalid magic\n");
179 crc
= crc32(0, fw
->data
+ sizeof(*ssfw_head
),
180 fw
->size
- sizeof(*ssfw_head
));
181 pr_debug("%s: crc=%x\n", __func__
, crc
);
182 if (crc
!= le32_to_cpu(ssfw_head
->crc
)) {
183 dev_err(dev
, "Failed to load firmware: Wrong crc checksum: expected %x got %x\n",
184 le32_to_cpu(ssfw_head
->crc
), crc
);
188 ssfw
->pos
= sizeof(*ssfw_head
);
190 /* finally process all of the actions */
191 ret
= process_sigma_actions(ssfw
);
194 release_firmware(fw
);
196 pr_debug("%s: loaded %s\n", __func__
, name
);
201 #if IS_ENABLED(CONFIG_I2C)
203 static int sigma_action_write_i2c(void *control_data
,
204 const struct sigma_action
*sa
, size_t len
)
206 return i2c_master_send(control_data
, (const unsigned char *)&sa
->addr
,
210 int process_sigma_firmware(struct i2c_client
*client
, const char *name
)
212 struct sigma_firmware ssfw
;
214 ssfw
.control_data
= client
;
215 ssfw
.write
= sigma_action_write_i2c
;
217 return _process_sigma_firmware(&client
->dev
, &ssfw
, name
);
219 EXPORT_SYMBOL(process_sigma_firmware
);
223 #if IS_ENABLED(CONFIG_REGMAP)
225 static int sigma_action_write_regmap(void *control_data
,
226 const struct sigma_action
*sa
, size_t len
)
228 return regmap_raw_write(control_data
, le16_to_cpu(sa
->addr
),
229 sa
->payload
, len
- 2);
232 int process_sigma_firmware_regmap(struct device
*dev
, struct regmap
*regmap
,
235 struct sigma_firmware ssfw
;
237 ssfw
.control_data
= regmap
;
238 ssfw
.write
= sigma_action_write_regmap
;
240 return _process_sigma_firmware(dev
, &ssfw
, name
);
242 EXPORT_SYMBOL(process_sigma_firmware_regmap
);
246 MODULE_LICENSE("GPL");