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