Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | |
| 3 | bttv-i2c.c -- all the i2c code is here |
| 4 | |
| 5 | bttv - Bt848 frame grabber driver |
| 6 | |
| 7 | Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de) |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 8 | & Marcus Metzler (mocm@thp.uni-koeln.de) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org> |
| 10 | |
Mauro Carvalho Chehab | 141276b | 2006-09-06 19:04:28 -0300 | [diff] [blame] | 11 | (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org> |
| 12 | - Multituner support and i2c address binding |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | This program is free software; you can redistribute it and/or modify |
| 15 | it under the terms of the GNU General Public License as published by |
| 16 | the Free Software Foundation; either version 2 of the License, or |
| 17 | (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, |
| 20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | GNU General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 27 | |
| 28 | */ |
| 29 | |
| 30 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/init.h> |
| 32 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #include "bttvp.h" |
Michael Krufky | 5e453dc | 2006-01-09 15:32:31 -0200 | [diff] [blame] | 35 | #include <media/v4l2-common.h> |
Mauro Carvalho Chehab | b5b8ab8 | 2006-01-09 15:25:20 -0200 | [diff] [blame] | 36 | #include <linux/jiffies.h> |
| 37 | #include <asm/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | static int attach_inform(struct i2c_client *client); |
| 40 | |
Mauro Carvalho Chehab | a5ed425 | 2006-01-13 14:10:19 -0200 | [diff] [blame] | 41 | static int i2c_debug; |
| 42 | static int i2c_hw; |
| 43 | static int i2c_scan; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | module_param(i2c_debug, int, 0644); |
Mauro Carvalho Chehab | 141276b | 2006-09-06 19:04:28 -0300 | [diff] [blame] | 45 | MODULE_PARM_DESC(i2c_hw,"configure i2c debug level"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | module_param(i2c_hw, int, 0444); |
Mauro Carvalho Chehab | 141276b | 2006-09-06 19:04:28 -0300 | [diff] [blame] | 47 | MODULE_PARM_DESC(i2c_hw,"force use of hardware i2c support, " |
| 48 | "instead of software bitbang"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | module_param(i2c_scan, int, 0444); |
| 50 | MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time"); |
| 51 | |
Mauro Carvalho Chehab | 141276b | 2006-09-06 19:04:28 -0300 | [diff] [blame] | 52 | static unsigned int i2c_udelay = 5; |
| 53 | module_param(i2c_udelay, int, 0444); |
| 54 | MODULE_PARM_DESC(i2c_udelay,"soft i2c delay at insmod time, in usecs " |
| 55 | "(should be 5 or higher). Lower value means higher bus speed."); |
| 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | /* ----------------------------------------------------------------------- */ |
| 58 | /* I2C functions - bitbanging adapter (software i2c) */ |
| 59 | |
| 60 | static void bttv_bit_setscl(void *data, int state) |
| 61 | { |
| 62 | struct bttv *btv = (struct bttv*)data; |
| 63 | |
| 64 | if (state) |
| 65 | btv->i2c_state |= 0x02; |
| 66 | else |
| 67 | btv->i2c_state &= ~0x02; |
| 68 | btwrite(btv->i2c_state, BT848_I2C); |
| 69 | btread(BT848_I2C); |
| 70 | } |
| 71 | |
| 72 | static void bttv_bit_setsda(void *data, int state) |
| 73 | { |
| 74 | struct bttv *btv = (struct bttv*)data; |
| 75 | |
| 76 | if (state) |
| 77 | btv->i2c_state |= 0x01; |
| 78 | else |
| 79 | btv->i2c_state &= ~0x01; |
| 80 | btwrite(btv->i2c_state, BT848_I2C); |
| 81 | btread(BT848_I2C); |
| 82 | } |
| 83 | |
| 84 | static int bttv_bit_getscl(void *data) |
| 85 | { |
| 86 | struct bttv *btv = (struct bttv*)data; |
| 87 | int state; |
| 88 | |
| 89 | state = btread(BT848_I2C) & 0x02 ? 1 : 0; |
| 90 | return state; |
| 91 | } |
| 92 | |
| 93 | static int bttv_bit_getsda(void *data) |
| 94 | { |
| 95 | struct bttv *btv = (struct bttv*)data; |
| 96 | int state; |
| 97 | |
| 98 | state = btread(BT848_I2C) & 0x01; |
| 99 | return state; |
| 100 | } |
| 101 | |
Jean Delvare | df9b5d4 | 2008-06-15 12:20:18 -0300 | [diff] [blame^] | 102 | static struct i2c_algo_bit_data __devinitdata bttv_i2c_algo_bit_template = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | .setsda = bttv_bit_setsda, |
| 104 | .setscl = bttv_bit_setscl, |
| 105 | .getsda = bttv_bit_getsda, |
| 106 | .getscl = bttv_bit_getscl, |
| 107 | .udelay = 16, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | .timeout = 200, |
| 109 | }; |
| 110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | /* ----------------------------------------------------------------------- */ |
| 112 | /* I2C functions - hardware i2c */ |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | static u32 functionality(struct i2c_adapter *adap) |
| 115 | { |
| 116 | return I2C_FUNC_SMBUS_EMUL; |
| 117 | } |
| 118 | |
| 119 | static int |
| 120 | bttv_i2c_wait_done(struct bttv *btv) |
| 121 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | int rc = 0; |
| 123 | |
Manu Abraham | fc9d53a | 2005-05-05 16:16:01 -0700 | [diff] [blame] | 124 | /* timeout */ |
| 125 | if (wait_event_interruptible_timeout(btv->i2c_queue, |
| 126 | btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Manu Abraham | fc9d53a | 2005-05-05 16:16:01 -0700 | [diff] [blame] | 128 | rc = -EIO; |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | if (btv->i2c_done & BT848_INT_RACK) |
| 131 | rc = 1; |
| 132 | btv->i2c_done = 0; |
| 133 | return rc; |
| 134 | } |
| 135 | |
| 136 | #define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\ |
| 137 | BT848_I2C_SCL | BT848_I2C_SDA) |
| 138 | |
| 139 | static int |
| 140 | bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last) |
| 141 | { |
| 142 | u32 xmit; |
| 143 | int retval,cnt; |
| 144 | |
| 145 | /* sanity checks */ |
| 146 | if (0 == msg->len) |
| 147 | return -EINVAL; |
| 148 | |
| 149 | /* start, address + first byte */ |
| 150 | xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW; |
| 151 | if (msg->len > 1 || !last) |
| 152 | xmit |= BT878_I2C_NOSTOP; |
| 153 | btwrite(xmit, BT848_I2C); |
| 154 | retval = bttv_i2c_wait_done(btv); |
| 155 | if (retval < 0) |
| 156 | goto err; |
| 157 | if (retval == 0) |
| 158 | goto eio; |
| 159 | if (i2c_debug) { |
| 160 | printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]); |
| 161 | if (!(xmit & BT878_I2C_NOSTOP)) |
| 162 | printk(" >\n"); |
| 163 | } |
| 164 | |
| 165 | for (cnt = 1; cnt < msg->len; cnt++ ) { |
| 166 | /* following bytes */ |
| 167 | xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART; |
| 168 | if (cnt < msg->len-1 || !last) |
| 169 | xmit |= BT878_I2C_NOSTOP; |
| 170 | btwrite(xmit, BT848_I2C); |
| 171 | retval = bttv_i2c_wait_done(btv); |
| 172 | if (retval < 0) |
| 173 | goto err; |
| 174 | if (retval == 0) |
| 175 | goto eio; |
| 176 | if (i2c_debug) { |
| 177 | printk(" %02x", msg->buf[cnt]); |
| 178 | if (!(xmit & BT878_I2C_NOSTOP)) |
| 179 | printk(" >\n"); |
| 180 | } |
| 181 | } |
| 182 | return msg->len; |
| 183 | |
| 184 | eio: |
| 185 | retval = -EIO; |
| 186 | err: |
| 187 | if (i2c_debug) |
| 188 | printk(" ERR: %d\n",retval); |
| 189 | return retval; |
| 190 | } |
| 191 | |
| 192 | static int |
| 193 | bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last) |
| 194 | { |
| 195 | u32 xmit; |
| 196 | u32 cnt; |
| 197 | int retval; |
| 198 | |
| 199 | for(cnt = 0; cnt < msg->len; cnt++) { |
| 200 | xmit = (msg->addr << 25) | (1 << 24) | I2C_HW; |
| 201 | if (cnt < msg->len-1) |
| 202 | xmit |= BT848_I2C_W3B; |
| 203 | if (cnt < msg->len-1 || !last) |
| 204 | xmit |= BT878_I2C_NOSTOP; |
| 205 | if (cnt) |
| 206 | xmit |= BT878_I2C_NOSTART; |
| 207 | btwrite(xmit, BT848_I2C); |
| 208 | retval = bttv_i2c_wait_done(btv); |
| 209 | if (retval < 0) |
| 210 | goto err; |
| 211 | if (retval == 0) |
| 212 | goto eio; |
| 213 | msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff; |
| 214 | if (i2c_debug) { |
| 215 | if (!(xmit & BT878_I2C_NOSTART)) |
| 216 | printk(" <R %02x", (msg->addr << 1) +1); |
| 217 | printk(" =%02x", msg->buf[cnt]); |
| 218 | if (!(xmit & BT878_I2C_NOSTOP)) |
| 219 | printk(" >\n"); |
| 220 | } |
| 221 | } |
| 222 | return msg->len; |
| 223 | |
| 224 | eio: |
| 225 | retval = -EIO; |
| 226 | err: |
| 227 | if (i2c_debug) |
| 228 | printk(" ERR: %d\n",retval); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 229 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num) |
| 233 | { |
| 234 | struct bttv *btv = i2c_get_adapdata(i2c_adap); |
| 235 | int retval = 0; |
| 236 | int i; |
| 237 | |
| 238 | if (i2c_debug) |
| 239 | printk("bt-i2c:"); |
| 240 | btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT); |
| 241 | for (i = 0 ; i < num; i++) { |
| 242 | if (msgs[i].flags & I2C_M_RD) { |
| 243 | /* read */ |
| 244 | retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num); |
| 245 | if (retval < 0) |
| 246 | goto err; |
| 247 | } else { |
| 248 | /* write */ |
| 249 | retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num); |
| 250 | if (retval < 0) |
| 251 | goto err; |
| 252 | } |
| 253 | } |
| 254 | return num; |
| 255 | |
| 256 | err: |
| 257 | return retval; |
| 258 | } |
| 259 | |
Jean Delvare | df9b5d4 | 2008-06-15 12:20:18 -0300 | [diff] [blame^] | 260 | static const struct i2c_algorithm bttv_algo = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | .master_xfer = bttv_i2c_xfer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | .functionality = functionality, |
| 263 | }; |
| 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | /* ----------------------------------------------------------------------- */ |
| 266 | /* I2C functions - common stuff */ |
| 267 | |
| 268 | static int attach_inform(struct i2c_client *client) |
| 269 | { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 270 | struct bttv *btv = i2c_get_adapdata(client->adapter); |
Ricardo Cerqueira | 291d1d7 | 2005-11-08 21:38:33 -0800 | [diff] [blame] | 271 | int addr=ADDR_UNSET; |
Mauro Carvalho Chehab | aa8d5e7 | 2005-11-08 21:38:16 -0800 | [diff] [blame] | 272 | |
Ricardo Cerqueira | 291d1d7 | 2005-11-08 21:38:33 -0800 | [diff] [blame] | 273 | |
Lubomir Bulej | 7418f34 | 2005-11-08 21:38:34 -0800 | [diff] [blame] | 274 | if (ADDR_UNSET != bttv_tvcards[btv->c.type].tuner_addr) |
| 275 | addr = bttv_tvcards[btv->c.type].tuner_addr; |
Ricardo Cerqueira | 291d1d7 | 2005-11-08 21:38:33 -0800 | [diff] [blame] | 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Mauro Carvalho Chehab | fa9846a | 2005-07-12 13:58:42 -0700 | [diff] [blame] | 278 | if (bttv_debug) |
| 279 | printk(KERN_DEBUG "bttv%d: %s i2c attach [addr=0x%x,client=%s]\n", |
Laurent Riffard | 604f28e | 2005-11-26 20:43:39 +0100 | [diff] [blame] | 280 | btv->c.nr, client->driver->driver.name, client->addr, |
Jean Delvare | fae91e7 | 2005-08-15 19:57:04 +0200 | [diff] [blame] | 281 | client->name); |
Mauro Carvalho Chehab | fa9846a | 2005-07-12 13:58:42 -0700 | [diff] [blame] | 282 | if (!client->driver->command) |
| 283 | return 0; |
| 284 | |
Hans Verkuil | 8bf2f8e | 2006-03-18 21:31:00 -0300 | [diff] [blame] | 285 | if (client->driver->id == I2C_DRIVERID_MSP3400) |
| 286 | btv->i2c_msp34xx_client = client; |
| 287 | if (client->driver->id == I2C_DRIVERID_TVAUDIO) |
| 288 | btv->i2c_tvaudio_client = client; |
Mauro Carvalho Chehab | fa9846a | 2005-07-12 13:58:42 -0700 | [diff] [blame] | 289 | if (btv->tuner_type != UNSET) { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 290 | struct tuner_setup tun_setup; |
Mauro Carvalho Chehab | fa9846a | 2005-07-12 13:58:42 -0700 | [diff] [blame] | 291 | |
Ricardo Cerqueira | 291d1d7 | 2005-11-08 21:38:33 -0800 | [diff] [blame] | 292 | if ((addr==ADDR_UNSET) || |
| 293 | (addr==client->addr)) { |
| 294 | |
Lubomir Bulej | 7418f34 | 2005-11-08 21:38:34 -0800 | [diff] [blame] | 295 | tun_setup.mode_mask = T_ANALOG_TV | T_DIGITAL_TV | T_RADIO; |
| 296 | tun_setup.type = btv->tuner_type; |
Ricardo Cerqueira | 291d1d7 | 2005-11-08 21:38:33 -0800 | [diff] [blame] | 297 | tun_setup.addr = addr; |
Lubomir Bulej | 7418f34 | 2005-11-08 21:38:34 -0800 | [diff] [blame] | 298 | bttv_call_i2c_clients(btv, TUNER_SET_TYPE_ADDR, &tun_setup); |
Mauro Carvalho Chehab | 85a2eb0 | 2005-11-08 21:38:18 -0800 | [diff] [blame] | 299 | } |
Ricardo Cerqueira | 291d1d7 | 2005-11-08 21:38:33 -0800 | [diff] [blame] | 300 | |
Mauro Carvalho Chehab | fa9846a | 2005-07-12 13:58:42 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 303 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg) |
| 307 | { |
| 308 | if (0 != btv->i2c_rc) |
| 309 | return; |
| 310 | i2c_clients_command(&btv->c.i2c_adap, cmd, arg); |
| 311 | } |
| 312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | /* read I2C */ |
| 315 | int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for) |
| 316 | { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 317 | unsigned char buffer = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
| 319 | if (0 != btv->i2c_rc) |
| 320 | return -1; |
| 321 | if (bttv_verbose && NULL != probe_for) |
| 322 | printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ", |
| 323 | btv->c.nr,probe_for,addr); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 324 | btv->i2c_client.addr = addr >> 1; |
| 325 | if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | if (NULL != probe_for) { |
| 327 | if (bttv_verbose) |
| 328 | printk("not found\n"); |
| 329 | } else |
| 330 | printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n", |
| 331 | btv->c.nr,addr); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 332 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | } |
| 334 | if (bttv_verbose && NULL != probe_for) |
| 335 | printk("found\n"); |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 336 | return buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* write I2C */ |
| 340 | int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1, |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 341 | unsigned char b2, int both) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | { |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 343 | unsigned char buffer[2]; |
| 344 | int bytes = both ? 2 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
| 346 | if (0 != btv->i2c_rc) |
| 347 | return -1; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 348 | btv->i2c_client.addr = addr >> 1; |
| 349 | buffer[0] = b1; |
| 350 | buffer[1] = b2; |
| 351 | if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | return -1; |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 353 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /* read EEPROM content */ |
| 357 | void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr) |
| 358 | { |
Gerd Knorr | 5daf05f | 2005-05-25 12:31:26 -0700 | [diff] [blame] | 359 | memset(eedata, 0, 256); |
| 360 | if (0 != btv->i2c_rc) |
| 361 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | btv->i2c_client.addr = addr >> 1; |
| 363 | tveeprom_read(&btv->i2c_client, eedata, 256); |
| 364 | } |
| 365 | |
| 366 | static char *i2c_devs[128] = { |
Mauro Carvalho Chehab | 24a70fd | 2005-09-09 13:03:39 -0700 | [diff] [blame] | 367 | [ 0x1c >> 1 ] = "lgdt330x", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | [ 0x30 >> 1 ] = "IR (hauppauge)", |
| 369 | [ 0x80 >> 1 ] = "msp34xx", |
| 370 | [ 0x86 >> 1 ] = "tda9887", |
| 371 | [ 0xa0 >> 1 ] = "eeprom", |
| 372 | [ 0xc0 >> 1 ] = "tuner (analog)", |
| 373 | [ 0xc2 >> 1 ] = "tuner (analog)", |
| 374 | }; |
| 375 | |
| 376 | static void do_i2c_scan(char *name, struct i2c_client *c) |
| 377 | { |
| 378 | unsigned char buf; |
| 379 | int i,rc; |
| 380 | |
Mauro Carvalho Chehab | 53c4e95 | 2007-03-29 08:42:30 -0300 | [diff] [blame] | 381 | for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | c->addr = i; |
| 383 | rc = i2c_master_recv(c,&buf,0); |
| 384 | if (rc < 0) |
| 385 | continue; |
| 386 | printk("%s: i2c scan: found device @ 0x%x [%s]\n", |
| 387 | name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???"); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | /* init + register i2c algo-bit adapter */ |
| 392 | int __devinit init_bttv_i2c(struct bttv *btv) |
| 393 | { |
Jean Delvare | df9b5d4 | 2008-06-15 12:20:18 -0300 | [diff] [blame^] | 394 | strlcpy(btv->i2c_client.name, "bttv internal", I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
| 396 | if (i2c_hw) |
| 397 | btv->use_i2c_hw = 1; |
| 398 | if (btv->use_i2c_hw) { |
| 399 | /* bt878 */ |
Jean Delvare | df9b5d4 | 2008-06-15 12:20:18 -0300 | [diff] [blame^] | 400 | strlcpy(btv->c.i2c_adap.name, "bt878", |
| 401 | sizeof(btv->c.i2c_adap.name)); |
| 402 | btv->c.i2c_adap.id = I2C_HW_B_BT848; /* FIXME */ |
| 403 | btv->c.i2c_adap.algo = &bttv_algo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | } else { |
| 405 | /* bt848 */ |
Mauro Carvalho Chehab | 141276b | 2006-09-06 19:04:28 -0300 | [diff] [blame] | 406 | /* Prevents usage of invalid delay values */ |
| 407 | if (i2c_udelay<5) |
| 408 | i2c_udelay=5; |
Mauro Carvalho Chehab | 141276b | 2006-09-06 19:04:28 -0300 | [diff] [blame] | 409 | |
Jean Delvare | df9b5d4 | 2008-06-15 12:20:18 -0300 | [diff] [blame^] | 410 | strlcpy(btv->c.i2c_adap.name, "bttv", |
| 411 | sizeof(btv->c.i2c_adap.name)); |
| 412 | btv->c.i2c_adap.id = I2C_HW_B_BT848; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template, |
| 414 | sizeof(bttv_i2c_algo_bit_template)); |
Jean Delvare | df9b5d4 | 2008-06-15 12:20:18 -0300 | [diff] [blame^] | 415 | btv->i2c_algo.udelay = i2c_udelay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | btv->i2c_algo.data = btv; |
| 417 | btv->c.i2c_adap.algo_data = &btv->i2c_algo; |
| 418 | } |
Jean Delvare | df9b5d4 | 2008-06-15 12:20:18 -0300 | [diff] [blame^] | 419 | btv->c.i2c_adap.owner = THIS_MODULE; |
| 420 | btv->c.i2c_adap.class = I2C_CLASS_TV_ANALOG; |
| 421 | btv->c.i2c_adap.client_register = attach_inform; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | |
| 423 | btv->c.i2c_adap.dev.parent = &btv->c.pci->dev; |
| 424 | snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name), |
| 425 | "bt%d #%d [%s]", btv->id, btv->c.nr, |
| 426 | btv->use_i2c_hw ? "hw" : "sw"); |
| 427 | |
Mauro Carvalho Chehab | 4ac9791 | 2005-11-08 21:37:43 -0800 | [diff] [blame] | 428 | i2c_set_adapdata(&btv->c.i2c_adap, btv); |
| 429 | btv->i2c_client.adapter = &btv->c.i2c_adap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | if (bttv_tvcards[btv->c.type].no_video) |
| 432 | btv->c.i2c_adap.class &= ~I2C_CLASS_TV_ANALOG; |
| 433 | if (bttv_tvcards[btv->c.type].has_dvb) |
| 434 | btv->c.i2c_adap.class |= I2C_CLASS_TV_DIGITAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | |
| 436 | if (btv->use_i2c_hw) { |
| 437 | btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap); |
| 438 | } else { |
| 439 | bttv_bit_setscl(btv,1); |
| 440 | bttv_bit_setsda(btv,1); |
| 441 | btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap); |
| 442 | } |
| 443 | if (0 == btv->i2c_rc && i2c_scan) |
| 444 | do_i2c_scan(btv->c.name,&btv->i2c_client); |
| 445 | return btv->i2c_rc; |
| 446 | } |
| 447 | |
| 448 | int __devexit fini_bttv_i2c(struct bttv *btv) |
| 449 | { |
| 450 | if (0 != btv->i2c_rc) |
| 451 | return 0; |
| 452 | |
Jean Delvare | 3269711 | 2006-12-10 21:21:33 +0100 | [diff] [blame] | 453 | return i2c_del_adapter(&btv->c.i2c_adap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Local variables: |
| 458 | * c-basic-offset: 8 |
| 459 | * End: |
| 460 | */ |