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