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