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