mfd: remove duplicated #include from pcf50633
[linux/fpc-iii.git] / arch / cris / arch-v10 / drivers / i2c.c
blob7f656ae0b21d2e16d940ad22f81748d36f4730d6
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/slab.h>
18 #include <linux/smp_lock.h>
19 #include <linux/errno.h>
20 #include <linux/kernel.h>
21 #include <linux/fs.h>
22 #include <linux/string.h>
23 #include <linux/init.h>
25 #include <asm/etraxi2c.h>
27 #include <asm/system.h>
28 #include <arch/svinto.h>
29 #include <asm/io.h>
30 #include <asm/delay.h>
31 #include <arch/io_interface_mux.h>
33 #include "i2c.h"
35 /****************** I2C DEFINITION SECTION *************************/
37 #define D(x)
39 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
40 static const char i2c_name[] = "i2c";
42 #define CLOCK_LOW_TIME 8
43 #define CLOCK_HIGH_TIME 8
44 #define START_CONDITION_HOLD_TIME 8
45 #define STOP_CONDITION_HOLD_TIME 8
46 #define ENABLE_OUTPUT 0x01
47 #define ENABLE_INPUT 0x00
48 #define I2C_CLOCK_HIGH 1
49 #define I2C_CLOCK_LOW 0
50 #define I2C_DATA_HIGH 1
51 #define I2C_DATA_LOW 0
53 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
54 /* Use PB and not PB_I2C */
55 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
56 #define CONFIG_ETRAX_I2C_DATA_PORT 0
57 #endif
58 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
59 #define CONFIG_ETRAX_I2C_CLK_PORT 1
60 #endif
62 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
63 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
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() \
70 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1)
71 #define i2c_dir_in() \
72 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0)
74 /* control the i2c clock and data signals */
76 #define i2c_clk(x) \
77 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x)
78 #define i2c_data(x) \
79 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x)
81 /* read a bit from the i2c interface */
83 #define i2c_getbit() (((*R_PORT_PB_READ & (1 << SDABIT))) >> SDABIT)
85 #else
86 /* enable or disable the i2c interface */
88 #define i2c_enable() *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_en))
89 #define i2c_disable() *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_en))
91 /* enable or disable output-enable, to select output or input on the i2c bus */
93 #define i2c_dir_out() \
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, 1);
96 #define i2c_dir_in() \
97 *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
98 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0);
100 /* control the i2c clock and data signals */
102 #define i2c_clk(x) \
103 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
104 ~IO_MASK(R_PORT_PB_I2C, i2c_clk)) | IO_FIELD(R_PORT_PB_I2C, i2c_clk, (x))); \
105 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 1, x);
107 #define i2c_data(x) \
108 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
109 ~IO_MASK(R_PORT_PB_I2C, i2c_d)) | IO_FIELD(R_PORT_PB_I2C, i2c_d, (x))); \
110 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 0, x);
112 /* read a bit from the i2c interface */
114 #define i2c_getbit() (*R_PORT_PB_READ & 0x1)
115 #endif
117 /* use the kernels delay routine */
119 #define i2c_delay(usecs) udelay(usecs)
121 static DEFINE_SPINLOCK(i2c_lock); /* Protect directions etc */
123 /****************** FUNCTION DEFINITION SECTION *************************/
126 /* generate i2c start condition */
128 void
129 i2c_start(void)
132 * SCL=1 SDA=1
134 i2c_dir_out();
135 i2c_delay(CLOCK_HIGH_TIME/6);
136 i2c_data(I2C_DATA_HIGH);
137 i2c_clk(I2C_CLOCK_HIGH);
138 i2c_delay(CLOCK_HIGH_TIME);
140 * SCL=1 SDA=0
142 i2c_data(I2C_DATA_LOW);
143 i2c_delay(START_CONDITION_HOLD_TIME);
145 * SCL=0 SDA=0
147 i2c_clk(I2C_CLOCK_LOW);
148 i2c_delay(CLOCK_LOW_TIME);
151 /* generate i2c stop condition */
153 void
154 i2c_stop(void)
156 i2c_dir_out();
159 * SCL=0 SDA=0
161 i2c_clk(I2C_CLOCK_LOW);
162 i2c_data(I2C_DATA_LOW);
163 i2c_delay(CLOCK_LOW_TIME*2);
165 * SCL=1 SDA=0
167 i2c_clk(I2C_CLOCK_HIGH);
168 i2c_delay(CLOCK_HIGH_TIME*2);
170 * SCL=1 SDA=1
172 i2c_data(I2C_DATA_HIGH);
173 i2c_delay(STOP_CONDITION_HOLD_TIME);
175 i2c_dir_in();
178 /* write a byte to the i2c interface */
180 void
181 i2c_outbyte(unsigned char x)
183 int i;
185 i2c_dir_out();
187 for (i = 0; i < 8; i++) {
188 if (x & 0x80) {
189 i2c_data(I2C_DATA_HIGH);
190 } else {
191 i2c_data(I2C_DATA_LOW);
194 i2c_delay(CLOCK_LOW_TIME/2);
195 i2c_clk(I2C_CLOCK_HIGH);
196 i2c_delay(CLOCK_HIGH_TIME);
197 i2c_clk(I2C_CLOCK_LOW);
198 i2c_delay(CLOCK_LOW_TIME/2);
199 x <<= 1;
201 i2c_data(I2C_DATA_LOW);
202 i2c_delay(CLOCK_LOW_TIME/2);
205 * enable input
207 i2c_dir_in();
210 /* read a byte from the i2c interface */
212 unsigned char
213 i2c_inbyte(void)
215 unsigned char aBitByte = 0;
216 int i;
218 /* Switch off I2C to get bit */
219 i2c_disable();
220 i2c_dir_in();
221 i2c_delay(CLOCK_HIGH_TIME/2);
223 /* Get bit */
224 aBitByte |= i2c_getbit();
226 /* Enable I2C */
227 i2c_enable();
228 i2c_delay(CLOCK_LOW_TIME/2);
230 for (i = 1; i < 8; i++) {
231 aBitByte <<= 1;
232 /* Clock pulse */
233 i2c_clk(I2C_CLOCK_HIGH);
234 i2c_delay(CLOCK_HIGH_TIME);
235 i2c_clk(I2C_CLOCK_LOW);
236 i2c_delay(CLOCK_LOW_TIME);
238 /* Switch off I2C to get bit */
239 i2c_disable();
240 i2c_dir_in();
241 i2c_delay(CLOCK_HIGH_TIME/2);
243 /* Get bit */
244 aBitByte |= i2c_getbit();
246 /* Enable I2C */
247 i2c_enable();
248 i2c_delay(CLOCK_LOW_TIME/2);
250 i2c_clk(I2C_CLOCK_HIGH);
251 i2c_delay(CLOCK_HIGH_TIME);
254 * we leave the clock low, getbyte is usually followed
255 * by sendack/nack, they assume the clock to be low
257 i2c_clk(I2C_CLOCK_LOW);
258 return aBitByte;
261 /*#---------------------------------------------------------------------------
263 *# FUNCTION NAME: i2c_getack
265 *# DESCRIPTION : checks if ack was received from ic2
267 *#--------------------------------------------------------------------------*/
270 i2c_getack(void)
272 int ack = 1;
274 * enable output
276 i2c_dir_out();
278 * Release data bus by setting
279 * data high
281 i2c_data(I2C_DATA_HIGH);
283 * enable input
285 i2c_dir_in();
286 i2c_delay(CLOCK_HIGH_TIME/4);
288 * generate ACK clock pulse
290 i2c_clk(I2C_CLOCK_HIGH);
292 * Use PORT PB instead of I2C
293 * for input. (I2C not working)
295 i2c_clk(1);
296 i2c_data(1);
298 * switch off I2C
300 i2c_data(1);
301 i2c_disable();
302 i2c_dir_in();
304 * now wait for ack
306 i2c_delay(CLOCK_HIGH_TIME/2);
308 * check for ack
310 if(i2c_getbit())
311 ack = 0;
312 i2c_delay(CLOCK_HIGH_TIME/2);
313 if(!ack){
314 if(!i2c_getbit()) /* receiver pulld SDA low */
315 ack = 1;
316 i2c_delay(CLOCK_HIGH_TIME/2);
320 * our clock is high now, make sure data is low
321 * before we enable our output. If we keep data high
322 * and enable output, we would generate a stop condition.
324 i2c_data(I2C_DATA_LOW);
327 * end clock pulse
329 i2c_enable();
330 i2c_dir_out();
331 i2c_clk(I2C_CLOCK_LOW);
332 i2c_delay(CLOCK_HIGH_TIME/4);
334 * enable output
336 i2c_dir_out();
338 * remove ACK clock pulse
340 i2c_data(I2C_DATA_HIGH);
341 i2c_delay(CLOCK_LOW_TIME/2);
342 return ack;
345 /*#---------------------------------------------------------------------------
347 *# FUNCTION NAME: I2C::sendAck
349 *# DESCRIPTION : Send ACK on received data
351 *#--------------------------------------------------------------------------*/
352 void
353 i2c_sendack(void)
356 * enable output
358 i2c_delay(CLOCK_LOW_TIME);
359 i2c_dir_out();
361 * set ack pulse high
363 i2c_data(I2C_DATA_LOW);
365 * generate clock pulse
367 i2c_delay(CLOCK_HIGH_TIME/6);
368 i2c_clk(I2C_CLOCK_HIGH);
369 i2c_delay(CLOCK_HIGH_TIME);
370 i2c_clk(I2C_CLOCK_LOW);
371 i2c_delay(CLOCK_LOW_TIME/6);
373 * reset data out
375 i2c_data(I2C_DATA_HIGH);
376 i2c_delay(CLOCK_LOW_TIME);
378 i2c_dir_in();
381 /*#---------------------------------------------------------------------------
383 *# FUNCTION NAME: i2c_sendnack
385 *# DESCRIPTION : Sends NACK on received data
387 *#--------------------------------------------------------------------------*/
388 void
389 i2c_sendnack(void)
392 * enable output
394 i2c_delay(CLOCK_LOW_TIME);
395 i2c_dir_out();
397 * set data high
399 i2c_data(I2C_DATA_HIGH);
401 * generate clock pulse
403 i2c_delay(CLOCK_HIGH_TIME/6);
404 i2c_clk(I2C_CLOCK_HIGH);
405 i2c_delay(CLOCK_HIGH_TIME);
406 i2c_clk(I2C_CLOCK_LOW);
407 i2c_delay(CLOCK_LOW_TIME);
409 i2c_dir_in();
412 /*#---------------------------------------------------------------------------
414 *# FUNCTION NAME: i2c_writereg
416 *# DESCRIPTION : Writes a value to an I2C device
418 *#--------------------------------------------------------------------------*/
420 i2c_writereg(unsigned char theSlave, unsigned char theReg,
421 unsigned char theValue)
423 int error, cntr = 3;
424 unsigned long flags;
426 spin_lock(&i2c_lock);
428 do {
429 error = 0;
431 * we don't like to be interrupted
433 local_irq_save(flags);
435 i2c_start();
437 * send slave address
439 i2c_outbyte((theSlave & 0xfe));
441 * wait for ack
443 if(!i2c_getack())
444 error = 1;
446 * now select register
448 i2c_dir_out();
449 i2c_outbyte(theReg);
451 * now it's time to wait for ack
453 if(!i2c_getack())
454 error |= 2;
456 * send register register data
458 i2c_outbyte(theValue);
460 * now it's time to wait for ack
462 if(!i2c_getack())
463 error |= 4;
465 * end byte stream
467 i2c_stop();
469 * enable interrupt again
471 local_irq_restore(flags);
473 } while(error && cntr--);
475 i2c_delay(CLOCK_LOW_TIME);
477 spin_unlock(&i2c_lock);
479 return -error;
482 /*#---------------------------------------------------------------------------
484 *# FUNCTION NAME: i2c_readreg
486 *# DESCRIPTION : Reads a value from the decoder registers.
488 *#--------------------------------------------------------------------------*/
489 unsigned char
490 i2c_readreg(unsigned char theSlave, unsigned char theReg)
492 unsigned char b = 0;
493 int error, cntr = 3;
494 unsigned long flags;
496 spin_lock(&i2c_lock);
498 do {
499 error = 0;
501 * we don't like to be interrupted
503 local_irq_save(flags);
505 * generate start condition
507 i2c_start();
510 * send slave address
512 i2c_outbyte((theSlave & 0xfe));
514 * wait for ack
516 if(!i2c_getack())
517 error = 1;
519 * now select register
521 i2c_dir_out();
522 i2c_outbyte(theReg);
524 * now it's time to wait for ack
526 if(!i2c_getack())
527 error = 1;
529 * repeat start condition
531 i2c_delay(CLOCK_LOW_TIME);
532 i2c_start();
534 * send slave address
536 i2c_outbyte(theSlave | 0x01);
538 * wait for ack
540 if(!i2c_getack())
541 error = 1;
543 * fetch register
545 b = i2c_inbyte();
547 * last received byte needs to be nacked
548 * instead of acked
550 i2c_sendnack();
552 * end sequence
554 i2c_stop();
556 * enable interrupt again
558 local_irq_restore(flags);
560 } while(error && cntr--);
562 spin_unlock(&i2c_lock);
564 return b;
567 static int
568 i2c_open(struct inode *inode, struct file *filp)
570 cycle_kernel_lock();
571 return 0;
574 static int
575 i2c_release(struct inode *inode, struct file *filp)
577 return 0;
580 /* Main device API. ioctl's to write or read to/from i2c registers.
583 static int
584 i2c_ioctl(struct inode *inode, struct file *file,
585 unsigned int cmd, unsigned long arg)
587 if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
588 return -EINVAL;
591 switch (_IOC_NR(cmd)) {
592 case I2C_WRITEREG:
593 /* write to an i2c slave */
594 D(printk("i2cw %d %d %d\n",
595 I2C_ARGSLAVE(arg),
596 I2C_ARGREG(arg),
597 I2C_ARGVALUE(arg)));
599 return i2c_writereg(I2C_ARGSLAVE(arg),
600 I2C_ARGREG(arg),
601 I2C_ARGVALUE(arg));
602 case I2C_READREG:
604 unsigned char val;
605 /* read from an i2c slave */
606 D(printk("i2cr %d %d ",
607 I2C_ARGSLAVE(arg),
608 I2C_ARGREG(arg)));
609 val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
610 D(printk("= %d\n", val));
611 return val;
613 default:
614 return -EINVAL;
618 return 0;
621 static const struct file_operations i2c_fops = {
622 .owner = THIS_MODULE,
623 .ioctl = i2c_ioctl,
624 .open = i2c_open,
625 .release = i2c_release,
628 int __init
629 i2c_init(void)
631 static int res = 0;
632 static int first = 1;
634 if (!first) {
635 return res;
637 first = 0;
639 /* Setup and enable the Port B I2C interface */
641 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
642 if ((res = cris_request_io_interface(if_i2c, "I2C"))) {
643 printk(KERN_CRIT "i2c_init: Failed to get IO interface\n");
644 return res;
647 *R_PORT_PB_I2C = port_pb_i2c_shadow |=
648 IO_STATE(R_PORT_PB_I2C, i2c_en, on) |
649 IO_FIELD(R_PORT_PB_I2C, i2c_d, 1) |
650 IO_FIELD(R_PORT_PB_I2C, i2c_clk, 1) |
651 IO_STATE(R_PORT_PB_I2C, i2c_oe_, enable);
653 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir0);
654 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir1);
656 *R_PORT_PB_DIR = (port_pb_dir_shadow |=
657 IO_STATE(R_PORT_PB_DIR, dir0, input) |
658 IO_STATE(R_PORT_PB_DIR, dir1, output));
659 #else
660 if ((res = cris_io_interface_allocate_pins(if_i2c,
661 'b',
662 CONFIG_ETRAX_I2C_DATA_PORT,
663 CONFIG_ETRAX_I2C_DATA_PORT))) {
664 printk(KERN_WARNING "i2c_init: Failed to get IO pin for I2C data port\n");
665 return res;
666 } else if ((res = cris_io_interface_allocate_pins(if_i2c,
667 'b',
668 CONFIG_ETRAX_I2C_CLK_PORT,
669 CONFIG_ETRAX_I2C_CLK_PORT))) {
670 cris_io_interface_free_pins(if_i2c,
671 'b',
672 CONFIG_ETRAX_I2C_DATA_PORT,
673 CONFIG_ETRAX_I2C_DATA_PORT);
674 printk(KERN_WARNING "i2c_init: Failed to get IO pin for I2C clk port\n");
676 #endif
678 return res;
681 static int __init
682 i2c_register(void)
684 int res;
686 res = i2c_init();
687 if (res < 0)
688 return res;
689 res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
690 if(res < 0) {
691 printk(KERN_ERR "i2c: couldn't get a major number.\n");
692 return res;
695 printk(KERN_INFO "I2C driver v2.2, (c) 1999-2004 Axis Communications AB\n");
697 return 0;
700 /* this makes sure that i2c_register is called during boot */
702 module_init(i2c_register);
704 /****************** END OF FILE i2c.c ********************************/