blob: c89dac7fd2e7b793217119f2ccee849cf75ebcfe [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 /*
356 * An I2C ID table is not mandatory, if and only if, a suitable Device
357 * Tree match table entry is supplied for the probing device.
358 */
359 if (!driver->id_table &&
360 !i2c_of_match_device(dev->driver->of_match_table, client))
David Brownell7b4fbc52007-05-01 23:26:30 +0200361 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200362
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700363 if (client->flags & I2C_CLIENT_WAKE) {
364 int wakeirq = -ENOENT;
365
366 if (dev->of_node) {
367 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
368 if (wakeirq == -EPROBE_DEFER)
369 return wakeirq;
370 }
371
372 device_init_wakeup(&client->dev, true);
373
374 if (wakeirq > 0 && wakeirq != client->irq)
375 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
376 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200377 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700378 else
379 status = 0;
380
381 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300382 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700383 }
384
David Brownell7b4fbc52007-05-01 23:26:30 +0200385 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200386
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200387 status = of_clk_set_defaults(dev->of_node, false);
388 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700389 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200390
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200391 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +0100392 if (status == -EPROBE_DEFER)
393 goto err_clear_wakeup_irq;
394
Lee Jonesb8a1a4c2016-11-07 12:47:41 +0000395 /*
396 * When there are no more users of probe(),
397 * rename probe_new to probe.
398 */
399 if (driver->probe_new)
400 status = driver->probe_new(client);
401 else if (driver->probe)
402 status = driver->probe(client,
403 i2c_match_id(driver->id_table, client));
404 else
405 status = -EINVAL;
406
Kieran Bingham74cedd32015-10-12 21:54:43 +0100407 if (status)
408 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100409
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700410 return 0;
411
412err_detach_pm_domain:
413 dev_pm_domain_detach(&client->dev, true);
414err_clear_wakeup_irq:
415 dev_pm_clear_wake_irq(&client->dev);
416 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +0100417 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420static int i2c_device_remove(struct device *dev)
421{
Jean Delvare51298d12009-09-18 22:45:45 +0200422 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200423 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100424 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200425
Jean Delvare51298d12009-09-18 22:45:45 +0200426 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200427 return 0;
428
429 driver = to_i2c_driver(dev->driver);
430 if (driver->remove) {
431 dev_dbg(dev, "remove\n");
432 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200433 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100434
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200435 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700436
437 dev_pm_clear_wake_irq(&client->dev);
438 device_init_wakeup(&client->dev, false);
439
David Brownella1d9e6e2007-05-01 23:26:30 +0200440 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441}
442
David Brownellf37dd802007-02-13 22:09:00 +0100443static void i2c_device_shutdown(struct device *dev)
444{
Jean Delvare51298d12009-09-18 22:45:45 +0200445 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100446 struct i2c_driver *driver;
447
Jean Delvare51298d12009-09-18 22:45:45 +0200448 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100449 return;
450 driver = to_i2c_driver(dev->driver);
451 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200452 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100453}
454
David Brownell9c1600e2007-05-01 23:26:31 +0200455static void i2c_client_dev_release(struct device *dev)
456{
457 kfree(to_i2c_client(dev));
458}
459
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100460static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200461show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200462{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200463 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
464 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200465}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100466static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200467
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100468static ssize_t
469show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200470{
471 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800472 int len;
473
474 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
475 if (len != -ENODEV)
476 return len;
477
Jean Delvareeb8a7902008-05-18 20:49:41 +0200478 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200479}
Jean Delvare51298d12009-09-18 22:45:45 +0200480static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
481
482static struct attribute *i2c_dev_attrs[] = {
483 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200484 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200485 &dev_attr_modalias.attr,
486 NULL
487};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100488ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100489
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200490struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100491 .name = "i2c",
492 .match = i2c_device_match,
493 .probe = i2c_device_probe,
494 .remove = i2c_device_remove,
495 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000496};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200497EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000498
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800499struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100500 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200501 .uevent = i2c_device_uevent,
502 .release = i2c_client_dev_release,
503};
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800504EXPORT_SYMBOL_GPL(i2c_client_type);
Jean Delvare51298d12009-09-18 22:45:45 +0200505
David Brownell9b766b82008-01-27 18:14:51 +0100506
507/**
508 * i2c_verify_client - return parameter as i2c_client, or NULL
509 * @dev: device, probably from some driver model iterator
510 *
511 * When traversing the driver model tree, perhaps using driver model
512 * iterators like @device_for_each_child(), you can't assume very much
513 * about the nodes you find. Use this function to avoid oopses caused
514 * by wrongly treating some non-I2C device as an i2c_client.
515 */
516struct i2c_client *i2c_verify_client(struct device *dev)
517{
Jean Delvare51298d12009-09-18 22:45:45 +0200518 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100519 ? to_i2c_client(dev)
520 : NULL;
521}
522EXPORT_SYMBOL(i2c_verify_client);
523
524
Wolfram Sangda899f52015-05-18 21:09:12 +0200525/* Return a unique address which takes the flags of the client into account */
526static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
527{
528 unsigned short addr = client->addr;
529
530 /* For some client flags, add an arbitrary offset to avoid collisions */
531 if (client->flags & I2C_CLIENT_TEN)
532 addr |= I2C_ADDR_OFFSET_TEN_BIT;
533
534 if (client->flags & I2C_CLIENT_SLAVE)
535 addr |= I2C_ADDR_OFFSET_SLAVE;
536
537 return addr;
538}
539
Jean Delvare3a89db52010-06-03 11:33:52 +0200540/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300541 * are purposely not enforced, except for the general call address. */
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200542int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +0200543{
Wolfram Sangc4019b72015-07-17 12:50:06 +0200544 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +0200545 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200546 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +0200547 return -EINVAL;
548 } else {
549 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200550 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +0200551 return -EINVAL;
552 }
553 return 0;
554}
555
Jean Delvare656b8762010-06-03 11:33:53 +0200556/* And this is a strict address validity check, used when probing. If a
557 * device uses a reserved address, then it shouldn't be probed. 7-bit
558 * addressing is assumed, 10-bit address devices are rare and should be
559 * explicitly enumerated. */
Wolfram Sange4991ec2017-05-23 11:14:17 +0200560int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +0200561{
562 /*
563 * Reserved addresses per I2C specification:
564 * 0x00 General call address / START byte
565 * 0x01 CBUS address
566 * 0x02 Reserved for different bus format
567 * 0x03 Reserved for future purposes
568 * 0x04-0x07 Hs-mode master code
569 * 0x78-0x7b 10-bit slave addressing
570 * 0x7c-0x7f Reserved for future purposes
571 */
572 if (addr < 0x08 || addr > 0x77)
573 return -EINVAL;
574 return 0;
575}
576
Jean Delvare3b5f7942010-06-03 11:33:55 +0200577static int __i2c_check_addr_busy(struct device *dev, void *addrp)
578{
579 struct i2c_client *client = i2c_verify_client(dev);
580 int addr = *(int *)addrp;
581
Wolfram Sang9bccc702015-07-17 14:48:56 +0200582 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +0200583 return -EBUSY;
584 return 0;
585}
586
Michael Lawnick08263742010-08-11 18:21:02 +0200587/* walk up mux tree */
588static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
589{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200590 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200591 int result;
592
593 result = device_for_each_child(&adapter->dev, &addr,
594 __i2c_check_addr_busy);
595
Jean Delvare97cc4d42010-10-24 18:16:57 +0200596 if (!result && parent)
597 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200598
599 return result;
600}
601
602/* recurse down mux tree */
603static int i2c_check_mux_children(struct device *dev, void *addrp)
604{
605 int result;
606
607 if (dev->type == &i2c_adapter_type)
608 result = device_for_each_child(dev, addrp,
609 i2c_check_mux_children);
610 else
611 result = __i2c_check_addr_busy(dev, addrp);
612
613 return result;
614}
615
Jean Delvare3b5f7942010-06-03 11:33:55 +0200616static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
617{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200618 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200619 int result = 0;
620
Jean Delvare97cc4d42010-10-24 18:16:57 +0200621 if (parent)
622 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200623
624 if (!result)
625 result = device_for_each_child(&adapter->dev, &addr,
626 i2c_check_mux_children);
627
628 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200629}
630
David Brownell9c1600e2007-05-01 23:26:31 +0200631/**
Peter Rosin8320f492016-05-04 22:15:27 +0200632 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200633 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200634 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
635 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200636 */
Peter Rosin8320f492016-05-04 22:15:27 +0200637static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
638 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200639{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200640 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200641}
Jean Delvarefe61e072010-08-11 18:20:58 +0200642
643/**
Peter Rosin8320f492016-05-04 22:15:27 +0200644 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200645 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200646 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
647 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200648 */
Peter Rosin8320f492016-05-04 22:15:27 +0200649static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
650 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200651{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200652 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200653}
654
655/**
Peter Rosin8320f492016-05-04 22:15:27 +0200656 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200657 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200658 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
659 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200660 */
Peter Rosin8320f492016-05-04 22:15:27 +0200661static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
662 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200663{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200664 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200665}
Jean Delvarefe61e072010-08-11 18:20:58 +0200666
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200667static void i2c_dev_set_name(struct i2c_adapter *adap,
668 struct i2c_client *client)
669{
670 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
671
672 if (adev) {
673 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
674 return;
675 }
676
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200677 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +0200678 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200679}
680
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800681static int i2c_dev_irq_from_resources(const struct resource *resources,
682 unsigned int num_resources)
683{
684 struct irq_data *irqd;
685 int i;
686
687 for (i = 0; i < num_resources; i++) {
688 const struct resource *r = &resources[i];
689
690 if (resource_type(r) != IORESOURCE_IRQ)
691 continue;
692
693 if (r->flags & IORESOURCE_BITS) {
694 irqd = irq_get_irq_data(r->start);
695 if (!irqd)
696 break;
697
698 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
699 }
700
701 return r->start;
702 }
703
704 return 0;
705}
706
Jean Delvarefe61e072010-08-11 18:20:58 +0200707/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200708 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200709 * @adap: the adapter managing the device
710 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200711 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200712 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200713 * Create an i2c device. Binding is handled through driver model
714 * probe()/remove() methods. A driver may be bound to this device when we
715 * return from this function, or any later moment (e.g. maybe hotplugging will
716 * load the driver module). This call is not appropriate for use by mainboard
717 * initialization logic, which usually runs during an arch_initcall() long
718 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200719 *
720 * This returns the new i2c client, which may be saved for later use with
721 * i2c_unregister_device(); or NULL to indicate an error.
722 */
723struct i2c_client *
724i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
725{
726 struct i2c_client *client;
727 int status;
728
729 client = kzalloc(sizeof *client, GFP_KERNEL);
730 if (!client)
731 return NULL;
732
733 client->adapter = adap;
734
735 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200736
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200737 if (info->archdata)
738 client->dev.archdata = *info->archdata;
739
Marc Pignatee354252008-08-28 08:33:22 +0200740 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200741 client->addr = info->addr;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800742
David Brownell9c1600e2007-05-01 23:26:31 +0200743 client->irq = info->irq;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800744 if (!client->irq)
745 client->irq = i2c_dev_irq_from_resources(info->resources,
746 info->num_resources);
David Brownell9c1600e2007-05-01 23:26:31 +0200747
David Brownell9c1600e2007-05-01 23:26:31 +0200748 strlcpy(client->name, info->type, sizeof(client->name));
749
Wolfram Sangc4019b72015-07-17 12:50:06 +0200750 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +0200751 if (status) {
752 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
753 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
754 goto out_err_silent;
755 }
756
Jean Delvaref8a227e2009-06-19 16:58:18 +0200757 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +0200758 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200759 if (status)
760 goto out_err;
761
762 client->dev.parent = &client->adapter->dev;
763 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200764 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700765 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100766 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200767
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200768 i2c_dev_set_name(adap, client);
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800769
770 if (info->properties) {
771 status = device_add_properties(&client->dev, info->properties);
772 if (status) {
773 dev_err(&adap->dev,
774 "Failed to add properties to client %s: %d\n",
775 client->name, status);
776 goto out_err;
777 }
778 }
779
Jean Delvaref8a227e2009-06-19 16:58:18 +0200780 status = device_register(&client->dev);
781 if (status)
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800782 goto out_free_props;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200783
Jean Delvaref8a227e2009-06-19 16:58:18 +0200784 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
785 client->name, dev_name(&client->dev));
786
David Brownell9c1600e2007-05-01 23:26:31 +0200787 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200788
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800789out_free_props:
790 if (info->properties)
791 device_remove_properties(&client->dev);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200792out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300793 dev_err(&adap->dev,
794 "Failed to register i2c client %s at 0x%02x (%d)\n",
795 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200796out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200797 kfree(client);
798 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200799}
800EXPORT_SYMBOL_GPL(i2c_new_device);
801
802
803/**
804 * i2c_unregister_device - reverse effect of i2c_new_device()
805 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200806 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200807 */
808void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200809{
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +0200810 if (client->dev.of_node)
811 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300812 if (ACPI_COMPANION(&client->dev))
813 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +0200814 device_unregister(&client->dev);
815}
David Brownell9c1600e2007-05-01 23:26:31 +0200816EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200817
818
Jean Delvare60b129d2008-05-11 20:37:06 +0200819static const struct i2c_device_id dummy_id[] = {
820 { "dummy", 0 },
821 { },
822};
823
Jean Delvared2653e92008-04-29 23:11:39 +0200824static int dummy_probe(struct i2c_client *client,
825 const struct i2c_device_id *id)
826{
827 return 0;
828}
829
830static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100831{
832 return 0;
833}
834
835static struct i2c_driver dummy_driver = {
836 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200837 .probe = dummy_probe,
838 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200839 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100840};
841
842/**
843 * i2c_new_dummy - return a new i2c device bound to a dummy driver
844 * @adapter: the adapter managing the device
845 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100846 * Context: can sleep
847 *
848 * This returns an I2C client bound to the "dummy" driver, intended for use
849 * with devices that consume multiple addresses. Examples of such chips
850 * include various EEPROMS (like 24c04 and 24c08 models).
851 *
852 * These dummy devices have two main uses. First, most I2C and SMBus calls
853 * except i2c_transfer() need a client handle; the dummy will be that handle.
854 * And second, this prevents the specified address from being bound to a
855 * different driver.
856 *
857 * This returns the new i2c client, which should be saved for later use with
858 * i2c_unregister_device(); or NULL to indicate an error.
859 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100860struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100861{
862 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200863 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100864 };
865
David Brownelle9f13732008-01-27 18:14:52 +0100866 return i2c_new_device(adapter, &info);
867}
868EXPORT_SYMBOL_GPL(i2c_new_dummy);
869
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +0100870/**
871 * i2c_new_secondary_device - Helper to get the instantiated secondary address
872 * and create the associated device
873 * @client: Handle to the primary client
874 * @name: Handle to specify which secondary address to get
875 * @default_addr: Used as a fallback if no secondary address was specified
876 * Context: can sleep
877 *
878 * I2C clients can be composed of multiple I2C slaves bound together in a single
879 * component. The I2C client driver then binds to the master I2C slave and needs
880 * to create I2C dummy clients to communicate with all the other slaves.
881 *
882 * This function creates and returns an I2C dummy client whose I2C address is
883 * retrieved from the platform firmware based on the given slave name. If no
884 * address is specified by the firmware default_addr is used.
885 *
886 * On DT-based platforms the address is retrieved from the "reg" property entry
887 * cell whose "reg-names" value matches the slave name.
888 *
889 * This returns the new i2c client, which should be saved for later use with
890 * i2c_unregister_device(); or NULL to indicate an error.
891 */
892struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
893 const char *name,
894 u16 default_addr)
895{
896 struct device_node *np = client->dev.of_node;
897 u32 addr = default_addr;
898 int i;
899
900 if (np) {
901 i = of_property_match_string(np, "reg-names", name);
902 if (i >= 0)
903 of_property_read_u32_index(np, "reg", i, &addr);
904 }
905
906 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
907 return i2c_new_dummy(client->adapter, addr);
908}
909EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
910
David Brownellf37dd802007-02-13 22:09:00 +0100911/* ------------------------------------------------------------------------- */
912
David Brownell16ffadf2007-05-01 23:26:28 +0200913/* I2C bus adapters -- one roots each I2C or SMBUS segment */
914
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200915static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
David Brownellef2c83212007-05-01 23:26:28 +0200917 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 complete(&adap->dev_released);
919}
920
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +0200921unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +0200922{
923 unsigned int depth = 0;
924
925 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
926 depth++;
927
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +0200928 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
929 "adapter depth exceeds lockdep subclass limit\n");
930
Jean Delvare390946b2012-09-10 10:14:02 +0200931 return depth;
932}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +0200933EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +0200934
935/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200936 * Let users instantiate I2C devices through sysfs. This can be used when
937 * platform initialization code doesn't contain the proper data for
938 * whatever reason. Also useful for drivers that do device detection and
939 * detection fails, either because the device uses an unexpected address,
940 * or this is a compatible device with different ID register values.
941 *
942 * Parameter checking may look overzealous, but we really don't want
943 * the user to provide incorrect parameters.
944 */
945static ssize_t
946i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
947 const char *buf, size_t count)
948{
949 struct i2c_adapter *adap = to_i2c_adapter(dev);
950 struct i2c_board_info info;
951 struct i2c_client *client;
952 char *blank, end;
953 int res;
954
Jean Delvare99cd8e22009-06-19 16:58:20 +0200955 memset(&info, 0, sizeof(struct i2c_board_info));
956
957 blank = strchr(buf, ' ');
958 if (!blank) {
959 dev_err(dev, "%s: Missing parameters\n", "new_device");
960 return -EINVAL;
961 }
962 if (blank - buf > I2C_NAME_SIZE - 1) {
963 dev_err(dev, "%s: Invalid device name\n", "new_device");
964 return -EINVAL;
965 }
966 memcpy(info.type, buf, blank - buf);
967
968 /* Parse remaining parameters, reject extra parameters */
969 res = sscanf(++blank, "%hi%c", &info.addr, &end);
970 if (res < 1) {
971 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
972 return -EINVAL;
973 }
974 if (res > 1 && end != '\n') {
975 dev_err(dev, "%s: Extra parameters\n", "new_device");
976 return -EINVAL;
977 }
978
Wolfram Sangcfa03272015-07-27 14:03:38 +0200979 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
980 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
981 info.flags |= I2C_CLIENT_TEN;
982 }
983
984 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
985 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
986 info.flags |= I2C_CLIENT_SLAVE;
987 }
988
Jean Delvare99cd8e22009-06-19 16:58:20 +0200989 client = i2c_new_device(adap, &info);
990 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200991 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200992
993 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200994 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200995 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200996 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200997 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
998 info.type, info.addr);
999
1000 return count;
1001}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001002static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001003
1004/*
1005 * And of course let the users delete the devices they instantiated, if
1006 * they got it wrong. This interface can only be used to delete devices
1007 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1008 * don't delete devices to which some kernel code still has references.
1009 *
1010 * Parameter checking may look overzealous, but we really don't want
1011 * the user to delete the wrong device.
1012 */
1013static ssize_t
1014i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1015 const char *buf, size_t count)
1016{
1017 struct i2c_adapter *adap = to_i2c_adapter(dev);
1018 struct i2c_client *client, *next;
1019 unsigned short addr;
1020 char end;
1021 int res;
1022
1023 /* Parse parameters, reject extra parameters */
1024 res = sscanf(buf, "%hi%c", &addr, &end);
1025 if (res < 1) {
1026 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1027 return -EINVAL;
1028 }
1029 if (res > 1 && end != '\n') {
1030 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1031 return -EINVAL;
1032 }
1033
1034 /* Make sure the device was added through sysfs */
1035 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001036 mutex_lock_nested(&adap->userspace_clients_lock,
1037 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001038 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1039 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001040 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001041 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1042 "delete_device", client->name, client->addr);
1043
1044 list_del(&client->detected);
1045 i2c_unregister_device(client);
1046 res = count;
1047 break;
1048 }
1049 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001050 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001051
1052 if (res < 0)
1053 dev_err(dev, "%s: Can't find device in list\n",
1054 "delete_device");
1055 return res;
1056}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001057static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1058 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001059
1060static struct attribute *i2c_adapter_attrs[] = {
1061 &dev_attr_name.attr,
1062 &dev_attr_new_device.attr,
1063 &dev_attr_delete_device.attr,
1064 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001065};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001066ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001067
Michael Lawnick08263742010-08-11 18:21:02 +02001068struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001069 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001070 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001071};
Michael Lawnick08263742010-08-11 18:21:02 +02001072EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Stephen Warren643dd092012-04-17 12:43:33 -06001074/**
1075 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1076 * @dev: device, probably from some driver model iterator
1077 *
1078 * When traversing the driver model tree, perhaps using driver model
1079 * iterators like @device_for_each_child(), you can't assume very much
1080 * about the nodes you find. Use this function to avoid oopses caused
1081 * by wrongly treating some non-I2C device as an i2c_adapter.
1082 */
1083struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1084{
1085 return (dev->type == &i2c_adapter_type)
1086 ? to_i2c_adapter(dev)
1087 : NULL;
1088}
1089EXPORT_SYMBOL(i2c_verify_adapter);
1090
Jean Delvare2bb50952009-09-18 22:45:46 +02001091#ifdef CONFIG_I2C_COMPAT
1092static struct class_compat *i2c_adapter_compat_class;
1093#endif
1094
David Brownell9c1600e2007-05-01 23:26:31 +02001095static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1096{
1097 struct i2c_devinfo *devinfo;
1098
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001099 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001100 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1101 if (devinfo->busnum == adapter->nr
1102 && !i2c_new_device(adapter,
1103 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001104 dev_err(&adapter->dev,
1105 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001106 devinfo->board_info.addr);
1107 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001108 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001109}
1110
Jean Delvare69b00892009-12-06 17:06:27 +01001111static int i2c_do_add_adapter(struct i2c_driver *driver,
1112 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001113{
Jean Delvare4735c982008-07-14 22:38:36 +02001114 /* Detect supported devices on that bus, and instantiate them */
1115 i2c_detect(adap, driver);
1116
1117 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001118 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001119 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1120 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001121 dev_warn(&adap->dev,
1122 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001123 /* We ignore the return code; if it fails, too bad */
1124 driver->attach_adapter(adap);
1125 }
1126 return 0;
1127}
1128
Jean Delvare69b00892009-12-06 17:06:27 +01001129static int __process_new_adapter(struct device_driver *d, void *data)
1130{
1131 return i2c_do_add_adapter(to_i2c_driver(d), data);
1132}
1133
Peter Rosind1ed7982016-08-25 23:07:01 +02001134static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1135 .lock_bus = i2c_adapter_lock_bus,
1136 .trylock_bus = i2c_adapter_trylock_bus,
1137 .unlock_bus = i2c_adapter_unlock_bus,
1138};
1139
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001140static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap)
1141{
1142 struct irq_domain *domain = adap->host_notify_domain;
1143 irq_hw_number_t hwirq;
1144
1145 if (!domain)
1146 return;
1147
1148 for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++)
1149 irq_dispose_mapping(irq_find_mapping(domain, hwirq));
1150
1151 irq_domain_remove(domain);
1152 adap->host_notify_domain = NULL;
1153}
1154
1155static int i2c_host_notify_irq_map(struct irq_domain *h,
1156 unsigned int virq,
1157 irq_hw_number_t hw_irq_num)
1158{
1159 irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
1160
1161 return 0;
1162}
1163
1164static const struct irq_domain_ops i2c_host_notify_irq_ops = {
1165 .map = i2c_host_notify_irq_map,
1166};
1167
1168static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap)
1169{
1170 struct irq_domain *domain;
1171
1172 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
1173 return 0;
1174
1175 domain = irq_domain_create_linear(adap->dev.fwnode,
1176 I2C_ADDR_7BITS_COUNT,
1177 &i2c_host_notify_irq_ops, adap);
1178 if (!domain)
1179 return -ENOMEM;
1180
1181 adap->host_notify_domain = domain;
1182
1183 return 0;
1184}
1185
1186/**
1187 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
1188 * I2C client.
1189 * @adap: the adapter
1190 * @addr: the I2C address of the notifying device
1191 * Context: can't sleep
1192 *
1193 * Helper function to be called from an I2C bus driver's interrupt
1194 * handler. It will schedule the Host Notify IRQ.
1195 */
1196int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
1197{
1198 int irq;
1199
1200 if (!adap)
1201 return -EINVAL;
1202
1203 irq = irq_find_mapping(adap->host_notify_domain, addr);
1204 if (irq <= 0)
1205 return -ENXIO;
1206
1207 generic_handle_irq(irq);
1208
1209 return 0;
1210}
1211EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
1212
David Brownell6e13e642007-05-01 23:26:31 +02001213static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001215 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
David Brownell1d0b19c2008-10-14 17:30:05 +02001217 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301218 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001219 res = -EAGAIN;
1220 goto out_list;
1221 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001222
Jean Delvare2236baa2010-11-15 22:40:38 +01001223 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001224 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001225 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001226
1227 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001228 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001229 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01001230 }
1231
Peter Rosind1ed7982016-08-25 23:07:01 +02001232 if (!adap->lock_ops)
1233 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001234
Mika Kuoppala194684e2009-12-06 17:06:22 +01001235 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001236 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001237 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001238 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Jean Delvare8fcfef62009-03-28 21:34:43 +01001240 /* Set default timeout to 1 second if not already set */
1241 if (adap->timeout == 0)
1242 adap->timeout = HZ;
1243
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001244 /* register soft irqs for Host Notify */
1245 res = i2c_setup_host_notify_irq_domain(adap);
1246 if (res) {
1247 pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n",
1248 adap->name, res);
1249 goto out_list;
1250 }
1251
Kay Sievers27d9c182009-01-07 14:29:16 +01001252 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001253 adap->dev.bus = &i2c_bus_type;
1254 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001255 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001256 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001257 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001258 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001261 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1262
Charles Keepax6ada5c12015-04-16 13:05:19 +01001263 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001264 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001265 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001266
Jean Delvare2bb50952009-09-18 22:45:46 +02001267#ifdef CONFIG_I2C_COMPAT
1268 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1269 adap->dev.parent);
1270 if (res)
1271 dev_warn(&adap->dev,
1272 "Failed to create compatibility class link\n");
1273#endif
1274
Wolfram Sangd3b11d82016-07-09 13:34:59 +09001275 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301276
Jean Delvare729d6dd2009-06-19 16:58:18 +02001277 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001278 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001279 i2c_acpi_register_devices(adap);
1280 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001281
David Brownell6e13e642007-05-01 23:26:31 +02001282 if (adap->nr < __i2c_first_dynamic_bus_num)
1283 i2c_scan_static_board_info(adap);
1284
Jean Delvare4735c982008-07-14 22:38:36 +02001285 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001286 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001287 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001288 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001289
1290 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001291
Jean Delvareb119c6c2006-08-15 18:26:30 +02001292out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001293 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001294 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001295 mutex_unlock(&core_lock);
1296 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
1298
David Brownell6e13e642007-05-01 23:26:31 +02001299/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001300 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1301 * @adap: the adapter to register (with adap->nr initialized)
1302 * Context: can sleep
1303 *
1304 * See i2c_add_numbered_adapter() for details.
1305 */
1306static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1307{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001308 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001309
1310 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001311 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001312 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001313 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001314 return id == -ENOSPC ? -EBUSY : id;
1315
1316 return i2c_register_adapter(adap);
1317}
1318
1319/**
David Brownell6e13e642007-05-01 23:26:31 +02001320 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1321 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001322 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001323 *
1324 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001325 * doesn't matter or when its bus number is specified by an dt alias.
1326 * Examples of bases when the bus number doesn't matter: I2C adapters
1327 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001328 *
1329 * When this returns zero, a new bus number was allocated and stored
1330 * in adap->nr, and the specified adapter became available for clients.
1331 * Otherwise, a negative errno value is returned.
1332 */
1333int i2c_add_adapter(struct i2c_adapter *adapter)
1334{
Doug Andersonee5c2742013-03-01 08:57:31 -08001335 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001336 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001337
Doug Andersonee5c2742013-03-01 08:57:31 -08001338 if (dev->of_node) {
1339 id = of_alias_get_id(dev->of_node, "i2c");
1340 if (id >= 0) {
1341 adapter->nr = id;
1342 return __i2c_add_numbered_adapter(adapter);
1343 }
1344 }
1345
Jean Delvarecaada322008-01-27 18:14:49 +01001346 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001347 id = idr_alloc(&i2c_adapter_idr, adapter,
1348 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001349 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001350 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08001351 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001352
1353 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001354
David Brownell6e13e642007-05-01 23:26:31 +02001355 return i2c_register_adapter(adapter);
1356}
1357EXPORT_SYMBOL(i2c_add_adapter);
1358
1359/**
1360 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1361 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001362 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001363 *
1364 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001365 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1366 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001367 * is used to properly configure I2C devices.
1368 *
Grant Likely488bf312011-07-25 17:49:43 +02001369 * If the requested bus number is set to -1, then this function will behave
1370 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1371 *
David Brownell6e13e642007-05-01 23:26:31 +02001372 * If no devices have pre-been declared for this bus, then be sure to
1373 * register the adapter before any dynamically allocated ones. Otherwise
1374 * the required bus ID may not be available.
1375 *
1376 * When this returns zero, the specified adapter became available for
1377 * clients using the bus number provided in adap->nr. Also, the table
1378 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1379 * and the appropriate driver model device nodes are created. Otherwise, a
1380 * negative errno value is returned.
1381 */
1382int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1383{
Grant Likely488bf312011-07-25 17:49:43 +02001384 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1385 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001386
Doug Andersonee5c2742013-03-01 08:57:31 -08001387 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001388}
1389EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1390
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001391static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001392 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001393{
Jean Delvare4735c982008-07-14 22:38:36 +02001394 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001395
Jean Delvareacec2112009-03-28 21:34:40 +01001396 /* Remove the devices we created ourselves as the result of hardware
1397 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001398 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1399 if (client->adapter == adapter) {
1400 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1401 client->name, client->addr);
1402 list_del(&client->detected);
1403 i2c_unregister_device(client);
1404 }
1405 }
Jean Delvare026526f2008-01-27 18:14:49 +01001406}
1407
Jean Delvaree549c2b2009-06-19 16:58:19 +02001408static int __unregister_client(struct device *dev, void *dummy)
1409{
1410 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001411 if (client && strcmp(client->name, "dummy"))
1412 i2c_unregister_device(client);
1413 return 0;
1414}
1415
1416static int __unregister_dummy(struct device *dev, void *dummy)
1417{
1418 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001419 if (client)
1420 i2c_unregister_device(client);
1421 return 0;
1422}
1423
Jean Delvare69b00892009-12-06 17:06:27 +01001424static int __process_removed_adapter(struct device_driver *d, void *data)
1425{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001426 i2c_do_del_adapter(to_i2c_driver(d), data);
1427 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001428}
1429
David Brownelld64f73b2007-07-12 14:12:28 +02001430/**
1431 * i2c_del_adapter - unregister I2C adapter
1432 * @adap: the adapter being unregistered
1433 * Context: can sleep
1434 *
1435 * This unregisters an I2C adapter which was previously registered
1436 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1437 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001438void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001440 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001441 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001444 mutex_lock(&core_lock);
1445 found = idr_find(&i2c_adapter_idr, adap->nr);
1446 mutex_unlock(&core_lock);
1447 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001448 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001449 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001452 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001453 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001454 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001455 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001456 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001457 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001459 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001460 mutex_lock_nested(&adap->userspace_clients_lock,
1461 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001462 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1463 detected) {
1464 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1465 client->addr);
1466 list_del(&client->detected);
1467 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001468 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001469 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001470
Jean Delvaree549c2b2009-06-19 16:58:19 +02001471 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001472 * check the returned value. This is a two-pass process, because
1473 * we can't remove the dummy devices during the first pass: they
1474 * could have been instantiated by real devices wishing to clean
1475 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001476 device_for_each_child(&adap->dev, NULL, __unregister_client);
1477 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Jean Delvare2bb50952009-09-18 22:45:46 +02001479#ifdef CONFIG_I2C_COMPAT
1480 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1481 adap->dev.parent);
1482#endif
1483
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001484 /* device name is gone after device_unregister */
1485 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1486
Wolfram Sang9f924162015-12-23 18:19:28 +01001487 pm_runtime_disable(&adap->dev);
1488
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001489 i2c_host_notify_irq_teardown(adap);
1490
Wolfram Sang26680ee2015-01-29 22:45:09 +01001491 /* wait until all references to the device are gone
1492 *
1493 * FIXME: This is old code and should ideally be replaced by an
1494 * alternative which results in decoupling the lifetime of the struct
1495 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301496 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001497 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
David Brownell6e13e642007-05-01 23:26:31 +02001502 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001503 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001505 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001507 /* Clear the device structure in case this adapter is ever going to be
1508 added again */
1509 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
David Brownellc0564602007-05-01 23:26:31 +02001511EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Wolfram Sang54177cc2015-12-17 13:32:36 +01001513/**
1514 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
1515 * @dev: The device to scan for I2C timing properties
1516 * @t: the i2c_timings struct to be filled with values
1517 * @use_defaults: bool to use sane defaults derived from the I2C specification
1518 * when properties are not found, otherwise use 0
1519 *
1520 * Scan the device for the generic I2C properties describing timing parameters
1521 * for the signal and fill the given struct with the results. If a property was
1522 * not found and use_defaults was true, then maximum timings are assumed which
1523 * are derived from the I2C specification. If use_defaults is not used, the
1524 * results will be 0, so drivers can apply their own defaults later. The latter
1525 * is mainly intended for avoiding regressions of existing drivers which want
1526 * to switch to this function. New drivers almost always should use the defaults.
1527 */
1528
1529void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
1530{
1531 int ret;
1532
1533 memset(t, 0, sizeof(*t));
1534
1535 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
1536 if (ret && use_defaults)
1537 t->bus_freq_hz = 100000;
1538
1539 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
1540 if (ret && use_defaults) {
1541 if (t->bus_freq_hz <= 100000)
1542 t->scl_rise_ns = 1000;
1543 else if (t->bus_freq_hz <= 400000)
1544 t->scl_rise_ns = 300;
1545 else
1546 t->scl_rise_ns = 120;
1547 }
1548
1549 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
1550 if (ret && use_defaults) {
1551 if (t->bus_freq_hz <= 400000)
1552 t->scl_fall_ns = 300;
1553 else
1554 t->scl_fall_ns = 120;
1555 }
1556
1557 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
1558
1559 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
1560 if (ret && use_defaults)
1561 t->sda_fall_ns = t->scl_fall_ns;
1562}
1563EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
1564
David Brownell7b4fbc52007-05-01 23:26:30 +02001565/* ------------------------------------------------------------------------- */
1566
Jean Delvare7ae31482011-03-20 14:50:52 +01001567int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1568{
1569 int res;
1570
1571 mutex_lock(&core_lock);
1572 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1573 mutex_unlock(&core_lock);
1574
1575 return res;
1576}
1577EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1578
Jean Delvare69b00892009-12-06 17:06:27 +01001579static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001580{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001581 if (dev->type != &i2c_adapter_type)
1582 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001583 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001584}
1585
David Brownell7b4fbc52007-05-01 23:26:30 +02001586/*
1587 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001588 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 */
1590
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001591int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001593 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
David Brownell1d0b19c2008-10-14 17:30:05 +02001595 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301596 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02001597 return -EAGAIN;
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001600 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02001602 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Jean Delvare729d6dd2009-06-19 16:58:18 +02001604 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001605 * will have called probe() for all matching-but-unbound devices.
1606 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 res = driver_register(&driver->driver);
1608 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001609 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001610
Wolfram Sang44239a52016-07-09 13:35:04 +09001611 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Jean Delvare4735c982008-07-14 22:38:36 +02001613 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001614 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001615
Jean Delvare7eebcb72006-02-05 23:28:21 +01001616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001618EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Jean Delvare69b00892009-12-06 17:06:27 +01001620static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001621{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001622 if (dev->type == &i2c_adapter_type)
1623 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1624 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001625}
1626
David Brownella1d9e6e2007-05-01 23:26:30 +02001627/**
1628 * i2c_del_driver - unregister I2C driver
1629 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001630 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001631 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001632void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
Jean Delvare7ae31482011-03-20 14:50:52 +01001634 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09001637 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
David Brownellc0564602007-05-01 23:26:31 +02001639EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
David Brownell7b4fbc52007-05-01 23:26:30 +02001641/* ------------------------------------------------------------------------- */
1642
Jean Delvaree48d3312008-01-27 18:14:48 +01001643/**
1644 * i2c_use_client - increments the reference count of the i2c client structure
1645 * @client: the client being referenced
1646 *
1647 * Each live reference to a client should be refcounted. The driver model does
1648 * that automatically as part of driver binding, so that most drivers don't
1649 * need to do this explicitly: they hold a reference until they're unbound
1650 * from the device.
1651 *
1652 * A pointer to the client with the incremented reference counter is returned.
1653 */
1654struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
David Brownell6ea438e2008-07-14 22:38:24 +02001656 if (client && get_device(&client->dev))
1657 return client;
1658 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
David Brownellc0564602007-05-01 23:26:31 +02001660EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Jean Delvaree48d3312008-01-27 18:14:48 +01001662/**
1663 * i2c_release_client - release a use of the i2c client structure
1664 * @client: the client being no longer referenced
1665 *
1666 * Must be called when a user of a client is finished with it.
1667 */
1668void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669{
David Brownell6ea438e2008-07-14 22:38:24 +02001670 if (client)
1671 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
David Brownellc0564602007-05-01 23:26:31 +02001673EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
David Brownell9b766b82008-01-27 18:14:51 +01001675struct i2c_cmd_arg {
1676 unsigned cmd;
1677 void *arg;
1678};
1679
1680static int i2c_cmd(struct device *dev, void *_arg)
1681{
1682 struct i2c_client *client = i2c_verify_client(dev);
1683 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001684 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001685
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001686 if (!client || !client->dev.driver)
1687 return 0;
1688
1689 driver = to_i2c_driver(client->dev.driver);
1690 if (driver->command)
1691 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001692 return 0;
1693}
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1696{
David Brownell9b766b82008-01-27 18:14:51 +01001697 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
David Brownell9b766b82008-01-27 18:14:51 +01001699 cmd_arg.cmd = cmd;
1700 cmd_arg.arg = arg;
1701 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
David Brownellc0564602007-05-01 23:26:31 +02001703EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705static int __init i2c_init(void)
1706{
1707 int retval;
1708
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001709 retval = of_alias_get_highest_id("i2c");
1710
1711 down_write(&__i2c_board_lock);
1712 if (retval >= __i2c_first_dynamic_bus_num)
1713 __i2c_first_dynamic_bus_num = retval + 1;
1714 up_write(&__i2c_board_lock);
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 retval = bus_register(&i2c_bus_type);
1717 if (retval)
1718 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01001719
1720 is_registered = true;
1721
Jean Delvare2bb50952009-09-18 22:45:46 +02001722#ifdef CONFIG_I2C_COMPAT
1723 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1724 if (!i2c_adapter_compat_class) {
1725 retval = -ENOMEM;
1726 goto bus_err;
1727 }
1728#endif
David Brownelle9f13732008-01-27 18:14:52 +01001729 retval = i2c_add_driver(&dummy_driver);
1730 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001731 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001732
1733 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1734 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001735 if (IS_ENABLED(CONFIG_ACPI))
1736 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001737
David Brownelle9f13732008-01-27 18:14:52 +01001738 return 0;
1739
Jean Delvare2bb50952009-09-18 22:45:46 +02001740class_err:
1741#ifdef CONFIG_I2C_COMPAT
1742 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001743bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001744#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01001745 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01001746 bus_unregister(&i2c_bus_type);
1747 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748}
1749
1750static void __exit i2c_exit(void)
1751{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001752 if (IS_ENABLED(CONFIG_ACPI))
1753 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001754 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1755 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001756 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001757#ifdef CONFIG_I2C_COMPAT
1758 class_compat_unregister(i2c_adapter_compat_class);
1759#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001761 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762}
1763
David Brownella10f9e72008-10-14 17:30:06 +02001764/* We must initialize early, because some subsystems register i2c drivers
1765 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1766 */
1767postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768module_exit(i2c_exit);
1769
1770/* ----------------------------------------------------
1771 * the functional interface to the i2c busses.
1772 * ----------------------------------------------------
1773 */
1774
Wolfram Sangb7f62582015-01-05 23:45:59 +01001775/* Check if val is exceeding the quirk IFF quirk is non 0 */
1776#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1777
1778static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
1779{
1780 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
1781 err_msg, msg->addr, msg->len,
1782 msg->flags & I2C_M_RD ? "read" : "write");
1783 return -EOPNOTSUPP;
1784}
1785
1786static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1787{
1788 const struct i2c_adapter_quirks *q = adap->quirks;
1789 int max_num = q->max_num_msgs, i;
1790 bool do_len_check = true;
1791
1792 if (q->flags & I2C_AQ_COMB) {
1793 max_num = 2;
1794
1795 /* special checks for combined messages */
1796 if (num == 2) {
1797 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
1798 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
1799
1800 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
1801 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
1802
1803 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
1804 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
1805
1806 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
1807 return i2c_quirk_error(adap, &msgs[0], "msg too long");
1808
1809 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
1810 return i2c_quirk_error(adap, &msgs[1], "msg too long");
1811
1812 do_len_check = false;
1813 }
1814 }
1815
1816 if (i2c_quirk_exceeded(num, max_num))
1817 return i2c_quirk_error(adap, &msgs[0], "too many messages");
1818
1819 for (i = 0; i < num; i++) {
1820 u16 len = msgs[i].len;
1821
1822 if (msgs[i].flags & I2C_M_RD) {
1823 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
1824 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1825 } else {
1826 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
1827 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1828 }
1829 }
1830
1831 return 0;
1832}
1833
David Brownella1cdeda2008-07-14 22:38:24 +02001834/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001835 * __i2c_transfer - unlocked flavor of i2c_transfer
1836 * @adap: Handle to I2C bus
1837 * @msgs: One or more messages to execute before STOP is issued to
1838 * terminate the operation; each message begins with a START.
1839 * @num: Number of messages to be executed.
1840 *
1841 * Returns negative errno, else the number of messages executed.
1842 *
1843 * Adapter lock must be held when calling this function. No debug logging
1844 * takes place. adap->algo->master_xfer existence isn't checked.
1845 */
1846int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1847{
1848 unsigned long orig_jiffies;
1849 int ret, try;
1850
Wolfram Sangb7f62582015-01-05 23:45:59 +01001851 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
1852 return -EOPNOTSUPP;
1853
David Howellsd9a83d62014-03-06 13:35:59 +00001854 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
1855 * enabled. This is an efficient way of keeping the for-loop from
1856 * being executed when not needed.
1857 */
1858 if (static_key_false(&i2c_trace_msg)) {
1859 int i;
1860 for (i = 0; i < num; i++)
1861 if (msgs[i].flags & I2C_M_RD)
1862 trace_i2c_read(adap, &msgs[i], i);
1863 else
1864 trace_i2c_write(adap, &msgs[i], i);
1865 }
1866
Jean Delvareb37d2a32012-06-29 07:47:19 -03001867 /* Retry automatically on arbitration loss */
1868 orig_jiffies = jiffies;
1869 for (ret = 0, try = 0; try <= adap->retries; try++) {
1870 ret = adap->algo->master_xfer(adap, msgs, num);
1871 if (ret != -EAGAIN)
1872 break;
1873 if (time_after(jiffies, orig_jiffies + adap->timeout))
1874 break;
1875 }
1876
David Howellsd9a83d62014-03-06 13:35:59 +00001877 if (static_key_false(&i2c_trace_msg)) {
1878 int i;
1879 for (i = 0; i < ret; i++)
1880 if (msgs[i].flags & I2C_M_RD)
1881 trace_i2c_reply(adap, &msgs[i], i);
1882 trace_i2c_result(adap, i, ret);
1883 }
1884
Jean Delvareb37d2a32012-06-29 07:47:19 -03001885 return ret;
1886}
1887EXPORT_SYMBOL(__i2c_transfer);
1888
1889/**
David Brownella1cdeda2008-07-14 22:38:24 +02001890 * i2c_transfer - execute a single or combined I2C message
1891 * @adap: Handle to I2C bus
1892 * @msgs: One or more messages to execute before STOP is issued to
1893 * terminate the operation; each message begins with a START.
1894 * @num: Number of messages to be executed.
1895 *
1896 * Returns negative errno, else the number of messages executed.
1897 *
1898 * Note that there is no requirement that each message be sent to
1899 * the same slave address, although that is the most common model.
1900 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001901int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001903 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
David Brownella1cdeda2008-07-14 22:38:24 +02001905 /* REVISIT the fault reporting model here is weak:
1906 *
1907 * - When we get an error after receiving N bytes from a slave,
1908 * there is no way to report "N".
1909 *
1910 * - When we get a NAK after transmitting N bytes to a slave,
1911 * there is no way to report "N" ... or to let the master
1912 * continue executing the rest of this combined message, if
1913 * that's the appropriate response.
1914 *
1915 * - When for example "num" is two and we successfully complete
1916 * the first message but get an error part way through the
1917 * second, it's unclear whether that should be reported as
1918 * one (discarding status on the second message) or errno
1919 * (discarding status on the first one).
1920 */
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 if (adap->algo->master_xfer) {
1923#ifdef DEBUG
1924 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001925 dev_dbg(&adap->dev,
1926 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
1927 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
1928 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02001929 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
1931#endif
1932
Mike Rapoportcea443a82008-01-27 18:14:50 +01001933 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02001934 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001935 if (!ret)
1936 /* I2C activity is ongoing. */
1937 return -EAGAIN;
1938 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02001939 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001940 }
1941
Jean Delvareb37d2a32012-06-29 07:47:19 -03001942 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02001943 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 return ret;
1946 } else {
1947 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001948 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950}
David Brownellc0564602007-05-01 23:26:31 +02001951EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
David Brownella1cdeda2008-07-14 22:38:24 +02001953/**
1954 * i2c_master_send - issue a single I2C message in master transmit mode
1955 * @client: Handle to slave device
1956 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001957 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001958 *
1959 * Returns negative errno, or else the number of bytes written.
1960 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001961int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001964 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 struct i2c_msg msg;
1966
Jean Delvare815f55f2005-05-07 22:58:46 +02001967 msg.addr = client->addr;
1968 msg.flags = client->flags & I2C_M_TEN;
1969 msg.len = count;
1970 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001971
Jean Delvare815f55f2005-05-07 22:58:46 +02001972 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001974 /*
1975 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1976 * transmitted, else error code.
1977 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001978 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
David Brownellc0564602007-05-01 23:26:31 +02001980EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
David Brownella1cdeda2008-07-14 22:38:24 +02001982/**
1983 * i2c_master_recv - issue a single I2C message in master receive mode
1984 * @client: Handle to slave device
1985 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001986 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001987 *
1988 * Returns negative errno, or else the number of bytes read.
1989 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001990int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991{
Farid Hammane7225acf2010-05-21 18:40:58 +02001992 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 struct i2c_msg msg;
1994 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Jean Delvare815f55f2005-05-07 22:58:46 +02001996 msg.addr = client->addr;
1997 msg.flags = client->flags & I2C_M_TEN;
1998 msg.flags |= I2C_M_RD;
1999 msg.len = count;
2000 msg.buf = buf;
2001
2002 ret = i2c_transfer(adap, &msg, 1);
2003
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002004 /*
2005 * If everything went ok (i.e. 1 msg received), return #bytes received,
2006 * else error code.
2007 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002008 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
David Brownellc0564602007-05-01 23:26:31 +02002010EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012/* ----------------------------------------------------
2013 * the i2c address scanning function
2014 * Will not work for 10-bit addresses!
2015 * ----------------------------------------------------
2016 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002017
Jean Delvare63e4e802010-06-03 11:33:51 +02002018/*
2019 * Legacy default probe function, mostly relevant for SMBus. The default
2020 * probe method is a quick write, but it is known to corrupt the 24RF08
2021 * EEPROMs due to a state machine bug, and could also irreversibly
2022 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2023 * we use a short byte read instead. Also, some bus drivers don't implement
2024 * quick write, so we fallback to a byte read in that case too.
2025 * On x86, there is another special case for FSC hardware monitoring chips,
2026 * which want regular byte reads (address 0x73.) Fortunately, these are the
2027 * only known chips using this I2C address on PC hardware.
2028 * Returns 1 if probe succeeded, 0 if not.
2029 */
2030static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2031{
2032 int err;
2033 union i2c_smbus_data dummy;
2034
2035#ifdef CONFIG_X86
2036 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2037 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2038 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2039 I2C_SMBUS_BYTE_DATA, &dummy);
2040 else
2041#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002042 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2043 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002044 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2045 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002046 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2047 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2048 I2C_SMBUS_BYTE, &dummy);
2049 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002050 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2051 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002052 err = -EOPNOTSUPP;
2053 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002054
2055 return err >= 0;
2056}
2057
Jean Delvareccfbbd02009-12-06 17:06:25 +01002058static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002059 struct i2c_driver *driver)
2060{
2061 struct i2c_board_info info;
2062 struct i2c_adapter *adapter = temp_client->adapter;
2063 int addr = temp_client->addr;
2064 int err;
2065
2066 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002067 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002068 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002069 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2070 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002071 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002072 }
2073
Wolfram Sang9bccc702015-07-17 14:48:56 +02002074 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002075 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002076 return 0;
2077
Jean Delvareccfbbd02009-12-06 17:06:25 +01002078 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002079 if (!i2c_default_probe(adapter, addr))
2080 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002081
2082 /* Finally call the custom detection function */
2083 memset(&info, 0, sizeof(struct i2c_board_info));
2084 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002085 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002086 if (err) {
2087 /* -ENODEV is returned if the detection fails. We catch it
2088 here as this isn't an error. */
2089 return err == -ENODEV ? 0 : err;
2090 }
2091
2092 /* Consistency check */
2093 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002094 dev_err(&adapter->dev,
2095 "%s detection function provided no name for 0x%x\n",
2096 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002097 } else {
2098 struct i2c_client *client;
2099
2100 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002101 if (adapter->class & I2C_CLASS_DEPRECATED)
2102 dev_warn(&adapter->dev,
2103 "This adapter will soon drop class based instantiation of devices. "
2104 "Please make sure client 0x%02x gets instantiated by other means. "
2105 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2106 info.addr);
2107
Jean Delvare4735c982008-07-14 22:38:36 +02002108 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2109 info.type, info.addr);
2110 client = i2c_new_device(adapter, &info);
2111 if (client)
2112 list_add_tail(&client->detected, &driver->clients);
2113 else
2114 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2115 info.type, info.addr);
2116 }
2117 return 0;
2118}
2119
2120static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2121{
Jean Delvarec3813d62009-12-14 21:17:25 +01002122 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002123 struct i2c_client *temp_client;
2124 int i, err = 0;
2125 int adap_id = i2c_adapter_id(adapter);
2126
Jean Delvarec3813d62009-12-14 21:17:25 +01002127 address_list = driver->address_list;
2128 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002129 return 0;
2130
Wolfram Sang45552272014-07-10 13:46:21 +02002131 /* Warn that the adapter lost class based instantiation */
2132 if (adapter->class == I2C_CLASS_DEPRECATED) {
2133 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002134 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2135 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002136 driver->driver.name);
2137 return 0;
2138 }
2139
Jean Delvare51b54ba2010-10-24 18:16:58 +02002140 /* Stop here if the classes do not match */
2141 if (!(adapter->class & driver->class))
2142 return 0;
2143
Jean Delvare4735c982008-07-14 22:38:36 +02002144 /* Set up a temporary client to help detect callback */
2145 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2146 if (!temp_client)
2147 return -ENOMEM;
2148 temp_client->adapter = adapter;
2149
Jean Delvarec3813d62009-12-14 21:17:25 +01002150 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002151 dev_dbg(&adapter->dev,
2152 "found normal entry for adapter %d, addr 0x%02x\n",
2153 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002154 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002155 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002156 if (unlikely(err))
2157 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002158 }
2159
Jean Delvare4735c982008-07-14 22:38:36 +02002160 kfree(temp_client);
2161 return err;
2162}
2163
Jean Delvared44f19d2010-08-11 18:20:57 +02002164int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2165{
2166 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2167 I2C_SMBUS_QUICK, NULL) >= 0;
2168}
2169EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2170
Jean Delvare12b5053a2007-05-01 23:26:31 +02002171struct i2c_client *
2172i2c_new_probed_device(struct i2c_adapter *adap,
2173 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002174 unsigned short const *addr_list,
2175 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002176{
2177 int i;
2178
Jean Delvare8031d792010-08-11 18:21:00 +02002179 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002180 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002181
Jean Delvare12b5053a2007-05-01 23:26:31 +02002182 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2183 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002184 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002185 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2186 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002187 continue;
2188 }
2189
Wolfram Sang9bccc702015-07-17 14:48:56 +02002190 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002191 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002192 dev_dbg(&adap->dev,
2193 "Address 0x%02x already in use, not probing\n",
2194 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002195 continue;
2196 }
2197
Jean Delvare63e4e802010-06-03 11:33:51 +02002198 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002199 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002200 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002201 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002202
2203 if (addr_list[i] == I2C_CLIENT_END) {
2204 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2205 return NULL;
2206 }
2207
2208 info->addr = addr_list[i];
2209 return i2c_new_device(adap, info);
2210}
2211EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2212
Jean Delvared735b342011-03-20 14:50:52 +01002213struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002216
Jean Delvarecaada322008-01-27 18:14:49 +01002217 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002218 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002219 if (!adapter)
2220 goto exit;
2221
2222 if (try_module_get(adapter->owner))
2223 get_device(&adapter->dev);
2224 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002225 adapter = NULL;
2226
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002227 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002228 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002229 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
David Brownellc0564602007-05-01 23:26:31 +02002231EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233void i2c_put_adapter(struct i2c_adapter *adap)
2234{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002235 if (!adap)
2236 return;
2237
2238 put_device(&adap->dev);
2239 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
David Brownellc0564602007-05-01 23:26:31 +02002241EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2244MODULE_DESCRIPTION("I2C-Bus main module");
2245MODULE_LICENSE("GPL");