blob: c3e1885776873839b7913bd5f6351aa7fc39eee6 [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 +020028static struct i2c_device_id foo_idtable[] = {
29 { "foo", my_id_for_foo },
30 { "bar", my_id_for_bar },
31 { }
32};
33
34MODULE_DEVICE_TABLE(i2c, foo_idtable);
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static struct i2c_driver foo_driver = {
Jean Delvared45d2042005-11-26 20:55:35 +010037 .driver = {
Jean Delvared45d2042005-11-26 20:55:35 +010038 .name = "foo",
39 },
David Brownell4298cfc2007-05-01 23:26:31 +020040
Ben Dooks2260e632008-07-01 22:38:18 +020041 .id_table = foo_ids,
David Brownell4298cfc2007-05-01 23:26:31 +020042 .probe = foo_probe,
43 .remove = foo_remove,
Jean Delvare4735c982008-07-14 22:38:36 +020044 /* if device autodetection is needed: */
45 .class = I2C_CLASS_SOMETHING,
46 .detect = foo_detect,
47 .address_data = &addr_data,
David Brownell4298cfc2007-05-01 23:26:31 +020048
David Brownellf37dd802007-02-13 22:09:00 +010049 .shutdown = foo_shutdown, /* optional */
50 .suspend = foo_suspend, /* optional */
51 .resume = foo_resume, /* optional */
52 .command = foo_command, /* optional */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
David Brownellf37dd802007-02-13 22:09:00 +010055The name field is the driver name, and must not contain spaces. It
56should match the module name (if the driver can be compiled as a module),
57although you can use MODULE_ALIAS (passing "foo" in this example) to add
David Brownell4298cfc2007-05-01 23:26:31 +020058another name for the module. If the driver name doesn't match the module
59name, the module won't be automatically loaded (hotplug/coldplug).
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061All other fields are for call-back functions which will be explained
62below.
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65Extra client data
66=================
67
David Brownellf37dd802007-02-13 22:09:00 +010068Each client structure has a special `data' field that can point to any
69structure at all. You should use this to keep device-specific data,
70especially in drivers that handle multiple I2C or SMBUS devices. You
Linus Torvalds1da177e2005-04-16 15:20:36 -070071do not always need this, but especially for `sensors' drivers, it can
72be very useful.
73
David Brownellf37dd802007-02-13 22:09:00 +010074 /* store the value */
75 void i2c_set_clientdata(struct i2c_client *client, void *data);
76
77 /* retrieve the value */
Jean Delvare7d1d8992008-10-22 20:21:31 +020078 void *i2c_get_clientdata(const struct i2c_client *client);
David Brownellf37dd802007-02-13 22:09:00 +010079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080An example structure is below.
81
82 struct foo_data {
Jean Delvaree3133532008-10-22 20:21:31 +020083 struct i2c_client *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 enum chips type; /* To keep the chips type for `sensors' drivers. */
85
86 /* Because the i2c bus is slow, it is often useful to cache the read
87 information of a chip for some time (for example, 1 or 2 seconds).
88 It depends of course on the device whether this is really worthwhile
89 or even sensible. */
Jean Delvareeefcd752007-05-01 23:26:35 +020090 struct mutex update_lock; /* When we are reading lots of information,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 another process should not update the
92 below information */
93 char valid; /* != 0 if the following fields are valid. */
94 unsigned long last_updated; /* In jiffies */
95 /* Add the read information here too */
96 };
97
98
99Accessing the client
100====================
101
102Let's say we have a valid client structure. At some time, we will need
103to gather information from the client, or write new information to the
104client. How we will export this information to user-space is less
105important at this moment (perhaps we do not need to do this at all for
106some obscure clients). But we need generic reading and writing routines.
107
108I have found it useful to define foo_read and foo_write function for this.
109For some cases, it will be easier to call the i2c functions directly,
110but many chips have some kind of register-value idea that can easily
Jean Delvareeefcd752007-05-01 23:26:35 +0200111be encapsulated.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113The below functions are simple examples, and should not be copied
114literally.
115
116 int foo_read_value(struct i2c_client *client, u8 reg)
117 {
118 if (reg < 0x10) /* byte-sized register */
119 return i2c_smbus_read_byte_data(client,reg);
120 else /* word-sized register */
121 return i2c_smbus_read_word_data(client,reg);
122 }
123
124 int foo_write_value(struct i2c_client *client, u8 reg, u16 value)
125 {
126 if (reg == 0x10) /* Impossible to write - driver error! */ {
127 return -1;
128 else if (reg < 0x10) /* byte-sized register */
129 return i2c_smbus_write_byte_data(client,reg,value);
130 else /* word-sized register */
131 return i2c_smbus_write_word_data(client,reg,value);
132 }
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135Probing and attaching
136=====================
137
David Brownell4298cfc2007-05-01 23:26:31 +0200138The Linux I2C stack was originally written to support access to hardware
Jean Delvaree3133532008-10-22 20:21:31 +0200139monitoring chips on PC motherboards, and thus used to embed some assumptions
140that were more appropriate to SMBus (and PCs) than to I2C. One of these
141assumptions was that most adapters and devices drivers support the SMBUS_QUICK
142protocol to probe device presence. Another was that devices and their drivers
David Brownell4298cfc2007-05-01 23:26:31 +0200143can be sufficiently configured using only such probe primitives.
144
145As Linux and its I2C stack became more widely used in embedded systems
146and complex components such as DVB adapters, those assumptions became more
147problematic. Drivers for I2C devices that issue interrupts need more (and
148different) configuration information, as do drivers handling chip variants
149that can't be distinguished by protocol probing, or which need some board
150specific information to operate correctly.
151
152Accordingly, the I2C stack now has two models for associating I2C devices
153with their drivers: the original "legacy" model, and a newer one that's
154fully compatible with the Linux 2.6 driver model. These models do not mix,
155since the "legacy" model requires drivers to create "i2c_client" device
156objects after SMBus style probing, while the Linux driver model expects
157drivers to be given such device objects in their probe() routines.
158
Jean Delvaree3133532008-10-22 20:21:31 +0200159The legacy model is deprecated now and will soon be removed, so we no
160longer document it here.
161
David Brownell4298cfc2007-05-01 23:26:31 +0200162
163Standard Driver Model Binding ("New Style")
164-------------------------------------------
165
166System infrastructure, typically board-specific initialization code or
167boot firmware, reports what I2C devices exist. For example, there may be
168a table, in the kernel or from the boot loader, identifying I2C devices
169and linking them to board-specific configuration information about IRQs
170and other wiring artifacts, chip type, and so on. That could be used to
171create i2c_client objects for each I2C device.
172
173I2C device drivers using this binding model work just like any other
174kind of driver in Linux: they provide a probe() method to bind to
175those devices, and a remove() method to unbind.
176
Jean Delvared2653e92008-04-29 23:11:39 +0200177 static int foo_probe(struct i2c_client *client,
178 const struct i2c_device_id *id);
David Brownell4298cfc2007-05-01 23:26:31 +0200179 static int foo_remove(struct i2c_client *client);
180
181Remember that the i2c_driver does not create those client handles. The
182handle may be used during foo_probe(). If foo_probe() reports success
183(zero not a negative status code) it may save the handle and use it until
184foo_remove() returns. That binding model is used by most Linux drivers.
185
Ben Dooks2260e632008-07-01 22:38:18 +0200186The probe function is called when an entry in the id_table name field
187matches the device's name. It is passed the entry that was matched so
188the driver knows which one in the table matched.
David Brownell4298cfc2007-05-01 23:26:31 +0200189
190
Jean Delvaree3133532008-10-22 20:21:31 +0200191Device Creation
192---------------
Jean Delvarece9e0792007-05-01 23:26:32 +0200193
194If you know for a fact that an I2C device is connected to a given I2C bus,
195you can instantiate that device by simply filling an i2c_board_info
196structure with the device address and driver name, and calling
197i2c_new_device(). This will create the device, then the driver core will
198take care of finding the right driver and will call its probe() method.
199If a driver supports different device types, you can specify the type you
200want using the type field. You can also specify an IRQ and platform data
201if needed.
202
203Sometimes you know that a device is connected to a given I2C bus, but you
204don't know the exact address it uses. This happens on TV adapters for
205example, where the same driver supports dozens of slightly different
206models, and I2C device addresses change from one model to the next. In
207that case, you can use the i2c_new_probed_device() variant, which is
208similar to i2c_new_device(), except that it takes an additional list of
209possible I2C addresses to probe. A device is created for the first
210responsive address in the list. If you expect more than one device to be
211present in the address range, simply call i2c_new_probed_device() that
212many times.
213
214The call to i2c_new_device() or i2c_new_probed_device() typically happens
215in the I2C bus driver. You may want to save the returned i2c_client
216reference for later use.
217
218
Jean Delvaree3133532008-10-22 20:21:31 +0200219Device Detection
220----------------
Jean Delvare4735c982008-07-14 22:38:36 +0200221
222Sometimes you do not know in advance which I2C devices are connected to
223a given I2C bus. This is for example the case of hardware monitoring
224devices on a PC's SMBus. In that case, you may want to let your driver
225detect supported devices automatically. This is how the legacy model
226was working, and is now available as an extension to the standard
227driver model (so that we can finally get rid of the legacy model.)
228
229You simply have to define a detect callback which will attempt to
230identify supported devices (returning 0 for supported ones and -ENODEV
231for unsupported ones), a list of addresses to probe, and a device type
232(or class) so that only I2C buses which may have that type of device
233connected (and not otherwise enumerated) will be probed. The i2c
234core will then call you back as needed and will instantiate a device
235for you for every successful detection.
236
237Note that this mechanism is purely optional and not suitable for all
238devices. You need some reliable way to identify the supported devices
239(typically using device-specific, dedicated identification registers),
240otherwise misdetections are likely to occur and things can get wrong
241quickly.
242
243
Jean Delvaree3133532008-10-22 20:21:31 +0200244Device Deletion
245---------------
Jean Delvarece9e0792007-05-01 23:26:32 +0200246
247Each I2C device which has been created using i2c_new_device() or
248i2c_new_probed_device() can be unregistered by calling
249i2c_unregister_device(). If you don't call it explicitly, it will be
250called automatically before the underlying I2C bus itself is removed, as a
251device can't survive its parent in the device driver model.
252
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254Initializing the module or kernel
255=================================
256
257When the kernel is booted, or when your foo driver module is inserted,
258you have to do some initializing. Fortunately, just attaching (registering)
259the driver module is usually enough.
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 static int __init foo_init(void)
262 {
263 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 if ((res = i2c_add_driver(&foo_driver))) {
266 printk("foo: Driver registration failed, module not inserted.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return res;
268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return 0;
270 }
271
Jean Delvareeefcd752007-05-01 23:26:35 +0200272 static void __exit foo_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 {
Jean Delvareeefcd752007-05-01 23:26:35 +0200274 i2c_del_driver(&foo_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 }
276
277 /* Substitute your own name and email address */
278 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"
279 MODULE_DESCRIPTION("Driver for Barf Inc. Foo I2C devices");
280
Jean Delvareeefcd752007-05-01 23:26:35 +0200281 /* a few non-GPL license types are also allowed */
282 MODULE_LICENSE("GPL");
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 module_init(foo_init);
285 module_exit(foo_cleanup);
286
287Note that some functions are marked by `__init', and some data structures
Jean Delvareeefcd752007-05-01 23:26:35 +0200288by `__initdata'. These functions and structures can be removed after
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289kernel booting (or module loading) is completed.
290
Jean Delvarefb687d72005-12-18 16:51:55 +0100291
David Brownellf37dd802007-02-13 22:09:00 +0100292Power Management
293================
294
295If your I2C device needs special handling when entering a system low
296power state -- like putting a transceiver into a low power mode, or
297activating a system wakeup mechanism -- do that in the suspend() method.
298The resume() method should reverse what the suspend() method does.
299
300These are standard driver model calls, and they work just like they
301would for any other driver stack. The calls can sleep, and can use
302I2C messaging to the device being suspended or resumed (since their
303parent I2C adapter is active when these calls are issued, and IRQs
304are still enabled).
305
306
307System Shutdown
308===============
309
310If your I2C device needs special handling when the system shuts down
311or reboots (including kexec) -- like turning something off -- use a
312shutdown() method.
313
314Again, this is a standard driver model call, working just like it
315would for any other driver stack: the calls can sleep, and can use
316I2C messaging.
317
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319Command function
320================
321
322A generic ioctl-like function call back is supported. You will seldom
Jean Delvarefb687d72005-12-18 16:51:55 +0100323need this, and its use is deprecated anyway, so newer design should not
324use it. Set it to NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326
327Sending and receiving
328=====================
329
330If you want to communicate with your device, there are several functions
331to do this. You can find all of them in i2c.h.
332
333If you can choose between plain i2c communication and SMBus level
334communication, please use the last. All adapters understand SMBus level
335commands, but only some of them understand plain i2c!
336
337
338Plain i2c communication
339-----------------------
340
341 extern int i2c_master_send(struct i2c_client *,const char* ,int);
342 extern int i2c_master_recv(struct i2c_client *,char* ,int);
343
344These routines read and write some bytes from/to a client. The client
345contains the i2c address, so you do not have to include it. The second
346parameter contains the bytes the read/write, the third the length of the
347buffer. Returned is the actual number of bytes read/written.
348
349 extern int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msg,
350 int num);
351
352This sends a series of messages. Each message can be a read or write,
353and they can be mixed in any way. The transactions are combined: no
354stop bit is sent between transaction. The i2c_msg structure contains
355for each message the client address, the number of bytes of the message
356and the message data itself.
357
358You can read the file `i2c-protocol' for more information about the
359actual i2c protocol.
360
361
362SMBus communication
363-------------------
364
365 extern s32 i2c_smbus_xfer (struct i2c_adapter * adapter, u16 addr,
366 unsigned short flags,
367 char read_write, u8 command, int size,
368 union i2c_smbus_data * data);
369
370 This is the generic SMBus function. All functions below are implemented
371 in terms of it. Never use this function directly!
372
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 extern s32 i2c_smbus_read_byte(struct i2c_client * client);
375 extern s32 i2c_smbus_write_byte(struct i2c_client * client, u8 value);
376 extern s32 i2c_smbus_read_byte_data(struct i2c_client * client, u8 command);
377 extern s32 i2c_smbus_write_byte_data(struct i2c_client * client,
378 u8 command, u8 value);
379 extern s32 i2c_smbus_read_word_data(struct i2c_client * client, u8 command);
380 extern s32 i2c_smbus_write_word_data(struct i2c_client * client,
381 u8 command, u16 value);
Prakash Mortha596c88f2008-10-14 17:30:06 +0200382 extern s32 i2c_smbus_process_call(struct i2c_client *client,
383 u8 command, u16 value);
Jean Delvare67c2e662008-07-14 22:38:23 +0200384 extern s32 i2c_smbus_read_block_data(struct i2c_client * client,
385 u8 command, u8 *values);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 extern s32 i2c_smbus_write_block_data(struct i2c_client * client,
387 u8 command, u8 length,
388 u8 *values);
Jean Delvare7865e242005-10-08 00:00:31 +0200389 extern s32 i2c_smbus_read_i2c_block_data(struct i2c_client * client,
Jean Delvare4b2643d2007-07-12 14:12:29 +0200390 u8 command, u8 length, u8 *values);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 extern s32 i2c_smbus_write_i2c_block_data(struct i2c_client * client,
392 u8 command, u8 length,
393 u8 *values);
Jean Delvare67c2e662008-07-14 22:38:23 +0200394
395These ones were removed from i2c-core because they had no users, but could
396be added back later if needed:
397
398 extern s32 i2c_smbus_write_quick(struct i2c_client * client, u8 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 extern s32 i2c_smbus_block_process_call(struct i2c_client *client,
400 u8 command, u8 length,
401 u8 *values)
402
David Brownell24a5bb72008-07-14 22:38:23 +0200403All these transactions return a negative errno value on failure. The 'write'
404transactions return 0 on success; the 'read' transactions return the read
405value, except for block transactions, which return the number of values
406read. The block buffers need not be longer than 32 bytes.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408You can read the file `smbus-protocol' for more information about the
409actual SMBus protocol.
410
411
412General purpose routines
413========================
414
415Below all general purpose routines are listed, that were not mentioned
416before.
417
Jean Delvareeefcd752007-05-01 23:26:35 +0200418 /* This call returns a unique low identifier for each registered adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 */
420 extern int i2c_adapter_id(struct i2c_adapter *adap);
421