| 1 | /* |
|---|
| 2 | smbus.h - A Linux module for reading sensor data. |
|---|
| 3 | Copyright (c) 1998 Frodo Looijaard <frodol@dds.nl> |
|---|
| 4 | |
|---|
| 5 | This program is free software; you can redistribute it and/or modify |
|---|
| 6 | it under the terms of the GNU General Public License as published by |
|---|
| 7 | the Free Software Foundation; either version 2 of the License, or |
|---|
| 8 | (at your option) any later version. |
|---|
| 9 | |
|---|
| 10 | This program is distributed in the hope that it will be useful, |
|---|
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | GNU General Public License for more details. |
|---|
| 14 | |
|---|
| 15 | You should have received a copy of the GNU General Public License |
|---|
| 16 | along with this program; if not, write to the Free Software |
|---|
| 17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|---|
| 18 | */ |
|---|
| 19 | |
|---|
| 20 | #ifndef SENSORS_SMBUS_H |
|---|
| 21 | #define SENSORS_SMBUS_H |
|---|
| 22 | |
|---|
| 23 | #ifdef __KERNEL__ |
|---|
| 24 | |
|---|
| 25 | /* This file must interface with Simon Vogl's i2c driver. Version 19981006 is |
|---|
| 26 | OK, earlier versions are not; later versions will probably give problems |
|---|
| 27 | too. |
|---|
| 28 | */ |
|---|
| 29 | #include <asm/types.h> |
|---|
| 30 | |
|---|
| 31 | /* SPINLOCK is defined in i2c.h. */ |
|---|
| 32 | #ifdef SPINLOCK |
|---|
| 33 | #include <asm/spinlock.h> |
|---|
| 34 | #else |
|---|
| 35 | #include <asm/semaphore.h> |
|---|
| 36 | #endif |
|---|
| 37 | |
|---|
| 38 | #ifdef LM_SENSORS |
|---|
| 39 | #include "i2c.h" |
|---|
| 40 | #else |
|---|
| 41 | #include <linux/i2c.h> |
|---|
| 42 | #endif |
|---|
| 43 | |
|---|
| 44 | /* Declarations, to keep the compiler happy */ |
|---|
| 45 | struct smbus_driver; |
|---|
| 46 | struct smbus_client; |
|---|
| 47 | struct smbus_algorithm; |
|---|
| 48 | struct smbus_adapter; |
|---|
| 49 | union smbus_data; |
|---|
| 50 | |
|---|
| 51 | /* A driver tells us how we should handle a specific kind of chip. |
|---|
| 52 | A specific instance of such a chip is called a client. |
|---|
| 53 | This structure is essentially the same as i2c_driver. */ |
|---|
| 54 | struct smbus_driver { |
|---|
| 55 | char name[32]; |
|---|
| 56 | int id; |
|---|
| 57 | unsigned int flags; |
|---|
| 58 | int (* attach_adapter) (struct smbus_adapter *); |
|---|
| 59 | int (* detach_client) (struct smbus_client *); |
|---|
| 60 | int (* command) (struct smbus_client *, unsigned int cmd, void *arg); |
|---|
| 61 | void (* inc_use) (struct smbus_client *); |
|---|
| 62 | void (* dec_use) (struct smbus_client *); |
|---|
| 63 | }; |
|---|
| 64 | |
|---|
| 65 | /* A client is a specifc instance of a chip: for each detected chip, there will |
|---|
| 66 | be a client. Its operation is controlled by a driver. |
|---|
| 67 | This structure is essentially the same as i2c_client. */ |
|---|
| 68 | struct smbus_client { |
|---|
| 69 | char name[32]; |
|---|
| 70 | int id; |
|---|
| 71 | unsigned int flags; |
|---|
| 72 | unsigned char addr; |
|---|
| 73 | struct smbus_adapter *adapter; |
|---|
| 74 | struct smbus_driver *driver; |
|---|
| 75 | void *data; |
|---|
| 76 | }; |
|---|
| 77 | |
|---|
| 78 | /* An algorithm describes how a certain class of busses can be accessed. |
|---|
| 79 | A specific instance of sucj a bus is called an adapter. |
|---|
| 80 | This structure is essentially the same as i2c_adapter. */ |
|---|
| 81 | struct smbus_algorithm { |
|---|
| 82 | char name[32]; |
|---|
| 83 | unsigned int id; |
|---|
| 84 | int (* master_xfer) (struct smbus_adapter *adap, struct i2c_msg msgs[], |
|---|
| 85 | int num); |
|---|
| 86 | int (* slave_send) (struct smbus_adapter *,char *, int); |
|---|
| 87 | int (* slave_recv) (struct smbus_adapter *,char *, int); |
|---|
| 88 | int (* algo_control) (struct smbus_adapter *, unsigned int, unsigned long); |
|---|
| 89 | int (* client_register) (struct smbus_client *); |
|---|
| 90 | int (* client_unregister) (struct smbus_client *); |
|---|
| 91 | }; |
|---|
| 92 | |
|---|
| 93 | /* An adapter is a specifc instance of a bus: for each detected bus, there will |
|---|
| 94 | be an adapter. Its operation is controlled by an algorithm. |
|---|
| 95 | SPINLOCK must be the same as declared in i2c.h. |
|---|
| 96 | This structure is an extension of i2c_algorithm. */ |
|---|
| 97 | struct smbus_adapter { |
|---|
| 98 | char name[32]; |
|---|
| 99 | unsigned int id; |
|---|
| 100 | struct smbus_algorithm *algo; |
|---|
| 101 | void *data; |
|---|
| 102 | #ifdef SPINLOCK |
|---|
| 103 | spinlock_t lock; |
|---|
| 104 | unsigned long lockflags; |
|---|
| 105 | #else |
|---|
| 106 | struct semaphore lock; |
|---|
| 107 | #endif |
|---|
| 108 | unsigned int flags; |
|---|
| 109 | struct smbus_client *clients[I2C_CLIENT_MAX]; |
|---|
| 110 | int client_count; |
|---|
| 111 | int timeout; |
|---|
| 112 | int retries; |
|---|
| 113 | |
|---|
| 114 | /* Here ended i2c_adapter */ |
|---|
| 115 | s32 (* smbus_access) (u8 addr, char read_write, |
|---|
| 116 | u8 command, int size, union smbus_data * data); |
|---|
| 117 | }; |
|---|
| 118 | |
|---|
| 119 | /* We need to mark SMBus algorithms in the algorithm structure. |
|---|
| 120 | Note that any and all adapters using a non-i2c driver use in this |
|---|
| 121 | setup ALGO_SMBUS. Adapters define their own smbus access routine. |
|---|
| 122 | This also means that adapter->smbus_access is only available if |
|---|
| 123 | this flag is set! */ |
|---|
| 124 | #define ALGO_SMBUS 0x40000 |
|---|
| 125 | |
|---|
| 126 | /* SMBus Adapter ids */ |
|---|
| 127 | #define SMBUS_PIIX4 1 |
|---|
| 128 | |
|---|
| 129 | /* Detect whether we are on an SMBus-only bus. Note that if this returns |
|---|
| 130 | false, you can still use the smbus access routines, as these emulate |
|---|
| 131 | the SMBus on I2C. Unless they are undefined on your algorithm, of |
|---|
| 132 | course. */ |
|---|
| 133 | #define i2c_is_smbus_client(clientptr) \ |
|---|
| 134 | ((clientptr)->adapter->algo->id == ALGO_SMBUS) |
|---|
| 135 | #define i2c_is_smbus_adapter(adapptr) \ |
|---|
| 136 | ((adapptr)->algo->id == ALGO_SMBUS) |
|---|
| 137 | |
|---|
| 138 | /* This union is used within smbus_access routines */ |
|---|
| 139 | union smbus_data { |
|---|
| 140 | u8 byte; |
|---|
| 141 | u16 word; |
|---|
| 142 | u8 block[32]; |
|---|
| 143 | }; |
|---|
| 144 | |
|---|
| 145 | /* smbus_access read or write markers */ |
|---|
| 146 | #define SMBUS_READ 1 |
|---|
| 147 | #define SMBUS_WRITE 0 |
|---|
| 148 | |
|---|
| 149 | /* SMBus transaction types (size parameter in the above functions) |
|---|
| 150 | Note: these no longer correspond to the (arbitrary) PIIX4 internal codes! */ |
|---|
| 151 | #define SMBUS_QUICK 0 |
|---|
| 152 | #define SMBUS_BYTE 1 |
|---|
| 153 | #define SMBUS_BYTE_DATA 2 |
|---|
| 154 | #define SMBUS_WORD_DATA 3 |
|---|
| 155 | #define SMBUS_PROC_CALL 4 |
|---|
| 156 | #define SMBUS_BLOCK_DATA 5 |
|---|
| 157 | |
|---|
| 158 | /* Declare an algorithm structure. All SMBus derived adapters should use this |
|---|
| 159 | algorithm! */ |
|---|
| 160 | extern struct smbus_algorithm smbus_algorithm; |
|---|
| 161 | |
|---|
| 162 | /* This is the very generalized SMBus access routine. You probably do not |
|---|
| 163 | want to use this, though; one of the functions below may be much easier, |
|---|
| 164 | and probably just as fast. |
|---|
| 165 | Note that we use i2c_adapter here, because you do not need a specific |
|---|
| 166 | smbus adapter to call this function. */ |
|---|
| 167 | extern s32 smbus_access (struct i2c_adapter * adapter, u8 addr, |
|---|
| 168 | char read_write, u8 command, int size, |
|---|
| 169 | union smbus_data * data); |
|---|
| 170 | |
|---|
| 171 | /* Now follow the 'nice' access routines. These also document the calling |
|---|
| 172 | conventions of smbus_access. */ |
|---|
| 173 | |
|---|
| 174 | extern inline s32 smbus_write_quick(struct i2c_adapter * adapter, u8 addr, |
|---|
| 175 | u8 value) |
|---|
| 176 | { |
|---|
| 177 | return smbus_access(adapter,addr,value,0,SMBUS_QUICK,NULL); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | extern inline s32 smbus_read_byte(struct i2c_adapter * adapter,u8 addr) |
|---|
| 181 | { |
|---|
| 182 | union smbus_data data; |
|---|
| 183 | if (smbus_access(adapter,addr,SMBUS_READ,0,SMBUS_BYTE,&data)) |
|---|
| 184 | return -1; |
|---|
| 185 | else |
|---|
| 186 | return data.byte; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | extern inline s32 smbus_write_byte(struct i2c_adapter * adapter, u8 addr, |
|---|
| 190 | u8 value) |
|---|
| 191 | { |
|---|
| 192 | return smbus_access(adapter,addr,SMBUS_WRITE,value, SMBUS_BYTE,NULL); |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | extern inline s32 smbus_read_byte_data(struct i2c_adapter * adapter, |
|---|
| 196 | u8 addr, u8 command) |
|---|
| 197 | { |
|---|
| 198 | union smbus_data data; |
|---|
| 199 | if (smbus_access(adapter,addr,SMBUS_READ,command,SMBUS_BYTE_DATA,&data)) |
|---|
| 200 | return -1; |
|---|
| 201 | else |
|---|
| 202 | return data.byte; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | extern inline s32 smbus_write_byte_data(struct i2c_adapter * adapter, |
|---|
| 206 | u8 addr, u8 command, u8 value) |
|---|
| 207 | { |
|---|
| 208 | union smbus_data data; |
|---|
| 209 | data.byte = value; |
|---|
| 210 | return smbus_access(adapter,addr,SMBUS_WRITE,command,SMBUS_BYTE_DATA,&data); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | extern inline s32 smbus_read_word_data(struct i2c_adapter * adapter, |
|---|
| 214 | u8 addr, u8 command) |
|---|
| 215 | { |
|---|
| 216 | union smbus_data data; |
|---|
| 217 | if (smbus_access(adapter,addr,SMBUS_READ,command,SMBUS_WORD_DATA,&data)) |
|---|
| 218 | return -1; |
|---|
| 219 | else |
|---|
| 220 | return data.word; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | extern inline s32 smbus_write_word_data(struct i2c_adapter * adapter, |
|---|
| 224 | u8 addr, u8 command, u16 value) |
|---|
| 225 | { |
|---|
| 226 | union smbus_data data; |
|---|
| 227 | data.word = value; |
|---|
| 228 | return smbus_access(adapter,addr,SMBUS_WRITE,command,SMBUS_WORD_DATA,&data); |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | extern inline s32 smbus_process_call(struct i2c_adapter * adapter, |
|---|
| 232 | u8 addr, u8 command, u16 value) |
|---|
| 233 | { |
|---|
| 234 | union smbus_data data; |
|---|
| 235 | data.word = value; |
|---|
| 236 | if (smbus_access(adapter,addr,SMBUS_WRITE,command,SMBUS_PROC_CALL,&data)) |
|---|
| 237 | return -1; |
|---|
| 238 | else |
|---|
| 239 | return data.word; |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | /* Returns the number of read bytes */ |
|---|
| 243 | extern inline s32 smbus_read_block_data(struct i2c_adapter * adapter, |
|---|
| 244 | u8 addr, u8 command, u8 *values) |
|---|
| 245 | { |
|---|
| 246 | union smbus_data data; |
|---|
| 247 | int i; |
|---|
| 248 | if (smbus_access(adapter,addr,SMBUS_READ,command,SMBUS_BLOCK_DATA,&data)) |
|---|
| 249 | return -1; |
|---|
| 250 | else { |
|---|
| 251 | for (i = 1; i <= data.block[0]; i++) |
|---|
| 252 | values[i-1] = data.block[i]; |
|---|
| 253 | return data.block[0]; |
|---|
| 254 | } |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | extern inline int smbus_write_block_data(struct i2c_adapter * adapter, |
|---|
| 258 | u8 addr, u8 command, u8 length, |
|---|
| 259 | u8 *values) |
|---|
| 260 | { |
|---|
| 261 | union smbus_data data; |
|---|
| 262 | int i; |
|---|
| 263 | if (length > 32) |
|---|
| 264 | length = 32; |
|---|
| 265 | for (i = 1; i <= length; i++) |
|---|
| 266 | data.block[i] = values[i-1]; |
|---|
| 267 | data.block[0] = length; |
|---|
| 268 | return smbus_access(adapter,addr,SMBUS_WRITE,command,SMBUS_BLOCK_DATA,&data); |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | /* Next: define SMBus variants of registering. */ |
|---|
| 273 | |
|---|
| 274 | #define smbus_add_algorithm(algoptr) \ |
|---|
| 275 | i2c_add_algorithm((struct i2c_algorithm *) (algoptr)) |
|---|
| 276 | #define smbus_del_algorithm(algoptr) \ |
|---|
| 277 | i2c_del_algorithm((struct i2c_algorithm *) (algoptr)) |
|---|
| 278 | |
|---|
| 279 | #define smbus_add_adapter(adapptr) \ |
|---|
| 280 | i2c_add_adapter((struct i2c_adapter *) (adapptr)) |
|---|
| 281 | #define smbus_del_adapter(adapptr) \ |
|---|
| 282 | i2c_del_adapter((struct i2c_adapter *) (adapptr)) |
|---|
| 283 | |
|---|
| 284 | #define smbus_add_driver(driverptr) \ |
|---|
| 285 | i2c_add_driver((struct i2c_driver *) (driverptr)) |
|---|
| 286 | #define smbus_del_driver(driverptr) \ |
|---|
| 287 | i2c_add_driver((struct i2c_driver *) (driverptr)) |
|---|
| 288 | |
|---|
| 289 | #define smbus_attach_client(clientptr) \ |
|---|
| 290 | i2c_attach_client((struct i2c_client *) (clientptr)) |
|---|
| 291 | #define smbus_detach_client(clientptr) \ |
|---|
| 292 | i2c_detach_client((struct i2c_client *) (clientptr)) |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | #endif /* def __KERNEL__ */ |
|---|
| 296 | |
|---|
| 297 | #endif /* ndef SENSORS_SMBUS_H */ |
|---|
| 298 | |
|---|