blob: d43911deb617a97a78348bc5e7bdb5f6ee1bf265 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
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 Chehab4ac97912005-11-08 21:37:43 -08008 & Marcus Metzler (mocm@thp.uni-koeln.de)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 (c) 1999-2003 Gerd Knorr <kraxel@bytesex.org>
10
Mauro Carvalho Chehab141276b2006-09-06 19:04:28 -030011 (c) 2005 Mauro Carvalho Chehab <mchehab@infradead.org>
12 - Multituner support and i2c address binding
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 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
Joe Perches8af443e2011-08-21 19:56:48 -030030#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/init.h>
34#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include "bttvp.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020037#include <media/v4l2-common.h>
Mauro Carvalho Chehabb5b8ab82006-01-09 15:25:20 -020038#include <linux/jiffies.h>
39#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Mauro Carvalho Chehaba5ed4252006-01-13 14:10:19 -020041static int i2c_debug;
42static int i2c_hw;
43static int i2c_scan;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044module_param(i2c_debug, int, 0644);
Jarod Wilson0a25f3b2009-12-10 16:54:48 -030045MODULE_PARM_DESC(i2c_debug, "configure i2c debug level");
Linus Torvalds1da177e2005-04-16 15:20:36 -070046module_param(i2c_hw, int, 0444);
Mauro Carvalho Chehab141276b2006-09-06 19:04:28 -030047MODULE_PARM_DESC(i2c_hw,"force use of hardware i2c support, "
48 "instead of software bitbang");
Linus Torvalds1da177e2005-04-16 15:20:36 -070049module_param(i2c_scan, int, 0444);
50MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
51
Mauro Carvalho Chehab141276b2006-09-06 19:04:28 -030052static unsigned int i2c_udelay = 5;
53module_param(i2c_udelay, int, 0444);
54MODULE_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 Torvalds1da177e2005-04-16 15:20:36 -070057/* ----------------------------------------------------------------------- */
58/* I2C functions - bitbanging adapter (software i2c) */
59
60static 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
72static 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
84static 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
93static 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
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800102static struct i2c_algo_bit_data bttv_i2c_algo_bit_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .setsda = bttv_bit_setsda,
104 .setscl = bttv_bit_setscl,
105 .getsda = bttv_bit_getsda,
106 .getscl = bttv_bit_getscl,
107 .udelay = 16,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 .timeout = 200,
109};
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111/* ----------------------------------------------------------------------- */
112/* I2C functions - hardware i2c */
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114static u32 functionality(struct i2c_adapter *adap)
115{
116 return I2C_FUNC_SMBUS_EMUL;
117}
118
119static int
120bttv_i2c_wait_done(struct bttv *btv)
121{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 int rc = 0;
123
Manu Abrahamfc9d53a2005-05-05 16:16:01 -0700124 /* timeout */
125 if (wait_event_interruptible_timeout(btv->i2c_queue,
Julia Lawall3074fc82010-08-05 17:29:55 -0300126 btv->i2c_done, msecs_to_jiffies(85)) == -ERESTARTSYS)
127 rc = -EIO;
Manu Abrahamfc9d53a2005-05-05 16:16:01 -0700128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (btv->i2c_done & BT848_INT_RACK)
130 rc = 1;
131 btv->i2c_done = 0;
132 return rc;
133}
134
135#define I2C_HW (BT878_I2C_MODE | BT848_I2C_SYNC |\
136 BT848_I2C_SCL | BT848_I2C_SDA)
137
138static int
139bttv_i2c_sendbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
140{
141 u32 xmit;
142 int retval,cnt;
143
144 /* sanity checks */
145 if (0 == msg->len)
146 return -EINVAL;
147
148 /* start, address + first byte */
149 xmit = (msg->addr << 25) | (msg->buf[0] << 16) | I2C_HW;
150 if (msg->len > 1 || !last)
151 xmit |= BT878_I2C_NOSTOP;
152 btwrite(xmit, BT848_I2C);
153 retval = bttv_i2c_wait_done(btv);
154 if (retval < 0)
155 goto err;
156 if (retval == 0)
157 goto eio;
158 if (i2c_debug) {
Joe Perches8af443e2011-08-21 19:56:48 -0300159 pr_cont(" <W %02x %02x", msg->addr << 1, msg->buf[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
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;
Joe Perches8af443e2011-08-21 19:56:48 -0300173 if (i2c_debug)
174 pr_cont(" %02x", msg->buf[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
John Törnblom1ca6ae82012-12-17 08:53:54 -0300176 if (i2c_debug && !(xmit & BT878_I2C_NOSTOP))
Joe Perches8af443e2011-08-21 19:56:48 -0300177 pr_cont(">\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return msg->len;
179
180 eio:
181 retval = -EIO;
182 err:
183 if (i2c_debug)
Joe Perches8af443e2011-08-21 19:56:48 -0300184 pr_cont(" ERR: %d\n",retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 return retval;
186}
187
188static int
189bttv_i2c_readbytes(struct bttv *btv, const struct i2c_msg *msg, int last)
190{
191 u32 xmit;
192 u32 cnt;
193 int retval;
194
Joe Perches8af443e2011-08-21 19:56:48 -0300195 for (cnt = 0; cnt < msg->len; cnt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 xmit = (msg->addr << 25) | (1 << 24) | I2C_HW;
197 if (cnt < msg->len-1)
198 xmit |= BT848_I2C_W3B;
199 if (cnt < msg->len-1 || !last)
200 xmit |= BT878_I2C_NOSTOP;
201 if (cnt)
202 xmit |= BT878_I2C_NOSTART;
Joe Perches8af443e2011-08-21 19:56:48 -0300203
204 if (i2c_debug) {
205 if (!(xmit & BT878_I2C_NOSTART))
206 pr_cont(" <R %02x", (msg->addr << 1) +1);
207 }
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 btwrite(xmit, BT848_I2C);
210 retval = bttv_i2c_wait_done(btv);
211 if (retval < 0)
212 goto err;
213 if (retval == 0)
214 goto eio;
215 msg->buf[cnt] = ((u32)btread(BT848_I2C) >> 8) & 0xff;
216 if (i2c_debug) {
Joe Perches8af443e2011-08-21 19:56:48 -0300217 pr_cont(" =%02x", msg->buf[cnt]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Joe Perches8af443e2011-08-21 19:56:48 -0300219 if (i2c_debug && !(xmit & BT878_I2C_NOSTOP))
220 pr_cont(" >\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Joe Perches8af443e2011-08-21 19:56:48 -0300222
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return msg->len;
225
226 eio:
227 retval = -EIO;
228 err:
229 if (i2c_debug)
Joe Perches8af443e2011-08-21 19:56:48 -0300230 pr_cont(" ERR: %d\n",retval);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800231 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234static int bttv_i2c_xfer(struct i2c_adapter *i2c_adap, struct i2c_msg *msgs, int num)
235{
Hans Verkuil74fc7bd2009-03-14 12:36:54 -0300236 struct v4l2_device *v4l2_dev = i2c_get_adapdata(i2c_adap);
237 struct bttv *btv = to_bttv(v4l2_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 int retval = 0;
239 int i;
240
241 if (i2c_debug)
Joe Perches8af443e2011-08-21 19:56:48 -0300242 pr_debug("bt-i2c:");
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 btwrite(BT848_INT_I2CDONE|BT848_INT_RACK, BT848_INT_STAT);
245 for (i = 0 ; i < num; i++) {
246 if (msgs[i].flags & I2C_M_RD) {
247 /* read */
248 retval = bttv_i2c_readbytes(btv, &msgs[i], i+1 == num);
249 if (retval < 0)
250 goto err;
251 } else {
252 /* write */
253 retval = bttv_i2c_sendbytes(btv, &msgs[i], i+1 == num);
254 if (retval < 0)
255 goto err;
256 }
257 }
258 return num;
259
260 err:
261 return retval;
262}
263
Jean Delvaredf9b5d42008-06-15 12:20:18 -0300264static const struct i2c_algorithm bttv_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 .master_xfer = bttv_i2c_xfer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 .functionality = functionality,
267};
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/* ----------------------------------------------------------------------- */
270/* I2C functions - common stuff */
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/* read I2C */
273int bttv_I2CRead(struct bttv *btv, unsigned char addr, char *probe_for)
274{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800275 unsigned char buffer = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 if (0 != btv->i2c_rc)
278 return -1;
279 if (bttv_verbose && NULL != probe_for)
Joe Perches8af443e2011-08-21 19:56:48 -0300280 pr_info("%d: i2c: checking for %s @ 0x%02x... ",
281 btv->c.nr, probe_for, addr);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800282 btv->i2c_client.addr = addr >> 1;
283 if (1 != i2c_master_recv(&btv->i2c_client, &buffer, 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (NULL != probe_for) {
285 if (bttv_verbose)
Joe Perches8af443e2011-08-21 19:56:48 -0300286 pr_cont("not found\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 } else
Joe Perches8af443e2011-08-21 19:56:48 -0300288 pr_warn("%d: i2c read 0x%x: error\n",
289 btv->c.nr, addr);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800290 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292 if (bttv_verbose && NULL != probe_for)
Joe Perches8af443e2011-08-21 19:56:48 -0300293 pr_cont("found\n");
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800294 return buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297/* write I2C */
298int bttv_I2CWrite(struct bttv *btv, unsigned char addr, unsigned char b1,
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800299 unsigned char b2, int both)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800301 unsigned char buffer[2];
302 int bytes = both ? 2 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 if (0 != btv->i2c_rc)
305 return -1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800306 btv->i2c_client.addr = addr >> 1;
307 buffer[0] = b1;
308 buffer[1] = b2;
309 if (bytes != i2c_master_send(&btv->i2c_client, buffer, bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return -1;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314/* read EEPROM content */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800315void bttv_readee(struct bttv *btv, unsigned char *eedata, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Gerd Knorr5daf05f2005-05-25 12:31:26 -0700317 memset(eedata, 0, 256);
318 if (0 != btv->i2c_rc)
319 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 btv->i2c_client.addr = addr >> 1;
321 tveeprom_read(&btv->i2c_client, eedata, 256);
322}
323
324static char *i2c_devs[128] = {
Mauro Carvalho Chehab24a70fd2005-09-09 13:03:39 -0700325 [ 0x1c >> 1 ] = "lgdt330x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 [ 0x30 >> 1 ] = "IR (hauppauge)",
327 [ 0x80 >> 1 ] = "msp34xx",
328 [ 0x86 >> 1 ] = "tda9887",
329 [ 0xa0 >> 1 ] = "eeprom",
330 [ 0xc0 >> 1 ] = "tuner (analog)",
331 [ 0xc2 >> 1 ] = "tuner (analog)",
332};
333
334static void do_i2c_scan(char *name, struct i2c_client *c)
335{
336 unsigned char buf;
337 int i,rc;
338
Mauro Carvalho Chehab53c4e952007-03-29 08:42:30 -0300339 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 c->addr = i;
341 rc = i2c_master_recv(c,&buf,0);
342 if (rc < 0)
343 continue;
Joe Perches8af443e2011-08-21 19:56:48 -0300344 pr_info("%s: i2c scan: found device @ 0x%x [%s]\n",
345 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347}
348
Jean Delvarea824f0f2011-11-09 13:27:53 -0300349/* init + register i2c adapter */
Greg Kroah-Hartman4c62e972012-12-21 13:17:53 -0800350int init_bttv_i2c(struct bttv *btv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Jean Delvaredf9b5d42008-06-15 12:20:18 -0300352 strlcpy(btv->i2c_client.name, "bttv internal", I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 if (i2c_hw)
355 btv->use_i2c_hw = 1;
356 if (btv->use_i2c_hw) {
357 /* bt878 */
Jean Delvaredf9b5d42008-06-15 12:20:18 -0300358 strlcpy(btv->c.i2c_adap.name, "bt878",
359 sizeof(btv->c.i2c_adap.name));
Jean Delvaredf9b5d42008-06-15 12:20:18 -0300360 btv->c.i2c_adap.algo = &bttv_algo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 } else {
362 /* bt848 */
Mauro Carvalho Chehab141276b2006-09-06 19:04:28 -0300363 /* Prevents usage of invalid delay values */
364 if (i2c_udelay<5)
365 i2c_udelay=5;
Mauro Carvalho Chehab141276b2006-09-06 19:04:28 -0300366
Jean Delvaredf9b5d42008-06-15 12:20:18 -0300367 strlcpy(btv->c.i2c_adap.name, "bttv",
368 sizeof(btv->c.i2c_adap.name));
Ezequiel Garciab4b1d042012-10-23 15:57:22 -0300369 btv->i2c_algo = bttv_i2c_algo_bit_template;
Jean Delvaredf9b5d42008-06-15 12:20:18 -0300370 btv->i2c_algo.udelay = i2c_udelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 btv->i2c_algo.data = btv;
372 btv->c.i2c_adap.algo_data = &btv->i2c_algo;
373 }
Jean Delvaredf9b5d42008-06-15 12:20:18 -0300374 btv->c.i2c_adap.owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376 btv->c.i2c_adap.dev.parent = &btv->c.pci->dev;
377 snprintf(btv->c.i2c_adap.name, sizeof(btv->c.i2c_adap.name),
378 "bt%d #%d [%s]", btv->id, btv->c.nr,
379 btv->use_i2c_hw ? "hw" : "sw");
380
Hans Verkuil74fc7bd2009-03-14 12:36:54 -0300381 i2c_set_adapdata(&btv->c.i2c_adap, &btv->c.v4l2_dev);
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800382 btv->i2c_client.adapter = &btv->c.i2c_adap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 if (btv->use_i2c_hw) {
386 btv->i2c_rc = i2c_add_adapter(&btv->c.i2c_adap);
387 } else {
388 bttv_bit_setscl(btv,1);
389 bttv_bit_setsda(btv,1);
390 btv->i2c_rc = i2c_bit_add_bus(&btv->c.i2c_adap);
391 }
392 if (0 == btv->i2c_rc && i2c_scan)
Hans Verkuil74fc7bd2009-03-14 12:36:54 -0300393 do_i2c_scan(btv->c.v4l2_dev.name, &btv->i2c_client);
Jean Delvarec668f322009-05-13 16:48:50 -0300394
Jean Delvare24344662010-02-19 00:18:41 -0300395 return btv->i2c_rc;
396}
Frank Schaefer457ba4c2013-02-17 09:41:29 -0300397
398int fini_bttv_i2c(struct bttv *btv)
399{
Linus Torvalds99bece72013-05-02 14:38:53 -0700400 if (btv->i2c_rc == 0)
401 i2c_del_adapter(&btv->c.i2c_adap);
Frank Schaefer457ba4c2013-02-17 09:41:29 -0300402
Linus Torvalds99bece72013-05-02 14:38:53 -0700403 return 0;
Frank Schaefer457ba4c2013-02-17 09:41:29 -0300404}