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