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