diff options
Diffstat (limited to 'drivers/media/video/bt8xx/bttv-i2c.c')
-rw-r--r-- | drivers/media/video/bt8xx/bttv-i2c.c | 391 |
1 files changed, 391 insertions, 0 deletions
diff --git a/drivers/media/video/bt8xx/bttv-i2c.c b/drivers/media/video/bt8xx/bttv-i2c.c new file mode 100644 index 00000000000..d49b675045f --- /dev/null +++ b/drivers/media/video/bt8xx/bttv-i2c.c | |||
@@ -0,0 +1,391 @@ | |||
1 | /* | ||
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) | ||
8 | & Marcus Metzler (mocm@thp.uni-koeln.de) | ||
9 | (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org> | ||
10 | |||
11 | (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org> | ||
12 | - Multituner support and i2c address binding | ||
13 | |||
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/init.h> | ||
32 | #include <linux/delay.h> | ||
33 | |||
34 | #include "bttvp.h" | ||
35 | #include <media/v4l2-common.h> | ||
36 | #include <linux/jiffies.h> | ||
37 | #include <asm/io.h> | ||
38 | |||
39 | static int i2c_debug; | ||
40 | static int i2c_hw; | ||
41 | static int i2c_scan; | ||
42 | module_param(i2c_debug, int, 0644); | ||
43 | MODULE_PARM_DESC(i2c_debug, "configure i2c debug level"); | ||
44 | module_param(i2c_hw, int, 0444); | ||
45 | MODULE_PARM_DESC(i2c_hw,"force use of hardware i2c support, " | ||
46 | "instead of software bitbang"); | ||
47 | module_param(i2c_scan, int, 0444); | ||
48 | MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time"); | ||
49 | |||
50 | static unsigned int i2c_udelay = 5; | ||
51 | module_param(i2c_udelay, int, 0444); | ||
52 | MODULE_PARM_DESC(i2c_udelay,"soft i2c delay at insmod time, in usecs " | ||
53 | "(should be 5 or higher). Lower value means higher bus speed."); | ||
54 | |||
55 | /* ----------------------------------------------------------------------- */ | ||
56 | /* I2C functions - bitbanging adapter (software i2c) */ | ||
57 | |||
58 | static void bttv_bit_setscl(void *data, int state) | ||
59 | { | ||
60 | struct bttv *btv = (struct bttv*)data; | ||
61 | |||
62 | if (state) | ||
63 | btv->i2c_state |= 0x02; | ||
64 | else | ||
65 | btv->i2c_state &= ~0x02; | ||
66 | btwrite(btv->i2c_state, BT848_I2C); | ||
67 | btread(BT848_I2C); | ||
68 | } | ||
69 | |||
70 | static void bttv_bit_setsda(void *data, int state) | ||
71 | { | ||
72 | struct bttv *btv = (struct bttv*)data; | ||
73 | |||
74 | if (state) | ||
75 | btv->i2c_state |= 0x01; | ||
76 | else | ||
77 | btv->i2c_state &= ~0x01; | ||
78 | btwrite(btv->i2c_state, BT848_I2C); | ||
79 | btread(BT848_I2C); | ||
80 | } | ||
81 | |||
82 | static int bttv_bit_getscl(void *data) | ||
83 | { | ||
84 | struct bttv *btv = (struct bttv*)data; | ||
85 | int state; | ||
86 | |||
87 | state = btread(BT848_I2C) & 0x02 ? 1 : 0; | ||
88 | return state; | ||
89 | } | ||
90 | |||
91 | static int bttv_bit_getsda(void *data) | ||
92 | { | ||
93 | struct bttv *btv = (struct bttv*)data; | ||
94 | int state; | ||
95 | |||
96 | state = btread(BT848_I2C) & 0x01; | ||
97 | return state; | ||
98 | } | ||
99 | |||
100 | static struct i2c_algo_bit_data __devinitdata bttv_i2c_algo_bit_template = { | ||
101 | .setsda = bttv_bit_setsda, | ||
102 | .setscl = bttv_bit_setscl, | ||
103 | .getsda = bttv_bit_getsda, | ||
104 | .getscl = bttv_bit_getscl, | ||
105 | .udelay = 16, | ||
106 | .timeout = 200, | ||
107 | }; | ||
108 | |||
109 | /* ----------------------------------------------------------------------- */ | ||
110 | /* I2C functions - hardware i2c */ | ||
111 | |||
112 | static u32 functionality(struct i2c_adapter *adap) | ||
113 | { | ||
114 | return I2C_FUNC_SMBUS_EMUL; | ||
115 | } | ||
116 | |||
117 | static int | ||
118 | bttv_i2c_wait_done(struct bttv *btv) | ||
119 | { | ||
120 | int rc = 0; | ||
121 | |||
122 | /* timeout */ | ||
123 | if (wait_event_interruptible_timeout(btv->i2c_queue, | ||
124 | btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS) | ||
125 | rc = -EIO; | ||
126 | |||
127 | if (btv->i2c_done & BT848_INT_RACK) | ||
128 | rc = 1; | ||
129 | btv->i2c_done = 0; | ||
130 | return rc; | ||
131 | } | ||
132 | |||
133 | #define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\ | ||
134 | BT848_I2C_SCL | BT848_I2C_SDA) | ||
135 | |||
136 | static int | ||
137 | bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last) | ||
138 | { | ||
139 | u32 xmit; | ||
140 | int retval,cnt; | ||
141 | |||
142 | /* sanity checks */ | ||
143 | if (0 == msg->len) | ||
144 | return -EINVAL; | ||
145 | |||
146 | /* start, address + first byte */ | ||
147 | xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW; | ||
148 | if (msg->len > 1 || !last) | ||
149 | xmit |= BT878_I2C_NOSTOP; | ||
150 | btwrite(xmit, BT848_I2C); | ||
151 | retval = bttv_i2c_wait_done(btv); | ||
152 | if (retval < 0) | ||
153 | goto err; | ||
154 | if (retval == 0) | ||
155 | goto eio; | ||
156 | if (i2c_debug) { | ||
157 | printk(" <W %02x %02x", msg->addr << 1, msg->buf[0]); | ||
158 | if (!(xmit & BT878_I2C_NOSTOP)) | ||
159 | printk(" >\n"); | ||
160 | } | ||
161 | |||
162 | for (cnt = 1; cnt < msg->len; cnt++ ) { | ||
163 | /* following bytes */ | ||
164 | xmit = (msg->buf[cnt] << 24) | I2C_HW | BT878_I2C_NOSTART; | ||
165 | if (cnt < msg->len-1 || !last) | ||
166 | xmit |= BT878_I2C_NOSTOP; | ||
167 | btwrite(xmit, BT848_I2C); | ||
168 | retval = bttv_i2c_wait_done(btv); | ||
169 | if (retval < 0) | ||
170 | goto err; | ||
171 | if (retval == 0) | ||
172 | goto eio; | ||
173 | if (i2c_debug) { | ||
174 | printk(" %02x", msg->buf[cnt]); | ||
175 | if (!(xmit & BT878_I2C_NOSTOP)) | ||
176 | printk(" >\n"); | ||
177 | } | ||
178 | } | ||
179 | return msg->len; | ||
180 | |||
181 | eio: | ||
182 | retval = -EIO; | ||
183 | err: | ||
184 | if (i2c_debug) | ||
185 | printk(" ERR: %d\n",retval); | ||
186 | return retval; | ||
187 | } | ||
188 | |||
189 | static int | ||
190 | bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last) | ||
191 | { | ||
192 | u32 xmit; | ||
193 | u32 cnt; | ||
194 | int retval; | ||
195 | |||
196 | for(cnt = 0; cnt < msg->len; cnt++) { | ||
197 | xmit = (msg->addr << 25) | (1 << 24) | I2C_HW; | ||
198 | if (cnt < msg->len-1) | ||
199 | xmit |= BT848_I2C_W3B; | ||
200 | if (cnt < msg->len-1 || !last) | ||
201 | xmit |= BT878_I2C_NOSTOP; | ||
202 | if (cnt) | ||
203 | xmit |= BT878_I2C_NOSTART; | ||
204 | btwrite(xmit, BT848_I2C); | ||
205 | retval = bttv_i2c_wait_done(btv); | ||
206 | if (retval < 0) | ||
207 | goto err; | ||
208 | if (retval == 0) | ||
209 | goto eio; | ||
210 | msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff; | ||
211 | if (i2c_debug) { | ||
212 | if (!(xmit & BT878_I2C_NOSTART)) | ||
213 | printk(" <R %02x", (msg->addr << 1) +1); | ||
214 | printk(" =%02x", msg->buf[cnt]); | ||
215 | if (!(xmit & BT878_I2C_NOSTOP)) | ||
216 | printk(" >\n"); | ||
217 | } | ||
218 | } | ||
219 | return msg->len; | ||
220 | |||
221 | eio: | ||
222 | retval = -EIO; | ||
223 | err: | ||
224 | if (i2c_debug) | ||
225 | printk(" ERR: %d\n",retval); | ||
226 | return retval; | ||
227 | } | ||
228 | |||
229 | static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num) | ||
230 | { | ||
231 | struct v4l2_device *v4l2_dev = i2c_get_adapdata(i2c_adap); | ||
232 | struct bttv *btv = to_bttv(v4l2_dev); | ||
233 | int retval = 0; | ||
234 | int i; | ||
235 | |||
236 | if (i2c_debug) | ||
237 | printk("bt-i2c:"); | ||
238 | btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT); | ||
239 | for (i = 0 ; i < num; i++) { | ||
240 | if (msgs[i].flags & I2C_M_RD) { | ||
241 | /* read */ | ||
242 | retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num); | ||
243 | if (retval < 0) | ||
244 | goto err; | ||
245 | } else { | ||
246 | /* write */ | ||
247 | retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num); | ||
248 | if (retval < 0) | ||
249 | goto err; | ||
250 | } | ||
251 | } | ||
252 | return num; | ||
253 | |||
254 | err: | ||
255 | return retval; | ||
256 | } | ||
257 | |||
258 | static const struct i2c_algorithm bttv_algo = { | ||
259 | .master_xfer = bttv_i2c_xfer, | ||
260 | .functionality = functionality, | ||
261 | }; | ||
262 | |||
263 | /* ----------------------------------------------------------------------- */ | ||
264 | /* I2C functions - common stuff */ | ||
265 | |||
266 | /* read I2C */ | ||
267 | int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for) | ||
268 | { | ||
269 | unsigned char buffer = 0; | ||
270 | |||
271 | if (0 != btv->i2c_rc) | ||
272 | return -1; | ||
273 | if (bttv_verbose && NULL != probe_for) | ||
274 | printk(KERN_INFO "bttv%d: i2c: checking for %s @ 0x%02x... ", | ||
275 | btv->c.nr,probe_for,addr); | ||
276 | btv->i2c_client.addr = addr >> 1; | ||
277 | if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) { | ||
278 | if (NULL != probe_for) { | ||
279 | if (bttv_verbose) | ||
280 | printk("not found\n"); | ||
281 | } else | ||
282 | printk(KERN_WARNING "bttv%d: i2c read 0x%x: error\n", | ||
283 | btv->c.nr,addr); | ||
284 | return -1; | ||
285 | } | ||
286 | if (bttv_verbose && NULL != probe_for) | ||
287 | printk("found\n"); | ||
288 | return buffer; | ||
289 | } | ||
290 | |||
291 | /* write I2C */ | ||
292 | int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1, | ||
293 | unsigned char b2, int both) | ||
294 | { | ||
295 | unsigned char buffer[2]; | ||
296 | int bytes = both ? 2 : 1; | ||
297 | |||
298 | if (0 != btv->i2c_rc) | ||
299 | return -1; | ||
300 | btv->i2c_client.addr = addr >> 1; | ||
301 | buffer[0] = b1; | ||
302 | buffer[1] = b2; | ||
303 | if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes)) | ||
304 | return -1; | ||
305 | return 0; | ||
306 | } | ||
307 | |||
308 | /* read EEPROM content */ | ||
309 | void __devinit bttv_readee(struct bttv *btv, unsigned char *eedata, int addr) | ||
310 | { | ||
311 | memset(eedata, 0, 256); | ||
312 | if (0 != btv->i2c_rc) | ||
313 | return; | ||
314 | btv->i2c_client.addr = addr >> 1; | ||
315 | tveeprom_read(&btv->i2c_client, eedata, 256); | ||
316 | } | ||
317 | |||
318 | static char *i2c_devs[128] = { | ||
319 | [ 0x1c >> 1 ] = "lgdt330x", | ||
320 | [ 0x30 >> 1 ] = "IR (hauppauge)", | ||
321 | [ 0x80 >> 1 ] = "msp34xx", | ||
322 | [ 0x86 >> 1 ] = "tda9887", | ||
323 | [ 0xa0 >> 1 ] = "eeprom", | ||
324 | [ 0xc0 >> 1 ] = "tuner (analog)", | ||
325 | [ 0xc2 >> 1 ] = "tuner (analog)", | ||
326 | }; | ||
327 | |||
328 | static void do_i2c_scan(char *name, struct i2c_client *c) | ||
329 | { | ||
330 | unsigned char buf; | ||
331 | int i,rc; | ||
332 | |||
333 | for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) { | ||
334 | c->addr = i; | ||
335 | rc = i2c_master_recv(c,&buf,0); | ||
336 | if (rc < 0) | ||
337 | continue; | ||
338 | printk("%s: i2c scan: found device @ 0x%x [%s]\n", | ||
339 | name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???"); | ||
340 | } | ||
341 | } | ||
342 | |||
343 | /* init + register i2c algo-bit adapter */ | ||
344 | int __devinit init_bttv_i2c(struct bttv *btv) | ||
345 | { | ||
346 | strlcpy(btv->i2c_client.name, "bttv internal", I2C_NAME_SIZE); | ||
347 | |||
348 | if (i2c_hw) | ||
349 | btv->use_i2c_hw = 1; | ||
350 | if (btv->use_i2c_hw) { | ||
351 | /* bt878 */ | ||
352 | strlcpy(btv->c.i2c_adap.name, "bt878", | ||
353 | sizeof(btv->c.i2c_adap.name)); | ||
354 | btv->c.i2c_adap.algo = &bttv_algo; | ||
355 | } else { | ||
356 | /* bt848 */ | ||
357 | /* Prevents usage of invalid delay values */ | ||
358 | if (i2c_udelay<5) | ||
359 | i2c_udelay=5; | ||
360 | |||
361 | strlcpy(btv->c.i2c_adap.name, "bttv", | ||
362 | sizeof(btv->c.i2c_adap.name)); | ||
363 | memcpy(&btv->i2c_algo, &bttv_i2c_algo_bit_template, | ||
364 | sizeof(bttv_i2c_algo_bit_template)); | ||
365 | btv->i2c_algo.udelay = i2c_udelay; | ||
366 | btv->i2c_algo.data = btv; | ||
367 | btv->c.i2c_adap.algo_data = &btv->i2c_algo; | ||
368 | } | ||
369 | btv->c.i2c_adap.owner = THIS_MODULE; | ||
370 | |||
371 | btv->c.i2c_adap.dev.parent = &btv->c.pci->dev; | ||
372 | snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name), | ||
373 | "bt%d #%d [%s]", btv->id, btv->c.nr, | ||
374 | btv->use_i2c_hw ? "hw" : "sw"); | ||
375 | |||
376 | i2c_set_adapdata(&btv->c.i2c_adap, &btv->c.v4l2_dev); | ||
377 | btv->i2c_client.adapter = &btv->c.i2c_adap; | ||
378 | |||
379 | |||
380 | if (btv->use_i2c_hw) { | ||
381 | btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap); | ||
382 | } else { | ||
383 | bttv_bit_setscl(btv,1); | ||
384 | bttv_bit_setsda(btv,1); | ||
385 | btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap); | ||
386 | } | ||
387 | if (0 == btv->i2c_rc && i2c_scan) | ||
388 | do_i2c_scan(btv->c.v4l2_dev.name, &btv->i2c_client); | ||
389 | |||
390 | return btv->i2c_rc; | ||
391 | } | ||