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