patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / media / video / saa7134 / saa7134-i2c.c
1 /*
2  * device driver for philips saa7134 based TV cards
3  * i2c interface support
4  *
5  * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
6  *
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.
11  *
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.
16  *
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.
20  */
21
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>
28
29 #include "saa7134-reg.h"
30 #include "saa7134.h"
31
32 /* ----------------------------------------------------------- */
33
34 static unsigned int i2c_debug = 0;
35 MODULE_PARM(i2c_debug,"i");
36 MODULE_PARM_DESC(i2c_debug,"enable debug messages [i2c]");
37
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");
41
42 #define d1printk if (1 == i2c_debug) printk
43 #define d2printk if (2 == i2c_debug) printk
44
45 #define I2C_WAIT_DELAY  32
46 #define I2C_WAIT_RETRY  16
47
48 /* ----------------------------------------------------------- */
49
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"
54 };
55
56 enum i2c_status {
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
73 };
74
75 static char *str_i2c_attr[] = {
76         "NOP", "STOP", "CONTINUE", "START"
77 };
78
79 enum i2c_attr {
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
84 };
85
86 static inline enum i2c_status i2c_get_status(struct saa7134_dev *dev)
87 {
88         enum i2c_status status;
89         
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]);
93         return status;
94 }
95
96 static inline void i2c_set_status(struct saa7134_dev *dev,
97                                   enum i2c_status status)
98 {
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);
102 }
103
104 static inline void i2c_set_attr(struct saa7134_dev *dev, enum i2c_attr attr)
105 {
106         d2printk(KERN_DEBUG "%s: i2c attr => %s\n",dev->name,
107                  str_i2c_attr[attr]);
108         saa_andorb(SAA7134_I2C_ATTR_STATUS,0xc0,attr << 6);
109 }
110
111 static inline int i2c_is_error(enum i2c_status status)
112 {
113         switch (status) {
114         case NO_DEVICE:
115         case NO_ACKN:
116         case BUS_ERR:
117         case ARB_LOST:
118         case SEQ_ERR:
119         case ST_ERR:
120                 return TRUE;
121         default:
122                 return FALSE;
123         }
124 }
125
126 static inline int i2c_is_idle(enum i2c_status status)
127 {
128         switch (status) {
129         case IDLE:
130         case DONE_STOP:
131                 return TRUE;
132         default:
133                 return FALSE;
134         }
135 }
136
137 static inline int i2c_is_busy(enum i2c_status status)
138 {
139         switch (status) {
140         case BUSY:
141                 return TRUE;
142         default:
143                 return FALSE;
144         }
145 }
146
147 static int i2c_is_busy_wait(struct saa7134_dev *dev)
148 {
149         enum i2c_status status;
150         int count;
151
152         for (count = 0; count < I2C_WAIT_RETRY; count++) {
153                 status = i2c_get_status(dev);
154                 if (!i2c_is_busy(status))
155                         break;
156                 saa_wait(I2C_WAIT_DELAY);
157         }
158         if (I2C_WAIT_RETRY == count)
159                 return FALSE;
160         return TRUE;
161 }
162
163 static int i2c_reset(struct saa7134_dev *dev)
164 {
165         enum i2c_status status;
166         int count;
167
168         d2printk(KERN_DEBUG "%s: i2c reset\n",dev->name);
169         status = i2c_get_status(dev);
170         if (!i2c_is_error(status))
171                 return TRUE;
172         i2c_set_status(dev,status);
173
174         for (count = 0; count < I2C_WAIT_RETRY; count++) {
175                 status = i2c_get_status(dev);
176                 if (!i2c_is_error(status))
177                         break;
178                 udelay(I2C_WAIT_DELAY);
179         }
180         if (I2C_WAIT_RETRY == count)
181                 return FALSE;
182
183         if (!i2c_is_idle(status))
184                 return FALSE;
185         
186         i2c_set_attr(dev,NOP);
187         return TRUE;
188 }
189
190 static inline int i2c_send_byte(struct saa7134_dev *dev,
191                                 enum i2c_attr attr,
192                                 unsigned char data)
193 {
194         enum i2c_status status;
195         __u32 dword;
196
197 #if 0
198         i2c_set_attr(dev,attr);
199         saa_writeb(SAA7134_I2C_DATA, data);
200 #else
201         /* have to write both attr + data in one 32bit word */
202         dword  = saa_readl(SAA7134_I2C_ATTR_STATUS >> 2);
203         dword &= 0x0f;
204         dword |= (attr << 6);
205         dword |= ((__u32)data << 8);
206         dword |= 0x00 << 16;
207         dword |= 0xf0 << 24;
208         saa_writel(SAA7134_I2C_ATTR_STATUS >> 2, dword);
209 #endif
210         d2printk(KERN_DEBUG "%s: i2c data => 0x%x\n",dev->name,data);
211         
212         if (!i2c_is_busy_wait(dev))
213                 return -EIO;
214         status = i2c_get_status(dev);
215         if (i2c_is_error(status))
216                 return -EIO;
217         return 0;
218 }
219
220 static inline int i2c_recv_byte(struct saa7134_dev *dev)
221 {
222         enum i2c_status status;
223         unsigned char data;
224         
225         i2c_set_attr(dev,CONTINUE);
226         if (!i2c_is_busy_wait(dev))
227                 return -EIO;
228         status = i2c_get_status(dev);
229         if (i2c_is_error(status))
230                 return -EIO;
231         data = saa_readb(SAA7134_I2C_DATA);
232         d2printk(KERN_DEBUG "%s: i2c data <= 0x%x\n",dev->name,data);
233         return data;
234 }
235
236 static int saa7134_i2c_xfer(struct i2c_adapter *i2c_adap,
237                             struct i2c_msg msgs[], int num)
238 {
239         struct saa7134_dev *dev = i2c_adap->algo_data;
240         enum i2c_status status;
241         unsigned char data;
242         int addr,rc,i,byte;
243
244         status = i2c_get_status(dev);
245         if (!i2c_is_idle(status))
246                 if (!i2c_reset(dev))
247                         return -EIO;
248
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                         addr  = msgs[i].addr << 1;
254                         if (msgs[i].flags & I2C_M_RD)
255                                 addr |= 1;
256                         d1printk(" < %02x", addr);
257                         rc = i2c_send_byte(dev,START,addr);
258                         if (rc < 0)
259                                  goto err;
260                 }
261                 if (msgs[i].flags & I2C_M_RD) {
262                         /* read bytes */
263                         for (byte = 0; byte < msgs[i].len; byte++) {
264                                 d1printk(" =");
265                                 rc = i2c_recv_byte(dev);
266                                 if (rc < 0)
267                                         goto err;
268                                 d1printk("%02x", rc);
269                                 msgs[i].buf[byte] = rc;
270                         }
271                 } else {
272                         /* write bytes */
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);
277                                 if (rc < 0)
278                                         goto err;
279                         }
280                 }
281         }
282         d1printk(" >");
283         i2c_set_attr(dev,STOP);
284         rc = -EIO;
285         if (!i2c_is_busy_wait(dev))
286                 goto err;
287         status = i2c_get_status(dev);
288         if (i2c_is_error(status))
289                 goto err;
290
291         d1printk("\n");
292         return num;
293  err:
294         if (1 == i2c_debug) {
295                 status = i2c_get_status(dev);
296                 printk(" ERROR: %s\n",str_i2c_status[status]);
297         }
298         return rc;
299 }
300
301 /* ----------------------------------------------------------- */
302
303 static int algo_control(struct i2c_adapter *adapter, 
304                         unsigned int cmd, unsigned long arg)
305 {
306         return 0;
307 }
308
309 static u32 functionality(struct i2c_adapter *adap)
310 {
311         return I2C_FUNC_SMBUS_EMUL;
312 }
313
314 #ifndef I2C_PEC
315 static void inc_use(struct i2c_adapter *adap)
316 {
317         MOD_INC_USE_COUNT;
318 }
319
320 static void dec_use(struct i2c_adapter *adap)
321 {
322         MOD_DEC_USE_COUNT;
323 }
324 #endif
325
326 static int attach_inform(struct i2c_client *client)
327 {
328         struct saa7134_dev *dev = client->adapter->algo_data;
329         int tuner = dev->tuner_type;
330
331         saa7134_i2c_call_clients(dev,TUNER_SET_TYPE,&tuner);
332         return 0;
333 }
334
335 static struct i2c_algorithm saa7134_algo = {
336         .name          = "saa7134",
337         .id            = I2C_ALGO_SAA7134,
338         .master_xfer   = saa7134_i2c_xfer,
339         .algo_control  = algo_control,
340         .functionality = functionality,
341 };
342
343 static struct i2c_adapter saa7134_adap_template = {
344 #ifdef I2C_PEC
345         .owner         = THIS_MODULE,
346 #else
347         .inc_use       = inc_use,
348         .dec_use       = dec_use,
349 #endif
350 #ifdef I2C_CLASS_TV_ANALOG
351         .class         = I2C_CLASS_TV_ANALOG,
352 #endif
353         I2C_DEVNAME("saa7134"),
354         .id            = I2C_ALGO_SAA7134,
355         .algo          = &saa7134_algo,
356         .client_register = attach_inform,
357 };
358
359 static struct i2c_client saa7134_client_template = {
360         I2C_DEVNAME("saa7134 internal"),
361         .id        = -1,
362 };
363
364 /* ----------------------------------------------------------- */
365
366 static int
367 saa7134_i2c_eeprom(struct saa7134_dev *dev, unsigned char *eedata, int len)
368 {
369         unsigned char buf;
370         int i,err;
371
372         dev->i2c_client.addr = 0xa0 >> 1;
373         buf = 0;
374         if (1 != (err = i2c_master_send(&dev->i2c_client,&buf,1))) {
375                 printk(KERN_INFO "%s: Huh, no eeprom present (err=%d)?\n",
376                        dev->name,err);
377                 return -1;
378         }
379         if (len != (err = i2c_master_recv(&dev->i2c_client,eedata,len))) {
380                 printk(KERN_WARNING "%s: i2c eeprom read error (err=%d)\n",
381                        dev->name,err);
382                 return -1;
383         }
384         for (i = 0; i < len; i++) {
385                 if (0 == (i % 16))
386                         printk(KERN_INFO "%s: i2c eeprom %02x:",dev->name,i);
387                 printk(" %02x",eedata[i]);
388                 if (15 == (i % 16))
389                         printk("\n");
390         }
391         return 0;
392 }
393
394 static int
395 saa7134_i2c_scan(struct saa7134_dev *dev)
396 {
397         unsigned char buf;
398         int i,rc;
399
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);
403                 if (rc < 0)
404                         continue;
405                 printk("%s: i2c scan: found device @ %x%s\n",
406                        dev->name, i, (i == 0xa0) ? " [eeprom]" : "");
407         }
408         return 0;
409 }
410
411 void saa7134_i2c_call_clients(struct saa7134_dev *dev,
412                               unsigned int cmd, void *arg)
413 {
414         BUG_ON(NULL == dev->i2c_adap.algo_data);
415         i2c_clients_command(&dev->i2c_adap, cmd, arg);
416 }
417
418 int saa7134_i2c_register(struct saa7134_dev *dev)
419 {
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);
425         
426         dev->i2c_client = saa7134_client_template;
427         dev->i2c_client.adapter = &dev->i2c_adap;
428         
429         saa7134_i2c_eeprom(dev,dev->eedata,sizeof(dev->eedata));
430         if (i2c_scan)
431                 saa7134_i2c_scan(dev);
432         return 0;
433 }
434
435 int saa7134_i2c_unregister(struct saa7134_dev *dev)
436 {
437         i2c_del_adapter(&dev->i2c_adap);
438         return 0;
439 }
440
441 /* ----------------------------------------------------------- */
442 /*
443  * Local variables:
444  * c-basic-offset: 8
445  * End:
446  */