1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* cx25840 firmware functions
5 #include <linux/module.h>
7 #include <linux/firmware.h>
8 #include <media/v4l2-common.h>
9 #include <media/drv-intf/cx25840.h>
11 #include "cx25840-core.h"
14 * Mike Isely <isely@pobox.com> - The FWSEND parameter controls the
15 * size of the firmware chunks sent down the I2C bus to the chip.
16 * Previously this had been set to 1024 but unfortunately some I2C
17 * implementations can't transfer data in such big gulps.
18 * Specifically, the pvrusb2 driver has a hard limit of around 60
19 * bytes, due to the encapsulation there of I2C traffic into USB
20 * messages. So we have to significantly reduce this parameter.
24 #define FWDEV(x) &((x)->dev)
26 static char *firmware = "";
28 module_param(firmware, charp, 0444);
30 MODULE_PARM_DESC(firmware, "Firmware image to load");
32 static void start_fw_load(struct i2c_client *client)
34 /* DL_ADDR_LB=0 DL_ADDR_HB=0 */
35 cx25840_write(client, 0x800, 0x00);
36 cx25840_write(client, 0x801, 0x00);
37 // DL_MAP=3 DL_AUTO_INC=0 DL_ENABLE=1
38 cx25840_write(client, 0x803, 0x0b);
40 cx25840_write(client, 0x000, 0x20);
43 static void end_fw_load(struct i2c_client *client)
46 cx25840_write(client, 0x000, 0x00);
48 cx25840_write(client, 0x803, 0x03);
51 #define CX2388x_FIRMWARE "/*(DEBLOBBED)*/"
52 #define CX231xx_FIRMWARE "/*(DEBLOBBED)*/"
53 #define CX25840_FIRMWARE "/*(DEBLOBBED)*/"
55 static const char *get_fw_name(struct i2c_client *client)
57 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
61 if (is_cx2388x(state))
62 return CX2388x_FIRMWARE;
63 if (is_cx231xx(state))
64 return CX231xx_FIRMWARE;
65 return CX25840_FIRMWARE;
68 static int check_fw_load(struct i2c_client *client, int size)
70 /* DL_ADDR_HB DL_ADDR_LB */
71 int s = cx25840_read(client, 0x801) << 8;
72 s |= cx25840_read(client, 0x800);
75 v4l_err(client, "firmware %s load failed\n",
80 v4l_info(client, "loaded %s firmware (%d bytes)\n",
81 get_fw_name(client), size);
85 static int fw_write(struct i2c_client *client, const u8 *data, int size)
87 if (i2c_master_send(client, data, size) < size) {
88 v4l_err(client, "firmware load i2c failure\n");
95 int cx25840_loadfw(struct i2c_client *client)
97 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
98 const struct firmware *fw = NULL;
101 const char *fwname = get_fw_name(client);
103 int max_buf_size = FWSEND;
104 u32 gpio_oe = 0, gpio_da = 0;
106 if (is_cx2388x(state)) {
107 /* Preserve the GPIO OE and output bits */
108 gpio_oe = cx25840_read(client, 0x160);
109 gpio_da = cx25840_read(client, 0x164);
112 /* cx231xx cannot accept more than 16 bytes at a time */
113 if (is_cx231xx(state) && max_buf_size > 16)
116 if (reject_firmware(&fw, fwname, FWDEV(client)) != 0) {
117 v4l_err(client, "unable to open firmware %s\n", fwname);
121 start_fw_load(client);
129 int len = min(max_buf_size - 2, size);
131 memcpy(buffer + 2, ptr, len);
133 retval = fw_write(client, buffer, len + 2);
136 release_firmware(fw);
147 release_firmware(fw);
149 if (is_cx2388x(state)) {
150 /* Restore GPIO configuration after f/w load */
151 cx25840_write(client, 0x160, gpio_oe);
152 cx25840_write(client, 0x164, gpio_da);
155 return check_fw_load(client, size);