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