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