Merge branch 'for-linus' of master.kernel.org:/home/rmk/linux-2.6-arm
[sfrench/cifs-2.6.git] / drivers / media / video / cx25840 / cx25840-firmware.c
1 /* 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
18 #include <linux/module.h>
19 #include <linux/i2c.h>
20 #include <linux/firmware.h>
21 #include <media/v4l2-common.h>
22 #include <media/cx25840.h>
23
24 #include "cx25840-core.h"
25
26 #define FWFILE "v4l-cx25840.fw"
27
28 /*
29  * Mike Isely <isely@pobox.com> - The FWSEND parameter controls the
30  * size of the firmware chunks sent down the I2C bus to the chip.
31  * Previously this had been set to 1024 but unfortunately some I2C
32  * implementations can't transfer data in such big gulps.
33  * Specifically, the pvrusb2 driver has a hard limit of around 60
34  * bytes, due to the encapsulation there of I2C traffic into USB
35  * messages.  So we have to significantly reduce this parameter.
36  */
37 #define FWSEND 48
38
39 #define FWDEV(x) &((x)->dev)
40
41 static char *firmware = FWFILE;
42
43 module_param(firmware, charp, 0444);
44
45 MODULE_PARM_DESC(firmware, "Firmware image [default: " FWFILE "]");
46
47 static void start_fw_load(struct i2c_client *client)
48 {
49         /* DL_ADDR_LB=0 DL_ADDR_HB=0 */
50         cx25840_write(client, 0x800, 0x00);
51         cx25840_write(client, 0x801, 0x00);
52         // DL_MAP=3 DL_AUTO_INC=0 DL_ENABLE=1
53         cx25840_write(client, 0x803, 0x0b);
54         /* AUTO_INC_DIS=1 */
55         cx25840_write(client, 0x000, 0x20);
56 }
57
58 static void end_fw_load(struct i2c_client *client)
59 {
60         /* AUTO_INC_DIS=0 */
61         cx25840_write(client, 0x000, 0x00);
62         /* DL_ENABLE=0 */
63         cx25840_write(client, 0x803, 0x03);
64 }
65
66 static int check_fw_load(struct i2c_client *client, int size)
67 {
68         /* DL_ADDR_HB DL_ADDR_LB */
69         int s = cx25840_read(client, 0x801) << 8;
70         s |= cx25840_read(client, 0x800);
71
72         if (size != s) {
73                 v4l_err(client, "firmware %s load failed\n", firmware);
74                 return -EINVAL;
75         }
76
77         v4l_info(client, "loaded %s firmware (%d bytes)\n", firmware, size);
78         return 0;
79 }
80
81 static int fw_write(struct i2c_client *client, u8 * data, int size)
82 {
83         int sent;
84
85         if ((sent = i2c_master_send(client, data, size)) < size) {
86                 v4l_err(client, "firmware load i2c failure\n");
87                 return -ENOSYS;
88         }
89
90         return 0;
91 }
92
93 int cx25840_loadfw(struct i2c_client *client)
94 {
95         const struct firmware *fw = NULL;
96         u8 buffer[4], *ptr;
97         int size, send, retval;
98
99         if (request_firmware(&fw, firmware, FWDEV(client)) != 0) {
100                 v4l_err(client, "unable to open firmware %s\n", firmware);
101                 return -EINVAL;
102         }
103
104         start_fw_load(client);
105
106         buffer[0] = 0x08;
107         buffer[1] = 0x02;
108         buffer[2] = fw->data[0];
109         buffer[3] = fw->data[1];
110         retval = fw_write(client, buffer, 4);
111
112         if (retval < 0) {
113                 release_firmware(fw);
114                 return retval;
115         }
116
117         size = fw->size - 2;
118         ptr = fw->data;
119         while (size > 0) {
120                 ptr[0] = 0x08;
121                 ptr[1] = 0x02;
122                 send = size > (FWSEND - 2) ? FWSEND : size + 2;
123                 retval = fw_write(client, ptr, send);
124
125                 if (retval < 0) {
126                         release_firmware(fw);
127                         return retval;
128                 }
129
130                 size -= FWSEND - 2;
131                 ptr += FWSEND - 2;
132         }
133
134         end_fw_load(client);
135
136         size = fw->size;
137         release_firmware(fw);
138
139         return check_fw_load(client, size);
140 }