Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1 | /* |
| 2 | cx231xx-i2c.c - driver for Conexant Cx23100/101/102 USB video capture devices |
| 3 | |
| 4 | Copyright (C) 2008 <srinivasa.deevi at conexant dot com> |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 5 | Based on em28xx driver |
| 6 | Based on Cx23885 driver |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 7 | |
| 8 | This program is free software; you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation; either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/usb.h> |
| 26 | #include <linux/i2c.h> |
| 27 | #include <media/v4l2-common.h> |
| 28 | #include <media/tuner.h> |
| 29 | |
| 30 | #include "cx231xx.h" |
| 31 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 32 | /* ----------------------------------------------------------- */ |
| 33 | |
| 34 | static unsigned int i2c_scan; |
| 35 | module_param(i2c_scan, int, 0444); |
| 36 | MODULE_PARM_DESC(i2c_scan, "scan i2c bus at insmod time"); |
| 37 | |
| 38 | static unsigned int i2c_debug; |
| 39 | module_param(i2c_debug, int, 0644); |
| 40 | MODULE_PARM_DESC(i2c_debug, "enable debug messages [i2c]"); |
| 41 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 42 | #define dprintk1(lvl, fmt, args...) \ |
| 43 | do { \ |
| 44 | if (i2c_debug >= lvl) { \ |
Sri Deevi | 6e4f574 | 2009-03-10 21:16:26 -0300 | [diff] [blame] | 45 | printk(fmt, ##args); \ |
| 46 | } \ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 47 | } while (0) |
| 48 | |
| 49 | #define dprintk2(lvl, fmt, args...) \ |
| 50 | do { \ |
| 51 | if (i2c_debug >= lvl) { \ |
| 52 | printk(KERN_DEBUG "%s at %s: " fmt, \ |
| 53 | dev->name, __func__ , ##args); \ |
| 54 | } \ |
| 55 | } while (0) |
| 56 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 57 | /* |
| 58 | * cx231xx_i2c_send_bytes() |
| 59 | */ |
| 60 | int cx231xx_i2c_send_bytes(struct i2c_adapter *i2c_adap, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 61 | const struct i2c_msg *msg) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 62 | { |
| 63 | struct cx231xx_i2c *bus = i2c_adap->algo_data; |
| 64 | struct cx231xx *dev = bus->dev; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 65 | struct cx231xx_i2c_xfer_data req_data; |
| 66 | int status = 0; |
| 67 | u16 size = 0; |
| 68 | u8 loop = 0; |
| 69 | u8 saddr_len = 1; |
| 70 | u8 *buf_ptr = NULL; |
| 71 | u16 saddr = 0; |
| 72 | u8 need_gpio = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 73 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 74 | if ((bus->nr == 1) && (msg->addr == 0x61) |
| 75 | && (dev->tuner_type == TUNER_XC5000)) { |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 76 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 77 | size = msg->len; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 78 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 79 | if (size == 2) { /* register write sub addr */ |
Sri Deevi | 6e4f574 | 2009-03-10 21:16:26 -0300 | [diff] [blame] | 80 | /* Just writing sub address will cause problem |
| 81 | * to XC5000. So ignore the request */ |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 82 | return 0; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 83 | } else if (size == 4) { /* register write with sub addr */ |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 84 | if (msg->len >= 2) |
| 85 | saddr = msg->buf[0] << 8 | msg->buf[1]; |
| 86 | else if (msg->len == 1) |
| 87 | saddr = msg->buf[0]; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 88 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 89 | switch (saddr) { |
| 90 | case 0x0000: /* start tuner calibration mode */ |
| 91 | need_gpio = 1; |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 92 | /* FW Loading is done */ |
| 93 | dev->xc_fw_load_done = 1; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 94 | break; |
| 95 | case 0x000D: /* Set signal source */ |
| 96 | case 0x0001: /* Set TV standard - Video */ |
| 97 | case 0x0002: /* Set TV standard - Audio */ |
| 98 | case 0x0003: /* Set RF Frequency */ |
| 99 | need_gpio = 1; |
| 100 | break; |
| 101 | default: |
| 102 | if (dev->xc_fw_load_done) |
| 103 | need_gpio = 1; |
| 104 | break; |
| 105 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 106 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 107 | if (need_gpio) { |
| 108 | dprintk1(1, |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 109 | "GPIO WRITE: addr 0x%x, len %d, saddr 0x%x\n", |
| 110 | msg->addr, msg->len, saddr); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 111 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 112 | return dev->cx231xx_gpio_i2c_write(dev, |
| 113 | msg->addr, |
| 114 | msg->buf, |
| 115 | msg->len); |
| 116 | } |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 117 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 118 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 119 | /* special case for Xc5000 tuner case */ |
| 120 | saddr_len = 1; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 121 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 122 | /* adjust the length to correct length */ |
| 123 | size -= saddr_len; |
| 124 | buf_ptr = (u8 *) (msg->buf + 1); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 125 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 126 | do { |
| 127 | /* prepare xfer_data struct */ |
| 128 | req_data.dev_addr = msg->addr; |
| 129 | req_data.direction = msg->flags; |
| 130 | req_data.saddr_len = saddr_len; |
| 131 | req_data.saddr_dat = msg->buf[0]; |
| 132 | req_data.buf_size = size > 16 ? 16 : size; |
| 133 | req_data.p_buffer = (u8 *) (buf_ptr + loop * 16); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 134 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 135 | bus->i2c_nostop = (size > 16) ? 1 : 0; |
| 136 | bus->i2c_reserve = (loop == 0) ? 0 : 1; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 137 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 138 | /* usb send command */ |
| 139 | status = dev->cx231xx_send_usb_command(bus, &req_data); |
| 140 | loop++; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 141 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 142 | if (size >= 16) |
| 143 | size -= 16; |
| 144 | else |
| 145 | size = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 146 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 147 | } while (size > 0); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 148 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 149 | bus->i2c_nostop = 0; |
| 150 | bus->i2c_reserve = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 151 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 152 | } else { /* regular case */ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 153 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 154 | /* prepare xfer_data struct */ |
| 155 | req_data.dev_addr = msg->addr; |
| 156 | req_data.direction = msg->flags; |
| 157 | req_data.saddr_len = 0; |
| 158 | req_data.saddr_dat = 0; |
| 159 | req_data.buf_size = msg->len; |
| 160 | req_data.p_buffer = msg->buf; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 161 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 162 | /* usb send command */ |
| 163 | status = dev->cx231xx_send_usb_command(bus, &req_data); |
| 164 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 165 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 166 | return status < 0 ? status : 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* |
| 170 | * cx231xx_i2c_recv_bytes() |
| 171 | * read a byte from the i2c device |
| 172 | */ |
| 173 | static int cx231xx_i2c_recv_bytes(struct i2c_adapter *i2c_adap, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 174 | const struct i2c_msg *msg) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 175 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 176 | struct cx231xx_i2c *bus = i2c_adap->algo_data; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 177 | struct cx231xx *dev = bus->dev; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 178 | struct cx231xx_i2c_xfer_data req_data; |
| 179 | int status = 0; |
| 180 | u16 saddr = 0; |
| 181 | u8 need_gpio = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 182 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 183 | if ((bus->nr == 1) && (msg->addr == 0x61) |
| 184 | && dev->tuner_type == TUNER_XC5000) { |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 185 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 186 | if (msg->len == 2) |
| 187 | saddr = msg->buf[0] << 8 | msg->buf[1]; |
| 188 | else if (msg->len == 1) |
| 189 | saddr = msg->buf[0]; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 190 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 191 | if (dev->xc_fw_load_done) { |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 192 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 193 | switch (saddr) { |
| 194 | case 0x0009: /* BUSY check */ |
| 195 | dprintk1(1, |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 196 | "GPIO R E A D: Special case BUSY check \n"); |
| 197 | /*Try read BUSY register, just set it to zero*/ |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 198 | msg->buf[0] = 0; |
| 199 | if (msg->len == 2) |
| 200 | msg->buf[1] = 0; |
| 201 | return 0; |
| 202 | case 0x0004: /* read Lock status */ |
| 203 | need_gpio = 1; |
| 204 | break; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 205 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 206 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 207 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 208 | if (need_gpio) { |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 209 | /* this is a special case to handle Xceive tuner |
| 210 | clock stretch issue with gpio based I2C */ |
| 211 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 212 | dprintk1(1, |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 213 | "GPIO R E A D: addr 0x%x, len %d, saddr 0x%x\n", |
| 214 | msg->addr, msg->len, |
| 215 | msg->buf[0] << 8 | msg->buf[1]); |
| 216 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 217 | status = |
| 218 | dev->cx231xx_gpio_i2c_write(dev, msg->addr, |
| 219 | msg->buf, |
| 220 | msg->len); |
| 221 | status = |
| 222 | dev->cx231xx_gpio_i2c_read(dev, msg->addr, |
| 223 | msg->buf, |
| 224 | msg->len); |
| 225 | return status; |
| 226 | } |
| 227 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 228 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 229 | /* prepare xfer_data struct */ |
| 230 | req_data.dev_addr = msg->addr; |
| 231 | req_data.direction = msg->flags; |
| 232 | req_data.saddr_len = msg->len; |
| 233 | req_data.saddr_dat = msg->buf[0] << 8 | msg->buf[1]; |
| 234 | req_data.buf_size = msg->len; |
| 235 | req_data.p_buffer = msg->buf; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 236 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 237 | /* usb send command */ |
| 238 | status = dev->cx231xx_send_usb_command(bus, &req_data); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 239 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 240 | } else { |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 241 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 242 | /* prepare xfer_data struct */ |
| 243 | req_data.dev_addr = msg->addr; |
| 244 | req_data.direction = msg->flags; |
| 245 | req_data.saddr_len = 0; |
| 246 | req_data.saddr_dat = 0; |
| 247 | req_data.buf_size = msg->len; |
| 248 | req_data.p_buffer = msg->buf; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 249 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 250 | /* usb send command */ |
| 251 | status = dev->cx231xx_send_usb_command(bus, &req_data); |
| 252 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 253 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 254 | return status < 0 ? status : 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | /* |
| 258 | * cx231xx_i2c_recv_bytes_with_saddr() |
| 259 | * read a byte from the i2c device |
| 260 | */ |
| 261 | static int cx231xx_i2c_recv_bytes_with_saddr(struct i2c_adapter *i2c_adap, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 262 | const struct i2c_msg *msg1, |
| 263 | const struct i2c_msg *msg2) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 264 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 265 | struct cx231xx_i2c *bus = i2c_adap->algo_data; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 266 | struct cx231xx *dev = bus->dev; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 267 | struct cx231xx_i2c_xfer_data req_data; |
| 268 | int status = 0; |
| 269 | u16 saddr = 0; |
| 270 | u8 need_gpio = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 271 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 272 | if (msg1->len == 2) |
| 273 | saddr = msg1->buf[0] << 8 | msg1->buf[1]; |
| 274 | else if (msg1->len == 1) |
| 275 | saddr = msg1->buf[0]; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 276 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 277 | if ((bus->nr == 1) && (msg2->addr == 0x61) |
| 278 | && dev->tuner_type == TUNER_XC5000) { |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 279 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 280 | if ((msg2->len < 16)) { |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 281 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 282 | dprintk1(1, |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 283 | "i2c_read: addr 0x%x, len %d, saddr 0x%x, len %d\n", |
| 284 | msg2->addr, msg2->len, saddr, msg1->len); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 285 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 286 | switch (saddr) { |
| 287 | case 0x0008: /* read FW load status */ |
| 288 | need_gpio = 1; |
| 289 | break; |
| 290 | case 0x0004: /* read Lock status */ |
| 291 | need_gpio = 1; |
| 292 | break; |
| 293 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 294 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 295 | if (need_gpio) { |
| 296 | status = |
| 297 | dev->cx231xx_gpio_i2c_write(dev, msg1->addr, |
| 298 | msg1->buf, |
| 299 | msg1->len); |
| 300 | status = |
| 301 | dev->cx231xx_gpio_i2c_read(dev, msg2->addr, |
| 302 | msg2->buf, |
| 303 | msg2->len); |
| 304 | return status; |
| 305 | } |
| 306 | } |
| 307 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 308 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 309 | /* prepare xfer_data struct */ |
| 310 | req_data.dev_addr = msg2->addr; |
| 311 | req_data.direction = msg2->flags; |
| 312 | req_data.saddr_len = msg1->len; |
| 313 | req_data.saddr_dat = saddr; |
| 314 | req_data.buf_size = msg2->len; |
| 315 | req_data.p_buffer = msg2->buf; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 316 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 317 | /* usb send command */ |
| 318 | status = dev->cx231xx_send_usb_command(bus, &req_data); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 319 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 320 | return status < 0 ? status : 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | /* |
| 324 | * cx231xx_i2c_check_for_device() |
| 325 | * check if there is a i2c_device at the supplied address |
| 326 | */ |
| 327 | static int cx231xx_i2c_check_for_device(struct i2c_adapter *i2c_adap, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 328 | const struct i2c_msg *msg) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 329 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 330 | struct cx231xx_i2c *bus = i2c_adap->algo_data; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 331 | struct cx231xx *dev = bus->dev; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 332 | struct cx231xx_i2c_xfer_data req_data; |
| 333 | int status = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 334 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 335 | /* prepare xfer_data struct */ |
| 336 | req_data.dev_addr = msg->addr; |
| 337 | req_data.direction = msg->flags; |
| 338 | req_data.saddr_len = 0; |
| 339 | req_data.saddr_dat = 0; |
| 340 | req_data.buf_size = 0; |
| 341 | req_data.p_buffer = NULL; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 342 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 343 | /* usb send command */ |
| 344 | status = dev->cx231xx_send_usb_command(bus, &req_data); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 345 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 346 | return status < 0 ? status : 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* |
| 350 | * cx231xx_i2c_xfer() |
| 351 | * the main i2c transfer function |
| 352 | */ |
| 353 | static int cx231xx_i2c_xfer(struct i2c_adapter *i2c_adap, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 354 | struct i2c_msg msgs[], int num) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 355 | { |
| 356 | struct cx231xx_i2c *bus = i2c_adap->algo_data; |
| 357 | struct cx231xx *dev = bus->dev; |
| 358 | int addr, rc, i, byte; |
| 359 | |
| 360 | if (num <= 0) |
| 361 | return 0; |
| 362 | |
| 363 | for (i = 0; i < num; i++) { |
| 364 | |
| 365 | addr = msgs[i].addr >> 1; |
| 366 | |
| 367 | dprintk2(2, "%s %s addr=%x len=%d:", |
| 368 | (msgs[i].flags & I2C_M_RD) ? "read" : "write", |
| 369 | i == num - 1 ? "stop" : "nonstop", addr, msgs[i].len); |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 370 | if (!msgs[i].len) { |
| 371 | /* no len: check only for device presence */ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 372 | rc = cx231xx_i2c_check_for_device(i2c_adap, &msgs[i]); |
| 373 | if (rc < 0) { |
| 374 | dprintk2(2, " no device\n"); |
| 375 | return rc; |
| 376 | } |
| 377 | |
| 378 | } else if (msgs[i].flags & I2C_M_RD) { |
| 379 | /* read bytes */ |
| 380 | rc = cx231xx_i2c_recv_bytes(i2c_adap, &msgs[i]); |
| 381 | if (i2c_debug >= 2) { |
| 382 | for (byte = 0; byte < msgs[i].len; byte++) |
| 383 | printk(" %02x", msgs[i].buf[byte]); |
| 384 | } |
| 385 | } else if (i + 1 < num && (msgs[i + 1].flags & I2C_M_RD) && |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 386 | msgs[i].addr == msgs[i + 1].addr |
| 387 | && (msgs[i].len <= 2) && (bus->nr < 2)) { |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 388 | /* read bytes */ |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 389 | rc = cx231xx_i2c_recv_bytes_with_saddr(i2c_adap, |
| 390 | &msgs[i], |
| 391 | &msgs[i + 1]); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 392 | if (i2c_debug >= 2) { |
| 393 | for (byte = 0; byte < msgs[i].len; byte++) |
| 394 | printk(" %02x", msgs[i].buf[byte]); |
| 395 | } |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 396 | i++; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 397 | } else { |
| 398 | /* write bytes */ |
| 399 | if (i2c_debug >= 2) { |
| 400 | for (byte = 0; byte < msgs[i].len; byte++) |
| 401 | printk(" %02x", msgs[i].buf[byte]); |
| 402 | } |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 403 | rc = cx231xx_i2c_send_bytes(i2c_adap, &msgs[i]); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 404 | } |
| 405 | if (rc < 0) |
| 406 | goto err; |
| 407 | if (i2c_debug >= 2) |
| 408 | printk("\n"); |
| 409 | } |
| 410 | |
| 411 | return num; |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 412 | err: |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 413 | dprintk2(2, " ERROR: %i\n", rc); |
| 414 | return rc; |
| 415 | } |
| 416 | |
| 417 | /* ----------------------------------------------------------- */ |
| 418 | |
| 419 | /* |
| 420 | * functionality() |
| 421 | */ |
| 422 | static u32 functionality(struct i2c_adapter *adap) |
| 423 | { |
| 424 | return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_I2C; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * attach_inform() |
| 429 | * gets called when a device attaches to the i2c bus |
| 430 | * does some basic configuration |
| 431 | */ |
| 432 | static int attach_inform(struct i2c_client *client) |
| 433 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 434 | struct cx231xx_i2c *bus = i2c_get_adapdata(client->adapter); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 435 | struct cx231xx *dev = bus->dev; |
| 436 | |
| 437 | switch (client->addr << 1) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 438 | case 0x8e: |
| 439 | { |
| 440 | struct IR_i2c *ir = i2c_get_clientdata(client); |
| 441 | dprintk1(1, "attach_inform: IR detected (%s).\n", |
| 442 | ir->phys); |
| 443 | cx231xx_set_ir(dev, ir); |
| 444 | break; |
| 445 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 446 | break; |
| 447 | |
| 448 | default: |
Sri Deevi | b119612 | 2009-03-20 23:33:48 -0300 | [diff] [blame^] | 449 | break; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 455 | static struct i2c_algorithm cx231xx_algo = { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 456 | .master_xfer = cx231xx_i2c_xfer, |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 457 | .functionality = functionality, |
| 458 | }; |
| 459 | |
| 460 | static struct i2c_adapter cx231xx_adap_template = { |
| 461 | .owner = THIS_MODULE, |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 462 | .name = "cx231xx", |
| 463 | .id = I2C_HW_B_CX231XX, |
| 464 | .algo = &cx231xx_algo, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 465 | .client_register = attach_inform, |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 466 | }; |
| 467 | |
| 468 | static struct i2c_client cx231xx_client_template = { |
| 469 | .name = "cx231xx internal", |
| 470 | }; |
| 471 | |
| 472 | /* ----------------------------------------------------------- */ |
| 473 | |
| 474 | /* |
| 475 | * i2c_devs |
| 476 | * incomplete list of known devices |
| 477 | */ |
| 478 | static char *i2c_devs[128] = { |
| 479 | [0x60 >> 1] = "colibri", |
| 480 | [0x88 >> 1] = "hammerhead", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 481 | [0x8e >> 1] = "CIR", |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 482 | [0x32 >> 1] = "GeminiIII", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 483 | [0x02 >> 1] = "Aquarius", |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 484 | [0xa0 >> 1] = "eeprom", |
| 485 | [0xc0 >> 1] = "tuner/XC3028", |
| 486 | [0xc2 >> 1] = "tuner/XC5000", |
| 487 | }; |
| 488 | |
| 489 | /* |
| 490 | * cx231xx_do_i2c_scan() |
| 491 | * check i2c address range for devices |
| 492 | */ |
| 493 | void cx231xx_do_i2c_scan(struct cx231xx *dev, struct i2c_client *c) |
| 494 | { |
| 495 | unsigned char buf; |
| 496 | int i, rc; |
| 497 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 498 | cx231xx_info(": Checking for I2C devices ..\n"); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 499 | for (i = 0; i < 128; i++) { |
| 500 | c->addr = i; |
| 501 | rc = i2c_master_recv(c, &buf, 0); |
| 502 | if (rc < 0) |
| 503 | continue; |
| 504 | cx231xx_info("%s: i2c scan: found device @ 0x%x [%s]\n", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 505 | dev->name, i << 1, |
| 506 | i2c_devs[i] ? i2c_devs[i] : "???"); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 507 | } |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 508 | cx231xx_info(": Completed Checking for I2C devices.\n"); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | /* |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 512 | * cx231xx_i2c_register() |
| 513 | * register i2c bus |
| 514 | */ |
| 515 | int cx231xx_i2c_register(struct cx231xx_i2c *bus) |
| 516 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 517 | struct cx231xx *dev = bus->dev; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 518 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 519 | BUG_ON(!dev->cx231xx_send_usb_command); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 520 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 521 | cx231xx_info("%s(bus = %d)\n", __func__, bus->nr); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 522 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 523 | memcpy(&bus->i2c_adap, &cx231xx_adap_template, sizeof(bus->i2c_adap)); |
| 524 | memcpy(&bus->i2c_algo, &cx231xx_algo, sizeof(bus->i2c_algo)); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 525 | memcpy(&bus->i2c_client, &cx231xx_client_template, |
| 526 | sizeof(bus->i2c_client)); |
| 527 | |
| 528 | bus->i2c_adap.dev.parent = &dev->udev->dev; |
| 529 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 530 | strlcpy(bus->i2c_adap.name, bus->dev->name, sizeof(bus->i2c_adap.name)); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 531 | |
| 532 | bus->i2c_algo.data = bus; |
| 533 | bus->i2c_adap.algo_data = bus; |
Sri Deevi | b119612 | 2009-03-20 23:33:48 -0300 | [diff] [blame^] | 534 | i2c_set_adapdata(&bus->i2c_adap, &dev->v4l2_dev); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 535 | i2c_add_adapter(&bus->i2c_adap); |
| 536 | |
| 537 | bus->i2c_client.adapter = &bus->i2c_adap; |
| 538 | |
| 539 | if (0 == bus->i2c_rc) { |
| 540 | cx231xx_info("%s: i2c bus %d registered\n", dev->name, bus->nr); |
| 541 | if (i2c_scan) |
| 542 | cx231xx_do_i2c_scan(dev, &bus->i2c_client); |
| 543 | } else |
| 544 | cx231xx_warn("%s: i2c bus %d register FAILED\n", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 545 | dev->name, bus->nr); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 546 | |
| 547 | return bus->i2c_rc; |
| 548 | } |
| 549 | |
| 550 | /* |
| 551 | * cx231xx_i2c_unregister() |
| 552 | * unregister i2c_bus |
| 553 | */ |
| 554 | int cx231xx_i2c_unregister(struct cx231xx_i2c *bus) |
| 555 | { |
| 556 | i2c_del_adapter(&bus->i2c_adap); |
| 557 | return 0; |
| 558 | } |