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