ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / cris / arch-v10 / drivers / i2c.c
1 /*!***************************************************************************
2 *!
3 *! FILE NAME  : i2c.c
4 *!
5 *! DESCRIPTION: implements an interface for IIC/I2C, both directly from other
6 *!              kernel modules (i2c_writereg/readreg) and from userspace using
7 *!              ioctl()'s
8 *!
9 *! Nov 30 1998  Torbjorn Eliasson  Initial version.
10 *!              Bjorn Wesen        Elinux kernel version.
11 *! Jan 14 2000  Johan Adolfsson    Fixed PB shadow register stuff - 
12 *!                                 don't use PB_I2C if DS1302 uses same bits,
13 *!                                 use PB.
14 *! $Log: i2c.c,v $
15 *! Revision 1.4  2002/12/11 13:13:57  starvik
16 *! Added arch/ to v10 specific includes
17 *! Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
18 *!
19 *! Revision 1.3  2002/11/20 11:56:11  starvik
20 *! Merge of Linux 2.5.48
21 *!
22 *! Revision 1.2  2002/11/18 13:16:06  starvik
23 *! Linux 2.5 port of latest 2.4 drivers
24 *!
25 *! Revision 1.9  2002/10/31 15:32:26  starvik
26 *! Update Port B register and shadow even when running with hardware support
27 *!   to avoid glitches when reading bits
28 *! Never set direction to out in i2c_inbyte
29 *! Removed incorrect clock togling at end of i2c_inbyte
30 *!
31 *! Revision 1.8  2002/08/13 06:31:53  starvik
32 *! Made SDA and SCL line configurable
33 *! Modified i2c_inbyte to work with PCF8563
34 *!
35 *! Revision 1.7  2001/04/04 13:11:36  markusl
36 *! Updated according to review remarks
37 *!
38 *! Revision 1.6  2001/03/19 12:43:00  markusl
39 *! Made some symbols unstatic (used by the eeprom driver)
40 *!
41 *! Revision 1.5  2001/02/27 13:52:48  bjornw
42 *! malloc.h -> slab.h
43 *!
44 *! Revision 1.4  2001/02/15 07:17:40  starvik
45 *! Corrected usage if port_pb_i2c_shadow
46 *!
47 *! Revision 1.3  2001/01/26 17:55:13  bjornw
48 *! * Made I2C_USES_PB_NOT_PB_I2C a CONFIG option instead of assigning it
49 *!   magically. Config.in needs to set it for the options that need it, like
50 *!   Dallas 1302 support. Actually, it should be default since it screws up
51 *!   the PB bits even if you don't use I2C..
52 *! * Include linux/config.h to get the above
53 *!
54 *! Revision 1.2  2001/01/18 15:49:30  bjornw
55 *! 2.4 port of I2C including some cleanups (untested of course)
56 *!
57 *! Revision 1.1  2001/01/18 15:35:25  bjornw
58 *! Verbatim copy of the Etrax i2c driver, 2.0 elinux version
59 *!
60 *!
61 *! ---------------------------------------------------------------------------
62 *!
63 *! (C) Copyright 1999-2002 Axis Communications AB, LUND, SWEDEN
64 *!
65 *!***************************************************************************/
66 /* $Id: i2c.c,v 1.4 2002/12/11 13:13:57 starvik Exp $ */
67
68 /****************** INCLUDE FILES SECTION ***********************************/
69
70 #include <linux/module.h>
71 #include <linux/sched.h>
72 #include <linux/slab.h>
73 #include <linux/errno.h>
74 #include <linux/kernel.h>
75 #include <linux/fs.h>
76 #include <linux/string.h>
77 #include <linux/init.h>
78 #include <linux/config.h>
79
80 #include <asm/etraxi2c.h>
81
82 #include <asm/system.h>
83 #include <asm/arch/svinto.h>
84 #include <asm/io.h>
85 #include <asm/delay.h>
86
87 #include "i2c.h"
88
89 /****************** I2C DEFINITION SECTION *************************/
90
91 #define D(x)
92
93 #define I2C_MAJOR 123  /* LOCAL/EXPERIMENTAL */
94 static const char i2c_name[] = "i2c";
95
96 #define CLOCK_LOW_TIME            8
97 #define CLOCK_HIGH_TIME           8
98 #define START_CONDITION_HOLD_TIME 8
99 #define STOP_CONDITION_HOLD_TIME  8
100 #define ENABLE_OUTPUT 0x01
101 #define ENABLE_INPUT 0x00
102 #define I2C_CLOCK_HIGH 1
103 #define I2C_CLOCK_LOW 0
104 #define I2C_DATA_HIGH 1
105 #define I2C_DATA_LOW 0
106
107 #if 0
108 /* TODO: fix this so the CONFIG_ETRAX_I2C_USES... is set in Config.in instead */
109 #if defined(CONFIG_DS1302) && (CONFIG_DS1302_SDABIT==0) && \
110            (CONFIG_DS1302_SCLBIT == 1)
111 #define CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
112 #endif
113 #endif
114
115 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
116 /* Use PB and not PB_I2C */
117 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
118 #define CONFIG_ETRAX_I2C_DATA_PORT 0
119 #endif
120 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
121 #define CONFIG_ETRAX_I2C_CLK_PORT 1
122 #endif
123
124 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
125 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
126 #define i2c_enable() 
127 #define i2c_disable() 
128
129 /* enable or disable output-enable, to select output or input on the i2c bus */
130
131 #define i2c_dir_out() \
132   REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1)
133 #define i2c_dir_in()  \
134   REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0)
135
136 /* control the i2c clock and data signals */
137
138 #define i2c_clk(x) \
139   REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x)
140 #define i2c_data(x) \
141   REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x)
142
143 /* read a bit from the i2c interface */
144
145 #define i2c_getbit() (((*R_PORT_PB_READ & (1 << SDABIT))) >> SDABIT)
146
147 #else
148 /* enable or disable the i2c interface */
149
150 #define i2c_enable() *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_en))
151 #define i2c_disable() *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_en))
152
153 /* enable or disable output-enable, to select output or input on the i2c bus */
154
155 #define i2c_dir_out() \
156         *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
157         REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1); 
158 #define i2c_dir_in() \
159         *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
160         REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0);
161
162 /* control the i2c clock and data signals */
163
164 #define i2c_clk(x) \
165         *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
166        ~IO_MASK(R_PORT_PB_I2C, i2c_clk)) | IO_FIELD(R_PORT_PB_I2C, i2c_clk, (x))); \
167        REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 1, x);
168
169 #define i2c_data(x) \
170         *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
171            ~IO_MASK(R_PORT_PB_I2C, i2c_d)) | IO_FIELD(R_PORT_PB_I2C, i2c_d, (x))); \
172         REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 0, x);
173
174 /* read a bit from the i2c interface */
175
176 #define i2c_getbit() (*R_PORT_PB_READ & 0x1)
177 #endif
178
179 /* use the kernels delay routine */
180
181 #define i2c_delay(usecs) udelay(usecs)
182
183
184 /****************** FUNCTION DEFINITION SECTION *************************/
185
186
187 /* generate i2c start condition */
188
189 void
190 i2c_start(void)
191 {
192         /*
193          * SCL=1 SDA=1
194          */
195         i2c_dir_out();
196         i2c_delay(CLOCK_HIGH_TIME/6);
197         i2c_data(I2C_DATA_HIGH);
198         i2c_clk(I2C_CLOCK_HIGH);
199         i2c_delay(CLOCK_HIGH_TIME);
200         /*
201          * SCL=1 SDA=0
202          */
203         i2c_data(I2C_DATA_LOW);
204         i2c_delay(START_CONDITION_HOLD_TIME);
205         /*
206          * SCL=0 SDA=0
207          */
208         i2c_clk(I2C_CLOCK_LOW);
209         i2c_delay(CLOCK_LOW_TIME);
210 }
211
212 /* generate i2c stop condition */
213
214 void
215 i2c_stop(void)
216 {
217         i2c_dir_out();
218
219         /*
220          * SCL=0 SDA=0
221          */
222         i2c_clk(I2C_CLOCK_LOW);
223         i2c_data(I2C_DATA_LOW);
224         i2c_delay(CLOCK_LOW_TIME*2);
225         /*
226          * SCL=1 SDA=0
227          */
228         i2c_clk(I2C_CLOCK_HIGH);
229         i2c_delay(CLOCK_HIGH_TIME*2);
230         /*
231          * SCL=1 SDA=1
232          */
233         i2c_data(I2C_DATA_HIGH);
234         i2c_delay(STOP_CONDITION_HOLD_TIME);
235
236         i2c_dir_in();
237 }
238
239 /* write a byte to the i2c interface */
240
241 void
242 i2c_outbyte(unsigned char x)
243 {
244         int i;
245
246         i2c_dir_out();
247
248         for (i = 0; i < 8; i++) {
249                 if (x & 0x80) {
250                         i2c_data(I2C_DATA_HIGH);
251                 } else {
252                         i2c_data(I2C_DATA_LOW);
253                 }
254                 
255                 i2c_delay(CLOCK_LOW_TIME/2);
256                 i2c_clk(I2C_CLOCK_HIGH);
257                 i2c_delay(CLOCK_HIGH_TIME);
258                 i2c_clk(I2C_CLOCK_LOW);
259                 i2c_delay(CLOCK_LOW_TIME/2);
260                 x <<= 1;
261         }
262         i2c_data(I2C_DATA_LOW);
263         i2c_delay(CLOCK_LOW_TIME/2);
264
265         /*
266          * enable input
267          */
268         i2c_dir_in();
269 }
270
271 /* read a byte from the i2c interface */
272
273 unsigned char
274 i2c_inbyte(void)
275 {
276         unsigned char aBitByte = 0;
277         int i;
278
279         /* Switch off I2C to get bit */
280         i2c_disable();
281         i2c_dir_in();
282         i2c_delay(CLOCK_HIGH_TIME/2);
283
284         /* Get bit */
285         aBitByte |= i2c_getbit();
286
287         /* Enable I2C */
288         i2c_enable();
289         i2c_delay(CLOCK_LOW_TIME/2);
290
291         for (i = 1; i < 8; i++) {
292                 aBitByte <<= 1;
293                 /* Clock pulse */
294                 i2c_clk(I2C_CLOCK_HIGH);
295                 i2c_delay(CLOCK_HIGH_TIME);
296                 i2c_clk(I2C_CLOCK_LOW);
297                 i2c_delay(CLOCK_LOW_TIME);
298
299                 /* Switch off I2C to get bit */
300                 i2c_disable();
301                 i2c_dir_in();
302                 i2c_delay(CLOCK_HIGH_TIME/2);
303
304                 /* Get bit */
305                 aBitByte |= i2c_getbit();
306
307                 /* Enable I2C */
308                 i2c_enable();
309                 i2c_delay(CLOCK_LOW_TIME/2);
310         }
311         i2c_clk(I2C_CLOCK_HIGH);
312         i2c_delay(CLOCK_HIGH_TIME);
313         return aBitByte;
314 }
315
316 /*#---------------------------------------------------------------------------
317 *#
318 *# FUNCTION NAME: i2c_getack
319 *#
320 *# DESCRIPTION  : checks if ack was received from ic2
321 *#
322 *#--------------------------------------------------------------------------*/
323
324 int
325 i2c_getack(void)
326 {
327         int ack = 1;
328         /*
329          * enable output
330          */
331         i2c_dir_out();
332         /*
333          * Release data bus by setting
334          * data high
335          */
336         i2c_data(I2C_DATA_HIGH);
337         /*
338          * enable input
339          */
340         i2c_dir_in();
341         i2c_delay(CLOCK_HIGH_TIME/4);
342         /*
343          * generate ACK clock pulse
344          */
345         i2c_clk(I2C_CLOCK_HIGH);
346         /*
347          * Use PORT PB instead of I2C
348          * for input. (I2C not working)
349          */
350         i2c_clk(1);
351         i2c_data(1);
352         /*
353          * switch off I2C
354          */
355         i2c_data(1);
356         i2c_disable();
357         i2c_dir_in();
358         /*
359          * now wait for ack
360          */
361         i2c_delay(CLOCK_HIGH_TIME/2);
362         /*
363          * check for ack
364          */
365         if(i2c_getbit())
366                 ack = 0;
367         i2c_delay(CLOCK_HIGH_TIME/2);
368         if(!ack){
369                 if(!i2c_getbit()) /* receiver pulld SDA low */
370                         ack = 1;
371                 i2c_delay(CLOCK_HIGH_TIME/2);
372         }
373
374         /*
375          * end clock pulse
376          */
377         i2c_enable();
378         i2c_dir_out();
379         i2c_clk(I2C_CLOCK_LOW);
380         i2c_delay(CLOCK_HIGH_TIME/4);
381         /*
382          * enable output
383          */
384         i2c_dir_out();
385         /*
386          * remove ACK clock pulse
387          */
388         i2c_data(I2C_DATA_HIGH);
389         i2c_delay(CLOCK_LOW_TIME/2);
390         return ack;
391 }
392
393 /*#---------------------------------------------------------------------------
394 *#
395 *# FUNCTION NAME: I2C::sendAck
396 *#
397 *# DESCRIPTION  : Send ACK on received data
398 *#
399 *#--------------------------------------------------------------------------*/
400 void
401 i2c_sendack(void)
402 {
403         /*
404          * enable output
405          */
406         i2c_delay(CLOCK_LOW_TIME);
407         i2c_dir_out();
408         /*
409          * set ack pulse high
410          */
411         i2c_data(I2C_DATA_LOW);
412         /*
413          * generate clock pulse
414          */
415         i2c_delay(CLOCK_HIGH_TIME/6);
416         i2c_clk(I2C_CLOCK_HIGH);
417         i2c_delay(CLOCK_HIGH_TIME);
418         i2c_clk(I2C_CLOCK_LOW);
419         i2c_delay(CLOCK_LOW_TIME/6);
420         /*
421          * reset data out
422          */
423         i2c_data(I2C_DATA_HIGH);
424         i2c_delay(CLOCK_LOW_TIME);
425         
426         i2c_dir_in();
427 }
428
429 /*#---------------------------------------------------------------------------
430 *#
431 *# FUNCTION NAME: i2c_writereg
432 *#
433 *# DESCRIPTION  : Writes a value to an I2C device
434 *#
435 *#--------------------------------------------------------------------------*/
436 int
437 i2c_writereg(unsigned char theSlave, unsigned char theReg, 
438              unsigned char theValue)
439 {
440         int error, cntr = 3;
441         unsigned long flags;
442
443         do {
444                 error = 0;
445                 /*
446                  * we don't like to be interrupted
447                  */
448                 local_irq_save(flags);
449                 local_irq_disable();
450
451                 i2c_start();
452                 /*
453                  * send slave address
454                  */
455                 i2c_outbyte((theSlave & 0xfe));
456                 /*
457                  * wait for ack
458                  */
459                 if(!i2c_getack())
460                         error = 1;
461                 /*
462                  * now select register
463                  */
464                 i2c_dir_out();
465                 i2c_outbyte(theReg);
466                 /*
467                  * now it's time to wait for ack
468                  */
469                 if(!i2c_getack())
470                         error |= 2;
471                 /*
472                  * send register register data
473                  */
474                 i2c_outbyte(theValue);
475                 /*
476                  * now it's time to wait for ack
477                  */
478                 if(!i2c_getack())
479                         error |= 4;
480                 /*
481                  * end byte stream
482                  */
483                 i2c_stop();
484                 /*
485                  * enable interrupt again
486                  */
487                 local_irq_restore(flags);
488                 
489         } while(error && cntr--);
490
491         i2c_delay(CLOCK_LOW_TIME);
492         
493         return -error;
494 }
495
496 /*#---------------------------------------------------------------------------
497 *#
498 *# FUNCTION NAME: i2c_readreg
499 *#
500 *# DESCRIPTION  : Reads a value from the decoder registers.
501 *#
502 *#--------------------------------------------------------------------------*/
503 unsigned char
504 i2c_readreg(unsigned char theSlave, unsigned char theReg)
505 {
506         unsigned char b = 0;
507         int error, cntr = 3;
508         unsigned long flags;
509
510         do {
511                 error = 0;
512                 /*
513                  * we don't like to be interrupted
514                  */
515                 local_irq_save(flags);
516                 local_irq_disable();
517                 /*
518                  * generate start condition
519                  */
520                 i2c_start();
521     
522                 /*
523                  * send slave address
524                  */
525                 i2c_outbyte((theSlave & 0xfe));
526                 /*
527                  * wait for ack
528                  */
529                 if(!i2c_getack())
530                         error = 1;
531                 /*
532                  * now select register
533                  */
534                 i2c_dir_out();
535                 i2c_outbyte(theReg);
536                 /*
537                  * now it's time to wait for ack
538                  */
539                 if(!i2c_getack())
540                         error = 1;
541                 /*
542                  * repeat start condition
543                  */
544                 i2c_delay(CLOCK_LOW_TIME);
545                 i2c_start();
546                 /*
547                  * send slave address
548                  */
549                 i2c_outbyte(theSlave | 0x01);
550                 /*
551                  * wait for ack
552                  */
553                 if(!i2c_getack())
554                         error = 1;
555                 /*
556                  * fetch register
557                  */
558                 b = i2c_inbyte();
559                 /*
560                  * send Ack
561                  */
562                 i2c_sendack();
563                 /*
564                  * end sequence
565                  */
566                 i2c_stop();
567                 /*
568                  * enable interrupt again
569                  */
570                 local_irq_restore(flags);
571                 
572         } while(error && cntr--);
573
574         return b;
575 }
576
577 static int
578 i2c_open(struct inode *inode, struct file *filp)
579 {
580         return 0;
581 }
582
583 static int
584 i2c_release(struct inode *inode, struct file *filp)
585 {
586         return 0;
587 }
588
589 /* Main device API. ioctl's to write or read to/from i2c registers.
590  */
591
592 static int
593 i2c_ioctl(struct inode *inode, struct file *file,
594           unsigned int cmd, unsigned long arg)
595 {
596         if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
597                 return -EINVAL;
598         }
599
600         switch (_IOC_NR(cmd)) {
601                 case I2C_WRITEREG:
602                         /* write to an i2c slave */
603                         D(printk("i2cw %d %d %d\n", 
604                                  I2C_ARGSLAVE(arg),
605                                  I2C_ARGREG(arg),
606                                  I2C_ARGVALUE(arg)));
607
608                         return i2c_writereg(I2C_ARGSLAVE(arg),
609                                             I2C_ARGREG(arg),
610                                             I2C_ARGVALUE(arg));
611                 case I2C_READREG:
612                 {
613                         unsigned char val;
614                         /* read from an i2c slave */
615                         D(printk("i2cr %d %d ", 
616                                 I2C_ARGSLAVE(arg),
617                                 I2C_ARGREG(arg)));
618                         val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
619                         D(printk("= %d\n", val));
620                         return val;
621                 }                                           
622                 default:
623                         return -EINVAL;
624
625         }
626         
627         return 0;
628 }
629
630 static struct file_operations i2c_fops = {
631         .owner    = THIS_MODULE,
632         .ioctl    = i2c_ioctl,
633         .open     = i2c_open,
634         .release  = i2c_release,
635 };
636
637 static int __init
638 i2c_init(void)
639 {
640         int res;
641
642         /* Setup and enable the Port B I2C interface */
643
644 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
645         *R_PORT_PB_I2C = port_pb_i2c_shadow |= 
646                 IO_STATE(R_PORT_PB_I2C, i2c_en,  on) |
647                 IO_FIELD(R_PORT_PB_I2C, i2c_d,   1)  |
648                 IO_FIELD(R_PORT_PB_I2C, i2c_clk, 1)  |
649                 IO_STATE(R_PORT_PB_I2C, i2c_oe_, enable);
650 #endif
651
652         port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir0);
653         port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir1);
654
655         *R_PORT_PB_DIR = (port_pb_dir_shadow |=
656                           IO_STATE(R_PORT_PB_DIR, dir0, input)  |
657                           IO_STATE(R_PORT_PB_DIR, dir1, output));
658
659         /* register char device */
660
661         res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
662         if(res < 0) {
663                 printk(KERN_ERR "i2c: couldn't get a major number.\n");
664                 return res;
665         }
666
667         printk("I2C driver v2.2, (c) 1999-2001 Axis Communications AB\n");
668         
669         return 0;
670 }
671
672 /* this makes sure that i2c_init is called during boot */
673
674 module_init(i2c_init);
675
676 /****************** END OF FILE i2c.c ********************************/