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