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