1 /* cx25840 firmware functions
2  *
3  * This program is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public License
5  * as published by the Free Software Foundation; either version 2
6  * of the License, or (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  */
13 
14 #include <linux/module.h>
15 #include <linux/i2c.h>
16 #include <linux/firmware.h>
17 #include <media/v4l2-common.h>
18 #include <media/drv-intf/cx25840.h>
19 
20 #include "cx25840-core.h"
21 
22 /*
23  * Mike Isely <isely@pobox.com> - The FWSEND parameter controls the
24  * size of the firmware chunks sent down the I2C bus to the chip.
25  * Previously this had been set to 1024 but unfortunately some I2C
26  * implementations can't transfer data in such big gulps.
27  * Specifically, the pvrusb2 driver has a hard limit of around 60
28  * bytes, due to the encapsulation there of I2C traffic into USB
29  * messages.  So we have to significantly reduce this parameter.
30  */
31 #define FWSEND 48
32 
33 #define FWDEV(x) &((x)->dev)
34 
35 static char *firmware = "";
36 
37 module_param(firmware, charp, 0444);
38 
39 MODULE_PARM_DESC(firmware, "Firmware image to load");
40 
41 static void start_fw_load(struct i2c_client *client)
42 {
43 	/* DL_ADDR_LB=0 DL_ADDR_HB=0 */
44 	cx25840_write(client, 0x800, 0x00);
45 	cx25840_write(client, 0x801, 0x00);
46 	// DL_MAP=3 DL_AUTO_INC=0 DL_ENABLE=1
47 	cx25840_write(client, 0x803, 0x0b);
48 	/* AUTO_INC_DIS=1 */
49 	cx25840_write(client, 0x000, 0x20);
50 }
51 
52 static void end_fw_load(struct i2c_client *client)
53 {
54 	/* AUTO_INC_DIS=0 */
55 	cx25840_write(client, 0x000, 0x00);
56 	/* DL_ENABLE=0 */
57 	cx25840_write(client, 0x803, 0x03);
58 }
59 
60 #define CX2388x_FIRMWARE "v4l-cx23885-avcore-01.fw"
61 #define CX231xx_FIRMWARE "v4l-cx231xx-avcore-01.fw"
62 #define CX25840_FIRMWARE "v4l-cx25840.fw"
63 
64 static const char *get_fw_name(struct i2c_client *client)
65 {
66 	struct cx25840_state *state = to_state(i2c_get_clientdata(client));
67 
68 	if (firmware[0])
69 		return firmware;
70 	if (is_cx2388x(state))
71 		return CX2388x_FIRMWARE;
72 	if (is_cx231xx(state))
73 		return CX231xx_FIRMWARE;
74 	return CX25840_FIRMWARE;
75 }
76 
77 static int check_fw_load(struct i2c_client *client, int size)
78 {
79 	/* DL_ADDR_HB DL_ADDR_LB */
80 	int s = cx25840_read(client, 0x801) << 8;
81 	s |= cx25840_read(client, 0x800);
82 
83 	if (size != s) {
84 		v4l_err(client, "firmware %s load failed\n",
85 				get_fw_name(client));
86 		return -EINVAL;
87 	}
88 
89 	v4l_info(client, "loaded %s firmware (%d bytes)\n",
90 			get_fw_name(client), size);
91 	return 0;
92 }
93 
94 static int fw_write(struct i2c_client *client, const u8 *data, int size)
95 {
96 	if (i2c_master_send(client, data, size) < size) {
97 		v4l_err(client, "firmware load i2c failure\n");
98 		return -ENOSYS;
99 	}
100 
101 	return 0;
102 }
103 
104 int cx25840_loadfw(struct i2c_client *client)
105 {
106 	struct cx25840_state *state = to_state(i2c_get_clientdata(client));
107 	const struct firmware *fw = NULL;
108 	u8 buffer[FWSEND];
109 	const u8 *ptr;
110 	const char *fwname = get_fw_name(client);
111 	int size, retval;
112 	int max_buf_size = FWSEND;
113 	u32 gpio_oe = 0, gpio_da = 0;
114 
115 	if (is_cx2388x(state)) {
116 		/* Preserve the GPIO OE and output bits */
117 		gpio_oe = cx25840_read(client, 0x160);
118 		gpio_da = cx25840_read(client, 0x164);
119 	}
120 
121 	/* cx231xx cannot accept more than 16 bytes at a time */
122 	if (is_cx231xx(state) && max_buf_size > 16)
123 		max_buf_size = 16;
124 
125 	if (request_firmware(&fw, fwname, FWDEV(client)) != 0) {
126 		v4l_err(client, "unable to open firmware %s\n", fwname);
127 		return -EINVAL;
128 	}
129 
130 	start_fw_load(client);
131 
132 	buffer[0] = 0x08;
133 	buffer[1] = 0x02;
134 
135 	size = fw->size;
136 	ptr = fw->data;
137 	while (size > 0) {
138 		int len = min(max_buf_size - 2, size);
139 
140 		memcpy(buffer + 2, ptr, len);
141 
142 		retval = fw_write(client, buffer, len + 2);
143 
144 		if (retval < 0) {
145 			release_firmware(fw);
146 			return retval;
147 		}
148 
149 		size -= len;
150 		ptr += len;
151 	}
152 
153 	end_fw_load(client);
154 
155 	size = fw->size;
156 	release_firmware(fw);
157 
158 	if (is_cx2388x(state)) {
159 		/* Restore GPIO configuration after f/w load */
160 		cx25840_write(client, 0x160, gpio_oe);
161 		cx25840_write(client, 0x164, gpio_da);
162 	}
163 
164 	return check_fw_load(client, size);
165 }
166 
167 MODULE_FIRMWARE(CX2388x_FIRMWARE);
168 MODULE_FIRMWARE(CX231xx_FIRMWARE);
169 MODULE_FIRMWARE(CX25840_FIRMWARE);
170 
171