blob: edfc23e496300d2b702dc0a455b59f72e8796d36 [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
Javier Martinez Canillasaf503712017-12-03 22:40:50 +0100127 rc = of_device_uevent_modalias(dev, env);
128 if (rc != -ENODEV)
129 return rc;
130
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800131 rc = acpi_device_uevent_modalias(dev, env);
132 if (rc != -ENODEV)
133 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200134
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100135 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200136}
137
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530138/* i2c bus recovery routines */
139static int get_scl_gpio_value(struct i2c_adapter *adap)
140{
Phil Reid3991c5c2017-11-02 10:40:24 +0800141 return gpiod_get_value_cansleep(adap->bus_recovery_info->scl_gpiod);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530142}
143
144static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
145{
Phil Reid3991c5c2017-11-02 10:40:24 +0800146 gpiod_set_value_cansleep(adap->bus_recovery_info->scl_gpiod, val);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530147}
148
149static int get_sda_gpio_value(struct i2c_adapter *adap)
150{
Phil Reid3991c5c2017-11-02 10:40:24 +0800151 return gpiod_get_value_cansleep(adap->bus_recovery_info->sda_gpiod);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530152}
153
Wolfram Sang80921782018-01-09 14:58:56 +0100154static void set_sda_gpio_value(struct i2c_adapter *adap, int val)
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530155{
Wolfram Sang80921782018-01-09 14:58:56 +0100156 gpiod_set_value_cansleep(adap->bus_recovery_info->sda_gpiod, val);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530157}
158
159/*
160 * We are generating clock pulses. ndelay() determines durating of clk pulses.
161 * We will generate clock with rate 100 KHz and so duration of both clock levels
162 * is: delay in ns = (10^6 / 100) / 2
163 */
164#define RECOVERY_NDELAY 5000
165#define RECOVERY_CLK_CNT 9
166
Phil Reide1eb7d22017-11-02 10:40:30 +0800167int i2c_generic_scl_recovery(struct i2c_adapter *adap)
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530168{
169 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
170 int i = 0, val = 1, ret = 0;
171
172 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300173 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530174
Jan Luebbe8b062602015-07-08 16:35:06 +0200175 bri->set_scl(adap, val);
Wolfram Sang72b08fc2018-01-09 14:58:57 +0100176 if (bri->set_sda)
177 bri->set_sda(adap, 1);
Jan Luebbe8b062602015-07-08 16:35:06 +0200178 ndelay(RECOVERY_NDELAY);
179
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530180 /*
181 * By this time SCL is high, as we need to give 9 falling-rising edges
182 */
183 while (i++ < RECOVERY_CLK_CNT * 2) {
184 if (val) {
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530185 /* SCL shouldn't be low here */
186 if (!bri->get_scl(adap)) {
187 dev_err(&adap->dev,
188 "SCL is stuck low, exit recovery\n");
189 ret = -EBUSY;
190 break;
191 }
Claudio Foellmi1f35b862017-10-05 14:44:14 +0200192 /* Break if SDA is high */
193 if (bri->get_sda && bri->get_sda(adap))
194 break;
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530195 }
196
197 val = !val;
198 bri->set_scl(adap, val);
199 ndelay(RECOVERY_NDELAY);
200 }
201
Claudio Foellmi1f35b862017-10-05 14:44:14 +0200202 /* check if recovery actually succeeded */
203 if (bri->get_sda && !bri->get_sda(adap))
204 ret = -EBUSY;
205
Wolfram Sang2806e6a2018-01-09 14:58:58 +0100206 /* If all went well, send STOP for a sane bus state. */
207 if (ret == 0 && bri->set_sda) {
208 bri->set_scl(adap, 0);
209 ndelay(RECOVERY_NDELAY / 2);
210 bri->set_sda(adap, 0);
211 ndelay(RECOVERY_NDELAY / 2);
212 bri->set_scl(adap, 1);
213 ndelay(RECOVERY_NDELAY / 2);
214 bri->set_sda(adap, 1);
215 ndelay(RECOVERY_NDELAY / 2);
216 }
217
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530218 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300219 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530220
221 return ret;
222}
Mark Brownc1c21f42015-04-15 19:18:39 +0100223EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530224
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530225int i2c_recover_bus(struct i2c_adapter *adap)
226{
227 if (!adap->bus_recovery_info)
228 return -EOPNOTSUPP;
229
230 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
231 return adap->bus_recovery_info->recover_bus(adap);
232}
Mark Brownc1c21f42015-04-15 19:18:39 +0100233EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530234
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900235static void i2c_init_recovery(struct i2c_adapter *adap)
236{
237 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
238 char *err_str;
239
240 if (!bri)
241 return;
242
243 if (!bri->recover_bus) {
244 err_str = "no recover_bus() found";
245 goto err;
246 }
247
Phil Reid3991c5c2017-11-02 10:40:24 +0800248 if (bri->scl_gpiod && bri->recover_bus == i2c_generic_scl_recovery) {
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900249 bri->get_scl = get_scl_gpio_value;
250 bri->set_scl = set_scl_gpio_value;
Wolfram Sang80921782018-01-09 14:58:56 +0100251 if (bri->sda_gpiod) {
Phil Reid3991c5c2017-11-02 10:40:24 +0800252 bri->get_sda = get_sda_gpio_value;
Wolfram Sang80921782018-01-09 14:58:56 +0100253 /* FIXME: add proper flag instead of '0' once available */
254 if (gpiod_get_direction(bri->sda_gpiod) == 0)
255 bri->set_sda = set_sda_gpio_value;
256 }
Phil Reid3991c5c2017-11-02 10:40:24 +0800257 return;
258 }
259
Phil Reide1eb7d22017-11-02 10:40:30 +0800260 if (bri->recover_bus == i2c_generic_scl_recovery) {
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900261 /* Generic SCL recovery */
262 if (!bri->set_scl || !bri->get_scl) {
263 err_str = "no {get|set}_scl() found";
264 goto err;
265 }
266 }
267
268 return;
269 err:
270 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
271 adap->bus_recovery_info = NULL;
272}
273
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200274static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
275{
276 struct i2c_adapter *adap = client->adapter;
277 unsigned int irq;
278
279 if (!adap->host_notify_domain)
280 return -ENXIO;
281
282 if (client->flags & I2C_CLIENT_TEN)
283 return -EINVAL;
284
285 irq = irq_find_mapping(adap->host_notify_domain, client->addr);
286 if (!irq)
287 irq = irq_create_mapping(adap->host_notify_domain,
288 client->addr);
289
290 return irq > 0 ? irq : -ENXIO;
291}
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293static int i2c_device_probe(struct device *dev)
294{
Jean Delvare51298d12009-09-18 22:45:45 +0200295 struct i2c_client *client = i2c_verify_client(dev);
296 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100297 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200298
Jean Delvare51298d12009-09-18 22:45:45 +0200299 if (!client)
300 return 0;
301
Hans de Goeded1d84bb2017-04-05 00:03:34 +0200302 driver = to_i2c_driver(dev->driver);
303
304 if (!client->irq && !driver->disable_i2c_core_irq_mapping) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300305 int irq = -ENOENT;
306
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800307 if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
308 dev_dbg(dev, "Using Host Notify IRQ\n");
309 irq = i2c_smbus_host_notify_to_irq(client);
310 } else if (dev->of_node) {
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700311 irq = of_irq_get_byname(dev->of_node, "irq");
312 if (irq == -EINVAL || irq == -ENODATA)
313 irq = of_irq_get(dev->of_node, 0);
314 } else if (ACPI_COMPANION(dev)) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300315 irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700316 }
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100317 if (irq == -EPROBE_DEFER)
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200318 return irq;
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800319
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100320 if (irq < 0)
321 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200322
323 client->irq = irq;
324 }
325
Lee Jonesda10c062016-11-07 12:47:39 +0000326 /*
Javier Martinez Canillasf4b17a12017-08-09 11:21:28 +0200327 * An I2C ID table is not mandatory, if and only if, a suitable OF
328 * or ACPI ID table is supplied for the probing device.
Lee Jonesda10c062016-11-07 12:47:39 +0000329 */
330 if (!driver->id_table &&
Andy Shevchenkoc64ffff2017-07-17 17:13:28 +0300331 !i2c_acpi_match_device(dev->driver->acpi_match_table, client) &&
Lee Jonesda10c062016-11-07 12:47:39 +0000332 !i2c_of_match_device(dev->driver->of_match_table, client))
David Brownell7b4fbc52007-05-01 23:26:30 +0200333 return -ENODEV;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200334
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700335 if (client->flags & I2C_CLIENT_WAKE) {
336 int wakeirq = -ENOENT;
337
338 if (dev->of_node) {
339 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
340 if (wakeirq == -EPROBE_DEFER)
341 return wakeirq;
342 }
343
344 device_init_wakeup(&client->dev, true);
345
346 if (wakeirq > 0 && wakeirq != client->irq)
347 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
348 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200349 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700350 else
351 status = 0;
352
353 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300354 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700355 }
356
David Brownell7b4fbc52007-05-01 23:26:30 +0200357 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200358
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200359 status = of_clk_set_defaults(dev->of_node, false);
360 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700361 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200362
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200363 status = dev_pm_domain_attach(&client->dev, true);
Kieran Bingham74cedd32015-10-12 21:54:43 +0100364 if (status == -EPROBE_DEFER)
365 goto err_clear_wakeup_irq;
366
Lee Jonesb8a1a4c2016-11-07 12:47:41 +0000367 /*
368 * When there are no more users of probe(),
369 * rename probe_new to probe.
370 */
371 if (driver->probe_new)
372 status = driver->probe_new(client);
373 else if (driver->probe)
374 status = driver->probe(client,
375 i2c_match_id(driver->id_table, client));
376 else
377 status = -EINVAL;
378
Kieran Bingham74cedd32015-10-12 21:54:43 +0100379 if (status)
380 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100381
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700382 return 0;
383
384err_detach_pm_domain:
385 dev_pm_domain_detach(&client->dev, true);
386err_clear_wakeup_irq:
387 dev_pm_clear_wake_irq(&client->dev);
388 device_init_wakeup(&client->dev, false);
Hans Verkuil50c33042008-03-12 14:15:00 +0100389 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392static int i2c_device_remove(struct device *dev)
393{
Jean Delvare51298d12009-09-18 22:45:45 +0200394 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200395 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100396 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200397
Jean Delvare51298d12009-09-18 22:45:45 +0200398 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200399 return 0;
400
401 driver = to_i2c_driver(dev->driver);
402 if (driver->remove) {
403 dev_dbg(dev, "remove\n");
404 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200405 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100406
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200407 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700408
409 dev_pm_clear_wake_irq(&client->dev);
410 device_init_wakeup(&client->dev, false);
411
David Brownella1d9e6e2007-05-01 23:26:30 +0200412 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
David Brownellf37dd802007-02-13 22:09:00 +0100415static void i2c_device_shutdown(struct device *dev)
416{
Jean Delvare51298d12009-09-18 22:45:45 +0200417 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100418 struct i2c_driver *driver;
419
Jean Delvare51298d12009-09-18 22:45:45 +0200420 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100421 return;
422 driver = to_i2c_driver(dev->driver);
423 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200424 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100425}
426
David Brownell9c1600e2007-05-01 23:26:31 +0200427static void i2c_client_dev_release(struct device *dev)
428{
429 kfree(to_i2c_client(dev));
430}
431
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100432static ssize_t
Jean Delvare4f8cf822009-09-18 22:45:46 +0200433show_name(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200434{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200435 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
436 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200437}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100438static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
David Brownell7b4fbc52007-05-01 23:26:30 +0200439
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100440static ssize_t
441show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200442{
443 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800444 int len;
445
Javier Martinez Canillasaf503712017-12-03 22:40:50 +0100446 len = of_device_modalias(dev, buf, PAGE_SIZE);
447 if (len != -ENODEV)
448 return len;
449
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800450 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
451 if (len != -ENODEV)
452 return len;
453
Jean Delvareeb8a7902008-05-18 20:49:41 +0200454 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200455}
Jean Delvare51298d12009-09-18 22:45:45 +0200456static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
457
458static struct attribute *i2c_dev_attrs[] = {
459 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200460 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200461 &dev_attr_modalias.attr,
462 NULL
463};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100464ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100465
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200466struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100467 .name = "i2c",
468 .match = i2c_device_match,
469 .probe = i2c_device_probe,
470 .remove = i2c_device_remove,
471 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000472};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200473EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000474
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800475struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100476 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200477 .uevent = i2c_device_uevent,
478 .release = i2c_client_dev_release,
479};
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800480EXPORT_SYMBOL_GPL(i2c_client_type);
Jean Delvare51298d12009-09-18 22:45:45 +0200481
David Brownell9b766b82008-01-27 18:14:51 +0100482
483/**
484 * i2c_verify_client - return parameter as i2c_client, or NULL
485 * @dev: device, probably from some driver model iterator
486 *
487 * When traversing the driver model tree, perhaps using driver model
488 * iterators like @device_for_each_child(), you can't assume very much
489 * about the nodes you find. Use this function to avoid oopses caused
490 * by wrongly treating some non-I2C device as an i2c_client.
491 */
492struct i2c_client *i2c_verify_client(struct device *dev)
493{
Jean Delvare51298d12009-09-18 22:45:45 +0200494 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100495 ? to_i2c_client(dev)
496 : NULL;
497}
498EXPORT_SYMBOL(i2c_verify_client);
499
500
Wolfram Sangda899f52015-05-18 21:09:12 +0200501/* Return a unique address which takes the flags of the client into account */
502static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
503{
504 unsigned short addr = client->addr;
505
506 /* For some client flags, add an arbitrary offset to avoid collisions */
507 if (client->flags & I2C_CLIENT_TEN)
508 addr |= I2C_ADDR_OFFSET_TEN_BIT;
509
510 if (client->flags & I2C_CLIENT_SLAVE)
511 addr |= I2C_ADDR_OFFSET_SLAVE;
512
513 return addr;
514}
515
Jean Delvare3a89db52010-06-03 11:33:52 +0200516/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300517 * are purposely not enforced, except for the general call address. */
Wolfram Sang5bf4fa72017-05-23 11:50:58 +0200518int i2c_check_addr_validity(unsigned addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +0200519{
Wolfram Sangc4019b72015-07-17 12:50:06 +0200520 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +0200521 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200522 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +0200523 return -EINVAL;
524 } else {
525 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200526 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +0200527 return -EINVAL;
528 }
529 return 0;
530}
531
Jean Delvare656b8762010-06-03 11:33:53 +0200532/* And this is a strict address validity check, used when probing. If a
533 * device uses a reserved address, then it shouldn't be probed. 7-bit
534 * addressing is assumed, 10-bit address devices are rare and should be
535 * explicitly enumerated. */
Wolfram Sange4991ec2017-05-23 11:14:17 +0200536int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +0200537{
538 /*
539 * Reserved addresses per I2C specification:
540 * 0x00 General call address / START byte
541 * 0x01 CBUS address
542 * 0x02 Reserved for different bus format
543 * 0x03 Reserved for future purposes
544 * 0x04-0x07 Hs-mode master code
545 * 0x78-0x7b 10-bit slave addressing
546 * 0x7c-0x7f Reserved for future purposes
547 */
548 if (addr < 0x08 || addr > 0x77)
549 return -EINVAL;
550 return 0;
551}
552
Jean Delvare3b5f7942010-06-03 11:33:55 +0200553static int __i2c_check_addr_busy(struct device *dev, void *addrp)
554{
555 struct i2c_client *client = i2c_verify_client(dev);
556 int addr = *(int *)addrp;
557
Wolfram Sang9bccc702015-07-17 14:48:56 +0200558 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +0200559 return -EBUSY;
560 return 0;
561}
562
Michael Lawnick08263742010-08-11 18:21:02 +0200563/* walk up mux tree */
564static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
565{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200566 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200567 int result;
568
569 result = device_for_each_child(&adapter->dev, &addr,
570 __i2c_check_addr_busy);
571
Jean Delvare97cc4d42010-10-24 18:16:57 +0200572 if (!result && parent)
573 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200574
575 return result;
576}
577
578/* recurse down mux tree */
579static int i2c_check_mux_children(struct device *dev, void *addrp)
580{
581 int result;
582
583 if (dev->type == &i2c_adapter_type)
584 result = device_for_each_child(dev, addrp,
585 i2c_check_mux_children);
586 else
587 result = __i2c_check_addr_busy(dev, addrp);
588
589 return result;
590}
591
Jean Delvare3b5f7942010-06-03 11:33:55 +0200592static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
593{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200594 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200595 int result = 0;
596
Jean Delvare97cc4d42010-10-24 18:16:57 +0200597 if (parent)
598 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200599
600 if (!result)
601 result = device_for_each_child(&adapter->dev, &addr,
602 i2c_check_mux_children);
603
604 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200605}
606
David Brownell9c1600e2007-05-01 23:26:31 +0200607/**
Peter Rosin8320f492016-05-04 22:15:27 +0200608 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200609 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200610 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
611 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200612 */
Peter Rosin8320f492016-05-04 22:15:27 +0200613static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
614 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200615{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200616 rt_mutex_lock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200617}
Jean Delvarefe61e072010-08-11 18:20:58 +0200618
619/**
Peter Rosin8320f492016-05-04 22:15:27 +0200620 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200621 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200622 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
623 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200624 */
Peter Rosin8320f492016-05-04 22:15:27 +0200625static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
626 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200627{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200628 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200629}
630
631/**
Peter Rosin8320f492016-05-04 22:15:27 +0200632 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200633 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200634 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
635 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200636 */
Peter Rosin8320f492016-05-04 22:15:27 +0200637static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
638 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200639{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200640 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200641}
Jean Delvarefe61e072010-08-11 18:20:58 +0200642
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200643static void i2c_dev_set_name(struct i2c_adapter *adap,
Hans de Goede728fe6c2017-10-11 11:41:19 +0200644 struct i2c_client *client,
645 struct i2c_board_info const *info)
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200646{
647 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
648
Hans de Goede728fe6c2017-10-11 11:41:19 +0200649 if (info && info->dev_name) {
650 dev_set_name(&client->dev, "i2c-%s", info->dev_name);
651 return;
652 }
653
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200654 if (adev) {
655 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
656 return;
657 }
658
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200659 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +0200660 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200661}
662
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800663static int i2c_dev_irq_from_resources(const struct resource *resources,
664 unsigned int num_resources)
665{
666 struct irq_data *irqd;
667 int i;
668
669 for (i = 0; i < num_resources; i++) {
670 const struct resource *r = &resources[i];
671
672 if (resource_type(r) != IORESOURCE_IRQ)
673 continue;
674
675 if (r->flags & IORESOURCE_BITS) {
676 irqd = irq_get_irq_data(r->start);
677 if (!irqd)
678 break;
679
680 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
681 }
682
683 return r->start;
684 }
685
686 return 0;
687}
688
Jean Delvarefe61e072010-08-11 18:20:58 +0200689/**
Jean Delvaref8a227e2009-06-19 16:58:18 +0200690 * i2c_new_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200691 * @adap: the adapter managing the device
692 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200693 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200694 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200695 * Create an i2c device. Binding is handled through driver model
696 * probe()/remove() methods. A driver may be bound to this device when we
697 * return from this function, or any later moment (e.g. maybe hotplugging will
698 * load the driver module). This call is not appropriate for use by mainboard
699 * initialization logic, which usually runs during an arch_initcall() long
700 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200701 *
702 * This returns the new i2c client, which may be saved for later use with
703 * i2c_unregister_device(); or NULL to indicate an error.
704 */
705struct i2c_client *
706i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
707{
708 struct i2c_client *client;
709 int status;
710
711 client = kzalloc(sizeof *client, GFP_KERNEL);
712 if (!client)
713 return NULL;
714
715 client->adapter = adap;
716
717 client->dev.platform_data = info->platform_data;
David Brownell3bbb8352007-10-13 23:56:29 +0200718
Anton Vorontsov11f1f2a2008-10-22 20:21:33 +0200719 if (info->archdata)
720 client->dev.archdata = *info->archdata;
721
Marc Pignatee354252008-08-28 08:33:22 +0200722 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200723 client->addr = info->addr;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800724
David Brownell9c1600e2007-05-01 23:26:31 +0200725 client->irq = info->irq;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800726 if (!client->irq)
727 client->irq = i2c_dev_irq_from_resources(info->resources,
728 info->num_resources);
David Brownell9c1600e2007-05-01 23:26:31 +0200729
David Brownell9c1600e2007-05-01 23:26:31 +0200730 strlcpy(client->name, info->type, sizeof(client->name));
731
Wolfram Sangc4019b72015-07-17 12:50:06 +0200732 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +0200733 if (status) {
734 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
735 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
736 goto out_err_silent;
737 }
738
Jean Delvaref8a227e2009-06-19 16:58:18 +0200739 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +0200740 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200741 if (status)
742 goto out_err;
743
744 client->dev.parent = &client->adapter->dev;
745 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200746 client->dev.type = &i2c_client_type;
Grant Likelyd12d42f2010-04-13 16:12:28 -0700747 client->dev.of_node = info->of_node;
Rafael J. Wysockice793482015-03-16 23:49:03 +0100748 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200749
Hans de Goede728fe6c2017-10-11 11:41:19 +0200750 i2c_dev_set_name(adap, client, info);
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800751
752 if (info->properties) {
753 status = device_add_properties(&client->dev, info->properties);
754 if (status) {
755 dev_err(&adap->dev,
756 "Failed to add properties to client %s: %d\n",
757 client->name, status);
758 goto out_err;
759 }
760 }
761
Jean Delvaref8a227e2009-06-19 16:58:18 +0200762 status = device_register(&client->dev);
763 if (status)
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800764 goto out_free_props;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200765
Jean Delvaref8a227e2009-06-19 16:58:18 +0200766 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
767 client->name, dev_name(&client->dev));
768
David Brownell9c1600e2007-05-01 23:26:31 +0200769 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200770
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800771out_free_props:
772 if (info->properties)
773 device_remove_properties(&client->dev);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200774out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300775 dev_err(&adap->dev,
776 "Failed to register i2c client %s at 0x%02x (%d)\n",
777 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200778out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200779 kfree(client);
780 return NULL;
David Brownell9c1600e2007-05-01 23:26:31 +0200781}
782EXPORT_SYMBOL_GPL(i2c_new_device);
783
784
785/**
786 * i2c_unregister_device - reverse effect of i2c_new_device()
787 * @client: value returned from i2c_new_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200788 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200789 */
790void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200791{
Andy Shevchenko7b43dd12017-10-31 16:21:35 +0200792 if (!client)
793 return;
Lixin Wange0638fa2017-11-27 15:06:55 +0800794
795 if (client->dev.of_node) {
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +0200796 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Lixin Wange0638fa2017-11-27 15:06:55 +0800797 of_node_put(client->dev.of_node);
798 }
799
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300800 if (ACPI_COMPANION(&client->dev))
801 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +0200802 device_unregister(&client->dev);
803}
David Brownell9c1600e2007-05-01 23:26:31 +0200804EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200805
806
Jean Delvare60b129d2008-05-11 20:37:06 +0200807static const struct i2c_device_id dummy_id[] = {
808 { "dummy", 0 },
809 { },
810};
811
Jean Delvared2653e92008-04-29 23:11:39 +0200812static int dummy_probe(struct i2c_client *client,
813 const struct i2c_device_id *id)
814{
815 return 0;
816}
817
818static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100819{
820 return 0;
821}
822
823static struct i2c_driver dummy_driver = {
824 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200825 .probe = dummy_probe,
826 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200827 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100828};
829
830/**
831 * i2c_new_dummy - return a new i2c device bound to a dummy driver
832 * @adapter: the adapter managing the device
833 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100834 * Context: can sleep
835 *
836 * This returns an I2C client bound to the "dummy" driver, intended for use
837 * with devices that consume multiple addresses. Examples of such chips
838 * include various EEPROMS (like 24c04 and 24c08 models).
839 *
840 * These dummy devices have two main uses. First, most I2C and SMBus calls
841 * except i2c_transfer() need a client handle; the dummy will be that handle.
842 * And second, this prevents the specified address from being bound to a
843 * different driver.
844 *
845 * This returns the new i2c client, which should be saved for later use with
846 * i2c_unregister_device(); or NULL to indicate an error.
847 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100848struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100849{
850 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200851 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100852 };
853
David Brownelle9f13732008-01-27 18:14:52 +0100854 return i2c_new_device(adapter, &info);
855}
856EXPORT_SYMBOL_GPL(i2c_new_dummy);
857
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +0100858/**
859 * i2c_new_secondary_device - Helper to get the instantiated secondary address
860 * and create the associated device
861 * @client: Handle to the primary client
862 * @name: Handle to specify which secondary address to get
863 * @default_addr: Used as a fallback if no secondary address was specified
864 * Context: can sleep
865 *
866 * I2C clients can be composed of multiple I2C slaves bound together in a single
867 * component. The I2C client driver then binds to the master I2C slave and needs
868 * to create I2C dummy clients to communicate with all the other slaves.
869 *
870 * This function creates and returns an I2C dummy client whose I2C address is
871 * retrieved from the platform firmware based on the given slave name. If no
872 * address is specified by the firmware default_addr is used.
873 *
874 * On DT-based platforms the address is retrieved from the "reg" property entry
875 * cell whose "reg-names" value matches the slave name.
876 *
877 * This returns the new i2c client, which should be saved for later use with
878 * i2c_unregister_device(); or NULL to indicate an error.
879 */
880struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
881 const char *name,
882 u16 default_addr)
883{
884 struct device_node *np = client->dev.of_node;
885 u32 addr = default_addr;
886 int i;
887
888 if (np) {
889 i = of_property_match_string(np, "reg-names", name);
890 if (i >= 0)
891 of_property_read_u32_index(np, "reg", i, &addr);
892 }
893
894 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
895 return i2c_new_dummy(client->adapter, addr);
896}
897EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
898
David Brownellf37dd802007-02-13 22:09:00 +0100899/* ------------------------------------------------------------------------- */
900
David Brownell16ffadf2007-05-01 23:26:28 +0200901/* I2C bus adapters -- one roots each I2C or SMBUS segment */
902
Adrian Bunk83eaaed2007-10-13 23:56:30 +0200903static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904{
David Brownellef2c83212007-05-01 23:26:28 +0200905 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 complete(&adap->dev_released);
907}
908
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +0200909unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +0200910{
911 unsigned int depth = 0;
912
913 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
914 depth++;
915
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +0200916 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
917 "adapter depth exceeds lockdep subclass limit\n");
918
Jean Delvare390946b2012-09-10 10:14:02 +0200919 return depth;
920}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +0200921EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +0200922
923/*
Jean Delvare99cd8e22009-06-19 16:58:20 +0200924 * Let users instantiate I2C devices through sysfs. This can be used when
925 * platform initialization code doesn't contain the proper data for
926 * whatever reason. Also useful for drivers that do device detection and
927 * detection fails, either because the device uses an unexpected address,
928 * or this is a compatible device with different ID register values.
929 *
930 * Parameter checking may look overzealous, but we really don't want
931 * the user to provide incorrect parameters.
932 */
933static ssize_t
934i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
935 const char *buf, size_t count)
936{
937 struct i2c_adapter *adap = to_i2c_adapter(dev);
938 struct i2c_board_info info;
939 struct i2c_client *client;
940 char *blank, end;
941 int res;
942
Jean Delvare99cd8e22009-06-19 16:58:20 +0200943 memset(&info, 0, sizeof(struct i2c_board_info));
944
945 blank = strchr(buf, ' ');
946 if (!blank) {
947 dev_err(dev, "%s: Missing parameters\n", "new_device");
948 return -EINVAL;
949 }
950 if (blank - buf > I2C_NAME_SIZE - 1) {
951 dev_err(dev, "%s: Invalid device name\n", "new_device");
952 return -EINVAL;
953 }
954 memcpy(info.type, buf, blank - buf);
955
956 /* Parse remaining parameters, reject extra parameters */
957 res = sscanf(++blank, "%hi%c", &info.addr, &end);
958 if (res < 1) {
959 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
960 return -EINVAL;
961 }
962 if (res > 1 && end != '\n') {
963 dev_err(dev, "%s: Extra parameters\n", "new_device");
964 return -EINVAL;
965 }
966
Wolfram Sangcfa03272015-07-27 14:03:38 +0200967 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
968 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
969 info.flags |= I2C_CLIENT_TEN;
970 }
971
972 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
973 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
974 info.flags |= I2C_CLIENT_SLAVE;
975 }
976
Jean Delvare99cd8e22009-06-19 16:58:20 +0200977 client = i2c_new_device(adap, &info);
978 if (!client)
Jean Delvare3a89db52010-06-03 11:33:52 +0200979 return -EINVAL;
Jean Delvare99cd8e22009-06-19 16:58:20 +0200980
981 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +0200982 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +0200983 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +0200984 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200985 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
986 info.type, info.addr);
987
988 return count;
989}
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100990static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +0200991
992/*
993 * And of course let the users delete the devices they instantiated, if
994 * they got it wrong. This interface can only be used to delete devices
995 * instantiated by i2c_sysfs_new_device above. This guarantees that we
996 * don't delete devices to which some kernel code still has references.
997 *
998 * Parameter checking may look overzealous, but we really don't want
999 * the user to delete the wrong device.
1000 */
1001static ssize_t
1002i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1003 const char *buf, size_t count)
1004{
1005 struct i2c_adapter *adap = to_i2c_adapter(dev);
1006 struct i2c_client *client, *next;
1007 unsigned short addr;
1008 char end;
1009 int res;
1010
1011 /* Parse parameters, reject extra parameters */
1012 res = sscanf(buf, "%hi%c", &addr, &end);
1013 if (res < 1) {
1014 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1015 return -EINVAL;
1016 }
1017 if (res > 1 && end != '\n') {
1018 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1019 return -EINVAL;
1020 }
1021
1022 /* Make sure the device was added through sysfs */
1023 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001024 mutex_lock_nested(&adap->userspace_clients_lock,
1025 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001026 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1027 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001028 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001029 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1030 "delete_device", client->name, client->addr);
1031
1032 list_del(&client->detected);
1033 i2c_unregister_device(client);
1034 res = count;
1035 break;
1036 }
1037 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001038 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001039
1040 if (res < 0)
1041 dev_err(dev, "%s: Can't find device in list\n",
1042 "delete_device");
1043 return res;
1044}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001045static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1046 i2c_sysfs_delete_device);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001047
1048static struct attribute *i2c_adapter_attrs[] = {
1049 &dev_attr_name.attr,
1050 &dev_attr_new_device.attr,
1051 &dev_attr_delete_device.attr,
1052 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001053};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001054ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001055
Michael Lawnick08263742010-08-11 18:21:02 +02001056struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001057 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001058 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001059};
Michael Lawnick08263742010-08-11 18:21:02 +02001060EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Stephen Warren643dd092012-04-17 12:43:33 -06001062/**
1063 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1064 * @dev: device, probably from some driver model iterator
1065 *
1066 * When traversing the driver model tree, perhaps using driver model
1067 * iterators like @device_for_each_child(), you can't assume very much
1068 * about the nodes you find. Use this function to avoid oopses caused
1069 * by wrongly treating some non-I2C device as an i2c_adapter.
1070 */
1071struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1072{
1073 return (dev->type == &i2c_adapter_type)
1074 ? to_i2c_adapter(dev)
1075 : NULL;
1076}
1077EXPORT_SYMBOL(i2c_verify_adapter);
1078
Jean Delvare2bb50952009-09-18 22:45:46 +02001079#ifdef CONFIG_I2C_COMPAT
1080static struct class_compat *i2c_adapter_compat_class;
1081#endif
1082
David Brownell9c1600e2007-05-01 23:26:31 +02001083static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1084{
1085 struct i2c_devinfo *devinfo;
1086
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001087 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001088 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1089 if (devinfo->busnum == adapter->nr
1090 && !i2c_new_device(adapter,
1091 &devinfo->board_info))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001092 dev_err(&adapter->dev,
1093 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001094 devinfo->board_info.addr);
1095 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001096 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001097}
1098
Jean Delvare69b00892009-12-06 17:06:27 +01001099static int i2c_do_add_adapter(struct i2c_driver *driver,
1100 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001101{
Jean Delvare4735c982008-07-14 22:38:36 +02001102 /* Detect supported devices on that bus, and instantiate them */
1103 i2c_detect(adap, driver);
1104
1105 /* Let legacy drivers scan this bus for matching devices */
Jean Delvare026526f2008-01-27 18:14:49 +01001106 if (driver->attach_adapter) {
Jean Delvarea920ff42011-04-17 10:20:19 +02001107 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
1108 driver->driver.name);
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001109 dev_warn(&adap->dev,
1110 "Please use another way to instantiate your i2c_client\n");
Jean Delvare026526f2008-01-27 18:14:49 +01001111 /* We ignore the return code; if it fails, too bad */
1112 driver->attach_adapter(adap);
1113 }
1114 return 0;
1115}
1116
Jean Delvare69b00892009-12-06 17:06:27 +01001117static int __process_new_adapter(struct device_driver *d, void *data)
1118{
1119 return i2c_do_add_adapter(to_i2c_driver(d), data);
1120}
1121
Peter Rosind1ed7982016-08-25 23:07:01 +02001122static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1123 .lock_bus = i2c_adapter_lock_bus,
1124 .trylock_bus = i2c_adapter_trylock_bus,
1125 .unlock_bus = i2c_adapter_unlock_bus,
1126};
1127
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001128static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap)
1129{
1130 struct irq_domain *domain = adap->host_notify_domain;
1131 irq_hw_number_t hwirq;
1132
1133 if (!domain)
1134 return;
1135
1136 for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++)
1137 irq_dispose_mapping(irq_find_mapping(domain, hwirq));
1138
1139 irq_domain_remove(domain);
1140 adap->host_notify_domain = NULL;
1141}
1142
1143static int i2c_host_notify_irq_map(struct irq_domain *h,
1144 unsigned int virq,
1145 irq_hw_number_t hw_irq_num)
1146{
1147 irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
1148
1149 return 0;
1150}
1151
1152static const struct irq_domain_ops i2c_host_notify_irq_ops = {
1153 .map = i2c_host_notify_irq_map,
1154};
1155
1156static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap)
1157{
1158 struct irq_domain *domain;
1159
1160 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
1161 return 0;
1162
1163 domain = irq_domain_create_linear(adap->dev.fwnode,
1164 I2C_ADDR_7BITS_COUNT,
1165 &i2c_host_notify_irq_ops, adap);
1166 if (!domain)
1167 return -ENOMEM;
1168
1169 adap->host_notify_domain = domain;
1170
1171 return 0;
1172}
1173
1174/**
1175 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
1176 * I2C client.
1177 * @adap: the adapter
1178 * @addr: the I2C address of the notifying device
1179 * Context: can't sleep
1180 *
1181 * Helper function to be called from an I2C bus driver's interrupt
1182 * handler. It will schedule the Host Notify IRQ.
1183 */
1184int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
1185{
1186 int irq;
1187
1188 if (!adap)
1189 return -EINVAL;
1190
1191 irq = irq_find_mapping(adap->host_notify_domain, addr);
1192 if (irq <= 0)
1193 return -ENXIO;
1194
1195 generic_handle_irq(irq);
1196
1197 return 0;
1198}
1199EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
1200
David Brownell6e13e642007-05-01 23:26:31 +02001201static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001203 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
David Brownell1d0b19c2008-10-14 17:30:05 +02001205 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301206 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001207 res = -EAGAIN;
1208 goto out_list;
1209 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001210
Jean Delvare2236baa2010-11-15 22:40:38 +01001211 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001212 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001213 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001214
1215 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001216 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001217 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01001218 }
1219
Peter Rosind1ed7982016-08-25 23:07:01 +02001220 if (!adap->lock_ops)
1221 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001222
Mika Kuoppala194684e2009-12-06 17:06:22 +01001223 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001224 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001225 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001226 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Jean Delvare8fcfef62009-03-28 21:34:43 +01001228 /* Set default timeout to 1 second if not already set */
1229 if (adap->timeout == 0)
1230 adap->timeout = HZ;
1231
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001232 /* register soft irqs for Host Notify */
1233 res = i2c_setup_host_notify_irq_domain(adap);
1234 if (res) {
1235 pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n",
1236 adap->name, res);
1237 goto out_list;
1238 }
1239
Kay Sievers27d9c182009-01-07 14:29:16 +01001240 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001241 adap->dev.bus = &i2c_bus_type;
1242 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001243 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001244 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001245 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001246 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Phil Reidf8756c62017-08-24 17:31:04 +08001249 res = of_i2c_setup_smbus_alert(adap);
1250 if (res)
1251 goto out_reg;
1252
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001253 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1254
Charles Keepax6ada5c12015-04-16 13:05:19 +01001255 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001256 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001257 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001258
Jean Delvare2bb50952009-09-18 22:45:46 +02001259#ifdef CONFIG_I2C_COMPAT
1260 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1261 adap->dev.parent);
1262 if (res)
1263 dev_warn(&adap->dev,
1264 "Failed to create compatibility class link\n");
1265#endif
1266
Wolfram Sangd3b11d82016-07-09 13:34:59 +09001267 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301268
Jean Delvare729d6dd2009-06-19 16:58:18 +02001269 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001270 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001271 i2c_acpi_register_devices(adap);
1272 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001273
David Brownell6e13e642007-05-01 23:26:31 +02001274 if (adap->nr < __i2c_first_dynamic_bus_num)
1275 i2c_scan_static_board_info(adap);
1276
Jean Delvare4735c982008-07-14 22:38:36 +02001277 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001278 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001279 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001280 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001281
1282 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001283
Phil Reidf8756c62017-08-24 17:31:04 +08001284out_reg:
1285 init_completion(&adap->dev_released);
1286 device_unregister(&adap->dev);
1287 wait_for_completion(&adap->dev_released);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001288out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001289 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001290 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001291 mutex_unlock(&core_lock);
1292 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
David Brownell6e13e642007-05-01 23:26:31 +02001295/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001296 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1297 * @adap: the adapter to register (with adap->nr initialized)
1298 * Context: can sleep
1299 *
1300 * See i2c_add_numbered_adapter() for details.
1301 */
1302static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1303{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001304 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001305
1306 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001307 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001308 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001309 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001310 return id == -ENOSPC ? -EBUSY : id;
1311
1312 return i2c_register_adapter(adap);
1313}
1314
1315/**
David Brownell6e13e642007-05-01 23:26:31 +02001316 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1317 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001318 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001319 *
1320 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001321 * doesn't matter or when its bus number is specified by an dt alias.
1322 * Examples of bases when the bus number doesn't matter: I2C adapters
1323 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001324 *
1325 * When this returns zero, a new bus number was allocated and stored
1326 * in adap->nr, and the specified adapter became available for clients.
1327 * Otherwise, a negative errno value is returned.
1328 */
1329int i2c_add_adapter(struct i2c_adapter *adapter)
1330{
Doug Andersonee5c2742013-03-01 08:57:31 -08001331 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001332 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001333
Doug Andersonee5c2742013-03-01 08:57:31 -08001334 if (dev->of_node) {
1335 id = of_alias_get_id(dev->of_node, "i2c");
1336 if (id >= 0) {
1337 adapter->nr = id;
1338 return __i2c_add_numbered_adapter(adapter);
1339 }
1340 }
1341
Jean Delvarecaada322008-01-27 18:14:49 +01001342 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001343 id = idr_alloc(&i2c_adapter_idr, adapter,
1344 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001345 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001346 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08001347 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001348
1349 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001350
David Brownell6e13e642007-05-01 23:26:31 +02001351 return i2c_register_adapter(adapter);
1352}
1353EXPORT_SYMBOL(i2c_add_adapter);
1354
1355/**
1356 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1357 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001358 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001359 *
1360 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001361 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1362 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001363 * is used to properly configure I2C devices.
1364 *
Grant Likely488bf312011-07-25 17:49:43 +02001365 * If the requested bus number is set to -1, then this function will behave
1366 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1367 *
David Brownell6e13e642007-05-01 23:26:31 +02001368 * If no devices have pre-been declared for this bus, then be sure to
1369 * register the adapter before any dynamically allocated ones. Otherwise
1370 * the required bus ID may not be available.
1371 *
1372 * When this returns zero, the specified adapter became available for
1373 * clients using the bus number provided in adap->nr. Also, the table
1374 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1375 * and the appropriate driver model device nodes are created. Otherwise, a
1376 * negative errno value is returned.
1377 */
1378int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1379{
Grant Likely488bf312011-07-25 17:49:43 +02001380 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1381 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001382
Doug Andersonee5c2742013-03-01 08:57:31 -08001383 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001384}
1385EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1386
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001387static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001388 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001389{
Jean Delvare4735c982008-07-14 22:38:36 +02001390 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001391
Jean Delvareacec2112009-03-28 21:34:40 +01001392 /* Remove the devices we created ourselves as the result of hardware
1393 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001394 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1395 if (client->adapter == adapter) {
1396 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1397 client->name, client->addr);
1398 list_del(&client->detected);
1399 i2c_unregister_device(client);
1400 }
1401 }
Jean Delvare026526f2008-01-27 18:14:49 +01001402}
1403
Jean Delvaree549c2b2009-06-19 16:58:19 +02001404static int __unregister_client(struct device *dev, void *dummy)
1405{
1406 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001407 if (client && strcmp(client->name, "dummy"))
1408 i2c_unregister_device(client);
1409 return 0;
1410}
1411
1412static int __unregister_dummy(struct device *dev, void *dummy)
1413{
1414 struct i2c_client *client = i2c_verify_client(dev);
Andy Shevchenko7b43dd12017-10-31 16:21:35 +02001415 i2c_unregister_device(client);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001416 return 0;
1417}
1418
Jean Delvare69b00892009-12-06 17:06:27 +01001419static int __process_removed_adapter(struct device_driver *d, void *data)
1420{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001421 i2c_do_del_adapter(to_i2c_driver(d), data);
1422 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001423}
1424
David Brownelld64f73b2007-07-12 14:12:28 +02001425/**
1426 * i2c_del_adapter - unregister I2C adapter
1427 * @adap: the adapter being unregistered
1428 * Context: can sleep
1429 *
1430 * This unregisters an I2C adapter which was previously registered
1431 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1432 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001433void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001435 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001436 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001439 mutex_lock(&core_lock);
1440 found = idr_find(&i2c_adapter_idr, adap->nr);
1441 mutex_unlock(&core_lock);
1442 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001443 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001444 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001447 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001448 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001449 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001450 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001451 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001452 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001454 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001455 mutex_lock_nested(&adap->userspace_clients_lock,
1456 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001457 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1458 detected) {
1459 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1460 client->addr);
1461 list_del(&client->detected);
1462 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001463 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001464 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001465
Jean Delvaree549c2b2009-06-19 16:58:19 +02001466 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001467 * check the returned value. This is a two-pass process, because
1468 * we can't remove the dummy devices during the first pass: they
1469 * could have been instantiated by real devices wishing to clean
1470 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001471 device_for_each_child(&adap->dev, NULL, __unregister_client);
1472 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Jean Delvare2bb50952009-09-18 22:45:46 +02001474#ifdef CONFIG_I2C_COMPAT
1475 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1476 adap->dev.parent);
1477#endif
1478
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001479 /* device name is gone after device_unregister */
1480 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1481
Wolfram Sang9f924162015-12-23 18:19:28 +01001482 pm_runtime_disable(&adap->dev);
1483
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001484 i2c_host_notify_irq_teardown(adap);
1485
Wolfram Sang26680ee2015-01-29 22:45:09 +01001486 /* wait until all references to the device are gone
1487 *
1488 * FIXME: This is old code and should ideally be replaced by an
1489 * alternative which results in decoupling the lifetime of the struct
1490 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301491 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001492 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
David Brownell6e13e642007-05-01 23:26:31 +02001497 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001498 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001500 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001502 /* Clear the device structure in case this adapter is ever going to be
1503 added again */
1504 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505}
David Brownellc0564602007-05-01 23:26:31 +02001506EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Wolfram Sang54177cc2015-12-17 13:32:36 +01001508/**
1509 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
1510 * @dev: The device to scan for I2C timing properties
1511 * @t: the i2c_timings struct to be filled with values
1512 * @use_defaults: bool to use sane defaults derived from the I2C specification
1513 * when properties are not found, otherwise use 0
1514 *
1515 * Scan the device for the generic I2C properties describing timing parameters
1516 * for the signal and fill the given struct with the results. If a property was
1517 * not found and use_defaults was true, then maximum timings are assumed which
1518 * are derived from the I2C specification. If use_defaults is not used, the
1519 * results will be 0, so drivers can apply their own defaults later. The latter
1520 * is mainly intended for avoiding regressions of existing drivers which want
1521 * to switch to this function. New drivers almost always should use the defaults.
1522 */
1523
1524void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
1525{
1526 int ret;
1527
1528 memset(t, 0, sizeof(*t));
1529
1530 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
1531 if (ret && use_defaults)
1532 t->bus_freq_hz = 100000;
1533
1534 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
1535 if (ret && use_defaults) {
1536 if (t->bus_freq_hz <= 100000)
1537 t->scl_rise_ns = 1000;
1538 else if (t->bus_freq_hz <= 400000)
1539 t->scl_rise_ns = 300;
1540 else
1541 t->scl_rise_ns = 120;
1542 }
1543
1544 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
1545 if (ret && use_defaults) {
1546 if (t->bus_freq_hz <= 400000)
1547 t->scl_fall_ns = 300;
1548 else
1549 t->scl_fall_ns = 120;
1550 }
1551
1552 device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
1553
1554 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
1555 if (ret && use_defaults)
1556 t->sda_fall_ns = t->scl_fall_ns;
1557}
1558EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
1559
David Brownell7b4fbc52007-05-01 23:26:30 +02001560/* ------------------------------------------------------------------------- */
1561
Jean Delvare7ae31482011-03-20 14:50:52 +01001562int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1563{
1564 int res;
1565
1566 mutex_lock(&core_lock);
1567 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1568 mutex_unlock(&core_lock);
1569
1570 return res;
1571}
1572EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1573
Jean Delvare69b00892009-12-06 17:06:27 +01001574static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001575{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001576 if (dev->type != &i2c_adapter_type)
1577 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001578 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001579}
1580
David Brownell7b4fbc52007-05-01 23:26:30 +02001581/*
1582 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001583 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 */
1585
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001586int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001588 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
David Brownell1d0b19c2008-10-14 17:30:05 +02001590 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301591 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02001592 return -EAGAIN;
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001595 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02001597 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598
Jean Delvare729d6dd2009-06-19 16:58:18 +02001599 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001600 * will have called probe() for all matching-but-unbound devices.
1601 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 res = driver_register(&driver->driver);
1603 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001604 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001605
Wolfram Sang44239a52016-07-09 13:35:04 +09001606 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Jean Delvare4735c982008-07-14 22:38:36 +02001608 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001609 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001610
Jean Delvare7eebcb72006-02-05 23:28:21 +01001611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001613EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Jean Delvare69b00892009-12-06 17:06:27 +01001615static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001616{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001617 if (dev->type == &i2c_adapter_type)
1618 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1619 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001620}
1621
David Brownella1d9e6e2007-05-01 23:26:30 +02001622/**
1623 * i2c_del_driver - unregister I2C driver
1624 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001625 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001626 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001627void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
Jean Delvare7ae31482011-03-20 14:50:52 +01001629 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09001632 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
David Brownellc0564602007-05-01 23:26:31 +02001634EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
David Brownell7b4fbc52007-05-01 23:26:30 +02001636/* ------------------------------------------------------------------------- */
1637
Jean Delvaree48d3312008-01-27 18:14:48 +01001638/**
1639 * i2c_use_client - increments the reference count of the i2c client structure
1640 * @client: the client being referenced
1641 *
1642 * Each live reference to a client should be refcounted. The driver model does
1643 * that automatically as part of driver binding, so that most drivers don't
1644 * need to do this explicitly: they hold a reference until they're unbound
1645 * from the device.
1646 *
1647 * A pointer to the client with the incremented reference counter is returned.
1648 */
1649struct i2c_client *i2c_use_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
David Brownell6ea438e2008-07-14 22:38:24 +02001651 if (client && get_device(&client->dev))
1652 return client;
1653 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
David Brownellc0564602007-05-01 23:26:31 +02001655EXPORT_SYMBOL(i2c_use_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Jean Delvaree48d3312008-01-27 18:14:48 +01001657/**
1658 * i2c_release_client - release a use of the i2c client structure
1659 * @client: the client being no longer referenced
1660 *
1661 * Must be called when a user of a client is finished with it.
1662 */
1663void i2c_release_client(struct i2c_client *client)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
David Brownell6ea438e2008-07-14 22:38:24 +02001665 if (client)
1666 put_device(&client->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
David Brownellc0564602007-05-01 23:26:31 +02001668EXPORT_SYMBOL(i2c_release_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
David Brownell9b766b82008-01-27 18:14:51 +01001670struct i2c_cmd_arg {
1671 unsigned cmd;
1672 void *arg;
1673};
1674
1675static int i2c_cmd(struct device *dev, void *_arg)
1676{
1677 struct i2c_client *client = i2c_verify_client(dev);
1678 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001679 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001680
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001681 if (!client || !client->dev.driver)
1682 return 0;
1683
1684 driver = to_i2c_driver(client->dev.driver);
1685 if (driver->command)
1686 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001687 return 0;
1688}
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1691{
David Brownell9b766b82008-01-27 18:14:51 +01001692 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
David Brownell9b766b82008-01-27 18:14:51 +01001694 cmd_arg.cmd = cmd;
1695 cmd_arg.arg = arg;
1696 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
David Brownellc0564602007-05-01 23:26:31 +02001698EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700static int __init i2c_init(void)
1701{
1702 int retval;
1703
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001704 retval = of_alias_get_highest_id("i2c");
1705
1706 down_write(&__i2c_board_lock);
1707 if (retval >= __i2c_first_dynamic_bus_num)
1708 __i2c_first_dynamic_bus_num = retval + 1;
1709 up_write(&__i2c_board_lock);
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 retval = bus_register(&i2c_bus_type);
1712 if (retval)
1713 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01001714
1715 is_registered = true;
1716
Jean Delvare2bb50952009-09-18 22:45:46 +02001717#ifdef CONFIG_I2C_COMPAT
1718 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1719 if (!i2c_adapter_compat_class) {
1720 retval = -ENOMEM;
1721 goto bus_err;
1722 }
1723#endif
David Brownelle9f13732008-01-27 18:14:52 +01001724 retval = i2c_add_driver(&dummy_driver);
1725 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001726 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001727
1728 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1729 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001730 if (IS_ENABLED(CONFIG_ACPI))
1731 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001732
David Brownelle9f13732008-01-27 18:14:52 +01001733 return 0;
1734
Jean Delvare2bb50952009-09-18 22:45:46 +02001735class_err:
1736#ifdef CONFIG_I2C_COMPAT
1737 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001738bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001739#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01001740 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01001741 bus_unregister(&i2c_bus_type);
1742 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
1745static void __exit i2c_exit(void)
1746{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001747 if (IS_ENABLED(CONFIG_ACPI))
1748 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001749 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1750 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001751 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001752#ifdef CONFIG_I2C_COMPAT
1753 class_compat_unregister(i2c_adapter_compat_class);
1754#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001756 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757}
1758
David Brownella10f9e72008-10-14 17:30:06 +02001759/* We must initialize early, because some subsystems register i2c drivers
1760 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1761 */
1762postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763module_exit(i2c_exit);
1764
1765/* ----------------------------------------------------
1766 * the functional interface to the i2c busses.
1767 * ----------------------------------------------------
1768 */
1769
Wolfram Sangb7f62582015-01-05 23:45:59 +01001770/* Check if val is exceeding the quirk IFF quirk is non 0 */
1771#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1772
1773static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
1774{
1775 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
1776 err_msg, msg->addr, msg->len,
1777 msg->flags & I2C_M_RD ? "read" : "write");
1778 return -EOPNOTSUPP;
1779}
1780
1781static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1782{
1783 const struct i2c_adapter_quirks *q = adap->quirks;
1784 int max_num = q->max_num_msgs, i;
1785 bool do_len_check = true;
1786
1787 if (q->flags & I2C_AQ_COMB) {
1788 max_num = 2;
1789
1790 /* special checks for combined messages */
1791 if (num == 2) {
1792 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
1793 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
1794
1795 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
1796 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
1797
1798 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
1799 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
1800
1801 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
1802 return i2c_quirk_error(adap, &msgs[0], "msg too long");
1803
1804 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
1805 return i2c_quirk_error(adap, &msgs[1], "msg too long");
1806
1807 do_len_check = false;
1808 }
1809 }
1810
1811 if (i2c_quirk_exceeded(num, max_num))
1812 return i2c_quirk_error(adap, &msgs[0], "too many messages");
1813
1814 for (i = 0; i < num; i++) {
1815 u16 len = msgs[i].len;
1816
1817 if (msgs[i].flags & I2C_M_RD) {
1818 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
1819 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1820 } else {
1821 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
1822 return i2c_quirk_error(adap, &msgs[i], "msg too long");
1823 }
1824 }
1825
1826 return 0;
1827}
1828
David Brownella1cdeda2008-07-14 22:38:24 +02001829/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001830 * __i2c_transfer - unlocked flavor of i2c_transfer
1831 * @adap: Handle to I2C bus
1832 * @msgs: One or more messages to execute before STOP is issued to
1833 * terminate the operation; each message begins with a START.
1834 * @num: Number of messages to be executed.
1835 *
1836 * Returns negative errno, else the number of messages executed.
1837 *
1838 * Adapter lock must be held when calling this function. No debug logging
1839 * takes place. adap->algo->master_xfer existence isn't checked.
1840 */
1841int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1842{
1843 unsigned long orig_jiffies;
1844 int ret, try;
1845
Wolfram Sangb7f62582015-01-05 23:45:59 +01001846 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
1847 return -EOPNOTSUPP;
1848
David Howellsd9a83d62014-03-06 13:35:59 +00001849 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
1850 * enabled. This is an efficient way of keeping the for-loop from
1851 * being executed when not needed.
1852 */
1853 if (static_key_false(&i2c_trace_msg)) {
1854 int i;
1855 for (i = 0; i < num; i++)
1856 if (msgs[i].flags & I2C_M_RD)
1857 trace_i2c_read(adap, &msgs[i], i);
1858 else
1859 trace_i2c_write(adap, &msgs[i], i);
1860 }
1861
Jean Delvareb37d2a32012-06-29 07:47:19 -03001862 /* Retry automatically on arbitration loss */
1863 orig_jiffies = jiffies;
1864 for (ret = 0, try = 0; try <= adap->retries; try++) {
1865 ret = adap->algo->master_xfer(adap, msgs, num);
1866 if (ret != -EAGAIN)
1867 break;
1868 if (time_after(jiffies, orig_jiffies + adap->timeout))
1869 break;
1870 }
1871
David Howellsd9a83d62014-03-06 13:35:59 +00001872 if (static_key_false(&i2c_trace_msg)) {
1873 int i;
1874 for (i = 0; i < ret; i++)
1875 if (msgs[i].flags & I2C_M_RD)
1876 trace_i2c_reply(adap, &msgs[i], i);
1877 trace_i2c_result(adap, i, ret);
1878 }
1879
Jean Delvareb37d2a32012-06-29 07:47:19 -03001880 return ret;
1881}
1882EXPORT_SYMBOL(__i2c_transfer);
1883
1884/**
David Brownella1cdeda2008-07-14 22:38:24 +02001885 * i2c_transfer - execute a single or combined I2C message
1886 * @adap: Handle to I2C bus
1887 * @msgs: One or more messages to execute before STOP is issued to
1888 * terminate the operation; each message begins with a START.
1889 * @num: Number of messages to be executed.
1890 *
1891 * Returns negative errno, else the number of messages executed.
1892 *
1893 * Note that there is no requirement that each message be sent to
1894 * the same slave address, although that is the most common model.
1895 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001896int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001898 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
David Brownella1cdeda2008-07-14 22:38:24 +02001900 /* REVISIT the fault reporting model here is weak:
1901 *
1902 * - When we get an error after receiving N bytes from a slave,
1903 * there is no way to report "N".
1904 *
1905 * - When we get a NAK after transmitting N bytes to a slave,
1906 * there is no way to report "N" ... or to let the master
1907 * continue executing the rest of this combined message, if
1908 * that's the appropriate response.
1909 *
1910 * - When for example "num" is two and we successfully complete
1911 * the first message but get an error part way through the
1912 * second, it's unclear whether that should be reported as
1913 * one (discarding status on the second message) or errno
1914 * (discarding status on the first one).
1915 */
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (adap->algo->master_xfer) {
1918#ifdef DEBUG
1919 for (ret = 0; ret < num; ret++) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03001920 dev_dbg(&adap->dev,
1921 "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
1922 ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
1923 msgs[ret].addr, msgs[ret].len,
Jean Delvare209d27c2007-05-01 23:26:29 +02001924 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
1926#endif
1927
Mike Rapoportcea443a82008-01-27 18:14:50 +01001928 if (in_atomic() || irqs_disabled()) {
Peter Rosinfb79e092016-06-29 15:04:03 +02001929 ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001930 if (!ret)
1931 /* I2C activity is ongoing. */
1932 return -EAGAIN;
1933 } else {
Peter Rosin8320f492016-05-04 22:15:27 +02001934 i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
Mike Rapoportcea443a82008-01-27 18:14:50 +01001935 }
1936
Jean Delvareb37d2a32012-06-29 07:47:19 -03001937 ret = __i2c_transfer(adap, msgs, num);
Peter Rosin8320f492016-05-04 22:15:27 +02001938 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
1940 return ret;
1941 } else {
1942 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
David Brownell24a5bb72008-07-14 22:38:23 +02001943 return -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945}
David Brownellc0564602007-05-01 23:26:31 +02001946EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
David Brownella1cdeda2008-07-14 22:38:24 +02001948/**
Wolfram Sang8a917322017-11-04 21:20:04 +01001949 * i2c_transfer_buffer_flags - issue a single I2C message transferring data
1950 * to/from a buffer
David Brownella1cdeda2008-07-14 22:38:24 +02001951 * @client: Handle to slave device
Wolfram Sang8a917322017-11-04 21:20:04 +01001952 * @buf: Where the data is stored
1953 * @count: How many bytes to transfer, must be less than 64k since msg.len is u16
1954 * @flags: The flags to be used for the message, e.g. I2C_M_RD for reads
David Brownella1cdeda2008-07-14 22:38:24 +02001955 *
Wolfram Sang8a917322017-11-04 21:20:04 +01001956 * Returns negative errno, or else the number of bytes transferred.
David Brownella1cdeda2008-07-14 22:38:24 +02001957 */
Wolfram Sang8a917322017-11-04 21:20:04 +01001958int i2c_transfer_buffer_flags(const struct i2c_client *client, char *buf,
1959 int count, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960{
1961 int ret;
Wolfram Sang8a917322017-11-04 21:20:04 +01001962 struct i2c_msg msg = {
1963 .addr = client->addr,
1964 .flags = flags | (client->flags & I2C_M_TEN),
1965 .len = count,
1966 .buf = buf,
1967 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Wolfram Sang8a917322017-11-04 21:20:04 +01001969 ret = i2c_transfer(client->adapter, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001971 /*
Wolfram Sang8a917322017-11-04 21:20:04 +01001972 * If everything went ok (i.e. 1 msg transferred), return #bytes
1973 * transferred, else error code.
Wolfram Sang834aa6f2012-03-15 18:11:05 +01001974 */
Jean Delvare815f55f2005-05-07 22:58:46 +02001975 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
Wolfram Sang8a917322017-11-04 21:20:04 +01001977EXPORT_SYMBOL(i2c_transfer_buffer_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979/* ----------------------------------------------------
1980 * the i2c address scanning function
1981 * Will not work for 10-bit addresses!
1982 * ----------------------------------------------------
1983 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02001984
Jean Delvare63e4e802010-06-03 11:33:51 +02001985/*
1986 * Legacy default probe function, mostly relevant for SMBus. The default
1987 * probe method is a quick write, but it is known to corrupt the 24RF08
1988 * EEPROMs due to a state machine bug, and could also irreversibly
1989 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1990 * we use a short byte read instead. Also, some bus drivers don't implement
1991 * quick write, so we fallback to a byte read in that case too.
1992 * On x86, there is another special case for FSC hardware monitoring chips,
1993 * which want regular byte reads (address 0x73.) Fortunately, these are the
1994 * only known chips using this I2C address on PC hardware.
1995 * Returns 1 if probe succeeded, 0 if not.
1996 */
1997static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1998{
1999 int err;
2000 union i2c_smbus_data dummy;
2001
2002#ifdef CONFIG_X86
2003 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2004 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2005 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2006 I2C_SMBUS_BYTE_DATA, &dummy);
2007 else
2008#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002009 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2010 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002011 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2012 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002013 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2014 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2015 I2C_SMBUS_BYTE, &dummy);
2016 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002017 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2018 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002019 err = -EOPNOTSUPP;
2020 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002021
2022 return err >= 0;
2023}
2024
Jean Delvareccfbbd02009-12-06 17:06:25 +01002025static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002026 struct i2c_driver *driver)
2027{
2028 struct i2c_board_info info;
2029 struct i2c_adapter *adapter = temp_client->adapter;
2030 int addr = temp_client->addr;
2031 int err;
2032
2033 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002034 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002035 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002036 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2037 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002038 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002039 }
2040
Wolfram Sang9bccc702015-07-17 14:48:56 +02002041 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002042 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002043 return 0;
2044
Jean Delvareccfbbd02009-12-06 17:06:25 +01002045 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002046 if (!i2c_default_probe(adapter, addr))
2047 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002048
2049 /* Finally call the custom detection function */
2050 memset(&info, 0, sizeof(struct i2c_board_info));
2051 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002052 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002053 if (err) {
2054 /* -ENODEV is returned if the detection fails. We catch it
2055 here as this isn't an error. */
2056 return err == -ENODEV ? 0 : err;
2057 }
2058
2059 /* Consistency check */
2060 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002061 dev_err(&adapter->dev,
2062 "%s detection function provided no name for 0x%x\n",
2063 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002064 } else {
2065 struct i2c_client *client;
2066
2067 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002068 if (adapter->class & I2C_CLASS_DEPRECATED)
2069 dev_warn(&adapter->dev,
2070 "This adapter will soon drop class based instantiation of devices. "
2071 "Please make sure client 0x%02x gets instantiated by other means. "
2072 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2073 info.addr);
2074
Jean Delvare4735c982008-07-14 22:38:36 +02002075 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2076 info.type, info.addr);
2077 client = i2c_new_device(adapter, &info);
2078 if (client)
2079 list_add_tail(&client->detected, &driver->clients);
2080 else
2081 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2082 info.type, info.addr);
2083 }
2084 return 0;
2085}
2086
2087static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2088{
Jean Delvarec3813d62009-12-14 21:17:25 +01002089 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002090 struct i2c_client *temp_client;
2091 int i, err = 0;
2092 int adap_id = i2c_adapter_id(adapter);
2093
Jean Delvarec3813d62009-12-14 21:17:25 +01002094 address_list = driver->address_list;
2095 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002096 return 0;
2097
Wolfram Sang45552272014-07-10 13:46:21 +02002098 /* Warn that the adapter lost class based instantiation */
2099 if (adapter->class == I2C_CLASS_DEPRECATED) {
2100 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002101 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2102 "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002103 driver->driver.name);
2104 return 0;
2105 }
2106
Jean Delvare51b54ba2010-10-24 18:16:58 +02002107 /* Stop here if the classes do not match */
2108 if (!(adapter->class & driver->class))
2109 return 0;
2110
Jean Delvare4735c982008-07-14 22:38:36 +02002111 /* Set up a temporary client to help detect callback */
2112 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2113 if (!temp_client)
2114 return -ENOMEM;
2115 temp_client->adapter = adapter;
2116
Jean Delvarec3813d62009-12-14 21:17:25 +01002117 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002118 dev_dbg(&adapter->dev,
2119 "found normal entry for adapter %d, addr 0x%02x\n",
2120 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002121 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002122 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002123 if (unlikely(err))
2124 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002125 }
2126
Jean Delvare4735c982008-07-14 22:38:36 +02002127 kfree(temp_client);
2128 return err;
2129}
2130
Jean Delvared44f19d2010-08-11 18:20:57 +02002131int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2132{
2133 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2134 I2C_SMBUS_QUICK, NULL) >= 0;
2135}
2136EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2137
Jean Delvare12b5053a2007-05-01 23:26:31 +02002138struct i2c_client *
2139i2c_new_probed_device(struct i2c_adapter *adap,
2140 struct i2c_board_info *info,
Jean Delvare9a942412010-08-11 18:20:56 +02002141 unsigned short const *addr_list,
2142 int (*probe)(struct i2c_adapter *, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002143{
2144 int i;
2145
Jean Delvare8031d792010-08-11 18:21:00 +02002146 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002147 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002148
Jean Delvare12b5053a2007-05-01 23:26:31 +02002149 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2150 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002151 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002152 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2153 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002154 continue;
2155 }
2156
Wolfram Sang9bccc702015-07-17 14:48:56 +02002157 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002158 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002159 dev_dbg(&adap->dev,
2160 "Address 0x%02x already in use, not probing\n",
2161 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002162 continue;
2163 }
2164
Jean Delvare63e4e802010-06-03 11:33:51 +02002165 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002166 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002167 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002168 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002169
2170 if (addr_list[i] == I2C_CLIENT_END) {
2171 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2172 return NULL;
2173 }
2174
2175 info->addr = addr_list[i];
2176 return i2c_new_device(adap, info);
2177}
2178EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2179
Jean Delvared735b342011-03-20 14:50:52 +01002180struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002183
Jean Delvarecaada322008-01-27 18:14:49 +01002184 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002185 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002186 if (!adapter)
2187 goto exit;
2188
2189 if (try_module_get(adapter->owner))
2190 get_device(&adapter->dev);
2191 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002192 adapter = NULL;
2193
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002194 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002195 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002196 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
David Brownellc0564602007-05-01 23:26:31 +02002198EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199
2200void i2c_put_adapter(struct i2c_adapter *adap)
2201{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002202 if (!adap)
2203 return;
2204
2205 put_device(&adap->dev);
2206 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207}
David Brownellc0564602007-05-01 23:26:31 +02002208EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002210/**
2211 * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg
2212 * @msg: the message to be checked
2213 * @threshold: the minimum number of bytes for which using DMA makes sense
2214 *
2215 * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.
2216 * Or a valid pointer to be used with DMA. After use, release it by
2217 * calling i2c_release_dma_safe_msg_buf().
2218 *
2219 * This function must only be called from process context!
2220 */
2221u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)
2222{
2223 if (msg->len < threshold)
2224 return NULL;
2225
2226 if (msg->flags & I2C_M_DMA_SAFE)
2227 return msg->buf;
2228
2229 pr_debug("using bounce buffer for addr=0x%02x, len=%d\n",
2230 msg->addr, msg->len);
2231
2232 if (msg->flags & I2C_M_RD)
2233 return kzalloc(msg->len, GFP_KERNEL);
2234 else
2235 return kmemdup(msg->buf, msg->len, GFP_KERNEL);
2236}
2237EXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf);
2238
2239/**
2240 * i2c_release_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
2241 * @msg: the message to be synced with
2242 * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.
2243 */
2244void i2c_release_dma_safe_msg_buf(struct i2c_msg *msg, u8 *buf)
2245{
2246 if (!buf || buf == msg->buf)
2247 return;
2248
2249 if (msg->flags & I2C_M_RD)
2250 memcpy(msg->buf, buf, msg->len);
2251
2252 kfree(buf);
2253}
2254EXPORT_SYMBOL_GPL(i2c_release_dma_safe_msg_buf);
2255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2257MODULE_DESCRIPTION("I2C-Bus main module");
2258MODULE_LICENSE("GPL");