blob: 97e138cbb2a9b49ca3e23e0e5f61d68e560b0f76 [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
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200151function i2c_probe() if you don't.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153NOTE: If you want to write a `sensors' driver, the interface is slightly
154 different! See below.
155
156
157
Jean Delvaref4b50262005-07-31 21:49:03 +0200158Probing classes
159---------------
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
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.
Jean Delvaref4b50262005-07-31 21:49:03 +0200174 This parameter overrules the 'normal_i2c' list only.
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 Delvaref4b50262005-07-31 21:49:03 +0200180Additionally, kind-specific force lists may optionally be defined if
181the driver supports several chip kinds. They are grouped in a
182NULL-terminated list of pointers named forces, those first element if the
183generic force list mentioned above. Each additional list correspond to an
184insmod parameter of the form force_<kind>.
185
Jean Delvareb3d54962005-04-02 20:31:02 +0200186Fortunately, as a module writer, you just have to define the `normal_i2c'
187parameter. The complete declaration could look like this:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Jean Delvareb3d54962005-04-02 20:31:02 +0200189 /* Scan 0x37, and 0x48 to 0x4f */
190 static unsigned short normal_i2c[] = { 0x37, 0x48, 0x49, 0x4a, 0x4b, 0x4c,
191 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 /* Magic definition of all other variables and things */
194 I2C_CLIENT_INSMOD;
Jean Delvaref4b50262005-07-31 21:49:03 +0200195 /* Or, if your driver supports, say, 2 kind of devices: */
196 I2C_CLIENT_INSMOD_2(foo, bar);
197
198If you use the multi-kind form, an enum will be defined for you:
199 enum chips { any_chip, foo, bar, ... }
200You can then (and certainly should) use it in the driver code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Jean Delvareb3d54962005-04-02 20:31:02 +0200202Note that you *have* to call the defined variable `normal_i2c',
203without any prefix!
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206Attaching to an adapter
207-----------------------
208
209Whenever a new adapter is inserted, or for all adapters if the driver is
210being registered, the callback attach_adapter() is called. Now is the
211time to determine what devices are present on the adapter, and to register
212a client for each of them.
213
214The attach_adapter callback is really easy: we just call the generic
215detection function. This function will scan the bus for us, using the
216information as defined in the lists explained above. If a device is
217detected at a specific address, another callback is called.
218
219 int foo_attach_adapter(struct i2c_adapter *adapter)
220 {
221 return i2c_probe(adapter,&addr_data,&foo_detect_client);
222 }
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224Remember, structure `addr_data' is defined by the macros explained above,
225so you do not have to define it yourself.
226
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200227The i2c_probe function will call the foo_detect_client
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228function only for those i2c addresses that actually have a device on
229them (unless a `force' parameter was used). In addition, addresses that
230are already in use (by some other registered client) are skipped.
231
232
233The detect client function
234--------------------------
235
Jean Delvare2ed2dc32005-07-31 21:42:02 +0200236The detect client function is called by i2c_probe. The `kind' parameter
237contains -1 for a probed detection, 0 for a forced detection, or a positive
238number for a forced detection with a chip type forced.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240Below, some things are only needed if this is a `sensors' driver. Those
241parts are between /* SENSORS ONLY START */ and /* SENSORS ONLY END */
242markers.
243
244This function should only return an error (any value != 0) if there is
245some reason why no more detection should be done anymore. If the
246detection just fails for this address, return 0.
247
248For now, you can ignore the `flags' parameter. It is there for future use.
249
250 int foo_detect_client(struct i2c_adapter *adapter, int address,
251 unsigned short flags, int kind)
252 {
253 int err = 0;
254 int i;
255 struct i2c_client *new_client;
256 struct foo_data *data;
257 const char *client_name = ""; /* For non-`sensors' drivers, put the real
258 name here! */
259
260 /* Let's see whether this adapter can support what we need.
261 Please substitute the things you need here!
262 For `sensors' drivers, add `! is_isa &&' to the if statement */
263 if (!i2c_check_functionality(adapter,I2C_FUNC_SMBUS_WORD_DATA |
264 I2C_FUNC_SMBUS_WRITE_BYTE))
265 goto ERROR0;
266
267 /* SENSORS ONLY START */
268 const char *type_name = "";
269 int is_isa = i2c_is_isa_adapter(adapter);
270
Jean Delvare02ff9822005-07-20 00:05:33 +0200271 /* Do this only if the chip can additionally be found on the ISA bus
272 (hybrid chip). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Jean Delvare02ff9822005-07-20 00:05:33 +0200274 if (is_isa) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /* Discard immediately if this ISA range is already used */
277 if (check_region(address,FOO_EXTENT))
278 goto ERROR0;
279
280 /* Probe whether there is anything on this address.
281 Some example code is below, but you will have to adapt this
282 for your own driver */
283
284 if (kind < 0) /* Only if no force parameter was used */ {
285 /* We may need long timeouts at least for some chips. */
286 #define REALLY_SLOW_IO
287 i = inb_p(address + 1);
288 if (inb_p(address + 2) != i)
289 goto ERROR0;
290 if (inb_p(address + 3) != i)
291 goto ERROR0;
292 if (inb_p(address + 7) != i)
293 goto ERROR0;
294 #undef REALLY_SLOW_IO
295
296 /* Let's just hope nothing breaks here */
297 i = inb_p(address + 5) & 0x7f;
298 outb_p(~i & 0x7f,address+5);
299 if ((inb_p(address + 5) & 0x7f) != (~i & 0x7f)) {
300 outb_p(i,address+5);
301 return 0;
302 }
303 }
304 }
305
306 /* SENSORS ONLY END */
307
308 /* OK. For now, we presume we have a valid client. We now create the
309 client structure, even though we cannot fill it completely yet.
310 But it allows us to access several i2c functions safely */
311
312 /* Note that we reserve some space for foo_data too. If you don't
313 need it, remove it. We do it here to help to lessen memory
314 fragmentation. */
315 if (! (new_client = kmalloc(sizeof(struct i2c_client) +
316 sizeof(struct foo_data),
317 GFP_KERNEL))) {
318 err = -ENOMEM;
319 goto ERROR0;
320 }
321
322 /* This is tricky, but it will set the data to the right value. */
323 client->data = new_client + 1;
324 data = (struct foo_data *) (client->data);
325
326 new_client->addr = address;
327 new_client->data = data;
328 new_client->adapter = adapter;
329 new_client->driver = &foo_driver;
330 new_client->flags = 0;
331
332 /* Now, we do the remaining detection. If no `force' parameter is used. */
333
334 /* First, the generic detection (if any), that is skipped if any force
335 parameter was used. */
336 if (kind < 0) {
337 /* The below is of course bogus */
338 if (foo_read(new_client,FOO_REG_GENERIC) != FOO_GENERIC_VALUE)
339 goto ERROR1;
340 }
341
342 /* SENSORS ONLY START */
343
344 /* Next, specific detection. This is especially important for `sensors'
345 devices. */
346
347 /* Determine the chip type. Not needed if a `force_CHIPTYPE' parameter
348 was used. */
349 if (kind <= 0) {
350 i = foo_read(new_client,FOO_REG_CHIPTYPE);
351 if (i == FOO_TYPE_1)
352 kind = chip1; /* As defined in the enum */
353 else if (i == FOO_TYPE_2)
354 kind = chip2;
355 else {
356 printk("foo: Ignoring 'force' parameter for unknown chip at "
357 "adapter %d, address 0x%02x\n",i2c_adapter_id(adapter),address);
358 goto ERROR1;
359 }
360 }
361
362 /* Now set the type and chip names */
363 if (kind == chip1) {
364 type_name = "chip1"; /* For /proc entry */
365 client_name = "CHIP 1";
366 } else if (kind == chip2) {
367 type_name = "chip2"; /* For /proc entry */
368 client_name = "CHIP 2";
369 }
370
371 /* Reserve the ISA region */
372 if (is_isa)
373 request_region(address,FOO_EXTENT,type_name);
374
375 /* SENSORS ONLY END */
376
377 /* Fill in the remaining client fields. */
378 strcpy(new_client->name,client_name);
379
380 /* SENSORS ONLY BEGIN */
381 data->type = kind;
382 /* SENSORS ONLY END */
383
384 data->valid = 0; /* Only if you use this field */
385 init_MUTEX(&data->update_lock); /* Only if you use this field */
386
387 /* Any other initializations in data must be done here too. */
388
389 /* Tell the i2c layer a new client has arrived */
390 if ((err = i2c_attach_client(new_client)))
391 goto ERROR3;
392
393 /* SENSORS ONLY BEGIN */
394 /* Register a new directory entry with module sensors. See below for
395 the `template' structure. */
396 if ((i = i2c_register_entry(new_client, type_name,
397 foo_dir_table_template,THIS_MODULE)) < 0) {
398 err = i;
399 goto ERROR4;
400 }
401 data->sysctl_id = i;
402
403 /* SENSORS ONLY END */
404
405 /* This function can write default values to the client registers, if
406 needed. */
407 foo_init_client(new_client);
408 return 0;
409
410 /* OK, this is not exactly good programming practice, usually. But it is
411 very code-efficient in this case. */
412
413 ERROR4:
414 i2c_detach_client(new_client);
415 ERROR3:
416 ERROR2:
417 /* SENSORS ONLY START */
418 if (is_isa)
419 release_region(address,FOO_EXTENT);
420 /* SENSORS ONLY END */
421 ERROR1:
422 kfree(new_client);
423 ERROR0:
424 return err;
425 }
426
427
428Removing the client
429===================
430
431The detach_client call back function is called when a client should be
432removed. It may actually fail, but only when panicking. This code is
433much simpler than the attachment code, fortunately!
434
435 int foo_detach_client(struct i2c_client *client)
436 {
437 int err,i;
438
439 /* SENSORS ONLY START */
440 /* Deregister with the `i2c-proc' module. */
441 i2c_deregister_entry(((struct lm78_data *)(client->data))->sysctl_id);
442 /* SENSORS ONLY END */
443
444 /* Try to detach the client from i2c space */
Jean Delvare7bef5592005-07-27 22:14:49 +0200445 if ((err = i2c_detach_client(client)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Jean Delvare02ff9822005-07-20 00:05:33 +0200448 /* HYBRID SENSORS CHIP ONLY START */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if i2c_is_isa_client(client)
450 release_region(client->addr,LM78_EXTENT);
Jean Delvare02ff9822005-07-20 00:05:33 +0200451 /* HYBRID SENSORS CHIP ONLY END */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 kfree(client); /* Frees client data too, if allocated at the same time */
454 return 0;
455 }
456
457
458Initializing the module or kernel
459=================================
460
461When the kernel is booted, or when your foo driver module is inserted,
462you have to do some initializing. Fortunately, just attaching (registering)
463the driver module is usually enough.
464
465 /* Keep track of how far we got in the initialization process. If several
466 things have to initialized, and we fail halfway, only those things
467 have to be cleaned up! */
468 static int __initdata foo_initialized = 0;
469
470 static int __init foo_init(void)
471 {
472 int res;
473 printk("foo version %s (%s)\n",FOO_VERSION,FOO_DATE);
474
475 if ((res = i2c_add_driver(&foo_driver))) {
476 printk("foo: Driver registration failed, module not inserted.\n");
477 foo_cleanup();
478 return res;
479 }
480 foo_initialized ++;
481 return 0;
482 }
483
484 void foo_cleanup(void)
485 {
486 if (foo_initialized == 1) {
487 if ((res = i2c_del_driver(&foo_driver))) {
488 printk("foo: Driver registration failed, module not removed.\n");
489 return;
490 }
491 foo_initialized --;
492 }
493 }
494
495 /* Substitute your own name and email address */
496 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>"
497 MODULE_DESCRIPTION("Driver for Barf Inc. Foo I2C devices");
498
499 module_init(foo_init);
500 module_exit(foo_cleanup);
501
502Note that some functions are marked by `__init', and some data structures
503by `__init_data'. Hose functions and structures can be removed after
504kernel booting (or module loading) is completed.
505
506Command function
507================
508
509A generic ioctl-like function call back is supported. You will seldom
510need this. You may even set it to NULL.
511
512 /* No commands defined */
513 int foo_command(struct i2c_client *client, unsigned int cmd, void *arg)
514 {
515 return 0;
516 }
517
518
519Sending and receiving
520=====================
521
522If you want to communicate with your device, there are several functions
523to do this. You can find all of them in i2c.h.
524
525If you can choose between plain i2c communication and SMBus level
526communication, please use the last. All adapters understand SMBus level
527commands, but only some of them understand plain i2c!
528
529
530Plain i2c communication
531-----------------------
532
533 extern int i2c_master_send(struct i2c_client *,const char* ,int);
534 extern int i2c_master_recv(struct i2c_client *,char* ,int);
535
536These routines read and write some bytes from/to a client. The client
537contains the i2c address, so you do not have to include it. The second
538parameter contains the bytes the read/write, the third the length of the
539buffer. Returned is the actual number of bytes read/written.
540
541 extern int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msg,
542 int num);
543
544This sends a series of messages. Each message can be a read or write,
545and they can be mixed in any way. The transactions are combined: no
546stop bit is sent between transaction. The i2c_msg structure contains
547for each message the client address, the number of bytes of the message
548and the message data itself.
549
550You can read the file `i2c-protocol' for more information about the
551actual i2c protocol.
552
553
554SMBus communication
555-------------------
556
557 extern s32 i2c_smbus_xfer (struct i2c_adapter * adapter, u16 addr,
558 unsigned short flags,
559 char read_write, u8 command, int size,
560 union i2c_smbus_data * data);
561
562 This is the generic SMBus function. All functions below are implemented
563 in terms of it. Never use this function directly!
564
565
566 extern s32 i2c_smbus_write_quick(struct i2c_client * client, u8 value);
567 extern s32 i2c_smbus_read_byte(struct i2c_client * client);
568 extern s32 i2c_smbus_write_byte(struct i2c_client * client, u8 value);
569 extern s32 i2c_smbus_read_byte_data(struct i2c_client * client, u8 command);
570 extern s32 i2c_smbus_write_byte_data(struct i2c_client * client,
571 u8 command, u8 value);
572 extern s32 i2c_smbus_read_word_data(struct i2c_client * client, u8 command);
573 extern s32 i2c_smbus_write_word_data(struct i2c_client * client,
574 u8 command, u16 value);
575 extern s32 i2c_smbus_write_block_data(struct i2c_client * client,
576 u8 command, u8 length,
577 u8 *values);
578
579These ones were removed in Linux 2.6.10 because they had no users, but could
580be added back later if needed:
581
582 extern s32 i2c_smbus_read_i2c_block_data(struct i2c_client * client,
583 u8 command, u8 *values);
584 extern s32 i2c_smbus_read_block_data(struct i2c_client * client,
585 u8 command, u8 *values);
586 extern s32 i2c_smbus_write_i2c_block_data(struct i2c_client * client,
587 u8 command, u8 length,
588 u8 *values);
589 extern s32 i2c_smbus_process_call(struct i2c_client * client,
590 u8 command, u16 value);
591 extern s32 i2c_smbus_block_process_call(struct i2c_client *client,
592 u8 command, u8 length,
593 u8 *values)
594
595All these transactions return -1 on failure. The 'write' transactions
596return 0 on success; the 'read' transactions return the read value, except
597for read_block, which returns the number of values read. The block buffers
598need not be longer than 32 bytes.
599
600You can read the file `smbus-protocol' for more information about the
601actual SMBus protocol.
602
603
604General purpose routines
605========================
606
607Below all general purpose routines are listed, that were not mentioned
608before.
609
610 /* This call returns a unique low identifier for each registered adapter,
611 * or -1 if the adapter was not registered.
612 */
613 extern int i2c_adapter_id(struct i2c_adapter *adap);
614
615
616The sensors sysctl/proc interface
617=================================
618
619This section only applies if you write `sensors' drivers.
620
621Each sensors driver creates a directory in /proc/sys/dev/sensors for each
622registered client. The directory is called something like foo-i2c-4-65.
623The sensors module helps you to do this as easily as possible.
624
625The template
626------------
627
628You will need to define a ctl_table template. This template will automatically
629be copied to a newly allocated structure and filled in where necessary when
630you call sensors_register_entry.
631
632First, I will give an example definition.
633 static ctl_table foo_dir_table_template[] = {
634 { FOO_SYSCTL_FUNC1, "func1", NULL, 0, 0644, NULL, &i2c_proc_real,
635 &i2c_sysctl_real,NULL,&foo_func },
636 { FOO_SYSCTL_FUNC2, "func2", NULL, 0, 0644, NULL, &i2c_proc_real,
637 &i2c_sysctl_real,NULL,&foo_func },
638 { FOO_SYSCTL_DATA, "data", NULL, 0, 0644, NULL, &i2c_proc_real,
639 &i2c_sysctl_real,NULL,&foo_data },
640 { 0 }
641 };
642
643In the above example, three entries are defined. They can either be
644accessed through the /proc interface, in the /proc/sys/dev/sensors/*
645directories, as files named func1, func2 and data, or alternatively
646through the sysctl interface, in the appropriate table, with identifiers
647FOO_SYSCTL_FUNC1, FOO_SYSCTL_FUNC2 and FOO_SYSCTL_DATA.
648
649The third, sixth and ninth parameters should always be NULL, and the
650fourth should always be 0. The fifth is the mode of the /proc file;
6510644 is safe, as the file will be owned by root:root.
652
653The seventh and eighth parameters should be &i2c_proc_real and
654&i2c_sysctl_real if you want to export lists of reals (scaled
655integers). You can also use your own function for them, as usual.
656Finally, the last parameter is the call-back to gather the data
657(see below) if you use the *_proc_real functions.
658
659
660Gathering the data
661------------------
662
663The call back functions (foo_func and foo_data in the above example)
664can be called in several ways; the operation parameter determines
665what should be done:
666
667 * If operation == SENSORS_PROC_REAL_INFO, you must return the
668 magnitude (scaling) in nrels_mag;
669 * If operation == SENSORS_PROC_REAL_READ, you must read information
670 from the chip and return it in results. The number of integers
671 to display should be put in nrels_mag;
672 * If operation == SENSORS_PROC_REAL_WRITE, you must write the
673 supplied information to the chip. nrels_mag will contain the number
674 of integers, results the integers themselves.
675
676The *_proc_real functions will display the elements as reals for the
677/proc interface. If you set the magnitude to 2, and supply 345 for
678SENSORS_PROC_REAL_READ, it would display 3.45; and if the user would
679write 45.6 to the /proc file, it would be returned as 4560 for
680SENSORS_PROC_REAL_WRITE. A magnitude may even be negative!
681
682An example function:
683
684 /* FOO_FROM_REG and FOO_TO_REG translate between scaled values and
685 register values. Note the use of the read cache. */
686 void foo_in(struct i2c_client *client, int operation, int ctl_name,
687 int *nrels_mag, long *results)
688 {
689 struct foo_data *data = client->data;
690 int nr = ctl_name - FOO_SYSCTL_FUNC1; /* reduce to 0 upwards */
691
692 if (operation == SENSORS_PROC_REAL_INFO)
693 *nrels_mag = 2;
694 else if (operation == SENSORS_PROC_REAL_READ) {
695 /* Update the readings cache (if necessary) */
696 foo_update_client(client);
697 /* Get the readings from the cache */
698 results[0] = FOO_FROM_REG(data->foo_func_base[nr]);
699 results[1] = FOO_FROM_REG(data->foo_func_more[nr]);
700 results[2] = FOO_FROM_REG(data->foo_func_readonly[nr]);
701 *nrels_mag = 2;
702 } else if (operation == SENSORS_PROC_REAL_WRITE) {
703 if (*nrels_mag >= 1) {
704 /* Update the cache */
705 data->foo_base[nr] = FOO_TO_REG(results[0]);
706 /* Update the chip */
707 foo_write_value(client,FOO_REG_FUNC_BASE(nr),data->foo_base[nr]);
708 }
709 if (*nrels_mag >= 2) {
710 /* Update the cache */
711 data->foo_more[nr] = FOO_TO_REG(results[1]);
712 /* Update the chip */
713 foo_write_value(client,FOO_REG_FUNC_MORE(nr),data->foo_more[nr]);
714 }
715 }
716 }