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