Project

General

Profile

RE: I2C Access with 5CSX Development Board » i2c-dev.h

Michael Williamson, 06/17/2014 11:04 AM

 
1
/*
2
    i2c-dev.h - i2c-bus driver, char device interface
3

    
4
    Copyright (C) 1995-97 Simon G. Vogl
5
    Copyright (C) 1998-99 Frodo Looijaard <frodol@dds.nl>
6

    
7
    This program is free software; you can redistribute it and/or modify
8
    it under the terms of the GNU General Public License as published by
9
    the Free Software Foundation; either version 2 of the License, or
10
    (at your option) any later version.
11

    
12
    This program is distributed in the hope that it will be useful,
13
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU General Public License for more details.
16

    
17
    You should have received a copy of the GNU General Public License
18
    along with this program; if not, write to the Free Software
19
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
    MA 02110-1301 USA.
21
*/
22

    
23
/* $Id: i2c-dev.h 5361 2008-10-19 09:47:02Z khali $ */
24

    
25
#ifndef LIB_I2CDEV_H
26
#define LIB_I2CDEV_H
27

    
28
#include <linux/types.h>
29
#include <sys/ioctl.h>
30
#include <string.h>
31

    
32

    
33
/* -- i2c.h -- */
34

    
35

    
36
/*
37
 * I2C Message - used for pure i2c transaction, also from /dev interface
38
 */
39
struct i2c_msg {
40
	__u16 addr;	/* slave address			*/
41
	unsigned short flags;		
42
#define I2C_M_TEN	0x10	/* we have a ten bit chip address	*/
43
#define I2C_M_RD	0x01
44
#define I2C_M_NOSTART	0x4000
45
#define I2C_M_REV_DIR_ADDR	0x2000
46
#define I2C_M_IGNORE_NAK	0x1000
47
#define I2C_M_NO_RD_ACK		0x0800
48
	short len;		/* msg length				*/
49
	char *buf;		/* pointer to msg data			*/
50
};
51

    
52
/* To determine what functionality is present */
53

    
54
#define I2C_FUNC_I2C			0x00000001
55
#define I2C_FUNC_10BIT_ADDR		0x00000002
56
#define I2C_FUNC_PROTOCOL_MANGLING	0x00000004 /* I2C_M_{REV_DIR_ADDR,NOSTART,..} */
57
#define I2C_FUNC_SMBUS_PEC		0x00000008
58
#define I2C_FUNC_SMBUS_BLOCK_PROC_CALL	0x00008000 /* SMBus 2.0 */
59
#define I2C_FUNC_SMBUS_QUICK		0x00010000 
60
#define I2C_FUNC_SMBUS_READ_BYTE	0x00020000 
61
#define I2C_FUNC_SMBUS_WRITE_BYTE	0x00040000 
62
#define I2C_FUNC_SMBUS_READ_BYTE_DATA	0x00080000 
63
#define I2C_FUNC_SMBUS_WRITE_BYTE_DATA	0x00100000 
64
#define I2C_FUNC_SMBUS_READ_WORD_DATA	0x00200000 
65
#define I2C_FUNC_SMBUS_WRITE_WORD_DATA	0x00400000 
66
#define I2C_FUNC_SMBUS_PROC_CALL	0x00800000 
67
#define I2C_FUNC_SMBUS_READ_BLOCK_DATA	0x01000000 
68
#define I2C_FUNC_SMBUS_WRITE_BLOCK_DATA 0x02000000 
69
#define I2C_FUNC_SMBUS_READ_I2C_BLOCK	0x04000000 /* I2C-like block xfer  */
70
#define I2C_FUNC_SMBUS_WRITE_I2C_BLOCK	0x08000000 /* w/ 1-byte reg. addr. */
71

    
72
#define I2C_FUNC_SMBUS_BYTE (I2C_FUNC_SMBUS_READ_BYTE | \
73
                             I2C_FUNC_SMBUS_WRITE_BYTE)
74
#define I2C_FUNC_SMBUS_BYTE_DATA (I2C_FUNC_SMBUS_READ_BYTE_DATA | \
75
                                  I2C_FUNC_SMBUS_WRITE_BYTE_DATA)
76
#define I2C_FUNC_SMBUS_WORD_DATA (I2C_FUNC_SMBUS_READ_WORD_DATA | \
77
                                  I2C_FUNC_SMBUS_WRITE_WORD_DATA)
78
#define I2C_FUNC_SMBUS_BLOCK_DATA (I2C_FUNC_SMBUS_READ_BLOCK_DATA | \
79
                                   I2C_FUNC_SMBUS_WRITE_BLOCK_DATA)
