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