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