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