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