[etrax]: ...and probably claudio forgot to rename this directory, too :)
[openwrt.git] / target / linux / etrax / files / arch / cris / arch-v10 / drivers / i2c_gvc.c
1 /*!***************************************************************************
2 *!
3 *! FILE NAME : i2c.c
4 *!
5 *!
6 *! ---------------------------------------------------------------------------
7 *!
8 *! ( C ) Copyright 1999-2002 Axis Communications AB, LUND, SWEDEN
9 *!
10 *!***************************************************************************/
11
12 /******************** INCLUDE FILES SECTION ****************************/
13
14 #include <linux/module.h>
15 #include <linux/fs.h>
16
17 #ifdef CONFIG_ETRAX_I2C_DYN_ALLOC
18 #include <linux/types.h> /* for dev_t */
19 #include <linux/cdev.h> /* for struct cdev */
20 #endif
21
22 #include <linux/device.h>
23
24 #include "etraxi2c.h"
25
26 #include "i2c_errno.h"
27
28 #include <asm/io.h>
29 #include <asm/delay.h>
30 #include <asm/arch/io_interface_mux.h>
31 #include <asm/uaccess.h>
32
33 #include "i2c_gvc.h"
34
35 MODULE_DESCRIPTION( "I2C Device Driver - 2.3" );
36
37 /*!*********************************************************************
38 *!History I2C driver Geert Vancompernolle
39 *!---------------------------------------
40 *!
41 *! - v1.0:
42 *! First official version.
43 *!
44 *! - v1.1:
45 *! Changes to remove unwanted spikes at ACK/NACK time.
46 *!
47 *!*********************************************************************/
48
49 MODULE_LICENSE( "GPL" );
50
51 /****************** MACRO's **********************/
52
53 #define D( x )
54
55 #ifndef CONFIG_ETRAX_I2C_DYN_ALLOC
56 #define I2C_MAJOR 123 /* LOCAL/EXPERIMENTAL */
57 #endif
58
59 #define WAITONEUS 1
60 /* Following are abbreviations taken from Philips I2C standard */
61 /* Values are representing time in us and are rounded to next whole number, if relevant */
62 #define THDSTA 4 /* Hold delay time for (repeated) START condition */
63 #define TLOW 5 /* LOW period of the SCL clock */
64 #define THDDAT 1 /* Hold delay time for DATA: value of 0 is allowed but 1 taken to be sure */
65 #define TSUDAT 1 /* Set-up time for DATA */
66 #define THIGH 4 /* HIGH period of the SCL clock */
67 #define TSUSTA 5 /* Set-up time for a repeated START condition */
68 #define TSUSTO 4 /* Set-up time for STOP condition */
69 #define TBUF 5 /* Bus-free time between STOP and START condition */
70
71 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
72 #define MAXSCLRETRIES 100
73 #endif
74
75 #define MAXBUSFREERETRIES 5
76 #define MAXRETRIES 3
77 #define WRITEADDRESS_MASK ( 0xFE )
78 #define READADDRESS_MASK ( 0x01 )
79
80 #define SCL_HIGH 1
81 #define SCL_LOW 0
82 #define SDA_HIGH 1
83 #define SDA_LOW 0
84
85 #ifdef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
86 /* Use PB and not PB_I2C */
87 #ifndef CONFIG_ETRAX_I2C_DATA_PORT
88 #define CONFIG_ETRAX_I2C_DATA_PORT 0
89 #endif
90 #ifndef CONFIG_ETRAX_I2C_CLK_PORT
91 #define CONFIG_ETRAX_I2C_CLK_PORT 1
92 #endif
93
94 #define SDABIT CONFIG_ETRAX_I2C_DATA_PORT
95 #define SCLBIT CONFIG_ETRAX_I2C_CLK_PORT
96 #define i2c_enable()
97 #define i2c_disable()
98
99 /* enable or disable output-enable, to select output or input on the i2c bus */
100 #define i2c_sda_dir_out() \
101 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 1 )
102 #define i2c_sda_dir_in() \
103 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, SDABIT, 0 )
104
105 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
106 #define i2c_scl_dir_out() \
107 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, SCLBIT, 1 )
108 #define i2c_scl_dir_in() \
109 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, SCLBIT, 0 )
110 #endif
111
112 /* control the i2c clock and data signals */
113 #define i2c_set_scl( x ) \
114 REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, SCLBIT, x )
115 #define i2c_set_sda( x ) \
116 REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, SDABIT, x )
117
118 /* read status of SDA bit from the i2c interface */
119 #define i2c_sda_is_high() ( ( ( *R_PORT_PB_READ & ( 1 << SDABIT ) ) ) >> SDABIT )
120
121 /* read status of SCL bit from the i2c interface */
122 #define i2c_scl_is_high() ( ( ( *R_PORT_PB_READ & ( 1 << SCLBIT ) ) ) >> SCLBIT )
123
124 #else
125 /* enable or disable the i2c interface */
126 #define i2c_enable() *R_PORT_PB_I2C = ( port_pb_i2c_shadow |= IO_MASK( R_PORT_PB_I2C, i2c_en ) )
127 #define i2c_disable() *R_PORT_PB_I2C = ( port_pb_i2c_shadow &= ~IO_MASK( R_PORT_PB_I2C, i2c_en ) )
128
129 /* enable or disable output-enable, to select output or input on the i2c bus */
130 #define i2c_sda_dir_out() \
131 *R_PORT_PB_I2C = ( port_pb_i2c_shadow &= ~IO_MASK( R_PORT_PB_I2C, i2c_oe_ ) ); \
132 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, 0, 1 );
133 #define i2c_sda_dir_in() \
134 *R_PORT_PB_I2C = ( port_pb_i2c_shadow |= IO_MASK( R_PORT_PB_I2C, i2c_oe_ ) ); \
135 REG_SHADOW_SET( R_PORT_PB_DIR, port_pb_dir_shadow, 0, 0 );
136
137 /* control the i2c clock and data signals */
138 #define i2c_set_scl( x ) \
139 *R_PORT_PB_I2C = ( port_pb_i2c_shadow = ( port_pb_i2c_shadow & \
140 ~IO_MASK( R_PORT_PB_I2C, i2c_set_scl ) ) | IO_FIELD( R_PORT_PB_I2C, i2c_set_scl, ( x ) ) ); \
141 REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, 1, x );
142
143 #define i2c_set_sda( x ) \
144 *R_PORT_PB_I2C = ( port_pb_i2c_shadow = ( port_pb_i2c_shadow & \
145 ~IO_MASK( R_PORT_PB_I2C, i2c_d ) ) | IO_FIELD( R_PORT_PB_I2C, i2c_d, ( x ) ) ); \
146 REG_SHADOW_SET( R_PORT_PB_DATA, port_pb_data_shadow, 0, x );
147
148 /* read a bit from the i2c interface */
149 #define i2c_sda_is_high() ( *R_PORT_PB_READ & 0x1 )
150 #endif
151
152 /* use the kernels delay routine */
153 #define i2c_delay( usecs ) udelay( usecs )
154
155
156 /****************** TYPEDEF's **********************/
157
158
159 /****************** STATIC (file scope) VARIABLES **********************/
160 static DEFINE_SPINLOCK( i2c_lock ); /* Protect directions etc */
161 static const char i2c_name[] = "i2c";
162
163
164
165 /****************** PROTOTYPING SECTION *************************/
166 static int i2c_open( struct inode *inode, struct file *filp );
167 static int i2c_release( struct inode *inode, struct file *filp );
168 static int i2c_command( unsigned char slave
169 , unsigned char* wbuf
170 , unsigned char wlen
171 , unsigned char* rbuf
172 , unsigned char rlen
173 );
174 static int i2c_bus_free_check( unsigned char maxretries );
175 static void i2c_finalise( const char* text, unsigned long irqflags );
176
177
178 /************************************************************************/
179 /****************** AUXILIARIES *************************/
180 /************************************************************************/
181
182 /*#---------------------------------------------------------------------------
183 *#
184 *# FUNCTION NAME: i2c_open
185 *#
186 *# DESCRIPTION : opens an I2C device
187 *#
188 *# PARAMETERS : *inode: reference to inode
189 *# *filp : reference to file pointer
190 *#
191 *#---------------------------------------------------------------------------
192 */
193 static int i2c_open( struct inode *inode, struct file *filp )
194 {
195 return 0;
196 } /* i2c_open */
197
198
199 /*#---------------------------------------------------------------------------
200 *#
201 *# FUNCTION NAME: i2c_release
202 *#
203 *# DESCRIPTION : Releases the I2C device
204 *#
205 *# PARAMETERS : *inode: reference to inode
206 *# *filp : reference to file pointer
207 *#
208 *#---------------------------------------------------------------------------
209 */
210 static int i2c_release( struct inode *inode, struct file *filp )
211 {
212 return 0;
213 } /* i2c_release */
214
215
216 /*#---------------------------------------------------------------------------
217 *#
218 *# FUNCTION NAME: i2c_ioctl
219 *#
220 *# DESCRIPTION : Main device API: ioctl's to write/read
221 *# to/from i2c registers
222 *#
223 *# PARAMETERS : *inode: reference to inode
224 *# *filp : reference to file pointer
225 *# cmd : command to be executed during the ioctl call
226 *# arg : pointer to a structure with the data???
227 *#
228 *# RETURN : result of the ioctl call
229 *#
230 *#---------------------------------------------------------------------------
231 */
232 static int i2c_ioctl( struct inode *inode
233 , struct file *file
234 , unsigned int cmd
235 , unsigned long arg
236 )
237 {
238 /* the acme ioctls */
239 I2C_DATA i2cdata;
240 int RetVal = EI2CNOERRORS;
241
242 if ( _IOC_TYPE( cmd ) != ETRAXI2C_IOCTYPE )
243 {
244 return ( -EINVAL );
245 }
246
247 switch ( _IOC_NR( cmd ) )
248 {
249 case I2C_WRITEREG:
250 /* write to an i2c slave */
251 RetVal = i2c_writereg( I2C_ARGSLAVE( arg )
252 , I2C_ARGREG( arg )
253 , I2C_ARGVALUE( arg )
254 );
255 break;
256
257 case I2C_READREG:
258 RetVal = i2c_readreg( I2C_ARGSLAVE( arg ), I2C_ARGREG( arg ) );
259 break;
260
261 /* New functions added by GVC */
262 case I2C_READ:
263 copy_from_user( (char*)&i2cdata, (char*)arg, sizeof( I2C_DATA ) );
264 {
265 int RetryCntr = MAXRETRIES;
266
267 do
268 {
269 RetVal = i2c_command( i2cdata.slave
270 , NULL
271 , 0
272 , i2cdata.rbuf
273 , i2cdata.rlen
274 );
275 } while ( ( EI2CNOERRORS != RetVal )
276 &&( --RetryCntr )
277 );
278 }
279 copy_to_user( (char*)arg, (char*)&i2cdata, sizeof( I2C_DATA ) );
280 break;
281
282 case I2C_WRITE:
283 copy_from_user( (char*)&i2cdata, (char*)arg, sizeof( I2C_DATA ) );
284 {
285 int RetryCntr = MAXRETRIES;
286
287 do
288 {
289 RetVal = i2c_command( i2cdata.slave
290 , i2cdata.wbuf
291 , i2cdata.wlen
292 , NULL
293 , 0
294 );
295 } while ( ( EI2CNOERRORS != RetVal )
296 &&( --RetryCntr )
297 );
298 }
299 break;
300
301 case I2C_WRITEREAD:
302 copy_from_user( (char*)&i2cdata, (char*)arg, sizeof( I2C_DATA ) );
303 {
304 int RetryCntr = MAXRETRIES;
305
306 do
307 {
308 RetVal = i2c_command( i2cdata.slave
309 , i2cdata.wbuf
310 , i2cdata.wlen
311 , i2cdata.rbuf
312 , i2cdata.rlen
313 );
314 } while ( ( EI2CNOERRORS != RetVal )
315 &&( --RetryCntr )
316 );
317 }
318 copy_to_user( (char*)arg, (char*)&i2cdata, sizeof( I2C_DATA ) );
319 break;
320
321 default:
322 RetVal = -EINVAL;
323 }
324
325 return ( -RetVal );
326 } /* i2c_ioctl */
327
328
329 /*#---------------------------------------------------------------------------
330 *#
331 *# FUNCTION NAME: i2c_command
332 *#
333 *# DESCRIPTION : general routine to read/write bytes from an I2C device
334 *#
335 *# 'i2c_command()' sends wlen bytes to the I2c bus and receives
336 *# rlen bytes from the I2c bus.
337 *# The data to be send must be placed in wbuf[ 0 ] upto wbuf[ wlen - 1 ).
338 *# The data to be received is assembled in rbuf[ 0 ] upto rbuf[ rlen - 1 ].
339 *#
340 *# If no data is to be sent or received, put appropriate buffer parameter
341 *# to "NULL" and appropriate length parameter to "0".
342 *#
343 *# PARAMETERS : slave = slave address of the I2C device
344 *# wbuf = address of first element of write buffer (wbuf)
345 *# wlen = number of bytes to be written to slave
346 *# rbuf = address of first element of read buffer (rbuf)
347 *# rlen = number of bytes to be read from slave
348 *#
349 *# RETURN :
350 *# EI2CNOERRORS: I2C communication went fine
351 *# EI2CBUSNFREE: I2C bus is not free
352 *# EI2CWADDRESS: I2C write address failed
353 *# EI2CRADDRESS: I2C read address failed
354 *# EI2CSENDDATA: I2C send data failed
355 *# EI2CRECVDATA: I2C receive data failed
356 *# EI2CSTRTCOND: I2C start condition failed
357 *# EI2CRSTACOND: I2C repeated start condition failed
358 *# EI2CSTOPCOND: I2C stop condition failed
359 *# EI2CNOSNDBYT: I2C no bytes to be sent
360 *# EI2CNOSNDBUF: I2C no send buffer defined
361 *# EI2CNORCVBYT: I2C no bytes to be received
362 *# EI2CNORCVBUF: I2C no receive buffer defined
363 *# EI2CNOACKNLD: I2C no acknowledge received
364 *#
365 *# REMARK :
366 *# First, the send part is completed.
367 *# In the send routine, there is no stop generated. This is because maybe
368 *# a repeated start condition must be generated.
369 *# This happens when we want to receive some data from the I2c bus. If not,
370 *# at the end of the general I2c loop the stopcondition is generated.
371 *# If, on the contrary, there are a number of bytes to be received, a new
372 *# startcondition is generated in the 'if' part of the main I2c routine,
373 *# which controls the receiving part.
374 *# Only when the receiving of data is finished, a final stopcondition is
375 *# generated.
376 *#
377 *#---------------------------------------------------------------------------
378 */
379 static int i2c_command( unsigned char slave
380 , unsigned char* wbuf
381 , unsigned char wlen
382 , unsigned char* rbuf
383 , unsigned char rlen
384 )
385 {
386 /* Check arguments and report error if relevant... */
387 if ( ( wlen > 0 ) && ( wbuf == NULL ) )
388 {
389 printk( KERN_DEBUG "I2C: EI2CNOSNDBUF\n" );
390 return ( EI2CNOSNDBUF );
391 }
392 else if ( ( wlen == 0 ) && ( wbuf != NULL ) )
393 {
394 printk( KERN_DEBUG "I2C: EI2CNOSNDBYT\n" );
395 return ( EI2CNOSNDBYT );
396 }
397 else if ( ( rlen > 0 ) && ( rbuf == NULL ) )
398 {
399 printk( KERN_DEBUG "I2C: EI2CNORCVBUF\n" );
400 return ( EI2CNORCVBUF );
401 }
402 else if ( ( rlen == 0 ) && ( rbuf != NULL ) )
403 {
404 printk( KERN_DEBUG "I2C: EI2CNORCVBYT\n" );
405 return ( EI2CNORCVBYT );
406 }
407 else if ( EI2CBUSNFREE == i2c_bus_free_check( MAXBUSFREERETRIES ) )
408 {
409 /* There's no need to try more, since we weren't even
410 * able to start the I2C communication.
411 * So, no IRQ flags are stored yet, no changes to any other
412 * stuff like START, STOP, SENDBYTES...
413 * Result, simply write down the error and return the correct error code.
414 */
415 printk( KERN_DEBUG "I2C: EI2CBUSNFREE\n" );
416 return ( EI2CBUSNFREE );
417 }
418 else
419 {
420 /* Finally... We made it... */
421 unsigned long irqflags = 0;
422
423 /* we don't like to be interrupted */
424 local_irq_save( irqflags );
425
426 /* Check if there are bytes to be send,
427 * or if you immediately want to receive data.
428 */
429 if ( 0 < wlen )
430 {
431 /* start I2C communication */
432 if ( EI2CNOERRORS != i2c_start() )
433 {
434 return ( i2c_finalise( "I2C: EI2CSTRTCOND\n", irqflags )
435 , EI2CSTRTCOND
436 );
437 }
438
439 /* send slave address: xxxxxxx0B (last bit must be zero) */
440 if ( EI2CNOERRORS != i2c_outbyte( slave & WRITEADDRESS_MASK ) )
441 {
442 return ( i2c_finalise( "I2C: EI2CWADDRESS\n", irqflags )
443 , EI2CWADDRESS
444 );
445 }
446
447 while ( wlen-- )
448 {
449 /* send register data */
450 if ( EI2CNOERRORS != i2c_outbyte( *wbuf ) && wlen )
451 {
452 return ( i2c_finalise( "I2C: EI2CSENDDATA\n", irqflags )
453 , EI2CSENDDATA
454 );
455 }
456
457 wbuf++;
458 };
459
460 i2c_delay( TLOW );
461 }
462
463 /*
464 * Receiving data from I2c_bus
465 * If there are bytes to be received, a new start condition is
466 * generated => Repeated Startcondition.
467 * A final stopcondition is generated at the end of the main I2c
468 * routine.
469 */
470 if ( 0 < rlen )
471 {
472 /*
473 * Generate start condition if wlen == 0
474 * or repeated start condition if wlen != 0...
475 */
476 if ( EI2CNOERRORS != i2c_start() )
477 {
478 return ( i2c_finalise( ( ( 0 < wlen )
479 ? "I2C: EI2CRSTACOND\n"
480 : "I2C: EI2CSTRTCOND\n"
481 )
482 , irqflags
483 )
484 , ( ( 0 < wlen ) ? EI2CRSTACOND : EI2CSTRTCOND )
485 );
486 }
487
488 /* Send ReadAddress: xxxxxxx1B (last bit must be one) */
489 if ( EI2CNOERRORS != i2c_outbyte( slave | READADDRESS_MASK ) )
490 {
491 return ( i2c_finalise( "I2C: EI2CRADDRESS\n", irqflags )
492 , EI2CRADDRESS
493 );
494 }
495
496 while ( rlen-- )
497 {
498 /* fetch register */
499 *rbuf = i2c_inbyte();
500 rbuf++;
501
502 /* last received byte needs to be NACK-ed instead of ACK-ed */
503 if ( rlen )
504 {
505 i2c_sendack();
506 }
507 else
508 {
509 i2c_sendnack();
510 }
511 };
512 }
513
514 /* Generate final stop condition */
515 if ( EI2CNOERRORS != i2c_stop() )
516 {
517 return ( i2c_finalise( "I2C CMD: EI2CSTOPCOND\n", irqflags )
518 , EI2CSTOPCOND
519 );
520 }
521
522 /* enable interrupt again */
523 local_irq_restore( irqflags );
524 }
525
526 return ( EI2CNOERRORS );
527 } /* i2c_command */
528
529
530 /*#---------------------------------------------------------------------------
531 *#
532 *# FUNCTION NAME: i2c_bus_free_check
533 *#
534 *# DESCRIPTION : checks if the I2C bus is free before starting
535 *# an I2C communication
536 *#
537 *# PARAMETERS : maxretries, the number of times we will try to release
538 *# the I2C bus
539 *#
540 *# RETURN : I2cStatus_I2cBusNotFreeError in case the bus is not free,
541 *# I2cStatus_I2cNoError otherwise
542 *#
543 *#---------------------------------------------------------------------------
544 */
545 static int i2c_bus_free_check( unsigned char maxretries )
546 {
547 i2c_sda_dir_in(); /* Release SDA line */
548 i2c_set_scl( SCL_HIGH ); /* put SCL line high */
549
550 i2c_delay( WAITONEUS );
551
552 while ( ( !i2c_sda_is_high() || !i2c_scl_is_high() )
553 &&( maxretries-- )
554 )
555 {
556 /* Try to release I2C bus by generating STOP conditions */
557 i2c_stop();
558 }
559
560 if ( 0 == maxretries )
561 {
562 printk( KERN_DEBUG "I2C: EI2CBUSNFREE\n" );
563 return ( EI2CBUSNFREE );
564 }
565 else
566 {
567 return ( EI2CNOERRORS );
568 }
569 } /* i2c_bus_free_check */
570
571
572 static void i2c_finalise( const char* errortxt
573 , unsigned long irqflags
574 )
575 {
576 printk( KERN_DEBUG "%s", errortxt );
577 local_irq_restore( irqflags );
578 /* The least we can do when things go terribly wrong,
579 * is to try to release the bus.
580 * If this fails, well, then I don't know
581 * what I can do more for the moment...
582 */
583 (void)i2c_bus_free_check( MAXBUSFREERETRIES );
584 } /* i2c_finalise */
585
586
587 static struct file_operations i2c_fops =
588 {
589 .owner = THIS_MODULE
590 , .ioctl = i2c_ioctl
591 , .open = i2c_open
592 , .release = i2c_release
593 };
594
595
596 /***********************************************************************/
597 /************* EXTERNAL FUNCTION DEFINITION SECTION ********************/
598 /***********************************************************************/
599
600 /*#---------------------------------------------------------------------------
601 *#
602 *# FUNCTION NAME: i2c_init
603 *#
604 *# DESCRIPTION : initialises the I2C device driver
605 *#
606 *# PARAMETERS :
607 *#
608 *#---------------------------------------------------------------------------
609 */
610 int __init i2c_init( void )
611 {
612 static int res = 0;
613 static int first = 1;
614
615 if ( !first )
616 {
617 return res;
618 }
619
620 first = 0;
621
622 /* Setup and enable the Port B I2C interface */
623
624 #ifndef CONFIG_ETRAX_I2C_USES_PB_NOT_PB_I2C
625 /* here, we're using the dedicated I2C pins of FoxBoard */
626 if ( ( res = cris_request_io_interface( if_i2c, "I2C" ) ) )
627 {
628 printk( KERN_CRIT "i2c_init: Failed to get IO interface\n" );
629 return res;
630 }
631
632 *R_PORT_PB_I2C = port_pb_i2c_shadow |=
633 IO_STATE( R_PORT_PB_I2C, i2c_en, on ) |
634 IO_FIELD( R_PORT_PB_I2C, i2c_d, 1 ) |
635 IO_FIELD( R_PORT_PB_I2C, i2c_set_scl, 1 ) |
636 IO_STATE( R_PORT_PB_I2C, i2c_oe_, enable );
637
638 port_pb_dir_shadow &= ~IO_MASK( R_PORT_PB_DIR, dir0 );
639 port_pb_dir_shadow &= ~IO_MASK( R_PORT_PB_DIR, dir1 );
640
641 *R_PORT_PB_DIR = ( port_pb_dir_shadow |=
642 IO_STATE( R_PORT_PB_DIR, dir0, input ) |
643 IO_STATE( R_PORT_PB_DIR, dir1, output ) );
644 #else
645 /* If everything goes fine, res = 0, meaning "if" fails =>
646 * will do the "else" too and as such initialise the clock port...
647 * Clever trick!
648 */
649 if ( ( res = cris_io_interface_allocate_pins( if_i2c
650 , 'b'
651 , CONFIG_ETRAX_I2C_DATA_PORT
652 , CONFIG_ETRAX_I2C_DATA_PORT
653 )
654 )
655 )
656 {
657 printk( KERN_WARNING "i2c_init: Failed to get IO pin for I2C data port\n" );
658 return ( res );
659 }
660 /* Same here...*/
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 )
666 )
667 )
668 {
669 cris_io_interface_free_pins( if_i2c
670 , 'b'
671 , CONFIG_ETRAX_I2C_DATA_PORT
672 , CONFIG_ETRAX_I2C_DATA_PORT
673 );
674 printk( KERN_WARNING "i2c_init: Failed to get IO pin for I2C clk port\n" );
675 }
676 #endif
677
678 return ( res );
679 } /* i2c_init */
680
681
682 /*#---------------------------------------------------------------------------
683 *#
684 *# FUNCTION NAME: i2c_register
685 *#
686 *# DESCRIPTION : this registers the i2c driver as a character device
687 *#
688 *# PARAMETERS :
689 *#
690 *#---------------------------------------------------------------------------
691 */
692
693 static struct class *i2c_class;
694
695 static int __init i2c_register( void )
696 {
697 int res;
698 #ifdef CONFIG_ETRAX_I2C_DYN_ALLOC
699 dev_t devt;
700 struct cdev *my_i2cdev = NULL;
701 #endif
702
703 res = i2c_init();
704
705 if ( res < 0 )
706 {
707 return res;
708 }
709
710 #ifdef CONFIG_ETRAX_I2C_DYN_ALLOC
711 res = alloc_chrdev_region( &devt, 0, 1, i2c_name );
712
713 if ( res < 0 )
714 {
715 printk( KERN_DEBUG "I2C: EI2CNOMNUMBR\n" );
716 return ( res );
717 }
718
719 my_i2cdev = cdev_alloc();
720 my_i2cdev->ops = &i2c_fops;
721 my_i2cdev->owner = THIS_MODULE;
722
723 /* make device "alive" */
724 res = cdev_add( my_i2cdev, devt, 1 );
725
726 if ( res < 0 )
727 {
728 printk( KERN_DEBUG "I2C: EI2CDADDFAIL\n" );
729 return ( res );
730 }
731
732 int i2c_major = MAJOR( devt );
733 #else
734 res = register_chrdev( I2C_MAJOR, i2c_name, &i2c_fops );
735
736 if ( res < 0 )
737 {
738 printk( KERN_ERR "i2c: couldn't get a major number.\n" );
739 return res;
740 }
741
742 int i2c_major = I2C_MAJOR;
743 #endif
744
745 printk( KERN_INFO "I2C: driver v2.3, (c) 1999-2004 Axis Communications AB\n" );
746 printk( KERN_INFO "I2C: Improvements by Geert Vancompernolle, Positive Going, BK srl\n" );
747
748 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
749 printk( KERN_INFO "I2C: with master/slave delay patch\n" );
750 #endif
751
752 i2c_class = class_create (THIS_MODULE, "i2c_etrax");
753 device_create (i2c_class, NULL,
754 MKDEV(i2c_major,0), NULL, i2c_name);
755
756 return ( 0 );
757 } /* i2c_register */
758
759
760 /*#---------------------------------------------------------------------------
761 *#
762 *# FUNCTION NAME: i2c_start
763 *#
764 *# DESCRIPTION : generate i2c start condition
765 *#
766 *# PARAMETERS : none
767 *#
768 *# RETURN : EI2CNOERRORS if OK, EI2CSTRTCOND otherwise
769 *#
770 *#---------------------------------------------------------------------------
771 */
772 int i2c_start( void )
773 {
774 /* Set SCL=1, SDA=1 */
775 i2c_sda_dir_out();
776 i2c_set_sda( SDA_HIGH );
777 i2c_delay( WAITONEUS );
778 i2c_set_scl( SCL_HIGH );
779 i2c_delay( WAITONEUS );
780
781 /* Set SCL=1, SDA=0 */
782 i2c_set_sda( SDA_LOW );
783 i2c_delay( THDSTA );
784
785 /* Set SCL=0, SDA=0 */
786 i2c_set_scl( SCL_LOW );
787 /* We can take 1 us less than defined in spec (5 us), since the next action
788 * will be to set the dataline high or low and this action is 1 us
789 * before the clock is put high, so that makes our 5 us.
790 */
791 i2c_delay( TLOW - WAITONEUS );
792
793 if ( i2c_sda_is_high() || i2c_scl_is_high() )
794 {
795 printk( KERN_DEBUG "I2C: EI2CSTRTCOND\n" );
796 return ( EI2CSTRTCOND );
797 }
798
799 return ( EI2CNOERRORS );
800 } /* i2c_start */
801
802
803 /*#---------------------------------------------------------------------------
804 *#
805 *# FUNCTION NAME: i2c_stop
806 *#
807 *# DESCRIPTION : generate i2c stop condition
808 *#
809 *# PARAMETERS : none
810 *#
811 *# RETURN : none
812 *#
813 *#---------------------------------------------------------------------------
814 */
815 int i2c_stop( void )
816 {
817 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
818 int n=MAXSCLRETRIES;
819 #endif
820 i2c_sda_dir_out();
821
822 /* Set SCL=0, SDA=0 */
823 /* Don't change order, otherwise you might generate a start condition! */
824 i2c_set_scl( SCL_LOW );
825 i2c_delay( WAITONEUS );
826 i2c_set_sda( SDA_LOW );
827 i2c_delay( WAITONEUS );
828
829 /* Set SCL=1, SDA=0 */
830
831 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
832 i2c_set_scl( SCL_HIGH );
833 i2c_scl_dir_in();
834 for( ; n>0; n-- )
835 {
836 if( i2c_scl_is_high() )
837 break;
838 i2c_delay( TSUSTO );
839 }
840
841 i2c_scl_dir_out();
842 #else
843 i2c_set_scl( SCL_HIGH );
844 #endif
845 i2c_delay( TSUSTO );
846
847 /* Set SCL=1, SDA=1 */
848 i2c_set_sda( SDA_HIGH );
849 i2c_delay( TBUF );
850
851 i2c_sda_dir_in();
852
853 if ( !i2c_sda_is_high() || !i2c_scl_is_high() )
854 {
855 return ( EI2CSTOPCOND );
856 }
857
858 return ( EI2CNOERRORS );
859 } /* i2c_stop */
860
861
862 /*#---------------------------------------------------------------------------
863 *#
864 *# FUNCTION NAME: i2c_outbyte
865 *#
866 *# DESCRIPTION : write a byte to the i2c interface
867 *#
868 *# PARAMETERS : x: byte to be sent on the I2C bus
869 *#
870 *# RETURN : none
871 *#
872 *#---------------------------------------------------------------------------
873 */
874 int i2c_outbyte( unsigned char x )
875 {
876 int i;
877
878 i2c_sda_dir_out();
879
880 for ( i = 0; i < 8; i++ )
881 {
882 if ( x & 0x80 )
883 {
884 i2c_set_sda( SDA_HIGH );
885 }
886 else
887 {
888 i2c_set_sda( SDA_LOW );
889 }
890
891 i2c_delay( TSUDAT );
892 i2c_set_scl( SCL_HIGH );
893 i2c_delay( THIGH );
894 i2c_set_scl( SCL_LOW );
895 i2c_delay( TSUDAT );
896 i2c_set_sda( SDA_LOW );
897 /* There should be only 5 us between falling edge and new rising
898 * edge of clock pulse.
899 * Since we spend already 1 us since clock edge was low, there are
900 * only ( TLOW - TSUDAT ) us left.
901 * Next to this, since the data line will be set up 1 us before the
902 * clock line is set up, we can reduce the delay with another us.
903 */
904 i2c_delay( TLOW - TSUDAT - WAITONEUS );
905 x <<= 1;
906 }
907
908 /* enable input */
909 i2c_sda_dir_in();
910
911 if ( !i2c_getack() )
912 {
913 return( EI2CNOACKNLD );
914 }
915
916 return ( EI2CNOERRORS );
917 } /* i2c_outbyte */
918
919
920 /*#---------------------------------------------------------------------------
921 *#
922 *# FUNCTION NAME: i2c_inbyte
923 *#
924 *# DESCRIPTION : read a byte from the i2c interface
925 *#
926 *# PARAMETERS : none
927 *#
928 *# RETURN : returns the byte read from the I2C device
929 *#
930 *#---------------------------------------------------------------------------
931 */
932 unsigned char i2c_inbyte( void )
933 {
934 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
935 int n=MAXSCLRETRIES;
936 #endif
937 unsigned char aBitByte = 0;
938 unsigned char Mask = 0x80; /* !!! ATTENTION: do NOT use 'char', otherwise shifting is wrong!!! */
939 /* Must be UNSIGNED, not SIGNED! */
940
941
942 /* Switch off I2C to get bit */
943 i2c_disable();
944 i2c_sda_dir_in();
945
946 while ( Mask != 0 )
947 {
948 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
949 i2c_scl_dir_in();
950 for( ; n>0; n-- )
951 {
952 if( i2c_scl_is_high() )
953 break;
954 i2c_delay( THIGH );
955 }
956
957 i2c_set_scl( SCL_HIGH );
958 i2c_scl_dir_out();
959 #else
960 i2c_set_scl( SCL_HIGH );
961 #endif
962 i2c_delay( THIGH );
963
964 if ( i2c_sda_is_high() )
965 {
966 aBitByte |= Mask;
967 }
968
969 i2c_set_scl( SCL_LOW );
970
971 Mask >>= 1;
972
973 i2c_delay( TLOW );
974 }
975
976 /*
977 * we leave the clock low, getbyte is usually followed
978 * by sendack/nack, they assume the clock to be low
979 */
980 return ( aBitByte );
981 } /* i2c_inbyte */
982
983
984 /*#---------------------------------------------------------------------------
985 *#
986 *# FUNCTION NAME: i2c_getack
987 *#
988 *# DESCRIPTION : checks if ack was received from ic2
989 *#
990 *# PARAMETERS : none
991 *#
992 *# RETURN : returns the ack state of the I2C device
993 *#
994 *#---------------------------------------------------------------------------
995 */
996 int i2c_getack( void )
997 {
998 int ack = 1;
999 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
1000 int n=MAXSCLRETRIES;
1001 #endif
1002
1003 /* generate ACK clock pulse */
1004 i2c_set_scl( SCL_HIGH );
1005
1006 /* switch off I2C */
1007 i2c_disable();
1008
1009 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
1010 /* set clock low */
1011 i2c_set_scl( SCL_LOW );
1012
1013 /* now wait for ack */
1014 i2c_delay( THIGH );
1015
1016 /* set clock as input */
1017 i2c_scl_dir_in();
1018
1019 /* wait for clock to rise (n=MAXSCLRETRIES) */
1020 for( ; n>0; n-- )
1021 {
1022 if( i2c_scl_is_high() )
1023 break;
1024 i2c_delay( THIGH );
1025 }
1026
1027 i2c_set_scl( SCL_HIGH );
1028
1029 i2c_scl_dir_out();
1030
1031 i2c_delay( THIGH );
1032 #else
1033 /* now wait for ack */
1034 i2c_delay( THIGH );
1035 #endif
1036
1037 /* check for ack: if SDA is high, then NACK, else ACK */
1038 if ( i2c_sda_is_high() )
1039 {
1040 ack = 0;
1041 }
1042 else
1043 {
1044 ack = 1;
1045 }
1046
1047 /* end clock pulse */
1048 i2c_enable();
1049 i2c_set_scl( SCL_LOW );
1050 i2c_sda_dir_out();
1051 i2c_set_sda( SDA_LOW );
1052
1053 /* Since we "lost" already THDDAT time, we can subtract it here... */
1054 i2c_delay( TLOW - THDDAT );
1055
1056 return ( ack );
1057 } /* i2c_getack */
1058
1059
1060 /*#---------------------------------------------------------------------------
1061 *#
1062 *# FUNCTION NAME: i2c_sendack
1063 *#
1064 *# DESCRIPTION : sends ACK on received data
1065 *#
1066 *# PARAMETERS : none
1067 *#
1068 *# RETURN : none
1069 *#
1070 *#---------------------------------------------------------------------------
1071 */
1072 void i2c_sendack( void )
1073 {
1074 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
1075 int n=MAXSCLRETRIES;
1076 #endif
1077
1078 /* enable output */
1079 /* Clock has been set to TLOW already at end of i2c_inbyte()
1080 * and i2c_outbyte(), so no need to do it again.
1081 */
1082 i2c_sda_dir_out();
1083 /* set ack pulse low */
1084 i2c_set_sda( SDA_LOW );
1085 /* generate clock pulse */
1086 i2c_delay( TSUDAT );
1087
1088 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
1089 i2c_scl_dir_in();
1090 /* wait for clock to rise (n=MAXSCLRETRIES) */
1091 for( ; n>0; n-- )
1092 {
1093 if( i2c_scl_is_high() )
1094 break;
1095 i2c_delay( THIGH );
1096 }
1097
1098 i2c_set_scl( SCL_HIGH );
1099 i2c_scl_dir_out();
1100 i2c_delay( THIGH );
1101 #else
1102 i2c_set_scl( SCL_HIGH );
1103
1104 i2c_delay( THIGH );
1105 #endif
1106 i2c_set_scl( SCL_LOW );
1107 i2c_delay( THDDAT );
1108 /* reset data out */
1109 i2c_set_sda( SDA_HIGH );
1110 /* Subtract time spend already when waited to put SDA high */
1111 i2c_delay( TLOW - THDDAT );
1112
1113 /* release the SDA line */
1114 i2c_sda_dir_in();
1115 } /* i2c_sendack */
1116
1117
1118 /*#---------------------------------------------------------------------------
1119 *#
1120 *# FUNCTION NAME: i2c_sendnack
1121 *#
1122 *# DESCRIPTION : sends NACK on received data
1123 *#
1124 *# PARAMETERS : none
1125 *#
1126 *# RETURN : none
1127 *#
1128 *#---------------------------------------------------------------------------
1129 */
1130 void i2c_sendnack( void )
1131 {
1132 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
1133 int n=MAXSCLRETRIES;
1134 #endif
1135
1136 /* make sure the SDA line is set high prior to activation of the output.
1137 * this way, you avoid an unnecessary peak to ground when a NACK has to
1138 * be created.
1139 */
1140 /* set data high */
1141 i2c_set_sda( SDA_HIGH );
1142 /* enable output */
1143 i2c_sda_dir_out();
1144
1145 /* generate clock pulse */
1146 i2c_delay( TSUDAT );
1147
1148 #ifdef CONFIG_ETRAX_I2C_SLAVE_DELAY
1149 i2c_scl_dir_in();
1150 /* wait for clock to rise (n=MAXSCLRETRIES) */
1151 for( ; n>0; n-- )
1152 {
1153 if( i2c_scl_is_high() )
1154 break;
1155 i2c_delay( THIGH );
1156 }
1157
1158 i2c_set_scl( SCL_HIGH );
1159 i2c_scl_dir_out();
1160 i2c_delay( THIGH );
1161 #else
1162 i2c_set_scl( SCL_HIGH );
1163
1164 i2c_delay( THIGH );
1165 #endif
1166 i2c_set_scl( SCL_LOW );
1167 i2c_delay( TSUDAT );
1168 i2c_set_sda( SDA_LOW );
1169 i2c_delay( TLOW - TSUDAT );
1170
1171 /* There's no need to change the direction of SDA to "in" again,
1172 * since a NACK is always followed by a stop condition.
1173 * A STOP condition will put the direction of SDA back to "out"
1174 * resulting in a useless SDA "dip" on the line...
1175 */
1176 /* i2c_sda_dir_in(); */
1177 } /* i2c_sendnack */
1178
1179
1180 /*#---------------------------------------------------------------------------
1181 *#
1182 *# FUNCTION NAME: i2c_writereg
1183 *#
1184 *# DESCRIPTION : writes a value to a register of an I2C device
1185 *#
1186 *# PARAMETERS : theSlave = slave address of the I2C device
1187 *# theReg = register of the I2C device that needs to be written
1188 *# theValue = value to be written to the register
1189 *#
1190 *# RETURN : returns OR-ed result of the write action:
1191 *# 0 = Ok
1192 *# 1 = Slave_NoAck
1193 *# 2 = Reg_NoAck
1194 *# 4 = Val_NoAck
1195 *#
1196 *#---------------------------------------------------------------------------
1197 */
1198 int i2c_writereg( unsigned char theSlave
1199 , unsigned char theReg
1200 , unsigned char theValue
1201 )
1202 {
1203 int error, cntr = 3;
1204 unsigned long flags;
1205
1206 spin_lock( &i2c_lock );
1207
1208 do
1209 {
1210 error = 0;
1211 /* we don't like to be interrupted */
1212 local_irq_save( flags );
1213
1214 i2c_start();
1215 /* send slave address */
1216 if ( EI2CNOACKNLD == i2c_outbyte( theSlave & 0xfe ) )
1217 {
1218 error = 1;
1219 }
1220
1221 /* now select register */
1222 if ( EI2CNOACKNLD == i2c_outbyte( theReg ) )
1223 {
1224 error |= 2;
1225 }
1226
1227 /* send register register data */
1228 if ( EI2CNOACKNLD == i2c_outbyte( theValue ) )
1229 {
1230 error |= 4;
1231 }
1232
1233 /* end byte stream */
1234 i2c_stop();
1235 /* enable interrupt again */
1236 local_irq_restore( flags );
1237
1238 } while ( error && cntr-- );
1239
1240 i2c_delay( TLOW );
1241
1242 spin_unlock( &i2c_lock );
1243
1244 return ( -error );
1245 } /* i2c_writereg */
1246
1247
1248 /*#---------------------------------------------------------------------------
1249 *#
1250 *# FUNCTION NAME: i2c_readreg
1251 *#
1252 *# DESCRIPTION : reads the value from a certain register of an I2C device.
1253 *# Function first writes the register that it wants to read
1254 *# later on.
1255 *#
1256 *# PARAMETERS : theSlave = slave address of the I2C device
1257 *# theReg = register of the I2C device that needs to be written
1258 *#
1259 *# RETURN : returns OR-ed result of the write action:
1260 *# 0 = Ok
1261 *# 1 = Slave_NoAck
1262 *# 2 = Reg_NoAck
1263 *# 4 = Val_NoAck
1264 *#
1265 *#---------------------------------------------------------------------------
1266 */
1267 unsigned char i2c_readreg( unsigned char theSlave
1268 , unsigned char theReg
1269 )
1270 {
1271 unsigned char b = 0;
1272 int error, cntr = 3;
1273 unsigned long flags;
1274
1275 spin_lock( &i2c_lock );
1276
1277 do
1278 {
1279 error = 0;
1280
1281 /* we don't like to be interrupted */
1282 local_irq_save( flags );
1283
1284 /* generate start condition */
1285 i2c_start();
1286
1287 /* send slave address */
1288 if ( EI2CNOACKNLD == i2c_outbyte( theSlave & 0xfe ) )
1289 {
1290 error = 1;
1291 }
1292
1293 /* now select register */
1294 i2c_sda_dir_out();
1295
1296 if ( EI2CNOACKNLD == i2c_outbyte( theReg ) )
1297 {
1298 error |= 2;
1299 }
1300
1301 /* repeat start condition */
1302 i2c_delay( TLOW );
1303 i2c_start();
1304
1305 /* send slave address */
1306 if ( EI2CNOACKNLD == i2c_outbyte( theSlave | 0x01 ) )
1307 {
1308 error |= 1;
1309 }
1310
1311 /* fetch register */
1312 b = i2c_inbyte();
1313 /*
1314 * last received byte needs to be nacked
1315 * instead of acked
1316 */
1317 i2c_sendnack();
1318
1319 /* end sequence */
1320 i2c_stop();
1321
1322 /* enable interrupt again */
1323 local_irq_restore( flags );
1324
1325 } while ( error && cntr-- );
1326
1327 spin_unlock( &i2c_lock );
1328
1329 return ( b );
1330 } /* i2c_readreg */
1331
1332
1333 /*#---------------------------------------------------------------------------
1334 *#
1335 *# FUNCTION NAME: i2c_read
1336 *#
1337 *# DESCRIPTION :
1338 *#
1339 *# PARAMETERS :
1340 *#
1341 *# RETURN :
1342 *#
1343 *#---------------------------------------------------------------------------
1344 */
1345 int i2c_read( unsigned char slave, unsigned char* rbuf, unsigned char rlen )
1346 {
1347 return ( i2c_command( slave, NULL, 0, rbuf, rlen ) );
1348 } /* i2c_read */
1349
1350
1351 /*#---------------------------------------------------------------------------
1352 *#
1353 *# FUNCTION NAME: i2c_write
1354 *#
1355 *# DESCRIPTION :
1356 *#
1357 *# PARAMETERS :
1358 *#
1359 *# RETURN :
1360 *#
1361 *#---------------------------------------------------------------------------
1362 */
1363 int i2c_write( unsigned char slave, unsigned char* wbuf, unsigned char wlen )
1364 {
1365 return ( i2c_command( slave, wbuf, wlen, NULL, 0 ) );
1366 } /* i2c_write */
1367
1368
1369 /*#---------------------------------------------------------------------------
1370 *#
1371 *# FUNCTION NAME: i2c_writeread
1372 *#
1373 *# DESCRIPTION :
1374 *#
1375 *# PARAMETERS :
1376 *#
1377 *# RETURN :
1378 *#
1379 *#---------------------------------------------------------------------------
1380 */
1381 int i2c_writeread( unsigned char slave
1382 , unsigned char* wbuf
1383 , unsigned char wlen
1384 , unsigned char* rbuf
1385 , unsigned char rlen
1386 )
1387 {
1388 return ( i2c_command( slave, wbuf, wlen, rbuf, rlen ) );
1389 } /* i2c_writeread */
1390
1391
1392 /*#---------------------------------------------------------------------------
1393 *#
1394 *# FUNCTION NAME: module_init
1395 *#
1396 *# DESCRIPTION : this makes sure that i2c_register is called during boot
1397 *#
1398 *# PARAMETERS :
1399 *#
1400 *#---------------------------------------------------------------------------
1401 */
1402 module_init( i2c_register );
1403
1404 /****************** END OF FILE i2c.c ********************************/
This page took 0.107142 seconds and 5 git commands to generate.