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