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