Merge branch 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mchehab...
[pandora-kernel.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 #define FWFILE_CX23885 "v4l-cx23885-avcore-01.fw"
28 #define FWFILE_CX231XX "v4l-cx231xx-avcore-01.fw"
29
30 /*
31  * Mike Isely <isely@pobox.com> - The FWSEND parameter controls the
32  * size of the firmware chunks sent down the I2C bus to the chip.
33  * Previously this had been set to 1024 but unfortunately some I2C
34  * implementations can't transfer data in such big gulps.
35  * Specifically, the pvrusb2 driver has a hard limit of around 60
36  * bytes, due to the encapsulation there of I2C traffic into USB
37  * messages.  So we have to significantly reduce this parameter.
38  */
39 #define FWSEND 48
40
41 #define FWDEV(x) &((x)->dev)
42
43 static char *firmware = FWFILE;
44
45 module_param(firmware, charp, 0444);
46
47 MODULE_PARM_DESC(firmware, "Firmware image [default: " FWFILE "]");
48
49 static void start_fw_load(struct i2c_client *client)
50 {
51         /* DL_ADDR_LB=0 DL_ADDR_HB=0 */
52         cx25840_write(client, 0x800, 0x00);
53         cx25840_write(client, 0x801, 0x00);
54         // DL_MAP=3 DL_AUTO_INC=0 DL_ENABLE=1
55         cx25840_write(client, 0x803, 0x0b);
56         /* AUTO_INC_DIS=1 */
57         cx25840_write(client, 0x000, 0x20);
58 }
59
60 static void end_fw_load(struct i2c_client *client)
61 {
62         /* AUTO_INC_DIS=0 */
63         cx25840_write(client, 0x000, 0x00);
64         /* DL_ENABLE=0 */
65         cx25840_write(client, 0x803, 0x03);
66 }
67
68 static int check_fw_load(struct i2c_client *client, int size)
69 {
70         /* DL_ADDR_HB DL_ADDR_LB */
71         int s = cx25840_read(client, 0x801) << 8;
72         s |= cx25840_read(client, 0x800);
73
74         if (size != s) {
75                 v4l_err(client, "firmware %s load failed\n", firmware);
76                 return -EINVAL;
77         }
78
79         v4l_info(client, "loaded %s firmware (%d bytes)\n", firmware, size);
80         return 0;
81 }
82
83 static int fw_write(struct i2c_client *client, const u8 *data, int size)
84 {
85         if (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         struct cx25840_state *state = to_state(i2c_get_clientdata(client));
96         const struct firmware *fw = NULL;
97         u8 buffer[FWSEND];
98         const u8 *ptr;
99         int size, retval;
100         int MAX_BUF_SIZE = FWSEND;
101
102         if (state->is_cx23885)
103                 firmware = FWFILE_CX23885;
104         else if (state->is_cx231xx)
105                 firmware = FWFILE_CX231XX;
106
107         if ((state->is_cx231xx) && MAX_BUF_SIZE > 16) {
108                 v4l_err(client, " Firmware download size changed to 16 bytes max length\n");
109                 MAX_BUF_SIZE = 16;  /* cx231xx cannot accept more than 16 bytes at a time */
110         }
111
112         if (request_firmware(&fw, firmware, FWDEV(client)) != 0) {
113                 v4l_err(client, "unable to open firmware %s\n", firmware);
114                 return -EINVAL;
115         }
116
117         start_fw_load(client);
118
119         buffer[0] = 0x08;
120         buffer[1] = 0x02;
121
122         size = fw->size;
123         ptr = fw->data;
124         while (size > 0) {
125                 int len = min(MAX_BUF_SIZE - 2, size);
126
127                 memcpy(buffer + 2, ptr, len);
128
129                 retval = fw_write(client, buffer, len + 2);
130
131                 if (retval < 0) {
132                         release_firmware(fw);
133                         return retval;
134                 }
135
136                 size -= len;
137                 ptr += len;
138         }
139
140         end_fw_load(client);
141
142         size = fw->size;
143         release_firmware(fw);
144
145         return check_fw_load(client, size);
146 }