[PATCH] UFS: inode->i_sem is not released in error path
[linux-2.6/verdex.git] / arch / cris / arch-v10 / drivers / i2c.c
blobb38267d60d30fe948d9de2d14c114ebf5da51d36
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.13 2005/03/07 13:13:07 starvik
16 *! Added spinlocks to protect states etc
18 *! Revision 1.12 2005/01/05 06:11:22 starvik
19 *! No need to do local_irq_disable after local_irq_save.
21 *! Revision 1.11 2004/12/13 12:21:52 starvik
22 *! Added I/O and DMA allocators from Linux 2.4
24 *! Revision 1.9 2004/08/24 06:49:14 starvik
25 *! Whitespace cleanup
27 *! Revision 1.8 2004/06/08 08:48:26 starvik
28 *! Removed unused code
30 *! Revision 1.7 2004/05/28 09:26:59 starvik
31 *! Modified I2C initialization to work in 2.6.
33 *! Revision 1.6 2004/05/14 07:58:03 starvik
34 *! Merge of changes from 2.4
36 *! Revision 1.4 2002/12/11 13:13:57 starvik
37 *! Added arch/ to v10 specific includes
38 *! Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
40 *! Revision 1.3 2002/11/20 11:56:11 starvik
41 *! Merge of Linux 2.5.48
43 *! Revision 1.2 2002/11/18 13:16:06 starvik
44 *! Linux 2.5 port of latest 2.4 drivers
46 *! Revision 1.9 2002/10/31 15:32:26 starvik
47 *! Update Port B register and shadow even when running with hardware support
48 *! to avoid glitches when reading bits
49 *! Never set direction to out in i2c_inbyte
50 *! Removed incorrect clock togling at end of i2c_inbyte
52 *! Revision 1.8 2002/08/13 06:31:53 starvik
53 *! Made SDA and SCL line configurable
54 *! Modified i2c_inbyte to work with PCF8563
56 *! Revision 1.7 2001/04/04 13:11:36 markusl
57 *! Updated according to review remarks
59 *! Revision 1.6 2001/03/19 12:43:00 markusl
60 *! Made some symbols unstatic (used by the eeprom driver)
62 *! Revision 1.5 2001/02/27 13:52:48 bjornw
63 *! malloc.h -> slab.h
65 *! Revision 1.4 2001/02/15 07:17:40 starvik
66 *! Corrected usage if port_pb_i2c_shadow
68 *! Revision 1.3 2001/01/26 17:55:13 bjornw
69 *! * Made I2C_USES_PB_NOT_PB_I2C a CONFIG option instead of assigning it
70 *! magically. Config.in needs to set it for the options that need it, like
71 *! Dallas 1302 support. Actually, it should be default since it screws up
72 *! the PB bits even if you don't use I2C..
73 *! * Include linux/config.h to get the above
75 *! Revision 1.2 2001/01/18 15:49:30 bjornw
76 *! 2.4 port of I2C including some cleanups (untested of course)
78 *! Revision 1.1 2001/01/18 15:35:25 bjornw
79 *! Verbatim copy of the Etrax i2c driver, 2.0 elinux version
82 *! ---------------------------------------------------------------------------
84 *! (C) Copyright 1999-2002 Axis Communications AB, LUND, SWEDEN
86 *!***************************************************************************/
87 /* $Id: i2c.c,v 1.13 2005/03/07 13:13:07 starvik Exp $ */
89 /****************** INCLUDE FILES SECTION ***********************************/
91 #include <linux/module.h>
92 #include <linux/sched.h>
93 #include <linux/slab.h>
94 #include <linux/errno.h>
95 #include <linux/kernel.h>
96 #include <linux/fs.h>
97 #include <linux/string.h>
98 #include <linux/init.h>
99 #include <linux/config.h>
101 #include <asm/etraxi2c.h>
103 #include <asm/system.h>
104 #include <asm/arch/svinto.h>
105 #include <asm/io.h>
106 #include <asm/delay.h>
107 #include <asm/arch/io_interface_mux.h>
109 #include "i2c.h"
111 /****************** I2C DEFINITION SECTION *************************/
113 #define D(x)
115 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
116 static const char i2c_name[] = "i2c";
118 #define CLOCK_LOW_TIME 8
119 #define CLOCK_HIGH_TIME 8
120 #define START_CONDITION_HOLD_TIME 8
121 #define STOP_CONDITION_HOLD_TIME 8
122 #define ENABLE_OUTPUT 0x01
123 #define ENABLE_INPUT 0x00
124 #define I2C_CLOCK_HIGH 1
125 #define I2C_CLOCK_LOW 0
126 #define I2C_DATA_HIGH 1
127 #define I2C_DATA_LOW 0
129 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
130 /* Use PB and not PB_I2C */
131 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
132 #define CONFIG_ETRAX_I2C_DATA_PORT 0
133 #endif
134 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
135 #define CONFIG_ETRAX_I2C_CLK_PORT 1
136 #endif
138 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
139 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
140 #define i2c_enable()
141 #define i2c_disable()
143 /* enable or disable output-enable, to select output or input on the i2c bus */
145 #define i2c_dir_out() \
146 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1)
147 #define i2c_dir_in() \
148 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0)
150 /* control the i2c clock and data signals */
152 #define i2c_clk(x) \
153 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x)
154 #define i2c_data(x) \
155 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x)
157 /* read a bit from the i2c interface */
159 #define i2c_getbit() (((*R_PORT_PB_READ & (1 << SDABIT))) >> SDABIT)
161 #else
162 /* enable or disable the i2c interface */
164 #define i2c_enable() *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_en))
165 #define i2c_disable() *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_en))
167 /* enable or disable output-enable, to select output or input on the i2c bus */
169 #define i2c_dir_out() \
170 *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
171 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1);
172 #define i2c_dir_in() \
173 *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
174 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0);
176 /* control the i2c clock and data signals */
178 #define i2c_clk(x) \
179 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
180 ~IO_MASK(R_PORT_PB_I2C, i2c_clk)) | IO_FIELD(R_PORT_PB_I2C, i2c_clk, (x))); \
181 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 1, x);
183 #define i2c_data(x) \
184 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
185 ~IO_MASK(R_PORT_PB_I2C, i2c_d)) | IO_FIELD(R_PORT_PB_I2C, i2c_d, (x))); \
186 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 0, x);
188 /* read a bit from the i2c interface */
190 #define i2c_getbit() (*R_PORT_PB_READ & 0x1)
191 #endif
193 /* use the kernels delay routine */
195 #define i2c_delay(usecs) udelay(usecs)
197 static DEFINE_SPINLOCK(i2c_lock); /* Protect directions etc */
199 /****************** FUNCTION DEFINITION SECTION *************************/
202 /* generate i2c start condition */
204 void
205 i2c_start(void)
208 * SCL=1 SDA=1
210 i2c_dir_out();
211 i2c_delay(CLOCK_HIGH_TIME/6);
212 i2c_data(I2C_DATA_HIGH);
213 i2c_clk(I2C_CLOCK_HIGH);
214 i2c_delay(CLOCK_HIGH_TIME);
216 * SCL=1 SDA=0
218 i2c_data(I2C_DATA_LOW);
219 i2c_delay(START_CONDITION_HOLD_TIME);
221 * SCL=0 SDA=0
223 i2c_clk(I2C_CLOCK_LOW);
224 i2c_delay(CLOCK_LOW_TIME);
227 /* generate i2c stop condition */
229 void
230 i2c_stop(void)
232 i2c_dir_out();
235 * SCL=0 SDA=0
237 i2c_clk(I2C_CLOCK_LOW);
238 i2c_data(I2C_DATA_LOW);
239 i2c_delay(CLOCK_LOW_TIME*2);
241 * SCL=1 SDA=0
243 i2c_clk(I2C_CLOCK_HIGH);
244 i2c_delay(CLOCK_HIGH_TIME*2);
246 * SCL=1 SDA=1
248 i2c_data(I2C_DATA_HIGH);
249 i2c_delay(STOP_CONDITION_HOLD_TIME);
251 i2c_dir_in();
254 /* write a byte to the i2c interface */
256 void
257 i2c_outbyte(unsigned char x)
259 int i;
261 i2c_dir_out();
263 for (i = 0; i < 8; i++) {
264 if (x & 0x80) {
265 i2c_data(I2C_DATA_HIGH);
266 } else {
267 i2c_data(I2C_DATA_LOW);
270 i2c_delay(CLOCK_LOW_TIME/2);
271 i2c_clk(I2C_CLOCK_HIGH);
272 i2c_delay(CLOCK_HIGH_TIME);
273 i2c_clk(I2C_CLOCK_LOW);
274 i2c_delay(CLOCK_LOW_TIME/2);
275 x <<= 1;
277 i2c_data(I2C_DATA_LOW);
278 i2c_delay(CLOCK_LOW_TIME/2);
281 * enable input
283 i2c_dir_in();
286 /* read a byte from the i2c interface */
288 unsigned char
289 i2c_inbyte(void)
291 unsigned char aBitByte = 0;
292 int i;
294 /* Switch off I2C to get bit */
295 i2c_disable();
296 i2c_dir_in();
297 i2c_delay(CLOCK_HIGH_TIME/2);
299 /* Get bit */
300 aBitByte |= i2c_getbit();
302 /* Enable I2C */
303 i2c_enable();
304 i2c_delay(CLOCK_LOW_TIME/2);
306 for (i = 1; i < 8; i++) {
307 aBitByte <<= 1;
308 /* Clock pulse */
309 i2c_clk(I2C_CLOCK_HIGH);
310 i2c_delay(CLOCK_HIGH_TIME);
311 i2c_clk(I2C_CLOCK_LOW);
312 i2c_delay(CLOCK_LOW_TIME);
314 /* Switch off I2C to get bit */
315 i2c_disable();
316 i2c_dir_in();
317 i2c_delay(CLOCK_HIGH_TIME/2);
319 /* Get bit */
320 aBitByte |= i2c_getbit();
322 /* Enable I2C */
323 i2c_enable();
324 i2c_delay(CLOCK_LOW_TIME/2);
326 i2c_clk(I2C_CLOCK_HIGH);
327 i2c_delay(CLOCK_HIGH_TIME);
330 * we leave the clock low, getbyte is usually followed
331 * by sendack/nack, they assume the clock to be low
333 i2c_clk(I2C_CLOCK_LOW);
334 return aBitByte;
337 /*#---------------------------------------------------------------------------
339 *# FUNCTION NAME: i2c_getack
341 *# DESCRIPTION : checks if ack was received from ic2
343 *#--------------------------------------------------------------------------*/
346 i2c_getack(void)
348 int ack = 1;
350 * enable output
352 i2c_dir_out();
354 * Release data bus by setting
355 * data high
357 i2c_data(I2C_DATA_HIGH);
359 * enable input
361 i2c_dir_in();
362 i2c_delay(CLOCK_HIGH_TIME/4);
364 * generate ACK clock pulse
366 i2c_clk(I2C_CLOCK_HIGH);
368 * Use PORT PB instead of I2C
369 * for input. (I2C not working)
371 i2c_clk(1);
372 i2c_data(1);
374 * switch off I2C
376 i2c_data(1);
377 i2c_disable();
378 i2c_dir_in();
380 * now wait for ack
382 i2c_delay(CLOCK_HIGH_TIME/2);
384 * check for ack
386 if(i2c_getbit())
387 ack = 0;
388 i2c_delay(CLOCK_HIGH_TIME/2);
389 if(!ack){
390 if(!i2c_getbit()) /* receiver pulld SDA low */
391 ack = 1;
392 i2c_delay(CLOCK_HIGH_TIME/2);
396 * our clock is high now, make sure data is low
397 * before we enable our output. If we keep data high
398 * and enable output, we would generate a stop condition.
400 i2c_data(I2C_DATA_LOW);
403 * end clock pulse
405 i2c_enable();
406 i2c_dir_out();
407 i2c_clk(I2C_CLOCK_LOW);
408 i2c_delay(CLOCK_HIGH_TIME/4);
410 * enable output
412 i2c_dir_out();
414 * remove ACK clock pulse
416 i2c_data(I2C_DATA_HIGH);
417 i2c_delay(CLOCK_LOW_TIME/2);
418 return ack;
421 /*#---------------------------------------------------------------------------
423 *# FUNCTION NAME: I2C::sendAck
425 *# DESCRIPTION : Send ACK on received data
427 *#--------------------------------------------------------------------------*/
428 void
429 i2c_sendack(void)
432 * enable output
434 i2c_delay(CLOCK_LOW_TIME);
435 i2c_dir_out();
437 * set ack pulse high
439 i2c_data(I2C_DATA_LOW);
441 * generate clock pulse
443 i2c_delay(CLOCK_HIGH_TIME/6);
444 i2c_clk(I2C_CLOCK_HIGH);
445 i2c_delay(CLOCK_HIGH_TIME);
446 i2c_clk(I2C_CLOCK_LOW);
447 i2c_delay(CLOCK_LOW_TIME/6);
449 * reset data out
451 i2c_data(I2C_DATA_HIGH);
452 i2c_delay(CLOCK_LOW_TIME);
454 i2c_dir_in();
457 /*#---------------------------------------------------------------------------
459 *# FUNCTION NAME: i2c_sendnack
461 *# DESCRIPTION : Sends NACK on received data
463 *#--------------------------------------------------------------------------*/
464 void
465 i2c_sendnack(void)
468 * enable output
470 i2c_delay(CLOCK_LOW_TIME);
471 i2c_dir_out();
473 * set data high
475 i2c_data(I2C_DATA_HIGH);
477 * generate clock pulse
479 i2c_delay(CLOCK_HIGH_TIME/6);
480 i2c_clk(I2C_CLOCK_HIGH);
481 i2c_delay(CLOCK_HIGH_TIME);
482 i2c_clk(I2C_CLOCK_LOW);
483 i2c_delay(CLOCK_LOW_TIME);
485 i2c_dir_in();
488 /*#---------------------------------------------------------------------------
490 *# FUNCTION NAME: i2c_writereg
492 *# DESCRIPTION : Writes a value to an I2C device
494 *#--------------------------------------------------------------------------*/
496 i2c_writereg(unsigned char theSlave, unsigned char theReg,
497 unsigned char theValue)
499 int error, cntr = 3;
500 unsigned long flags;
502 spin_lock(&i2c_lock);
504 do {
505 error = 0;
507 * we don't like to be interrupted
509 local_irq_save(flags);
511 i2c_start();
513 * send slave address
515 i2c_outbyte((theSlave & 0xfe));
517 * wait for ack
519 if(!i2c_getack())
520 error = 1;
522 * now select register
524 i2c_dir_out();
525 i2c_outbyte(theReg);
527 * now it's time to wait for ack
529 if(!i2c_getack())
530 error |= 2;
532 * send register register data
534 i2c_outbyte(theValue);
536 * now it's time to wait for ack
538 if(!i2c_getack())
539 error |= 4;
541 * end byte stream
543 i2c_stop();
545 * enable interrupt again
547 local_irq_restore(flags);
549 } while(error && cntr--);
551 i2c_delay(CLOCK_LOW_TIME);
553 spin_unlock(&i2c_lock);
555 return -error;
558 /*#---------------------------------------------------------------------------
560 *# FUNCTION NAME: i2c_readreg
562 *# DESCRIPTION : Reads a value from the decoder registers.
564 *#--------------------------------------------------------------------------*/
565 unsigned char
566 i2c_readreg(unsigned char theSlave, unsigned char theReg)
568 unsigned char b = 0;
569 int error, cntr = 3;
570 unsigned long flags;
572 spin_lock(&i2c_lock);
574 do {
575 error = 0;
577 * we don't like to be interrupted
579 local_irq_save(flags);
581 * generate start condition
583 i2c_start();
586 * send slave address
588 i2c_outbyte((theSlave & 0xfe));
590 * wait for ack
592 if(!i2c_getack())
593 error = 1;
595 * now select register
597 i2c_dir_out();
598 i2c_outbyte(theReg);
600 * now it's time to wait for ack
602 if(!i2c_getack())
603 error = 1;
605 * repeat start condition
607 i2c_delay(CLOCK_LOW_TIME);
608 i2c_start();
610 * send slave address
612 i2c_outbyte(theSlave | 0x01);
614 * wait for ack
616 if(!i2c_getack())
617 error = 1;
619 * fetch register
621 b = i2c_inbyte();
623 * last received byte needs to be nacked
624 * instead of acked
626 i2c_sendack();
628 * end sequence
630 i2c_stop();
632 * enable interrupt again
634 local_irq_restore(flags);
636 } while(error && cntr--);
638 spin_unlock(&i2c_lock);
640 return b;
643 static int
644 i2c_open(struct inode *inode, struct file *filp)
646 return 0;
649 static int
650 i2c_release(struct inode *inode, struct file *filp)
652 return 0;
655 /* Main device API. ioctl's to write or read to/from i2c registers.
658 static int
659 i2c_ioctl(struct inode *inode, struct file *file,
660 unsigned int cmd, unsigned long arg)
662 if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
663 return -EINVAL;
666 switch (_IOC_NR(cmd)) {
667 case I2C_WRITEREG:
668 /* write to an i2c slave */
669 D(printk("i2cw %d %d %d\n",
670 I2C_ARGSLAVE(arg),
671 I2C_ARGREG(arg),
672 I2C_ARGVALUE(arg)));
674 return i2c_writereg(I2C_ARGSLAVE(arg),
675 I2C_ARGREG(arg),
676 I2C_ARGVALUE(arg));
677 case I2C_READREG:
679 unsigned char val;
680 /* read from an i2c slave */
681 D(printk("i2cr %d %d ",
682 I2C_ARGSLAVE(arg),
683 I2C_ARGREG(arg)));
684 val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
685 D(printk("= %d\n", val));
686 return val;
688 default:
689 return -EINVAL;
693 return 0;
696 static struct file_operations i2c_fops = {
697 .owner = THIS_MODULE,
698 .ioctl = i2c_ioctl,
699 .open = i2c_open,
700 .release = i2c_release,
703 int __init
704 i2c_init(void)
706 static int res = 0;
707 static int first = 1;
709 if (!first) {
710 return res;
713 /* Setup and enable the Port B I2C interface */
715 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
716 if ((res = cris_request_io_interface(if_i2c, "I2C"))) {
717 printk(KERN_CRIT "i2c_init: Failed to get IO interface\n");
718 return res;
721 *R_PORT_PB_I2C = port_pb_i2c_shadow |=
722 IO_STATE(R_PORT_PB_I2C, i2c_en, on) |
723 IO_FIELD(R_PORT_PB_I2C, i2c_d, 1) |
724 IO_FIELD(R_PORT_PB_I2C, i2c_clk, 1) |
725 IO_STATE(R_PORT_PB_I2C, i2c_oe_, enable);
727 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir0);
728 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir1);
730 *R_PORT_PB_DIR = (port_pb_dir_shadow |=
731 IO_STATE(R_PORT_PB_DIR, dir0, input) |
732 IO_STATE(R_PORT_PB_DIR, dir1, output));
733 #else
734 if ((res = cris_io_interface_allocate_pins(if_i2c,
735 'b',
736 CONFIG_ETRAX_I2C_DATA_PORT,
737 CONFIG_ETRAX_I2C_DATA_PORT))) {
738 printk(KERN_WARNING "i2c_init: Failed to get IO pin for I2C data port\n");
739 return res;
740 } else if ((res = cris_io_interface_allocate_pins(if_i2c,
741 'b',
742 CONFIG_ETRAX_I2C_CLK_PORT,
743 CONFIG_ETRAX_I2C_CLK_PORT))) {
744 cris_io_interface_free_pins(if_i2c,
745 'b',
746 CONFIG_ETRAX_I2C_DATA_PORT,
747 CONFIG_ETRAX_I2C_DATA_PORT);
748 printk(KERN_WARNING "i2c_init: Failed to get IO pin for I2C clk port\n");
750 #endif
752 return res;
755 static int __init
756 i2c_register(void)
758 int res;
760 res = i2c_init();
761 if (res < 0)
762 return res;
763 res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
764 if(res < 0) {
765 printk(KERN_ERR "i2c: couldn't get a major number.\n");
766 return res;
769 printk(KERN_INFO "I2C driver v2.2, (c) 1999-2004 Axis Communications AB\n");
771 return 0;
774 /* this makes sure that i2c_register is called during boot */
776 module_init(i2c_register);
778 /****************** END OF FILE i2c.c ********************************/