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