blob: fdc6a9d1394edb01505520c1e7cd383009bf4f8e [file] [log] [blame]
Wolfram Sang61e3d0f2017-05-23 19:23:11 +02001/*
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 Sang687b81d2013-07-11 12:56:15 +010019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Wolfram Sang44239a52016-07-09 13:35:04 +090021#define pr_fmt(fmt) "i2c-core: " fmt
22
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +020023#include <dt-bindings/i2c/i2c.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010024#include <linux/acpi.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020025#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010026#include <linux/completion.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010027#include <linux/delay.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020028#include <linux/err.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010029#include <linux/errno.h>
30#include <linux/gpio.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010031#include <linux/i2c.h>
Phil Reidf8756c62017-08-24 17:31:04 +080032#include <linux/i2c-smbus.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010033#include <linux/idr.h>
34#include <linux/init.h>
35#include <linux/irqflags.h>
36#include <linux/jump_label.h>
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/mutex.h>
40#include <linux/of_device.h>
41#include <linux/of.h>
42#include <linux/of_irq.h>
43#include <linux/pm_domain.h>
44#include <linux/pm_runtime.h>
45#include <linux/pm_wakeirq.h>
Wolfram Sange1dba012015-12-08 10:37:46 +010046#include <linux/property.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010047#include <linux/rwsem.h>
48#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
David Brownell9c1600e2007-05-01 23:26:31 +020050#include "i2c-core.h"
51
David Howellsd9a83d62014-03-06 13:35:59 +000052#define CREATE_TRACE_POINTS
53#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Wolfram Sangda899f52015-05-18 21:09:12 +020055#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
56#define I2C_ADDR_OFFSET_SLAVE 0x1000
57
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +020058#define I2C_ADDR_7BITS_MAX 0x77
59#define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1)
60
Wolfram Sang61e3d0f2017-05-23 19:23:11 +020061/*
62 * core_lock protects i2c_adapter_idr, and guarantees that device detection,
63 * deletion of detected devices, and attach_adapter calls are serialized
64 */
Jean Delvarecaada322008-01-27 18:14:49 +010065static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static DEFINE_IDR(i2c_adapter_idr);
67
Jean Delvare4735c982008-07-14 22:38:36 +020068static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010069
David Howellsd9a83d62014-03-06 13:35:59 +000070static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
Sudip Mukherjee95026652016-03-07 17:19:17 +053071static bool is_registered;
David Howellsd9a83d62014-03-06 13:35:59 +000072
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050073int i2c_transfer_trace_reg(void)
David Howellsd9a83d62014-03-06 13:35:59 +000074{
75 static_key_slow_inc(&i2c_trace_msg);
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050076 return 0;
David Howellsd9a83d62014-03-06 13:35:59 +000077}
78
79void i2c_transfer_trace_unreg(void)
80{
81 static_key_slow_dec(&i2c_trace_msg);
82}
83
Lee Jones5f441fc2016-11-07 12:47:40 +000084const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
Jean Delvared2653e92008-04-29 23:11:39 +020085 const struct i2c_client *client)
86{
Lee Jones811073b2016-11-07 12:47:36 +000087 if (!(id && client))
88 return NULL;
89
Jean Delvared2653e92008-04-29 23:11:39 +020090 while (id->name[0]) {
91 if (strcmp(client->name, id->name) == 0)
92 return id;
93 id++;
94 }
95 return NULL;
96}
Lee Jones5f441fc2016-11-07 12:47:40 +000097EXPORT_SYMBOL_GPL(i2c_match_id);
Jean Delvared2653e92008-04-29 23:11:39 +020098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int i2c_device_match(struct device *dev, struct device_driver *drv)
100{
Jean Delvare51298d12009-09-18 22:45:45 +0200101 struct i2c_client *client = i2c_verify_client(dev);
102 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200103
Jean Delvare51298d12009-09-18 22:45:45 +0200104
Grant Likely959e85f2010-06-08 07:48:19 -0600105 /* Attempt an OF style match */
Lee Jonesda10c062016-11-07 12:47:39 +0000106 if (i2c_of_match_device(drv->of_match_table, client))
Grant Likely959e85f2010-06-08 07:48:19 -0600107 return 1;
108
Mika Westerberg907ddf82012-11-23 12:23:40 +0100109 /* Then ACPI style match */
110 if (acpi_driver_match_device(dev, drv))
111 return 1;
112
Jean Delvare51298d12009-09-18 22:45:45 +0200113 driver = to_i2c_driver(drv);
Lee Jones811073b2016-11-07 12:47:36 +0000114
115 /* Finally an I2C match */
116 if (i2c_match_id(driver->id_table, client))
117 return 1;
Jean Delvared2653e92008-04-29 23:11:39 +0200118
Jean Delvareeb8a7902008-05-18 20:49:41 +0200119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Kay Sievers7eff2e72007-08-14 15:15:12 +0200122static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200123{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100124 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800125 int rc;
126
127 rc = acpi_device_uevent_modalias(dev, env);
128 if (rc != -ENODEV)
129 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200130
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100131 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200132}
133
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530134/* i2c bus recovery routines */
135static int get_scl_gpio_value(struct i2c_adapter *adap)
136{
Phil Reid3991c5c2017-11-02 10:40:24 +0800137 return gpiod_get_value_cansleep(adap->bus_recovery_info->scl_gpiod);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530138}
139
140static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
141{
Phil Reid3991c5c2017-11-02 10:40:24 +0800142 gpiod_set_value_cansleep(adap->bus_recovery_info->scl_gpiod, val);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530143}
144
145static int get_sda_gpio_value(struct i2c_adapter *adap)
146{
Phil Reid3991c5c2017-11-02 10:40:24 +0800147 return gpiod_get_value_cansleep(adap->bus_recovery_info->sda_gpiod);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530148}
149
150static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
151{
152 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
153 struct device *dev = &adap->dev;
154 int ret = 0;
155
156 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
157 GPIOF_OUT_INIT_HIGH, "i2c-scl");
158 if (ret) {
159 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
160 return ret;
161 }
Phil Reid3991c5c2017-11-02 10:40:24 +0800162 bri->scl_gpiod = gpio_to_desc(bri->scl_gpio);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530163
164 if (bri->get_sda) {
165 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
166 /* work without SDA polling */
167 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
168 bri->sda_gpio);
169 bri->get_sda = NULL;
170 }
Phil Reid3991c5c2017-11-02 10:40:24 +0800171 bri->sda_gpiod = gpio_to_desc(bri->sda_gpio);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530172 }
173
174 return ret;
175}
176
177static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
178{
179 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
180
Phil Reid3991c5c2017-11-02 10:40:24 +0800181 if (bri->get_sda) {
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530182 gpio_free(bri->sda_gpio);
Phil Reid3991c5c2017-11-02 10:40:24 +0800183 bri->sda_gpiod = NULL;
184 }
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530185
186 gpio_free(bri->scl_gpio);
Phil Reid3991c5c2017-11-02 10:40:24 +0800187 bri->scl_gpiod = NULL;
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530188}
189
190/*
191 * We are generating clock pulses. ndelay() determines durating of clk pulses.
192 * We will generate clock with rate 100 KHz and so duration of both clock levels
193 * is: delay in ns = (10^6 / 100) / 2
194 */
195#define RECOVERY_NDELAY 5000
196#define RECOVERY_CLK_CNT 9
197
198static int i2c_generic_recovery(struct i2c_adapter *adap)
199{
200 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
201 int i = 0, val = 1, ret = 0;
202
203 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300204 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530205
Jan Luebbe8b062602015-07-08 16:35:06 +0200206 bri->set_scl(adap, val);
207 ndelay(RECOVERY_NDELAY);
208
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530209 /*
210 * By this time SCL is high, as we need to give 9 falling-rising edges
211 */
212 while (i++ < RECOVERY_CLK_CNT * 2) {
213 if (val) {
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530214 /* SCL shouldn't be low here */
215 if (!bri->get_scl(adap)) {
216 dev_err(&adap->dev,
217 "SCL is stuck low, exit recovery\n");
218 ret = -EBUSY;
219 break;
220 }
Claudio Foellmi1f35b862017-10-05 14:44:14 +0200221 /* Break if SDA is high */
222 if (bri->get_sda && bri->get_sda(adap))
223 break;
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530224 }
225
226 val = !val;
227 bri->set_scl(adap, val);
228 ndelay(RECOVERY_NDELAY);
229 }
230
Claudio Foellmi1f35b862017-10-05 14:44:14 +0200231 /* check if recovery actually succeeded */
232 if (bri->get_sda && !bri->get_sda(adap))
233 ret = -EBUSY;
234
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530235 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300236 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530237
238 return ret;
239}
240
241int i2c_generic_scl_recovery(struct i2c_adapter *adap)
242{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530243 return i2c_generic_recovery(adap);
244}
Mark Brownc1c21f42015-04-15 19:18:39 +0100245EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530246
247int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
248{
249 int ret;
250
251 ret = i2c_get_gpios_for_recovery(adap);
252 if (ret)
253 return ret;
254
255 ret = i2c_generic_recovery(adap);
256 i2c_put_gpios_for_recovery(adap);
257
258 return ret;
259}
Mark Brownc1c21f42015-04-15 19:18:39 +0100260EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530261
262int i2c_recover_bus(struct i2c_adapter *adap)
263{
264 if (!adap->bus_recovery_info)
265 return -EOPNOTSUPP;
266
267 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
268 return adap->bus_recovery_info->recover_bus(adap);
269}
Mark Brownc1c21f42015-04-15 19:18:39 +0100270EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530271
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900272static void i2c_init_recovery(struct i2c_adapter *adap)
273{
274 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
275 char *err_str;
276
277 if (!bri)
278 return;
279
280 if (!bri->recover_bus) {
281 err_str = "no recover_bus() found";
282 goto err;
283 }
284
Phil Reid3991c5c2017-11-02 10:40:24 +0800285 if (bri->scl_gpiod && bri->recover_bus == i2c_generic_scl_recovery) {
286 bri->get_scl = get_scl_gpio_value;
287 bri->set_scl = set_scl_gpio_value;
288 if (bri->sda_gpiod)
289 bri->get_sda = get_sda_gpio_value;
290 return;
291 }
292
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900293 /* Generic GPIO recovery */
294 if (bri->recover_bus == i2c_generic_gpio_recovery) {
295 if (!gpio_is_valid(bri->scl_gpio)) {
296 err_str = "invalid SCL gpio";
297 goto err;
298 }
299
300 if (gpio_is_valid(bri->sda_gpio))
301 bri->get_sda = get_sda_gpio_value;
302 else
303 bri->get_sda = NULL;
304
305 bri->get_scl = get_scl_gpio_value;
306 bri->set_scl = set_scl_gpio_value;
307 } else if (bri->recover_bus == i2c_generic_scl_recovery) {
308 /* Generic SCL recovery */
309 if (!bri->set_scl || !bri->get_scl) {
310 err_str = "no {get|set}_scl() found";
311 goto err;
312 }
313 }
314
315 return;
316 err:
317 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
318 adap->bus_recovery_info = NULL;
319}
320
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200321static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
322{
323 struct i2c_adapter *adap = client->adapter;
324 unsigned int irq;
325
326 if (!adap->host_notify_domain)
327 return -ENXIO;
328
329 if (client->flags & I2C_CLIENT_TEN)
330 return -EINVAL;
331
332 irq = irq_find_mapping(adap->host_notify_domain, client->addr);
333 if (!irq)
334 irq = irq_create_mapping(adap->host_notify_domain,
335 client->addr);
336
337 return irq > 0 ? irq : -ENXIO;
338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340static int i2c_device_probe(struct device *dev)
341{
Jean Delvare51298d12009-09-18 22:45:45 +0200342 struct i2c_client *client = i2c_verify_client(dev);
343 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100344 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200345
Jean Delvare51298d12009-09-18 22:45:45 +0200346 if (!client)
347 return 0;
348
Hans de Goeded1d84bb2017-04-05 00:03:34 +0200349 driver = to_i2c_driver(dev->driver);
350
351 if (!client->irq && !driver->disable_i2c_core_irq_mapping) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300352 int irq = -ENOENT;
353
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800354 if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
355 dev_dbg(dev, "Using Host Notify IRQ\n");
356 irq = i2c_smbus_host_notify_to_irq(client);
357 } else if (dev->of_node) {
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700358 irq = of_irq_get_byname(dev->of_node, "irq");
359 if (irq == -EINVAL || irq == -ENODATA)
360 irq = of_irq_get(dev->of_node, 0);
361 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300362 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700363 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100364 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200365 return irq;
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800366
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100367 if (irq < 0)
368 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200369
370 client->irq = irq;
371 }
372
Lee Jonesda10c062016-11-07 12:47:39 +0000373 /*
Javier Martinez Canillasf4b17a12017-08-09 11:21:28 +0200374 * An I2C ID table is not mandatory, if and only if, a suitable OF
375 * or ACPI ID table is supplied for the probing device.
Lee Jonesda10c062016-11-07 12:47:39 +0000376 */
377 if (!driver->id_table &&
Andy Shevchenkoc64ffff2017-07-17 17:13:28 +0300378 !i2c_acpi_match_device(dev->driver->acpi_match_table, client) &&
Lee Jonesda10c062016-11-07 12:47:39 +0000379 !i2c_of_match_device(dev->driver->of_match_table, client))
David Brownell7b4fbc52007-05-01 23:26:30 +0200380 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200381
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700382 if (client->flags & I2C_CLIENT_WAKE) {
383 int wakeirq = -ENOENT;
384
385 if (dev->of_node) {
386 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
387 if (wakeirq == -EPROBE_DEFER)
388 return wakeirq;
389 }
390
391 device_init_wakeup(&client->dev, true);
392
393 if (wakeirq > 0 && wakeirq != client->irq)
394 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
395 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200396 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700397 else
398 status = 0;
399
400 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300401 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700402 }
403
David Brownell7b4fbc52007-05-01 23:26:30 +0200404 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200405
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200406 status = of_clk_set_defaults(dev->of_node, false);
407 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700408 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200409
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200410 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +0100411 if (status == -EPROBE_DEFER)
412 goto err_clear_wakeup_irq;
413
Lee Jonesb8a1a4c2016-11-07 12:47:41 +0000414 /*
415 * When there are no more users of probe(),
416 * rename probe_new to probe.
417 */
418 if (driver->probe_new)
419 status = driver->probe_new(client);
420 else if (driver->probe)
421 status = driver->probe(client,
422 i2c_match_id(driver->id_table, client));
423 else
424 status = -EINVAL;
425
Kieran Bingham74cedd32015-10-12 21:54:43 +0100426 if (status)
427 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100428
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700429 return 0;
430
431err_detach_pm_domain:
432 dev_pm_domain_detach(&client->dev, true);
433err_clear_wakeup_irq:
434 dev_pm_clear_wake_irq(&client->dev);
435 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +0100436 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439static int i2c_device_remove(struct device *dev)
440{
Jean Delvare51298d12009-09-18 22:45:45 +0200441 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200442 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100443 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200444
Jean Delvare51298d12009-09-18 22:45:45 +0200445 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200446 return 0;
447
448 driver = to_i2c_driver(dev->driver);
449 if (driver->remove) {
450 dev_dbg(dev, "remove\n");
451 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200452 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100453
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200454 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700455
456 dev_pm_clear_wake_irq(&client->dev);
457 device_init_wakeup(&client->dev, false);
458
David Brownella1d9e6e2007-05-01 23:26:30 +0200459 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
David Brownellf37dd802007-02-13 22:09:00 +0100462static void i2c_device_shutdown(struct device *dev)
463{
Jean Delvare51298d12009-09-18 22:45:45 +0200464 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100465 struct i2c_driver *driver;
466
Jean Delvare51298d12009-09-18 22:45:45 +0200467 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100468 return;
469 driver = to_i2c_driver(dev->driver);
470 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200471 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100472}
473
David Brownell9c1600e2007-05-01 23:26:31 +0200474static void i2c_client_dev_release(struct device *dev)
475{
476 kfree(to_i2c_client(dev));
477}
478
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100479static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200480show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200481{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200482 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
483 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200484}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100485static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200486
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100487static ssize_t
488show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200489{
490 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800491 int len;
492
493 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
494 if (len != -ENODEV)
495 return len;
496
Jean Delvareeb8a7902008-05-18 20:49:41 +0200497 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200498}
Jean Delvare51298d12009-09-18 22:45:45 +0200499static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
500
501static struct attribute *i2c_dev_attrs[] = {
502 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200503 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200504 &dev_attr_modalias.attr,
505 NULL
506};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100507ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100508
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200509struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100510 .name = "i2c",
511 .match = i2c_device_match,
512 .probe = i2c_device_probe,
513 .remove = i2c_device_remove,
514 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000515};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200516EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000517
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800518struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100519 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200520 .uevent = i2c_device_uevent,
521 .release = i2c_client_dev_release,
522};
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800523EXPORT_SYMBOL_GPL(i2c_client_type);
Jean Delvare51298d12009-09-18 22:45:45 +0200524
David Brownell9b766b82008-01-27 18:14:51 +0100525
526/**
527 * i2c_verify_client - return parameter as i2c_client, or NULL
528 * @dev: device, probably from some driver model iterator
529 *
530 * When traversing the driver model tree, perhaps using driver model
531 * iterators like @device_for_each_child(), you can't assume very much
532 * about the nodes you find. Use this function to avoid oopses caused
533 * by wrongly treating some non-I2C device as an i2c_client.
534 */
535struct i2c_client *i2c_verify_client(struct device *dev)
536{
Jean Delvare51298d12009-09-18 22:45:45 +0200537 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100538 ? to_i2c_client(dev)
539 : NULL;
540}
541EXPORT_SYMBOL(i2c_verify_client);
542
543
Wolfram Sangda899f52015-05-18 21:09:12 +0200544/* Return a unique address which takes the flags of the client into account */
545static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
546{
547 unsigned short addr = client->addr;
548
549 /* For some client flags, add an arbitrary offset to avoid collisions */
550 if (client->flags & I2C_CLIENT_TEN)
551 addr |= I2C_ADDR_OFFSET_TEN_BIT;
552
553 if (client->flags & I2C_CLIENT_SLAVE)
554 addr |= I2C_ADDR_OFFSET_SLAVE;
555
556 return addr;
557}
558
Jean Delvare3a89db52010-06-03 11:33:52 +0200559/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300560 * are purposely not enforced, except for the general call address. */
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200561int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +0200562{
Wolfram Sangc4019b72015-07-17 12:50:06 +0200563 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +0200564 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200565 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +0200566 return -EINVAL;
567 } else {
568 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200569 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +0200570 return -EINVAL;
571 }
572 return 0;
573}
574
Jean Delvare656b8762010-06-03 11:33:53 +0200575/* And this is a strict address validity check, used when probing. If a
576 * device uses a reserved address, then it shouldn't be probed. 7-bit
577 * addressing is assumed, 10-bit address devices are rare and should be
578 * explicitly enumerated. */
Wolfram Sange4991ec2017-05-23 11:14:17 +0200579int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +0200580{
581 /*
582 * Reserved addresses per I2C specification:
583 * 0x00 General call address / START byte
584 * 0x01 CBUS address
585 * 0x02 Reserved for different bus format
586 * 0x03 Reserved for future purposes
587 * 0x04-0x07 Hs-mode master code
588 * 0x78-0x7b 10-bit slave addressing
589 * 0x7c-0x7f Reserved for future purposes
590 */
591 if (addr < 0x08 || addr > 0x77)
592 return -EINVAL;
593 return 0;
594}
595
Jean Delvare3b5f7942010-06-03 11:33:55 +0200596static int __i2c_check_addr_busy(struct device *dev, void *addrp)
597{
598 struct i2c_client *client = i2c_verify_client(dev);
599 int addr = *(int *)addrp;
600
Wolfram Sang9bccc702015-07-17 14:48:56 +0200601 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +0200602 return -EBUSY;
603 return 0;
604}
605
Michael Lawnick08263742010-08-11 18:21:02 +0200606/* walk up mux tree */
607static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
608{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200609 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200610 int result;
611
612 result = device_for_each_child(&adapter->dev, &addr,
613 __i2c_check_addr_busy);
614
Jean Delvare97cc4d42010-10-24 18:16:57 +0200615 if (!result && parent)
616 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200617
618 return result;
619}
620
621/* recurse down mux tree */
622static int i2c_check_mux_children(struct device *dev, void *addrp)
623{
624 int result;
625
626 if (dev->type == &i2c_adapter_type)
627 result = device_for_each_child(dev, addrp,
628 i2c_check_mux_children);
629 else
630 result = __i2c_check_addr_busy(dev, addrp);
631
632 return result;
633}
634
Jean Delvare3b5f7942010-06-03 11:33:55 +0200635static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
636{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200637 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200638 int result = 0;
639
Jean Delvare97cc4d42010-10-24 18:16:57 +0200640 if (parent)
641 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200642
643 if (!result)
644 result = device_for_each_child(&adapter->dev, &addr,
645 i2c_check_mux_children);
646
647 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200648}
649
David Brownell9c1600e2007-05-01 23:26:31 +0200650/**
Peter Rosin8320f492016-05-04 22:15:27 +0200651 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200652 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200653 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
654 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200655 */
Peter Rosin8320f492016-05-04 22:15:27 +0200656static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
657 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200658{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200659 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200660}
Jean Delvarefe61e072010-08-11 18:20:58 +0200661
662/**
Peter Rosin8320f492016-05-04 22:15:27 +0200663 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200664 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200665 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
666 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200667 */
Peter Rosin8320f492016-05-04 22:15:27 +0200668static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
669 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200670{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200671 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200672}
673
674/**
Peter Rosin8320f492016-05-04 22:15:27 +0200675 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200676 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200677 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
678 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200679 */
Peter Rosin8320f492016-05-04 22:15:27 +0200680static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
681 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200682{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200683 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200684}
Jean Delvarefe61e072010-08-11 18:20:58 +0200685
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200686static void i2c_dev_set_name(struct i2c_adapter *adap,
Hans de Goede728fe6c2017-10-11 11:41:19 +0200687 struct i2c_client *client,
688 struct i2c_board_info const *info)
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200689{
690 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
691
Hans de Goede728fe6c2017-10-11 11:41:19 +0200692 if (info && info->dev_name) {
693 dev_set_name(&client->dev, "i2c-%s", info->dev_name);
694 return;
695 }
696
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200697 if (adev) {
698 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
699 return;
700 }
701
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200702 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +0200703 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200704}
705
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800706static int i2c_dev_irq_from_resources(const struct resource *resources,
707 unsigned int num_resources)
708{
709 struct irq_data *irqd;
710 int i;
711
712 for (i = 0; i < num_resources; i++) {
713 const struct resource *r = &resources[i];
714
715 if (resource_type(r) != IORESOURCE_IRQ)
716 continue;
717
718 if (r->flags & IORESOURCE_BITS) {
719 irqd = irq_get_irq_data(r->start);
720 if (!irqd)
721 break;
722
723 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
724 }
725
726 return r->start;
727 }
728
729 return 0;
730}
731
Jean Delvarefe61e072010-08-11 18:20:58 +0200732/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200733 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200734 * @adap: the adapter managing the device
735 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200736 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200737 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200738 * Create an i2c device. Binding is handled through driver model
739 * probe()/remove() methods. A driver may be bound to this device when we
740 * return from this function, or any later moment (e.g. maybe hotplugging will
741 * load the driver module). This call is not appropriate for use by mainboard
742 * initialization logic, which usually runs during an arch_initcall() long
743 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200744 *
745 * This returns the new i2c client, which may be saved for later use with
746 * i2c_unregister_device(); or NULL to indicate an error.
747 */
748struct i2c_client *
749i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
750{
751 struct i2c_client *client;
752 int status;
753
754 client = kzalloc(sizeof *client, GFP_KERNEL);
755 if (!client)
756 return NULL;
757
758 client->adapter = adap;
759
760 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200761
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200762 if (info->archdata)
763 client->dev.archdata = *info->archdata;
764
Marc Pignatee354252008-08-28 08:33:22 +0200765 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200766 client->addr = info->addr;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800767
David Brownell9c1600e2007-05-01 23:26:31 +0200768 client->irq = info->irq;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800769 if (!client->irq)
770 client->irq = i2c_dev_irq_from_resources(info->resources,
771 info->num_resources);
David Brownell9c1600e2007-05-01 23:26:31 +0200772
David Brownell9c1600e2007-05-01 23:26:31 +0200773 strlcpy(client->name, info->type, sizeof(client->name));
774
Wolfram Sangc4019b72015-07-17 12:50:06 +0200775 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +0200776 if (status) {
777 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
778 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
779 goto out_err_silent;
780 }
781
Jean Delvaref8a227e2009-06-19 16:58:18 +0200782 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +0200783 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200784 if (status)
785 goto out_err;
786
787 client->dev.parent = &client->adapter->dev;
788 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200789 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700790 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100791 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200792
Hans de Goede728fe6c2017-10-11 11:41:19 +0200793 i2c_dev_set_name(adap, client, info);
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800794
795 if (info->properties) {
796 status = device_add_properties(&client->dev, info->properties);
797 if (status) {
798 dev_err(&adap->dev,
799 "Failed to add properties to client %s: %d\n",
800 client->name, status);
801 goto out_err;
802 }
803 }
804
Jean Delvaref8a227e2009-06-19 16:58:18 +0200805 status = device_register(&client->dev);
806 if (status)
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800807 goto out_free_props;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200808
Jean Delvaref8a227e2009-06-19 16:58:18 +0200809 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
810 client->name, dev_name(&client->dev));
811
David Brownell9c1600e2007-05-01 23:26:31 +0200812 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200813
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800814out_free_props:
815 if (info->properties)
816 device_remove_properties(&client->dev);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200817out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300818 dev_err(&adap->dev,
819 "Failed to register i2c client %s at 0x%02x (%d)\n",
820 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200821out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200822 kfree(client);
823 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200824}
825EXPORT_SYMBOL_GPL(i2c_new_device);
826
827
828/**
829 * i2c_unregister_device - reverse effect of i2c_new_device()
830 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200831 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200832 */
833void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200834{
Andy Shevchenko7b43dd12017-10-31 16:21:35 +0200835 if (!client)
836 return;
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +0200837 if (client->dev.of_node)
838 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300839 if (ACPI_COMPANION(&client->dev))
840 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +0200841 device_unregister(&client->dev);
842}
David Brownell9c1600e2007-05-01 23:26:31 +0200843EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200844
845
Jean Delvare60b129d2008-05-11 20:37:06 +0200846static const struct i2c_device_id dummy_id[] = {
847 { "dummy", 0 },
848 { },
849};
850
Jean Delvared2653e92008-04-29 23:11:39 +0200851static int dummy_probe(struct i2c_client *client,
852 const struct i2c_device_id *id)
853{
854 return 0;
855}
856
857static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100858{
859 return 0;
860}
861
862static struct i2c_driver dummy_driver = {
863 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200864 .probe = dummy_probe,
865 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200866 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100867};
868
869/**
870 * i2c_new_dummy - return a new i2c device bound to a dummy driver
871 * @adapter: the adapter managing the device
872 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100873 * Context: can sleep
874 *
875 * This returns an I2C client bound to the "dummy" driver, intended for use
876 * with devices that consume multiple addresses. Examples of such chips
877 * include various EEPROMS (like 24c04 and 24c08 models).
878 *
879 * These dummy devices have two main uses. First, most I2C and SMBus calls
880 * except i2c_transfer() need a client handle; the dummy will be that handle.
881 * And second, this prevents the specified address from being bound to a
882 * different driver.
883 *
884 * This returns the new i2c client, which should be saved for later use with
885 * i2c_unregister_device(); or NULL to indicate an error.
886 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100887struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100888{
889 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200890 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100891 };
892
David Brownelle9f13732008-01-27 18:14:52 +0100893 return i2c_new_device(adapter, &info);
894}
895EXPORT_SYMBOL_GPL(i2c_new_dummy);
896
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +0100897/**
898 * i2c_new_secondary_device - Helper to get the instantiated secondary address
899 * and create the associated device
900 * @client: Handle to the primary client
901 * @name: Handle to specify which secondary address to get
902 * @default_addr: Used as a fallback if no secondary address was specified
903 * Context: can sleep
904 *
905 * I2C clients can be composed of multiple I2C slaves bound together in a single
906 * component. The I2C client driver then binds to the master I2C slave and needs
907 * to create I2C dummy clients to communicate with all the other slaves.
908 *
909 * This function creates and returns an I2C dummy client whose I2C address is
910 * retrieved from the platform firmware based on the given slave name. If no
911 * address is specified by the firmware default_addr is used.
912 *
913 * On DT-based platforms the address is retrieved from the "reg" property entry
914 * cell whose "reg-names" value matches the slave name.
915 *
916 * This returns the new i2c client, which should be saved for later use with
917 * i2c_unregister_device(); or NULL to indicate an error.
918 */
919struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
920 const char *name,
921 u16 default_addr)
922{
923 struct device_node *np = client->dev.of_node;
924 u32 addr = default_addr;
925 int i;
926
927 if (np) {
928 i = of_property_match_string(np, "reg-names", name);
929 if (i >= 0)
930 of_property_read_u32_index(np, "reg", i, &addr);
931 }
932
933 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
934 return i2c_new_dummy(client->adapter, addr);
935}
936EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
937
David Brownellf37dd802007-02-13 22:09:00 +0100938/* ------------------------------------------------------------------------- */
939
David Brownell16ffadf2007-05-01 23:26:28 +0200940/* I2C bus adapters -- one roots each I2C or SMBUS segment */
941
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200942static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943{
David Brownellef2c83212007-05-01 23:26:28 +0200944 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 complete(&adap->dev_released);
946}
947
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +0200948unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +0200949{
950 unsigned int depth = 0;
951
952 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
953 depth++;
954
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +0200955 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
956 "adapter depth exceeds lockdep subclass limit\n");
957
Jean Delvare390946b2012-09-10 10:14:02 +0200958 return depth;
959}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +0200960EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +0200961
962/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200963 * Let users instantiate I2C devices through sysfs. This can be used when
964 * platform initialization code doesn't contain the proper data for
965 * whatever reason. Also useful for drivers that do device detection and
966 * detection fails, either because the device uses an unexpected address,
967 * or this is a compatible device with different ID register values.
968 *
969 * Parameter checking may look overzealous, but we really don't want
970 * the user to provide incorrect parameters.
971 */
972static ssize_t
973i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
974 const char *buf, size_t count)
975{
976 struct i2c_adapter *adap = to_i2c_adapter(dev);
977 struct i2c_board_info info;
978 struct i2c_client *client;
979 char *blank, end;
980 int res;
981
Jean Delvare99cd8e22009-06-19 16:58:20 +0200982 memset(&info, 0, sizeof(struct i2c_board_info));
983
984 blank = strchr(buf, ' ');
985 if (!blank) {
986 dev_err(dev, "%s: Missing parameters\n", "new_device");
987 return -EINVAL;
988 }
989 if (blank - buf > I2C_NAME_SIZE - 1) {
990 dev_err(dev, "%s: Invalid device name\n", "new_device");
991 return -EINVAL;
992 }
993 memcpy(info.type, buf, blank - buf);
994
995 /* Parse remaining parameters, reject extra parameters */
996 res = sscanf(++blank, "%hi%c", &info.addr, &end);
997 if (res < 1) {
998 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
999 return -EINVAL;
1000 }
1001 if (res > 1 && end != '\n') {
1002 dev_err(dev, "%s: Extra parameters\n", "new_device");
1003 return -EINVAL;
1004 }
1005
Wolfram Sangcfa03272015-07-27 14:03:38 +02001006 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
1007 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
1008 info.flags |= I2C_CLIENT_TEN;
1009 }
1010
1011 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
1012 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1013 info.flags |= I2C_CLIENT_SLAVE;
1014 }
1015
Jean Delvare99cd8e22009-06-19 16:58:20 +02001016 client = i2c_new_device(adap, &info);
1017 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001018 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001019
1020 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001021 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001022 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001023 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001024 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1025 info.type, info.addr);
1026
1027 return count;
1028}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001029static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001030
1031/*
1032 * And of course let the users delete the devices they instantiated, if
1033 * they got it wrong. This interface can only be used to delete devices
1034 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1035 * don't delete devices to which some kernel code still has references.
1036 *
1037 * Parameter checking may look overzealous, but we really don't want
1038 * the user to delete the wrong device.
1039 */
1040static ssize_t
1041i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1042 const char *buf, size_t count)
1043{
1044 struct i2c_adapter *adap = to_i2c_adapter(dev);
1045 struct i2c_client *client, *next;
1046 unsigned short addr;
1047 char end;
1048 int res;
1049
1050 /* Parse parameters, reject extra parameters */
1051 res = sscanf(buf, "%hi%c", &addr, &end);
1052 if (res < 1) {
1053 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1054 return -EINVAL;
1055 }
1056 if (res > 1 && end != '\n') {
1057 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1058 return -EINVAL;
1059 }
1060
1061 /* Make sure the device was added through sysfs */
1062 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001063 mutex_lock_nested(&adap->userspace_clients_lock,
1064 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001065 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1066 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001067 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001068 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1069 "delete_device", client->name, client->addr);
1070
1071 list_del(&client->detected);
1072 i2c_unregister_device(client);
1073 res = count;
1074 break;
1075 }
1076 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001077 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001078
1079 if (res < 0)
1080 dev_err(dev, "%s: Can't find device in list\n",
1081 "delete_device");
1082 return res;
1083}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001084static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1085 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001086
1087static struct attribute *i2c_adapter_attrs[] = {
1088 &dev_attr_name.attr,
1089 &dev_attr_new_device.attr,
1090 &dev_attr_delete_device.attr,
1091 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001092};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001093ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001094
Michael Lawnick08263742010-08-11 18:21:02 +02001095struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001096 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001097 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001098};
Michael Lawnick08263742010-08-11 18:21:02 +02001099EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Stephen Warren643dd092012-04-17 12:43:33 -06001101/**
1102 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1103 * @dev: device, probably from some driver model iterator
1104 *
1105 * When traversing the driver model tree, perhaps using driver model
1106 * iterators like @device_for_each_child(), you can't assume very much
1107 * about the nodes you find. Use this function to avoid oopses caused
1108 * by wrongly treating some non-I2C device as an i2c_adapter.
1109 */
1110struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1111{
1112 return (dev->type == &i2c_adapter_type)
1113 ? to_i2c_adapter(dev)
1114 : NULL;
1115}
1116EXPORT_SYMBOL(i2c_verify_adapter);
1117
Jean Delvare2bb50952009-09-18 22:45:46 +02001118#ifdef CONFIG_I2C_COMPAT
1119static struct class_compat *i2c_adapter_compat_class;
1120#endif
1121
David Brownell9c1600e2007-05-01 23:26:31 +02001122static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1123{
1124 struct i2c_devinfo *devinfo;
1125
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001126 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001127 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1128 if (devinfo->busnum == adapter->nr
1129 && !i2c_new_device(adapter,
1130 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001131 dev_err(&adapter->dev,
1132 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001133 devinfo->board_info.addr);
1134 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001135 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001136}
1137
Jean Delvare69b00892009-12-06 17:06:27 +01001138static int i2c_do_add_adapter(struct i2c_driver *driver,
1139 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001140{
Jean Delvare4735c982008-07-14 22:38:36 +02001141 /* Detect supported devices on that bus, and instantiate them */
1142 i2c_detect(adap, driver);
1143
1144 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001145 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001146 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1147 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001148 dev_warn(&adap->dev,
1149 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001150 /* We ignore the return code; if it fails, too bad */
1151 driver->attach_adapter(adap);
1152 }
1153 return 0;
1154}
1155
Jean Delvare69b00892009-12-06 17:06:27 +01001156static int __process_new_adapter(struct device_driver *d, void *data)
1157{
1158 return i2c_do_add_adapter(to_i2c_driver(d), data);
1159}
1160
Peter Rosind1ed7982016-08-25 23:07:01 +02001161static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1162 .lock_bus = i2c_adapter_lock_bus,
1163 .trylock_bus = i2c_adapter_trylock_bus,
1164 .unlock_bus = i2c_adapter_unlock_bus,
1165};
1166
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001167static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap)
1168{
1169 struct irq_domain *domain = adap->host_notify_domain;
1170 irq_hw_number_t hwirq;
1171
1172 if (!domain)
1173 return;
1174
1175 for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++)
1176 irq_dispose_mapping(irq_find_mapping(domain, hwirq));
1177
1178 irq_domain_remove(domain);
1179 adap->host_notify_domain = NULL;
1180}
1181
1182static int i2c_host_notify_irq_map(struct irq_domain *h,
1183 unsigned int virq,
1184 irq_hw_number_t hw_irq_num)
1185{
1186 irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
1187
1188 return 0;
1189}
1190
1191static const struct irq_domain_ops i2c_host_notify_irq_ops = {
1192 .map = i2c_host_notify_irq_map,
1193};
1194
1195static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap)
1196{
1197 struct irq_domain *domain;
1198
1199 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
1200 return 0;
1201
1202 domain = irq_domain_create_linear(adap->dev.fwnode,
1203 I2C_ADDR_7BITS_COUNT,
1204 &i2c_host_notify_irq_ops, adap);
1205 if (!domain)
1206 return -ENOMEM;
1207
1208 adap->host_notify_domain = domain;
1209
1210 return 0;
1211}
1212
1213/**
1214 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
1215 * I2C client.
1216 * @adap: the adapter
1217 * @addr: the I2C address of the notifying device
1218 * Context: can't sleep
1219 *
1220 * Helper function to be called from an I2C bus driver's interrupt
1221 * handler. It will schedule the Host Notify IRQ.
1222 */
1223int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
1224{
1225 int irq;
1226
1227 if (!adap)
1228 return -EINVAL;
1229
1230 irq = irq_find_mapping(adap->host_notify_domain, addr);
1231 if (irq <= 0)
1232 return -ENXIO;
1233
1234 generic_handle_irq(irq);
1235
1236 return 0;
1237}
1238EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
1239
David Brownell6e13e642007-05-01 23:26:31 +02001240static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001242 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
David Brownell1d0b19c2008-10-14 17:30:05 +02001244 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301245 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001246 res = -EAGAIN;
1247 goto out_list;
1248 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001249
Jean Delvare2236baa2010-11-15 22:40:38 +01001250 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001251 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001252 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001253
1254 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001255 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001256 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01001257 }
1258
Peter Rosind1ed7982016-08-25 23:07:01 +02001259 if (!adap->lock_ops)
1260 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001261
Mika Kuoppala194684e2009-12-06 17:06:22 +01001262 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001263 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001264 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001265 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Jean Delvare8fcfef62009-03-28 21:34:43 +01001267 /* Set default timeout to 1 second if not already set */
1268 if (adap->timeout == 0)
1269 adap->timeout = HZ;
1270
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001271 /* register soft irqs for Host Notify */
1272 res = i2c_setup_host_notify_irq_domain(adap);
1273 if (res) {
1274 pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n",
1275 adap->name, res);
1276 goto out_list;
1277 }
1278
Kay Sievers27d9c182009-01-07 14:29:16 +01001279 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001280 adap->dev.bus = &i2c_bus_type;
1281 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001282 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001283 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001284 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001285 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001286 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Phil Reidf8756c62017-08-24 17:31:04 +08001288 res = of_i2c_setup_smbus_alert(adap);
1289 if (res)
1290 goto out_reg;
1291
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001292 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1293
Charles Keepax6ada5c12015-04-16 13:05:19 +01001294 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001295 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001296 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001297
Jean Delvare2bb50952009-09-18 22:45:46 +02001298#ifdef CONFIG_I2C_COMPAT
1299 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1300 adap->dev.parent);
1301 if (res)
1302 dev_warn(&adap->dev,
1303 "Failed to create compatibility class link\n");
1304#endif
1305
Wolfram Sangd3b11d82016-07-09 13:34:59 +09001306 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301307
Jean Delvare729d6dd2009-06-19 16:58:18 +02001308 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001309 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001310 i2c_acpi_register_devices(adap);
1311 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001312
David Brownell6e13e642007-05-01 23:26:31 +02001313 if (adap->nr < __i2c_first_dynamic_bus_num)
1314 i2c_scan_static_board_info(adap);
1315
Jean Delvare4735c982008-07-14 22:38:36 +02001316 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001317 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001318 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001319 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001320
1321 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001322
Phil Reidf8756c62017-08-24 17:31:04 +08001323out_reg:
1324 init_completion(&adap->dev_released);
1325 device_unregister(&adap->dev);
1326 wait_for_completion(&adap->dev_released);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001327out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001328 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001329 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001330 mutex_unlock(&core_lock);
1331 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332}
1333
David Brownell6e13e642007-05-01 23:26:31 +02001334/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001335 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1336 * @adap: the adapter to register (with adap->nr initialized)
1337 * Context: can sleep
1338 *
1339 * See i2c_add_numbered_adapter() for details.
1340 */
1341static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1342{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001343 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001344
1345 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001346 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001347 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001348 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001349 return id == -ENOSPC ? -EBUSY : id;
1350
1351 return i2c_register_adapter(adap);
1352}
1353
1354/**
David Brownell6e13e642007-05-01 23:26:31 +02001355 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1356 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001357 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001358 *
1359 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001360 * doesn't matter or when its bus number is specified by an dt alias.
1361 * Examples of bases when the bus number doesn't matter: I2C adapters
1362 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001363 *
1364 * When this returns zero, a new bus number was allocated and stored
1365 * in adap->nr, and the specified adapter became available for clients.
1366 * Otherwise, a negative errno value is returned.
1367 */
1368int i2c_add_adapter(struct i2c_adapter *adapter)
1369{
Doug Andersonee5c2742013-03-01 08:57:31 -08001370 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001371 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001372
Doug Andersonee5c2742013-03-01 08:57:31 -08001373 if (dev->of_node) {
1374 id = of_alias_get_id(dev->of_node, "i2c");
1375 if (id >= 0) {
1376 adapter->nr = id;
1377 return __i2c_add_numbered_adapter(adapter);
1378 }
1379 }
1380
Jean Delvarecaada322008-01-27 18:14:49 +01001381 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001382 id = idr_alloc(&i2c_adapter_idr, adapter,
1383 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001384 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001385 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08001386 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001387
1388 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001389
David Brownell6e13e642007-05-01 23:26:31 +02001390 return i2c_register_adapter(adapter);
1391}
1392EXPORT_SYMBOL(i2c_add_adapter);
1393
1394/**
1395 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1396 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001397 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001398 *
1399 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001400 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1401 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001402 * is used to properly configure I2C devices.
1403 *
Grant Likely488bf312011-07-25 17:49:43 +02001404 * If the requested bus number is set to -1, then this function will behave
1405 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1406 *
David Brownell6e13e642007-05-01 23:26:31 +02001407 * If no devices have pre-been declared for this bus, then be sure to
1408 * register the adapter before any dynamically allocated ones. Otherwise
1409 * the required bus ID may not be available.
1410 *
1411 * When this returns zero, the specified adapter became available for
1412 * clients using the bus number provided in adap->nr. Also, the table
1413 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1414 * and the appropriate driver model device nodes are created. Otherwise, a
1415 * negative errno value is returned.
1416 */
1417int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1418{
Grant Likely488bf312011-07-25 17:49:43 +02001419 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1420 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001421
Doug Andersonee5c2742013-03-01 08:57:31 -08001422 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001423}
1424EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1425
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001426static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001427 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001428{
Jean Delvare4735c982008-07-14 22:38:36 +02001429 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001430
Jean Delvareacec2112009-03-28 21:34:40 +01001431 /* Remove the devices we created ourselves as the result of hardware
1432 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001433 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1434 if (client->adapter == adapter) {
1435 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1436 client->name, client->addr);
1437 list_del(&client->detected);
1438 i2c_unregister_device(client);
1439 }
1440 }
Jean Delvare026526f2008-01-27 18:14:49 +01001441}
1442
Jean Delvaree549c2b2009-06-19 16:58:19 +02001443static int __unregister_client(struct device *dev, void *dummy)
1444{
1445 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001446 if (client && strcmp(client->name, "dummy"))
1447 i2c_unregister_device(client);
1448 return 0;
1449}
1450
1451static int __unregister_dummy(struct device *dev, void *dummy)
1452{
1453 struct i2c_client *client = i2c_verify_client(dev);
Andy Shevchenko7b43dd12017-10-31 16:21:35 +02001454 i2c_unregister_device(client);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001455 return 0;
1456}
1457
Jean Delvare69b00892009-12-06 17:06:27 +01001458static int __process_removed_adapter(struct device_driver *d, void *data)
1459{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001460 i2c_do_del_adapter(to_i2c_driver(d), data);
1461 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001462}
1463
David Brownelld64f73b2007-07-12 14:12:28 +02001464/**
1465 * i2c_del_adapter - unregister I2C adapter
1466 * @adap: the adapter being unregistered
1467 * Context: can sleep
1468 *
1469 * This unregisters an I2C adapter which was previously registered
1470 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1471 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001472void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001474 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001475 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001478 mutex_lock(&core_lock);
1479 found = idr_find(&i2c_adapter_idr, adap->nr);
1480 mutex_unlock(&core_lock);
1481 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001482 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001483 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 }
1485
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001486 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001487 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001488 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001489 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001490 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001491 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001493 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001494 mutex_lock_nested(&adap->userspace_clients_lock,
1495 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001496 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1497 detected) {
1498 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1499 client->addr);
1500 list_del(&client->detected);
1501 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001502 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001503 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001504
Jean Delvaree549c2b2009-06-19 16:58:19 +02001505 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001506 * check the returned value. This is a two-pass process, because
1507 * we can't remove the dummy devices during the first pass: they
1508 * could have been instantiated by real devices wishing to clean
1509 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001510 device_for_each_child(&adap->dev, NULL, __unregister_client);
1511 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Jean Delvare2bb50952009-09-18 22:45:46 +02001513#ifdef CONFIG_I2C_COMPAT
1514 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1515 adap->dev.parent);
1516#endif
1517
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001518 /* device name is gone after device_unregister */
1519 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1520
Wolfram Sang9f924162015-12-23 18:19:28 +01001521 pm_runtime_disable(&adap->dev);
1522
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001523 i2c_host_notify_irq_teardown(adap);
1524
Wolfram Sang26680ee2015-01-29 22:45:09 +01001525 /* wait until all references to the device are gone
1526 *
1527 * FIXME: This is old code and should ideally be replaced by an
1528 * alternative which results in decoupling the lifetime of the struct
1529 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301530 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001531 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
David Brownell6e13e642007-05-01 23:26:31 +02001536 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001537 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001539 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001541 /* Clear the device structure in case this adapter is ever going to be
1542 added again */
1543 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
David Brownellc0564602007-05-01 23:26:31 +02001545EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Wolfram Sang54177cc2015-12-17 13:32:36 +01001547/**
1548 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
1549 * @dev: The device to scan for I2C timing properties
1550 * @t: the i2c_timings struct to be filled with values
1551 * @use_defaults: bool to use sane defaults derived from the I2C specification
1552 * when properties are not found, otherwise use 0
1553 *
1554 * Scan the device for the generic I2C properties describing timing parameters
1555 * for the signal and fill the given struct with the results. If a property was
1556 * not found and use_defaults was true, then maximum timings are assumed which
1557 * are derived from the I2C specification. If use_defaults is not used, the
1558 * results will be 0, so drivers can apply their own defaults later. The latter
1559 * is mainly intended for avoiding regressions of existing drivers which want
1560 * to switch to this function. New drivers almost always should use the defaults.
1561 */
1562
1563void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
1564{
1565 int ret;
1566
1567 memset(t, 0, sizeof(*t));
1568
1569 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
1570 if (ret && use_defaults)
1571 t->bus_freq_hz = 100000;
1572
1573 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
1574 if (ret && use_defaults) {
1575 if (t->bus_freq_hz <= 100000)
1576 t->scl_rise_ns = 1000;
1577 else if (t->bus_freq_hz <= 400000)
1578 t->scl_rise_ns = 300;
1579 else
1580 t->scl_rise_ns = 120;
1581 }
1582
1583 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
1584 if (ret && use_defaults) {
1585 if (t->bus_freq_hz <= 400000)
1586 t->scl_fall_ns = 300;
1587 else
1588 t->scl_fall_ns = 120;
1589 }
1590
1591 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
1592
1593 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
1594 if (ret && use_defaults)
1595 t->sda_fall_ns = t->scl_fall_ns;
1596}
1597EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
1598
David Brownell7b4fbc52007-05-01 23:26:30 +02001599/* ------------------------------------------------------------------------- */
1600
Jean Delvare7ae31482011-03-20 14:50:52 +01001601int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1602{
1603 int res;
1604
1605 mutex_lock(&core_lock);
1606 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1607 mutex_unlock(&core_lock);
1608
1609 return res;
1610}
1611EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1612
Jean Delvare69b00892009-12-06 17:06:27 +01001613static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001614{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001615 if (dev->type != &i2c_adapter_type)
1616 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001617 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001618}
1619
David Brownell7b4fbc52007-05-01 23:26:30 +02001620/*
1621 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001622 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 */
1624
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001625int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001627 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
David Brownell1d0b19c2008-10-14 17:30:05 +02001629 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301630 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02001631 return -EAGAIN;
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001634 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02001636 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Jean Delvare729d6dd2009-06-19 16:58:18 +02001638 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001639 * will have called probe() for all matching-but-unbound devices.
1640 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 res = driver_register(&driver->driver);
1642 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001643 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001644
Wolfram Sang44239a52016-07-09 13:35:04 +09001645 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Jean Delvare4735c982008-07-14 22:38:36 +02001647 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001648 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001649
Jean Delvare7eebcb72006-02-05 23:28:21 +01001650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001652EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Jean Delvare69b00892009-12-06 17:06:27 +01001654static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001655{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001656 if (dev->type == &i2c_adapter_type)
1657 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1658 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001659}
1660
David Brownella1d9e6e2007-05-01 23:26:30 +02001661/**
1662 * i2c_del_driver - unregister I2C driver
1663 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001664 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001665 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001666void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
Jean Delvare7ae31482011-03-20 14:50:52 +01001668 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09001671 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
David Brownellc0564602007-05-01 23:26:31 +02001673EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
David Brownell7b4fbc52007-05-01 23:26:30 +02001675/* ------------------------------------------------------------------------- */
1676
Jean Delvaree48d3312008-01-27 18:14:48 +01001677/**
1678 * i2c_use_client - increments the reference count of the i2c client structure
1679 * @client: the client being referenced
1680 *
1681 * Each live reference to a client should be refcounted. The driver model does
1682 * that automatically as part of driver binding, so that most drivers don't
1683 * need to do this explicitly: they hold a reference until they're unbound
1684 * from the device.
1685 *
1686 * A pointer to the client with the incremented reference counter is returned.
1687 */
1688struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
David Brownell6ea438e2008-07-14 22:38:24 +02001690 if (client && get_device(&client->dev))
1691 return client;
1692 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
David Brownellc0564602007-05-01 23:26:31 +02001694EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Jean Delvaree48d3312008-01-27 18:14:48 +01001696/**
1697 * i2c_release_client - release a use of the i2c client structure
1698 * @client: the client being no longer referenced
1699 *
1700 * Must be called when a user of a client is finished with it.
1701 */
1702void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
David Brownell6ea438e2008-07-14 22:38:24 +02001704 if (client)
1705 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
David Brownellc0564602007-05-01 23:26:31 +02001707EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
David Brownell9b766b82008-01-27 18:14:51 +01001709struct i2c_cmd_arg {
1710 unsigned cmd;
1711 void *arg;
1712};
1713
1714static int i2c_cmd(struct device *dev, void *_arg)
1715{
1716 struct i2c_client *client = i2c_verify_client(dev);
1717 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001718 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001719
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001720 if (!client || !client->dev.driver)
1721 return 0;
1722
1723 driver = to_i2c_driver(client->dev.driver);
1724 if (driver->command)
1725 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001726 return 0;
1727}
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1730{
David Brownell9b766b82008-01-27 18:14:51 +01001731 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
David Brownell9b766b82008-01-27 18:14:51 +01001733 cmd_arg.cmd = cmd;
1734 cmd_arg.arg = arg;
1735 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736}
David Brownellc0564602007-05-01 23:26:31 +02001737EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739static int __init i2c_init(void)
1740{
1741 int retval;
1742
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001743 retval = of_alias_get_highest_id("i2c");
1744
1745 down_write(&__i2c_board_lock);
1746 if (retval >= __i2c_first_dynamic_bus_num)
1747 __i2c_first_dynamic_bus_num = retval + 1;
1748 up_write(&__i2c_board_lock);
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 retval = bus_register(&i2c_bus_type);
1751 if (retval)
1752 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01001753
1754 is_registered = true;
1755
Jean Delvare2bb50952009-09-18 22:45:46 +02001756#ifdef CONFIG_I2C_COMPAT
1757 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1758 if (!i2c_adapter_compat_class) {
1759 retval = -ENOMEM;
1760 goto bus_err;
1761 }
1762#endif
David Brownelle9f13732008-01-27 18:14:52 +01001763 retval = i2c_add_driver(&dummy_driver);
1764 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001765 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001766
1767 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1768 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001769 if (IS_ENABLED(CONFIG_ACPI))
1770 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001771
David Brownelle9f13732008-01-27 18:14:52 +01001772 return 0;
1773
Jean Delvare2bb50952009-09-18 22:45:46 +02001774class_err:
1775#ifdef CONFIG_I2C_COMPAT
1776 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001777bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001778#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01001779 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01001780 bus_unregister(&i2c_bus_type);
1781 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
1784static void __exit i2c_exit(void)
1785{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001786 if (IS_ENABLED(CONFIG_ACPI))
1787 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001788 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1789 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001790 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001791#ifdef CONFIG_I2C_COMPAT
1792 class_compat_unregister(i2c_adapter_compat_class);
1793#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001795 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
David Brownella10f9e72008-10-14 17:30:06 +02001798/* We must initialize early, because some subsystems register i2c drivers
1799 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1800 */
1801postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802module_exit(i2c_exit);
1803
1804/* ----------------------------------------------------
1805 * the functional interface to the i2c busses.
1806 * ----------------------------------------------------
1807 */
1808
Wolfram Sangb7f62582015-01-05 23:45:59 +01001809/* Check if val is exceeding the quirk IFF quirk is non 0 */
1810#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1811
1812static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
1813{
1814 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
1815 err_msg, msg->addr, msg->len,
1816 msg->flags & I2C_M_RD ? "read" : "write");
1817 return -EOPNOTSUPP;
1818}
1819
1820static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1821{
1822 const struct i2c_adapter_quirks *q = adap->quirks;
1823 int max_num = q->max_num_msgs, i;
1824 bool do_len_check = true;
1825
1826 if (q->flags & I2C_AQ_COMB) {
1827 max_num = 2;
1828
1829 /* special checks for combined messages */
1830 if (num == 2) {
1831 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
1832 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
1833
1834 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
1835 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
1836
1837 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
1838 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
1839
1840 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
1841 return i2c_quirk_error(adap, &msgs[0], "msg too long");
1842
1843 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
1844 return i2c_quirk_error(adap, &msgs[1], "msg too long");
1845
1846 do_len_check = false;
1847 }
1848 }
1849
1850 if (i2c_quirk_exceeded(num, max_num))
1851 return i2c_quirk_error(adap, &msgs[0], "too many messages");
1852
1853 for (i = 0; i < num; i++) {
1854 u16 len = msgs[i].len;
1855
1856 if (msgs[i].flags & I2C_M_RD) {
1857 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
1858 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1859 } else {
1860 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
1861 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1862 }
1863 }
1864
1865 return 0;
1866}
1867
David Brownella1cdeda2008-07-14 22:38:24 +02001868/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001869 * __i2c_transfer - unlocked flavor of i2c_transfer
1870 * @adap: Handle to I2C bus
1871 * @msgs: One or more messages to execute before STOP is issued to
1872 * terminate the operation; each message begins with a START.
1873 * @num: Number of messages to be executed.
1874 *
1875 * Returns negative errno, else the number of messages executed.
1876 *
1877 * Adapter lock must be held when calling this function. No debug logging
1878 * takes place. adap->algo->master_xfer existence isn't checked.
1879 */
1880int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1881{
1882 unsigned long orig_jiffies;
1883 int ret, try;
1884
Wolfram Sangb7f62582015-01-05 23:45:59 +01001885 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
1886 return -EOPNOTSUPP;
1887
David Howellsd9a83d62014-03-06 13:35:59 +00001888 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
1889 * enabled. This is an efficient way of keeping the for-loop from
1890 * being executed when not needed.
1891 */
1892 if (static_key_false(&i2c_trace_msg)) {
1893 int i;
1894 for (i = 0; i < num; i++)
1895 if (msgs[i].flags & I2C_M_RD)
1896 trace_i2c_read(adap, &msgs[i], i);
1897 else
1898 trace_i2c_write(adap, &msgs[i], i);
1899 }
1900
Jean Delvareb37d2a32012-06-29 07:47:19 -03001901 /* Retry automatically on arbitration loss */
1902 orig_jiffies = jiffies;
1903 for (ret = 0, try = 0; try <= adap->retries; try++) {
1904 ret = adap->algo->master_xfer(adap, msgs, num);
1905 if (ret != -EAGAIN)
1906 break;
1907 if (time_after(jiffies, orig_jiffies + adap->timeout))
1908 break;
1909 }
1910
David Howellsd9a83d62014-03-06 13:35:59 +00001911 if (static_key_false(&i2c_trace_msg)) {
1912 int i;
1913 for (i = 0; i < ret; i++)
1914 if (msgs[i].flags & I2C_M_RD)
1915 trace_i2c_reply(adap, &msgs[i], i);
1916 trace_i2c_result(adap, i, ret);
1917 }
1918
Jean Delvareb37d2a32012-06-29 07:47:19 -03001919 return ret;
1920}
1921EXPORT_SYMBOL(__i2c_transfer);
1922
1923/**
David Brownella1cdeda2008-07-14 22:38:24 +02001924 * i2c_transfer - execute a single or combined I2C message
1925 * @adap: Handle to I2C bus
1926 * @msgs: One or more messages to execute before STOP is issued to
1927 * terminate the operation; each message begins with a START.
1928 * @num: Number of messages to be executed.
1929 *
1930 * Returns negative errno, else the number of messages executed.
1931 *
1932 * Note that there is no requirement that each message be sent to
1933 * the same slave address, although that is the most common model.
1934 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001935int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001937 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
David Brownella1cdeda2008-07-14 22:38:24 +02001939 /* REVISIT the fault reporting model here is weak:
1940 *
1941 * - When we get an error after receiving N bytes from a slave,
1942 * there is no way to report "N".
1943 *
1944 * - When we get a NAK after transmitting N bytes to a slave,
1945 * there is no way to report "N" ... or to let the master
1946 * continue executing the rest of this combined message, if
1947 * that's the appropriate response.
1948 *
1949 * - When for example "num" is two and we successfully complete
1950 * the first message but get an error part way through the
1951 * second, it's unclear whether that should be reported as
1952 * one (discarding status on the second message) or errno
1953 * (discarding status on the first one).
1954 */
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (adap->algo->master_xfer) {
1957#ifdef DEBUG
1958 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001959 dev_dbg(&adap->dev,
1960 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
1961 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
1962 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02001963 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965#endif
1966
Mike Rapoportcea443a82008-01-27 18:14:50 +01001967 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02001968 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001969 if (!ret)
1970 /* I2C activity is ongoing. */
1971 return -EAGAIN;
1972 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02001973 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001974 }
1975
Jean Delvareb37d2a32012-06-29 07:47:19 -03001976 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02001977 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 return ret;
1980 } else {
1981 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001982 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984}
David Brownellc0564602007-05-01 23:26:31 +02001985EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
David Brownella1cdeda2008-07-14 22:38:24 +02001987/**
1988 * i2c_master_send - issue a single I2C message in master transmit mode
1989 * @client: Handle to slave device
1990 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001991 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001992 *
1993 * Returns negative errno, or else the number of bytes written.
1994 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001995int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
1997 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001998 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 struct i2c_msg msg;
2000
Jean Delvare815f55f2005-05-07 22:58:46 +02002001 msg.addr = client->addr;
2002 msg.flags = client->flags & I2C_M_TEN;
2003 msg.len = count;
2004 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01002005
Jean Delvare815f55f2005-05-07 22:58:46 +02002006 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002008 /*
2009 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2010 * transmitted, else error code.
2011 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002012 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013}
David Brownellc0564602007-05-01 23:26:31 +02002014EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
David Brownella1cdeda2008-07-14 22:38:24 +02002016/**
2017 * i2c_master_recv - issue a single I2C message in master receive mode
2018 * @client: Handle to slave device
2019 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002020 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002021 *
2022 * Returns negative errno, or else the number of bytes read.
2023 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002024int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Farid Hammane7225acf2010-05-21 18:40:58 +02002026 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 struct i2c_msg msg;
2028 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Jean Delvare815f55f2005-05-07 22:58:46 +02002030 msg.addr = client->addr;
2031 msg.flags = client->flags & I2C_M_TEN;
2032 msg.flags |= I2C_M_RD;
2033 msg.len = count;
2034 msg.buf = buf;
2035
2036 ret = i2c_transfer(adap, &msg, 1);
2037
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002038 /*
2039 * If everything went ok (i.e. 1 msg received), return #bytes received,
2040 * else error code.
2041 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002042 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
David Brownellc0564602007-05-01 23:26:31 +02002044EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046/* ----------------------------------------------------
2047 * the i2c address scanning function
2048 * Will not work for 10-bit addresses!
2049 * ----------------------------------------------------
2050 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002051
Jean Delvare63e4e802010-06-03 11:33:51 +02002052/*
2053 * Legacy default probe function, mostly relevant for SMBus. The default
2054 * probe method is a quick write, but it is known to corrupt the 24RF08
2055 * EEPROMs due to a state machine bug, and could also irreversibly
2056 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2057 * we use a short byte read instead. Also, some bus drivers don't implement
2058 * quick write, so we fallback to a byte read in that case too.
2059 * On x86, there is another special case for FSC hardware monitoring chips,
2060 * which want regular byte reads (address 0x73.) Fortunately, these are the
2061 * only known chips using this I2C address on PC hardware.
2062 * Returns 1 if probe succeeded, 0 if not.
2063 */
2064static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2065{
2066 int err;
2067 union i2c_smbus_data dummy;
2068
2069#ifdef CONFIG_X86
2070 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2071 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2072 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2073 I2C_SMBUS_BYTE_DATA, &dummy);
2074 else
2075#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002076 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2077 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002078 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2079 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002080 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2081 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2082 I2C_SMBUS_BYTE, &dummy);
2083 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002084 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2085 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002086 err = -EOPNOTSUPP;
2087 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002088
2089 return err >= 0;
2090}
2091
Jean Delvareccfbbd02009-12-06 17:06:25 +01002092static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002093 struct i2c_driver *driver)
2094{
2095 struct i2c_board_info info;
2096 struct i2c_adapter *adapter = temp_client->adapter;
2097 int addr = temp_client->addr;
2098 int err;
2099
2100 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002101 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002102 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002103 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2104 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002105 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002106 }
2107
Wolfram Sang9bccc702015-07-17 14:48:56 +02002108 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002109 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002110 return 0;
2111
Jean Delvareccfbbd02009-12-06 17:06:25 +01002112 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002113 if (!i2c_default_probe(adapter, addr))
2114 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002115
2116 /* Finally call the custom detection function */
2117 memset(&info, 0, sizeof(struct i2c_board_info));
2118 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002119 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002120 if (err) {
2121 /* -ENODEV is returned if the detection fails. We catch it
2122 here as this isn't an error. */
2123 return err == -ENODEV ? 0 : err;
2124 }
2125
2126 /* Consistency check */
2127 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002128 dev_err(&adapter->dev,
2129 "%s detection function provided no name for 0x%x\n",
2130 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002131 } else {
2132 struct i2c_client *client;
2133
2134 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002135 if (adapter->class & I2C_CLASS_DEPRECATED)
2136 dev_warn(&adapter->dev,
2137 "This adapter will soon drop class based instantiation of devices. "
2138 "Please make sure client 0x%02x gets instantiated by other means. "
2139 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2140 info.addr);
2141
Jean Delvare4735c982008-07-14 22:38:36 +02002142 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2143 info.type, info.addr);
2144 client = i2c_new_device(adapter, &info);
2145 if (client)
2146 list_add_tail(&client->detected, &driver->clients);
2147 else
2148 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2149 info.type, info.addr);
2150 }
2151 return 0;
2152}
2153
2154static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2155{
Jean Delvarec3813d62009-12-14 21:17:25 +01002156 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002157 struct i2c_client *temp_client;
2158 int i, err = 0;
2159 int adap_id = i2c_adapter_id(adapter);
2160
Jean Delvarec3813d62009-12-14 21:17:25 +01002161 address_list = driver->address_list;
2162 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002163 return 0;
2164
Wolfram Sang45552272014-07-10 13:46:21 +02002165 /* Warn that the adapter lost class based instantiation */
2166 if (adapter->class == I2C_CLASS_DEPRECATED) {
2167 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002168 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2169 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002170 driver->driver.name);
2171 return 0;
2172 }
2173
Jean Delvare51b54ba2010-10-24 18:16:58 +02002174 /* Stop here if the classes do not match */
2175 if (!(adapter->class & driver->class))
2176 return 0;
2177
Jean Delvare4735c982008-07-14 22:38:36 +02002178 /* Set up a temporary client to help detect callback */
2179 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2180 if (!temp_client)
2181 return -ENOMEM;
2182 temp_client->adapter = adapter;
2183
Jean Delvarec3813d62009-12-14 21:17:25 +01002184 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002185 dev_dbg(&adapter->dev,
2186 "found normal entry for adapter %d, addr 0x%02x\n",
2187 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002188 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002189 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002190 if (unlikely(err))
2191 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002192 }
2193
Jean Delvare4735c982008-07-14 22:38:36 +02002194 kfree(temp_client);
2195 return err;
2196}
2197
Jean Delvared44f19d2010-08-11 18:20:57 +02002198int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2199{
2200 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2201 I2C_SMBUS_QUICK, NULL) >= 0;
2202}
2203EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2204
Jean Delvare12b5053a2007-05-01 23:26:31 +02002205struct i2c_client *
2206i2c_new_probed_device(struct i2c_adapter *adap,
2207 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002208 unsigned short const *addr_list,
2209 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002210{
2211 int i;
2212
Jean Delvare8031d792010-08-11 18:21:00 +02002213 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002214 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002215
Jean Delvare12b5053a2007-05-01 23:26:31 +02002216 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2217 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002218 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002219 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2220 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002221 continue;
2222 }
2223
Wolfram Sang9bccc702015-07-17 14:48:56 +02002224 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002225 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002226 dev_dbg(&adap->dev,
2227 "Address 0x%02x already in use, not probing\n",
2228 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002229 continue;
2230 }
2231
Jean Delvare63e4e802010-06-03 11:33:51 +02002232 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002233 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002234 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002235 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002236
2237 if (addr_list[i] == I2C_CLIENT_END) {
2238 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2239 return NULL;
2240 }
2241
2242 info->addr = addr_list[i];
2243 return i2c_new_device(adap, info);
2244}
2245EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2246
Jean Delvared735b342011-03-20 14:50:52 +01002247struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002250
Jean Delvarecaada322008-01-27 18:14:49 +01002251 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002252 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002253 if (!adapter)
2254 goto exit;
2255
2256 if (try_module_get(adapter->owner))
2257 get_device(&adapter->dev);
2258 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002259 adapter = NULL;
2260
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002261 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002262 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002263 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264}
David Brownellc0564602007-05-01 23:26:31 +02002265EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
2267void i2c_put_adapter(struct i2c_adapter *adap)
2268{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002269 if (!adap)
2270 return;
2271
2272 put_device(&adap->dev);
2273 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
David Brownellc0564602007-05-01 23:26:31 +02002275EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2278MODULE_DESCRIPTION("I2C-Bus main module");
2279MODULE_LICENSE("GPL");