blob: 875d6cacaa17cf2284b89cbbd16e1eb5049f25ed [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>
32#include <linux/idr.h>
33#include <linux/init.h>
34#include <linux/irqflags.h>
35#include <linux/jump_label.h>
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/mutex.h>
39#include <linux/of_device.h>
40#include <linux/of.h>
41#include <linux/of_irq.h>
42#include <linux/pm_domain.h>
43#include <linux/pm_runtime.h>
44#include <linux/pm_wakeirq.h>
Wolfram Sange1dba012015-12-08 10:37:46 +010045#include <linux/property.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010046#include <linux/rwsem.h>
47#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
David Brownell9c1600e2007-05-01 23:26:31 +020049#include "i2c-core.h"
50
David Howellsd9a83d62014-03-06 13:35:59 +000051#define CREATE_TRACE_POINTS
52#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Wolfram Sangda899f52015-05-18 21:09:12 +020054#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
55#define I2C_ADDR_OFFSET_SLAVE 0x1000
56
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +020057#define I2C_ADDR_7BITS_MAX 0x77
58#define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1)
59
Wolfram Sang61e3d0f2017-05-23 19:23:11 +020060/*
61 * core_lock protects i2c_adapter_idr, and guarantees that device detection,
62 * deletion of detected devices, and attach_adapter calls are serialized
63 */
Jean Delvarecaada322008-01-27 18:14:49 +010064static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static DEFINE_IDR(i2c_adapter_idr);
66
Jean Delvare4735c982008-07-14 22:38:36 +020067static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010068
David Howellsd9a83d62014-03-06 13:35:59 +000069static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
Sudip Mukherjee95026652016-03-07 17:19:17 +053070static bool is_registered;
David Howellsd9a83d62014-03-06 13:35:59 +000071
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050072int i2c_transfer_trace_reg(void)
David Howellsd9a83d62014-03-06 13:35:59 +000073{
74 static_key_slow_inc(&i2c_trace_msg);
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050075 return 0;
David Howellsd9a83d62014-03-06 13:35:59 +000076}
77
78void i2c_transfer_trace_unreg(void)
79{
80 static_key_slow_dec(&i2c_trace_msg);
81}
82
Lee Jones5f441fc2016-11-07 12:47:40 +000083const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
Jean Delvared2653e92008-04-29 23:11:39 +020084 const struct i2c_client *client)
85{
Lee Jones811073b2016-11-07 12:47:36 +000086 if (!(id && client))
87 return NULL;
88
Jean Delvared2653e92008-04-29 23:11:39 +020089 while (id->name[0]) {
90 if (strcmp(client->name, id->name) == 0)
91 return id;
92 id++;
93 }
94 return NULL;
95}
Lee Jones5f441fc2016-11-07 12:47:40 +000096EXPORT_SYMBOL_GPL(i2c_match_id);
Jean Delvared2653e92008-04-29 23:11:39 +020097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static int i2c_device_match(struct device *dev, struct device_driver *drv)
99{
Jean Delvare51298d12009-09-18 22:45:45 +0200100 struct i2c_client *client = i2c_verify_client(dev);
101 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200102
Jean Delvare51298d12009-09-18 22:45:45 +0200103
Grant Likely959e85f2010-06-08 07:48:19 -0600104 /* Attempt an OF style match */
Lee Jonesda10c062016-11-07 12:47:39 +0000105 if (i2c_of_match_device(drv->of_match_table, client))
Grant Likely959e85f2010-06-08 07:48:19 -0600106 return 1;
107
Mika Westerberg907ddf82012-11-23 12:23:40 +0100108 /* Then ACPI style match */
109 if (acpi_driver_match_device(dev, drv))
110 return 1;
111
Jean Delvare51298d12009-09-18 22:45:45 +0200112 driver = to_i2c_driver(drv);
Lee Jones811073b2016-11-07 12:47:36 +0000113
114 /* Finally an I2C match */
115 if (i2c_match_id(driver->id_table, client))
116 return 1;
Jean Delvared2653e92008-04-29 23:11:39 +0200117
Jean Delvareeb8a7902008-05-18 20:49:41 +0200118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Kay Sievers7eff2e72007-08-14 15:15:12 +0200121static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200122{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100123 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800124 int rc;
125
126 rc = acpi_device_uevent_modalias(dev, env);
127 if (rc != -ENODEV)
128 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200129
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100130 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200131}
132
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530133/* i2c bus recovery routines */
134static int get_scl_gpio_value(struct i2c_adapter *adap)
135{
136 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
137}
138
139static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
140{
141 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
142}
143
144static int get_sda_gpio_value(struct i2c_adapter *adap)
145{
146 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
147}
148
149static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
150{
151 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
152 struct device *dev = &adap->dev;
153 int ret = 0;
154
155 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
156 GPIOF_OUT_INIT_HIGH, "i2c-scl");
157 if (ret) {
158 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
159 return ret;
160 }
161
162 if (bri->get_sda) {
163 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
164 /* work without SDA polling */
165 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
166 bri->sda_gpio);
167 bri->get_sda = NULL;
168 }
169 }
170
171 return ret;
172}
173
174static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
175{
176 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
177
178 if (bri->get_sda)
179 gpio_free(bri->sda_gpio);
180
181 gpio_free(bri->scl_gpio);
182}
183
184/*
185 * We are generating clock pulses. ndelay() determines durating of clk pulses.
186 * We will generate clock with rate 100 KHz and so duration of both clock levels
187 * is: delay in ns = (10^6 / 100) / 2
188 */
189#define RECOVERY_NDELAY 5000
190#define RECOVERY_CLK_CNT 9
191
192static int i2c_generic_recovery(struct i2c_adapter *adap)
193{
194 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
195 int i = 0, val = 1, ret = 0;
196
197 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300198 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530199
Jan Luebbe8b062602015-07-08 16:35:06 +0200200 bri->set_scl(adap, val);
201 ndelay(RECOVERY_NDELAY);
202
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530203 /*
204 * By this time SCL is high, as we need to give 9 falling-rising edges
205 */
206 while (i++ < RECOVERY_CLK_CNT * 2) {
207 if (val) {
208 /* Break if SDA is high */
209 if (bri->get_sda && bri->get_sda(adap))
210 break;
211 /* SCL shouldn't be low here */
212 if (!bri->get_scl(adap)) {
213 dev_err(&adap->dev,
214 "SCL is stuck low, exit recovery\n");
215 ret = -EBUSY;
216 break;
217 }
218 }
219
220 val = !val;
221 bri->set_scl(adap, val);
222 ndelay(RECOVERY_NDELAY);
223 }
224
225 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300226 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530227
228 return ret;
229}
230
231int i2c_generic_scl_recovery(struct i2c_adapter *adap)
232{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530233 return i2c_generic_recovery(adap);
234}
Mark Brownc1c21f42015-04-15 19:18:39 +0100235EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530236
237int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
238{
239 int ret;
240
241 ret = i2c_get_gpios_for_recovery(adap);
242 if (ret)
243 return ret;
244
245 ret = i2c_generic_recovery(adap);
246 i2c_put_gpios_for_recovery(adap);
247
248 return ret;
249}
Mark Brownc1c21f42015-04-15 19:18:39 +0100250EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530251
252int i2c_recover_bus(struct i2c_adapter *adap)
253{
254 if (!adap->bus_recovery_info)
255 return -EOPNOTSUPP;
256
257 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
258 return adap->bus_recovery_info->recover_bus(adap);
259}
Mark Brownc1c21f42015-04-15 19:18:39 +0100260EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530261
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900262static void i2c_init_recovery(struct i2c_adapter *adap)
263{
264 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
265 char *err_str;
266
267 if (!bri)
268 return;
269
270 if (!bri->recover_bus) {
271 err_str = "no recover_bus() found";
272 goto err;
273 }
274
275 /* Generic GPIO recovery */
276 if (bri->recover_bus == i2c_generic_gpio_recovery) {
277 if (!gpio_is_valid(bri->scl_gpio)) {
278 err_str = "invalid SCL gpio";
279 goto err;
280 }
281
282 if (gpio_is_valid(bri->sda_gpio))
283 bri->get_sda = get_sda_gpio_value;
284 else
285 bri->get_sda = NULL;
286
287 bri->get_scl = get_scl_gpio_value;
288 bri->set_scl = set_scl_gpio_value;
289 } else if (bri->recover_bus == i2c_generic_scl_recovery) {
290 /* Generic SCL recovery */
291 if (!bri->set_scl || !bri->get_scl) {
292 err_str = "no {get|set}_scl() found";
293 goto err;
294 }
295 }
296
297 return;
298 err:
299 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
300 adap->bus_recovery_info = NULL;
301}
302
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200303static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
304{
305 struct i2c_adapter *adap = client->adapter;
306 unsigned int irq;
307
308 if (!adap->host_notify_domain)
309 return -ENXIO;
310
311 if (client->flags & I2C_CLIENT_TEN)
312 return -EINVAL;
313
314 irq = irq_find_mapping(adap->host_notify_domain, client->addr);
315 if (!irq)
316 irq = irq_create_mapping(adap->host_notify_domain,
317 client->addr);
318
319 return irq > 0 ? irq : -ENXIO;
320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322static int i2c_device_probe(struct device *dev)
323{
Jean Delvare51298d12009-09-18 22:45:45 +0200324 struct i2c_client *client = i2c_verify_client(dev);
325 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100326 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200327
Jean Delvare51298d12009-09-18 22:45:45 +0200328 if (!client)
329 return 0;
330
Hans de Goeded1d84bb2017-04-05 00:03:34 +0200331 driver = to_i2c_driver(dev->driver);
332
333 if (!client->irq && !driver->disable_i2c_core_irq_mapping) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300334 int irq = -ENOENT;
335
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800336 if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
337 dev_dbg(dev, "Using Host Notify IRQ\n");
338 irq = i2c_smbus_host_notify_to_irq(client);
339 } else if (dev->of_node) {
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700340 irq = of_irq_get_byname(dev->of_node, "irq");
341 if (irq == -EINVAL || irq == -ENODATA)
342 irq = of_irq_get(dev->of_node, 0);
343 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300344 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700345 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100346 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200347 return irq;
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800348
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100349 if (irq < 0)
350 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200351
352 client->irq = irq;
353 }
354
Lee Jonesda10c062016-11-07 12:47:39 +0000355 /*
Javier Martinez Canillasf4b17a12017-08-09 11:21:28 +0200356 * An I2C ID table is not mandatory, if and only if, a suitable OF
357 * or ACPI ID table is supplied for the probing device.
Lee Jonesda10c062016-11-07 12:47:39 +0000358 */
359 if (!driver->id_table &&
Andy Shevchenkoc64ffff2017-07-17 17:13:28 +0300360 !i2c_acpi_match_device(dev->driver->acpi_match_table, client) &&
Lee Jonesda10c062016-11-07 12:47:39 +0000361 !i2c_of_match_device(dev->driver->of_match_table, client))
David Brownell7b4fbc52007-05-01 23:26:30 +0200362 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200363
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700364 if (client->flags & I2C_CLIENT_WAKE) {
365 int wakeirq = -ENOENT;
366
367 if (dev->of_node) {
368 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
369 if (wakeirq == -EPROBE_DEFER)
370 return wakeirq;
371 }
372
373 device_init_wakeup(&client->dev, true);
374
375 if (wakeirq > 0 && wakeirq != client->irq)
376 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
377 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200378 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700379 else
380 status = 0;
381
382 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300383 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700384 }
385
David Brownell7b4fbc52007-05-01 23:26:30 +0200386 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200387
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200388 status = of_clk_set_defaults(dev->of_node, false);
389 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700390 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200391
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200392 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +0100393 if (status == -EPROBE_DEFER)
394 goto err_clear_wakeup_irq;
395
Lee Jonesb8a1a4c2016-11-07 12:47:41 +0000396 /*
397 * When there are no more users of probe(),
398 * rename probe_new to probe.
399 */
400 if (driver->probe_new)
401 status = driver->probe_new(client);
402 else if (driver->probe)
403 status = driver->probe(client,
404 i2c_match_id(driver->id_table, client));
405 else
406 status = -EINVAL;
407
Kieran Bingham74cedd32015-10-12 21:54:43 +0100408 if (status)
409 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100410
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700411 return 0;
412
413err_detach_pm_domain:
414 dev_pm_domain_detach(&client->dev, true);
415err_clear_wakeup_irq:
416 dev_pm_clear_wake_irq(&client->dev);
417 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +0100418 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421static int i2c_device_remove(struct device *dev)
422{
Jean Delvare51298d12009-09-18 22:45:45 +0200423 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200424 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100425 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200426
Jean Delvare51298d12009-09-18 22:45:45 +0200427 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200428 return 0;
429
430 driver = to_i2c_driver(dev->driver);
431 if (driver->remove) {
432 dev_dbg(dev, "remove\n");
433 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200434 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100435
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200436 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700437
438 dev_pm_clear_wake_irq(&client->dev);
439 device_init_wakeup(&client->dev, false);
440
David Brownella1d9e6e2007-05-01 23:26:30 +0200441 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
David Brownellf37dd802007-02-13 22:09:00 +0100444static void i2c_device_shutdown(struct device *dev)
445{
Jean Delvare51298d12009-09-18 22:45:45 +0200446 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100447 struct i2c_driver *driver;
448
Jean Delvare51298d12009-09-18 22:45:45 +0200449 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100450 return;
451 driver = to_i2c_driver(dev->driver);
452 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200453 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100454}
455
David Brownell9c1600e2007-05-01 23:26:31 +0200456static void i2c_client_dev_release(struct device *dev)
457{
458 kfree(to_i2c_client(dev));
459}
460
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100461static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200462show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200463{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200464 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
465 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200466}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100467static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200468
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100469static ssize_t
470show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200471{
472 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800473 int len;
474
475 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
476 if (len != -ENODEV)
477 return len;
478
Jean Delvareeb8a7902008-05-18 20:49:41 +0200479 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200480}
Jean Delvare51298d12009-09-18 22:45:45 +0200481static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
482
483static struct attribute *i2c_dev_attrs[] = {
484 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200485 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200486 &dev_attr_modalias.attr,
487 NULL
488};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100489ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100490
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200491struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100492 .name = "i2c",
493 .match = i2c_device_match,
494 .probe = i2c_device_probe,
495 .remove = i2c_device_remove,
496 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000497};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200498EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000499
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800500struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100501 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200502 .uevent = i2c_device_uevent,
503 .release = i2c_client_dev_release,
504};
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800505EXPORT_SYMBOL_GPL(i2c_client_type);
Jean Delvare51298d12009-09-18 22:45:45 +0200506
David Brownell9b766b82008-01-27 18:14:51 +0100507
508/**
509 * i2c_verify_client - return parameter as i2c_client, or NULL
510 * @dev: device, probably from some driver model iterator
511 *
512 * When traversing the driver model tree, perhaps using driver model
513 * iterators like @device_for_each_child(), you can't assume very much
514 * about the nodes you find. Use this function to avoid oopses caused
515 * by wrongly treating some non-I2C device as an i2c_client.
516 */
517struct i2c_client *i2c_verify_client(struct device *dev)
518{
Jean Delvare51298d12009-09-18 22:45:45 +0200519 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100520 ? to_i2c_client(dev)
521 : NULL;
522}
523EXPORT_SYMBOL(i2c_verify_client);
524
525
Wolfram Sangda899f52015-05-18 21:09:12 +0200526/* Return a unique address which takes the flags of the client into account */
527static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
528{
529 unsigned short addr = client->addr;
530
531 /* For some client flags, add an arbitrary offset to avoid collisions */
532 if (client->flags & I2C_CLIENT_TEN)
533 addr |= I2C_ADDR_OFFSET_TEN_BIT;
534
535 if (client->flags & I2C_CLIENT_SLAVE)
536 addr |= I2C_ADDR_OFFSET_SLAVE;
537
538 return addr;
539}
540
Jean Delvare3a89db52010-06-03 11:33:52 +0200541/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300542 * are purposely not enforced, except for the general call address. */
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200543int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +0200544{
Wolfram Sangc4019b72015-07-17 12:50:06 +0200545 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +0200546 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200547 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +0200548 return -EINVAL;
549 } else {
550 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200551 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +0200552 return -EINVAL;
553 }
554 return 0;
555}
556
Jean Delvare656b8762010-06-03 11:33:53 +0200557/* And this is a strict address validity check, used when probing. If a
558 * device uses a reserved address, then it shouldn't be probed. 7-bit
559 * addressing is assumed, 10-bit address devices are rare and should be
560 * explicitly enumerated. */
Wolfram Sange4991ec2017-05-23 11:14:17 +0200561int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +0200562{
563 /*
564 * Reserved addresses per I2C specification:
565 * 0x00 General call address / START byte
566 * 0x01 CBUS address
567 * 0x02 Reserved for different bus format
568 * 0x03 Reserved for future purposes
569 * 0x04-0x07 Hs-mode master code
570 * 0x78-0x7b 10-bit slave addressing
571 * 0x7c-0x7f Reserved for future purposes
572 */
573 if (addr < 0x08 || addr > 0x77)
574 return -EINVAL;
575 return 0;
576}
577
Jean Delvare3b5f7942010-06-03 11:33:55 +0200578static int __i2c_check_addr_busy(struct device *dev, void *addrp)
579{
580 struct i2c_client *client = i2c_verify_client(dev);
581 int addr = *(int *)addrp;
582
Wolfram Sang9bccc702015-07-17 14:48:56 +0200583 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +0200584 return -EBUSY;
585 return 0;
586}
587
Michael Lawnick08263742010-08-11 18:21:02 +0200588/* walk up mux tree */
589static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
590{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200591 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200592 int result;
593
594 result = device_for_each_child(&adapter->dev, &addr,
595 __i2c_check_addr_busy);
596
Jean Delvare97cc4d42010-10-24 18:16:57 +0200597 if (!result && parent)
598 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200599
600 return result;
601}
602
603/* recurse down mux tree */
604static int i2c_check_mux_children(struct device *dev, void *addrp)
605{
606 int result;
607
608 if (dev->type == &i2c_adapter_type)
609 result = device_for_each_child(dev, addrp,
610 i2c_check_mux_children);
611 else
612 result = __i2c_check_addr_busy(dev, addrp);
613
614 return result;
615}
616
Jean Delvare3b5f7942010-06-03 11:33:55 +0200617static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
618{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200619 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200620 int result = 0;
621
Jean Delvare97cc4d42010-10-24 18:16:57 +0200622 if (parent)
623 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200624
625 if (!result)
626 result = device_for_each_child(&adapter->dev, &addr,
627 i2c_check_mux_children);
628
629 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200630}
631
David Brownell9c1600e2007-05-01 23:26:31 +0200632/**
Peter Rosin8320f492016-05-04 22:15:27 +0200633 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200634 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200635 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
636 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200637 */
Peter Rosin8320f492016-05-04 22:15:27 +0200638static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
639 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200640{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200641 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200642}
Jean Delvarefe61e072010-08-11 18:20:58 +0200643
644/**
Peter Rosin8320f492016-05-04 22:15:27 +0200645 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200646 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200647 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
648 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200649 */
Peter Rosin8320f492016-05-04 22:15:27 +0200650static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
651 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200652{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200653 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200654}
655
656/**
Peter Rosin8320f492016-05-04 22:15:27 +0200657 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200658 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200659 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
660 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200661 */
Peter Rosin8320f492016-05-04 22:15:27 +0200662static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
663 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200664{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200665 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200666}
Jean Delvarefe61e072010-08-11 18:20:58 +0200667
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200668static void i2c_dev_set_name(struct i2c_adapter *adap,
Hans de Goede728fe6c2017-10-11 11:41:19 +0200669 struct i2c_client *client,
670 struct i2c_board_info const *info)
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200671{
672 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
673
Hans de Goede728fe6c2017-10-11 11:41:19 +0200674 if (info && info->dev_name) {
675 dev_set_name(&client->dev, "i2c-%s", info->dev_name);
676 return;
677 }
678
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200679 if (adev) {
680 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
681 return;
682 }
683
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200684 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +0200685 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200686}
687
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800688static int i2c_dev_irq_from_resources(const struct resource *resources,
689 unsigned int num_resources)
690{
691 struct irq_data *irqd;
692 int i;
693
694 for (i = 0; i < num_resources; i++) {
695 const struct resource *r = &resources[i];
696
697 if (resource_type(r) != IORESOURCE_IRQ)
698 continue;
699
700 if (r->flags & IORESOURCE_BITS) {
701 irqd = irq_get_irq_data(r->start);
702 if (!irqd)
703 break;
704
705 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
706 }
707
708 return r->start;
709 }
710
711 return 0;
712}
713
Jean Delvarefe61e072010-08-11 18:20:58 +0200714/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200715 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200716 * @adap: the adapter managing the device
717 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200718 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200719 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200720 * Create an i2c device. Binding is handled through driver model
721 * probe()/remove() methods. A driver may be bound to this device when we
722 * return from this function, or any later moment (e.g. maybe hotplugging will
723 * load the driver module). This call is not appropriate for use by mainboard
724 * initialization logic, which usually runs during an arch_initcall() long
725 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200726 *
727 * This returns the new i2c client, which may be saved for later use with
728 * i2c_unregister_device(); or NULL to indicate an error.
729 */
730struct i2c_client *
731i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
732{
733 struct i2c_client *client;
734 int status;
735
736 client = kzalloc(sizeof *client, GFP_KERNEL);
737 if (!client)
738 return NULL;
739
740 client->adapter = adap;
741
742 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200743
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200744 if (info->archdata)
745 client->dev.archdata = *info->archdata;
746
Marc Pignatee354252008-08-28 08:33:22 +0200747 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200748 client->addr = info->addr;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800749
David Brownell9c1600e2007-05-01 23:26:31 +0200750 client->irq = info->irq;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800751 if (!client->irq)
752 client->irq = i2c_dev_irq_from_resources(info->resources,
753 info->num_resources);
David Brownell9c1600e2007-05-01 23:26:31 +0200754
David Brownell9c1600e2007-05-01 23:26:31 +0200755 strlcpy(client->name, info->type, sizeof(client->name));
756
Wolfram Sangc4019b72015-07-17 12:50:06 +0200757 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +0200758 if (status) {
759 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
760 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
761 goto out_err_silent;
762 }
763
Jean Delvaref8a227e2009-06-19 16:58:18 +0200764 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +0200765 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200766 if (status)
767 goto out_err;
768
769 client->dev.parent = &client->adapter->dev;
770 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200771 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700772 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100773 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200774
Hans de Goede728fe6c2017-10-11 11:41:19 +0200775 i2c_dev_set_name(adap, client, info);
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800776
777 if (info->properties) {
778 status = device_add_properties(&client->dev, info->properties);
779 if (status) {
780 dev_err(&adap->dev,
781 "Failed to add properties to client %s: %d\n",
782 client->name, status);
783 goto out_err;
784 }
785 }
786
Jean Delvaref8a227e2009-06-19 16:58:18 +0200787 status = device_register(&client->dev);
788 if (status)
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800789 goto out_free_props;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200790
Jean Delvaref8a227e2009-06-19 16:58:18 +0200791 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
792 client->name, dev_name(&client->dev));
793
David Brownell9c1600e2007-05-01 23:26:31 +0200794 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200795
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800796out_free_props:
797 if (info->properties)
798 device_remove_properties(&client->dev);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200799out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300800 dev_err(&adap->dev,
801 "Failed to register i2c client %s at 0x%02x (%d)\n",
802 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200803out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200804 kfree(client);
805 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200806}
807EXPORT_SYMBOL_GPL(i2c_new_device);
808
809
810/**
811 * i2c_unregister_device - reverse effect of i2c_new_device()
812 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200813 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200814 */
815void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200816{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +0200817 if (client->dev.of_node)
818 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300819 if (ACPI_COMPANION(&client->dev))
820 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +0200821 device_unregister(&client->dev);
822}
David Brownell9c1600e2007-05-01 23:26:31 +0200823EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200824
825
Jean Delvare60b129d2008-05-11 20:37:06 +0200826static const struct i2c_device_id dummy_id[] = {
827 { "dummy", 0 },
828 { },
829};
830
Jean Delvared2653e92008-04-29 23:11:39 +0200831static int dummy_probe(struct i2c_client *client,
832 const struct i2c_device_id *id)
833{
834 return 0;
835}
836
837static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100838{
839 return 0;
840}
841
842static struct i2c_driver dummy_driver = {
843 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200844 .probe = dummy_probe,
845 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200846 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100847};
848
849/**
850 * i2c_new_dummy - return a new i2c device bound to a dummy driver
851 * @adapter: the adapter managing the device
852 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100853 * Context: can sleep
854 *
855 * This returns an I2C client bound to the "dummy" driver, intended for use
856 * with devices that consume multiple addresses. Examples of such chips
857 * include various EEPROMS (like 24c04 and 24c08 models).
858 *
859 * These dummy devices have two main uses. First, most I2C and SMBus calls
860 * except i2c_transfer() need a client handle; the dummy will be that handle.
861 * And second, this prevents the specified address from being bound to a
862 * different driver.
863 *
864 * This returns the new i2c client, which should be saved for later use with
865 * i2c_unregister_device(); or NULL to indicate an error.
866 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100867struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100868{
869 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200870 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100871 };
872
David Brownelle9f13732008-01-27 18:14:52 +0100873 return i2c_new_device(adapter, &info);
874}
875EXPORT_SYMBOL_GPL(i2c_new_dummy);
876
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +0100877/**
878 * i2c_new_secondary_device - Helper to get the instantiated secondary address
879 * and create the associated device
880 * @client: Handle to the primary client
881 * @name: Handle to specify which secondary address to get
882 * @default_addr: Used as a fallback if no secondary address was specified
883 * Context: can sleep
884 *
885 * I2C clients can be composed of multiple I2C slaves bound together in a single
886 * component. The I2C client driver then binds to the master I2C slave and needs
887 * to create I2C dummy clients to communicate with all the other slaves.
888 *
889 * This function creates and returns an I2C dummy client whose I2C address is
890 * retrieved from the platform firmware based on the given slave name. If no
891 * address is specified by the firmware default_addr is used.
892 *
893 * On DT-based platforms the address is retrieved from the "reg" property entry
894 * cell whose "reg-names" value matches the slave name.
895 *
896 * This returns the new i2c client, which should be saved for later use with
897 * i2c_unregister_device(); or NULL to indicate an error.
898 */
899struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
900 const char *name,
901 u16 default_addr)
902{
903 struct device_node *np = client->dev.of_node;
904 u32 addr = default_addr;
905 int i;
906
907 if (np) {
908 i = of_property_match_string(np, "reg-names", name);
909 if (i >= 0)
910 of_property_read_u32_index(np, "reg", i, &addr);
911 }
912
913 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
914 return i2c_new_dummy(client->adapter, addr);
915}
916EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
917
David Brownellf37dd802007-02-13 22:09:00 +0100918/* ------------------------------------------------------------------------- */
919
David Brownell16ffadf2007-05-01 23:26:28 +0200920/* I2C bus adapters -- one roots each I2C or SMBUS segment */
921
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200922static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
David Brownellef2c83212007-05-01 23:26:28 +0200924 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 complete(&adap->dev_released);
926}
927
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +0200928unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +0200929{
930 unsigned int depth = 0;
931
932 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
933 depth++;
934
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +0200935 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
936 "adapter depth exceeds lockdep subclass limit\n");
937
Jean Delvare390946b2012-09-10 10:14:02 +0200938 return depth;
939}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +0200940EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +0200941
942/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200943 * Let users instantiate I2C devices through sysfs. This can be used when
944 * platform initialization code doesn't contain the proper data for
945 * whatever reason. Also useful for drivers that do device detection and
946 * detection fails, either because the device uses an unexpected address,
947 * or this is a compatible device with different ID register values.
948 *
949 * Parameter checking may look overzealous, but we really don't want
950 * the user to provide incorrect parameters.
951 */
952static ssize_t
953i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
954 const char *buf, size_t count)
955{
956 struct i2c_adapter *adap = to_i2c_adapter(dev);
957 struct i2c_board_info info;
958 struct i2c_client *client;
959 char *blank, end;
960 int res;
961
Jean Delvare99cd8e22009-06-19 16:58:20 +0200962 memset(&info, 0, sizeof(struct i2c_board_info));
963
964 blank = strchr(buf, ' ');
965 if (!blank) {
966 dev_err(dev, "%s: Missing parameters\n", "new_device");
967 return -EINVAL;
968 }
969 if (blank - buf > I2C_NAME_SIZE - 1) {
970 dev_err(dev, "%s: Invalid device name\n", "new_device");
971 return -EINVAL;
972 }
973 memcpy(info.type, buf, blank - buf);
974
975 /* Parse remaining parameters, reject extra parameters */
976 res = sscanf(++blank, "%hi%c", &info.addr, &end);
977 if (res < 1) {
978 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
979 return -EINVAL;
980 }
981 if (res > 1 && end != '\n') {
982 dev_err(dev, "%s: Extra parameters\n", "new_device");
983 return -EINVAL;
984 }
985
Wolfram Sangcfa03272015-07-27 14:03:38 +0200986 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
987 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
988 info.flags |= I2C_CLIENT_TEN;
989 }
990
991 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
992 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
993 info.flags |= I2C_CLIENT_SLAVE;
994 }
995
Jean Delvare99cd8e22009-06-19 16:58:20 +0200996 client = i2c_new_device(adap, &info);
997 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200998 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200999
1000 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001001 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001002 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001003 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001004 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1005 info.type, info.addr);
1006
1007 return count;
1008}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001009static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001010
1011/*
1012 * And of course let the users delete the devices they instantiated, if
1013 * they got it wrong. This interface can only be used to delete devices
1014 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1015 * don't delete devices to which some kernel code still has references.
1016 *
1017 * Parameter checking may look overzealous, but we really don't want
1018 * the user to delete the wrong device.
1019 */
1020static ssize_t
1021i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1022 const char *buf, size_t count)
1023{
1024 struct i2c_adapter *adap = to_i2c_adapter(dev);
1025 struct i2c_client *client, *next;
1026 unsigned short addr;
1027 char end;
1028 int res;
1029
1030 /* Parse parameters, reject extra parameters */
1031 res = sscanf(buf, "%hi%c", &addr, &end);
1032 if (res < 1) {
1033 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1034 return -EINVAL;
1035 }
1036 if (res > 1 && end != '\n') {
1037 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1038 return -EINVAL;
1039 }
1040
1041 /* Make sure the device was added through sysfs */
1042 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001043 mutex_lock_nested(&adap->userspace_clients_lock,
1044 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001045 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1046 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001047 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001048 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1049 "delete_device", client->name, client->addr);
1050
1051 list_del(&client->detected);
1052 i2c_unregister_device(client);
1053 res = count;
1054 break;
1055 }
1056 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001057 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001058
1059 if (res < 0)
1060 dev_err(dev, "%s: Can't find device in list\n",
1061 "delete_device");
1062 return res;
1063}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001064static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1065 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001066
1067static struct attribute *i2c_adapter_attrs[] = {
1068 &dev_attr_name.attr,
1069 &dev_attr_new_device.attr,
1070 &dev_attr_delete_device.attr,
1071 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001072};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001073ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001074
Michael Lawnick08263742010-08-11 18:21:02 +02001075struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001076 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001077 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001078};
Michael Lawnick08263742010-08-11 18:21:02 +02001079EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Stephen Warren643dd092012-04-17 12:43:33 -06001081/**
1082 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1083 * @dev: device, probably from some driver model iterator
1084 *
1085 * When traversing the driver model tree, perhaps using driver model
1086 * iterators like @device_for_each_child(), you can't assume very much
1087 * about the nodes you find. Use this function to avoid oopses caused
1088 * by wrongly treating some non-I2C device as an i2c_adapter.
1089 */
1090struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1091{
1092 return (dev->type == &i2c_adapter_type)
1093 ? to_i2c_adapter(dev)
1094 : NULL;
1095}
1096EXPORT_SYMBOL(i2c_verify_adapter);
1097
Jean Delvare2bb50952009-09-18 22:45:46 +02001098#ifdef CONFIG_I2C_COMPAT
1099static struct class_compat *i2c_adapter_compat_class;
1100#endif
1101
David Brownell9c1600e2007-05-01 23:26:31 +02001102static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1103{
1104 struct i2c_devinfo *devinfo;
1105
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001106 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001107 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1108 if (devinfo->busnum == adapter->nr
1109 && !i2c_new_device(adapter,
1110 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001111 dev_err(&adapter->dev,
1112 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001113 devinfo->board_info.addr);
1114 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001115 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001116}
1117
Jean Delvare69b00892009-12-06 17:06:27 +01001118static int i2c_do_add_adapter(struct i2c_driver *driver,
1119 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001120{
Jean Delvare4735c982008-07-14 22:38:36 +02001121 /* Detect supported devices on that bus, and instantiate them */
1122 i2c_detect(adap, driver);
1123
1124 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001125 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001126 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1127 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001128 dev_warn(&adap->dev,
1129 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001130 /* We ignore the return code; if it fails, too bad */
1131 driver->attach_adapter(adap);
1132 }
1133 return 0;
1134}
1135
Jean Delvare69b00892009-12-06 17:06:27 +01001136static int __process_new_adapter(struct device_driver *d, void *data)
1137{
1138 return i2c_do_add_adapter(to_i2c_driver(d), data);
1139}
1140
Peter Rosind1ed7982016-08-25 23:07:01 +02001141static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1142 .lock_bus = i2c_adapter_lock_bus,
1143 .trylock_bus = i2c_adapter_trylock_bus,
1144 .unlock_bus = i2c_adapter_unlock_bus,
1145};
1146
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001147static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap)
1148{
1149 struct irq_domain *domain = adap->host_notify_domain;
1150 irq_hw_number_t hwirq;
1151
1152 if (!domain)
1153 return;
1154
1155 for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++)
1156 irq_dispose_mapping(irq_find_mapping(domain, hwirq));
1157
1158 irq_domain_remove(domain);
1159 adap->host_notify_domain = NULL;
1160}
1161
1162static int i2c_host_notify_irq_map(struct irq_domain *h,
1163 unsigned int virq,
1164 irq_hw_number_t hw_irq_num)
1165{
1166 irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
1167
1168 return 0;
1169}
1170
1171static const struct irq_domain_ops i2c_host_notify_irq_ops = {
1172 .map = i2c_host_notify_irq_map,
1173};
1174
1175static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap)
1176{
1177 struct irq_domain *domain;
1178
1179 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
1180 return 0;
1181
1182 domain = irq_domain_create_linear(adap->dev.fwnode,
1183 I2C_ADDR_7BITS_COUNT,
1184 &i2c_host_notify_irq_ops, adap);
1185 if (!domain)
1186 return -ENOMEM;
1187
1188 adap->host_notify_domain = domain;
1189
1190 return 0;
1191}
1192
1193/**
1194 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
1195 * I2C client.
1196 * @adap: the adapter
1197 * @addr: the I2C address of the notifying device
1198 * Context: can't sleep
1199 *
1200 * Helper function to be called from an I2C bus driver's interrupt
1201 * handler. It will schedule the Host Notify IRQ.
1202 */
1203int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
1204{
1205 int irq;
1206
1207 if (!adap)
1208 return -EINVAL;
1209
1210 irq = irq_find_mapping(adap->host_notify_domain, addr);
1211 if (irq <= 0)
1212 return -ENXIO;
1213
1214 generic_handle_irq(irq);
1215
1216 return 0;
1217}
1218EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
1219
David Brownell6e13e642007-05-01 23:26:31 +02001220static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001222 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
David Brownell1d0b19c2008-10-14 17:30:05 +02001224 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301225 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001226 res = -EAGAIN;
1227 goto out_list;
1228 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001229
Jean Delvare2236baa2010-11-15 22:40:38 +01001230 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001231 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001232 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001233
1234 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001235 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001236 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01001237 }
1238
Peter Rosind1ed7982016-08-25 23:07:01 +02001239 if (!adap->lock_ops)
1240 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001241
Mika Kuoppala194684e2009-12-06 17:06:22 +01001242 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001243 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001244 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001245 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Jean Delvare8fcfef62009-03-28 21:34:43 +01001247 /* Set default timeout to 1 second if not already set */
1248 if (adap->timeout == 0)
1249 adap->timeout = HZ;
1250
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001251 /* register soft irqs for Host Notify */
1252 res = i2c_setup_host_notify_irq_domain(adap);
1253 if (res) {
1254 pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n",
1255 adap->name, res);
1256 goto out_list;
1257 }
1258
Kay Sievers27d9c182009-01-07 14:29:16 +01001259 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001260 adap->dev.bus = &i2c_bus_type;
1261 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001262 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001263 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001264 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001265 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001268 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1269
Charles Keepax6ada5c12015-04-16 13:05:19 +01001270 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001271 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001272 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001273
Jean Delvare2bb50952009-09-18 22:45:46 +02001274#ifdef CONFIG_I2C_COMPAT
1275 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1276 adap->dev.parent);
1277 if (res)
1278 dev_warn(&adap->dev,
1279 "Failed to create compatibility class link\n");
1280#endif
1281
Wolfram Sangd3b11d82016-07-09 13:34:59 +09001282 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301283
Jean Delvare729d6dd2009-06-19 16:58:18 +02001284 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001285 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001286 i2c_acpi_register_devices(adap);
1287 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001288
David Brownell6e13e642007-05-01 23:26:31 +02001289 if (adap->nr < __i2c_first_dynamic_bus_num)
1290 i2c_scan_static_board_info(adap);
1291
Jean Delvare4735c982008-07-14 22:38:36 +02001292 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001293 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001294 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001295 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001296
1297 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001298
Jean Delvareb119c6c2006-08-15 18:26:30 +02001299out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001300 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001301 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001302 mutex_unlock(&core_lock);
1303 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
David Brownell6e13e642007-05-01 23:26:31 +02001306/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001307 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1308 * @adap: the adapter to register (with adap->nr initialized)
1309 * Context: can sleep
1310 *
1311 * See i2c_add_numbered_adapter() for details.
1312 */
1313static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1314{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001315 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001316
1317 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001318 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001319 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001320 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001321 return id == -ENOSPC ? -EBUSY : id;
1322
1323 return i2c_register_adapter(adap);
1324}
1325
1326/**
David Brownell6e13e642007-05-01 23:26:31 +02001327 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1328 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001329 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001330 *
1331 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001332 * doesn't matter or when its bus number is specified by an dt alias.
1333 * Examples of bases when the bus number doesn't matter: I2C adapters
1334 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001335 *
1336 * When this returns zero, a new bus number was allocated and stored
1337 * in adap->nr, and the specified adapter became available for clients.
1338 * Otherwise, a negative errno value is returned.
1339 */
1340int i2c_add_adapter(struct i2c_adapter *adapter)
1341{
Doug Andersonee5c2742013-03-01 08:57:31 -08001342 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001343 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001344
Doug Andersonee5c2742013-03-01 08:57:31 -08001345 if (dev->of_node) {
1346 id = of_alias_get_id(dev->of_node, "i2c");
1347 if (id >= 0) {
1348 adapter->nr = id;
1349 return __i2c_add_numbered_adapter(adapter);
1350 }
1351 }
1352
Jean Delvarecaada322008-01-27 18:14:49 +01001353 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001354 id = idr_alloc(&i2c_adapter_idr, adapter,
1355 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001356 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001357 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08001358 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001359
1360 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001361
David Brownell6e13e642007-05-01 23:26:31 +02001362 return i2c_register_adapter(adapter);
1363}
1364EXPORT_SYMBOL(i2c_add_adapter);
1365
1366/**
1367 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1368 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001369 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001370 *
1371 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001372 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1373 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001374 * is used to properly configure I2C devices.
1375 *
Grant Likely488bf312011-07-25 17:49:43 +02001376 * If the requested bus number is set to -1, then this function will behave
1377 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1378 *
David Brownell6e13e642007-05-01 23:26:31 +02001379 * If no devices have pre-been declared for this bus, then be sure to
1380 * register the adapter before any dynamically allocated ones. Otherwise
1381 * the required bus ID may not be available.
1382 *
1383 * When this returns zero, the specified adapter became available for
1384 * clients using the bus number provided in adap->nr. Also, the table
1385 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1386 * and the appropriate driver model device nodes are created. Otherwise, a
1387 * negative errno value is returned.
1388 */
1389int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1390{
Grant Likely488bf312011-07-25 17:49:43 +02001391 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1392 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001393
Doug Andersonee5c2742013-03-01 08:57:31 -08001394 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001395}
1396EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1397
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001398static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001399 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001400{
Jean Delvare4735c982008-07-14 22:38:36 +02001401 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001402
Jean Delvareacec2112009-03-28 21:34:40 +01001403 /* Remove the devices we created ourselves as the result of hardware
1404 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001405 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1406 if (client->adapter == adapter) {
1407 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1408 client->name, client->addr);
1409 list_del(&client->detected);
1410 i2c_unregister_device(client);
1411 }
1412 }
Jean Delvare026526f2008-01-27 18:14:49 +01001413}
1414
Jean Delvaree549c2b2009-06-19 16:58:19 +02001415static int __unregister_client(struct device *dev, void *dummy)
1416{
1417 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001418 if (client && strcmp(client->name, "dummy"))
1419 i2c_unregister_device(client);
1420 return 0;
1421}
1422
1423static int __unregister_dummy(struct device *dev, void *dummy)
1424{
1425 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001426 if (client)
1427 i2c_unregister_device(client);
1428 return 0;
1429}
1430
Jean Delvare69b00892009-12-06 17:06:27 +01001431static int __process_removed_adapter(struct device_driver *d, void *data)
1432{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001433 i2c_do_del_adapter(to_i2c_driver(d), data);
1434 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001435}
1436
David Brownelld64f73b2007-07-12 14:12:28 +02001437/**
1438 * i2c_del_adapter - unregister I2C adapter
1439 * @adap: the adapter being unregistered
1440 * Context: can sleep
1441 *
1442 * This unregisters an I2C adapter which was previously registered
1443 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1444 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001445void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001447 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001448 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
1450 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001451 mutex_lock(&core_lock);
1452 found = idr_find(&i2c_adapter_idr, adap->nr);
1453 mutex_unlock(&core_lock);
1454 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001455 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001456 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 }
1458
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001459 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001460 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001461 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001462 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001463 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001464 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001466 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001467 mutex_lock_nested(&adap->userspace_clients_lock,
1468 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001469 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1470 detected) {
1471 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1472 client->addr);
1473 list_del(&client->detected);
1474 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001475 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001476 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001477
Jean Delvaree549c2b2009-06-19 16:58:19 +02001478 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001479 * check the returned value. This is a two-pass process, because
1480 * we can't remove the dummy devices during the first pass: they
1481 * could have been instantiated by real devices wishing to clean
1482 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001483 device_for_each_child(&adap->dev, NULL, __unregister_client);
1484 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Jean Delvare2bb50952009-09-18 22:45:46 +02001486#ifdef CONFIG_I2C_COMPAT
1487 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1488 adap->dev.parent);
1489#endif
1490
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001491 /* device name is gone after device_unregister */
1492 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1493
Wolfram Sang9f924162015-12-23 18:19:28 +01001494 pm_runtime_disable(&adap->dev);
1495
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001496 i2c_host_notify_irq_teardown(adap);
1497
Wolfram Sang26680ee2015-01-29 22:45:09 +01001498 /* wait until all references to the device are gone
1499 *
1500 * FIXME: This is old code and should ideally be replaced by an
1501 * alternative which results in decoupling the lifetime of the struct
1502 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301503 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001504 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
David Brownell6e13e642007-05-01 23:26:31 +02001509 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001510 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001512 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001514 /* Clear the device structure in case this adapter is ever going to be
1515 added again */
1516 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517}
David Brownellc0564602007-05-01 23:26:31 +02001518EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Wolfram Sang54177cc2015-12-17 13:32:36 +01001520/**
1521 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
1522 * @dev: The device to scan for I2C timing properties
1523 * @t: the i2c_timings struct to be filled with values
1524 * @use_defaults: bool to use sane defaults derived from the I2C specification
1525 * when properties are not found, otherwise use 0
1526 *
1527 * Scan the device for the generic I2C properties describing timing parameters
1528 * for the signal and fill the given struct with the results. If a property was
1529 * not found and use_defaults was true, then maximum timings are assumed which
1530 * are derived from the I2C specification. If use_defaults is not used, the
1531 * results will be 0, so drivers can apply their own defaults later. The latter
1532 * is mainly intended for avoiding regressions of existing drivers which want
1533 * to switch to this function. New drivers almost always should use the defaults.
1534 */
1535
1536void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
1537{
1538 int ret;
1539
1540 memset(t, 0, sizeof(*t));
1541
1542 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
1543 if (ret && use_defaults)
1544 t->bus_freq_hz = 100000;
1545
1546 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
1547 if (ret && use_defaults) {
1548 if (t->bus_freq_hz <= 100000)
1549 t->scl_rise_ns = 1000;
1550 else if (t->bus_freq_hz <= 400000)
1551 t->scl_rise_ns = 300;
1552 else
1553 t->scl_rise_ns = 120;
1554 }
1555
1556 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
1557 if (ret && use_defaults) {
1558 if (t->bus_freq_hz <= 400000)
1559 t->scl_fall_ns = 300;
1560 else
1561 t->scl_fall_ns = 120;
1562 }
1563
1564 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
1565
1566 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
1567 if (ret && use_defaults)
1568 t->sda_fall_ns = t->scl_fall_ns;
1569}
1570EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
1571
David Brownell7b4fbc52007-05-01 23:26:30 +02001572/* ------------------------------------------------------------------------- */
1573
Jean Delvare7ae31482011-03-20 14:50:52 +01001574int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1575{
1576 int res;
1577
1578 mutex_lock(&core_lock);
1579 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1580 mutex_unlock(&core_lock);
1581
1582 return res;
1583}
1584EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1585
Jean Delvare69b00892009-12-06 17:06:27 +01001586static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001587{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001588 if (dev->type != &i2c_adapter_type)
1589 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001590 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001591}
1592
David Brownell7b4fbc52007-05-01 23:26:30 +02001593/*
1594 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001595 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 */
1597
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001598int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001600 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
David Brownell1d0b19c2008-10-14 17:30:05 +02001602 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301603 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02001604 return -EAGAIN;
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001607 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02001609 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Jean Delvare729d6dd2009-06-19 16:58:18 +02001611 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001612 * will have called probe() for all matching-but-unbound devices.
1613 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 res = driver_register(&driver->driver);
1615 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001616 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001617
Wolfram Sang44239a52016-07-09 13:35:04 +09001618 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Jean Delvare4735c982008-07-14 22:38:36 +02001620 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001621 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001622
Jean Delvare7eebcb72006-02-05 23:28:21 +01001623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001625EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Jean Delvare69b00892009-12-06 17:06:27 +01001627static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001628{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001629 if (dev->type == &i2c_adapter_type)
1630 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1631 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001632}
1633
David Brownella1d9e6e2007-05-01 23:26:30 +02001634/**
1635 * i2c_del_driver - unregister I2C driver
1636 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001637 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001638 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001639void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Jean Delvare7ae31482011-03-20 14:50:52 +01001641 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09001644 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
David Brownellc0564602007-05-01 23:26:31 +02001646EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
David Brownell7b4fbc52007-05-01 23:26:30 +02001648/* ------------------------------------------------------------------------- */
1649
Jean Delvaree48d3312008-01-27 18:14:48 +01001650/**
1651 * i2c_use_client - increments the reference count of the i2c client structure
1652 * @client: the client being referenced
1653 *
1654 * Each live reference to a client should be refcounted. The driver model does
1655 * that automatically as part of driver binding, so that most drivers don't
1656 * need to do this explicitly: they hold a reference until they're unbound
1657 * from the device.
1658 *
1659 * A pointer to the client with the incremented reference counter is returned.
1660 */
1661struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
David Brownell6ea438e2008-07-14 22:38:24 +02001663 if (client && get_device(&client->dev))
1664 return client;
1665 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
David Brownellc0564602007-05-01 23:26:31 +02001667EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Jean Delvaree48d3312008-01-27 18:14:48 +01001669/**
1670 * i2c_release_client - release a use of the i2c client structure
1671 * @client: the client being no longer referenced
1672 *
1673 * Must be called when a user of a client is finished with it.
1674 */
1675void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
David Brownell6ea438e2008-07-14 22:38:24 +02001677 if (client)
1678 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
David Brownellc0564602007-05-01 23:26:31 +02001680EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
David Brownell9b766b82008-01-27 18:14:51 +01001682struct i2c_cmd_arg {
1683 unsigned cmd;
1684 void *arg;
1685};
1686
1687static int i2c_cmd(struct device *dev, void *_arg)
1688{
1689 struct i2c_client *client = i2c_verify_client(dev);
1690 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001691 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001692
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001693 if (!client || !client->dev.driver)
1694 return 0;
1695
1696 driver = to_i2c_driver(client->dev.driver);
1697 if (driver->command)
1698 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001699 return 0;
1700}
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1703{
David Brownell9b766b82008-01-27 18:14:51 +01001704 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
David Brownell9b766b82008-01-27 18:14:51 +01001706 cmd_arg.cmd = cmd;
1707 cmd_arg.arg = arg;
1708 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
David Brownellc0564602007-05-01 23:26:31 +02001710EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712static int __init i2c_init(void)
1713{
1714 int retval;
1715
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001716 retval = of_alias_get_highest_id("i2c");
1717
1718 down_write(&__i2c_board_lock);
1719 if (retval >= __i2c_first_dynamic_bus_num)
1720 __i2c_first_dynamic_bus_num = retval + 1;
1721 up_write(&__i2c_board_lock);
1722
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 retval = bus_register(&i2c_bus_type);
1724 if (retval)
1725 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01001726
1727 is_registered = true;
1728
Jean Delvare2bb50952009-09-18 22:45:46 +02001729#ifdef CONFIG_I2C_COMPAT
1730 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1731 if (!i2c_adapter_compat_class) {
1732 retval = -ENOMEM;
1733 goto bus_err;
1734 }
1735#endif
David Brownelle9f13732008-01-27 18:14:52 +01001736 retval = i2c_add_driver(&dummy_driver);
1737 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001738 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001739
1740 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1741 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001742 if (IS_ENABLED(CONFIG_ACPI))
1743 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001744
David Brownelle9f13732008-01-27 18:14:52 +01001745 return 0;
1746
Jean Delvare2bb50952009-09-18 22:45:46 +02001747class_err:
1748#ifdef CONFIG_I2C_COMPAT
1749 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001750bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001751#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01001752 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01001753 bus_unregister(&i2c_bus_type);
1754 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
1757static void __exit i2c_exit(void)
1758{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001759 if (IS_ENABLED(CONFIG_ACPI))
1760 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001761 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1762 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001763 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001764#ifdef CONFIG_I2C_COMPAT
1765 class_compat_unregister(i2c_adapter_compat_class);
1766#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001768 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
David Brownella10f9e72008-10-14 17:30:06 +02001771/* We must initialize early, because some subsystems register i2c drivers
1772 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1773 */
1774postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775module_exit(i2c_exit);
1776
1777/* ----------------------------------------------------
1778 * the functional interface to the i2c busses.
1779 * ----------------------------------------------------
1780 */
1781
Wolfram Sangb7f62582015-01-05 23:45:59 +01001782/* Check if val is exceeding the quirk IFF quirk is non 0 */
1783#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1784
1785static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
1786{
1787 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
1788 err_msg, msg->addr, msg->len,
1789 msg->flags & I2C_M_RD ? "read" : "write");
1790 return -EOPNOTSUPP;
1791}
1792
1793static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1794{
1795 const struct i2c_adapter_quirks *q = adap->quirks;
1796 int max_num = q->max_num_msgs, i;
1797 bool do_len_check = true;
1798
1799 if (q->flags & I2C_AQ_COMB) {
1800 max_num = 2;
1801
1802 /* special checks for combined messages */
1803 if (num == 2) {
1804 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
1805 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
1806
1807 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
1808 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
1809
1810 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
1811 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
1812
1813 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
1814 return i2c_quirk_error(adap, &msgs[0], "msg too long");
1815
1816 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
1817 return i2c_quirk_error(adap, &msgs[1], "msg too long");
1818
1819 do_len_check = false;
1820 }
1821 }
1822
1823 if (i2c_quirk_exceeded(num, max_num))
1824 return i2c_quirk_error(adap, &msgs[0], "too many messages");
1825
1826 for (i = 0; i < num; i++) {
1827 u16 len = msgs[i].len;
1828
1829 if (msgs[i].flags & I2C_M_RD) {
1830 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
1831 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1832 } else {
1833 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
1834 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1835 }
1836 }
1837
1838 return 0;
1839}
1840
David Brownella1cdeda2008-07-14 22:38:24 +02001841/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001842 * __i2c_transfer - unlocked flavor of i2c_transfer
1843 * @adap: Handle to I2C bus
1844 * @msgs: One or more messages to execute before STOP is issued to
1845 * terminate the operation; each message begins with a START.
1846 * @num: Number of messages to be executed.
1847 *
1848 * Returns negative errno, else the number of messages executed.
1849 *
1850 * Adapter lock must be held when calling this function. No debug logging
1851 * takes place. adap->algo->master_xfer existence isn't checked.
1852 */
1853int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1854{
1855 unsigned long orig_jiffies;
1856 int ret, try;
1857
Wolfram Sangb7f62582015-01-05 23:45:59 +01001858 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
1859 return -EOPNOTSUPP;
1860
David Howellsd9a83d62014-03-06 13:35:59 +00001861 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
1862 * enabled. This is an efficient way of keeping the for-loop from
1863 * being executed when not needed.
1864 */
1865 if (static_key_false(&i2c_trace_msg)) {
1866 int i;
1867 for (i = 0; i < num; i++)
1868 if (msgs[i].flags & I2C_M_RD)
1869 trace_i2c_read(adap, &msgs[i], i);
1870 else
1871 trace_i2c_write(adap, &msgs[i], i);
1872 }
1873
Jean Delvareb37d2a32012-06-29 07:47:19 -03001874 /* Retry automatically on arbitration loss */
1875 orig_jiffies = jiffies;
1876 for (ret = 0, try = 0; try <= adap->retries; try++) {
1877 ret = adap->algo->master_xfer(adap, msgs, num);
1878 if (ret != -EAGAIN)
1879 break;
1880 if (time_after(jiffies, orig_jiffies + adap->timeout))
1881 break;
1882 }
1883
David Howellsd9a83d62014-03-06 13:35:59 +00001884 if (static_key_false(&i2c_trace_msg)) {
1885 int i;
1886 for (i = 0; i < ret; i++)
1887 if (msgs[i].flags & I2C_M_RD)
1888 trace_i2c_reply(adap, &msgs[i], i);
1889 trace_i2c_result(adap, i, ret);
1890 }
1891
Jean Delvareb37d2a32012-06-29 07:47:19 -03001892 return ret;
1893}
1894EXPORT_SYMBOL(__i2c_transfer);
1895
1896/**
David Brownella1cdeda2008-07-14 22:38:24 +02001897 * i2c_transfer - execute a single or combined I2C message
1898 * @adap: Handle to I2C bus
1899 * @msgs: One or more messages to execute before STOP is issued to
1900 * terminate the operation; each message begins with a START.
1901 * @num: Number of messages to be executed.
1902 *
1903 * Returns negative errno, else the number of messages executed.
1904 *
1905 * Note that there is no requirement that each message be sent to
1906 * the same slave address, although that is the most common model.
1907 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001908int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001910 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
David Brownella1cdeda2008-07-14 22:38:24 +02001912 /* REVISIT the fault reporting model here is weak:
1913 *
1914 * - When we get an error after receiving N bytes from a slave,
1915 * there is no way to report "N".
1916 *
1917 * - When we get a NAK after transmitting N bytes to a slave,
1918 * there is no way to report "N" ... or to let the master
1919 * continue executing the rest of this combined message, if
1920 * that's the appropriate response.
1921 *
1922 * - When for example "num" is two and we successfully complete
1923 * the first message but get an error part way through the
1924 * second, it's unclear whether that should be reported as
1925 * one (discarding status on the second message) or errno
1926 * (discarding status on the first one).
1927 */
1928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 if (adap->algo->master_xfer) {
1930#ifdef DEBUG
1931 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001932 dev_dbg(&adap->dev,
1933 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
1934 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
1935 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02001936 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
1938#endif
1939
Mike Rapoportcea443a82008-01-27 18:14:50 +01001940 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02001941 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001942 if (!ret)
1943 /* I2C activity is ongoing. */
1944 return -EAGAIN;
1945 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02001946 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001947 }
1948
Jean Delvareb37d2a32012-06-29 07:47:19 -03001949 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02001950 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 return ret;
1953 } else {
1954 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001955 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
1957}
David Brownellc0564602007-05-01 23:26:31 +02001958EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
David Brownella1cdeda2008-07-14 22:38:24 +02001960/**
1961 * i2c_master_send - issue a single I2C message in master transmit mode
1962 * @client: Handle to slave device
1963 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001964 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001965 *
1966 * Returns negative errno, or else the number of bytes written.
1967 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001968int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001971 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 struct i2c_msg msg;
1973
Jean Delvare815f55f2005-05-07 22:58:46 +02001974 msg.addr = client->addr;
1975 msg.flags = client->flags & I2C_M_TEN;
1976 msg.len = count;
1977 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001978
Jean Delvare815f55f2005-05-07 22:58:46 +02001979 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001981 /*
1982 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1983 * transmitted, else error code.
1984 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001985 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986}
David Brownellc0564602007-05-01 23:26:31 +02001987EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
David Brownella1cdeda2008-07-14 22:38:24 +02001989/**
1990 * i2c_master_recv - issue a single I2C message in master receive mode
1991 * @client: Handle to slave device
1992 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001993 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001994 *
1995 * Returns negative errno, or else the number of bytes read.
1996 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001997int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998{
Farid Hammane7225acf2010-05-21 18:40:58 +02001999 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 struct i2c_msg msg;
2001 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Jean Delvare815f55f2005-05-07 22:58:46 +02002003 msg.addr = client->addr;
2004 msg.flags = client->flags & I2C_M_TEN;
2005 msg.flags |= I2C_M_RD;
2006 msg.len = count;
2007 msg.buf = buf;
2008
2009 ret = i2c_transfer(adap, &msg, 1);
2010
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002011 /*
2012 * If everything went ok (i.e. 1 msg received), return #bytes received,
2013 * else error code.
2014 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002015 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
David Brownellc0564602007-05-01 23:26:31 +02002017EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019/* ----------------------------------------------------
2020 * the i2c address scanning function
2021 * Will not work for 10-bit addresses!
2022 * ----------------------------------------------------
2023 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002024
Jean Delvare63e4e802010-06-03 11:33:51 +02002025/*
2026 * Legacy default probe function, mostly relevant for SMBus. The default
2027 * probe method is a quick write, but it is known to corrupt the 24RF08
2028 * EEPROMs due to a state machine bug, and could also irreversibly
2029 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2030 * we use a short byte read instead. Also, some bus drivers don't implement
2031 * quick write, so we fallback to a byte read in that case too.
2032 * On x86, there is another special case for FSC hardware monitoring chips,
2033 * which want regular byte reads (address 0x73.) Fortunately, these are the
2034 * only known chips using this I2C address on PC hardware.
2035 * Returns 1 if probe succeeded, 0 if not.
2036 */
2037static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2038{
2039 int err;
2040 union i2c_smbus_data dummy;
2041
2042#ifdef CONFIG_X86
2043 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2044 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2045 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2046 I2C_SMBUS_BYTE_DATA, &dummy);
2047 else
2048#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002049 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2050 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002051 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2052 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002053 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2054 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2055 I2C_SMBUS_BYTE, &dummy);
2056 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002057 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2058 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002059 err = -EOPNOTSUPP;
2060 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002061
2062 return err >= 0;
2063}
2064
Jean Delvareccfbbd02009-12-06 17:06:25 +01002065static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002066 struct i2c_driver *driver)
2067{
2068 struct i2c_board_info info;
2069 struct i2c_adapter *adapter = temp_client->adapter;
2070 int addr = temp_client->addr;
2071 int err;
2072
2073 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002074 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002075 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002076 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2077 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002078 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002079 }
2080
Wolfram Sang9bccc702015-07-17 14:48:56 +02002081 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002082 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002083 return 0;
2084
Jean Delvareccfbbd02009-12-06 17:06:25 +01002085 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002086 if (!i2c_default_probe(adapter, addr))
2087 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002088
2089 /* Finally call the custom detection function */
2090 memset(&info, 0, sizeof(struct i2c_board_info));
2091 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002092 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002093 if (err) {
2094 /* -ENODEV is returned if the detection fails. We catch it
2095 here as this isn't an error. */
2096 return err == -ENODEV ? 0 : err;
2097 }
2098
2099 /* Consistency check */
2100 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002101 dev_err(&adapter->dev,
2102 "%s detection function provided no name for 0x%x\n",
2103 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002104 } else {
2105 struct i2c_client *client;
2106
2107 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002108 if (adapter->class & I2C_CLASS_DEPRECATED)
2109 dev_warn(&adapter->dev,
2110 "This adapter will soon drop class based instantiation of devices. "
2111 "Please make sure client 0x%02x gets instantiated by other means. "
2112 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2113 info.addr);
2114
Jean Delvare4735c982008-07-14 22:38:36 +02002115 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2116 info.type, info.addr);
2117 client = i2c_new_device(adapter, &info);
2118 if (client)
2119 list_add_tail(&client->detected, &driver->clients);
2120 else
2121 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2122 info.type, info.addr);
2123 }
2124 return 0;
2125}
2126
2127static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2128{
Jean Delvarec3813d62009-12-14 21:17:25 +01002129 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002130 struct i2c_client *temp_client;
2131 int i, err = 0;
2132 int adap_id = i2c_adapter_id(adapter);
2133
Jean Delvarec3813d62009-12-14 21:17:25 +01002134 address_list = driver->address_list;
2135 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002136 return 0;
2137
Wolfram Sang45552272014-07-10 13:46:21 +02002138 /* Warn that the adapter lost class based instantiation */
2139 if (adapter->class == I2C_CLASS_DEPRECATED) {
2140 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002141 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2142 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002143 driver->driver.name);
2144 return 0;
2145 }
2146
Jean Delvare51b54ba2010-10-24 18:16:58 +02002147 /* Stop here if the classes do not match */
2148 if (!(adapter->class & driver->class))
2149 return 0;
2150
Jean Delvare4735c982008-07-14 22:38:36 +02002151 /* Set up a temporary client to help detect callback */
2152 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2153 if (!temp_client)
2154 return -ENOMEM;
2155 temp_client->adapter = adapter;
2156
Jean Delvarec3813d62009-12-14 21:17:25 +01002157 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002158 dev_dbg(&adapter->dev,
2159 "found normal entry for adapter %d, addr 0x%02x\n",
2160 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002161 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002162 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002163 if (unlikely(err))
2164 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002165 }
2166
Jean Delvare4735c982008-07-14 22:38:36 +02002167 kfree(temp_client);
2168 return err;
2169}
2170
Jean Delvared44f19d2010-08-11 18:20:57 +02002171int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2172{
2173 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2174 I2C_SMBUS_QUICK, NULL) >= 0;
2175}
2176EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2177
Jean Delvare12b5053a2007-05-01 23:26:31 +02002178struct i2c_client *
2179i2c_new_probed_device(struct i2c_adapter *adap,
2180 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002181 unsigned short const *addr_list,
2182 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002183{
2184 int i;
2185
Jean Delvare8031d792010-08-11 18:21:00 +02002186 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002187 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002188
Jean Delvare12b5053a2007-05-01 23:26:31 +02002189 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2190 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002191 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002192 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2193 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002194 continue;
2195 }
2196
Wolfram Sang9bccc702015-07-17 14:48:56 +02002197 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002198 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002199 dev_dbg(&adap->dev,
2200 "Address 0x%02x already in use, not probing\n",
2201 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002202 continue;
2203 }
2204
Jean Delvare63e4e802010-06-03 11:33:51 +02002205 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002206 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002207 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002208 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002209
2210 if (addr_list[i] == I2C_CLIENT_END) {
2211 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2212 return NULL;
2213 }
2214
2215 info->addr = addr_list[i];
2216 return i2c_new_device(adap, info);
2217}
2218EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2219
Jean Delvared735b342011-03-20 14:50:52 +01002220struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002223
Jean Delvarecaada322008-01-27 18:14:49 +01002224 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002225 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002226 if (!adapter)
2227 goto exit;
2228
2229 if (try_module_get(adapter->owner))
2230 get_device(&adapter->dev);
2231 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002232 adapter = NULL;
2233
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002234 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002235 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002236 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
David Brownellc0564602007-05-01 23:26:31 +02002238EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
2240void i2c_put_adapter(struct i2c_adapter *adap)
2241{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002242 if (!adap)
2243 return;
2244
2245 put_device(&adap->dev);
2246 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
David Brownellc0564602007-05-01 23:26:31 +02002248EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2251MODULE_DESCRIPTION("I2C-Bus main module");
2252MODULE_LICENSE("GPL");