blob: ee75cbace28d529cb690e573ec4c2cb67f75bd0f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001This is a small guide for those who want to write kernel drivers for I2C
David Brownell4298cfc2007-05-01 23:26:31 +02002or SMBus devices, using Linux as the protocol host/master (not slave).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
4To set up a driver, you need to do several things. Some are optional, and
5some things can be done slightly or completely different. Use this as a
6guide, not as a rule book!
7
8
9General remarks
10===============
11
12Try to keep the kernel namespace as clean as possible. The best way to
13do this is to use a unique prefix for all global symbols. This is
14especially important for exported symbols, but it is a good idea to do
15it for non-exported symbols too. We will use the prefix `foo_' in this
16tutorial, and `FOO_' for preprocessor variables.
17
18
19The driver structure
20====================
21
22Usually, you will implement a single driver structure, and instantiate
23all clients from it. Remember, a driver structure contains general access
David Brownellf37dd802007-02-13 22:09:00 +010024routines, and should be zero-initialized except for fields with data you
25provide. A client structure holds device-specific information like the
26driver model device node, and its I2C address.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28static struct i2c_driver foo_driver = {
Jean Delvared45d2042005-11-26 20:55:35 +010029 .driver = {
Jean Delvared45d2042005-11-26 20:55:35 +010030 .name = "foo",
31 },
David Brownell4298cfc2007-05-01 23:26:31 +020032
33 /* iff driver uses driver model ("new style") binding model: */
34 .probe = foo_probe,
35 .remove = foo_remove,
36
37 /* else, driver uses "legacy" binding model: */
David Brownellf37dd802007-02-13 22:09:00 +010038 .attach_adapter = foo_attach_adapter,
39 .detach_client = foo_detach_client,
David Brownell4298cfc2007-05-01 23:26:31 +020040
41 /* these may be used regardless of the driver binding model */
David Brownellf37dd802007-02-13 22:09:00 +010042 .shutdown = foo_shutdown, /* optional */
43 .suspend = foo_suspend, /* optional */
44 .resume = foo_resume, /* optional */
45 .command = foo_command, /* optional */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
David Brownellf37dd802007-02-13 22:09:00 +010048The name field is the driver name, and must not contain spaces. It
49should match the module name (if the driver can be compiled as a module),
50although you can use MODULE_ALIAS (passing "foo" in this example) to add
David Brownell4298cfc2007-05-01 23:26:31 +020051another name for the module. If the driver name doesn't match the module
52name, the module won't be automatically loaded (hotplug/coldplug).
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054All other fields are for call-back functions which will be explained
55below.
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58Extra client data
59=================
60
David Brownellf37dd802007-02-13 22:09:00 +010061Each client structure has a special `data' field that can point to any
62structure at all. You should use this to keep device-specific data,
63especially in drivers that handle multiple I2C or SMBUS devices. You
Linus Torvalds1da177e2005-04-16 15:20:36 -070064do not always need this, but especially for `sensors' drivers, it can
65be very useful.
66
David Brownellf37dd802007-02-13 22:09:00 +010067 /* store the value */
68 void i2c_set_clientdata(struct i2c_client *client, void *data);
69
70 /* retrieve the value */
71 void *i2c_get_clientdata(struct i2c_client *client);
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073An example structure is below.
74
75 struct foo_data {
Jean Delvare2445eb62005-10-17 23:16:25 +020076 struct i2c_client client;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 enum chips type; /* To keep the chips type for `sensors' drivers. */
78
79 /* Because the i2c bus is slow, it is often useful to cache the read
80 information of a chip for some time (for example, 1 or 2 seconds).
81 It depends of course on the device whether this is really worthwhile
82 or even sensible. */
Jean Delvareeefcd752007-05-01 23:26:35 +020083 struct mutex update_lock; /* When we are reading lots of information,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 another process should not update the
85 below information */
86 char valid; /* != 0 if the following fields are valid. */
87 unsigned long last_updated; /* In jiffies */
88 /* Add the read information here too */
89 };
90
91
92Accessing the client
93====================
94
95Let's say we have a valid client structure. At some time, we will need
96to gather information from the client, or write new information to the
97client. How we will export this information to user-space is less
98important at this moment (perhaps we do not need to do this at all for
99some obscure clients). But we need generic reading and writing routines.
100
101I have found it useful to define foo_read and foo_write function for this.
102For some cases, it will be easier to call the i2c functions directly,
103but many chips have some kind of register-value idea that can easily
Jean Delvareeefcd752007-05-01 23:26:35 +0200104be encapsulated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106The below functions are simple examples, and should not be copied
107literally.
108
109 int foo_read_value(struct i2c_client *client, u8 reg)
110 {
111 if (reg < 0x10) /* byte-sized register */
112 return i2c_smbus_read_byte_data(client,reg);
113 else /* word-sized register */
114 return i2c_smbus_read_word_data(client,reg);
115 }
116
117 int foo_write_value(struct i2c_client *client, u8 reg, u16 value)
118 {
119 if (reg == 0x10) /* Impossible to write - driver error! */ {
120 return -1;
121 else if (reg < 0x10) /* byte-sized register */
122 return i2c_smbus_write_byte_data(client,reg,value);
123 else /* word-sized register */
124 return i2c_smbus_write_word_data(client,reg,value);
125 }
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128Probing and attaching
129=====================
130
David Brownell4298cfc2007-05-01 23:26:31 +0200131The Linux I2C stack was originally written to support access to hardware
132monitoring chips on PC motherboards, and thus it embeds some assumptions
133that are more appropriate to SMBus (and PCs) than to I2C. One of these
134assumptions is that most adapters and devices drivers support the SMBUS_QUICK
135protocol to probe device presence. Another is that devices and their drivers
136can be sufficiently configured using only such probe primitives.
137
138As Linux and its I2C stack became more widely used in embedded systems
139and complex components such as DVB adapters, those assumptions became more
140problematic. Drivers for I2C devices that issue interrupts need more (and
141different) configuration information, as do drivers handling chip variants
142that can't be distinguished by protocol probing, or which need some board
143specific information to operate correctly.
144
145Accordingly, the I2C stack now has two models for associating I2C devices
146with their drivers: the original "legacy" model, and a newer one that's
147fully compatible with the Linux 2.6 driver model. These models do not mix,
148since the "legacy" model requires drivers to create "i2c_client" device
149objects after SMBus style probing, while the Linux driver model expects
150drivers to be given such device objects in their probe() routines.
151
152
153Standard Driver Model Binding ("New Style")
154-------------------------------------------
155
156System infrastructure, typically board-specific initialization code or
157boot firmware, reports what I2C devices exist. For example, there may be
158a table, in the kernel or from the boot loader, identifying I2C devices
159and linking them to board-specific configuration information about IRQs
160and other wiring artifacts, chip type, and so on. That could be used to
161create i2c_client objects for each I2C device.
162
163I2C device drivers using this binding model work just like any other
164kind of driver in Linux: they provide a probe() method to bind to
165those devices, and a remove() method to unbind.
166
Jean Delvared2653e92008-04-29 23:11:39 +0200167 static int foo_probe(struct i2c_client *client,
168 const struct i2c_device_id *id);
David Brownell4298cfc2007-05-01 23:26:31 +0200169 static int foo_remove(struct i2c_client *client);
170
171Remember that the i2c_driver does not create those client handles. The
172handle may be used during foo_probe(). If foo_probe() reports success
173(zero not a negative status code) it may save the handle and use it until
174foo_remove() returns. That binding model is used by most Linux drivers.
175
176Drivers match devices when i2c_client.driver_name and the driver name are
177the same; this approach is used in several other busses that don't have
178device typing support in the hardware. The driver and module name should
179match, so hotplug/coldplug mechanisms will modprobe the driver.
180
181
Jean Delvarece9e0792007-05-01 23:26:32 +0200182Device Creation (Standard driver model)
183---------------------------------------
184
185If you know for a fact that an I2C device is connected to a given I2C bus,
186you can instantiate that device by simply filling an i2c_board_info
187structure with the device address and driver name, and calling
188i2c_new_device(). This will create the device, then the driver core will
189take care of finding the right driver and will call its probe() method.
190If a driver supports different device types, you can specify the type you
191want using the type field. You can also specify an IRQ and platform data
192if needed.
193
194Sometimes you know that a device is connected to a given I2C bus, but you
195don't know the exact address it uses. This happens on TV adapters for
196example, where the same driver supports dozens of slightly different
197models, and I2C device addresses change from one model to the next. In
198that case, you can use the i2c_new_probed_device() variant, which is
199similar to i2c_new_device(), except that it takes an additional list of
200possible I2C addresses to probe. A device is created for the first
201responsive address in the list. If you expect more than one device to be
202present in the address range, simply call i2c_new_probed_device() that
203many times.
204
205The call to i2c_new_device() or i2c_new_probed_device() typically happens
206in the I2C bus driver. You may want to save the returned i2c_client
207reference for later use.
208
209
210Device Deletion (Standard driver model)
211---------------------------------------
212
213Each I2C device which has been created using i2c_new_device() or
214i2c_new_probed_device() can be unregistered by calling
215i2c_unregister_device(). If you don't call it explicitly, it will be
216called automatically before the underlying I2C bus itself is removed, as a
217device can't survive its parent in the device driver model.
218
219
David Brownell4298cfc2007-05-01 23:26:31 +0200220Legacy Driver Binding Model
221---------------------------
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223Most i2c devices can be present on several i2c addresses; for some this
224is determined in hardware (by soldering some chip pins to Vcc or Ground),
225for others this can be changed in software (by writing to specific client
226registers). Some devices are usually on a specific address, but not always;
227and some are even more tricky. So you will probably need to scan several
228i2c addresses for your clients, and do some sort of detection to see
229whether it is actually a device supported by your driver.
230
231To give the user a maximum of possibilities, some default module parameters
232are defined to help determine what addresses are scanned. Several macros
233are defined in i2c.h to help you support them, as well as a generic
234detection algorithm.
235
236You do not have to use this parameter interface; but don't try to use
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200237function i2c_probe() if you don't.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
David Brownell4298cfc2007-05-01 23:26:31 +0200240Probing classes (Legacy model)
241------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243All parameters are given as lists of unsigned 16-bit integers. Lists are
244terminated by I2C_CLIENT_END.
245The following lists are used internally:
246
247 normal_i2c: filled in by the module writer.
248 A list of I2C addresses which should normally be examined.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 probe: insmod parameter.
250 A list of pairs. The first value is a bus number (-1 for any I2C bus),
251 the second is the address. These addresses are also probed, as if they
252 were in the 'normal' list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 ignore: insmod parameter.
254 A list of pairs. The first value is a bus number (-1 for any I2C bus),
255 the second is the I2C address. These addresses are never probed.
Jean Delvaref4b50262005-07-31 21:49:03 +0200256 This parameter overrules the 'normal_i2c' list only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 force: insmod parameter.
258 A list of pairs. The first value is a bus number (-1 for any I2C bus),
259 the second is the I2C address. A device is blindly assumed to be on
260 the given address, no probing is done.
261
Jean Delvaref4b50262005-07-31 21:49:03 +0200262Additionally, kind-specific force lists may optionally be defined if
263the driver supports several chip kinds. They are grouped in a
264NULL-terminated list of pointers named forces, those first element if the
265generic force list mentioned above. Each additional list correspond to an
266insmod parameter of the form force_<kind>.
267
Jean Delvareb3d54962005-04-02 20:31:02 +0200268Fortunately, as a module writer, you just have to define the `normal_i2c'
269parameter. The complete declaration could look like this:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Jean Delvare2cdddeb2008-01-27 18:14:47 +0100271 /* Scan 0x4c to 0x4f */
272 static const unsigned short normal_i2c[] = { 0x4c, 0x4d, 0x4e, 0x4f,
273 I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /* Magic definition of all other variables and things */
276 I2C_CLIENT_INSMOD;
Jean Delvaref4b50262005-07-31 21:49:03 +0200277 /* Or, if your driver supports, say, 2 kind of devices: */
278 I2C_CLIENT_INSMOD_2(foo, bar);
279
280If you use the multi-kind form, an enum will be defined for you:
281 enum chips { any_chip, foo, bar, ... }
282You can then (and certainly should) use it in the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Jean Delvareb3d54962005-04-02 20:31:02 +0200284Note that you *have* to call the defined variable `normal_i2c',
285without any prefix!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287
David Brownell4298cfc2007-05-01 23:26:31 +0200288Attaching to an adapter (Legacy model)
289--------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291Whenever a new adapter is inserted, or for all adapters if the driver is
292being registered, the callback attach_adapter() is called. Now is the
293time to determine what devices are present on the adapter, and to register
294a client for each of them.
295
296The attach_adapter callback is really easy: we just call the generic
297detection function. This function will scan the bus for us, using the
298information as defined in the lists explained above. If a device is
299detected at a specific address, another callback is called.
300
301 int foo_attach_adapter(struct i2c_adapter *adapter)
302 {
303 return i2c_probe(adapter,&addr_data,&foo_detect_client);
304 }
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306Remember, structure `addr_data' is defined by the macros explained above,
307so you do not have to define it yourself.
308
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200309The i2c_probe function will call the foo_detect_client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310function only for those i2c addresses that actually have a device on
311them (unless a `force' parameter was used). In addition, addresses that
312are already in use (by some other registered client) are skipped.
313
314
David Brownell4298cfc2007-05-01 23:26:31 +0200315The detect client function (Legacy model)
316-----------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200318The detect client function is called by i2c_probe. The `kind' parameter
319contains -1 for a probed detection, 0 for a forced detection, or a positive
320number for a forced detection with a chip type forced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200322Returning an error different from -ENODEV in a detect function will cause
323the detection to stop: other addresses and adapters won't be scanned.
324This should only be done on fatal or internal errors, such as a memory
325shortage or i2c_attach_client failing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327For now, you can ignore the `flags' parameter. It is there for future use.
328
329 int foo_detect_client(struct i2c_adapter *adapter, int address,
Jean Delvareeefcd752007-05-01 23:26:35 +0200330 int kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 {
332 int err = 0;
333 int i;
Jean Delvareeefcd752007-05-01 23:26:35 +0200334 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 struct foo_data *data;
Jean Delvareeefcd752007-05-01 23:26:35 +0200336 const char *name = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /* Let's see whether this adapter can support what we need.
Jean Delvareeefcd752007-05-01 23:26:35 +0200339 Please substitute the things you need here! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 if (!i2c_check_functionality(adapter,I2C_FUNC_SMBUS_WORD_DATA |
341 I2C_FUNC_SMBUS_WRITE_BYTE))
342 goto ERROR0;
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /* OK. For now, we presume we have a valid client. We now create the
345 client structure, even though we cannot fill it completely yet.
346 But it allows us to access several i2c functions safely */
347
Jean Delvare2445eb62005-10-17 23:16:25 +0200348 if (!(data = kzalloc(sizeof(struct foo_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 err = -ENOMEM;
350 goto ERROR0;
351 }
352
Jean Delvareeefcd752007-05-01 23:26:35 +0200353 client = &data->client;
354 i2c_set_clientdata(client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Jean Delvareeefcd752007-05-01 23:26:35 +0200356 client->addr = address;
357 client->adapter = adapter;
358 client->driver = &foo_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 /* Now, we do the remaining detection. If no `force' parameter is used. */
361
362 /* First, the generic detection (if any), that is skipped if any force
363 parameter was used. */
364 if (kind < 0) {
365 /* The below is of course bogus */
Jean Delvareeefcd752007-05-01 23:26:35 +0200366 if (foo_read(client, FOO_REG_GENERIC) != FOO_GENERIC_VALUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 goto ERROR1;
368 }
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* Next, specific detection. This is especially important for `sensors'
371 devices. */
372
373 /* Determine the chip type. Not needed if a `force_CHIPTYPE' parameter
374 was used. */
375 if (kind <= 0) {
Jean Delvareeefcd752007-05-01 23:26:35 +0200376 i = foo_read(client, FOO_REG_CHIPTYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (i == FOO_TYPE_1)
378 kind = chip1; /* As defined in the enum */
379 else if (i == FOO_TYPE_2)
380 kind = chip2;
381 else {
382 printk("foo: Ignoring 'force' parameter for unknown chip at "
383 "adapter %d, address 0x%02x\n",i2c_adapter_id(adapter),address);
384 goto ERROR1;
385 }
386 }
387
388 /* Now set the type and chip names */
389 if (kind == chip1) {
Jean Delvareeefcd752007-05-01 23:26:35 +0200390 name = "chip1";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 } else if (kind == chip2) {
Jean Delvareeefcd752007-05-01 23:26:35 +0200392 name = "chip2";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* Fill in the remaining client fields. */
Jean Delvareeefcd752007-05-01 23:26:35 +0200396 strlcpy(client->name, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 data->type = kind;
Jean Delvareeefcd752007-05-01 23:26:35 +0200398 mutex_init(&data->update_lock); /* Only if you use this field */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /* Any other initializations in data must be done here too. */
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /* This function can write default values to the client registers, if
403 needed. */
Jean Delvareeefcd752007-05-01 23:26:35 +0200404 foo_init_client(client);
405
406 /* Tell the i2c layer a new client has arrived */
407 if ((err = i2c_attach_client(client)))
408 goto ERROR1;
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 return 0;
411
412 /* OK, this is not exactly good programming practice, usually. But it is
413 very code-efficient in this case. */
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 ERROR1:
Jean Delvarea852daa2005-11-02 21:42:48 +0100416 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 ERROR0:
418 return err;
419 }
420
421
David Brownell4298cfc2007-05-01 23:26:31 +0200422Removing the client (Legacy model)
423==================================
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425The detach_client call back function is called when a client should be
426removed. It may actually fail, but only when panicking. This code is
427much simpler than the attachment code, fortunately!
428
429 int foo_detach_client(struct i2c_client *client)
430 {
Jean Delvareeefcd752007-05-01 23:26:35 +0200431 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 /* Try to detach the client from i2c space */
Jean Delvare7bef5592005-07-27 22:14:49 +0200434 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Jean Delvarea852daa2005-11-02 21:42:48 +0100437 kfree(i2c_get_clientdata(client));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439 }
440
441
442Initializing the module or kernel
443=================================
444
445When the kernel is booted, or when your foo driver module is inserted,
446you have to do some initializing. Fortunately, just attaching (registering)
447the driver module is usually enough.
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 static int __init foo_init(void)
450 {
451 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 if ((res = i2c_add_driver(&foo_driver))) {
454 printk("foo: Driver registration failed, module not inserted.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return res;
456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return 0;
458 }
459
Jean Delvareeefcd752007-05-01 23:26:35 +0200460 static void __exit foo_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 {
Jean Delvareeefcd752007-05-01 23:26:35 +0200462 i2c_del_driver(&foo_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 }
464
465 /* Substitute your own name and email address */
466 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"
467 MODULE_DESCRIPTION("Driver for Barf Inc. Foo I2C devices");
468
Jean Delvareeefcd752007-05-01 23:26:35 +0200469 /* a few non-GPL license types are also allowed */
470 MODULE_LICENSE("GPL");
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 module_init(foo_init);
473 module_exit(foo_cleanup);
474
475Note that some functions are marked by `__init', and some data structures
Jean Delvareeefcd752007-05-01 23:26:35 +0200476by `__initdata'. These functions and structures can be removed after
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477kernel booting (or module loading) is completed.
478
Jean Delvarefb687d72005-12-18 16:51:55 +0100479
David Brownellf37dd802007-02-13 22:09:00 +0100480Power Management
481================
482
483If your I2C device needs special handling when entering a system low
484power state -- like putting a transceiver into a low power mode, or
485activating a system wakeup mechanism -- do that in the suspend() method.
486The resume() method should reverse what the suspend() method does.
487
488These are standard driver model calls, and they work just like they
489would for any other driver stack. The calls can sleep, and can use
490I2C messaging to the device being suspended or resumed (since their
491parent I2C adapter is active when these calls are issued, and IRQs
492are still enabled).
493
494
495System Shutdown
496===============
497
498If your I2C device needs special handling when the system shuts down
499or reboots (including kexec) -- like turning something off -- use a
500shutdown() method.
501
502Again, this is a standard driver model call, working just like it
503would for any other driver stack: the calls can sleep, and can use
504I2C messaging.
505
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507Command function
508================
509
510A generic ioctl-like function call back is supported. You will seldom
Jean Delvarefb687d72005-12-18 16:51:55 +0100511need this, and its use is deprecated anyway, so newer design should not
512use it. Set it to NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514
515Sending and receiving
516=====================
517
518If you want to communicate with your device, there are several functions
519to do this. You can find all of them in i2c.h.
520
521If you can choose between plain i2c communication and SMBus level
522communication, please use the last. All adapters understand SMBus level
523commands, but only some of them understand plain i2c!
524
525
526Plain i2c communication
527-----------------------
528
529 extern int i2c_master_send(struct i2c_client *,const char* ,int);
530 extern int i2c_master_recv(struct i2c_client *,char* ,int);
531
532These routines read and write some bytes from/to a client. The client
533contains the i2c address, so you do not have to include it. The second
534parameter contains the bytes the read/write, the third the length of the
535buffer. Returned is the actual number of bytes read/written.
536
537 extern int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msg,
538 int num);
539
540This sends a series of messages. Each message can be a read or write,
541and they can be mixed in any way. The transactions are combined: no
542stop bit is sent between transaction. The i2c_msg structure contains
543for each message the client address, the number of bytes of the message
544and the message data itself.
545
546You can read the file `i2c-protocol' for more information about the
547actual i2c protocol.
548
549
550SMBus communication
551-------------------
552
553 extern s32 i2c_smbus_xfer (struct i2c_adapter * adapter, u16 addr,
554 unsigned short flags,
555 char read_write, u8 command, int size,
556 union i2c_smbus_data * data);
557
558 This is the generic SMBus function. All functions below are implemented
559 in terms of it. Never use this function directly!
560
561
562 extern s32 i2c_smbus_write_quick(struct i2c_client * client, u8 value);
563 extern s32 i2c_smbus_read_byte(struct i2c_client * client);
564 extern s32 i2c_smbus_write_byte(struct i2c_client * client, u8 value);
565 extern s32 i2c_smbus_read_byte_data(struct i2c_client * client, u8 command);
566 extern s32 i2c_smbus_write_byte_data(struct i2c_client * client,
567 u8 command, u8 value);
568 extern s32 i2c_smbus_read_word_data(struct i2c_client * client, u8 command);
569 extern s32 i2c_smbus_write_word_data(struct i2c_client * client,
570 u8 command, u16 value);
571 extern s32 i2c_smbus_write_block_data(struct i2c_client * client,
572 u8 command, u8 length,
573 u8 *values);
Jean Delvare7865e242005-10-08 00:00:31 +0200574 extern s32 i2c_smbus_read_i2c_block_data(struct i2c_client * client,
Jean Delvare4b2643d2007-07-12 14:12:29 +0200575 u8 command, u8 length, u8 *values);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577These ones were removed in Linux 2.6.10 because they had no users, but could
578be added back later if needed:
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 extern s32 i2c_smbus_read_block_data(struct i2c_client * client,
581 u8 command, u8 *values);
582 extern s32 i2c_smbus_write_i2c_block_data(struct i2c_client * client,
583 u8 command, u8 length,
584 u8 *values);
585 extern s32 i2c_smbus_process_call(struct i2c_client * client,
586 u8 command, u16 value);
587 extern s32 i2c_smbus_block_process_call(struct i2c_client *client,
588 u8 command, u8 length,
589 u8 *values)
590
591All these transactions return -1 on failure. The 'write' transactions
592return 0 on success; the 'read' transactions return the read value, except
593for read_block, which returns the number of values read. The block buffers
594need not be longer than 32 bytes.
595
596You can read the file `smbus-protocol' for more information about the
597actual SMBus protocol.
598
599
600General purpose routines
601========================
602
603Below all general purpose routines are listed, that were not mentioned
604before.
605
Jean Delvareeefcd752007-05-01 23:26:35 +0200606 /* This call returns a unique low identifier for each registered adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 */
608 extern int i2c_adapter_id(struct i2c_adapter *adap);
609