Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | This is a small guide for those who want to write kernel drivers for I2C |
David Brownell | 4298cfc | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 2 | or SMBus devices, using Linux as the protocol host/master (not slave). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | |
| 4 | To set up a driver, you need to do several things. Some are optional, and |
| 5 | some things can be done slightly or completely different. Use this as a |
| 6 | guide, not as a rule book! |
| 7 | |
| 8 | |
| 9 | General remarks |
| 10 | =============== |
| 11 | |
| 12 | Try to keep the kernel namespace as clean as possible. The best way to |
| 13 | do this is to use a unique prefix for all global symbols. This is |
| 14 | especially important for exported symbols, but it is a good idea to do |
| 15 | it for non-exported symbols too. We will use the prefix `foo_' in this |
| 16 | tutorial, and `FOO_' for preprocessor variables. |
| 17 | |
| 18 | |
| 19 | The driver structure |
| 20 | ==================== |
| 21 | |
| 22 | Usually, you will implement a single driver structure, and instantiate |
| 23 | all clients from it. Remember, a driver structure contains general access |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 24 | routines, and should be zero-initialized except for fields with data you |
| 25 | provide. A client structure holds device-specific information like the |
| 26 | driver model device node, and its I2C address. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Ben Dooks | 2260e63 | 2008-07-01 22:38:18 +0200 | [diff] [blame^] | 28 | /* iff driver uses driver model ("new style") binding model: */ |
| 29 | |
| 30 | static struct i2c_device_id foo_idtable[] = { |
| 31 | { "foo", my_id_for_foo }, |
| 32 | { "bar", my_id_for_bar }, |
| 33 | { } |
| 34 | }; |
| 35 | |
| 36 | MODULE_DEVICE_TABLE(i2c, foo_idtable); |
| 37 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | static struct i2c_driver foo_driver = { |
Jean Delvare | d45d204 | 2005-11-26 20:55:35 +0100 | [diff] [blame] | 39 | .driver = { |
Jean Delvare | d45d204 | 2005-11-26 20:55:35 +0100 | [diff] [blame] | 40 | .name = "foo", |
| 41 | }, |
David Brownell | 4298cfc | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 42 | |
| 43 | /* iff driver uses driver model ("new style") binding model: */ |
Ben Dooks | 2260e63 | 2008-07-01 22:38:18 +0200 | [diff] [blame^] | 44 | .id_table = foo_ids, |
David Brownell | 4298cfc | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 45 | .probe = foo_probe, |
| 46 | .remove = foo_remove, |
| 47 | |
| 48 | /* else, driver uses "legacy" binding model: */ |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 49 | .attach_adapter = foo_attach_adapter, |
| 50 | .detach_client = foo_detach_client, |
David Brownell | 4298cfc | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 51 | |
| 52 | /* these may be used regardless of the driver binding model */ |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 53 | .shutdown = foo_shutdown, /* optional */ |
| 54 | .suspend = foo_suspend, /* optional */ |
| 55 | .resume = foo_resume, /* optional */ |
| 56 | .command = foo_command, /* optional */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 59 | The name field is the driver name, and must not contain spaces. It |
| 60 | should match the module name (if the driver can be compiled as a module), |
| 61 | although you can use MODULE_ALIAS (passing "foo" in this example) to add |
David Brownell | 4298cfc | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 62 | another name for the module. If the driver name doesn't match the module |
| 63 | name, the module won't be automatically loaded (hotplug/coldplug). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | All other fields are for call-back functions which will be explained |
| 66 | below. |
| 67 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
| 69 | Extra client data |
| 70 | ================= |
| 71 | |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 72 | Each client structure has a special `data' field that can point to any |
| 73 | structure at all. You should use this to keep device-specific data, |
| 74 | especially in drivers that handle multiple I2C or SMBUS devices. You |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | do not always need this, but especially for `sensors' drivers, it can |
| 76 | be very useful. |
| 77 | |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 78 | /* store the value */ |
| 79 | void i2c_set_clientdata(struct i2c_client *client, void *data); |
| 80 | |
| 81 | /* retrieve the value */ |
| 82 | void *i2c_get_clientdata(struct i2c_client *client); |
| 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | An example structure is below. |
| 85 | |
| 86 | struct foo_data { |
Jean Delvare | 2445eb6 | 2005-10-17 23:16:25 +0200 | [diff] [blame] | 87 | struct i2c_client client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | enum chips type; /* To keep the chips type for `sensors' drivers. */ |
| 89 | |
| 90 | /* Because the i2c bus is slow, it is often useful to cache the read |
| 91 | information of a chip for some time (for example, 1 or 2 seconds). |
| 92 | It depends of course on the device whether this is really worthwhile |
| 93 | or even sensible. */ |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 94 | struct mutex update_lock; /* When we are reading lots of information, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | another process should not update the |
| 96 | below information */ |
| 97 | char valid; /* != 0 if the following fields are valid. */ |
| 98 | unsigned long last_updated; /* In jiffies */ |
| 99 | /* Add the read information here too */ |
| 100 | }; |
| 101 | |
| 102 | |
| 103 | Accessing the client |
| 104 | ==================== |
| 105 | |
| 106 | Let's say we have a valid client structure. At some time, we will need |
| 107 | to gather information from the client, or write new information to the |
| 108 | client. How we will export this information to user-space is less |
| 109 | important at this moment (perhaps we do not need to do this at all for |
| 110 | some obscure clients). But we need generic reading and writing routines. |
| 111 | |
| 112 | I have found it useful to define foo_read and foo_write function for this. |
| 113 | For some cases, it will be easier to call the i2c functions directly, |
| 114 | but many chips have some kind of register-value idea that can easily |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 115 | be encapsulated. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | |
| 117 | The below functions are simple examples, and should not be copied |
| 118 | literally. |
| 119 | |
| 120 | int foo_read_value(struct i2c_client *client, u8 reg) |
| 121 | { |
| 122 | if (reg < 0x10) /* byte-sized register */ |
| 123 | return i2c_smbus_read_byte_data(client,reg); |
| 124 | else /* word-sized register */ |
| 125 | return i2c_smbus_read_word_data(client,reg); |
| 126 | } |
| 127 | |
| 128 | int foo_write_value(struct i2c_client *client, u8 reg, u16 value) |
| 129 | { |
| 130 | if (reg == 0x10) /* Impossible to write - driver error! */ { |
| 131 | return -1; |
| 132 | else if (reg < 0x10) /* byte-sized register */ |
| 133 | return i2c_smbus_write_byte_data(client,reg,value); |
| 134 | else /* word-sized register */ |
| 135 | return i2c_smbus_write_word_data(client,reg,value); |
| 136 | } |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | Probing and attaching |
| 140 | ===================== |
| 141 | |
David Brownell | 4298cfc | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 142 | The Linux I2C stack was originally written to support access to hardware |
| 143 | monitoring chips on PC motherboards, and thus it embeds some assumptions |
| 144 | that are more appropriate to SMBus (and PCs) than to I2C. One of these |
| 145 | assumptions is that most adapters and devices drivers support the SMBUS_QUICK |
| 146 | protocol to probe device presence. Another is that devices and their drivers |
| 147 | can be sufficiently configured using only such probe primitives. |
| 148 | |
| 149 | As Linux and its I2C stack became more widely used in embedded systems |
| 150 | and complex components such as DVB adapters, those assumptions became more |
| 151 | problematic. Drivers for I2C devices that issue interrupts need more (and |
| 152 | different) configuration information, as do drivers handling chip variants |
| 153 | that can't be distinguished by protocol probing, or which need some board |
| 154 | specific information to operate correctly. |
| 155 | |
| 156 | Accordingly, the I2C stack now has two models for associating I2C devices |
| 157 | with their drivers: the original "legacy" model, and a newer one that's |
| 158 | fully compatible with the Linux 2.6 driver model. These models do not mix, |
| 159 | since the "legacy" model requires drivers to create "i2c_client" device |
| 160 | objects after SMBus style probing, while the Linux driver model expects |
| 161 | drivers to be given such device objects in their probe() routines. |
| 162 | |
| 163 | |
| 164 | Standard Driver Model Binding ("New Style") |
| 165 | ------------------------------------------- |
| 166 | |
| 167 | System infrastructure, typically board-specific initialization code or |
| 168 | boot firmware, reports what I2C devices exist. For example, there may be |
| 169 | a table, in the kernel or from the boot loader, identifying I2C devices |
| 170 | and linking them to board-specific configuration information about IRQs |
| 171 | and other wiring artifacts, chip type, and so on. That could be used to |
| 172 | create i2c_client objects for each I2C device. |
| 173 | |
| 174 | I2C device drivers using this binding model work just like any other |
| 175 | kind of driver in Linux: they provide a probe() method to bind to |
| 176 | those devices, and a remove() method to unbind. |
| 177 | |
Jean Delvare | d2653e9 | 2008-04-29 23:11:39 +0200 | [diff] [blame] | 178 | static int foo_probe(struct i2c_client *client, |
| 179 | const struct i2c_device_id *id); |
David Brownell | 4298cfc | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 180 | static int foo_remove(struct i2c_client *client); |
| 181 | |
| 182 | Remember that the i2c_driver does not create those client handles. The |
| 183 | handle may be used during foo_probe(). If foo_probe() reports success |
| 184 | (zero not a negative status code) it may save the handle and use it until |
| 185 | foo_remove() returns. That binding model is used by most Linux drivers. |
| 186 | |
Ben Dooks | 2260e63 | 2008-07-01 22:38:18 +0200 | [diff] [blame^] | 187 | The probe function is called when an entry in the id_table name field |
| 188 | matches the device's name. It is passed the entry that was matched so |
| 189 | the driver knows which one in the table matched. |
David Brownell | 4298cfc | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 190 | |
| 191 | |
Jean Delvare | ce9e079 | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 192 | Device Creation (Standard driver model) |
| 193 | --------------------------------------- |
| 194 | |
| 195 | If you know for a fact that an I2C device is connected to a given I2C bus, |
| 196 | you can instantiate that device by simply filling an i2c_board_info |
| 197 | structure with the device address and driver name, and calling |
| 198 | i2c_new_device(). This will create the device, then the driver core will |
| 199 | take care of finding the right driver and will call its probe() method. |
| 200 | If a driver supports different device types, you can specify the type you |
| 201 | want using the type field. You can also specify an IRQ and platform data |
| 202 | if needed. |
| 203 | |
| 204 | Sometimes you know that a device is connected to a given I2C bus, but you |
| 205 | don't know the exact address it uses. This happens on TV adapters for |
| 206 | example, where the same driver supports dozens of slightly different |
| 207 | models, and I2C device addresses change from one model to the next. In |
| 208 | that case, you can use the i2c_new_probed_device() variant, which is |
| 209 | similar to i2c_new_device(), except that it takes an additional list of |
| 210 | possible I2C addresses to probe. A device is created for the first |
| 211 | responsive address in the list. If you expect more than one device to be |
| 212 | present in the address range, simply call i2c_new_probed_device() that |
| 213 | many times. |
| 214 | |
| 215 | The call to i2c_new_device() or i2c_new_probed_device() typically happens |
| 216 | in the I2C bus driver. You may want to save the returned i2c_client |
| 217 | reference for later use. |
| 218 | |
| 219 | |
| 220 | Device Deletion (Standard driver model) |
| 221 | --------------------------------------- |
| 222 | |
| 223 | Each I2C device which has been created using i2c_new_device() or |
| 224 | i2c_new_probed_device() can be unregistered by calling |
| 225 | i2c_unregister_device(). If you don't call it explicitly, it will be |
| 226 | called automatically before the underlying I2C bus itself is removed, as a |
| 227 | device can't survive its parent in the device driver model. |
| 228 | |
| 229 | |
David Brownell | 4298cfc | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 230 | Legacy Driver Binding Model |
| 231 | --------------------------- |
| 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | Most i2c devices can be present on several i2c addresses; for some this |
| 234 | is determined in hardware (by soldering some chip pins to Vcc or Ground), |
| 235 | for others this can be changed in software (by writing to specific client |
| 236 | registers). Some devices are usually on a specific address, but not always; |
| 237 | and some are even more tricky. So you will probably need to scan several |
| 238 | i2c addresses for your clients, and do some sort of detection to see |
| 239 | whether it is actually a device supported by your driver. |
| 240 | |
| 241 | To give the user a maximum of possibilities, some default module parameters |
| 242 | are defined to help determine what addresses are scanned. Several macros |
| 243 | are defined in i2c.h to help you support them, as well as a generic |
| 244 | detection algorithm. |
| 245 | |
| 246 | You do not have to use this parameter interface; but don't try to use |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 247 | function i2c_probe() if you don't. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
David Brownell | 4298cfc | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 250 | Probing classes (Legacy model) |
| 251 | ------------------------------ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
| 253 | All parameters are given as lists of unsigned 16-bit integers. Lists are |
| 254 | terminated by I2C_CLIENT_END. |
| 255 | The following lists are used internally: |
| 256 | |
| 257 | normal_i2c: filled in by the module writer. |
| 258 | A list of I2C addresses which should normally be examined. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | probe: insmod parameter. |
| 260 | A list of pairs. The first value is a bus number (-1 for any I2C bus), |
| 261 | the second is the address. These addresses are also probed, as if they |
| 262 | were in the 'normal' list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | ignore: insmod parameter. |
| 264 | A list of pairs. The first value is a bus number (-1 for any I2C bus), |
| 265 | the second is the I2C address. These addresses are never probed. |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 266 | This parameter overrules the 'normal_i2c' list only. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | force: insmod parameter. |
| 268 | A list of pairs. The first value is a bus number (-1 for any I2C bus), |
| 269 | the second is the I2C address. A device is blindly assumed to be on |
| 270 | the given address, no probing is done. |
| 271 | |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 272 | Additionally, kind-specific force lists may optionally be defined if |
| 273 | the driver supports several chip kinds. They are grouped in a |
| 274 | NULL-terminated list of pointers named forces, those first element if the |
| 275 | generic force list mentioned above. Each additional list correspond to an |
| 276 | insmod parameter of the form force_<kind>. |
| 277 | |
Jean Delvare | b3d5496 | 2005-04-02 20:31:02 +0200 | [diff] [blame] | 278 | Fortunately, as a module writer, you just have to define the `normal_i2c' |
| 279 | parameter. The complete declaration could look like this: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | |
Jean Delvare | 2cdddeb | 2008-01-27 18:14:47 +0100 | [diff] [blame] | 281 | /* Scan 0x4c to 0x4f */ |
| 282 | static const unsigned short normal_i2c[] = { 0x4c, 0x4d, 0x4e, 0x4f, |
| 283 | I2C_CLIENT_END }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | |
| 285 | /* Magic definition of all other variables and things */ |
| 286 | I2C_CLIENT_INSMOD; |
Jean Delvare | f4b5026 | 2005-07-31 21:49:03 +0200 | [diff] [blame] | 287 | /* Or, if your driver supports, say, 2 kind of devices: */ |
| 288 | I2C_CLIENT_INSMOD_2(foo, bar); |
| 289 | |
| 290 | If you use the multi-kind form, an enum will be defined for you: |
| 291 | enum chips { any_chip, foo, bar, ... } |
| 292 | You can then (and certainly should) use it in the driver code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
Jean Delvare | b3d5496 | 2005-04-02 20:31:02 +0200 | [diff] [blame] | 294 | Note that you *have* to call the defined variable `normal_i2c', |
| 295 | without any prefix! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
| 297 | |
David Brownell | 4298cfc | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 298 | Attaching to an adapter (Legacy model) |
| 299 | -------------------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
| 301 | Whenever a new adapter is inserted, or for all adapters if the driver is |
| 302 | being registered, the callback attach_adapter() is called. Now is the |
| 303 | time to determine what devices are present on the adapter, and to register |
| 304 | a client for each of them. |
| 305 | |
| 306 | The attach_adapter callback is really easy: we just call the generic |
| 307 | detection function. This function will scan the bus for us, using the |
| 308 | information as defined in the lists explained above. If a device is |
| 309 | detected at a specific address, another callback is called. |
| 310 | |
| 311 | int foo_attach_adapter(struct i2c_adapter *adapter) |
| 312 | { |
| 313 | return i2c_probe(adapter,&addr_data,&foo_detect_client); |
| 314 | } |
| 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | Remember, structure `addr_data' is defined by the macros explained above, |
| 317 | so you do not have to define it yourself. |
| 318 | |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 319 | The i2c_probe function will call the foo_detect_client |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | function only for those i2c addresses that actually have a device on |
| 321 | them (unless a `force' parameter was used). In addition, addresses that |
| 322 | are already in use (by some other registered client) are skipped. |
| 323 | |
| 324 | |
David Brownell | 4298cfc | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 325 | The detect client function (Legacy model) |
| 326 | ----------------------------------------- |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | |
Jean Delvare | 2ed2dc3 | 2005-07-31 21:42:02 +0200 | [diff] [blame] | 328 | The detect client function is called by i2c_probe. The `kind' parameter |
| 329 | contains -1 for a probed detection, 0 for a forced detection, or a positive |
| 330 | number for a forced detection with a chip type forced. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 332 | Returning an error different from -ENODEV in a detect function will cause |
| 333 | the detection to stop: other addresses and adapters won't be scanned. |
| 334 | This should only be done on fatal or internal errors, such as a memory |
| 335 | shortage or i2c_attach_client failing. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | |
| 337 | For now, you can ignore the `flags' parameter. It is there for future use. |
| 338 | |
| 339 | int foo_detect_client(struct i2c_adapter *adapter, int address, |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 340 | int kind) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
| 342 | int err = 0; |
| 343 | int i; |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 344 | struct i2c_client *client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | struct foo_data *data; |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 346 | const char *name = ""; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | /* Let's see whether this adapter can support what we need. |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 349 | Please substitute the things you need here! */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | if (!i2c_check_functionality(adapter,I2C_FUNC_SMBUS_WORD_DATA | |
| 351 | I2C_FUNC_SMBUS_WRITE_BYTE)) |
| 352 | goto ERROR0; |
| 353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | /* OK. For now, we presume we have a valid client. We now create the |
| 355 | client structure, even though we cannot fill it completely yet. |
| 356 | But it allows us to access several i2c functions safely */ |
| 357 | |
Jean Delvare | 2445eb6 | 2005-10-17 23:16:25 +0200 | [diff] [blame] | 358 | if (!(data = kzalloc(sizeof(struct foo_data), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | err = -ENOMEM; |
| 360 | goto ERROR0; |
| 361 | } |
| 362 | |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 363 | client = &data->client; |
| 364 | i2c_set_clientdata(client, data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 366 | client->addr = address; |
| 367 | client->adapter = adapter; |
| 368 | client->driver = &foo_driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
| 370 | /* Now, we do the remaining detection. If no `force' parameter is used. */ |
| 371 | |
| 372 | /* First, the generic detection (if any), that is skipped if any force |
| 373 | parameter was used. */ |
| 374 | if (kind < 0) { |
| 375 | /* The below is of course bogus */ |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 376 | if (foo_read(client, FOO_REG_GENERIC) != FOO_GENERIC_VALUE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | goto ERROR1; |
| 378 | } |
| 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | /* Next, specific detection. This is especially important for `sensors' |
| 381 | devices. */ |
| 382 | |
| 383 | /* Determine the chip type. Not needed if a `force_CHIPTYPE' parameter |
| 384 | was used. */ |
| 385 | if (kind <= 0) { |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 386 | i = foo_read(client, FOO_REG_CHIPTYPE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | if (i == FOO_TYPE_1) |
| 388 | kind = chip1; /* As defined in the enum */ |
| 389 | else if (i == FOO_TYPE_2) |
| 390 | kind = chip2; |
| 391 | else { |
| 392 | printk("foo: Ignoring 'force' parameter for unknown chip at " |
| 393 | "adapter %d, address 0x%02x\n",i2c_adapter_id(adapter),address); |
| 394 | goto ERROR1; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /* Now set the type and chip names */ |
| 399 | if (kind == chip1) { |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 400 | name = "chip1"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } else if (kind == chip2) { |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 402 | name = "chip2"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | /* Fill in the remaining client fields. */ |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 406 | strlcpy(client->name, name, I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | data->type = kind; |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 408 | mutex_init(&data->update_lock); /* Only if you use this field */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | |
| 410 | /* Any other initializations in data must be done here too. */ |
| 411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | /* This function can write default values to the client registers, if |
| 413 | needed. */ |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 414 | foo_init_client(client); |
| 415 | |
| 416 | /* Tell the i2c layer a new client has arrived */ |
| 417 | if ((err = i2c_attach_client(client))) |
| 418 | goto ERROR1; |
| 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | return 0; |
| 421 | |
| 422 | /* OK, this is not exactly good programming practice, usually. But it is |
| 423 | very code-efficient in this case. */ |
| 424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | ERROR1: |
Jean Delvare | a852daa | 2005-11-02 21:42:48 +0100 | [diff] [blame] | 426 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | ERROR0: |
| 428 | return err; |
| 429 | } |
| 430 | |
| 431 | |
David Brownell | 4298cfc | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 432 | Removing the client (Legacy model) |
| 433 | ================================== |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
| 435 | The detach_client call back function is called when a client should be |
| 436 | removed. It may actually fail, but only when panicking. This code is |
| 437 | much simpler than the attachment code, fortunately! |
| 438 | |
| 439 | int foo_detach_client(struct i2c_client *client) |
| 440 | { |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 441 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
| 443 | /* Try to detach the client from i2c space */ |
Jean Delvare | 7bef559 | 2005-07-27 22:14:49 +0200 | [diff] [blame] | 444 | if ((err = i2c_detach_client(client))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
Jean Delvare | a852daa | 2005-11-02 21:42:48 +0100 | [diff] [blame] | 447 | kfree(i2c_get_clientdata(client)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | |
| 452 | Initializing the module or kernel |
| 453 | ================================= |
| 454 | |
| 455 | When the kernel is booted, or when your foo driver module is inserted, |
| 456 | you have to do some initializing. Fortunately, just attaching (registering) |
| 457 | the driver module is usually enough. |
| 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | static int __init foo_init(void) |
| 460 | { |
| 461 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
| 463 | if ((res = i2c_add_driver(&foo_driver))) { |
| 464 | printk("foo: Driver registration failed, module not inserted.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | return res; |
| 466 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | return 0; |
| 468 | } |
| 469 | |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 470 | static void __exit foo_cleanup(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | { |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 472 | i2c_del_driver(&foo_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /* Substitute your own name and email address */ |
| 476 | MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>" |
| 477 | MODULE_DESCRIPTION("Driver for Barf Inc. Foo I2C devices"); |
| 478 | |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 479 | /* a few non-GPL license types are also allowed */ |
| 480 | MODULE_LICENSE("GPL"); |
| 481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | module_init(foo_init); |
| 483 | module_exit(foo_cleanup); |
| 484 | |
| 485 | Note that some functions are marked by `__init', and some data structures |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 486 | by `__initdata'. These functions and structures can be removed after |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | kernel booting (or module loading) is completed. |
| 488 | |
Jean Delvare | fb687d7 | 2005-12-18 16:51:55 +0100 | [diff] [blame] | 489 | |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 490 | Power Management |
| 491 | ================ |
| 492 | |
| 493 | If your I2C device needs special handling when entering a system low |
| 494 | power state -- like putting a transceiver into a low power mode, or |
| 495 | activating a system wakeup mechanism -- do that in the suspend() method. |
| 496 | The resume() method should reverse what the suspend() method does. |
| 497 | |
| 498 | These are standard driver model calls, and they work just like they |
| 499 | would for any other driver stack. The calls can sleep, and can use |
| 500 | I2C messaging to the device being suspended or resumed (since their |
| 501 | parent I2C adapter is active when these calls are issued, and IRQs |
| 502 | are still enabled). |
| 503 | |
| 504 | |
| 505 | System Shutdown |
| 506 | =============== |
| 507 | |
| 508 | If your I2C device needs special handling when the system shuts down |
| 509 | or reboots (including kexec) -- like turning something off -- use a |
| 510 | shutdown() method. |
| 511 | |
| 512 | Again, this is a standard driver model call, working just like it |
| 513 | would for any other driver stack: the calls can sleep, and can use |
| 514 | I2C messaging. |
| 515 | |
| 516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | Command function |
| 518 | ================ |
| 519 | |
| 520 | A generic ioctl-like function call back is supported. You will seldom |
Jean Delvare | fb687d7 | 2005-12-18 16:51:55 +0100 | [diff] [blame] | 521 | need this, and its use is deprecated anyway, so newer design should not |
| 522 | use it. Set it to NULL. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
| 524 | |
| 525 | Sending and receiving |
| 526 | ===================== |
| 527 | |
| 528 | If you want to communicate with your device, there are several functions |
| 529 | to do this. You can find all of them in i2c.h. |
| 530 | |
| 531 | If you can choose between plain i2c communication and SMBus level |
| 532 | communication, please use the last. All adapters understand SMBus level |
| 533 | commands, but only some of them understand plain i2c! |
| 534 | |
| 535 | |
| 536 | Plain i2c communication |
| 537 | ----------------------- |
| 538 | |
| 539 | extern int i2c_master_send(struct i2c_client *,const char* ,int); |
| 540 | extern int i2c_master_recv(struct i2c_client *,char* ,int); |
| 541 | |
| 542 | These routines read and write some bytes from/to a client. The client |
| 543 | contains the i2c address, so you do not have to include it. The second |
| 544 | parameter contains the bytes the read/write, the third the length of the |
| 545 | buffer. Returned is the actual number of bytes read/written. |
| 546 | |
| 547 | extern int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msg, |
| 548 | int num); |
| 549 | |
| 550 | This sends a series of messages. Each message can be a read or write, |
| 551 | and they can be mixed in any way. The transactions are combined: no |
| 552 | stop bit is sent between transaction. The i2c_msg structure contains |
| 553 | for each message the client address, the number of bytes of the message |
| 554 | and the message data itself. |
| 555 | |
| 556 | You can read the file `i2c-protocol' for more information about the |
| 557 | actual i2c protocol. |
| 558 | |
| 559 | |
| 560 | SMBus communication |
| 561 | ------------------- |
| 562 | |
| 563 | extern s32 i2c_smbus_xfer (struct i2c_adapter * adapter, u16 addr, |
| 564 | unsigned short flags, |
| 565 | char read_write, u8 command, int size, |
| 566 | union i2c_smbus_data * data); |
| 567 | |
| 568 | This is the generic SMBus function. All functions below are implemented |
| 569 | in terms of it. Never use this function directly! |
| 570 | |
| 571 | |
| 572 | extern s32 i2c_smbus_write_quick(struct i2c_client * client, u8 value); |
| 573 | extern s32 i2c_smbus_read_byte(struct i2c_client * client); |
| 574 | extern s32 i2c_smbus_write_byte(struct i2c_client * client, u8 value); |
| 575 | extern s32 i2c_smbus_read_byte_data(struct i2c_client * client, u8 command); |
| 576 | extern s32 i2c_smbus_write_byte_data(struct i2c_client * client, |
| 577 | u8 command, u8 value); |
| 578 | extern s32 i2c_smbus_read_word_data(struct i2c_client * client, u8 command); |
| 579 | extern s32 i2c_smbus_write_word_data(struct i2c_client * client, |
| 580 | u8 command, u16 value); |
| 581 | extern s32 i2c_smbus_write_block_data(struct i2c_client * client, |
| 582 | u8 command, u8 length, |
| 583 | u8 *values); |
Jean Delvare | 7865e24 | 2005-10-08 00:00:31 +0200 | [diff] [blame] | 584 | extern s32 i2c_smbus_read_i2c_block_data(struct i2c_client * client, |
Jean Delvare | 4b2643d | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 585 | u8 command, u8 length, u8 *values); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
| 587 | These ones were removed in Linux 2.6.10 because they had no users, but could |
| 588 | be added back later if needed: |
| 589 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | extern s32 i2c_smbus_read_block_data(struct i2c_client * client, |
| 591 | u8 command, u8 *values); |
| 592 | extern s32 i2c_smbus_write_i2c_block_data(struct i2c_client * client, |
| 593 | u8 command, u8 length, |
| 594 | u8 *values); |
| 595 | extern s32 i2c_smbus_process_call(struct i2c_client * client, |
| 596 | u8 command, u16 value); |
| 597 | extern s32 i2c_smbus_block_process_call(struct i2c_client *client, |
| 598 | u8 command, u8 length, |
| 599 | u8 *values) |
| 600 | |
| 601 | All these transactions return -1 on failure. The 'write' transactions |
| 602 | return 0 on success; the 'read' transactions return the read value, except |
| 603 | for read_block, which returns the number of values read. The block buffers |
| 604 | need not be longer than 32 bytes. |
| 605 | |
| 606 | You can read the file `smbus-protocol' for more information about the |
| 607 | actual SMBus protocol. |
| 608 | |
| 609 | |
| 610 | General purpose routines |
| 611 | ======================== |
| 612 | |
| 613 | Below all general purpose routines are listed, that were not mentioned |
| 614 | before. |
| 615 | |
Jean Delvare | eefcd75 | 2007-05-01 23:26:35 +0200 | [diff] [blame] | 616 | /* This call returns a unique low identifier for each registered adapter. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | */ |
| 618 | extern int i2c_adapter_id(struct i2c_adapter *adap); |
| 619 | |