blob: 706164b4c5be46bf2f98aa5a8ddde44800eeae0e [file] [log] [blame]
Wolfram Sang61e3d0f2017-05-23 19:23:11 +02001/*
2 * Linux I2C core
3 *
4 * Copyright (C) 1995-99 Simon G. Vogl
5 * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>
6 * Mux support by Rodolfo Giometti <giometti@enneenne.com> and
7 * Michael Lawnick <michael.lawnick.ext@nsn.com>
8 *
9 * Copyright (C) 2013-2017 Wolfram Sang <wsa@the-dreams.de>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
Wolfram Sang687b81d2013-07-11 12:56:15 +010019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Wolfram Sang44239a52016-07-09 13:35:04 +090021#define pr_fmt(fmt) "i2c-core: " fmt
22
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +020023#include <dt-bindings/i2c/i2c.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010024#include <linux/acpi.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020025#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010026#include <linux/completion.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010027#include <linux/delay.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020028#include <linux/err.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010029#include <linux/errno.h>
30#include <linux/gpio.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010031#include <linux/i2c.h>
Phil Reidf8756c62017-08-24 17:31:04 +080032#include <linux/i2c-smbus.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010033#include <linux/idr.h>
34#include <linux/init.h>
35#include <linux/irqflags.h>
36#include <linux/jump_label.h>
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/mutex.h>
40#include <linux/of_device.h>
41#include <linux/of.h>
42#include <linux/of_irq.h>
43#include <linux/pm_domain.h>
44#include <linux/pm_runtime.h>
45#include <linux/pm_wakeirq.h>
Wolfram Sange1dba012015-12-08 10:37:46 +010046#include <linux/property.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010047#include <linux/rwsem.h>
48#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
David Brownell9c1600e2007-05-01 23:26:31 +020050#include "i2c-core.h"
51
David Howellsd9a83d62014-03-06 13:35:59 +000052#define CREATE_TRACE_POINTS
53#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Wolfram Sangda899f52015-05-18 21:09:12 +020055#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
56#define I2C_ADDR_OFFSET_SLAVE 0x1000
57
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +020058#define I2C_ADDR_7BITS_MAX 0x77
59#define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1)
60
Wolfram Sang61e3d0f2017-05-23 19:23:11 +020061/*
62 * core_lock protects i2c_adapter_idr, and guarantees that device detection,
63 * deletion of detected devices, and attach_adapter calls are serialized
64 */
Jean Delvarecaada322008-01-27 18:14:49 +010065static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static DEFINE_IDR(i2c_adapter_idr);
67
Jean Delvare4735c982008-07-14 22:38:36 +020068static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010069
David Howellsd9a83d62014-03-06 13:35:59 +000070static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
Sudip Mukherjee95026652016-03-07 17:19:17 +053071static bool is_registered;
David Howellsd9a83d62014-03-06 13:35:59 +000072
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050073int i2c_transfer_trace_reg(void)
David Howellsd9a83d62014-03-06 13:35:59 +000074{
75 static_key_slow_inc(&i2c_trace_msg);
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050076 return 0;
David Howellsd9a83d62014-03-06 13:35:59 +000077}
78
79void i2c_transfer_trace_unreg(void)
80{
81 static_key_slow_dec(&i2c_trace_msg);
82}
83
Lee Jones5f441fc2016-11-07 12:47:40 +000084const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
Jean Delvared2653e92008-04-29 23:11:39 +020085 const struct i2c_client *client)
86{
Lee Jones811073b2016-11-07 12:47:36 +000087 if (!(id && client))
88 return NULL;
89
Jean Delvared2653e92008-04-29 23:11:39 +020090 while (id->name[0]) {
91 if (strcmp(client->name, id->name) == 0)
92 return id;
93 id++;
94 }
95 return NULL;
96}
Lee Jones5f441fc2016-11-07 12:47:40 +000097EXPORT_SYMBOL_GPL(i2c_match_id);
Jean Delvared2653e92008-04-29 23:11:39 +020098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int i2c_device_match(struct device *dev, struct device_driver *drv)
100{
Jean Delvare51298d12009-09-18 22:45:45 +0200101 struct i2c_client *client = i2c_verify_client(dev);
102 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200103
Jean Delvare51298d12009-09-18 22:45:45 +0200104
Grant Likely959e85f2010-06-08 07:48:19 -0600105 /* Attempt an OF style match */
Lee Jonesda10c062016-11-07 12:47:39 +0000106 if (i2c_of_match_device(drv->of_match_table, client))
Grant Likely959e85f2010-06-08 07:48:19 -0600107 return 1;
108
Mika Westerberg907ddf82012-11-23 12:23:40 +0100109 /* Then ACPI style match */
110 if (acpi_driver_match_device(dev, drv))
111 return 1;
112
Jean Delvare51298d12009-09-18 22:45:45 +0200113 driver = to_i2c_driver(drv);
Lee Jones811073b2016-11-07 12:47:36 +0000114
115 /* Finally an I2C match */
116 if (i2c_match_id(driver->id_table, client))
117 return 1;
Jean Delvared2653e92008-04-29 23:11:39 +0200118
Jean Delvareeb8a7902008-05-18 20:49:41 +0200119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Kay Sievers7eff2e72007-08-14 15:15:12 +0200122static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200123{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100124 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800125 int rc;
126
127 rc = acpi_device_uevent_modalias(dev, env);
128 if (rc != -ENODEV)
129 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200130
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100131 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200132}
133
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530134/* i2c bus recovery routines */
135static int get_scl_gpio_value(struct i2c_adapter *adap)
136{
137 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
138}
139
140static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
141{
142 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
143}
144
145static int get_sda_gpio_value(struct i2c_adapter *adap)
146{
147 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
148}
149
150static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
151{
152 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
153 struct device *dev = &adap->dev;
154 int ret = 0;
155
156 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
157 GPIOF_OUT_INIT_HIGH, "i2c-scl");
158 if (ret) {
159 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
160 return ret;
161 }
162
163 if (bri->get_sda) {
164 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
165 /* work without SDA polling */
166 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
167 bri->sda_gpio);
168 bri->get_sda = NULL;
169 }
170 }
171
172 return ret;
173}
174
175static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
176{
177 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
178
179 if (bri->get_sda)
180 gpio_free(bri->sda_gpio);
181
182 gpio_free(bri->scl_gpio);
183}
184
185/*
186 * We are generating clock pulses. ndelay() determines durating of clk pulses.
187 * We will generate clock with rate 100 KHz and so duration of both clock levels
188 * is: delay in ns = (10^6 / 100) / 2
189 */
190#define RECOVERY_NDELAY 5000
191#define RECOVERY_CLK_CNT 9
192
193static int i2c_generic_recovery(struct i2c_adapter *adap)
194{
195 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
196 int i = 0, val = 1, ret = 0;
197
198 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300199 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530200
Jan Luebbe8b062602015-07-08 16:35:06 +0200201 bri->set_scl(adap, val);
202 ndelay(RECOVERY_NDELAY);
203
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530204 /*
205 * By this time SCL is high, as we need to give 9 falling-rising edges
206 */
207 while (i++ < RECOVERY_CLK_CNT * 2) {
208 if (val) {
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530209 /* SCL shouldn't be low here */
210 if (!bri->get_scl(adap)) {
211 dev_err(&adap->dev,
212 "SCL is stuck low, exit recovery\n");
213 ret = -EBUSY;
214 break;
215 }
Claudio Foellmi1f35b862017-10-05 14:44:14 +0200216 /* Break if SDA is high */
217 if (bri->get_sda && bri->get_sda(adap))
218 break;
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530219 }
220
221 val = !val;
222 bri->set_scl(adap, val);
223 ndelay(RECOVERY_NDELAY);
224 }
225
Claudio Foellmi1f35b862017-10-05 14:44:14 +0200226 /* check if recovery actually succeeded */
227 if (bri->get_sda && !bri->get_sda(adap))
228 ret = -EBUSY;
229
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530230 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300231 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530232
233 return ret;
234}
235
236int i2c_generic_scl_recovery(struct i2c_adapter *adap)
237{
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530238 return i2c_generic_recovery(adap);
239}
Mark Brownc1c21f42015-04-15 19:18:39 +0100240EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530241
242int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
243{
244 int ret;
245
246 ret = i2c_get_gpios_for_recovery(adap);
247 if (ret)
248 return ret;
249
250 ret = i2c_generic_recovery(adap);
251 i2c_put_gpios_for_recovery(adap);
252
253 return ret;
254}
Mark Brownc1c21f42015-04-15 19:18:39 +0100255EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530256
257int i2c_recover_bus(struct i2c_adapter *adap)
258{
259 if (!adap->bus_recovery_info)
260 return -EOPNOTSUPP;
261
262 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
263 return adap->bus_recovery_info->recover_bus(adap);
264}
Mark Brownc1c21f42015-04-15 19:18:39 +0100265EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530266
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900267static void i2c_init_recovery(struct i2c_adapter *adap)
268{
269 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
270 char *err_str;
271
272 if (!bri)
273 return;
274
275 if (!bri->recover_bus) {
276 err_str = "no recover_bus() found";
277 goto err;
278 }
279
280 /* Generic GPIO recovery */
281 if (bri->recover_bus == i2c_generic_gpio_recovery) {
282 if (!gpio_is_valid(bri->scl_gpio)) {
283 err_str = "invalid SCL gpio";
284 goto err;
285 }
286
287 if (gpio_is_valid(bri->sda_gpio))
288 bri->get_sda = get_sda_gpio_value;
289 else
290 bri->get_sda = NULL;
291
292 bri->get_scl = get_scl_gpio_value;
293 bri->set_scl = set_scl_gpio_value;
294 } else if (bri->recover_bus == i2c_generic_scl_recovery) {
295 /* Generic SCL recovery */
296 if (!bri->set_scl || !bri->get_scl) {
297 err_str = "no {get|set}_scl() found";
298 goto err;
299 }
300 }
301
302 return;
303 err:
304 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
305 adap->bus_recovery_info = NULL;
306}
307
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200308static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
309{
310 struct i2c_adapter *adap = client->adapter;
311 unsigned int irq;
312
313 if (!adap->host_notify_domain)
314 return -ENXIO;
315
316 if (client->flags & I2C_CLIENT_TEN)
317 return -EINVAL;
318
319 irq = irq_find_mapping(adap->host_notify_domain, client->addr);
320 if (!irq)
321 irq = irq_create_mapping(adap->host_notify_domain,
322 client->addr);
323
324 return irq > 0 ? irq : -ENXIO;
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static int i2c_device_probe(struct device *dev)
328{
Jean Delvare51298d12009-09-18 22:45:45 +0200329 struct i2c_client *client = i2c_verify_client(dev);
330 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100331 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200332
Jean Delvare51298d12009-09-18 22:45:45 +0200333 if (!client)
334 return 0;
335
Hans de Goeded1d84bb2017-04-05 00:03:34 +0200336 driver = to_i2c_driver(dev->driver);
337
338 if (!client->irq && !driver->disable_i2c_core_irq_mapping) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300339 int irq = -ENOENT;
340
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800341 if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
342 dev_dbg(dev, "Using Host Notify IRQ\n");
343 irq = i2c_smbus_host_notify_to_irq(client);
344 } else if (dev->of_node) {
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700345 irq = of_irq_get_byname(dev->of_node, "irq");
346 if (irq == -EINVAL || irq == -ENODATA)
347 irq = of_irq_get(dev->of_node, 0);
348 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300349 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700350 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100351 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200352 return irq;
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800353
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100354 if (irq < 0)
355 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200356
357 client->irq = irq;
358 }
359
Lee Jonesda10c062016-11-07 12:47:39 +0000360 /*
Javier Martinez Canillasf4b17a12017-08-09 11:21:28 +0200361 * An I2C ID table is not mandatory, if and only if, a suitable OF
362 * or ACPI ID table is supplied for the probing device.
Lee Jonesda10c062016-11-07 12:47:39 +0000363 */
364 if (!driver->id_table &&
Andy Shevchenkoc64ffff2017-07-17 17:13:28 +0300365 !i2c_acpi_match_device(dev->driver->acpi_match_table, client) &&
Lee Jonesda10c062016-11-07 12:47:39 +0000366 !i2c_of_match_device(dev->driver->of_match_table, client))
David Brownell7b4fbc52007-05-01 23:26:30 +0200367 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200368
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700369 if (client->flags & I2C_CLIENT_WAKE) {
370 int wakeirq = -ENOENT;
371
372 if (dev->of_node) {
373 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
374 if (wakeirq == -EPROBE_DEFER)
375 return wakeirq;
376 }
377
378 device_init_wakeup(&client->dev, true);
379
380 if (wakeirq > 0 && wakeirq != client->irq)
381 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
382 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200383 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700384 else
385 status = 0;
386
387 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300388 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700389 }
390
David Brownell7b4fbc52007-05-01 23:26:30 +0200391 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200392
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200393 status = of_clk_set_defaults(dev->of_node, false);
394 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700395 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200396
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200397 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +0100398 if (status == -EPROBE_DEFER)
399 goto err_clear_wakeup_irq;
400
Lee Jonesb8a1a4c2016-11-07 12:47:41 +0000401 /*
402 * When there are no more users of probe(),
403 * rename probe_new to probe.
404 */
405 if (driver->probe_new)
406 status = driver->probe_new(client);
407 else if (driver->probe)
408 status = driver->probe(client,
409 i2c_match_id(driver->id_table, client));
410 else
411 status = -EINVAL;
412
Kieran Bingham74cedd32015-10-12 21:54:43 +0100413 if (status)
414 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100415
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700416 return 0;
417
418err_detach_pm_domain:
419 dev_pm_domain_detach(&client->dev, true);
420err_clear_wakeup_irq:
421 dev_pm_clear_wake_irq(&client->dev);
422 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +0100423 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426static int i2c_device_remove(struct device *dev)
427{
Jean Delvare51298d12009-09-18 22:45:45 +0200428 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200429 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100430 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200431
Jean Delvare51298d12009-09-18 22:45:45 +0200432 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200433 return 0;
434
435 driver = to_i2c_driver(dev->driver);
436 if (driver->remove) {
437 dev_dbg(dev, "remove\n");
438 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200439 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100440
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200441 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700442
443 dev_pm_clear_wake_irq(&client->dev);
444 device_init_wakeup(&client->dev, false);
445
David Brownella1d9e6e2007-05-01 23:26:30 +0200446 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
David Brownellf37dd802007-02-13 22:09:00 +0100449static void i2c_device_shutdown(struct device *dev)
450{
Jean Delvare51298d12009-09-18 22:45:45 +0200451 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100452 struct i2c_driver *driver;
453
Jean Delvare51298d12009-09-18 22:45:45 +0200454 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100455 return;
456 driver = to_i2c_driver(dev->driver);
457 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200458 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100459}
460
David Brownell9c1600e2007-05-01 23:26:31 +0200461static void i2c_client_dev_release(struct device *dev)
462{
463 kfree(to_i2c_client(dev));
464}
465
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100466static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200467show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200468{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200469 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
470 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200471}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100472static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200473
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100474static ssize_t
475show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200476{
477 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800478 int len;
479
480 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
481 if (len != -ENODEV)
482 return len;
483
Jean Delvareeb8a7902008-05-18 20:49:41 +0200484 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200485}
Jean Delvare51298d12009-09-18 22:45:45 +0200486static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
487
488static struct attribute *i2c_dev_attrs[] = {
489 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200490 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200491 &dev_attr_modalias.attr,
492 NULL
493};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100494ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100495
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200496struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100497 .name = "i2c",
498 .match = i2c_device_match,
499 .probe = i2c_device_probe,
500 .remove = i2c_device_remove,
501 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000502};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200503EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000504
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800505struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100506 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200507 .uevent = i2c_device_uevent,
508 .release = i2c_client_dev_release,
509};
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800510EXPORT_SYMBOL_GPL(i2c_client_type);
Jean Delvare51298d12009-09-18 22:45:45 +0200511
David Brownell9b766b82008-01-27 18:14:51 +0100512
513/**
514 * i2c_verify_client - return parameter as i2c_client, or NULL
515 * @dev: device, probably from some driver model iterator
516 *
517 * When traversing the driver model tree, perhaps using driver model
518 * iterators like @device_for_each_child(), you can't assume very much
519 * about the nodes you find. Use this function to avoid oopses caused
520 * by wrongly treating some non-I2C device as an i2c_client.
521 */
522struct i2c_client *i2c_verify_client(struct device *dev)
523{
Jean Delvare51298d12009-09-18 22:45:45 +0200524 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100525 ? to_i2c_client(dev)
526 : NULL;
527}
528EXPORT_SYMBOL(i2c_verify_client);
529
530
Wolfram Sangda899f52015-05-18 21:09:12 +0200531/* Return a unique address which takes the flags of the client into account */
532static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
533{
534 unsigned short addr = client->addr;
535
536 /* For some client flags, add an arbitrary offset to avoid collisions */
537 if (client->flags & I2C_CLIENT_TEN)
538 addr |= I2C_ADDR_OFFSET_TEN_BIT;
539
540 if (client->flags & I2C_CLIENT_SLAVE)
541 addr |= I2C_ADDR_OFFSET_SLAVE;
542
543 return addr;
544}
545
Jean Delvare3a89db52010-06-03 11:33:52 +0200546/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300547 * are purposely not enforced, except for the general call address. */
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200548int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +0200549{
Wolfram Sangc4019b72015-07-17 12:50:06 +0200550 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +0200551 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200552 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +0200553 return -EINVAL;
554 } else {
555 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200556 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +0200557 return -EINVAL;
558 }
559 return 0;
560}
561
Jean Delvare656b8762010-06-03 11:33:53 +0200562/* And this is a strict address validity check, used when probing. If a
563 * device uses a reserved address, then it shouldn't be probed. 7-bit
564 * addressing is assumed, 10-bit address devices are rare and should be
565 * explicitly enumerated. */
Wolfram Sange4991ec2017-05-23 11:14:17 +0200566int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +0200567{
568 /*
569 * Reserved addresses per I2C specification:
570 * 0x00 General call address / START byte
571 * 0x01 CBUS address
572 * 0x02 Reserved for different bus format
573 * 0x03 Reserved for future purposes
574 * 0x04-0x07 Hs-mode master code
575 * 0x78-0x7b 10-bit slave addressing
576 * 0x7c-0x7f Reserved for future purposes
577 */
578 if (addr < 0x08 || addr > 0x77)
579 return -EINVAL;
580 return 0;
581}
582
Jean Delvare3b5f7942010-06-03 11:33:55 +0200583static int __i2c_check_addr_busy(struct device *dev, void *addrp)
584{
585 struct i2c_client *client = i2c_verify_client(dev);
586 int addr = *(int *)addrp;
587
Wolfram Sang9bccc702015-07-17 14:48:56 +0200588 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +0200589 return -EBUSY;
590 return 0;
591}
592
Michael Lawnick08263742010-08-11 18:21:02 +0200593/* walk up mux tree */
594static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
595{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200596 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200597 int result;
598
599 result = device_for_each_child(&adapter->dev, &addr,
600 __i2c_check_addr_busy);
601
Jean Delvare97cc4d42010-10-24 18:16:57 +0200602 if (!result && parent)
603 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200604
605 return result;
606}
607
608/* recurse down mux tree */
609static int i2c_check_mux_children(struct device *dev, void *addrp)
610{
611 int result;
612
613 if (dev->type == &i2c_adapter_type)
614 result = device_for_each_child(dev, addrp,
615 i2c_check_mux_children);
616 else
617 result = __i2c_check_addr_busy(dev, addrp);
618
619 return result;
620}
621
Jean Delvare3b5f7942010-06-03 11:33:55 +0200622static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
623{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200624 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200625 int result = 0;
626
Jean Delvare97cc4d42010-10-24 18:16:57 +0200627 if (parent)
628 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200629
630 if (!result)
631 result = device_for_each_child(&adapter->dev, &addr,
632 i2c_check_mux_children);
633
634 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200635}
636
David Brownell9c1600e2007-05-01 23:26:31 +0200637/**
Peter Rosin8320f492016-05-04 22:15:27 +0200638 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200639 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200640 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
641 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200642 */
Peter Rosin8320f492016-05-04 22:15:27 +0200643static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
644 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200645{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200646 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200647}
Jean Delvarefe61e072010-08-11 18:20:58 +0200648
649/**
Peter Rosin8320f492016-05-04 22:15:27 +0200650 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200651 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200652 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
653 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200654 */
Peter Rosin8320f492016-05-04 22:15:27 +0200655static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
656 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200657{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200658 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200659}
660
661/**
Peter Rosin8320f492016-05-04 22:15:27 +0200662 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200663 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200664 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
665 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200666 */
Peter Rosin8320f492016-05-04 22:15:27 +0200667static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
668 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200669{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200670 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200671}
Jean Delvarefe61e072010-08-11 18:20:58 +0200672
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200673static void i2c_dev_set_name(struct i2c_adapter *adap,
Hans de Goede728fe6c2017-10-11 11:41:19 +0200674 struct i2c_client *client,
675 struct i2c_board_info const *info)
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200676{
677 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
678
Hans de Goede728fe6c2017-10-11 11:41:19 +0200679 if (info && info->dev_name) {
680 dev_set_name(&client->dev, "i2c-%s", info->dev_name);
681 return;
682 }
683
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200684 if (adev) {
685 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
686 return;
687 }
688
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200689 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +0200690 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200691}
692
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800693static int i2c_dev_irq_from_resources(const struct resource *resources,
694 unsigned int num_resources)
695{
696 struct irq_data *irqd;
697 int i;
698
699 for (i = 0; i < num_resources; i++) {
700 const struct resource *r = &resources[i];
701
702 if (resource_type(r) != IORESOURCE_IRQ)
703 continue;
704
705 if (r->flags & IORESOURCE_BITS) {
706 irqd = irq_get_irq_data(r->start);
707 if (!irqd)
708 break;
709
710 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
711 }
712
713 return r->start;
714 }
715
716 return 0;
717}
718
Jean Delvarefe61e072010-08-11 18:20:58 +0200719/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200720 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200721 * @adap: the adapter managing the device
722 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200723 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200724 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200725 * Create an i2c device. Binding is handled through driver model
726 * probe()/remove() methods. A driver may be bound to this device when we
727 * return from this function, or any later moment (e.g. maybe hotplugging will
728 * load the driver module). This call is not appropriate for use by mainboard
729 * initialization logic, which usually runs during an arch_initcall() long
730 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200731 *
732 * This returns the new i2c client, which may be saved for later use with
733 * i2c_unregister_device(); or NULL to indicate an error.
734 */
735struct i2c_client *
736i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
737{
738 struct i2c_client *client;
739 int status;
740
741 client = kzalloc(sizeof *client, GFP_KERNEL);
742 if (!client)
743 return NULL;
744
745 client->adapter = adap;
746
747 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200748
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200749 if (info->archdata)
750 client->dev.archdata = *info->archdata;
751
Marc Pignatee354252008-08-28 08:33:22 +0200752 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200753 client->addr = info->addr;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800754
David Brownell9c1600e2007-05-01 23:26:31 +0200755 client->irq = info->irq;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800756 if (!client->irq)
757 client->irq = i2c_dev_irq_from_resources(info->resources,
758 info->num_resources);
David Brownell9c1600e2007-05-01 23:26:31 +0200759
David Brownell9c1600e2007-05-01 23:26:31 +0200760 strlcpy(client->name, info->type, sizeof(client->name));
761
Wolfram Sangc4019b72015-07-17 12:50:06 +0200762 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +0200763 if (status) {
764 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
765 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
766 goto out_err_silent;
767 }
768
Jean Delvaref8a227e2009-06-19 16:58:18 +0200769 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +0200770 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200771 if (status)
772 goto out_err;
773
774 client->dev.parent = &client->adapter->dev;
775 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200776 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700777 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100778 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200779
Hans de Goede728fe6c2017-10-11 11:41:19 +0200780 i2c_dev_set_name(adap, client, info);
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800781
782 if (info->properties) {
783 status = device_add_properties(&client->dev, info->properties);
784 if (status) {
785 dev_err(&adap->dev,
786 "Failed to add properties to client %s: %d\n",
787 client->name, status);
788 goto out_err;
789 }
790 }
791
Jean Delvaref8a227e2009-06-19 16:58:18 +0200792 status = device_register(&client->dev);
793 if (status)
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800794 goto out_free_props;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200795
Jean Delvaref8a227e2009-06-19 16:58:18 +0200796 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
797 client->name, dev_name(&client->dev));
798
David Brownell9c1600e2007-05-01 23:26:31 +0200799 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200800
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800801out_free_props:
802 if (info->properties)
803 device_remove_properties(&client->dev);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200804out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300805 dev_err(&adap->dev,
806 "Failed to register i2c client %s at 0x%02x (%d)\n",
807 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200808out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200809 kfree(client);
810 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200811}
812EXPORT_SYMBOL_GPL(i2c_new_device);
813
814
815/**
816 * i2c_unregister_device - reverse effect of i2c_new_device()
817 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200818 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200819 */
820void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200821{
Andy Shevchenko7b43dd12017-10-31 16:21:35 +0200822 if (!client)
823 return;
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +0200824 if (client->dev.of_node)
825 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300826 if (ACPI_COMPANION(&client->dev))
827 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +0200828 device_unregister(&client->dev);
829}
David Brownell9c1600e2007-05-01 23:26:31 +0200830EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200831
832
Jean Delvare60b129d2008-05-11 20:37:06 +0200833static const struct i2c_device_id dummy_id[] = {
834 { "dummy", 0 },
835 { },
836};
837
Jean Delvared2653e92008-04-29 23:11:39 +0200838static int dummy_probe(struct i2c_client *client,
839 const struct i2c_device_id *id)
840{
841 return 0;
842}
843
844static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100845{
846 return 0;
847}
848
849static struct i2c_driver dummy_driver = {
850 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200851 .probe = dummy_probe,
852 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200853 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100854};
855
856/**
857 * i2c_new_dummy - return a new i2c device bound to a dummy driver
858 * @adapter: the adapter managing the device
859 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100860 * Context: can sleep
861 *
862 * This returns an I2C client bound to the "dummy" driver, intended for use
863 * with devices that consume multiple addresses. Examples of such chips
864 * include various EEPROMS (like 24c04 and 24c08 models).
865 *
866 * These dummy devices have two main uses. First, most I2C and SMBus calls
867 * except i2c_transfer() need a client handle; the dummy will be that handle.
868 * And second, this prevents the specified address from being bound to a
869 * different driver.
870 *
871 * This returns the new i2c client, which should be saved for later use with
872 * i2c_unregister_device(); or NULL to indicate an error.
873 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100874struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100875{
876 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200877 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100878 };
879
David Brownelle9f13732008-01-27 18:14:52 +0100880 return i2c_new_device(adapter, &info);
881}
882EXPORT_SYMBOL_GPL(i2c_new_dummy);
883
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +0100884/**
885 * i2c_new_secondary_device - Helper to get the instantiated secondary address
886 * and create the associated device
887 * @client: Handle to the primary client
888 * @name: Handle to specify which secondary address to get
889 * @default_addr: Used as a fallback if no secondary address was specified
890 * Context: can sleep
891 *
892 * I2C clients can be composed of multiple I2C slaves bound together in a single
893 * component. The I2C client driver then binds to the master I2C slave and needs
894 * to create I2C dummy clients to communicate with all the other slaves.
895 *
896 * This function creates and returns an I2C dummy client whose I2C address is
897 * retrieved from the platform firmware based on the given slave name. If no
898 * address is specified by the firmware default_addr is used.
899 *
900 * On DT-based platforms the address is retrieved from the "reg" property entry
901 * cell whose "reg-names" value matches the slave name.
902 *
903 * This returns the new i2c client, which should be saved for later use with
904 * i2c_unregister_device(); or NULL to indicate an error.
905 */
906struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
907 const char *name,
908 u16 default_addr)
909{
910 struct device_node *np = client->dev.of_node;
911 u32 addr = default_addr;
912 int i;
913
914 if (np) {
915 i = of_property_match_string(np, "reg-names", name);
916 if (i >= 0)
917 of_property_read_u32_index(np, "reg", i, &addr);
918 }
919
920 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
921 return i2c_new_dummy(client->adapter, addr);
922}
923EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
924
David Brownellf37dd802007-02-13 22:09:00 +0100925/* ------------------------------------------------------------------------- */
926
David Brownell16ffadf2007-05-01 23:26:28 +0200927/* I2C bus adapters -- one roots each I2C or SMBUS segment */
928
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200929static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
David Brownellef2c83212007-05-01 23:26:28 +0200931 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 complete(&adap->dev_released);
933}
934
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +0200935unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +0200936{
937 unsigned int depth = 0;
938
939 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
940 depth++;
941
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +0200942 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
943 "adapter depth exceeds lockdep subclass limit\n");
944
Jean Delvare390946b2012-09-10 10:14:02 +0200945 return depth;
946}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +0200947EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +0200948
949/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200950 * Let users instantiate I2C devices through sysfs. This can be used when
951 * platform initialization code doesn't contain the proper data for
952 * whatever reason. Also useful for drivers that do device detection and
953 * detection fails, either because the device uses an unexpected address,
954 * or this is a compatible device with different ID register values.
955 *
956 * Parameter checking may look overzealous, but we really don't want
957 * the user to provide incorrect parameters.
958 */
959static ssize_t
960i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
961 const char *buf, size_t count)
962{
963 struct i2c_adapter *adap = to_i2c_adapter(dev);
964 struct i2c_board_info info;
965 struct i2c_client *client;
966 char *blank, end;
967 int res;
968
Jean Delvare99cd8e22009-06-19 16:58:20 +0200969 memset(&info, 0, sizeof(struct i2c_board_info));
970
971 blank = strchr(buf, ' ');
972 if (!blank) {
973 dev_err(dev, "%s: Missing parameters\n", "new_device");
974 return -EINVAL;
975 }
976 if (blank - buf > I2C_NAME_SIZE - 1) {
977 dev_err(dev, "%s: Invalid device name\n", "new_device");
978 return -EINVAL;
979 }
980 memcpy(info.type, buf, blank - buf);
981
982 /* Parse remaining parameters, reject extra parameters */
983 res = sscanf(++blank, "%hi%c", &info.addr, &end);
984 if (res < 1) {
985 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
986 return -EINVAL;
987 }
988 if (res > 1 && end != '\n') {
989 dev_err(dev, "%s: Extra parameters\n", "new_device");
990 return -EINVAL;
991 }
992
Wolfram Sangcfa03272015-07-27 14:03:38 +0200993 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
994 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
995 info.flags |= I2C_CLIENT_TEN;
996 }
997
998 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
999 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1000 info.flags |= I2C_CLIENT_SLAVE;
1001 }
1002
Jean Delvare99cd8e22009-06-19 16:58:20 +02001003 client = i2c_new_device(adap, &info);
1004 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +02001005 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +02001006
1007 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001008 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001009 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001010 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001011 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1012 info.type, info.addr);
1013
1014 return count;
1015}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001016static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001017
1018/*
1019 * And of course let the users delete the devices they instantiated, if
1020 * they got it wrong. This interface can only be used to delete devices
1021 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1022 * don't delete devices to which some kernel code still has references.
1023 *
1024 * Parameter checking may look overzealous, but we really don't want
1025 * the user to delete the wrong device.
1026 */
1027static ssize_t
1028i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1029 const char *buf, size_t count)
1030{
1031 struct i2c_adapter *adap = to_i2c_adapter(dev);
1032 struct i2c_client *client, *next;
1033 unsigned short addr;
1034 char end;
1035 int res;
1036
1037 /* Parse parameters, reject extra parameters */
1038 res = sscanf(buf, "%hi%c", &addr, &end);
1039 if (res < 1) {
1040 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1041 return -EINVAL;
1042 }
1043 if (res > 1 && end != '\n') {
1044 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1045 return -EINVAL;
1046 }
1047
1048 /* Make sure the device was added through sysfs */
1049 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001050 mutex_lock_nested(&adap->userspace_clients_lock,
1051 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001052 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1053 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001054 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001055 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1056 "delete_device", client->name, client->addr);
1057
1058 list_del(&client->detected);
1059 i2c_unregister_device(client);
1060 res = count;
1061 break;
1062 }
1063 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001064 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001065
1066 if (res < 0)
1067 dev_err(dev, "%s: Can't find device in list\n",
1068 "delete_device");
1069 return res;
1070}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001071static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1072 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001073
1074static struct attribute *i2c_adapter_attrs[] = {
1075 &dev_attr_name.attr,
1076 &dev_attr_new_device.attr,
1077 &dev_attr_delete_device.attr,
1078 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001079};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001080ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001081
Michael Lawnick08263742010-08-11 18:21:02 +02001082struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001083 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001084 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001085};
Michael Lawnick08263742010-08-11 18:21:02 +02001086EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Stephen Warren643dd092012-04-17 12:43:33 -06001088/**
1089 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1090 * @dev: device, probably from some driver model iterator
1091 *
1092 * When traversing the driver model tree, perhaps using driver model
1093 * iterators like @device_for_each_child(), you can't assume very much
1094 * about the nodes you find. Use this function to avoid oopses caused
1095 * by wrongly treating some non-I2C device as an i2c_adapter.
1096 */
1097struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1098{
1099 return (dev->type == &i2c_adapter_type)
1100 ? to_i2c_adapter(dev)
1101 : NULL;
1102}
1103EXPORT_SYMBOL(i2c_verify_adapter);
1104
Jean Delvare2bb50952009-09-18 22:45:46 +02001105#ifdef CONFIG_I2C_COMPAT
1106static struct class_compat *i2c_adapter_compat_class;
1107#endif
1108
David Brownell9c1600e2007-05-01 23:26:31 +02001109static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1110{
1111 struct i2c_devinfo *devinfo;
1112
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001113 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001114 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1115 if (devinfo->busnum == adapter->nr
1116 && !i2c_new_device(adapter,
1117 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001118 dev_err(&adapter->dev,
1119 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001120 devinfo->board_info.addr);
1121 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001122 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001123}
1124
Jean Delvare69b00892009-12-06 17:06:27 +01001125static int i2c_do_add_adapter(struct i2c_driver *driver,
1126 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001127{
Jean Delvare4735c982008-07-14 22:38:36 +02001128 /* Detect supported devices on that bus, and instantiate them */
1129 i2c_detect(adap, driver);
1130
1131 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001132 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001133 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1134 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001135 dev_warn(&adap->dev,
1136 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001137 /* We ignore the return code; if it fails, too bad */
1138 driver->attach_adapter(adap);
1139 }
1140 return 0;
1141}
1142
Jean Delvare69b00892009-12-06 17:06:27 +01001143static int __process_new_adapter(struct device_driver *d, void *data)
1144{
1145 return i2c_do_add_adapter(to_i2c_driver(d), data);
1146}
1147
Peter Rosind1ed7982016-08-25 23:07:01 +02001148static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1149 .lock_bus = i2c_adapter_lock_bus,
1150 .trylock_bus = i2c_adapter_trylock_bus,
1151 .unlock_bus = i2c_adapter_unlock_bus,
1152};
1153
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001154static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap)
1155{
1156 struct irq_domain *domain = adap->host_notify_domain;
1157 irq_hw_number_t hwirq;
1158
1159 if (!domain)
1160 return;
1161
1162 for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++)
1163 irq_dispose_mapping(irq_find_mapping(domain, hwirq));
1164
1165 irq_domain_remove(domain);
1166 adap->host_notify_domain = NULL;
1167}
1168
1169static int i2c_host_notify_irq_map(struct irq_domain *h,
1170 unsigned int virq,
1171 irq_hw_number_t hw_irq_num)
1172{
1173 irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
1174
1175 return 0;
1176}
1177
1178static const struct irq_domain_ops i2c_host_notify_irq_ops = {
1179 .map = i2c_host_notify_irq_map,
1180};
1181
1182static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap)
1183{
1184 struct irq_domain *domain;
1185
1186 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
1187 return 0;
1188
1189 domain = irq_domain_create_linear(adap->dev.fwnode,
1190 I2C_ADDR_7BITS_COUNT,
1191 &i2c_host_notify_irq_ops, adap);
1192 if (!domain)
1193 return -ENOMEM;
1194
1195 adap->host_notify_domain = domain;
1196
1197 return 0;
1198}
1199
1200/**
1201 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
1202 * I2C client.
1203 * @adap: the adapter
1204 * @addr: the I2C address of the notifying device
1205 * Context: can't sleep
1206 *
1207 * Helper function to be called from an I2C bus driver's interrupt
1208 * handler. It will schedule the Host Notify IRQ.
1209 */
1210int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
1211{
1212 int irq;
1213
1214 if (!adap)
1215 return -EINVAL;
1216
1217 irq = irq_find_mapping(adap->host_notify_domain, addr);
1218 if (irq <= 0)
1219 return -ENXIO;
1220
1221 generic_handle_irq(irq);
1222
1223 return 0;
1224}
1225EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
1226
David Brownell6e13e642007-05-01 23:26:31 +02001227static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001229 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
David Brownell1d0b19c2008-10-14 17:30:05 +02001231 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301232 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001233 res = -EAGAIN;
1234 goto out_list;
1235 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001236
Jean Delvare2236baa2010-11-15 22:40:38 +01001237 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001238 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001239 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001240
1241 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001242 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001243 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01001244 }
1245
Peter Rosind1ed7982016-08-25 23:07:01 +02001246 if (!adap->lock_ops)
1247 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001248
Mika Kuoppala194684e2009-12-06 17:06:22 +01001249 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001250 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001251 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001252 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Jean Delvare8fcfef62009-03-28 21:34:43 +01001254 /* Set default timeout to 1 second if not already set */
1255 if (adap->timeout == 0)
1256 adap->timeout = HZ;
1257
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001258 /* register soft irqs for Host Notify */
1259 res = i2c_setup_host_notify_irq_domain(adap);
1260 if (res) {
1261 pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n",
1262 adap->name, res);
1263 goto out_list;
1264 }
1265
Kay Sievers27d9c182009-01-07 14:29:16 +01001266 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001267 adap->dev.bus = &i2c_bus_type;
1268 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001269 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001270 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001271 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001272 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Phil Reidf8756c62017-08-24 17:31:04 +08001275 res = of_i2c_setup_smbus_alert(adap);
1276 if (res)
1277 goto out_reg;
1278
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001279 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1280
Charles Keepax6ada5c12015-04-16 13:05:19 +01001281 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001282 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001283 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001284
Jean Delvare2bb50952009-09-18 22:45:46 +02001285#ifdef CONFIG_I2C_COMPAT
1286 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1287 adap->dev.parent);
1288 if (res)
1289 dev_warn(&adap->dev,
1290 "Failed to create compatibility class link\n");
1291#endif
1292
Wolfram Sangd3b11d82016-07-09 13:34:59 +09001293 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301294
Jean Delvare729d6dd2009-06-19 16:58:18 +02001295 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001296 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001297 i2c_acpi_register_devices(adap);
1298 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001299
David Brownell6e13e642007-05-01 23:26:31 +02001300 if (adap->nr < __i2c_first_dynamic_bus_num)
1301 i2c_scan_static_board_info(adap);
1302
Jean Delvare4735c982008-07-14 22:38:36 +02001303 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001304 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001305 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001306 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001307
1308 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001309
Phil Reidf8756c62017-08-24 17:31:04 +08001310out_reg:
1311 init_completion(&adap->dev_released);
1312 device_unregister(&adap->dev);
1313 wait_for_completion(&adap->dev_released);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001314out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001315 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001316 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001317 mutex_unlock(&core_lock);
1318 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319}
1320
David Brownell6e13e642007-05-01 23:26:31 +02001321/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001322 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1323 * @adap: the adapter to register (with adap->nr initialized)
1324 * Context: can sleep
1325 *
1326 * See i2c_add_numbered_adapter() for details.
1327 */
1328static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1329{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001330 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001331
1332 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001333 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001334 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001335 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001336 return id == -ENOSPC ? -EBUSY : id;
1337
1338 return i2c_register_adapter(adap);
1339}
1340
1341/**
David Brownell6e13e642007-05-01 23:26:31 +02001342 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1343 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001344 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001345 *
1346 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001347 * doesn't matter or when its bus number is specified by an dt alias.
1348 * Examples of bases when the bus number doesn't matter: I2C adapters
1349 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001350 *
1351 * When this returns zero, a new bus number was allocated and stored
1352 * in adap->nr, and the specified adapter became available for clients.
1353 * Otherwise, a negative errno value is returned.
1354 */
1355int i2c_add_adapter(struct i2c_adapter *adapter)
1356{
Doug Andersonee5c2742013-03-01 08:57:31 -08001357 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001358 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001359
Doug Andersonee5c2742013-03-01 08:57:31 -08001360 if (dev->of_node) {
1361 id = of_alias_get_id(dev->of_node, "i2c");
1362 if (id >= 0) {
1363 adapter->nr = id;
1364 return __i2c_add_numbered_adapter(adapter);
1365 }
1366 }
1367
Jean Delvarecaada322008-01-27 18:14:49 +01001368 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001369 id = idr_alloc(&i2c_adapter_idr, adapter,
1370 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001371 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001372 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08001373 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001374
1375 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001376
David Brownell6e13e642007-05-01 23:26:31 +02001377 return i2c_register_adapter(adapter);
1378}
1379EXPORT_SYMBOL(i2c_add_adapter);
1380
1381/**
1382 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1383 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001384 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001385 *
1386 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001387 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1388 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001389 * is used to properly configure I2C devices.
1390 *
Grant Likely488bf312011-07-25 17:49:43 +02001391 * If the requested bus number is set to -1, then this function will behave
1392 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1393 *
David Brownell6e13e642007-05-01 23:26:31 +02001394 * If no devices have pre-been declared for this bus, then be sure to
1395 * register the adapter before any dynamically allocated ones. Otherwise
1396 * the required bus ID may not be available.
1397 *
1398 * When this returns zero, the specified adapter became available for
1399 * clients using the bus number provided in adap->nr. Also, the table
1400 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1401 * and the appropriate driver model device nodes are created. Otherwise, a
1402 * negative errno value is returned.
1403 */
1404int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1405{
Grant Likely488bf312011-07-25 17:49:43 +02001406 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1407 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001408
Doug Andersonee5c2742013-03-01 08:57:31 -08001409 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001410}
1411EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1412
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001413static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001414 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001415{
Jean Delvare4735c982008-07-14 22:38:36 +02001416 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001417
Jean Delvareacec2112009-03-28 21:34:40 +01001418 /* Remove the devices we created ourselves as the result of hardware
1419 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001420 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1421 if (client->adapter == adapter) {
1422 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1423 client->name, client->addr);
1424 list_del(&client->detected);
1425 i2c_unregister_device(client);
1426 }
1427 }
Jean Delvare026526f2008-01-27 18:14:49 +01001428}
1429
Jean Delvaree549c2b2009-06-19 16:58:19 +02001430static int __unregister_client(struct device *dev, void *dummy)
1431{
1432 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001433 if (client && strcmp(client->name, "dummy"))
1434 i2c_unregister_device(client);
1435 return 0;
1436}
1437
1438static int __unregister_dummy(struct device *dev, void *dummy)
1439{
1440 struct i2c_client *client = i2c_verify_client(dev);
Andy Shevchenko7b43dd12017-10-31 16:21:35 +02001441 i2c_unregister_device(client);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001442 return 0;
1443}
1444
Jean Delvare69b00892009-12-06 17:06:27 +01001445static int __process_removed_adapter(struct device_driver *d, void *data)
1446{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001447 i2c_do_del_adapter(to_i2c_driver(d), data);
1448 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001449}
1450
David Brownelld64f73b2007-07-12 14:12:28 +02001451/**
1452 * i2c_del_adapter - unregister I2C adapter
1453 * @adap: the adapter being unregistered
1454 * Context: can sleep
1455 *
1456 * This unregisters an I2C adapter which was previously registered
1457 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1458 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001459void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001461 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001462 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001465 mutex_lock(&core_lock);
1466 found = idr_find(&i2c_adapter_idr, adap->nr);
1467 mutex_unlock(&core_lock);
1468 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001469 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001470 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001473 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001474 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001475 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001476 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001477 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001478 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001480 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001481 mutex_lock_nested(&adap->userspace_clients_lock,
1482 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001483 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1484 detected) {
1485 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1486 client->addr);
1487 list_del(&client->detected);
1488 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001489 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001490 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001491
Jean Delvaree549c2b2009-06-19 16:58:19 +02001492 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001493 * check the returned value. This is a two-pass process, because
1494 * we can't remove the dummy devices during the first pass: they
1495 * could have been instantiated by real devices wishing to clean
1496 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001497 device_for_each_child(&adap->dev, NULL, __unregister_client);
1498 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Jean Delvare2bb50952009-09-18 22:45:46 +02001500#ifdef CONFIG_I2C_COMPAT
1501 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1502 adap->dev.parent);
1503#endif
1504
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001505 /* device name is gone after device_unregister */
1506 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1507
Wolfram Sang9f924162015-12-23 18:19:28 +01001508 pm_runtime_disable(&adap->dev);
1509
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001510 i2c_host_notify_irq_teardown(adap);
1511
Wolfram Sang26680ee2015-01-29 22:45:09 +01001512 /* wait until all references to the device are gone
1513 *
1514 * FIXME: This is old code and should ideally be replaced by an
1515 * alternative which results in decoupling the lifetime of the struct
1516 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301517 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001518 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
David Brownell6e13e642007-05-01 23:26:31 +02001523 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001524 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001526 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001528 /* Clear the device structure in case this adapter is ever going to be
1529 added again */
1530 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
David Brownellc0564602007-05-01 23:26:31 +02001532EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Wolfram Sang54177cc2015-12-17 13:32:36 +01001534/**
1535 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
1536 * @dev: The device to scan for I2C timing properties
1537 * @t: the i2c_timings struct to be filled with values
1538 * @use_defaults: bool to use sane defaults derived from the I2C specification
1539 * when properties are not found, otherwise use 0
1540 *
1541 * Scan the device for the generic I2C properties describing timing parameters
1542 * for the signal and fill the given struct with the results. If a property was
1543 * not found and use_defaults was true, then maximum timings are assumed which
1544 * are derived from the I2C specification. If use_defaults is not used, the
1545 * results will be 0, so drivers can apply their own defaults later. The latter
1546 * is mainly intended for avoiding regressions of existing drivers which want
1547 * to switch to this function. New drivers almost always should use the defaults.
1548 */
1549
1550void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
1551{
1552 int ret;
1553
1554 memset(t, 0, sizeof(*t));
1555
1556 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
1557 if (ret && use_defaults)
1558 t->bus_freq_hz = 100000;
1559
1560 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
1561 if (ret && use_defaults) {
1562 if (t->bus_freq_hz <= 100000)
1563 t->scl_rise_ns = 1000;
1564 else if (t->bus_freq_hz <= 400000)
1565 t->scl_rise_ns = 300;
1566 else
1567 t->scl_rise_ns = 120;
1568 }
1569
1570 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
1571 if (ret && use_defaults) {
1572 if (t->bus_freq_hz <= 400000)
1573 t->scl_fall_ns = 300;
1574 else
1575 t->scl_fall_ns = 120;
1576 }
1577
1578 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
1579
1580 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
1581 if (ret && use_defaults)
1582 t->sda_fall_ns = t->scl_fall_ns;
1583}
1584EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
1585
David Brownell7b4fbc52007-05-01 23:26:30 +02001586/* ------------------------------------------------------------------------- */
1587
Jean Delvare7ae31482011-03-20 14:50:52 +01001588int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1589{
1590 int res;
1591
1592 mutex_lock(&core_lock);
1593 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1594 mutex_unlock(&core_lock);
1595
1596 return res;
1597}
1598EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1599
Jean Delvare69b00892009-12-06 17:06:27 +01001600static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001601{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001602 if (dev->type != &i2c_adapter_type)
1603 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001604 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001605}
1606
David Brownell7b4fbc52007-05-01 23:26:30 +02001607/*
1608 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001609 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 */
1611
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001612int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001614 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
David Brownell1d0b19c2008-10-14 17:30:05 +02001616 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301617 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02001618 return -EAGAIN;
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001621 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02001623 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Jean Delvare729d6dd2009-06-19 16:58:18 +02001625 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001626 * will have called probe() for all matching-but-unbound devices.
1627 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 res = driver_register(&driver->driver);
1629 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001630 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001631
Wolfram Sang44239a52016-07-09 13:35:04 +09001632 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Jean Delvare4735c982008-07-14 22:38:36 +02001634 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001635 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001636
Jean Delvare7eebcb72006-02-05 23:28:21 +01001637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001639EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Jean Delvare69b00892009-12-06 17:06:27 +01001641static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001642{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001643 if (dev->type == &i2c_adapter_type)
1644 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1645 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001646}
1647
David Brownella1d9e6e2007-05-01 23:26:30 +02001648/**
1649 * i2c_del_driver - unregister I2C driver
1650 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001651 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001652 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001653void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654{
Jean Delvare7ae31482011-03-20 14:50:52 +01001655 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09001658 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
David Brownellc0564602007-05-01 23:26:31 +02001660EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
David Brownell7b4fbc52007-05-01 23:26:30 +02001662/* ------------------------------------------------------------------------- */
1663
Jean Delvaree48d3312008-01-27 18:14:48 +01001664/**
1665 * i2c_use_client - increments the reference count of the i2c client structure
1666 * @client: the client being referenced
1667 *
1668 * Each live reference to a client should be refcounted. The driver model does
1669 * that automatically as part of driver binding, so that most drivers don't
1670 * need to do this explicitly: they hold a reference until they're unbound
1671 * from the device.
1672 *
1673 * A pointer to the client with the incremented reference counter is returned.
1674 */
1675struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
David Brownell6ea438e2008-07-14 22:38:24 +02001677 if (client && get_device(&client->dev))
1678 return client;
1679 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
David Brownellc0564602007-05-01 23:26:31 +02001681EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Jean Delvaree48d3312008-01-27 18:14:48 +01001683/**
1684 * i2c_release_client - release a use of the i2c client structure
1685 * @client: the client being no longer referenced
1686 *
1687 * Must be called when a user of a client is finished with it.
1688 */
1689void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
David Brownell6ea438e2008-07-14 22:38:24 +02001691 if (client)
1692 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
David Brownellc0564602007-05-01 23:26:31 +02001694EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
David Brownell9b766b82008-01-27 18:14:51 +01001696struct i2c_cmd_arg {
1697 unsigned cmd;
1698 void *arg;
1699};
1700
1701static int i2c_cmd(struct device *dev, void *_arg)
1702{
1703 struct i2c_client *client = i2c_verify_client(dev);
1704 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001705 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001706
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001707 if (!client || !client->dev.driver)
1708 return 0;
1709
1710 driver = to_i2c_driver(client->dev.driver);
1711 if (driver->command)
1712 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001713 return 0;
1714}
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1717{
David Brownell9b766b82008-01-27 18:14:51 +01001718 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
David Brownell9b766b82008-01-27 18:14:51 +01001720 cmd_arg.cmd = cmd;
1721 cmd_arg.arg = arg;
1722 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
David Brownellc0564602007-05-01 23:26:31 +02001724EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726static int __init i2c_init(void)
1727{
1728 int retval;
1729
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001730 retval = of_alias_get_highest_id("i2c");
1731
1732 down_write(&__i2c_board_lock);
1733 if (retval >= __i2c_first_dynamic_bus_num)
1734 __i2c_first_dynamic_bus_num = retval + 1;
1735 up_write(&__i2c_board_lock);
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 retval = bus_register(&i2c_bus_type);
1738 if (retval)
1739 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01001740
1741 is_registered = true;
1742
Jean Delvare2bb50952009-09-18 22:45:46 +02001743#ifdef CONFIG_I2C_COMPAT
1744 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1745 if (!i2c_adapter_compat_class) {
1746 retval = -ENOMEM;
1747 goto bus_err;
1748 }
1749#endif
David Brownelle9f13732008-01-27 18:14:52 +01001750 retval = i2c_add_driver(&dummy_driver);
1751 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001752 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001753
1754 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1755 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001756 if (IS_ENABLED(CONFIG_ACPI))
1757 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001758
David Brownelle9f13732008-01-27 18:14:52 +01001759 return 0;
1760
Jean Delvare2bb50952009-09-18 22:45:46 +02001761class_err:
1762#ifdef CONFIG_I2C_COMPAT
1763 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001764bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001765#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01001766 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01001767 bus_unregister(&i2c_bus_type);
1768 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769}
1770
1771static void __exit i2c_exit(void)
1772{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001773 if (IS_ENABLED(CONFIG_ACPI))
1774 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001775 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1776 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001777 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001778#ifdef CONFIG_I2C_COMPAT
1779 class_compat_unregister(i2c_adapter_compat_class);
1780#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001782 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784
David Brownella10f9e72008-10-14 17:30:06 +02001785/* We must initialize early, because some subsystems register i2c drivers
1786 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1787 */
1788postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789module_exit(i2c_exit);
1790
1791/* ----------------------------------------------------
1792 * the functional interface to the i2c busses.
1793 * ----------------------------------------------------
1794 */
1795
Wolfram Sangb7f62582015-01-05 23:45:59 +01001796/* Check if val is exceeding the quirk IFF quirk is non 0 */
1797#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1798
1799static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
1800{
1801 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
1802 err_msg, msg->addr, msg->len,
1803 msg->flags & I2C_M_RD ? "read" : "write");
1804 return -EOPNOTSUPP;
1805}
1806
1807static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1808{
1809 const struct i2c_adapter_quirks *q = adap->quirks;
1810 int max_num = q->max_num_msgs, i;
1811 bool do_len_check = true;
1812
1813 if (q->flags & I2C_AQ_COMB) {
1814 max_num = 2;
1815
1816 /* special checks for combined messages */
1817 if (num == 2) {
1818 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
1819 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
1820
1821 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
1822 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
1823
1824 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
1825 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
1826
1827 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
1828 return i2c_quirk_error(adap, &msgs[0], "msg too long");
1829
1830 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
1831 return i2c_quirk_error(adap, &msgs[1], "msg too long");
1832
1833 do_len_check = false;
1834 }
1835 }
1836
1837 if (i2c_quirk_exceeded(num, max_num))
1838 return i2c_quirk_error(adap, &msgs[0], "too many messages");
1839
1840 for (i = 0; i < num; i++) {
1841 u16 len = msgs[i].len;
1842
1843 if (msgs[i].flags & I2C_M_RD) {
1844 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
1845 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1846 } else {
1847 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
1848 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1849 }
1850 }
1851
1852 return 0;
1853}
1854
David Brownella1cdeda2008-07-14 22:38:24 +02001855/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001856 * __i2c_transfer - unlocked flavor of i2c_transfer
1857 * @adap: Handle to I2C bus
1858 * @msgs: One or more messages to execute before STOP is issued to
1859 * terminate the operation; each message begins with a START.
1860 * @num: Number of messages to be executed.
1861 *
1862 * Returns negative errno, else the number of messages executed.
1863 *
1864 * Adapter lock must be held when calling this function. No debug logging
1865 * takes place. adap->algo->master_xfer existence isn't checked.
1866 */
1867int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1868{
1869 unsigned long orig_jiffies;
1870 int ret, try;
1871
Wolfram Sangb7f62582015-01-05 23:45:59 +01001872 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
1873 return -EOPNOTSUPP;
1874
David Howellsd9a83d62014-03-06 13:35:59 +00001875 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
1876 * enabled. This is an efficient way of keeping the for-loop from
1877 * being executed when not needed.
1878 */
1879 if (static_key_false(&i2c_trace_msg)) {
1880 int i;
1881 for (i = 0; i < num; i++)
1882 if (msgs[i].flags & I2C_M_RD)
1883 trace_i2c_read(adap, &msgs[i], i);
1884 else
1885 trace_i2c_write(adap, &msgs[i], i);
1886 }
1887
Jean Delvareb37d2a32012-06-29 07:47:19 -03001888 /* Retry automatically on arbitration loss */
1889 orig_jiffies = jiffies;
1890 for (ret = 0, try = 0; try <= adap->retries; try++) {
1891 ret = adap->algo->master_xfer(adap, msgs, num);
1892 if (ret != -EAGAIN)
1893 break;
1894 if (time_after(jiffies, orig_jiffies + adap->timeout))
1895 break;
1896 }
1897
David Howellsd9a83d62014-03-06 13:35:59 +00001898 if (static_key_false(&i2c_trace_msg)) {
1899 int i;
1900 for (i = 0; i < ret; i++)
1901 if (msgs[i].flags & I2C_M_RD)
1902 trace_i2c_reply(adap, &msgs[i], i);
1903 trace_i2c_result(adap, i, ret);
1904 }
1905
Jean Delvareb37d2a32012-06-29 07:47:19 -03001906 return ret;
1907}
1908EXPORT_SYMBOL(__i2c_transfer);
1909
1910/**
David Brownella1cdeda2008-07-14 22:38:24 +02001911 * i2c_transfer - execute a single or combined I2C message
1912 * @adap: Handle to I2C bus
1913 * @msgs: One or more messages to execute before STOP is issued to
1914 * terminate the operation; each message begins with a START.
1915 * @num: Number of messages to be executed.
1916 *
1917 * Returns negative errno, else the number of messages executed.
1918 *
1919 * Note that there is no requirement that each message be sent to
1920 * the same slave address, although that is the most common model.
1921 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001922int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001924 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
David Brownella1cdeda2008-07-14 22:38:24 +02001926 /* REVISIT the fault reporting model here is weak:
1927 *
1928 * - When we get an error after receiving N bytes from a slave,
1929 * there is no way to report "N".
1930 *
1931 * - When we get a NAK after transmitting N bytes to a slave,
1932 * there is no way to report "N" ... or to let the master
1933 * continue executing the rest of this combined message, if
1934 * that's the appropriate response.
1935 *
1936 * - When for example "num" is two and we successfully complete
1937 * the first message but get an error part way through the
1938 * second, it's unclear whether that should be reported as
1939 * one (discarding status on the second message) or errno
1940 * (discarding status on the first one).
1941 */
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 if (adap->algo->master_xfer) {
1944#ifdef DEBUG
1945 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001946 dev_dbg(&adap->dev,
1947 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
1948 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
1949 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02001950 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952#endif
1953
Mike Rapoportcea443a82008-01-27 18:14:50 +01001954 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02001955 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001956 if (!ret)
1957 /* I2C activity is ongoing. */
1958 return -EAGAIN;
1959 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02001960 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001961 }
1962
Jean Delvareb37d2a32012-06-29 07:47:19 -03001963 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02001964 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 return ret;
1967 } else {
1968 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001969 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971}
David Brownellc0564602007-05-01 23:26:31 +02001972EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
David Brownella1cdeda2008-07-14 22:38:24 +02001974/**
1975 * i2c_master_send - issue a single I2C message in master transmit mode
1976 * @client: Handle to slave device
1977 * @buf: Data that will be written to the slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01001978 * @count: How many bytes to write, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02001979 *
1980 * Returns negative errno, or else the number of bytes written.
1981 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01001982int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
1984 int ret;
Farid Hammane7225acf2010-05-21 18:40:58 +02001985 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 struct i2c_msg msg;
1987
Jean Delvare815f55f2005-05-07 22:58:46 +02001988 msg.addr = client->addr;
1989 msg.flags = client->flags & I2C_M_TEN;
1990 msg.len = count;
1991 msg.buf = (char *)buf;
David Brownell438d6c22006-12-10 21:21:31 +01001992
Jean Delvare815f55f2005-05-07 22:58:46 +02001993 ret = i2c_transfer(adap, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001995 /*
1996 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1997 * transmitted, else error code.
1998 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001999 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
David Brownellc0564602007-05-01 23:26:31 +02002001EXPORT_SYMBOL(i2c_master_send);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
David Brownella1cdeda2008-07-14 22:38:24 +02002003/**
2004 * i2c_master_recv - issue a single I2C message in master receive mode
2005 * @client: Handle to slave device
2006 * @buf: Where to store data read from slave
Zhangfei Gao0c43ea52010-03-02 12:23:49 +01002007 * @count: How many bytes to read, must be less than 64k since msg.len is u16
David Brownella1cdeda2008-07-14 22:38:24 +02002008 *
2009 * Returns negative errno, or else the number of bytes read.
2010 */
Jean Delvare0cc43a12011-01-10 22:11:23 +01002011int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012{
Farid Hammane7225acf2010-05-21 18:40:58 +02002013 struct i2c_adapter *adap = client->adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 struct i2c_msg msg;
2015 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Jean Delvare815f55f2005-05-07 22:58:46 +02002017 msg.addr = client->addr;
2018 msg.flags = client->flags & I2C_M_TEN;
2019 msg.flags |= I2C_M_RD;
2020 msg.len = count;
2021 msg.buf = buf;
2022
2023 ret = i2c_transfer(adap, &msg, 1);
2024
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002025 /*
2026 * If everything went ok (i.e. 1 msg received), return #bytes received,
2027 * else error code.
2028 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002029 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
David Brownellc0564602007-05-01 23:26:31 +02002031EXPORT_SYMBOL(i2c_master_recv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033/* ----------------------------------------------------
2034 * the i2c address scanning function
2035 * Will not work for 10-bit addresses!
2036 * ----------------------------------------------------
2037 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002038
Jean Delvare63e4e802010-06-03 11:33:51 +02002039/*
2040 * Legacy default probe function, mostly relevant for SMBus. The default
2041 * probe method is a quick write, but it is known to corrupt the 24RF08
2042 * EEPROMs due to a state machine bug, and could also irreversibly
2043 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2044 * we use a short byte read instead. Also, some bus drivers don't implement
2045 * quick write, so we fallback to a byte read in that case too.
2046 * On x86, there is another special case for FSC hardware monitoring chips,
2047 * which want regular byte reads (address 0x73.) Fortunately, these are the
2048 * only known chips using this I2C address on PC hardware.
2049 * Returns 1 if probe succeeded, 0 if not.
2050 */
2051static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2052{
2053 int err;
2054 union i2c_smbus_data dummy;
2055
2056#ifdef CONFIG_X86
2057 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2058 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2059 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2060 I2C_SMBUS_BYTE_DATA, &dummy);
2061 else
2062#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002063 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2064 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002065 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2066 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002067 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2068 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2069 I2C_SMBUS_BYTE, &dummy);
2070 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002071 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2072 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002073 err = -EOPNOTSUPP;
2074 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002075
2076 return err >= 0;
2077}
2078
Jean Delvareccfbbd02009-12-06 17:06:25 +01002079static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002080 struct i2c_driver *driver)
2081{
2082 struct i2c_board_info info;
2083 struct i2c_adapter *adapter = temp_client->adapter;
2084 int addr = temp_client->addr;
2085 int err;
2086
2087 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002088 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002089 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002090 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2091 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002092 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002093 }
2094
Wolfram Sang9bccc702015-07-17 14:48:56 +02002095 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002096 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002097 return 0;
2098
Jean Delvareccfbbd02009-12-06 17:06:25 +01002099 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002100 if (!i2c_default_probe(adapter, addr))
2101 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002102
2103 /* Finally call the custom detection function */
2104 memset(&info, 0, sizeof(struct i2c_board_info));
2105 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002106 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002107 if (err) {
2108 /* -ENODEV is returned if the detection fails. We catch it
2109 here as this isn't an error. */
2110 return err == -ENODEV ? 0 : err;
2111 }
2112
2113 /* Consistency check */
2114 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002115 dev_err(&adapter->dev,
2116 "%s detection function provided no name for 0x%x\n",
2117 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002118 } else {
2119 struct i2c_client *client;
2120
2121 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002122 if (adapter->class & I2C_CLASS_DEPRECATED)
2123 dev_warn(&adapter->dev,
2124 "This adapter will soon drop class based instantiation of devices. "
2125 "Please make sure client 0x%02x gets instantiated by other means. "
2126 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2127 info.addr);
2128
Jean Delvare4735c982008-07-14 22:38:36 +02002129 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2130 info.type, info.addr);
2131 client = i2c_new_device(adapter, &info);
2132 if (client)
2133 list_add_tail(&client->detected, &driver->clients);
2134 else
2135 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2136 info.type, info.addr);
2137 }
2138 return 0;
2139}
2140
2141static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2142{
Jean Delvarec3813d62009-12-14 21:17:25 +01002143 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002144 struct i2c_client *temp_client;
2145 int i, err = 0;
2146 int adap_id = i2c_adapter_id(adapter);
2147
Jean Delvarec3813d62009-12-14 21:17:25 +01002148 address_list = driver->address_list;
2149 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002150 return 0;
2151
Wolfram Sang45552272014-07-10 13:46:21 +02002152 /* Warn that the adapter lost class based instantiation */
2153 if (adapter->class == I2C_CLASS_DEPRECATED) {
2154 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002155 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2156 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002157 driver->driver.name);
2158 return 0;
2159 }
2160
Jean Delvare51b54ba2010-10-24 18:16:58 +02002161 /* Stop here if the classes do not match */
2162 if (!(adapter->class & driver->class))
2163 return 0;
2164
Jean Delvare4735c982008-07-14 22:38:36 +02002165 /* Set up a temporary client to help detect callback */
2166 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2167 if (!temp_client)
2168 return -ENOMEM;
2169 temp_client->adapter = adapter;
2170
Jean Delvarec3813d62009-12-14 21:17:25 +01002171 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002172 dev_dbg(&adapter->dev,
2173 "found normal entry for adapter %d, addr 0x%02x\n",
2174 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002175 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002176 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002177 if (unlikely(err))
2178 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002179 }
2180
Jean Delvare4735c982008-07-14 22:38:36 +02002181 kfree(temp_client);
2182 return err;
2183}
2184
Jean Delvared44f19d2010-08-11 18:20:57 +02002185int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2186{
2187 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2188 I2C_SMBUS_QUICK, NULL) >= 0;
2189}
2190EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2191
Jean Delvare12b5053a2007-05-01 23:26:31 +02002192struct i2c_client *
2193i2c_new_probed_device(struct i2c_adapter *adap,
2194 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002195 unsigned short const *addr_list,
2196 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002197{
2198 int i;
2199
Jean Delvare8031d792010-08-11 18:21:00 +02002200 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002201 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002202
Jean Delvare12b5053a2007-05-01 23:26:31 +02002203 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2204 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002205 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002206 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2207 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002208 continue;
2209 }
2210
Wolfram Sang9bccc702015-07-17 14:48:56 +02002211 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002212 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002213 dev_dbg(&adap->dev,
2214 "Address 0x%02x already in use, not probing\n",
2215 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002216 continue;
2217 }
2218
Jean Delvare63e4e802010-06-03 11:33:51 +02002219 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002220 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002221 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002222 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002223
2224 if (addr_list[i] == I2C_CLIENT_END) {
2225 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2226 return NULL;
2227 }
2228
2229 info->addr = addr_list[i];
2230 return i2c_new_device(adap, info);
2231}
2232EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2233
Jean Delvared735b342011-03-20 14:50:52 +01002234struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002237
Jean Delvarecaada322008-01-27 18:14:49 +01002238 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002239 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002240 if (!adapter)
2241 goto exit;
2242
2243 if (try_module_get(adapter->owner))
2244 get_device(&adapter->dev);
2245 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002246 adapter = NULL;
2247
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002248 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002249 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002250 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
David Brownellc0564602007-05-01 23:26:31 +02002252EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
2254void i2c_put_adapter(struct i2c_adapter *adap)
2255{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002256 if (!adap)
2257 return;
2258
2259 put_device(&adap->dev);
2260 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261}
David Brownellc0564602007-05-01 23:26:31 +02002262EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2265MODULE_DESCRIPTION("I2C-Bus main module");
2266MODULE_LICENSE("GPL");