1 /*!***************************************************************************
5 *! DESCRIPTION: implements an interface for IIC/I2C, both directly from other
6 *! kernel modules (i2c_writereg/readreg) and from userspace using
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,
14 *| June 23 2003 Pieter Grimmerink Added 'i2c_sendnack'. i2c_readreg now
15 *| generates nack on last received byte,
17 *| i2c_getack changed data level while clock
18 *| was high, causing DS75 to see a stop condition
20 *! ---------------------------------------------------------------------------
22 *! (C) Copyright 1999-2002 Axis Communications AB, LUND, SWEDEN
24 *!***************************************************************************/
25 /* $Id: i2c.c,v 1.2 2005/05/09 15:29:49 starvik Exp $ */
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>
34 #include <linux/string.h>
35 #include <linux/init.h>
37 #include <asm/etraxi2c.h>
39 #include <asm/system.h>
41 #include <asm/delay.h>
45 /****************** I2C DEFINITION SECTION *************************/
49 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
50 static const char i2c_name[] = "i2c";
52 #define CLOCK_LOW_TIME 8
53 #define CLOCK_HIGH_TIME 8
54 #define START_CONDITION_HOLD_TIME 8
55 #define STOP_CONDITION_HOLD_TIME 8
56 #define ENABLE_OUTPUT 0x01
57 #define ENABLE_INPUT 0x00
58 #define I2C_CLOCK_HIGH 1
59 #define I2C_CLOCK_LOW 0
60 #define I2C_DATA_HIGH 1
61 #define I2C_DATA_LOW 0
66 /* enable or disable output-enable, to select output or input on the i2c bus */
68 #define i2c_dir_out() crisv32_io_set_dir(&cris_i2c_data, crisv32_io_dir_out)
69 #define i2c_dir_in() crisv32_io_set_dir(&cris_i2c_data, crisv32_io_dir_in)
71 /* control the i2c clock and data signals */
73 #define i2c_clk(x) crisv32_io_set(&cris_i2c_clk, x)
74 #define i2c_data(x) crisv32_io_set(&cris_i2c_data, x)
76 /* read a bit from the i2c interface */
78 #define i2c_getbit() crisv32_io_rd(&cris_i2c_data)
80 #define i2c_delay(usecs) udelay(usecs)
82 /****************** VARIABLE SECTION ************************************/
84 static struct crisv32_iopin cris_i2c_clk;
85 static struct crisv32_iopin cris_i2c_data;
87 /****************** FUNCTION DEFINITION SECTION *************************/
90 /* generate i2c start condition */
99 i2c_delay(CLOCK_HIGH_TIME/6);
100 i2c_data(I2C_DATA_HIGH);
101 i2c_clk(I2C_CLOCK_HIGH);
102 i2c_delay(CLOCK_HIGH_TIME);
106 i2c_data(I2C_DATA_LOW);
107 i2c_delay(START_CONDITION_HOLD_TIME);
111 i2c_clk(I2C_CLOCK_LOW);
112 i2c_delay(CLOCK_LOW_TIME);
115 /* generate i2c stop condition */
125 i2c_clk(I2C_CLOCK_LOW);
126 i2c_data(I2C_DATA_LOW);
127 i2c_delay(CLOCK_LOW_TIME*2);
131 i2c_clk(I2C_CLOCK_HIGH);
132 i2c_delay(CLOCK_HIGH_TIME*2);
136 i2c_data(I2C_DATA_HIGH);
137 i2c_delay(STOP_CONDITION_HOLD_TIME);
142 /* write a byte to the i2c interface */
145 i2c_outbyte(unsigned char x)
151 for (i = 0; i < 8; i++) {
153 i2c_data(I2C_DATA_HIGH);
155 i2c_data(I2C_DATA_LOW);
158 i2c_delay(CLOCK_LOW_TIME/2);
159 i2c_clk(I2C_CLOCK_HIGH);
160 i2c_delay(CLOCK_HIGH_TIME);
161 i2c_clk(I2C_CLOCK_LOW);
162 i2c_delay(CLOCK_LOW_TIME/2);
165 i2c_data(I2C_DATA_LOW);
166 i2c_delay(CLOCK_LOW_TIME/2);
174 /* read a byte from the i2c interface */
179 unsigned char aBitByte = 0;
182 /* Switch off I2C to get bit */
185 i2c_delay(CLOCK_HIGH_TIME/2);
188 aBitByte |= i2c_getbit();
192 i2c_delay(CLOCK_LOW_TIME/2);
194 for (i = 1; i < 8; i++) {
197 i2c_clk(I2C_CLOCK_HIGH);
198 i2c_delay(CLOCK_HIGH_TIME);
199 i2c_clk(I2C_CLOCK_LOW);
200 i2c_delay(CLOCK_LOW_TIME);
202 /* Switch off I2C to get bit */
205 i2c_delay(CLOCK_HIGH_TIME/2);
208 aBitByte |= i2c_getbit();
212 i2c_delay(CLOCK_LOW_TIME/2);
214 i2c_clk(I2C_CLOCK_HIGH);
215 i2c_delay(CLOCK_HIGH_TIME);
218 * we leave the clock low, getbyte is usually followed
219 * by sendack/nack, they assume the clock to be low
221 i2c_clk(I2C_CLOCK_LOW);
225 /*#---------------------------------------------------------------------------
227 *# FUNCTION NAME: i2c_getack
229 *# DESCRIPTION : checks if ack was received from ic2
231 *#--------------------------------------------------------------------------*/
242 * Release data bus by setting
245 i2c_data(I2C_DATA_HIGH);
250 i2c_delay(CLOCK_HIGH_TIME/4);
252 * generate ACK clock pulse
254 i2c_clk(I2C_CLOCK_HIGH);
256 * Use PORT PB instead of I2C
257 * for input. (I2C not working)
270 i2c_delay(CLOCK_HIGH_TIME/2);
276 i2c_delay(CLOCK_HIGH_TIME/2);
278 if(!i2c_getbit()) /* receiver pulld SDA low */
280 i2c_delay(CLOCK_HIGH_TIME/2);
284 * our clock is high now, make sure data is low
285 * before we enable our output. If we keep data high
286 * and enable output, we would generate a stop condition.
288 i2c_data(I2C_DATA_LOW);
295 i2c_clk(I2C_CLOCK_LOW);
296 i2c_delay(CLOCK_HIGH_TIME/4);
302 * remove ACK clock pulse
304 i2c_data(I2C_DATA_HIGH);
305 i2c_delay(CLOCK_LOW_TIME/2);
309 /*#---------------------------------------------------------------------------
311 *# FUNCTION NAME: I2C::sendAck
313 *# DESCRIPTION : Send ACK on received data
315 *#--------------------------------------------------------------------------*/
322 i2c_delay(CLOCK_LOW_TIME);
327 i2c_data(I2C_DATA_LOW);
329 * generate clock pulse
331 i2c_delay(CLOCK_HIGH_TIME/6);
332 i2c_clk(I2C_CLOCK_HIGH);
333 i2c_delay(CLOCK_HIGH_TIME);
334 i2c_clk(I2C_CLOCK_LOW);
335 i2c_delay(CLOCK_LOW_TIME/6);
339 i2c_data(I2C_DATA_HIGH);
340 i2c_delay(CLOCK_LOW_TIME);
345 /*#---------------------------------------------------------------------------
347 *# FUNCTION NAME: i2c_sendnack
349 *# DESCRIPTION : Sends NACK on received data
351 *#--------------------------------------------------------------------------*/
358 i2c_delay(CLOCK_LOW_TIME);
363 i2c_data(I2C_DATA_HIGH);
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);
376 /*#---------------------------------------------------------------------------
378 *# FUNCTION NAME: i2c_writereg
380 *# DESCRIPTION : Writes a value to an I2C device
382 *#--------------------------------------------------------------------------*/
384 i2c_writereg(unsigned char theSlave, unsigned char theReg,
385 unsigned char theValue)
393 * we don't like to be interrupted
395 local_irq_save(flags);
401 i2c_outbyte((theSlave & 0xfe));
408 * now select register
413 * now it's time to wait for ack
418 * send register register data
420 i2c_outbyte(theValue);
422 * now it's time to wait for ack
431 * enable interrupt again
433 local_irq_restore(flags);
435 } while(error && cntr--);
437 i2c_delay(CLOCK_LOW_TIME);
442 /*#---------------------------------------------------------------------------
444 *# FUNCTION NAME: i2c_readreg
446 *# DESCRIPTION : Reads a value from the decoder registers.
448 *#--------------------------------------------------------------------------*/
450 i2c_readreg(unsigned char theSlave, unsigned char theReg)
459 * we don't like to be interrupted
461 local_irq_save(flags);
463 * generate start condition
470 i2c_outbyte((theSlave & 0xfe));
477 * now select register
482 * now it's time to wait for ack
487 * repeat start condition
489 i2c_delay(CLOCK_LOW_TIME);
494 i2c_outbyte(theSlave | 0x01);
505 * last received byte needs to be nacked
514 * enable interrupt again
516 local_irq_restore(flags);
518 } while(error && cntr--);
524 i2c_open(struct inode *inode, struct file *filp)
530 i2c_release(struct inode *inode, struct file *filp)
535 /* Main device API. ioctl's to write or read to/from i2c registers.
539 i2c_ioctl(struct inode *inode, struct file *file,
540 unsigned int cmd, unsigned long arg)
542 if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
546 switch (_IOC_NR(cmd)) {
548 /* write to an i2c slave */
549 D(printk("i2cw %d %d %d\n",
554 return i2c_writereg(I2C_ARGSLAVE(arg),
560 /* read from an i2c slave */
561 D(printk("i2cr %d %d ",
564 val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
565 D(printk("= %d\n", val));
576 static struct file_operations i2c_fops = {
580 release: i2c_release,
588 /* Setup and enable the Port B I2C interface */
590 crisv32_io_get_name(&cris_i2c_data, CONFIG_ETRAX_I2C_DATA_PORT);
591 crisv32_io_get_name(&cris_i2c_clk, CONFIG_ETRAX_I2C_CLK_PORT);
593 /* register char device */
595 res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
597 printk(KERN_ERR "i2c: couldn't get a major number.\n");
601 printk(KERN_INFO "I2C driver v2.2, (c) 1999-2001 Axis Communications AB\n");
606 /* this makes sure that i2c_init is called during boot */
608 module_init(i2c_init);
610 /****************** END OF FILE i2c.c ********************************/