aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Documentation/i2c/writing-clients226
1 files changed, 97 insertions, 129 deletions
diff --git a/Documentation/i2c/writing-clients b/Documentation/i2c/writing-clients
index c3e188577687..6b9af7d479c2 100644
--- a/Documentation/i2c/writing-clients
+++ b/Documentation/i2c/writing-clients
@@ -10,17 +10,17 @@ General remarks
10=============== 10===============
11 11
12Try to keep the kernel namespace as clean as possible. The best way to 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 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 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 15it for non-exported symbols too. We will use the prefix `foo_' in this
16tutorial, and `FOO_' for preprocessor variables. 16tutorial.
17 17
18 18
19The driver structure 19The driver structure
20==================== 20====================
21 21
22Usually, you will implement a single driver structure, and instantiate 22Usually, you will implement a single driver structure, and instantiate
23all clients from it. Remember, a driver structure contains general access 23all clients from it. Remember, a driver structure contains general access
24routines, and should be zero-initialized except for fields with data you 24routines, and should be zero-initialized except for fields with data you
25provide. A client structure holds device-specific information like the 25provide. A client structure holds device-specific information like the
26driver model device node, and its I2C address. 26driver model device node, and its I2C address.
@@ -49,16 +49,16 @@ static struct i2c_driver foo_driver = {
49 .shutdown = foo_shutdown, /* optional */ 49 .shutdown = foo_shutdown, /* optional */
50 .suspend = foo_suspend, /* optional */ 50 .suspend = foo_suspend, /* optional */
51 .resume = foo_resume, /* optional */ 51 .resume = foo_resume, /* optional */
52 .command = foo_command, /* optional */ 52 .command = foo_command, /* optional, deprecated */
53} 53}
54 54
55The name field is the driver name, and must not contain spaces. It 55The 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), 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 57although you can use MODULE_ALIAS (passing "foo" in this example) to add
58another name for the module. If the driver name doesn't match the module 58another name for the module. If the driver name doesn't match the module
59name, the module won't be automatically loaded (hotplug/coldplug). 59name, the module won't be automatically loaded (hotplug/coldplug).
60 60
61All other fields are for call-back functions which will be explained 61All other fields are for call-back functions which will be explained
62below. 62below.
63 63
64 64
@@ -66,10 +66,7 @@ Extra client data
66================= 66=================
67 67
68Each client structure has a special `data' field that can point to any 68Each client structure has a special `data' field that can point to any
69structure at all. You should use this to keep device-specific data, 69structure at all. You should use this to keep device-specific data.
70especially in drivers that handle multiple I2C or SMBUS devices. You
71do not always need this, but especially for `sensors' drivers, it can
72be very useful.
73 70
74 /* store the value */ 71 /* store the value */
75 void i2c_set_clientdata(struct i2c_client *client, void *data); 72 void i2c_set_clientdata(struct i2c_client *client, void *data);
@@ -77,35 +74,15 @@ be very useful.
77 /* retrieve the value */ 74 /* retrieve the value */
78 void *i2c_get_clientdata(const struct i2c_client *client); 75 void *i2c_get_clientdata(const struct i2c_client *client);
79 76
80An example structure is below.
81
82 struct foo_data {
83 struct i2c_client *client;
84 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. */
90 struct mutex update_lock; /* When we are reading lots of information,
91 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 77
99Accessing the client 78Accessing the client
100==================== 79====================
101 80
102Let's say we have a valid client structure. At some time, we will need 81Let'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 82to gather information from the client, or write new information to the
104client. How we will export this information to user-space is less 83client.
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 84
108I have found it useful to define foo_read and foo_write function for this. 85I have found it useful to define foo_read and foo_write functions for this.
109For some cases, it will be easier to call the i2c functions directly, 86For 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 87but many chips have some kind of register-value idea that can easily
111be encapsulated. 88be encapsulated.
@@ -113,23 +90,23 @@ be encapsulated.
113The below functions are simple examples, and should not be copied 90The below functions are simple examples, and should not be copied
114literally. 91literally.
115 92
116 int foo_read_value(struct i2c_client *client, u8 reg) 93int foo_read_value(struct i2c_client *client, u8 reg)
117 { 94{
118 if (reg < 0x10) /* byte-sized register */ 95 if (reg < 0x10) /* byte-sized register */
119 return i2c_smbus_read_byte_data(client,reg); 96 return i2c_smbus_read_byte_data(client, reg);
120 else /* word-sized register */ 97 else /* word-sized register */
121 return i2c_smbus_read_word_data(client,reg); 98 return i2c_smbus_read_word_data(client, reg);
122 } 99}
123 100
124 int foo_write_value(struct i2c_client *client, u8 reg, u16 value) 101int foo_write_value(struct i2c_client *client, u8 reg, u16 value)
125 { 102{
126 if (reg == 0x10) /* Impossible to write - driver error! */ { 103 if (reg == 0x10) /* Impossible to write - driver error! */
127 return -1; 104 return -EINVAL;
128 else if (reg < 0x10) /* byte-sized register */ 105 else if (reg < 0x10) /* byte-sized register */
129 return i2c_smbus_write_byte_data(client,reg,value); 106 return i2c_smbus_write_byte_data(client, reg, value);
130 else /* word-sized register */ 107 else /* word-sized register */
131 return i2c_smbus_write_word_data(client,reg,value); 108 return i2c_smbus_write_word_data(client, reg, value);
132 } 109}
133 110
134 111
135Probing and attaching 112Probing and attaching
@@ -251,42 +228,37 @@ called automatically before the underlying I2C bus itself is removed, as a
251device can't survive its parent in the device driver model. 228device can't survive its parent in the device driver model.
252 229
253 230
254Initializing the module or kernel 231Initializing the driver
255================================= 232=======================
256 233
257When the kernel is booted, or when your foo driver module is inserted, 234When the kernel is booted, or when your foo driver module is inserted,
258you have to do some initializing. Fortunately, just attaching (registering) 235you have to do some initializing. Fortunately, just registering the
259the driver module is usually enough. 236driver module is usually enough.
260 237
261 static int __init foo_init(void) 238static int __init foo_init(void)
262 { 239{
263 int res; 240 return i2c_add_driver(&foo_driver);
264 241}
265 if ((res = i2c_add_driver(&foo_driver))) {
266 printk("foo: Driver registration failed, module not inserted.\n");
267 return res;
268 }
269 return 0;
270 }
271 242
272 static void __exit foo_cleanup(void) 243static void __exit foo_cleanup(void)
273 { 244{
274 i2c_del_driver(&foo_driver); 245 i2c_del_driver(&foo_driver);
275 } 246}
276 247
277 /* Substitute your own name and email address */ 248/* Substitute your own name and email address */
278 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>" 249MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"
279 MODULE_DESCRIPTION("Driver for Barf Inc. Foo I2C devices"); 250MODULE_DESCRIPTION("Driver for Barf Inc. Foo I2C devices");
280 251
281 /* a few non-GPL license types are also allowed */ 252/* a few non-GPL license types are also allowed */
282 MODULE_LICENSE("GPL"); 253MODULE_LICENSE("GPL");
283 254
284 module_init(foo_init); 255module_init(foo_init);
285 module_exit(foo_cleanup); 256module_exit(foo_cleanup);
286 257
287Note that some functions are marked by `__init', and some data structures 258Note that some functions are marked by `__init'. These functions can
288by `__initdata'. These functions and structures can be removed after 259be removed after kernel booting (or module loading) is completed.
289kernel booting (or module loading) is completed. 260Likewise, functions marked by `__exit' are dropped by the compiler when
261the code is built into the kernel, as they would never be called.
290 262
291 263
292Power Management 264Power Management
@@ -321,33 +293,35 @@ Command function
321 293
322A generic ioctl-like function call back is supported. You will seldom 294A generic ioctl-like function call back is supported. You will seldom
323need this, and its use is deprecated anyway, so newer design should not 295need this, and its use is deprecated anyway, so newer design should not
324use it. Set it to NULL. 296use it.
325 297
326 298
327Sending and receiving 299Sending and receiving
328===================== 300=====================
329 301
330If you want to communicate with your device, there are several functions 302If you want to communicate with your device, there are several functions
331to do this. You can find all of them in i2c.h. 303to do this. You can find all of them in <linux/i2c.h>.
332 304
333If you can choose between plain i2c communication and SMBus level 305If you can choose between plain I2C communication and SMBus level
334communication, please use the last. All adapters understand SMBus level 306communication, please use the latter. All adapters understand SMBus level
335commands, but only some of them understand plain i2c! 307commands, but only some of them understand plain I2C!
336 308
337 309
338Plain i2c communication 310Plain I2C communication
339----------------------- 311-----------------------
340 312
341 extern int i2c_master_send(struct i2c_client *,const char* ,int); 313 int i2c_master_send(struct i2c_client *client, const char *buf,
342 extern int i2c_master_recv(struct i2c_client *,char* ,int); 314 int count);
315 int i2c_master_recv(struct i2c_client *client, char *buf, int count);
343 316
344These routines read and write some bytes from/to a client. The client 317These 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 318contains 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 319parameter contains the bytes to read/write, the third the number of bytes
347buffer. Returned is the actual number of bytes read/written. 320to read/write (must be less than the length of the buffer.) Returned is
348 321the actual number of bytes read/written.
349 extern int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msg, 322
350 int num); 323 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msg,
324 int num);
351 325
352This sends a series of messages. Each message can be a read or write, 326This 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 327and they can be mixed in any way. The transactions are combined: no
@@ -356,49 +330,45 @@ for each message the client address, the number of bytes of the message
356and the message data itself. 330and the message data itself.
357 331
358You can read the file `i2c-protocol' for more information about the 332You can read the file `i2c-protocol' for more information about the
359actual i2c protocol. 333actual I2C protocol.
360 334
361 335
362SMBus communication 336SMBus communication
363------------------- 337-------------------
364 338
365 extern s32 i2c_smbus_xfer (struct i2c_adapter * adapter, u16 addr, 339 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr,
366 unsigned short flags, 340 unsigned short flags, char read_write, u8 command,
367 char read_write, u8 command, int size, 341 int size, union i2c_smbus_data *data);
368 union i2c_smbus_data * data); 342
369 343This is the generic SMBus function. All functions below are implemented
370 This is the generic SMBus function. All functions below are implemented 344in terms of it. Never use this function directly!
371 in terms of it. Never use this function directly! 345
372 346 s32 i2c_smbus_read_byte(struct i2c_client *client);
373 347 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value);
374 extern s32 i2c_smbus_read_byte(struct i2c_client * client); 348 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command);
375 extern s32 i2c_smbus_write_byte(struct i2c_client * client, u8 value); 349 s32 i2c_smbus_write_byte_data(struct i2c_client *client,
376 extern s32 i2c_smbus_read_byte_data(struct i2c_client * client, u8 command); 350 u8 command, u8 value);
377 extern s32 i2c_smbus_write_byte_data(struct i2c_client * client, 351 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command);
378 u8 command, u8 value); 352 s32 i2c_smbus_write_word_data(struct i2c_client *client,
379 extern s32 i2c_smbus_read_word_data(struct i2c_client * client, u8 command); 353 u8 command, u16 value);
380 extern s32 i2c_smbus_write_word_data(struct i2c_client * client, 354 s32 i2c_smbus_process_call(struct i2c_client *client,
381 u8 command, u16 value); 355 u8 command, u16 value);
382 extern s32 i2c_smbus_process_call(struct i2c_client *client, 356 s32 i2c_smbus_read_block_data(struct i2c_client *client,
383 u8 command, u16 value); 357 u8 command, u8 *values);
384 extern s32 i2c_smbus_read_block_data(struct i2c_client * client, 358 s32 i2c_smbus_write_block_data(struct i2c_client *client,
385 u8 command, u8 *values); 359 u8 command, u8 length, const u8 *values);
386 extern s32 i2c_smbus_write_block_data(struct i2c_client * client, 360 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client,
387 u8 command, u8 length, 361 u8 command, u8 length, u8 *values);
388 u8 *values); 362 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client,
389 extern s32 i2c_smbus_read_i2c_block_data(struct i2c_client * client, 363 u8 command, u8 length,
390 u8 command, u8 length, u8 *values); 364 const u8 *values);
391 extern s32 i2c_smbus_write_i2c_block_data(struct i2c_client * client,
392 u8 command, u8 length,
393 u8 *values);
394 365
395These ones were removed from i2c-core because they had no users, but could 366These ones were removed from i2c-core because they had no users, but could
396be added back later if needed: 367be added back later if needed:
397 368
398 extern s32 i2c_smbus_write_quick(struct i2c_client * client, u8 value); 369 s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value);
399 extern s32 i2c_smbus_block_process_call(struct i2c_client *client, 370 s32 i2c_smbus_block_process_call(struct i2c_client *client,
400 u8 command, u8 length, 371 u8 command, u8 length, u8 *values);
401 u8 *values)
402 372
403All these transactions return a negative errno value on failure. The 'write' 373All these transactions return a negative errno value on failure. The 'write'
404transactions return 0 on success; the 'read' transactions return the read 374transactions return 0 on success; the 'read' transactions return the read
@@ -415,7 +385,5 @@ General purpose routines
415Below all general purpose routines are listed, that were not mentioned 385Below all general purpose routines are listed, that were not mentioned
416before. 386before.
417 387
418 /* This call returns a unique low identifier for each registered adapter. 388 /* Return the adapter number for a specific adapter */
419 */ 389 int i2c_adapter_id(struct i2c_adapter *adap);
420 extern int i2c_adapter_id(struct i2c_adapter *adap);
421