blob: f59ced181c553a074da086ee4c630903c1d9879a [file] [log] [blame]
Hans Verkuilbd985162005-11-13 16:07:56 -08001/* 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 * 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.
16 */
17
Hans Verkuilbd985162005-11-13 16:07:56 -080018#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>
Hans Verkuil31bc09b2006-03-25 10:26:09 -030023#include <media/cx25840.h>
Hans Verkuilbd985162005-11-13 16:07:56 -080024
Hans Verkuil31bc09b2006-03-25 10:26:09 -030025#include "cx25840-core.h"
Hans Verkuilbd985162005-11-13 16:07:56 -080026
Hans Verkuil60f6c462005-11-13 16:08:12 -080027#define FWFILE "v4l-cx25840.fw"
Mike Isely4263fa82006-03-25 20:43:14 -030028
29/*
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.
37 */
38#define FWSEND 48
Hans Verkuilbd985162005-11-13 16:07:56 -080039
40#define FWDEV(x) &((x)->adapter->dev)
41
42static int fastfw = 1;
43static char *firmware = FWFILE;
44
45module_param(fastfw, bool, 0444);
46module_param(firmware, charp, 0444);
47
48MODULE_PARM_DESC(fastfw, "Load firmware fast [0=100MHz 1=333MHz (default)]");
49MODULE_PARM_DESC(firmware, "Firmware image [default: " FWFILE "]");
50
Hans Verkuild92c20e2006-01-09 15:32:41 -020051static void set_i2c_delay(struct i2c_client *client, int delay)
Hans Verkuilbd985162005-11-13 16:07:56 -080052{
53 struct i2c_algo_bit_data *algod = client->adapter->algo_data;
54
55 /* We aren't guaranteed to be using algo_bit,
56 * so avoid the null pointer dereference
57 * and disable the 'fast firmware load' */
58 if (algod) {
59 algod->udelay = delay;
60 } else {
61 fastfw = 0;
62 }
63}
64
Hans Verkuild92c20e2006-01-09 15:32:41 -020065static void start_fw_load(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -080066{
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);
72 /* AUTO_INC_DIS=1 */
73 cx25840_write(client, 0x000, 0x20);
74
75 if (fastfw)
76 set_i2c_delay(client, 3);
77}
78
Hans Verkuild92c20e2006-01-09 15:32:41 -020079static void end_fw_load(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -080080{
81 if (fastfw)
82 set_i2c_delay(client, 10);
83
84 /* AUTO_INC_DIS=0 */
85 cx25840_write(client, 0x000, 0x00);
86 /* DL_ENABLE=0 */
87 cx25840_write(client, 0x803, 0x03);
88}
89
Hans Verkuild92c20e2006-01-09 15:32:41 -020090static int check_fw_load(struct i2c_client *client, int size)
Hans Verkuilbd985162005-11-13 16:07:56 -080091{
92 /* DL_ADDR_HB DL_ADDR_LB */
93 int s = cx25840_read(client, 0x801) << 8;
94 s |= cx25840_read(client, 0x800);
95
96 if (size != s) {
Hans Verkuilfac9e892006-01-09 15:32:40 -020097 v4l_err(client, "firmware %s load failed\n", firmware);
Hans Verkuilbd985162005-11-13 16:07:56 -080098 return -EINVAL;
99 }
100
Hans Verkuilfac9e892006-01-09 15:32:40 -0200101 v4l_info(client, "loaded %s firmware (%d bytes)\n", firmware, size);
Hans Verkuilbd985162005-11-13 16:07:56 -0800102 return 0;
103}
104
Hans Verkuild92c20e2006-01-09 15:32:41 -0200105static int fw_write(struct i2c_client *client, u8 * data, int size)
Hans Verkuilbd985162005-11-13 16:07:56 -0800106{
Tyler Trafford210e2072006-01-09 15:25:29 -0200107 int sent;
108
109 if ((sent = i2c_master_send(client, data, size)) < size) {
Hans Verkuilbd985162005-11-13 16:07:56 -0800110
111 if (fastfw) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200112 v4l_err(client, "333MHz i2c firmware load failed\n");
Hans Verkuilbd985162005-11-13 16:07:56 -0800113 fastfw = 0;
114 set_i2c_delay(client, 10);
115
Tyler Trafford210e2072006-01-09 15:25:29 -0200116 if (sent > 2) {
117 u16 dl_addr = cx25840_read(client, 0x801) << 8;
118 dl_addr |= cx25840_read(client, 0x800);
119 dl_addr -= sent - 2;
120 cx25840_write(client, 0x801, dl_addr >> 8);
121 cx25840_write(client, 0x800, dl_addr & 0xff);
122 }
123
Hans Verkuilbd985162005-11-13 16:07:56 -0800124 if (i2c_master_send(client, data, size) < size) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200125 v4l_err(client, "100MHz i2c firmware load failed\n");
Hans Verkuilbd985162005-11-13 16:07:56 -0800126 return -ENOSYS;
127 }
128
129 } else {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200130 v4l_err(client, "firmware load i2c failure\n");
Hans Verkuilbd985162005-11-13 16:07:56 -0800131 return -ENOSYS;
132 }
133
134 }
135
136 return 0;
137}
138
139int cx25840_loadfw(struct i2c_client *client)
140{
141 const struct firmware *fw = NULL;
142 u8 buffer[4], *ptr;
143 int size, send, retval;
144
145 if (request_firmware(&fw, firmware, FWDEV(client)) != 0) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200146 v4l_err(client, "unable to open firmware %s\n", firmware);
Hans Verkuilbd985162005-11-13 16:07:56 -0800147 return -EINVAL;
148 }
149
150 start_fw_load(client);
151
152 buffer[0] = 0x08;
153 buffer[1] = 0x02;
154 buffer[2] = fw->data[0];
155 buffer[3] = fw->data[1];
156 retval = fw_write(client, buffer, 4);
157
158 if (retval < 0) {
159 release_firmware(fw);
160 return retval;
161 }
162
163 size = fw->size - 2;
164 ptr = fw->data;
165 while (size > 0) {
166 ptr[0] = 0x08;
167 ptr[1] = 0x02;
168 send = size > (FWSEND - 2) ? FWSEND : size + 2;
169 retval = fw_write(client, ptr, send);
170
171 if (retval < 0) {
172 release_firmware(fw);
173 return retval;
174 }
175
176 size -= FWSEND - 2;
177 ptr += FWSEND - 2;
178 }
179
180 end_fw_load(client);
181
182 size = fw->size;
183 release_firmware(fw);
184
185 return check_fw_load(client, size);
186}