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