80
#define I2C_FUNC_SMBUS_I2C_BLOCK (I2C_FUNC_SMBUS_READ_I2C_BLOCK | \
81
                                  I2C_FUNC_SMBUS_WRITE_I2C_BLOCK)
82

    
83
/* Old name, for compatibility */
84
#define I2C_FUNC_SMBUS_HWPEC_CALC	I2C_FUNC_SMBUS_PEC
85

    
86
/* 
87
 * Data for SMBus Messages 
88
 */
89
#define I2C_SMBUS_BLOCK_MAX	32	/* As specified in SMBus standard */	
90
#define I2C_SMBUS_I2C_BLOCK_MAX	32	/* Not specified but we use same structure */
91
union i2c_smbus_data {
92
	__u8 byte;
93
	__u16 word;
94
	__u8 block[I2C_SMBUS_BLOCK_MAX + 2]; /* block[0] is used for length */
95
	                                            /* and one more for PEC */
96
};
97

    
98
/* smbus_access read or write markers */
99
#define I2C_SMBUS_READ	1
100
#define I2C_SMBUS_WRITE	0
101

    
102
/* SMBus transaction types (size parameter in the above functions) 
103
   Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */
104
#define I2C_SMBUS_QUICK		    0
105
#define I2C_SMBUS_BYTE		    1
106
#define I2C_SMBUS_BYTE_DATA	    2 
107
#define I2C_SMBUS_WORD_DATA	    3
108
#define I2C_SMBUS_PROC_CALL	    4
109
#define I2C_SMBUS_BLOCK_DATA	    5
110
#define I2C_SMBUS_I2C_BLOCK_BROKEN  6
111
#define I2C_SMBUS_BLOCK_PROC_CALL   7		/* SMBus 2.0 */
112
#define I2C_SMBUS_I2C_BLOCK_DATA    8
113

    
114

    
115
/* ----- commands for the ioctl like i2c_command call:
116
 * note that additional calls are defined in the algorithm and hw 
117
 *	dependent layers - these can be listed here, or see the 
118
 *	corresponding header files.
119
 */
120
				/* -> bit-adapter specific ioctls	*/
121
#define I2C_RETRIES	0x0701	/* number of times a device address      */
122
				/* should be polled when not            */
123
                                /* acknowledging 			*/
124
#define I2C_TIMEOUT	0x0702	/* set timeout - call with int 		*/
125

    
126

    
127
/* this is for i2c-dev.c	*/
128
#define I2C_SLAVE	0x0703	/* Change slave address			*/
129
				/* Attn.: Slave address is 7 or 10 bits */
130
#define I2C_SLAVE_FORCE	0x0706	/* Change slave address			*/
131
				/* Attn.: Slave address is 7 or 10 bits */
132
				/* This changes the address, even if it */
133
				/* is already taken!			*/
134
#define I2C_TENBIT	0x0704	/* 0 for 7 bit addrs, != 0 for 10 bit	*/
135

    
136
#define I2C_FUNCS	0x0705	/* Get the adapter functionality */
137
#define I2C_RDWR	0x0707	/* Combined R/W transfer (one stop only)*/
138
#define I2C_PEC		0x0708	/* != 0 for SMBus PEC                   */
139

    
140
#define I2C_SMBUS	0x0720	/* SMBus-level access */
141

    
142
/* -- i2c.h -- */
143

    
144

    
145
/* Note: 10-bit addresses are NOT supported! */
146

    
147
/* This is the structure as used in the I2C_SMBUS ioctl call */
148
struct i2c_smbus_ioctl_data {
149
	char read_write;
150
	__u8 command;
151
	int size;
152
	union i2c_smbus_data *data;
153
};
154

    
155
/* This is the structure as used in the I2C_RDWR ioctl call */
156
struct i2c_rdwr_ioctl_data {
157
	struct i2c_msg *msgs;	/* pointers to i2c_msgs */
158
	int nmsgs;		/* number of i2c_msgs */
159
};
160

    
161

    
162
static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command, 
163
                                     int size, union i2c_smbus_data *data)
164
{
165
	struct i2c_smbus_ioctl_data args;
166

    
167
	args.read_write = read_write;
168
	args.command = command;
169
	args.size = size;
170
	args.data = data;
171
	return ioctl(file,I2C_SMBUS,&args);
172
}
173

    
174

    
175
static inline __s32 i2c_smbus_write_quick(int file, __u8 value)
176
{
177
	return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);
178
}
179
	
180
static inline __s32 i2c_smbus_read_byte(int file)
181
{
182
	union i2c_smbus_data data;
183
	if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data))
184
		return -1;
185
	else
186
		return 0x0FF & data.byte;
