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