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