root/i2c/trunk/doc/writing-clients

Revision 4022, 31.0 kB (checked in by khali, 2 years ago)

Drop i2c-dev's empty command implementation. This is a backport
from Linux 2.6, original patch from Laurent Riffard. Also update the
documentation not to suggest an empty implementation of command.

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