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 *! 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,
13 *! use PB.
14 *| June 23 2003 Pieter Grimmerink Added 'i2c_sendnack'. i2c_readreg now
15 *| generates nack on last received byte,
16 *| instead of ack.
17 *| i2c_getack changed data level while clock
18 *| was high, causing DS75 to see a stop condition
19 *|
20 *! $Log: i2c.c,v $
21 *! Revision 1.12 2003/06/23 14:43:47 oskarp
22 *! * i2c_sendnack is added.
23 *! - i2c_readreg now generates nack on last received byte, instead of ack.
24 *! i2c_getack changed data level while clock was high, causing DS75 to
25 *! see a stop condition.
26 *!
27 *! Revision 1.11 2003/05/23 09:37:23 oskarp
28 *! * Made i2c_init() visible.
29 *! - e.g. pcf856.c uses i2c_init().
30 *!
31 *! Revision 1.10 2003/04/01 14:12:06 starvik
32 *! Added loglevel for lots of printks
33 *!
34 *! Revision 1.9 2002/10/31 15:32:26 starvik
35 *! Update Port B register and shadow even when running with hardware support
36 *! to avoid glitches when reading bits
37 *! Never set direction to out in i2c_inbyte
38 *! Removed incorrect clock togling at end of i2c_inbyte
39 *!
40 *! Revision 1.8 2002/08/13 06:31:53 starvik
41 *! Made SDA and SCL line configurable
42 *! Modified i2c_inbyte to work with PCF8563
43 *!
44 *! Revision 1.7 2001/04/04 13:11:36 markusl
45 *! Updated according to review remarks
46 *!
47 *! Revision 1.6 2001/03/19 12:43:00 markusl
48 *! Made some symbols unstatic (used by the eeprom driver)
49 *!
50 *! Revision 1.5 2001/02/27 13:52:48 bjornw
51 *! malloc.h -> slab.h
52 *!
53 *! Revision 1.4 2001/02/15 07:17:40 starvik
54 *! Corrected usage if port_pb_i2c_shadow
55 *!
56 *! Revision 1.3 2001/01/26 17:55:13 bjornw
57 *! * Made I2C_USES_PB_NOT_PB_I2C a CONFIG option instead of assigning it
58 *! magically. Config.in needs to set it for the options that need it, like
59 *! Dallas 1302 support. Actually, it should be default since it screws up
60 *! the PB bits even if you don't use I2C..
61 *! * Include linux/config.h to get the above
62 *!
63 *! Revision 1.2 2001/01/18 15:49:30 bjornw
64 *! 2.4 port of I2C including some cleanups (untested of course)
65 *!
66 *! Revision 1.1 2001/01/18 15:35:25 bjornw
67 *! Verbatim copy of the Etrax i2c driver, 2.0 elinux version
68 *!
69 *!
70 *! ---------------------------------------------------------------------------
71 *!
72 *! (C) Copyright 1999-2002 Axis Communications AB, LUND, SWEDEN
73 *!
74 *!***************************************************************************/
75 /* $Id: i2c.c,v 1.12 2003/06/23 14:43:47 oskarp Exp $ */
76 /****************** INCLUDE FILES SECTION ***********************************/
77
78 #include <linux/module.h>
79 #include <linux/sched.h>
80 #include <linux/slab.h>
81 #include <linux/errno.h>
82 #include <linux/kernel.h>
83 #include <linux/fs.h>
84 #include <linux/string.h>
85 #include <linux/init.h>
86 #include <linux/config.h>
87
88 #include <asm/etraxi2c.h>
89
90 #include <asm/system.h>
91 #include <asm/svinto.h>
92 #include <asm/io.h>
93 #include <asm/delay.h>
94
95 #include "i2c.h"
96
97 /****************** I2C DEFINITION SECTION *************************/
98
99 #define D(x)
100
101 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
102 static const char i2c_name[] = "i2c";
103
104 #define CLOCK_LOW_TIME 8
105 #define CLOCK_HIGH_TIME 8
106 #define START_CONDITION_HOLD_TIME 8
107 #define STOP_CONDITION_HOLD_TIME 8
108 #define ENABLE_OUTPUT 0x01
109 #define ENABLE_INPUT 0x00
110 #define I2C_CLOCK_HIGH 1
111 #define I2C_CLOCK_LOW 0
112 #define I2C_DATA_HIGH 1
113 #define I2C_DATA_LOW 0
114
115 #if 0
116 /* TODO: fix this so the CONFIG_ETRAX_I2C_USES... is set in Config.in instead */
117 #if defined(CONFIG_DS1302) && (CONFIG_DS1302_SDABIT==0) && \
118 (CONFIG_DS1302_SCLBIT == 1)
119 #define CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
120 #endif
121 #endif
122
123 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
124 /* Use PB and not PB_I2C */
125 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
126 #define CONFIG_ETRAX_I2C_DATA_PORT 0
127 #endif
128 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
129 #define CONFIG_ETRAX_I2C_CLK_PORT 1
130 #endif
131
132 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
133 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
134 #define i2c_enable()
135 #define i2c_disable()
136
137 /* enable or disable output-enable, to select output or input on the i2c bus */
138
139 #define i2c_dir_out() \
140 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1)
141 #define i2c_dir_in() \
142 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0)
143
144 /* control the i2c clock and data signals */
145
146 #define i2c_clk(x) \
147 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x)
148 #define i2c_data(x) \
149 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x)
150
151 /* read a bit from the i2c interface */
152
153 #define i2c_getbit() (((*R_PORT_PB_READ & (1 << SDABIT))) >> SDABIT)
154
155 #else /* !CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C */
156 /* enable or disable the i2c interface */
157
158 #define i2c_enable() *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_en))
159 #define i2c_disable() *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_en))
160
161 /* enable or disable output-enable, to select output or input on the i2c bus */
162
163 #define i2c_dir_out() \
164 *R_PORT_PB_I2C = (port_pb_i2c_shadow &= ~IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
165 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1);
166 #define i2c_dir_in() \
167 *R_PORT_PB_I2C = (port_pb_i2c_shadow |= IO_MASK(R_PORT_PB_I2C, i2c_oe_)); \
168 REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0);
169
170 /* control the i2c clock and data signals */
171
172 #define i2c_clk(x) \
173 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
174 ~IO_MASK(R_PORT_PB_I2C, i2c_clk)) | IO_FIELD(R_PORT_PB_I2C, i2c_clk, (x))); \
175 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 1, x);
176
177 #define i2c_data(x) \
178 *R_PORT_PB_I2C = (port_pb_i2c_shadow = (port_pb_i2c_shadow & \
179 ~IO_MASK(R_PORT_PB_I2C, i2c_d)) | IO_FIELD(R_PORT_PB_I2C, i2c_d, (x))); \
180 REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, 0, x);
181
182 /* read a bit from the i2c interface */
183
184 #define i2c_getbit() (*R_PORT_PB_READ & 0x1)
185
186 #endif /* CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C */
187
188 /* use the kernels delay routine */
189
190 #define i2c_delay(usecs) udelay(usecs)
191
192
193 /****************** FUNCTION DEFINITION SECTION *************************/
194
195
196 /* generate i2c start condition */
197
198 void
i2c_start(void)199 i2c_start(void)
200 {
201 /*
202 * SCL=1 SDA=1
203 */
204 i2c_dir_out();
205 i2c_delay(CLOCK_HIGH_TIME/6);
206 i2c_data(I2C_DATA_HIGH);
207 i2c_clk(I2C_CLOCK_HIGH);
208 i2c_delay(CLOCK_HIGH_TIME);
209 /*
210 * SCL=1 SDA=0
211 */
212 i2c_data(I2C_DATA_LOW);
213 i2c_delay(START_CONDITION_HOLD_TIME);
214 /*
215 * SCL=0 SDA=0
216 */
217 i2c_clk(I2C_CLOCK_LOW);
218 i2c_delay(CLOCK_LOW_TIME);
219 }
220
221 /* generate i2c stop condition */
222
223 void
i2c_stop(void)224 i2c_stop(void)
225 {
226 i2c_dir_out();
227
228 /*
229 * SCL=0 SDA=0
230 */
231 i2c_clk(I2C_CLOCK_LOW);
232 i2c_data(I2C_DATA_LOW);
233 i2c_delay(CLOCK_LOW_TIME*2);
234 /*
235 * SCL=1 SDA=0
236 */
237 i2c_clk(I2C_CLOCK_HIGH);
238 i2c_delay(CLOCK_HIGH_TIME*2);
239 /*
240 * SCL=1 SDA=1
241 */
242 i2c_data(I2C_DATA_HIGH);
243 i2c_delay(STOP_CONDITION_HOLD_TIME);
244
245 i2c_dir_in();
246 }
247
248 /* write a byte to the i2c interface */
249
250 void
i2c_outbyte(unsigned char x)251 i2c_outbyte(unsigned char x)
252 {
253 int i;
254
255 i2c_dir_out();
256
257 for (i = 0; i < 8; i++) {
258 if (x & 0x80) {
259 i2c_data(I2C_DATA_HIGH);
260 } else {
261 i2c_data(I2C_DATA_LOW);
262 }
263
264 i2c_delay(CLOCK_LOW_TIME/2);
265 i2c_clk(I2C_CLOCK_HIGH);
266 i2c_delay(CLOCK_HIGH_TIME);
267 i2c_clk(I2C_CLOCK_LOW);
268 i2c_delay(CLOCK_LOW_TIME/2);
269 x <<= 1;
270 }
271 i2c_data(I2C_DATA_LOW);
272 i2c_delay(CLOCK_LOW_TIME/2);
273
274 /*
275 * enable input
276 */
277 i2c_dir_in();
278 }
279
280 /* read a byte from the i2c interface */
281
282 unsigned char
i2c_inbyte(void)283 i2c_inbyte(void)
284 {
285 unsigned char aBitByte = 0;
286 int i;
287
288 /* Switch off I2C to get bit */
289 i2c_disable();
290 i2c_dir_in();
291 i2c_delay(CLOCK_HIGH_TIME/2);
292
293 /* Get bit */
294 aBitByte |= i2c_getbit();
295
296 /* Enable I2C */
297 i2c_enable();
298 i2c_delay(CLOCK_LOW_TIME/2);
299
300 for (i = 1; i < 8; i++) {
301 aBitByte <<= 1;
302 /* Clock pulse */
303 i2c_clk(I2C_CLOCK_HIGH);
304 i2c_delay(CLOCK_HIGH_TIME);
305 i2c_clk(I2C_CLOCK_LOW);
306 i2c_delay(CLOCK_LOW_TIME);
307
308 /* Switch off I2C to get bit */
309 i2c_disable();
310 i2c_dir_in();
311 i2c_delay(CLOCK_HIGH_TIME/2);
312
313 /* Get bit */
314 aBitByte |= i2c_getbit();
315
316 /* Enable I2C */
317 i2c_enable();
318 i2c_delay(CLOCK_LOW_TIME/2);
319 }
320 i2c_clk(I2C_CLOCK_HIGH);
321 i2c_delay(CLOCK_HIGH_TIME);
322
323 /*
324 * we leave the clock low, getbyte is usually followed
325 * by sendack/nack, they assume the clock to be low
326 */
327 i2c_clk(I2C_CLOCK_LOW);
328 return aBitByte;
329 }
330
331 /*#---------------------------------------------------------------------------
332 *#
333 *# FUNCTION NAME: i2c_getack
334 *#
335 *# DESCRIPTION : checks if ack was received from ic2
336 *#
337 *#--------------------------------------------------------------------------*/
338
339 int
i2c_getack(void)340 i2c_getack(void)
341 {
342 int ack = 1;
343 /*
344 * enable output
345 */
346 i2c_dir_out();
347 /*
348 * Release data bus by setting
349 * data high
350 */
351 i2c_data(I2C_DATA_HIGH);
352 /*
353 * enable input
354 */
355 i2c_dir_in();
356 i2c_delay(CLOCK_HIGH_TIME/4);
357 /*
358 * generate ACK clock pulse
359 */
360 i2c_clk(I2C_CLOCK_HIGH);
361 /*
362 * Use PORT PB instead of I2C
363 * for input. (I2C not working)
364 */
365 i2c_clk(1);
366 i2c_data(1);
367 /*
368 * switch off I2C
369 */
370 i2c_data(1);
371 i2c_disable();
372 i2c_dir_in();
373 /*
374 * now wait for ack
375 */
376 i2c_delay(CLOCK_HIGH_TIME/2);
377 /*
378 * check for ack
379 */
380 if(i2c_getbit())
381 ack = 0;
382 i2c_delay(CLOCK_HIGH_TIME/2);
383 if(!ack){
384 if(!i2c_getbit()) /* receiver pulld SDA low */
385 ack = 1;
386 i2c_delay(CLOCK_HIGH_TIME/2);
387 }
388
389 /*
390 * our clock is high now, make sure data is low
391 * before we enable our output. If we keep data high
392 * and enable output, we would generate a stop condition.
393 */
394 i2c_data(I2C_DATA_LOW);
395
396 /*
397 * end clock pulse
398 */
399 i2c_enable();
400 i2c_dir_out();
401 i2c_clk(I2C_CLOCK_LOW);
402 i2c_delay(CLOCK_HIGH_TIME/4);
403 /*
404 * enable output
405 */
406 i2c_dir_out();
407 /*
408 * remove ACK clock pulse
409 */
410 i2c_data(I2C_DATA_HIGH);
411 i2c_delay(CLOCK_LOW_TIME/2);
412 return ack;
413 }
414
415 /*#---------------------------------------------------------------------------
416 *#
417 *# FUNCTION NAME: I2C::sendAck
418 *#
419 *# DESCRIPTION : Send ACK on received data
420 *#
421 *#--------------------------------------------------------------------------*/
422 void
i2c_sendack(void)423 i2c_sendack(void)
424 {
425 /*
426 * enable output
427 */
428 i2c_delay(CLOCK_LOW_TIME);
429 i2c_dir_out();
430 /*
431 * set ack pulse high
432 */
433 i2c_data(I2C_DATA_LOW);
434 /*
435 * generate clock pulse
436 */
437 i2c_delay(CLOCK_HIGH_TIME/6);
438 i2c_clk(I2C_CLOCK_HIGH);
439 i2c_delay(CLOCK_HIGH_TIME);
440 i2c_clk(I2C_CLOCK_LOW);
441 i2c_delay(CLOCK_LOW_TIME/6);
442 /*
443 * reset data out
444 */
445 i2c_data(I2C_DATA_HIGH);
446 i2c_delay(CLOCK_LOW_TIME);
447
448 i2c_dir_in();
449 }
450
451 /*#---------------------------------------------------------------------------
452 *#
453 *# FUNCTION NAME: i2c_sendnack
454 *#
455 *# DESCRIPTION : Sends NACK on received data
456 *#
457 *#--------------------------------------------------------------------------*/
458 void
i2c_sendnack(void)459 i2c_sendnack(void)
460 {
461 /*
462 * enable output
463 */
464 i2c_delay(CLOCK_LOW_TIME);
465 i2c_dir_out();
466 /*
467 * set data high
468 */
469 i2c_data(I2C_DATA_HIGH);
470 /*
471 * generate clock pulse
472 */
473 i2c_delay(CLOCK_HIGH_TIME/6);
474 i2c_clk(I2C_CLOCK_HIGH);
475 i2c_delay(CLOCK_HIGH_TIME);
476 i2c_clk(I2C_CLOCK_LOW);
477 i2c_delay(CLOCK_LOW_TIME);
478
479 i2c_dir_in();
480 }
481
482 /*#---------------------------------------------------------------------------
483 *#
484 *# FUNCTION NAME: i2c_writereg
485 *#
486 *# DESCRIPTION : Writes a value to an I2C device
487 *#
488 *#--------------------------------------------------------------------------*/
489 int
i2c_writereg(unsigned char theSlave,unsigned char theReg,unsigned char theValue)490 i2c_writereg(unsigned char theSlave, unsigned char theReg,
491 unsigned char theValue)
492 {
493 int error, cntr = 3;
494 unsigned long flags;
495
496 do {
497 error = 0;
498 /*
499 * we don't like to be interrupted
500 */
501 save_flags(flags);
502 cli();
503
504 i2c_start();
505 /*
506 * send slave address
507 */
508 i2c_outbyte((theSlave & 0xfe));
509 /*
510 * wait for ack
511 */
512 if(!i2c_getack())
513 error = 1;
514 /*
515 * now select register
516 */
517 i2c_dir_out();
518 i2c_outbyte(theReg);
519 /*
520 * now it's time to wait for ack
521 */
522 if(!i2c_getack())
523 error |= 2;
524 /*
525 * send register register data
526 */
527 i2c_outbyte(theValue);
528 /*
529 * now it's time to wait for ack
530 */
531 if(!i2c_getack())
532 error |= 4;
533 /*
534 * end byte stream
535 */
536 i2c_stop();
537 /*
538 * enable interrupt again
539 */
540 restore_flags(flags);
541
542 } while(error && cntr--);
543
544 i2c_delay(CLOCK_LOW_TIME);
545
546 return -error;
547 }
548
549 /*#---------------------------------------------------------------------------
550 *#
551 *# FUNCTION NAME: i2c_readreg
552 *#
553 *# DESCRIPTION : Reads a value from the decoder registers.
554 *#
555 *#--------------------------------------------------------------------------*/
556 unsigned char
i2c_readreg(unsigned char theSlave,unsigned char theReg)557 i2c_readreg(unsigned char theSlave, unsigned char theReg)
558 {
559 unsigned char b = 0;
560 int error, cntr = 3;
561 unsigned long flags;
562
563 do {
564 error = 0;
565 /*
566 * we don't like to be interrupted
567 */
568 save_flags(flags);
569 cli();
570 /*
571 * generate start condition
572 */
573 i2c_start();
574
575 /*
576 * send slave address
577 */
578 i2c_outbyte((theSlave & 0xfe));
579 /*
580 * wait for ack
581 */
582 if(!i2c_getack())
583 error = 1;
584 /*
585 * now select register
586 */
587 i2c_dir_out();
588 i2c_outbyte(theReg);
589 /*
590 * now it's time to wait for ack
591 */
592 if(!i2c_getack())
593 error = 1;
594 /*
595 * repeat start condition
596 */
597 i2c_delay(CLOCK_LOW_TIME);
598 i2c_start();
599 /*
600 * send slave address
601 */
602 i2c_outbyte(theSlave | 0x01);
603 /*
604 * wait for ack
605 */
606 if(!i2c_getack())
607 error = 1;
608 /*
609 * fetch register
610 */
611 b = i2c_inbyte();
612 /*
613 * last received byte needs to be nacked
614 * instead of acked
615 */
616 i2c_sendnack();
617 /*
618 * end sequence
619 */
620 i2c_stop();
621 /*
622 * enable interrupt again
623 */
624 restore_flags(flags);
625
626 } while(error && cntr--);
627
628 return b;
629 }
630
631 static int
i2c_open(struct inode * inode,struct file * filp)632 i2c_open(struct inode *inode, struct file *filp)
633 {
634 return 0;
635 }
636
637 static int
i2c_release(struct inode * inode,struct file * filp)638 i2c_release(struct inode *inode, struct file *filp)
639 {
640 return 0;
641 }
642
643 /* Main device API. ioctl's to write or read to/from i2c registers.
644 */
645
646 static int
i2c_ioctl(struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)647 i2c_ioctl(struct inode *inode, struct file *file,
648 unsigned int cmd, unsigned long arg)
649 {
650 if(_IOC_TYPE(cmd) != ETRAXI2C_IOCTYPE) {
651 return -EINVAL;
652 }
653
654 switch (_IOC_NR(cmd)) {
655 case I2C_WRITEREG:
656 /* write to an i2c slave */
657 D(printk("i2cw %d %d %d\n",
658 I2C_ARGSLAVE(arg),
659 I2C_ARGREG(arg),
660 I2C_ARGVALUE(arg)));
661
662 return i2c_writereg(I2C_ARGSLAVE(arg),
663 I2C_ARGREG(arg),
664 I2C_ARGVALUE(arg));
665 case I2C_READREG:
666 {
667 unsigned char val;
668 /* read from an i2c slave */
669 D(printk("i2cr %d %d ",
670 I2C_ARGSLAVE(arg),
671 I2C_ARGREG(arg)));
672 val = i2c_readreg(I2C_ARGSLAVE(arg), I2C_ARGREG(arg));
673 D(printk("= %d\n", val));
674 return val;
675 }
676 default:
677 return -EINVAL;
678
679 }
680
681 return 0;
682 }
683
684 static struct file_operations i2c_fops = {
685 owner: THIS_MODULE,
686 ioctl: i2c_ioctl,
687 open: i2c_open,
688 release: i2c_release,
689 };
690
691 int __init
i2c_init(void)692 i2c_init(void)
693 {
694 int res;
695
696 /* Setup and enable the Port B I2C interface */
697
698 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
699 *R_PORT_PB_I2C = port_pb_i2c_shadow |=
700 IO_STATE(R_PORT_PB_I2C, i2c_en, on) |
701 IO_FIELD(R_PORT_PB_I2C, i2c_d, 1) |
702 IO_FIELD(R_PORT_PB_I2C, i2c_clk, 1) |
703 IO_STATE(R_PORT_PB_I2C, i2c_oe_, enable);
704 #endif
705
706 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir0);
707 port_pb_dir_shadow &= ~IO_MASK(R_PORT_PB_DIR, dir1);
708
709 *R_PORT_PB_DIR = (port_pb_dir_shadow |=
710 IO_STATE(R_PORT_PB_DIR, dir0, input) |
711 IO_STATE(R_PORT_PB_DIR, dir1, output));
712
713 /* register char device */
714
715 res = register_chrdev(I2C_MAJOR, i2c_name, &i2c_fops);
716 if(res < 0) {
717 printk(KERN_ERR "i2c: couldn't get a major number.\n");
718 return res;
719 }
720
721 printk(KERN_INFO "I2C driver v2.2, (c) 1999-2001 Axis Communications AB\n");
722
723 return 0;
724 }
725
726 /* this makes sure that i2c_init is called during boot */
727
728 module_init(i2c_init);
729
730 /****************** END OF FILE i2c.c ********************************/
731