1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* cx25840 firmware functions
3  */
4 
5 #include <linux/module.h>
6 #include <linux/i2c.h>
7 #include <linux/firmware.h>
8 #include <media/v4l2-common.h>
9 #include <media/drv-intf/cx25840.h>
10 
11 #include "cx25840-core.h"
12 
13 /*
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.
21  */
22 #define FWSEND 48
23 
24 #define FWDEV(x) &((x)->dev)
25 
26 static char *firmware = "";
27 
28 module_param(firmware, charp, 0444);
29 
30 MODULE_PARM_DESC(firmware, "Firmware image to load");
31 
32 static void start_fw_load(struct i2c_client *client)
33 {
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);
39 	/* AUTO_INC_DIS=1 */
40 	cx25840_write(client, 0x000, 0x20);
41 }
42 
43 static void end_fw_load(struct i2c_client *client)
44 {
45 	/* AUTO_INC_DIS=0 */
46 	cx25840_write(client, 0x000, 0x00);
47 	/* DL_ENABLE=0 */
48 	cx25840_write(client, 0x803, 0x03);
49 }
50 
51 #define CX2388x_FIRMWARE "v4l-cx23885-avcore-01.fw"
52 #define CX231xx_FIRMWARE "v4l-cx231xx-avcore-01.fw"
53 #define CX25840_FIRMWARE "v4l-cx25840.fw"
54 
55 static const char *get_fw_name(struct i2c_client *client)
56 {
57 	struct cx25840_state *state = to_state(i2c_get_clientdata(client));
58 
59 	if (firmware[0])
60 		return firmware;
61 	if (is_cx2388x(state))
62 		return CX2388x_FIRMWARE;
63 	if (is_cx231xx(state))
64 		return CX231xx_FIRMWARE;
65 	return CX25840_FIRMWARE;
66 }
67 
68 static int check_fw_load(struct i2c_client *client, int size)
69 {
70 	/* DL_ADDR_HB DL_ADDR_LB */
71 	int s = cx25840_read(client, 0x801) << 8;
72 	s |= cx25840_read(client, 0x800);
73 
74 	if (size != s) {
75 		v4l_err(client, "firmware %s load failed\n",
76 				get_fw_name(client));
77 		return -EINVAL;
78 	}
79 
80 	v4l_info(client, "loaded %s firmware (%d bytes)\n",
81 			get_fw_name(client), size);
82 	return 0;
83 }
84 
85 static int fw_write(struct i2c_client *client, const u8 *data, int size)
86 {
87 	if (i2c_master_send(client, data, size) < size) {
88 		v4l_err(client, "firmware load i2c failure\n");
89 		return -ENOSYS;
90 	}
91 
92 	return 0;
93 }
94 
95 int cx25840_loadfw(struct i2c_client *client)
96 {
97 	struct cx25840_state *state = to_state(i2c_get_clientdata(client));
98 	const struct firmware *fw = NULL;
99 	u8 buffer[FWSEND];
100 	const u8 *ptr;
101 	const char *fwname = get_fw_name(client);
102 	int size, retval;
103 	int max_buf_size = FWSEND;
104 	u32 gpio_oe = 0, gpio_da = 0;
105 
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);
110 	}
111 
112 	/* cx231xx cannot accept more than 16 bytes at a time */
113 	if (is_cx231xx(state) && max_buf_size > 16)
114 		max_buf_size = 16;
115 
116 	if (request_firmware(&fw, fwname, FWDEV(client)) != 0) {
117 		v4l_err(client, "unable to open firmware %s\n", fwname);
118 		return -EINVAL;
119 	}
120 
121 	start_fw_load(client);
122 
123 	buffer[0] = 0x08;
124 	buffer[1] = 0x02;
125 
126 	size = fw->size;
127 	ptr = fw->data;
128 	while (size > 0) {
129 		int len = min(max_buf_size - 2, size);
130 
131 		memcpy(buffer + 2, ptr, len);
132 
133 		retval = fw_write(client, buffer, len + 2);
134 
135 		if (retval < 0) {
136 			release_firmware(fw);
137 			return retval;
138 		}
139 
140 		size -= len;
141 		ptr += len;
142 	}
143 
144 	end_fw_load(client);
145 
146 	size = fw->size;
147 	release_firmware(fw);
148 
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);
153 	}
154 
155 	return check_fw_load(client, size);
156 }
157 
158 MODULE_FIRMWARE(CX2388x_FIRMWARE);
159 MODULE_FIRMWARE(CX231xx_FIRMWARE);
160 MODULE_FIRMWARE(CX25840_FIRMWARE);
161 
162