3 * device driver for philips saa7134 based TV cards
4 * i2c interface support
6 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
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.
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.
20 #include "saa7134-reg.h"
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/delay.h>
28 #include <media/v4l2-common.h>
30 /* ----------------------------------------------------------- */
32 static unsigned int i2c_debug;
33 module_param(i2c_debug, int, 0644);
34 MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");
36 static unsigned int i2c_scan;
37 module_param(i2c_scan, int, 0444);
38 MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
40 #define i2c_dbg(level, fmt, arg...) do { \
41 if (i2c_debug == level) \
42 printk(KERN_DEBUG pr_fmt("i2c: " fmt), ## arg); \
45 #define i2c_cont(level, fmt, arg...) do { \
46 if (i2c_debug == level) \
47 pr_cont(fmt, ## arg); \
50 #define I2C_WAIT_DELAY 32
51 #define I2C_WAIT_RETRY 16
53 /* ----------------------------------------------------------- */
55 static char *str_i2c_status[] = {
56 "IDLE", "DONE_STOP", "BUSY", "TO_SCL", "TO_ARB", "DONE_WRITE",
57 "DONE_READ", "DONE_WRITE_TO", "DONE_READ_TO", "NO_DEVICE",
58 "NO_ACKN", "BUS_ERR", "ARB_LOST", "SEQ_ERR", "ST_ERR", "SW_ERR"
62 IDLE = 0, // no I2C command pending
63 DONE_STOP = 1, // I2C command done and STOP executed
64 BUSY = 2, // executing I2C command
65 TO_SCL = 3, // executing I2C command, time out on clock stretching
66 TO_ARB = 4, // time out on arbitration trial, still trying
67 DONE_WRITE = 5, // I2C command done and awaiting next write command
68 DONE_READ = 6, // I2C command done and awaiting next read command
69 DONE_WRITE_TO = 7, // see 5, and time out on status echo
70 DONE_READ_TO = 8, // see 6, and time out on status echo
71 NO_DEVICE = 9, // no acknowledge on device slave address
72 NO_ACKN = 10, // no acknowledge after data byte transfer
73 BUS_ERR = 11, // bus error
74 ARB_LOST = 12, // arbitration lost during transfer
75 SEQ_ERR = 13, // erroneous programming sequence
76 ST_ERR = 14, // wrong status echoing
77 SW_ERR = 15 // software error
80 static char *str_i2c_attr[] = {
81 "NOP", "STOP", "CONTINUE", "START"
85 NOP = 0, // no operation on I2C bus
86 STOP = 1, // stop condition, no associated byte transfer
87 CONTINUE = 2, // continue with byte transfer
88 START = 3 // start condition with byte transfer
91 static inline enum i2c_status i2c_get_status(struct saa7134_dev *dev)
93 enum i2c_status status;
95 status = saa_readb(SAA7134_I2C_ATTR_STATUS) & 0x0f;
96 i2c_dbg(2, "i2c stat <= %s\n", str_i2c_status[status]);
100 static inline void i2c_set_status(struct saa7134_dev *dev,
101 enum i2c_status status)
103 i2c_dbg(2, "i2c stat => %s\n", str_i2c_status[status]);
104 saa_andorb(SAA7134_I2C_ATTR_STATUS,0x0f,status);
107 static inline void i2c_set_attr(struct saa7134_dev *dev, enum i2c_attr attr)
109 i2c_dbg(2, "i2c attr => %s\n", str_i2c_attr[attr]);
110 saa_andorb(SAA7134_I2C_ATTR_STATUS,0xc0,attr << 6);
113 static inline int i2c_is_error(enum i2c_status status)
128 static inline int i2c_is_idle(enum i2c_status status)
139 static inline int i2c_is_busy(enum i2c_status status)
151 static int i2c_is_busy_wait(struct saa7134_dev *dev)
153 enum i2c_status status;
156 for (count = 0; count < I2C_WAIT_RETRY; count++) {
157 status = i2c_get_status(dev);
158 if (!i2c_is_busy(status))
160 saa_wait(I2C_WAIT_DELAY);
162 if (I2C_WAIT_RETRY == count)
167 static int i2c_reset(struct saa7134_dev *dev)
169 enum i2c_status status;
172 i2c_dbg(2, "i2c reset\n");
173 status = i2c_get_status(dev);
174 if (!i2c_is_error(status))
176 i2c_set_status(dev,status);
178 for (count = 0; count < I2C_WAIT_RETRY; count++) {
179 status = i2c_get_status(dev);
180 if (!i2c_is_error(status))
182 udelay(I2C_WAIT_DELAY);
184 if (I2C_WAIT_RETRY == count)
187 if (!i2c_is_idle(status))
190 i2c_set_attr(dev,NOP);
194 static inline int i2c_send_byte(struct saa7134_dev *dev,
198 enum i2c_status status;
201 /* have to write both attr + data in one 32bit word */
202 dword = saa_readl(SAA7134_I2C_ATTR_STATUS >> 2);
204 dword |= (attr << 6);
205 dword |= ((__u32)data << 8);
206 dword |= 0x00 << 16; /* 100 kHz */
207 // dword |= 0x40 << 16; /* 400 kHz */
209 saa_writel(SAA7134_I2C_ATTR_STATUS >> 2, dword);
210 i2c_dbg(2, "i2c data => 0x%x\n", data);
212 if (!i2c_is_busy_wait(dev))
214 status = i2c_get_status(dev);
215 if (i2c_is_error(status))
220 static inline int i2c_recv_byte(struct saa7134_dev *dev)
222 enum i2c_status status;
225 i2c_set_attr(dev,CONTINUE);
226 if (!i2c_is_busy_wait(dev))
228 status = i2c_get_status(dev);
229 if (i2c_is_error(status))
231 data = saa_readb(SAA7134_I2C_DATA);
232 i2c_dbg(2, "i2c data <= 0x%x\n", data);
236 static int saa7134_i2c_xfer(struct i2c_adapter *i2c_adap,
237 struct i2c_msg *msgs, int num)
239 struct saa7134_dev *dev = i2c_adap->algo_data;
240 enum i2c_status status;
244 status = i2c_get_status(dev);
245 if (!i2c_is_idle(status))
249 i2c_dbg(2, "start xfer\n");
250 i2c_dbg(1, "i2c xfer:");
251 for (i = 0; i < num; i++) {
252 if (!(msgs[i].flags & I2C_M_NOSTART) || 0 == i) {
254 i2c_dbg(2, "send address\n");
255 addr = msgs[i].addr << 1;
256 if (msgs[i].flags & I2C_M_RD)
258 if (i > 0 && msgs[i].flags &
259 I2C_M_RD && msgs[i].addr != 0x40 &&
260 msgs[i].addr != 0x41 &&
261 msgs[i].addr != 0x19) {
262 /* workaround for a saa7134 i2c bug
263 * needed to talk to the mt352 demux
264 * thanks to pinnacle for the hint */
266 i2c_cont(1, " [%02x quirk]", quirk);
267 i2c_send_byte(dev,START,quirk);
270 i2c_cont(1, " < %02x", addr);
271 rc = i2c_send_byte(dev,START,addr);
275 if (msgs[i].flags & I2C_M_RD) {
277 i2c_dbg(2, "read bytes\n");
278 for (byte = 0; byte < msgs[i].len; byte++) {
280 rc = i2c_recv_byte(dev);
283 i2c_cont(1, "%02x", rc);
284 msgs[i].buf[byte] = rc;
286 /* discard mysterious extra byte when reading
287 from Samsung S5H1411. i2c bus gets error
289 if (0x19 == msgs[i].addr) {
291 rc = i2c_recv_byte(dev);
294 i2c_cont(1, "%02x", rc);
298 i2c_dbg(2, "write bytes\n");
299 for (byte = 0; byte < msgs[i].len; byte++) {
300 data = msgs[i].buf[byte];
301 i2c_cont(1, " %02x", data);
302 rc = i2c_send_byte(dev,CONTINUE,data);
308 i2c_dbg(2, "xfer done\n");
310 i2c_set_attr(dev,STOP);
312 if (!i2c_is_busy_wait(dev))
314 status = i2c_get_status(dev);
315 if (i2c_is_error(status))
317 /* ensure that the bus is idle for at least one bit slot */
323 if (1 == i2c_debug) {
324 status = i2c_get_status(dev);
325 i2c_cont(1, " ERROR: %s\n", str_i2c_status[status]);
330 /* ----------------------------------------------------------- */
332 static u32 functionality(struct i2c_adapter *adap)
334 return I2C_FUNC_SMBUS_EMUL;
337 static const struct i2c_algorithm saa7134_algo = {
338 .master_xfer = saa7134_i2c_xfer,
339 .functionality = functionality,
342 static const struct i2c_adapter saa7134_adap_template = {
343 .owner = THIS_MODULE,
345 .algo = &saa7134_algo,
348 static const struct i2c_client saa7134_client_template = {
349 .name = "saa7134 internal",
352 /* ----------------------------------------------------------- */
355 * On Medion 7134 reading the SAA7134 chip config EEPROM needs DVB-T
356 * demod i2c gate closed due to an address clash between this EEPROM
359 static void saa7134_i2c_eeprom_md7134_gate(struct saa7134_dev *dev)
361 u8 subaddr = 0x7, dmdregval;
364 struct i2c_msg i2cgatemsg_r[] = { {.addr = 0x08, .flags = 0,
365 .buf = &subaddr, .len = 1},
368 .buf = &dmdregval, .len = 1}
370 struct i2c_msg i2cgatemsg_w[] = { {.addr = 0x08, .flags = 0,
371 .buf = data, .len = 2} };
373 ret = i2c_transfer(&dev->i2c_adap, i2cgatemsg_r, 2);
374 if ((ret == 2) && (dmdregval & 0x2)) {
375 pr_debug("%s: DVB-T demod i2c gate was left open\n",
379 data[1] = (dmdregval & ~0x2);
380 if (i2c_transfer(&dev->i2c_adap, i2cgatemsg_w, 1) != 1)
381 pr_err("%s: EEPROM i2c gate close failure\n",
387 saa7134_i2c_eeprom(struct saa7134_dev *dev, unsigned char *eedata, int len)
392 if (dev->board == SAA7134_BOARD_MD7134)
393 saa7134_i2c_eeprom_md7134_gate(dev);
395 dev->i2c_client.addr = 0xa0 >> 1;
397 if (1 != (err = i2c_master_send(&dev->i2c_client,&buf,1))) {
398 pr_info("%s: Huh, no eeprom present (err=%d)?\n",
402 if (len != (err = i2c_master_recv(&dev->i2c_client,eedata,len))) {
403 pr_warn("%s: i2c eeprom read error (err=%d)\n",
408 for (i = 0; i < len; i += 16) {
409 int size = (len - i) > 16 ? 16 : len - i;
411 pr_info("i2c eeprom %02x: %*ph\n", i, size, &eedata[i]);
417 static char *i2c_devs[128] = {
418 [ 0x20 ] = "mpeg encoder (saa6752hs)",
419 [ 0xa0 >> 1 ] = "eeprom",
420 [ 0xc0 >> 1 ] = "tuner (analog)",
421 [ 0x86 >> 1 ] = "tda9887",
422 [ 0x5a >> 1 ] = "remote control",
425 static void do_i2c_scan(struct i2c_client *c)
430 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
432 rc = i2c_master_recv(c,&buf,0);
435 pr_info("i2c scan: found device @ 0x%x [%s]\n",
436 i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
440 int saa7134_i2c_register(struct saa7134_dev *dev)
442 dev->i2c_adap = saa7134_adap_template;
443 dev->i2c_adap.dev.parent = &dev->pci->dev;
444 strcpy(dev->i2c_adap.name,dev->name);
445 dev->i2c_adap.algo_data = dev;
446 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
447 i2c_add_adapter(&dev->i2c_adap);
449 dev->i2c_client = saa7134_client_template;
450 dev->i2c_client.adapter = &dev->i2c_adap;
452 saa7134_i2c_eeprom(dev,dev->eedata,sizeof(dev->eedata));
454 do_i2c_scan(&dev->i2c_client);
456 /* Instantiate the IR receiver device, if present */
457 saa7134_probe_i2c_ir(dev);
461 int saa7134_i2c_unregister(struct saa7134_dev *dev)
463 i2c_del_adapter(&dev->i2c_adap);