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