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.
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.
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/delay.h>
30 #include "saa7134-reg.h"
32 #include <media/v4l2-common.h>
34 /* ----------------------------------------------------------- */
36 static unsigned int i2c_debug;
37 module_param(i2c_debug, int, 0644);
38 MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");
40 static unsigned int i2c_scan;
41 module_param(i2c_scan, int, 0444);
42 MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
44 #define d1printk if (1 == i2c_debug) printk
45 #define d2printk if (2 == i2c_debug) printk
47 #define I2C_WAIT_DELAY 32
48 #define I2C_WAIT_RETRY 16
50 /* ----------------------------------------------------------- */
52 static char *str_i2c_status[] = {
53 "IDLE", "DONE_STOP", "BUSY", "TO_SCL", "TO_ARB", "DONE_WRITE",
54 "DONE_READ", "DONE_WRITE_TO", "DONE_READ_TO", "NO_DEVICE",
55 "NO_ACKN", "BUS_ERR", "ARB_LOST", "SEQ_ERR", "ST_ERR", "SW_ERR"
59 IDLE = 0, // no I2C command pending
60 DONE_STOP = 1, // I2C command done and STOP executed
61 BUSY = 2, // executing I2C command
62 TO_SCL = 3, // executing I2C command, time out on clock stretching
63 TO_ARB = 4, // time out on arbitration trial, still trying
64 DONE_WRITE = 5, // I2C command done and awaiting next write command
65 DONE_READ = 6, // I2C command done and awaiting next read command
66 DONE_WRITE_TO = 7, // see 5, and time out on status echo
67 DONE_READ_TO = 8, // see 6, and time out on status echo
68 NO_DEVICE = 9, // no acknowledge on device slave address
69 NO_ACKN = 10, // no acknowledge after data byte transfer
70 BUS_ERR = 11, // bus error
71 ARB_LOST = 12, // arbitration lost during transfer
72 SEQ_ERR = 13, // erroneous programming sequence
73 ST_ERR = 14, // wrong status echoing
74 SW_ERR = 15 // software error
77 static char *str_i2c_attr[] = {
78 "NOP", "STOP", "CONTINUE", "START"
82 NOP = 0, // no operation on I2C bus
83 STOP = 1, // stop condition, no associated byte transfer
84 CONTINUE = 2, // continue with byte transfer
85 START = 3 // start condition with byte transfer
88 static inline enum i2c_status i2c_get_status(struct saa7134_dev *dev)
90 enum i2c_status status;
92 status = saa_readb(SAA7134_I2C_ATTR_STATUS) & 0x0f;
93 d2printk(KERN_DEBUG "%s: i2c stat <= %s\n",dev->name,
94 str_i2c_status[status]);
98 static inline void i2c_set_status(struct saa7134_dev *dev,
99 enum i2c_status status)
101 d2printk(KERN_DEBUG "%s: i2c stat => %s\n",dev->name,
102 str_i2c_status[status]);
103 saa_andorb(SAA7134_I2C_ATTR_STATUS,0x0f,status);
106 static inline void i2c_set_attr(struct saa7134_dev *dev, enum i2c_attr attr)
108 d2printk(KERN_DEBUG "%s: i2c attr => %s\n",dev->name,
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 d2printk(KERN_DEBUG "%s: i2c reset\n",dev->name);
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 d2printk(KERN_DEBUG "%s: i2c data => 0x%x\n",dev->name,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 d2printk(KERN_DEBUG "%s: i2c data <= 0x%x\n",dev->name,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 d2printk("start xfer\n");
250 d1printk(KERN_DEBUG "%s: i2c xfer:",dev->name);
251 for (i = 0; i < num; i++) {
252 if (!(msgs[i].flags & I2C_M_NOSTART) || 0 == i) {
254 d2printk("send address\n");
255 addr = msgs[i].addr << 1;
256 if (msgs[i].flags & I2C_M_RD)
258 if (i > 0 && msgs[i].flags & I2C_M_RD && msgs[i].addr != 0x40) {
259 /* workaround for a saa7134 i2c bug
260 * needed to talk to the mt352 demux
261 * thanks to pinnacle for the hint */
263 d1printk(" [%02x quirk]",quirk);
264 i2c_send_byte(dev,START,quirk);
267 d1printk(" < %02x", addr);
268 rc = i2c_send_byte(dev,START,addr);
272 if (msgs[i].flags & I2C_M_RD) {
274 d2printk("read bytes\n");
275 for (byte = 0; byte < msgs[i].len; byte++) {
277 rc = i2c_recv_byte(dev);
280 d1printk("%02x", rc);
281 msgs[i].buf[byte] = rc;
285 d2printk("write bytes\n");
286 for (byte = 0; byte < msgs[i].len; byte++) {
287 data = msgs[i].buf[byte];
288 d1printk(" %02x", data);
289 rc = i2c_send_byte(dev,CONTINUE,data);
295 d2printk("xfer done\n");
297 i2c_set_attr(dev,STOP);
299 if (!i2c_is_busy_wait(dev))
301 status = i2c_get_status(dev);
302 if (i2c_is_error(status))
304 /* ensure that the bus is idle for at least one bit slot */
310 if (1 == i2c_debug) {
311 status = i2c_get_status(dev);
312 printk(" ERROR: %s\n",str_i2c_status[status]);
317 /* ----------------------------------------------------------- */
319 static u32 functionality(struct i2c_adapter *adap)
321 return I2C_FUNC_SMBUS_EMUL;
324 static int attach_inform(struct i2c_client *client)
326 struct saa7134_dev *dev = client->adapter->algo_data;
328 d1printk( "%s i2c attach [addr=0x%x,client=%s]\n",
329 client->driver->driver.name, client->addr, client->name);
331 /* Am I an i2c remote control? */
333 switch (client->addr) {
340 struct IR_i2c *ir = i2c_get_clientdata(client);
341 d1printk("%s i2c IR detected (%s).\n",
342 client->driver->driver.name, ir->phys);
343 saa7134_set_i2c_ir(dev,ir);
351 static struct i2c_algorithm saa7134_algo = {
352 .master_xfer = saa7134_i2c_xfer,
353 .functionality = functionality,
356 static struct i2c_adapter saa7134_adap_template = {
357 .owner = THIS_MODULE,
359 .id = I2C_HW_SAA7134,
360 .algo = &saa7134_algo,
361 .client_register = attach_inform,
364 static struct i2c_client saa7134_client_template = {
365 .name = "saa7134 internal",
368 /* ----------------------------------------------------------- */
371 saa7134_i2c_eeprom(struct saa7134_dev *dev, unsigned char *eedata, int len)
376 dev->i2c_client.addr = 0xa0 >> 1;
378 if (1 != (err = i2c_master_send(&dev->i2c_client,&buf,1))) {
379 printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
383 if (len != (err = i2c_master_recv(&dev->i2c_client,eedata,len))) {
384 printk(KERN_WARNING "%s: i2c eeprom read error (err=%d)\n",
388 for (i = 0; i < len; i++) {
390 printk(KERN_INFO "%s: i2c eeprom %02x:",dev->name,i);
391 printk(" %02x",eedata[i]);
398 static char *i2c_devs[128] = {
399 [ 0x20 ] = "mpeg encoder (saa6752hs)",
400 [ 0xa0 >> 1 ] = "eeprom",
401 [ 0xc0 >> 1 ] = "tuner (analog)",
402 [ 0x86 >> 1 ] = "tda9887",
403 [ 0x5a >> 1 ] = "remote control",
406 static void do_i2c_scan(char *name, struct i2c_client *c)
411 for (i = 0; i < ARRAY_SIZE(i2c_devs); i++) {
413 rc = i2c_master_recv(c,&buf,0);
416 printk("%s: i2c scan: found device @ 0x%x [%s]\n",
417 name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
421 int saa7134_i2c_register(struct saa7134_dev *dev)
423 dev->i2c_adap = saa7134_adap_template;
424 dev->i2c_adap.dev.parent = &dev->pci->dev;
425 strcpy(dev->i2c_adap.name,dev->name);
426 dev->i2c_adap.algo_data = dev;
427 i2c_set_adapdata(&dev->i2c_adap, &dev->v4l2_dev);
428 i2c_add_adapter(&dev->i2c_adap);
430 dev->i2c_client = saa7134_client_template;
431 dev->i2c_client.adapter = &dev->i2c_adap;
433 saa7134_i2c_eeprom(dev,dev->eedata,sizeof(dev->eedata));
435 do_i2c_scan(dev->name,&dev->i2c_client);
439 int saa7134_i2c_unregister(struct saa7134_dev *dev)
441 i2c_del_adapter(&dev->i2c_adap);
445 /* ----------------------------------------------------------- */