blob: a8b3fbe16fa08c4529b4ccb29da9e481c9942481 [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>
Phil Reid10c9ef02017-11-28 11:09:10 +080030#include <linux/gpio/consumer.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010031#include <linux/i2c.h>
Phil Reidf8756c62017-08-24 17:31:04 +080032#include <linux/i2c-smbus.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010033#include <linux/idr.h>
34#include <linux/init.h>
35#include <linux/irqflags.h>
36#include <linux/jump_label.h>
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/mutex.h>
40#include <linux/of_device.h>
41#include <linux/of.h>
42#include <linux/of_irq.h>
43#include <linux/pm_domain.h>
44#include <linux/pm_runtime.h>
45#include <linux/pm_wakeirq.h>
Wolfram Sange1dba012015-12-08 10:37:46 +010046#include <linux/property.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010047#include <linux/rwsem.h>
48#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
David Brownell9c1600e2007-05-01 23:26:31 +020050#include "i2c-core.h"
51
David Howellsd9a83d62014-03-06 13:35:59 +000052#define CREATE_TRACE_POINTS
53#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Wolfram Sangda899f52015-05-18 21:09:12 +020055#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
56#define I2C_ADDR_OFFSET_SLAVE 0x1000
57
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +020058#define I2C_ADDR_7BITS_MAX 0x77
59#define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1)
60
Wolfram Sang61e3d0f2017-05-23 19:23:11 +020061/*
62 * core_lock protects i2c_adapter_idr, and guarantees that device detection,
63 * deletion of detected devices, and attach_adapter calls are serialized
64 */
Jean Delvarecaada322008-01-27 18:14:49 +010065static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static DEFINE_IDR(i2c_adapter_idr);
67
Jean Delvare4735c982008-07-14 22:38:36 +020068static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010069
David Howellsd9a83d62014-03-06 13:35:59 +000070static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
Sudip Mukherjee95026652016-03-07 17:19:17 +053071static bool is_registered;
David Howellsd9a83d62014-03-06 13:35:59 +000072
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050073int i2c_transfer_trace_reg(void)
David Howellsd9a83d62014-03-06 13:35:59 +000074{
75 static_key_slow_inc(&i2c_trace_msg);
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050076 return 0;
David Howellsd9a83d62014-03-06 13:35:59 +000077}
78
79void i2c_transfer_trace_unreg(void)
80{
81 static_key_slow_dec(&i2c_trace_msg);
82}
83
Lee Jones5f441fc2016-11-07 12:47:40 +000084const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
Jean Delvared2653e92008-04-29 23:11:39 +020085 const struct i2c_client *client)
86{
Lee Jones811073b2016-11-07 12:47:36 +000087 if (!(id && client))
88 return NULL;
89
Jean Delvared2653e92008-04-29 23:11:39 +020090 while (id->name[0]) {
91 if (strcmp(client->name, id->name) == 0)
92 return id;
93 id++;
94 }
95 return NULL;
96}
Lee Jones5f441fc2016-11-07 12:47:40 +000097EXPORT_SYMBOL_GPL(i2c_match_id);
Jean Delvared2653e92008-04-29 23:11:39 +020098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int i2c_device_match(struct device *dev, struct device_driver *drv)
100{
Jean Delvare51298d12009-09-18 22:45:45 +0200101 struct i2c_client *client = i2c_verify_client(dev);
102 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200103
Jean Delvare51298d12009-09-18 22:45:45 +0200104
Grant Likely959e85f2010-06-08 07:48:19 -0600105 /* Attempt an OF style match */
Lee Jonesda10c062016-11-07 12:47:39 +0000106 if (i2c_of_match_device(drv->of_match_table, client))
Grant Likely959e85f2010-06-08 07:48:19 -0600107 return 1;
108
Mika Westerberg907ddf82012-11-23 12:23:40 +0100109 /* Then ACPI style match */
110 if (acpi_driver_match_device(dev, drv))
111 return 1;
112
Jean Delvare51298d12009-09-18 22:45:45 +0200113 driver = to_i2c_driver(drv);
Lee Jones811073b2016-11-07 12:47:36 +0000114
115 /* Finally an I2C match */
116 if (i2c_match_id(driver->id_table, client))
117 return 1;
Jean Delvared2653e92008-04-29 23:11:39 +0200118
Jean Delvareeb8a7902008-05-18 20:49:41 +0200119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Kay Sievers7eff2e72007-08-14 15:15:12 +0200122static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200123{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100124 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800125 int rc;
126
127 rc = acpi_device_uevent_modalias(dev, env);
128 if (rc != -ENODEV)
129 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200130
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100131 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200132}
133
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530134/* i2c bus recovery routines */
135static int get_scl_gpio_value(struct i2c_adapter *adap)
136{
Phil Reid3991c5c2017-11-02 10:40:24 +0800137 return gpiod_get_value_cansleep(adap->bus_recovery_info->scl_gpiod);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530138}
139
140static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
141{
Phil Reid3991c5c2017-11-02 10:40:24 +0800142 gpiod_set_value_cansleep(adap->bus_recovery_info->scl_gpiod, val);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530143}
144
145static int get_sda_gpio_value(struct i2c_adapter *adap)
146{
Phil Reid3991c5c2017-11-02 10:40:24 +0800147 return gpiod_get_value_cansleep(adap->bus_recovery_info->sda_gpiod);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530148}
149
Wolfram Sang80921782018-01-09 14:58:56 +0100150static void set_sda_gpio_value(struct i2c_adapter *adap, int val)
151{
152 gpiod_set_value_cansleep(adap->bus_recovery_info->sda_gpiod, val);
153}
154
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530155/*
156 * We are generating clock pulses. ndelay() determines durating of clk pulses.
157 * We will generate clock with rate 100 KHz and so duration of both clock levels
158 * is: delay in ns = (10^6 / 100) / 2
159 */
160#define RECOVERY_NDELAY 5000
161#define RECOVERY_CLK_CNT 9
162
Phil Reide1eb7d22017-11-02 10:40:30 +0800163int i2c_generic_scl_recovery(struct i2c_adapter *adap)
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530164{
165 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
166 int i = 0, val = 1, ret = 0;
167
168 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300169 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530170
Jan Luebbe8b062602015-07-08 16:35:06 +0200171 bri->set_scl(adap, val);
172 ndelay(RECOVERY_NDELAY);
173
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530174 /*
175 * By this time SCL is high, as we need to give 9 falling-rising edges
176 */
177 while (i++ < RECOVERY_CLK_CNT * 2) {
178 if (val) {
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530179 /* SCL shouldn't be low here */
180 if (!bri->get_scl(adap)) {
181 dev_err(&adap->dev,
182 "SCL is stuck low, exit recovery\n");
183 ret = -EBUSY;
184 break;
185 }
Claudio Foellmi1f35b862017-10-05 14:44:14 +0200186 /* Break if SDA is high */
187 if (bri->get_sda && bri->get_sda(adap))
188 break;
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530189 }
190
191 val = !val;
192 bri->set_scl(adap, val);
193 ndelay(RECOVERY_NDELAY);
194 }
195
Claudio Foellmi1f35b862017-10-05 14:44:14 +0200196 /* check if recovery actually succeeded */
197 if (bri->get_sda && !bri->get_sda(adap))
198 ret = -EBUSY;
199
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530200 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300201 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530202
203 return ret;
204}
Mark Brownc1c21f42015-04-15 19:18:39 +0100205EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530206
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530207int i2c_recover_bus(struct i2c_adapter *adap)
208{
209 if (!adap->bus_recovery_info)
210 return -EOPNOTSUPP;
211
212 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
213 return adap->bus_recovery_info->recover_bus(adap);
214}
Mark Brownc1c21f42015-04-15 19:18:39 +0100215EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530216
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900217static void i2c_init_recovery(struct i2c_adapter *adap)
218{
219 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
220 char *err_str;
221
222 if (!bri)
223 return;
224
225 if (!bri->recover_bus) {
226 err_str = "no recover_bus() found";
227 goto err;
228 }
229
Phil Reid3991c5c2017-11-02 10:40:24 +0800230 if (bri->scl_gpiod && bri->recover_bus == i2c_generic_scl_recovery) {
231 bri->get_scl = get_scl_gpio_value;
232 bri->set_scl = set_scl_gpio_value;
Wolfram Sang80921782018-01-09 14:58:56 +0100233 if (bri->sda_gpiod) {
Phil Reid3991c5c2017-11-02 10:40:24 +0800234 bri->get_sda = get_sda_gpio_value;
Wolfram Sang80921782018-01-09 14:58:56 +0100235 /* FIXME: add proper flag instead of '0' once available */
236 if (gpiod_get_direction(bri->sda_gpiod) == 0)
237 bri->set_sda = set_sda_gpio_value;
238 }
Phil Reid3991c5c2017-11-02 10:40:24 +0800239 return;
240 }
241
Phil Reide1eb7d22017-11-02 10:40:30 +0800242 if (bri->recover_bus == i2c_generic_scl_recovery) {
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900243 /* Generic SCL recovery */
244 if (!bri->set_scl || !bri->get_scl) {
245 err_str = "no {get|set}_scl() found";
246 goto err;
247 }
248 }
249
250 return;
251 err:
252 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
253 adap->bus_recovery_info = NULL;
254}
255
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200256static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
257{
258 struct i2c_adapter *adap = client->adapter;
259 unsigned int irq;
260
261 if (!adap->host_notify_domain)
262 return -ENXIO;
263
264 if (client->flags & I2C_CLIENT_TEN)
265 return -EINVAL;
266
267 irq = irq_find_mapping(adap->host_notify_domain, client->addr);
268 if (!irq)
269 irq = irq_create_mapping(adap->host_notify_domain,
270 client->addr);
271
272 return irq > 0 ? irq : -ENXIO;
273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static int i2c_device_probe(struct device *dev)
276{
Jean Delvare51298d12009-09-18 22:45:45 +0200277 struct i2c_client *client = i2c_verify_client(dev);
278 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100279 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200280
Jean Delvare51298d12009-09-18 22:45:45 +0200281 if (!client)
282 return 0;
283
Hans de Goeded1d84bb2017-04-05 00:03:34 +0200284 driver = to_i2c_driver(dev->driver);
285
286 if (!client->irq && !driver->disable_i2c_core_irq_mapping) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300287 int irq = -ENOENT;
288
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800289 if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
290 dev_dbg(dev, "Using Host Notify IRQ\n");
291 irq = i2c_smbus_host_notify_to_irq(client);
292 } else if (dev->of_node) {
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700293 irq = of_irq_get_byname(dev->of_node, "irq");
294 if (irq == -EINVAL || irq == -ENODATA)
295 irq = of_irq_get(dev->of_node, 0);
296 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300297 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700298 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100299 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200300 return irq;
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800301
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100302 if (irq < 0)
303 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200304
305 client->irq = irq;
306 }
307
Lee Jonesda10c062016-11-07 12:47:39 +0000308 /*
Javier Martinez Canillasf4b17a12017-08-09 11:21:28 +0200309 * An I2C ID table is not mandatory, if and only if, a suitable OF
310 * or ACPI ID table is supplied for the probing device.
Lee Jonesda10c062016-11-07 12:47:39 +0000311 */
312 if (!driver->id_table &&
Andy Shevchenkoc64ffff2017-07-17 17:13:28 +0300313 !i2c_acpi_match_device(dev->driver->acpi_match_table, client) &&
Lee Jonesda10c062016-11-07 12:47:39 +0000314 !i2c_of_match_device(dev->driver->of_match_table, client))
David Brownell7b4fbc52007-05-01 23:26:30 +0200315 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200316
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700317 if (client->flags & I2C_CLIENT_WAKE) {
318 int wakeirq = -ENOENT;
319
320 if (dev->of_node) {
321 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
322 if (wakeirq == -EPROBE_DEFER)
323 return wakeirq;
324 }
325
326 device_init_wakeup(&client->dev, true);
327
328 if (wakeirq > 0 && wakeirq != client->irq)
329 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
330 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200331 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700332 else
333 status = 0;
334
335 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300336 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700337 }
338
David Brownell7b4fbc52007-05-01 23:26:30 +0200339 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200340
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200341 status = of_clk_set_defaults(dev->of_node, false);
342 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700343 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200344
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200345 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +0100346 if (status == -EPROBE_DEFER)
347 goto err_clear_wakeup_irq;
348
Lee Jonesb8a1a4c2016-11-07 12:47:41 +0000349 /*
350 * When there are no more users of probe(),
351 * rename probe_new to probe.
352 */
353 if (driver->probe_new)
354 status = driver->probe_new(client);
355 else if (driver->probe)
356 status = driver->probe(client,
357 i2c_match_id(driver->id_table, client));
358 else
359 status = -EINVAL;
360
Kieran Bingham74cedd32015-10-12 21:54:43 +0100361 if (status)
362 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100363
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700364 return 0;
365
366err_detach_pm_domain:
367 dev_pm_domain_detach(&client->dev, true);
368err_clear_wakeup_irq:
369 dev_pm_clear_wake_irq(&client->dev);
370 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +0100371 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
374static int i2c_device_remove(struct device *dev)
375{
Jean Delvare51298d12009-09-18 22:45:45 +0200376 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200377 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100378 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200379
Jean Delvare51298d12009-09-18 22:45:45 +0200380 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200381 return 0;
382
383 driver = to_i2c_driver(dev->driver);
384 if (driver->remove) {
385 dev_dbg(dev, "remove\n");
386 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200387 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100388
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200389 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700390
391 dev_pm_clear_wake_irq(&client->dev);
392 device_init_wakeup(&client->dev, false);
393
David Brownella1d9e6e2007-05-01 23:26:30 +0200394 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
David Brownellf37dd802007-02-13 22:09:00 +0100397static void i2c_device_shutdown(struct device *dev)
398{
Jean Delvare51298d12009-09-18 22:45:45 +0200399 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100400 struct i2c_driver *driver;
401
Jean Delvare51298d12009-09-18 22:45:45 +0200402 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100403 return;
404 driver = to_i2c_driver(dev->driver);
405 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200406 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100407}
408
David Brownell9c1600e2007-05-01 23:26:31 +0200409static void i2c_client_dev_release(struct device *dev)
410{
411 kfree(to_i2c_client(dev));
412}
413
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100414static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200415show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200416{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200417 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
418 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200419}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100420static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200421
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100422static ssize_t
423show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200424{
425 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800426 int len;
427
428 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
429 if (len != -ENODEV)
430 return len;
431
Jean Delvareeb8a7902008-05-18 20:49:41 +0200432 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200433}
Jean Delvare51298d12009-09-18 22:45:45 +0200434static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
435
436static struct attribute *i2c_dev_attrs[] = {
437 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200438 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200439 &dev_attr_modalias.attr,
440 NULL
441};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100442ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100443
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200444struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100445 .name = "i2c",
446 .match = i2c_device_match,
447 .probe = i2c_device_probe,
448 .remove = i2c_device_remove,
449 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000450};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200451EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000452
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800453struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100454 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200455 .uevent = i2c_device_uevent,
456 .release = i2c_client_dev_release,
457};
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800458EXPORT_SYMBOL_GPL(i2c_client_type);
Jean Delvare51298d12009-09-18 22:45:45 +0200459
David Brownell9b766b82008-01-27 18:14:51 +0100460
461/**
462 * i2c_verify_client - return parameter as i2c_client, or NULL
463 * @dev: device, probably from some driver model iterator
464 *
465 * When traversing the driver model tree, perhaps using driver model
466 * iterators like @device_for_each_child(), you can't assume very much
467 * about the nodes you find. Use this function to avoid oopses caused
468 * by wrongly treating some non-I2C device as an i2c_client.
469 */
470struct i2c_client *i2c_verify_client(struct device *dev)
471{
Jean Delvare51298d12009-09-18 22:45:45 +0200472 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100473 ? to_i2c_client(dev)
474 : NULL;
475}
476EXPORT_SYMBOL(i2c_verify_client);
477
478
Wolfram Sangda899f52015-05-18 21:09:12 +0200479/* Return a unique address which takes the flags of the client into account */
480static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
481{
482 unsigned short addr = client->addr;
483
484 /* For some client flags, add an arbitrary offset to avoid collisions */
485 if (client->flags & I2C_CLIENT_TEN)
486 addr |= I2C_ADDR_OFFSET_TEN_BIT;
487
488 if (client->flags & I2C_CLIENT_SLAVE)
489 addr |= I2C_ADDR_OFFSET_SLAVE;
490
491 return addr;
492}
493
Jean Delvare3a89db52010-06-03 11:33:52 +0200494/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300495 * are purposely not enforced, except for the general call address. */
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200496int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +0200497{
Wolfram Sangc4019b72015-07-17 12:50:06 +0200498 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +0200499 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200500 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +0200501 return -EINVAL;
502 } else {
503 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200504 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +0200505 return -EINVAL;
506 }
507 return 0;
508}
509
Jean Delvare656b8762010-06-03 11:33:53 +0200510/* And this is a strict address validity check, used when probing. If a
511 * device uses a reserved address, then it shouldn't be probed. 7-bit
512 * addressing is assumed, 10-bit address devices are rare and should be
513 * explicitly enumerated. */
Wolfram Sange4991ec2017-05-23 11:14:17 +0200514int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +0200515{
516 /*
517 * Reserved addresses per I2C specification:
518 * 0x00 General call address / START byte
519 * 0x01 CBUS address
520 * 0x02 Reserved for different bus format
521 * 0x03 Reserved for future purposes
522 * 0x04-0x07 Hs-mode master code
523 * 0x78-0x7b 10-bit slave addressing
524 * 0x7c-0x7f Reserved for future purposes
525 */
526 if (addr < 0x08 || addr > 0x77)
527 return -EINVAL;
528 return 0;
529}
530
Jean Delvare3b5f7942010-06-03 11:33:55 +0200531static int __i2c_check_addr_busy(struct device *dev, void *addrp)
532{
533 struct i2c_client *client = i2c_verify_client(dev);
534 int addr = *(int *)addrp;
535
Wolfram Sang9bccc702015-07-17 14:48:56 +0200536 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +0200537 return -EBUSY;
538 return 0;
539}
540
Michael Lawnick08263742010-08-11 18:21:02 +0200541/* walk up mux tree */
542static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
543{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200544 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200545 int result;
546
547 result = device_for_each_child(&adapter->dev, &addr,
548 __i2c_check_addr_busy);
549
Jean Delvare97cc4d42010-10-24 18:16:57 +0200550 if (!result && parent)
551 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200552
553 return result;
554}
555
556/* recurse down mux tree */
557static int i2c_check_mux_children(struct device *dev, void *addrp)
558{
559 int result;
560
561 if (dev->type == &i2c_adapter_type)
562 result = device_for_each_child(dev, addrp,
563 i2c_check_mux_children);
564 else
565 result = __i2c_check_addr_busy(dev, addrp);
566
567 return result;
568}
569
Jean Delvare3b5f7942010-06-03 11:33:55 +0200570static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
571{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200572 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200573 int result = 0;
574
Jean Delvare97cc4d42010-10-24 18:16:57 +0200575 if (parent)
576 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200577
578 if (!result)
579 result = device_for_each_child(&adapter->dev, &addr,
580 i2c_check_mux_children);
581
582 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200583}
584
David Brownell9c1600e2007-05-01 23:26:31 +0200585/**
Peter Rosin8320f492016-05-04 22:15:27 +0200586 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200587 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200588 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
589 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200590 */
Peter Rosin8320f492016-05-04 22:15:27 +0200591static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
592 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200593{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200594 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200595}
Jean Delvarefe61e072010-08-11 18:20:58 +0200596
597/**
Peter Rosin8320f492016-05-04 22:15:27 +0200598 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200599 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200600 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
601 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200602 */
Peter Rosin8320f492016-05-04 22:15:27 +0200603static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
604 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200605{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200606 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200607}
608
609/**
Peter Rosin8320f492016-05-04 22:15:27 +0200610 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200611 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200612 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
613 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200614 */
Peter Rosin8320f492016-05-04 22:15:27 +0200615static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
616 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200617{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200618 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200619}
Jean Delvarefe61e072010-08-11 18:20:58 +0200620
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200621static void i2c_dev_set_name(struct i2c_adapter *adap,
Hans de Goede728fe6c2017-10-11 11:41:19 +0200622 struct i2c_client *client,
623 struct i2c_board_info const *info)
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200624{
625 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
626
Hans de Goede728fe6c2017-10-11 11:41:19 +0200627 if (info && info->dev_name) {
628 dev_set_name(&client->dev, "i2c-%s", info->dev_name);
629 return;
630 }
631
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200632 if (adev) {
633 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
634 return;
635 }
636
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200637 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +0200638 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200639}
640
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800641static int i2c_dev_irq_from_resources(const struct resource *resources,
642 unsigned int num_resources)
643{
644 struct irq_data *irqd;
645 int i;
646
647 for (i = 0; i < num_resources; i++) {
648 const struct resource *r = &resources[i];
649
650 if (resource_type(r) != IORESOURCE_IRQ)
651 continue;
652
653 if (r->flags & IORESOURCE_BITS) {
654 irqd = irq_get_irq_data(r->start);
655 if (!irqd)
656 break;
657
658 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
659 }
660
661 return r->start;
662 }
663
664 return 0;
665}
666
Jean Delvarefe61e072010-08-11 18:20:58 +0200667/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200668 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200669 * @adap: the adapter managing the device
670 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200671 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200672 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200673 * Create an i2c device. Binding is handled through driver model
674 * probe()/remove() methods. A driver may be bound to this device when we
675 * return from this function, or any later moment (e.g. maybe hotplugging will
676 * load the driver module). This call is not appropriate for use by mainboard
677 * initialization logic, which usually runs during an arch_initcall() long
678 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200679 *
680 * This returns the new i2c client, which may be saved for later use with
681 * i2c_unregister_device(); or NULL to indicate an error.
682 */
683struct i2c_client *
684i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
685{
686 struct i2c_client *client;
687 int status;
688
689 client = kzalloc(sizeof *client, GFP_KERNEL);
690 if (!client)
691 return NULL;
692
693 client->adapter = adap;
694
695 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200696
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200697 if (info->archdata)
698 client->dev.archdata = *info->archdata;
699
Marc Pignatee354252008-08-28 08:33:22 +0200700 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200701 client->addr = info->addr;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800702
David Brownell9c1600e2007-05-01 23:26:31 +0200703 client->irq = info->irq;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800704 if (!client->irq)
705 client->irq = i2c_dev_irq_from_resources(info->resources,
706 info->num_resources);
David Brownell9c1600e2007-05-01 23:26:31 +0200707
David Brownell9c1600e2007-05-01 23:26:31 +0200708 strlcpy(client->name, info->type, sizeof(client->name));
709
Wolfram Sangc4019b72015-07-17 12:50:06 +0200710 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +0200711 if (status) {
712 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
713 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
714 goto out_err_silent;
715 }
716
Jean Delvaref8a227e2009-06-19 16:58:18 +0200717 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +0200718 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200719 if (status)
720 goto out_err;
721
722 client->dev.parent = &client->adapter->dev;
723 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200724 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700725 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100726 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200727
Hans de Goede728fe6c2017-10-11 11:41:19 +0200728 i2c_dev_set_name(adap, client, info);
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800729
730 if (info->properties) {
731 status = device_add_properties(&client->dev, info->properties);
732 if (status) {
733 dev_err(&adap->dev,
734 "Failed to add properties to client %s: %d\n",
735 client->name, status);
736 goto out_err;
737 }
738 }
739
Jean Delvaref8a227e2009-06-19 16:58:18 +0200740 status = device_register(&client->dev);
741 if (status)
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800742 goto out_free_props;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200743
Jean Delvaref8a227e2009-06-19 16:58:18 +0200744 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
745 client->name, dev_name(&client->dev));
746
David Brownell9c1600e2007-05-01 23:26:31 +0200747 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200748
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800749out_free_props:
750 if (info->properties)
751 device_remove_properties(&client->dev);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200752out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300753 dev_err(&adap->dev,
754 "Failed to register i2c client %s at 0x%02x (%d)\n",
755 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200756out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200757 kfree(client);
758 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200759}
760EXPORT_SYMBOL_GPL(i2c_new_device);
761
762
763/**
764 * i2c_unregister_device - reverse effect of i2c_new_device()
765 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200766 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200767 */
768void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200769{
Andy Shevchenko7b43dd12017-10-31 16:21:35 +0200770 if (!client)
771 return;
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +0200772 if (client->dev.of_node)
773 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300774 if (ACPI_COMPANION(&client->dev))
775 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +0200776 device_unregister(&client->dev);
777}
David Brownell9c1600e2007-05-01 23:26:31 +0200778EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200779
780
Jean Delvare60b129d2008-05-11 20:37:06 +0200781static const struct i2c_device_id dummy_id[] = {
782 { "dummy", 0 },
783 { },
784};
785
Jean Delvared2653e92008-04-29 23:11:39 +0200786static int dummy_probe(struct i2c_client *client,
787 const struct i2c_device_id *id)
788{
789 return 0;
790}
791
792static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100793{
794 return 0;
795}
796
797static struct i2c_driver dummy_driver = {
798 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200799 .probe = dummy_probe,
800 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200801 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100802};
803
804/**
805 * i2c_new_dummy - return a new i2c device bound to a dummy driver
806 * @adapter: the adapter managing the device
807 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100808 * Context: can sleep
809 *
810 * This returns an I2C client bound to the "dummy" driver, intended for use
811 * with devices that consume multiple addresses. Examples of such chips
812 * include various EEPROMS (like 24c04 and 24c08 models).
813 *
814 * These dummy devices have two main uses. First, most I2C and SMBus calls
815 * except i2c_transfer() need a client handle; the dummy will be that handle.
816 * And second, this prevents the specified address from being bound to a
817 * different driver.
818 *
819 * This returns the new i2c client, which should be saved for later use with
820 * i2c_unregister_device(); or NULL to indicate an error.
821 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100822struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100823{
824 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200825 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100826 };
827
David Brownelle9f13732008-01-27 18:14:52 +0100828 return i2c_new_device(adapter, &info);
829}
830EXPORT_SYMBOL_GPL(i2c_new_dummy);
831
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +0100832/**
833 * i2c_new_secondary_device - Helper to get the instantiated secondary address
834 * and create the associated device
835 * @client: Handle to the primary client
836 * @name: Handle to specify which secondary address to get
837 * @default_addr: Used as a fallback if no secondary address was specified
838 * Context: can sleep
839 *
840 * I2C clients can be composed of multiple I2C slaves bound together in a single
841 * component. The I2C client driver then binds to the master I2C slave and needs
842 * to create I2C dummy clients to communicate with all the other slaves.
843 *
844 * This function creates and returns an I2C dummy client whose I2C address is
845 * retrieved from the platform firmware based on the given slave name. If no
846 * address is specified by the firmware default_addr is used.
847 *
848 * On DT-based platforms the address is retrieved from the "reg" property entry
849 * cell whose "reg-names" value matches the slave name.
850 *
851 * This returns the new i2c client, which should be saved for later use with
852 * i2c_unregister_device(); or NULL to indicate an error.
853 */
854struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
855 const char *name,
856 u16 default_addr)
857{
858 struct device_node *np = client->dev.of_node;
859 u32 addr = default_addr;
860 int i;
861
862 if (np) {
863 i = of_property_match_string(np, "reg-names", name);
864 if (i >= 0)
865 of_property_read_u32_index(np, "reg", i, &addr);
866 }
867
868 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
869 return i2c_new_dummy(client->adapter, addr);
870}
871EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
872
David Brownellf37dd802007-02-13 22:09:00 +0100873/* ------------------------------------------------------------------------- */
874
David Brownell16ffadf2007-05-01 23:26:28 +0200875/* I2C bus adapters -- one roots each I2C or SMBUS segment */
876
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200877static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
David Brownellef2c83212007-05-01 23:26:28 +0200879 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 complete(&adap->dev_released);
881}
882
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +0200883unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +0200884{
885 unsigned int depth = 0;
886
887 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
888 depth++;
889
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +0200890 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
891 "adapter depth exceeds lockdep subclass limit\n");
892
Jean Delvare390946b2012-09-10 10:14:02 +0200893 return depth;
894}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +0200895EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +0200896
897/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200898 * Let users instantiate I2C devices through sysfs. This can be used when
899 * platform initialization code doesn't contain the proper data for
900 * whatever reason. Also useful for drivers that do device detection and
901 * detection fails, either because the device uses an unexpected address,
902 * or this is a compatible device with different ID register values.
903 *
904 * Parameter checking may look overzealous, but we really don't want
905 * the user to provide incorrect parameters.
906 */
907static ssize_t
908i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
909 const char *buf, size_t count)
910{
911 struct i2c_adapter *adap = to_i2c_adapter(dev);
912 struct i2c_board_info info;
913 struct i2c_client *client;
914 char *blank, end;
915 int res;
916
Jean Delvare99cd8e22009-06-19 16:58:20 +0200917 memset(&info, 0, sizeof(struct i2c_board_info));
918
919 blank = strchr(buf, ' ');
920 if (!blank) {
921 dev_err(dev, "%s: Missing parameters\n", "new_device");
922 return -EINVAL;
923 }
924 if (blank - buf > I2C_NAME_SIZE - 1) {
925 dev_err(dev, "%s: Invalid device name\n", "new_device");
926 return -EINVAL;
927 }
928 memcpy(info.type, buf, blank - buf);
929
930 /* Parse remaining parameters, reject extra parameters */
931 res = sscanf(++blank, "%hi%c", &info.addr, &end);
932 if (res < 1) {
933 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
934 return -EINVAL;
935 }
936 if (res > 1 && end != '\n') {
937 dev_err(dev, "%s: Extra parameters\n", "new_device");
938 return -EINVAL;
939 }
940
Wolfram Sangcfa03272015-07-27 14:03:38 +0200941 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
942 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
943 info.flags |= I2C_CLIENT_TEN;
944 }
945
946 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
947 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
948 info.flags |= I2C_CLIENT_SLAVE;
949 }
950
Jean Delvare99cd8e22009-06-19 16:58:20 +0200951 client = i2c_new_device(adap, &info);
952 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200953 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200954
955 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200956 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200957 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200958 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200959 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
960 info.type, info.addr);
961
962 return count;
963}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100964static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200965
966/*
967 * And of course let the users delete the devices they instantiated, if
968 * they got it wrong. This interface can only be used to delete devices
969 * instantiated by i2c_sysfs_new_device above. This guarantees that we
970 * don't delete devices to which some kernel code still has references.
971 *
972 * Parameter checking may look overzealous, but we really don't want
973 * the user to delete the wrong device.
974 */
975static ssize_t
976i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
977 const char *buf, size_t count)
978{
979 struct i2c_adapter *adap = to_i2c_adapter(dev);
980 struct i2c_client *client, *next;
981 unsigned short addr;
982 char end;
983 int res;
984
985 /* Parse parameters, reject extra parameters */
986 res = sscanf(buf, "%hi%c", &addr, &end);
987 if (res < 1) {
988 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
989 return -EINVAL;
990 }
991 if (res > 1 && end != '\n') {
992 dev_err(dev, "%s: Extra parameters\n", "delete_device");
993 return -EINVAL;
994 }
995
996 /* Make sure the device was added through sysfs */
997 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +0200998 mutex_lock_nested(&adap->userspace_clients_lock,
999 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001000 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1001 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001002 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001003 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1004 "delete_device", client->name, client->addr);
1005
1006 list_del(&client->detected);
1007 i2c_unregister_device(client);
1008 res = count;
1009 break;
1010 }
1011 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001012 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001013
1014 if (res < 0)
1015 dev_err(dev, "%s: Can't find device in list\n",
1016 "delete_device");
1017 return res;
1018}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001019static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1020 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001021
1022static struct attribute *i2c_adapter_attrs[] = {
1023 &dev_attr_name.attr,
1024 &dev_attr_new_device.attr,
1025 &dev_attr_delete_device.attr,
1026 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001027};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001028ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001029
Michael Lawnick08263742010-08-11 18:21:02 +02001030struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001031 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001032 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001033};
Michael Lawnick08263742010-08-11 18:21:02 +02001034EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Stephen Warren643dd092012-04-17 12:43:33 -06001036/**
1037 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1038 * @dev: device, probably from some driver model iterator
1039 *
1040 * When traversing the driver model tree, perhaps using driver model
1041 * iterators like @device_for_each_child(), you can't assume very much
1042 * about the nodes you find. Use this function to avoid oopses caused
1043 * by wrongly treating some non-I2C device as an i2c_adapter.
1044 */
1045struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1046{
1047 return (dev->type == &i2c_adapter_type)
1048 ? to_i2c_adapter(dev)
1049 : NULL;
1050}
1051EXPORT_SYMBOL(i2c_verify_adapter);
1052
Jean Delvare2bb50952009-09-18 22:45:46 +02001053#ifdef CONFIG_I2C_COMPAT
1054static struct class_compat *i2c_adapter_compat_class;
1055#endif
1056
David Brownell9c1600e2007-05-01 23:26:31 +02001057static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1058{
1059 struct i2c_devinfo *devinfo;
1060
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001061 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001062 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1063 if (devinfo->busnum == adapter->nr
1064 && !i2c_new_device(adapter,
1065 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001066 dev_err(&adapter->dev,
1067 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001068 devinfo->board_info.addr);
1069 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001070 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001071}
1072
Jean Delvare69b00892009-12-06 17:06:27 +01001073static int i2c_do_add_adapter(struct i2c_driver *driver,
1074 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001075{
Jean Delvare4735c982008-07-14 22:38:36 +02001076 /* Detect supported devices on that bus, and instantiate them */
1077 i2c_detect(adap, driver);
1078
1079 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001080 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001081 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1082 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001083 dev_warn(&adap->dev,
1084 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001085 /* We ignore the return code; if it fails, too bad */
1086 driver->attach_adapter(adap);
1087 }
1088 return 0;
1089}
1090
Jean Delvare69b00892009-12-06 17:06:27 +01001091static int __process_new_adapter(struct device_driver *d, void *data)
1092{
1093 return i2c_do_add_adapter(to_i2c_driver(d), data);
1094}
1095
Peter Rosind1ed7982016-08-25 23:07:01 +02001096static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1097 .lock_bus = i2c_adapter_lock_bus,
1098 .trylock_bus = i2c_adapter_trylock_bus,
1099 .unlock_bus = i2c_adapter_unlock_bus,
1100};
1101
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001102static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap)
1103{
1104 struct irq_domain *domain = adap->host_notify_domain;
1105 irq_hw_number_t hwirq;
1106
1107 if (!domain)
1108 return;
1109
1110 for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++)
1111 irq_dispose_mapping(irq_find_mapping(domain, hwirq));
1112
1113 irq_domain_remove(domain);
1114 adap->host_notify_domain = NULL;
1115}
1116
1117static int i2c_host_notify_irq_map(struct irq_domain *h,
1118 unsigned int virq,
1119 irq_hw_number_t hw_irq_num)
1120{
1121 irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
1122
1123 return 0;
1124}
1125
1126static const struct irq_domain_ops i2c_host_notify_irq_ops = {
1127 .map = i2c_host_notify_irq_map,
1128};
1129
1130static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap)
1131{
1132 struct irq_domain *domain;
1133
1134 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
1135 return 0;
1136
1137 domain = irq_domain_create_linear(adap->dev.fwnode,
1138 I2C_ADDR_7BITS_COUNT,
1139 &i2c_host_notify_irq_ops, adap);
1140 if (!domain)
1141 return -ENOMEM;
1142
1143 adap->host_notify_domain = domain;
1144
1145 return 0;
1146}
1147
1148/**
1149 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
1150 * I2C client.
1151 * @adap: the adapter
1152 * @addr: the I2C address of the notifying device
1153 * Context: can't sleep
1154 *
1155 * Helper function to be called from an I2C bus driver's interrupt
1156 * handler. It will schedule the Host Notify IRQ.
1157 */
1158int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
1159{
1160 int irq;
1161
1162 if (!adap)
1163 return -EINVAL;
1164
1165 irq = irq_find_mapping(adap->host_notify_domain, addr);
1166 if (irq <= 0)
1167 return -ENXIO;
1168
1169 generic_handle_irq(irq);
1170
1171 return 0;
1172}
1173EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
1174
David Brownell6e13e642007-05-01 23:26:31 +02001175static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001177 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
David Brownell1d0b19c2008-10-14 17:30:05 +02001179 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301180 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001181 res = -EAGAIN;
1182 goto out_list;
1183 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001184
Jean Delvare2236baa2010-11-15 22:40:38 +01001185 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001186 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001187 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001188
1189 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001190 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001191 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01001192 }
1193
Peter Rosind1ed7982016-08-25 23:07:01 +02001194 if (!adap->lock_ops)
1195 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001196
Mika Kuoppala194684e2009-12-06 17:06:22 +01001197 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001198 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001199 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001200 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Jean Delvare8fcfef62009-03-28 21:34:43 +01001202 /* Set default timeout to 1 second if not already set */
1203 if (adap->timeout == 0)
1204 adap->timeout = HZ;
1205
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001206 /* register soft irqs for Host Notify */
1207 res = i2c_setup_host_notify_irq_domain(adap);
1208 if (res) {
1209 pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n",
1210 adap->name, res);
1211 goto out_list;
1212 }
1213
Kay Sievers27d9c182009-01-07 14:29:16 +01001214 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001215 adap->dev.bus = &i2c_bus_type;
1216 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001217 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001218 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001219 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001220 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Phil Reidf8756c62017-08-24 17:31:04 +08001223 res = of_i2c_setup_smbus_alert(adap);
1224 if (res)
1225 goto out_reg;
1226
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001227 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1228
Charles Keepax6ada5c12015-04-16 13:05:19 +01001229 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001230 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001231 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001232
Jean Delvare2bb50952009-09-18 22:45:46 +02001233#ifdef CONFIG_I2C_COMPAT
1234 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1235 adap->dev.parent);
1236 if (res)
1237 dev_warn(&adap->dev,
1238 "Failed to create compatibility class link\n");
1239#endif
1240
Wolfram Sangd3b11d82016-07-09 13:34:59 +09001241 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301242
Jean Delvare729d6dd2009-06-19 16:58:18 +02001243 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001244 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001245 i2c_acpi_register_devices(adap);
1246 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001247
David Brownell6e13e642007-05-01 23:26:31 +02001248 if (adap->nr < __i2c_first_dynamic_bus_num)
1249 i2c_scan_static_board_info(adap);
1250
Jean Delvare4735c982008-07-14 22:38:36 +02001251 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001252 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001253 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001254 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001255
1256 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001257
Phil Reidf8756c62017-08-24 17:31:04 +08001258out_reg:
1259 init_completion(&adap->dev_released);
1260 device_unregister(&adap->dev);
1261 wait_for_completion(&adap->dev_released);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001262out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001263 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001264 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001265 mutex_unlock(&core_lock);
1266 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
David Brownell6e13e642007-05-01 23:26:31 +02001269/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001270 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1271 * @adap: the adapter to register (with adap->nr initialized)
1272 * Context: can sleep
1273 *
1274 * See i2c_add_numbered_adapter() for details.
1275 */
1276static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1277{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001278 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001279
1280 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001281 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001282 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001283 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001284 return id == -ENOSPC ? -EBUSY : id;
1285
1286 return i2c_register_adapter(adap);
1287}
1288
1289/**
David Brownell6e13e642007-05-01 23:26:31 +02001290 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1291 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001292 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001293 *
1294 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001295 * doesn't matter or when its bus number is specified by an dt alias.
1296 * Examples of bases when the bus number doesn't matter: I2C adapters
1297 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001298 *
1299 * When this returns zero, a new bus number was allocated and stored
1300 * in adap->nr, and the specified adapter became available for clients.
1301 * Otherwise, a negative errno value is returned.
1302 */
1303int i2c_add_adapter(struct i2c_adapter *adapter)
1304{
Doug Andersonee5c2742013-03-01 08:57:31 -08001305 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001306 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001307
Doug Andersonee5c2742013-03-01 08:57:31 -08001308 if (dev->of_node) {
1309 id = of_alias_get_id(dev->of_node, "i2c");
1310 if (id >= 0) {
1311 adapter->nr = id;
1312 return __i2c_add_numbered_adapter(adapter);
1313 }
1314 }
1315
Jean Delvarecaada322008-01-27 18:14:49 +01001316 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001317 id = idr_alloc(&i2c_adapter_idr, adapter,
1318 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001319 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001320 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08001321 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001322
1323 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001324
David Brownell6e13e642007-05-01 23:26:31 +02001325 return i2c_register_adapter(adapter);
1326}
1327EXPORT_SYMBOL(i2c_add_adapter);
1328
1329/**
1330 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1331 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001332 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001333 *
1334 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001335 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1336 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001337 * is used to properly configure I2C devices.
1338 *
Grant Likely488bf312011-07-25 17:49:43 +02001339 * If the requested bus number is set to -1, then this function will behave
1340 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1341 *
David Brownell6e13e642007-05-01 23:26:31 +02001342 * If no devices have pre-been declared for this bus, then be sure to
1343 * register the adapter before any dynamically allocated ones. Otherwise
1344 * the required bus ID may not be available.
1345 *
1346 * When this returns zero, the specified adapter became available for
1347 * clients using the bus number provided in adap->nr. Also, the table
1348 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1349 * and the appropriate driver model device nodes are created. Otherwise, a
1350 * negative errno value is returned.
1351 */
1352int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1353{
Grant Likely488bf312011-07-25 17:49:43 +02001354 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1355 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001356
Doug Andersonee5c2742013-03-01 08:57:31 -08001357 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001358}
1359EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1360
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001361static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001362 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001363{
Jean Delvare4735c982008-07-14 22:38:36 +02001364 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001365
Jean Delvareacec2112009-03-28 21:34:40 +01001366 /* Remove the devices we created ourselves as the result of hardware
1367 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001368 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1369 if (client->adapter == adapter) {
1370 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1371 client->name, client->addr);
1372 list_del(&client->detected);
1373 i2c_unregister_device(client);
1374 }
1375 }
Jean Delvare026526f2008-01-27 18:14:49 +01001376}
1377
Jean Delvaree549c2b2009-06-19 16:58:19 +02001378static int __unregister_client(struct device *dev, void *dummy)
1379{
1380 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001381 if (client && strcmp(client->name, "dummy"))
1382 i2c_unregister_device(client);
1383 return 0;
1384}
1385
1386static int __unregister_dummy(struct device *dev, void *dummy)
1387{
1388 struct i2c_client *client = i2c_verify_client(dev);
Andy Shevchenko7b43dd12017-10-31 16:21:35 +02001389 i2c_unregister_device(client);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001390 return 0;
1391}
1392
Jean Delvare69b00892009-12-06 17:06:27 +01001393static int __process_removed_adapter(struct device_driver *d, void *data)
1394{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001395 i2c_do_del_adapter(to_i2c_driver(d), data);
1396 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001397}
1398
David Brownelld64f73b2007-07-12 14:12:28 +02001399/**
1400 * i2c_del_adapter - unregister I2C adapter
1401 * @adap: the adapter being unregistered
1402 * Context: can sleep
1403 *
1404 * This unregisters an I2C adapter which was previously registered
1405 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1406 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001407void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001409 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001410 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
1412 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001413 mutex_lock(&core_lock);
1414 found = idr_find(&i2c_adapter_idr, adap->nr);
1415 mutex_unlock(&core_lock);
1416 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001417 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001418 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
1420
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001421 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001422 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001423 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001424 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001425 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001426 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001428 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001429 mutex_lock_nested(&adap->userspace_clients_lock,
1430 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001431 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1432 detected) {
1433 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1434 client->addr);
1435 list_del(&client->detected);
1436 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001437 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001438 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001439
Jean Delvaree549c2b2009-06-19 16:58:19 +02001440 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001441 * check the returned value. This is a two-pass process, because
1442 * we can't remove the dummy devices during the first pass: they
1443 * could have been instantiated by real devices wishing to clean
1444 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001445 device_for_each_child(&adap->dev, NULL, __unregister_client);
1446 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Jean Delvare2bb50952009-09-18 22:45:46 +02001448#ifdef CONFIG_I2C_COMPAT
1449 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1450 adap->dev.parent);
1451#endif
1452
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001453 /* device name is gone after device_unregister */
1454 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1455
Wolfram Sang9f924162015-12-23 18:19:28 +01001456 pm_runtime_disable(&adap->dev);
1457
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001458 i2c_host_notify_irq_teardown(adap);
1459
Wolfram Sang26680ee2015-01-29 22:45:09 +01001460 /* wait until all references to the device are gone
1461 *
1462 * FIXME: This is old code and should ideally be replaced by an
1463 * alternative which results in decoupling the lifetime of the struct
1464 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301465 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001466 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
David Brownell6e13e642007-05-01 23:26:31 +02001471 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001472 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001474 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001476 /* Clear the device structure in case this adapter is ever going to be
1477 added again */
1478 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
David Brownellc0564602007-05-01 23:26:31 +02001480EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Wolfram Sang54177cc2015-12-17 13:32:36 +01001482/**
1483 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
1484 * @dev: The device to scan for I2C timing properties
1485 * @t: the i2c_timings struct to be filled with values
1486 * @use_defaults: bool to use sane defaults derived from the I2C specification
1487 * when properties are not found, otherwise use 0
1488 *
1489 * Scan the device for the generic I2C properties describing timing parameters
1490 * for the signal and fill the given struct with the results. If a property was
1491 * not found and use_defaults was true, then maximum timings are assumed which
1492 * are derived from the I2C specification. If use_defaults is not used, the
1493 * results will be 0, so drivers can apply their own defaults later. The latter
1494 * is mainly intended for avoiding regressions of existing drivers which want
1495 * to switch to this function. New drivers almost always should use the defaults.
1496 */
1497
1498void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
1499{
1500 int ret;
1501
1502 memset(t, 0, sizeof(*t));
1503
1504 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
1505 if (ret && use_defaults)
1506 t->bus_freq_hz = 100000;
1507
1508 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
1509 if (ret && use_defaults) {
1510 if (t->bus_freq_hz <= 100000)
1511 t->scl_rise_ns = 1000;
1512 else if (t->bus_freq_hz <= 400000)
1513 t->scl_rise_ns = 300;
1514 else
1515 t->scl_rise_ns = 120;
1516 }
1517
1518 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
1519 if (ret && use_defaults) {
1520 if (t->bus_freq_hz <= 400000)
1521 t->scl_fall_ns = 300;
1522 else
1523 t->scl_fall_ns = 120;
1524 }
1525
1526 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
1527
1528 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
1529 if (ret && use_defaults)
1530 t->sda_fall_ns = t->scl_fall_ns;
1531}
1532EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
1533
David Brownell7b4fbc52007-05-01 23:26:30 +02001534/* ------------------------------------------------------------------------- */
1535
Jean Delvare7ae31482011-03-20 14:50:52 +01001536int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1537{
1538 int res;
1539
1540 mutex_lock(&core_lock);
1541 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1542 mutex_unlock(&core_lock);
1543
1544 return res;
1545}
1546EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1547
Jean Delvare69b00892009-12-06 17:06:27 +01001548static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001549{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001550 if (dev->type != &i2c_adapter_type)
1551 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001552 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001553}
1554
David Brownell7b4fbc52007-05-01 23:26:30 +02001555/*
1556 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001557 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 */
1559
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001560int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001562 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
David Brownell1d0b19c2008-10-14 17:30:05 +02001564 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301565 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02001566 return -EAGAIN;
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001569 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02001571 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Jean Delvare729d6dd2009-06-19 16:58:18 +02001573 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001574 * will have called probe() for all matching-but-unbound devices.
1575 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 res = driver_register(&driver->driver);
1577 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001578 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001579
Wolfram Sang44239a52016-07-09 13:35:04 +09001580 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Jean Delvare4735c982008-07-14 22:38:36 +02001582 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001583 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001584
Jean Delvare7eebcb72006-02-05 23:28:21 +01001585 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001587EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Jean Delvare69b00892009-12-06 17:06:27 +01001589static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001590{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001591 if (dev->type == &i2c_adapter_type)
1592 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1593 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001594}
1595
David Brownella1d9e6e2007-05-01 23:26:30 +02001596/**
1597 * i2c_del_driver - unregister I2C driver
1598 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001599 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001600 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001601void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
Jean Delvare7ae31482011-03-20 14:50:52 +01001603 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09001606 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
David Brownellc0564602007-05-01 23:26:31 +02001608EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
David Brownell7b4fbc52007-05-01 23:26:30 +02001610/* ------------------------------------------------------------------------- */
1611
Jean Delvaree48d3312008-01-27 18:14:48 +01001612/**
1613 * i2c_use_client - increments the reference count of the i2c client structure
1614 * @client: the client being referenced
1615 *
1616 * Each live reference to a client should be refcounted. The driver model does
1617 * that automatically as part of driver binding, so that most drivers don't
1618 * need to do this explicitly: they hold a reference until they're unbound
1619 * from the device.
1620 *
1621 * A pointer to the client with the incremented reference counter is returned.
1622 */
1623struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
David Brownell6ea438e2008-07-14 22:38:24 +02001625 if (client && get_device(&client->dev))
1626 return client;
1627 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
David Brownellc0564602007-05-01 23:26:31 +02001629EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Jean Delvaree48d3312008-01-27 18:14:48 +01001631/**
1632 * i2c_release_client - release a use of the i2c client structure
1633 * @client: the client being no longer referenced
1634 *
1635 * Must be called when a user of a client is finished with it.
1636 */
1637void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
David Brownell6ea438e2008-07-14 22:38:24 +02001639 if (client)
1640 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
David Brownellc0564602007-05-01 23:26:31 +02001642EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
David Brownell9b766b82008-01-27 18:14:51 +01001644struct i2c_cmd_arg {
1645 unsigned cmd;
1646 void *arg;
1647};
1648
1649static int i2c_cmd(struct device *dev, void *_arg)
1650{
1651 struct i2c_client *client = i2c_verify_client(dev);
1652 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001653 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001654
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001655 if (!client || !client->dev.driver)
1656 return 0;
1657
1658 driver = to_i2c_driver(client->dev.driver);
1659 if (driver->command)
1660 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001661 return 0;
1662}
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1665{
David Brownell9b766b82008-01-27 18:14:51 +01001666 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
David Brownell9b766b82008-01-27 18:14:51 +01001668 cmd_arg.cmd = cmd;
1669 cmd_arg.arg = arg;
1670 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
David Brownellc0564602007-05-01 23:26:31 +02001672EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674static int __init i2c_init(void)
1675{
1676 int retval;
1677
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001678 retval = of_alias_get_highest_id("i2c");
1679
1680 down_write(&__i2c_board_lock);
1681 if (retval >= __i2c_first_dynamic_bus_num)
1682 __i2c_first_dynamic_bus_num = retval + 1;
1683 up_write(&__i2c_board_lock);
1684
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 retval = bus_register(&i2c_bus_type);
1686 if (retval)
1687 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01001688
1689 is_registered = true;
1690
Jean Delvare2bb50952009-09-18 22:45:46 +02001691#ifdef CONFIG_I2C_COMPAT
1692 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1693 if (!i2c_adapter_compat_class) {
1694 retval = -ENOMEM;
1695 goto bus_err;
1696 }
1697#endif
David Brownelle9f13732008-01-27 18:14:52 +01001698 retval = i2c_add_driver(&dummy_driver);
1699 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001700 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001701
1702 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1703 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001704 if (IS_ENABLED(CONFIG_ACPI))
1705 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001706
David Brownelle9f13732008-01-27 18:14:52 +01001707 return 0;
1708
Jean Delvare2bb50952009-09-18 22:45:46 +02001709class_err:
1710#ifdef CONFIG_I2C_COMPAT
1711 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001712bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001713#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01001714 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01001715 bus_unregister(&i2c_bus_type);
1716 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
1719static void __exit i2c_exit(void)
1720{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001721 if (IS_ENABLED(CONFIG_ACPI))
1722 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001723 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1724 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001725 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001726#ifdef CONFIG_I2C_COMPAT
1727 class_compat_unregister(i2c_adapter_compat_class);
1728#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001730 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
David Brownella10f9e72008-10-14 17:30:06 +02001733/* We must initialize early, because some subsystems register i2c drivers
1734 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1735 */
1736postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737module_exit(i2c_exit);
1738
1739/* ----------------------------------------------------
1740 * the functional interface to the i2c busses.
1741 * ----------------------------------------------------
1742 */
1743
Wolfram Sangb7f62582015-01-05 23:45:59 +01001744/* Check if val is exceeding the quirk IFF quirk is non 0 */
1745#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1746
1747static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
1748{
1749 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
1750 err_msg, msg->addr, msg->len,
1751 msg->flags & I2C_M_RD ? "read" : "write");
1752 return -EOPNOTSUPP;
1753}
1754
1755static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1756{
1757 const struct i2c_adapter_quirks *q = adap->quirks;
1758 int max_num = q->max_num_msgs, i;
1759 bool do_len_check = true;
1760
1761 if (q->flags & I2C_AQ_COMB) {
1762 max_num = 2;
1763
1764 /* special checks for combined messages */
1765 if (num == 2) {
1766 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
1767 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
1768
1769 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
1770 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
1771
1772 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
1773 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
1774
1775 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
1776 return i2c_quirk_error(adap, &msgs[0], "msg too long");
1777
1778 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
1779 return i2c_quirk_error(adap, &msgs[1], "msg too long");
1780
1781 do_len_check = false;
1782 }
1783 }
1784
1785 if (i2c_quirk_exceeded(num, max_num))
1786 return i2c_quirk_error(adap, &msgs[0], "too many messages");
1787
1788 for (i = 0; i < num; i++) {
1789 u16 len = msgs[i].len;
1790
1791 if (msgs[i].flags & I2C_M_RD) {
1792 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
1793 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1794 } else {
1795 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
1796 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1797 }
1798 }
1799
1800 return 0;
1801}
1802
David Brownella1cdeda2008-07-14 22:38:24 +02001803/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001804 * __i2c_transfer - unlocked flavor of i2c_transfer
1805 * @adap: Handle to I2C bus
1806 * @msgs: One or more messages to execute before STOP is issued to
1807 * terminate the operation; each message begins with a START.
1808 * @num: Number of messages to be executed.
1809 *
1810 * Returns negative errno, else the number of messages executed.
1811 *
1812 * Adapter lock must be held when calling this function. No debug logging
1813 * takes place. adap->algo->master_xfer existence isn't checked.
1814 */
1815int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1816{
1817 unsigned long orig_jiffies;
1818 int ret, try;
1819
Wolfram Sangb7f62582015-01-05 23:45:59 +01001820 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
1821 return -EOPNOTSUPP;
1822
David Howellsd9a83d62014-03-06 13:35:59 +00001823 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
1824 * enabled. This is an efficient way of keeping the for-loop from
1825 * being executed when not needed.
1826 */
1827 if (static_key_false(&i2c_trace_msg)) {
1828 int i;
1829 for (i = 0; i < num; i++)
1830 if (msgs[i].flags & I2C_M_RD)
1831 trace_i2c_read(adap, &msgs[i], i);
1832 else
1833 trace_i2c_write(adap, &msgs[i], i);
1834 }
1835
Jean Delvareb37d2a32012-06-29 07:47:19 -03001836 /* Retry automatically on arbitration loss */
1837 orig_jiffies = jiffies;
1838 for (ret = 0, try = 0; try <= adap->retries; try++) {
1839 ret = adap->algo->master_xfer(adap, msgs, num);
1840 if (ret != -EAGAIN)
1841 break;
1842 if (time_after(jiffies, orig_jiffies + adap->timeout))
1843 break;
1844 }
1845
David Howellsd9a83d62014-03-06 13:35:59 +00001846 if (static_key_false(&i2c_trace_msg)) {
1847 int i;
1848 for (i = 0; i < ret; i++)
1849 if (msgs[i].flags & I2C_M_RD)
1850 trace_i2c_reply(adap, &msgs[i], i);
1851 trace_i2c_result(adap, i, ret);
1852 }
1853
Jean Delvareb37d2a32012-06-29 07:47:19 -03001854 return ret;
1855}
1856EXPORT_SYMBOL(__i2c_transfer);
1857
1858/**
David Brownella1cdeda2008-07-14 22:38:24 +02001859 * i2c_transfer - execute a single or combined I2C message
1860 * @adap: Handle to I2C bus
1861 * @msgs: One or more messages to execute before STOP is issued to
1862 * terminate the operation; each message begins with a START.
1863 * @num: Number of messages to be executed.
1864 *
1865 * Returns negative errno, else the number of messages executed.
1866 *
1867 * Note that there is no requirement that each message be sent to
1868 * the same slave address, although that is the most common model.
1869 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001870int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001872 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
David Brownella1cdeda2008-07-14 22:38:24 +02001874 /* REVISIT the fault reporting model here is weak:
1875 *
1876 * - When we get an error after receiving N bytes from a slave,
1877 * there is no way to report "N".
1878 *
1879 * - When we get a NAK after transmitting N bytes to a slave,
1880 * there is no way to report "N" ... or to let the master
1881 * continue executing the rest of this combined message, if
1882 * that's the appropriate response.
1883 *
1884 * - When for example "num" is two and we successfully complete
1885 * the first message but get an error part way through the
1886 * second, it's unclear whether that should be reported as
1887 * one (discarding status on the second message) or errno
1888 * (discarding status on the first one).
1889 */
1890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 if (adap->algo->master_xfer) {
1892#ifdef DEBUG
1893 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001894 dev_dbg(&adap->dev,
1895 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
1896 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
1897 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02001898 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
1900#endif
1901
Mike Rapoportcea443a82008-01-27 18:14:50 +01001902 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02001903 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001904 if (!ret)
1905 /* I2C activity is ongoing. */
1906 return -EAGAIN;
1907 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02001908 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001909 }
1910
Jean Delvareb37d2a32012-06-29 07:47:19 -03001911 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02001912 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 return ret;
1915 } else {
1916 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001917 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
1919}
David Brownellc0564602007-05-01 23:26:31 +02001920EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
David Brownella1cdeda2008-07-14 22:38:24 +02001922/**
Wolfram Sang8a917322017-11-04 21:20:04 +01001923 * i2c_transfer_buffer_flags - issue a single I2C message transferring data
1924 * to/from a buffer
David Brownella1cdeda2008-07-14 22:38:24 +02001925 * @client: Handle to slave device
Wolfram Sang8a917322017-11-04 21:20:04 +01001926 * @buf: Where the data is stored
1927 * @count: How many bytes to transfer, must be less than 64k since msg.len is u16
1928 * @flags: The flags to be used for the message, e.g. I2C_M_RD for reads
David Brownella1cdeda2008-07-14 22:38:24 +02001929 *
Wolfram Sang8a917322017-11-04 21:20:04 +01001930 * Returns negative errno, or else the number of bytes transferred.
David Brownella1cdeda2008-07-14 22:38:24 +02001931 */
Wolfram Sang8a917322017-11-04 21:20:04 +01001932int i2c_transfer_buffer_flags(const struct i2c_client *client, char *buf,
1933 int count, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
1935 int ret;
Wolfram Sang8a917322017-11-04 21:20:04 +01001936 struct i2c_msg msg = {
1937 .addr = client->addr,
1938 .flags = flags | (client->flags & I2C_M_TEN),
1939 .len = count,
1940 .buf = buf,
1941 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Wolfram Sang8a917322017-11-04 21:20:04 +01001943 ret = i2c_transfer(client->adapter, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001945 /*
Wolfram Sang8a917322017-11-04 21:20:04 +01001946 * If everything went ok (i.e. 1 msg transferred), return #bytes
1947 * transferred, else error code.
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001948 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001949 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
Wolfram Sang8a917322017-11-04 21:20:04 +01001951EXPORT_SYMBOL(i2c_transfer_buffer_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953/* ----------------------------------------------------
1954 * the i2c address scanning function
1955 * Will not work for 10-bit addresses!
1956 * ----------------------------------------------------
1957 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001958
Jean Delvare63e4e802010-06-03 11:33:51 +02001959/*
1960 * Legacy default probe function, mostly relevant for SMBus. The default
1961 * probe method is a quick write, but it is known to corrupt the 24RF08
1962 * EEPROMs due to a state machine bug, and could also irreversibly
1963 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1964 * we use a short byte read instead. Also, some bus drivers don't implement
1965 * quick write, so we fallback to a byte read in that case too.
1966 * On x86, there is another special case for FSC hardware monitoring chips,
1967 * which want regular byte reads (address 0x73.) Fortunately, these are the
1968 * only known chips using this I2C address on PC hardware.
1969 * Returns 1 if probe succeeded, 0 if not.
1970 */
1971static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1972{
1973 int err;
1974 union i2c_smbus_data dummy;
1975
1976#ifdef CONFIG_X86
1977 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1978 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1979 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1980 I2C_SMBUS_BYTE_DATA, &dummy);
1981 else
1982#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001983 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1984 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001985 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1986 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02001987 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1988 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1989 I2C_SMBUS_BYTE, &dummy);
1990 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07001991 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
1992 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02001993 err = -EOPNOTSUPP;
1994 }
Jean Delvare63e4e802010-06-03 11:33:51 +02001995
1996 return err >= 0;
1997}
1998
Jean Delvareccfbbd02009-12-06 17:06:25 +01001999static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002000 struct i2c_driver *driver)
2001{
2002 struct i2c_board_info info;
2003 struct i2c_adapter *adapter = temp_client->adapter;
2004 int addr = temp_client->addr;
2005 int err;
2006
2007 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002008 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002009 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002010 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2011 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002012 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002013 }
2014
Wolfram Sang9bccc702015-07-17 14:48:56 +02002015 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002016 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002017 return 0;
2018
Jean Delvareccfbbd02009-12-06 17:06:25 +01002019 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002020 if (!i2c_default_probe(adapter, addr))
2021 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002022
2023 /* Finally call the custom detection function */
2024 memset(&info, 0, sizeof(struct i2c_board_info));
2025 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002026 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002027 if (err) {
2028 /* -ENODEV is returned if the detection fails. We catch it
2029 here as this isn't an error. */
2030 return err == -ENODEV ? 0 : err;
2031 }
2032
2033 /* Consistency check */
2034 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002035 dev_err(&adapter->dev,
2036 "%s detection function provided no name for 0x%x\n",
2037 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002038 } else {
2039 struct i2c_client *client;
2040
2041 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002042 if (adapter->class & I2C_CLASS_DEPRECATED)
2043 dev_warn(&adapter->dev,
2044 "This adapter will soon drop class based instantiation of devices. "
2045 "Please make sure client 0x%02x gets instantiated by other means. "
2046 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2047 info.addr);
2048
Jean Delvare4735c982008-07-14 22:38:36 +02002049 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2050 info.type, info.addr);
2051 client = i2c_new_device(adapter, &info);
2052 if (client)
2053 list_add_tail(&client->detected, &driver->clients);
2054 else
2055 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2056 info.type, info.addr);
2057 }
2058 return 0;
2059}
2060
2061static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2062{
Jean Delvarec3813d62009-12-14 21:17:25 +01002063 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002064 struct i2c_client *temp_client;
2065 int i, err = 0;
2066 int adap_id = i2c_adapter_id(adapter);
2067
Jean Delvarec3813d62009-12-14 21:17:25 +01002068 address_list = driver->address_list;
2069 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002070 return 0;
2071
Wolfram Sang45552272014-07-10 13:46:21 +02002072 /* Warn that the adapter lost class based instantiation */
2073 if (adapter->class == I2C_CLASS_DEPRECATED) {
2074 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002075 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2076 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002077 driver->driver.name);
2078 return 0;
2079 }
2080
Jean Delvare51b54ba2010-10-24 18:16:58 +02002081 /* Stop here if the classes do not match */
2082 if (!(adapter->class & driver->class))
2083 return 0;
2084
Jean Delvare4735c982008-07-14 22:38:36 +02002085 /* Set up a temporary client to help detect callback */
2086 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2087 if (!temp_client)
2088 return -ENOMEM;
2089 temp_client->adapter = adapter;
2090
Jean Delvarec3813d62009-12-14 21:17:25 +01002091 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002092 dev_dbg(&adapter->dev,
2093 "found normal entry for adapter %d, addr 0x%02x\n",
2094 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002095 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002096 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002097 if (unlikely(err))
2098 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002099 }
2100
Jean Delvare4735c982008-07-14 22:38:36 +02002101 kfree(temp_client);
2102 return err;
2103}
2104
Jean Delvared44f19d2010-08-11 18:20:57 +02002105int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2106{
2107 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2108 I2C_SMBUS_QUICK, NULL) >= 0;
2109}
2110EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2111
Jean Delvare12b5053a2007-05-01 23:26:31 +02002112struct i2c_client *
2113i2c_new_probed_device(struct i2c_adapter *adap,
2114 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002115 unsigned short const *addr_list,
2116 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002117{
2118 int i;
2119
Jean Delvare8031d792010-08-11 18:21:00 +02002120 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002121 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002122
Jean Delvare12b5053a2007-05-01 23:26:31 +02002123 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2124 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002125 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002126 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2127 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002128 continue;
2129 }
2130
Wolfram Sang9bccc702015-07-17 14:48:56 +02002131 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002132 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002133 dev_dbg(&adap->dev,
2134 "Address 0x%02x already in use, not probing\n",
2135 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002136 continue;
2137 }
2138
Jean Delvare63e4e802010-06-03 11:33:51 +02002139 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002140 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002141 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002142 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002143
2144 if (addr_list[i] == I2C_CLIENT_END) {
2145 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2146 return NULL;
2147 }
2148
2149 info->addr = addr_list[i];
2150 return i2c_new_device(adap, info);
2151}
2152EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2153
Jean Delvared735b342011-03-20 14:50:52 +01002154struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002157
Jean Delvarecaada322008-01-27 18:14:49 +01002158 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002159 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002160 if (!adapter)
2161 goto exit;
2162
2163 if (try_module_get(adapter->owner))
2164 get_device(&adapter->dev);
2165 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002166 adapter = NULL;
2167
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002168 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002169 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002170 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
David Brownellc0564602007-05-01 23:26:31 +02002172EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
2174void i2c_put_adapter(struct i2c_adapter *adap)
2175{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002176 if (!adap)
2177 return;
2178
2179 put_device(&adap->dev);
2180 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
David Brownellc0564602007-05-01 23:26:31 +02002182EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002184/**
2185 * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg
2186 * @msg: the message to be checked
2187 * @threshold: the minimum number of bytes for which using DMA makes sense
2188 *
2189 * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.
2190 * Or a valid pointer to be used with DMA. After use, release it by
2191 * calling i2c_release_dma_safe_msg_buf().
2192 *
2193 * This function must only be called from process context!
2194 */
2195u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)
2196{
2197 if (msg->len < threshold)
2198 return NULL;
2199
2200 if (msg->flags & I2C_M_DMA_SAFE)
2201 return msg->buf;
2202
2203 pr_debug("using bounce buffer for addr=0x%02x, len=%d\n",
2204 msg->addr, msg->len);
2205
2206 if (msg->flags & I2C_M_RD)
2207 return kzalloc(msg->len, GFP_KERNEL);
2208 else
2209 return kmemdup(msg->buf, msg->len, GFP_KERNEL);
2210}
2211EXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf);
2212
2213/**
2214 * i2c_release_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
2215 * @msg: the message to be synced with
2216 * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.
2217 */
2218void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf)
2219{
2220 if (!buf || buf == msg->buf)
2221 return;
2222
2223 if (msg->flags & I2C_M_RD)
2224 memcpy(msg->buf, buf, msg->len);
2225
2226 kfree(buf);
2227}
2228EXPORT_SYMBOL_GPL(i2c_release_dma_safe_msg_buf);
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2231MODULE_DESCRIPTION("I2C-Bus main module");
2232MODULE_LICENSE("GPL");