187
}
188

    
189
static inline __s32 i2c_smbus_write_byte(int file, __u8 value)
190
{
191
	return i2c_smbus_access(file,I2C_SMBUS_WRITE,value,
192
	                        I2C_SMBUS_BYTE,NULL);
193
}
194

    
195
static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
196
{
197
	union i2c_smbus_data data;
198
	if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
199
	                     I2C_SMBUS_BYTE_DATA,&data))
200
		return -1;
201
	else
202
		return 0x0FF & data.byte;
203
}
204

    
205
static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command, 
206
                                              __u8 value)
207
{
208
	union i2c_smbus_data data;
209
	data.byte = value;
210
	return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
211
	                        I2C_SMBUS_BYTE_DATA, &data);
212
}
213

    
214
static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
215
{
216
	union i2c_smbus_data data;
217
	if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
218
	                     I2C_SMBUS_WORD_DATA,&data))
219
		return -1;
220
	else
221
		return 0x0FFFF & data.word;
222
}
223

    
224
static inline __s32 i2c_smbus_write_word_data(int file, __u8 command, 
225
                                              __u16 value)
226
{
227
	union i2c_smbus_data data;
228
	data.word = value;
229
	return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
230
	                        I2C_SMBUS_WORD_DATA, &data);
231
}
232

    
233
static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
234
{
235
	union i2c_smbus_data data;
236
	data.word = value;
237
	if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
238
	                     I2C_SMBUS_PROC_CALL,&data))
239
		return -1;
240
	else
241
		return 0x0FFFF & data.word;
242
}
243

    
244

    
245
/* Returns the number of read bytes */
246
static inline __s32 i2c_smbus_read_block_data(int file, __u8 command, 
247
                                              __u8 *values)
248
{
249
	union i2c_smbus_data data;
250
	int i;
251
	if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
252
	                     I2C_SMBUS_BLOCK_DATA,&data))
253
		return -1;
254
	else {
255
		for (i = 1; i <= data.block[0]; i++)
256
			values[i-1] = data.block[i];
257
		return data.block[0];
258
	}
259
}
260

    
261
static inline __s32 i2c_smbus_write_block_data(int file, __u8 command, 
262
                                               __u8 length, __u8 *values)
263
{
264
	union i2c_smbus_data data;
265
	int i;
266
	if (length > 32)
267
		length = 32;
268
	for (i = 1; i <= length; i++)
269
		data.block[i] = values[i-1];
270
	data.block[0] = length;
271
	return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
272
	                        I2C_SMBUS_BLOCK_DATA, &data);
273
}
274

    
275
/* Returns the number of read bytes */
276
/* Until kernel 2.6.22, the length is hardcoded to 32 bytes. If you
277
   ask for less than 32 bytes, your code will only work with kernels
278
   2.6.23 and later. */
279
static inline __s32 i2c_smbus_read_i2c_block_data(int file, __u8 command,
280
                                                  __u8 length, __u8 *values)
281
{
282
	union i2c_smbus_data data;
283
	int i;
284

    
285
	if (length > 32)
286
		length = 32;
287
	data.block[0] = length;
288
	if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
289
	                     length == 32 ? I2C_SMBUS_I2C_BLOCK_BROKEN :
290
	                      I2C_SMBUS_I2C_BLOCK_DATA,&data))
291
		return -1;
292
	else {
293
		for (i = 1; i <= data.block[0]; i++)
294
			values[i-1] = data.block[i];
295
		return data.block[0];
296
	}
297
}
298

    
299
static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,
300
                                               __u8 length, __u8 *values)
301
{
302
	union i2c_smbus_data data;
303
	int i;
304
	if (length > 32)
305
		length = 32;
306
	for (i = 1; i <= length; i++)
307
		data.block[i] = values[i-1];
308
	data.block[0] = length;
309
	return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
310
	                        I2C_SMBUS_I2C_BLOCK_BROKEN, &data);
311
}
312

    
313
/* Returns the number of read bytes */
314
static inline __s32 i2c_smbus_block_process_call(int file, __u8 command,
315
                                                 __u8 length, __u8 *values)
316
{
317
	union i2c_smbus_data data;
318
	int i;
319
	if (length > 32)
320
		length = 32;
321
	for (i = 1; i <= length; i++)
322
		data.block[i] = values[i-1];
323
	data.block[0] = length;
324
	if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
325
	                     I2C_SMBUS_BLOCK_PROC_CALL,&data))
326
		return -1;
327
	else {
328
		for (i = 1; i <= data.block[0]; i++)
329
			values[i-1] = data.block[i];
330
		return data.block[0];
331
	}
332
}
333

    
334

    
335
#endif /* LIB_I2CDEV_H */
(3-3/6) Go to top
Add picture from clipboard (Maximum size: 1 GB)