blob: e1a7823d82cdeee0e81d3d44b664a72f8da1a93d [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>
23
24#include "cx25840.h"
25
Hans Verkuil60f6c462005-11-13 16:08:12 -080026#define FWFILE "v4l-cx25840.fw"
Hans Verkuilbd985162005-11-13 16:07:56 -080027#define FWSEND 1024
28
29#define FWDEV(x) &((x)->adapter->dev)
30
31static int fastfw = 1;
32static char *firmware = FWFILE;
33
34module_param(fastfw, bool, 0444);
35module_param(firmware, charp, 0444);
36
37MODULE_PARM_DESC(fastfw, "Load firmware fast [0=100MHz 1=333MHz (default)]");
38MODULE_PARM_DESC(firmware, "Firmware image [default: " FWFILE "]");
39
Hans Verkuild92c20e2006-01-09 15:32:41 -020040static void set_i2c_delay(struct i2c_client *client, int delay)
Hans Verkuilbd985162005-11-13 16:07:56 -080041{
42 struct i2c_algo_bit_data *algod = client->adapter->algo_data;
43
44 /* We aren't guaranteed to be using algo_bit,
45 * so avoid the null pointer dereference
46 * and disable the 'fast firmware load' */
47 if (algod) {
48 algod->udelay = delay;
49 } else {
50 fastfw = 0;
51 }
52}
53
Hans Verkuild92c20e2006-01-09 15:32:41 -020054static void start_fw_load(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -080055{
56 /* DL_ADDR_LB=0 DL_ADDR_HB=0 */
57 cx25840_write(client, 0x800, 0x00);
58 cx25840_write(client, 0x801, 0x00);
59 // DL_MAP=3 DL_AUTO_INC=0 DL_ENABLE=1
60 cx25840_write(client, 0x803, 0x0b);
61 /* AUTO_INC_DIS=1 */
62 cx25840_write(client, 0x000, 0x20);
63
64 if (fastfw)
65 set_i2c_delay(client, 3);
66}
67
Hans Verkuild92c20e2006-01-09 15:32:41 -020068static void end_fw_load(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -080069{
70 if (fastfw)
71 set_i2c_delay(client, 10);
72
73 /* AUTO_INC_DIS=0 */
74 cx25840_write(client, 0x000, 0x00);
75 /* DL_ENABLE=0 */
76 cx25840_write(client, 0x803, 0x03);
77}
78
Hans Verkuild92c20e2006-01-09 15:32:41 -020079static int check_fw_load(struct i2c_client *client, int size)
Hans Verkuilbd985162005-11-13 16:07:56 -080080{
81 /* DL_ADDR_HB DL_ADDR_LB */
82 int s = cx25840_read(client, 0x801) << 8;
83 s |= cx25840_read(client, 0x800);
84
85 if (size != s) {
Hans Verkuilfac9e892006-01-09 15:32:40 -020086 v4l_err(client, "firmware %s load failed\n", firmware);
Hans Verkuilbd985162005-11-13 16:07:56 -080087 return -EINVAL;
88 }
89
Hans Verkuilfac9e892006-01-09 15:32:40 -020090 v4l_info(client, "loaded %s firmware (%d bytes)\n", firmware, size);
Hans Verkuilbd985162005-11-13 16:07:56 -080091 return 0;
92}
93
Hans Verkuild92c20e2006-01-09 15:32:41 -020094static int fw_write(struct i2c_client *client, u8 * data, int size)
Hans Verkuilbd985162005-11-13 16:07:56 -080095{
Tyler Trafford210e2072006-01-09 15:25:29 -020096 int sent;
97
98 if ((sent = i2c_master_send(client, data, size)) < size) {
Hans Verkuilbd985162005-11-13 16:07:56 -080099
100 if (fastfw) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200101 v4l_err(client, "333MHz i2c firmware load failed\n");
Hans Verkuilbd985162005-11-13 16:07:56 -0800102 fastfw = 0;
103 set_i2c_delay(client, 10);
104
Tyler Trafford210e2072006-01-09 15:25:29 -0200105 if (sent > 2) {
106 u16 dl_addr = cx25840_read(client, 0x801) << 8;
107 dl_addr |= cx25840_read(client, 0x800);
108 dl_addr -= sent - 2;
109 cx25840_write(client, 0x801, dl_addr >> 8);
110 cx25840_write(client, 0x800, dl_addr & 0xff);
111 }
112
Hans Verkuilbd985162005-11-13 16:07:56 -0800113 if (i2c_master_send(client, data, size) < size) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200114 v4l_err(client, "100MHz i2c firmware load failed\n");
Hans Verkuilbd985162005-11-13 16:07:56 -0800115 return -ENOSYS;
116 }
117
118 } else {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200119 v4l_err(client, "firmware load i2c failure\n");
Hans Verkuilbd985162005-11-13 16:07:56 -0800120 return -ENOSYS;
121 }
122
123 }
124
125 return 0;
126}
127
128int cx25840_loadfw(struct i2c_client *client)
129{
130 const struct firmware *fw = NULL;
131 u8 buffer[4], *ptr;
132 int size, send, retval;
133
134 if (request_firmware(&fw, firmware, FWDEV(client)) != 0) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200135 v4l_err(client, "unable to open firmware %s\n", firmware);
Hans Verkuilbd985162005-11-13 16:07:56 -0800136 return -EINVAL;
137 }
138
139 start_fw_load(client);
140
141 buffer[0] = 0x08;
142 buffer[1] = 0x02;
143 buffer[2] = fw->data[0];
144 buffer[3] = fw->data[1];
145 retval = fw_write(client, buffer, 4);
146
147 if (retval < 0) {
148 release_firmware(fw);
149 return retval;
150 }
151
152 size = fw->size - 2;
153 ptr = fw->data;
154 while (size > 0) {
155 ptr[0] = 0x08;
156 ptr[1] = 0x02;
157 send = size > (FWSEND - 2) ? FWSEND : size + 2;
158 retval = fw_write(client, ptr, send);
159
160 if (retval < 0) {
161 release_firmware(fw);
162 return retval;
163 }
164
165 size -= FWSEND - 2;
166 ptr += FWSEND - 2;
167 }
168
169 end_fw_load(client);
170
171 size = fw->size;
172 release_firmware(fw);
173
174 return check_fw_load(client, size);
175}