1 /* cx25840 firmware functions
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.
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.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #include <linux/module.h>
19 #include <linux/i2c.h>
20 #include <linux/i2c-algo-bit.h>
21 #include <linux/firmware.h>
22 #include <media/v4l2-common.h>
23 #include <media/cx25840.h>
25 #include "cx25840-core.h"
27 #define FWFILE "v4l-cx25840.fw"
30 * Mike Isely <isely@pobox.com> - The FWSEND parameter controls the
31 * size of the firmware chunks sent down the I2C bus to the chip.
32 * Previously this had been set to 1024 but unfortunately some I2C
33 * implementations can't transfer data in such big gulps.
34 * Specifically, the pvrusb2 driver has a hard limit of around 60
35 * bytes, due to the encapsulation there of I2C traffic into USB
36 * messages. So we have to significantly reduce this parameter.
40 #define FWDEV(x) &((x)->adapter->dev)
42 static int fastfw = 1;
43 static char *firmware = FWFILE;
45 module_param(fastfw, bool, 0444);
46 module_param(firmware, charp, 0444);
48 MODULE_PARM_DESC(fastfw, "Load firmware fast [0=100MHz 1=333MHz (default)]");
49 MODULE_PARM_DESC(firmware, "Firmware image [default: " FWFILE "]");
51 static void set_i2c_delay(struct i2c_client *client, int delay)
53 struct i2c_algo_bit_data *algod = client->adapter->algo_data;
55 /* We aren't guaranteed to be using algo_bit,
56 * so avoid the null pointer dereference
57 * and disable the 'fast firmware load' */
59 algod->udelay = delay;
65 static void start_fw_load(struct i2c_client *client)
67 /* DL_ADDR_LB=0 DL_ADDR_HB=0 */
68 cx25840_write(client, 0x800, 0x00);
69 cx25840_write(client, 0x801, 0x00);
70 // DL_MAP=3 DL_AUTO_INC=0 DL_ENABLE=1
71 cx25840_write(client, 0x803, 0x0b);
73 cx25840_write(client, 0x000, 0x20);
76 set_i2c_delay(client, 3);
79 static void end_fw_load(struct i2c_client *client)
82 set_i2c_delay(client, 10);
85 cx25840_write(client, 0x000, 0x00);
87 cx25840_write(client, 0x803, 0x03);
90 static int check_fw_load(struct i2c_client *client, int size)
92 /* DL_ADDR_HB DL_ADDR_LB */
93 int s = cx25840_read(client, 0x801) << 8;
94 s |= cx25840_read(client, 0x800);
97 v4l_err(client, "firmware %s load failed\n", firmware);
101 v4l_info(client, "loaded %s firmware (%d bytes)\n", firmware, size);
105 static int fw_write(struct i2c_client *client, u8 * data, int size)
109 if ((sent = i2c_master_send(client, data, size)) < size) {
112 v4l_err(client, "333MHz i2c firmware load failed\n");
114 set_i2c_delay(client, 10);
117 u16 dl_addr = cx25840_read(client, 0x801) << 8;
118 dl_addr |= cx25840_read(client, 0x800);
120 cx25840_write(client, 0x801, dl_addr >> 8);
121 cx25840_write(client, 0x800, dl_addr & 0xff);
124 if (i2c_master_send(client, data, size) < size) {
125 v4l_err(client, "100MHz i2c firmware load failed\n");
130 v4l_err(client, "firmware load i2c failure\n");
139 int cx25840_loadfw(struct i2c_client *client)
141 const struct firmware *fw = NULL;
143 int size, send, retval;
145 if (request_firmware(&fw, firmware, FWDEV(client)) != 0) {
146 v4l_err(client, "unable to open firmware %s\n", firmware);
150 start_fw_load(client);
154 buffer[2] = fw->data[0];
155 buffer[3] = fw->data[1];
156 retval = fw_write(client, buffer, 4);
159 release_firmware(fw);
168 send = size > (FWSEND - 2) ? FWSEND : size + 2;
169 retval = fw_write(client, ptr, send);
172 release_firmware(fw);
183 release_firmware(fw);
185 return check_fw_load(client, size);