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