blob: 1f1442dfcad7b0c478e261932a1b4330cd208cd6 [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>
35#include <linux/pm_domain.h>
36#include <linux/pm_runtime.h>
37#include <linux/pm_wakeirq.h>
Wolfram Sange1dba012015-12-08 10:37:46 +010038#include <linux/property.h>
Wolfram Sang53feedf2016-02-20 23:33:38 +010039#include <linux/rwsem.h>
40#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
David Brownell9c1600e2007-05-01 23:26:31 +020042#include "i2c-core.h"
43
David Howellsd9a83d62014-03-06 13:35:59 +000044#define CREATE_TRACE_POINTS
45#include <trace/events/i2c.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Wolfram Sangda899f52015-05-18 21:09:12 +020047#define I2C_ADDR_OFFSET_TEN_BIT 0xa000
48#define I2C_ADDR_OFFSET_SLAVE 0x1000
49
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +020050#define I2C_ADDR_7BITS_MAX 0x77
51#define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1)
52
Peter Rosindde67eb2018-01-22 08:32:01 +010053#define I2C_ADDR_DEVICE_ID 0x7c
54
Wolfram Sang61e3d0f2017-05-23 19:23:11 +020055/*
56 * core_lock protects i2c_adapter_idr, and guarantees that device detection,
Wolfram Sang0c36dd32018-08-21 17:02:40 +020057 * deletion of detected devices are serialized
Wolfram Sang61e3d0f2017-05-23 19:23:11 +020058 */
Jean Delvarecaada322008-01-27 18:14:49 +010059static DEFINE_MUTEX(core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060static DEFINE_IDR(i2c_adapter_idr);
61
Jean Delvare4735c982008-07-14 22:38:36 +020062static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
David Brownellf37dd802007-02-13 22:09:00 +010063
Davidlohr Bueso50888b02018-03-26 14:09:24 -070064static DEFINE_STATIC_KEY_FALSE(i2c_trace_msg_key);
Sudip Mukherjee95026652016-03-07 17:19:17 +053065static bool is_registered;
David Howellsd9a83d62014-03-06 13:35:59 +000066
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050067int i2c_transfer_trace_reg(void)
David Howellsd9a83d62014-03-06 13:35:59 +000068{
Davidlohr Bueso50888b02018-03-26 14:09:24 -070069 static_branch_inc(&i2c_trace_msg_key);
Steven Rostedt (Red Hat)8cf868a2016-11-28 13:03:21 -050070 return 0;
David Howellsd9a83d62014-03-06 13:35:59 +000071}
72
73void i2c_transfer_trace_unreg(void)
74{
Davidlohr Bueso50888b02018-03-26 14:09:24 -070075 static_branch_dec(&i2c_trace_msg_key);
David Howellsd9a83d62014-03-06 13:35:59 +000076}
77
Lee Jones5f441fc2016-11-07 12:47:40 +000078const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
Jean Delvared2653e92008-04-29 23:11:39 +020079 const struct i2c_client *client)
80{
Lee Jones811073b2016-11-07 12:47:36 +000081 if (!(id && client))
82 return NULL;
83
Jean Delvared2653e92008-04-29 23:11:39 +020084 while (id->name[0]) {
85 if (strcmp(client->name, id->name) == 0)
86 return id;
87 id++;
88 }
89 return NULL;
90}
Lee Jones5f441fc2016-11-07 12:47:40 +000091EXPORT_SYMBOL_GPL(i2c_match_id);
Jean Delvared2653e92008-04-29 23:11:39 +020092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static int i2c_device_match(struct device *dev, struct device_driver *drv)
94{
Jean Delvare51298d12009-09-18 22:45:45 +020095 struct i2c_client *client = i2c_verify_client(dev);
96 struct i2c_driver *driver;
David Brownell7b4fbc52007-05-01 23:26:30 +020097
Jean Delvare51298d12009-09-18 22:45:45 +020098
Grant Likely959e85f2010-06-08 07:48:19 -060099 /* Attempt an OF style match */
Lee Jonesda10c062016-11-07 12:47:39 +0000100 if (i2c_of_match_device(drv->of_match_table, client))
Grant Likely959e85f2010-06-08 07:48:19 -0600101 return 1;
102
Mika Westerberg907ddf82012-11-23 12:23:40 +0100103 /* Then ACPI style match */
104 if (acpi_driver_match_device(dev, drv))
105 return 1;
106
Jean Delvare51298d12009-09-18 22:45:45 +0200107 driver = to_i2c_driver(drv);
Lee Jones811073b2016-11-07 12:47:36 +0000108
109 /* Finally an I2C match */
110 if (i2c_match_id(driver->id_table, client))
111 return 1;
Jean Delvared2653e92008-04-29 23:11:39 +0200112
Jean Delvareeb8a7902008-05-18 20:49:41 +0200113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Kay Sievers7eff2e72007-08-14 15:15:12 +0200116static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
David Brownell7b4fbc52007-05-01 23:26:30 +0200117{
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100118 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800119 int rc;
120
Javier Martinez Canillasaf503712017-12-03 22:40:50 +0100121 rc = of_device_uevent_modalias(dev, env);
122 if (rc != -ENODEV)
123 return rc;
124
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800125 rc = acpi_device_uevent_modalias(dev, env);
126 if (rc != -ENODEV)
127 return rc;
David Brownell7b4fbc52007-05-01 23:26:30 +0200128
Wolfram Sang8dcf3212016-03-23 20:47:02 +0100129 return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200130}
131
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530132/* i2c bus recovery routines */
133static int get_scl_gpio_value(struct i2c_adapter *adap)
134{
Phil Reid3991c5c2017-11-02 10:40:24 +0800135 return gpiod_get_value_cansleep(adap->bus_recovery_info->scl_gpiod);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530136}
137
138static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
139{
Phil Reid3991c5c2017-11-02 10:40:24 +0800140 gpiod_set_value_cansleep(adap->bus_recovery_info->scl_gpiod, val);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530141}
142
143static int get_sda_gpio_value(struct i2c_adapter *adap)
144{
Phil Reid3991c5c2017-11-02 10:40:24 +0800145 return gpiod_get_value_cansleep(adap->bus_recovery_info->sda_gpiod);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530146}
147
Wolfram Sang80921782018-01-09 14:58:56 +0100148static void set_sda_gpio_value(struct i2c_adapter *adap, int val)
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530149{
Wolfram Sang80921782018-01-09 14:58:56 +0100150 gpiod_set_value_cansleep(adap->bus_recovery_info->sda_gpiod, val);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530151}
152
Wolfram Sang7ca5f6b2018-07-11 00:24:22 +0200153static int i2c_generic_bus_free(struct i2c_adapter *adap)
154{
155 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
156 int ret = -EOPNOTSUPP;
157
158 if (bri->get_bus_free)
159 ret = bri->get_bus_free(adap);
160 else if (bri->get_sda)
161 ret = bri->get_sda(adap);
162
163 if (ret < 0)
164 return ret;
165
166 return ret ? 0 : -EBUSY;
167}
168
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530169/*
170 * We are generating clock pulses. ndelay() determines durating of clk pulses.
171 * We will generate clock with rate 100 KHz and so duration of both clock levels
172 * is: delay in ns = (10^6 / 100) / 2
173 */
174#define RECOVERY_NDELAY 5000
175#define RECOVERY_CLK_CNT 9
176
Phil Reide1eb7d22017-11-02 10:40:30 +0800177int i2c_generic_scl_recovery(struct i2c_adapter *adap)
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530178{
179 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
Linus Torvaldscf676902019-05-01 11:07:40 -0700180 int i = 0, scl = 1, ret = 0;
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530181
182 if (bri->prepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300183 bri->prepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530184
Wolfram Sangc4ae05b2018-07-17 11:00:05 +0200185 /*
186 * If we can set SDA, we will always create a STOP to ensure additional
187 * pulses will do no harm. This is achieved by letting SDA follow SCL
188 * half a cycle later. Check the 'incomplete_write_byte' fault injector
Russell Kingcf8ce8b2019-12-15 16:39:05 +0000189 * for details. Note that we must honour tsu:sto, 4us, but lets use 5us
190 * here for simplicity.
Wolfram Sangc4ae05b2018-07-17 11:00:05 +0200191 */
Wolfram Sangf7ff75e2018-07-11 00:27:22 +0200192 bri->set_scl(adap, scl);
Russell Kingcf8ce8b2019-12-15 16:39:05 +0000193 ndelay(RECOVERY_NDELAY);
Wolfram Sang72b08fc2018-01-09 14:58:57 +0100194 if (bri->set_sda)
Wolfram Sangc4ae05b2018-07-17 11:00:05 +0200195 bri->set_sda(adap, scl);
196 ndelay(RECOVERY_NDELAY / 2);
Jan Luebbe8b062602015-07-08 16:35:06 +0200197
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530198 /*
199 * By this time SCL is high, as we need to give 9 falling-rising edges
200 */
201 while (i++ < RECOVERY_CLK_CNT * 2) {
Wolfram Sangf7ff75e2018-07-11 00:27:22 +0200202 if (scl) {
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530203 /* SCL shouldn't be low here */
204 if (!bri->get_scl(adap)) {
205 dev_err(&adap->dev,
206 "SCL is stuck low, exit recovery\n");
207 ret = -EBUSY;
208 break;
209 }
210 }
211
Wolfram Sangf7ff75e2018-07-11 00:27:22 +0200212 scl = !scl;
213 bri->set_scl(adap, scl);
Wolfram Sangc4ae05b2018-07-17 11:00:05 +0200214 /* Creating STOP again, see above */
Russell Kingcf8ce8b2019-12-15 16:39:05 +0000215 if (scl) {
216 /* Honour minimum tsu:sto */
217 ndelay(RECOVERY_NDELAY);
218 } else {
219 /* Honour minimum tf and thd:dat */
220 ndelay(RECOVERY_NDELAY / 2);
221 }
Wolfram Sangabe41182018-07-10 23:42:15 +0200222 if (bri->set_sda)
Wolfram Sangf7ff75e2018-07-11 00:27:22 +0200223 bri->set_sda(adap, scl);
Wolfram Sangabe41182018-07-10 23:42:15 +0200224 ndelay(RECOVERY_NDELAY / 2);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530225
Wolfram Sangf7ff75e2018-07-11 00:27:22 +0200226 if (scl) {
Wolfram Sang7ca5f6b2018-07-11 00:24:22 +0200227 ret = i2c_generic_bus_free(adap);
Wolfram Sang0b710262018-07-10 23:42:17 +0200228 if (ret == 0)
229 break;
230 }
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530231 }
232
Wolfram Sang7ca5f6b2018-07-11 00:24:22 +0200233 /* If we can't check bus status, assume recovery worked */
234 if (ret == -EOPNOTSUPP)
235 ret = 0;
Wolfram Sang2806e6a2018-01-09 14:58:58 +0100236
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530237 if (bri->unprepare_recovery)
Grygorii Strashko2b2190a2015-04-06 15:38:39 +0300238 bri->unprepare_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530239
240 return ret;
241}
Mark Brownc1c21f42015-04-15 19:18:39 +0100242EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530243
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530244int i2c_recover_bus(struct i2c_adapter *adap)
245{
246 if (!adap->bus_recovery_info)
247 return -EOPNOTSUPP;
248
249 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
250 return adap->bus_recovery_info->recover_bus(adap);
251}
Mark Brownc1c21f42015-04-15 19:18:39 +0100252EXPORT_SYMBOL_GPL(i2c_recover_bus);
Viresh Kumar5f9296b2012-02-28 18:26:31 +0530253
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900254static void i2c_init_recovery(struct i2c_adapter *adap)
255{
256 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
257 char *err_str;
258
259 if (!bri)
260 return;
261
262 if (!bri->recover_bus) {
263 err_str = "no recover_bus() found";
264 goto err;
265 }
266
Phil Reid3991c5c2017-11-02 10:40:24 +0800267 if (bri->scl_gpiod && bri->recover_bus == i2c_generic_scl_recovery) {
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900268 bri->get_scl = get_scl_gpio_value;
269 bri->set_scl = set_scl_gpio_value;
Wolfram Sang80921782018-01-09 14:58:56 +0100270 if (bri->sda_gpiod) {
Phil Reid3991c5c2017-11-02 10:40:24 +0800271 bri->get_sda = get_sda_gpio_value;
Wolfram Sang80921782018-01-09 14:58:56 +0100272 /* FIXME: add proper flag instead of '0' once available */
273 if (gpiod_get_direction(bri->sda_gpiod) == 0)
274 bri->set_sda = set_sda_gpio_value;
275 }
Phil Reid3991c5c2017-11-02 10:40:24 +0800276 return;
277 }
278
Phil Reide1eb7d22017-11-02 10:40:30 +0800279 if (bri->recover_bus == i2c_generic_scl_recovery) {
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900280 /* Generic SCL recovery */
281 if (!bri->set_scl || !bri->get_scl) {
282 err_str = "no {get|set}_scl() found";
283 goto err;
284 }
Wolfram Sangffc59c42018-07-10 23:42:16 +0200285 if (!bri->set_sda && !bri->get_sda) {
286 err_str = "either get_sda() or set_sda() needed";
287 goto err;
288 }
Wolfram Sangd3b11d82016-07-09 13:34:59 +0900289 }
290
291 return;
292 err:
293 dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
294 adap->bus_recovery_info = NULL;
295}
296
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200297static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
298{
299 struct i2c_adapter *adap = client->adapter;
300 unsigned int irq;
301
302 if (!adap->host_notify_domain)
303 return -ENXIO;
304
305 if (client->flags & I2C_CLIENT_TEN)
306 return -EINVAL;
307
Charles Keepaxb9bb3fd2018-10-19 09:59:57 +0100308 irq = irq_create_mapping(adap->host_notify_domain, client->addr);
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +0200309
310 return irq > 0 ? irq : -ENXIO;
311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313static int i2c_device_probe(struct device *dev)
314{
Jean Delvare51298d12009-09-18 22:45:45 +0200315 struct i2c_client *client = i2c_verify_client(dev);
316 struct i2c_driver *driver;
Hans Verkuil50c33042008-03-12 14:15:00 +0100317 int status;
David Brownell7b4fbc52007-05-01 23:26:30 +0200318
Jean Delvare51298d12009-09-18 22:45:45 +0200319 if (!client)
320 return 0;
321
Hans de Goeded1d84bb2017-04-05 00:03:34 +0200322 driver = to_i2c_driver(dev->driver);
323
Charles Keepax6e76cb72019-06-27 10:24:11 +0100324 client->irq = client->init_irq;
325
Hans de Goeded1d84bb2017-04-05 00:03:34 +0200326 if (!client->irq && !driver->disable_i2c_core_irq_mapping) {
Mika Westerberg845c8772015-05-06 13:29:08 +0300327 int irq = -ENOENT;
328
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800329 if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
330 dev_dbg(dev, "Using Host Notify IRQ\n");
Jarkko Nikula72bfcee2019-04-30 17:23:22 +0300331 /* Keep adapter active when Host Notify is required */
332 pm_runtime_get_sync(&client->adapter->dev);
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800333 irq = i2c_smbus_host_notify_to_irq(client);
334 } else if (dev->of_node) {
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700335 irq = of_irq_get_byname(dev->of_node, "irq");
336 if (irq == -EINVAL || irq == -ENODATA)
337 irq = of_irq_get(dev->of_node, 0);
338 } else if (ACPI_COMPANION(dev)) {
Charles Keepax16c9db12019-06-27 10:24:09 +0100339 irq = i2c_acpi_get_irq(client);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700340 }
Alain Volmat3c3dd562020-04-30 17:43:21 +0200341 if (irq == -EPROBE_DEFER) {
342 status = irq;
343 goto put_sync_adapter;
344 }
Dmitry Torokhov331c3422017-01-04 20:57:22 -0800345
Geert Uytterhoeven6f34be72014-11-17 12:43:00 +0100346 if (irq < 0)
347 irq = 0;
Laurent Pinchart2fd36c5522014-10-30 15:59:38 +0200348
349 client->irq = irq;
350 }
351
Lee Jonesda10c062016-11-07 12:47:39 +0000352 /*
Javier Martinez Canillasf4b17a12017-08-09 11:21:28 +0200353 * An I2C ID table is not mandatory, if and only if, a suitable OF
354 * or ACPI ID table is supplied for the probing device.
Lee Jonesda10c062016-11-07 12:47:39 +0000355 */
356 if (!driver->id_table &&
Andy Shevchenkoc64ffff2017-07-17 17:13:28 +0300357 !i2c_acpi_match_device(dev->driver->acpi_match_table, client) &&
Alain Volmat3c3dd562020-04-30 17:43:21 +0200358 !i2c_of_match_device(dev->driver->of_match_table, client)) {
359 status = -ENODEV;
360 goto put_sync_adapter;
361 }
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +0200362
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700363 if (client->flags & I2C_CLIENT_WAKE) {
Andy Shevchenko3e998342019-07-26 16:14:21 +0300364 int wakeirq;
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700365
Andy Shevchenko3e998342019-07-26 16:14:21 +0300366 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
Alain Volmat3c3dd562020-04-30 17:43:21 +0200367 if (wakeirq == -EPROBE_DEFER) {
368 status = wakeirq;
369 goto put_sync_adapter;
370 }
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700371
372 device_init_wakeup(&client->dev, true);
373
374 if (wakeirq > 0 && wakeirq != client->irq)
375 status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
376 else if (client->irq > 0)
Grygorii Strashkoc18fba22015-11-12 15:42:26 +0200377 status = dev_pm_set_wake_irq(dev, client->irq);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700378 else
379 status = 0;
380
381 if (status)
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300382 dev_warn(&client->dev, "failed to set up wakeup irq\n");
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700383 }
384
David Brownell7b4fbc52007-05-01 23:26:30 +0200385 dev_dbg(dev, "probe\n");
Jean Delvared2653e92008-04-29 23:11:39 +0200386
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200387 status = of_clk_set_defaults(dev->of_node, false);
388 if (status < 0)
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700389 goto err_clear_wakeup_irq;
Sylwester Nawrocki86be4082014-06-18 17:29:32 +0200390
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200391 status = dev_pm_domain_attach(&client->dev, true);
Ulf Hanssone6a20b62018-04-26 10:53:07 +0200392 if (status)
Kieran Bingham74cedd32015-10-12 21:54:43 +0100393 goto err_clear_wakeup_irq;
394
Lee Jonesb8a1a4c2016-11-07 12:47:41 +0000395 /*
396 * When there are no more users of probe(),
397 * rename probe_new to probe.
398 */
399 if (driver->probe_new)
400 status = driver->probe_new(client);
401 else if (driver->probe)
402 status = driver->probe(client,
403 i2c_match_id(driver->id_table, client));
404 else
405 status = -EINVAL;
406
Kieran Bingham74cedd32015-10-12 21:54:43 +0100407 if (status)
408 goto err_detach_pm_domain;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100409
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700410 return 0;
411
412err_detach_pm_domain:
413 dev_pm_domain_detach(&client->dev, true);
414err_clear_wakeup_irq:
415 dev_pm_clear_wake_irq(&client->dev);
416 device_init_wakeup(&client->dev, false);
Alain Volmat3c3dd562020-04-30 17:43:21 +0200417put_sync_adapter:
418 if (client->flags & I2C_CLIENT_HOST_NOTIFY)
419 pm_runtime_put_sync(&client->adapter->dev);
420
Hans Verkuil50c33042008-03-12 14:15:00 +0100421 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
424static int i2c_device_remove(struct device *dev)
425{
Jean Delvare51298d12009-09-18 22:45:45 +0200426 struct i2c_client *client = i2c_verify_client(dev);
David Brownella1d9e6e2007-05-01 23:26:30 +0200427 struct i2c_driver *driver;
Wolfram Sang72fa8182014-01-21 17:48:34 +0100428 int status = 0;
David Brownella1d9e6e2007-05-01 23:26:30 +0200429
Jean Delvare51298d12009-09-18 22:45:45 +0200430 if (!client || !dev->driver)
David Brownella1d9e6e2007-05-01 23:26:30 +0200431 return 0;
432
433 driver = to_i2c_driver(dev->driver);
434 if (driver->remove) {
435 dev_dbg(dev, "remove\n");
436 status = driver->remove(client);
David Brownella1d9e6e2007-05-01 23:26:30 +0200437 }
Wolfram Sang72fa8182014-01-21 17:48:34 +0100438
Ulf Hanssone09b0d42014-09-19 20:27:39 +0200439 dev_pm_domain_detach(&client->dev, true);
Dmitry Torokhov3fffd122015-08-17 23:52:51 -0700440
441 dev_pm_clear_wake_irq(&client->dev);
442 device_init_wakeup(&client->dev, false);
443
Charles Keepax6e76cb72019-06-27 10:24:11 +0100444 client->irq = 0;
Jarkko Nikula72bfcee2019-04-30 17:23:22 +0300445 if (client->flags & I2C_CLIENT_HOST_NOTIFY)
446 pm_runtime_put(&client->adapter->dev);
Charles Keepax6f108dd2018-10-19 09:59:58 +0100447
David Brownella1d9e6e2007-05-01 23:26:30 +0200448 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
David Brownellf37dd802007-02-13 22:09:00 +0100451static void i2c_device_shutdown(struct device *dev)
452{
Jean Delvare51298d12009-09-18 22:45:45 +0200453 struct i2c_client *client = i2c_verify_client(dev);
David Brownellf37dd802007-02-13 22:09:00 +0100454 struct i2c_driver *driver;
455
Jean Delvare51298d12009-09-18 22:45:45 +0200456 if (!client || !dev->driver)
David Brownellf37dd802007-02-13 22:09:00 +0100457 return;
458 driver = to_i2c_driver(dev->driver);
459 if (driver->shutdown)
Jean Delvare51298d12009-09-18 22:45:45 +0200460 driver->shutdown(client);
David Brownellf37dd802007-02-13 22:09:00 +0100461}
462
David Brownell9c1600e2007-05-01 23:26:31 +0200463static void i2c_client_dev_release(struct device *dev)
464{
465 kfree(to_i2c_client(dev));
466}
467
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100468static ssize_t
Geert Uytterhoeven54a19fd2019-11-13 16:23:06 +0100469name_show(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200470{
Jean Delvare4f8cf822009-09-18 22:45:46 +0200471 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
472 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200473}
Geert Uytterhoeven54a19fd2019-11-13 16:23:06 +0100474static DEVICE_ATTR_RO(name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200475
Zhenwen Xu09b8ce02009-03-28 21:34:46 +0100476static ssize_t
Geert Uytterhoeven54a19fd2019-11-13 16:23:06 +0100477modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
David Brownell7b4fbc52007-05-01 23:26:30 +0200478{
479 struct i2c_client *client = to_i2c_client(dev);
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800480 int len;
481
Javier Martinez Canillasaf503712017-12-03 22:40:50 +0100482 len = of_device_modalias(dev, buf, PAGE_SIZE);
483 if (len != -ENODEV)
484 return len;
485
Zhang Rui8c4ff6d2014-01-14 16:46:37 +0800486 len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
487 if (len != -ENODEV)
488 return len;
489
Jean Delvareeb8a7902008-05-18 20:49:41 +0200490 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
David Brownell7b4fbc52007-05-01 23:26:30 +0200491}
Geert Uytterhoeven54a19fd2019-11-13 16:23:06 +0100492static DEVICE_ATTR_RO(modalias);
Jean Delvare51298d12009-09-18 22:45:45 +0200493
494static struct attribute *i2c_dev_attrs[] = {
495 &dev_attr_name.attr,
David Brownell7b4fbc52007-05-01 23:26:30 +0200496 /* modalias helps coldplug: modprobe $(cat .../modalias) */
Jean Delvare51298d12009-09-18 22:45:45 +0200497 &dev_attr_modalias.attr,
498 NULL
499};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100500ATTRIBUTE_GROUPS(i2c_dev);
sonic zhang54067ee2009-12-14 21:17:30 +0100501
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200502struct bus_type i2c_bus_type = {
David Brownellf37dd802007-02-13 22:09:00 +0100503 .name = "i2c",
504 .match = i2c_device_match,
505 .probe = i2c_device_probe,
506 .remove = i2c_device_remove,
507 .shutdown = i2c_device_shutdown,
Russell Kingb864c7d2006-01-05 14:37:50 +0000508};
Jon Smirle9ca9eb2008-07-14 22:38:35 +0200509EXPORT_SYMBOL_GPL(i2c_bus_type);
Russell Kingb864c7d2006-01-05 14:37:50 +0000510
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800511struct device_type i2c_client_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +0100512 .groups = i2c_dev_groups,
Jean Delvare51298d12009-09-18 22:45:45 +0200513 .uevent = i2c_device_uevent,
514 .release = i2c_client_dev_release,
515};
Dmitry Torokhov00a06c22017-03-04 11:29:34 -0800516EXPORT_SYMBOL_GPL(i2c_client_type);
Jean Delvare51298d12009-09-18 22:45:45 +0200517
David Brownell9b766b82008-01-27 18:14:51 +0100518
519/**
520 * i2c_verify_client - return parameter as i2c_client, or NULL
521 * @dev: device, probably from some driver model iterator
522 *
523 * When traversing the driver model tree, perhaps using driver model
524 * iterators like @device_for_each_child(), you can't assume very much
525 * about the nodes you find. Use this function to avoid oopses caused
526 * by wrongly treating some non-I2C device as an i2c_client.
527 */
528struct i2c_client *i2c_verify_client(struct device *dev)
529{
Jean Delvare51298d12009-09-18 22:45:45 +0200530 return (dev->type == &i2c_client_type)
David Brownell9b766b82008-01-27 18:14:51 +0100531 ? to_i2c_client(dev)
532 : NULL;
533}
534EXPORT_SYMBOL(i2c_verify_client);
535
536
Wolfram Sangda899f52015-05-18 21:09:12 +0200537/* Return a unique address which takes the flags of the client into account */
538static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
539{
540 unsigned short addr = client->addr;
541
542 /* For some client flags, add an arbitrary offset to avoid collisions */
543 if (client->flags & I2C_CLIENT_TEN)
544 addr |= I2C_ADDR_OFFSET_TEN_BIT;
545
546 if (client->flags & I2C_CLIENT_SLAVE)
547 addr |= I2C_ADDR_OFFSET_SLAVE;
548
549 return addr;
550}
551
Jean Delvare3a89db52010-06-03 11:33:52 +0200552/* This is a permissive address validity check, I2C address map constraints
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300553 * are purposely not enforced, except for the general call address. */
Wolfram Sang398432e2018-03-20 21:54:38 +0100554static int i2c_check_addr_validity(unsigned int addr, unsigned short flags)
Jean Delvare3a89db52010-06-03 11:33:52 +0200555{
Wolfram Sangc4019b72015-07-17 12:50:06 +0200556 if (flags & I2C_CLIENT_TEN) {
Jean Delvare3a89db52010-06-03 11:33:52 +0200557 /* 10-bit address, all values are valid */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200558 if (addr > 0x3ff)
Jean Delvare3a89db52010-06-03 11:33:52 +0200559 return -EINVAL;
560 } else {
561 /* 7-bit address, reject the general call address */
Wolfram Sangc4019b72015-07-17 12:50:06 +0200562 if (addr == 0x00 || addr > 0x7f)
Jean Delvare3a89db52010-06-03 11:33:52 +0200563 return -EINVAL;
564 }
565 return 0;
566}
567
Jean Delvare656b8762010-06-03 11:33:53 +0200568/* And this is a strict address validity check, used when probing. If a
569 * device uses a reserved address, then it shouldn't be probed. 7-bit
570 * addressing is assumed, 10-bit address devices are rare and should be
571 * explicitly enumerated. */
Wolfram Sange4991ec2017-05-23 11:14:17 +0200572int i2c_check_7bit_addr_validity_strict(unsigned short addr)
Jean Delvare656b8762010-06-03 11:33:53 +0200573{
574 /*
575 * Reserved addresses per I2C specification:
576 * 0x00 General call address / START byte
577 * 0x01 CBUS address
578 * 0x02 Reserved for different bus format
579 * 0x03 Reserved for future purposes
580 * 0x04-0x07 Hs-mode master code
581 * 0x78-0x7b 10-bit slave addressing
582 * 0x7c-0x7f Reserved for future purposes
583 */
584 if (addr < 0x08 || addr > 0x77)
585 return -EINVAL;
586 return 0;
587}
588
Jean Delvare3b5f7942010-06-03 11:33:55 +0200589static int __i2c_check_addr_busy(struct device *dev, void *addrp)
590{
591 struct i2c_client *client = i2c_verify_client(dev);
592 int addr = *(int *)addrp;
593
Wolfram Sang9bccc702015-07-17 14:48:56 +0200594 if (client && i2c_encode_flags_to_addr(client) == addr)
Jean Delvare3b5f7942010-06-03 11:33:55 +0200595 return -EBUSY;
596 return 0;
597}
598
Michael Lawnick08263742010-08-11 18:21:02 +0200599/* walk up mux tree */
600static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
601{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200602 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200603 int result;
604
605 result = device_for_each_child(&adapter->dev, &addr,
606 __i2c_check_addr_busy);
607
Jean Delvare97cc4d42010-10-24 18:16:57 +0200608 if (!result && parent)
609 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200610
611 return result;
612}
613
614/* recurse down mux tree */
615static int i2c_check_mux_children(struct device *dev, void *addrp)
616{
617 int result;
618
619 if (dev->type == &i2c_adapter_type)
620 result = device_for_each_child(dev, addrp,
621 i2c_check_mux_children);
622 else
623 result = __i2c_check_addr_busy(dev, addrp);
624
625 return result;
626}
627
Jean Delvare3b5f7942010-06-03 11:33:55 +0200628static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
629{
Jean Delvare97cc4d42010-10-24 18:16:57 +0200630 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
Michael Lawnick08263742010-08-11 18:21:02 +0200631 int result = 0;
632
Jean Delvare97cc4d42010-10-24 18:16:57 +0200633 if (parent)
634 result = i2c_check_mux_parents(parent, addr);
Michael Lawnick08263742010-08-11 18:21:02 +0200635
636 if (!result)
637 result = device_for_each_child(&adapter->dev, &addr,
638 i2c_check_mux_children);
639
640 return result;
Jean Delvare3b5f7942010-06-03 11:33:55 +0200641}
642
David Brownell9c1600e2007-05-01 23:26:31 +0200643/**
Peter Rosin8320f492016-05-04 22:15:27 +0200644 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200645 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200646 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
647 * locks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200648 */
Peter Rosin8320f492016-05-04 22:15:27 +0200649static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
650 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200651{
Peter Rosin7b94ea52018-07-20 10:39:14 +0200652 rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter));
Jean Delvarefe61e072010-08-11 18:20:58 +0200653}
Jean Delvarefe61e072010-08-11 18:20:58 +0200654
655/**
Peter Rosin8320f492016-05-04 22:15:27 +0200656 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200657 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200658 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
659 * trylocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200660 */
Peter Rosin8320f492016-05-04 22:15:27 +0200661static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
662 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200663{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200664 return rt_mutex_trylock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200665}
666
667/**
Peter Rosin8320f492016-05-04 22:15:27 +0200668 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
Jean Delvarefe61e072010-08-11 18:20:58 +0200669 * @adapter: Target I2C bus segment
Peter Rosin8320f492016-05-04 22:15:27 +0200670 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
671 * unlocks only this branch in the adapter tree
Jean Delvarefe61e072010-08-11 18:20:58 +0200672 */
Peter Rosin8320f492016-05-04 22:15:27 +0200673static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
674 unsigned int flags)
Jean Delvarefe61e072010-08-11 18:20:58 +0200675{
Peter Rosinfa96f0c2016-05-04 22:15:28 +0200676 rt_mutex_unlock(&adapter->bus_lock);
Jean Delvarefe61e072010-08-11 18:20:58 +0200677}
Jean Delvarefe61e072010-08-11 18:20:58 +0200678
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200679static void i2c_dev_set_name(struct i2c_adapter *adap,
Hans de Goede728fe6c2017-10-11 11:41:19 +0200680 struct i2c_client *client,
681 struct i2c_board_info const *info)
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200682{
683 struct acpi_device *adev = ACPI_COMPANION(&client->dev);
684
Hans de Goede728fe6c2017-10-11 11:41:19 +0200685 if (info && info->dev_name) {
686 dev_set_name(&client->dev, "i2c-%s", info->dev_name);
687 return;
688 }
689
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200690 if (adev) {
691 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
692 return;
693 }
694
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200695 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
Wolfram Sangda899f52015-05-18 21:09:12 +0200696 i2c_encode_flags_to_addr(client));
Jarkko Nikula70762ab2013-11-14 14:03:52 +0200697}
698
Charles Keepax1d7534b2019-06-27 10:24:06 +0100699int i2c_dev_irq_from_resources(const struct resource *resources,
700 unsigned int num_resources)
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800701{
702 struct irq_data *irqd;
703 int i;
704
705 for (i = 0; i < num_resources; i++) {
706 const struct resource *r = &resources[i];
707
708 if (resource_type(r) != IORESOURCE_IRQ)
709 continue;
710
711 if (r->flags & IORESOURCE_BITS) {
712 irqd = irq_get_irq_data(r->start);
713 if (!irqd)
714 break;
715
716 irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
717 }
718
719 return r->start;
720 }
721
722 return 0;
723}
724
Jean Delvarefe61e072010-08-11 18:20:58 +0200725/**
Heiner Kallweit7159dbda2019-05-16 23:13:08 +0200726 * i2c_new_client_device - instantiate an i2c device
David Brownell9c1600e2007-05-01 23:26:31 +0200727 * @adap: the adapter managing the device
728 * @info: describes one I2C device; bus_num is ignored
David Brownelld64f73b2007-07-12 14:12:28 +0200729 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200730 *
Jean Delvaref8a227e2009-06-19 16:58:18 +0200731 * Create an i2c device. Binding is handled through driver model
732 * probe()/remove() methods. A driver may be bound to this device when we
733 * return from this function, or any later moment (e.g. maybe hotplugging will
734 * load the driver module). This call is not appropriate for use by mainboard
735 * initialization logic, which usually runs during an arch_initcall() long
736 * before any i2c_adapter could exist.
David Brownell9c1600e2007-05-01 23:26:31 +0200737 *
738 * This returns the new i2c client, which may be saved for later use with
Heiner Kallweit7159dbda2019-05-16 23:13:08 +0200739 * i2c_unregister_device(); or an ERR_PTR to describe the error.
David Brownell9c1600e2007-05-01 23:26:31 +0200740 */
Wolfram Sang550113d2019-06-24 19:04:02 +0200741struct i2c_client *
Heiner Kallweit7159dbda2019-05-16 23:13:08 +0200742i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
David Brownell9c1600e2007-05-01 23:26:31 +0200743{
744 struct i2c_client *client;
745 int status;
746
747 client = kzalloc(sizeof *client, GFP_KERNEL);
748 if (!client)
Heiner Kallweit7159dbda2019-05-16 23:13:08 +0200749 return ERR_PTR(-ENOMEM);
David Brownell9c1600e2007-05-01 23:26:31 +0200750
751 client->adapter = adap;
752
753 client->dev.platform_data = info->platform_data;
Marc Pignatee354252008-08-28 08:33:22 +0200754 client->flags = info->flags;
David Brownell9c1600e2007-05-01 23:26:31 +0200755 client->addr = info->addr;
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800756
Jim Broadus93b66042019-02-19 11:30:27 -0800757 client->init_irq = info->irq;
758 if (!client->init_irq)
759 client->init_irq = i2c_dev_irq_from_resources(info->resources,
Dmitry Torokhov4124c4e2017-03-01 11:45:51 -0800760 info->num_resources);
David Brownell9c1600e2007-05-01 23:26:31 +0200761
David Brownell9c1600e2007-05-01 23:26:31 +0200762 strlcpy(client->name, info->type, sizeof(client->name));
763
Wolfram Sangc4019b72015-07-17 12:50:06 +0200764 status = i2c_check_addr_validity(client->addr, client->flags);
Jean Delvare3a89db52010-06-03 11:33:52 +0200765 if (status) {
766 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
767 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
768 goto out_err_silent;
769 }
770
Jean Delvaref8a227e2009-06-19 16:58:18 +0200771 /* Check for address business */
Wolfram Sang9bccc702015-07-17 14:48:56 +0200772 status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
Jean Delvaref8a227e2009-06-19 16:58:18 +0200773 if (status)
774 goto out_err;
775
776 client->dev.parent = &client->adapter->dev;
777 client->dev.bus = &i2c_bus_type;
Jean Delvare51298d12009-09-18 22:45:45 +0200778 client->dev.type = &i2c_client_type;
Boris Brezillon04782262018-03-25 14:49:02 +0200779 client->dev.of_node = of_node_get(info->of_node);
Rafael J. Wysockice793482015-03-16 23:49:03 +0100780 client->dev.fwnode = info->fwnode;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200781
Hans de Goede728fe6c2017-10-11 11:41:19 +0200782 i2c_dev_set_name(adap, client, info);
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800783
784 if (info->properties) {
785 status = device_add_properties(&client->dev, info->properties);
786 if (status) {
787 dev_err(&adap->dev,
788 "Failed to add properties to client %s: %d\n",
789 client->name, status);
Boris Brezillon04782262018-03-25 14:49:02 +0200790 goto out_err_put_of_node;
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800791 }
792 }
793
Jean Delvaref8a227e2009-06-19 16:58:18 +0200794 status = device_register(&client->dev);
795 if (status)
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800796 goto out_free_props;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200797
Jean Delvaref8a227e2009-06-19 16:58:18 +0200798 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
799 client->name, dev_name(&client->dev));
800
David Brownell9c1600e2007-05-01 23:26:31 +0200801 return client;
Jean Delvaref8a227e2009-06-19 16:58:18 +0200802
Dmitry Torokhovd3e1b612017-02-02 17:41:28 -0800803out_free_props:
804 if (info->properties)
805 device_remove_properties(&client->dev);
Boris Brezillon04782262018-03-25 14:49:02 +0200806out_err_put_of_node:
807 of_node_put(info->of_node);
Jean Delvaref8a227e2009-06-19 16:58:18 +0200808out_err:
Andy Shevchenkob93d3d32016-08-25 17:42:10 +0300809 dev_err(&adap->dev,
810 "Failed to register i2c client %s at 0x%02x (%d)\n",
811 client->name, client->addr, status);
Jean Delvare3a89db52010-06-03 11:33:52 +0200812out_err_silent:
Jean Delvaref8a227e2009-06-19 16:58:18 +0200813 kfree(client);
Heiner Kallweit7159dbda2019-05-16 23:13:08 +0200814 return ERR_PTR(status);
815}
816EXPORT_SYMBOL_GPL(i2c_new_client_device);
817
818/**
819 * i2c_new_device - instantiate an i2c device
820 * @adap: the adapter managing the device
821 * @info: describes one I2C device; bus_num is ignored
822 * Context: can sleep
823 *
824 * This deprecated function has the same functionality as
825 * @i2c_new_client_device, it just returns NULL instead of an ERR_PTR in case of
826 * an error for compatibility with current I2C API. It will be removed once all
827 * users are converted.
828 *
829 * This returns the new i2c client, which may be saved for later use with
830 * i2c_unregister_device(); or NULL to indicate an error.
831 */
832struct i2c_client *
833i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
834{
835 struct i2c_client *ret;
836
837 ret = i2c_new_client_device(adap, info);
838 return IS_ERR(ret) ? NULL : ret;
David Brownell9c1600e2007-05-01 23:26:31 +0200839}
840EXPORT_SYMBOL_GPL(i2c_new_device);
841
842
843/**
Wolfram Sang87e07432020-01-07 18:47:43 +0100844 * i2c_unregister_device - reverse effect of i2c_new_*_device()
845 * @client: value returned from i2c_new_*_device()
David Brownelld64f73b2007-07-12 14:12:28 +0200846 * Context: can sleep
David Brownell9c1600e2007-05-01 23:26:31 +0200847 */
848void i2c_unregister_device(struct i2c_client *client)
David Brownella1d9e6e2007-05-01 23:26:30 +0200849{
Wolfram Sang689f5352019-08-19 22:48:25 +0200850 if (IS_ERR_OR_NULL(client))
Andy Shevchenko7b43dd12017-10-31 16:21:35 +0200851 return;
Lixin Wange0638fa2017-11-27 15:06:55 +0800852
853 if (client->dev.of_node) {
Pantelis Antoniou4f001fd2015-01-24 09:16:29 +0200854 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
Lixin Wange0638fa2017-11-27 15:06:55 +0800855 of_node_put(client->dev.of_node);
856 }
857
Octavian Purdila525e6fa2016-07-08 19:13:10 +0300858 if (ACPI_COMPANION(&client->dev))
859 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
David Brownella1d9e6e2007-05-01 23:26:30 +0200860 device_unregister(&client->dev);
861}
David Brownell9c1600e2007-05-01 23:26:31 +0200862EXPORT_SYMBOL_GPL(i2c_unregister_device);
David Brownella1d9e6e2007-05-01 23:26:30 +0200863
864
Jean Delvare60b129d2008-05-11 20:37:06 +0200865static const struct i2c_device_id dummy_id[] = {
866 { "dummy", 0 },
867 { },
868};
869
Jean Delvared2653e92008-04-29 23:11:39 +0200870static int dummy_probe(struct i2c_client *client,
871 const struct i2c_device_id *id)
872{
873 return 0;
874}
875
876static int dummy_remove(struct i2c_client *client)
David Brownelle9f13732008-01-27 18:14:52 +0100877{
878 return 0;
879}
880
881static struct i2c_driver dummy_driver = {
882 .driver.name = "dummy",
Jean Delvared2653e92008-04-29 23:11:39 +0200883 .probe = dummy_probe,
884 .remove = dummy_remove,
Jean Delvare60b129d2008-05-11 20:37:06 +0200885 .id_table = dummy_id,
David Brownelle9f13732008-01-27 18:14:52 +0100886};
887
888/**
Heiner Kallweit7159dbda2019-05-16 23:13:08 +0200889 * i2c_new_dummy_device - return a new i2c device bound to a dummy driver
David Brownelle9f13732008-01-27 18:14:52 +0100890 * @adapter: the adapter managing the device
891 * @address: seven bit address to be used
David Brownelle9f13732008-01-27 18:14:52 +0100892 * Context: can sleep
893 *
894 * This returns an I2C client bound to the "dummy" driver, intended for use
895 * with devices that consume multiple addresses. Examples of such chips
896 * include various EEPROMS (like 24c04 and 24c08 models).
897 *
898 * These dummy devices have two main uses. First, most I2C and SMBus calls
899 * except i2c_transfer() need a client handle; the dummy will be that handle.
900 * And second, this prevents the specified address from being bound to a
901 * different driver.
902 *
903 * This returns the new i2c client, which should be saved for later use with
Heiner Kallweit7159dbda2019-05-16 23:13:08 +0200904 * i2c_unregister_device(); or an ERR_PTR to describe the error.
David Brownelle9f13732008-01-27 18:14:52 +0100905 */
Wolfram Sang550113d2019-06-24 19:04:02 +0200906struct i2c_client *i2c_new_dummy_device(struct i2c_adapter *adapter, u16 address)
David Brownelle9f13732008-01-27 18:14:52 +0100907{
908 struct i2c_board_info info = {
Jean Delvare60b129d2008-05-11 20:37:06 +0200909 I2C_BOARD_INFO("dummy", address),
David Brownelle9f13732008-01-27 18:14:52 +0100910 };
911
Heiner Kallweit7159dbda2019-05-16 23:13:08 +0200912 return i2c_new_client_device(adapter, &info);
913}
914EXPORT_SYMBOL_GPL(i2c_new_dummy_device);
915
Heiner Kallweitb8f5fe32019-05-16 23:13:09 +0200916struct i2c_dummy_devres {
917 struct i2c_client *client;
918};
919
920static void devm_i2c_release_dummy(struct device *dev, void *res)
921{
922 struct i2c_dummy_devres *this = res;
923
924 i2c_unregister_device(this->client);
925}
926
927/**
928 * devm_i2c_new_dummy_device - return a new i2c device bound to a dummy driver
929 * @dev: device the managed resource is bound to
930 * @adapter: the adapter managing the device
931 * @address: seven bit address to be used
932 * Context: can sleep
933 *
934 * This is the device-managed version of @i2c_new_dummy_device. It returns the
935 * new i2c client or an ERR_PTR in case of an error.
936 */
937struct i2c_client *devm_i2c_new_dummy_device(struct device *dev,
938 struct i2c_adapter *adapter,
939 u16 address)
940{
941 struct i2c_dummy_devres *dr;
942 struct i2c_client *client;
943
944 dr = devres_alloc(devm_i2c_release_dummy, sizeof(*dr), GFP_KERNEL);
945 if (!dr)
946 return ERR_PTR(-ENOMEM);
947
948 client = i2c_new_dummy_device(adapter, address);
949 if (IS_ERR(client)) {
950 devres_free(dr);
951 } else {
952 dr->client = client;
953 devres_add(dev, dr);
954 }
955
956 return client;
957}
958EXPORT_SYMBOL_GPL(devm_i2c_new_dummy_device);
959
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +0100960/**
Wolfram Sangaf805592019-08-09 17:40:47 +0200961 * i2c_new_ancillary_device - Helper to get the instantiated secondary address
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +0100962 * and create the associated device
963 * @client: Handle to the primary client
964 * @name: Handle to specify which secondary address to get
965 * @default_addr: Used as a fallback if no secondary address was specified
966 * Context: can sleep
967 *
968 * I2C clients can be composed of multiple I2C slaves bound together in a single
969 * component. The I2C client driver then binds to the master I2C slave and needs
970 * to create I2C dummy clients to communicate with all the other slaves.
971 *
972 * This function creates and returns an I2C dummy client whose I2C address is
973 * retrieved from the platform firmware based on the given slave name. If no
974 * address is specified by the firmware default_addr is used.
975 *
976 * On DT-based platforms the address is retrieved from the "reg" property entry
977 * cell whose "reg-names" value matches the slave name.
978 *
979 * This returns the new i2c client, which should be saved for later use with
Wolfram Sangaf805592019-08-09 17:40:47 +0200980 * i2c_unregister_device(); or an ERR_PTR to describe the error.
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +0100981 */
Wolfram Sangaf805592019-08-09 17:40:47 +0200982struct i2c_client *i2c_new_ancillary_device(struct i2c_client *client,
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +0100983 const char *name,
984 u16 default_addr)
985{
986 struct device_node *np = client->dev.of_node;
987 u32 addr = default_addr;
988 int i;
989
990 if (np) {
991 i = of_property_match_string(np, "reg-names", name);
992 if (i >= 0)
993 of_property_read_u32_index(np, "reg", i, &addr);
994 }
995
996 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
Wolfram Sangaf805592019-08-09 17:40:47 +0200997 return i2c_new_dummy_device(client->adapter, addr);
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +0100998}
Wolfram Sangaf805592019-08-09 17:40:47 +0200999EXPORT_SYMBOL_GPL(i2c_new_ancillary_device);
Jean-Michel Hautbois0f614d82016-01-31 16:33:00 +01001000
David Brownellf37dd802007-02-13 22:09:00 +01001001/* ------------------------------------------------------------------------- */
1002
David Brownell16ffadf2007-05-01 23:26:28 +02001003/* I2C bus adapters -- one roots each I2C or SMBUS segment */
1004
Adrian Bunk83eaaed2007-10-13 23:56:30 +02001005static void i2c_adapter_dev_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
David Brownellef2c83212007-05-01 23:26:28 +02001007 struct i2c_adapter *adap = to_i2c_adapter(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 complete(&adap->dev_released);
1009}
1010
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001011unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
Jean Delvare390946b2012-09-10 10:14:02 +02001012{
1013 unsigned int depth = 0;
1014
1015 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
1016 depth++;
1017
Bartosz Golaszewski2771dc32016-09-16 18:02:44 +02001018 WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
1019 "adapter depth exceeds lockdep subclass limit\n");
1020
Jean Delvare390946b2012-09-10 10:14:02 +02001021 return depth;
1022}
Bartosz Golaszewski8dd1fe12016-09-16 18:02:42 +02001023EXPORT_SYMBOL_GPL(i2c_adapter_depth);
Jean Delvare390946b2012-09-10 10:14:02 +02001024
1025/*
Jean Delvare99cd8e22009-06-19 16:58:20 +02001026 * Let users instantiate I2C devices through sysfs. This can be used when
1027 * platform initialization code doesn't contain the proper data for
1028 * whatever reason. Also useful for drivers that do device detection and
1029 * detection fails, either because the device uses an unexpected address,
1030 * or this is a compatible device with different ID register values.
1031 *
1032 * Parameter checking may look overzealous, but we really don't want
1033 * the user to provide incorrect parameters.
1034 */
1035static ssize_t
Geert Uytterhoeven54a19fd2019-11-13 16:23:06 +01001036new_device_store(struct device *dev, struct device_attribute *attr,
1037 const char *buf, size_t count)
Jean Delvare99cd8e22009-06-19 16:58:20 +02001038{
1039 struct i2c_adapter *adap = to_i2c_adapter(dev);
1040 struct i2c_board_info info;
1041 struct i2c_client *client;
1042 char *blank, end;
1043 int res;
1044
Jean Delvare99cd8e22009-06-19 16:58:20 +02001045 memset(&info, 0, sizeof(struct i2c_board_info));
1046
1047 blank = strchr(buf, ' ');
1048 if (!blank) {
1049 dev_err(dev, "%s: Missing parameters\n", "new_device");
1050 return -EINVAL;
1051 }
1052 if (blank - buf > I2C_NAME_SIZE - 1) {
1053 dev_err(dev, "%s: Invalid device name\n", "new_device");
1054 return -EINVAL;
1055 }
1056 memcpy(info.type, buf, blank - buf);
1057
1058 /* Parse remaining parameters, reject extra parameters */
1059 res = sscanf(++blank, "%hi%c", &info.addr, &end);
1060 if (res < 1) {
1061 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
1062 return -EINVAL;
1063 }
1064 if (res > 1 && end != '\n') {
1065 dev_err(dev, "%s: Extra parameters\n", "new_device");
1066 return -EINVAL;
1067 }
1068
Wolfram Sangcfa03272015-07-27 14:03:38 +02001069 if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
1070 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
1071 info.flags |= I2C_CLIENT_TEN;
1072 }
1073
1074 if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
1075 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
1076 info.flags |= I2C_CLIENT_SLAVE;
1077 }
1078
Heiner Kallweit7159dbda2019-05-16 23:13:08 +02001079 client = i2c_new_client_device(adap, &info);
1080 if (IS_ERR(client))
1081 return PTR_ERR(client);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001082
1083 /* Keep track of the added device */
Jean Delvaredafc50d2010-08-11 18:21:01 +02001084 mutex_lock(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001085 list_add_tail(&client->detected, &adap->userspace_clients);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001086 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001087 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1088 info.type, info.addr);
1089
1090 return count;
1091}
Geert Uytterhoeven54a19fd2019-11-13 16:23:06 +01001092static DEVICE_ATTR_WO(new_device);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001093
1094/*
1095 * And of course let the users delete the devices they instantiated, if
1096 * they got it wrong. This interface can only be used to delete devices
1097 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1098 * don't delete devices to which some kernel code still has references.
1099 *
1100 * Parameter checking may look overzealous, but we really don't want
1101 * the user to delete the wrong device.
1102 */
1103static ssize_t
Geert Uytterhoeven54a19fd2019-11-13 16:23:06 +01001104delete_device_store(struct device *dev, struct device_attribute *attr,
1105 const char *buf, size_t count)
Jean Delvare99cd8e22009-06-19 16:58:20 +02001106{
1107 struct i2c_adapter *adap = to_i2c_adapter(dev);
1108 struct i2c_client *client, *next;
1109 unsigned short addr;
1110 char end;
1111 int res;
1112
1113 /* Parse parameters, reject extra parameters */
1114 res = sscanf(buf, "%hi%c", &addr, &end);
1115 if (res < 1) {
1116 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
1117 return -EINVAL;
1118 }
1119 if (res > 1 && end != '\n') {
1120 dev_err(dev, "%s: Extra parameters\n", "delete_device");
1121 return -EINVAL;
1122 }
1123
1124 /* Make sure the device was added through sysfs */
1125 res = -ENOENT;
Jean Delvare390946b2012-09-10 10:14:02 +02001126 mutex_lock_nested(&adap->userspace_clients_lock,
1127 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001128 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1129 detected) {
Wolfram Sangcfa03272015-07-27 14:03:38 +02001130 if (i2c_encode_flags_to_addr(client) == addr) {
Jean Delvare99cd8e22009-06-19 16:58:20 +02001131 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
1132 "delete_device", client->name, client->addr);
1133
1134 list_del(&client->detected);
1135 i2c_unregister_device(client);
1136 res = count;
1137 break;
1138 }
1139 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001140 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvare99cd8e22009-06-19 16:58:20 +02001141
1142 if (res < 0)
1143 dev_err(dev, "%s: Can't find device in list\n",
1144 "delete_device");
1145 return res;
1146}
Alexander Sverdline9b526f2013-05-17 14:56:35 +02001147static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
Geert Uytterhoeven54a19fd2019-11-13 16:23:06 +01001148 delete_device_store);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001149
1150static struct attribute *i2c_adapter_attrs[] = {
1151 &dev_attr_name.attr,
1152 &dev_attr_new_device.attr,
1153 &dev_attr_delete_device.attr,
1154 NULL
David Brownell16ffadf2007-05-01 23:26:28 +02001155};
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001156ATTRIBUTE_GROUPS(i2c_adapter);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001157
Michael Lawnick08263742010-08-11 18:21:02 +02001158struct device_type i2c_adapter_type = {
Wolfram Sanga5eb71b2015-01-19 20:12:24 +01001159 .groups = i2c_adapter_groups,
Jean Delvare4f8cf822009-09-18 22:45:46 +02001160 .release = i2c_adapter_dev_release,
David Brownell16ffadf2007-05-01 23:26:28 +02001161};
Michael Lawnick08263742010-08-11 18:21:02 +02001162EXPORT_SYMBOL_GPL(i2c_adapter_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Stephen Warren643dd092012-04-17 12:43:33 -06001164/**
1165 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1166 * @dev: device, probably from some driver model iterator
1167 *
1168 * When traversing the driver model tree, perhaps using driver model
1169 * iterators like @device_for_each_child(), you can't assume very much
1170 * about the nodes you find. Use this function to avoid oopses caused
1171 * by wrongly treating some non-I2C device as an i2c_adapter.
1172 */
1173struct i2c_adapter *i2c_verify_adapter(struct device *dev)
1174{
1175 return (dev->type == &i2c_adapter_type)
1176 ? to_i2c_adapter(dev)
1177 : NULL;
1178}
1179EXPORT_SYMBOL(i2c_verify_adapter);
1180
Jean Delvare2bb50952009-09-18 22:45:46 +02001181#ifdef CONFIG_I2C_COMPAT
1182static struct class_compat *i2c_adapter_compat_class;
1183#endif
1184
David Brownell9c1600e2007-05-01 23:26:31 +02001185static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
1186{
1187 struct i2c_devinfo *devinfo;
1188
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001189 down_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001190 list_for_each_entry(devinfo, &__i2c_board_list, list) {
Wolfram Sang87e07432020-01-07 18:47:43 +01001191 if (devinfo->busnum == adapter->nr &&
1192 IS_ERR(i2c_new_client_device(adapter, &devinfo->board_info)))
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001193 dev_err(&adapter->dev,
1194 "Can't create device at 0x%02x\n",
David Brownell9c1600e2007-05-01 23:26:31 +02001195 devinfo->board_info.addr);
1196 }
Rodolfo Giomettif18c41d2009-06-19 16:58:20 +02001197 up_read(&__i2c_board_lock);
David Brownell9c1600e2007-05-01 23:26:31 +02001198}
1199
Jean Delvare69b00892009-12-06 17:06:27 +01001200static int i2c_do_add_adapter(struct i2c_driver *driver,
1201 struct i2c_adapter *adap)
Jean Delvare026526f2008-01-27 18:14:49 +01001202{
Jean Delvare4735c982008-07-14 22:38:36 +02001203 /* Detect supported devices on that bus, and instantiate them */
1204 i2c_detect(adap, driver);
1205
Jean Delvare026526f2008-01-27 18:14:49 +01001206 return 0;
1207}
1208
Jean Delvare69b00892009-12-06 17:06:27 +01001209static int __process_new_adapter(struct device_driver *d, void *data)
1210{
1211 return i2c_do_add_adapter(to_i2c_driver(d), data);
1212}
1213
Peter Rosind1ed7982016-08-25 23:07:01 +02001214static const struct i2c_lock_operations i2c_adapter_lock_ops = {
1215 .lock_bus = i2c_adapter_lock_bus,
1216 .trylock_bus = i2c_adapter_trylock_bus,
1217 .unlock_bus = i2c_adapter_unlock_bus,
1218};
1219
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001220static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap)
1221{
1222 struct irq_domain *domain = adap->host_notify_domain;
1223 irq_hw_number_t hwirq;
1224
1225 if (!domain)
1226 return;
1227
1228 for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++)
1229 irq_dispose_mapping(irq_find_mapping(domain, hwirq));
1230
1231 irq_domain_remove(domain);
1232 adap->host_notify_domain = NULL;
1233}
1234
1235static int i2c_host_notify_irq_map(struct irq_domain *h,
1236 unsigned int virq,
1237 irq_hw_number_t hw_irq_num)
1238{
1239 irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
1240
1241 return 0;
1242}
1243
1244static const struct irq_domain_ops i2c_host_notify_irq_ops = {
1245 .map = i2c_host_notify_irq_map,
1246};
1247
1248static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap)
1249{
1250 struct irq_domain *domain;
1251
1252 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
1253 return 0;
1254
1255 domain = irq_domain_create_linear(adap->dev.fwnode,
1256 I2C_ADDR_7BITS_COUNT,
1257 &i2c_host_notify_irq_ops, adap);
1258 if (!domain)
1259 return -ENOMEM;
1260
1261 adap->host_notify_domain = domain;
1262
1263 return 0;
1264}
1265
1266/**
1267 * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
1268 * I2C client.
1269 * @adap: the adapter
1270 * @addr: the I2C address of the notifying device
1271 * Context: can't sleep
1272 *
1273 * Helper function to be called from an I2C bus driver's interrupt
1274 * handler. It will schedule the Host Notify IRQ.
1275 */
1276int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
1277{
1278 int irq;
1279
1280 if (!adap)
1281 return -EINVAL;
1282
1283 irq = irq_find_mapping(adap->host_notify_domain, addr);
1284 if (irq <= 0)
1285 return -ENXIO;
1286
1287 generic_handle_irq(irq);
1288
1289 return 0;
1290}
1291EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
1292
David Brownell6e13e642007-05-01 23:26:31 +02001293static int i2c_register_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001295 int res = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
David Brownell1d0b19c2008-10-14 17:30:05 +02001297 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301298 if (WARN_ON(!is_registered)) {
Jean Delvare35fc37f2009-06-19 16:58:19 +02001299 res = -EAGAIN;
1300 goto out_list;
1301 }
David Brownell1d0b19c2008-10-14 17:30:05 +02001302
Jean Delvare2236baa2010-11-15 22:40:38 +01001303 /* Sanity checks */
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001304 if (WARN(!adap->name[0], "i2c adapter has no name"))
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001305 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001306
1307 if (!adap->algo) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001308 pr_err("adapter '%s': no algo supplied!\n", adap->name);
Wolfram Sangce0dffa2016-07-09 13:34:58 +09001309 goto out_list;
Jean Delvare2236baa2010-11-15 22:40:38 +01001310 }
1311
Peter Rosind1ed7982016-08-25 23:07:01 +02001312 if (!adap->lock_ops)
1313 adap->lock_ops = &i2c_adapter_lock_ops;
Peter Rosin8320f492016-05-04 22:15:27 +02001314
Wolfram Sang9ac6cb52018-12-19 17:48:17 +01001315 adap->locked_flags = 0;
Mika Kuoppala194684e2009-12-06 17:06:22 +01001316 rt_mutex_init(&adap->bus_lock);
Peter Rosin6ef91fc2016-05-04 22:15:29 +02001317 rt_mutex_init(&adap->mux_lock);
Jean Delvaredafc50d2010-08-11 18:21:01 +02001318 mutex_init(&adap->userspace_clients_lock);
Jean Delvare6629dcf2010-05-04 11:09:28 +02001319 INIT_LIST_HEAD(&adap->userspace_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Jean Delvare8fcfef62009-03-28 21:34:43 +01001321 /* Set default timeout to 1 second if not already set */
1322 if (adap->timeout == 0)
1323 adap->timeout = HZ;
1324
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001325 /* register soft irqs for Host Notify */
1326 res = i2c_setup_host_notify_irq_domain(adap);
1327 if (res) {
1328 pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n",
1329 adap->name, res);
1330 goto out_list;
1331 }
1332
Kay Sievers27d9c182009-01-07 14:29:16 +01001333 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
Jean Delvare4f8cf822009-09-18 22:45:46 +02001334 adap->dev.bus = &i2c_bus_type;
1335 adap->dev.type = &i2c_adapter_type;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001336 res = device_register(&adap->dev);
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001337 if (res) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001338 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001339 goto out_list;
Wolfram Sang8ddfe412016-07-09 13:35:00 +09001340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Phil Reidf8756c62017-08-24 17:31:04 +08001342 res = of_i2c_setup_smbus_alert(adap);
1343 if (res)
1344 goto out_reg;
1345
Jean Delvareb6d7b3d2005-07-31 19:02:53 +02001346 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1347
Charles Keepax6ada5c12015-04-16 13:05:19 +01001348 pm_runtime_no_callbacks(&adap->dev);
Linus Walleij04f59142016-04-12 09:57:35 +02001349 pm_suspend_ignore_children(&adap->dev, true);
Wolfram Sang9f924162015-12-23 18:19:28 +01001350 pm_runtime_enable(&adap->dev);
Charles Keepax6ada5c12015-04-16 13:05:19 +01001351
Jean Delvare2bb50952009-09-18 22:45:46 +02001352#ifdef CONFIG_I2C_COMPAT
1353 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1354 adap->dev.parent);
1355 if (res)
1356 dev_warn(&adap->dev,
1357 "Failed to create compatibility class link\n");
1358#endif
1359
Wolfram Sangd3b11d82016-07-09 13:34:59 +09001360 i2c_init_recovery(adap);
Viresh Kumar5f9296b2012-02-28 18:26:31 +05301361
Jean Delvare729d6dd2009-06-19 16:58:18 +02001362 /* create pre-declared device nodes */
Wolfram Sang687b81d2013-07-11 12:56:15 +01001363 of_i2c_register_devices(adap);
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001364 i2c_acpi_register_devices(adap);
1365 i2c_acpi_install_space_handler(adap);
Wolfram Sang687b81d2013-07-11 12:56:15 +01001366
David Brownell6e13e642007-05-01 23:26:31 +02001367 if (adap->nr < __i2c_first_dynamic_bus_num)
1368 i2c_scan_static_board_info(adap);
1369
Jean Delvare4735c982008-07-14 22:38:36 +02001370 /* Notify drivers */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001371 mutex_lock(&core_lock);
Jean Delvared6703282010-08-11 18:20:59 +02001372 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
Jean Delvarecaada322008-01-27 18:14:49 +01001373 mutex_unlock(&core_lock);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001374
1375 return 0;
Jean Delvareb119c6c2006-08-15 18:26:30 +02001376
Phil Reidf8756c62017-08-24 17:31:04 +08001377out_reg:
1378 init_completion(&adap->dev_released);
1379 device_unregister(&adap->dev);
1380 wait_for_completion(&adap->dev_released);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001381out_list:
Jean Delvare35fc37f2009-06-19 16:58:19 +02001382 mutex_lock(&core_lock);
Jean Delvareb119c6c2006-08-15 18:26:30 +02001383 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001384 mutex_unlock(&core_lock);
1385 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
David Brownell6e13e642007-05-01 23:26:31 +02001388/**
Doug Andersonee5c2742013-03-01 08:57:31 -08001389 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1390 * @adap: the adapter to register (with adap->nr initialized)
1391 * Context: can sleep
1392 *
1393 * See i2c_add_numbered_adapter() for details.
1394 */
1395static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1396{
Wolfram Sang84d0b612016-07-09 13:35:01 +09001397 int id;
Doug Andersonee5c2742013-03-01 08:57:31 -08001398
1399 mutex_lock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001400 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
Doug Andersonee5c2742013-03-01 08:57:31 -08001401 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001402 if (WARN(id < 0, "couldn't get idr"))
Doug Andersonee5c2742013-03-01 08:57:31 -08001403 return id == -ENOSPC ? -EBUSY : id;
1404
1405 return i2c_register_adapter(adap);
1406}
1407
1408/**
David Brownell6e13e642007-05-01 23:26:31 +02001409 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1410 * @adapter: the adapter to add
David Brownelld64f73b2007-07-12 14:12:28 +02001411 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001412 *
1413 * This routine is used to declare an I2C adapter when its bus number
Doug Andersonee5c2742013-03-01 08:57:31 -08001414 * doesn't matter or when its bus number is specified by an dt alias.
1415 * Examples of bases when the bus number doesn't matter: I2C adapters
1416 * dynamically added by USB links or PCI plugin cards.
David Brownell6e13e642007-05-01 23:26:31 +02001417 *
1418 * When this returns zero, a new bus number was allocated and stored
1419 * in adap->nr, and the specified adapter became available for clients.
1420 * Otherwise, a negative errno value is returned.
1421 */
1422int i2c_add_adapter(struct i2c_adapter *adapter)
1423{
Doug Andersonee5c2742013-03-01 08:57:31 -08001424 struct device *dev = &adapter->dev;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001425 int id;
David Brownell6e13e642007-05-01 23:26:31 +02001426
Doug Andersonee5c2742013-03-01 08:57:31 -08001427 if (dev->of_node) {
1428 id = of_alias_get_id(dev->of_node, "i2c");
1429 if (id >= 0) {
1430 adapter->nr = id;
1431 return __i2c_add_numbered_adapter(adapter);
1432 }
1433 }
1434
Jean Delvarecaada322008-01-27 18:14:49 +01001435 mutex_lock(&core_lock);
Tejun Heo4ae42b02013-02-27 17:04:15 -08001436 id = idr_alloc(&i2c_adapter_idr, adapter,
1437 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
Jean Delvarecaada322008-01-27 18:14:49 +01001438 mutex_unlock(&core_lock);
Wolfram Sang84d0b612016-07-09 13:35:01 +09001439 if (WARN(id < 0, "couldn't get idr"))
Tejun Heo4ae42b02013-02-27 17:04:15 -08001440 return id;
David Brownell6e13e642007-05-01 23:26:31 +02001441
1442 adapter->nr = id;
Tejun Heo4ae42b02013-02-27 17:04:15 -08001443
David Brownell6e13e642007-05-01 23:26:31 +02001444 return i2c_register_adapter(adapter);
1445}
1446EXPORT_SYMBOL(i2c_add_adapter);
1447
1448/**
1449 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1450 * @adap: the adapter to register (with adap->nr initialized)
David Brownelld64f73b2007-07-12 14:12:28 +02001451 * Context: can sleep
David Brownell6e13e642007-05-01 23:26:31 +02001452 *
1453 * This routine is used to declare an I2C adapter when its bus number
Randy Dunlap8c07e462008-03-23 20:28:20 +01001454 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1455 * or otherwise built in to the system's mainboard, and where i2c_board_info
David Brownell6e13e642007-05-01 23:26:31 +02001456 * is used to properly configure I2C devices.
1457 *
Grant Likely488bf312011-07-25 17:49:43 +02001458 * If the requested bus number is set to -1, then this function will behave
1459 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1460 *
David Brownell6e13e642007-05-01 23:26:31 +02001461 * If no devices have pre-been declared for this bus, then be sure to
1462 * register the adapter before any dynamically allocated ones. Otherwise
1463 * the required bus ID may not be available.
1464 *
1465 * When this returns zero, the specified adapter became available for
1466 * clients using the bus number provided in adap->nr. Also, the table
1467 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1468 * and the appropriate driver model device nodes are created. Otherwise, a
1469 * negative errno value is returned.
1470 */
1471int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1472{
Grant Likely488bf312011-07-25 17:49:43 +02001473 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1474 return i2c_add_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001475
Doug Andersonee5c2742013-03-01 08:57:31 -08001476 return __i2c_add_numbered_adapter(adap);
David Brownell6e13e642007-05-01 23:26:31 +02001477}
1478EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1479
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001480static void i2c_do_del_adapter(struct i2c_driver *driver,
Jean Delvare69b00892009-12-06 17:06:27 +01001481 struct i2c_adapter *adapter)
Jean Delvare026526f2008-01-27 18:14:49 +01001482{
Jean Delvare4735c982008-07-14 22:38:36 +02001483 struct i2c_client *client, *_n;
Jean Delvare026526f2008-01-27 18:14:49 +01001484
Jean Delvareacec2112009-03-28 21:34:40 +01001485 /* Remove the devices we created ourselves as the result of hardware
1486 * probing (using a driver's detect method) */
Jean Delvare4735c982008-07-14 22:38:36 +02001487 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1488 if (client->adapter == adapter) {
1489 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1490 client->name, client->addr);
1491 list_del(&client->detected);
1492 i2c_unregister_device(client);
1493 }
1494 }
Jean Delvare026526f2008-01-27 18:14:49 +01001495}
1496
Jean Delvaree549c2b2009-06-19 16:58:19 +02001497static int __unregister_client(struct device *dev, void *dummy)
1498{
1499 struct i2c_client *client = i2c_verify_client(dev);
Jean Delvare5219bf82011-01-14 22:03:49 +01001500 if (client && strcmp(client->name, "dummy"))
1501 i2c_unregister_device(client);
1502 return 0;
1503}
1504
1505static int __unregister_dummy(struct device *dev, void *dummy)
1506{
1507 struct i2c_client *client = i2c_verify_client(dev);
Andy Shevchenko7b43dd12017-10-31 16:21:35 +02001508 i2c_unregister_device(client);
Jean Delvaree549c2b2009-06-19 16:58:19 +02001509 return 0;
1510}
1511
Jean Delvare69b00892009-12-06 17:06:27 +01001512static int __process_removed_adapter(struct device_driver *d, void *data)
1513{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001514 i2c_do_del_adapter(to_i2c_driver(d), data);
1515 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001516}
1517
David Brownelld64f73b2007-07-12 14:12:28 +02001518/**
1519 * i2c_del_adapter - unregister I2C adapter
1520 * @adap: the adapter being unregistered
1521 * Context: can sleep
1522 *
1523 * This unregisters an I2C adapter which was previously registered
1524 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1525 */
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001526void i2c_del_adapter(struct i2c_adapter *adap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Jean Delvare35fc37f2009-06-19 16:58:19 +02001528 struct i2c_adapter *found;
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001529 struct i2c_client *client, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
1531 /* First make sure that this adapter was ever added */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001532 mutex_lock(&core_lock);
1533 found = idr_find(&i2c_adapter_idr, adap->nr);
1534 mutex_unlock(&core_lock);
1535 if (found != adap) {
Wolfram Sang44239a52016-07-09 13:35:04 +09001536 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
Lars-Peter Clausen71546302013-03-09 08:16:47 +00001537 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
Jarkko Nikulaaec809f2016-08-12 17:02:52 +03001540 i2c_acpi_remove_space_handler(adap);
Jean Delvare026526f2008-01-27 18:14:49 +01001541 /* Tell drivers about this removal */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001542 mutex_lock(&core_lock);
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001543 bus_for_each_drv(&i2c_bus_type, NULL, adap,
Jean Delvare69b00892009-12-06 17:06:27 +01001544 __process_removed_adapter);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001545 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001547 /* Remove devices instantiated from sysfs */
Jean Delvare390946b2012-09-10 10:14:02 +02001548 mutex_lock_nested(&adap->userspace_clients_lock,
1549 i2c_adapter_depth(adap));
Jean Delvare6629dcf2010-05-04 11:09:28 +02001550 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1551 detected) {
1552 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1553 client->addr);
1554 list_del(&client->detected);
1555 i2c_unregister_device(client);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001556 }
Jean Delvaredafc50d2010-08-11 18:21:01 +02001557 mutex_unlock(&adap->userspace_clients_lock);
Jean Delvarebbd2d9c2009-11-26 09:22:33 +01001558
Jean Delvaree549c2b2009-06-19 16:58:19 +02001559 /* Detach any active clients. This can't fail, thus we do not
Jean Delvare5219bf82011-01-14 22:03:49 +01001560 * check the returned value. This is a two-pass process, because
1561 * we can't remove the dummy devices during the first pass: they
1562 * could have been instantiated by real devices wishing to clean
1563 * them up properly, so we give them a chance to do that first. */
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001564 device_for_each_child(&adap->dev, NULL, __unregister_client);
1565 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Jean Delvare2bb50952009-09-18 22:45:46 +02001567#ifdef CONFIG_I2C_COMPAT
1568 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1569 adap->dev.parent);
1570#endif
1571
Thadeu Lima de Souza Cascardoc5567522010-01-16 20:43:13 +01001572 /* device name is gone after device_unregister */
1573 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1574
Wolfram Sang9f924162015-12-23 18:19:28 +01001575 pm_runtime_disable(&adap->dev);
1576
Benjamin Tissoires4d5538f2016-10-13 14:10:40 +02001577 i2c_host_notify_irq_teardown(adap);
1578
Wolfram Sang26680ee2015-01-29 22:45:09 +01001579 /* wait until all references to the device are gone
1580 *
1581 * FIXME: This is old code and should ideally be replaced by an
1582 * alternative which results in decoupling the lifetime of the struct
1583 * device from the i2c_adapter, like spi or netdev do. Any solution
Shailendra Verma95cc1e32015-05-18 22:24:01 +05301584 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
Wolfram Sang26680ee2015-01-29 22:45:09 +01001585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 init_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 device_unregister(&adap->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 wait_for_completion(&adap->dev_released);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
David Brownell6e13e642007-05-01 23:26:31 +02001590 /* free bus id */
Jean Delvare35fc37f2009-06-19 16:58:19 +02001591 mutex_lock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 idr_remove(&i2c_adapter_idr, adap->nr);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001593 mutex_unlock(&core_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Jean Delvarebd4bc3db2008-07-16 19:30:05 +02001595 /* Clear the device structure in case this adapter is ever going to be
1596 added again */
1597 memset(&adap->dev, 0, sizeof(adap->dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
David Brownellc0564602007-05-01 23:26:31 +02001599EXPORT_SYMBOL(i2c_del_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Wolfram Sang54177cc2015-12-17 13:32:36 +01001601/**
1602 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
1603 * @dev: The device to scan for I2C timing properties
1604 * @t: the i2c_timings struct to be filled with values
1605 * @use_defaults: bool to use sane defaults derived from the I2C specification
Andy Shevchenko263a5642020-03-24 14:32:12 +02001606 * when properties are not found, otherwise don't update
Wolfram Sang54177cc2015-12-17 13:32:36 +01001607 *
1608 * Scan the device for the generic I2C properties describing timing parameters
1609 * for the signal and fill the given struct with the results. If a property was
1610 * not found and use_defaults was true, then maximum timings are assumed which
1611 * are derived from the I2C specification. If use_defaults is not used, the
Andy Shevchenko263a5642020-03-24 14:32:12 +02001612 * results will be as before, so drivers can apply their own defaults before
1613 * calling this helper. The latter is mainly intended for avoiding regressions
1614 * of existing drivers which want to switch to this function. New drivers
1615 * almost always should use the defaults.
Wolfram Sang54177cc2015-12-17 13:32:36 +01001616 */
Wolfram Sang54177cc2015-12-17 13:32:36 +01001617void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
1618{
1619 int ret;
1620
Wolfram Sang54177cc2015-12-17 13:32:36 +01001621 ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
1622 if (ret && use_defaults)
Andy Shevchenkoe6282fc62020-03-24 14:32:11 +02001623 t->bus_freq_hz = I2C_MAX_STANDARD_MODE_FREQ;
Wolfram Sang54177cc2015-12-17 13:32:36 +01001624
1625 ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
1626 if (ret && use_defaults) {
Andy Shevchenkoe6282fc62020-03-24 14:32:11 +02001627 if (t->bus_freq_hz <= I2C_MAX_STANDARD_MODE_FREQ)
Wolfram Sang54177cc2015-12-17 13:32:36 +01001628 t->scl_rise_ns = 1000;
Andy Shevchenkoe6282fc62020-03-24 14:32:11 +02001629 else if (t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ)
Wolfram Sang54177cc2015-12-17 13:32:36 +01001630 t->scl_rise_ns = 300;
1631 else
1632 t->scl_rise_ns = 120;
1633 }
1634
1635 ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
1636 if (ret && use_defaults) {
Andy Shevchenkoe6282fc62020-03-24 14:32:11 +02001637 if (t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ)
Wolfram Sang54177cc2015-12-17 13:32:36 +01001638 t->scl_fall_ns = 300;
1639 else
1640 t->scl_fall_ns = 120;
1641 }
1642
Andy Shevchenko263a5642020-03-24 14:32:12 +02001643 ret = device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
1644 if (ret && use_defaults)
1645 t->scl_int_delay_ns = 0;
Wolfram Sang54177cc2015-12-17 13:32:36 +01001646
1647 ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
1648 if (ret && use_defaults)
1649 t->sda_fall_ns = t->scl_fall_ns;
Andy Shevchenko4717be72018-07-25 17:39:25 +03001650
Andy Shevchenko263a5642020-03-24 14:32:12 +02001651 ret = device_property_read_u32(dev, "i2c-sda-hold-time-ns", &t->sda_hold_ns);
1652 if (ret && use_defaults)
1653 t->sda_hold_ns = 0;
Eugen Hristevb84dfe12019-10-23 12:40:14 +00001654
Andy Shevchenko263a5642020-03-24 14:32:12 +02001655 ret = device_property_read_u32(dev, "i2c-digital-filter-width-ns", &t->digital_filter_width_ns);
1656 if (ret && use_defaults)
1657 t->digital_filter_width_ns = 0;
Eugen Hristevb84dfe12019-10-23 12:40:14 +00001658
Andy Shevchenko263a5642020-03-24 14:32:12 +02001659 ret = device_property_read_u32(dev, "i2c-analog-filter-cutoff-frequency", &t->analog_filter_cutoff_freq_hz);
1660 if (ret && use_defaults)
1661 t->analog_filter_cutoff_freq_hz = 0;
Wolfram Sang54177cc2015-12-17 13:32:36 +01001662}
1663EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
1664
David Brownell7b4fbc52007-05-01 23:26:30 +02001665/* ------------------------------------------------------------------------- */
1666
Wolfram Sangedd7a562019-06-03 10:25:33 +02001667int i2c_for_each_dev(void *data, int (*fn)(struct device *dev, void *data))
Jean Delvare7ae31482011-03-20 14:50:52 +01001668{
1669 int res;
1670
1671 mutex_lock(&core_lock);
1672 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1673 mutex_unlock(&core_lock);
1674
1675 return res;
1676}
1677EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1678
Jean Delvare69b00892009-12-06 17:06:27 +01001679static int __process_new_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001680{
Jean Delvare4f8cf822009-09-18 22:45:46 +02001681 if (dev->type != &i2c_adapter_type)
1682 return 0;
Jean Delvare69b00892009-12-06 17:06:27 +01001683 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
Dave Young7f101a92008-07-14 22:38:19 +02001684}
1685
David Brownell7b4fbc52007-05-01 23:26:30 +02001686/*
1687 * An i2c_driver is used with one or more i2c_client (device) nodes to access
Jean Delvare729d6dd2009-06-19 16:58:18 +02001688 * i2c slave chips, on a bus instance associated with some i2c_adapter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 */
1690
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001691int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Jean Delvare7eebcb72006-02-05 23:28:21 +01001693 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
David Brownell1d0b19c2008-10-14 17:30:05 +02001695 /* Can't register until after driver model init */
Sudip Mukherjee95026652016-03-07 17:19:17 +05301696 if (WARN_ON(!is_registered))
David Brownell1d0b19c2008-10-14 17:30:05 +02001697 return -EAGAIN;
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /* add the driver to the list of i2c drivers in the driver core */
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001700 driver->driver.owner = owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 driver->driver.bus = &i2c_bus_type;
Vladimir Zapolskiy147b36d2016-10-31 21:46:24 +02001702 INIT_LIST_HEAD(&driver->clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Jean Delvare729d6dd2009-06-19 16:58:18 +02001704 /* When registration returns, the driver core
David Brownell6e13e642007-05-01 23:26:31 +02001705 * will have called probe() for all matching-but-unbound devices.
1706 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 res = driver_register(&driver->driver);
1708 if (res)
Jean Delvare7eebcb72006-02-05 23:28:21 +01001709 return res;
David Brownell438d6c22006-12-10 21:21:31 +01001710
Wolfram Sang44239a52016-07-09 13:35:04 +09001711 pr_debug("driver [%s] registered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Jean Delvare4735c982008-07-14 22:38:36 +02001713 /* Walk the adapters that are already present */
Jean Delvare7ae31482011-03-20 14:50:52 +01001714 i2c_for_each_dev(driver, __process_new_driver);
Jean Delvare35fc37f2009-06-19 16:58:19 +02001715
Jean Delvare7eebcb72006-02-05 23:28:21 +01001716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
Greg Kroah-Hartmande59cf92005-12-06 15:33:15 -08001718EXPORT_SYMBOL(i2c_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Jean Delvare69b00892009-12-06 17:06:27 +01001720static int __process_removed_driver(struct device *dev, void *data)
Dave Young7f101a92008-07-14 22:38:19 +02001721{
Lars-Peter Clausen19baba42013-03-09 08:16:44 +00001722 if (dev->type == &i2c_adapter_type)
1723 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1724 return 0;
Dave Young7f101a92008-07-14 22:38:19 +02001725}
1726
David Brownella1d9e6e2007-05-01 23:26:30 +02001727/**
1728 * i2c_del_driver - unregister I2C driver
1729 * @driver: the driver being unregistered
David Brownelld64f73b2007-07-12 14:12:28 +02001730 * Context: can sleep
David Brownella1d9e6e2007-05-01 23:26:30 +02001731 */
Jean Delvareb3e82092007-05-01 23:26:32 +02001732void i2c_del_driver(struct i2c_driver *driver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Jean Delvare7ae31482011-03-20 14:50:52 +01001734 i2c_for_each_dev(driver, __process_removed_driver);
David Brownella1d9e6e2007-05-01 23:26:30 +02001735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 driver_unregister(&driver->driver);
Wolfram Sang44239a52016-07-09 13:35:04 +09001737 pr_debug("driver [%s] unregistered\n", driver->driver.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738}
David Brownellc0564602007-05-01 23:26:31 +02001739EXPORT_SYMBOL(i2c_del_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
David Brownell7b4fbc52007-05-01 23:26:30 +02001741/* ------------------------------------------------------------------------- */
1742
David Brownell9b766b82008-01-27 18:14:51 +01001743struct i2c_cmd_arg {
1744 unsigned cmd;
1745 void *arg;
1746};
1747
1748static int i2c_cmd(struct device *dev, void *_arg)
1749{
1750 struct i2c_client *client = i2c_verify_client(dev);
1751 struct i2c_cmd_arg *arg = _arg;
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001752 struct i2c_driver *driver;
David Brownell9b766b82008-01-27 18:14:51 +01001753
Lars-Peter Clausen0acc2b32013-09-29 10:51:06 +02001754 if (!client || !client->dev.driver)
1755 return 0;
1756
1757 driver = to_i2c_driver(client->dev.driver);
1758 if (driver->command)
1759 driver->command(client, arg->cmd, arg->arg);
David Brownell9b766b82008-01-27 18:14:51 +01001760 return 0;
1761}
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1764{
David Brownell9b766b82008-01-27 18:14:51 +01001765 struct i2c_cmd_arg cmd_arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
David Brownell9b766b82008-01-27 18:14:51 +01001767 cmd_arg.cmd = cmd;
1768 cmd_arg.arg = arg;
1769 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
David Brownellc0564602007-05-01 23:26:31 +02001771EXPORT_SYMBOL(i2c_clients_command);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773static int __init i2c_init(void)
1774{
1775 int retval;
1776
Wolfram Sang03bde7c2015-03-12 17:17:59 +01001777 retval = of_alias_get_highest_id("i2c");
1778
1779 down_write(&__i2c_board_lock);
1780 if (retval >= __i2c_first_dynamic_bus_num)
1781 __i2c_first_dynamic_bus_num = retval + 1;
1782 up_write(&__i2c_board_lock);
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 retval = bus_register(&i2c_bus_type);
1785 if (retval)
1786 return retval;
Wolfram Sangb980a262016-03-14 10:41:52 +01001787
1788 is_registered = true;
1789
Jean Delvare2bb50952009-09-18 22:45:46 +02001790#ifdef CONFIG_I2C_COMPAT
1791 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1792 if (!i2c_adapter_compat_class) {
1793 retval = -ENOMEM;
1794 goto bus_err;
1795 }
1796#endif
David Brownelle9f13732008-01-27 18:14:52 +01001797 retval = i2c_add_driver(&dummy_driver);
1798 if (retval)
Jean Delvare2bb50952009-09-18 22:45:46 +02001799 goto class_err;
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001800
1801 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1802 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001803 if (IS_ENABLED(CONFIG_ACPI))
1804 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001805
David Brownelle9f13732008-01-27 18:14:52 +01001806 return 0;
1807
Jean Delvare2bb50952009-09-18 22:45:46 +02001808class_err:
1809#ifdef CONFIG_I2C_COMPAT
1810 class_compat_unregister(i2c_adapter_compat_class);
David Brownelle9f13732008-01-27 18:14:52 +01001811bus_err:
Jean Delvare2bb50952009-09-18 22:45:46 +02001812#endif
Wolfram Sangb980a262016-03-14 10:41:52 +01001813 is_registered = false;
David Brownelle9f13732008-01-27 18:14:52 +01001814 bus_unregister(&i2c_bus_type);
1815 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
1818static void __exit i2c_exit(void)
1819{
Octavian Purdila525e6fa2016-07-08 19:13:10 +03001820 if (IS_ENABLED(CONFIG_ACPI))
1821 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
Pantelis Antoniouea7513b2014-10-28 22:36:03 +02001822 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
1823 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
David Brownelle9f13732008-01-27 18:14:52 +01001824 i2c_del_driver(&dummy_driver);
Jean Delvare2bb50952009-09-18 22:45:46 +02001825#ifdef CONFIG_I2C_COMPAT
1826 class_compat_unregister(i2c_adapter_compat_class);
1827#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 bus_unregister(&i2c_bus_type);
David Howellsd9a83d62014-03-06 13:35:59 +00001829 tracepoint_synchronize_unregister();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
David Brownella10f9e72008-10-14 17:30:06 +02001832/* We must initialize early, because some subsystems register i2c drivers
1833 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1834 */
1835postcore_initcall(i2c_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836module_exit(i2c_exit);
1837
1838/* ----------------------------------------------------
1839 * the functional interface to the i2c busses.
1840 * ----------------------------------------------------
1841 */
1842
Wolfram Sangb7f62582015-01-05 23:45:59 +01001843/* Check if val is exceeding the quirk IFF quirk is non 0 */
1844#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1845
1846static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
1847{
1848 dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
1849 err_msg, msg->addr, msg->len,
1850 msg->flags & I2C_M_RD ? "read" : "write");
1851 return -EOPNOTSUPP;
1852}
1853
1854static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1855{
1856 const struct i2c_adapter_quirks *q = adap->quirks;
1857 int max_num = q->max_num_msgs, i;
1858 bool do_len_check = true;
1859
1860 if (q->flags & I2C_AQ_COMB) {
1861 max_num = 2;
1862
1863 /* special checks for combined messages */
1864 if (num == 2) {
1865 if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
1866 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
1867
1868 if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
1869 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
1870
1871 if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
1872 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
1873
1874 if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
1875 return i2c_quirk_error(adap, &msgs[0], "msg too long");
1876
1877 if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
1878 return i2c_quirk_error(adap, &msgs[1], "msg too long");
1879
1880 do_len_check = false;
1881 }
1882 }
1883
1884 if (i2c_quirk_exceeded(num, max_num))
1885 return i2c_quirk_error(adap, &msgs[0], "too many messages");
1886
1887 for (i = 0; i < num; i++) {
1888 u16 len = msgs[i].len;
1889
1890 if (msgs[i].flags & I2C_M_RD) {
1891 if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
1892 return i2c_quirk_error(adap, &msgs[i], "msg too long");
Wolfram Sangd9cfe2c2018-07-23 22:26:05 +02001893
1894 if (q->flags & I2C_AQ_NO_ZERO_LEN_READ && len == 0)
1895 return i2c_quirk_error(adap, &msgs[i], "no zero length");
Wolfram Sangb7f62582015-01-05 23:45:59 +01001896 } else {
1897 if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
1898 return i2c_quirk_error(adap, &msgs[i], "msg too long");
Wolfram Sangd9cfe2c2018-07-23 22:26:05 +02001899
1900 if (q->flags & I2C_AQ_NO_ZERO_LEN_WRITE && len == 0)
1901 return i2c_quirk_error(adap, &msgs[i], "no zero length");
Wolfram Sangb7f62582015-01-05 23:45:59 +01001902 }
1903 }
1904
1905 return 0;
1906}
1907
David Brownella1cdeda2008-07-14 22:38:24 +02001908/**
Jean Delvareb37d2a32012-06-29 07:47:19 -03001909 * __i2c_transfer - unlocked flavor of i2c_transfer
1910 * @adap: Handle to I2C bus
1911 * @msgs: One or more messages to execute before STOP is issued to
1912 * terminate the operation; each message begins with a START.
1913 * @num: Number of messages to be executed.
1914 *
1915 * Returns negative errno, else the number of messages executed.
1916 *
1917 * Adapter lock must be held when calling this function. No debug logging
1918 * takes place. adap->algo->master_xfer existence isn't checked.
1919 */
1920int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1921{
1922 unsigned long orig_jiffies;
1923 int ret, try;
1924
Ard Biesheuvel1eace832018-04-03 21:11:50 +02001925 if (WARN_ON(!msgs || num < 1))
1926 return -EINVAL;
Wolfram Sang5d756112019-04-25 16:19:48 +02001927
1928 ret = __i2c_check_suspended(adap);
1929 if (ret)
1930 return ret;
Ard Biesheuvel1eace832018-04-03 21:11:50 +02001931
Wolfram Sangb7f62582015-01-05 23:45:59 +01001932 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
1933 return -EOPNOTSUPP;
1934
Davidlohr Bueso50888b02018-03-26 14:09:24 -07001935 /*
1936 * i2c_trace_msg_key gets enabled when tracepoint i2c_transfer gets
David Howellsd9a83d62014-03-06 13:35:59 +00001937 * enabled. This is an efficient way of keeping the for-loop from
1938 * being executed when not needed.
1939 */
Davidlohr Bueso50888b02018-03-26 14:09:24 -07001940 if (static_branch_unlikely(&i2c_trace_msg_key)) {
David Howellsd9a83d62014-03-06 13:35:59 +00001941 int i;
1942 for (i = 0; i < num; i++)
1943 if (msgs[i].flags & I2C_M_RD)
1944 trace_i2c_read(adap, &msgs[i], i);
1945 else
1946 trace_i2c_write(adap, &msgs[i], i);
1947 }
1948
Jean Delvareb37d2a32012-06-29 07:47:19 -03001949 /* Retry automatically on arbitration loss */
1950 orig_jiffies = jiffies;
1951 for (ret = 0, try = 0; try <= adap->retries; try++) {
Wolfram Sang63b96982019-04-03 14:40:10 +02001952 if (i2c_in_atomic_xfer_mode() && adap->algo->master_xfer_atomic)
1953 ret = adap->algo->master_xfer_atomic(adap, msgs, num);
1954 else
1955 ret = adap->algo->master_xfer(adap, msgs, num);
1956
Jean Delvareb37d2a32012-06-29 07:47:19 -03001957 if (ret != -EAGAIN)
1958 break;
1959 if (time_after(jiffies, orig_jiffies + adap->timeout))
1960 break;
1961 }
1962
Davidlohr Bueso50888b02018-03-26 14:09:24 -07001963 if (static_branch_unlikely(&i2c_trace_msg_key)) {
David Howellsd9a83d62014-03-06 13:35:59 +00001964 int i;
1965 for (i = 0; i < ret; i++)
1966 if (msgs[i].flags & I2C_M_RD)
1967 trace_i2c_reply(adap, &msgs[i], i);
Ahbong Chang4a3f7692018-03-31 16:17:34 +08001968 trace_i2c_result(adap, num, ret);
David Howellsd9a83d62014-03-06 13:35:59 +00001969 }
1970
Jean Delvareb37d2a32012-06-29 07:47:19 -03001971 return ret;
1972}
1973EXPORT_SYMBOL(__i2c_transfer);
1974
1975/**
David Brownella1cdeda2008-07-14 22:38:24 +02001976 * i2c_transfer - execute a single or combined I2C message
1977 * @adap: Handle to I2C bus
1978 * @msgs: One or more messages to execute before STOP is issued to
1979 * terminate the operation; each message begins with a START.
1980 * @num: Number of messages to be executed.
1981 *
1982 * Returns negative errno, else the number of messages executed.
1983 *
1984 * Note that there is no requirement that each message be sent to
1985 * the same slave address, although that is the most common model.
1986 */
Zhenwen Xu09b8ce02009-03-28 21:34:46 +01001987int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Jean Delvareb37d2a32012-06-29 07:47:19 -03001989 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Wolfram Sangcc526122018-09-20 18:14:21 +02001991 if (!adap->algo->master_xfer) {
1992 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1993 return -EOPNOTSUPP;
1994 }
1995
David Brownella1cdeda2008-07-14 22:38:24 +02001996 /* REVISIT the fault reporting model here is weak:
1997 *
1998 * - When we get an error after receiving N bytes from a slave,
1999 * there is no way to report "N".
2000 *
2001 * - When we get a NAK after transmitting N bytes to a slave,
2002 * there is no way to report "N" ... or to let the master
2003 * continue executing the rest of this combined message, if
2004 * that's the appropriate response.
2005 *
2006 * - When for example "num" is two and we successfully complete
2007 * the first message but get an error part way through the
2008 * second, it's unclear whether that should be reported as
2009 * one (discarding status on the second message) or errno
2010 * (discarding status on the first one).
2011 */
Wolfram Sang83c42212019-04-03 14:40:09 +02002012 ret = __i2c_lock_bus_helper(adap);
2013 if (ret)
2014 return ret;
Wolfram Sangcc526122018-09-20 18:14:21 +02002015
2016 ret = __i2c_transfer(adap, msgs, num);
2017 i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
2018
2019 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020}
David Brownellc0564602007-05-01 23:26:31 +02002021EXPORT_SYMBOL(i2c_transfer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
David Brownella1cdeda2008-07-14 22:38:24 +02002023/**
Wolfram Sang8a917322017-11-04 21:20:04 +01002024 * i2c_transfer_buffer_flags - issue a single I2C message transferring data
2025 * to/from a buffer
David Brownella1cdeda2008-07-14 22:38:24 +02002026 * @client: Handle to slave device
Wolfram Sang8a917322017-11-04 21:20:04 +01002027 * @buf: Where the data is stored
2028 * @count: How many bytes to transfer, must be less than 64k since msg.len is u16
2029 * @flags: The flags to be used for the message, e.g. I2C_M_RD for reads
David Brownella1cdeda2008-07-14 22:38:24 +02002030 *
Wolfram Sang8a917322017-11-04 21:20:04 +01002031 * Returns negative errno, or else the number of bytes transferred.
David Brownella1cdeda2008-07-14 22:38:24 +02002032 */
Wolfram Sang8a917322017-11-04 21:20:04 +01002033int i2c_transfer_buffer_flags(const struct i2c_client *client, char *buf,
2034 int count, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
2036 int ret;
Wolfram Sang8a917322017-11-04 21:20:04 +01002037 struct i2c_msg msg = {
2038 .addr = client->addr,
2039 .flags = flags | (client->flags & I2C_M_TEN),
2040 .len = count,
2041 .buf = buf,
2042 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Wolfram Sang8a917322017-11-04 21:20:04 +01002044 ret = i2c_transfer(client->adapter, &msg, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002046 /*
Wolfram Sang8a917322017-11-04 21:20:04 +01002047 * If everything went ok (i.e. 1 msg transferred), return #bytes
2048 * transferred, else error code.
Wolfram Sang834aa6f2012-03-15 18:11:05 +01002049 */
Jean Delvare815f55f2005-05-07 22:58:46 +02002050 return (ret == 1) ? count : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
Wolfram Sang8a917322017-11-04 21:20:04 +01002052EXPORT_SYMBOL(i2c_transfer_buffer_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Peter Rosindde67eb2018-01-22 08:32:01 +01002054/**
2055 * i2c_get_device_id - get manufacturer, part id and die revision of a device
2056 * @client: The device to query
2057 * @id: The queried information
2058 *
2059 * Returns negative errno on error, zero on success.
2060 */
2061int i2c_get_device_id(const struct i2c_client *client,
2062 struct i2c_device_identity *id)
2063{
2064 struct i2c_adapter *adap = client->adapter;
2065 union i2c_smbus_data raw_id;
2066 int ret;
2067
2068 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
2069 return -EOPNOTSUPP;
2070
2071 raw_id.block[0] = 3;
2072 ret = i2c_smbus_xfer(adap, I2C_ADDR_DEVICE_ID, 0,
2073 I2C_SMBUS_READ, client->addr << 1,
2074 I2C_SMBUS_I2C_BLOCK_DATA, &raw_id);
2075 if (ret)
2076 return ret;
2077
2078 id->manufacturer_id = (raw_id.block[1] << 4) | (raw_id.block[2] >> 4);
2079 id->part_id = ((raw_id.block[2] & 0xf) << 5) | (raw_id.block[3] >> 3);
2080 id->die_revision = raw_id.block[3] & 0x7;
2081 return 0;
2082}
2083EXPORT_SYMBOL_GPL(i2c_get_device_id);
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085/* ----------------------------------------------------
2086 * the i2c address scanning function
2087 * Will not work for 10-bit addresses!
2088 * ----------------------------------------------------
2089 */
Jean Delvare9fc6adf2005-07-31 21:20:43 +02002090
Jean Delvare63e4e802010-06-03 11:33:51 +02002091/*
2092 * Legacy default probe function, mostly relevant for SMBus. The default
2093 * probe method is a quick write, but it is known to corrupt the 24RF08
2094 * EEPROMs due to a state machine bug, and could also irreversibly
2095 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2096 * we use a short byte read instead. Also, some bus drivers don't implement
2097 * quick write, so we fallback to a byte read in that case too.
2098 * On x86, there is another special case for FSC hardware monitoring chips,
2099 * which want regular byte reads (address 0x73.) Fortunately, these are the
2100 * only known chips using this I2C address on PC hardware.
2101 * Returns 1 if probe succeeded, 0 if not.
2102 */
2103static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
2104{
2105 int err;
2106 union i2c_smbus_data dummy;
2107
2108#ifdef CONFIG_X86
2109 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
2110 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
2111 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2112 I2C_SMBUS_BYTE_DATA, &dummy);
2113 else
2114#endif
Jean Delvare8031d792010-08-11 18:21:00 +02002115 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
2116 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
Jean Delvare63e4e802010-06-03 11:33:51 +02002117 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
2118 I2C_SMBUS_QUICK, NULL);
Jean Delvare8031d792010-08-11 18:21:00 +02002119 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
2120 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2121 I2C_SMBUS_BYTE, &dummy);
2122 else {
Andy Lutomirskid63a9e82013-07-17 13:27:59 -07002123 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
2124 addr);
Jean Delvare8031d792010-08-11 18:21:00 +02002125 err = -EOPNOTSUPP;
2126 }
Jean Delvare63e4e802010-06-03 11:33:51 +02002127
2128 return err >= 0;
2129}
2130
Jean Delvareccfbbd02009-12-06 17:06:25 +01002131static int i2c_detect_address(struct i2c_client *temp_client,
Jean Delvare4735c982008-07-14 22:38:36 +02002132 struct i2c_driver *driver)
2133{
2134 struct i2c_board_info info;
2135 struct i2c_adapter *adapter = temp_client->adapter;
2136 int addr = temp_client->addr;
2137 int err;
2138
2139 /* Make sure the address is valid */
Wolfram Sang66be60562015-07-17 12:43:22 +02002140 err = i2c_check_7bit_addr_validity_strict(addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002141 if (err) {
Jean Delvare4735c982008-07-14 22:38:36 +02002142 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
2143 addr);
Jean Delvare656b8762010-06-03 11:33:53 +02002144 return err;
Jean Delvare4735c982008-07-14 22:38:36 +02002145 }
2146
Wolfram Sang9bccc702015-07-17 14:48:56 +02002147 /* Skip if already in use (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002148 if (i2c_check_addr_busy(adapter, addr))
Jean Delvare4735c982008-07-14 22:38:36 +02002149 return 0;
2150
Jean Delvareccfbbd02009-12-06 17:06:25 +01002151 /* Make sure there is something at this address */
Jean Delvare63e4e802010-06-03 11:33:51 +02002152 if (!i2c_default_probe(adapter, addr))
2153 return 0;
Jean Delvare4735c982008-07-14 22:38:36 +02002154
2155 /* Finally call the custom detection function */
2156 memset(&info, 0, sizeof(struct i2c_board_info));
2157 info.addr = addr;
Jean Delvare310ec792009-12-14 21:17:23 +01002158 err = driver->detect(temp_client, &info);
Jean Delvare4735c982008-07-14 22:38:36 +02002159 if (err) {
2160 /* -ENODEV is returned if the detection fails. We catch it
2161 here as this isn't an error. */
2162 return err == -ENODEV ? 0 : err;
2163 }
2164
2165 /* Consistency check */
2166 if (info.type[0] == '\0') {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002167 dev_err(&adapter->dev,
2168 "%s detection function provided no name for 0x%x\n",
2169 driver->driver.name, addr);
Jean Delvare4735c982008-07-14 22:38:36 +02002170 } else {
2171 struct i2c_client *client;
2172
2173 /* Detection succeeded, instantiate the device */
Wolfram Sang0c176172014-02-10 11:03:56 +01002174 if (adapter->class & I2C_CLASS_DEPRECATED)
2175 dev_warn(&adapter->dev,
2176 "This adapter will soon drop class based instantiation of devices. "
2177 "Please make sure client 0x%02x gets instantiated by other means. "
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -03002178 "Check 'Documentation/i2c/instantiating-devices.rst' for details.\n",
Wolfram Sang0c176172014-02-10 11:03:56 +01002179 info.addr);
2180
Jean Delvare4735c982008-07-14 22:38:36 +02002181 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
2182 info.type, info.addr);
Wolfram Sang87e07432020-01-07 18:47:43 +01002183 client = i2c_new_client_device(adapter, &info);
2184 if (!IS_ERR(client))
Jean Delvare4735c982008-07-14 22:38:36 +02002185 list_add_tail(&client->detected, &driver->clients);
2186 else
2187 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
2188 info.type, info.addr);
2189 }
2190 return 0;
2191}
2192
2193static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
2194{
Jean Delvarec3813d62009-12-14 21:17:25 +01002195 const unsigned short *address_list;
Jean Delvare4735c982008-07-14 22:38:36 +02002196 struct i2c_client *temp_client;
2197 int i, err = 0;
2198 int adap_id = i2c_adapter_id(adapter);
2199
Jean Delvarec3813d62009-12-14 21:17:25 +01002200 address_list = driver->address_list;
2201 if (!driver->detect || !address_list)
Jean Delvare4735c982008-07-14 22:38:36 +02002202 return 0;
2203
Wolfram Sang45552272014-07-10 13:46:21 +02002204 /* Warn that the adapter lost class based instantiation */
2205 if (adapter->class == I2C_CLASS_DEPRECATED) {
2206 dev_dbg(&adapter->dev,
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002207 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
Mauro Carvalho Chehabccf988b2019-07-26 09:51:16 -03002208 "If you need it, check 'Documentation/i2c/instantiating-devices.rst' for alternatives.\n",
Wolfram Sang45552272014-07-10 13:46:21 +02002209 driver->driver.name);
2210 return 0;
2211 }
2212
Jean Delvare51b54ba2010-10-24 18:16:58 +02002213 /* Stop here if the classes do not match */
2214 if (!(adapter->class & driver->class))
2215 return 0;
2216
Jean Delvare4735c982008-07-14 22:38:36 +02002217 /* Set up a temporary client to help detect callback */
2218 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
2219 if (!temp_client)
2220 return -ENOMEM;
2221 temp_client->adapter = adapter;
2222
Jean Delvarec3813d62009-12-14 21:17:25 +01002223 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002224 dev_dbg(&adapter->dev,
2225 "found normal entry for adapter %d, addr 0x%02x\n",
2226 adap_id, address_list[i]);
Jean Delvarec3813d62009-12-14 21:17:25 +01002227 temp_client->addr = address_list[i];
Jean Delvareccfbbd02009-12-06 17:06:25 +01002228 err = i2c_detect_address(temp_client, driver);
Jean Delvare51b54ba2010-10-24 18:16:58 +02002229 if (unlikely(err))
2230 break;
Jean Delvare4735c982008-07-14 22:38:36 +02002231 }
2232
Jean Delvare4735c982008-07-14 22:38:36 +02002233 kfree(temp_client);
2234 return err;
2235}
2236
Jean Delvared44f19d2010-08-11 18:20:57 +02002237int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
2238{
2239 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
2240 I2C_SMBUS_QUICK, NULL) >= 0;
2241}
2242EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
2243
Jean Delvare12b5053a2007-05-01 23:26:31 +02002244struct i2c_client *
Wolfram Sangc1d084752019-11-06 10:50:19 +01002245i2c_new_scanned_device(struct i2c_adapter *adap,
2246 struct i2c_board_info *info,
2247 unsigned short const *addr_list,
2248 int (*probe)(struct i2c_adapter *adap, unsigned short addr))
Jean Delvare12b5053a2007-05-01 23:26:31 +02002249{
2250 int i;
2251
Jean Delvare8031d792010-08-11 18:21:00 +02002252 if (!probe)
Jean Delvare9a942412010-08-11 18:20:56 +02002253 probe = i2c_default_probe;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002254
Jean Delvare12b5053a2007-05-01 23:26:31 +02002255 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
2256 /* Check address validity */
Wolfram Sang66be60562015-07-17 12:43:22 +02002257 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002258 dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
2259 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002260 continue;
2261 }
2262
Wolfram Sang9bccc702015-07-17 14:48:56 +02002263 /* Check address availability (7 bit, no need to encode flags) */
Jean Delvare3b5f7942010-06-03 11:33:55 +02002264 if (i2c_check_addr_busy(adap, addr_list[i])) {
Andy Shevchenkob93d3d32016-08-25 17:42:10 +03002265 dev_dbg(&adap->dev,
2266 "Address 0x%02x already in use, not probing\n",
2267 addr_list[i]);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002268 continue;
2269 }
2270
Jean Delvare63e4e802010-06-03 11:33:51 +02002271 /* Test address responsiveness */
Jean Delvare9a942412010-08-11 18:20:56 +02002272 if (probe(adap, addr_list[i]))
Jean Delvare63e4e802010-06-03 11:33:51 +02002273 break;
Jean Delvare12b5053a2007-05-01 23:26:31 +02002274 }
Jean Delvare12b5053a2007-05-01 23:26:31 +02002275
2276 if (addr_list[i] == I2C_CLIENT_END) {
2277 dev_dbg(&adap->dev, "Probing failed, no device found\n");
Wolfram Sangc1d084752019-11-06 10:50:19 +01002278 return ERR_PTR(-ENODEV);
Jean Delvare12b5053a2007-05-01 23:26:31 +02002279 }
2280
2281 info->addr = addr_list[i];
Wolfram Sangc1d084752019-11-06 10:50:19 +01002282 return i2c_new_client_device(adap, info);
2283}
2284EXPORT_SYMBOL_GPL(i2c_new_scanned_device);
2285
Jean Delvared735b342011-03-20 14:50:52 +01002286struct i2c_adapter *i2c_get_adapter(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 struct i2c_adapter *adapter;
David Brownell438d6c22006-12-10 21:21:31 +01002289
Jean Delvarecaada322008-01-27 18:14:49 +01002290 mutex_lock(&core_lock);
Jean Delvared735b342011-03-20 14:50:52 +01002291 adapter = idr_find(&i2c_adapter_idr, nr);
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002292 if (!adapter)
2293 goto exit;
2294
2295 if (try_module_get(adapter->owner))
2296 get_device(&adapter->dev);
2297 else
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002298 adapter = NULL;
2299
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002300 exit:
Jean Delvarecaada322008-01-27 18:14:49 +01002301 mutex_unlock(&core_lock);
Mark M. Hoffmana0920e12005-06-28 00:21:30 -04002302 return adapter;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303}
David Brownellc0564602007-05-01 23:26:31 +02002304EXPORT_SYMBOL(i2c_get_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306void i2c_put_adapter(struct i2c_adapter *adap)
2307{
Vladimir Zapolskiy611e12e2015-07-27 17:30:49 +03002308 if (!adap)
2309 return;
2310
2311 put_device(&adap->dev);
2312 module_put(adap->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
David Brownellc0564602007-05-01 23:26:31 +02002314EXPORT_SYMBOL(i2c_put_adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002316/**
2317 * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg
2318 * @msg: the message to be checked
Wolfram Sangbf263c32019-03-12 13:44:42 +01002319 * @threshold: the minimum number of bytes for which using DMA makes sense.
2320 * Should at least be 1.
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002321 *
2322 * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.
2323 * Or a valid pointer to be used with DMA. After use, release it by
Stephen Boyd34d1b822018-10-08 07:55:20 -07002324 * calling i2c_put_dma_safe_msg_buf().
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002325 *
2326 * This function must only be called from process context!
2327 */
2328u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)
2329{
Wolfram Sangbf263c32019-03-12 13:44:42 +01002330 /* also skip 0-length msgs for bogus thresholds of 0 */
2331 if (!threshold)
2332 pr_debug("DMA buffer for addr=0x%02x with length 0 is bogus\n",
2333 msg->addr);
2334 if (msg->len < threshold || msg->len == 0)
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002335 return NULL;
2336
2337 if (msg->flags & I2C_M_DMA_SAFE)
2338 return msg->buf;
2339
2340 pr_debug("using bounce buffer for addr=0x%02x, len=%d\n",
2341 msg->addr, msg->len);
2342
2343 if (msg->flags & I2C_M_RD)
2344 return kzalloc(msg->len, GFP_KERNEL);
2345 else
2346 return kmemdup(msg->buf, msg->len, GFP_KERNEL);
2347}
2348EXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf);
2349
2350/**
Wolfram Sang82fe39a2018-08-24 16:52:44 +02002351 * i2c_put_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002352 * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.
Wolfram Sang82fe39a2018-08-24 16:52:44 +02002353 * @msg: the message which the buffer corresponds to
2354 * @xferred: bool saying if the message was transferred
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002355 */
Wolfram Sang82fe39a2018-08-24 16:52:44 +02002356void i2c_put_dma_safe_msg_buf(u8 *buf, struct i2c_msg *msg, bool xferred)
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002357{
2358 if (!buf || buf == msg->buf)
2359 return;
2360
Wolfram Sang82fe39a2018-08-24 16:52:44 +02002361 if (xferred && msg->flags & I2C_M_RD)
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002362 memcpy(msg->buf, buf, msg->len);
2363
2364 kfree(buf);
2365}
Wolfram Sang82fe39a2018-08-24 16:52:44 +02002366EXPORT_SYMBOL_GPL(i2c_put_dma_safe_msg_buf);
Wolfram Sange94bc5d2017-11-04 21:20:02 +01002367
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2369MODULE_DESCRIPTION("I2C-Bus main module");
2370MODULE_LICENSE("GPL");