blob: cce8da6a4f94dceb456b8893bb35a0ebbd2a905e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * device driver for philips saa7134 based TV cards
4 * i2c interface support
5 *
6 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/init.h>
24#include <linux/list.h>
25#include <linux/module.h>
26#include <linux/moduleparam.h>
27#include <linux/kernel.h>
28#include <linux/slab.h>
29#include <linux/delay.h>
30
31#include "saa7134-reg.h"
32#include "saa7134.h"
Michael Krufky5e453dc2006-01-09 15:32:31 -020033#include <media/v4l2-common.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/* ----------------------------------------------------------- */
36
37static unsigned int i2c_debug = 0;
38module_param(i2c_debug, int, 0644);
39MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");
40
41static unsigned int i2c_scan = 0;
42module_param(i2c_scan, int, 0444);
43MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
44
45#define d1printk if (1 == i2c_debug) printk
46#define d2printk if (2 == i2c_debug) printk
47
48#define I2C_WAIT_DELAY 32
49#define I2C_WAIT_RETRY 16
50
51/* ----------------------------------------------------------- */
52
53static char *str_i2c_status[] = {
54 "IDLE", "DONE_STOP", "BUSY", "TO_SCL", "TO_ARB", "DONE_WRITE",
55 "DONE_READ", "DONE_WRITE_TO", "DONE_READ_TO", "NO_DEVICE",
56 "NO_ACKN", "BUS_ERR", "ARB_LOST", "SEQ_ERR", "ST_ERR", "SW_ERR"
57};
58
59enum i2c_status {
60 IDLE = 0, // no I2C command pending
61 DONE_STOP = 1, // I2C command done and STOP executed
62 BUSY = 2, // executing I2C command
63 TO_SCL = 3, // executing I2C command, time out on clock stretching
64 TO_ARB = 4, // time out on arbitration trial, still trying
65 DONE_WRITE = 5, // I2C command done and awaiting next write command
66 DONE_READ = 6, // I2C command done and awaiting next read command
67 DONE_WRITE_TO = 7, // see 5, and time out on status echo
68 DONE_READ_TO = 8, // see 6, and time out on status echo
69 NO_DEVICE = 9, // no acknowledge on device slave address
70 NO_ACKN = 10, // no acknowledge after data byte transfer
71 BUS_ERR = 11, // bus error
72 ARB_LOST = 12, // arbitration lost during transfer
73 SEQ_ERR = 13, // erroneous programming sequence
74 ST_ERR = 14, // wrong status echoing
75 SW_ERR = 15 // software error
76};
77
78static char *str_i2c_attr[] = {
79 "NOP", "STOP", "CONTINUE", "START"
80};
81
82enum i2c_attr {
83 NOP = 0, // no operation on I2C bus
84 STOP = 1, // stop condition, no associated byte transfer
85 CONTINUE = 2, // continue with byte transfer
86 START = 3 // start condition with byte transfer
87};
88
89static inline enum i2c_status i2c_get_status(struct saa7134_dev *dev)
90{
91 enum i2c_status status;
92
93 status = saa_readb(SAA7134_I2C_ATTR_STATUS) & 0x0f;
94 d2printk(KERN_DEBUG "%s: i2c stat <= %s\n",dev->name,
95 str_i2c_status[status]);
96 return status;
97}
98
99static inline void i2c_set_status(struct saa7134_dev *dev,
100 enum i2c_status status)
101{
102 d2printk(KERN_DEBUG "%s: i2c stat => %s\n",dev->name,
103 str_i2c_status[status]);
104 saa_andorb(SAA7134_I2C_ATTR_STATUS,0x0f,status);
105}
106
107static inline void i2c_set_attr(struct saa7134_dev *dev, enum i2c_attr attr)
108{
109 d2printk(KERN_DEBUG "%s: i2c attr => %s\n",dev->name,
110 str_i2c_attr[attr]);
111 saa_andorb(SAA7134_I2C_ATTR_STATUS,0xc0,attr << 6);
112}
113
114static inline int i2c_is_error(enum i2c_status status)
115{
116 switch (status) {
117 case NO_DEVICE:
118 case NO_ACKN:
119 case BUS_ERR:
120 case ARB_LOST:
121 case SEQ_ERR:
122 case ST_ERR:
Richard Knutssone8be02a2007-02-06 21:52:04 -0300123 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 default:
Richard Knutssone8be02a2007-02-06 21:52:04 -0300125 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127}
128
129static inline int i2c_is_idle(enum i2c_status status)
130{
131 switch (status) {
132 case IDLE:
133 case DONE_STOP:
Richard Knutssone8be02a2007-02-06 21:52:04 -0300134 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 default:
Richard Knutssone8be02a2007-02-06 21:52:04 -0300136 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138}
139
140static inline int i2c_is_busy(enum i2c_status status)
141{
142 switch (status) {
143 case BUSY:
Richard Knutssone8be02a2007-02-06 21:52:04 -0300144 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 default:
Richard Knutssone8be02a2007-02-06 21:52:04 -0300146 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148}
149
150static int i2c_is_busy_wait(struct saa7134_dev *dev)
151{
152 enum i2c_status status;
153 int count;
154
155 for (count = 0; count < I2C_WAIT_RETRY; count++) {
156 status = i2c_get_status(dev);
157 if (!i2c_is_busy(status))
158 break;
159 saa_wait(I2C_WAIT_DELAY);
160 }
161 if (I2C_WAIT_RETRY == count)
Richard Knutssone8be02a2007-02-06 21:52:04 -0300162 return false;
163 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
166static int i2c_reset(struct saa7134_dev *dev)
167{
168 enum i2c_status status;
169 int count;
170
171 d2printk(KERN_DEBUG "%s: i2c reset\n",dev->name);
172 status = i2c_get_status(dev);
173 if (!i2c_is_error(status))
Richard Knutssone8be02a2007-02-06 21:52:04 -0300174 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 i2c_set_status(dev,status);
176
177 for (count = 0; count < I2C_WAIT_RETRY; count++) {
178 status = i2c_get_status(dev);
179 if (!i2c_is_error(status))
180 break;
181 udelay(I2C_WAIT_DELAY);
182 }
183 if (I2C_WAIT_RETRY == count)
Richard Knutssone8be02a2007-02-06 21:52:04 -0300184 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 if (!i2c_is_idle(status))
Richard Knutssone8be02a2007-02-06 21:52:04 -0300187 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 i2c_set_attr(dev,NOP);
Richard Knutssone8be02a2007-02-06 21:52:04 -0300190 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193static inline int i2c_send_byte(struct saa7134_dev *dev,
194 enum i2c_attr attr,
195 unsigned char data)
196{
197 enum i2c_status status;
198 __u32 dword;
199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 /* have to write both attr + data in one 32bit word */
201 dword = saa_readl(SAA7134_I2C_ATTR_STATUS >> 2);
202 dword &= 0x0f;
203 dword |= (attr << 6);
204 dword |= ((__u32)data << 8);
205 dword |= 0x00 << 16; /* 100 kHz */
206// dword |= 0x40 << 16; /* 400 kHz */
207 dword |= 0xf0 << 24;
208 saa_writel(SAA7134_I2C_ATTR_STATUS >> 2, dword);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 d2printk(KERN_DEBUG "%s: i2c data => 0x%x\n",dev->name,data);
210
211 if (!i2c_is_busy_wait(dev))
212 return -EIO;
213 status = i2c_get_status(dev);
214 if (i2c_is_error(status))
215 return -EIO;
216 return 0;
217}
218
219static inline int i2c_recv_byte(struct saa7134_dev *dev)
220{
221 enum i2c_status status;
222 unsigned char data;
223
224 i2c_set_attr(dev,CONTINUE);
225 if (!i2c_is_busy_wait(dev))
226 return -EIO;
227 status = i2c_get_status(dev);
228 if (i2c_is_error(status))
229 return -EIO;
230 data = saa_readb(SAA7134_I2C_DATA);
231 d2printk(KERN_DEBUG "%s: i2c data <= 0x%x\n",dev->name,data);
232 return data;
233}
234
235static int saa7134_i2c_xfer(struct i2c_adapter *i2c_adap,
236 struct i2c_msg *msgs, int num)
237{
238 struct saa7134_dev *dev = i2c_adap->algo_data;
239 enum i2c_status status;
240 unsigned char data;
241 int addr,rc,i,byte;
242
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800243 status = i2c_get_status(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 if (!i2c_is_idle(status))
245 if (!i2c_reset(dev))
246 return -EIO;
247
248 d2printk("start xfer\n");
249 d1printk(KERN_DEBUG "%s: i2c xfer:",dev->name);
250 for (i = 0; i < num; i++) {
251 if (!(msgs[i].flags & I2C_M_NOSTART) || 0 == i) {
252 /* send address */
253 d2printk("send address\n");
254 addr = msgs[i].addr << 1;
255 if (msgs[i].flags & I2C_M_RD)
256 addr |= 1;
257 if (i > 0 && msgs[i].flags & I2C_M_RD) {
258 /* workaround for a saa7134 i2c bug
259 * needed to talk to the mt352 demux
260 * thanks to pinnacle for the hint */
261 int quirk = 0xfd;
262 d1printk(" [%02x quirk]",quirk);
263 i2c_send_byte(dev,START,quirk);
264 i2c_recv_byte(dev);
265 }
266 d1printk(" < %02x", addr);
267 rc = i2c_send_byte(dev,START,addr);
268 if (rc < 0)
269 goto err;
270 }
271 if (msgs[i].flags & I2C_M_RD) {
272 /* read bytes */
273 d2printk("read bytes\n");
274 for (byte = 0; byte < msgs[i].len; byte++) {
275 d1printk(" =");
276 rc = i2c_recv_byte(dev);
277 if (rc < 0)
278 goto err;
279 d1printk("%02x", rc);
280 msgs[i].buf[byte] = rc;
281 }
282 } else {
283 /* write bytes */
284 d2printk("write bytes\n");
285 for (byte = 0; byte < msgs[i].len; byte++) {
286 data = msgs[i].buf[byte];
287 d1printk(" %02x", data);
288 rc = i2c_send_byte(dev,CONTINUE,data);
289 if (rc < 0)
290 goto err;
291 }
292 }
293 }
294 d2printk("xfer done\n");
295 d1printk(" >");
296 i2c_set_attr(dev,STOP);
297 rc = -EIO;
298 if (!i2c_is_busy_wait(dev))
299 goto err;
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800300 status = i2c_get_status(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (i2c_is_error(status))
302 goto err;
Mauro Carvalho Chehabfd3113e2005-07-31 22:34:43 -0700303 /* ensure that the bus is idle for at least one bit slot */
304 msleep(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 d1printk("\n");
307 return num;
308 err:
309 if (1 == i2c_debug) {
310 status = i2c_get_status(dev);
311 printk(" ERROR: %s\n",str_i2c_status[status]);
312 }
313 return rc;
314}
315
316/* ----------------------------------------------------------- */
317
318static int algo_control(struct i2c_adapter *adapter,
319 unsigned int cmd, unsigned long arg)
320{
321 return 0;
322}
323
324static u32 functionality(struct i2c_adapter *adap)
325{
326 return I2C_FUNC_SMBUS_EMUL;
327}
328
329static int attach_inform(struct i2c_client *client)
330{
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700331 struct saa7134_dev *dev = client->adapter->algo_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 int tuner = dev->tuner_type;
333 int conf = dev->tda9887_conf;
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700334 struct tuner_setup tun_setup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700336 d1printk( "%s i2c attach [addr=0x%x,client=%s]\n",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100337 client->driver->driver.name, client->addr, client->name);
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700338
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800339 /* Am I an i2c remote control? */
340
341 switch (client->addr) {
342 case 0x7a:
343 case 0x47:
Thomas Genty177aaaf2006-11-29 21:57:24 -0300344 case 0x71:
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800345 {
346 struct IR_i2c *ir = i2c_get_clientdata(client);
347 d1printk("%s i2c IR detected (%s).\n",
Laurent Riffard604f28e2005-11-26 20:43:39 +0100348 client->driver->driver.name, ir->phys);
Ricardo Cerqueiraac9cd972005-11-08 21:37:56 -0800349 saa7134_set_i2c_ir(dev,ir);
350 break;
351 }
352 }
353
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700354 if (!client->driver->command)
355 return 0;
356
357 if (saa7134_boards[dev->board].radio_type != UNSET) {
358
359 tun_setup.type = saa7134_boards[dev->board].radio_type;
360 tun_setup.addr = saa7134_boards[dev->board].radio_addr;
361
362 if ((tun_setup.addr == ADDR_UNSET) || (tun_setup.addr == client->addr)) {
363 tun_setup.mode_mask = T_RADIO;
364
365 client->driver->command(client, TUNER_SET_TYPE_ADDR, &tun_setup);
366 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800367 }
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700368
369 if (tuner != UNSET) {
370
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800371 tun_setup.type = tuner;
372 tun_setup.addr = saa7134_boards[dev->board].tuner_addr;
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700373
374 if ((tun_setup.addr == ADDR_UNSET)||(tun_setup.addr == client->addr)) {
375
376 tun_setup.mode_mask = T_ANALOG_TV;
377
378 client->driver->command(client,TUNER_SET_TYPE_ADDR, &tun_setup);
379 }
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800380 }
Mauro Carvalho Chehab330a1152005-07-12 13:59:01 -0700381
382 client->driver->command(client, TDA9887_SET_CONFIG, &conf);
383
Mauro Carvalho Chehab4ac97912005-11-08 21:37:43 -0800384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387static struct i2c_algorithm saa7134_algo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 .master_xfer = saa7134_i2c_xfer,
389 .algo_control = algo_control,
390 .functionality = functionality,
391};
392
393static struct i2c_adapter saa7134_adap_template = {
394 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 .class = I2C_CLASS_TV_ANALOG,
Jean Delvarefae91e72005-08-15 19:57:04 +0200396 .name = "saa7134",
Jean Delvare1684a9842005-08-11 23:51:10 +0200397 .id = I2C_HW_SAA7134,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 .algo = &saa7134_algo,
399 .client_register = attach_inform,
400};
401
402static struct i2c_client saa7134_client_template = {
Jean Delvarefae91e72005-08-15 19:57:04 +0200403 .name = "saa7134 internal",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404};
405
406/* ----------------------------------------------------------- */
407
408static int
409saa7134_i2c_eeprom(struct saa7134_dev *dev, unsigned char *eedata, int len)
410{
411 unsigned char buf;
412 int i,err;
413
414 dev->i2c_client.addr = 0xa0 >> 1;
415 buf = 0;
416 if (1 != (err = i2c_master_send(&dev->i2c_client,&buf,1))) {
417 printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
418 dev->name,err);
419 return -1;
420 }
421 if (len != (err = i2c_master_recv(&dev->i2c_client,eedata,len))) {
422 printk(KERN_WARNING "%s: i2c eeprom read error (err=%d)\n",
423 dev->name,err);
424 return -1;
425 }
426 for (i = 0; i < len; i++) {
427 if (0 == (i % 16))
428 printk(KERN_INFO "%s: i2c eeprom %02x:",dev->name,i);
429 printk(" %02x",eedata[i]);
430 if (15 == (i % 16))
431 printk("\n");
432 }
433 return 0;
434}
435
436static char *i2c_devs[128] = {
437 [ 0x20 ] = "mpeg encoder (saa6752hs)",
438 [ 0xa0 >> 1 ] = "eeprom",
439 [ 0xc0 >> 1 ] = "tuner (analog)",
440 [ 0x86 >> 1 ] = "tda9887",
441};
442
443static void do_i2c_scan(char *name, struct i2c_client *c)
444{
445 unsigned char buf;
446 int i,rc;
447
448 for (i = 0; i < 128; i++) {
449 c->addr = i;
450 rc = i2c_master_recv(c,&buf,0);
451 if (rc < 0)
452 continue;
453 printk("%s: i2c scan: found device @ 0x%x [%s]\n",
454 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
455 }
456}
457
458void saa7134_i2c_call_clients(struct saa7134_dev *dev,
459 unsigned int cmd, void *arg)
460{
461 BUG_ON(NULL == dev->i2c_adap.algo_data);
462 i2c_clients_command(&dev->i2c_adap, cmd, arg);
463}
464
465int saa7134_i2c_register(struct saa7134_dev *dev)
466{
467 dev->i2c_adap = saa7134_adap_template;
468 dev->i2c_adap.dev.parent = &dev->pci->dev;
469 strcpy(dev->i2c_adap.name,dev->name);
470 dev->i2c_adap.algo_data = dev;
471 i2c_add_adapter(&dev->i2c_adap);
472
473 dev->i2c_client = saa7134_client_template;
474 dev->i2c_client.adapter = &dev->i2c_adap;
475
476 saa7134_i2c_eeprom(dev,dev->eedata,sizeof(dev->eedata));
477 if (i2c_scan)
478 do_i2c_scan(dev->name,&dev->i2c_client);
479 return 0;
480}
481
482int saa7134_i2c_unregister(struct saa7134_dev *dev)
483{
484 i2c_del_adapter(&dev->i2c_adap);
485 return 0;
486}
487
488/* ----------------------------------------------------------- */
489/*
490 * Local variables:
491 * c-basic-offset: 8
492 * End:
493 */