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 | |
Mike Isely | 4263fa8 | 2006-03-25 20:43:14 -0300 | [diff] [blame] | 26 | /* |
| 27 | * Mike Isely <isely@pobox.com> - The FWSEND parameter controls the |
| 28 | * size of the firmware chunks sent down the I2C bus to the chip. |
| 29 | * Previously this had been set to 1024 but unfortunately some I2C |
| 30 | * implementations can't transfer data in such big gulps. |
| 31 | * Specifically, the pvrusb2 driver has a hard limit of around 60 |
| 32 | * bytes, due to the encapsulation there of I2C traffic into USB |
| 33 | * messages. So we have to significantly reduce this parameter. |
| 34 | */ |
| 35 | #define FWSEND 48 |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 36 | |
Hans Verkuil | d55c7ae | 2007-02-15 03:40:34 -0300 | [diff] [blame] | 37 | #define FWDEV(x) &((x)->dev) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 38 | |
Hans Verkuil | 8d81ae2 | 2009-08-31 17:57:52 -0300 | [diff] [blame] | 39 | static char *firmware = ""; |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 40 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 41 | module_param(firmware, charp, 0444); |
| 42 | |
Hans Verkuil | 8d81ae2 | 2009-08-31 17:57:52 -0300 | [diff] [blame] | 43 | MODULE_PARM_DESC(firmware, "Firmware image to load"); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 44 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 45 | static void start_fw_load(struct i2c_client *client) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 46 | { |
| 47 | /* DL_ADDR_LB=0 DL_ADDR_HB=0 */ |
| 48 | cx25840_write(client, 0x800, 0x00); |
| 49 | cx25840_write(client, 0x801, 0x00); |
| 50 | // DL_MAP=3 DL_AUTO_INC=0 DL_ENABLE=1 |
| 51 | cx25840_write(client, 0x803, 0x0b); |
| 52 | /* AUTO_INC_DIS=1 */ |
| 53 | cx25840_write(client, 0x000, 0x20); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 56 | static void end_fw_load(struct i2c_client *client) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 57 | { |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 58 | /* AUTO_INC_DIS=0 */ |
| 59 | cx25840_write(client, 0x000, 0x00); |
| 60 | /* DL_ENABLE=0 */ |
| 61 | cx25840_write(client, 0x803, 0x03); |
| 62 | } |
| 63 | |
Hans Verkuil | 8d81ae2 | 2009-08-31 17:57:52 -0300 | [diff] [blame] | 64 | static const char *get_fw_name(struct i2c_client *client) |
| 65 | { |
| 66 | struct cx25840_state *state = to_state(i2c_get_clientdata(client)); |
| 67 | |
| 68 | if (firmware[0]) |
| 69 | return firmware; |
Andy Walls | 2a03f03 | 2009-09-26 23:47:21 -0300 | [diff] [blame] | 70 | if (is_cx2388x(state)) |
Hans Verkuil | 8d81ae2 | 2009-08-31 17:57:52 -0300 | [diff] [blame] | 71 | return "v4l-cx23885-avcore-01.fw"; |
Andy Walls | 2a03f03 | 2009-09-26 23:47:21 -0300 | [diff] [blame] | 72 | if (is_cx231xx(state)) |
Hans Verkuil | 8d81ae2 | 2009-08-31 17:57:52 -0300 | [diff] [blame] | 73 | return "v4l-cx231xx-avcore-01.fw"; |
| 74 | return "v4l-cx25840.fw"; |
| 75 | } |
| 76 | |
Hans Verkuil | d92c20e | 2006-01-09 15:32:41 -0200 | [diff] [blame] | 77 | static int check_fw_load(struct i2c_client *client, int size) |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 78 | { |
| 79 | /* DL_ADDR_HB DL_ADDR_LB */ |
| 80 | int s = cx25840_read(client, 0x801) << 8; |
| 81 | s |= cx25840_read(client, 0x800); |
| 82 | |
| 83 | if (size != s) { |
Hans Verkuil | 8d81ae2 | 2009-08-31 17:57:52 -0300 | [diff] [blame] | 84 | v4l_err(client, "firmware %s load failed\n", |
| 85 | get_fw_name(client)); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 86 | return -EINVAL; |
| 87 | } |
| 88 | |
Hans Verkuil | 8d81ae2 | 2009-08-31 17:57:52 -0300 | [diff] [blame] | 89 | v4l_info(client, "loaded %s firmware (%d bytes)\n", |
| 90 | get_fw_name(client), size); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | |
David Woodhouse | 9ad46a6 | 2008-05-23 23:58:24 +0100 | [diff] [blame] | 94 | 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] | 95 | { |
Tyler Trafford | 520ebe5 | 2008-04-22 14:42:14 -0300 | [diff] [blame] | 96 | if (i2c_master_send(client, data, size) < size) { |
Hans Verkuil | 7d16eaa | 2006-04-23 05:54:56 -0300 | [diff] [blame] | 97 | v4l_err(client, "firmware load i2c failure\n"); |
| 98 | return -ENOSYS; |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | int cx25840_loadfw(struct i2c_client *client) |
| 105 | { |
Hans Verkuil | 9357b31 | 2008-11-29 12:50:06 -0300 | [diff] [blame] | 106 | struct cx25840_state *state = to_state(i2c_get_clientdata(client)); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 107 | const struct firmware *fw = NULL; |
David Woodhouse | 9ad46a6 | 2008-05-23 23:58:24 +0100 | [diff] [blame] | 108 | u8 buffer[FWSEND]; |
| 109 | const u8 *ptr; |
Hans Verkuil | 8d81ae2 | 2009-08-31 17:57:52 -0300 | [diff] [blame] | 110 | const char *fwname = get_fw_name(client); |
Tyler Trafford | 520ebe5 | 2008-04-22 14:42:14 -0300 | [diff] [blame] | 111 | int size, retval; |
Mauro Carvalho Chehab | 95b14fb | 2009-03-03 14:36:55 -0300 | [diff] [blame] | 112 | int MAX_BUF_SIZE = FWSEND; |
Steven Toth | f3d6f63 | 2009-07-23 12:18:54 -0300 | [diff] [blame] | 113 | u32 gpio_oe = 0, gpio_da = 0; |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 114 | |
Andy Walls | 2a03f03 | 2009-09-26 23:47:21 -0300 | [diff] [blame] | 115 | if (is_cx2388x(state)) { |
Steven Toth | f3d6f63 | 2009-07-23 12:18:54 -0300 | [diff] [blame] | 116 | /* Preserve the GPIO OE and output bits */ |
| 117 | gpio_oe = cx25840_read(client, 0x160); |
| 118 | gpio_da = cx25840_read(client, 0x164); |
| 119 | } |
Sri Deevi | 149783b | 2009-03-03 06:07:42 -0300 | [diff] [blame] | 120 | |
Andy Walls | 2a03f03 | 2009-09-26 23:47:21 -0300 | [diff] [blame] | 121 | if (is_cx231xx(state) && MAX_BUF_SIZE > 16) { |
Mauro Carvalho Chehab | 95b14fb | 2009-03-03 14:36:55 -0300 | [diff] [blame] | 122 | v4l_err(client, " Firmware download size changed to 16 bytes max length\n"); |
| 123 | MAX_BUF_SIZE = 16; /* cx231xx cannot accept more than 16 bytes at a time */ |
| 124 | } |
Steven Toth | f234081 | 2008-01-10 01:22:39 -0300 | [diff] [blame] | 125 | |
Hans Verkuil | 8d81ae2 | 2009-08-31 17:57:52 -0300 | [diff] [blame] | 126 | if (request_firmware(&fw, fwname, FWDEV(client)) != 0) { |
| 127 | v4l_err(client, "unable to open firmware %s\n", fwname); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 128 | return -EINVAL; |
| 129 | } |
| 130 | |
| 131 | start_fw_load(client); |
| 132 | |
| 133 | buffer[0] = 0x08; |
| 134 | buffer[1] = 0x02; |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 135 | |
David Woodhouse | 9ad46a6 | 2008-05-23 23:58:24 +0100 | [diff] [blame] | 136 | size = fw->size; |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 137 | ptr = fw->data; |
| 138 | while (size > 0) { |
Sri Deevi | 149783b | 2009-03-03 06:07:42 -0300 | [diff] [blame] | 139 | int len = min(MAX_BUF_SIZE - 2, size); |
David Woodhouse | 9ad46a6 | 2008-05-23 23:58:24 +0100 | [diff] [blame] | 140 | |
| 141 | memcpy(buffer + 2, ptr, len); |
| 142 | |
| 143 | retval = fw_write(client, buffer, len + 2); |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 144 | |
| 145 | if (retval < 0) { |
| 146 | release_firmware(fw); |
| 147 | return retval; |
| 148 | } |
| 149 | |
David Woodhouse | 9ad46a6 | 2008-05-23 23:58:24 +0100 | [diff] [blame] | 150 | size -= len; |
| 151 | ptr += len; |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | end_fw_load(client); |
| 155 | |
| 156 | size = fw->size; |
| 157 | release_firmware(fw); |
| 158 | |
Andy Walls | 2a03f03 | 2009-09-26 23:47:21 -0300 | [diff] [blame] | 159 | if (is_cx2388x(state)) { |
Steven Toth | f3d6f63 | 2009-07-23 12:18:54 -0300 | [diff] [blame] | 160 | /* Restore GPIO configuration after f/w load */ |
| 161 | cx25840_write(client, 0x160, gpio_oe); |
| 162 | cx25840_write(client, 0x164, gpio_da); |
| 163 | } |
| 164 | |
Hans Verkuil | bd98516 | 2005-11-13 16:07:56 -0800 | [diff] [blame] | 165 | return check_fw_load(client, size); |
| 166 | } |