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