blob: 6b61b3a2e90bee2b7cbed065160bd029db89ba08 [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
Ben Dooks2260e632008-07-01 22:38:18 +020028/* iff driver uses driver model ("new style") binding model: */
29
30static struct i2c_device_id foo_idtable[] = {
31 { "foo", my_id_for_foo },
32 { "bar", my_id_for_bar },
33 { }
34};
35
36MODULE_DEVICE_TABLE(i2c, foo_idtable);
37
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static struct i2c_driver foo_driver = {
Jean Delvared45d2042005-11-26 20:55:35 +010039 .driver = {
Jean Delvared45d2042005-11-26 20:55:35 +010040 .name = "foo",
41 },
David Brownell4298cfc2007-05-01 23:26:31 +020042
43 /* iff driver uses driver model ("new style") binding model: */
Ben Dooks2260e632008-07-01 22:38:18 +020044 .id_table = foo_ids,
David Brownell4298cfc2007-05-01 23:26:31 +020045 .probe = foo_probe,
46 .remove = foo_remove,
Jean Delvare4735c982008-07-14 22:38:36 +020047 /* if device autodetection is needed: */
48 .class = I2C_CLASS_SOMETHING,
49 .detect = foo_detect,
50 .address_data = &addr_data,
David Brownell4298cfc2007-05-01 23:26:31 +020051
52 /* else, driver uses "legacy" binding model: */
David Brownellf37dd802007-02-13 22:09:00 +010053 .attach_adapter = foo_attach_adapter,
54 .detach_client = foo_detach_client,
David Brownell4298cfc2007-05-01 23:26:31 +020055
56 /* these may be used regardless of the driver binding model */
David Brownellf37dd802007-02-13 22:09:00 +010057 .shutdown = foo_shutdown, /* optional */
58 .suspend = foo_suspend, /* optional */
59 .resume = foo_resume, /* optional */
60 .command = foo_command, /* optional */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061}
62
David Brownellf37dd802007-02-13 22:09:00 +010063The name field is the driver name, and must not contain spaces. It
64should match the module name (if the driver can be compiled as a module),
65although you can use MODULE_ALIAS (passing "foo" in this example) to add
David Brownell4298cfc2007-05-01 23:26:31 +020066another name for the module. If the driver name doesn't match the module
67name, the module won't be automatically loaded (hotplug/coldplug).
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Linus Torvalds1da177e2005-04-16 15:20:36 -070069All other fields are for call-back functions which will be explained
70below.
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73Extra client data
74=================
75
David Brownellf37dd802007-02-13 22:09:00 +010076Each client structure has a special `data' field that can point to any
77structure at all. You should use this to keep device-specific data,
78especially in drivers that handle multiple I2C or SMBUS devices. You
Linus Torvalds1da177e2005-04-16 15:20:36 -070079do not always need this, but especially for `sensors' drivers, it can
80be very useful.
81
David Brownellf37dd802007-02-13 22:09:00 +010082 /* store the value */
83 void i2c_set_clientdata(struct i2c_client *client, void *data);
84
85 /* retrieve the value */
86 void *i2c_get_clientdata(struct i2c_client *client);
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088An example structure is below.
89
90 struct foo_data {
Jean Delvare2445eb62005-10-17 23:16:25 +020091 struct i2c_client client;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 enum chips type; /* To keep the chips type for `sensors' drivers. */
93
94 /* Because the i2c bus is slow, it is often useful to cache the read
95 information of a chip for some time (for example, 1 or 2 seconds).
96 It depends of course on the device whether this is really worthwhile
97 or even sensible. */
Jean Delvareeefcd752007-05-01 23:26:35 +020098 struct mutex update_lock; /* When we are reading lots of information,
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 another process should not update the
100 below information */
101 char valid; /* != 0 if the following fields are valid. */
102 unsigned long last_updated; /* In jiffies */
103 /* Add the read information here too */
104 };
105
106
107Accessing the client
108====================
109
110Let's say we have a valid client structure. At some time, we will need
111to gather information from the client, or write new information to the
112client. How we will export this information to user-space is less
113important at this moment (perhaps we do not need to do this at all for
114some obscure clients). But we need generic reading and writing routines.
115
116I have found it useful to define foo_read and foo_write function for this.
117For some cases, it will be easier to call the i2c functions directly,
118but many chips have some kind of register-value idea that can easily
Jean Delvareeefcd752007-05-01 23:26:35 +0200119be encapsulated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121The below functions are simple examples, and should not be copied
122literally.
123
124 int foo_read_value(struct i2c_client *client, u8 reg)
125 {
126 if (reg < 0x10) /* byte-sized register */
127 return i2c_smbus_read_byte_data(client,reg);
128 else /* word-sized register */
129 return i2c_smbus_read_word_data(client,reg);
130 }
131
132 int foo_write_value(struct i2c_client *client, u8 reg, u16 value)
133 {
134 if (reg == 0x10) /* Impossible to write - driver error! */ {
135 return -1;
136 else if (reg < 0x10) /* byte-sized register */
137 return i2c_smbus_write_byte_data(client,reg,value);
138 else /* word-sized register */
139 return i2c_smbus_write_word_data(client,reg,value);
140 }
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143Probing and attaching
144=====================
145
David Brownell4298cfc2007-05-01 23:26:31 +0200146The Linux I2C stack was originally written to support access to hardware
147monitoring chips on PC motherboards, and thus it embeds some assumptions
148that are more appropriate to SMBus (and PCs) than to I2C. One of these
149assumptions is that most adapters and devices drivers support the SMBUS_QUICK
150protocol to probe device presence. Another is that devices and their drivers
151can be sufficiently configured using only such probe primitives.
152
153As Linux and its I2C stack became more widely used in embedded systems
154and complex components such as DVB adapters, those assumptions became more
155problematic. Drivers for I2C devices that issue interrupts need more (and
156different) configuration information, as do drivers handling chip variants
157that can't be distinguished by protocol probing, or which need some board
158specific information to operate correctly.
159
160Accordingly, the I2C stack now has two models for associating I2C devices
161with their drivers: the original "legacy" model, and a newer one that's
162fully compatible with the Linux 2.6 driver model. These models do not mix,
163since the "legacy" model requires drivers to create "i2c_client" device
164objects after SMBus style probing, while the Linux driver model expects
165drivers to be given such device objects in their probe() routines.
166
167
168Standard Driver Model Binding ("New Style")
169-------------------------------------------
170
171System infrastructure, typically board-specific initialization code or
172boot firmware, reports what I2C devices exist. For example, there may be
173a table, in the kernel or from the boot loader, identifying I2C devices
174and linking them to board-specific configuration information about IRQs
175and other wiring artifacts, chip type, and so on. That could be used to
176create i2c_client objects for each I2C device.
177
178I2C device drivers using this binding model work just like any other
179kind of driver in Linux: they provide a probe() method to bind to
180those devices, and a remove() method to unbind.
181
Jean Delvared2653e92008-04-29 23:11:39 +0200182 static int foo_probe(struct i2c_client *client,
183 const struct i2c_device_id *id);
David Brownell4298cfc2007-05-01 23:26:31 +0200184 static int foo_remove(struct i2c_client *client);
185
186Remember that the i2c_driver does not create those client handles. The
187handle may be used during foo_probe(). If foo_probe() reports success
188(zero not a negative status code) it may save the handle and use it until
189foo_remove() returns. That binding model is used by most Linux drivers.
190
Ben Dooks2260e632008-07-01 22:38:18 +0200191The probe function is called when an entry in the id_table name field
192matches the device's name. It is passed the entry that was matched so
193the driver knows which one in the table matched.
David Brownell4298cfc2007-05-01 23:26:31 +0200194
195
Jean Delvarece9e0792007-05-01 23:26:32 +0200196Device Creation (Standard driver model)
197---------------------------------------
198
199If you know for a fact that an I2C device is connected to a given I2C bus,
200you can instantiate that device by simply filling an i2c_board_info
201structure with the device address and driver name, and calling
202i2c_new_device(). This will create the device, then the driver core will
203take care of finding the right driver and will call its probe() method.
204If a driver supports different device types, you can specify the type you
205want using the type field. You can also specify an IRQ and platform data
206if needed.
207
208Sometimes you know that a device is connected to a given I2C bus, but you
209don't know the exact address it uses. This happens on TV adapters for
210example, where the same driver supports dozens of slightly different
211models, and I2C device addresses change from one model to the next. In
212that case, you can use the i2c_new_probed_device() variant, which is
213similar to i2c_new_device(), except that it takes an additional list of
214possible I2C addresses to probe. A device is created for the first
215responsive address in the list. If you expect more than one device to be
216present in the address range, simply call i2c_new_probed_device() that
217many times.
218
219The call to i2c_new_device() or i2c_new_probed_device() typically happens
220in the I2C bus driver. You may want to save the returned i2c_client
221reference for later use.
222
223
Jean Delvare4735c982008-07-14 22:38:36 +0200224Device Detection (Standard driver model)
225----------------------------------------
226
227Sometimes you do not know in advance which I2C devices are connected to
228a given I2C bus. This is for example the case of hardware monitoring
229devices on a PC's SMBus. In that case, you may want to let your driver
230detect supported devices automatically. This is how the legacy model
231was working, and is now available as an extension to the standard
232driver model (so that we can finally get rid of the legacy model.)
233
234You simply have to define a detect callback which will attempt to
235identify supported devices (returning 0 for supported ones and -ENODEV
236for unsupported ones), a list of addresses to probe, and a device type
237(or class) so that only I2C buses which may have that type of device
238connected (and not otherwise enumerated) will be probed. The i2c
239core will then call you back as needed and will instantiate a device
240for you for every successful detection.
241
242Note that this mechanism is purely optional and not suitable for all
243devices. You need some reliable way to identify the supported devices
244(typically using device-specific, dedicated identification registers),
245otherwise misdetections are likely to occur and things can get wrong
246quickly.
247
248
Jean Delvarece9e0792007-05-01 23:26:32 +0200249Device Deletion (Standard driver model)
250---------------------------------------
251
252Each I2C device which has been created using i2c_new_device() or
253i2c_new_probed_device() can be unregistered by calling
254i2c_unregister_device(). If you don't call it explicitly, it will be
255called automatically before the underlying I2C bus itself is removed, as a
256device can't survive its parent in the device driver model.
257
258
David Brownell4298cfc2007-05-01 23:26:31 +0200259Legacy Driver Binding Model
260---------------------------
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262Most i2c devices can be present on several i2c addresses; for some this
263is determined in hardware (by soldering some chip pins to Vcc or Ground),
264for others this can be changed in software (by writing to specific client
265registers). Some devices are usually on a specific address, but not always;
266and some are even more tricky. So you will probably need to scan several
267i2c addresses for your clients, and do some sort of detection to see
268whether it is actually a device supported by your driver.
269
270To give the user a maximum of possibilities, some default module parameters
271are defined to help determine what addresses are scanned. Several macros
272are defined in i2c.h to help you support them, as well as a generic
273detection algorithm.
274
275You do not have to use this parameter interface; but don't try to use
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200276function i2c_probe() if you don't.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
David Brownell4298cfc2007-05-01 23:26:31 +0200279Probing classes (Legacy model)
280------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282All parameters are given as lists of unsigned 16-bit integers. Lists are
283terminated by I2C_CLIENT_END.
284The following lists are used internally:
285
286 normal_i2c: filled in by the module writer.
287 A list of I2C addresses which should normally be examined.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 probe: insmod parameter.
289 A list of pairs. The first value is a bus number (-1 for any I2C bus),
290 the second is the address. These addresses are also probed, as if they
291 were in the 'normal' list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 ignore: insmod parameter.
293 A list of pairs. The first value is a bus number (-1 for any I2C bus),
294 the second is the I2C address. These addresses are never probed.
Jean Delvaref4b50262005-07-31 21:49:03 +0200295 This parameter overrules the 'normal_i2c' list only.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 force: insmod parameter.
297 A list of pairs. The first value is a bus number (-1 for any I2C bus),
298 the second is the I2C address. A device is blindly assumed to be on
299 the given address, no probing is done.
300
Jean Delvaref4b50262005-07-31 21:49:03 +0200301Additionally, kind-specific force lists may optionally be defined if
302the driver supports several chip kinds. They are grouped in a
303NULL-terminated list of pointers named forces, those first element if the
304generic force list mentioned above. Each additional list correspond to an
305insmod parameter of the form force_<kind>.
306
Jean Delvareb3d54962005-04-02 20:31:02 +0200307Fortunately, as a module writer, you just have to define the `normal_i2c'
308parameter. The complete declaration could look like this:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Jean Delvare2cdddeb2008-01-27 18:14:47 +0100310 /* Scan 0x4c to 0x4f */
311 static const unsigned short normal_i2c[] = { 0x4c, 0x4d, 0x4e, 0x4f,
312 I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* Magic definition of all other variables and things */
315 I2C_CLIENT_INSMOD;
Jean Delvaref4b50262005-07-31 21:49:03 +0200316 /* Or, if your driver supports, say, 2 kind of devices: */
317 I2C_CLIENT_INSMOD_2(foo, bar);
318
319If you use the multi-kind form, an enum will be defined for you:
320 enum chips { any_chip, foo, bar, ... }
321You can then (and certainly should) use it in the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Jean Delvareb3d54962005-04-02 20:31:02 +0200323Note that you *have* to call the defined variable `normal_i2c',
324without any prefix!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326
David Brownell4298cfc2007-05-01 23:26:31 +0200327Attaching to an adapter (Legacy model)
328--------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330Whenever a new adapter is inserted, or for all adapters if the driver is
331being registered, the callback attach_adapter() is called. Now is the
332time to determine what devices are present on the adapter, and to register
333a client for each of them.
334
335The attach_adapter callback is really easy: we just call the generic
336detection function. This function will scan the bus for us, using the
337information as defined in the lists explained above. If a device is
338detected at a specific address, another callback is called.
339
340 int foo_attach_adapter(struct i2c_adapter *adapter)
341 {
342 return i2c_probe(adapter,&addr_data,&foo_detect_client);
343 }
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345Remember, structure `addr_data' is defined by the macros explained above,
346so you do not have to define it yourself.
347
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200348The i2c_probe function will call the foo_detect_client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349function only for those i2c addresses that actually have a device on
350them (unless a `force' parameter was used). In addition, addresses that
351are already in use (by some other registered client) are skipped.
352
353
David Brownell4298cfc2007-05-01 23:26:31 +0200354The detect client function (Legacy model)
355-----------------------------------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200357The detect client function is called by i2c_probe. The `kind' parameter
358contains -1 for a probed detection, 0 for a forced detection, or a positive
359number for a forced detection with a chip type forced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Jean Delvarea89ba0b2005-08-09 20:17:55 +0200361Returning an error different from -ENODEV in a detect function will cause
362the detection to stop: other addresses and adapters won't be scanned.
363This should only be done on fatal or internal errors, such as a memory
364shortage or i2c_attach_client failing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366For now, you can ignore the `flags' parameter. It is there for future use.
367
368 int foo_detect_client(struct i2c_adapter *adapter, int address,
Jean Delvareeefcd752007-05-01 23:26:35 +0200369 int kind)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 {
371 int err = 0;
372 int i;
Jean Delvareeefcd752007-05-01 23:26:35 +0200373 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct foo_data *data;
Jean Delvareeefcd752007-05-01 23:26:35 +0200375 const char *name = "";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /* Let's see whether this adapter can support what we need.
Jean Delvareeefcd752007-05-01 23:26:35 +0200378 Please substitute the things you need here! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (!i2c_check_functionality(adapter,I2C_FUNC_SMBUS_WORD_DATA |
380 I2C_FUNC_SMBUS_WRITE_BYTE))
381 goto ERROR0;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /* OK. For now, we presume we have a valid client. We now create the
384 client structure, even though we cannot fill it completely yet.
385 But it allows us to access several i2c functions safely */
386
Jean Delvare2445eb62005-10-17 23:16:25 +0200387 if (!(data = kzalloc(sizeof(struct foo_data), GFP_KERNEL))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 err = -ENOMEM;
389 goto ERROR0;
390 }
391
Jean Delvareeefcd752007-05-01 23:26:35 +0200392 client = &data->client;
393 i2c_set_clientdata(client, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Jean Delvareeefcd752007-05-01 23:26:35 +0200395 client->addr = address;
396 client->adapter = adapter;
397 client->driver = &foo_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /* Now, we do the remaining detection. If no `force' parameter is used. */
400
401 /* First, the generic detection (if any), that is skipped if any force
402 parameter was used. */
403 if (kind < 0) {
404 /* The below is of course bogus */
Jean Delvareeefcd752007-05-01 23:26:35 +0200405 if (foo_read(client, FOO_REG_GENERIC) != FOO_GENERIC_VALUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 goto ERROR1;
407 }
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /* Next, specific detection. This is especially important for `sensors'
410 devices. */
411
412 /* Determine the chip type. Not needed if a `force_CHIPTYPE' parameter
413 was used. */
414 if (kind <= 0) {
Jean Delvareeefcd752007-05-01 23:26:35 +0200415 i = foo_read(client, FOO_REG_CHIPTYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (i == FOO_TYPE_1)
417 kind = chip1; /* As defined in the enum */
418 else if (i == FOO_TYPE_2)
419 kind = chip2;
420 else {
421 printk("foo: Ignoring 'force' parameter for unknown chip at "
422 "adapter %d, address 0x%02x\n",i2c_adapter_id(adapter),address);
423 goto ERROR1;
424 }
425 }
426
427 /* Now set the type and chip names */
428 if (kind == chip1) {
Jean Delvareeefcd752007-05-01 23:26:35 +0200429 name = "chip1";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 } else if (kind == chip2) {
Jean Delvareeefcd752007-05-01 23:26:35 +0200431 name = "chip2";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /* Fill in the remaining client fields. */
Jean Delvareeefcd752007-05-01 23:26:35 +0200435 strlcpy(client->name, name, I2C_NAME_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 data->type = kind;
Jean Delvareeefcd752007-05-01 23:26:35 +0200437 mutex_init(&data->update_lock); /* Only if you use this field */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /* Any other initializations in data must be done here too. */
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 /* This function can write default values to the client registers, if
442 needed. */
Jean Delvareeefcd752007-05-01 23:26:35 +0200443 foo_init_client(client);
444
445 /* Tell the i2c layer a new client has arrived */
446 if ((err = i2c_attach_client(client)))
447 goto ERROR1;
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return 0;
450
451 /* OK, this is not exactly good programming practice, usually. But it is
452 very code-efficient in this case. */
453
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 ERROR1:
Jean Delvarea852daa2005-11-02 21:42:48 +0100455 kfree(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 ERROR0:
457 return err;
458 }
459
460
David Brownell4298cfc2007-05-01 23:26:31 +0200461Removing the client (Legacy model)
462==================================
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464The detach_client call back function is called when a client should be
465removed. It may actually fail, but only when panicking. This code is
466much simpler than the attachment code, fortunately!
467
468 int foo_detach_client(struct i2c_client *client)
469 {
Jean Delvareeefcd752007-05-01 23:26:35 +0200470 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 /* Try to detach the client from i2c space */
Jean Delvare7bef5592005-07-27 22:14:49 +0200473 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Jean Delvarea852daa2005-11-02 21:42:48 +0100476 kfree(i2c_get_clientdata(client));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 return 0;
478 }
479
480
481Initializing the module or kernel
482=================================
483
484When the kernel is booted, or when your foo driver module is inserted,
485you have to do some initializing. Fortunately, just attaching (registering)
486the driver module is usually enough.
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 static int __init foo_init(void)
489 {
490 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 if ((res = i2c_add_driver(&foo_driver))) {
493 printk("foo: Driver registration failed, module not inserted.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 return res;
495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 return 0;
497 }
498
Jean Delvareeefcd752007-05-01 23:26:35 +0200499 static void __exit foo_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 {
Jean Delvareeefcd752007-05-01 23:26:35 +0200501 i2c_del_driver(&foo_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503
504 /* Substitute your own name and email address */
505 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"
506 MODULE_DESCRIPTION("Driver for Barf Inc. Foo I2C devices");
507
Jean Delvareeefcd752007-05-01 23:26:35 +0200508 /* a few non-GPL license types are also allowed */
509 MODULE_LICENSE("GPL");
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 module_init(foo_init);
512 module_exit(foo_cleanup);
513
514Note that some functions are marked by `__init', and some data structures
Jean Delvareeefcd752007-05-01 23:26:35 +0200515by `__initdata'. These functions and structures can be removed after
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516kernel booting (or module loading) is completed.
517
Jean Delvarefb687d72005-12-18 16:51:55 +0100518
David Brownellf37dd802007-02-13 22:09:00 +0100519Power Management
520================
521
522If your I2C device needs special handling when entering a system low
523power state -- like putting a transceiver into a low power mode, or
524activating a system wakeup mechanism -- do that in the suspend() method.
525The resume() method should reverse what the suspend() method does.
526
527These are standard driver model calls, and they work just like they
528would for any other driver stack. The calls can sleep, and can use
529I2C messaging to the device being suspended or resumed (since their
530parent I2C adapter is active when these calls are issued, and IRQs
531are still enabled).
532
533
534System Shutdown
535===============
536
537If your I2C device needs special handling when the system shuts down
538or reboots (including kexec) -- like turning something off -- use a
539shutdown() method.
540
541Again, this is a standard driver model call, working just like it
542would for any other driver stack: the calls can sleep, and can use
543I2C messaging.
544
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546Command function
547================
548
549A generic ioctl-like function call back is supported. You will seldom
Jean Delvarefb687d72005-12-18 16:51:55 +0100550need this, and its use is deprecated anyway, so newer design should not
551use it. Set it to NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553
554Sending and receiving
555=====================
556
557If you want to communicate with your device, there are several functions
558to do this. You can find all of them in i2c.h.
559
560If you can choose between plain i2c communication and SMBus level
561communication, please use the last. All adapters understand SMBus level
562commands, but only some of them understand plain i2c!
563
564
565Plain i2c communication
566-----------------------
567
568 extern int i2c_master_send(struct i2c_client *,const char* ,int);
569 extern int i2c_master_recv(struct i2c_client *,char* ,int);
570
571These routines read and write some bytes from/to a client. The client
572contains the i2c address, so you do not have to include it. The second
573parameter contains the bytes the read/write, the third the length of the
574buffer. Returned is the actual number of bytes read/written.
575
576 extern int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msg,
577 int num);
578
579This sends a series of messages. Each message can be a read or write,
580and they can be mixed in any way. The transactions are combined: no
581stop bit is sent between transaction. The i2c_msg structure contains
582for each message the client address, the number of bytes of the message
583and the message data itself.
584
585You can read the file `i2c-protocol' for more information about the
586actual i2c protocol.
587
588
589SMBus communication
590-------------------
591
592 extern s32 i2c_smbus_xfer (struct i2c_adapter * adapter, u16 addr,
593 unsigned short flags,
594 char read_write, u8 command, int size,
595 union i2c_smbus_data * data);
596
597 This is the generic SMBus function. All functions below are implemented
598 in terms of it. Never use this function directly!
599
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 extern s32 i2c_smbus_read_byte(struct i2c_client * client);
602 extern s32 i2c_smbus_write_byte(struct i2c_client * client, u8 value);
603 extern s32 i2c_smbus_read_byte_data(struct i2c_client * client, u8 command);
604 extern s32 i2c_smbus_write_byte_data(struct i2c_client * client,
605 u8 command, u8 value);
606 extern s32 i2c_smbus_read_word_data(struct i2c_client * client, u8 command);
607 extern s32 i2c_smbus_write_word_data(struct i2c_client * client,
608 u8 command, u16 value);
Jean Delvare67c2e662008-07-14 22:38:23 +0200609 extern s32 i2c_smbus_read_block_data(struct i2c_client * client,
610 u8 command, u8 *values);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 extern s32 i2c_smbus_write_block_data(struct i2c_client * client,
612 u8 command, u8 length,
613 u8 *values);
Jean Delvare7865e242005-10-08 00:00:31 +0200614 extern s32 i2c_smbus_read_i2c_block_data(struct i2c_client * client,
Jean Delvare4b2643d2007-07-12 14:12:29 +0200615 u8 command, u8 length, u8 *values);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 extern s32 i2c_smbus_write_i2c_block_data(struct i2c_client * client,
617 u8 command, u8 length,
618 u8 *values);
Jean Delvare67c2e662008-07-14 22:38:23 +0200619
620These ones were removed from i2c-core because they had no users, but could
621be added back later if needed:
622
623 extern s32 i2c_smbus_write_quick(struct i2c_client * client, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 extern s32 i2c_smbus_process_call(struct i2c_client * client,
625 u8 command, u16 value);
626 extern s32 i2c_smbus_block_process_call(struct i2c_client *client,
627 u8 command, u8 length,
628 u8 *values)
629
David Brownell24a5bb72008-07-14 22:38:23 +0200630All these transactions return a negative errno value on failure. The 'write'
631transactions return 0 on success; the 'read' transactions return the read
632value, except for block transactions, which return the number of values
633read. The block buffers need not be longer than 32 bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635You can read the file `smbus-protocol' for more information about the
636actual SMBus protocol.
637
638
639General purpose routines
640========================
641
642Below all general purpose routines are listed, that were not mentioned
643before.
644
Jean Delvareeefcd752007-05-01 23:26:35 +0200645 /* This call returns a unique low identifier for each registered adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 */
647 extern int i2c_adapter_id(struct i2c_adapter *adap);
648