2 * device driver for philips saa7134 based TV cards
3 * i2c interface support
5 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/delay.h>
29 #include "saa7134-reg.h"
32 /* ----------------------------------------------------------- */
34 static unsigned int i2c_debug = 0;
35 MODULE_PARM(i2c_debug,"i");
36 MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");
38 static unsigned int i2c_scan = 0;
39 MODULE_PARM(i2c_scan,"i");
40 MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
42 #define d1printk if (1 == i2c_debug) printk
43 #define d2printk if (2 == i2c_debug) printk
45 #define I2C_WAIT_DELAY 32
46 #define I2C_WAIT_RETRY 16
48 /* ----------------------------------------------------------- */
50 static char *str_i2c_status[] = {
51 "IDLE", "DONE_STOP", "BUSY", "TO_SCL", "TO_ARB", "DONE_WRITE",
52 "DONE_READ", "DONE_WRITE_TO", "DONE_READ_TO", "NO_DEVICE",
53 "NO_ACKN", "BUS_ERR", "ARB_LOST", "SEQ_ERR", "ST_ERR", "SW_ERR"
57 IDLE = 0, // no I2C command pending
58 DONE_STOP = 1, // I2C command done and STOP executed
59 BUSY = 2, // executing I2C command
60 TO_SCL = 3, // executing I2C command, time out on clock stretching
61 TO_ARB = 4, // time out on arbitration trial, still trying
62 DONE_WRITE = 5, // I2C command done and awaiting next write command
63 DONE_READ = 6, // I2C command done and awaiting next read command
64 DONE_WRITE_TO = 7, // see 5, and time out on status echo
65 DONE_READ_TO = 8, // see 6, and time out on status echo
66 NO_DEVICE = 9, // no acknowledge on device slave address
67 NO_ACKN = 10, // no acknowledge after data byte transfer
68 BUS_ERR = 11, // bus error
69 ARB_LOST = 12, // arbitration lost during transfer
70 SEQ_ERR = 13, // erroneous programming sequence
71 ST_ERR = 14, // wrong status echoing
72 SW_ERR = 15 // software error
75 static char *str_i2c_attr[] = {
76 "NOP", "STOP", "CONTINUE", "START"
80 NOP = 0, // no operation on I2C bus
81 STOP = 1, // stop condition, no associated byte transfer
82 CONTINUE = 2, // continue with byte transfer
83 START = 3 // start condition with byte transfer
86 static inline enum i2c_status i2c_get_status(struct saa7134_dev *dev)
88 enum i2c_status status;
90 status = saa_readb(SAA7134_I2C_ATTR_STATUS) & 0x0f;
91 d2printk(KERN_DEBUG "%s: i2c stat <= %s\n",dev->name,
92 str_i2c_status[status]);
96 static inline void i2c_set_status(struct saa7134_dev *dev,
97 enum i2c_status status)
99 d2printk(KERN_DEBUG "%s: i2c stat => %s\n",dev->name,
100 str_i2c_status[status]);
101 saa_andorb(SAA7134_I2C_ATTR_STATUS,0x0f,status);
104 static inline void i2c_set_attr(struct saa7134_dev *dev, enum i2c_attr attr)
106 d2printk(KERN_DEBUG "%s: i2c attr => %s\n",dev->name,
108 saa_andorb(SAA7134_I2C_ATTR_STATUS,0xc0,attr << 6);
111 static inline int i2c_is_error(enum i2c_status status)
126 static inline int i2c_is_idle(enum i2c_status status)
137 static inline int i2c_is_busy(enum i2c_status status)
147 static int i2c_is_busy_wait(struct saa7134_dev *dev)
149 enum i2c_status status;
152 for (count = 0; count < I2C_WAIT_RETRY; count++) {
153 status = i2c_get_status(dev);
154 if (!i2c_is_busy(status))
156 saa_wait(I2C_WAIT_DELAY);
158 if (I2C_WAIT_RETRY == count)
163 static int i2c_reset(struct saa7134_dev *dev)
165 enum i2c_status status;
168 d2printk(KERN_DEBUG "%s: i2c reset\n",dev->name);
169 status = i2c_get_status(dev);
170 if (!i2c_is_error(status))
172 i2c_set_status(dev,status);
174 for (count = 0; count < I2C_WAIT_RETRY; count++) {
175 status = i2c_get_status(dev);
176 if (!i2c_is_error(status))
178 udelay(I2C_WAIT_DELAY);
180 if (I2C_WAIT_RETRY == count)
183 if (!i2c_is_idle(status))
186 i2c_set_attr(dev,NOP);
190 static inline int i2c_send_byte(struct saa7134_dev *dev,
194 enum i2c_status status;
198 i2c_set_attr(dev,attr);
199 saa_writeb(SAA7134_I2C_DATA, data);
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);
208 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 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) {
253 addr = msgs[i].addr << 1;
254 if (msgs[i].flags & I2C_M_RD)
256 d1printk(" < %02x", addr);
257 rc = i2c_send_byte(dev,START,addr);
261 if (msgs[i].flags & I2C_M_RD) {
263 for (byte = 0; byte < msgs[i].len; byte++) {
265 rc = i2c_recv_byte(dev);
268 d1printk("%02x", rc);
269 msgs[i].buf[byte] = rc;
273 for (byte = 0; byte < msgs[i].len; byte++) {
274 data = msgs[i].buf[byte];
275 d1printk(" %02x", data);
276 rc = i2c_send_byte(dev,CONTINUE,data);
283 i2c_set_attr(dev,STOP);
285 if (!i2c_is_busy_wait(dev))
287 status = i2c_get_status(dev);
288 if (i2c_is_error(status))
294 if (1 == i2c_debug) {
295 status = i2c_get_status(dev);
296 printk(" ERROR: %s\n",str_i2c_status[status]);
301 /* ----------------------------------------------------------- */
303 static int algo_control(struct i2c_adapter *adapter,
304 unsigned int cmd, unsigned long arg)
309 static u32 functionality(struct i2c_adapter *adap)
311 return I2C_FUNC_SMBUS_EMUL;
315 static void inc_use(struct i2c_adapter *adap)
320 static void dec_use(struct i2c_adapter *adap)
326 static int attach_inform(struct i2c_client *client)
328 struct saa7134_dev *dev = client->adapter->algo_data;
329 int tuner = dev->tuner_type;
331 saa7134_i2c_call_clients(dev,TUNER_SET_TYPE,&tuner);
335 static struct i2c_algorithm saa7134_algo = {
337 .id = I2C_ALGO_SAA7134,
338 .master_xfer = saa7134_i2c_xfer,
339 .algo_control = algo_control,
340 .functionality = functionality,
343 static struct i2c_adapter saa7134_adap_template = {
345 .owner = THIS_MODULE,
350 #ifdef I2C_CLASS_TV_ANALOG
351 .class = I2C_CLASS_TV_ANALOG,
353 I2C_DEVNAME("saa7134"),
354 .id = I2C_ALGO_SAA7134,
355 .algo = &saa7134_algo,
356 .client_register = attach_inform,
359 static struct i2c_client saa7134_client_template = {
360 I2C_DEVNAME("saa7134 internal"),
364 /* ----------------------------------------------------------- */
367 saa7134_i2c_eeprom(struct saa7134_dev *dev, unsigned char *eedata, int len)
372 dev->i2c_client.addr = 0xa0 >> 1;
374 if (1 != (err = i2c_master_send(&dev->i2c_client,&buf,1))) {
375 printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
379 if (len != (err = i2c_master_recv(&dev->i2c_client,eedata,len))) {
380 printk(KERN_WARNING "%s: i2c eeprom read error (err=%d)\n",
384 for (i = 0; i < len; i++) {
386 printk(KERN_INFO "%s: i2c eeprom %02x:",dev->name,i);
387 printk(" %02x",eedata[i]);
395 saa7134_i2c_scan(struct saa7134_dev *dev)
400 for (i = 0; i < 256; i+= 2) {
401 dev->i2c_client.addr = i >> 1;
402 rc = i2c_master_recv(&dev->i2c_client,&buf,0);
405 printk("%s: i2c scan: found device @ %x%s\n",
406 dev->name, i, (i == 0xa0) ? " [eeprom]" : "");
411 void saa7134_i2c_call_clients(struct saa7134_dev *dev,
412 unsigned int cmd, void *arg)
414 BUG_ON(NULL == dev->i2c_adap.algo_data);
415 i2c_clients_command(&dev->i2c_adap, cmd, arg);
418 int saa7134_i2c_register(struct saa7134_dev *dev)
420 dev->i2c_adap = saa7134_adap_template;
421 dev->i2c_adap.dev.parent = &dev->pci->dev;
422 strcpy(dev->i2c_adap.name,dev->name);
423 dev->i2c_adap.algo_data = dev;
424 i2c_add_adapter(&dev->i2c_adap);
426 dev->i2c_client = saa7134_client_template;
427 dev->i2c_client.adapter = &dev->i2c_adap;
429 saa7134_i2c_eeprom(dev,dev->eedata,sizeof(dev->eedata));
431 saa7134_i2c_scan(dev);
435 int saa7134_i2c_unregister(struct saa7134_dev *dev)
437 i2c_del_adapter(&dev->i2c_adap);
441 /* ----------------------------------------------------------- */