Use dentry_path() to create full path to inode object
[pohmelfs.git] / arch / cris / arch-v10 / drivers / i2c.c
blobc413539d4205f929c75382bac787d74893575924
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 <asm/system.h>
26 #include <arch/svinto.h>
27 #include <asm/io.h>
28 #include <asm/delay.h>
29 #include <arch/io_interface_mux.h>
31 #include "i2c.h"
33 /****************** I2C DEFINITION SECTION *************************/
35 #define D(x)
37 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
38 static const char i2c_name[] = "i2c";
40 #define CLOCK_LOW_TIME 8
41 #define CLOCK_HIGH_TIME 8
42 #define START_CONDITION_HOLD_TIME 8
43 #define STOP_CONDITION_HOLD_TIME 8
44 #define ENABLE_OUTPUT 0x01
45 #define ENABLE_INPUT 0x00
46 #define I2C_CLOCK_HIGH 1
47 #define I2C_CLOCK_LOW 0
48 #define I2C_DATA_HIGH 1
49 #define I2C_DATA_LOW 0
51 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
52 /* Use PB and not PB_I2C */
53 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
54 #define CONFIG_ETRAX_I2C_DATA_PORT 0
55 #endif
56 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
57 #define CONFIG_ETRAX_I2C_CLK_PORT 1
58 #endif
60 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
61 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
62 #define i2c_enable()
63 #define i2c_disable()
65 /* enable or disable output-enable, to select output or input on the i2c bus */
67 #define i2c_dir_out() \
68 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1)
69 #define i2c_dir_in() \
70 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0)
72 /* control the i2c clock and data signals */
74 #define i2c_clk(x) \
75 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x)
76 #define i2c_data(x) \
77 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x)
79 /* read a bit from the i2c interface */
81 #define i2c_getbit() (((*R_PORT_PB_READ & (1 << SDABIT))) >> SDABIT)
83 #else
84 /* enable or disable the i2c interface */
86 #define i2c_enable() *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_en))
87 #define i2c_disable() *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_en))
89 /* enable or disable output-enable, to select output or input on the i2c bus */
91 #define i2c_dir_out() \
92 *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
93 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1);
94 #define i2c_dir_in() \
95 *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
96 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0);
98 /* control the i2c clock and data signals */
100 #define i2c_clk(x) \
101 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
102 ~IO_MASK(R_PORT_PB_I2C, i2c_clk)) | IO_FIELD(R_PORT_PB_I2C, i2c_clk, (x))); \
103 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 1, x);
105 #define i2c_data(x) \
106 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
107 ~IO_MASK(R_PORT_PB_I2C, i2c_d)) | IO_FIELD(R_PORT_PB_I2C, i2c_d, (x))); \
108 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 0, x);
110 /* read a bit from the i2c interface */
112 #define i2c_getbit() (*R_PORT_PB_READ & 0x1)
113 #endif
115 /* use the kernels delay routine */
117 #define i2c_delay(usecs) udelay(usecs)
119 static DEFINE_SPINLOCK(i2c_lock); /* Protect directions etc */
121 /****************** FUNCTION DEFINITION SECTION *************************/
124 /* generate i2c start condition */
126 void
127 i2c_start(void)
130 * SCL=1 SDA=1
132 i2c_dir_out();
133 i2c_delay(CLOCK_HIGH_TIME/6);
134 i2c_data(I2C_DATA_HIGH);
135 i2c_clk(I2C_CLOCK_HIGH);
136 i2c_delay(CLOCK_HIGH_TIME);
138 * SCL=1 SDA=0
140 i2c_data(I2C_DATA_LOW);
141 i2c_delay(START_CONDITION_HOLD_TIME);
143 * SCL=0 SDA=0
145 i2c_clk(I2C_CLOCK_LOW);
146 i2c_delay(CLOCK_LOW_TIME);
149 /* generate i2c stop condition */
151 void
152 i2c_stop(void)
154 i2c_dir_out();
157 * SCL=0 SDA=0
159 i2c_clk(I2C_CLOCK_LOW);
160 i2c_data(I2C_DATA_LOW);
161 i2c_delay(CLOCK_LOW_TIME*2);
163 * SCL=1 SDA=0
165 i2c_clk(I2C_CLOCK_HIGH);
166 i2c_delay(CLOCK_HIGH_TIME*2);
168 * SCL=1 SDA=1
170 i2c_data(I2C_DATA_HIGH);
171 i2c_delay(STOP_CONDITION_HOLD_TIME);
173 i2c_dir_in();
176 /* write a byte to the i2c interface */
178 void
179 i2c_outbyte(unsigned char x)
181 int i;
183 i2c_dir_out();
185 for (i = 0; i < 8; i++) {
186 if (x & 0x80) {
187 i2c_data(I2C_DATA_HIGH);
188 } else {
189 i2c_data(I2C_DATA_LOW);
192 i2c_delay(CLOCK_LOW_TIME/2);
193 i2c_clk(I2C_CLOCK_HIGH);
194 i2c_delay(CLOCK_HIGH_TIME);
195 i2c_clk(I2C_CLOCK_LOW);
196 i2c_delay(CLOCK_LOW_TIME/2);
197 x <<= 1;
199 i2c_data(I2C_DATA_LOW);
200 i2c_delay(CLOCK_LOW_TIME/2);
203 * enable input
205 i2c_dir_in();
208 /* read a byte from the i2c interface */
210 unsigned char
211 i2c_inbyte(void)
213 unsigned char aBitByte = 0;
214 int i;
216 /* Switch off I2C to get bit */
217 i2c_disable();
218 i2c_dir_in();
219 i2c_delay(CLOCK_HIGH_TIME/2);
221 /* Get bit */
222 aBitByte |= i2c_getbit();
224 /* Enable I2C */
225 i2c_enable();
226 i2c_delay(CLOCK_LOW_TIME/2);
228 for (i = 1; i < 8; i++) {
229 aBitByte <<= 1;
230 /* Clock pulse */
231 i2c_clk(I2C_CLOCK_HIGH);
232 i2c_delay(CLOCK_HIGH_TIME);
233 i2c_clk(I2C_CLOCK_LOW);
234 i2c_delay(CLOCK_LOW_TIME);
236 /* Switch off I2C to get bit */
237 i2c_disable();
238 i2c_dir_in();
239 i2c_delay(CLOCK_HIGH_TIME/2);
241 /* Get bit */
242 aBitByte |= i2c_getbit();
244 /* Enable I2C */
245 i2c_enable();
246 i2c_delay(CLOCK_LOW_TIME/2);
248 i2c_clk(I2C_CLOCK_HIGH);
249 i2c_delay(CLOCK_HIGH_TIME);
252 * we leave the clock low, getbyte is usually followed
253 * by sendack/nack, they assume the clock to be low
255 i2c_clk(I2C_CLOCK_LOW);
256 return aBitByte;
259 /*#---------------------------------------------------------------------------
261 *# FUNCTION NAME: i2c_getack
263 *# DESCRIPTION : checks if ack was received from ic2
265 *#--------------------------------------------------------------------------*/
268 i2c_getack(void)
270 int ack = 1;
272 * enable output
274 i2c_dir_out();
276 * Release data bus by setting
277 * data high
279 i2c_data(I2C_DATA_HIGH);
281 * enable input
283 i2c_dir_in();
284 i2c_delay(CLOCK_HIGH_TIME/4);
286 * generate ACK clock pulse
288 i2c_clk(I2C_CLOCK_HIGH);
290 * Use PORT PB instead of I2C
291 * for input. (I2C not working)
293 i2c_clk(1);
294 i2c_data(1);
296 * switch off I2C
298 i2c_data(1);
299 i2c_disable();
300 i2c_dir_in();
302 * now wait for ack
304 i2c_delay(CLOCK_HIGH_TIME/2);
306 * check for ack
308 if(i2c_getbit())
309 ack = 0;
310 i2c_delay(CLOCK_HIGH_TIME/2);
311 if(!ack){
312 if(!i2c_getbit()) /* receiver pulld SDA low */
313 ack = 1;
314 i2c_delay(CLOCK_HIGH_TIME/2);
318 * our clock is high now, make sure data is low
319 * before we enable our output. If we keep data high
320 * and enable output, we would generate a stop condition.
322 i2c_data(I2C_DATA_LOW);
325 * end clock pulse
327 i2c_enable();
328 i2c_dir_out();
329 i2c_clk(I2C_CLOCK_LOW);
330 i2c_delay(CLOCK_HIGH_TIME/4);
332 * enable output
334 i2c_dir_out();
336 * remove ACK clock pulse
338 i2c_data(I2C_DATA_HIGH);
339 i2c_delay(CLOCK_LOW_TIME/2);
340 return ack;
343 /*#---------------------------------------------------------------------------
345 *# FUNCTION NAME: I2C::sendAck
347 *# DESCRIPTION : Send ACK on received data
349 *#--------------------------------------------------------------------------*/
350 void
351 i2c_sendack(void)
354 * enable output
356 i2c_delay(CLOCK_LOW_TIME);
357 i2c_dir_out();
359 * set ack pulse high
361 i2c_data(I2C_DATA_LOW);
363 * generate clock pulse
365 i2c_delay(CLOCK_HIGH_TIME/6);
366 i2c_clk(I2C_CLOCK_HIGH);
367 i2c_delay(CLOCK_HIGH_TIME);
368 i2c_clk(I2C_CLOCK_LOW);
369 i2c_delay(CLOCK_LOW_TIME/6);
371 * reset data out
373 i2c_data(I2C_DATA_HIGH);
374 i2c_delay(CLOCK_LOW_TIME);
376 i2c_dir_in();
379 /*#---------------------------------------------------------------------------
381 *# FUNCTION NAME: i2c_sendnack
383 *# DESCRIPTION : Sends NACK on received data
385 *#--------------------------------------------------------------------------*/
386 void
387 i2c_sendnack(void)
390 * enable output
392 i2c_delay(CLOCK_LOW_TIME);
393 i2c_dir_out();
395 * set data high
397 i2c_data(I2C_DATA_HIGH);
399 * generate clock pulse
401 i2c_delay(CLOCK_HIGH_TIME/6);
402 i2c_clk(I2C_CLOCK_HIGH);
403 i2c_delay(CLOCK_HIGH_TIME);
404 i2c_clk(I2C_CLOCK_LOW);
405 i2c_delay(CLOCK_LOW_TIME);
407 i2c_dir_in();
410 /*#---------------------------------------------------------------------------
412 *# FUNCTION NAME: i2c_writereg
414 *# DESCRIPTION : Writes a value to an I2C device
416 *#--------------------------------------------------------------------------*/
418 i2c_writereg(unsigned char theSlave, unsigned char theReg,
419 unsigned char theValue)
421 int error, cntr = 3;
422 unsigned long flags;
424 spin_lock(&i2c_lock);
426 do {
427 error = 0;
429 * we don't like to be interrupted
431 local_irq_save(flags);
433 i2c_start();
435 * send slave address
437 i2c_outbyte((theSlave & 0xfe));
439 * wait for ack
441 if(!i2c_getack())
442 error = 1;
444 * now select register
446 i2c_dir_out();
447 i2c_outbyte(theReg);
449 * now it's time to wait for ack
451 if(!i2c_getack())
452 error |= 2;
454 * send register register data
456 i2c_outbyte(theValue);
458 * now it's time to wait for ack
460 if(!i2c_getack())
461 error |= 4;
463 * end byte stream
465 i2c_stop();
467 * enable interrupt again
469 local_irq_restore(flags);
471 } while(error && cntr--);
473 i2c_delay(CLOCK_LOW_TIME);
475 spin_unlock(&i2c_lock);
477 return -error;
480 /*#---------------------------------------------------------------------------
482 *# FUNCTION NAME: i2c_readreg
484 *# DESCRIPTION : Reads a value from the decoder registers.
486 *#--------------------------------------------------------------------------*/
487 unsigned char
488 i2c_readreg(unsigned char theSlave, unsigned char theReg)
490 unsigned char b = 0;
491 int error, cntr = 3;
492 unsigned long flags;
494 spin_lock(&i2c_lock);
496 do {
497 error = 0;
499 * we don't like to be interrupted
501 local_irq_save(flags);
503 * generate start condition
505 i2c_start();
508 * send slave address
510 i2c_outbyte((theSlave & 0xfe));
512 * wait for ack
514 if(!i2c_getack())
515 error = 1;
517 * now select register
519 i2c_dir_out();
520 i2c_outbyte(theReg);
522 * now it's time to wait for ack
524 if(!i2c_getack())
525 error = 1;
527 * repeat start condition
529 i2c_delay(CLOCK_LOW_TIME);
530 i2c_start();
532 * send slave address
534 i2c_outbyte(theSlave | 0x01);
536 * wait for ack
538 if(!i2c_getack())
539 error = 1;
541 * fetch register
543 b = i2c_inbyte();
545 * last received byte needs to be nacked
546 * instead of acked
548 i2c_sendnack();
550 * end sequence
552 i2c_stop();
554 * enable interrupt again
556 local_irq_restore(flags);
558 } while(error && cntr--);
560 spin_unlock(&i2c_lock);
562 return b;
565 static int
566 i2c_open(struct inode *inode, struct file *filp)
568 return 0;
571 static int
572 i2c_release(struct inode *inode, struct file *filp)
574 return 0;
577 /* Main device API. ioctl's to write or read to/from i2c registers.
580 static long i2c_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
582 if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
583 return -EINVAL;
586 switch (_IOC_NR(cmd)) {
587 case I2C_WRITEREG:
588 /* write to an i2c slave */
589 D(printk(KERN_DEBUG "i2cw %d %d %d\n",
590 I2C_ARGSLAVE(arg),
591 I2C_ARGREG(arg),
592 I2C_ARGVALUE(arg)));
594 return i2c_writereg(I2C_ARGSLAVE(arg),
595 I2C_ARGREG(arg),
596 I2C_ARGVALUE(arg));
597 case I2C_READREG:
599 unsigned char val;
600 /* read from an i2c slave */
601 D(printk(KERN_DEBUG "i2cr %d %d ",
602 I2C_ARGSLAVE(arg),
603 I2C_ARGREG(arg)));
604 val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
605 D(printk(KERN_DEBUG "= %d\n", val));
606 return val;
608 default:
609 return -EINVAL;
612 return 0;
615 static const struct file_operations i2c_fops = {
616 .owner = THIS_MODULE,
617 .unlocked_ioctl = i2c_ioctl,
618 .open = i2c_open,
619 .release = i2c_release,
620 .llseek = noop_llseek,
623 int __init
624 i2c_init(void)
626 static int res = 0;
627 static int first = 1;
629 if (!first) {
630 return res;
632 first = 0;
634 /* Setup and enable the Port B I2C interface */
636 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
637 if ((res = cris_request_io_interface(if_i2c, "I2C"))) {
638 printk(KERN_CRIT "i2c_init: Failed to get IO interface\n");
639 return res;
642 *R_PORT_PB_I2C = port_pb_i2c_shadow |=
643 IO_STATE(R_PORT_PB_I2C, i2c_en, on) |
644 IO_FIELD(R_PORT_PB_I2C, i2c_d, 1) |
645 IO_FIELD(R_PORT_PB_I2C, i2c_clk, 1) |
646 IO_STATE(R_PORT_PB_I2C, i2c_oe_, enable);
648 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir0);
649 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir1);
651 *R_PORT_PB_DIR = (port_pb_dir_shadow |=
652 IO_STATE(R_PORT_PB_DIR, dir0, input) |
653 IO_STATE(R_PORT_PB_DIR, dir1, output));
654 #else
655 if ((res = cris_io_interface_allocate_pins(if_i2c,
656 'b',
657 CONFIG_ETRAX_I2C_DATA_PORT,
658 CONFIG_ETRAX_I2C_DATA_PORT))) {
659 printk(KERN_WARNING "i2c_init: Failed to get IO pin for I2C data port\n");
660 return res;
661 } else if ((res = cris_io_interface_allocate_pins(if_i2c,
662 'b',
663 CONFIG_ETRAX_I2C_CLK_PORT,
664 CONFIG_ETRAX_I2C_CLK_PORT))) {
665 cris_io_interface_free_pins(if_i2c,
666 'b',
667 CONFIG_ETRAX_I2C_DATA_PORT,
668 CONFIG_ETRAX_I2C_DATA_PORT);
669 printk(KERN_WARNING "i2c_init: Failed to get IO pin for I2C clk port\n");
671 #endif
673 return res;
676 static int __init
677 i2c_register(void)
679 int res;
681 res = i2c_init();
682 if (res < 0)
683 return res;
684 res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
685 if(res < 0) {
686 printk(KERN_ERR "i2c: couldn't get a major number.\n");
687 return res;
690 printk(KERN_INFO "I2C driver v2.2, (c) 1999-2004 Axis Communications AB\n");
692 return 0;
695 /* this makes sure that i2c_register is called during boot */
697 module_init(i2c_register);
699 /****************** END OF FILE i2c.c ********************************/