Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | /* bttv-if.c -- interfaces to other kernel modules all the i2c code is here also the gpio interface exported by bttv (used by lirc) bttv - Bt848 frame grabber driver Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de) & Marcus Metzler (mocm@thp.uni-koeln.de) (c) 1999,2000 Gerd Knorr <kraxel@goldbach.in-berlin.de> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #define __NO_VERSION__ 1 #include <linux/version.h> #include <linux/module.h> #include <linux/init.h> #include <asm/io.h> #include "bttvp.h" #include "tuner.h" static struct i2c_algo_bit_data bttv_i2c_algo_template; static struct i2c_adapter bttv_i2c_adap_template; static struct i2c_client bttv_i2c_client_template; EXPORT_SYMBOL(bttv_get_cardinfo); EXPORT_SYMBOL(bttv_get_id); EXPORT_SYMBOL(bttv_gpio_enable); EXPORT_SYMBOL(bttv_read_gpio); EXPORT_SYMBOL(bttv_write_gpio); EXPORT_SYMBOL(bttv_get_gpio_queue); /* ----------------------------------------------------------------------- */ /* Exported functions - for other modules which want to access the */ /* gpio ports (IR for example) */ /* see bttv.h for comments */ int bttv_get_cardinfo(unsigned int card, int *type, int *cardid) { if (card >= bttv_num) { return -1; } *type = bttvs[card].type; *cardid = bttvs[card].cardid; return 0; } int bttv_get_id(unsigned int card) { printk("bttv_get_id is obsolete, use bttv_get_cardinfo instead\n"); if (card >= bttv_num) { return -1; } return bttvs[card].type; } int bttv_gpio_enable(unsigned int card, unsigned long mask, unsigned long data) { struct bttv *btv; if (card >= bttv_num) { return -EINVAL; } btv = &bttvs[card]; btaor(data, ~mask, BT848_GPIO_OUT_EN); if (bttv_gpio) bttv_gpio_tracking(btv,"extern enable"); return 0; } int bttv_read_gpio(unsigned int card, unsigned long *data) { struct bttv *btv; if (card >= bttv_num) { return -EINVAL; } btv = &bttvs[card]; if(btv->shutdown) { return -ENODEV; } /* prior setting BT848_GPIO_REG_INP is (probably) not needed because we set direct input on init */ *data = btread(BT848_GPIO_DATA); return 0; } int bttv_write_gpio(unsigned int card, unsigned long mask, unsigned long data) { struct bttv *btv; if (card >= bttv_num) { return -EINVAL; } btv = &bttvs[card]; /* prior setting BT848_GPIO_REG_INP is (probably) not needed because direct input is set on init */ btaor(data & mask, ~mask, BT848_GPIO_DATA); if (bttv_gpio) bttv_gpio_tracking(btv,"extern write"); return 0; } wait_queue_head_t* bttv_get_gpio_queue(unsigned int card) { struct bttv *btv; if (card >= bttv_num) { return NULL; } btv = &bttvs[card]; if (bttvs[card].shutdown) { return NULL; } return &btv->gpioq; } /* ----------------------------------------------------------------------- */ /* I2C functions */ void bttv_bit_setscl(void *data, int state) { struct bttv *btv = (struct bttv*)data; if (state) btv->i2c_state |= 0x02; else btv->i2c_state &= ~0x02; btwrite(btv->i2c_state, BT848_I2C); btread(BT848_I2C); } void bttv_bit_setsda(void *data, int state) { struct bttv *btv = (struct bttv*)data; if (state) btv->i2c_state |= 0x01; else btv->i2c_state &= ~0x01; btwrite(btv->i2c_state, BT848_I2C); btread(BT848_I2C); } static int bttv_bit_getscl(void *data) { struct bttv *btv = (struct bttv*)data; int state; state = btread(BT848_I2C) & 0x02 ? 1 : 0; return state; } static int bttv_bit_getsda(void *data) { struct bttv *btv = (struct bttv*)data; int state; state = btread(BT848_I2C) & 0x01; return state; } static void bttv_inc_use(struct i2c_adapter *adap) { MOD_INC_USE_COUNT; } static void bttv_dec_use(struct i2c_adapter *adap) { MOD_DEC_USE_COUNT; } static int attach_inform(struct i2c_client *client) { struct bttv *btv = (struct bttv*)client->adapter->data; int i; for (i = 0; i < I2C_CLIENTS_MAX; i++) { if (btv->i2c_clients[i] == NULL) { btv->i2c_clients[i] = client; break; } } if (btv->tuner_type != -1) bttv_call_i2c_clients(btv,TUNER_SET_TYPE,&btv->tuner_type); if (bttv_verbose) printk("bttv%d: i2c attach [client=%s,%s]\n",btv->nr, client->name, (i < I2C_CLIENTS_MAX) ? "ok" : "failed"); return 0; } static int detach_inform(struct i2c_client *client) { struct bttv *btv = (struct bttv*)client->adapter->data; int i; for (i = 0; i < I2C_CLIENTS_MAX; i++) { if (btv->i2c_clients[i] == client) { btv->i2c_clients[i] = NULL; break; } } if (bttv_verbose) printk("bttv%d: i2c detach [client=%s,%s]\n",btv->nr, client->name, (i < I2C_CLIENTS_MAX) ? "ok" : "failed"); return 0; } void bttv_call_i2c_clients(struct bttv *btv, unsigned int cmd, void *arg) { int i; for (i = 0; i < I2C_CLIENTS_MAX; i++) { if (NULL == btv->i2c_clients[i]) continue; if (NULL == btv->i2c_clients[i]->driver->command) continue; btv->i2c_clients[i]->driver->command( btv->i2c_clients[i],cmd,arg); } } static struct i2c_algo_bit_data bttv_i2c_algo_template = { setsda: bttv_bit_setsda, setscl: bttv_bit_setscl, getsda: bttv_bit_getsda, getscl: bttv_bit_getscl, udelay: 16, mdelay: 10, timeout: 200, }; static struct i2c_adapter bttv_i2c_adap_template = { name: "bt848", id: I2C_HW_B_BT848, inc_use: bttv_inc_use, dec_use: bttv_dec_use, client_register: attach_inform, client_unregister: detach_inform, }; static struct i2c_client bttv_i2c_client_template = { name: "bttv internal use only", id: -1, }; /* read I2C */ int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for) { unsigned char buffer = 0; if (0 != btv->i2c_rc) return -1; if (bttv_verbose && NULL != probe_for) printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ", btv->nr,probe_for,addr); btv->i2c_client.addr = addr >> 1; if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) { if (NULL != probe_for) { if (bttv_verbose) printk("not found\n"); } else printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n", btv->nr,addr); return -1; } if (bttv_verbose && NULL != probe_for) printk("found\n"); return buffer; } /* write I2C */ int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1, unsigned char b2, int both) { unsigned char buffer[2]; int bytes = both ? 2 : 1; if (0 != btv->i2c_rc) return -1; btv->i2c_client.addr = addr >> 1; buffer[0] = b1; buffer[1] = b2; if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes)) return -1; return 0; } /* read EEPROM content */ void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr) { int i; if (bttv_I2CWrite(btv, addr, 0, -1, 0)<0) { printk(KERN_WARNING "bttv: readee error\n"); return; } btv->i2c_client.addr = addr >> 1; for (i=0; i<256; i+=16) { if (16 != i2c_master_recv(&btv->i2c_client,eedata+i,16)) { printk(KERN_WARNING "bttv: readee error\n"); break; } } } /* init + register i2c algo-bit adapter */ int __devinit init_bttv_i2c(struct bttv *btv) { memcpy(&btv->i2c_adap, &bttv_i2c_adap_template, sizeof(struct i2c_adapter)); memcpy(&btv->i2c_algo, &bttv_i2c_algo_template, sizeof(struct i2c_algo_bit_data)); memcpy(&btv->i2c_client, &bttv_i2c_client_template, sizeof(struct i2c_client)); sprintf(btv->i2c_adap.name+strlen(btv->i2c_adap.name), " #%d", btv->nr); btv->i2c_algo.data = btv; btv->i2c_adap.data = btv; btv->i2c_adap.algo_data = &btv->i2c_algo; btv->i2c_client.adapter = &btv->i2c_adap; bttv_bit_setscl(btv,1); bttv_bit_setsda(btv,1); btv->i2c_rc = i2c_bit_add_bus(&btv->i2c_adap); return btv->i2c_rc; } /* * Local variables: * c-basic-offset: 8 * End: */ |