I2C: TWL4030: Kconfig and Makefile changes
[linux-ginger.git] / arch / cris / arch-v10 / drivers / i2c.c
blobd6d22067d0c80529733a4d8818a04ad120e62e59
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/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/fs.h>
21 #include <linux/string.h>
22 #include <linux/init.h>
24 #include <asm/etraxi2c.h>
26 #include <asm/system.h>
27 #include <asm/arch/svinto.h>
28 #include <asm/io.h>
29 #include <asm/delay.h>
30 #include <asm/arch/io_interface_mux.h>
32 #include "i2c.h"
34 /****************** I2C DEFINITION SECTION *************************/
36 #define D(x)
38 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
39 static const char i2c_name[] = "i2c";
41 #define CLOCK_LOW_TIME 8
42 #define CLOCK_HIGH_TIME 8
43 #define START_CONDITION_HOLD_TIME 8
44 #define STOP_CONDITION_HOLD_TIME 8
45 #define ENABLE_OUTPUT 0x01
46 #define ENABLE_INPUT 0x00
47 #define I2C_CLOCK_HIGH 1
48 #define I2C_CLOCK_LOW 0
49 #define I2C_DATA_HIGH 1
50 #define I2C_DATA_LOW 0
52 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
53 /* Use PB and not PB_I2C */
54 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
55 #define CONFIG_ETRAX_I2C_DATA_PORT 0
56 #endif
57 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
58 #define CONFIG_ETRAX_I2C_CLK_PORT 1
59 #endif
61 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
62 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
63 #define i2c_enable()
64 #define i2c_disable()
66 /* enable or disable output-enable, to select output or input on the i2c bus */
68 #define i2c_dir_out() \
69 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1)
70 #define i2c_dir_in() \
71 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0)
73 /* control the i2c clock and data signals */
75 #define i2c_clk(x) \
76 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x)
77 #define i2c_data(x) \
78 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x)
80 /* read a bit from the i2c interface */
82 #define i2c_getbit() (((*R_PORT_PB_READ & (1 << SDABIT))) >> SDABIT)
84 #else
85 /* enable or disable the i2c interface */
87 #define i2c_enable() *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_en))
88 #define i2c_disable() *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_en))
90 /* enable or disable output-enable, to select output or input on the i2c bus */
92 #define i2c_dir_out() \
93 *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
94 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1);
95 #define i2c_dir_in() \
96 *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
97 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0);
99 /* control the i2c clock and data signals */
101 #define i2c_clk(x) \
102 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
103 ~IO_MASK(R_PORT_PB_I2C, i2c_clk)) | IO_FIELD(R_PORT_PB_I2C, i2c_clk, (x))); \
104 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 1, x);
106 #define i2c_data(x) \
107 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
108 ~IO_MASK(R_PORT_PB_I2C, i2c_d)) | IO_FIELD(R_PORT_PB_I2C, i2c_d, (x))); \
109 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 0, x);
111 /* read a bit from the i2c interface */
113 #define i2c_getbit() (*R_PORT_PB_READ & 0x1)
114 #endif
116 /* use the kernels delay routine */
118 #define i2c_delay(usecs) udelay(usecs)
120 static DEFINE_SPINLOCK(i2c_lock); /* Protect directions etc */
122 /****************** FUNCTION DEFINITION SECTION *************************/
125 /* generate i2c start condition */
127 void
128 i2c_start(void)
131 * SCL=1 SDA=1
133 i2c_dir_out();
134 i2c_delay(CLOCK_HIGH_TIME/6);
135 i2c_data(I2C_DATA_HIGH);
136 i2c_clk(I2C_CLOCK_HIGH);
137 i2c_delay(CLOCK_HIGH_TIME);
139 * SCL=1 SDA=0
141 i2c_data(I2C_DATA_LOW);
142 i2c_delay(START_CONDITION_HOLD_TIME);
144 * SCL=0 SDA=0
146 i2c_clk(I2C_CLOCK_LOW);
147 i2c_delay(CLOCK_LOW_TIME);
150 /* generate i2c stop condition */
152 void
153 i2c_stop(void)
155 i2c_dir_out();
158 * SCL=0 SDA=0
160 i2c_clk(I2C_CLOCK_LOW);
161 i2c_data(I2C_DATA_LOW);
162 i2c_delay(CLOCK_LOW_TIME*2);
164 * SCL=1 SDA=0
166 i2c_clk(I2C_CLOCK_HIGH);
167 i2c_delay(CLOCK_HIGH_TIME*2);
169 * SCL=1 SDA=1
171 i2c_data(I2C_DATA_HIGH);
172 i2c_delay(STOP_CONDITION_HOLD_TIME);
174 i2c_dir_in();
177 /* write a byte to the i2c interface */
179 void
180 i2c_outbyte(unsigned char x)
182 int i;
184 i2c_dir_out();
186 for (i = 0; i < 8; i++) {
187 if (x & 0x80) {
188 i2c_data(I2C_DATA_HIGH);
189 } else {
190 i2c_data(I2C_DATA_LOW);
193 i2c_delay(CLOCK_LOW_TIME/2);
194 i2c_clk(I2C_CLOCK_HIGH);
195 i2c_delay(CLOCK_HIGH_TIME);
196 i2c_clk(I2C_CLOCK_LOW);
197 i2c_delay(CLOCK_LOW_TIME/2);
198 x <<= 1;
200 i2c_data(I2C_DATA_LOW);
201 i2c_delay(CLOCK_LOW_TIME/2);
204 * enable input
206 i2c_dir_in();
209 /* read a byte from the i2c interface */
211 unsigned char
212 i2c_inbyte(void)
214 unsigned char aBitByte = 0;
215 int i;
217 /* Switch off I2C to get bit */
218 i2c_disable();
219 i2c_dir_in();
220 i2c_delay(CLOCK_HIGH_TIME/2);
222 /* Get bit */
223 aBitByte |= i2c_getbit();
225 /* Enable I2C */
226 i2c_enable();
227 i2c_delay(CLOCK_LOW_TIME/2);
229 for (i = 1; i < 8; i++) {
230 aBitByte <<= 1;
231 /* Clock pulse */
232 i2c_clk(I2C_CLOCK_HIGH);
233 i2c_delay(CLOCK_HIGH_TIME);
234 i2c_clk(I2C_CLOCK_LOW);
235 i2c_delay(CLOCK_LOW_TIME);
237 /* Switch off I2C to get bit */
238 i2c_disable();
239 i2c_dir_in();
240 i2c_delay(CLOCK_HIGH_TIME/2);
242 /* Get bit */
243 aBitByte |= i2c_getbit();
245 /* Enable I2C */
246 i2c_enable();
247 i2c_delay(CLOCK_LOW_TIME/2);
249 i2c_clk(I2C_CLOCK_HIGH);
250 i2c_delay(CLOCK_HIGH_TIME);
253 * we leave the clock low, getbyte is usually followed
254 * by sendack/nack, they assume the clock to be low
256 i2c_clk(I2C_CLOCK_LOW);
257 return aBitByte;
260 /*#---------------------------------------------------------------------------
262 *# FUNCTION NAME: i2c_getack
264 *# DESCRIPTION : checks if ack was received from ic2
266 *#--------------------------------------------------------------------------*/
269 i2c_getack(void)
271 int ack = 1;
273 * enable output
275 i2c_dir_out();
277 * Release data bus by setting
278 * data high
280 i2c_data(I2C_DATA_HIGH);
282 * enable input
284 i2c_dir_in();
285 i2c_delay(CLOCK_HIGH_TIME/4);
287 * generate ACK clock pulse
289 i2c_clk(I2C_CLOCK_HIGH);
291 * Use PORT PB instead of I2C
292 * for input. (I2C not working)
294 i2c_clk(1);
295 i2c_data(1);
297 * switch off I2C
299 i2c_data(1);
300 i2c_disable();
301 i2c_dir_in();
303 * now wait for ack
305 i2c_delay(CLOCK_HIGH_TIME/2);
307 * check for ack
309 if(i2c_getbit())
310 ack = 0;
311 i2c_delay(CLOCK_HIGH_TIME/2);
312 if(!ack){
313 if(!i2c_getbit()) /* receiver pulld SDA low */
314 ack = 1;
315 i2c_delay(CLOCK_HIGH_TIME/2);
319 * our clock is high now, make sure data is low
320 * before we enable our output. If we keep data high
321 * and enable output, we would generate a stop condition.
323 i2c_data(I2C_DATA_LOW);
326 * end clock pulse
328 i2c_enable();
329 i2c_dir_out();
330 i2c_clk(I2C_CLOCK_LOW);
331 i2c_delay(CLOCK_HIGH_TIME/4);
333 * enable output
335 i2c_dir_out();
337 * remove ACK clock pulse
339 i2c_data(I2C_DATA_HIGH);
340 i2c_delay(CLOCK_LOW_TIME/2);
341 return ack;
344 /*#---------------------------------------------------------------------------
346 *# FUNCTION NAME: I2C::sendAck
348 *# DESCRIPTION : Send ACK on received data
350 *#--------------------------------------------------------------------------*/
351 void
352 i2c_sendack(void)
355 * enable output
357 i2c_delay(CLOCK_LOW_TIME);
358 i2c_dir_out();
360 * set ack pulse high
362 i2c_data(I2C_DATA_LOW);
364 * generate clock pulse
366 i2c_delay(CLOCK_HIGH_TIME/6);
367 i2c_clk(I2C_CLOCK_HIGH);
368 i2c_delay(CLOCK_HIGH_TIME);
369 i2c_clk(I2C_CLOCK_LOW);
370 i2c_delay(CLOCK_LOW_TIME/6);
372 * reset data out
374 i2c_data(I2C_DATA_HIGH);
375 i2c_delay(CLOCK_LOW_TIME);
377 i2c_dir_in();
380 /*#---------------------------------------------------------------------------
382 *# FUNCTION NAME: i2c_sendnack
384 *# DESCRIPTION : Sends NACK on received data
386 *#--------------------------------------------------------------------------*/
387 void
388 i2c_sendnack(void)
391 * enable output
393 i2c_delay(CLOCK_LOW_TIME);
394 i2c_dir_out();
396 * set data high
398 i2c_data(I2C_DATA_HIGH);
400 * generate clock pulse
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);
408 i2c_dir_in();
411 /*#---------------------------------------------------------------------------
413 *# FUNCTION NAME: i2c_writereg
415 *# DESCRIPTION : Writes a value to an I2C device
417 *#--------------------------------------------------------------------------*/
419 i2c_writereg(unsigned char theSlave, unsigned char theReg,
420 unsigned char theValue)
422 int error, cntr = 3;
423 unsigned long flags;
425 spin_lock(&i2c_lock);
427 do {
428 error = 0;
430 * we don't like to be interrupted
432 local_irq_save(flags);
434 i2c_start();
436 * send slave address
438 i2c_outbyte((theSlave & 0xfe));
440 * wait for ack
442 if(!i2c_getack())
443 error = 1;
445 * now select register
447 i2c_dir_out();
448 i2c_outbyte(theReg);
450 * now it's time to wait for ack
452 if(!i2c_getack())
453 error |= 2;
455 * send register register data
457 i2c_outbyte(theValue);
459 * now it's time to wait for ack
461 if(!i2c_getack())
462 error |= 4;
464 * end byte stream
466 i2c_stop();
468 * enable interrupt again
470 local_irq_restore(flags);
472 } while(error && cntr--);
474 i2c_delay(CLOCK_LOW_TIME);
476 spin_unlock(&i2c_lock);
478 return -error;
481 /*#---------------------------------------------------------------------------
483 *# FUNCTION NAME: i2c_readreg
485 *# DESCRIPTION : Reads a value from the decoder registers.
487 *#--------------------------------------------------------------------------*/
488 unsigned char
489 i2c_readreg(unsigned char theSlave, unsigned char theReg)
491 unsigned char b = 0;
492 int error, cntr = 3;
493 unsigned long flags;
495 spin_lock(&i2c_lock);
497 do {
498 error = 0;
500 * we don't like to be interrupted
502 local_irq_save(flags);
504 * generate start condition
506 i2c_start();
509 * send slave address
511 i2c_outbyte((theSlave & 0xfe));
513 * wait for ack
515 if(!i2c_getack())
516 error = 1;
518 * now select register
520 i2c_dir_out();
521 i2c_outbyte(theReg);
523 * now it's time to wait for ack
525 if(!i2c_getack())
526 error = 1;
528 * repeat start condition
530 i2c_delay(CLOCK_LOW_TIME);
531 i2c_start();
533 * send slave address
535 i2c_outbyte(theSlave | 0x01);
537 * wait for ack
539 if(!i2c_getack())
540 error = 1;
542 * fetch register
544 b = i2c_inbyte();
546 * last received byte needs to be nacked
547 * instead of acked
549 i2c_sendnack();
551 * end sequence
553 i2c_stop();
555 * enable interrupt again
557 local_irq_restore(flags);
559 } while(error && cntr--);
561 spin_unlock(&i2c_lock);
563 return b;
566 static int
567 i2c_open(struct inode *inode, struct file *filp)
569 return 0;
572 static int
573 i2c_release(struct inode *inode, struct file *filp)
575 return 0;
578 /* Main device API. ioctl's to write or read to/from i2c registers.
581 static int
582 i2c_ioctl(struct inode *inode, struct file *file,
583 unsigned int cmd, unsigned long arg)
585 if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
586 return -EINVAL;
589 switch (_IOC_NR(cmd)) {
590 case I2C_WRITEREG:
591 /* write to an i2c slave */
592 D(printk("i2cw %d %d %d\n",
593 I2C_ARGSLAVE(arg),
594 I2C_ARGREG(arg),
595 I2C_ARGVALUE(arg)));
597 return i2c_writereg(I2C_ARGSLAVE(arg),
598 I2C_ARGREG(arg),
599 I2C_ARGVALUE(arg));
600 case I2C_READREG:
602 unsigned char val;
603 /* read from an i2c slave */
604 D(printk("i2cr %d %d ",
605 I2C_ARGSLAVE(arg),
606 I2C_ARGREG(arg)));
607 val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
608 D(printk("= %d\n", val));
609 return val;
611 default:
612 return -EINVAL;
616 return 0;
619 static const struct file_operations i2c_fops = {
620 .owner = THIS_MODULE,
621 .ioctl = i2c_ioctl,
622 .open = i2c_open,
623 .release = i2c_release,
626 int __init
627 i2c_init(void)
629 static int res = 0;
630 static int first = 1;
632 if (!first) {
633 return res;
635 first = 0;
637 /* Setup and enable the Port B I2C interface */
639 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
640 if ((res = cris_request_io_interface(if_i2c, "I2C"))) {
641 printk(KERN_CRIT "i2c_init: Failed to get IO interface\n");
642 return res;
645 *R_PORT_PB_I2C = port_pb_i2c_shadow |=
646 IO_STATE(R_PORT_PB_I2C, i2c_en, on) |
647 IO_FIELD(R_PORT_PB_I2C, i2c_d, 1) |
648 IO_FIELD(R_PORT_PB_I2C, i2c_clk, 1) |
649 IO_STATE(R_PORT_PB_I2C, i2c_oe_, enable);
651 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir0);
652 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir1);
654 *R_PORT_PB_DIR = (port_pb_dir_shadow |=
655 IO_STATE(R_PORT_PB_DIR, dir0, input) |
656 IO_STATE(R_PORT_PB_DIR, dir1, output));
657 #else
658 if ((res = cris_io_interface_allocate_pins(if_i2c,
659 'b',
660 CONFIG_ETRAX_I2C_DATA_PORT,
661 CONFIG_ETRAX_I2C_DATA_PORT))) {
662 printk(KERN_WARNING "i2c_init: Failed to get IO pin for I2C data port\n");
663 return res;
664 } else if ((res = cris_io_interface_allocate_pins(if_i2c,
665 'b',
666 CONFIG_ETRAX_I2C_CLK_PORT,
667 CONFIG_ETRAX_I2C_CLK_PORT))) {
668 cris_io_interface_free_pins(if_i2c,
669 'b',
670 CONFIG_ETRAX_I2C_DATA_PORT,
671 CONFIG_ETRAX_I2C_DATA_PORT);
672 printk(KERN_WARNING "i2c_init: Failed to get IO pin for I2C clk port\n");
674 #endif
676 return res;
679 static int __init
680 i2c_register(void)
682 int res;
684 res = i2c_init();
685 if (res < 0)
686 return res;
687 res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
688 if(res < 0) {
689 printk(KERN_ERR "i2c: couldn't get a major number.\n");
690 return res;
693 printk(KERN_INFO "I2C driver v2.2, (c) 1999-2004 Axis Communications AB\n");
695 return 0;
698 /* this makes sure that i2c_register is called during boot */
700 module_init(i2c_register);
702 /****************** END OF FILE i2c.c ********************************/