blob: 1958d4016ea15c4bb5b9e7faa59377fe80d9a4f6 [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
Hans Verkuilbd985162005-11-13 16:07:56 -080042static char *firmware = FWFILE;
43
Hans Verkuilbd985162005-11-13 16:07:56 -080044module_param(firmware, charp, 0444);
45
Hans Verkuilbd985162005-11-13 16:07:56 -080046MODULE_PARM_DESC(firmware, "Firmware image [default: " FWFILE "]");
47
Hans Verkuild92c20e2006-01-09 15:32:41 -020048static void start_fw_load(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -080049{
50 /* DL_ADDR_LB=0 DL_ADDR_HB=0 */
51 cx25840_write(client, 0x800, 0x00);
52 cx25840_write(client, 0x801, 0x00);
53 // DL_MAP=3 DL_AUTO_INC=0 DL_ENABLE=1
54 cx25840_write(client, 0x803, 0x0b);
55 /* AUTO_INC_DIS=1 */
56 cx25840_write(client, 0x000, 0x20);
Hans Verkuilbd985162005-11-13 16:07:56 -080057}
58
Hans Verkuild92c20e2006-01-09 15:32:41 -020059static void end_fw_load(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -080060{
Hans Verkuilbd985162005-11-13 16:07:56 -080061 /* AUTO_INC_DIS=0 */
62 cx25840_write(client, 0x000, 0x00);
63 /* DL_ENABLE=0 */
64 cx25840_write(client, 0x803, 0x03);
65}
66
Hans Verkuild92c20e2006-01-09 15:32:41 -020067static int check_fw_load(struct i2c_client *client, int size)
Hans Verkuilbd985162005-11-13 16:07:56 -080068{
69 /* DL_ADDR_HB DL_ADDR_LB */
70 int s = cx25840_read(client, 0x801) << 8;
71 s |= cx25840_read(client, 0x800);
72
73 if (size != s) {
Hans Verkuilfac9e892006-01-09 15:32:40 -020074 v4l_err(client, "firmware %s load failed\n", firmware);
Hans Verkuilbd985162005-11-13 16:07:56 -080075 return -EINVAL;
76 }
77
Hans Verkuilfac9e892006-01-09 15:32:40 -020078 v4l_info(client, "loaded %s firmware (%d bytes)\n", firmware, size);
Hans Verkuilbd985162005-11-13 16:07:56 -080079 return 0;
80}
81
Hans Verkuild92c20e2006-01-09 15:32:41 -020082static int fw_write(struct i2c_client *client, u8 * data, int size)
Hans Verkuilbd985162005-11-13 16:07:56 -080083{
Tyler Trafford210e2072006-01-09 15:25:29 -020084 int sent;
85
86 if ((sent = i2c_master_send(client, data, size)) < size) {
Hans Verkuil7d16eaa2006-04-23 05:54:56 -030087 v4l_err(client, "firmware load i2c failure\n");
88 return -ENOSYS;
Hans Verkuilbd985162005-11-13 16:07:56 -080089 }
90
91 return 0;
92}
93
94int cx25840_loadfw(struct i2c_client *client)
95{
96 const struct firmware *fw = NULL;
97 u8 buffer[4], *ptr;
98 int size, send, retval;
99
100 if (request_firmware(&fw, firmware, FWDEV(client)) != 0) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200101 v4l_err(client, "unable to open firmware %s\n", firmware);
Hans Verkuilbd985162005-11-13 16:07:56 -0800102 return -EINVAL;
103 }
104
105 start_fw_load(client);
106
107 buffer[0] = 0x08;
108 buffer[1] = 0x02;
109 buffer[2] = fw->data[0];
110 buffer[3] = fw->data[1];
111 retval = fw_write(client, buffer, 4);
112
113 if (retval < 0) {
114 release_firmware(fw);
115 return retval;
116 }
117
118 size = fw->size - 2;
119 ptr = fw->data;
120 while (size > 0) {
121 ptr[0] = 0x08;
122 ptr[1] = 0x02;
123 send = size > (FWSEND - 2) ? FWSEND : size + 2;
124 retval = fw_write(client, ptr, send);
125
126 if (retval < 0) {
127 release_firmware(fw);
128 return retval;
129 }
130
131 size -= FWSEND - 2;
132 ptr += FWSEND - 2;
133 }
134
135 end_fw_load(client);
136
137 size = fw->size;
138 release_firmware(fw);
139
140 return check_fw_load(client, size);
141}