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