x86/xen: resume timer irqs early
[linux/fpc-iii.git] / arch / cris / arch-v10 / drivers / i2c.c
blobb3d1f9ed1b9881730b3583bb03df58f2acc08229
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 *! (C) Copyright 1999-2007 Axis Communications AB, LUND, SWEDEN
11 *!***************************************************************************/
13 /****************** INCLUDE FILES SECTION ***********************************/
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/errno.h>
18 #include <linux/kernel.h>
19 #include <linux/fs.h>
20 #include <linux/string.h>
21 #include <linux/init.h>
23 #include <asm/etraxi2c.h>
25 #include <arch/svinto.h>
26 #include <asm/io.h>
27 #include <asm/delay.h>
28 #include <arch/io_interface_mux.h>
30 #include "i2c.h"
32 /****************** I2C DEFINITION SECTION *************************/
34 #define D(x)
36 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
37 static const char i2c_name[] = "i2c";
39 #define CLOCK_LOW_TIME 8
40 #define CLOCK_HIGH_TIME 8
41 #define START_CONDITION_HOLD_TIME 8
42 #define STOP_CONDITION_HOLD_TIME 8
43 #define ENABLE_OUTPUT 0x01
44 #define ENABLE_INPUT 0x00
45 #define I2C_CLOCK_HIGH 1
46 #define I2C_CLOCK_LOW 0
47 #define I2C_DATA_HIGH 1
48 #define I2C_DATA_LOW 0
50 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
51 /* Use PB and not PB_I2C */
52 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
53 #define CONFIG_ETRAX_I2C_DATA_PORT 0
54 #endif
55 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
56 #define CONFIG_ETRAX_I2C_CLK_PORT 1
57 #endif
59 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
60 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
61 #define i2c_enable()
62 #define i2c_disable()
64 /* enable or disable output-enable, to select output or input on the i2c bus */
66 #define i2c_dir_out() \
67 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1)
68 #define i2c_dir_in() \
69 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0)
71 /* control the i2c clock and data signals */
73 #define i2c_clk(x) \
74 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x)
75 #define i2c_data(x) \
76 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x)
78 /* read a bit from the i2c interface */
80 #define i2c_getbit() (((*R_PORT_PB_READ & (1 << SDABIT))) >> SDABIT)
82 #else
83 /* enable or disable the i2c interface */
85 #define i2c_enable() *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_en))
86 #define i2c_disable() *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_en))
88 /* enable or disable output-enable, to select output or input on the i2c bus */
90 #define i2c_dir_out() \
91 *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
92 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1);
93 #define i2c_dir_in() \
94 *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
95 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0);
97 /* control the i2c clock and data signals */
99 #define i2c_clk(x) \
100 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
101 ~IO_MASK(R_PORT_PB_I2C, i2c_clk)) | IO_FIELD(R_PORT_PB_I2C, i2c_clk, (x))); \
102 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 1, x);
104 #define i2c_data(x) \
105 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
106 ~IO_MASK(R_PORT_PB_I2C, i2c_d)) | IO_FIELD(R_PORT_PB_I2C, i2c_d, (x))); \
107 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 0, x);
109 /* read a bit from the i2c interface */
111 #define i2c_getbit() (*R_PORT_PB_READ & 0x1)
112 #endif
114 /* use the kernels delay routine */
116 #define i2c_delay(usecs) udelay(usecs)
118 static DEFINE_SPINLOCK(i2c_lock); /* Protect directions etc */
120 /****************** FUNCTION DEFINITION SECTION *************************/
123 /* generate i2c start condition */
125 void
126 i2c_start(void)
129 * SCL=1 SDA=1
131 i2c_dir_out();
132 i2c_delay(CLOCK_HIGH_TIME/6);
133 i2c_data(I2C_DATA_HIGH);
134 i2c_clk(I2C_CLOCK_HIGH);
135 i2c_delay(CLOCK_HIGH_TIME);
137 * SCL=1 SDA=0
139 i2c_data(I2C_DATA_LOW);
140 i2c_delay(START_CONDITION_HOLD_TIME);
142 * SCL=0 SDA=0
144 i2c_clk(I2C_CLOCK_LOW);
145 i2c_delay(CLOCK_LOW_TIME);
148 /* generate i2c stop condition */
150 void
151 i2c_stop(void)
153 i2c_dir_out();
156 * SCL=0 SDA=0
158 i2c_clk(I2C_CLOCK_LOW);
159 i2c_data(I2C_DATA_LOW);
160 i2c_delay(CLOCK_LOW_TIME*2);
162 * SCL=1 SDA=0
164 i2c_clk(I2C_CLOCK_HIGH);
165 i2c_delay(CLOCK_HIGH_TIME*2);
167 * SCL=1 SDA=1
169 i2c_data(I2C_DATA_HIGH);
170 i2c_delay(STOP_CONDITION_HOLD_TIME);
172 i2c_dir_in();
175 /* write a byte to the i2c interface */
177 void
178 i2c_outbyte(unsigned char x)
180 int i;
182 i2c_dir_out();
184 for (i = 0; i < 8; i++) {
185 if (x & 0x80) {
186 i2c_data(I2C_DATA_HIGH);
187 } else {
188 i2c_data(I2C_DATA_LOW);
191 i2c_delay(CLOCK_LOW_TIME/2);
192 i2c_clk(I2C_CLOCK_HIGH);
193 i2c_delay(CLOCK_HIGH_TIME);
194 i2c_clk(I2C_CLOCK_LOW);
195 i2c_delay(CLOCK_LOW_TIME/2);
196 x <<= 1;
198 i2c_data(I2C_DATA_LOW);
199 i2c_delay(CLOCK_LOW_TIME/2);
202 * enable input
204 i2c_dir_in();
207 /* read a byte from the i2c interface */
209 unsigned char
210 i2c_inbyte(void)
212 unsigned char aBitByte = 0;
213 int i;
215 /* Switch off I2C to get bit */
216 i2c_disable();
217 i2c_dir_in();
218 i2c_delay(CLOCK_HIGH_TIME/2);
220 /* Get bit */
221 aBitByte |= i2c_getbit();
223 /* Enable I2C */
224 i2c_enable();
225 i2c_delay(CLOCK_LOW_TIME/2);
227 for (i = 1; i < 8; i++) {
228 aBitByte <<= 1;
229 /* Clock pulse */
230 i2c_clk(I2C_CLOCK_HIGH);
231 i2c_delay(CLOCK_HIGH_TIME);
232 i2c_clk(I2C_CLOCK_LOW);
233 i2c_delay(CLOCK_LOW_TIME);
235 /* Switch off I2C to get bit */
236 i2c_disable();
237 i2c_dir_in();
238 i2c_delay(CLOCK_HIGH_TIME/2);
240 /* Get bit */
241 aBitByte |= i2c_getbit();
243 /* Enable I2C */
244 i2c_enable();
245 i2c_delay(CLOCK_LOW_TIME/2);
247 i2c_clk(I2C_CLOCK_HIGH);
248 i2c_delay(CLOCK_HIGH_TIME);
251 * we leave the clock low, getbyte is usually followed
252 * by sendack/nack, they assume the clock to be low
254 i2c_clk(I2C_CLOCK_LOW);
255 return aBitByte;
258 /*#---------------------------------------------------------------------------
260 *# FUNCTION NAME: i2c_getack
262 *# DESCRIPTION : checks if ack was received from ic2
264 *#--------------------------------------------------------------------------*/
267 i2c_getack(void)
269 int ack = 1;
271 * enable output
273 i2c_dir_out();
275 * Release data bus by setting
276 * data high
278 i2c_data(I2C_DATA_HIGH);
280 * enable input
282 i2c_dir_in();
283 i2c_delay(CLOCK_HIGH_TIME/4);
285 * generate ACK clock pulse
287 i2c_clk(I2C_CLOCK_HIGH);
289 * Use PORT PB instead of I2C
290 * for input. (I2C not working)
292 i2c_clk(1);
293 i2c_data(1);
295 * switch off I2C
297 i2c_data(1);
298 i2c_disable();
299 i2c_dir_in();
301 * now wait for ack
303 i2c_delay(CLOCK_HIGH_TIME/2);
305 * check for ack
307 if(i2c_getbit())
308 ack = 0;
309 i2c_delay(CLOCK_HIGH_TIME/2);
310 if(!ack){
311 if(!i2c_getbit()) /* receiver pulld SDA low */
312 ack = 1;
313 i2c_delay(CLOCK_HIGH_TIME/2);
317 * our clock is high now, make sure data is low
318 * before we enable our output. If we keep data high
319 * and enable output, we would generate a stop condition.
321 i2c_data(I2C_DATA_LOW);
324 * end clock pulse
326 i2c_enable();
327 i2c_dir_out();
328 i2c_clk(I2C_CLOCK_LOW);
329 i2c_delay(CLOCK_HIGH_TIME/4);
331 * enable output
333 i2c_dir_out();
335 * remove ACK clock pulse
337 i2c_data(I2C_DATA_HIGH);
338 i2c_delay(CLOCK_LOW_TIME/2);
339 return ack;
342 /*#---------------------------------------------------------------------------
344 *# FUNCTION NAME: I2C::sendAck
346 *# DESCRIPTION : Send ACK on received data
348 *#--------------------------------------------------------------------------*/
349 void
350 i2c_sendack(void)
353 * enable output
355 i2c_delay(CLOCK_LOW_TIME);
356 i2c_dir_out();
358 * set ack pulse high
360 i2c_data(I2C_DATA_LOW);
362 * generate clock pulse
364 i2c_delay(CLOCK_HIGH_TIME/6);
365 i2c_clk(I2C_CLOCK_HIGH);
366 i2c_delay(CLOCK_HIGH_TIME);
367 i2c_clk(I2C_CLOCK_LOW);
368 i2c_delay(CLOCK_LOW_TIME/6);
370 * reset data out
372 i2c_data(I2C_DATA_HIGH);
373 i2c_delay(CLOCK_LOW_TIME);
375 i2c_dir_in();
378 /*#---------------------------------------------------------------------------
380 *# FUNCTION NAME: i2c_sendnack
382 *# DESCRIPTION : Sends NACK on received data
384 *#--------------------------------------------------------------------------*/
385 void
386 i2c_sendnack(void)
389 * enable output
391 i2c_delay(CLOCK_LOW_TIME);
392 i2c_dir_out();
394 * set data high
396 i2c_data(I2C_DATA_HIGH);
398 * generate clock pulse
400 i2c_delay(CLOCK_HIGH_TIME/6);
401 i2c_clk(I2C_CLOCK_HIGH);
402 i2c_delay(CLOCK_HIGH_TIME);
403 i2c_clk(I2C_CLOCK_LOW);
404 i2c_delay(CLOCK_LOW_TIME);
406 i2c_dir_in();
409 /*#---------------------------------------------------------------------------
411 *# FUNCTION NAME: i2c_writereg
413 *# DESCRIPTION : Writes a value to an I2C device
415 *#--------------------------------------------------------------------------*/
417 i2c_writereg(unsigned char theSlave, unsigned char theReg,
418 unsigned char theValue)
420 int error, cntr = 3;
421 unsigned long flags;
423 spin_lock(&i2c_lock);
425 do {
426 error = 0;
428 * we don't like to be interrupted
430 local_irq_save(flags);
432 i2c_start();
434 * send slave address
436 i2c_outbyte((theSlave & 0xfe));
438 * wait for ack
440 if(!i2c_getack())
441 error = 1;
443 * now select register
445 i2c_dir_out();
446 i2c_outbyte(theReg);
448 * now it's time to wait for ack
450 if(!i2c_getack())
451 error |= 2;
453 * send register register data
455 i2c_outbyte(theValue);
457 * now it's time to wait for ack
459 if(!i2c_getack())
460 error |= 4;
462 * end byte stream
464 i2c_stop();
466 * enable interrupt again
468 local_irq_restore(flags);
470 } while(error && cntr--);
472 i2c_delay(CLOCK_LOW_TIME);
474 spin_unlock(&i2c_lock);
476 return -error;
479 /*#---------------------------------------------------------------------------
481 *# FUNCTION NAME: i2c_readreg
483 *# DESCRIPTION : Reads a value from the decoder registers.
485 *#--------------------------------------------------------------------------*/
486 unsigned char
487 i2c_readreg(unsigned char theSlave, unsigned char theReg)
489 unsigned char b = 0;
490 int error, cntr = 3;
491 unsigned long flags;
493 spin_lock(&i2c_lock);
495 do {
496 error = 0;
498 * we don't like to be interrupted
500 local_irq_save(flags);
502 * generate start condition
504 i2c_start();
507 * send slave address
509 i2c_outbyte((theSlave & 0xfe));
511 * wait for ack
513 if(!i2c_getack())
514 error = 1;
516 * now select register
518 i2c_dir_out();
519 i2c_outbyte(theReg);
521 * now it's time to wait for ack
523 if(!i2c_getack())
524 error = 1;
526 * repeat start condition
528 i2c_delay(CLOCK_LOW_TIME);
529 i2c_start();
531 * send slave address
533 i2c_outbyte(theSlave | 0x01);
535 * wait for ack
537 if(!i2c_getack())
538 error = 1;
540 * fetch register
542 b = i2c_inbyte();
544 * last received byte needs to be nacked
545 * instead of acked
547 i2c_sendnack();
549 * end sequence
551 i2c_stop();
553 * enable interrupt again
555 local_irq_restore(flags);
557 } while(error && cntr--);
559 spin_unlock(&i2c_lock);
561 return b;
564 static int
565 i2c_open(struct inode *inode, struct file *filp)
567 return 0;
570 static int
571 i2c_release(struct inode *inode, struct file *filp)
573 return 0;
576 /* Main device API. ioctl's to write or read to/from i2c registers.
579 static long i2c_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
581 if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
582 return -EINVAL;
585 switch (_IOC_NR(cmd)) {
586 case I2C_WRITEREG:
587 /* write to an i2c slave */
588 D(printk(KERN_DEBUG "i2cw %d %d %d\n",
589 I2C_ARGSLAVE(arg),
590 I2C_ARGREG(arg),
591 I2C_ARGVALUE(arg)));
593 return i2c_writereg(I2C_ARGSLAVE(arg),
594 I2C_ARGREG(arg),
595 I2C_ARGVALUE(arg));
596 case I2C_READREG:
598 unsigned char val;
599 /* read from an i2c slave */
600 D(printk(KERN_DEBUG "i2cr %d %d ",
601 I2C_ARGSLAVE(arg),
602 I2C_ARGREG(arg)));
603 val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
604 D(printk(KERN_DEBUG "= %d\n", val));
605 return val;
607 default:
608 return -EINVAL;
611 return 0;
614 static const struct file_operations i2c_fops = {
615 .owner = THIS_MODULE,
616 .unlocked_ioctl = i2c_ioctl,
617 .open = i2c_open,
618 .release = i2c_release,
619 .llseek = noop_llseek,
622 int __init
623 i2c_init(void)
625 static int res = 0;
626 static int first = 1;
628 if (!first) {
629 return res;
631 first = 0;
633 /* Setup and enable the Port B I2C interface */
635 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
636 if ((res = cris_request_io_interface(if_i2c, "I2C"))) {
637 printk(KERN_CRIT "i2c_init: Failed to get IO interface\n");
638 return res;
641 *R_PORT_PB_I2C = port_pb_i2c_shadow |=
642 IO_STATE(R_PORT_PB_I2C, i2c_en, on) |
643 IO_FIELD(R_PORT_PB_I2C, i2c_d, 1) |
644 IO_FIELD(R_PORT_PB_I2C, i2c_clk, 1) |
645 IO_STATE(R_PORT_PB_I2C, i2c_oe_, enable);
647 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir0);
648 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir1);
650 *R_PORT_PB_DIR = (port_pb_dir_shadow |=
651 IO_STATE(R_PORT_PB_DIR, dir0, input) |
652 IO_STATE(R_PORT_PB_DIR, dir1, output));
653 #else
654 if ((res = cris_io_interface_allocate_pins(if_i2c,
655 'b',
656 CONFIG_ETRAX_I2C_DATA_PORT,
657 CONFIG_ETRAX_I2C_DATA_PORT))) {
658 printk(KERN_WARNING "i2c_init: Failed to get IO pin for I2C data port\n");
659 return res;
660 } else if ((res = cris_io_interface_allocate_pins(if_i2c,
661 'b',
662 CONFIG_ETRAX_I2C_CLK_PORT,
663 CONFIG_ETRAX_I2C_CLK_PORT))) {
664 cris_io_interface_free_pins(if_i2c,
665 'b',
666 CONFIG_ETRAX_I2C_DATA_PORT,
667 CONFIG_ETRAX_I2C_DATA_PORT);
668 printk(KERN_WARNING "i2c_init: Failed to get IO pin for I2C clk port\n");
670 #endif
672 return res;
675 static int __init
676 i2c_register(void)
678 int res;
680 res = i2c_init();
681 if (res < 0)
682 return res;
683 res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
684 if(res < 0) {
685 printk(KERN_ERR "i2c: couldn't get a major number.\n");
686 return res;
689 printk(KERN_INFO "I2C driver v2.2, (c) 1999-2004 Axis Communications AB\n");
691 return 0;
694 /* this makes sure that i2c_register is called during boot */
696 module_init(i2c_register);
698 /****************** END OF FILE i2c.c ********************************/