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