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