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