blob: aa03eeb438146918a0cbc7e58e6088e4ac0b1eed [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
Peter Rosindde67eb2018-01-22 08:32:01 +010061#define I2C_ADDR_DEVICE_ID 0x7c
62
Wolfram Sang61e3d0f2017-05-23 19:23:11 +020063/*
64 * core_lock protects i2c_adapter_idr, and guarantees that device detection,
65 * deletion of detected devices, and attach_adapter calls are serialized
66 */
Jean Delvarecaada322008-01-27 18:14:49 +010067static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static DEFINE_IDR(i2c_adapter_idr);
69
Jean Delvare4735c982008-07-14 22:38:36 +020070static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010071
David Howellsd9a83d62014-03-06 13:35:59 +000072static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
Sudip Mukherjee95026652016-03-07 17:19:17 +053073static bool is_registered;
David Howellsd9a83d62014-03-06 13:35:59 +000074
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050075int i2c_transfer_trace_reg(void)
David Howellsd9a83d62014-03-06 13:35:59 +000076{
77 static_key_slow_inc(&i2c_trace_msg);
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050078 return 0;
David Howellsd9a83d62014-03-06 13:35:59 +000079}
80
81void i2c_transfer_trace_unreg(void)
82{
83 static_key_slow_dec(&i2c_trace_msg);
84}
85
Lee Jones5f441fc2016-11-07 12:47:40 +000086const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
Jean Delvared2653e92008-04-29 23:11:39 +020087 const struct i2c_client *client)
88{
Lee Jones811073b2016-11-07 12:47:36 +000089 if (!(id && client))
90 return NULL;
91
Jean Delvared2653e92008-04-29 23:11:39 +020092 while (id->name[0]) {
93 if (strcmp(client->name, id->name) == 0)
94 return id;
95 id++;
96 }
97 return NULL;
98}
Lee Jones5f441fc2016-11-07 12:47:40 +000099EXPORT_SYMBOL_GPL(i2c_match_id);
Jean Delvared2653e92008-04-29 23:11:39 +0200100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static int i2c_device_match(struct device *dev, struct device_driver *drv)
102{
Jean Delvare51298d12009-09-18 22:45:45 +0200103 struct i2c_client *client = i2c_verify_client(dev);
104 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200105
Jean Delvare51298d12009-09-18 22:45:45 +0200106
Grant Likely959e85f2010-06-08 07:48:19 -0600107 /* Attempt an OF style match */
Lee Jonesda10c062016-11-07 12:47:39 +0000108 if (i2c_of_match_device(drv->of_match_table, client))
Grant Likely959e85f2010-06-08 07:48:19 -0600109 return 1;
110
Mika Westerberg907ddf82012-11-23 12:23:40 +0100111 /* Then ACPI style match */
112 if (acpi_driver_match_device(dev, drv))
113 return 1;
114
Jean Delvare51298d12009-09-18 22:45:45 +0200115 driver = to_i2c_driver(drv);
Lee Jones811073b2016-11-07 12:47:36 +0000116
117 /* Finally an I2C match */
118 if (i2c_match_id(driver->id_table, client))
119 return 1;
Jean Delvared2653e92008-04-29 23:11:39 +0200120
Jean Delvareeb8a7902008-05-18 20:49:41 +0200121 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Kay Sievers7eff2e72007-08-14 15:15:12 +0200124static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200125{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100126 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800127 int rc;
128
129 rc = acpi_device_uevent_modalias(dev, env);
130 if (rc != -ENODEV)
131 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200132
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100133 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200134}
135
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530136/* i2c bus recovery routines */
137static int get_scl_gpio_value(struct i2c_adapter *adap)
138{
Phil Reid3991c5c2017-11-02 10:40:24 +0800139 return gpiod_get_value_cansleep(adap->bus_recovery_info->scl_gpiod);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530140}
141
142static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
143{
Phil Reid3991c5c2017-11-02 10:40:24 +0800144 gpiod_set_value_cansleep(adap->bus_recovery_info->scl_gpiod, val);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530145}
146
147static int get_sda_gpio_value(struct i2c_adapter *adap)
148{
Phil Reid3991c5c2017-11-02 10:40:24 +0800149 return gpiod_get_value_cansleep(adap->bus_recovery_info->sda_gpiod);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530150}
151
Wolfram Sang80921782018-01-09 14:58:56 +0100152static void set_sda_gpio_value(struct i2c_adapter *adap, int val)
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530153{
Wolfram Sang80921782018-01-09 14:58:56 +0100154 gpiod_set_value_cansleep(adap->bus_recovery_info->sda_gpiod, val);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530155}
156
157/*
158 * We are generating clock pulses. ndelay() determines durating of clk pulses.
159 * We will generate clock with rate 100 KHz and so duration of both clock levels
160 * is: delay in ns = (10^6 / 100) / 2
161 */
162#define RECOVERY_NDELAY 5000
163#define RECOVERY_CLK_CNT 9
164
Phil Reide1eb7d22017-11-02 10:40:30 +0800165int i2c_generic_scl_recovery(struct i2c_adapter *adap)
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530166{
167 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
168 int i = 0, val = 1, ret = 0;
169
170 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300171 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530172
Jan Luebbe8b062602015-07-08 16:35:06 +0200173 bri->set_scl(adap, val);
Wolfram Sang72b08fc2018-01-09 14:58:57 +0100174 if (bri->set_sda)
175 bri->set_sda(adap, 1);
Jan Luebbe8b062602015-07-08 16:35:06 +0200176 ndelay(RECOVERY_NDELAY);
177
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530178 /*
179 * By this time SCL is high, as we need to give 9 falling-rising edges
180 */
181 while (i++ < RECOVERY_CLK_CNT * 2) {
182 if (val) {
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530183 /* SCL shouldn't be low here */
184 if (!bri->get_scl(adap)) {
185 dev_err(&adap->dev,
186 "SCL is stuck low, exit recovery\n");
187 ret = -EBUSY;
188 break;
189 }
Claudio Foellmi1f35b862017-10-05 14:44:14 +0200190 /* Break if SDA is high */
191 if (bri->get_sda && bri->get_sda(adap))
192 break;
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530193 }
194
195 val = !val;
196 bri->set_scl(adap, val);
197 ndelay(RECOVERY_NDELAY);
198 }
199
Claudio Foellmi1f35b862017-10-05 14:44:14 +0200200 /* check if recovery actually succeeded */
201 if (bri->get_sda && !bri->get_sda(adap))
202 ret = -EBUSY;
203
Wolfram Sang2806e6a2018-01-09 14:58:58 +0100204 /* If all went well, send STOP for a sane bus state. */
205 if (ret == 0 && bri->set_sda) {
206 bri->set_scl(adap, 0);
207 ndelay(RECOVERY_NDELAY / 2);
208 bri->set_sda(adap, 0);
209 ndelay(RECOVERY_NDELAY / 2);
210 bri->set_scl(adap, 1);
211 ndelay(RECOVERY_NDELAY / 2);
212 bri->set_sda(adap, 1);
213 ndelay(RECOVERY_NDELAY / 2);
214 }
215
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530216 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300217 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530218
219 return ret;
220}
Mark Brownc1c21f42015-04-15 19:18:39 +0100221EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530222
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530223int i2c_recover_bus(struct i2c_adapter *adap)
224{
225 if (!adap->bus_recovery_info)
226 return -EOPNOTSUPP;
227
228 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
229 return adap->bus_recovery_info->recover_bus(adap);
230}
Mark Brownc1c21f42015-04-15 19:18:39 +0100231EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530232
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900233static void i2c_init_recovery(struct i2c_adapter *adap)
234{
235 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
236 char *err_str;
237
238 if (!bri)
239 return;
240
241 if (!bri->recover_bus) {
242 err_str = "no recover_bus() found";
243 goto err;
244 }
245
Phil Reid3991c5c2017-11-02 10:40:24 +0800246 if (bri->scl_gpiod && bri->recover_bus == i2c_generic_scl_recovery) {
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900247 bri->get_scl = get_scl_gpio_value;
248 bri->set_scl = set_scl_gpio_value;
Wolfram Sang80921782018-01-09 14:58:56 +0100249 if (bri->sda_gpiod) {
Phil Reid3991c5c2017-11-02 10:40:24 +0800250 bri->get_sda = get_sda_gpio_value;
Wolfram Sang80921782018-01-09 14:58:56 +0100251 /* FIXME: add proper flag instead of '0' once available */
252 if (gpiod_get_direction(bri->sda_gpiod) == 0)
253 bri->set_sda = set_sda_gpio_value;
254 }
Phil Reid3991c5c2017-11-02 10:40:24 +0800255 return;
256 }
257
Phil Reide1eb7d22017-11-02 10:40:30 +0800258 if (bri->recover_bus == i2c_generic_scl_recovery) {
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900259 /* Generic SCL recovery */
260 if (!bri->set_scl || !bri->get_scl) {
261 err_str = "no {get|set}_scl() found";
262 goto err;
263 }
264 }
265
266 return;
267 err:
268 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
269 adap->bus_recovery_info = NULL;
270}
271
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200272static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
273{
274 struct i2c_adapter *adap = client->adapter;
275 unsigned int irq;
276
277 if (!adap->host_notify_domain)
278 return -ENXIO;
279
280 if (client->flags & I2C_CLIENT_TEN)
281 return -EINVAL;
282
283 irq = irq_find_mapping(adap->host_notify_domain, client->addr);
284 if (!irq)
285 irq = irq_create_mapping(adap->host_notify_domain,
286 client->addr);
287
288 return irq > 0 ? irq : -ENXIO;
289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291static int i2c_device_probe(struct device *dev)
292{
Jean Delvare51298d12009-09-18 22:45:45 +0200293 struct i2c_client *client = i2c_verify_client(dev);
294 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100295 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200296
Jean Delvare51298d12009-09-18 22:45:45 +0200297 if (!client)
298 return 0;
299
Hans de Goeded1d84bb2017-04-05 00:03:34 +0200300 driver = to_i2c_driver(dev->driver);
301
302 if (!client->irq && !driver->disable_i2c_core_irq_mapping) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300303 int irq = -ENOENT;
304
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800305 if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
306 dev_dbg(dev, "Using Host Notify IRQ\n");
307 irq = i2c_smbus_host_notify_to_irq(client);
308 } else if (dev->of_node) {
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700309 irq = of_irq_get_byname(dev->of_node, "irq");
310 if (irq == -EINVAL || irq == -ENODATA)
311 irq = of_irq_get(dev->of_node, 0);
312 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300313 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700314 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100315 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200316 return irq;
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800317
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100318 if (irq < 0)
319 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200320
321 client->irq = irq;
322 }
323
Lee Jonesda10c062016-11-07 12:47:39 +0000324 /*
Javier Martinez Canillasf4b17a12017-08-09 11:21:28 +0200325 * An I2C ID table is not mandatory, if and only if, a suitable OF
326 * or ACPI ID table is supplied for the probing device.
Lee Jonesda10c062016-11-07 12:47:39 +0000327 */
328 if (!driver->id_table &&
Andy Shevchenkoc64ffff2017-07-17 17:13:28 +0300329 !i2c_acpi_match_device(dev->driver->acpi_match_table, client) &&
Lee Jonesda10c062016-11-07 12:47:39 +0000330 !i2c_of_match_device(dev->driver->of_match_table, client))
David Brownell7b4fbc52007-05-01 23:26:30 +0200331 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200332
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700333 if (client->flags & I2C_CLIENT_WAKE) {
334 int wakeirq = -ENOENT;
335
336 if (dev->of_node) {
337 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
338 if (wakeirq == -EPROBE_DEFER)
339 return wakeirq;
340 }
341
342 device_init_wakeup(&client->dev, true);
343
344 if (wakeirq > 0 && wakeirq != client->irq)
345 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
346 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200347 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700348 else
349 status = 0;
350
351 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300352 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700353 }
354
David Brownell7b4fbc52007-05-01 23:26:30 +0200355 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200356
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200357 status = of_clk_set_defaults(dev->of_node, false);
358 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700359 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200360
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200361 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +0100362 if (status == -EPROBE_DEFER)
363 goto err_clear_wakeup_irq;
364
Lee Jonesb8a1a4c2016-11-07 12:47:41 +0000365 /*
366 * When there are no more users of probe(),
367 * rename probe_new to probe.
368 */
369 if (driver->probe_new)
370 status = driver->probe_new(client);
371 else if (driver->probe)
372 status = driver->probe(client,
373 i2c_match_id(driver->id_table, client));
374 else
375 status = -EINVAL;
376
Kieran Bingham74cedd32015-10-12 21:54:43 +0100377 if (status)
378 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100379
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700380 return 0;
381
382err_detach_pm_domain:
383 dev_pm_domain_detach(&client->dev, true);
384err_clear_wakeup_irq:
385 dev_pm_clear_wake_irq(&client->dev);
386 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +0100387 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390static int i2c_device_remove(struct device *dev)
391{
Jean Delvare51298d12009-09-18 22:45:45 +0200392 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200393 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100394 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200395
Jean Delvare51298d12009-09-18 22:45:45 +0200396 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200397 return 0;
398
399 driver = to_i2c_driver(dev->driver);
400 if (driver->remove) {
401 dev_dbg(dev, "remove\n");
402 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200403 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100404
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200405 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700406
407 dev_pm_clear_wake_irq(&client->dev);
408 device_init_wakeup(&client->dev, false);
409
David Brownella1d9e6e2007-05-01 23:26:30 +0200410 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
David Brownellf37dd802007-02-13 22:09:00 +0100413static void i2c_device_shutdown(struct device *dev)
414{
Jean Delvare51298d12009-09-18 22:45:45 +0200415 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100416 struct i2c_driver *driver;
417
Jean Delvare51298d12009-09-18 22:45:45 +0200418 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100419 return;
420 driver = to_i2c_driver(dev->driver);
421 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200422 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100423}
424
David Brownell9c1600e2007-05-01 23:26:31 +0200425static void i2c_client_dev_release(struct device *dev)
426{
427 kfree(to_i2c_client(dev));
428}
429
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100430static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200431show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200432{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200433 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
434 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200435}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100436static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200437
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100438static ssize_t
439show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200440{
441 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800442 int len;
443
444 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
445 if (len != -ENODEV)
446 return len;
447
Jean Delvareeb8a7902008-05-18 20:49:41 +0200448 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200449}
Jean Delvare51298d12009-09-18 22:45:45 +0200450static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
451
452static struct attribute *i2c_dev_attrs[] = {
453 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200454 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200455 &dev_attr_modalias.attr,
456 NULL
457};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100458ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100459
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200460struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100461 .name = "i2c",
462 .match = i2c_device_match,
463 .probe = i2c_device_probe,
464 .remove = i2c_device_remove,
465 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000466};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200467EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000468
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800469struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100470 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200471 .uevent = i2c_device_uevent,
472 .release = i2c_client_dev_release,
473};
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800474EXPORT_SYMBOL_GPL(i2c_client_type);
Jean Delvare51298d12009-09-18 22:45:45 +0200475
David Brownell9b766b82008-01-27 18:14:51 +0100476
477/**
478 * i2c_verify_client - return parameter as i2c_client, or NULL
479 * @dev: device, probably from some driver model iterator
480 *
481 * When traversing the driver model tree, perhaps using driver model
482 * iterators like @device_for_each_child(), you can't assume very much
483 * about the nodes you find. Use this function to avoid oopses caused
484 * by wrongly treating some non-I2C device as an i2c_client.
485 */
486struct i2c_client *i2c_verify_client(struct device *dev)
487{
Jean Delvare51298d12009-09-18 22:45:45 +0200488 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100489 ? to_i2c_client(dev)
490 : NULL;
491}
492EXPORT_SYMBOL(i2c_verify_client);
493
494
Wolfram Sangda899f52015-05-18 21:09:12 +0200495/* Return a unique address which takes the flags of the client into account */
496static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
497{
498 unsigned short addr = client->addr;
499
500 /* For some client flags, add an arbitrary offset to avoid collisions */
501 if (client->flags & I2C_CLIENT_TEN)
502 addr |= I2C_ADDR_OFFSET_TEN_BIT;
503
504 if (client->flags & I2C_CLIENT_SLAVE)
505 addr |= I2C_ADDR_OFFSET_SLAVE;
506
507 return addr;
508}
509
Jean Delvare3a89db52010-06-03 11:33:52 +0200510/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300511 * are purposely not enforced, except for the general call address. */
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200512int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +0200513{
Wolfram Sangc4019b72015-07-17 12:50:06 +0200514 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +0200515 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200516 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +0200517 return -EINVAL;
518 } else {
519 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200520 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +0200521 return -EINVAL;
522 }
523 return 0;
524}
525
Jean Delvare656b8762010-06-03 11:33:53 +0200526/* And this is a strict address validity check, used when probing. If a
527 * device uses a reserved address, then it shouldn't be probed. 7-bit
528 * addressing is assumed, 10-bit address devices are rare and should be
529 * explicitly enumerated. */
Wolfram Sange4991ec2017-05-23 11:14:17 +0200530int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +0200531{
532 /*
533 * Reserved addresses per I2C specification:
534 * 0x00 General call address / START byte
535 * 0x01 CBUS address
536 * 0x02 Reserved for different bus format
537 * 0x03 Reserved for future purposes
538 * 0x04-0x07 Hs-mode master code
539 * 0x78-0x7b 10-bit slave addressing
540 * 0x7c-0x7f Reserved for future purposes
541 */
542 if (addr < 0x08 || addr > 0x77)
543 return -EINVAL;
544 return 0;
545}
546
Jean Delvare3b5f7942010-06-03 11:33:55 +0200547static int __i2c_check_addr_busy(struct device *dev, void *addrp)
548{
549 struct i2c_client *client = i2c_verify_client(dev);
550 int addr = *(int *)addrp;
551
Wolfram Sang9bccc702015-07-17 14:48:56 +0200552 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +0200553 return -EBUSY;
554 return 0;
555}
556
Michael Lawnick08263742010-08-11 18:21:02 +0200557/* walk up mux tree */
558static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
559{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200560 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200561 int result;
562
563 result = device_for_each_child(&adapter->dev, &addr,
564 __i2c_check_addr_busy);
565
Jean Delvare97cc4d42010-10-24 18:16:57 +0200566 if (!result && parent)
567 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200568
569 return result;
570}
571
572/* recurse down mux tree */
573static int i2c_check_mux_children(struct device *dev, void *addrp)
574{
575 int result;
576
577 if (dev->type == &i2c_adapter_type)
578 result = device_for_each_child(dev, addrp,
579 i2c_check_mux_children);
580 else
581 result = __i2c_check_addr_busy(dev, addrp);
582
583 return result;
584}
585
Jean Delvare3b5f7942010-06-03 11:33:55 +0200586static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
587{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200588 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200589 int result = 0;
590
Jean Delvare97cc4d42010-10-24 18:16:57 +0200591 if (parent)
592 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200593
594 if (!result)
595 result = device_for_each_child(&adapter->dev, &addr,
596 i2c_check_mux_children);
597
598 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200599}
600
David Brownell9c1600e2007-05-01 23:26:31 +0200601/**
Peter Rosin8320f492016-05-04 22:15:27 +0200602 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200603 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200604 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
605 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200606 */
Peter Rosin8320f492016-05-04 22:15:27 +0200607static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
608 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200609{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200610 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200611}
Jean Delvarefe61e072010-08-11 18:20:58 +0200612
613/**
Peter Rosin8320f492016-05-04 22:15:27 +0200614 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200615 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200616 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
617 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200618 */
Peter Rosin8320f492016-05-04 22:15:27 +0200619static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
620 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200621{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200622 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200623}
624
625/**
Peter Rosin8320f492016-05-04 22:15:27 +0200626 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200627 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200628 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
629 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200630 */
Peter Rosin8320f492016-05-04 22:15:27 +0200631static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
632 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200633{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200634 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200635}
Jean Delvarefe61e072010-08-11 18:20:58 +0200636
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200637static void i2c_dev_set_name(struct i2c_adapter *adap,
Hans de Goede728fe6c2017-10-11 11:41:19 +0200638 struct i2c_client *client,
639 struct i2c_board_info const *info)
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200640{
641 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
642
Hans de Goede728fe6c2017-10-11 11:41:19 +0200643 if (info && info->dev_name) {
644 dev_set_name(&client->dev, "i2c-%s", info->dev_name);
645 return;
646 }
647
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200648 if (adev) {
649 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
650 return;
651 }
652
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200653 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +0200654 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200655}
656
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800657static int i2c_dev_irq_from_resources(const struct resource *resources,
658 unsigned int num_resources)
659{
660 struct irq_data *irqd;
661 int i;
662
663 for (i = 0; i < num_resources; i++) {
664 const struct resource *r = &resources[i];
665
666 if (resource_type(r) != IORESOURCE_IRQ)
667 continue;
668
669 if (r->flags & IORESOURCE_BITS) {
670 irqd = irq_get_irq_data(r->start);
671 if (!irqd)
672 break;
673
674 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
675 }
676
677 return r->start;
678 }
679
680 return 0;
681}
682
Jean Delvarefe61e072010-08-11 18:20:58 +0200683/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200684 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200685 * @adap: the adapter managing the device
686 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200687 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200688 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200689 * Create an i2c device. Binding is handled through driver model
690 * probe()/remove() methods. A driver may be bound to this device when we
691 * return from this function, or any later moment (e.g. maybe hotplugging will
692 * load the driver module). This call is not appropriate for use by mainboard
693 * initialization logic, which usually runs during an arch_initcall() long
694 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200695 *
696 * This returns the new i2c client, which may be saved for later use with
697 * i2c_unregister_device(); or NULL to indicate an error.
698 */
699struct i2c_client *
700i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
701{
702 struct i2c_client *client;
703 int status;
704
705 client = kzalloc(sizeof *client, GFP_KERNEL);
706 if (!client)
707 return NULL;
708
709 client->adapter = adap;
710
711 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200712
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200713 if (info->archdata)
714 client->dev.archdata = *info->archdata;
715
Marc Pignatee354252008-08-28 08:33:22 +0200716 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200717 client->addr = info->addr;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800718
David Brownell9c1600e2007-05-01 23:26:31 +0200719 client->irq = info->irq;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800720 if (!client->irq)
721 client->irq = i2c_dev_irq_from_resources(info->resources,
722 info->num_resources);
David Brownell9c1600e2007-05-01 23:26:31 +0200723
David Brownell9c1600e2007-05-01 23:26:31 +0200724 strlcpy(client->name, info->type, sizeof(client->name));
725
Wolfram Sangc4019b72015-07-17 12:50:06 +0200726 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +0200727 if (status) {
728 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
729 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
730 goto out_err_silent;
731 }
732
Jean Delvaref8a227e2009-06-19 16:58:18 +0200733 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +0200734 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200735 if (status)
736 goto out_err;
737
738 client->dev.parent = &client->adapter->dev;
739 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200740 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700741 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100742 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200743
Hans de Goede728fe6c2017-10-11 11:41:19 +0200744 i2c_dev_set_name(adap, client, info);
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800745
746 if (info->properties) {
747 status = device_add_properties(&client->dev, info->properties);
748 if (status) {
749 dev_err(&adap->dev,
750 "Failed to add properties to client %s: %d\n",
751 client->name, status);
752 goto out_err;
753 }
754 }
755
Jean Delvaref8a227e2009-06-19 16:58:18 +0200756 status = device_register(&client->dev);
757 if (status)
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800758 goto out_free_props;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200759
Jean Delvaref8a227e2009-06-19 16:58:18 +0200760 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
761 client->name, dev_name(&client->dev));
762
David Brownell9c1600e2007-05-01 23:26:31 +0200763 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200764
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800765out_free_props:
766 if (info->properties)
767 device_remove_properties(&client->dev);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200768out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300769 dev_err(&adap->dev,
770 "Failed to register i2c client %s at 0x%02x (%d)\n",
771 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200772out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200773 kfree(client);
774 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200775}
776EXPORT_SYMBOL_GPL(i2c_new_device);
777
778
779/**
780 * i2c_unregister_device - reverse effect of i2c_new_device()
781 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200782 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200783 */
784void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200785{
Andy Shevchenko7b43dd12017-10-31 16:21:35 +0200786 if (!client)
787 return;
Lixin Wange0638fa2017-11-27 15:06:55 +0800788
789 if (client->dev.of_node) {
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +0200790 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Lixin Wange0638fa2017-11-27 15:06:55 +0800791 of_node_put(client->dev.of_node);
792 }
793
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300794 if (ACPI_COMPANION(&client->dev))
795 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +0200796 device_unregister(&client->dev);
797}
David Brownell9c1600e2007-05-01 23:26:31 +0200798EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200799
800
Jean Delvare60b129d2008-05-11 20:37:06 +0200801static const struct i2c_device_id dummy_id[] = {
802 { "dummy", 0 },
803 { },
804};
805
Jean Delvared2653e92008-04-29 23:11:39 +0200806static int dummy_probe(struct i2c_client *client,
807 const struct i2c_device_id *id)
808{
809 return 0;
810}
811
812static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100813{
814 return 0;
815}
816
817static struct i2c_driver dummy_driver = {
818 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200819 .probe = dummy_probe,
820 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200821 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100822};
823
824/**
825 * i2c_new_dummy - return a new i2c device bound to a dummy driver
826 * @adapter: the adapter managing the device
827 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100828 * Context: can sleep
829 *
830 * This returns an I2C client bound to the "dummy" driver, intended for use
831 * with devices that consume multiple addresses. Examples of such chips
832 * include various EEPROMS (like 24c04 and 24c08 models).
833 *
834 * These dummy devices have two main uses. First, most I2C and SMBus calls
835 * except i2c_transfer() need a client handle; the dummy will be that handle.
836 * And second, this prevents the specified address from being bound to a
837 * different driver.
838 *
839 * This returns the new i2c client, which should be saved for later use with
840 * i2c_unregister_device(); or NULL to indicate an error.
841 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100842struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100843{
844 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200845 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100846 };
847
David Brownelle9f13732008-01-27 18:14:52 +0100848 return i2c_new_device(adapter, &info);
849}
850EXPORT_SYMBOL_GPL(i2c_new_dummy);
851
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +0100852/**
853 * i2c_new_secondary_device - Helper to get the instantiated secondary address
854 * and create the associated device
855 * @client: Handle to the primary client
856 * @name: Handle to specify which secondary address to get
857 * @default_addr: Used as a fallback if no secondary address was specified
858 * Context: can sleep
859 *
860 * I2C clients can be composed of multiple I2C slaves bound together in a single
861 * component. The I2C client driver then binds to the master I2C slave and needs
862 * to create I2C dummy clients to communicate with all the other slaves.
863 *
864 * This function creates and returns an I2C dummy client whose I2C address is
865 * retrieved from the platform firmware based on the given slave name. If no
866 * address is specified by the firmware default_addr is used.
867 *
868 * On DT-based platforms the address is retrieved from the "reg" property entry
869 * cell whose "reg-names" value matches the slave name.
870 *
871 * This returns the new i2c client, which should be saved for later use with
872 * i2c_unregister_device(); or NULL to indicate an error.
873 */
874struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
875 const char *name,
876 u16 default_addr)
877{
878 struct device_node *np = client->dev.of_node;
879 u32 addr = default_addr;
880 int i;
881
882 if (np) {
883 i = of_property_match_string(np, "reg-names", name);
884 if (i >= 0)
885 of_property_read_u32_index(np, "reg", i, &addr);
886 }
887
888 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
889 return i2c_new_dummy(client->adapter, addr);
890}
891EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
892
David Brownellf37dd802007-02-13 22:09:00 +0100893/* ------------------------------------------------------------------------- */
894
David Brownell16ffadf2007-05-01 23:26:28 +0200895/* I2C bus adapters -- one roots each I2C or SMBUS segment */
896
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200897static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898{
David Brownellef2c83212007-05-01 23:26:28 +0200899 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 complete(&adap->dev_released);
901}
902
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +0200903unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +0200904{
905 unsigned int depth = 0;
906
907 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
908 depth++;
909
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +0200910 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
911 "adapter depth exceeds lockdep subclass limit\n");
912
Jean Delvare390946b2012-09-10 10:14:02 +0200913 return depth;
914}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +0200915EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +0200916
917/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200918 * Let users instantiate I2C devices through sysfs. This can be used when
919 * platform initialization code doesn't contain the proper data for
920 * whatever reason. Also useful for drivers that do device detection and
921 * detection fails, either because the device uses an unexpected address,
922 * or this is a compatible device with different ID register values.
923 *
924 * Parameter checking may look overzealous, but we really don't want
925 * the user to provide incorrect parameters.
926 */
927static ssize_t
928i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
929 const char *buf, size_t count)
930{
931 struct i2c_adapter *adap = to_i2c_adapter(dev);
932 struct i2c_board_info info;
933 struct i2c_client *client;
934 char *blank, end;
935 int res;
936
Jean Delvare99cd8e22009-06-19 16:58:20 +0200937 memset(&info, 0, sizeof(struct i2c_board_info));
938
939 blank = strchr(buf, ' ');
940 if (!blank) {
941 dev_err(dev, "%s: Missing parameters\n", "new_device");
942 return -EINVAL;
943 }
944 if (blank - buf > I2C_NAME_SIZE - 1) {
945 dev_err(dev, "%s: Invalid device name\n", "new_device");
946 return -EINVAL;
947 }
948 memcpy(info.type, buf, blank - buf);
949
950 /* Parse remaining parameters, reject extra parameters */
951 res = sscanf(++blank, "%hi%c", &info.addr, &end);
952 if (res < 1) {
953 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
954 return -EINVAL;
955 }
956 if (res > 1 && end != '\n') {
957 dev_err(dev, "%s: Extra parameters\n", "new_device");
958 return -EINVAL;
959 }
960
Wolfram Sangcfa03272015-07-27 14:03:38 +0200961 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
962 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
963 info.flags |= I2C_CLIENT_TEN;
964 }
965
966 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
967 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
968 info.flags |= I2C_CLIENT_SLAVE;
969 }
970
Jean Delvare99cd8e22009-06-19 16:58:20 +0200971 client = i2c_new_device(adap, &info);
972 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200973 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200974
975 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200976 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200977 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200978 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200979 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
980 info.type, info.addr);
981
982 return count;
983}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100984static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200985
986/*
987 * And of course let the users delete the devices they instantiated, if
988 * they got it wrong. This interface can only be used to delete devices
989 * instantiated by i2c_sysfs_new_device above. This guarantees that we
990 * don't delete devices to which some kernel code still has references.
991 *
992 * Parameter checking may look overzealous, but we really don't want
993 * the user to delete the wrong device.
994 */
995static ssize_t
996i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
997 const char *buf, size_t count)
998{
999 struct i2c_adapter *adap = to_i2c_adapter(dev);
1000 struct i2c_client *client, *next;
1001 unsigned short addr;
1002 char end;
1003 int res;
1004
1005 /* Parse parameters, reject extra parameters */
1006 res = sscanf(buf, "%hi%c", &addr, &end);
1007 if (res < 1) {
1008 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1009 return -EINVAL;
1010 }
1011 if (res > 1 && end != '\n') {
1012 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1013 return -EINVAL;
1014 }
1015
1016 /* Make sure the device was added through sysfs */
1017 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001018 mutex_lock_nested(&adap->userspace_clients_lock,
1019 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001020 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1021 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001022 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001023 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1024 "delete_device", client->name, client->addr);
1025
1026 list_del(&client->detected);
1027 i2c_unregister_device(client);
1028 res = count;
1029 break;
1030 }
1031 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001032 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001033
1034 if (res < 0)
1035 dev_err(dev, "%s: Can't find device in list\n",
1036 "delete_device");
1037 return res;
1038}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001039static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1040 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001041
1042static struct attribute *i2c_adapter_attrs[] = {
1043 &dev_attr_name.attr,
1044 &dev_attr_new_device.attr,
1045 &dev_attr_delete_device.attr,
1046 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001047};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001048ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001049
Michael Lawnick08263742010-08-11 18:21:02 +02001050struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001051 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001052 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001053};
Michael Lawnick08263742010-08-11 18:21:02 +02001054EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Stephen Warren643dd092012-04-17 12:43:33 -06001056/**
1057 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1058 * @dev: device, probably from some driver model iterator
1059 *
1060 * When traversing the driver model tree, perhaps using driver model
1061 * iterators like @device_for_each_child(), you can't assume very much
1062 * about the nodes you find. Use this function to avoid oopses caused
1063 * by wrongly treating some non-I2C device as an i2c_adapter.
1064 */
1065struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1066{
1067 return (dev->type == &i2c_adapter_type)
1068 ? to_i2c_adapter(dev)
1069 : NULL;
1070}
1071EXPORT_SYMBOL(i2c_verify_adapter);
1072
Jean Delvare2bb50952009-09-18 22:45:46 +02001073#ifdef CONFIG_I2C_COMPAT
1074static struct class_compat *i2c_adapter_compat_class;
1075#endif
1076
David Brownell9c1600e2007-05-01 23:26:31 +02001077static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1078{
1079 struct i2c_devinfo *devinfo;
1080
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001081 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001082 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1083 if (devinfo->busnum == adapter->nr
1084 && !i2c_new_device(adapter,
1085 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001086 dev_err(&adapter->dev,
1087 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001088 devinfo->board_info.addr);
1089 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001090 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001091}
1092
Jean Delvare69b00892009-12-06 17:06:27 +01001093static int i2c_do_add_adapter(struct i2c_driver *driver,
1094 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001095{
Jean Delvare4735c982008-07-14 22:38:36 +02001096 /* Detect supported devices on that bus, and instantiate them */
1097 i2c_detect(adap, driver);
1098
1099 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001100 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001101 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1102 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001103 dev_warn(&adap->dev,
1104 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001105 /* We ignore the return code; if it fails, too bad */
1106 driver->attach_adapter(adap);
1107 }
1108 return 0;
1109}
1110
Jean Delvare69b00892009-12-06 17:06:27 +01001111static int __process_new_adapter(struct device_driver *d, void *data)
1112{
1113 return i2c_do_add_adapter(to_i2c_driver(d), data);
1114}
1115
Peter Rosind1ed7982016-08-25 23:07:01 +02001116static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1117 .lock_bus = i2c_adapter_lock_bus,
1118 .trylock_bus = i2c_adapter_trylock_bus,
1119 .unlock_bus = i2c_adapter_unlock_bus,
1120};
1121
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001122static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap)
1123{
1124 struct irq_domain *domain = adap->host_notify_domain;
1125 irq_hw_number_t hwirq;
1126
1127 if (!domain)
1128 return;
1129
1130 for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++)
1131 irq_dispose_mapping(irq_find_mapping(domain, hwirq));
1132
1133 irq_domain_remove(domain);
1134 adap->host_notify_domain = NULL;
1135}
1136
1137static int i2c_host_notify_irq_map(struct irq_domain *h,
1138 unsigned int virq,
1139 irq_hw_number_t hw_irq_num)
1140{
1141 irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
1142
1143 return 0;
1144}
1145
1146static const struct irq_domain_ops i2c_host_notify_irq_ops = {
1147 .map = i2c_host_notify_irq_map,
1148};
1149
1150static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap)
1151{
1152 struct irq_domain *domain;
1153
1154 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
1155 return 0;
1156
1157 domain = irq_domain_create_linear(adap->dev.fwnode,
1158 I2C_ADDR_7BITS_COUNT,
1159 &i2c_host_notify_irq_ops, adap);
1160 if (!domain)
1161 return -ENOMEM;
1162
1163 adap->host_notify_domain = domain;
1164
1165 return 0;
1166}
1167
1168/**
1169 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
1170 * I2C client.
1171 * @adap: the adapter
1172 * @addr: the I2C address of the notifying device
1173 * Context: can't sleep
1174 *
1175 * Helper function to be called from an I2C bus driver's interrupt
1176 * handler. It will schedule the Host Notify IRQ.
1177 */
1178int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
1179{
1180 int irq;
1181
1182 if (!adap)
1183 return -EINVAL;
1184
1185 irq = irq_find_mapping(adap->host_notify_domain, addr);
1186 if (irq <= 0)
1187 return -ENXIO;
1188
1189 generic_handle_irq(irq);
1190
1191 return 0;
1192}
1193EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
1194
David Brownell6e13e642007-05-01 23:26:31 +02001195static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001197 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
David Brownell1d0b19c2008-10-14 17:30:05 +02001199 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301200 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001201 res = -EAGAIN;
1202 goto out_list;
1203 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001204
Jean Delvare2236baa2010-11-15 22:40:38 +01001205 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001206 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001207 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001208
1209 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001210 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001211 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01001212 }
1213
Peter Rosind1ed7982016-08-25 23:07:01 +02001214 if (!adap->lock_ops)
1215 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001216
Mika Kuoppala194684e2009-12-06 17:06:22 +01001217 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001218 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001219 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001220 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Jean Delvare8fcfef62009-03-28 21:34:43 +01001222 /* Set default timeout to 1 second if not already set */
1223 if (adap->timeout == 0)
1224 adap->timeout = HZ;
1225
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001226 /* register soft irqs for Host Notify */
1227 res = i2c_setup_host_notify_irq_domain(adap);
1228 if (res) {
1229 pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n",
1230 adap->name, res);
1231 goto out_list;
1232 }
1233
Kay Sievers27d9c182009-01-07 14:29:16 +01001234 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001235 adap->dev.bus = &i2c_bus_type;
1236 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001237 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001238 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001239 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001240 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Phil Reidf8756c62017-08-24 17:31:04 +08001243 res = of_i2c_setup_smbus_alert(adap);
1244 if (res)
1245 goto out_reg;
1246
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001247 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1248
Charles Keepax6ada5c12015-04-16 13:05:19 +01001249 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001250 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001251 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001252
Jean Delvare2bb50952009-09-18 22:45:46 +02001253#ifdef CONFIG_I2C_COMPAT
1254 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1255 adap->dev.parent);
1256 if (res)
1257 dev_warn(&adap->dev,
1258 "Failed to create compatibility class link\n");
1259#endif
1260
Wolfram Sangd3b11d82016-07-09 13:34:59 +09001261 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301262
Jean Delvare729d6dd2009-06-19 16:58:18 +02001263 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001264 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001265 i2c_acpi_register_devices(adap);
1266 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001267
David Brownell6e13e642007-05-01 23:26:31 +02001268 if (adap->nr < __i2c_first_dynamic_bus_num)
1269 i2c_scan_static_board_info(adap);
1270
Jean Delvare4735c982008-07-14 22:38:36 +02001271 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001272 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001273 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001274 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001275
1276 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001277
Phil Reidf8756c62017-08-24 17:31:04 +08001278out_reg:
1279 init_completion(&adap->dev_released);
1280 device_unregister(&adap->dev);
1281 wait_for_completion(&adap->dev_released);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001282out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001283 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001284 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001285 mutex_unlock(&core_lock);
1286 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
David Brownell6e13e642007-05-01 23:26:31 +02001289/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001290 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1291 * @adap: the adapter to register (with adap->nr initialized)
1292 * Context: can sleep
1293 *
1294 * See i2c_add_numbered_adapter() for details.
1295 */
1296static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1297{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001298 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001299
1300 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001301 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001302 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001303 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001304 return id == -ENOSPC ? -EBUSY : id;
1305
1306 return i2c_register_adapter(adap);
1307}
1308
1309/**
David Brownell6e13e642007-05-01 23:26:31 +02001310 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1311 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001312 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001313 *
1314 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001315 * doesn't matter or when its bus number is specified by an dt alias.
1316 * Examples of bases when the bus number doesn't matter: I2C adapters
1317 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001318 *
1319 * When this returns zero, a new bus number was allocated and stored
1320 * in adap->nr, and the specified adapter became available for clients.
1321 * Otherwise, a negative errno value is returned.
1322 */
1323int i2c_add_adapter(struct i2c_adapter *adapter)
1324{
Doug Andersonee5c2742013-03-01 08:57:31 -08001325 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001326 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001327
Doug Andersonee5c2742013-03-01 08:57:31 -08001328 if (dev->of_node) {
1329 id = of_alias_get_id(dev->of_node, "i2c");
1330 if (id >= 0) {
1331 adapter->nr = id;
1332 return __i2c_add_numbered_adapter(adapter);
1333 }
1334 }
1335
Jean Delvarecaada322008-01-27 18:14:49 +01001336 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001337 id = idr_alloc(&i2c_adapter_idr, adapter,
1338 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001339 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001340 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08001341 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001342
1343 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001344
David Brownell6e13e642007-05-01 23:26:31 +02001345 return i2c_register_adapter(adapter);
1346}
1347EXPORT_SYMBOL(i2c_add_adapter);
1348
1349/**
1350 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1351 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001352 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001353 *
1354 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001355 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1356 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001357 * is used to properly configure I2C devices.
1358 *
Grant Likely488bf312011-07-25 17:49:43 +02001359 * If the requested bus number is set to -1, then this function will behave
1360 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1361 *
David Brownell6e13e642007-05-01 23:26:31 +02001362 * If no devices have pre-been declared for this bus, then be sure to
1363 * register the adapter before any dynamically allocated ones. Otherwise
1364 * the required bus ID may not be available.
1365 *
1366 * When this returns zero, the specified adapter became available for
1367 * clients using the bus number provided in adap->nr. Also, the table
1368 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1369 * and the appropriate driver model device nodes are created. Otherwise, a
1370 * negative errno value is returned.
1371 */
1372int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1373{
Grant Likely488bf312011-07-25 17:49:43 +02001374 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1375 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001376
Doug Andersonee5c2742013-03-01 08:57:31 -08001377 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001378}
1379EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1380
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001381static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001382 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001383{
Jean Delvare4735c982008-07-14 22:38:36 +02001384 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001385
Jean Delvareacec2112009-03-28 21:34:40 +01001386 /* Remove the devices we created ourselves as the result of hardware
1387 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001388 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1389 if (client->adapter == adapter) {
1390 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1391 client->name, client->addr);
1392 list_del(&client->detected);
1393 i2c_unregister_device(client);
1394 }
1395 }
Jean Delvare026526f2008-01-27 18:14:49 +01001396}
1397
Jean Delvaree549c2b2009-06-19 16:58:19 +02001398static int __unregister_client(struct device *dev, void *dummy)
1399{
1400 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001401 if (client && strcmp(client->name, "dummy"))
1402 i2c_unregister_device(client);
1403 return 0;
1404}
1405
1406static int __unregister_dummy(struct device *dev, void *dummy)
1407{
1408 struct i2c_client *client = i2c_verify_client(dev);
Andy Shevchenko7b43dd12017-10-31 16:21:35 +02001409 i2c_unregister_device(client);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001410 return 0;
1411}
1412
Jean Delvare69b00892009-12-06 17:06:27 +01001413static int __process_removed_adapter(struct device_driver *d, void *data)
1414{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001415 i2c_do_del_adapter(to_i2c_driver(d), data);
1416 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001417}
1418
David Brownelld64f73b2007-07-12 14:12:28 +02001419/**
1420 * i2c_del_adapter - unregister I2C adapter
1421 * @adap: the adapter being unregistered
1422 * Context: can sleep
1423 *
1424 * This unregisters an I2C adapter which was previously registered
1425 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1426 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001427void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001429 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001430 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001433 mutex_lock(&core_lock);
1434 found = idr_find(&i2c_adapter_idr, adap->nr);
1435 mutex_unlock(&core_lock);
1436 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001437 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001438 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 }
1440
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001441 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001442 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001443 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001444 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001445 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001446 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001448 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001449 mutex_lock_nested(&adap->userspace_clients_lock,
1450 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001451 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1452 detected) {
1453 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1454 client->addr);
1455 list_del(&client->detected);
1456 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001457 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001458 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001459
Jean Delvaree549c2b2009-06-19 16:58:19 +02001460 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001461 * check the returned value. This is a two-pass process, because
1462 * we can't remove the dummy devices during the first pass: they
1463 * could have been instantiated by real devices wishing to clean
1464 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001465 device_for_each_child(&adap->dev, NULL, __unregister_client);
1466 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Jean Delvare2bb50952009-09-18 22:45:46 +02001468#ifdef CONFIG_I2C_COMPAT
1469 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1470 adap->dev.parent);
1471#endif
1472
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001473 /* device name is gone after device_unregister */
1474 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1475
Wolfram Sang9f924162015-12-23 18:19:28 +01001476 pm_runtime_disable(&adap->dev);
1477
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001478 i2c_host_notify_irq_teardown(adap);
1479
Wolfram Sang26680ee2015-01-29 22:45:09 +01001480 /* wait until all references to the device are gone
1481 *
1482 * FIXME: This is old code and should ideally be replaced by an
1483 * alternative which results in decoupling the lifetime of the struct
1484 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301485 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001486 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
David Brownell6e13e642007-05-01 23:26:31 +02001491 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001492 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001494 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001496 /* Clear the device structure in case this adapter is ever going to be
1497 added again */
1498 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
David Brownellc0564602007-05-01 23:26:31 +02001500EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Wolfram Sang54177cc2015-12-17 13:32:36 +01001502/**
1503 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
1504 * @dev: The device to scan for I2C timing properties
1505 * @t: the i2c_timings struct to be filled with values
1506 * @use_defaults: bool to use sane defaults derived from the I2C specification
1507 * when properties are not found, otherwise use 0
1508 *
1509 * Scan the device for the generic I2C properties describing timing parameters
1510 * for the signal and fill the given struct with the results. If a property was
1511 * not found and use_defaults was true, then maximum timings are assumed which
1512 * are derived from the I2C specification. If use_defaults is not used, the
1513 * results will be 0, so drivers can apply their own defaults later. The latter
1514 * is mainly intended for avoiding regressions of existing drivers which want
1515 * to switch to this function. New drivers almost always should use the defaults.
1516 */
1517
1518void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
1519{
1520 int ret;
1521
1522 memset(t, 0, sizeof(*t));
1523
1524 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
1525 if (ret && use_defaults)
1526 t->bus_freq_hz = 100000;
1527
1528 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
1529 if (ret && use_defaults) {
1530 if (t->bus_freq_hz <= 100000)
1531 t->scl_rise_ns = 1000;
1532 else if (t->bus_freq_hz <= 400000)
1533 t->scl_rise_ns = 300;
1534 else
1535 t->scl_rise_ns = 120;
1536 }
1537
1538 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
1539 if (ret && use_defaults) {
1540 if (t->bus_freq_hz <= 400000)
1541 t->scl_fall_ns = 300;
1542 else
1543 t->scl_fall_ns = 120;
1544 }
1545
1546 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
1547
1548 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
1549 if (ret && use_defaults)
1550 t->sda_fall_ns = t->scl_fall_ns;
1551}
1552EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
1553
David Brownell7b4fbc52007-05-01 23:26:30 +02001554/* ------------------------------------------------------------------------- */
1555
Jean Delvare7ae31482011-03-20 14:50:52 +01001556int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1557{
1558 int res;
1559
1560 mutex_lock(&core_lock);
1561 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1562 mutex_unlock(&core_lock);
1563
1564 return res;
1565}
1566EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1567
Jean Delvare69b00892009-12-06 17:06:27 +01001568static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001569{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001570 if (dev->type != &i2c_adapter_type)
1571 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001572 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001573}
1574
David Brownell7b4fbc52007-05-01 23:26:30 +02001575/*
1576 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001577 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 */
1579
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001580int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001582 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
David Brownell1d0b19c2008-10-14 17:30:05 +02001584 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301585 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02001586 return -EAGAIN;
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001589 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02001591 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Jean Delvare729d6dd2009-06-19 16:58:18 +02001593 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001594 * will have called probe() for all matching-but-unbound devices.
1595 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 res = driver_register(&driver->driver);
1597 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001598 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001599
Wolfram Sang44239a52016-07-09 13:35:04 +09001600 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Jean Delvare4735c982008-07-14 22:38:36 +02001602 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001603 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001604
Jean Delvare7eebcb72006-02-05 23:28:21 +01001605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001607EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Jean Delvare69b00892009-12-06 17:06:27 +01001609static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001610{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001611 if (dev->type == &i2c_adapter_type)
1612 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1613 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001614}
1615
David Brownella1d9e6e2007-05-01 23:26:30 +02001616/**
1617 * i2c_del_driver - unregister I2C driver
1618 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001619 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001620 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001621void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Jean Delvare7ae31482011-03-20 14:50:52 +01001623 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001624
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09001626 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
David Brownellc0564602007-05-01 23:26:31 +02001628EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
David Brownell7b4fbc52007-05-01 23:26:30 +02001630/* ------------------------------------------------------------------------- */
1631
Jean Delvaree48d3312008-01-27 18:14:48 +01001632/**
1633 * i2c_use_client - increments the reference count of the i2c client structure
1634 * @client: the client being referenced
1635 *
1636 * Each live reference to a client should be refcounted. The driver model does
1637 * that automatically as part of driver binding, so that most drivers don't
1638 * need to do this explicitly: they hold a reference until they're unbound
1639 * from the device.
1640 *
1641 * A pointer to the client with the incremented reference counter is returned.
1642 */
1643struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
David Brownell6ea438e2008-07-14 22:38:24 +02001645 if (client && get_device(&client->dev))
1646 return client;
1647 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648}
David Brownellc0564602007-05-01 23:26:31 +02001649EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Jean Delvaree48d3312008-01-27 18:14:48 +01001651/**
1652 * i2c_release_client - release a use of the i2c client structure
1653 * @client: the client being no longer referenced
1654 *
1655 * Must be called when a user of a client is finished with it.
1656 */
1657void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
David Brownell6ea438e2008-07-14 22:38:24 +02001659 if (client)
1660 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
David Brownellc0564602007-05-01 23:26:31 +02001662EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
David Brownell9b766b82008-01-27 18:14:51 +01001664struct i2c_cmd_arg {
1665 unsigned cmd;
1666 void *arg;
1667};
1668
1669static int i2c_cmd(struct device *dev, void *_arg)
1670{
1671 struct i2c_client *client = i2c_verify_client(dev);
1672 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001673 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001674
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001675 if (!client || !client->dev.driver)
1676 return 0;
1677
1678 driver = to_i2c_driver(client->dev.driver);
1679 if (driver->command)
1680 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001681 return 0;
1682}
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1685{
David Brownell9b766b82008-01-27 18:14:51 +01001686 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
David Brownell9b766b82008-01-27 18:14:51 +01001688 cmd_arg.cmd = cmd;
1689 cmd_arg.arg = arg;
1690 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
David Brownellc0564602007-05-01 23:26:31 +02001692EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694static int __init i2c_init(void)
1695{
1696 int retval;
1697
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001698 retval = of_alias_get_highest_id("i2c");
1699
1700 down_write(&__i2c_board_lock);
1701 if (retval >= __i2c_first_dynamic_bus_num)
1702 __i2c_first_dynamic_bus_num = retval + 1;
1703 up_write(&__i2c_board_lock);
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 retval = bus_register(&i2c_bus_type);
1706 if (retval)
1707 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01001708
1709 is_registered = true;
1710
Jean Delvare2bb50952009-09-18 22:45:46 +02001711#ifdef CONFIG_I2C_COMPAT
1712 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1713 if (!i2c_adapter_compat_class) {
1714 retval = -ENOMEM;
1715 goto bus_err;
1716 }
1717#endif
David Brownelle9f13732008-01-27 18:14:52 +01001718 retval = i2c_add_driver(&dummy_driver);
1719 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001720 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001721
1722 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1723 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001724 if (IS_ENABLED(CONFIG_ACPI))
1725 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001726
David Brownelle9f13732008-01-27 18:14:52 +01001727 return 0;
1728
Jean Delvare2bb50952009-09-18 22:45:46 +02001729class_err:
1730#ifdef CONFIG_I2C_COMPAT
1731 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001732bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001733#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01001734 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01001735 bus_unregister(&i2c_bus_type);
1736 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
1739static void __exit i2c_exit(void)
1740{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001741 if (IS_ENABLED(CONFIG_ACPI))
1742 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001743 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1744 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001745 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001746#ifdef CONFIG_I2C_COMPAT
1747 class_compat_unregister(i2c_adapter_compat_class);
1748#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001750 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751}
1752
David Brownella10f9e72008-10-14 17:30:06 +02001753/* We must initialize early, because some subsystems register i2c drivers
1754 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1755 */
1756postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757module_exit(i2c_exit);
1758
1759/* ----------------------------------------------------
1760 * the functional interface to the i2c busses.
1761 * ----------------------------------------------------
1762 */
1763
Wolfram Sangb7f62582015-01-05 23:45:59 +01001764/* Check if val is exceeding the quirk IFF quirk is non 0 */
1765#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1766
1767static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
1768{
1769 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
1770 err_msg, msg->addr, msg->len,
1771 msg->flags & I2C_M_RD ? "read" : "write");
1772 return -EOPNOTSUPP;
1773}
1774
1775static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1776{
1777 const struct i2c_adapter_quirks *q = adap->quirks;
1778 int max_num = q->max_num_msgs, i;
1779 bool do_len_check = true;
1780
1781 if (q->flags & I2C_AQ_COMB) {
1782 max_num = 2;
1783
1784 /* special checks for combined messages */
1785 if (num == 2) {
1786 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
1787 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
1788
1789 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
1790 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
1791
1792 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
1793 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
1794
1795 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
1796 return i2c_quirk_error(adap, &msgs[0], "msg too long");
1797
1798 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
1799 return i2c_quirk_error(adap, &msgs[1], "msg too long");
1800
1801 do_len_check = false;
1802 }
1803 }
1804
1805 if (i2c_quirk_exceeded(num, max_num))
1806 return i2c_quirk_error(adap, &msgs[0], "too many messages");
1807
1808 for (i = 0; i < num; i++) {
1809 u16 len = msgs[i].len;
1810
1811 if (msgs[i].flags & I2C_M_RD) {
1812 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
1813 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1814 } else {
1815 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
1816 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1817 }
1818 }
1819
1820 return 0;
1821}
1822
David Brownella1cdeda2008-07-14 22:38:24 +02001823/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001824 * __i2c_transfer - unlocked flavor of i2c_transfer
1825 * @adap: Handle to I2C bus
1826 * @msgs: One or more messages to execute before STOP is issued to
1827 * terminate the operation; each message begins with a START.
1828 * @num: Number of messages to be executed.
1829 *
1830 * Returns negative errno, else the number of messages executed.
1831 *
1832 * Adapter lock must be held when calling this function. No debug logging
1833 * takes place. adap->algo->master_xfer existence isn't checked.
1834 */
1835int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1836{
1837 unsigned long orig_jiffies;
1838 int ret, try;
1839
Wolfram Sangb7f62582015-01-05 23:45:59 +01001840 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
1841 return -EOPNOTSUPP;
1842
David Howellsd9a83d62014-03-06 13:35:59 +00001843 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
1844 * enabled. This is an efficient way of keeping the for-loop from
1845 * being executed when not needed.
1846 */
1847 if (static_key_false(&i2c_trace_msg)) {
1848 int i;
1849 for (i = 0; i < num; i++)
1850 if (msgs[i].flags & I2C_M_RD)
1851 trace_i2c_read(adap, &msgs[i], i);
1852 else
1853 trace_i2c_write(adap, &msgs[i], i);
1854 }
1855
Jean Delvareb37d2a32012-06-29 07:47:19 -03001856 /* Retry automatically on arbitration loss */
1857 orig_jiffies = jiffies;
1858 for (ret = 0, try = 0; try <= adap->retries; try++) {
1859 ret = adap->algo->master_xfer(adap, msgs, num);
1860 if (ret != -EAGAIN)
1861 break;
1862 if (time_after(jiffies, orig_jiffies + adap->timeout))
1863 break;
1864 }
1865
David Howellsd9a83d62014-03-06 13:35:59 +00001866 if (static_key_false(&i2c_trace_msg)) {
1867 int i;
1868 for (i = 0; i < ret; i++)
1869 if (msgs[i].flags & I2C_M_RD)
1870 trace_i2c_reply(adap, &msgs[i], i);
1871 trace_i2c_result(adap, i, ret);
1872 }
1873
Jean Delvareb37d2a32012-06-29 07:47:19 -03001874 return ret;
1875}
1876EXPORT_SYMBOL(__i2c_transfer);
1877
1878/**
David Brownella1cdeda2008-07-14 22:38:24 +02001879 * i2c_transfer - execute a single or combined I2C message
1880 * @adap: Handle to I2C bus
1881 * @msgs: One or more messages to execute before STOP is issued to
1882 * terminate the operation; each message begins with a START.
1883 * @num: Number of messages to be executed.
1884 *
1885 * Returns negative errno, else the number of messages executed.
1886 *
1887 * Note that there is no requirement that each message be sent to
1888 * the same slave address, although that is the most common model.
1889 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001890int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001892 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
David Brownella1cdeda2008-07-14 22:38:24 +02001894 /* REVISIT the fault reporting model here is weak:
1895 *
1896 * - When we get an error after receiving N bytes from a slave,
1897 * there is no way to report "N".
1898 *
1899 * - When we get a NAK after transmitting N bytes to a slave,
1900 * there is no way to report "N" ... or to let the master
1901 * continue executing the rest of this combined message, if
1902 * that's the appropriate response.
1903 *
1904 * - When for example "num" is two and we successfully complete
1905 * the first message but get an error part way through the
1906 * second, it's unclear whether that should be reported as
1907 * one (discarding status on the second message) or errno
1908 * (discarding status on the first one).
1909 */
1910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 if (adap->algo->master_xfer) {
1912#ifdef DEBUG
1913 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001914 dev_dbg(&adap->dev,
1915 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
1916 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
1917 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02001918 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920#endif
1921
Mike Rapoportcea443a82008-01-27 18:14:50 +01001922 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02001923 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001924 if (!ret)
1925 /* I2C activity is ongoing. */
1926 return -EAGAIN;
1927 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02001928 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001929 }
1930
Jean Delvareb37d2a32012-06-29 07:47:19 -03001931 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02001932 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 return ret;
1935 } else {
1936 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001937 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
1939}
David Brownellc0564602007-05-01 23:26:31 +02001940EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
David Brownella1cdeda2008-07-14 22:38:24 +02001942/**
Wolfram Sang8a917322017-11-04 21:20:04 +01001943 * i2c_transfer_buffer_flags - issue a single I2C message transferring data
1944 * to/from a buffer
David Brownella1cdeda2008-07-14 22:38:24 +02001945 * @client: Handle to slave device
Wolfram Sang8a917322017-11-04 21:20:04 +01001946 * @buf: Where the data is stored
1947 * @count: How many bytes to transfer, must be less than 64k since msg.len is u16
1948 * @flags: The flags to be used for the message, e.g. I2C_M_RD for reads
David Brownella1cdeda2008-07-14 22:38:24 +02001949 *
Wolfram Sang8a917322017-11-04 21:20:04 +01001950 * Returns negative errno, or else the number of bytes transferred.
David Brownella1cdeda2008-07-14 22:38:24 +02001951 */
Wolfram Sang8a917322017-11-04 21:20:04 +01001952int i2c_transfer_buffer_flags(const struct i2c_client *client, char *buf,
1953 int count, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 int ret;
Wolfram Sang8a917322017-11-04 21:20:04 +01001956 struct i2c_msg msg = {
1957 .addr = client->addr,
1958 .flags = flags | (client->flags & I2C_M_TEN),
1959 .len = count,
1960 .buf = buf,
1961 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Wolfram Sang8a917322017-11-04 21:20:04 +01001963 ret = i2c_transfer(client->adapter, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001965 /*
Wolfram Sang8a917322017-11-04 21:20:04 +01001966 * If everything went ok (i.e. 1 msg transferred), return #bytes
1967 * transferred, else error code.
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001968 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001969 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970}
Wolfram Sang8a917322017-11-04 21:20:04 +01001971EXPORT_SYMBOL(i2c_transfer_buffer_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Peter Rosindde67eb2018-01-22 08:32:01 +01001973/**
1974 * i2c_get_device_id - get manufacturer, part id and die revision of a device
1975 * @client: The device to query
1976 * @id: The queried information
1977 *
1978 * Returns negative errno on error, zero on success.
1979 */
1980int i2c_get_device_id(const struct i2c_client *client,
1981 struct i2c_device_identity *id)
1982{
1983 struct i2c_adapter *adap = client->adapter;
1984 union i2c_smbus_data raw_id;
1985 int ret;
1986
1987 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
1988 return -EOPNOTSUPP;
1989
1990 raw_id.block[0] = 3;
1991 ret = i2c_smbus_xfer(adap, I2C_ADDR_DEVICE_ID, 0,
1992 I2C_SMBUS_READ, client->addr << 1,
1993 I2C_SMBUS_I2C_BLOCK_DATA, &raw_id);
1994 if (ret)
1995 return ret;
1996
1997 id->manufacturer_id = (raw_id.block[1] << 4) | (raw_id.block[2] >> 4);
1998 id->part_id = ((raw_id.block[2] & 0xf) << 5) | (raw_id.block[3] >> 3);
1999 id->die_revision = raw_id.block[3] & 0x7;
2000 return 0;
2001}
2002EXPORT_SYMBOL_GPL(i2c_get_device_id);
2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004/* ----------------------------------------------------
2005 * the i2c address scanning function
2006 * Will not work for 10-bit addresses!
2007 * ----------------------------------------------------
2008 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002009
Jean Delvare63e4e802010-06-03 11:33:51 +02002010/*
2011 * Legacy default probe function, mostly relevant for SMBus. The default
2012 * probe method is a quick write, but it is known to corrupt the 24RF08
2013 * EEPROMs due to a state machine bug, and could also irreversibly
2014 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2015 * we use a short byte read instead. Also, some bus drivers don't implement
2016 * quick write, so we fallback to a byte read in that case too.
2017 * On x86, there is another special case for FSC hardware monitoring chips,
2018 * which want regular byte reads (address 0x73.) Fortunately, these are the
2019 * only known chips using this I2C address on PC hardware.
2020 * Returns 1 if probe succeeded, 0 if not.
2021 */
2022static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2023{
2024 int err;
2025 union i2c_smbus_data dummy;
2026
2027#ifdef CONFIG_X86
2028 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2029 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2030 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2031 I2C_SMBUS_BYTE_DATA, &dummy);
2032 else
2033#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002034 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2035 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002036 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2037 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002038 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2039 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2040 I2C_SMBUS_BYTE, &dummy);
2041 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002042 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2043 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002044 err = -EOPNOTSUPP;
2045 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002046
2047 return err >= 0;
2048}
2049
Jean Delvareccfbbd02009-12-06 17:06:25 +01002050static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002051 struct i2c_driver *driver)
2052{
2053 struct i2c_board_info info;
2054 struct i2c_adapter *adapter = temp_client->adapter;
2055 int addr = temp_client->addr;
2056 int err;
2057
2058 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002059 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002060 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002061 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2062 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002063 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002064 }
2065
Wolfram Sang9bccc702015-07-17 14:48:56 +02002066 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002067 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002068 return 0;
2069
Jean Delvareccfbbd02009-12-06 17:06:25 +01002070 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002071 if (!i2c_default_probe(adapter, addr))
2072 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002073
2074 /* Finally call the custom detection function */
2075 memset(&info, 0, sizeof(struct i2c_board_info));
2076 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002077 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002078 if (err) {
2079 /* -ENODEV is returned if the detection fails. We catch it
2080 here as this isn't an error. */
2081 return err == -ENODEV ? 0 : err;
2082 }
2083
2084 /* Consistency check */
2085 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002086 dev_err(&adapter->dev,
2087 "%s detection function provided no name for 0x%x\n",
2088 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002089 } else {
2090 struct i2c_client *client;
2091
2092 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002093 if (adapter->class & I2C_CLASS_DEPRECATED)
2094 dev_warn(&adapter->dev,
2095 "This adapter will soon drop class based instantiation of devices. "
2096 "Please make sure client 0x%02x gets instantiated by other means. "
2097 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2098 info.addr);
2099
Jean Delvare4735c982008-07-14 22:38:36 +02002100 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2101 info.type, info.addr);
2102 client = i2c_new_device(adapter, &info);
2103 if (client)
2104 list_add_tail(&client->detected, &driver->clients);
2105 else
2106 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2107 info.type, info.addr);
2108 }
2109 return 0;
2110}
2111
2112static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2113{
Jean Delvarec3813d62009-12-14 21:17:25 +01002114 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002115 struct i2c_client *temp_client;
2116 int i, err = 0;
2117 int adap_id = i2c_adapter_id(adapter);
2118
Jean Delvarec3813d62009-12-14 21:17:25 +01002119 address_list = driver->address_list;
2120 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002121 return 0;
2122
Wolfram Sang45552272014-07-10 13:46:21 +02002123 /* Warn that the adapter lost class based instantiation */
2124 if (adapter->class == I2C_CLASS_DEPRECATED) {
2125 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002126 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2127 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002128 driver->driver.name);
2129 return 0;
2130 }
2131
Jean Delvare51b54ba2010-10-24 18:16:58 +02002132 /* Stop here if the classes do not match */
2133 if (!(adapter->class & driver->class))
2134 return 0;
2135
Jean Delvare4735c982008-07-14 22:38:36 +02002136 /* Set up a temporary client to help detect callback */
2137 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2138 if (!temp_client)
2139 return -ENOMEM;
2140 temp_client->adapter = adapter;
2141
Jean Delvarec3813d62009-12-14 21:17:25 +01002142 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002143 dev_dbg(&adapter->dev,
2144 "found normal entry for adapter %d, addr 0x%02x\n",
2145 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002146 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002147 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002148 if (unlikely(err))
2149 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002150 }
2151
Jean Delvare4735c982008-07-14 22:38:36 +02002152 kfree(temp_client);
2153 return err;
2154}
2155
Jean Delvared44f19d2010-08-11 18:20:57 +02002156int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2157{
2158 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2159 I2C_SMBUS_QUICK, NULL) >= 0;
2160}
2161EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2162
Jean Delvare12b5053a2007-05-01 23:26:31 +02002163struct i2c_client *
2164i2c_new_probed_device(struct i2c_adapter *adap,
2165 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002166 unsigned short const *addr_list,
2167 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002168{
2169 int i;
2170
Jean Delvare8031d792010-08-11 18:21:00 +02002171 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002172 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002173
Jean Delvare12b5053a2007-05-01 23:26:31 +02002174 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2175 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002176 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002177 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2178 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002179 continue;
2180 }
2181
Wolfram Sang9bccc702015-07-17 14:48:56 +02002182 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002183 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002184 dev_dbg(&adap->dev,
2185 "Address 0x%02x already in use, not probing\n",
2186 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002187 continue;
2188 }
2189
Jean Delvare63e4e802010-06-03 11:33:51 +02002190 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002191 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002192 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002193 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002194
2195 if (addr_list[i] == I2C_CLIENT_END) {
2196 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2197 return NULL;
2198 }
2199
2200 info->addr = addr_list[i];
2201 return i2c_new_device(adap, info);
2202}
2203EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2204
Jean Delvared735b342011-03-20 14:50:52 +01002205struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002208
Jean Delvarecaada322008-01-27 18:14:49 +01002209 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002210 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002211 if (!adapter)
2212 goto exit;
2213
2214 if (try_module_get(adapter->owner))
2215 get_device(&adapter->dev);
2216 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002217 adapter = NULL;
2218
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002219 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002220 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002221 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
David Brownellc0564602007-05-01 23:26:31 +02002223EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
2225void i2c_put_adapter(struct i2c_adapter *adap)
2226{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002227 if (!adap)
2228 return;
2229
2230 put_device(&adap->dev);
2231 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
David Brownellc0564602007-05-01 23:26:31 +02002233EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002235/**
2236 * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg
2237 * @msg: the message to be checked
2238 * @threshold: the minimum number of bytes for which using DMA makes sense
2239 *
2240 * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.
2241 * Or a valid pointer to be used with DMA. After use, release it by
2242 * calling i2c_release_dma_safe_msg_buf().
2243 *
2244 * This function must only be called from process context!
2245 */
2246u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)
2247{
2248 if (msg->len < threshold)
2249 return NULL;
2250
2251 if (msg->flags & I2C_M_DMA_SAFE)
2252 return msg->buf;
2253
2254 pr_debug("using bounce buffer for addr=0x%02x, len=%d\n",
2255 msg->addr, msg->len);
2256
2257 if (msg->flags & I2C_M_RD)
2258 return kzalloc(msg->len, GFP_KERNEL);
2259 else
2260 return kmemdup(msg->buf, msg->len, GFP_KERNEL);
2261}
2262EXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf);
2263
2264/**
2265 * i2c_release_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
2266 * @msg: the message to be synced with
2267 * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.
2268 */
2269void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf)
2270{
2271 if (!buf || buf == msg->buf)
2272 return;
2273
2274 if (msg->flags & I2C_M_RD)
2275 memcpy(msg->buf, buf, msg->len);
2276
2277 kfree(buf);
2278}
2279EXPORT_SYMBOL_GPL(i2c_release_dma_safe_msg_buf);
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2282MODULE_DESCRIPTION("I2C-Bus main module");
2283MODULE_LICENSE("GPL");