blob: f03c2a02f806684c39c0cd8459eaab71a661496b [file] [log] [blame]
Jean Delvare92b42942005-11-26 21:05:17 +01001Revision 6, 2005-11-20
Linus Torvalds1da177e2005-04-16 15:20:36 -07002Jean Delvare <khali@linux-fr.org>
3Greg KH <greg@kroah.com>
4
5This is a guide on how to convert I2C chip drivers from Linux 2.4 to
6Linux 2.6. I have been using existing drivers (lm75, lm78) as examples.
7Then I converted a driver myself (lm83) and updated this document.
Jean Delvare92b42942005-11-26 21:05:17 +01008Note that this guide is strongly oriented towards hardware monitoring
9drivers. Many points are still valid for other type of drivers, but
10others may be irrelevant.
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12There are two sets of points below. The first set concerns technical
13changes. The second set concerns coding policy. Both are mandatory.
14
15Although reading this guide will help you porting drivers, I suggest
16you keep an eye on an already ported driver while porting your own
17driver. This will help you a lot understanding what this guide
18exactly means. Choose the chip driver that is the more similar to
19yours for best results.
20
21Technical changes:
22
Jean Delvaref4b50262005-07-31 21:49:03 +020023* [Includes] Get rid of "version.h" and <linux/i2c-proc.h>.
24 Includes typically look like that:
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 #include <linux/module.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
Jean Delvare92b42942005-11-26 21:05:17 +010028 #include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 #include <linux/i2c.h>
Jean Delvare92b42942005-11-26 21:05:17 +010030 #include <linux/i2c-isa.h> /* for ISA drivers */
Jean Delvare303760b2005-07-31 21:52:01 +020031 #include <linux/hwmon.h> /* for hardware monitoring drivers */
32 #include <linux/hwmon-sysfs.h>
33 #include <linux/hwmon-vid.h> /* if you need VRM support */
Jean Delvare92b42942005-11-26 21:05:17 +010034 #include <linux/err.h> /* for class registration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 #include <asm/io.h> /* if you have I/O operations */
36 Please respect this inclusion order. Some extra headers may be
37 required for a given driver (e.g. "lm75.h").
38
Jean Delvare50718602005-07-20 00:02:32 +020039* [Addresses] SENSORS_I2C_END becomes I2C_CLIENT_END, ISA addresses
Jean Delvare92b42942005-11-26 21:05:17 +010040 are no more handled by the i2c core. Address ranges are no more
41 supported either, define each individual address separately.
Jean Delvaref4b50262005-07-31 21:49:03 +020042 SENSORS_INSMOD_<n> becomes I2C_CLIENT_INSMOD_<n>.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44* [Client data] Get rid of sysctl_id. Try using standard names for
45 register values (for example, temp_os becomes temp_max). You're
46 still relatively free here, but you *have* to follow the standard
47 names for sysfs files (see the Sysctl section below).
48
49* [Function prototypes] The detect functions loses its flags
50 parameter. Sysctl (e.g. lm75_temp) and miscellaneous functions
51 are off the list of prototypes. This usually leaves five
52 prototypes:
53 static int lm75_attach_adapter(struct i2c_adapter *adapter);
54 static int lm75_detect(struct i2c_adapter *adapter, int address,
55 int kind);
56 static void lm75_init_client(struct i2c_client *client);
57 static int lm75_detach_client(struct i2c_client *client);
Jean Delvare92b42942005-11-26 21:05:17 +010058 static struct lm75_data lm75_update_device(struct device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60* [Sysctl] All sysctl stuff is of course gone (defines, ctl_table
61 and functions). Instead, you have to define show and set functions for
62 each sysfs file. Only define set for writable values. Take a look at an
Jean Delvare92b42942005-11-26 21:05:17 +010063 existing 2.6 driver for details (it87 for example). Don't forget
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 to define the attributes for each file (this is that step that
65 links callback functions). Use the file names specified in
Jean Delvare92b42942005-11-26 21:05:17 +010066 Documentation/hwmon/sysfs-interface for the individual files. Also
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 convert the units these files read and write to the specified ones.
68 If you need to add a new type of file, please discuss it on the
Jean Delvarecc0b07e2005-05-22 09:39:11 +020069 sensors mailing list <lm-sensors@lm-sensors.org> by providing a
Jean Delvare92b42942005-11-26 21:05:17 +010070 patch to the Documentation/hwmon/sysfs-interface file.
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72* [Attach] For I2C drivers, the attach function should make sure
Jean Delvare92b42942005-11-26 21:05:17 +010073 that the adapter's class has I2C_CLASS_HWMON (or whatever class is
74 suitable for your driver), using the following construct:
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (!(adapter->class & I2C_CLASS_HWMON))
76 return 0;
77 ISA-only drivers of course don't need this.
Jean Delvare2ed2dc32005-07-31 21:42:02 +020078 Call i2c_probe() instead of i2c_detect().
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80* [Detect] As mentioned earlier, the flags parameter is gone.
81 The type_name and client_name strings are replaced by a single
Jean Delvare92b42942005-11-26 21:05:17 +010082 name string, which will be filled with a lowercase, short string.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 In i2c-only drivers, drop the i2c_is_isa_adapter check, it's
Jean Delvare50718602005-07-20 00:02:32 +020084 useless. Same for isa-only drivers, as the test would always be
85 true. Only hybrid drivers (which are quite rare) still need it.
Jean Delvare92b42942005-11-26 21:05:17 +010086 The labels used for error paths are reduced to the number needed.
87 It is advised that the labels are given descriptive names such as
88 exit and exit_free. Don't forget to properly set err before
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 jumping to error labels. By the way, labels should be left-aligned.
Jean Delvare2445eb62005-10-17 23:16:25 +020090 Use kzalloc instead of kmalloc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 Use i2c_set_clientdata to set the client data (as opposed to
92 a direct access to client->data).
Jean Delvare92b42942005-11-26 21:05:17 +010093 Use strlcpy instead of strcpy or snprintf to copy the client name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 Replace the sysctl directory registration by calls to
95 device_create_file. Move the driver initialization before any
96 sysfs file creation.
Jean Delvare92b42942005-11-26 21:05:17 +010097 Register the client with the hwmon class (using hwmon_device_register)
98 if applicable.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 Drop client->id.
Jean Delvare4c9337d2005-08-09 20:28:10 +0200100 Drop any 24RF08 corruption prevention you find, as this is now done
101 at the i2c-core level, and doing it twice voids it.
Jean Delvarecf02df72005-11-26 21:03:41 +0100102 Don't add I2C_CLIENT_ALLOW_USE to client->flags, it's the default now.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104* [Init] Limits must not be set by the driver (can be done later in
105 user-space). Chip should not be reset default (although a module
Jean Delvare92b42942005-11-26 21:05:17 +0100106 parameter may be used to force it), and initialization should be
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 limited to the strictly necessary steps.
108
Jean Delvare92b42942005-11-26 21:05:17 +0100109* [Detach] Remove the call to i2c_deregister_entry. Do not log an
110 error message if i2c_detach_client fails, as i2c-core will now do
111 it for you.
112 Unregister from the hwmon class if applicable.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Jean Delvare92b42942005-11-26 21:05:17 +0100114* [Update] The function prototype changed, it is now
115 passed a device structure, which you have to convert to a client
116 using to_i2c_client(dev). The update function should return a
117 pointer to the client data.
118 Don't access client->data directly, use i2c_get_clientdata(client)
119 instead.
120 Use time_after() instead of direct jiffies comparison.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Jean Delvare92b42942005-11-26 21:05:17 +0100122* [Interface] Make sure there is a MODULE_LICENSE() line, at the bottom
123 of the file (after MODULE_AUTHOR() and MODULE_DESCRIPTION(), in this
124 order).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Jean Delvare8a994752005-11-26 20:28:06 +0100126* [Driver] The flags field of the i2c_driver structure is gone.
127 I2C_DF_NOTIFY is now the default behavior.
Jean Delvared45d2042005-11-26 20:55:35 +0100128 The i2c_driver structure has a driver member, which is itself a
Jean Delvared82c0bf2005-12-07 21:54:26 +0100129 structure, those name member should be initialized to a driver name
130 string. i2c_driver itself has no name member anymore.
Jean Delvare8a994752005-11-26 20:28:06 +0100131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132Coding policy:
133
134* [Copyright] Use (C), not (c), for copyright.
135
136* [Debug/log] Get rid of #ifdef DEBUG/#endif constructs whenever you
Jean Delvare92b42942005-11-26 21:05:17 +0100137 can. Calls to printk for debugging purposes are replaced by calls to
138 dev_dbg where possible, else to pr_debug. Here is an example of how
139 to call it (taken from lm75_detect):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 dev_dbg(&client->dev, "Starting lm75 update\n");
141 Replace other printk calls with the dev_info, dev_err or dev_warn
142 function, as appropriate.
143
Jean Delvare92b42942005-11-26 21:05:17 +0100144* [Constants] Constants defines (registers, conversions) should be
145 aligned. This greatly improves readability.
146 Alignments are achieved by the means of tabs, not spaces. Remember
147 that tabs are set to 8 in the Linux kernel code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149* [Layout] Avoid extra empty lines between comments and what they
150 comment. Respect the coding style (see Documentation/CodingStyle),
151 in particular when it comes to placing curly braces.
152
153* [Comments] Make sure that no comment refers to a file that isn't
154 part of the Linux source tree (typically doc/chips/<chip name>),
155 and that remaining comments still match the code. Merging comment
156 lines when possible is encouraged.