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