Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 1 | Revision 7, 2007-04-19 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | Jean Delvare <khali@linux-fr.org> |
| 3 | Greg KH <greg@kroah.com> |
| 4 | |
| 5 | This is a guide on how to convert I2C chip drivers from Linux 2.4 to |
| 6 | Linux 2.6. I have been using existing drivers (lm75, lm78) as examples. |
| 7 | Then I converted a driver myself (lm83) and updated this document. |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 8 | Note that this guide is strongly oriented towards hardware monitoring |
| 9 | drivers. Many points are still valid for other type of drivers, but |
| 10 | others may be irrelevant. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
| 12 | There are two sets of points below. The first set concerns technical |
| 13 | changes. The second set concerns coding policy. Both are mandatory. |
| 14 | |
| 15 | Although reading this guide will help you porting drivers, I suggest |
| 16 | you keep an eye on an already ported driver while porting your own |
| 17 | driver. This will help you a lot understanding what this guide |
| 18 | exactly means. Choose the chip driver that is the more similar to |
| 19 | yours for best results. |
| 20 | |
| 21 | Technical changes: |
| 22 | |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 23 | * [Driver type] Any driver that was relying on i2c-isa has to be |
| 24 | converted to a proper isa, platform or pci driver. This is not |
| 25 | covered by this guide. |
| 26 | |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 27 | * [Includes] Get rid of "version.h" and <linux/i2c-proc.h>. |
| 28 | Includes typically look like that: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/module.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/slab.h> |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 32 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/i2c.h> |
Jean Delvare | 303760b | 2005-07-31 21:52:01 +0200 | [diff] [blame] | 34 | #include <linux/hwmon.h> /* for hardware monitoring drivers */ |
| 35 | #include <linux/hwmon-sysfs.h> |
| 36 | #include <linux/hwmon-vid.h> /* if you need VRM support */ |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 37 | #include <linux/err.h> /* for class registration */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | Please respect this inclusion order. Some extra headers may be |
| 39 | required for a given driver (e.g. "lm75.h"). |
| 40 | |
Jean Delvare | 5071860 | 2005-07-20 00:02:32 +0200 | [diff] [blame] | 41 | * [Addresses] SENSORS_I2C_END becomes I2C_CLIENT_END, ISA addresses |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 42 | are no more handled by the i2c core. Address ranges are no more |
| 43 | supported either, define each individual address separately. |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 44 | SENSORS_INSMOD_<n> becomes I2C_CLIENT_INSMOD_<n>. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | * [Client data] Get rid of sysctl_id. Try using standard names for |
| 47 | register values (for example, temp_os becomes temp_max). You're |
| 48 | still relatively free here, but you *have* to follow the standard |
| 49 | names for sysfs files (see the Sysctl section below). |
| 50 | |
| 51 | * [Function prototypes] The detect functions loses its flags |
| 52 | parameter. Sysctl (e.g. lm75_temp) and miscellaneous functions |
| 53 | are off the list of prototypes. This usually leaves five |
| 54 | prototypes: |
| 55 | static int lm75_attach_adapter(struct i2c_adapter *adapter); |
| 56 | static int lm75_detect(struct i2c_adapter *adapter, int address, |
| 57 | int kind); |
| 58 | static void lm75_init_client(struct i2c_client *client); |
| 59 | static int lm75_detach_client(struct i2c_client *client); |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 60 | static struct lm75_data lm75_update_device(struct device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | * [Sysctl] All sysctl stuff is of course gone (defines, ctl_table |
| 63 | and functions). Instead, you have to define show and set functions for |
| 64 | each sysfs file. Only define set for writable values. Take a look at an |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 65 | existing 2.6 driver for details (it87 for example). Don't forget |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | to define the attributes for each file (this is that step that |
| 67 | links callback functions). Use the file names specified in |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 68 | Documentation/hwmon/sysfs-interface for the individual files. Also |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | convert the units these files read and write to the specified ones. |
| 70 | If you need to add a new type of file, please discuss it on the |
Jean Delvare | cc0b07e | 2005-05-22 09:39:11 +0200 | [diff] [blame] | 71 | sensors mailing list <lm-sensors@lm-sensors.org> by providing a |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 72 | patch to the Documentation/hwmon/sysfs-interface file. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 74 | * [Attach] The attach function should make sure that the adapter's |
| 75 | class has I2C_CLASS_HWMON (or whatever class is suitable for your |
| 76 | driver), using the following construct: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | if (!(adapter->class & I2C_CLASS_HWMON)) |
| 78 | return 0; |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 79 | Call i2c_probe() instead of i2c_detect(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | * [Detect] As mentioned earlier, the flags parameter is gone. |
| 82 | The type_name and client_name strings are replaced by a single |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 83 | name string, which will be filled with a lowercase, short string. |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 84 | The labels used for error paths are reduced to the number needed. |
| 85 | It is advised that the labels are given descriptive names such as |
| 86 | exit and exit_free. Don't forget to properly set err before |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | jumping to error labels. By the way, labels should be left-aligned. |
Jean Delvare | 2445eb6 | 2005-10-17 23:16:25 +0200 | [diff] [blame] | 88 | Use kzalloc instead of kmalloc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | Use i2c_set_clientdata to set the client data (as opposed to |
| 90 | a direct access to client->data). |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 91 | Use strlcpy instead of strcpy or snprintf to copy the client name. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | Replace the sysctl directory registration by calls to |
| 93 | device_create_file. Move the driver initialization before any |
| 94 | sysfs file creation. |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 95 | Register the client with the hwmon class (using hwmon_device_register) |
| 96 | if applicable. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | Drop client->id. |
Jean Delvare | 4c9337d | 2005-08-09 20:28:10 +0200 | [diff] [blame] | 98 | Drop any 24RF08 corruption prevention you find, as this is now done |
| 99 | at the i2c-core level, and doing it twice voids it. |
Jean Delvare | cf02df7 | 2005-11-26 21:03:41 +0100 | [diff] [blame] | 100 | Don't add I2C_CLIENT_ALLOW_USE to client->flags, it's the default now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | * [Init] Limits must not be set by the driver (can be done later in |
| 103 | user-space). Chip should not be reset default (although a module |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 104 | parameter may be used to force it), and initialization should be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | limited to the strictly necessary steps. |
| 106 | |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 107 | * [Detach] Remove the call to i2c_deregister_entry. Do not log an |
| 108 | error message if i2c_detach_client fails, as i2c-core will now do |
| 109 | it for you. |
| 110 | Unregister from the hwmon class if applicable. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 112 | * [Update] The function prototype changed, it is now |
| 113 | passed a device structure, which you have to convert to a client |
| 114 | using to_i2c_client(dev). The update function should return a |
| 115 | pointer to the client data. |
| 116 | Don't access client->data directly, use i2c_get_clientdata(client) |
| 117 | instead. |
| 118 | Use time_after() instead of direct jiffies comparison. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 120 | * [Interface] Make sure there is a MODULE_LICENSE() line, at the bottom |
| 121 | of the file (after MODULE_AUTHOR() and MODULE_DESCRIPTION(), in this |
| 122 | order). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | |
Jean Delvare | 8a99475 | 2005-11-26 20:28:06 +0100 | [diff] [blame] | 124 | * [Driver] The flags field of the i2c_driver structure is gone. |
| 125 | I2C_DF_NOTIFY is now the default behavior. |
Jean Delvare | d45d204 | 2005-11-26 20:55:35 +0100 | [diff] [blame] | 126 | The i2c_driver structure has a driver member, which is itself a |
Jean Delvare | d82c0bf | 2005-12-07 21:54:26 +0100 | [diff] [blame] | 127 | structure, those name member should be initialized to a driver name |
| 128 | string. i2c_driver itself has no name member anymore. |
Jean Delvare | 8a99475 | 2005-11-26 20:28:06 +0100 | [diff] [blame] | 129 | |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 130 | * [Driver model] Instead of shutdown or reboot notifiers, provide a |
| 131 | shutdown() method in your driver. |
| 132 | |
| 133 | * [Power management] Use the driver model suspend() and resume() |
| 134 | callbacks instead of the obsolete pm_register() calls. |
| 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | Coding policy: |
| 137 | |
| 138 | * [Copyright] Use (C), not (c), for copyright. |
| 139 | |
| 140 | * [Debug/log] Get rid of #ifdef DEBUG/#endif constructs whenever you |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 141 | can. Calls to printk for debugging purposes are replaced by calls to |
| 142 | dev_dbg where possible, else to pr_debug. Here is an example of how |
| 143 | to call it (taken from lm75_detect): |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | dev_dbg(&client->dev, "Starting lm75 update\n"); |
| 145 | Replace other printk calls with the dev_info, dev_err or dev_warn |
| 146 | function, as appropriate. |
| 147 | |
Jean Delvare | 92b4294 | 2005-11-26 21:05:17 +0100 | [diff] [blame] | 148 | * [Constants] Constants defines (registers, conversions) should be |
| 149 | aligned. This greatly improves readability. |
| 150 | Alignments are achieved by the means of tabs, not spaces. Remember |
| 151 | that tabs are set to 8 in the Linux kernel code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | * [Layout] Avoid extra empty lines between comments and what they |
| 154 | comment. Respect the coding style (see Documentation/CodingStyle), |
| 155 | in particular when it comes to placing curly braces. |
| 156 | |
| 157 | * [Comments] Make sure that no comment refers to a file that isn't |
| 158 | part of the Linux source tree (typically doc/chips/<chip name>), |
| 159 | and that remaining comments still match the code. Merging comment |
| 160 | lines when possible is encouraged. |