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.
19 #include <linux/module.h>
20 #include <linux/i2c.h>
21 #include <linux/i2c-algo-bit.h>
22 #include <linux/firmware.h>
23 #include <media/v4l2-common.h>
27 #define FWFILE "v4l-cx25840.fw"
30 #define FWDEV(x) &((x)->adapter->dev)
32 static int fastfw = 1;
33 static char *firmware = FWFILE;
35 module_param(fastfw, bool, 0444);
36 module_param(firmware, charp, 0444);
38 MODULE_PARM_DESC(fastfw, "Load firmware fast [0=100MHz 1=333MHz (default)]");
39 MODULE_PARM_DESC(firmware, "Firmware image [default: " FWFILE "]");
41 static inline void set_i2c_delay(struct i2c_client *client, int delay)
43 struct i2c_algo_bit_data *algod = client->adapter->algo_data;
45 /* We aren't guaranteed to be using algo_bit,
46 * so avoid the null pointer dereference
47 * and disable the 'fast firmware load' */
49 algod->udelay = delay;
55 static inline void start_fw_load(struct i2c_client *client)
57 /* DL_ADDR_LB=0 DL_ADDR_HB=0 */
58 cx25840_write(client, 0x800, 0x00);
59 cx25840_write(client, 0x801, 0x00);
60 // DL_MAP=3 DL_AUTO_INC=0 DL_ENABLE=1
61 cx25840_write(client, 0x803, 0x0b);
63 cx25840_write(client, 0x000, 0x20);
66 set_i2c_delay(client, 3);
69 static inline void end_fw_load(struct i2c_client *client)
72 set_i2c_delay(client, 10);
75 cx25840_write(client, 0x000, 0x00);
77 cx25840_write(client, 0x803, 0x03);
80 static inline int check_fw_load(struct i2c_client *client, int size)
82 /* DL_ADDR_HB DL_ADDR_LB */
83 int s = cx25840_read(client, 0x801) << 8;
84 s |= cx25840_read(client, 0x800);
87 cx25840_err("firmware %s load failed\n", firmware);
91 cx25840_info("loaded %s firmware (%d bytes)\n", firmware, size);
95 static inline int fw_write(struct i2c_client *client, u8 * data, int size)
97 if (i2c_master_send(client, data, size) < size) {
100 cx25840_err("333MHz i2c firmware load failed\n");
102 set_i2c_delay(client, 10);
104 if (i2c_master_send(client, data, size) < size) {
106 ("100MHz i2c firmware load failed\n");
111 cx25840_err("firmware load i2c failure\n");
120 int cx25840_loadfw(struct i2c_client *client)
122 const struct firmware *fw = NULL;
124 int size, send, retval;
126 if (request_firmware(&fw, firmware, FWDEV(client)) != 0) {
127 cx25840_err("unable to open firmware %s\n", firmware);
131 start_fw_load(client);
135 buffer[2] = fw->data[0];
136 buffer[3] = fw->data[1];
137 retval = fw_write(client, buffer, 4);
140 release_firmware(fw);
149 send = size > (FWSEND - 2) ? FWSEND : size + 2;
150 retval = fw_write(client, ptr, send);
153 release_firmware(fw);
164 release_firmware(fw);
166 return check_fw_load(client, size);