conn rcv_lock converted to spinlock, struct cor_sock created, kernel_packet skb_clone...
[cor_2_6_31.git] / arch / cris / arch-v32 / drivers / i2c.c
blob179e7b8043313eaab628b2ceabf79d67ba67dab4
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 *| June 23 2003 Pieter Grimmerink Added 'i2c_sendnack'. i2c_readreg now
15 *| generates nack on last received byte,
16 *| instead of ack.
17 *| i2c_getack changed data level while clock
18 *| was high, causing DS75 to see a stop condition
20 *! ---------------------------------------------------------------------------
22 *! (C) Copyright 1999-2007 Axis Communications AB, LUND, SWEDEN
24 *!***************************************************************************/
26 /****************** INCLUDE FILES SECTION ***********************************/
28 #include <linux/module.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/fs.h>
34 #include <linux/string.h>
35 #include <linux/init.h>
36 #include <linux/smp_lock.h>
38 #include <asm/etraxi2c.h>
40 #include <asm/system.h>
41 #include <asm/io.h>
42 #include <asm/delay.h>
44 #include "i2c.h"
46 /****************** I2C DEFINITION SECTION *************************/
48 #define D(x)
50 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
51 static const char i2c_name[] = "i2c";
53 #define CLOCK_LOW_TIME 8
54 #define CLOCK_HIGH_TIME 8
55 #define START_CONDITION_HOLD_TIME 8
56 #define STOP_CONDITION_HOLD_TIME 8
57 #define ENABLE_OUTPUT 0x01
58 #define ENABLE_INPUT 0x00
59 #define I2C_CLOCK_HIGH 1
60 #define I2C_CLOCK_LOW 0
61 #define I2C_DATA_HIGH 1
62 #define I2C_DATA_LOW 0
64 #define i2c_enable()
65 #define i2c_disable()
67 /* enable or disable output-enable, to select output or input on the i2c bus */
69 #define i2c_dir_out() crisv32_io_set_dir(&cris_i2c_data, crisv32_io_dir_out)
70 #define i2c_dir_in() crisv32_io_set_dir(&cris_i2c_data, crisv32_io_dir_in)
72 /* control the i2c clock and data signals */
74 #define i2c_clk(x) crisv32_io_set(&cris_i2c_clk, x)
75 #define i2c_data(x) crisv32_io_set(&cris_i2c_data, x)
77 /* read a bit from the i2c interface */
79 #define i2c_getbit() crisv32_io_rd(&cris_i2c_data)
81 #define i2c_delay(usecs) udelay(usecs)
83 static DEFINE_SPINLOCK(i2c_lock); /* Protect directions etc */
85 /****************** VARIABLE SECTION ************************************/
87 static struct crisv32_iopin cris_i2c_clk;
88 static struct crisv32_iopin cris_i2c_data;
90 /****************** FUNCTION DEFINITION SECTION *************************/
93 /* generate i2c start condition */
95 void
96 i2c_start(void)
99 * SCL=1 SDA=1
101 i2c_dir_out();
102 i2c_delay(CLOCK_HIGH_TIME/6);
103 i2c_data(I2C_DATA_HIGH);
104 i2c_clk(I2C_CLOCK_HIGH);
105 i2c_delay(CLOCK_HIGH_TIME);
107 * SCL=1 SDA=0
109 i2c_data(I2C_DATA_LOW);
110 i2c_delay(START_CONDITION_HOLD_TIME);
112 * SCL=0 SDA=0
114 i2c_clk(I2C_CLOCK_LOW);
115 i2c_delay(CLOCK_LOW_TIME);
118 /* generate i2c stop condition */
120 void
121 i2c_stop(void)
123 i2c_dir_out();
126 * SCL=0 SDA=0
128 i2c_clk(I2C_CLOCK_LOW);
129 i2c_data(I2C_DATA_LOW);
130 i2c_delay(CLOCK_LOW_TIME*2);
132 * SCL=1 SDA=0
134 i2c_clk(I2C_CLOCK_HIGH);
135 i2c_delay(CLOCK_HIGH_TIME*2);
137 * SCL=1 SDA=1
139 i2c_data(I2C_DATA_HIGH);
140 i2c_delay(STOP_CONDITION_HOLD_TIME);
142 i2c_dir_in();
145 /* write a byte to the i2c interface */
147 void
148 i2c_outbyte(unsigned char x)
150 int i;
152 i2c_dir_out();
154 for (i = 0; i < 8; i++) {
155 if (x & 0x80) {
156 i2c_data(I2C_DATA_HIGH);
157 } else {
158 i2c_data(I2C_DATA_LOW);
161 i2c_delay(CLOCK_LOW_TIME/2);
162 i2c_clk(I2C_CLOCK_HIGH);
163 i2c_delay(CLOCK_HIGH_TIME);
164 i2c_clk(I2C_CLOCK_LOW);
165 i2c_delay(CLOCK_LOW_TIME/2);
166 x <<= 1;
168 i2c_data(I2C_DATA_LOW);
169 i2c_delay(CLOCK_LOW_TIME/2);
172 * enable input
174 i2c_dir_in();
177 /* read a byte from the i2c interface */
179 unsigned char
180 i2c_inbyte(void)
182 unsigned char aBitByte = 0;
183 int i;
185 /* Switch off I2C to get bit */
186 i2c_disable();
187 i2c_dir_in();
188 i2c_delay(CLOCK_HIGH_TIME/2);
190 /* Get bit */
191 aBitByte |= i2c_getbit();
193 /* Enable I2C */
194 i2c_enable();
195 i2c_delay(CLOCK_LOW_TIME/2);
197 for (i = 1; i < 8; i++) {
198 aBitByte <<= 1;
199 /* Clock pulse */
200 i2c_clk(I2C_CLOCK_HIGH);
201 i2c_delay(CLOCK_HIGH_TIME);
202 i2c_clk(I2C_CLOCK_LOW);
203 i2c_delay(CLOCK_LOW_TIME);
205 /* Switch off I2C to get bit */
206 i2c_disable();
207 i2c_dir_in();
208 i2c_delay(CLOCK_HIGH_TIME/2);
210 /* Get bit */
211 aBitByte |= i2c_getbit();
213 /* Enable I2C */
214 i2c_enable();
215 i2c_delay(CLOCK_LOW_TIME/2);
217 i2c_clk(I2C_CLOCK_HIGH);
218 i2c_delay(CLOCK_HIGH_TIME);
221 * we leave the clock low, getbyte is usually followed
222 * by sendack/nack, they assume the clock to be low
224 i2c_clk(I2C_CLOCK_LOW);
225 return aBitByte;
228 /*#---------------------------------------------------------------------------
230 *# FUNCTION NAME: i2c_getack
232 *# DESCRIPTION : checks if ack was received from ic2
234 *#--------------------------------------------------------------------------*/
237 i2c_getack(void)
239 int ack = 1;
241 * enable output
243 i2c_dir_out();
245 * Release data bus by setting
246 * data high
248 i2c_data(I2C_DATA_HIGH);
250 * enable input
252 i2c_dir_in();
253 i2c_delay(CLOCK_HIGH_TIME/4);
255 * generate ACK clock pulse
257 i2c_clk(I2C_CLOCK_HIGH);
258 #if 0
260 * Use PORT PB instead of I2C
261 * for input. (I2C not working)
263 i2c_clk(1);
264 i2c_data(1);
266 * switch off I2C
268 i2c_data(1);
269 i2c_disable();
270 i2c_dir_in();
271 #endif
274 * now wait for ack
276 i2c_delay(CLOCK_HIGH_TIME/2);
278 * check for ack
280 if (i2c_getbit())
281 ack = 0;
282 i2c_delay(CLOCK_HIGH_TIME/2);
283 if (!ack) {
284 if (!i2c_getbit()) /* receiver pulld SDA low */
285 ack = 1;
286 i2c_delay(CLOCK_HIGH_TIME/2);
290 * our clock is high now, make sure data is low
291 * before we enable our output. If we keep data high
292 * and enable output, we would generate a stop condition.
294 #if 0
295 i2c_data(I2C_DATA_LOW);
298 * end clock pulse
300 i2c_enable();
301 i2c_dir_out();
302 #endif
303 i2c_clk(I2C_CLOCK_LOW);
304 i2c_delay(CLOCK_HIGH_TIME/4);
306 * enable output
308 i2c_dir_out();
310 * remove ACK clock pulse
312 i2c_data(I2C_DATA_HIGH);
313 i2c_delay(CLOCK_LOW_TIME/2);
314 return ack;
317 /*#---------------------------------------------------------------------------
319 *# FUNCTION NAME: I2C::sendAck
321 *# DESCRIPTION : Send ACK on received data
323 *#--------------------------------------------------------------------------*/
324 void
325 i2c_sendack(void)
328 * enable output
330 i2c_delay(CLOCK_LOW_TIME);
331 i2c_dir_out();
333 * set ack pulse high
335 i2c_data(I2C_DATA_LOW);
337 * generate clock pulse
339 i2c_delay(CLOCK_HIGH_TIME/6);
340 i2c_clk(I2C_CLOCK_HIGH);
341 i2c_delay(CLOCK_HIGH_TIME);
342 i2c_clk(I2C_CLOCK_LOW);
343 i2c_delay(CLOCK_LOW_TIME/6);
345 * reset data out
347 i2c_data(I2C_DATA_HIGH);
348 i2c_delay(CLOCK_LOW_TIME);
350 i2c_dir_in();
353 /*#---------------------------------------------------------------------------
355 *# FUNCTION NAME: i2c_sendnack
357 *# DESCRIPTION : Sends NACK on received data
359 *#--------------------------------------------------------------------------*/
360 void
361 i2c_sendnack(void)
364 * enable output
366 i2c_delay(CLOCK_LOW_TIME);
367 i2c_dir_out();
369 * set data high
371 i2c_data(I2C_DATA_HIGH);
373 * generate clock pulse
375 i2c_delay(CLOCK_HIGH_TIME/6);
376 i2c_clk(I2C_CLOCK_HIGH);
377 i2c_delay(CLOCK_HIGH_TIME);
378 i2c_clk(I2C_CLOCK_LOW);
379 i2c_delay(CLOCK_LOW_TIME);
381 i2c_dir_in();
384 /*#---------------------------------------------------------------------------
386 *# FUNCTION NAME: i2c_write
388 *# DESCRIPTION : Writes a value to an I2C device
390 *#--------------------------------------------------------------------------*/
392 i2c_write(unsigned char theSlave, void *data, size_t nbytes)
394 int error, cntr = 3;
395 unsigned char bytes_wrote = 0;
396 unsigned char value;
397 unsigned long flags;
399 spin_lock_irqsave(&i2c_lock, flags);
401 do {
402 error = 0;
404 i2c_start();
406 * send slave address
408 i2c_outbyte((theSlave & 0xfe));
410 * wait for ack
412 if (!i2c_getack())
413 error = 1;
415 * send data
417 for (bytes_wrote = 0; bytes_wrote < nbytes; bytes_wrote++) {
418 memcpy(&value, data + bytes_wrote, sizeof value);
419 i2c_outbyte(value);
421 * now it's time to wait for ack
423 if (!i2c_getack())
424 error |= 4;
427 * end byte stream
429 i2c_stop();
431 } while (error && cntr--);
433 i2c_delay(CLOCK_LOW_TIME);
435 spin_unlock_irqrestore(&i2c_lock, flags);
437 return -error;
440 /*#---------------------------------------------------------------------------
442 *# FUNCTION NAME: i2c_read
444 *# DESCRIPTION : Reads a value from an I2C device
446 *#--------------------------------------------------------------------------*/
448 i2c_read(unsigned char theSlave, void *data, size_t nbytes)
450 unsigned char b = 0;
451 unsigned char bytes_read = 0;
452 int error, cntr = 3;
453 unsigned long flags;
455 spin_lock_irqsave(&i2c_lock, flags);
457 do {
458 error = 0;
459 memset(data, 0, nbytes);
461 * generate start condition
463 i2c_start();
465 * send slave address
467 i2c_outbyte((theSlave | 0x01));
469 * wait for ack
471 if (!i2c_getack())
472 error = 1;
474 * fetch data
476 for (bytes_read = 0; bytes_read < nbytes; bytes_read++) {
477 b = i2c_inbyte();
478 memcpy(data + bytes_read, &b, sizeof b);
480 if (bytes_read < (nbytes - 1))
481 i2c_sendack();
484 * last received byte needs to be nacked
485 * instead of acked
487 i2c_sendnack();
489 * end sequence
491 i2c_stop();
492 } while (error && cntr--);
494 spin_unlock_irqrestore(&i2c_lock, flags);
496 return -error;
499 /*#---------------------------------------------------------------------------
501 *# FUNCTION NAME: i2c_writereg
503 *# DESCRIPTION : Writes a value to an I2C device
505 *#--------------------------------------------------------------------------*/
507 i2c_writereg(unsigned char theSlave, unsigned char theReg,
508 unsigned char theValue)
510 int error, cntr = 3;
511 unsigned long flags;
513 spin_lock_irqsave(&i2c_lock, flags);
515 do {
516 error = 0;
518 i2c_start();
520 * send slave address
522 i2c_outbyte((theSlave & 0xfe));
524 * wait for ack
526 if(!i2c_getack())
527 error = 1;
529 * now select register
531 i2c_dir_out();
532 i2c_outbyte(theReg);
534 * now it's time to wait for ack
536 if(!i2c_getack())
537 error |= 2;
539 * send register register data
541 i2c_outbyte(theValue);
543 * now it's time to wait for ack
545 if(!i2c_getack())
546 error |= 4;
548 * end byte stream
550 i2c_stop();
551 } while(error && cntr--);
553 i2c_delay(CLOCK_LOW_TIME);
555 spin_unlock_irqrestore(&i2c_lock, flags);
557 return -error;
560 /*#---------------------------------------------------------------------------
562 *# FUNCTION NAME: i2c_readreg
564 *# DESCRIPTION : Reads a value from the decoder registers.
566 *#--------------------------------------------------------------------------*/
567 unsigned char
568 i2c_readreg(unsigned char theSlave, unsigned char theReg)
570 unsigned char b = 0;
571 int error, cntr = 3;
572 unsigned long flags;
574 spin_lock_irqsave(&i2c_lock, flags);
576 do {
577 error = 0;
579 * generate start condition
581 i2c_start();
584 * send slave address
586 i2c_outbyte((theSlave & 0xfe));
588 * wait for ack
590 if(!i2c_getack())
591 error = 1;
593 * now select register
595 i2c_dir_out();
596 i2c_outbyte(theReg);
598 * now it's time to wait for ack
600 if(!i2c_getack())
601 error |= 2;
603 * repeat start condition
605 i2c_delay(CLOCK_LOW_TIME);
606 i2c_start();
608 * send slave address
610 i2c_outbyte(theSlave | 0x01);
612 * wait for ack
614 if(!i2c_getack())
615 error |= 4;
617 * fetch register
619 b = i2c_inbyte();
621 * last received byte needs to be nacked
622 * instead of acked
624 i2c_sendnack();
626 * end sequence
628 i2c_stop();
630 } while(error && cntr--);
632 spin_unlock_irqrestore(&i2c_lock, flags);
634 return b;
637 static int
638 i2c_open(struct inode *inode, struct file *filp)
640 cycle_kernel_lock();
641 return 0;
644 static int
645 i2c_release(struct inode *inode, struct file *filp)
647 return 0;
650 /* Main device API. ioctl's to write or read to/from i2c registers.
653 static int
654 i2c_ioctl(struct inode *inode, struct file *file,
655 unsigned int cmd, unsigned long arg)
657 if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
658 return -ENOTTY;
661 switch (_IOC_NR(cmd)) {
662 case I2C_WRITEREG:
663 /* write to an i2c slave */
664 D(printk("i2cw %d %d %d\n",
665 I2C_ARGSLAVE(arg),
666 I2C_ARGREG(arg),
667 I2C_ARGVALUE(arg)));
669 return i2c_writereg(I2C_ARGSLAVE(arg),
670 I2C_ARGREG(arg),
671 I2C_ARGVALUE(arg));
672 case I2C_READREG:
674 unsigned char val;
675 /* read from an i2c slave */
676 D(printk("i2cr %d %d ",
677 I2C_ARGSLAVE(arg),
678 I2C_ARGREG(arg)));
679 val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
680 D(printk("= %d\n", val));
681 return val;
683 default:
684 return -EINVAL;
688 return 0;
691 static const struct file_operations i2c_fops = {
692 .owner = THIS_MODULE,
693 .ioctl = i2c_ioctl,
694 .open = i2c_open,
695 .release = i2c_release,
698 static int __init i2c_init(void)
700 static int res;
701 static int first = 1;
703 if (!first)
704 return res;
706 first = 0;
708 /* Setup and enable the DATA and CLK pins */
710 res = crisv32_io_get_name(&cris_i2c_data,
711 CONFIG_ETRAX_V32_I2C_DATA_PORT);
712 if (res < 0)
713 return res;
715 res = crisv32_io_get_name(&cris_i2c_clk, CONFIG_ETRAX_V32_I2C_CLK_PORT);
716 crisv32_io_set_dir(&cris_i2c_clk, crisv32_io_dir_out);
718 return res;
722 static int __init i2c_register(void)
724 int res;
726 res = i2c_init();
727 if (res < 0)
728 return res;
730 /* register char device */
732 res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
733 if (res < 0) {
734 printk(KERN_ERR "i2c: couldn't get a major number.\n");
735 return res;
738 printk(KERN_INFO
739 "I2C driver v2.2, (c) 1999-2007 Axis Communications AB\n");
741 return 0;
743 /* this makes sure that i2c_init is called during boot */
744 module_init(i2c_register);
746 /****************** END OF FILE i2c.c ********************************/