blob: 7e2a8f11c220299a8f6a101c1b1ac3255fbb49a0 [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
2or SMBus devices.
3
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
24routines, a client structure specific information like the actual I2C
25address.
26
27static struct i2c_driver foo_driver = {
28 .owner = THIS_MODULE,
29 .name = "Foo version 2.3 driver",
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 .flags = I2C_DF_NOTIFY,
31 .attach_adapter = &foo_attach_adapter,
32 .detach_client = &foo_detach_client,
33 .command = &foo_command /* may be NULL */
34}
35
36The name can be chosen freely, and may be upto 40 characters long. Please
37use something descriptive here.
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039Don't worry about the flags field; just put I2C_DF_NOTIFY into it. This
40means that your driver will be notified when new adapters are found.
41This is almost always what you want.
42
43All other fields are for call-back functions which will be explained
44below.
45
46There use to be two additional fields in this structure, inc_use et dec_use,
47for module usage count, but these fields were obsoleted and removed.
48
49
50Extra client data
51=================
52
53The client structure has a special `data' field that can point to any
54structure at all. You can use this to keep client-specific data. You
55do not always need this, but especially for `sensors' drivers, it can
56be very useful.
57
58An example structure is below.
59
60 struct foo_data {
61 struct semaphore lock; /* For ISA access in `sensors' drivers. */
62 int sysctl_id; /* To keep the /proc directory entry for
63 `sensors' drivers. */
64 enum chips type; /* To keep the chips type for `sensors' drivers. */
65
66 /* Because the i2c bus is slow, it is often useful to cache the read
67 information of a chip for some time (for example, 1 or 2 seconds).
68 It depends of course on the device whether this is really worthwhile
69 or even sensible. */
70 struct semaphore update_lock; /* When we are reading lots of information,
71 another process should not update the
72 below information */
73 char valid; /* != 0 if the following fields are valid. */
74 unsigned long last_updated; /* In jiffies */
75 /* Add the read information here too */
76 };
77
78
79Accessing the client
80====================
81
82Let's say we have a valid client structure. At some time, we will need
83to gather information from the client, or write new information to the
84client. How we will export this information to user-space is less
85important at this moment (perhaps we do not need to do this at all for
86some obscure clients). But we need generic reading and writing routines.
87
88I have found it useful to define foo_read and foo_write function for this.
89For some cases, it will be easier to call the i2c functions directly,
90but many chips have some kind of register-value idea that can easily
91be encapsulated. Also, some chips have both ISA and I2C interfaces, and
92it useful to abstract from this (only for `sensors' drivers).
93
94The below functions are simple examples, and should not be copied
95literally.
96
97 int foo_read_value(struct i2c_client *client, u8 reg)
98 {
99 if (reg < 0x10) /* byte-sized register */
100 return i2c_smbus_read_byte_data(client,reg);
101 else /* word-sized register */
102 return i2c_smbus_read_word_data(client,reg);
103 }
104
105 int foo_write_value(struct i2c_client *client, u8 reg, u16 value)
106 {
107 if (reg == 0x10) /* Impossible to write - driver error! */ {
108 return -1;
109 else if (reg < 0x10) /* byte-sized register */
110 return i2c_smbus_write_byte_data(client,reg,value);
111 else /* word-sized register */
112 return i2c_smbus_write_word_data(client,reg,value);
113 }
114
115For sensors code, you may have to cope with ISA registers too. Something
116like the below often works. Note the locking!
117
118 int foo_read_value(struct i2c_client *client, u8 reg)
119 {
120 int res;
121 if (i2c_is_isa_client(client)) {
122 down(&(((struct foo_data *) (client->data)) -> lock));
123 outb_p(reg,client->addr + FOO_ADDR_REG_OFFSET);
124 res = inb_p(client->addr + FOO_DATA_REG_OFFSET);
125 up(&(((struct foo_data *) (client->data)) -> lock));
126 return res;
127 } else
128 return i2c_smbus_read_byte_data(client,reg);
129 }
130
131Writing is done the same way.
132
133
134Probing and attaching
135=====================
136
137Most i2c devices can be present on several i2c addresses; for some this
138is determined in hardware (by soldering some chip pins to Vcc or Ground),
139for others this can be changed in software (by writing to specific client
140registers). Some devices are usually on a specific address, but not always;
141and some are even more tricky. So you will probably need to scan several
142i2c addresses for your clients, and do some sort of detection to see
143whether it is actually a device supported by your driver.
144
145To give the user a maximum of possibilities, some default module parameters
146are defined to help determine what addresses are scanned. Several macros
147are defined in i2c.h to help you support them, as well as a generic
148detection algorithm.
149
150You do not have to use this parameter interface; but don't try to use
151function i2c_probe() (or i2c_detect()) if you don't.
152
153NOTE: If you want to write a `sensors' driver, the interface is slightly
154 different! See below.
155
156
157
158Probing classes (i2c)
159---------------------
160
161All parameters are given as lists of unsigned 16-bit integers. Lists are
162terminated by I2C_CLIENT_END.
163The following lists are used internally:
164
165 normal_i2c: filled in by the module writer.
166 A list of I2C addresses which should normally be examined.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 probe: insmod parameter.
168 A list of pairs. The first value is a bus number (-1 for any I2C bus),
169 the second is the address. These addresses are also probed, as if they
170 were in the 'normal' list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 ignore: insmod parameter.
172 A list of pairs. The first value is a bus number (-1 for any I2C bus),
173 the second is the I2C address. These addresses are never probed.
174 This parameter overrules 'normal' and 'probe', but not the 'force' lists.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 force: insmod parameter.
176 A list of pairs. The first value is a bus number (-1 for any I2C bus),
177 the second is the I2C address. A device is blindly assumed to be on
178 the given address, no probing is done.
179
Jean Delvareb3d54962005-04-02 20:31:02 +0200180Fortunately, as a module writer, you just have to define the `normal_i2c'
181parameter. The complete declaration could look like this:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Jean Delvareb3d54962005-04-02 20:31:02 +0200183 /* Scan 0x37, and 0x48 to 0x4f */
184 static unsigned short normal_i2c[] = { 0x37, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
185 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 /* Magic definition of all other variables and things */
188 I2C_CLIENT_INSMOD;
189
Jean Delvareb3d54962005-04-02 20:31:02 +0200190Note that you *have* to call the defined variable `normal_i2c',
191without any prefix!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193
194Probing classes (sensors)
195-------------------------
196
197If you write a `sensors' driver, you use a slightly different interface.
Jean Delvare50718602005-07-20 00:02:32 +0200198Also, we use a enum of chip types. Don't forget to include `sensors.h'.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200The following lists are used internally. They are all lists of integers.
201
Jean Delvare50718602005-07-20 00:02:32 +0200202 normal_i2c: filled in by the module writer. Terminated by I2C_CLIENT_END.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 A list of I2C addresses which should normally be examined.
Jean Delvare50718602005-07-20 00:02:32 +0200204 probe: insmod parameter. Initialize this list with I2C_CLIENT_END values.
205 A list of pairs. The first value is a bus number (ANY_I2C_BUS for any
206 I2C bus), the second is the address. These addresses are also probed,
207 as if they were in the 'normal' list.
208 ignore: insmod parameter. Initialize this list with I2C_CLIENT_END values.
209 A list of pairs. The first value is a bus number (ANY_I2C_BUS for any
210 I2C bus), the second is the I2C address. These addresses are never
211 probed. This parameter overrules 'normal' and 'probe', but not the
212 'force' lists.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214Also used is a list of pointers to sensors_force_data structures:
215 force_data: insmod parameters. A list, ending with an element of which
216 the force field is NULL.
217 Each element contains the type of chip and a list of pairs.
Jean Delvare50718602005-07-20 00:02:32 +0200218 The first value is a bus number (ANY_I2C_BUS for any I2C bus), the
219 second is the address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 These are automatically translated to insmod variables of the form
221 force_foo.
222
223So we have a generic insmod variabled `force', and chip-specific variables
224`force_CHIPNAME'.
225
Jean Delvareb3d54962005-04-02 20:31:02 +0200226Fortunately, as a module writer, you just have to define the `normal_i2c'
Jean Delvare50718602005-07-20 00:02:32 +0200227parameter, and define what chip names are used. The complete declaration
228could look like this:
Jean Delvareb3d54962005-04-02 20:31:02 +0200229 /* Scan i2c addresses 0x37, and 0x48 to 0x4f */
230 static unsigned short normal_i2c[] = { 0x37, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
231 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 /* Define chips foo and bar, as well as all module parameters and things */
234 SENSORS_INSMOD_2(foo,bar);
235
236If you have one chip, you use macro SENSORS_INSMOD_1(chip), if you have 2
237you use macro SENSORS_INSMOD_2(chip1,chip2), etc. If you do not want to
238bother with chip types, you can use SENSORS_INSMOD_0.
239
240A enum is automatically defined as follows:
241 enum chips { any_chip, chip1, chip2, ... }
242
243
244Attaching to an adapter
245-----------------------
246
247Whenever a new adapter is inserted, or for all adapters if the driver is
248being registered, the callback attach_adapter() is called. Now is the
249time to determine what devices are present on the adapter, and to register
250a client for each of them.
251
252The attach_adapter callback is really easy: we just call the generic
253detection function. This function will scan the bus for us, using the
254information as defined in the lists explained above. If a device is
255detected at a specific address, another callback is called.
256
257 int foo_attach_adapter(struct i2c_adapter *adapter)
258 {
259 return i2c_probe(adapter,&addr_data,&foo_detect_client);
260 }
261
262For `sensors' drivers, use the i2c_detect function instead:
263
264 int foo_attach_adapter(struct i2c_adapter *adapter)
265 {
266 return i2c_detect(adapter,&addr_data,&foo_detect_client);
267 }
268
269Remember, structure `addr_data' is defined by the macros explained above,
270so you do not have to define it yourself.
271
272The i2c_probe or i2c_detect function will call the foo_detect_client
273function only for those i2c addresses that actually have a device on
274them (unless a `force' parameter was used). In addition, addresses that
275are already in use (by some other registered client) are skipped.
276
277
278The detect client function
279--------------------------
280
281The detect client function is called by i2c_probe or i2c_detect.
282The `kind' parameter contains 0 if this call is due to a `force'
283parameter, and -1 otherwise (for i2c_detect, it contains 0 if
284this call is due to the generic `force' parameter, and the chip type
285number if it is due to a specific `force' parameter).
286
287Below, some things are only needed if this is a `sensors' driver. Those
288parts are between /* SENSORS ONLY START */ and /* SENSORS ONLY END */
289markers.
290
291This function should only return an error (any value != 0) if there is
292some reason why no more detection should be done anymore. If the
293detection just fails for this address, return 0.
294
295For now, you can ignore the `flags' parameter. It is there for future use.
296
297 int foo_detect_client(struct i2c_adapter *adapter, int address,
298 unsigned short flags, int kind)
299 {
300 int err = 0;
301 int i;
302 struct i2c_client *new_client;
303 struct foo_data *data;
304 const char *client_name = ""; /* For non-`sensors' drivers, put the real
305 name here! */
306
307 /* Let's see whether this adapter can support what we need.
308 Please substitute the things you need here!
309 For `sensors' drivers, add `! is_isa &&' to the if statement */
310 if (!i2c_check_functionality(adapter,I2C_FUNC_SMBUS_WORD_DATA |
311 I2C_FUNC_SMBUS_WRITE_BYTE))
312 goto ERROR0;
313
314 /* SENSORS ONLY START */
315 const char *type_name = "";
316 int is_isa = i2c_is_isa_adapter(adapter);
317
Jean Delvare02ff9822005-07-20 00:05:33 +0200318 /* Do this only if the chip can additionally be found on the ISA bus
319 (hybrid chip). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Jean Delvare02ff9822005-07-20 00:05:33 +0200321 if (is_isa) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 /* Discard immediately if this ISA range is already used */
324 if (check_region(address,FOO_EXTENT))
325 goto ERROR0;
326
327 /* Probe whether there is anything on this address.
328 Some example code is below, but you will have to adapt this
329 for your own driver */
330
331 if (kind < 0) /* Only if no force parameter was used */ {
332 /* We may need long timeouts at least for some chips. */
333 #define REALLY_SLOW_IO
334 i = inb_p(address + 1);
335 if (inb_p(address + 2) != i)
336 goto ERROR0;
337 if (inb_p(address + 3) != i)
338 goto ERROR0;
339 if (inb_p(address + 7) != i)
340 goto ERROR0;
341 #undef REALLY_SLOW_IO
342
343 /* Let's just hope nothing breaks here */
344 i = inb_p(address + 5) & 0x7f;
345 outb_p(~i & 0x7f,address+5);
346 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
347 outb_p(i,address+5);
348 return 0;
349 }
350 }
351 }
352
353 /* SENSORS ONLY END */
354
355 /* OK. For now, we presume we have a valid client. We now create the
356 client structure, even though we cannot fill it completely yet.
357 But it allows us to access several i2c functions safely */
358
359 /* Note that we reserve some space for foo_data too. If you don't
360 need it, remove it. We do it here to help to lessen memory
361 fragmentation. */
362 if (! (new_client = kmalloc(sizeof(struct i2c_client) +
363 sizeof(struct foo_data),
364 GFP_KERNEL))) {
365 err = -ENOMEM;
366 goto ERROR0;
367 }
368
369 /* This is tricky, but it will set the data to the right value. */
370 client->data = new_client + 1;
371 data = (struct foo_data *) (client->data);
372
373 new_client->addr = address;
374 new_client->data = data;
375 new_client->adapter = adapter;
376 new_client->driver = &foo_driver;
377 new_client->flags = 0;
378
379 /* Now, we do the remaining detection. If no `force' parameter is used. */
380
381 /* First, the generic detection (if any), that is skipped if any force
382 parameter was used. */
383 if (kind < 0) {
384 /* The below is of course bogus */
385 if (foo_read(new_client,FOO_REG_GENERIC) != FOO_GENERIC_VALUE)
386 goto ERROR1;
387 }
388
389 /* SENSORS ONLY START */
390
391 /* Next, specific detection. This is especially important for `sensors'
392 devices. */
393
394 /* Determine the chip type. Not needed if a `force_CHIPTYPE' parameter
395 was used. */
396 if (kind <= 0) {
397 i = foo_read(new_client,FOO_REG_CHIPTYPE);
398 if (i == FOO_TYPE_1)
399 kind = chip1; /* As defined in the enum */
400 else if (i == FOO_TYPE_2)
401 kind = chip2;
402 else {
403 printk("foo: Ignoring 'force' parameter for unknown chip at "
404 "adapter %d, address 0x%02x\n",i2c_adapter_id(adapter),address);
405 goto ERROR1;
406 }
407 }
408
409 /* Now set the type and chip names */
410 if (kind == chip1) {
411 type_name = "chip1"; /* For /proc entry */
412 client_name = "CHIP 1";
413 } else if (kind == chip2) {
414 type_name = "chip2"; /* For /proc entry */
415 client_name = "CHIP 2";
416 }
417
418 /* Reserve the ISA region */
419 if (is_isa)
420 request_region(address,FOO_EXTENT,type_name);
421
422 /* SENSORS ONLY END */
423
424 /* Fill in the remaining client fields. */
425 strcpy(new_client->name,client_name);
426
427 /* SENSORS ONLY BEGIN */
428 data->type = kind;
429 /* SENSORS ONLY END */
430
431 data->valid = 0; /* Only if you use this field */
432 init_MUTEX(&data->update_lock); /* Only if you use this field */
433
434 /* Any other initializations in data must be done here too. */
435
436 /* Tell the i2c layer a new client has arrived */
437 if ((err = i2c_attach_client(new_client)))
438 goto ERROR3;
439
440 /* SENSORS ONLY BEGIN */
441 /* Register a new directory entry with module sensors. See below for
442 the `template' structure. */
443 if ((i = i2c_register_entry(new_client, type_name,
444 foo_dir_table_template,THIS_MODULE)) < 0) {
445 err = i;
446 goto ERROR4;
447 }
448 data->sysctl_id = i;
449
450 /* SENSORS ONLY END */
451
452 /* This function can write default values to the client registers, if
453 needed. */
454 foo_init_client(new_client);
455 return 0;
456
457 /* OK, this is not exactly good programming practice, usually. But it is
458 very code-efficient in this case. */
459
460 ERROR4:
461 i2c_detach_client(new_client);
462 ERROR3:
463 ERROR2:
464 /* SENSORS ONLY START */
465 if (is_isa)
466 release_region(address,FOO_EXTENT);
467 /* SENSORS ONLY END */
468 ERROR1:
469 kfree(new_client);
470 ERROR0:
471 return err;
472 }
473
474
475Removing the client
476===================
477
478The detach_client call back function is called when a client should be
479removed. It may actually fail, but only when panicking. This code is
480much simpler than the attachment code, fortunately!
481
482 int foo_detach_client(struct i2c_client *client)
483 {
484 int err,i;
485
486 /* SENSORS ONLY START */
487 /* Deregister with the `i2c-proc' module. */
488 i2c_deregister_entry(((struct lm78_data *)(client->data))->sysctl_id);
489 /* SENSORS ONLY END */
490
491 /* Try to detach the client from i2c space */
Jean Delvare7bef5592005-07-27 22:14:49 +0200492 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Jean Delvare02ff9822005-07-20 00:05:33 +0200495 /* HYBRID SENSORS CHIP ONLY START */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 if i2c_is_isa_client(client)
497 release_region(client->addr,LM78_EXTENT);
Jean Delvare02ff9822005-07-20 00:05:33 +0200498 /* HYBRID SENSORS CHIP ONLY END */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
500 kfree(client); /* Frees client data too, if allocated at the same time */
501 return 0;
502 }
503
504
505Initializing the module or kernel
506=================================
507
508When the kernel is booted, or when your foo driver module is inserted,
509you have to do some initializing. Fortunately, just attaching (registering)
510the driver module is usually enough.
511
512 /* Keep track of how far we got in the initialization process. If several
513 things have to initialized, and we fail halfway, only those things
514 have to be cleaned up! */
515 static int __initdata foo_initialized = 0;
516
517 static int __init foo_init(void)
518 {
519 int res;
520 printk("foo version %s (%s)\n",FOO_VERSION,FOO_DATE);
521
522 if ((res = i2c_add_driver(&foo_driver))) {
523 printk("foo: Driver registration failed, module not inserted.\n");
524 foo_cleanup();
525 return res;
526 }
527 foo_initialized ++;
528 return 0;
529 }
530
531 void foo_cleanup(void)
532 {
533 if (foo_initialized == 1) {
534 if ((res = i2c_del_driver(&foo_driver))) {
535 printk("foo: Driver registration failed, module not removed.\n");
536 return;
537 }
538 foo_initialized --;
539 }
540 }
541
542 /* Substitute your own name and email address */
543 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"
544 MODULE_DESCRIPTION("Driver for Barf Inc. Foo I2C devices");
545
546 module_init(foo_init);
547 module_exit(foo_cleanup);
548
549Note that some functions are marked by `__init', and some data structures
550by `__init_data'. Hose functions and structures can be removed after
551kernel booting (or module loading) is completed.
552
553Command function
554================
555
556A generic ioctl-like function call back is supported. You will seldom
557need this. You may even set it to NULL.
558
559 /* No commands defined */
560 int foo_command(struct i2c_client *client, unsigned int cmd, void *arg)
561 {
562 return 0;
563 }
564
565
566Sending and receiving
567=====================
568
569If you want to communicate with your device, there are several functions
570to do this. You can find all of them in i2c.h.
571
572If you can choose between plain i2c communication and SMBus level
573communication, please use the last. All adapters understand SMBus level
574commands, but only some of them understand plain i2c!
575
576
577Plain i2c communication
578-----------------------
579
580 extern int i2c_master_send(struct i2c_client *,const char* ,int);
581 extern int i2c_master_recv(struct i2c_client *,char* ,int);
582
583These routines read and write some bytes from/to a client. The client
584contains the i2c address, so you do not have to include it. The second
585parameter contains the bytes the read/write, the third the length of the
586buffer. Returned is the actual number of bytes read/written.
587
588 extern int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msg,
589 int num);
590
591This sends a series of messages. Each message can be a read or write,
592and they can be mixed in any way. The transactions are combined: no
593stop bit is sent between transaction. The i2c_msg structure contains
594for each message the client address, the number of bytes of the message
595and the message data itself.
596
597You can read the file `i2c-protocol' for more information about the
598actual i2c protocol.
599
600
601SMBus communication
602-------------------
603
604 extern s32 i2c_smbus_xfer (struct i2c_adapter * adapter, u16 addr,
605 unsigned short flags,
606 char read_write, u8 command, int size,
607 union i2c_smbus_data * data);
608
609 This is the generic SMBus function. All functions below are implemented
610 in terms of it. Never use this function directly!
611
612
613 extern s32 i2c_smbus_write_quick(struct i2c_client * client, u8 value);
614 extern s32 i2c_smbus_read_byte(struct i2c_client * client);
615 extern s32 i2c_smbus_write_byte(struct i2c_client * client, u8 value);
616 extern s32 i2c_smbus_read_byte_data(struct i2c_client * client, u8 command);
617 extern s32 i2c_smbus_write_byte_data(struct i2c_client * client,
618 u8 command, u8 value);
619 extern s32 i2c_smbus_read_word_data(struct i2c_client * client, u8 command);
620 extern s32 i2c_smbus_write_word_data(struct i2c_client * client,
621 u8 command, u16 value);
622 extern s32 i2c_smbus_write_block_data(struct i2c_client * client,
623 u8 command, u8 length,
624 u8 *values);
625
626These ones were removed in Linux 2.6.10 because they had no users, but could
627be added back later if needed:
628
629 extern s32 i2c_smbus_read_i2c_block_data(struct i2c_client * client,
630 u8 command, u8 *values);
631 extern s32 i2c_smbus_read_block_data(struct i2c_client * client,
632 u8 command, u8 *values);
633 extern s32 i2c_smbus_write_i2c_block_data(struct i2c_client * client,
634 u8 command, u8 length,
635 u8 *values);
636 extern s32 i2c_smbus_process_call(struct i2c_client * client,
637 u8 command, u16 value);
638 extern s32 i2c_smbus_block_process_call(struct i2c_client *client,
639 u8 command, u8 length,
640 u8 *values)
641
642All these transactions return -1 on failure. The 'write' transactions
643return 0 on success; the 'read' transactions return the read value, except
644for read_block, which returns the number of values read. The block buffers
645need not be longer than 32 bytes.
646
647You can read the file `smbus-protocol' for more information about the
648actual SMBus protocol.
649
650
651General purpose routines
652========================
653
654Below all general purpose routines are listed, that were not mentioned
655before.
656
657 /* This call returns a unique low identifier for each registered adapter,
658 * or -1 if the adapter was not registered.
659 */
660 extern int i2c_adapter_id(struct i2c_adapter *adap);
661
662
663The sensors sysctl/proc interface
664=================================
665
666This section only applies if you write `sensors' drivers.
667
668Each sensors driver creates a directory in /proc/sys/dev/sensors for each
669registered client. The directory is called something like foo-i2c-4-65.
670The sensors module helps you to do this as easily as possible.
671
672The template
673------------
674
675You will need to define a ctl_table template. This template will automatically
676be copied to a newly allocated structure and filled in where necessary when
677you call sensors_register_entry.
678
679First, I will give an example definition.
680 static ctl_table foo_dir_table_template[] = {
681 { FOO_SYSCTL_FUNC1, "func1", NULL, 0, 0644, NULL, &i2c_proc_real,
682 &i2c_sysctl_real,NULL,&foo_func },
683 { FOO_SYSCTL_FUNC2, "func2", NULL, 0, 0644, NULL, &i2c_proc_real,
684 &i2c_sysctl_real,NULL,&foo_func },
685 { FOO_SYSCTL_DATA, "data", NULL, 0, 0644, NULL, &i2c_proc_real,
686 &i2c_sysctl_real,NULL,&foo_data },
687 { 0 }
688 };
689
690In the above example, three entries are defined. They can either be
691accessed through the /proc interface, in the /proc/sys/dev/sensors/*
692directories, as files named func1, func2 and data, or alternatively
693through the sysctl interface, in the appropriate table, with identifiers
694FOO_SYSCTL_FUNC1, FOO_SYSCTL_FUNC2 and FOO_SYSCTL_DATA.
695
696The third, sixth and ninth parameters should always be NULL, and the
697fourth should always be 0. The fifth is the mode of the /proc file;
6980644 is safe, as the file will be owned by root:root.
699
700The seventh and eighth parameters should be &i2c_proc_real and
701&i2c_sysctl_real if you want to export lists of reals (scaled
702integers). You can also use your own function for them, as usual.
703Finally, the last parameter is the call-back to gather the data
704(see below) if you use the *_proc_real functions.
705
706
707Gathering the data
708------------------
709
710The call back functions (foo_func and foo_data in the above example)
711can be called in several ways; the operation parameter determines
712what should be done:
713
714 * If operation == SENSORS_PROC_REAL_INFO, you must return the
715 magnitude (scaling) in nrels_mag;
716 * If operation == SENSORS_PROC_REAL_READ, you must read information
717 from the chip and return it in results. The number of integers
718 to display should be put in nrels_mag;
719 * If operation == SENSORS_PROC_REAL_WRITE, you must write the
720 supplied information to the chip. nrels_mag will contain the number
721 of integers, results the integers themselves.
722
723The *_proc_real functions will display the elements as reals for the
724/proc interface. If you set the magnitude to 2, and supply 345 for
725SENSORS_PROC_REAL_READ, it would display 3.45; and if the user would
726write 45.6 to the /proc file, it would be returned as 4560 for
727SENSORS_PROC_REAL_WRITE. A magnitude may even be negative!
728
729An example function:
730
731 /* FOO_FROM_REG and FOO_TO_REG translate between scaled values and
732 register values. Note the use of the read cache. */
733 void foo_in(struct i2c_client *client, int operation, int ctl_name,
734 int *nrels_mag, long *results)
735 {
736 struct foo_data *data = client->data;
737 int nr = ctl_name - FOO_SYSCTL_FUNC1; /* reduce to 0 upwards */
738
739 if (operation == SENSORS_PROC_REAL_INFO)
740 *nrels_mag = 2;
741 else if (operation == SENSORS_PROC_REAL_READ) {
742 /* Update the readings cache (if necessary) */
743 foo_update_client(client);
744 /* Get the readings from the cache */
745 results[0] = FOO_FROM_REG(data->foo_func_base[nr]);
746 results[1] = FOO_FROM_REG(data->foo_func_more[nr]);
747 results[2] = FOO_FROM_REG(data->foo_func_readonly[nr]);
748 *nrels_mag = 2;
749 } else if (operation == SENSORS_PROC_REAL_WRITE) {
750 if (*nrels_mag >= 1) {
751 /* Update the cache */
752 data->foo_base[nr] = FOO_TO_REG(results[0]);
753 /* Update the chip */
754 foo_write_value(client,FOO_REG_FUNC_BASE(nr),data->foo_base[nr]);
755 }
756 if (*nrels_mag >= 2) {
757 /* Update the cache */
758 data->foo_more[nr] = FOO_TO_REG(results[1]);
759 /* Update the chip */
760 foo_write_value(client,FOO_REG_FUNC_MORE(nr),data->foo_more[nr]);
761 }
762 }
763 }