Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* i2c-core.c - a device driver for the iic-bus interface */ |
| 2 | /* ------------------------------------------------------------------------- */ |
| 3 | /* Copyright (C) 1995-99 Simon G. Vogl |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 18 | /* ------------------------------------------------------------------------- */ |
| 19 | |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 20 | /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl> |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 22 | SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and |
| 23 | Jean Delvare <khali@linux-fr.org> */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/errno.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/i2c.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/idr.h> |
| 32 | #include <linux/seq_file.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 33 | #include <linux/platform_device.h> |
Arjan van de Ven | b3585e4 | 2006-01-11 10:50:26 +0100 | [diff] [blame] | 34 | #include <linux/mutex.h> |
Jean Delvare | b8d6f45 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 35 | #include <linux/completion.h> |
Mike Rapoport | cea443a8 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 36 | #include <linux/hardirq.h> |
| 37 | #include <linux/irqflags.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <asm/uaccess.h> |
Jean Delvare | 87c6c22 | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 39 | #include <asm/semaphore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 41 | #include "i2c-core.h" |
| 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 44 | static DEFINE_MUTEX(core_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | static DEFINE_IDR(i2c_adapter_idr); |
| 46 | |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 47 | #define is_newstyle_driver(d) ((d)->probe || (d)->remove) |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 48 | |
| 49 | /* ------------------------------------------------------------------------- */ |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | static int i2c_device_match(struct device *dev, struct device_driver *drv) |
| 52 | { |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 53 | struct i2c_client *client = to_i2c_client(dev); |
| 54 | struct i2c_driver *driver = to_i2c_driver(drv); |
| 55 | |
| 56 | /* make legacy i2c drivers bypass driver model probing entirely; |
| 57 | * such drivers scan each i2c adapter/bus themselves. |
| 58 | */ |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 59 | if (!is_newstyle_driver(driver)) |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 60 | return 0; |
| 61 | |
| 62 | /* new style drivers use the same kind of driver matching policy |
| 63 | * as platform devices or SPI: compare device and driver IDs. |
| 64 | */ |
| 65 | return strcmp(client->driver_name, drv->name) == 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | } |
| 67 | |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 68 | #ifdef CONFIG_HOTPLUG |
| 69 | |
| 70 | /* uevent helps with hotplug: modprobe -q $(MODALIAS) */ |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 71 | static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env) |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 72 | { |
| 73 | struct i2c_client *client = to_i2c_client(dev); |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 74 | |
| 75 | /* by definition, legacy drivers can't hotplug */ |
| 76 | if (dev->driver || !client->driver_name) |
| 77 | return 0; |
| 78 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 79 | if (add_uevent_var(env, "MODALIAS=%s", client->driver_name)) |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 80 | return -ENOMEM; |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 81 | dev_dbg(dev, "uevent\n"); |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | #else |
| 86 | #define i2c_device_uevent NULL |
| 87 | #endif /* CONFIG_HOTPLUG */ |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | static int i2c_device_probe(struct device *dev) |
| 90 | { |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 91 | struct i2c_client *client = to_i2c_client(dev); |
| 92 | struct i2c_driver *driver = to_i2c_driver(dev->driver); |
| 93 | |
| 94 | if (!driver->probe) |
| 95 | return -ENODEV; |
| 96 | client->driver = driver; |
| 97 | dev_dbg(dev, "probe\n"); |
| 98 | return driver->probe(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static int i2c_device_remove(struct device *dev) |
| 102 | { |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 103 | struct i2c_client *client = to_i2c_client(dev); |
| 104 | struct i2c_driver *driver; |
| 105 | int status; |
| 106 | |
| 107 | if (!dev->driver) |
| 108 | return 0; |
| 109 | |
| 110 | driver = to_i2c_driver(dev->driver); |
| 111 | if (driver->remove) { |
| 112 | dev_dbg(dev, "remove\n"); |
| 113 | status = driver->remove(client); |
| 114 | } else { |
| 115 | dev->driver = NULL; |
| 116 | status = 0; |
| 117 | } |
| 118 | if (status == 0) |
| 119 | client->driver = NULL; |
| 120 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | } |
| 122 | |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 123 | static void i2c_device_shutdown(struct device *dev) |
| 124 | { |
| 125 | struct i2c_driver *driver; |
| 126 | |
| 127 | if (!dev->driver) |
| 128 | return; |
| 129 | driver = to_i2c_driver(dev->driver); |
| 130 | if (driver->shutdown) |
| 131 | driver->shutdown(to_i2c_client(dev)); |
| 132 | } |
| 133 | |
| 134 | static int i2c_device_suspend(struct device * dev, pm_message_t mesg) |
| 135 | { |
| 136 | struct i2c_driver *driver; |
| 137 | |
| 138 | if (!dev->driver) |
| 139 | return 0; |
| 140 | driver = to_i2c_driver(dev->driver); |
| 141 | if (!driver->suspend) |
| 142 | return 0; |
| 143 | return driver->suspend(to_i2c_client(dev), mesg); |
| 144 | } |
| 145 | |
| 146 | static int i2c_device_resume(struct device * dev) |
| 147 | { |
| 148 | struct i2c_driver *driver; |
| 149 | |
| 150 | if (!dev->driver) |
| 151 | return 0; |
| 152 | driver = to_i2c_driver(dev->driver); |
| 153 | if (!driver->resume) |
| 154 | return 0; |
| 155 | return driver->resume(to_i2c_client(dev)); |
| 156 | } |
| 157 | |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 158 | static void i2c_client_release(struct device *dev) |
| 159 | { |
| 160 | struct i2c_client *client = to_i2c_client(dev); |
| 161 | complete(&client->released); |
| 162 | } |
| 163 | |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 164 | static void i2c_client_dev_release(struct device *dev) |
| 165 | { |
| 166 | kfree(to_i2c_client(dev)); |
| 167 | } |
| 168 | |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 169 | static ssize_t show_client_name(struct device *dev, struct device_attribute *attr, char *buf) |
| 170 | { |
| 171 | struct i2c_client *client = to_i2c_client(dev); |
| 172 | return sprintf(buf, "%s\n", client->name); |
| 173 | } |
| 174 | |
| 175 | static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf) |
| 176 | { |
| 177 | struct i2c_client *client = to_i2c_client(dev); |
| 178 | return client->driver_name |
| 179 | ? sprintf(buf, "%s\n", client->driver_name) |
| 180 | : 0; |
| 181 | } |
| 182 | |
| 183 | static struct device_attribute i2c_dev_attrs[] = { |
| 184 | __ATTR(name, S_IRUGO, show_client_name, NULL), |
| 185 | /* modalias helps coldplug: modprobe $(cat .../modalias) */ |
| 186 | __ATTR(modalias, S_IRUGO, show_modalias, NULL), |
| 187 | { }, |
| 188 | }; |
| 189 | |
Adrian Bunk | 83eaaed | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 190 | static struct bus_type i2c_bus_type = { |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 191 | .name = "i2c", |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 192 | .dev_attrs = i2c_dev_attrs, |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 193 | .match = i2c_device_match, |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 194 | .uevent = i2c_device_uevent, |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 195 | .probe = i2c_device_probe, |
| 196 | .remove = i2c_device_remove, |
| 197 | .shutdown = i2c_device_shutdown, |
| 198 | .suspend = i2c_device_suspend, |
| 199 | .resume = i2c_device_resume, |
Russell King | b864c7d | 2006-01-05 14:37:50 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | * i2c_verify_client - return parameter as i2c_client, or NULL |
| 205 | * @dev: device, probably from some driver model iterator |
| 206 | * |
| 207 | * When traversing the driver model tree, perhaps using driver model |
| 208 | * iterators like @device_for_each_child(), you can't assume very much |
| 209 | * about the nodes you find. Use this function to avoid oopses caused |
| 210 | * by wrongly treating some non-I2C device as an i2c_client. |
| 211 | */ |
| 212 | struct i2c_client *i2c_verify_client(struct device *dev) |
| 213 | { |
| 214 | return (dev->bus == &i2c_bus_type) |
| 215 | ? to_i2c_client(dev) |
| 216 | : NULL; |
| 217 | } |
| 218 | EXPORT_SYMBOL(i2c_verify_client); |
| 219 | |
| 220 | |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 221 | /** |
| 222 | * i2c_new_device - instantiate an i2c device for use with a new style driver |
| 223 | * @adap: the adapter managing the device |
| 224 | * @info: describes one I2C device; bus_num is ignored |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 225 | * Context: can sleep |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 226 | * |
| 227 | * Create a device to work with a new style i2c driver, where binding is |
| 228 | * handled through driver model probe()/remove() methods. This call is not |
| 229 | * appropriate for use by mainboad initialization logic, which usually runs |
| 230 | * during an arch_initcall() long before any i2c_adapter could exist. |
| 231 | * |
| 232 | * This returns the new i2c client, which may be saved for later use with |
| 233 | * i2c_unregister_device(); or NULL to indicate an error. |
| 234 | */ |
| 235 | struct i2c_client * |
| 236 | i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info) |
| 237 | { |
| 238 | struct i2c_client *client; |
| 239 | int status; |
| 240 | |
| 241 | client = kzalloc(sizeof *client, GFP_KERNEL); |
| 242 | if (!client) |
| 243 | return NULL; |
| 244 | |
| 245 | client->adapter = adap; |
| 246 | |
| 247 | client->dev.platform_data = info->platform_data; |
David Brownell | 3bbb835 | 2007-10-13 23:56:29 +0200 | [diff] [blame] | 248 | device_init_wakeup(&client->dev, info->flags & I2C_CLIENT_WAKE); |
| 249 | |
| 250 | client->flags = info->flags & ~I2C_CLIENT_WAKE; |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 251 | client->addr = info->addr; |
| 252 | client->irq = info->irq; |
| 253 | |
| 254 | strlcpy(client->driver_name, info->driver_name, |
| 255 | sizeof(client->driver_name)); |
| 256 | strlcpy(client->name, info->type, sizeof(client->name)); |
| 257 | |
| 258 | /* a new style driver may be bound to this device when we |
| 259 | * return from this function, or any later moment (e.g. maybe |
| 260 | * hotplugging will load the driver module). and the device |
| 261 | * refcount model is the standard driver model one. |
| 262 | */ |
| 263 | status = i2c_attach_client(client); |
| 264 | if (status < 0) { |
| 265 | kfree(client); |
| 266 | client = NULL; |
| 267 | } |
| 268 | return client; |
| 269 | } |
| 270 | EXPORT_SYMBOL_GPL(i2c_new_device); |
| 271 | |
| 272 | |
| 273 | /** |
| 274 | * i2c_unregister_device - reverse effect of i2c_new_device() |
| 275 | * @client: value returned from i2c_new_device() |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 276 | * Context: can sleep |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 277 | */ |
| 278 | void i2c_unregister_device(struct i2c_client *client) |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 279 | { |
| 280 | struct i2c_adapter *adapter = client->adapter; |
| 281 | struct i2c_driver *driver = client->driver; |
| 282 | |
| 283 | if (driver && !is_newstyle_driver(driver)) { |
| 284 | dev_err(&client->dev, "can't unregister devices " |
| 285 | "with legacy drivers\n"); |
| 286 | WARN_ON(1); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | mutex_lock(&adapter->clist_lock); |
| 291 | list_del(&client->list); |
| 292 | mutex_unlock(&adapter->clist_lock); |
| 293 | |
| 294 | device_unregister(&client->dev); |
| 295 | } |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 296 | EXPORT_SYMBOL_GPL(i2c_unregister_device); |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 297 | |
| 298 | |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 299 | static int dummy_nop(struct i2c_client *client) |
| 300 | { |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static struct i2c_driver dummy_driver = { |
| 305 | .driver.name = "dummy", |
| 306 | .probe = dummy_nop, |
| 307 | .remove = dummy_nop, |
| 308 | }; |
| 309 | |
| 310 | /** |
| 311 | * i2c_new_dummy - return a new i2c device bound to a dummy driver |
| 312 | * @adapter: the adapter managing the device |
| 313 | * @address: seven bit address to be used |
| 314 | * @type: optional label used for i2c_client.name |
| 315 | * Context: can sleep |
| 316 | * |
| 317 | * This returns an I2C client bound to the "dummy" driver, intended for use |
| 318 | * with devices that consume multiple addresses. Examples of such chips |
| 319 | * include various EEPROMS (like 24c04 and 24c08 models). |
| 320 | * |
| 321 | * These dummy devices have two main uses. First, most I2C and SMBus calls |
| 322 | * except i2c_transfer() need a client handle; the dummy will be that handle. |
| 323 | * And second, this prevents the specified address from being bound to a |
| 324 | * different driver. |
| 325 | * |
| 326 | * This returns the new i2c client, which should be saved for later use with |
| 327 | * i2c_unregister_device(); or NULL to indicate an error. |
| 328 | */ |
| 329 | struct i2c_client * |
| 330 | i2c_new_dummy(struct i2c_adapter *adapter, u16 address, const char *type) |
| 331 | { |
| 332 | struct i2c_board_info info = { |
| 333 | .driver_name = "dummy", |
| 334 | .addr = address, |
| 335 | }; |
| 336 | |
| 337 | if (type) |
| 338 | strlcpy(info.type, type, sizeof info.type); |
| 339 | return i2c_new_device(adapter, &info); |
| 340 | } |
| 341 | EXPORT_SYMBOL_GPL(i2c_new_dummy); |
| 342 | |
David Brownell | f37dd80 | 2007-02-13 22:09:00 +0100 | [diff] [blame] | 343 | /* ------------------------------------------------------------------------- */ |
| 344 | |
David Brownell | 16ffadf | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 345 | /* I2C bus adapters -- one roots each I2C or SMBUS segment */ |
| 346 | |
Adrian Bunk | 83eaaed | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 347 | static void i2c_adapter_dev_release(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
David Brownell | ef2c8321 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 349 | struct i2c_adapter *adap = to_i2c_adapter(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | complete(&adap->dev_released); |
| 351 | } |
| 352 | |
David Brownell | 16ffadf | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 353 | static ssize_t |
| 354 | show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { |
David Brownell | ef2c8321 | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 356 | struct i2c_adapter *adap = to_i2c_adapter(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | return sprintf(buf, "%s\n", adap->name); |
| 358 | } |
David Brownell | 16ffadf | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 359 | |
| 360 | static struct device_attribute i2c_adapter_attrs[] = { |
| 361 | __ATTR(name, S_IRUGO, show_adapter_name, NULL), |
| 362 | { }, |
| 363 | }; |
| 364 | |
Adrian Bunk | 83eaaed | 2007-10-13 23:56:30 +0200 | [diff] [blame] | 365 | static struct class i2c_adapter_class = { |
David Brownell | 16ffadf | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 366 | .owner = THIS_MODULE, |
| 367 | .name = "i2c-adapter", |
| 368 | .dev_attrs = i2c_adapter_attrs, |
| 369 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 371 | static void i2c_scan_static_board_info(struct i2c_adapter *adapter) |
| 372 | { |
| 373 | struct i2c_devinfo *devinfo; |
| 374 | |
| 375 | mutex_lock(&__i2c_board_lock); |
| 376 | list_for_each_entry(devinfo, &__i2c_board_list, list) { |
| 377 | if (devinfo->busnum == adapter->nr |
| 378 | && !i2c_new_device(adapter, |
| 379 | &devinfo->board_info)) |
| 380 | printk(KERN_ERR "i2c-core: can't create i2c%d-%04x\n", |
| 381 | i2c_adapter_id(adapter), |
| 382 | devinfo->board_info.addr); |
| 383 | } |
| 384 | mutex_unlock(&__i2c_board_lock); |
| 385 | } |
| 386 | |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 387 | static int i2c_do_add_adapter(struct device_driver *d, void *data) |
| 388 | { |
| 389 | struct i2c_driver *driver = to_i2c_driver(d); |
| 390 | struct i2c_adapter *adap = data; |
| 391 | |
| 392 | if (driver->attach_adapter) { |
| 393 | /* We ignore the return code; if it fails, too bad */ |
| 394 | driver->attach_adapter(adap); |
| 395 | } |
| 396 | return 0; |
| 397 | } |
| 398 | |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 399 | static int i2c_register_adapter(struct i2c_adapter *adap) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | { |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 401 | int res = 0, dummy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 403 | mutex_init(&adap->bus_lock); |
| 404 | mutex_init(&adap->clist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | INIT_LIST_HEAD(&adap->clients); |
| 406 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 407 | mutex_lock(&core_lock); |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | /* Add the adapter to the driver core. |
| 410 | * If the parent pointer is not set up, |
| 411 | * we add this adapter to the host bus. |
| 412 | */ |
David Brownell | b119dc3 | 2007-01-04 13:07:04 +0100 | [diff] [blame] | 413 | if (adap->dev.parent == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | adap->dev.parent = &platform_bus; |
Jean Delvare | fe2c8d5 | 2007-02-13 22:09:04 +0100 | [diff] [blame] | 415 | pr_debug("I2C adapter driver [%s] forgot to specify " |
| 416 | "physical device\n", adap->name); |
David Brownell | b119dc3 | 2007-01-04 13:07:04 +0100 | [diff] [blame] | 417 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | sprintf(adap->dev.bus_id, "i2c-%d", adap->nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | adap->dev.release = &i2c_adapter_dev_release; |
Jean Delvare | fccb56e | 2007-05-01 23:26:27 +0200 | [diff] [blame] | 420 | adap->dev.class = &i2c_adapter_class; |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 421 | res = device_register(&adap->dev); |
| 422 | if (res) |
| 423 | goto out_list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 425 | dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); |
| 426 | |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 427 | /* create pre-declared device nodes for new-style drivers */ |
| 428 | if (adap->nr < __i2c_first_dynamic_bus_num) |
| 429 | i2c_scan_static_board_info(adap); |
| 430 | |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 431 | /* let legacy drivers scan this bus for matching devices */ |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 432 | dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap, |
| 433 | i2c_do_add_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | out_unlock: |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 436 | mutex_unlock(&core_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | return res; |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 438 | |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 439 | out_list: |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 440 | idr_remove(&i2c_adapter_idr, adap->nr); |
| 441 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } |
| 443 | |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 444 | /** |
| 445 | * i2c_add_adapter - declare i2c adapter, use dynamic bus number |
| 446 | * @adapter: the adapter to add |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 447 | * Context: can sleep |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 448 | * |
| 449 | * This routine is used to declare an I2C adapter when its bus number |
| 450 | * doesn't matter. Examples: for I2C adapters dynamically added by |
| 451 | * USB links or PCI plugin cards. |
| 452 | * |
| 453 | * When this returns zero, a new bus number was allocated and stored |
| 454 | * in adap->nr, and the specified adapter became available for clients. |
| 455 | * Otherwise, a negative errno value is returned. |
| 456 | */ |
| 457 | int i2c_add_adapter(struct i2c_adapter *adapter) |
| 458 | { |
| 459 | int id, res = 0; |
| 460 | |
| 461 | retry: |
| 462 | if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) |
| 463 | return -ENOMEM; |
| 464 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 465 | mutex_lock(&core_lock); |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 466 | /* "above" here means "above or equal to", sigh */ |
| 467 | res = idr_get_new_above(&i2c_adapter_idr, adapter, |
| 468 | __i2c_first_dynamic_bus_num, &id); |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 469 | mutex_unlock(&core_lock); |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 470 | |
| 471 | if (res < 0) { |
| 472 | if (res == -EAGAIN) |
| 473 | goto retry; |
| 474 | return res; |
| 475 | } |
| 476 | |
| 477 | adapter->nr = id; |
| 478 | return i2c_register_adapter(adapter); |
| 479 | } |
| 480 | EXPORT_SYMBOL(i2c_add_adapter); |
| 481 | |
| 482 | /** |
| 483 | * i2c_add_numbered_adapter - declare i2c adapter, use static bus number |
| 484 | * @adap: the adapter to register (with adap->nr initialized) |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 485 | * Context: can sleep |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 486 | * |
| 487 | * This routine is used to declare an I2C adapter when its bus number |
| 488 | * matters. Example: for I2C adapters from system-on-chip CPUs, or |
| 489 | * otherwise built in to the system's mainboard, and where i2c_board_info |
| 490 | * is used to properly configure I2C devices. |
| 491 | * |
| 492 | * If no devices have pre-been declared for this bus, then be sure to |
| 493 | * register the adapter before any dynamically allocated ones. Otherwise |
| 494 | * the required bus ID may not be available. |
| 495 | * |
| 496 | * When this returns zero, the specified adapter became available for |
| 497 | * clients using the bus number provided in adap->nr. Also, the table |
| 498 | * of I2C devices pre-declared using i2c_register_board_info() is scanned, |
| 499 | * and the appropriate driver model device nodes are created. Otherwise, a |
| 500 | * negative errno value is returned. |
| 501 | */ |
| 502 | int i2c_add_numbered_adapter(struct i2c_adapter *adap) |
| 503 | { |
| 504 | int id; |
| 505 | int status; |
| 506 | |
| 507 | if (adap->nr & ~MAX_ID_MASK) |
| 508 | return -EINVAL; |
| 509 | |
| 510 | retry: |
| 511 | if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0) |
| 512 | return -ENOMEM; |
| 513 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 514 | mutex_lock(&core_lock); |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 515 | /* "above" here means "above or equal to", sigh; |
| 516 | * we need the "equal to" result to force the result |
| 517 | */ |
| 518 | status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id); |
| 519 | if (status == 0 && id != adap->nr) { |
| 520 | status = -EBUSY; |
| 521 | idr_remove(&i2c_adapter_idr, id); |
| 522 | } |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 523 | mutex_unlock(&core_lock); |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 524 | if (status == -EAGAIN) |
| 525 | goto retry; |
| 526 | |
| 527 | if (status == 0) |
| 528 | status = i2c_register_adapter(adap); |
| 529 | return status; |
| 530 | } |
| 531 | EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); |
| 532 | |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 533 | static int i2c_do_del_adapter(struct device_driver *d, void *data) |
| 534 | { |
| 535 | struct i2c_driver *driver = to_i2c_driver(d); |
| 536 | struct i2c_adapter *adapter = data; |
| 537 | int res; |
| 538 | |
| 539 | if (!driver->detach_adapter) |
| 540 | return 0; |
| 541 | res = driver->detach_adapter(adapter); |
| 542 | if (res) |
| 543 | dev_err(&adapter->dev, "detach_adapter failed (%d) " |
| 544 | "for driver [%s]\n", res, driver->driver.name); |
| 545 | return res; |
| 546 | } |
| 547 | |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 548 | /** |
| 549 | * i2c_del_adapter - unregister I2C adapter |
| 550 | * @adap: the adapter being unregistered |
| 551 | * Context: can sleep |
| 552 | * |
| 553 | * This unregisters an I2C adapter which was previously registered |
| 554 | * by @i2c_add_adapter or @i2c_add_numbered_adapter. |
| 555 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | int i2c_del_adapter(struct i2c_adapter *adap) |
| 557 | { |
| 558 | struct list_head *item, *_n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | struct i2c_client *client; |
| 560 | int res = 0; |
| 561 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 562 | mutex_lock(&core_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
| 564 | /* First make sure that this adapter was ever added */ |
Jean Delvare | 87c6c22 | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 565 | if (idr_find(&i2c_adapter_idr, adap->nr) != adap) { |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 566 | pr_debug("i2c-core: attempting to delete unregistered " |
| 567 | "adapter [%s]\n", adap->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | res = -EINVAL; |
| 569 | goto out_unlock; |
| 570 | } |
| 571 | |
Jean Delvare | 026526f | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 572 | /* Tell drivers about this removal */ |
| 573 | res = bus_for_each_drv(&i2c_bus_type, NULL, adap, |
| 574 | i2c_do_del_adapter); |
| 575 | if (res) |
| 576 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
| 578 | /* detach any active clients. This must be done first, because |
Tobias Klauser | a551acc | 2005-05-19 21:40:38 +0200 | [diff] [blame] | 579 | * it can fail; in which case we give up. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | list_for_each_safe(item, _n, &adap->clients) { |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 581 | struct i2c_driver *driver; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 583 | client = list_entry(item, struct i2c_client, list); |
| 584 | driver = client->driver; |
| 585 | |
| 586 | /* new style, follow standard driver model */ |
| 587 | if (!driver || is_newstyle_driver(driver)) { |
| 588 | i2c_unregister_device(client); |
| 589 | continue; |
| 590 | } |
| 591 | |
| 592 | /* legacy drivers create and remove clients themselves */ |
| 593 | if ((res = driver->detach_client(client))) { |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 594 | dev_err(&adap->dev, "detach_client failed for client " |
| 595 | "[%s] at address 0x%02x\n", client->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | client->addr); |
| 597 | goto out_unlock; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | /* clean up the sysfs representation */ |
| 602 | init_completion(&adap->dev_released); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | device_unregister(&adap->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
| 605 | /* wait for sysfs to drop all references */ |
| 606 | wait_for_completion(&adap->dev_released); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 608 | /* free bus id */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | idr_remove(&i2c_adapter_idr, adap->nr); |
| 610 | |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 611 | dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | |
| 613 | out_unlock: |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 614 | mutex_unlock(&core_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | return res; |
| 616 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 617 | EXPORT_SYMBOL(i2c_del_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
| 619 | |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 620 | /* ------------------------------------------------------------------------- */ |
| 621 | |
| 622 | /* |
| 623 | * An i2c_driver is used with one or more i2c_client (device) nodes to access |
| 624 | * i2c slave chips, on a bus instance associated with some i2c_adapter. There |
| 625 | * are two models for binding the driver to its device: "new style" drivers |
| 626 | * follow the standard Linux driver model and just respond to probe() calls |
| 627 | * issued if the driver core sees they match(); "legacy" drivers create device |
| 628 | * nodes themselves. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | */ |
| 630 | |
Greg Kroah-Hartman | de59cf9 | 2005-12-06 15:33:15 -0800 | [diff] [blame] | 631 | int i2c_register_driver(struct module *owner, struct i2c_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | { |
Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 633 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 635 | /* new style driver methods can't mix with legacy ones */ |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 636 | if (is_newstyle_driver(driver)) { |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 637 | if (driver->attach_adapter || driver->detach_adapter |
| 638 | || driver->detach_client) { |
| 639 | printk(KERN_WARNING |
| 640 | "i2c-core: driver [%s] is confused\n", |
| 641 | driver->driver.name); |
| 642 | return -EINVAL; |
| 643 | } |
| 644 | } |
| 645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | /* add the driver to the list of i2c drivers in the driver core */ |
Greg Kroah-Hartman | de59cf9 | 2005-12-06 15:33:15 -0800 | [diff] [blame] | 647 | driver->driver.owner = owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | driver->driver.bus = &i2c_bus_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
David Brownell | 6e13e64 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 650 | /* for new style drivers, when registration returns the driver core |
| 651 | * will have called probe() for all matching-but-unbound devices. |
| 652 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | res = driver_register(&driver->driver); |
| 654 | if (res) |
Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 655 | return res; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 656 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 657 | mutex_lock(&core_lock); |
Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 658 | |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 659 | pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 661 | /* legacy drivers scan i2c busses directly */ |
Jean Delvare | 8a99475 | 2005-11-26 20:28:06 +0100 | [diff] [blame] | 662 | if (driver->attach_adapter) { |
David Brownell | 4ad4eac | 2007-05-01 23:26:28 +0200 | [diff] [blame] | 663 | struct i2c_adapter *adapter; |
| 664 | |
Jean Delvare | 87c6c22 | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 665 | down(&i2c_adapter_class.sem); |
| 666 | list_for_each_entry(adapter, &i2c_adapter_class.devices, |
| 667 | dev.node) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | driver->attach_adapter(adapter); |
| 669 | } |
Jean Delvare | 87c6c22 | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 670 | up(&i2c_adapter_class.sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | } |
| 672 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 673 | mutex_unlock(&core_lock); |
Jean Delvare | 7eebcb7 | 2006-02-05 23:28:21 +0100 | [diff] [blame] | 674 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | } |
Greg Kroah-Hartman | de59cf9 | 2005-12-06 15:33:15 -0800 | [diff] [blame] | 676 | EXPORT_SYMBOL(i2c_register_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 678 | /** |
| 679 | * i2c_del_driver - unregister I2C driver |
| 680 | * @driver: the driver being unregistered |
David Brownell | d64f73b | 2007-07-12 14:12:28 +0200 | [diff] [blame] | 681 | * Context: can sleep |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 682 | */ |
Jean Delvare | b3e8209 | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 683 | void i2c_del_driver(struct i2c_driver *driver) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | { |
Jean Delvare | 87c6c22 | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 685 | struct list_head *item2, *_n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | struct i2c_client *client; |
| 687 | struct i2c_adapter *adap; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 688 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 689 | mutex_lock(&core_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 691 | /* new-style driver? */ |
| 692 | if (is_newstyle_driver(driver)) |
| 693 | goto unregister; |
| 694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | /* Have a look at each adapter, if clients of this driver are still |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 696 | * attached. If so, detach them to be able to kill the driver |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | * afterwards. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | */ |
Jean Delvare | 87c6c22 | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 699 | down(&i2c_adapter_class.sem); |
| 700 | list_for_each_entry(adap, &i2c_adapter_class.devices, dev.node) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | if (driver->detach_adapter) { |
Jean Delvare | b3e8209 | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 702 | if (driver->detach_adapter(adap)) { |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 703 | dev_err(&adap->dev, "detach_adapter failed " |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 704 | "for driver [%s]\n", |
| 705 | driver->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | } |
| 707 | } else { |
| 708 | list_for_each_safe(item2, _n, &adap->clients) { |
| 709 | client = list_entry(item2, struct i2c_client, list); |
| 710 | if (client->driver != driver) |
| 711 | continue; |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 712 | dev_dbg(&adap->dev, "detaching client [%s] " |
| 713 | "at 0x%02x\n", client->name, |
| 714 | client->addr); |
Jean Delvare | b3e8209 | 2007-05-01 23:26:32 +0200 | [diff] [blame] | 715 | if (driver->detach_client(client)) { |
Jean Delvare | b6d7b3d | 2005-07-31 19:02:53 +0200 | [diff] [blame] | 716 | dev_err(&adap->dev, "detach_client " |
| 717 | "failed for client [%s] at " |
| 718 | "0x%02x\n", client->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | client->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | } |
| 723 | } |
Jean Delvare | 87c6c22 | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 724 | up(&i2c_adapter_class.sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | |
David Brownell | a1d9e6e | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 726 | unregister: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | driver_unregister(&driver->driver); |
Laurent Riffard | 35d8b2e | 2005-11-26 20:34:05 +0100 | [diff] [blame] | 728 | pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 730 | mutex_unlock(&core_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 732 | EXPORT_SYMBOL(i2c_del_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
David Brownell | 7b4fbc5 | 2007-05-01 23:26:30 +0200 | [diff] [blame] | 734 | /* ------------------------------------------------------------------------- */ |
| 735 | |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 736 | static int __i2c_check_addr(struct device *dev, void *addrp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | { |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 738 | struct i2c_client *client = i2c_verify_client(dev); |
| 739 | int addr = *(int *)addrp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 741 | if (client && client->addr == addr) |
| 742 | return -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | return 0; |
| 744 | } |
| 745 | |
Jean Delvare | 5e31c2b | 2007-11-15 19:24:02 +0100 | [diff] [blame] | 746 | static int i2c_check_addr(struct i2c_adapter *adapter, int addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | { |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 748 | return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | int i2c_attach_client(struct i2c_client *client) |
| 752 | { |
| 753 | struct i2c_adapter *adapter = client->adapter; |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 754 | int res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 755 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | client->dev.parent = &client->adapter->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | client->dev.bus = &i2c_bus_type; |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 758 | |
| 759 | if (client->driver) |
| 760 | client->dev.driver = &client->driver->driver; |
| 761 | |
David Brownell | de81d2a | 2007-05-22 19:49:16 +0200 | [diff] [blame] | 762 | if (client->driver && !is_newstyle_driver(client->driver)) { |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 763 | client->dev.release = i2c_client_release; |
David Brownell | de81d2a | 2007-05-22 19:49:16 +0200 | [diff] [blame] | 764 | client->dev.uevent_suppress = 1; |
| 765 | } else |
David Brownell | 9c1600e | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 766 | client->dev.release = i2c_client_dev_release; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id), |
| 769 | "%d-%04x", i2c_adapter_id(adapter), client->addr); |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 770 | res = device_register(&client->dev); |
| 771 | if (res) |
David Brownell | 86ec5ec | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 772 | goto out_err; |
| 773 | |
| 774 | mutex_lock(&adapter->clist_lock); |
| 775 | list_add_tail(&client->list, &adapter->clients); |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 776 | mutex_unlock(&adapter->clist_lock); |
Jean Delvare | 77ed74d | 2006-09-30 17:18:59 +0200 | [diff] [blame] | 777 | |
David Brownell | 86ec5ec | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 778 | dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n", |
| 779 | client->name, client->dev.bus_id); |
| 780 | |
Jean Delvare | 77ed74d | 2006-09-30 17:18:59 +0200 | [diff] [blame] | 781 | if (adapter->client_register) { |
| 782 | if (adapter->client_register(client)) { |
| 783 | dev_dbg(&adapter->dev, "client_register " |
| 784 | "failed for client [%s] at 0x%02x\n", |
| 785 | client->name, client->addr); |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | return 0; |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 790 | |
David Brownell | 86ec5ec | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 791 | out_err: |
Jean Delvare | b119c6c | 2006-08-15 18:26:30 +0200 | [diff] [blame] | 792 | dev_err(&adapter->dev, "Failed to attach i2c client %s at 0x%02x " |
| 793 | "(%d)\n", client->name, client->addr, res); |
Jean Delvare | 77ed74d | 2006-09-30 17:18:59 +0200 | [diff] [blame] | 794 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 796 | EXPORT_SYMBOL(i2c_attach_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | |
| 798 | int i2c_detach_client(struct i2c_client *client) |
| 799 | { |
| 800 | struct i2c_adapter *adapter = client->adapter; |
| 801 | int res = 0; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | if (adapter->client_unregister) { |
| 804 | res = adapter->client_unregister(client); |
| 805 | if (res) { |
| 806 | dev_err(&client->dev, |
Jean Delvare | 86749e8 | 2005-07-29 12:15:29 -0700 | [diff] [blame] | 807 | "client_unregister [%s] failed, " |
| 808 | "client not detached\n", client->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | goto out; |
| 810 | } |
| 811 | } |
| 812 | |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 813 | mutex_lock(&adapter->clist_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | list_del(&client->list); |
Jean Delvare | 9ddced1 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 815 | mutex_unlock(&adapter->clist_lock); |
| 816 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | init_completion(&client->released); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | device_unregister(&client->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | wait_for_completion(&client->released); |
| 820 | |
| 821 | out: |
| 822 | return res; |
| 823 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 824 | EXPORT_SYMBOL(i2c_detach_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | |
Jean Delvare | e48d331 | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 826 | /** |
| 827 | * i2c_use_client - increments the reference count of the i2c client structure |
| 828 | * @client: the client being referenced |
| 829 | * |
| 830 | * Each live reference to a client should be refcounted. The driver model does |
| 831 | * that automatically as part of driver binding, so that most drivers don't |
| 832 | * need to do this explicitly: they hold a reference until they're unbound |
| 833 | * from the device. |
| 834 | * |
| 835 | * A pointer to the client with the incremented reference counter is returned. |
| 836 | */ |
| 837 | struct i2c_client *i2c_use_client(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | { |
Jean Delvare | bdc511f | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 839 | get_device(&client->dev); |
Jean Delvare | e48d331 | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 840 | return client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 842 | EXPORT_SYMBOL(i2c_use_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | |
Jean Delvare | e48d331 | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 844 | /** |
| 845 | * i2c_release_client - release a use of the i2c client structure |
| 846 | * @client: the client being no longer referenced |
| 847 | * |
| 848 | * Must be called when a user of a client is finished with it. |
| 849 | */ |
| 850 | void i2c_release_client(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | { |
Jean Delvare | bdc511f | 2008-01-27 18:14:48 +0100 | [diff] [blame] | 852 | put_device(&client->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 854 | EXPORT_SYMBOL(i2c_release_client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 856 | struct i2c_cmd_arg { |
| 857 | unsigned cmd; |
| 858 | void *arg; |
| 859 | }; |
| 860 | |
| 861 | static int i2c_cmd(struct device *dev, void *_arg) |
| 862 | { |
| 863 | struct i2c_client *client = i2c_verify_client(dev); |
| 864 | struct i2c_cmd_arg *arg = _arg; |
| 865 | |
| 866 | if (client && client->driver && client->driver->command) |
| 867 | client->driver->command(client, arg->cmd, arg->arg); |
| 868 | return 0; |
| 869 | } |
| 870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg) |
| 872 | { |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 873 | struct i2c_cmd_arg cmd_arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 875 | cmd_arg.cmd = cmd; |
| 876 | cmd_arg.arg = arg; |
| 877 | device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 879 | EXPORT_SYMBOL(i2c_clients_command); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | |
| 881 | static int __init i2c_init(void) |
| 882 | { |
| 883 | int retval; |
| 884 | |
| 885 | retval = bus_register(&i2c_bus_type); |
| 886 | if (retval) |
| 887 | return retval; |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 888 | retval = class_register(&i2c_adapter_class); |
| 889 | if (retval) |
| 890 | goto bus_err; |
| 891 | retval = i2c_add_driver(&dummy_driver); |
| 892 | if (retval) |
| 893 | goto class_err; |
| 894 | return 0; |
| 895 | |
| 896 | class_err: |
| 897 | class_unregister(&i2c_adapter_class); |
| 898 | bus_err: |
| 899 | bus_unregister(&i2c_bus_type); |
| 900 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | static void __exit i2c_exit(void) |
| 904 | { |
David Brownell | e9f1373 | 2008-01-27 18:14:52 +0100 | [diff] [blame] | 905 | i2c_del_driver(&dummy_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | class_unregister(&i2c_adapter_class); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | bus_unregister(&i2c_bus_type); |
| 908 | } |
| 909 | |
| 910 | subsys_initcall(i2c_init); |
| 911 | module_exit(i2c_exit); |
| 912 | |
| 913 | /* ---------------------------------------------------- |
| 914 | * the functional interface to the i2c busses. |
| 915 | * ---------------------------------------------------- |
| 916 | */ |
| 917 | |
| 918 | int i2c_transfer(struct i2c_adapter * adap, struct i2c_msg *msgs, int num) |
| 919 | { |
| 920 | int ret; |
| 921 | |
| 922 | if (adap->algo->master_xfer) { |
| 923 | #ifdef DEBUG |
| 924 | for (ret = 0; ret < num; ret++) { |
| 925 | dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, " |
Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 926 | "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD) |
| 927 | ? 'R' : 'W', msgs[ret].addr, msgs[ret].len, |
| 928 | (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | } |
| 930 | #endif |
| 931 | |
Mike Rapoport | cea443a8 | 2008-01-27 18:14:50 +0100 | [diff] [blame] | 932 | if (in_atomic() || irqs_disabled()) { |
| 933 | ret = mutex_trylock(&adap->bus_lock); |
| 934 | if (!ret) |
| 935 | /* I2C activity is ongoing. */ |
| 936 | return -EAGAIN; |
| 937 | } else { |
| 938 | mutex_lock_nested(&adap->bus_lock, adap->level); |
| 939 | } |
| 940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | ret = adap->algo->master_xfer(adap,msgs,num); |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 942 | mutex_unlock(&adap->bus_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | |
| 944 | return ret; |
| 945 | } else { |
| 946 | dev_dbg(&adap->dev, "I2C level transfers not supported\n"); |
| 947 | return -ENOSYS; |
| 948 | } |
| 949 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 950 | EXPORT_SYMBOL(i2c_transfer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
| 952 | int i2c_master_send(struct i2c_client *client,const char *buf ,int count) |
| 953 | { |
| 954 | int ret; |
| 955 | struct i2c_adapter *adap=client->adapter; |
| 956 | struct i2c_msg msg; |
| 957 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 958 | msg.addr = client->addr; |
| 959 | msg.flags = client->flags & I2C_M_TEN; |
| 960 | msg.len = count; |
| 961 | msg.buf = (char *)buf; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 962 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 963 | ret = i2c_transfer(adap, &msg, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 965 | /* If everything went ok (i.e. 1 msg transmitted), return #bytes |
| 966 | transmitted, else error code. */ |
| 967 | return (ret == 1) ? count : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 969 | EXPORT_SYMBOL(i2c_master_send); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
| 971 | int i2c_master_recv(struct i2c_client *client, char *buf ,int count) |
| 972 | { |
| 973 | struct i2c_adapter *adap=client->adapter; |
| 974 | struct i2c_msg msg; |
| 975 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | |
Jean Delvare | 815f55f | 2005-05-07 22:58:46 +0200 | [diff] [blame] | 977 | msg.addr = client->addr; |
| 978 | msg.flags = client->flags & I2C_M_TEN; |
| 979 | msg.flags |= I2C_M_RD; |
| 980 | msg.len = count; |
| 981 | msg.buf = buf; |
| 982 | |
| 983 | ret = i2c_transfer(adap, &msg, 1); |
| 984 | |
| 985 | /* If everything went ok (i.e. 1 msg transmitted), return #bytes |
| 986 | transmitted, else error code. */ |
| 987 | return (ret == 1) ? count : ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 989 | EXPORT_SYMBOL(i2c_master_recv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | /* ---------------------------------------------------- |
| 992 | * the i2c address scanning function |
| 993 | * Will not work for 10-bit addresses! |
| 994 | * ---------------------------------------------------- |
| 995 | */ |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 996 | static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind, |
| 997 | int (*found_proc) (struct i2c_adapter *, int, int)) |
Jean Delvare | 9fc6adf | 2005-07-31 21:20:43 +0200 | [diff] [blame] | 998 | { |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 999 | int err; |
Jean Delvare | 9fc6adf | 2005-07-31 21:20:43 +0200 | [diff] [blame] | 1000 | |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 1001 | /* Make sure the address is valid */ |
| 1002 | if (addr < 0x03 || addr > 0x77) { |
| 1003 | dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n", |
| 1004 | addr); |
| 1005 | return -EINVAL; |
Jean Delvare | 9fc6adf | 2005-07-31 21:20:43 +0200 | [diff] [blame] | 1006 | } |
| 1007 | |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 1008 | /* Skip if already in use */ |
| 1009 | if (i2c_check_addr(adapter, addr)) |
| 1010 | return 0; |
| 1011 | |
| 1012 | /* Make sure there is something at this address, unless forced */ |
Jean Delvare | 4c9337d | 2005-08-09 20:28:10 +0200 | [diff] [blame] | 1013 | if (kind < 0) { |
| 1014 | if (i2c_smbus_xfer(adapter, addr, 0, 0, 0, |
| 1015 | I2C_SMBUS_QUICK, NULL) < 0) |
| 1016 | return 0; |
| 1017 | |
| 1018 | /* prevent 24RF08 corruption */ |
| 1019 | if ((addr & ~0x0f) == 0x50) |
| 1020 | i2c_smbus_xfer(adapter, addr, 0, 0, 0, |
| 1021 | I2C_SMBUS_QUICK, NULL); |
| 1022 | } |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 1023 | |
| 1024 | /* Finally call the custom detection function */ |
| 1025 | err = found_proc(adapter, addr, kind); |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 1026 | /* -ENODEV can be returned if there is a chip at the given address |
| 1027 | but it isn't supported by this chip driver. We catch it here as |
| 1028 | this isn't an error. */ |
Jean Delvare | 114fd18 | 2006-09-03 22:25:04 +0200 | [diff] [blame] | 1029 | if (err == -ENODEV) |
| 1030 | err = 0; |
| 1031 | |
| 1032 | if (err) |
| 1033 | dev_warn(&adapter->dev, "Client creation failed at 0x%x (%d)\n", |
| 1034 | addr, err); |
| 1035 | return err; |
Jean Delvare | 9fc6adf | 2005-07-31 21:20:43 +0200 | [diff] [blame] | 1036 | } |
| 1037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | int i2c_probe(struct i2c_adapter *adapter, |
Mark M. Hoffman | bfb6df2 | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 1039 | const struct i2c_client_address_data *address_data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1040 | int (*found_proc) (struct i2c_adapter *, int, int)) |
| 1041 | { |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 1042 | int i, err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | int adap_id = i2c_adapter_id(adapter); |
| 1044 | |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 1045 | /* Force entries are done first, and are not affected by ignore |
| 1046 | entries */ |
| 1047 | if (address_data->forces) { |
Mark M. Hoffman | bfb6df2 | 2008-01-27 18:14:46 +0100 | [diff] [blame] | 1048 | const unsigned short * const *forces = address_data->forces; |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 1049 | int kind; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 1051 | for (kind = 0; forces[kind]; kind++) { |
| 1052 | for (i = 0; forces[kind][i] != I2C_CLIENT_END; |
| 1053 | i += 2) { |
| 1054 | if (forces[kind][i] == adap_id |
| 1055 | || forces[kind][i] == ANY_I2C_BUS) { |
| 1056 | dev_dbg(&adapter->dev, "found force " |
| 1057 | "parameter for adapter %d, " |
| 1058 | "addr 0x%02x, kind %d\n", |
| 1059 | adap_id, forces[kind][i + 1], |
| 1060 | kind); |
| 1061 | err = i2c_probe_address(adapter, |
| 1062 | forces[kind][i + 1], |
| 1063 | kind, found_proc); |
| 1064 | if (err) |
| 1065 | return err; |
| 1066 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | } |
| 1068 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | } |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 1070 | |
Jean Delvare | 4366dc9 | 2005-09-25 16:50:06 +0200 | [diff] [blame] | 1071 | /* Stop here if we can't use SMBUS_QUICK */ |
| 1072 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) { |
| 1073 | if (address_data->probe[0] == I2C_CLIENT_END |
| 1074 | && address_data->normal_i2c[0] == I2C_CLIENT_END) |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1075 | return 0; |
Jean Delvare | 4366dc9 | 2005-09-25 16:50:06 +0200 | [diff] [blame] | 1076 | |
| 1077 | dev_warn(&adapter->dev, "SMBus Quick command not supported, " |
| 1078 | "can't probe for chips\n"); |
| 1079 | return -1; |
| 1080 | } |
| 1081 | |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 1082 | /* Probe entries are done second, and are not affected by ignore |
| 1083 | entries either */ |
| 1084 | for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) { |
| 1085 | if (address_data->probe[i] == adap_id |
| 1086 | || address_data->probe[i] == ANY_I2C_BUS) { |
| 1087 | dev_dbg(&adapter->dev, "found probe parameter for " |
| 1088 | "adapter %d, addr 0x%02x\n", adap_id, |
| 1089 | address_data->probe[i + 1]); |
| 1090 | err = i2c_probe_address(adapter, |
| 1091 | address_data->probe[i + 1], |
| 1092 | -1, found_proc); |
| 1093 | if (err) |
| 1094 | return err; |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | /* Normal entries are done last, unless shadowed by an ignore entry */ |
| 1099 | for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) { |
| 1100 | int j, ignore; |
| 1101 | |
| 1102 | ignore = 0; |
| 1103 | for (j = 0; address_data->ignore[j] != I2C_CLIENT_END; |
| 1104 | j += 2) { |
| 1105 | if ((address_data->ignore[j] == adap_id || |
| 1106 | address_data->ignore[j] == ANY_I2C_BUS) |
| 1107 | && address_data->ignore[j + 1] |
| 1108 | == address_data->normal_i2c[i]) { |
| 1109 | dev_dbg(&adapter->dev, "found ignore " |
| 1110 | "parameter for adapter %d, " |
| 1111 | "addr 0x%02x\n", adap_id, |
| 1112 | address_data->ignore[j + 1]); |
Mark M. Hoffman | 2369df9 | 2006-07-01 17:01:59 +0200 | [diff] [blame] | 1113 | ignore = 1; |
| 1114 | break; |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 1115 | } |
Jean Delvare | a89ba0b | 2005-08-09 20:17:55 +0200 | [diff] [blame] | 1116 | } |
| 1117 | if (ignore) |
| 1118 | continue; |
| 1119 | |
| 1120 | dev_dbg(&adapter->dev, "found normal entry for adapter %d, " |
| 1121 | "addr 0x%02x\n", adap_id, |
| 1122 | address_data->normal_i2c[i]); |
| 1123 | err = i2c_probe_address(adapter, address_data->normal_i2c[i], |
| 1124 | -1, found_proc); |
| 1125 | if (err) |
| 1126 | return err; |
| 1127 | } |
| 1128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | return 0; |
| 1130 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1131 | EXPORT_SYMBOL(i2c_probe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1133 | struct i2c_client * |
| 1134 | i2c_new_probed_device(struct i2c_adapter *adap, |
| 1135 | struct i2c_board_info *info, |
| 1136 | unsigned short const *addr_list) |
| 1137 | { |
| 1138 | int i; |
| 1139 | |
| 1140 | /* Stop here if the bus doesn't support probing */ |
| 1141 | if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) { |
| 1142 | dev_err(&adap->dev, "Probing not supported\n"); |
| 1143 | return NULL; |
| 1144 | } |
| 1145 | |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1146 | for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) { |
| 1147 | /* Check address validity */ |
| 1148 | if (addr_list[i] < 0x03 || addr_list[i] > 0x77) { |
| 1149 | dev_warn(&adap->dev, "Invalid 7-bit address " |
| 1150 | "0x%02x\n", addr_list[i]); |
| 1151 | continue; |
| 1152 | } |
| 1153 | |
| 1154 | /* Check address availability */ |
David Brownell | 9b766b8 | 2008-01-27 18:14:51 +0100 | [diff] [blame] | 1155 | if (i2c_check_addr(adap, addr_list[i])) { |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1156 | dev_dbg(&adap->dev, "Address 0x%02x already in " |
| 1157 | "use, not probing\n", addr_list[i]); |
| 1158 | continue; |
| 1159 | } |
| 1160 | |
| 1161 | /* Test address responsiveness |
| 1162 | The default probe method is a quick write, but it is known |
| 1163 | to corrupt the 24RF08 EEPROMs due to a state machine bug, |
| 1164 | and could also irreversibly write-protect some EEPROMs, so |
| 1165 | for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte |
| 1166 | read instead. Also, some bus drivers don't implement |
| 1167 | quick write, so we fallback to a byte read it that case |
| 1168 | too. */ |
| 1169 | if ((addr_list[i] & ~0x07) == 0x30 |
| 1170 | || (addr_list[i] & ~0x0f) == 0x50 |
| 1171 | || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) { |
| 1172 | if (i2c_smbus_xfer(adap, addr_list[i], 0, |
| 1173 | I2C_SMBUS_READ, 0, |
| 1174 | I2C_SMBUS_BYTE, NULL) >= 0) |
| 1175 | break; |
| 1176 | } else { |
| 1177 | if (i2c_smbus_xfer(adap, addr_list[i], 0, |
| 1178 | I2C_SMBUS_WRITE, 0, |
| 1179 | I2C_SMBUS_QUICK, NULL) >= 0) |
| 1180 | break; |
| 1181 | } |
| 1182 | } |
Jean Delvare | 12b5053a | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1183 | |
| 1184 | if (addr_list[i] == I2C_CLIENT_END) { |
| 1185 | dev_dbg(&adap->dev, "Probing failed, no device found\n"); |
| 1186 | return NULL; |
| 1187 | } |
| 1188 | |
| 1189 | info->addr = addr_list[i]; |
| 1190 | return i2c_new_device(adap, info); |
| 1191 | } |
| 1192 | EXPORT_SYMBOL_GPL(i2c_new_probed_device); |
| 1193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | struct i2c_adapter* i2c_get_adapter(int id) |
| 1195 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | struct i2c_adapter *adapter; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1197 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1198 | mutex_lock(&core_lock); |
Mark M. Hoffman | a0920e1 | 2005-06-28 00:21:30 -0400 | [diff] [blame] | 1199 | adapter = (struct i2c_adapter *)idr_find(&i2c_adapter_idr, id); |
| 1200 | if (adapter && !try_module_get(adapter->owner)) |
| 1201 | adapter = NULL; |
| 1202 | |
Jean Delvare | caada32 | 2008-01-27 18:14:49 +0100 | [diff] [blame] | 1203 | mutex_unlock(&core_lock); |
Mark M. Hoffman | a0920e1 | 2005-06-28 00:21:30 -0400 | [diff] [blame] | 1204 | return adapter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1206 | EXPORT_SYMBOL(i2c_get_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | |
| 1208 | void i2c_put_adapter(struct i2c_adapter *adap) |
| 1209 | { |
| 1210 | module_put(adap->owner); |
| 1211 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1212 | EXPORT_SYMBOL(i2c_put_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1213 | |
| 1214 | /* The SMBus parts */ |
| 1215 | |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1216 | #define POLY (0x1070U << 3) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | static u8 |
| 1218 | crc8(u16 data) |
| 1219 | { |
| 1220 | int i; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | for(i = 0; i < 8; i++) { |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1223 | if (data & 0x8000) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | data = data ^ POLY; |
| 1225 | data = data << 1; |
| 1226 | } |
| 1227 | return (u8)(data >> 8); |
| 1228 | } |
| 1229 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1230 | /* Incremental CRC8 over count bytes in the array pointed to by p */ |
| 1231 | static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | { |
| 1233 | int i; |
| 1234 | |
| 1235 | for(i = 0; i < count; i++) |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1236 | crc = crc8((crc ^ p[i]) << 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | return crc; |
| 1238 | } |
| 1239 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1240 | /* Assume a 7-bit address, which is reasonable for SMBus */ |
| 1241 | static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | { |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1243 | /* The address will be sent first */ |
| 1244 | u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD); |
| 1245 | pec = i2c_smbus_pec(pec, &addr, 1); |
| 1246 | |
| 1247 | /* The data buffer follows */ |
| 1248 | return i2c_smbus_pec(pec, msg->buf, msg->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1251 | /* Used for write only transactions */ |
| 1252 | static inline void i2c_smbus_add_pec(struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | { |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1254 | msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg); |
| 1255 | msg->len++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1258 | /* Return <0 on CRC error |
| 1259 | If there was a write before this read (most cases) we need to take the |
| 1260 | partial CRC from the write part into account. |
| 1261 | Note that this function does modify the message (we need to decrease the |
| 1262 | message length to hide the CRC byte from the caller). */ |
| 1263 | static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | { |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1265 | u8 rpec = msg->buf[--msg->len]; |
| 1266 | cpec = i2c_smbus_msg_pec(cpec, msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | if (rpec != cpec) { |
| 1269 | pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n", |
| 1270 | rpec, cpec); |
| 1271 | return -1; |
| 1272 | } |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1273 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | s32 i2c_smbus_write_quick(struct i2c_client *client, u8 value) |
| 1277 | { |
| 1278 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1279 | value,0,I2C_SMBUS_QUICK,NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1281 | EXPORT_SYMBOL(i2c_smbus_write_quick); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | |
| 1283 | s32 i2c_smbus_read_byte(struct i2c_client *client) |
| 1284 | { |
| 1285 | union i2c_smbus_data data; |
| 1286 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 1287 | I2C_SMBUS_READ,0,I2C_SMBUS_BYTE, &data)) |
| 1288 | return -1; |
| 1289 | else |
Jean Delvare | 7eff82c | 2006-09-03 22:24:00 +0200 | [diff] [blame] | 1290 | return data.byte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1292 | EXPORT_SYMBOL(i2c_smbus_read_byte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | |
| 1294 | s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value) |
| 1295 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1297 | I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1299 | EXPORT_SYMBOL(i2c_smbus_write_byte); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
| 1301 | s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command) |
| 1302 | { |
| 1303 | union i2c_smbus_data data; |
| 1304 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 1305 | I2C_SMBUS_READ,command, I2C_SMBUS_BYTE_DATA,&data)) |
| 1306 | return -1; |
| 1307 | else |
Jean Delvare | 7eff82c | 2006-09-03 22:24:00 +0200 | [diff] [blame] | 1308 | return data.byte; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1310 | EXPORT_SYMBOL(i2c_smbus_read_byte_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | |
| 1312 | s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value) |
| 1313 | { |
| 1314 | union i2c_smbus_data data; |
| 1315 | data.byte = value; |
| 1316 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 1317 | I2C_SMBUS_WRITE,command, |
| 1318 | I2C_SMBUS_BYTE_DATA,&data); |
| 1319 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1320 | EXPORT_SYMBOL(i2c_smbus_write_byte_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | |
| 1322 | s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command) |
| 1323 | { |
| 1324 | union i2c_smbus_data data; |
| 1325 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 1326 | I2C_SMBUS_READ,command, I2C_SMBUS_WORD_DATA, &data)) |
| 1327 | return -1; |
| 1328 | else |
Jean Delvare | 7eff82c | 2006-09-03 22:24:00 +0200 | [diff] [blame] | 1329 | return data.word; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1331 | EXPORT_SYMBOL(i2c_smbus_read_word_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | |
| 1333 | s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value) |
| 1334 | { |
| 1335 | union i2c_smbus_data data; |
| 1336 | data.word = value; |
| 1337 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 1338 | I2C_SMBUS_WRITE,command, |
| 1339 | I2C_SMBUS_WORD_DATA,&data); |
| 1340 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1341 | EXPORT_SYMBOL(i2c_smbus_write_word_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | |
David Brownell | a64ec07 | 2007-10-13 23:56:31 +0200 | [diff] [blame] | 1343 | /** |
| 1344 | * i2c_smbus_read_block_data - SMBus block read request |
| 1345 | * @client: Handle to slave device |
| 1346 | * @command: Command byte issued to let the slave know what data should |
| 1347 | * be returned |
| 1348 | * @values: Byte array into which data will be read; big enough to hold |
| 1349 | * the data returned by the slave. SMBus allows at most 32 bytes. |
| 1350 | * |
| 1351 | * Returns the number of bytes read in the slave's response, else a |
| 1352 | * negative number to indicate some kind of error. |
| 1353 | * |
| 1354 | * Note that using this function requires that the client's adapter support |
| 1355 | * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers |
| 1356 | * support this; its emulation through I2C messaging relies on a specific |
| 1357 | * mechanism (I2C_M_RECV_LEN) which may not be implemented. |
| 1358 | */ |
Jean Delvare | b86a1bc | 2007-05-01 23:26:34 +0200 | [diff] [blame] | 1359 | s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command, |
| 1360 | u8 *values) |
| 1361 | { |
| 1362 | union i2c_smbus_data data; |
| 1363 | |
| 1364 | if (i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 1365 | I2C_SMBUS_READ, command, |
| 1366 | I2C_SMBUS_BLOCK_DATA, &data)) |
| 1367 | return -1; |
| 1368 | |
| 1369 | memcpy(values, &data.block[1], data.block[0]); |
| 1370 | return data.block[0]; |
| 1371 | } |
| 1372 | EXPORT_SYMBOL(i2c_smbus_read_block_data); |
| 1373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command, |
Krzysztof Halasa | 46f5ed7 | 2006-06-12 21:42:20 +0200 | [diff] [blame] | 1375 | u8 length, const u8 *values) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | { |
| 1377 | union i2c_smbus_data data; |
Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 1378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | if (length > I2C_SMBUS_BLOCK_MAX) |
| 1380 | length = I2C_SMBUS_BLOCK_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | data.block[0] = length; |
Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 1382 | memcpy(&data.block[1], values, length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | return i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 1384 | I2C_SMBUS_WRITE,command, |
| 1385 | I2C_SMBUS_BLOCK_DATA,&data); |
| 1386 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1387 | EXPORT_SYMBOL(i2c_smbus_write_block_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | |
| 1389 | /* Returns the number of read bytes */ |
Jean Delvare | 4b2643d | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 1390 | s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command, |
| 1391 | u8 length, u8 *values) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | { |
| 1393 | union i2c_smbus_data data; |
Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 1394 | |
Jean Delvare | 4b2643d | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 1395 | if (length > I2C_SMBUS_BLOCK_MAX) |
| 1396 | length = I2C_SMBUS_BLOCK_MAX; |
| 1397 | data.block[0] = length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | if (i2c_smbus_xfer(client->adapter,client->addr,client->flags, |
| 1399 | I2C_SMBUS_READ,command, |
| 1400 | I2C_SMBUS_I2C_BLOCK_DATA,&data)) |
| 1401 | return -1; |
Jean Delvare | 7656032 | 2006-01-18 23:14:55 +0100 | [diff] [blame] | 1402 | |
| 1403 | memcpy(values, &data.block[1], data.block[0]); |
| 1404 | return data.block[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1406 | EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | |
Jean Delvare | 21bbd69 | 2006-01-09 15:19:18 +1100 | [diff] [blame] | 1408 | s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command, |
Krzysztof Halasa | 46f5ed7 | 2006-06-12 21:42:20 +0200 | [diff] [blame] | 1409 | u8 length, const u8 *values) |
Jean Delvare | 21bbd69 | 2006-01-09 15:19:18 +1100 | [diff] [blame] | 1410 | { |
| 1411 | union i2c_smbus_data data; |
| 1412 | |
| 1413 | if (length > I2C_SMBUS_BLOCK_MAX) |
| 1414 | length = I2C_SMBUS_BLOCK_MAX; |
| 1415 | data.block[0] = length; |
| 1416 | memcpy(data.block + 1, values, length); |
| 1417 | return i2c_smbus_xfer(client->adapter, client->addr, client->flags, |
| 1418 | I2C_SMBUS_WRITE, command, |
| 1419 | I2C_SMBUS_I2C_BLOCK_DATA, &data); |
| 1420 | } |
David Brownell | c056460 | 2007-05-01 23:26:31 +0200 | [diff] [blame] | 1421 | EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data); |
Jean Delvare | 21bbd69 | 2006-01-09 15:19:18 +1100 | [diff] [blame] | 1422 | |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1423 | /* Simulate a SMBus command using the i2c protocol |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | No checking of parameters is done! */ |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1425 | static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | unsigned short flags, |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1427 | char read_write, u8 command, int size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | union i2c_smbus_data * data) |
| 1429 | { |
| 1430 | /* So we need to generate a series of msgs. In the case of writing, we |
| 1431 | need to use only one message; when reading, we need two. We initialize |
| 1432 | most things with sane defaults, to keep the code below somewhat |
| 1433 | simpler. */ |
Hideki Iwamoto | 5c50d18 | 2005-09-25 17:01:11 +0200 | [diff] [blame] | 1434 | unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3]; |
| 1435 | unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | int num = read_write == I2C_SMBUS_READ?2:1; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1437 | struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | { addr, flags | I2C_M_RD, 0, msgbuf1 } |
| 1439 | }; |
| 1440 | int i; |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1441 | u8 partial_pec = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1442 | |
| 1443 | msgbuf0[0] = command; |
| 1444 | switch(size) { |
| 1445 | case I2C_SMBUS_QUICK: |
| 1446 | msg[0].len = 0; |
| 1447 | /* Special case: The read/write field is used as data */ |
| 1448 | msg[0].flags = flags | (read_write==I2C_SMBUS_READ)?I2C_M_RD:0; |
| 1449 | num = 1; |
| 1450 | break; |
| 1451 | case I2C_SMBUS_BYTE: |
| 1452 | if (read_write == I2C_SMBUS_READ) { |
| 1453 | /* Special case: only a read! */ |
| 1454 | msg[0].flags = I2C_M_RD | flags; |
| 1455 | num = 1; |
| 1456 | } |
| 1457 | break; |
| 1458 | case I2C_SMBUS_BYTE_DATA: |
| 1459 | if (read_write == I2C_SMBUS_READ) |
| 1460 | msg[1].len = 1; |
| 1461 | else { |
| 1462 | msg[0].len = 2; |
| 1463 | msgbuf0[1] = data->byte; |
| 1464 | } |
| 1465 | break; |
| 1466 | case I2C_SMBUS_WORD_DATA: |
| 1467 | if (read_write == I2C_SMBUS_READ) |
| 1468 | msg[1].len = 2; |
| 1469 | else { |
| 1470 | msg[0].len=3; |
| 1471 | msgbuf0[1] = data->word & 0xff; |
Jean Delvare | 7eff82c | 2006-09-03 22:24:00 +0200 | [diff] [blame] | 1472 | msgbuf0[2] = data->word >> 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1473 | } |
| 1474 | break; |
| 1475 | case I2C_SMBUS_PROC_CALL: |
| 1476 | num = 2; /* Special case */ |
| 1477 | read_write = I2C_SMBUS_READ; |
| 1478 | msg[0].len = 3; |
| 1479 | msg[1].len = 2; |
| 1480 | msgbuf0[1] = data->word & 0xff; |
Jean Delvare | 7eff82c | 2006-09-03 22:24:00 +0200 | [diff] [blame] | 1481 | msgbuf0[2] = data->word >> 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | break; |
| 1483 | case I2C_SMBUS_BLOCK_DATA: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | if (read_write == I2C_SMBUS_READ) { |
Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 1485 | msg[1].flags |= I2C_M_RECV_LEN; |
| 1486 | msg[1].len = 1; /* block length will be added by |
| 1487 | the underlying bus driver */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | } else { |
| 1489 | msg[0].len = data->block[0] + 2; |
| 1490 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) { |
| 1491 | dev_err(&adapter->dev, "smbus_access called with " |
| 1492 | "invalid block write size (%d)\n", |
| 1493 | data->block[0]); |
| 1494 | return -1; |
| 1495 | } |
Hideki Iwamoto | 5c50d18 | 2005-09-25 17:01:11 +0200 | [diff] [blame] | 1496 | for (i = 1; i < msg[0].len; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | msgbuf0[i] = data->block[i-1]; |
| 1498 | } |
| 1499 | break; |
| 1500 | case I2C_SMBUS_BLOCK_PROC_CALL: |
Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 1501 | num = 2; /* Another special case */ |
| 1502 | read_write = I2C_SMBUS_READ; |
| 1503 | if (data->block[0] > I2C_SMBUS_BLOCK_MAX) { |
| 1504 | dev_err(&adapter->dev, "%s called with invalid " |
| 1505 | "block proc call size (%d)\n", __FUNCTION__, |
| 1506 | data->block[0]); |
| 1507 | return -1; |
| 1508 | } |
| 1509 | msg[0].len = data->block[0] + 2; |
| 1510 | for (i = 1; i < msg[0].len; i++) |
| 1511 | msgbuf0[i] = data->block[i-1]; |
| 1512 | msg[1].flags |= I2C_M_RECV_LEN; |
| 1513 | msg[1].len = 1; /* block length will be added by |
| 1514 | the underlying bus driver */ |
| 1515 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | case I2C_SMBUS_I2C_BLOCK_DATA: |
| 1517 | if (read_write == I2C_SMBUS_READ) { |
Jean Delvare | 4b2643d | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 1518 | msg[1].len = data->block[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | } else { |
| 1520 | msg[0].len = data->block[0] + 1; |
Jean Delvare | 30dac74 | 2005-10-08 00:15:59 +0200 | [diff] [blame] | 1521 | if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | dev_err(&adapter->dev, "i2c_smbus_xfer_emulated called with " |
| 1523 | "invalid block write size (%d)\n", |
| 1524 | data->block[0]); |
| 1525 | return -1; |
| 1526 | } |
| 1527 | for (i = 1; i <= data->block[0]; i++) |
| 1528 | msgbuf0[i] = data->block[i]; |
| 1529 | } |
| 1530 | break; |
| 1531 | default: |
| 1532 | dev_err(&adapter->dev, "smbus_access called with invalid size (%d)\n", |
| 1533 | size); |
| 1534 | return -1; |
| 1535 | } |
| 1536 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1537 | i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK |
| 1538 | && size != I2C_SMBUS_I2C_BLOCK_DATA); |
| 1539 | if (i) { |
| 1540 | /* Compute PEC if first message is a write */ |
| 1541 | if (!(msg[0].flags & I2C_M_RD)) { |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1542 | if (num == 1) /* Write only */ |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1543 | i2c_smbus_add_pec(&msg[0]); |
| 1544 | else /* Write followed by read */ |
| 1545 | partial_pec = i2c_smbus_msg_pec(0, &msg[0]); |
| 1546 | } |
| 1547 | /* Ask for PEC if last message is a read */ |
| 1548 | if (msg[num-1].flags & I2C_M_RD) |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1549 | msg[num-1].len++; |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1550 | } |
| 1551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | if (i2c_transfer(adapter, msg, num) < 0) |
| 1553 | return -1; |
| 1554 | |
Jean Delvare | 421ef47 | 2005-10-26 21:28:55 +0200 | [diff] [blame] | 1555 | /* Check PEC if last message is a read */ |
| 1556 | if (i && (msg[num-1].flags & I2C_M_RD)) { |
| 1557 | if (i2c_smbus_check_pec(partial_pec, &msg[num-1]) < 0) |
| 1558 | return -1; |
| 1559 | } |
| 1560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1561 | if (read_write == I2C_SMBUS_READ) |
| 1562 | switch(size) { |
| 1563 | case I2C_SMBUS_BYTE: |
| 1564 | data->byte = msgbuf0[0]; |
| 1565 | break; |
| 1566 | case I2C_SMBUS_BYTE_DATA: |
| 1567 | data->byte = msgbuf1[0]; |
| 1568 | break; |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1569 | case I2C_SMBUS_WORD_DATA: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | case I2C_SMBUS_PROC_CALL: |
| 1571 | data->word = msgbuf1[0] | (msgbuf1[1] << 8); |
| 1572 | break; |
| 1573 | case I2C_SMBUS_I2C_BLOCK_DATA: |
Jean Delvare | 4b2643d | 2007-07-12 14:12:29 +0200 | [diff] [blame] | 1574 | for (i = 0; i < data->block[0]; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | data->block[i+1] = msgbuf1[i]; |
| 1576 | break; |
Jean Delvare | 209d27c | 2007-05-01 23:26:29 +0200 | [diff] [blame] | 1577 | case I2C_SMBUS_BLOCK_DATA: |
| 1578 | case I2C_SMBUS_BLOCK_PROC_CALL: |
| 1579 | for (i = 0; i < msgbuf1[0] + 1; i++) |
| 1580 | data->block[i] = msgbuf1[i]; |
| 1581 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | } |
| 1583 | return 0; |
| 1584 | } |
| 1585 | |
| 1586 | |
| 1587 | s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags, |
David Brownell | 438d6c2 | 2006-12-10 21:21:31 +0100 | [diff] [blame] | 1588 | char read_write, u8 command, int size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | union i2c_smbus_data * data) |
| 1590 | { |
| 1591 | s32 res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | |
| 1593 | flags &= I2C_M_TEN | I2C_CLIENT_PEC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | |
| 1595 | if (adapter->algo->smbus_xfer) { |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 1596 | mutex_lock(&adapter->bus_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | res = adapter->algo->smbus_xfer(adapter,addr,flags,read_write, |
| 1598 | command,size,data); |
Ingo Molnar | 5c085d3 | 2006-01-18 23:16:04 +0100 | [diff] [blame] | 1599 | mutex_unlock(&adapter->bus_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | } else |
| 1601 | res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write, |
| 1602 | command,size,data); |
| 1603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | return res; |
| 1605 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | EXPORT_SYMBOL(i2c_smbus_xfer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | |
| 1608 | MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); |
| 1609 | MODULE_DESCRIPTION("I2C-Bus main module"); |
| 1610 | MODULE_LICENSE("GPL"); |