root/lm-sensors/trunk/src/smbus.h @ 12

Revision 12, 9.1 KB (checked in by frodo, 16 years ago)

isa module added, some bugfixes

'isa.o' is roughly what was called 'sensor.o' in doc/design.

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