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