vserver 1.9.5.x5
[linux-2.6.git] / drivers / media / video / saa7134 / saa7134-i2c.c
1 /*
2  * $Id: saa7134-i2c.c,v 1.7 2004/11/07 13:17:15 kraxel Exp $
3  *
4  * device driver for philips saa7134 based TV cards
5  * i2c interface support
6  *
7  * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.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"
33
34 /* ----------------------------------------------------------- */
35
36 static unsigned int i2c_debug = 0;
37 module_param(i2c_debug, int, 0644);
38 MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");
39
40 static unsigned int i2c_scan = 0;
41 module_param(i2c_scan, int, 0444);
42 MODULE_PARM_DESC(i2c_scan,"scan i2c bus at insmod time");
43
44 #define d1printk if (1 == i2c_debug) printk
45 #define d2printk if (2 == i2c_debug) printk
46
47 #define I2C_WAIT_DELAY  32
48 #define I2C_WAIT_RETRY  16
49
50 /* ----------------------------------------------------------- */
51
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"
56 };
57
58 enum i2c_status {
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
75 };
76
77 static char *str_i2c_attr[] = {
78         "NOP", "STOP", "CONTINUE", "START"
79 };
80
81 enum i2c_attr {
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
86 };
87
88 static inline enum i2c_status i2c_get_status(struct saa7134_dev *dev)
89 {
90         enum i2c_status status;
91
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]);
95         return status;
96 }
97
98 static inline void i2c_set_status(struct saa7134_dev *dev,
99                                   enum i2c_status status)
100 {
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);
104 }
105
106 static inline void i2c_set_attr(struct saa7134_dev *dev, enum i2c_attr attr)
107 {
108         d2printk(KERN_DEBUG "%s: i2c attr => %s\n",dev->name,
109                  str_i2c_attr[attr]);
110         saa_andorb(SAA7134_I2C_ATTR_STATUS,0xc0,attr << 6);
111 }
112
113 static inline int i2c_is_error(enum i2c_status status)
114 {
115         switch (status) {
116         case NO_DEVICE:
117         case NO_ACKN:
118         case BUS_ERR:
119         case ARB_LOST:
120         case SEQ_ERR:
121         case ST_ERR:
122                 return TRUE;
123         default:
124                 return FALSE;
125         }
126 }
127
128 static inline int i2c_is_idle(enum i2c_status status)
129 {
130         switch (status) {
131         case IDLE:
132         case DONE_STOP:
133                 return TRUE;
134         default:
135                 return FALSE;
136         }
137 }
138
139 static inline int i2c_is_busy(enum i2c_status status)
140 {
141         switch (status) {
142         case BUSY:
143                 return TRUE;
144         default:
145                 return FALSE;
146         }
147 }
148
149 static int i2c_is_busy_wait(struct saa7134_dev *dev)
150 {
151         enum i2c_status status;
152         int count;
153
154         for (count = 0; count < I2C_WAIT_RETRY; count++) {
155                 status = i2c_get_status(dev);
156                 if (!i2c_is_busy(status))
157                         break;
158                 saa_wait(I2C_WAIT_DELAY);
159         }
160         if (I2C_WAIT_RETRY == count)
161                 return FALSE;
162         return TRUE;
163 }
164
165 static int i2c_reset(struct saa7134_dev *dev)
166 {
167         enum i2c_status status;
168         int count;
169
170         d2printk(KERN_DEBUG "%s: i2c reset\n",dev->name);
171         status = i2c_get_status(dev);
172         if (!i2c_is_error(status))
173                 return TRUE;
174         i2c_set_status(dev,status);
175
176         for (count = 0; count < I2C_WAIT_RETRY; count++) {
177                 status = i2c_get_status(dev);
178                 if (!i2c_is_error(status))
179                         break;
180                 udelay(I2C_WAIT_DELAY);
181         }
182         if (I2C_WAIT_RETRY == count)
183                 return FALSE;
184
185         if (!i2c_is_idle(status))
186                 return FALSE;
187
188         i2c_set_attr(dev,NOP);
189         return TRUE;
190 }
191
192 static inline int i2c_send_byte(struct saa7134_dev *dev,
193                                 enum i2c_attr attr,
194                                 unsigned char data)
195 {
196         enum i2c_status status;
197         __u32 dword;
198
199 #if 0
200         i2c_set_attr(dev,attr);
201         saa_writeb(SAA7134_I2C_DATA, data);
202 #else
203         /* have to write both attr + data in one 32bit word */
204         dword  = saa_readl(SAA7134_I2C_ATTR_STATUS >> 2);
205         dword &= 0x0f;
206         dword |= (attr << 6);
207         dword |= ((__u32)data << 8);
208         dword |= 0x00 << 16;
209         dword |= 0xf0 << 24;
210         saa_writel(SAA7134_I2C_ATTR_STATUS >> 2, dword);
211 #endif
212         d2printk(KERN_DEBUG "%s: i2c data => 0x%x\n",dev->name,data);
213
214         if (!i2c_is_busy_wait(dev))
215                 return -EIO;
216         status = i2c_get_status(dev);
217         if (i2c_is_error(status))
218                 return -EIO;
219         return 0;
220 }
221
222 static inline int i2c_recv_byte(struct saa7134_dev *dev)
223 {
224         enum i2c_status status;
225         unsigned char data;
226
227         i2c_set_attr(dev,CONTINUE);
228         if (!i2c_is_busy_wait(dev))
229                 return -EIO;
230         status = i2c_get_status(dev);
231         if (i2c_is_error(status))
232                 return -EIO;
233         data = saa_readb(SAA7134_I2C_DATA);
234         d2printk(KERN_DEBUG "%s: i2c data <= 0x%x\n",dev->name,data);
235         return data;
236 }
237
238 static int saa7134_i2c_xfer(struct i2c_adapter *i2c_adap,
239                             struct i2c_msg msgs[], int num)
240 {
241         struct saa7134_dev *dev = i2c_adap->algo_data;
242         enum i2c_status status;
243         unsigned char data;
244         int addr,rc,i,byte;
245
246         status = i2c_get_status(dev);
247         if (!i2c_is_idle(status))
248                 if (!i2c_reset(dev))
249                         return -EIO;
250
251         d1printk(KERN_DEBUG "%s: i2c xfer:",dev->name);
252         for (i = 0; i < num; i++) {
253                 if (!(msgs[i].flags & I2C_M_NOSTART) || 0 == i) {
254                         /* send address */
255                         addr  = msgs[i].addr << 1;
256                         if (msgs[i].flags & I2C_M_RD)
257                                 addr |= 1;
258                         d1printk(" < %02x", addr);
259                         rc = i2c_send_byte(dev,START,addr);
260                         if (rc < 0)
261                                  goto err;
262                 }
263                 if (msgs[i].flags & I2C_M_RD) {
264                         /* read bytes */
265                         for (byte = 0; byte < msgs[i].len; byte++) {
266                                 d1printk(" =");
267                                 rc = i2c_recv_byte(dev);
268                                 if (rc < 0)
269                                         goto err;
270                                 d1printk("%02x", rc);
271                                 msgs[i].buf[byte] = rc;
272                         }
273                 } else {
274                         /* write bytes */
275                         for (byte = 0; byte < msgs[i].len; byte++) {
276                                 data = msgs[i].buf[byte];
277                                 d1printk(" %02x", data);
278                                 rc = i2c_send_byte(dev,CONTINUE,data);
279                                 if (rc < 0)
280                                         goto err;
281                         }
282                 }
283         }
284         d1printk(" >");
285         i2c_set_attr(dev,STOP);
286         rc = -EIO;
287         if (!i2c_is_busy_wait(dev))
288                 goto err;
289         status = i2c_get_status(dev);
290         if (i2c_is_error(status))
291                 goto err;
292
293         d1printk("\n");
294         return num;
295  err:
296         if (1 == i2c_debug) {
297                 status = i2c_get_status(dev);
298                 printk(" ERROR: %s\n",str_i2c_status[status]);
299         }
300         return rc;
301 }
302
303 /* ----------------------------------------------------------- */
304
305 static int algo_control(struct i2c_adapter *adapter,
306                         unsigned int cmd, unsigned long arg)
307 {
308         return 0;
309 }
310
311 static u32 functionality(struct i2c_adapter *adap)
312 {
313         return I2C_FUNC_SMBUS_EMUL;
314 }
315
316 #ifndef I2C_PEC
317 static void inc_use(struct i2c_adapter *adap)
318 {
319         MOD_INC_USE_COUNT;
320 }
321
322 static void dec_use(struct i2c_adapter *adap)
323 {
324         MOD_DEC_USE_COUNT;
325 }
326 #endif
327
328 static int attach_inform(struct i2c_client *client)
329 {
330         struct saa7134_dev *dev = client->adapter->algo_data;
331         int tuner = dev->tuner_type;
332         int conf  = dev->tda9887_conf;
333
334         saa7134_i2c_call_clients(dev,TUNER_SET_TYPE,&tuner);
335         saa7134_i2c_call_clients(dev,TDA9887_SET_CONFIG,&conf);
336         return 0;
337 }
338
339 static struct i2c_algorithm saa7134_algo = {
340         .name          = "saa7134",
341         .id            = I2C_ALGO_SAA7134,
342         .master_xfer   = saa7134_i2c_xfer,
343         .algo_control  = algo_control,
344         .functionality = functionality,
345 };
346
347 static struct i2c_adapter saa7134_adap_template = {
348 #ifdef I2C_PEC
349         .owner         = THIS_MODULE,
350 #else
351         .inc_use       = inc_use,
352         .dec_use       = dec_use,
353 #endif
354 #ifdef I2C_CLASS_TV_ANALOG
355         .class         = I2C_CLASS_TV_ANALOG,
356 #endif
357         I2C_DEVNAME("saa7134"),
358         .id            = I2C_ALGO_SAA7134,
359         .algo          = &saa7134_algo,
360         .client_register = attach_inform,
361 };
362
363 static struct i2c_client saa7134_client_template = {
364         I2C_DEVNAME("saa7134 internal"),
365         .id        = -1,
366 };
367
368 /* ----------------------------------------------------------- */
369
370 static int
371 saa7134_i2c_eeprom(struct saa7134_dev *dev, unsigned char *eedata, int len)
372 {
373         unsigned char buf;
374         int i,err;
375
376         dev->i2c_client.addr = 0xa0 >> 1;
377         buf = 0;
378         if (1 != (err = i2c_master_send(&dev->i2c_client,&buf,1))) {
379                 printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
380                        dev->name,err);
381                 return -1;
382         }
383         if (len != (err = i2c_master_recv(&dev->i2c_client,eedata,len))) {
384                 printk(KERN_WARNING "%s: i2c eeprom read error (err=%d)\n",
385                        dev->name,err);
386                 return -1;
387         }
388         for (i = 0; i < len; i++) {
389                 if (0 == (i % 16))
390                         printk(KERN_INFO "%s: i2c eeprom %02x:",dev->name,i);
391                 printk(" %02x",eedata[i]);
392                 if (15 == (i % 16))
393                         printk("\n");
394         }
395         return 0;
396 }
397
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 };
404
405 static void do_i2c_scan(char *name, struct i2c_client *c)
406 {
407         unsigned char buf;
408         int i,rc;
409
410         for (i = 0; i < 128; i++) {
411                 c->addr = i;
412                 rc = i2c_master_recv(c,&buf,0);
413                 if (rc < 0)
414                         continue;
415                 printk("%s: i2c scan: found device @ 0x%x  [%s]\n",
416                        name, i << 1, i2c_devs[i] ? i2c_devs[i] : "???");
417         }
418 }
419
420 void saa7134_i2c_call_clients(struct saa7134_dev *dev,
421                               unsigned int cmd, void *arg)
422 {
423         BUG_ON(NULL == dev->i2c_adap.algo_data);
424         i2c_clients_command(&dev->i2c_adap, cmd, arg);
425 }
426
427 int saa7134_i2c_register(struct saa7134_dev *dev)
428 {
429         dev->i2c_adap = saa7134_adap_template;
430         dev->i2c_adap.dev.parent = &dev->pci->dev;
431         strcpy(dev->i2c_adap.name,dev->name);
432         dev->i2c_adap.algo_data = dev;
433         i2c_add_adapter(&dev->i2c_adap);
434
435         dev->i2c_client = saa7134_client_template;
436         dev->i2c_client.adapter = &dev->i2c_adap;
437
438         saa7134_i2c_eeprom(dev,dev->eedata,sizeof(dev->eedata));
439         if (i2c_scan)
440                 do_i2c_scan(dev->name,&dev->i2c_client);
441         return 0;
442 }
443
444 int saa7134_i2c_unregister(struct saa7134_dev *dev)
445 {
446         i2c_del_adapter(&dev->i2c_adap);
447         return 0;
448 }
449
450 /* ----------------------------------------------------------- */
451 /*
452  * Local variables:
453  * c-basic-offset: 8
454  * End:
455  */