blob: 2a535d0403ede70e9c3357ec07db3fb919159b7f [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>
Hans Verkuilbd985162005-11-13 16:07:56 -080020#include <linux/firmware.h>
21#include <media/v4l2-common.h>
Hans Verkuil31bc09b2006-03-25 10:26:09 -030022#include <media/cx25840.h>
Hans Verkuilbd985162005-11-13 16:07:56 -080023
Hans Verkuil31bc09b2006-03-25 10:26:09 -030024#include "cx25840-core.h"
Hans Verkuilbd985162005-11-13 16:07:56 -080025
Hans Verkuil60f6c462005-11-13 16:08:12 -080026#define FWFILE "v4l-cx25840.fw"
Steven Tothf2340812008-01-10 01:22:39 -030027#define FWFILE_CX23885 "v4l-cx23885-avcore-01.fw"
Sri Deevi149783b2009-03-03 06:07:42 -030028#define FWFILE_CX231XX "v4l-cx231xx-avcore-01.fw"
Mike Isely4263fa82006-03-25 20:43:14 -030029
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
Hans Verkuilbd985162005-11-13 16:07:56 -080040
Hans Verkuild55c7ae2007-02-15 03:40:34 -030041#define FWDEV(x) &((x)->dev)
Hans Verkuilbd985162005-11-13 16:07:56 -080042
Hans Verkuilbd985162005-11-13 16:07:56 -080043static char *firmware = FWFILE;
44
Hans Verkuilbd985162005-11-13 16:07:56 -080045module_param(firmware, charp, 0444);
46
Hans Verkuilbd985162005-11-13 16:07:56 -080047MODULE_PARM_DESC(firmware, "Firmware image [default: " FWFILE "]");
48
Hans Verkuild92c20e2006-01-09 15:32:41 -020049static void start_fw_load(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -080050{
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);
Hans Verkuilbd985162005-11-13 16:07:56 -080058}
59
Hans Verkuild92c20e2006-01-09 15:32:41 -020060static void end_fw_load(struct i2c_client *client)
Hans Verkuilbd985162005-11-13 16:07:56 -080061{
Hans Verkuilbd985162005-11-13 16:07:56 -080062 /* AUTO_INC_DIS=0 */
63 cx25840_write(client, 0x000, 0x00);
64 /* DL_ENABLE=0 */
65 cx25840_write(client, 0x803, 0x03);
66}
67
Hans Verkuild92c20e2006-01-09 15:32:41 -020068static int check_fw_load(struct i2c_client *client, int size)
Hans Verkuilbd985162005-11-13 16:07:56 -080069{
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) {
Hans Verkuilfac9e892006-01-09 15:32:40 -020075 v4l_err(client, "firmware %s load failed\n", firmware);
Hans Verkuilbd985162005-11-13 16:07:56 -080076 return -EINVAL;
77 }
78
Hans Verkuilfac9e892006-01-09 15:32:40 -020079 v4l_info(client, "loaded %s firmware (%d bytes)\n", firmware, size);
Hans Verkuilbd985162005-11-13 16:07:56 -080080 return 0;
81}
82
David Woodhouse9ad46a62008-05-23 23:58:24 +010083static int fw_write(struct i2c_client *client, const u8 *data, int size)
Hans Verkuilbd985162005-11-13 16:07:56 -080084{
Tyler Trafford520ebe52008-04-22 14:42:14 -030085 if (i2c_master_send(client, data, size) < size) {
Hans Verkuil7d16eaa2006-04-23 05:54:56 -030086 v4l_err(client, "firmware load i2c failure\n");
87 return -ENOSYS;
Hans Verkuilbd985162005-11-13 16:07:56 -080088 }
89
90 return 0;
91}
92
93int cx25840_loadfw(struct i2c_client *client)
94{
Hans Verkuil9357b312008-11-29 12:50:06 -030095 struct cx25840_state *state = to_state(i2c_get_clientdata(client));
Hans Verkuilbd985162005-11-13 16:07:56 -080096 const struct firmware *fw = NULL;
David Woodhouse9ad46a62008-05-23 23:58:24 +010097 u8 buffer[FWSEND];
98 const u8 *ptr;
Tyler Trafford520ebe52008-04-22 14:42:14 -030099 int size, retval;
Mauro Carvalho Chehab95b14fb2009-03-03 14:36:55 -0300100 int MAX_BUF_SIZE = FWSEND;
Steven Tothf3d6f632009-07-23 12:18:54 -0300101 u32 gpio_oe = 0, gpio_da = 0;
Hans Verkuilbd985162005-11-13 16:07:56 -0800102
Steven Tothf3d6f632009-07-23 12:18:54 -0300103 if (state->is_cx23885) {
Steven Tothf2340812008-01-10 01:22:39 -0300104 firmware = FWFILE_CX23885;
Steven Tothf3d6f632009-07-23 12:18:54 -0300105 /* Preserve the GPIO OE and output bits */
106 gpio_oe = cx25840_read(client, 0x160);
107 gpio_da = cx25840_read(client, 0x164);
108 }
Mauro Carvalho Chehab95b14fb2009-03-03 14:36:55 -0300109 else if (state->is_cx231xx)
110 firmware = FWFILE_CX231XX;
Sri Deevi149783b2009-03-03 06:07:42 -0300111
Mauro Carvalho Chehab95b14fb2009-03-03 14:36:55 -0300112 if ((state->is_cx231xx) && MAX_BUF_SIZE > 16) {
113 v4l_err(client, " Firmware download size changed to 16 bytes max length\n");
114 MAX_BUF_SIZE = 16; /* cx231xx cannot accept more than 16 bytes at a time */
115 }
Steven Tothf2340812008-01-10 01:22:39 -0300116
Hans Verkuilbd985162005-11-13 16:07:56 -0800117 if (request_firmware(&fw, firmware, FWDEV(client)) != 0) {
Hans Verkuilfac9e892006-01-09 15:32:40 -0200118 v4l_err(client, "unable to open firmware %s\n", firmware);
Hans Verkuilbd985162005-11-13 16:07:56 -0800119 return -EINVAL;
120 }
121
122 start_fw_load(client);
123
124 buffer[0] = 0x08;
125 buffer[1] = 0x02;
Hans Verkuilbd985162005-11-13 16:07:56 -0800126
David Woodhouse9ad46a62008-05-23 23:58:24 +0100127 size = fw->size;
Hans Verkuilbd985162005-11-13 16:07:56 -0800128 ptr = fw->data;
129 while (size > 0) {
Sri Deevi149783b2009-03-03 06:07:42 -0300130 int len = min(MAX_BUF_SIZE - 2, size);
David Woodhouse9ad46a62008-05-23 23:58:24 +0100131
132 memcpy(buffer + 2, ptr, len);
133
134 retval = fw_write(client, buffer, len + 2);
Hans Verkuilbd985162005-11-13 16:07:56 -0800135
136 if (retval < 0) {
137 release_firmware(fw);
138 return retval;
139 }
140
David Woodhouse9ad46a62008-05-23 23:58:24 +0100141 size -= len;
142 ptr += len;
Hans Verkuilbd985162005-11-13 16:07:56 -0800143 }
144
145 end_fw_load(client);
146
147 size = fw->size;
148 release_firmware(fw);
149
Steven Tothf3d6f632009-07-23 12:18:54 -0300150 if (state->is_cx23885) {
151 /* Restore GPIO configuration after f/w load */
152 cx25840_write(client, 0x160, gpio_oe);
153 cx25840_write(client, 0x164, gpio_da);
154 }
155
Hans Verkuilbd985162005-11-13 16:07:56 -0800156 return check_fw_load(client, size);
157}