blob: 3478d208d174b4ea52c09e698539149368b90352 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Wolfram Sang61e3d0f2017-05-23 19:23:11 +02002/*
3 * Linux I2C core
4 *
5 * Copyright (C) 1995-99 Simon G. Vogl
6 * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>
7 * Mux support by Rodolfo Giometti <giometti@enneenne.com> and
8 * Michael Lawnick <michael.lawnick.ext@nsn.com>
9 *
Wolfram Sang2f5a55c2020-05-02 14:18:35 +020010 * Copyright (C) 2013-2017 Wolfram Sang <wsa@kernel.org>
Wolfram Sang687b81d2013-07-11 12:56:15 +010011 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
Wolfram Sang44239a52016-07-09 13:35:04 +090013#define pr_fmt(fmt) "i2c-core: " fmt
14
Wolfram Sangb4e2f6a2015-05-19 21:04:40 +020015#include <dt-bindings/i2c/i2c.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010016#include <linux/acpi.h>
Sylwester Nawrocki86be4082014-06-18 17:29:32 +020017#include <linux/clk/clk-conf.h>
Jean Delvareb8d6f452007-02-13 22:09:00 +010018#include <linux/completion.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010019#include <linux/delay.h>
Pantelis Antonioua430a3452014-10-28 22:36:02 +020020#include <linux/err.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010021#include <linux/errno.h>
Phil Reid10c9ef02017-11-28 11:09:10 +080022#include <linux/gpio/consumer.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010023#include <linux/i2c.h>
Phil Reidf8756c62017-08-24 17:31:04 +080024#include <linux/i2c-smbus.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010025#include <linux/idr.h>
26#include <linux/init.h>
27#include <linux/irqflags.h>
28#include <linux/jump_label.h>
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/mutex.h>
32#include <linux/of_device.h>
33#include <linux/of.h>
34#include <linux/of_irq.h>
Codrin Ciubotariu75820312020-08-04 12:59:24 +030035#include <linux/pinctrl/consumer.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010036#include <linux/pm_domain.h>
37#include <linux/pm_runtime.h>
38#include <linux/pm_wakeirq.h>
Wolfram Sange1dba012015-12-08 10:37:46 +010039#include <linux/property.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010040#include <linux/rwsem.h>
41#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
David Brownell9c1600e2007-05-01 23:26:31 +020043#include "i2c-core.h"
44
David Howellsd9a83d62014-03-06 13:35:59 +000045#define CREATE_TRACE_POINTS
46#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Wolfram Sangda899f52015-05-18 21:09:12 +020048#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
49#define I2C_ADDR_OFFSET_SLAVE 0x1000
50
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +020051#define I2C_ADDR_7BITS_MAX 0x77
52#define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1)
53
Peter Rosindde67eb2018-01-22 08:32:01 +010054#define I2C_ADDR_DEVICE_ID 0x7c
55
Wolfram Sang61e3d0f2017-05-23 19:23:11 +020056/*
57 * core_lock protects i2c_adapter_idr, and guarantees that device detection,
Wolfram Sang0c36dd32018-08-21 17:02:40 +020058 * deletion of detected devices are serialized
Wolfram Sang61e3d0f2017-05-23 19:23:11 +020059 */
Jean Delvarecaada322008-01-27 18:14:49 +010060static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static DEFINE_IDR(i2c_adapter_idr);
62
Jean Delvare4735c982008-07-14 22:38:36 +020063static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010064
Davidlohr Bueso50888b02018-03-26 14:09:24 -070065static DEFINE_STATIC_KEY_FALSE(i2c_trace_msg_key);
Sudip Mukherjee95026652016-03-07 17:19:17 +053066static bool is_registered;
David Howellsd9a83d62014-03-06 13:35:59 +000067
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050068int i2c_transfer_trace_reg(void)
David Howellsd9a83d62014-03-06 13:35:59 +000069{
Davidlohr Bueso50888b02018-03-26 14:09:24 -070070 static_branch_inc(&i2c_trace_msg_key);
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050071 return 0;
David Howellsd9a83d62014-03-06 13:35:59 +000072}
73
74void i2c_transfer_trace_unreg(void)
75{
Davidlohr Bueso50888b02018-03-26 14:09:24 -070076 static_branch_dec(&i2c_trace_msg_key);
David Howellsd9a83d62014-03-06 13:35:59 +000077}
78
Yicong Yang3b4c7472021-04-08 19:17:18 +080079const char *i2c_freq_mode_string(u32 bus_freq_hz)
80{
81 switch (bus_freq_hz) {
82 case I2C_MAX_STANDARD_MODE_FREQ:
83 return "Standard Mode (100 kHz)";
84 case I2C_MAX_FAST_MODE_FREQ:
85 return "Fast Mode (400 kHz)";
86 case I2C_MAX_FAST_MODE_PLUS_FREQ:
87 return "Fast Mode Plus (1.0 MHz)";
88 case I2C_MAX_TURBO_MODE_FREQ:
89 return "Turbo Mode (1.4 MHz)";
90 case I2C_MAX_HIGH_SPEED_MODE_FREQ:
91 return "High Speed Mode (3.4 MHz)";
92 case I2C_MAX_ULTRA_FAST_MODE_FREQ:
93 return "Ultra Fast Mode (5.0 MHz)";
94 default:
95 return "Unknown Mode";
96 }
97}
98EXPORT_SYMBOL_GPL(i2c_freq_mode_string);
99
Lee Jones5f441fc2016-11-07 12:47:40 +0000100const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
Jean Delvared2653e92008-04-29 23:11:39 +0200101 const struct i2c_client *client)
102{
Lee Jones811073b2016-11-07 12:47:36 +0000103 if (!(id && client))
104 return NULL;
105
Jean Delvared2653e92008-04-29 23:11:39 +0200106 while (id->name[0]) {
107 if (strcmp(client->name, id->name) == 0)
108 return id;
109 id++;
110 }
111 return NULL;
112}
Lee Jones5f441fc2016-11-07 12:47:40 +0000113EXPORT_SYMBOL_GPL(i2c_match_id);
Jean Delvared2653e92008-04-29 23:11:39 +0200114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115static int i2c_device_match(struct device *dev, struct device_driver *drv)
116{
Jean Delvare51298d12009-09-18 22:45:45 +0200117 struct i2c_client *client = i2c_verify_client(dev);
118 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +0200119
Jean Delvare51298d12009-09-18 22:45:45 +0200120
Grant Likely959e85f2010-06-08 07:48:19 -0600121 /* Attempt an OF style match */
Lee Jonesda10c062016-11-07 12:47:39 +0000122 if (i2c_of_match_device(drv->of_match_table, client))
Grant Likely959e85f2010-06-08 07:48:19 -0600123 return 1;
124
Mika Westerberg907ddf82012-11-23 12:23:40 +0100125 /* Then ACPI style match */
126 if (acpi_driver_match_device(dev, drv))
127 return 1;
128
Jean Delvare51298d12009-09-18 22:45:45 +0200129 driver = to_i2c_driver(drv);
Lee Jones811073b2016-11-07 12:47:36 +0000130
131 /* Finally an I2C match */
132 if (i2c_match_id(driver->id_table, client))
133 return 1;
Jean Delvared2653e92008-04-29 23:11:39 +0200134
Jean Delvareeb8a7902008-05-18 20:49:41 +0200135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Kay Sievers7eff2e72007-08-14 15:15:12 +0200138static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200139{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100140 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800141 int rc;
142
Javier Martinez Canillasaf503712017-12-03 22:40:50 +0100143 rc = of_device_uevent_modalias(dev, env);
144 if (rc != -ENODEV)
145 return rc;
146
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800147 rc = acpi_device_uevent_modalias(dev, env);
148 if (rc != -ENODEV)
149 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200150
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100151 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200152}
153
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530154/* i2c bus recovery routines */
155static int get_scl_gpio_value(struct i2c_adapter *adap)
156{
Phil Reid3991c5c2017-11-02 10:40:24 +0800157 return gpiod_get_value_cansleep(adap->bus_recovery_info->scl_gpiod);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530158}
159
160static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
161{
Phil Reid3991c5c2017-11-02 10:40:24 +0800162 gpiod_set_value_cansleep(adap->bus_recovery_info->scl_gpiod, val);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530163}
164
165static int get_sda_gpio_value(struct i2c_adapter *adap)
166{
Phil Reid3991c5c2017-11-02 10:40:24 +0800167 return gpiod_get_value_cansleep(adap->bus_recovery_info->sda_gpiod);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530168}
169
Wolfram Sang80921782018-01-09 14:58:56 +0100170static void set_sda_gpio_value(struct i2c_adapter *adap, int val)
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530171{
Wolfram Sang80921782018-01-09 14:58:56 +0100172 gpiod_set_value_cansleep(adap->bus_recovery_info->sda_gpiod, val);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530173}
174
Wolfram Sang7ca5f6b2018-07-11 00:24:22 +0200175static int i2c_generic_bus_free(struct i2c_adapter *adap)
176{
177 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
178 int ret = -EOPNOTSUPP;
179
180 if (bri->get_bus_free)
181 ret = bri->get_bus_free(adap);
182 else if (bri->get_sda)
183 ret = bri->get_sda(adap);
184
185 if (ret < 0)
186 return ret;
187
188 return ret ? 0 : -EBUSY;
189}
190
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530191/*
192 * We are generating clock pulses. ndelay() determines durating of clk pulses.
193 * We will generate clock with rate 100 KHz and so duration of both clock levels
194 * is: delay in ns = (10^6 / 100) / 2
195 */
196#define RECOVERY_NDELAY 5000
197#define RECOVERY_CLK_CNT 9
198
Phil Reide1eb7d22017-11-02 10:40:30 +0800199int i2c_generic_scl_recovery(struct i2c_adapter *adap)
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530200{
201 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
Linus Torvaldscf676902019-05-01 11:07:40 -0700202 int i = 0, scl = 1, ret = 0;
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530203
204 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300205 bri->prepare_recovery(adap);
Codrin Ciubotariu75820312020-08-04 12:59:24 +0300206 if (bri->pinctrl)
207 pinctrl_select_state(bri->pinctrl, bri->pins_gpio);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530208
Wolfram Sangc4ae05b2018-07-17 11:00:05 +0200209 /*
210 * If we can set SDA, we will always create a STOP to ensure additional
211 * pulses will do no harm. This is achieved by letting SDA follow SCL
212 * half a cycle later. Check the 'incomplete_write_byte' fault injector
Russell Kingcf8ce8b2019-12-15 16:39:05 +0000213 * for details. Note that we must honour tsu:sto, 4us, but lets use 5us
214 * here for simplicity.
Wolfram Sangc4ae05b2018-07-17 11:00:05 +0200215 */
Wolfram Sangf7ff75e2018-07-11 00:27:22 +0200216 bri->set_scl(adap, scl);
Russell Kingcf8ce8b2019-12-15 16:39:05 +0000217 ndelay(RECOVERY_NDELAY);
Wolfram Sang72b08fc2018-01-09 14:58:57 +0100218 if (bri->set_sda)
Wolfram Sangc4ae05b2018-07-17 11:00:05 +0200219 bri->set_sda(adap, scl);
220 ndelay(RECOVERY_NDELAY / 2);
Jan Luebbe8b062602015-07-08 16:35:06 +0200221
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530222 /*
223 * By this time SCL is high, as we need to give 9 falling-rising edges
224 */
225 while (i++ < RECOVERY_CLK_CNT * 2) {
Wolfram Sangf7ff75e2018-07-11 00:27:22 +0200226 if (scl) {
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530227 /* SCL shouldn't be low here */
228 if (!bri->get_scl(adap)) {
229 dev_err(&adap->dev,
230 "SCL is stuck low, exit recovery\n");
231 ret = -EBUSY;
232 break;
233 }
234 }
235
Wolfram Sangf7ff75e2018-07-11 00:27:22 +0200236 scl = !scl;
237 bri->set_scl(adap, scl);
Wolfram Sangc4ae05b2018-07-17 11:00:05 +0200238 /* Creating STOP again, see above */
Russell Kingcf8ce8b2019-12-15 16:39:05 +0000239 if (scl) {
240 /* Honour minimum tsu:sto */
241 ndelay(RECOVERY_NDELAY);
242 } else {
243 /* Honour minimum tf and thd:dat */
244 ndelay(RECOVERY_NDELAY / 2);
245 }
Wolfram Sangabe41182018-07-10 23:42:15 +0200246 if (bri->set_sda)
Wolfram Sangf7ff75e2018-07-11 00:27:22 +0200247 bri->set_sda(adap, scl);
Wolfram Sangabe41182018-07-10 23:42:15 +0200248 ndelay(RECOVERY_NDELAY / 2);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530249
Wolfram Sangf7ff75e2018-07-11 00:27:22 +0200250 if (scl) {
Wolfram Sang7ca5f6b2018-07-11 00:24:22 +0200251 ret = i2c_generic_bus_free(adap);
Wolfram Sang0b710262018-07-10 23:42:17 +0200252 if (ret == 0)
253 break;
254 }
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530255 }
256
Wolfram Sang7ca5f6b2018-07-11 00:24:22 +0200257 /* If we can't check bus status, assume recovery worked */
258 if (ret == -EOPNOTSUPP)
259 ret = 0;
Wolfram Sang2806e6a2018-01-09 14:58:58 +0100260
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530261 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300262 bri->unprepare_recovery(adap);
Codrin Ciubotariu75820312020-08-04 12:59:24 +0300263 if (bri->pinctrl)
264 pinctrl_select_state(bri->pinctrl, bri->pins_default);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530265
266 return ret;
267}
Mark Brownc1c21f42015-04-15 19:18:39 +0100268EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530269
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530270int i2c_recover_bus(struct i2c_adapter *adap)
271{
272 if (!adap->bus_recovery_info)
Dmitry Osipenkoc126f7c2021-03-29 22:55:30 +0300273 return -EBUSY;
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530274
275 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
276 return adap->bus_recovery_info->recover_bus(adap);
277}
Mark Brownc1c21f42015-04-15 19:18:39 +0100278EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530279
Codrin Ciubotariu75820312020-08-04 12:59:24 +0300280static void i2c_gpio_init_pinctrl_recovery(struct i2c_adapter *adap)
281{
282 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
283 struct device *dev = &adap->dev;
284 struct pinctrl *p = bri->pinctrl;
285
286 /*
287 * we can't change states without pinctrl, so remove the states if
288 * populated
289 */
290 if (!p) {
291 bri->pins_default = NULL;
292 bri->pins_gpio = NULL;
293 return;
294 }
295
296 if (!bri->pins_default) {
297 bri->pins_default = pinctrl_lookup_state(p,
298 PINCTRL_STATE_DEFAULT);
299 if (IS_ERR(bri->pins_default)) {
300 dev_dbg(dev, PINCTRL_STATE_DEFAULT " state not found for GPIO recovery\n");
301 bri->pins_default = NULL;
302 }
303 }
304 if (!bri->pins_gpio) {
305 bri->pins_gpio = pinctrl_lookup_state(p, "gpio");
306 if (IS_ERR(bri->pins_gpio))
307 bri->pins_gpio = pinctrl_lookup_state(p, "recovery");
308
309 if (IS_ERR(bri->pins_gpio)) {
310 dev_dbg(dev, "no gpio or recovery state found for GPIO recovery\n");
311 bri->pins_gpio = NULL;
312 }
313 }
314
315 /* for pinctrl state changes, we need all the information */
316 if (bri->pins_default && bri->pins_gpio) {
317 dev_info(dev, "using pinctrl states for GPIO recovery");
318 } else {
319 bri->pinctrl = NULL;
320 bri->pins_default = NULL;
321 bri->pins_gpio = NULL;
322 }
323}
324
325static int i2c_gpio_init_generic_recovery(struct i2c_adapter *adap)
326{
327 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
328 struct device *dev = &adap->dev;
329 struct gpio_desc *gpiod;
330 int ret = 0;
331
332 /*
333 * don't touch the recovery information if the driver is not using
334 * generic SCL recovery
335 */
336 if (bri->recover_bus && bri->recover_bus != i2c_generic_scl_recovery)
337 return 0;
338
339 /*
340 * pins might be taken as GPIO, so we should inform pinctrl about
341 * this and move the state to GPIO
342 */
343 if (bri->pinctrl)
344 pinctrl_select_state(bri->pinctrl, bri->pins_gpio);
345
346 /*
347 * if there is incomplete or no recovery information, see if generic
348 * GPIO recovery is available
349 */
350 if (!bri->scl_gpiod) {
351 gpiod = devm_gpiod_get(dev, "scl", GPIOD_OUT_HIGH_OPEN_DRAIN);
352 if (PTR_ERR(gpiod) == -EPROBE_DEFER) {
353 ret = -EPROBE_DEFER;
354 goto cleanup_pinctrl_state;
355 }
356 if (!IS_ERR(gpiod)) {
357 bri->scl_gpiod = gpiod;
358 bri->recover_bus = i2c_generic_scl_recovery;
359 dev_info(dev, "using generic GPIOs for recovery\n");
360 }
361 }
362
363 /* SDA GPIOD line is optional, so we care about DEFER only */
364 if (!bri->sda_gpiod) {
365 /*
366 * We have SCL. Pull SCL low and wait a bit so that SDA glitches
367 * have no effect.
368 */
369 gpiod_direction_output(bri->scl_gpiod, 0);
370 udelay(10);
371 gpiod = devm_gpiod_get(dev, "sda", GPIOD_IN);
372
373 /* Wait a bit in case of a SDA glitch, and then release SCL. */
374 udelay(10);
375 gpiod_direction_output(bri->scl_gpiod, 1);
376
377 if (PTR_ERR(gpiod) == -EPROBE_DEFER) {
378 ret = -EPROBE_DEFER;
379 goto cleanup_pinctrl_state;
380 }
381 if (!IS_ERR(gpiod))
382 bri->sda_gpiod = gpiod;
383 }
384
385cleanup_pinctrl_state:
386 /* change the state of the pins back to their default state */
387 if (bri->pinctrl)
388 pinctrl_select_state(bri->pinctrl, bri->pins_default);
389
390 return ret;
391}
392
393static int i2c_gpio_init_recovery(struct i2c_adapter *adap)
394{
395 i2c_gpio_init_pinctrl_recovery(adap);
396 return i2c_gpio_init_generic_recovery(adap);
397}
398
Codrin Ciubotariu23a698f2020-08-04 12:59:25 +0300399static int i2c_init_recovery(struct i2c_adapter *adap)
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900400{
401 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
402 char *err_str;
403
404 if (!bri)
Codrin Ciubotariu23a698f2020-08-04 12:59:25 +0300405 return 0;
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900406
Codrin Ciubotariu23a698f2020-08-04 12:59:25 +0300407 if (i2c_gpio_init_recovery(adap) == -EPROBE_DEFER)
408 return -EPROBE_DEFER;
Codrin Ciubotariu75820312020-08-04 12:59:24 +0300409
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900410 if (!bri->recover_bus) {
411 err_str = "no recover_bus() found";
412 goto err;
413 }
414
Phil Reid3991c5c2017-11-02 10:40:24 +0800415 if (bri->scl_gpiod && bri->recover_bus == i2c_generic_scl_recovery) {
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900416 bri->get_scl = get_scl_gpio_value;
417 bri->set_scl = set_scl_gpio_value;
Wolfram Sang80921782018-01-09 14:58:56 +0100418 if (bri->sda_gpiod) {
Phil Reid3991c5c2017-11-02 10:40:24 +0800419 bri->get_sda = get_sda_gpio_value;
Wolfram Sang80921782018-01-09 14:58:56 +0100420 /* FIXME: add proper flag instead of '0' once available */
421 if (gpiod_get_direction(bri->sda_gpiod) == 0)
422 bri->set_sda = set_sda_gpio_value;
423 }
Codrin Ciubotariu23a698f2020-08-04 12:59:25 +0300424 } else if (bri->recover_bus == i2c_generic_scl_recovery) {
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900425 /* Generic SCL recovery */
426 if (!bri->set_scl || !bri->get_scl) {
427 err_str = "no {get|set}_scl() found";
428 goto err;
429 }
Wolfram Sangffc59c42018-07-10 23:42:16 +0200430 if (!bri->set_sda && !bri->get_sda) {
431 err_str = "either get_sda() or set_sda() needed";
432 goto err;
433 }
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900434 }
435
Codrin Ciubotariu23a698f2020-08-04 12:59:25 +0300436 return 0;
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900437 err:
438 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
439 adap->bus_recovery_info = NULL;
Codrin Ciubotariu23a698f2020-08-04 12:59:25 +0300440
441 return -EINVAL;
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900442}
443
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200444static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
445{
446 struct i2c_adapter *adap = client->adapter;
447 unsigned int irq;
448
449 if (!adap->host_notify_domain)
450 return -ENXIO;
451
452 if (client->flags & I2C_CLIENT_TEN)
453 return -EINVAL;
454
Charles Keepaxb9bb3fd2018-10-19 09:59:57 +0100455 irq = irq_create_mapping(adap->host_notify_domain, client->addr);
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200456
457 return irq > 0 ? irq : -ENXIO;
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460static int i2c_device_probe(struct device *dev)
461{
Jean Delvare51298d12009-09-18 22:45:45 +0200462 struct i2c_client *client = i2c_verify_client(dev);
463 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100464 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200465
Jean Delvare51298d12009-09-18 22:45:45 +0200466 if (!client)
467 return 0;
468
Charles Keepax6e76cb72019-06-27 10:24:11 +0100469 client->irq = client->init_irq;
470
Wolfram Sang0c2a3492020-06-30 18:24:40 +0200471 if (!client->irq) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300472 int irq = -ENOENT;
473
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800474 if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
475 dev_dbg(dev, "Using Host Notify IRQ\n");
Jarkko Nikula72bfcee2019-04-30 17:23:22 +0300476 /* Keep adapter active when Host Notify is required */
477 pm_runtime_get_sync(&client->adapter->dev);
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800478 irq = i2c_smbus_host_notify_to_irq(client);
479 } else if (dev->of_node) {
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700480 irq = of_irq_get_byname(dev->of_node, "irq");
481 if (irq == -EINVAL || irq == -ENODATA)
482 irq = of_irq_get(dev->of_node, 0);
483 } else if (ACPI_COMPANION(dev)) {
Charles Keepax16c9db12019-06-27 10:24:09 +0100484 irq = i2c_acpi_get_irq(client);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700485 }
Alain Volmat3c3dd562020-04-30 17:43:21 +0200486 if (irq == -EPROBE_DEFER) {
487 status = irq;
488 goto put_sync_adapter;
489 }
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800490
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100491 if (irq < 0)
492 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200493
494 client->irq = irq;
495 }
496
Wolfram Sang0c2a3492020-06-30 18:24:40 +0200497 driver = to_i2c_driver(dev->driver);
498
Lee Jonesda10c062016-11-07 12:47:39 +0000499 /*
Javier Martinez Canillasf4b17a12017-08-09 11:21:28 +0200500 * An I2C ID table is not mandatory, if and only if, a suitable OF
501 * or ACPI ID table is supplied for the probing device.
Lee Jonesda10c062016-11-07 12:47:39 +0000502 */
503 if (!driver->id_table &&
Andy Shevchenkoe3cb82c2020-08-21 20:03:33 +0300504 !acpi_driver_match_device(dev, dev->driver) &&
Alain Volmat3c3dd562020-04-30 17:43:21 +0200505 !i2c_of_match_device(dev->driver->of_match_table, client)) {
506 status = -ENODEV;
507 goto put_sync_adapter;
508 }
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200509
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700510 if (client->flags & I2C_CLIENT_WAKE) {
Andy Shevchenko3e998342019-07-26 16:14:21 +0300511 int wakeirq;
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700512
Andy Shevchenko3e998342019-07-26 16:14:21 +0300513 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
Alain Volmat3c3dd562020-04-30 17:43:21 +0200514 if (wakeirq == -EPROBE_DEFER) {
515 status = wakeirq;
516 goto put_sync_adapter;
517 }
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700518
519 device_init_wakeup(&client->dev, true);
520
521 if (wakeirq > 0 && wakeirq != client->irq)
522 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
523 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200524 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700525 else
526 status = 0;
527
528 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300529 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700530 }
531
David Brownell7b4fbc52007-05-01 23:26:30 +0200532 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200533
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200534 status = of_clk_set_defaults(dev->of_node, false);
535 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700536 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200537
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200538 status = dev_pm_domain_attach(&client->dev, true);
Ulf Hanssone6a20b62018-04-26 10:53:07 +0200539 if (status)
Kieran Bingham74cedd32015-10-12 21:54:43 +0100540 goto err_clear_wakeup_irq;
541
Dmitry Torokhov5b5475822021-03-21 18:38:32 -0700542 client->devres_group_id = devres_open_group(&client->dev, NULL,
543 GFP_KERNEL);
544 if (!client->devres_group_id) {
545 status = -ENOMEM;
546 goto err_detach_pm_domain;
547 }
548
Lee Jonesb8a1a4c2016-11-07 12:47:41 +0000549 /*
550 * When there are no more users of probe(),
551 * rename probe_new to probe.
552 */
553 if (driver->probe_new)
554 status = driver->probe_new(client);
555 else if (driver->probe)
556 status = driver->probe(client,
557 i2c_match_id(driver->id_table, client));
558 else
559 status = -EINVAL;
560
Dmitry Torokhov5b5475822021-03-21 18:38:32 -0700561 /*
562 * Note that we are not closing the devres group opened above so
563 * even resources that were attached to the device after probe is
564 * run are released when i2c_device_remove() is executed. This is
565 * needed as some drivers would allocate additional resources,
566 * for example when updating firmware.
567 */
568
Kieran Bingham74cedd32015-10-12 21:54:43 +0100569 if (status)
Dmitry Torokhov5b5475822021-03-21 18:38:32 -0700570 goto err_release_driver_resources;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100571
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700572 return 0;
573
Dmitry Torokhov5b5475822021-03-21 18:38:32 -0700574err_release_driver_resources:
575 devres_release_group(&client->dev, client->devres_group_id);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700576err_detach_pm_domain:
577 dev_pm_domain_detach(&client->dev, true);
578err_clear_wakeup_irq:
579 dev_pm_clear_wake_irq(&client->dev);
580 device_init_wakeup(&client->dev, false);
Alain Volmat3c3dd562020-04-30 17:43:21 +0200581put_sync_adapter:
582 if (client->flags & I2C_CLIENT_HOST_NOTIFY)
583 pm_runtime_put_sync(&client->adapter->dev);
584
Hans Verkuil50c33042008-03-12 14:15:00 +0100585 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
588static int i2c_device_remove(struct device *dev)
589{
Uwe Kleine-König4e970a02020-11-26 08:23:31 +0100590 struct i2c_client *client = to_i2c_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200591 struct i2c_driver *driver;
David Brownella1d9e6e2007-05-01 23:26:30 +0200592
David Brownella1d9e6e2007-05-01 23:26:30 +0200593 driver = to_i2c_driver(dev->driver);
594 if (driver->remove) {
Uwe Kleine-König71637c62020-11-26 08:23:30 +0100595 int status;
596
David Brownella1d9e6e2007-05-01 23:26:30 +0200597 dev_dbg(dev, "remove\n");
Uwe Kleine-König71637c62020-11-26 08:23:30 +0100598
David Brownella1d9e6e2007-05-01 23:26:30 +0200599 status = driver->remove(client);
Uwe Kleine-König71637c62020-11-26 08:23:30 +0100600 if (status)
601 dev_warn(dev, "remove failed (%pe), will be ignored\n", ERR_PTR(status));
David Brownella1d9e6e2007-05-01 23:26:30 +0200602 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100603
Dmitry Torokhov5b5475822021-03-21 18:38:32 -0700604 devres_release_group(&client->dev, client->devres_group_id);
605
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200606 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700607
608 dev_pm_clear_wake_irq(&client->dev);
609 device_init_wakeup(&client->dev, false);
610
Charles Keepax6e76cb72019-06-27 10:24:11 +0100611 client->irq = 0;
Jarkko Nikula72bfcee2019-04-30 17:23:22 +0300612 if (client->flags & I2C_CLIENT_HOST_NOTIFY)
613 pm_runtime_put(&client->adapter->dev);
Charles Keepax6f108dd2018-10-19 09:59:58 +0100614
Uwe Kleine-König71637c62020-11-26 08:23:30 +0100615 /* return always 0 because there is WIP to make remove-functions void */
616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
David Brownellf37dd802007-02-13 22:09:00 +0100619static void i2c_device_shutdown(struct device *dev)
620{
Jean Delvare51298d12009-09-18 22:45:45 +0200621 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100622 struct i2c_driver *driver;
623
Jean Delvare51298d12009-09-18 22:45:45 +0200624 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100625 return;
626 driver = to_i2c_driver(dev->driver);
627 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200628 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100629}
630
David Brownell9c1600e2007-05-01 23:26:31 +0200631static void i2c_client_dev_release(struct device *dev)
632{
633 kfree(to_i2c_client(dev));
634}
635
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100636static ssize_t
Geert Uytterhoeven54a19fd2019-11-13 16:23:06 +0100637name_show(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200638{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200639 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
640 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200641}
Geert Uytterhoeven54a19fd2019-11-13 16:23:06 +0100642static DEVICE_ATTR_RO(name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200643
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100644static ssize_t
Geert Uytterhoeven54a19fd2019-11-13 16:23:06 +0100645modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200646{
647 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800648 int len;
649
Javier Martinez Canillasaf503712017-12-03 22:40:50 +0100650 len = of_device_modalias(dev, buf, PAGE_SIZE);
651 if (len != -ENODEV)
652 return len;
653
Tian Tao068ff572021-04-05 16:18:42 +0800654 len = acpi_device_modalias(dev, buf, PAGE_SIZE - 1);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800655 if (len != -ENODEV)
656 return len;
657
Jean Delvareeb8a7902008-05-18 20:49:41 +0200658 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200659}
Geert Uytterhoeven54a19fd2019-11-13 16:23:06 +0100660static DEVICE_ATTR_RO(modalias);
Jean Delvare51298d12009-09-18 22:45:45 +0200661
662static struct attribute *i2c_dev_attrs[] = {
663 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200664 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200665 &dev_attr_modalias.attr,
666 NULL
667};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100668ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100669
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200670struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100671 .name = "i2c",
672 .match = i2c_device_match,
673 .probe = i2c_device_probe,
674 .remove = i2c_device_remove,
675 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000676};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200677EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000678
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800679struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100680 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200681 .uevent = i2c_device_uevent,
682 .release = i2c_client_dev_release,
683};
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800684EXPORT_SYMBOL_GPL(i2c_client_type);
Jean Delvare51298d12009-09-18 22:45:45 +0200685
David Brownell9b766b82008-01-27 18:14:51 +0100686
687/**
688 * i2c_verify_client - return parameter as i2c_client, or NULL
689 * @dev: device, probably from some driver model iterator
690 *
691 * When traversing the driver model tree, perhaps using driver model
692 * iterators like @device_for_each_child(), you can't assume very much
693 * about the nodes you find. Use this function to avoid oopses caused
694 * by wrongly treating some non-I2C device as an i2c_client.
695 */
696struct i2c_client *i2c_verify_client(struct device *dev)
697{
Jean Delvare51298d12009-09-18 22:45:45 +0200698 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100699 ? to_i2c_client(dev)
700 : NULL;
701}
702EXPORT_SYMBOL(i2c_verify_client);
703
704
Wolfram Sangda899f52015-05-18 21:09:12 +0200705/* Return a unique address which takes the flags of the client into account */
706static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
707{
708 unsigned short addr = client->addr;
709
710 /* For some client flags, add an arbitrary offset to avoid collisions */
711 if (client->flags & I2C_CLIENT_TEN)
712 addr |= I2C_ADDR_OFFSET_TEN_BIT;
713
714 if (client->flags & I2C_CLIENT_SLAVE)
715 addr |= I2C_ADDR_OFFSET_SLAVE;
716
717 return addr;
718}
719
Jean Delvare3a89db52010-06-03 11:33:52 +0200720/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300721 * are purposely not enforced, except for the general call address. */
Wolfram Sang398432e2018-03-20 21:54:38 +0100722static int i2c_check_addr_validity(unsigned int addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +0200723{
Wolfram Sangc4019b72015-07-17 12:50:06 +0200724 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +0200725 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200726 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +0200727 return -EINVAL;
728 } else {
729 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200730 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +0200731 return -EINVAL;
732 }
733 return 0;
734}
735
Jean Delvare656b8762010-06-03 11:33:53 +0200736/* And this is a strict address validity check, used when probing. If a
737 * device uses a reserved address, then it shouldn't be probed. 7-bit
738 * addressing is assumed, 10-bit address devices are rare and should be
739 * explicitly enumerated. */
Wolfram Sange4991ec2017-05-23 11:14:17 +0200740int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +0200741{
742 /*
743 * Reserved addresses per I2C specification:
744 * 0x00 General call address / START byte
745 * 0x01 CBUS address
746 * 0x02 Reserved for different bus format
747 * 0x03 Reserved for future purposes
748 * 0x04-0x07 Hs-mode master code
749 * 0x78-0x7b 10-bit slave addressing
750 * 0x7c-0x7f Reserved for future purposes
751 */
752 if (addr < 0x08 || addr > 0x77)
753 return -EINVAL;
754 return 0;
755}
756
Jean Delvare3b5f7942010-06-03 11:33:55 +0200757static int __i2c_check_addr_busy(struct device *dev, void *addrp)
758{
759 struct i2c_client *client = i2c_verify_client(dev);
760 int addr = *(int *)addrp;
761
Wolfram Sang9bccc702015-07-17 14:48:56 +0200762 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +0200763 return -EBUSY;
764 return 0;
765}
766
Michael Lawnick08263742010-08-11 18:21:02 +0200767/* walk up mux tree */
768static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
769{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200770 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200771 int result;
772
773 result = device_for_each_child(&adapter->dev, &addr,
774 __i2c_check_addr_busy);
775
Jean Delvare97cc4d42010-10-24 18:16:57 +0200776 if (!result && parent)
777 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200778
779 return result;
780}
781
782/* recurse down mux tree */
783static int i2c_check_mux_children(struct device *dev, void *addrp)
784{
785 int result;
786
787 if (dev->type == &i2c_adapter_type)
788 result = device_for_each_child(dev, addrp,
789 i2c_check_mux_children);
790 else
791 result = __i2c_check_addr_busy(dev, addrp);
792
793 return result;
794}
795
Jean Delvare3b5f7942010-06-03 11:33:55 +0200796static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
797{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200798 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200799 int result = 0;
800
Jean Delvare97cc4d42010-10-24 18:16:57 +0200801 if (parent)
802 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200803
804 if (!result)
805 result = device_for_each_child(&adapter->dev, &addr,
806 i2c_check_mux_children);
807
808 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200809}
810
David Brownell9c1600e2007-05-01 23:26:31 +0200811/**
Peter Rosin8320f492016-05-04 22:15:27 +0200812 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200813 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200814 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
815 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200816 */
Peter Rosin8320f492016-05-04 22:15:27 +0200817static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
818 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200819{
Peter Rosin7b94ea52018-07-20 10:39:14 +0200820 rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter));
Jean Delvarefe61e072010-08-11 18:20:58 +0200821}
Jean Delvarefe61e072010-08-11 18:20:58 +0200822
823/**
Peter Rosin8320f492016-05-04 22:15:27 +0200824 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200825 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200826 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
827 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200828 */
Peter Rosin8320f492016-05-04 22:15:27 +0200829static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
830 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200831{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200832 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200833}
834
835/**
Peter Rosin8320f492016-05-04 22:15:27 +0200836 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200837 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200838 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
839 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200840 */
Peter Rosin8320f492016-05-04 22:15:27 +0200841static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
842 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200843{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200844 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200845}
Jean Delvarefe61e072010-08-11 18:20:58 +0200846
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200847static void i2c_dev_set_name(struct i2c_adapter *adap,
Hans de Goede728fe6c2017-10-11 11:41:19 +0200848 struct i2c_client *client,
849 struct i2c_board_info const *info)
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200850{
851 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
852
Hans de Goede728fe6c2017-10-11 11:41:19 +0200853 if (info && info->dev_name) {
854 dev_set_name(&client->dev, "i2c-%s", info->dev_name);
855 return;
856 }
857
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200858 if (adev) {
859 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
860 return;
861 }
862
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200863 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +0200864 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200865}
866
Charles Keepax1d7534b2019-06-27 10:24:06 +0100867int i2c_dev_irq_from_resources(const struct resource *resources,
868 unsigned int num_resources)
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800869{
870 struct irq_data *irqd;
871 int i;
872
873 for (i = 0; i < num_resources; i++) {
874 const struct resource *r = &resources[i];
875
876 if (resource_type(r) != IORESOURCE_IRQ)
877 continue;
878
879 if (r->flags & IORESOURCE_BITS) {
880 irqd = irq_get_irq_data(r->start);
881 if (!irqd)
882 break;
883
884 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
885 }
886
887 return r->start;
888 }
889
890 return 0;
891}
892
Jean Delvarefe61e072010-08-11 18:20:58 +0200893/**
Heiner Kallweit7159dbda2019-05-16 23:13:08 +0200894 * i2c_new_client_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200895 * @adap: the adapter managing the device
896 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200897 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200898 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200899 * Create an i2c device. Binding is handled through driver model
900 * probe()/remove() methods. A driver may be bound to this device when we
901 * return from this function, or any later moment (e.g. maybe hotplugging will
902 * load the driver module). This call is not appropriate for use by mainboard
903 * initialization logic, which usually runs during an arch_initcall() long
904 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200905 *
906 * This returns the new i2c client, which may be saved for later use with
Heiner Kallweit7159dbda2019-05-16 23:13:08 +0200907 * i2c_unregister_device(); or an ERR_PTR to describe the error.
David Brownell9c1600e2007-05-01 23:26:31 +0200908 */
Wolfram Sang550113d2019-06-24 19:04:02 +0200909struct i2c_client *
Heiner Kallweit7159dbda2019-05-16 23:13:08 +0200910i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
David Brownell9c1600e2007-05-01 23:26:31 +0200911{
912 struct i2c_client *client;
913 int status;
914
915 client = kzalloc(sizeof *client, GFP_KERNEL);
916 if (!client)
Heiner Kallweit7159dbda2019-05-16 23:13:08 +0200917 return ERR_PTR(-ENOMEM);
David Brownell9c1600e2007-05-01 23:26:31 +0200918
919 client->adapter = adap;
920
921 client->dev.platform_data = info->platform_data;
Marc Pignatee354252008-08-28 08:33:22 +0200922 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200923 client->addr = info->addr;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800924
Jim Broadus93b66042019-02-19 11:30:27 -0800925 client->init_irq = info->irq;
926 if (!client->init_irq)
927 client->init_irq = i2c_dev_irq_from_resources(info->resources,
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800928 info->num_resources);
David Brownell9c1600e2007-05-01 23:26:31 +0200929
David Brownell9c1600e2007-05-01 23:26:31 +0200930 strlcpy(client->name, info->type, sizeof(client->name));
931
Wolfram Sangc4019b72015-07-17 12:50:06 +0200932 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +0200933 if (status) {
934 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
935 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
936 goto out_err_silent;
937 }
938
Jean Delvaref8a227e2009-06-19 16:58:18 +0200939 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +0200940 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200941 if (status)
942 goto out_err;
943
944 client->dev.parent = &client->adapter->dev;
945 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200946 client->dev.type = &i2c_client_type;
Boris Brezillon04782262018-03-25 14:49:02 +0200947 client->dev.of_node = of_node_get(info->of_node);
Rafael J. Wysockice793482015-03-16 23:49:03 +0100948 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200949
Hans de Goede728fe6c2017-10-11 11:41:19 +0200950 i2c_dev_set_name(adap, client, info);
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800951
Heikki Krogerus714638e2021-03-29 13:50:36 +0300952 if (info->swnode) {
953 status = device_add_software_node(&client->dev, info->swnode);
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800954 if (status) {
955 dev_err(&adap->dev,
Heikki Krogerus714638e2021-03-29 13:50:36 +0300956 "Failed to add software node to client %s: %d\n",
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800957 client->name, status);
Boris Brezillon04782262018-03-25 14:49:02 +0200958 goto out_err_put_of_node;
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800959 }
960 }
961
Jean Delvaref8a227e2009-06-19 16:58:18 +0200962 status = device_register(&client->dev);
963 if (status)
Heikki Krogerus714638e2021-03-29 13:50:36 +0300964 goto out_remove_swnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200965
Jean Delvaref8a227e2009-06-19 16:58:18 +0200966 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
967 client->name, dev_name(&client->dev));
968
David Brownell9c1600e2007-05-01 23:26:31 +0200969 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200970
Heikki Krogerus714638e2021-03-29 13:50:36 +0300971out_remove_swnode:
972 device_remove_software_node(&client->dev);
Boris Brezillon04782262018-03-25 14:49:02 +0200973out_err_put_of_node:
974 of_node_put(info->of_node);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200975out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300976 dev_err(&adap->dev,
977 "Failed to register i2c client %s at 0x%02x (%d)\n",
978 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200979out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200980 kfree(client);
Heiner Kallweit7159dbda2019-05-16 23:13:08 +0200981 return ERR_PTR(status);
982}
983EXPORT_SYMBOL_GPL(i2c_new_client_device);
984
985/**
Wolfram Sang87e07432020-01-07 18:47:43 +0100986 * i2c_unregister_device - reverse effect of i2c_new_*_device()
987 * @client: value returned from i2c_new_*_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200988 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200989 */
990void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200991{
Wolfram Sang689f5352019-08-19 22:48:25 +0200992 if (IS_ERR_OR_NULL(client))
Andy Shevchenko7b43dd12017-10-31 16:21:35 +0200993 return;
Lixin Wange0638fa2017-11-27 15:06:55 +0800994
995 if (client->dev.of_node) {
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +0200996 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Lixin Wange0638fa2017-11-27 15:06:55 +0800997 of_node_put(client->dev.of_node);
998 }
999
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001000 if (ACPI_COMPANION(&client->dev))
1001 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
Heikki Krogerus714638e2021-03-29 13:50:36 +03001002 device_remove_software_node(&client->dev);
David Brownella1d9e6e2007-05-01 23:26:30 +02001003 device_unregister(&client->dev);
1004}
David Brownell9c1600e2007-05-01 23:26:31 +02001005EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +02001006
1007
Jean Delvare60b129d2008-05-11 20:37:06 +02001008static const struct i2c_device_id dummy_id[] = {
1009 { "dummy", 0 },
1010 { },
1011};
1012
Jean Delvared2653e92008-04-29 23:11:39 +02001013static int dummy_probe(struct i2c_client *client,
1014 const struct i2c_device_id *id)
1015{
1016 return 0;
1017}
1018
1019static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +01001020{
1021 return 0;
1022}
1023
1024static struct i2c_driver dummy_driver = {
1025 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +02001026 .probe = dummy_probe,
1027 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +02001028 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +01001029};
1030
1031/**
Heiner Kallweit7159dbda2019-05-16 23:13:08 +02001032 * i2c_new_dummy_device - return a new i2c device bound to a dummy driver
David Brownelle9f13732008-01-27 18:14:52 +01001033 * @adapter: the adapter managing the device
1034 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +01001035 * Context: can sleep
1036 *
1037 * This returns an I2C client bound to the "dummy" driver, intended for use
1038 * with devices that consume multiple addresses. Examples of such chips
1039 * include various EEPROMS (like 24c04 and 24c08 models).
1040 *
1041 * These dummy devices have two main uses. First, most I2C and SMBus calls
1042 * except i2c_transfer() need a client handle; the dummy will be that handle.
1043 * And second, this prevents the specified address from being bound to a
1044 * different driver.
1045 *
1046 * This returns the new i2c client, which should be saved for later use with
Heiner Kallweit7159dbda2019-05-16 23:13:08 +02001047 * i2c_unregister_device(); or an ERR_PTR to describe the error.
David Brownelle9f13732008-01-27 18:14:52 +01001048 */
Wolfram Sang550113d2019-06-24 19:04:02 +02001049struct i2c_client *i2c_new_dummy_device(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +01001050{
1051 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +02001052 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +01001053 };
1054
Heiner Kallweit7159dbda2019-05-16 23:13:08 +02001055 return i2c_new_client_device(adapter, &info);
1056}
1057EXPORT_SYMBOL_GPL(i2c_new_dummy_device);
1058
Yicong Yangbb7f0862021-04-08 20:17:40 +08001059static void devm_i2c_release_dummy(void *client)
Heiner Kallweitb8f5fe32019-05-16 23:13:09 +02001060{
Yicong Yangbb7f0862021-04-08 20:17:40 +08001061 i2c_unregister_device(client);
Heiner Kallweitb8f5fe32019-05-16 23:13:09 +02001062}
1063
1064/**
1065 * devm_i2c_new_dummy_device - return a new i2c device bound to a dummy driver
1066 * @dev: device the managed resource is bound to
1067 * @adapter: the adapter managing the device
1068 * @address: seven bit address to be used
1069 * Context: can sleep
1070 *
1071 * This is the device-managed version of @i2c_new_dummy_device. It returns the
1072 * new i2c client or an ERR_PTR in case of an error.
1073 */
1074struct i2c_client *devm_i2c_new_dummy_device(struct device *dev,
1075 struct i2c_adapter *adapter,
1076 u16 address)
1077{
Heiner Kallweitb8f5fe32019-05-16 23:13:09 +02001078 struct i2c_client *client;
Yicong Yangbb7f0862021-04-08 20:17:40 +08001079 int ret;
Heiner Kallweitb8f5fe32019-05-16 23:13:09 +02001080
1081 client = i2c_new_dummy_device(adapter, address);
Yicong Yangbb7f0862021-04-08 20:17:40 +08001082 if (IS_ERR(client))
1083 return client;
1084
1085 ret = devm_add_action_or_reset(dev, devm_i2c_release_dummy, client);
1086 if (ret)
1087 return ERR_PTR(ret);
Heiner Kallweitb8f5fe32019-05-16 23:13:09 +02001088
1089 return client;
1090}
1091EXPORT_SYMBOL_GPL(devm_i2c_new_dummy_device);
1092
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +01001093/**
Wolfram Sangaf805592019-08-09 17:40:47 +02001094 * i2c_new_ancillary_device - Helper to get the instantiated secondary address
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +01001095 * and create the associated device
1096 * @client: Handle to the primary client
1097 * @name: Handle to specify which secondary address to get
1098 * @default_addr: Used as a fallback if no secondary address was specified
1099 * Context: can sleep
1100 *
1101 * I2C clients can be composed of multiple I2C slaves bound together in a single
1102 * component. The I2C client driver then binds to the master I2C slave and needs
1103 * to create I2C dummy clients to communicate with all the other slaves.
1104 *
1105 * This function creates and returns an I2C dummy client whose I2C address is
1106 * retrieved from the platform firmware based on the given slave name. If no
1107 * address is specified by the firmware default_addr is used.
1108 *
1109 * On DT-based platforms the address is retrieved from the "reg" property entry
1110 * cell whose "reg-names" value matches the slave name.
1111 *
1112 * This returns the new i2c client, which should be saved for later use with
Wolfram Sangaf805592019-08-09 17:40:47 +02001113 * i2c_unregister_device(); or an ERR_PTR to describe the error.
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +01001114 */
Wolfram Sangaf805592019-08-09 17:40:47 +02001115struct i2c_client *i2c_new_ancillary_device(struct i2c_client *client,
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +01001116 const char *name,
1117 u16 default_addr)
1118{
1119 struct device_node *np = client->dev.of_node;
1120 u32 addr = default_addr;
1121 int i;
1122
1123 if (np) {
1124 i = of_property_match_string(np, "reg-names", name);
1125 if (i >= 0)
1126 of_property_read_u32_index(np, "reg", i, &addr);
1127 }
1128
1129 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
Wolfram Sangaf805592019-08-09 17:40:47 +02001130 return i2c_new_dummy_device(client->adapter, addr);
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +01001131}
Wolfram Sangaf805592019-08-09 17:40:47 +02001132EXPORT_SYMBOL_GPL(i2c_new_ancillary_device);
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +01001133
David Brownellf37dd802007-02-13 22:09:00 +01001134/* ------------------------------------------------------------------------- */
1135
David Brownell16ffadf2007-05-01 23:26:28 +02001136/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1137
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001138static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139{
David Brownellef2c83212007-05-01 23:26:28 +02001140 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 complete(&adap->dev_released);
1142}
1143
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001144unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +02001145{
1146 unsigned int depth = 0;
1147
1148 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1149 depth++;
1150
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +02001151 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
1152 "adapter depth exceeds lockdep subclass limit\n");
1153
Jean Delvare390946b2012-09-10 10:14:02 +02001154 return depth;
1155}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001156EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +02001157
1158/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001159 * Let users instantiate I2C devices through sysfs. This can be used when
1160 * platform initialization code doesn't contain the proper data for
1161 * whatever reason. Also useful for drivers that do device detection and
1162 * detection fails, either because the device uses an unexpected address,
1163 * or this is a compatible device with different ID register values.
1164 *
1165 * Parameter checking may look overzealous, but we really don't want
1166 * the user to provide incorrect parameters.
1167 */
1168static ssize_t
Geert Uytterhoeven54a19fd2019-11-13 16:23:06 +01001169new_device_store(struct device *dev, struct device_attribute *attr,
1170 const char *buf, size_t count)
Jean Delvare99cd8e22009-06-19 16:58:20 +02001171{
1172 struct i2c_adapter *adap = to_i2c_adapter(dev);
1173 struct i2c_board_info info;
1174 struct i2c_client *client;
1175 char *blank, end;
1176 int res;
1177
Jean Delvare99cd8e22009-06-19 16:58:20 +02001178 memset(&info, 0, sizeof(struct i2c_board_info));
1179
1180 blank = strchr(buf, ' ');
1181 if (!blank) {
1182 dev_err(dev, "%s: Missing parameters\n", "new_device");
1183 return -EINVAL;
1184 }
1185 if (blank - buf > I2C_NAME_SIZE - 1) {
1186 dev_err(dev, "%s: Invalid device name\n", "new_device");
1187 return -EINVAL;
1188 }
1189 memcpy(info.type, buf, blank - buf);
1190
1191 /* Parse remaining parameters, reject extra parameters */
1192 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1193 if (res < 1) {
1194 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1195 return -EINVAL;
1196 }
1197 if (res > 1 && end != '\n') {
1198 dev_err(dev, "%s: Extra parameters\n", "new_device");
1199 return -EINVAL;
1200 }
1201
Wolfram Sangcfa03272015-07-27 14:03:38 +02001202 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
1203 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
1204 info.flags |= I2C_CLIENT_TEN;
1205 }
1206
1207 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
1208 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1209 info.flags |= I2C_CLIENT_SLAVE;
1210 }
1211
Heiner Kallweit7159dbda2019-05-16 23:13:08 +02001212 client = i2c_new_client_device(adap, &info);
1213 if (IS_ERR(client))
1214 return PTR_ERR(client);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001215
1216 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001217 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001218 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001219 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001220 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1221 info.type, info.addr);
1222
1223 return count;
1224}
Geert Uytterhoeven54a19fd2019-11-13 16:23:06 +01001225static DEVICE_ATTR_WO(new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001226
1227/*
1228 * And of course let the users delete the devices they instantiated, if
1229 * they got it wrong. This interface can only be used to delete devices
1230 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1231 * don't delete devices to which some kernel code still has references.
1232 *
1233 * Parameter checking may look overzealous, but we really don't want
1234 * the user to delete the wrong device.
1235 */
1236static ssize_t
Geert Uytterhoeven54a19fd2019-11-13 16:23:06 +01001237delete_device_store(struct device *dev, struct device_attribute *attr,
1238 const char *buf, size_t count)
Jean Delvare99cd8e22009-06-19 16:58:20 +02001239{
1240 struct i2c_adapter *adap = to_i2c_adapter(dev);
1241 struct i2c_client *client, *next;
1242 unsigned short addr;
1243 char end;
1244 int res;
1245
1246 /* Parse parameters, reject extra parameters */
1247 res = sscanf(buf, "%hi%c", &addr, &end);
1248 if (res < 1) {
1249 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1250 return -EINVAL;
1251 }
1252 if (res > 1 && end != '\n') {
1253 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1254 return -EINVAL;
1255 }
1256
1257 /* Make sure the device was added through sysfs */
1258 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001259 mutex_lock_nested(&adap->userspace_clients_lock,
1260 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001261 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1262 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001263 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001264 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1265 "delete_device", client->name, client->addr);
1266
1267 list_del(&client->detected);
1268 i2c_unregister_device(client);
1269 res = count;
1270 break;
1271 }
1272 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001273 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001274
1275 if (res < 0)
1276 dev_err(dev, "%s: Can't find device in list\n",
1277 "delete_device");
1278 return res;
1279}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001280static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
Geert Uytterhoeven54a19fd2019-11-13 16:23:06 +01001281 delete_device_store);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001282
1283static struct attribute *i2c_adapter_attrs[] = {
1284 &dev_attr_name.attr,
1285 &dev_attr_new_device.attr,
1286 &dev_attr_delete_device.attr,
1287 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001288};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001289ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001290
Michael Lawnick08263742010-08-11 18:21:02 +02001291struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001292 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001293 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001294};
Michael Lawnick08263742010-08-11 18:21:02 +02001295EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Stephen Warren643dd092012-04-17 12:43:33 -06001297/**
1298 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1299 * @dev: device, probably from some driver model iterator
1300 *
1301 * When traversing the driver model tree, perhaps using driver model
1302 * iterators like @device_for_each_child(), you can't assume very much
1303 * about the nodes you find. Use this function to avoid oopses caused
1304 * by wrongly treating some non-I2C device as an i2c_adapter.
1305 */
1306struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1307{
1308 return (dev->type == &i2c_adapter_type)
1309 ? to_i2c_adapter(dev)
1310 : NULL;
1311}
1312EXPORT_SYMBOL(i2c_verify_adapter);
1313
Jean Delvare2bb50952009-09-18 22:45:46 +02001314#ifdef CONFIG_I2C_COMPAT
1315static struct class_compat *i2c_adapter_compat_class;
1316#endif
1317
David Brownell9c1600e2007-05-01 23:26:31 +02001318static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1319{
1320 struct i2c_devinfo *devinfo;
1321
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001322 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001323 list_for_each_entry(devinfo, &__i2c_board_list, list) {
Wolfram Sang87e07432020-01-07 18:47:43 +01001324 if (devinfo->busnum == adapter->nr &&
1325 IS_ERR(i2c_new_client_device(adapter, &devinfo->board_info)))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001326 dev_err(&adapter->dev,
1327 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001328 devinfo->board_info.addr);
1329 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001330 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001331}
1332
Jean Delvare69b00892009-12-06 17:06:27 +01001333static int i2c_do_add_adapter(struct i2c_driver *driver,
1334 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001335{
Jean Delvare4735c982008-07-14 22:38:36 +02001336 /* Detect supported devices on that bus, and instantiate them */
1337 i2c_detect(adap, driver);
1338
Jean Delvare026526f2008-01-27 18:14:49 +01001339 return 0;
1340}
1341
Jean Delvare69b00892009-12-06 17:06:27 +01001342static int __process_new_adapter(struct device_driver *d, void *data)
1343{
1344 return i2c_do_add_adapter(to_i2c_driver(d), data);
1345}
1346
Peter Rosind1ed7982016-08-25 23:07:01 +02001347static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1348 .lock_bus = i2c_adapter_lock_bus,
1349 .trylock_bus = i2c_adapter_trylock_bus,
1350 .unlock_bus = i2c_adapter_unlock_bus,
1351};
1352
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001353static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap)
1354{
1355 struct irq_domain *domain = adap->host_notify_domain;
1356 irq_hw_number_t hwirq;
1357
1358 if (!domain)
1359 return;
1360
1361 for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++)
1362 irq_dispose_mapping(irq_find_mapping(domain, hwirq));
1363
1364 irq_domain_remove(domain);
1365 adap->host_notify_domain = NULL;
1366}
1367
1368static int i2c_host_notify_irq_map(struct irq_domain *h,
1369 unsigned int virq,
1370 irq_hw_number_t hw_irq_num)
1371{
1372 irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
1373
1374 return 0;
1375}
1376
1377static const struct irq_domain_ops i2c_host_notify_irq_ops = {
1378 .map = i2c_host_notify_irq_map,
1379};
1380
1381static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap)
1382{
1383 struct irq_domain *domain;
1384
1385 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
1386 return 0;
1387
Wolfram Sang8682dc12020-07-01 10:23:18 +02001388 domain = irq_domain_create_linear(adap->dev.parent->fwnode,
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001389 I2C_ADDR_7BITS_COUNT,
1390 &i2c_host_notify_irq_ops, adap);
1391 if (!domain)
1392 return -ENOMEM;
1393
1394 adap->host_notify_domain = domain;
1395
1396 return 0;
1397}
1398
1399/**
1400 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
1401 * I2C client.
1402 * @adap: the adapter
1403 * @addr: the I2C address of the notifying device
1404 * Context: can't sleep
1405 *
1406 * Helper function to be called from an I2C bus driver's interrupt
1407 * handler. It will schedule the Host Notify IRQ.
1408 */
1409int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
1410{
1411 int irq;
1412
1413 if (!adap)
1414 return -EINVAL;
1415
1416 irq = irq_find_mapping(adap->host_notify_domain, addr);
1417 if (irq <= 0)
1418 return -ENXIO;
1419
1420 generic_handle_irq(irq);
1421
1422 return 0;
1423}
1424EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
1425
David Brownell6e13e642007-05-01 23:26:31 +02001426static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001428 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
David Brownell1d0b19c2008-10-14 17:30:05 +02001430 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301431 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001432 res = -EAGAIN;
1433 goto out_list;
1434 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001435
Jean Delvare2236baa2010-11-15 22:40:38 +01001436 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001437 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001438 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001439
1440 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001441 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001442 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01001443 }
1444
Peter Rosind1ed7982016-08-25 23:07:01 +02001445 if (!adap->lock_ops)
1446 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001447
Wolfram Sang9ac6cb52018-12-19 17:48:17 +01001448 adap->locked_flags = 0;
Mika Kuoppala194684e2009-12-06 17:06:22 +01001449 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001450 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001451 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001452 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Jean Delvare8fcfef62009-03-28 21:34:43 +01001454 /* Set default timeout to 1 second if not already set */
1455 if (adap->timeout == 0)
1456 adap->timeout = HZ;
1457
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001458 /* register soft irqs for Host Notify */
1459 res = i2c_setup_host_notify_irq_domain(adap);
1460 if (res) {
1461 pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n",
1462 adap->name, res);
1463 goto out_list;
1464 }
1465
Kay Sievers27d9c182009-01-07 14:29:16 +01001466 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001467 adap->dev.bus = &i2c_bus_type;
1468 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001469 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001470 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001471 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001472 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Phil Reidf8756c62017-08-24 17:31:04 +08001475 res = of_i2c_setup_smbus_alert(adap);
1476 if (res)
1477 goto out_reg;
1478
Charles Keepax6ada5c12015-04-16 13:05:19 +01001479 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001480 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001481 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001482
Codrin Ciubotariu23a698f2020-08-04 12:59:25 +03001483 res = i2c_init_recovery(adap);
1484 if (res == -EPROBE_DEFER)
1485 goto out_reg;
1486
1487 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1488
Jean Delvare2bb50952009-09-18 22:45:46 +02001489#ifdef CONFIG_I2C_COMPAT
1490 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1491 adap->dev.parent);
1492 if (res)
1493 dev_warn(&adap->dev,
1494 "Failed to create compatibility class link\n");
1495#endif
1496
Jean Delvare729d6dd2009-06-19 16:58:18 +02001497 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001498 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001499 i2c_acpi_install_space_handler(adap);
Hans de Goede21653a42020-09-09 12:32:33 +02001500 i2c_acpi_register_devices(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001501
David Brownell6e13e642007-05-01 23:26:31 +02001502 if (adap->nr < __i2c_first_dynamic_bus_num)
1503 i2c_scan_static_board_info(adap);
1504
Jean Delvare4735c982008-07-14 22:38:36 +02001505 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001506 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001507 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001508 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001509
1510 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001511
Phil Reidf8756c62017-08-24 17:31:04 +08001512out_reg:
1513 init_completion(&adap->dev_released);
1514 device_unregister(&adap->dev);
1515 wait_for_completion(&adap->dev_released);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001516out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001517 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001518 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001519 mutex_unlock(&core_lock);
1520 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
David Brownell6e13e642007-05-01 23:26:31 +02001523/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001524 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1525 * @adap: the adapter to register (with adap->nr initialized)
1526 * Context: can sleep
1527 *
1528 * See i2c_add_numbered_adapter() for details.
1529 */
1530static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1531{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001532 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001533
1534 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001535 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001536 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001537 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001538 return id == -ENOSPC ? -EBUSY : id;
1539
1540 return i2c_register_adapter(adap);
1541}
1542
1543/**
David Brownell6e13e642007-05-01 23:26:31 +02001544 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1545 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001546 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001547 *
1548 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001549 * doesn't matter or when its bus number is specified by an dt alias.
1550 * Examples of bases when the bus number doesn't matter: I2C adapters
1551 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001552 *
1553 * When this returns zero, a new bus number was allocated and stored
1554 * in adap->nr, and the specified adapter became available for clients.
1555 * Otherwise, a negative errno value is returned.
1556 */
1557int i2c_add_adapter(struct i2c_adapter *adapter)
1558{
Doug Andersonee5c2742013-03-01 08:57:31 -08001559 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001560 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001561
Doug Andersonee5c2742013-03-01 08:57:31 -08001562 if (dev->of_node) {
1563 id = of_alias_get_id(dev->of_node, "i2c");
1564 if (id >= 0) {
1565 adapter->nr = id;
1566 return __i2c_add_numbered_adapter(adapter);
1567 }
1568 }
1569
Jean Delvarecaada322008-01-27 18:14:49 +01001570 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001571 id = idr_alloc(&i2c_adapter_idr, adapter,
1572 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001573 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001574 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08001575 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001576
1577 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001578
David Brownell6e13e642007-05-01 23:26:31 +02001579 return i2c_register_adapter(adapter);
1580}
1581EXPORT_SYMBOL(i2c_add_adapter);
1582
1583/**
1584 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1585 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001586 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001587 *
1588 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001589 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1590 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001591 * is used to properly configure I2C devices.
1592 *
Grant Likely488bf312011-07-25 17:49:43 +02001593 * If the requested bus number is set to -1, then this function will behave
1594 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1595 *
David Brownell6e13e642007-05-01 23:26:31 +02001596 * If no devices have pre-been declared for this bus, then be sure to
1597 * register the adapter before any dynamically allocated ones. Otherwise
1598 * the required bus ID may not be available.
1599 *
1600 * When this returns zero, the specified adapter became available for
1601 * clients using the bus number provided in adap->nr. Also, the table
1602 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1603 * and the appropriate driver model device nodes are created. Otherwise, a
1604 * negative errno value is returned.
1605 */
1606int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1607{
Grant Likely488bf312011-07-25 17:49:43 +02001608 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1609 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001610
Doug Andersonee5c2742013-03-01 08:57:31 -08001611 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001612}
1613EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1614
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001615static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001616 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001617{
Jean Delvare4735c982008-07-14 22:38:36 +02001618 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001619
Jean Delvareacec2112009-03-28 21:34:40 +01001620 /* Remove the devices we created ourselves as the result of hardware
1621 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001622 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1623 if (client->adapter == adapter) {
1624 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1625 client->name, client->addr);
1626 list_del(&client->detected);
1627 i2c_unregister_device(client);
1628 }
1629 }
Jean Delvare026526f2008-01-27 18:14:49 +01001630}
1631
Jean Delvaree549c2b2009-06-19 16:58:19 +02001632static int __unregister_client(struct device *dev, void *dummy)
1633{
1634 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001635 if (client && strcmp(client->name, "dummy"))
1636 i2c_unregister_device(client);
1637 return 0;
1638}
1639
1640static int __unregister_dummy(struct device *dev, void *dummy)
1641{
1642 struct i2c_client *client = i2c_verify_client(dev);
Andy Shevchenko7b43dd12017-10-31 16:21:35 +02001643 i2c_unregister_device(client);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001644 return 0;
1645}
1646
Jean Delvare69b00892009-12-06 17:06:27 +01001647static int __process_removed_adapter(struct device_driver *d, void *data)
1648{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001649 i2c_do_del_adapter(to_i2c_driver(d), data);
1650 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001651}
1652
David Brownelld64f73b2007-07-12 14:12:28 +02001653/**
1654 * i2c_del_adapter - unregister I2C adapter
1655 * @adap: the adapter being unregistered
1656 * Context: can sleep
1657 *
1658 * This unregisters an I2C adapter which was previously registered
1659 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1660 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001661void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001663 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001664 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
1666 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001667 mutex_lock(&core_lock);
1668 found = idr_find(&i2c_adapter_idr, adap->nr);
1669 mutex_unlock(&core_lock);
1670 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001671 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001672 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 }
1674
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001675 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001676 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001677 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001678 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001679 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001680 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001682 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001683 mutex_lock_nested(&adap->userspace_clients_lock,
1684 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001685 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1686 detected) {
1687 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1688 client->addr);
1689 list_del(&client->detected);
1690 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001691 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001692 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001693
Jean Delvaree549c2b2009-06-19 16:58:19 +02001694 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001695 * check the returned value. This is a two-pass process, because
1696 * we can't remove the dummy devices during the first pass: they
1697 * could have been instantiated by real devices wishing to clean
1698 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001699 device_for_each_child(&adap->dev, NULL, __unregister_client);
1700 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Jean Delvare2bb50952009-09-18 22:45:46 +02001702#ifdef CONFIG_I2C_COMPAT
1703 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1704 adap->dev.parent);
1705#endif
1706
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001707 /* device name is gone after device_unregister */
1708 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1709
Wolfram Sang9f924162015-12-23 18:19:28 +01001710 pm_runtime_disable(&adap->dev);
1711
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001712 i2c_host_notify_irq_teardown(adap);
1713
Wolfram Sang26680ee2015-01-29 22:45:09 +01001714 /* wait until all references to the device are gone
1715 *
1716 * FIXME: This is old code and should ideally be replaced by an
1717 * alternative which results in decoupling the lifetime of the struct
1718 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301719 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001720 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724
David Brownell6e13e642007-05-01 23:26:31 +02001725 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001726 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001728 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001730 /* Clear the device structure in case this adapter is ever going to be
1731 added again */
1732 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
David Brownellc0564602007-05-01 23:26:31 +02001734EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Yicong Yang07740c92021-04-08 19:17:17 +08001736static void devm_i2c_del_adapter(void *adapter)
1737{
1738 i2c_del_adapter(adapter);
1739}
1740
1741/**
1742 * devm_i2c_add_adapter - device-managed variant of i2c_add_adapter()
1743 * @dev: managing device for adding this I2C adapter
1744 * @adapter: the adapter to add
1745 * Context: can sleep
1746 *
1747 * Add adapter with dynamic bus number, same with i2c_add_adapter()
1748 * but the adapter will be auto deleted on driver detach.
1749 */
1750int devm_i2c_add_adapter(struct device *dev, struct i2c_adapter *adapter)
1751{
1752 int ret;
1753
1754 ret = i2c_add_adapter(adapter);
1755 if (ret)
1756 return ret;
1757
1758 return devm_add_action_or_reset(dev, devm_i2c_del_adapter, adapter);
1759}
1760EXPORT_SYMBOL_GPL(devm_i2c_add_adapter);
1761
Wolfram Sangdef00b32020-04-01 21:30:18 +02001762static void i2c_parse_timing(struct device *dev, char *prop_name, u32 *cur_val_p,
1763 u32 def_val, bool use_def)
1764{
1765 int ret;
1766
1767 ret = device_property_read_u32(dev, prop_name, cur_val_p);
1768 if (ret && use_def)
1769 *cur_val_p = def_val;
1770
1771 dev_dbg(dev, "%s: %u\n", prop_name, *cur_val_p);
1772}
1773
Wolfram Sang54177cc2015-12-17 13:32:36 +01001774/**
1775 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
1776 * @dev: The device to scan for I2C timing properties
1777 * @t: the i2c_timings struct to be filled with values
1778 * @use_defaults: bool to use sane defaults derived from the I2C specification
Andy Shevchenko263a5642020-03-24 14:32:12 +02001779 * when properties are not found, otherwise don't update
Wolfram Sang54177cc2015-12-17 13:32:36 +01001780 *
1781 * Scan the device for the generic I2C properties describing timing parameters
1782 * for the signal and fill the given struct with the results. If a property was
1783 * not found and use_defaults was true, then maximum timings are assumed which
1784 * are derived from the I2C specification. If use_defaults is not used, the
Andy Shevchenko263a5642020-03-24 14:32:12 +02001785 * results will be as before, so drivers can apply their own defaults before
1786 * calling this helper. The latter is mainly intended for avoiding regressions
1787 * of existing drivers which want to switch to this function. New drivers
1788 * almost always should use the defaults.
Wolfram Sang54177cc2015-12-17 13:32:36 +01001789 */
Wolfram Sang54177cc2015-12-17 13:32:36 +01001790void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
1791{
Wolfram Sangdef00b32020-04-01 21:30:18 +02001792 bool u = use_defaults;
1793 u32 d;
Wolfram Sang54177cc2015-12-17 13:32:36 +01001794
Wolfram Sangdef00b32020-04-01 21:30:18 +02001795 i2c_parse_timing(dev, "clock-frequency", &t->bus_freq_hz,
1796 I2C_MAX_STANDARD_MODE_FREQ, u);
Wolfram Sang54177cc2015-12-17 13:32:36 +01001797
Wolfram Sangdef00b32020-04-01 21:30:18 +02001798 d = t->bus_freq_hz <= I2C_MAX_STANDARD_MODE_FREQ ? 1000 :
1799 t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ ? 300 : 120;
1800 i2c_parse_timing(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns, d, u);
Wolfram Sang54177cc2015-12-17 13:32:36 +01001801
Wolfram Sangdef00b32020-04-01 21:30:18 +02001802 d = t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ ? 300 : 120;
1803 i2c_parse_timing(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns, d, u);
Wolfram Sang54177cc2015-12-17 13:32:36 +01001804
Wolfram Sangdef00b32020-04-01 21:30:18 +02001805 i2c_parse_timing(dev, "i2c-scl-internal-delay-ns",
1806 &t->scl_int_delay_ns, 0, u);
1807 i2c_parse_timing(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns,
1808 t->scl_fall_ns, u);
1809 i2c_parse_timing(dev, "i2c-sda-hold-time-ns", &t->sda_hold_ns, 0, u);
1810 i2c_parse_timing(dev, "i2c-digital-filter-width-ns",
1811 &t->digital_filter_width_ns, 0, u);
1812 i2c_parse_timing(dev, "i2c-analog-filter-cutoff-frequency",
1813 &t->analog_filter_cutoff_freq_hz, 0, u);
Wolfram Sang54177cc2015-12-17 13:32:36 +01001814}
1815EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
1816
David Brownell7b4fbc52007-05-01 23:26:30 +02001817/* ------------------------------------------------------------------------- */
1818
Wolfram Sangedd7a562019-06-03 10:25:33 +02001819int i2c_for_each_dev(void *data, int (*fn)(struct device *dev, void *data))
Jean Delvare7ae31482011-03-20 14:50:52 +01001820{
1821 int res;
1822
1823 mutex_lock(&core_lock);
1824 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1825 mutex_unlock(&core_lock);
1826
1827 return res;
1828}
1829EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1830
Jean Delvare69b00892009-12-06 17:06:27 +01001831static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001832{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001833 if (dev->type != &i2c_adapter_type)
1834 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001835 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001836}
1837
David Brownell7b4fbc52007-05-01 23:26:30 +02001838/*
1839 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001840 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 */
1842
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001843int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001845 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
David Brownell1d0b19c2008-10-14 17:30:05 +02001847 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301848 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02001849 return -EAGAIN;
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001852 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02001854 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Jean Delvare729d6dd2009-06-19 16:58:18 +02001856 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001857 * will have called probe() for all matching-but-unbound devices.
1858 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 res = driver_register(&driver->driver);
1860 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001861 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001862
Wolfram Sang44239a52016-07-09 13:35:04 +09001863 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Jean Delvare4735c982008-07-14 22:38:36 +02001865 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001866 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001867
Jean Delvare7eebcb72006-02-05 23:28:21 +01001868 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001870EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Jean Delvare69b00892009-12-06 17:06:27 +01001872static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001873{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001874 if (dev->type == &i2c_adapter_type)
1875 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1876 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001877}
1878
David Brownella1d9e6e2007-05-01 23:26:30 +02001879/**
1880 * i2c_del_driver - unregister I2C driver
1881 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001882 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001883 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001884void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885{
Jean Delvare7ae31482011-03-20 14:50:52 +01001886 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09001889 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890}
David Brownellc0564602007-05-01 23:26:31 +02001891EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
David Brownell7b4fbc52007-05-01 23:26:30 +02001893/* ------------------------------------------------------------------------- */
1894
David Brownell9b766b82008-01-27 18:14:51 +01001895struct i2c_cmd_arg {
1896 unsigned cmd;
1897 void *arg;
1898};
1899
1900static int i2c_cmd(struct device *dev, void *_arg)
1901{
1902 struct i2c_client *client = i2c_verify_client(dev);
1903 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001904 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001905
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001906 if (!client || !client->dev.driver)
1907 return 0;
1908
1909 driver = to_i2c_driver(client->dev.driver);
1910 if (driver->command)
1911 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001912 return 0;
1913}
1914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1916{
David Brownell9b766b82008-01-27 18:14:51 +01001917 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
David Brownell9b766b82008-01-27 18:14:51 +01001919 cmd_arg.cmd = cmd;
1920 cmd_arg.arg = arg;
1921 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
David Brownellc0564602007-05-01 23:26:31 +02001923EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
1925static int __init i2c_init(void)
1926{
1927 int retval;
1928
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001929 retval = of_alias_get_highest_id("i2c");
1930
1931 down_write(&__i2c_board_lock);
1932 if (retval >= __i2c_first_dynamic_bus_num)
1933 __i2c_first_dynamic_bus_num = retval + 1;
1934 up_write(&__i2c_board_lock);
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 retval = bus_register(&i2c_bus_type);
1937 if (retval)
1938 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01001939
1940 is_registered = true;
1941
Jean Delvare2bb50952009-09-18 22:45:46 +02001942#ifdef CONFIG_I2C_COMPAT
1943 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1944 if (!i2c_adapter_compat_class) {
1945 retval = -ENOMEM;
1946 goto bus_err;
1947 }
1948#endif
David Brownelle9f13732008-01-27 18:14:52 +01001949 retval = i2c_add_driver(&dummy_driver);
1950 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001951 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001952
1953 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1954 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001955 if (IS_ENABLED(CONFIG_ACPI))
1956 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001957
David Brownelle9f13732008-01-27 18:14:52 +01001958 return 0;
1959
Jean Delvare2bb50952009-09-18 22:45:46 +02001960class_err:
1961#ifdef CONFIG_I2C_COMPAT
1962 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001963bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001964#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01001965 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01001966 bus_unregister(&i2c_bus_type);
1967 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
1970static void __exit i2c_exit(void)
1971{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001972 if (IS_ENABLED(CONFIG_ACPI))
1973 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001974 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1975 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001976 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001977#ifdef CONFIG_I2C_COMPAT
1978 class_compat_unregister(i2c_adapter_compat_class);
1979#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001981 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
David Brownella10f9e72008-10-14 17:30:06 +02001984/* We must initialize early, because some subsystems register i2c drivers
1985 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1986 */
1987postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988module_exit(i2c_exit);
1989
1990/* ----------------------------------------------------
1991 * the functional interface to the i2c busses.
1992 * ----------------------------------------------------
1993 */
1994
Wolfram Sangb7f62582015-01-05 23:45:59 +01001995/* Check if val is exceeding the quirk IFF quirk is non 0 */
1996#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1997
1998static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
1999{
2000 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2001 err_msg, msg->addr, msg->len,
2002 msg->flags & I2C_M_RD ? "read" : "write");
2003 return -EOPNOTSUPP;
2004}
2005
2006static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2007{
2008 const struct i2c_adapter_quirks *q = adap->quirks;
2009 int max_num = q->max_num_msgs, i;
2010 bool do_len_check = true;
2011
2012 if (q->flags & I2C_AQ_COMB) {
2013 max_num = 2;
2014
2015 /* special checks for combined messages */
2016 if (num == 2) {
2017 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
2018 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
2019
2020 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
2021 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
2022
2023 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
2024 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
2025
2026 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
2027 return i2c_quirk_error(adap, &msgs[0], "msg too long");
2028
2029 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
2030 return i2c_quirk_error(adap, &msgs[1], "msg too long");
2031
2032 do_len_check = false;
2033 }
2034 }
2035
2036 if (i2c_quirk_exceeded(num, max_num))
2037 return i2c_quirk_error(adap, &msgs[0], "too many messages");
2038
2039 for (i = 0; i < num; i++) {
2040 u16 len = msgs[i].len;
2041
2042 if (msgs[i].flags & I2C_M_RD) {
2043 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
2044 return i2c_quirk_error(adap, &msgs[i], "msg too long");
Wolfram Sangd9cfe2c2018-07-23 22:26:05 +02002045
2046 if (q->flags & I2C_AQ_NO_ZERO_LEN_READ && len == 0)
2047 return i2c_quirk_error(adap, &msgs[i], "no zero length");
Wolfram Sangb7f62582015-01-05 23:45:59 +01002048 } else {
2049 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
2050 return i2c_quirk_error(adap, &msgs[i], "msg too long");
Wolfram Sangd9cfe2c2018-07-23 22:26:05 +02002051
2052 if (q->flags & I2C_AQ_NO_ZERO_LEN_WRITE && len == 0)
2053 return i2c_quirk_error(adap, &msgs[i], "no zero length");
Wolfram Sangb7f62582015-01-05 23:45:59 +01002054 }
2055 }
2056
2057 return 0;
2058}
2059
David Brownella1cdeda2008-07-14 22:38:24 +02002060/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03002061 * __i2c_transfer - unlocked flavor of i2c_transfer
2062 * @adap: Handle to I2C bus
2063 * @msgs: One or more messages to execute before STOP is issued to
2064 * terminate the operation; each message begins with a START.
2065 * @num: Number of messages to be executed.
2066 *
2067 * Returns negative errno, else the number of messages executed.
2068 *
2069 * Adapter lock must be held when calling this function. No debug logging
2070 * takes place. adap->algo->master_xfer existence isn't checked.
2071 */
2072int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
2073{
2074 unsigned long orig_jiffies;
2075 int ret, try;
2076
Ard Biesheuvel1eace832018-04-03 21:11:50 +02002077 if (WARN_ON(!msgs || num < 1))
2078 return -EINVAL;
Wolfram Sang5d756112019-04-25 16:19:48 +02002079
2080 ret = __i2c_check_suspended(adap);
2081 if (ret)
2082 return ret;
Ard Biesheuvel1eace832018-04-03 21:11:50 +02002083
Wolfram Sangb7f62582015-01-05 23:45:59 +01002084 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
2085 return -EOPNOTSUPP;
2086
Davidlohr Bueso50888b02018-03-26 14:09:24 -07002087 /*
2088 * i2c_trace_msg_key gets enabled when tracepoint i2c_transfer gets
David Howellsd9a83d62014-03-06 13:35:59 +00002089 * enabled. This is an efficient way of keeping the for-loop from
2090 * being executed when not needed.
2091 */
Davidlohr Bueso50888b02018-03-26 14:09:24 -07002092 if (static_branch_unlikely(&i2c_trace_msg_key)) {
David Howellsd9a83d62014-03-06 13:35:59 +00002093 int i;
2094 for (i = 0; i < num; i++)
2095 if (msgs[i].flags & I2C_M_RD)
2096 trace_i2c_read(adap, &msgs[i], i);
2097 else
2098 trace_i2c_write(adap, &msgs[i], i);
2099 }
2100
Jean Delvareb37d2a32012-06-29 07:47:19 -03002101 /* Retry automatically on arbitration loss */
2102 orig_jiffies = jiffies;
2103 for (ret = 0, try = 0; try <= adap->retries; try++) {
Wolfram Sang63b96982019-04-03 14:40:10 +02002104 if (i2c_in_atomic_xfer_mode() && adap->algo->master_xfer_atomic)
2105 ret = adap->algo->master_xfer_atomic(adap, msgs, num);
2106 else
2107 ret = adap->algo->master_xfer(adap, msgs, num);
2108
Jean Delvareb37d2a32012-06-29 07:47:19 -03002109 if (ret != -EAGAIN)
2110 break;
2111 if (time_after(jiffies, orig_jiffies + adap->timeout))
2112 break;
2113 }
2114
Davidlohr Bueso50888b02018-03-26 14:09:24 -07002115 if (static_branch_unlikely(&i2c_trace_msg_key)) {
David Howellsd9a83d62014-03-06 13:35:59 +00002116 int i;
2117 for (i = 0; i < ret; i++)
2118 if (msgs[i].flags & I2C_M_RD)
2119 trace_i2c_reply(adap, &msgs[i], i);
Ahbong Chang4a3f7692018-03-31 16:17:34 +08002120 trace_i2c_result(adap, num, ret);
David Howellsd9a83d62014-03-06 13:35:59 +00002121 }
2122
Jean Delvareb37d2a32012-06-29 07:47:19 -03002123 return ret;
2124}
2125EXPORT_SYMBOL(__i2c_transfer);
2126
2127/**
David Brownella1cdeda2008-07-14 22:38:24 +02002128 * i2c_transfer - execute a single or combined I2C message
2129 * @adap: Handle to I2C bus
2130 * @msgs: One or more messages to execute before STOP is issued to
2131 * terminate the operation; each message begins with a START.
2132 * @num: Number of messages to be executed.
2133 *
2134 * Returns negative errno, else the number of messages executed.
2135 *
2136 * Note that there is no requirement that each message be sent to
2137 * the same slave address, although that is the most common model.
2138 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01002139int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
Jean Delvareb37d2a32012-06-29 07:47:19 -03002141 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Wolfram Sangcc526122018-09-20 18:14:21 +02002143 if (!adap->algo->master_xfer) {
2144 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
2145 return -EOPNOTSUPP;
2146 }
2147
David Brownella1cdeda2008-07-14 22:38:24 +02002148 /* REVISIT the fault reporting model here is weak:
2149 *
2150 * - When we get an error after receiving N bytes from a slave,
2151 * there is no way to report "N".
2152 *
2153 * - When we get a NAK after transmitting N bytes to a slave,
2154 * there is no way to report "N" ... or to let the master
2155 * continue executing the rest of this combined message, if
2156 * that's the appropriate response.
2157 *
2158 * - When for example "num" is two and we successfully complete
2159 * the first message but get an error part way through the
2160 * second, it's unclear whether that should be reported as
2161 * one (discarding status on the second message) or errno
2162 * (discarding status on the first one).
2163 */
Wolfram Sang83c42212019-04-03 14:40:09 +02002164 ret = __i2c_lock_bus_helper(adap);
2165 if (ret)
2166 return ret;
Wolfram Sangcc526122018-09-20 18:14:21 +02002167
2168 ret = __i2c_transfer(adap, msgs, num);
2169 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
2170
2171 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
David Brownellc0564602007-05-01 23:26:31 +02002173EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
David Brownella1cdeda2008-07-14 22:38:24 +02002175/**
Wolfram Sang8a917322017-11-04 21:20:04 +01002176 * i2c_transfer_buffer_flags - issue a single I2C message transferring data
2177 * to/from a buffer
David Brownella1cdeda2008-07-14 22:38:24 +02002178 * @client: Handle to slave device
Wolfram Sang8a917322017-11-04 21:20:04 +01002179 * @buf: Where the data is stored
2180 * @count: How many bytes to transfer, must be less than 64k since msg.len is u16
2181 * @flags: The flags to be used for the message, e.g. I2C_M_RD for reads
David Brownella1cdeda2008-07-14 22:38:24 +02002182 *
Wolfram Sang8a917322017-11-04 21:20:04 +01002183 * Returns negative errno, or else the number of bytes transferred.
David Brownella1cdeda2008-07-14 22:38:24 +02002184 */
Wolfram Sang8a917322017-11-04 21:20:04 +01002185int i2c_transfer_buffer_flags(const struct i2c_client *client, char *buf,
2186 int count, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 int ret;
Wolfram Sang8a917322017-11-04 21:20:04 +01002189 struct i2c_msg msg = {
2190 .addr = client->addr,
2191 .flags = flags | (client->flags & I2C_M_TEN),
2192 .len = count,
2193 .buf = buf,
2194 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Wolfram Sang8a917322017-11-04 21:20:04 +01002196 ret = i2c_transfer(client->adapter, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002198 /*
Wolfram Sang8a917322017-11-04 21:20:04 +01002199 * If everything went ok (i.e. 1 msg transferred), return #bytes
2200 * transferred, else error code.
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002201 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002202 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
Wolfram Sang8a917322017-11-04 21:20:04 +01002204EXPORT_SYMBOL(i2c_transfer_buffer_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Peter Rosindde67eb2018-01-22 08:32:01 +01002206/**
2207 * i2c_get_device_id - get manufacturer, part id and die revision of a device
2208 * @client: The device to query
2209 * @id: The queried information
2210 *
2211 * Returns negative errno on error, zero on success.
2212 */
2213int i2c_get_device_id(const struct i2c_client *client,
2214 struct i2c_device_identity *id)
2215{
2216 struct i2c_adapter *adap = client->adapter;
2217 union i2c_smbus_data raw_id;
2218 int ret;
2219
2220 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
2221 return -EOPNOTSUPP;
2222
2223 raw_id.block[0] = 3;
2224 ret = i2c_smbus_xfer(adap, I2C_ADDR_DEVICE_ID, 0,
2225 I2C_SMBUS_READ, client->addr << 1,
2226 I2C_SMBUS_I2C_BLOCK_DATA, &raw_id);
2227 if (ret)
2228 return ret;
2229
2230 id->manufacturer_id = (raw_id.block[1] << 4) | (raw_id.block[2] >> 4);
2231 id->part_id = ((raw_id.block[2] & 0xf) << 5) | (raw_id.block[3] >> 3);
2232 id->die_revision = raw_id.block[3] & 0x7;
2233 return 0;
2234}
2235EXPORT_SYMBOL_GPL(i2c_get_device_id);
2236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237/* ----------------------------------------------------
2238 * the i2c address scanning function
2239 * Will not work for 10-bit addresses!
2240 * ----------------------------------------------------
2241 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002242
Jean Delvare63e4e802010-06-03 11:33:51 +02002243/*
2244 * Legacy default probe function, mostly relevant for SMBus. The default
2245 * probe method is a quick write, but it is known to corrupt the 24RF08
2246 * EEPROMs due to a state machine bug, and could also irreversibly
2247 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2248 * we use a short byte read instead. Also, some bus drivers don't implement
2249 * quick write, so we fallback to a byte read in that case too.
2250 * On x86, there is another special case for FSC hardware monitoring chips,
2251 * which want regular byte reads (address 0x73.) Fortunately, these are the
2252 * only known chips using this I2C address on PC hardware.
2253 * Returns 1 if probe succeeded, 0 if not.
2254 */
2255static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2256{
2257 int err;
2258 union i2c_smbus_data dummy;
2259
2260#ifdef CONFIG_X86
2261 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2262 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2263 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2264 I2C_SMBUS_BYTE_DATA, &dummy);
2265 else
2266#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002267 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2268 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002269 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2270 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002271 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2272 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2273 I2C_SMBUS_BYTE, &dummy);
2274 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002275 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2276 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002277 err = -EOPNOTSUPP;
2278 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002279
2280 return err >= 0;
2281}
2282
Jean Delvareccfbbd02009-12-06 17:06:25 +01002283static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002284 struct i2c_driver *driver)
2285{
2286 struct i2c_board_info info;
2287 struct i2c_adapter *adapter = temp_client->adapter;
2288 int addr = temp_client->addr;
2289 int err;
2290
2291 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002292 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002293 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002294 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2295 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002296 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002297 }
2298
Wolfram Sang9bccc702015-07-17 14:48:56 +02002299 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002300 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002301 return 0;
2302
Jean Delvareccfbbd02009-12-06 17:06:25 +01002303 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002304 if (!i2c_default_probe(adapter, addr))
2305 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002306
2307 /* Finally call the custom detection function */
2308 memset(&info, 0, sizeof(struct i2c_board_info));
2309 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002310 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002311 if (err) {
2312 /* -ENODEV is returned if the detection fails. We catch it
2313 here as this isn't an error. */
2314 return err == -ENODEV ? 0 : err;
2315 }
2316
2317 /* Consistency check */
2318 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002319 dev_err(&adapter->dev,
2320 "%s detection function provided no name for 0x%x\n",
2321 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002322 } else {
2323 struct i2c_client *client;
2324
2325 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002326 if (adapter->class & I2C_CLASS_DEPRECATED)
2327 dev_warn(&adapter->dev,
2328 "This adapter will soon drop class based instantiation of devices. "
2329 "Please make sure client 0x%02x gets instantiated by other means. "
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -03002330 "Check 'Documentation/i2c/instantiating-devices.rst' for details.\n",
Wolfram Sang0c176172014-02-10 11:03:56 +01002331 info.addr);
2332
Jean Delvare4735c982008-07-14 22:38:36 +02002333 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2334 info.type, info.addr);
Wolfram Sang87e07432020-01-07 18:47:43 +01002335 client = i2c_new_client_device(adapter, &info);
2336 if (!IS_ERR(client))
Jean Delvare4735c982008-07-14 22:38:36 +02002337 list_add_tail(&client->detected, &driver->clients);
2338 else
2339 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2340 info.type, info.addr);
2341 }
2342 return 0;
2343}
2344
2345static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2346{
Jean Delvarec3813d62009-12-14 21:17:25 +01002347 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002348 struct i2c_client *temp_client;
2349 int i, err = 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002350
Jean Delvarec3813d62009-12-14 21:17:25 +01002351 address_list = driver->address_list;
2352 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002353 return 0;
2354
Wolfram Sang45552272014-07-10 13:46:21 +02002355 /* Warn that the adapter lost class based instantiation */
2356 if (adapter->class == I2C_CLASS_DEPRECATED) {
2357 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002358 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -03002359 "If you need it, check 'Documentation/i2c/instantiating-devices.rst' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002360 driver->driver.name);
2361 return 0;
2362 }
2363
Jean Delvare51b54ba2010-10-24 18:16:58 +02002364 /* Stop here if the classes do not match */
2365 if (!(adapter->class & driver->class))
2366 return 0;
2367
Jean Delvare4735c982008-07-14 22:38:36 +02002368 /* Set up a temporary client to help detect callback */
2369 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2370 if (!temp_client)
2371 return -ENOMEM;
2372 temp_client->adapter = adapter;
2373
Jean Delvarec3813d62009-12-14 21:17:25 +01002374 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002375 dev_dbg(&adapter->dev,
2376 "found normal entry for adapter %d, addr 0x%02x\n",
Wolfram Sangdd4f2ca2020-05-19 13:48:09 +02002377 i2c_adapter_id(adapter), address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002378 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002379 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002380 if (unlikely(err))
2381 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002382 }
2383
Jean Delvare4735c982008-07-14 22:38:36 +02002384 kfree(temp_client);
2385 return err;
2386}
2387
Jean Delvared44f19d2010-08-11 18:20:57 +02002388int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2389{
2390 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2391 I2C_SMBUS_QUICK, NULL) >= 0;
2392}
2393EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2394
Jean Delvare12b5053a2007-05-01 23:26:31 +02002395struct i2c_client *
Wolfram Sangc1d084752019-11-06 10:50:19 +01002396i2c_new_scanned_device(struct i2c_adapter *adap,
2397 struct i2c_board_info *info,
2398 unsigned short const *addr_list,
2399 int (*probe)(struct i2c_adapter *adap, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002400{
2401 int i;
2402
Jean Delvare8031d792010-08-11 18:21:00 +02002403 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002404 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002405
Jean Delvare12b5053a2007-05-01 23:26:31 +02002406 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2407 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002408 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002409 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2410 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002411 continue;
2412 }
2413
Wolfram Sang9bccc702015-07-17 14:48:56 +02002414 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002415 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002416 dev_dbg(&adap->dev,
2417 "Address 0x%02x already in use, not probing\n",
2418 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002419 continue;
2420 }
2421
Jean Delvare63e4e802010-06-03 11:33:51 +02002422 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002423 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002424 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002425 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002426
2427 if (addr_list[i] == I2C_CLIENT_END) {
2428 dev_dbg(&adap->dev, "Probing failed, no device found\n");
Wolfram Sangc1d084752019-11-06 10:50:19 +01002429 return ERR_PTR(-ENODEV);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002430 }
2431
2432 info->addr = addr_list[i];
Wolfram Sangc1d084752019-11-06 10:50:19 +01002433 return i2c_new_client_device(adap, info);
2434}
2435EXPORT_SYMBOL_GPL(i2c_new_scanned_device);
2436
Jean Delvared735b342011-03-20 14:50:52 +01002437struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002440
Jean Delvarecaada322008-01-27 18:14:49 +01002441 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002442 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002443 if (!adapter)
2444 goto exit;
2445
2446 if (try_module_get(adapter->owner))
2447 get_device(&adapter->dev);
2448 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002449 adapter = NULL;
2450
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002451 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002452 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002453 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
David Brownellc0564602007-05-01 23:26:31 +02002455EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457void i2c_put_adapter(struct i2c_adapter *adap)
2458{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002459 if (!adap)
2460 return;
2461
2462 put_device(&adap->dev);
2463 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464}
David Brownellc0564602007-05-01 23:26:31 +02002465EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002467/**
2468 * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg
2469 * @msg: the message to be checked
Wolfram Sangbf263c32019-03-12 13:44:42 +01002470 * @threshold: the minimum number of bytes for which using DMA makes sense.
2471 * Should at least be 1.
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002472 *
2473 * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.
2474 * Or a valid pointer to be used with DMA. After use, release it by
Stephen Boyd34d1b822018-10-08 07:55:20 -07002475 * calling i2c_put_dma_safe_msg_buf().
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002476 *
2477 * This function must only be called from process context!
2478 */
2479u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)
2480{
Wolfram Sangbf263c32019-03-12 13:44:42 +01002481 /* also skip 0-length msgs for bogus thresholds of 0 */
2482 if (!threshold)
2483 pr_debug("DMA buffer for addr=0x%02x with length 0 is bogus\n",
2484 msg->addr);
2485 if (msg->len < threshold || msg->len == 0)
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002486 return NULL;
2487
2488 if (msg->flags & I2C_M_DMA_SAFE)
2489 return msg->buf;
2490
2491 pr_debug("using bounce buffer for addr=0x%02x, len=%d\n",
2492 msg->addr, msg->len);
2493
2494 if (msg->flags & I2C_M_RD)
2495 return kzalloc(msg->len, GFP_KERNEL);
2496 else
2497 return kmemdup(msg->buf, msg->len, GFP_KERNEL);
2498}
2499EXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf);
2500
2501/**
Wolfram Sang82fe39a2018-08-24 16:52:44 +02002502 * i2c_put_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002503 * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.
Wolfram Sang82fe39a2018-08-24 16:52:44 +02002504 * @msg: the message which the buffer corresponds to
2505 * @xferred: bool saying if the message was transferred
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002506 */
Wolfram Sang82fe39a2018-08-24 16:52:44 +02002507void i2c_put_dma_safe_msg_buf(u8 *buf, struct i2c_msg *msg, bool xferred)
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002508{
2509 if (!buf || buf == msg->buf)
2510 return;
2511
Wolfram Sang82fe39a2018-08-24 16:52:44 +02002512 if (xferred && msg->flags & I2C_M_RD)
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002513 memcpy(msg->buf, buf, msg->len);
2514
2515 kfree(buf);
2516}
Wolfram Sang82fe39a2018-08-24 16:52:44 +02002517EXPORT_SYMBOL_GPL(i2c_put_dma_safe_msg_buf);
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2520MODULE_DESCRIPTION("I2C-Bus main module");
2521MODULE_LICENSE("GPL");