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