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