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