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