blob: 23f353630db32fdb25bb705334629ac86544fad5 [file] [log] [blame]
Wolfram Sang61e3d0f2017-05-23 19:23:11 +02001/*
2 * Linux I2C core
3 *
4 * Copyright (C) 1995-99 Simon G. Vogl
5 * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>
6 * Mux support by Rodolfo Giometti <giometti@enneenne.com> and
7 * Michael Lawnick <michael.lawnick.ext@nsn.com>
8 *
9 * Copyright (C) 2013-2017 Wolfram Sang <wsa@the-dreams.de>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
Wolfram Sang687b81d2013-07-11 12:56:15 +010019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Wolfram Sang44239a52016-07-09 13:35:04 +090021#define pr_fmt(fmt) "i2c-core: " fmt
22
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +020023#include <dt-bindings/i2c/i2c.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010024#include <linux/acpi.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020025#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010026#include <linux/completion.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010027#include <linux/delay.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020028#include <linux/err.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010029#include <linux/errno.h>
Phil Reid10c9ef02017-11-28 11:09:10 +080030#include <linux/gpio/consumer.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010031#include <linux/i2c.h>
Phil Reidf8756c62017-08-24 17:31:04 +080032#include <linux/i2c-smbus.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010033#include <linux/idr.h>
34#include <linux/init.h>
35#include <linux/irqflags.h>
36#include <linux/jump_label.h>
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/mutex.h>
40#include <linux/of_device.h>
41#include <linux/of.h>
42#include <linux/of_irq.h>
43#include <linux/pm_domain.h>
44#include <linux/pm_runtime.h>
45#include <linux/pm_wakeirq.h>
Wolfram Sange1dba012015-12-08 10:37:46 +010046#include <linux/property.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010047#include <linux/rwsem.h>
48#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
David Brownell9c1600e2007-05-01 23:26:31 +020050#include "i2c-core.h"
51
David Howellsd9a83d62014-03-06 13:35:59 +000052#define CREATE_TRACE_POINTS
53#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Wolfram Sangda899f52015-05-18 21:09:12 +020055#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
56#define I2C_ADDR_OFFSET_SLAVE 0x1000
57
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +020058#define I2C_ADDR_7BITS_MAX 0x77
59#define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1)
60
Wolfram Sang61e3d0f2017-05-23 19:23:11 +020061/*
62 * core_lock protects i2c_adapter_idr, and guarantees that device detection,
63 * deletion of detected devices, and attach_adapter calls are serialized
64 */
Jean Delvarecaada322008-01-27 18:14:49 +010065static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static DEFINE_IDR(i2c_adapter_idr);
67
Jean Delvare4735c982008-07-14 22:38:36 +020068static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010069
David Howellsd9a83d62014-03-06 13:35:59 +000070static struct static_key i2c_trace_msg = STATIC_KEY_INIT_FALSE;
Sudip Mukherjee95026652016-03-07 17:19:17 +053071static bool is_registered;
David Howellsd9a83d62014-03-06 13:35:59 +000072
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050073int i2c_transfer_trace_reg(void)
David Howellsd9a83d62014-03-06 13:35:59 +000074{
75 static_key_slow_inc(&i2c_trace_msg);
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050076 return 0;
David Howellsd9a83d62014-03-06 13:35:59 +000077}
78
79void i2c_transfer_trace_unreg(void)
80{
81 static_key_slow_dec(&i2c_trace_msg);
82}
83
Lee Jones5f441fc2016-11-07 12:47:40 +000084const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
Jean Delvared2653e92008-04-29 23:11:39 +020085 const struct i2c_client *client)
86{
Lee Jones811073b2016-11-07 12:47:36 +000087 if (!(id && client))
88 return NULL;
89
Jean Delvared2653e92008-04-29 23:11:39 +020090 while (id->name[0]) {
91 if (strcmp(client->name, id->name) == 0)
92 return id;
93 id++;
94 }
95 return NULL;
96}
Lee Jones5f441fc2016-11-07 12:47:40 +000097EXPORT_SYMBOL_GPL(i2c_match_id);
Jean Delvared2653e92008-04-29 23:11:39 +020098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static int i2c_device_match(struct device *dev, struct device_driver *drv)
100{
Jean Delvare51298d12009-09-18 22:45:45 +0200101 struct i2c_client *client = i2c_verify_client(dev);
102 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200103
Jean Delvare51298d12009-09-18 22:45:45 +0200104
Grant Likely959e85f2010-06-08 07:48:19 -0600105 /* Attempt an OF style match */
Lee Jonesda10c062016-11-07 12:47:39 +0000106 if (i2c_of_match_device(drv->of_match_table, client))
Grant Likely959e85f2010-06-08 07:48:19 -0600107 return 1;
108
Mika Westerberg907ddf82012-11-23 12:23:40 +0100109 /* Then ACPI style match */
110 if (acpi_driver_match_device(dev, drv))
111 return 1;
112
Jean Delvare51298d12009-09-18 22:45:45 +0200113 driver = to_i2c_driver(drv);
Lee Jones811073b2016-11-07 12:47:36 +0000114
115 /* Finally an I2C match */
116 if (i2c_match_id(driver->id_table, client))
117 return 1;
Jean Delvared2653e92008-04-29 23:11:39 +0200118
Jean Delvareeb8a7902008-05-18 20:49:41 +0200119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Kay Sievers7eff2e72007-08-14 15:15:12 +0200122static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200123{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100124 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800125 int rc;
126
127 rc = acpi_device_uevent_modalias(dev, env);
128 if (rc != -ENODEV)
129 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200130
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100131 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200132}
133
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530134/* i2c bus recovery routines */
135static int get_scl_gpio_value(struct i2c_adapter *adap)
136{
Phil Reid3991c5c2017-11-02 10:40:24 +0800137 return gpiod_get_value_cansleep(adap->bus_recovery_info->scl_gpiod);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530138}
139
140static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
141{
Phil Reid3991c5c2017-11-02 10:40:24 +0800142 gpiod_set_value_cansleep(adap->bus_recovery_info->scl_gpiod, val);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530143}
144
145static int get_sda_gpio_value(struct i2c_adapter *adap)
146{
Phil Reid3991c5c2017-11-02 10:40:24 +0800147 return gpiod_get_value_cansleep(adap->bus_recovery_info->sda_gpiod);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530148}
149
Wolfram Sang80921782018-01-09 14:58:56 +0100150static void set_sda_gpio_value(struct i2c_adapter *adap, int val)
151{
152 gpiod_set_value_cansleep(adap->bus_recovery_info->sda_gpiod, val);
153}
154
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530155/*
156 * We are generating clock pulses. ndelay() determines durating of clk pulses.
157 * We will generate clock with rate 100 KHz and so duration of both clock levels
158 * is: delay in ns = (10^6 / 100) / 2
159 */
160#define RECOVERY_NDELAY 5000
161#define RECOVERY_CLK_CNT 9
162
Phil Reide1eb7d22017-11-02 10:40:30 +0800163int i2c_generic_scl_recovery(struct i2c_adapter *adap)
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530164{
165 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
166 int i = 0, val = 1, ret = 0;
167
168 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300169 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530170
Jan Luebbe8b062602015-07-08 16:35:06 +0200171 bri->set_scl(adap, val);
Wolfram Sang72b08fc2018-01-09 14:58:57 +0100172 if (bri->set_sda)
173 bri->set_sda(adap, 1);
Jan Luebbe8b062602015-07-08 16:35:06 +0200174 ndelay(RECOVERY_NDELAY);
175
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530176 /*
177 * By this time SCL is high, as we need to give 9 falling-rising edges
178 */
179 while (i++ < RECOVERY_CLK_CNT * 2) {
180 if (val) {
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530181 /* SCL shouldn't be low here */
182 if (!bri->get_scl(adap)) {
183 dev_err(&adap->dev,
184 "SCL is stuck low, exit recovery\n");
185 ret = -EBUSY;
186 break;
187 }
Claudio Foellmi1f35b862017-10-05 14:44:14 +0200188 /* Break if SDA is high */
189 if (bri->get_sda && bri->get_sda(adap))
190 break;
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530191 }
192
193 val = !val;
194 bri->set_scl(adap, val);
195 ndelay(RECOVERY_NDELAY);
196 }
197
Claudio Foellmi1f35b862017-10-05 14:44:14 +0200198 /* check if recovery actually succeeded */
199 if (bri->get_sda && !bri->get_sda(adap))
200 ret = -EBUSY;
201
Wolfram Sang2806e6a2018-01-09 14:58:58 +0100202 /* If all went well, send STOP for a sane bus state. */
203 if (ret == 0 && bri->set_sda) {
204 bri->set_scl(adap, 0);
205 ndelay(RECOVERY_NDELAY / 2);
206 bri->set_sda(adap, 0);
207 ndelay(RECOVERY_NDELAY / 2);
208 bri->set_scl(adap, 1);
209 ndelay(RECOVERY_NDELAY / 2);
210 bri->set_sda(adap, 1);
211 ndelay(RECOVERY_NDELAY / 2);
212 }
213
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530214 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300215 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530216
217 return ret;
218}
Mark Brownc1c21f42015-04-15 19:18:39 +0100219EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530220
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530221int i2c_recover_bus(struct i2c_adapter *adap)
222{
223 if (!adap->bus_recovery_info)
224 return -EOPNOTSUPP;
225
226 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
227 return adap->bus_recovery_info->recover_bus(adap);
228}
Mark Brownc1c21f42015-04-15 19:18:39 +0100229EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530230
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900231static void i2c_init_recovery(struct i2c_adapter *adap)
232{
233 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
234 char *err_str;
235
236 if (!bri)
237 return;
238
239 if (!bri->recover_bus) {
240 err_str = "no recover_bus() found";
241 goto err;
242 }
243
Phil Reid3991c5c2017-11-02 10:40:24 +0800244 if (bri->scl_gpiod && bri->recover_bus == i2c_generic_scl_recovery) {
245 bri->get_scl = get_scl_gpio_value;
246 bri->set_scl = set_scl_gpio_value;
Wolfram Sang80921782018-01-09 14:58:56 +0100247 if (bri->sda_gpiod) {
Phil Reid3991c5c2017-11-02 10:40:24 +0800248 bri->get_sda = get_sda_gpio_value;
Wolfram Sang80921782018-01-09 14:58:56 +0100249 /* FIXME: add proper flag instead of '0' once available */
250 if (gpiod_get_direction(bri->sda_gpiod) == 0)
251 bri->set_sda = set_sda_gpio_value;
252 }
Phil Reid3991c5c2017-11-02 10:40:24 +0800253 return;
254 }
255
Phil Reide1eb7d22017-11-02 10:40:30 +0800256 if (bri->recover_bus == i2c_generic_scl_recovery) {
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900257 /* Generic SCL recovery */
258 if (!bri->set_scl || !bri->get_scl) {
259 err_str = "no {get|set}_scl() found";
260 goto err;
261 }
262 }
263
264 return;
265 err:
266 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
267 adap->bus_recovery_info = NULL;
268}
269
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200270static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
271{
272 struct i2c_adapter *adap = client->adapter;
273 unsigned int irq;
274
275 if (!adap->host_notify_domain)
276 return -ENXIO;
277
278 if (client->flags & I2C_CLIENT_TEN)
279 return -EINVAL;
280
281 irq = irq_find_mapping(adap->host_notify_domain, client->addr);
282 if (!irq)
283 irq = irq_create_mapping(adap->host_notify_domain,
284 client->addr);
285
286 return irq > 0 ? irq : -ENXIO;
287}
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static int i2c_device_probe(struct device *dev)
290{
Jean Delvare51298d12009-09-18 22:45:45 +0200291 struct i2c_client *client = i2c_verify_client(dev);
292 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100293 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200294
Jean Delvare51298d12009-09-18 22:45:45 +0200295 if (!client)
296 return 0;
297
Hans de Goeded1d84bb2017-04-05 00:03:34 +0200298 driver = to_i2c_driver(dev->driver);
299
300 if (!client->irq && !driver->disable_i2c_core_irq_mapping) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300301 int irq = -ENOENT;
302
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800303 if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
304 dev_dbg(dev, "Using Host Notify IRQ\n");
305 irq = i2c_smbus_host_notify_to_irq(client);
306 } else if (dev->of_node) {
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700307 irq = of_irq_get_byname(dev->of_node, "irq");
308 if (irq == -EINVAL || irq == -ENODATA)
309 irq = of_irq_get(dev->of_node, 0);
310 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300311 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700312 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100313 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200314 return irq;
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800315
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100316 if (irq < 0)
317 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200318
319 client->irq = irq;
320 }
321
Lee Jonesda10c062016-11-07 12:47:39 +0000322 /*
Javier Martinez Canillasf4b17a12017-08-09 11:21:28 +0200323 * An I2C ID table is not mandatory, if and only if, a suitable OF
324 * or ACPI ID table is supplied for the probing device.
Lee Jonesda10c062016-11-07 12:47:39 +0000325 */
326 if (!driver->id_table &&
Andy Shevchenkoc64ffff2017-07-17 17:13:28 +0300327 !i2c_acpi_match_device(dev->driver->acpi_match_table, client) &&
Lee Jonesda10c062016-11-07 12:47:39 +0000328 !i2c_of_match_device(dev->driver->of_match_table, client))
David Brownell7b4fbc52007-05-01 23:26:30 +0200329 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200330
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700331 if (client->flags & I2C_CLIENT_WAKE) {
332 int wakeirq = -ENOENT;
333
334 if (dev->of_node) {
335 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
336 if (wakeirq == -EPROBE_DEFER)
337 return wakeirq;
338 }
339
340 device_init_wakeup(&client->dev, true);
341
342 if (wakeirq > 0 && wakeirq != client->irq)
343 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
344 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200345 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700346 else
347 status = 0;
348
349 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300350 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700351 }
352
David Brownell7b4fbc52007-05-01 23:26:30 +0200353 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200354
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200355 status = of_clk_set_defaults(dev->of_node, false);
356 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700357 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200358
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200359 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +0100360 if (status == -EPROBE_DEFER)
361 goto err_clear_wakeup_irq;
362
Lee Jonesb8a1a4c2016-11-07 12:47:41 +0000363 /*
364 * When there are no more users of probe(),
365 * rename probe_new to probe.
366 */
367 if (driver->probe_new)
368 status = driver->probe_new(client);
369 else if (driver->probe)
370 status = driver->probe(client,
371 i2c_match_id(driver->id_table, client));
372 else
373 status = -EINVAL;
374
Kieran Bingham74cedd32015-10-12 21:54:43 +0100375 if (status)
376 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100377
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700378 return 0;
379
380err_detach_pm_domain:
381 dev_pm_domain_detach(&client->dev, true);
382err_clear_wakeup_irq:
383 dev_pm_clear_wake_irq(&client->dev);
384 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +0100385 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388static int i2c_device_remove(struct device *dev)
389{
Jean Delvare51298d12009-09-18 22:45:45 +0200390 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200391 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100392 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200393
Jean Delvare51298d12009-09-18 22:45:45 +0200394 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200395 return 0;
396
397 driver = to_i2c_driver(dev->driver);
398 if (driver->remove) {
399 dev_dbg(dev, "remove\n");
400 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200401 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100402
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200403 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700404
405 dev_pm_clear_wake_irq(&client->dev);
406 device_init_wakeup(&client->dev, false);
407
David Brownella1d9e6e2007-05-01 23:26:30 +0200408 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
David Brownellf37dd802007-02-13 22:09:00 +0100411static void i2c_device_shutdown(struct device *dev)
412{
Jean Delvare51298d12009-09-18 22:45:45 +0200413 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100414 struct i2c_driver *driver;
415
Jean Delvare51298d12009-09-18 22:45:45 +0200416 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100417 return;
418 driver = to_i2c_driver(dev->driver);
419 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200420 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100421}
422
David Brownell9c1600e2007-05-01 23:26:31 +0200423static void i2c_client_dev_release(struct device *dev)
424{
425 kfree(to_i2c_client(dev));
426}
427
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100428static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200429show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200430{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200431 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
432 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200433}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100434static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200435
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100436static ssize_t
437show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200438{
439 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800440 int len;
441
442 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
443 if (len != -ENODEV)
444 return len;
445
Jean Delvareeb8a7902008-05-18 20:49:41 +0200446 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200447}
Jean Delvare51298d12009-09-18 22:45:45 +0200448static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
449
450static struct attribute *i2c_dev_attrs[] = {
451 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200452 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200453 &dev_attr_modalias.attr,
454 NULL
455};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100456ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100457
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200458struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100459 .name = "i2c",
460 .match = i2c_device_match,
461 .probe = i2c_device_probe,
462 .remove = i2c_device_remove,
463 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000464};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200465EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000466
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800467struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100468 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200469 .uevent = i2c_device_uevent,
470 .release = i2c_client_dev_release,
471};
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800472EXPORT_SYMBOL_GPL(i2c_client_type);
Jean Delvare51298d12009-09-18 22:45:45 +0200473
David Brownell9b766b82008-01-27 18:14:51 +0100474
475/**
476 * i2c_verify_client - return parameter as i2c_client, or NULL
477 * @dev: device, probably from some driver model iterator
478 *
479 * When traversing the driver model tree, perhaps using driver model
480 * iterators like @device_for_each_child(), you can't assume very much
481 * about the nodes you find. Use this function to avoid oopses caused
482 * by wrongly treating some non-I2C device as an i2c_client.
483 */
484struct i2c_client *i2c_verify_client(struct device *dev)
485{
Jean Delvare51298d12009-09-18 22:45:45 +0200486 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100487 ? to_i2c_client(dev)
488 : NULL;
489}
490EXPORT_SYMBOL(i2c_verify_client);
491
492
Wolfram Sangda899f52015-05-18 21:09:12 +0200493/* Return a unique address which takes the flags of the client into account */
494static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
495{
496 unsigned short addr = client->addr;
497
498 /* For some client flags, add an arbitrary offset to avoid collisions */
499 if (client->flags & I2C_CLIENT_TEN)
500 addr |= I2C_ADDR_OFFSET_TEN_BIT;
501
502 if (client->flags & I2C_CLIENT_SLAVE)
503 addr |= I2C_ADDR_OFFSET_SLAVE;
504
505 return addr;
506}
507
Jean Delvare3a89db52010-06-03 11:33:52 +0200508/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300509 * are purposely not enforced, except for the general call address. */
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200510int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +0200511{
Wolfram Sangc4019b72015-07-17 12:50:06 +0200512 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +0200513 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200514 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +0200515 return -EINVAL;
516 } else {
517 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200518 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +0200519 return -EINVAL;
520 }
521 return 0;
522}
523
Jean Delvare656b8762010-06-03 11:33:53 +0200524/* And this is a strict address validity check, used when probing. If a
525 * device uses a reserved address, then it shouldn't be probed. 7-bit
526 * addressing is assumed, 10-bit address devices are rare and should be
527 * explicitly enumerated. */
Wolfram Sange4991ec2017-05-23 11:14:17 +0200528int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +0200529{
530 /*
531 * Reserved addresses per I2C specification:
532 * 0x00 General call address / START byte
533 * 0x01 CBUS address
534 * 0x02 Reserved for different bus format
535 * 0x03 Reserved for future purposes
536 * 0x04-0x07 Hs-mode master code
537 * 0x78-0x7b 10-bit slave addressing
538 * 0x7c-0x7f Reserved for future purposes
539 */
540 if (addr < 0x08 || addr > 0x77)
541 return -EINVAL;
542 return 0;
543}
544
Jean Delvare3b5f7942010-06-03 11:33:55 +0200545static int __i2c_check_addr_busy(struct device *dev, void *addrp)
546{
547 struct i2c_client *client = i2c_verify_client(dev);
548 int addr = *(int *)addrp;
549
Wolfram Sang9bccc702015-07-17 14:48:56 +0200550 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +0200551 return -EBUSY;
552 return 0;
553}
554
Michael Lawnick08263742010-08-11 18:21:02 +0200555/* walk up mux tree */
556static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
557{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200558 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200559 int result;
560
561 result = device_for_each_child(&adapter->dev, &addr,
562 __i2c_check_addr_busy);
563
Jean Delvare97cc4d42010-10-24 18:16:57 +0200564 if (!result && parent)
565 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200566
567 return result;
568}
569
570/* recurse down mux tree */
571static int i2c_check_mux_children(struct device *dev, void *addrp)
572{
573 int result;
574
575 if (dev->type == &i2c_adapter_type)
576 result = device_for_each_child(dev, addrp,
577 i2c_check_mux_children);
578 else
579 result = __i2c_check_addr_busy(dev, addrp);
580
581 return result;
582}
583
Jean Delvare3b5f7942010-06-03 11:33:55 +0200584static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
585{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200586 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200587 int result = 0;
588
Jean Delvare97cc4d42010-10-24 18:16:57 +0200589 if (parent)
590 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200591
592 if (!result)
593 result = device_for_each_child(&adapter->dev, &addr,
594 i2c_check_mux_children);
595
596 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200597}
598
David Brownell9c1600e2007-05-01 23:26:31 +0200599/**
Peter Rosin8320f492016-05-04 22:15:27 +0200600 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200601 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200602 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
603 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200604 */
Peter Rosin8320f492016-05-04 22:15:27 +0200605static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
606 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200607{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200608 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200609}
Jean Delvarefe61e072010-08-11 18:20:58 +0200610
611/**
Peter Rosin8320f492016-05-04 22:15:27 +0200612 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200613 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200614 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
615 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200616 */
Peter Rosin8320f492016-05-04 22:15:27 +0200617static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
618 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200619{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200620 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200621}
622
623/**
Peter Rosin8320f492016-05-04 22:15:27 +0200624 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200625 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200626 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
627 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200628 */
Peter Rosin8320f492016-05-04 22:15:27 +0200629static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
630 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200631{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200632 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200633}
Jean Delvarefe61e072010-08-11 18:20:58 +0200634
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200635static void i2c_dev_set_name(struct i2c_adapter *adap,
Hans de Goede728fe6c2017-10-11 11:41:19 +0200636 struct i2c_client *client,
637 struct i2c_board_info const *info)
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200638{
639 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
640
Hans de Goede728fe6c2017-10-11 11:41:19 +0200641 if (info && info->dev_name) {
642 dev_set_name(&client->dev, "i2c-%s", info->dev_name);
643 return;
644 }
645
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200646 if (adev) {
647 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
648 return;
649 }
650
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200651 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +0200652 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200653}
654
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800655static int i2c_dev_irq_from_resources(const struct resource *resources,
656 unsigned int num_resources)
657{
658 struct irq_data *irqd;
659 int i;
660
661 for (i = 0; i < num_resources; i++) {
662 const struct resource *r = &resources[i];
663
664 if (resource_type(r) != IORESOURCE_IRQ)
665 continue;
666
667 if (r->flags & IORESOURCE_BITS) {
668 irqd = irq_get_irq_data(r->start);
669 if (!irqd)
670 break;
671
672 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
673 }
674
675 return r->start;
676 }
677
678 return 0;
679}
680
Jean Delvarefe61e072010-08-11 18:20:58 +0200681/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200682 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200683 * @adap: the adapter managing the device
684 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200685 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200686 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200687 * Create an i2c device. Binding is handled through driver model
688 * probe()/remove() methods. A driver may be bound to this device when we
689 * return from this function, or any later moment (e.g. maybe hotplugging will
690 * load the driver module). This call is not appropriate for use by mainboard
691 * initialization logic, which usually runs during an arch_initcall() long
692 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200693 *
694 * This returns the new i2c client, which may be saved for later use with
695 * i2c_unregister_device(); or NULL to indicate an error.
696 */
697struct i2c_client *
698i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
699{
700 struct i2c_client *client;
701 int status;
702
703 client = kzalloc(sizeof *client, GFP_KERNEL);
704 if (!client)
705 return NULL;
706
707 client->adapter = adap;
708
709 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200710
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200711 if (info->archdata)
712 client->dev.archdata = *info->archdata;
713
Marc Pignatee354252008-08-28 08:33:22 +0200714 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200715 client->addr = info->addr;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800716
David Brownell9c1600e2007-05-01 23:26:31 +0200717 client->irq = info->irq;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800718 if (!client->irq)
719 client->irq = i2c_dev_irq_from_resources(info->resources,
720 info->num_resources);
David Brownell9c1600e2007-05-01 23:26:31 +0200721
David Brownell9c1600e2007-05-01 23:26:31 +0200722 strlcpy(client->name, info->type, sizeof(client->name));
723
Wolfram Sangc4019b72015-07-17 12:50:06 +0200724 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +0200725 if (status) {
726 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
727 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
728 goto out_err_silent;
729 }
730
Jean Delvaref8a227e2009-06-19 16:58:18 +0200731 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +0200732 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200733 if (status)
734 goto out_err;
735
736 client->dev.parent = &client->adapter->dev;
737 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200738 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700739 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100740 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200741
Hans de Goede728fe6c2017-10-11 11:41:19 +0200742 i2c_dev_set_name(adap, client, info);
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800743
744 if (info->properties) {
745 status = device_add_properties(&client->dev, info->properties);
746 if (status) {
747 dev_err(&adap->dev,
748 "Failed to add properties to client %s: %d\n",
749 client->name, status);
750 goto out_err;
751 }
752 }
753
Jean Delvaref8a227e2009-06-19 16:58:18 +0200754 status = device_register(&client->dev);
755 if (status)
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800756 goto out_free_props;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200757
Jean Delvaref8a227e2009-06-19 16:58:18 +0200758 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
759 client->name, dev_name(&client->dev));
760
David Brownell9c1600e2007-05-01 23:26:31 +0200761 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200762
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800763out_free_props:
764 if (info->properties)
765 device_remove_properties(&client->dev);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200766out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300767 dev_err(&adap->dev,
768 "Failed to register i2c client %s at 0x%02x (%d)\n",
769 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200770out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200771 kfree(client);
772 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200773}
774EXPORT_SYMBOL_GPL(i2c_new_device);
775
776
777/**
778 * i2c_unregister_device - reverse effect of i2c_new_device()
779 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200780 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200781 */
782void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200783{
Andy Shevchenko7b43dd12017-10-31 16:21:35 +0200784 if (!client)
785 return;
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +0200786 if (client->dev.of_node)
787 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300788 if (ACPI_COMPANION(&client->dev))
789 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +0200790 device_unregister(&client->dev);
791}
David Brownell9c1600e2007-05-01 23:26:31 +0200792EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200793
794
Jean Delvare60b129d2008-05-11 20:37:06 +0200795static const struct i2c_device_id dummy_id[] = {
796 { "dummy", 0 },
797 { },
798};
799
Jean Delvared2653e92008-04-29 23:11:39 +0200800static int dummy_probe(struct i2c_client *client,
801 const struct i2c_device_id *id)
802{
803 return 0;
804}
805
806static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100807{
808 return 0;
809}
810
811static struct i2c_driver dummy_driver = {
812 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200813 .probe = dummy_probe,
814 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200815 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100816};
817
818/**
819 * i2c_new_dummy - return a new i2c device bound to a dummy driver
820 * @adapter: the adapter managing the device
821 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100822 * Context: can sleep
823 *
824 * This returns an I2C client bound to the "dummy" driver, intended for use
825 * with devices that consume multiple addresses. Examples of such chips
826 * include various EEPROMS (like 24c04 and 24c08 models).
827 *
828 * These dummy devices have two main uses. First, most I2C and SMBus calls
829 * except i2c_transfer() need a client handle; the dummy will be that handle.
830 * And second, this prevents the specified address from being bound to a
831 * different driver.
832 *
833 * This returns the new i2c client, which should be saved for later use with
834 * i2c_unregister_device(); or NULL to indicate an error.
835 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100836struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100837{
838 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200839 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100840 };
841
David Brownelle9f13732008-01-27 18:14:52 +0100842 return i2c_new_device(adapter, &info);
843}
844EXPORT_SYMBOL_GPL(i2c_new_dummy);
845
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +0100846/**
847 * i2c_new_secondary_device - Helper to get the instantiated secondary address
848 * and create the associated device
849 * @client: Handle to the primary client
850 * @name: Handle to specify which secondary address to get
851 * @default_addr: Used as a fallback if no secondary address was specified
852 * Context: can sleep
853 *
854 * I2C clients can be composed of multiple I2C slaves bound together in a single
855 * component. The I2C client driver then binds to the master I2C slave and needs
856 * to create I2C dummy clients to communicate with all the other slaves.
857 *
858 * This function creates and returns an I2C dummy client whose I2C address is
859 * retrieved from the platform firmware based on the given slave name. If no
860 * address is specified by the firmware default_addr is used.
861 *
862 * On DT-based platforms the address is retrieved from the "reg" property entry
863 * cell whose "reg-names" value matches the slave name.
864 *
865 * This returns the new i2c client, which should be saved for later use with
866 * i2c_unregister_device(); or NULL to indicate an error.
867 */
868struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
869 const char *name,
870 u16 default_addr)
871{
872 struct device_node *np = client->dev.of_node;
873 u32 addr = default_addr;
874 int i;
875
876 if (np) {
877 i = of_property_match_string(np, "reg-names", name);
878 if (i >= 0)
879 of_property_read_u32_index(np, "reg", i, &addr);
880 }
881
882 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
883 return i2c_new_dummy(client->adapter, addr);
884}
885EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
886
David Brownellf37dd802007-02-13 22:09:00 +0100887/* ------------------------------------------------------------------------- */
888
David Brownell16ffadf2007-05-01 23:26:28 +0200889/* I2C bus adapters -- one roots each I2C or SMBUS segment */
890
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200891static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
David Brownellef2c83212007-05-01 23:26:28 +0200893 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 complete(&adap->dev_released);
895}
896
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +0200897unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +0200898{
899 unsigned int depth = 0;
900
901 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
902 depth++;
903
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +0200904 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
905 "adapter depth exceeds lockdep subclass limit\n");
906
Jean Delvare390946b2012-09-10 10:14:02 +0200907 return depth;
908}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +0200909EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +0200910
911/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200912 * Let users instantiate I2C devices through sysfs. This can be used when
913 * platform initialization code doesn't contain the proper data for
914 * whatever reason. Also useful for drivers that do device detection and
915 * detection fails, either because the device uses an unexpected address,
916 * or this is a compatible device with different ID register values.
917 *
918 * Parameter checking may look overzealous, but we really don't want
919 * the user to provide incorrect parameters.
920 */
921static ssize_t
922i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
923 const char *buf, size_t count)
924{
925 struct i2c_adapter *adap = to_i2c_adapter(dev);
926 struct i2c_board_info info;
927 struct i2c_client *client;
928 char *blank, end;
929 int res;
930
Jean Delvare99cd8e22009-06-19 16:58:20 +0200931 memset(&info, 0, sizeof(struct i2c_board_info));
932
933 blank = strchr(buf, ' ');
934 if (!blank) {
935 dev_err(dev, "%s: Missing parameters\n", "new_device");
936 return -EINVAL;
937 }
938 if (blank - buf > I2C_NAME_SIZE - 1) {
939 dev_err(dev, "%s: Invalid device name\n", "new_device");
940 return -EINVAL;
941 }
942 memcpy(info.type, buf, blank - buf);
943
944 /* Parse remaining parameters, reject extra parameters */
945 res = sscanf(++blank, "%hi%c", &info.addr, &end);
946 if (res < 1) {
947 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
948 return -EINVAL;
949 }
950 if (res > 1 && end != '\n') {
951 dev_err(dev, "%s: Extra parameters\n", "new_device");
952 return -EINVAL;
953 }
954
Wolfram Sangcfa03272015-07-27 14:03:38 +0200955 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
956 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
957 info.flags |= I2C_CLIENT_TEN;
958 }
959
960 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
961 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
962 info.flags |= I2C_CLIENT_SLAVE;
963 }
964
Jean Delvare99cd8e22009-06-19 16:58:20 +0200965 client = i2c_new_device(adap, &info);
966 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200967 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200968
969 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200970 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200971 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200972 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200973 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
974 info.type, info.addr);
975
976 return count;
977}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100978static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200979
980/*
981 * And of course let the users delete the devices they instantiated, if
982 * they got it wrong. This interface can only be used to delete devices
983 * instantiated by i2c_sysfs_new_device above. This guarantees that we
984 * don't delete devices to which some kernel code still has references.
985 *
986 * Parameter checking may look overzealous, but we really don't want
987 * the user to delete the wrong device.
988 */
989static ssize_t
990i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
991 const char *buf, size_t count)
992{
993 struct i2c_adapter *adap = to_i2c_adapter(dev);
994 struct i2c_client *client, *next;
995 unsigned short addr;
996 char end;
997 int res;
998
999 /* Parse parameters, reject extra parameters */
1000 res = sscanf(buf, "%hi%c", &addr, &end);
1001 if (res < 1) {
1002 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1003 return -EINVAL;
1004 }
1005 if (res > 1 && end != '\n') {
1006 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1007 return -EINVAL;
1008 }
1009
1010 /* Make sure the device was added through sysfs */
1011 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001012 mutex_lock_nested(&adap->userspace_clients_lock,
1013 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001014 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1015 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001016 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001017 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1018 "delete_device", client->name, client->addr);
1019
1020 list_del(&client->detected);
1021 i2c_unregister_device(client);
1022 res = count;
1023 break;
1024 }
1025 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001026 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001027
1028 if (res < 0)
1029 dev_err(dev, "%s: Can't find device in list\n",
1030 "delete_device");
1031 return res;
1032}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001033static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1034 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001035
1036static struct attribute *i2c_adapter_attrs[] = {
1037 &dev_attr_name.attr,
1038 &dev_attr_new_device.attr,
1039 &dev_attr_delete_device.attr,
1040 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001041};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001042ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001043
Michael Lawnick08263742010-08-11 18:21:02 +02001044struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001045 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001046 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001047};
Michael Lawnick08263742010-08-11 18:21:02 +02001048EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Stephen Warren643dd092012-04-17 12:43:33 -06001050/**
1051 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1052 * @dev: device, probably from some driver model iterator
1053 *
1054 * When traversing the driver model tree, perhaps using driver model
1055 * iterators like @device_for_each_child(), you can't assume very much
1056 * about the nodes you find. Use this function to avoid oopses caused
1057 * by wrongly treating some non-I2C device as an i2c_adapter.
1058 */
1059struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1060{
1061 return (dev->type == &i2c_adapter_type)
1062 ? to_i2c_adapter(dev)
1063 : NULL;
1064}
1065EXPORT_SYMBOL(i2c_verify_adapter);
1066
Jean Delvare2bb50952009-09-18 22:45:46 +02001067#ifdef CONFIG_I2C_COMPAT
1068static struct class_compat *i2c_adapter_compat_class;
1069#endif
1070
David Brownell9c1600e2007-05-01 23:26:31 +02001071static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1072{
1073 struct i2c_devinfo *devinfo;
1074
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001075 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001076 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1077 if (devinfo->busnum == adapter->nr
1078 && !i2c_new_device(adapter,
1079 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001080 dev_err(&adapter->dev,
1081 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001082 devinfo->board_info.addr);
1083 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001084 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001085}
1086
Jean Delvare69b00892009-12-06 17:06:27 +01001087static int i2c_do_add_adapter(struct i2c_driver *driver,
1088 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001089{
Jean Delvare4735c982008-07-14 22:38:36 +02001090 /* Detect supported devices on that bus, and instantiate them */
1091 i2c_detect(adap, driver);
1092
1093 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001094 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001095 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1096 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001097 dev_warn(&adap->dev,
1098 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001099 /* We ignore the return code; if it fails, too bad */
1100 driver->attach_adapter(adap);
1101 }
1102 return 0;
1103}
1104
Jean Delvare69b00892009-12-06 17:06:27 +01001105static int __process_new_adapter(struct device_driver *d, void *data)
1106{
1107 return i2c_do_add_adapter(to_i2c_driver(d), data);
1108}
1109
Peter Rosind1ed7982016-08-25 23:07:01 +02001110static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1111 .lock_bus = i2c_adapter_lock_bus,
1112 .trylock_bus = i2c_adapter_trylock_bus,
1113 .unlock_bus = i2c_adapter_unlock_bus,
1114};
1115
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001116static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap)
1117{
1118 struct irq_domain *domain = adap->host_notify_domain;
1119 irq_hw_number_t hwirq;
1120
1121 if (!domain)
1122 return;
1123
1124 for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++)
1125 irq_dispose_mapping(irq_find_mapping(domain, hwirq));
1126
1127 irq_domain_remove(domain);
1128 adap->host_notify_domain = NULL;
1129}
1130
1131static int i2c_host_notify_irq_map(struct irq_domain *h,
1132 unsigned int virq,
1133 irq_hw_number_t hw_irq_num)
1134{
1135 irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
1136
1137 return 0;
1138}
1139
1140static const struct irq_domain_ops i2c_host_notify_irq_ops = {
1141 .map = i2c_host_notify_irq_map,
1142};
1143
1144static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap)
1145{
1146 struct irq_domain *domain;
1147
1148 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
1149 return 0;
1150
1151 domain = irq_domain_create_linear(adap->dev.fwnode,
1152 I2C_ADDR_7BITS_COUNT,
1153 &i2c_host_notify_irq_ops, adap);
1154 if (!domain)
1155 return -ENOMEM;
1156
1157 adap->host_notify_domain = domain;
1158
1159 return 0;
1160}
1161
1162/**
1163 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
1164 * I2C client.
1165 * @adap: the adapter
1166 * @addr: the I2C address of the notifying device
1167 * Context: can't sleep
1168 *
1169 * Helper function to be called from an I2C bus driver's interrupt
1170 * handler. It will schedule the Host Notify IRQ.
1171 */
1172int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
1173{
1174 int irq;
1175
1176 if (!adap)
1177 return -EINVAL;
1178
1179 irq = irq_find_mapping(adap->host_notify_domain, addr);
1180 if (irq <= 0)
1181 return -ENXIO;
1182
1183 generic_handle_irq(irq);
1184
1185 return 0;
1186}
1187EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
1188
David Brownell6e13e642007-05-01 23:26:31 +02001189static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001191 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
David Brownell1d0b19c2008-10-14 17:30:05 +02001193 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301194 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001195 res = -EAGAIN;
1196 goto out_list;
1197 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001198
Jean Delvare2236baa2010-11-15 22:40:38 +01001199 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001200 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001201 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001202
1203 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001204 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001205 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01001206 }
1207
Peter Rosind1ed7982016-08-25 23:07:01 +02001208 if (!adap->lock_ops)
1209 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001210
Mika Kuoppala194684e2009-12-06 17:06:22 +01001211 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001212 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001213 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001214 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Jean Delvare8fcfef62009-03-28 21:34:43 +01001216 /* Set default timeout to 1 second if not already set */
1217 if (adap->timeout == 0)
1218 adap->timeout = HZ;
1219
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001220 /* register soft irqs for Host Notify */
1221 res = i2c_setup_host_notify_irq_domain(adap);
1222 if (res) {
1223 pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n",
1224 adap->name, res);
1225 goto out_list;
1226 }
1227
Kay Sievers27d9c182009-01-07 14:29:16 +01001228 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001229 adap->dev.bus = &i2c_bus_type;
1230 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001231 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001232 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001233 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001234 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Phil Reidf8756c62017-08-24 17:31:04 +08001237 res = of_i2c_setup_smbus_alert(adap);
1238 if (res)
1239 goto out_reg;
1240
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001241 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1242
Charles Keepax6ada5c12015-04-16 13:05:19 +01001243 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001244 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001245 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001246
Jean Delvare2bb50952009-09-18 22:45:46 +02001247#ifdef CONFIG_I2C_COMPAT
1248 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1249 adap->dev.parent);
1250 if (res)
1251 dev_warn(&adap->dev,
1252 "Failed to create compatibility class link\n");
1253#endif
1254
Wolfram Sangd3b11d82016-07-09 13:34:59 +09001255 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301256
Jean Delvare729d6dd2009-06-19 16:58:18 +02001257 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001258 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001259 i2c_acpi_register_devices(adap);
1260 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001261
David Brownell6e13e642007-05-01 23:26:31 +02001262 if (adap->nr < __i2c_first_dynamic_bus_num)
1263 i2c_scan_static_board_info(adap);
1264
Jean Delvare4735c982008-07-14 22:38:36 +02001265 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001266 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001267 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001268 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001269
1270 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001271
Phil Reidf8756c62017-08-24 17:31:04 +08001272out_reg:
1273 init_completion(&adap->dev_released);
1274 device_unregister(&adap->dev);
1275 wait_for_completion(&adap->dev_released);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001276out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001277 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001278 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001279 mutex_unlock(&core_lock);
1280 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
David Brownell6e13e642007-05-01 23:26:31 +02001283/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001284 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1285 * @adap: the adapter to register (with adap->nr initialized)
1286 * Context: can sleep
1287 *
1288 * See i2c_add_numbered_adapter() for details.
1289 */
1290static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1291{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001292 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001293
1294 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001295 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001296 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001297 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001298 return id == -ENOSPC ? -EBUSY : id;
1299
1300 return i2c_register_adapter(adap);
1301}
1302
1303/**
David Brownell6e13e642007-05-01 23:26:31 +02001304 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1305 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001306 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001307 *
1308 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001309 * doesn't matter or when its bus number is specified by an dt alias.
1310 * Examples of bases when the bus number doesn't matter: I2C adapters
1311 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001312 *
1313 * When this returns zero, a new bus number was allocated and stored
1314 * in adap->nr, and the specified adapter became available for clients.
1315 * Otherwise, a negative errno value is returned.
1316 */
1317int i2c_add_adapter(struct i2c_adapter *adapter)
1318{
Doug Andersonee5c2742013-03-01 08:57:31 -08001319 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001320 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001321
Doug Andersonee5c2742013-03-01 08:57:31 -08001322 if (dev->of_node) {
1323 id = of_alias_get_id(dev->of_node, "i2c");
1324 if (id >= 0) {
1325 adapter->nr = id;
1326 return __i2c_add_numbered_adapter(adapter);
1327 }
1328 }
1329
Jean Delvarecaada322008-01-27 18:14:49 +01001330 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001331 id = idr_alloc(&i2c_adapter_idr, adapter,
1332 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001333 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001334 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08001335 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001336
1337 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001338
David Brownell6e13e642007-05-01 23:26:31 +02001339 return i2c_register_adapter(adapter);
1340}
1341EXPORT_SYMBOL(i2c_add_adapter);
1342
1343/**
1344 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1345 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001346 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001347 *
1348 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001349 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1350 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001351 * is used to properly configure I2C devices.
1352 *
Grant Likely488bf312011-07-25 17:49:43 +02001353 * If the requested bus number is set to -1, then this function will behave
1354 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1355 *
David Brownell6e13e642007-05-01 23:26:31 +02001356 * If no devices have pre-been declared for this bus, then be sure to
1357 * register the adapter before any dynamically allocated ones. Otherwise
1358 * the required bus ID may not be available.
1359 *
1360 * When this returns zero, the specified adapter became available for
1361 * clients using the bus number provided in adap->nr. Also, the table
1362 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1363 * and the appropriate driver model device nodes are created. Otherwise, a
1364 * negative errno value is returned.
1365 */
1366int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1367{
Grant Likely488bf312011-07-25 17:49:43 +02001368 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1369 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001370
Doug Andersonee5c2742013-03-01 08:57:31 -08001371 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001372}
1373EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1374
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001375static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001376 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001377{
Jean Delvare4735c982008-07-14 22:38:36 +02001378 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001379
Jean Delvareacec2112009-03-28 21:34:40 +01001380 /* Remove the devices we created ourselves as the result of hardware
1381 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001382 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1383 if (client->adapter == adapter) {
1384 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1385 client->name, client->addr);
1386 list_del(&client->detected);
1387 i2c_unregister_device(client);
1388 }
1389 }
Jean Delvare026526f2008-01-27 18:14:49 +01001390}
1391
Jean Delvaree549c2b2009-06-19 16:58:19 +02001392static int __unregister_client(struct device *dev, void *dummy)
1393{
1394 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001395 if (client && strcmp(client->name, "dummy"))
1396 i2c_unregister_device(client);
1397 return 0;
1398}
1399
1400static int __unregister_dummy(struct device *dev, void *dummy)
1401{
1402 struct i2c_client *client = i2c_verify_client(dev);
Andy Shevchenko7b43dd12017-10-31 16:21:35 +02001403 i2c_unregister_device(client);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001404 return 0;
1405}
1406
Jean Delvare69b00892009-12-06 17:06:27 +01001407static int __process_removed_adapter(struct device_driver *d, void *data)
1408{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001409 i2c_do_del_adapter(to_i2c_driver(d), data);
1410 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001411}
1412
David Brownelld64f73b2007-07-12 14:12:28 +02001413/**
1414 * i2c_del_adapter - unregister I2C adapter
1415 * @adap: the adapter being unregistered
1416 * Context: can sleep
1417 *
1418 * This unregisters an I2C adapter which was previously registered
1419 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1420 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001421void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001423 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001424 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001427 mutex_lock(&core_lock);
1428 found = idr_find(&i2c_adapter_idr, adap->nr);
1429 mutex_unlock(&core_lock);
1430 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001431 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001432 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 }
1434
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001435 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001436 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001437 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001438 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001439 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001440 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001442 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001443 mutex_lock_nested(&adap->userspace_clients_lock,
1444 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001445 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1446 detected) {
1447 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1448 client->addr);
1449 list_del(&client->detected);
1450 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001451 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001452 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001453
Jean Delvaree549c2b2009-06-19 16:58:19 +02001454 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001455 * check the returned value. This is a two-pass process, because
1456 * we can't remove the dummy devices during the first pass: they
1457 * could have been instantiated by real devices wishing to clean
1458 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001459 device_for_each_child(&adap->dev, NULL, __unregister_client);
1460 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Jean Delvare2bb50952009-09-18 22:45:46 +02001462#ifdef CONFIG_I2C_COMPAT
1463 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1464 adap->dev.parent);
1465#endif
1466
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001467 /* device name is gone after device_unregister */
1468 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1469
Wolfram Sang9f924162015-12-23 18:19:28 +01001470 pm_runtime_disable(&adap->dev);
1471
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001472 i2c_host_notify_irq_teardown(adap);
1473
Wolfram Sang26680ee2015-01-29 22:45:09 +01001474 /* wait until all references to the device are gone
1475 *
1476 * FIXME: This is old code and should ideally be replaced by an
1477 * alternative which results in decoupling the lifetime of the struct
1478 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301479 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001480 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
David Brownell6e13e642007-05-01 23:26:31 +02001485 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001486 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001488 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001490 /* Clear the device structure in case this adapter is ever going to be
1491 added again */
1492 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493}
David Brownellc0564602007-05-01 23:26:31 +02001494EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495
Wolfram Sang54177cc2015-12-17 13:32:36 +01001496/**
1497 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
1498 * @dev: The device to scan for I2C timing properties
1499 * @t: the i2c_timings struct to be filled with values
1500 * @use_defaults: bool to use sane defaults derived from the I2C specification
1501 * when properties are not found, otherwise use 0
1502 *
1503 * Scan the device for the generic I2C properties describing timing parameters
1504 * for the signal and fill the given struct with the results. If a property was
1505 * not found and use_defaults was true, then maximum timings are assumed which
1506 * are derived from the I2C specification. If use_defaults is not used, the
1507 * results will be 0, so drivers can apply their own defaults later. The latter
1508 * is mainly intended for avoiding regressions of existing drivers which want
1509 * to switch to this function. New drivers almost always should use the defaults.
1510 */
1511
1512void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
1513{
1514 int ret;
1515
1516 memset(t, 0, sizeof(*t));
1517
1518 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
1519 if (ret && use_defaults)
1520 t->bus_freq_hz = 100000;
1521
1522 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
1523 if (ret && use_defaults) {
1524 if (t->bus_freq_hz <= 100000)
1525 t->scl_rise_ns = 1000;
1526 else if (t->bus_freq_hz <= 400000)
1527 t->scl_rise_ns = 300;
1528 else
1529 t->scl_rise_ns = 120;
1530 }
1531
1532 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
1533 if (ret && use_defaults) {
1534 if (t->bus_freq_hz <= 400000)
1535 t->scl_fall_ns = 300;
1536 else
1537 t->scl_fall_ns = 120;
1538 }
1539
1540 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
1541
1542 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
1543 if (ret && use_defaults)
1544 t->sda_fall_ns = t->scl_fall_ns;
1545}
1546EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
1547
David Brownell7b4fbc52007-05-01 23:26:30 +02001548/* ------------------------------------------------------------------------- */
1549
Jean Delvare7ae31482011-03-20 14:50:52 +01001550int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1551{
1552 int res;
1553
1554 mutex_lock(&core_lock);
1555 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1556 mutex_unlock(&core_lock);
1557
1558 return res;
1559}
1560EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1561
Jean Delvare69b00892009-12-06 17:06:27 +01001562static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001563{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001564 if (dev->type != &i2c_adapter_type)
1565 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001566 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001567}
1568
David Brownell7b4fbc52007-05-01 23:26:30 +02001569/*
1570 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001571 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 */
1573
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001574int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001576 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
David Brownell1d0b19c2008-10-14 17:30:05 +02001578 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301579 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02001580 return -EAGAIN;
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001583 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02001585 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Jean Delvare729d6dd2009-06-19 16:58:18 +02001587 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001588 * will have called probe() for all matching-but-unbound devices.
1589 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 res = driver_register(&driver->driver);
1591 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001592 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001593
Wolfram Sang44239a52016-07-09 13:35:04 +09001594 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Jean Delvare4735c982008-07-14 22:38:36 +02001596 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001597 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001598
Jean Delvare7eebcb72006-02-05 23:28:21 +01001599 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001601EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Jean Delvare69b00892009-12-06 17:06:27 +01001603static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001604{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001605 if (dev->type == &i2c_adapter_type)
1606 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1607 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001608}
1609
David Brownella1d9e6e2007-05-01 23:26:30 +02001610/**
1611 * i2c_del_driver - unregister I2C driver
1612 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001613 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001614 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001615void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Jean Delvare7ae31482011-03-20 14:50:52 +01001617 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09001620 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
David Brownellc0564602007-05-01 23:26:31 +02001622EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
David Brownell7b4fbc52007-05-01 23:26:30 +02001624/* ------------------------------------------------------------------------- */
1625
Jean Delvaree48d3312008-01-27 18:14:48 +01001626/**
1627 * i2c_use_client - increments the reference count of the i2c client structure
1628 * @client: the client being referenced
1629 *
1630 * Each live reference to a client should be refcounted. The driver model does
1631 * that automatically as part of driver binding, so that most drivers don't
1632 * need to do this explicitly: they hold a reference until they're unbound
1633 * from the device.
1634 *
1635 * A pointer to the client with the incremented reference counter is returned.
1636 */
1637struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
David Brownell6ea438e2008-07-14 22:38:24 +02001639 if (client && get_device(&client->dev))
1640 return client;
1641 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
David Brownellc0564602007-05-01 23:26:31 +02001643EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Jean Delvaree48d3312008-01-27 18:14:48 +01001645/**
1646 * i2c_release_client - release a use of the i2c client structure
1647 * @client: the client being no longer referenced
1648 *
1649 * Must be called when a user of a client is finished with it.
1650 */
1651void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652{
David Brownell6ea438e2008-07-14 22:38:24 +02001653 if (client)
1654 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
David Brownellc0564602007-05-01 23:26:31 +02001656EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
David Brownell9b766b82008-01-27 18:14:51 +01001658struct i2c_cmd_arg {
1659 unsigned cmd;
1660 void *arg;
1661};
1662
1663static int i2c_cmd(struct device *dev, void *_arg)
1664{
1665 struct i2c_client *client = i2c_verify_client(dev);
1666 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001667 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001668
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001669 if (!client || !client->dev.driver)
1670 return 0;
1671
1672 driver = to_i2c_driver(client->dev.driver);
1673 if (driver->command)
1674 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001675 return 0;
1676}
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1679{
David Brownell9b766b82008-01-27 18:14:51 +01001680 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
David Brownell9b766b82008-01-27 18:14:51 +01001682 cmd_arg.cmd = cmd;
1683 cmd_arg.arg = arg;
1684 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
David Brownellc0564602007-05-01 23:26:31 +02001686EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688static int __init i2c_init(void)
1689{
1690 int retval;
1691
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001692 retval = of_alias_get_highest_id("i2c");
1693
1694 down_write(&__i2c_board_lock);
1695 if (retval >= __i2c_first_dynamic_bus_num)
1696 __i2c_first_dynamic_bus_num = retval + 1;
1697 up_write(&__i2c_board_lock);
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 retval = bus_register(&i2c_bus_type);
1700 if (retval)
1701 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01001702
1703 is_registered = true;
1704
Jean Delvare2bb50952009-09-18 22:45:46 +02001705#ifdef CONFIG_I2C_COMPAT
1706 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1707 if (!i2c_adapter_compat_class) {
1708 retval = -ENOMEM;
1709 goto bus_err;
1710 }
1711#endif
David Brownelle9f13732008-01-27 18:14:52 +01001712 retval = i2c_add_driver(&dummy_driver);
1713 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001714 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001715
1716 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1717 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001718 if (IS_ENABLED(CONFIG_ACPI))
1719 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001720
David Brownelle9f13732008-01-27 18:14:52 +01001721 return 0;
1722
Jean Delvare2bb50952009-09-18 22:45:46 +02001723class_err:
1724#ifdef CONFIG_I2C_COMPAT
1725 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001726bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001727#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01001728 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01001729 bus_unregister(&i2c_bus_type);
1730 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
1733static void __exit i2c_exit(void)
1734{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001735 if (IS_ENABLED(CONFIG_ACPI))
1736 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001737 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1738 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001739 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001740#ifdef CONFIG_I2C_COMPAT
1741 class_compat_unregister(i2c_adapter_compat_class);
1742#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001744 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
David Brownella10f9e72008-10-14 17:30:06 +02001747/* We must initialize early, because some subsystems register i2c drivers
1748 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1749 */
1750postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751module_exit(i2c_exit);
1752
1753/* ----------------------------------------------------
1754 * the functional interface to the i2c busses.
1755 * ----------------------------------------------------
1756 */
1757
Wolfram Sangb7f62582015-01-05 23:45:59 +01001758/* Check if val is exceeding the quirk IFF quirk is non 0 */
1759#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1760
1761static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
1762{
1763 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
1764 err_msg, msg->addr, msg->len,
1765 msg->flags & I2C_M_RD ? "read" : "write");
1766 return -EOPNOTSUPP;
1767}
1768
1769static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1770{
1771 const struct i2c_adapter_quirks *q = adap->quirks;
1772 int max_num = q->max_num_msgs, i;
1773 bool do_len_check = true;
1774
1775 if (q->flags & I2C_AQ_COMB) {
1776 max_num = 2;
1777
1778 /* special checks for combined messages */
1779 if (num == 2) {
1780 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
1781 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
1782
1783 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
1784 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
1785
1786 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
1787 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
1788
1789 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
1790 return i2c_quirk_error(adap, &msgs[0], "msg too long");
1791
1792 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
1793 return i2c_quirk_error(adap, &msgs[1], "msg too long");
1794
1795 do_len_check = false;
1796 }
1797 }
1798
1799 if (i2c_quirk_exceeded(num, max_num))
1800 return i2c_quirk_error(adap, &msgs[0], "too many messages");
1801
1802 for (i = 0; i < num; i++) {
1803 u16 len = msgs[i].len;
1804
1805 if (msgs[i].flags & I2C_M_RD) {
1806 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
1807 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1808 } else {
1809 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
1810 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1811 }
1812 }
1813
1814 return 0;
1815}
1816
David Brownella1cdeda2008-07-14 22:38:24 +02001817/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001818 * __i2c_transfer - unlocked flavor of i2c_transfer
1819 * @adap: Handle to I2C bus
1820 * @msgs: One or more messages to execute before STOP is issued to
1821 * terminate the operation; each message begins with a START.
1822 * @num: Number of messages to be executed.
1823 *
1824 * Returns negative errno, else the number of messages executed.
1825 *
1826 * Adapter lock must be held when calling this function. No debug logging
1827 * takes place. adap->algo->master_xfer existence isn't checked.
1828 */
1829int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1830{
1831 unsigned long orig_jiffies;
1832 int ret, try;
1833
Wolfram Sangb7f62582015-01-05 23:45:59 +01001834 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
1835 return -EOPNOTSUPP;
1836
David Howellsd9a83d62014-03-06 13:35:59 +00001837 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
1838 * enabled. This is an efficient way of keeping the for-loop from
1839 * being executed when not needed.
1840 */
1841 if (static_key_false(&i2c_trace_msg)) {
1842 int i;
1843 for (i = 0; i < num; i++)
1844 if (msgs[i].flags & I2C_M_RD)
1845 trace_i2c_read(adap, &msgs[i], i);
1846 else
1847 trace_i2c_write(adap, &msgs[i], i);
1848 }
1849
Jean Delvareb37d2a32012-06-29 07:47:19 -03001850 /* Retry automatically on arbitration loss */
1851 orig_jiffies = jiffies;
1852 for (ret = 0, try = 0; try <= adap->retries; try++) {
1853 ret = adap->algo->master_xfer(adap, msgs, num);
1854 if (ret != -EAGAIN)
1855 break;
1856 if (time_after(jiffies, orig_jiffies + adap->timeout))
1857 break;
1858 }
1859
David Howellsd9a83d62014-03-06 13:35:59 +00001860 if (static_key_false(&i2c_trace_msg)) {
1861 int i;
1862 for (i = 0; i < ret; i++)
1863 if (msgs[i].flags & I2C_M_RD)
1864 trace_i2c_reply(adap, &msgs[i], i);
1865 trace_i2c_result(adap, i, ret);
1866 }
1867
Jean Delvareb37d2a32012-06-29 07:47:19 -03001868 return ret;
1869}
1870EXPORT_SYMBOL(__i2c_transfer);
1871
1872/**
David Brownella1cdeda2008-07-14 22:38:24 +02001873 * i2c_transfer - execute a single or combined I2C message
1874 * @adap: Handle to I2C bus
1875 * @msgs: One or more messages to execute before STOP is issued to
1876 * terminate the operation; each message begins with a START.
1877 * @num: Number of messages to be executed.
1878 *
1879 * Returns negative errno, else the number of messages executed.
1880 *
1881 * Note that there is no requirement that each message be sent to
1882 * the same slave address, although that is the most common model.
1883 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001884int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001886 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
David Brownella1cdeda2008-07-14 22:38:24 +02001888 /* REVISIT the fault reporting model here is weak:
1889 *
1890 * - When we get an error after receiving N bytes from a slave,
1891 * there is no way to report "N".
1892 *
1893 * - When we get a NAK after transmitting N bytes to a slave,
1894 * there is no way to report "N" ... or to let the master
1895 * continue executing the rest of this combined message, if
1896 * that's the appropriate response.
1897 *
1898 * - When for example "num" is two and we successfully complete
1899 * the first message but get an error part way through the
1900 * second, it's unclear whether that should be reported as
1901 * one (discarding status on the second message) or errno
1902 * (discarding status on the first one).
1903 */
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (adap->algo->master_xfer) {
1906#ifdef DEBUG
1907 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001908 dev_dbg(&adap->dev,
1909 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
1910 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
1911 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02001912 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914#endif
1915
Mike Rapoportcea443a82008-01-27 18:14:50 +01001916 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02001917 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001918 if (!ret)
1919 /* I2C activity is ongoing. */
1920 return -EAGAIN;
1921 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02001922 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001923 }
1924
Jean Delvareb37d2a32012-06-29 07:47:19 -03001925 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02001926 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
1928 return ret;
1929 } else {
1930 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001931 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933}
David Brownellc0564602007-05-01 23:26:31 +02001934EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
David Brownella1cdeda2008-07-14 22:38:24 +02001936/**
Wolfram Sang8a917322017-11-04 21:20:04 +01001937 * i2c_transfer_buffer_flags - issue a single I2C message transferring data
1938 * to/from a buffer
David Brownella1cdeda2008-07-14 22:38:24 +02001939 * @client: Handle to slave device
Wolfram Sang8a917322017-11-04 21:20:04 +01001940 * @buf: Where the data is stored
1941 * @count: How many bytes to transfer, must be less than 64k since msg.len is u16
1942 * @flags: The flags to be used for the message, e.g. I2C_M_RD for reads
David Brownella1cdeda2008-07-14 22:38:24 +02001943 *
Wolfram Sang8a917322017-11-04 21:20:04 +01001944 * Returns negative errno, or else the number of bytes transferred.
David Brownella1cdeda2008-07-14 22:38:24 +02001945 */
Wolfram Sang8a917322017-11-04 21:20:04 +01001946int i2c_transfer_buffer_flags(const struct i2c_client *client, char *buf,
1947 int count, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
1949 int ret;
Wolfram Sang8a917322017-11-04 21:20:04 +01001950 struct i2c_msg msg = {
1951 .addr = client->addr,
1952 .flags = flags | (client->flags & I2C_M_TEN),
1953 .len = count,
1954 .buf = buf,
1955 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Wolfram Sang8a917322017-11-04 21:20:04 +01001957 ret = i2c_transfer(client->adapter, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001959 /*
Wolfram Sang8a917322017-11-04 21:20:04 +01001960 * If everything went ok (i.e. 1 msg transferred), return #bytes
1961 * transferred, else error code.
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001962 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001963 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
Wolfram Sang8a917322017-11-04 21:20:04 +01001965EXPORT_SYMBOL(i2c_transfer_buffer_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967/* ----------------------------------------------------
1968 * the i2c address scanning function
1969 * Will not work for 10-bit addresses!
1970 * ----------------------------------------------------
1971 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001972
Jean Delvare63e4e802010-06-03 11:33:51 +02001973/*
1974 * Legacy default probe function, mostly relevant for SMBus. The default
1975 * probe method is a quick write, but it is known to corrupt the 24RF08
1976 * EEPROMs due to a state machine bug, and could also irreversibly
1977 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1978 * we use a short byte read instead. Also, some bus drivers don't implement
1979 * quick write, so we fallback to a byte read in that case too.
1980 * On x86, there is another special case for FSC hardware monitoring chips,
1981 * which want regular byte reads (address 0x73.) Fortunately, these are the
1982 * only known chips using this I2C address on PC hardware.
1983 * Returns 1 if probe succeeded, 0 if not.
1984 */
1985static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1986{
1987 int err;
1988 union i2c_smbus_data dummy;
1989
1990#ifdef CONFIG_X86
1991 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1992 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1993 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1994 I2C_SMBUS_BYTE_DATA, &dummy);
1995 else
1996#endif
Jean Delvare8031d792010-08-11 18:21:00 +02001997 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1998 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02001999 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2000 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002001 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2002 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2003 I2C_SMBUS_BYTE, &dummy);
2004 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002005 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2006 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002007 err = -EOPNOTSUPP;
2008 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002009
2010 return err >= 0;
2011}
2012
Jean Delvareccfbbd02009-12-06 17:06:25 +01002013static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002014 struct i2c_driver *driver)
2015{
2016 struct i2c_board_info info;
2017 struct i2c_adapter *adapter = temp_client->adapter;
2018 int addr = temp_client->addr;
2019 int err;
2020
2021 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002022 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002023 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002024 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2025 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002026 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002027 }
2028
Wolfram Sang9bccc702015-07-17 14:48:56 +02002029 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002030 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002031 return 0;
2032
Jean Delvareccfbbd02009-12-06 17:06:25 +01002033 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002034 if (!i2c_default_probe(adapter, addr))
2035 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002036
2037 /* Finally call the custom detection function */
2038 memset(&info, 0, sizeof(struct i2c_board_info));
2039 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002040 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002041 if (err) {
2042 /* -ENODEV is returned if the detection fails. We catch it
2043 here as this isn't an error. */
2044 return err == -ENODEV ? 0 : err;
2045 }
2046
2047 /* Consistency check */
2048 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002049 dev_err(&adapter->dev,
2050 "%s detection function provided no name for 0x%x\n",
2051 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002052 } else {
2053 struct i2c_client *client;
2054
2055 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002056 if (adapter->class & I2C_CLASS_DEPRECATED)
2057 dev_warn(&adapter->dev,
2058 "This adapter will soon drop class based instantiation of devices. "
2059 "Please make sure client 0x%02x gets instantiated by other means. "
2060 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2061 info.addr);
2062
Jean Delvare4735c982008-07-14 22:38:36 +02002063 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2064 info.type, info.addr);
2065 client = i2c_new_device(adapter, &info);
2066 if (client)
2067 list_add_tail(&client->detected, &driver->clients);
2068 else
2069 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2070 info.type, info.addr);
2071 }
2072 return 0;
2073}
2074
2075static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2076{
Jean Delvarec3813d62009-12-14 21:17:25 +01002077 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002078 struct i2c_client *temp_client;
2079 int i, err = 0;
2080 int adap_id = i2c_adapter_id(adapter);
2081
Jean Delvarec3813d62009-12-14 21:17:25 +01002082 address_list = driver->address_list;
2083 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002084 return 0;
2085
Wolfram Sang45552272014-07-10 13:46:21 +02002086 /* Warn that the adapter lost class based instantiation */
2087 if (adapter->class == I2C_CLASS_DEPRECATED) {
2088 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002089 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2090 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002091 driver->driver.name);
2092 return 0;
2093 }
2094
Jean Delvare51b54ba2010-10-24 18:16:58 +02002095 /* Stop here if the classes do not match */
2096 if (!(adapter->class & driver->class))
2097 return 0;
2098
Jean Delvare4735c982008-07-14 22:38:36 +02002099 /* Set up a temporary client to help detect callback */
2100 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2101 if (!temp_client)
2102 return -ENOMEM;
2103 temp_client->adapter = adapter;
2104
Jean Delvarec3813d62009-12-14 21:17:25 +01002105 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002106 dev_dbg(&adapter->dev,
2107 "found normal entry for adapter %d, addr 0x%02x\n",
2108 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002109 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002110 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002111 if (unlikely(err))
2112 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002113 }
2114
Jean Delvare4735c982008-07-14 22:38:36 +02002115 kfree(temp_client);
2116 return err;
2117}
2118
Jean Delvared44f19d2010-08-11 18:20:57 +02002119int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2120{
2121 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2122 I2C_SMBUS_QUICK, NULL) >= 0;
2123}
2124EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2125
Jean Delvare12b5053a2007-05-01 23:26:31 +02002126struct i2c_client *
2127i2c_new_probed_device(struct i2c_adapter *adap,
2128 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002129 unsigned short const *addr_list,
2130 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002131{
2132 int i;
2133
Jean Delvare8031d792010-08-11 18:21:00 +02002134 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002135 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002136
Jean Delvare12b5053a2007-05-01 23:26:31 +02002137 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2138 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002139 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002140 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2141 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002142 continue;
2143 }
2144
Wolfram Sang9bccc702015-07-17 14:48:56 +02002145 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002146 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002147 dev_dbg(&adap->dev,
2148 "Address 0x%02x already in use, not probing\n",
2149 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002150 continue;
2151 }
2152
Jean Delvare63e4e802010-06-03 11:33:51 +02002153 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002154 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002155 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002156 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002157
2158 if (addr_list[i] == I2C_CLIENT_END) {
2159 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2160 return NULL;
2161 }
2162
2163 info->addr = addr_list[i];
2164 return i2c_new_device(adap, info);
2165}
2166EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2167
Jean Delvared735b342011-03-20 14:50:52 +01002168struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002171
Jean Delvarecaada322008-01-27 18:14:49 +01002172 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002173 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002174 if (!adapter)
2175 goto exit;
2176
2177 if (try_module_get(adapter->owner))
2178 get_device(&adapter->dev);
2179 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002180 adapter = NULL;
2181
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002182 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002183 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002184 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
David Brownellc0564602007-05-01 23:26:31 +02002186EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188void i2c_put_adapter(struct i2c_adapter *adap)
2189{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002190 if (!adap)
2191 return;
2192
2193 put_device(&adap->dev);
2194 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
David Brownellc0564602007-05-01 23:26:31 +02002196EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002198/**
2199 * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg
2200 * @msg: the message to be checked
2201 * @threshold: the minimum number of bytes for which using DMA makes sense
2202 *
2203 * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.
2204 * Or a valid pointer to be used with DMA. After use, release it by
2205 * calling i2c_release_dma_safe_msg_buf().
2206 *
2207 * This function must only be called from process context!
2208 */
2209u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)
2210{
2211 if (msg->len < threshold)
2212 return NULL;
2213
2214 if (msg->flags & I2C_M_DMA_SAFE)
2215 return msg->buf;
2216
2217 pr_debug("using bounce buffer for addr=0x%02x, len=%d\n",
2218 msg->addr, msg->len);
2219
2220 if (msg->flags & I2C_M_RD)
2221 return kzalloc(msg->len, GFP_KERNEL);
2222 else
2223 return kmemdup(msg->buf, msg->len, GFP_KERNEL);
2224}
2225EXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf);
2226
2227/**
2228 * i2c_release_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
2229 * @msg: the message to be synced with
2230 * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.
2231 */
2232void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf)
2233{
2234 if (!buf || buf == msg->buf)
2235 return;
2236
2237 if (msg->flags & I2C_M_RD)
2238 memcpy(msg->buf, buf, msg->len);
2239
2240 kfree(buf);
2241}
2242EXPORT_SYMBOL_GPL(i2c_release_dma_safe_msg_buf);
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2245MODULE_DESCRIPTION("I2C-Bus main module");
2246MODULE_LICENSE("GPL");