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