diff options
author | Jean Delvare <khali@linux-fr.org> | 2008-10-22 14:21:32 -0400 |
---|---|---|
committer | Jean Delvare <khali@linux-fr.org> | 2008-10-22 14:21:32 -0400 |
commit | 0e47858da4b6825e1f1aede74742c7dd6d4ee476 (patch) | |
tree | 963c72d89fe4c9c83138b410848f285f5a20fd5b /Documentation/i2c/writing-clients | |
parent | c0589d4bc19294a49934af1be736eb6e9ad11673 (diff) |
i2c: Update and clean up writing-clients document
* Strip trailing white space.
* Remove out-of-date or irrelevant parts.
* Insist on the fact that command is deprecated.
* Fix spelling mistakes and typos.
* Reformat code examples and function prototypes to comply with the
kernel coding style.
Signed-off-by: Jean Delvare <khali@linux-fr.org>
Diffstat (limited to 'Documentation/i2c/writing-clients')
-rw-r--r-- | Documentation/i2c/writing-clients | 226 |
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 | ||
12 | Try to keep the kernel namespace as clean as possible. The best way to | 12 | Try to keep the kernel namespace as clean as possible. The best way to |
13 | do this is to use a unique prefix for all global symbols. This is | 13 | do this is to use a unique prefix for all global symbols. This is |
14 | especially important for exported symbols, but it is a good idea to do | 14 | especially important for exported symbols, but it is a good idea to do |
15 | it for non-exported symbols too. We will use the prefix `foo_' in this | 15 | it for non-exported symbols too. We will use the prefix `foo_' in this |
16 | tutorial, and `FOO_' for preprocessor variables. | 16 | tutorial. |
17 | 17 | ||
18 | 18 | ||
19 | The driver structure | 19 | The driver structure |
20 | ==================== | 20 | ==================== |
21 | 21 | ||
22 | Usually, you will implement a single driver structure, and instantiate | 22 | Usually, you will implement a single driver structure, and instantiate |
23 | all clients from it. Remember, a driver structure contains general access | 23 | all clients from it. Remember, a driver structure contains general access |
24 | routines, and should be zero-initialized except for fields with data you | 24 | routines, and should be zero-initialized except for fields with data you |
25 | provide. A client structure holds device-specific information like the | 25 | provide. A client structure holds device-specific information like the |
26 | driver model device node, and its I2C address. | 26 | driver 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 | ||
55 | The name field is the driver name, and must not contain spaces. It | 55 | The name field is the driver name, and must not contain spaces. It |
56 | should match the module name (if the driver can be compiled as a module), | 56 | should match the module name (if the driver can be compiled as a module), |
57 | although you can use MODULE_ALIAS (passing "foo" in this example) to add | 57 | although you can use MODULE_ALIAS (passing "foo" in this example) to add |
58 | another name for the module. If the driver name doesn't match the module | 58 | another name for the module. If the driver name doesn't match the module |
59 | name, the module won't be automatically loaded (hotplug/coldplug). | 59 | name, the module won't be automatically loaded (hotplug/coldplug). |
60 | 60 | ||
61 | All other fields are for call-back functions which will be explained | 61 | All other fields are for call-back functions which will be explained |
62 | below. | 62 | below. |
63 | 63 | ||
64 | 64 | ||
@@ -66,10 +66,7 @@ Extra client data | |||
66 | ================= | 66 | ================= |
67 | 67 | ||
68 | Each client structure has a special `data' field that can point to any | 68 | Each client structure has a special `data' field that can point to any |
69 | structure at all. You should use this to keep device-specific data, | 69 | structure at all. You should use this to keep device-specific data. |
70 | especially in drivers that handle multiple I2C or SMBUS devices. You | ||
71 | do not always need this, but especially for `sensors' drivers, it can | ||
72 | be 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 | ||
80 | An 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 | ||
99 | Accessing the client | 78 | Accessing the client |
100 | ==================== | 79 | ==================== |
101 | 80 | ||
102 | Let's say we have a valid client structure. At some time, we will need | 81 | Let's say we have a valid client structure. At some time, we will need |
103 | to gather information from the client, or write new information to the | 82 | to gather information from the client, or write new information to the |
104 | client. How we will export this information to user-space is less | 83 | client. |
105 | important at this moment (perhaps we do not need to do this at all for | ||
106 | some obscure clients). But we need generic reading and writing routines. | ||
107 | 84 | ||
108 | I have found it useful to define foo_read and foo_write function for this. | 85 | I have found it useful to define foo_read and foo_write functions for this. |
109 | For some cases, it will be easier to call the i2c functions directly, | 86 | For some cases, it will be easier to call the i2c functions directly, |
110 | but many chips have some kind of register-value idea that can easily | 87 | but many chips have some kind of register-value idea that can easily |
111 | be encapsulated. | 88 | be encapsulated. |
@@ -113,23 +90,23 @@ be encapsulated. | |||
113 | The below functions are simple examples, and should not be copied | 90 | The below functions are simple examples, and should not be copied |
114 | literally. | 91 | literally. |
115 | 92 | ||
116 | int foo_read_value(struct i2c_client *client, u8 reg) | 93 | int 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) | 101 | int 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 | ||
135 | Probing and attaching | 112 | Probing and attaching |
@@ -251,42 +228,37 @@ called automatically before the underlying I2C bus itself is removed, as a | |||
251 | device can't survive its parent in the device driver model. | 228 | device can't survive its parent in the device driver model. |
252 | 229 | ||
253 | 230 | ||
254 | Initializing the module or kernel | 231 | Initializing the driver |
255 | ================================= | 232 | ======================= |
256 | 233 | ||
257 | When the kernel is booted, or when your foo driver module is inserted, | 234 | When the kernel is booted, or when your foo driver module is inserted, |
258 | you have to do some initializing. Fortunately, just attaching (registering) | 235 | you have to do some initializing. Fortunately, just registering the |
259 | the driver module is usually enough. | 236 | driver module is usually enough. |
260 | 237 | ||
261 | static int __init foo_init(void) | 238 | static 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) | 243 | static 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>" | 249 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>" |
279 | MODULE_DESCRIPTION("Driver for Barf Inc. Foo I2C devices"); | 250 | MODULE_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"); | 253 | MODULE_LICENSE("GPL"); |
283 | 254 | ||
284 | module_init(foo_init); | 255 | module_init(foo_init); |
285 | module_exit(foo_cleanup); | 256 | module_exit(foo_cleanup); |
286 | 257 | ||
287 | Note that some functions are marked by `__init', and some data structures | 258 | Note that some functions are marked by `__init'. These functions can |
288 | by `__initdata'. These functions and structures can be removed after | 259 | be removed after kernel booting (or module loading) is completed. |
289 | kernel booting (or module loading) is completed. | 260 | Likewise, functions marked by `__exit' are dropped by the compiler when |
261 | the code is built into the kernel, as they would never be called. | ||
290 | 262 | ||
291 | 263 | ||
292 | Power Management | 264 | Power Management |
@@ -321,33 +293,35 @@ Command function | |||
321 | 293 | ||
322 | A generic ioctl-like function call back is supported. You will seldom | 294 | A generic ioctl-like function call back is supported. You will seldom |
323 | need this, and its use is deprecated anyway, so newer design should not | 295 | need this, and its use is deprecated anyway, so newer design should not |
324 | use it. Set it to NULL. | 296 | use it. |
325 | 297 | ||
326 | 298 | ||
327 | Sending and receiving | 299 | Sending and receiving |
328 | ===================== | 300 | ===================== |
329 | 301 | ||
330 | If you want to communicate with your device, there are several functions | 302 | If you want to communicate with your device, there are several functions |
331 | to do this. You can find all of them in i2c.h. | 303 | to do this. You can find all of them in <linux/i2c.h>. |
332 | 304 | ||
333 | If you can choose between plain i2c communication and SMBus level | 305 | If you can choose between plain I2C communication and SMBus level |
334 | communication, please use the last. All adapters understand SMBus level | 306 | communication, please use the latter. All adapters understand SMBus level |
335 | commands, but only some of them understand plain i2c! | 307 | commands, but only some of them understand plain I2C! |
336 | 308 | ||
337 | 309 | ||
338 | Plain i2c communication | 310 | Plain 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 | ||
344 | These routines read and write some bytes from/to a client. The client | 317 | These routines read and write some bytes from/to a client. The client |
345 | contains the i2c address, so you do not have to include it. The second | 318 | contains the i2c address, so you do not have to include it. The second |
346 | parameter contains the bytes the read/write, the third the length of the | 319 | parameter contains the bytes to read/write, the third the number of bytes |
347 | buffer. Returned is the actual number of bytes read/written. | 320 | to read/write (must be less than the length of the buffer.) Returned is |
348 | 321 | the 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 | ||
352 | This sends a series of messages. Each message can be a read or write, | 326 | This sends a series of messages. Each message can be a read or write, |
353 | and they can be mixed in any way. The transactions are combined: no | 327 | and 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 | |||
356 | and the message data itself. | 330 | and the message data itself. |
357 | 331 | ||
358 | You can read the file `i2c-protocol' for more information about the | 332 | You can read the file `i2c-protocol' for more information about the |
359 | actual i2c protocol. | 333 | actual I2C protocol. |
360 | 334 | ||
361 | 335 | ||
362 | SMBus communication | 336 | SMBus 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 | 343 | This is the generic SMBus function. All functions below are implemented | |
370 | This is the generic SMBus function. All functions below are implemented | 344 | in 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 | ||
395 | These ones were removed from i2c-core because they had no users, but could | 366 | These ones were removed from i2c-core because they had no users, but could |
396 | be added back later if needed: | 367 | be 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 | ||
403 | All these transactions return a negative errno value on failure. The 'write' | 373 | All these transactions return a negative errno value on failure. The 'write' |
404 | transactions return 0 on success; the 'read' transactions return the read | 374 | transactions return 0 on success; the 'read' transactions return the read |
@@ -415,7 +385,5 @@ General purpose routines | |||
415 | Below all general purpose routines are listed, that were not mentioned | 385 | Below all general purpose routines are listed, that were not mentioned |
416 | before. | 386 | before. |
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 | |||