Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 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 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 18 | #include <linux/module.h> |
| 19 | #include <linux/i2c.h> |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 20 | #include <linux/firmware.h> |
| 21 | #include <media/v4l2-common.h> |
Hans Verkuil | 31bc09b | 2006-03-25 10:26:09 -0300 | [diff] [blame] | 22 | #include <media/cx25840.h> |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 23 | |
Hans Verkuil | 31bc09b | 2006-03-25 10:26:09 -0300 | [diff] [blame] | 24 | #include "cx25840-core.h" |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 25 | |
Hans Verkuil | 60f6c46 | 2005-11-13 16:08:12 -0800 | [diff] [blame] | 26 | #define FWFILE "v4l-cx25840.fw" |
Steven Toth | f234081 | 2008-01-10 01:22:39 -0300 | [diff] [blame] | 27 | #define FWFILE_CX23885 "v4l-cx23885-avcore-01.fw" |
Mike Isely | 4263fa8 | 2006-03-25 20:43:14 -0300 | [diff] [blame] | 28 | |
| 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 Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 39 | |
Hans Verkuil | d55c7ae | 2007-02-15 03:40:34 -0300 | [diff] [blame] | 40 | #define FWDEV(x) &((x)->dev) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 41 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 42 | static char *firmware = FWFILE; |
| 43 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 44 | module_param(firmware, charp, 0444); |
| 45 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 46 | MODULE_PARM_DESC(firmware, "Firmware image [default: " FWFILE "]"); |
| 47 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 48 | static void start_fw_load(struct i2c_client *client) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 49 | { |
| 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 Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 59 | static void end_fw_load(struct i2c_client *client) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 60 | { |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 61 | /* AUTO_INC_DIS=0 */ |
| 62 | cx25840_write(client, 0x000, 0x00); |
| 63 | /* DL_ENABLE=0 */ |
| 64 | cx25840_write(client, 0x803, 0x03); |
| 65 | } |
| 66 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 67 | static int check_fw_load(struct i2c_client *client, int size) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 68 | { |
| 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 Verkuil | fac9e89 | 2006-01-09 15:32:40 -0200 | [diff] [blame] | 74 | v4l_err(client, "firmware %s load failed\n", firmware); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 75 | return -EINVAL; |
| 76 | } |
| 77 | |
Hans Verkuil | fac9e89 | 2006-01-09 15:32:40 -0200 | [diff] [blame] | 78 | v4l_info(client, "loaded %s firmware (%d bytes)\n", firmware, size); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | |
David Woodhouse | 9ad46a6 | 2008-05-23 23:58:24 +0100 | [diff] [blame^] | 82 | static int fw_write(struct i2c_client *client, const u8 *data, int size) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 83 | { |
Tyler Trafford | 520ebe5 | 2008-04-22 14:42:14 -0300 | [diff] [blame] | 84 | if (i2c_master_send(client, data, size) < size) { |
Hans Verkuil | 7d16eaa | 2006-04-23 05:54:56 -0300 | [diff] [blame] | 85 | v4l_err(client, "firmware load i2c failure\n"); |
| 86 | return -ENOSYS; |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | int cx25840_loadfw(struct i2c_client *client) |
| 93 | { |
Steven Toth | f234081 | 2008-01-10 01:22:39 -0300 | [diff] [blame] | 94 | struct cx25840_state *state = i2c_get_clientdata(client); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 95 | const struct firmware *fw = NULL; |
David Woodhouse | 9ad46a6 | 2008-05-23 23:58:24 +0100 | [diff] [blame^] | 96 | u8 buffer[FWSEND]; |
| 97 | const u8 *ptr; |
Tyler Trafford | 520ebe5 | 2008-04-22 14:42:14 -0300 | [diff] [blame] | 98 | int size, retval; |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 99 | |
Steven Toth | f234081 | 2008-01-10 01:22:39 -0300 | [diff] [blame] | 100 | if (state->is_cx23885) |
| 101 | firmware = FWFILE_CX23885; |
| 102 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 103 | if (request_firmware(&fw, firmware, FWDEV(client)) != 0) { |
Hans Verkuil | fac9e89 | 2006-01-09 15:32:40 -0200 | [diff] [blame] | 104 | v4l_err(client, "unable to open firmware %s\n", firmware); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 105 | return -EINVAL; |
| 106 | } |
| 107 | |
| 108 | start_fw_load(client); |
| 109 | |
| 110 | buffer[0] = 0x08; |
| 111 | buffer[1] = 0x02; |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 112 | |
David Woodhouse | 9ad46a6 | 2008-05-23 23:58:24 +0100 | [diff] [blame^] | 113 | size = fw->size; |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 114 | ptr = fw->data; |
| 115 | while (size > 0) { |
David Woodhouse | 9ad46a6 | 2008-05-23 23:58:24 +0100 | [diff] [blame^] | 116 | int len = min(FWSEND - 2, size); |
| 117 | |
| 118 | memcpy(buffer + 2, ptr, len); |
| 119 | |
| 120 | retval = fw_write(client, buffer, len + 2); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 121 | |
| 122 | if (retval < 0) { |
| 123 | release_firmware(fw); |
| 124 | return retval; |
| 125 | } |
| 126 | |
David Woodhouse | 9ad46a6 | 2008-05-23 23:58:24 +0100 | [diff] [blame^] | 127 | size -= len; |
| 128 | ptr += len; |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | end_fw_load(client); |
| 132 | |
| 133 | size = fw->size; |
| 134 | release_firmware(fw); |
| 135 | |
| 136 | return check_fw_load(client, size); |
| 137 | } |