more changes on original files
[linux-2.4.git] / include / linux / i2c-dev.h
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /* $Id: i2c-dev.h,v 1.9 2001/08/15 03:04:58 mds Exp $ */
23
24 #ifndef I2C_DEV_H
25 #define I2C_DEV_H
26
27
28 #include <linux/types.h>
29 #include <linux/i2c.h>
30
31 /* Some IOCTL commands are defined in <linux/i2c.h> */
32 /* Note: 10-bit addresses are NOT supported! */
33
34 /* This is the structure as used in the I2C_SMBUS ioctl call */
35 struct i2c_smbus_ioctl_data {
36         __u8 read_write;
37         __u8 command;
38         __u32 size;
39         union i2c_smbus_data *data;
40 };
41
42 /* This is the structure as used in the I2C_RDWR ioctl call */
43 struct i2c_rdwr_ioctl_data {
44         struct i2c_msg *msgs;   /* pointers to i2c_msgs */
45         __u32 nmsgs;            /* number of i2c_msgs */
46 };
47
48 #ifndef __KERNEL__
49
50 #include <sys/ioctl.h>
51
52 static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command, 
53                                      int size, union i2c_smbus_data *data)
54 {
55         struct i2c_smbus_ioctl_data args;
56
57         args.read_write = read_write;
58         args.command = command;
59         args.size = size;
60         args.data = data;
61         return ioctl(file,I2C_SMBUS,&args);
62 }
63
64
65 static inline __s32 i2c_smbus_write_quick(int file, __u8 value)
66 {
67         return i2c_smbus_access(file,value,0,I2C_SMBUS_QUICK,NULL);
68 }
69         
70 static inline __s32 i2c_smbus_read_byte(int file)
71 {
72         union i2c_smbus_data data;
73         if (i2c_smbus_access(file,I2C_SMBUS_READ,0,I2C_SMBUS_BYTE,&data))
74                 return -1;
75         else
76                 return 0x0FF & data.byte;
77 }
78
79 static inline __s32 i2c_smbus_write_byte(int file, __u8 value)
80 {
81         return i2c_smbus_access(file,I2C_SMBUS_WRITE,value,
82                                 I2C_SMBUS_BYTE,NULL);
83 }
84
85 static inline __s32 i2c_smbus_read_byte_data(int file, __u8 command)
86 {
87         union i2c_smbus_data data;
88         if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
89                              I2C_SMBUS_BYTE_DATA,&data))
90                 return -1;
91         else
92                 return 0x0FF & data.byte;
93 }
94
95 static inline __s32 i2c_smbus_write_byte_data(int file, __u8 command, 
96                                               __u8 value)
97 {
98         union i2c_smbus_data data;
99         data.byte = value;
100         return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
101                                 I2C_SMBUS_BYTE_DATA, &data);
102 }
103
104 static inline __s32 i2c_smbus_read_word_data(int file, __u8 command)
105 {
106         union i2c_smbus_data data;
107         if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
108                              I2C_SMBUS_WORD_DATA,&data))
109                 return -1;
110         else
111                 return 0x0FFFF & data.word;
112 }
113
114 static inline __s32 i2c_smbus_write_word_data(int file, __u8 command, 
115                                               __u16 value)
116 {
117         union i2c_smbus_data data;
118         data.word = value;
119         return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
120                                 I2C_SMBUS_WORD_DATA, &data);
121 }
122
123 static inline __s32 i2c_smbus_process_call(int file, __u8 command, __u16 value)
124 {
125         union i2c_smbus_data data;
126         data.word = value;
127         if (i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
128                              I2C_SMBUS_PROC_CALL,&data))
129                 return -1;
130         else
131                 return 0x0FFFF & data.word;
132 }
133
134
135 /* Returns the number of read bytes */
136 static inline __s32 i2c_smbus_read_block_data(int file, __u8 command, 
137                                               __u8 *values)
138 {
139         union i2c_smbus_data data;
140         int i;
141         if (i2c_smbus_access(file,I2C_SMBUS_READ,command,
142                              I2C_SMBUS_BLOCK_DATA,&data))
143                 return -1;
144         else {
145                 for (i = 1; i <= data.block[0]; i++)
146                         values[i-1] = data.block[i];
147                         return data.block[0];
148         }
149 }
150
151 static inline __s32 i2c_smbus_write_block_data(int file, __u8 command, 
152                                                __u8 length, __u8 *values)
153 {
154         union i2c_smbus_data data;
155         int i;
156         if (length > 32)
157                 length = 32;
158         for (i = 1; i <= length; i++)
159                 data.block[i] = values[i-1];
160         data.block[0] = length;
161         return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
162                                 I2C_SMBUS_BLOCK_DATA, &data);
163 }
164
165 static inline __s32 i2c_smbus_write_i2c_block_data(int file, __u8 command,
166                                                __u8 length, __u8 *values)
167 {
168         union i2c_smbus_data data;
169         int i;
170         if (length > 32)
171                 length = 32;
172         for (i = 1; i <= length; i++)
173                 data.block[i] = values[i-1];
174         data.block[0] = length;
175         return i2c_smbus_access(file,I2C_SMBUS_WRITE,command,
176                                 I2C_SMBUS_I2C_BLOCK_DATA, &data);
177 }
178
179 #endif /* ndef __KERNEL__ */
180
181 #endif