hc
2024-10-12 a5969cabbb4660eab42b6ef0412cbbd1200cf14d
kernel/drivers/i2c/i2c-core-base.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Linux I2C core
34 *
....@@ -6,16 +7,7 @@
67 * Mux support by Rodolfo Giometti <giometti@enneenne.com> and
78 * Michael Lawnick <michael.lawnick.ext@nsn.com>
89 *
9
- * Copyright (C) 2013-2017 Wolfram Sang <wsa@the-dreams.de>
10
- *
11
- * This program is free software; you can redistribute it and/or modify it
12
- * under the terms of the GNU General Public License as published by the Free
13
- * Software Foundation; either version 2 of the License, or (at your option)
14
- * any later version.
15
- *
16
- * This program is distributed in the hope that it will be useful, but WITHOUT
17
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
10
+ * Copyright (C) 2013-2017 Wolfram Sang <wsa@kernel.org>
1911 */
2012
2113 #define pr_fmt(fmt) "i2c-core: " fmt
....@@ -41,6 +33,7 @@
4133 #include <linux/of_device.h>
4234 #include <linux/of.h>
4335 #include <linux/of_irq.h>
36
+#include <linux/pinctrl/consumer.h>
4437 #include <linux/pm_domain.h>
4538 #include <linux/pm_runtime.h>
4639 #include <linux/pm_wakeirq.h>
....@@ -191,6 +184,8 @@
191184
192185 if (bri->prepare_recovery)
193186 bri->prepare_recovery(adap);
187
+ if (bri->pinctrl)
188
+ pinctrl_select_state(bri->pinctrl, bri->pins_gpio);
194189
195190 /*
196191 * If we can set SDA, we will always create a STOP to ensure additional
....@@ -246,6 +241,8 @@
246241
247242 if (bri->unprepare_recovery)
248243 bri->unprepare_recovery(adap);
244
+ if (bri->pinctrl)
245
+ pinctrl_select_state(bri->pinctrl, bri->pins_default);
249246
250247 return ret;
251248 }
....@@ -261,13 +258,135 @@
261258 }
262259 EXPORT_SYMBOL_GPL(i2c_recover_bus);
263260
264
-static void i2c_init_recovery(struct i2c_adapter *adap)
261
+static void i2c_gpio_init_pinctrl_recovery(struct i2c_adapter *adap)
262
+{
263
+ struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
264
+ struct device *dev = &adap->dev;
265
+ struct pinctrl *p = bri->pinctrl;
266
+
267
+ /*
268
+ * we can't change states without pinctrl, so remove the states if
269
+ * populated
270
+ */
271
+ if (!p) {
272
+ bri->pins_default = NULL;
273
+ bri->pins_gpio = NULL;
274
+ return;
275
+ }
276
+
277
+ if (!bri->pins_default) {
278
+ bri->pins_default = pinctrl_lookup_state(p,
279
+ PINCTRL_STATE_DEFAULT);
280
+ if (IS_ERR(bri->pins_default)) {
281
+ dev_dbg(dev, PINCTRL_STATE_DEFAULT " state not found for GPIO recovery\n");
282
+ bri->pins_default = NULL;
283
+ }
284
+ }
285
+ if (!bri->pins_gpio) {
286
+ bri->pins_gpio = pinctrl_lookup_state(p, "gpio");
287
+ if (IS_ERR(bri->pins_gpio))
288
+ bri->pins_gpio = pinctrl_lookup_state(p, "recovery");
289
+
290
+ if (IS_ERR(bri->pins_gpio)) {
291
+ dev_dbg(dev, "no gpio or recovery state found for GPIO recovery\n");
292
+ bri->pins_gpio = NULL;
293
+ }
294
+ }
295
+
296
+ /* for pinctrl state changes, we need all the information */
297
+ if (bri->pins_default && bri->pins_gpio) {
298
+ dev_info(dev, "using pinctrl states for GPIO recovery");
299
+ } else {
300
+ bri->pinctrl = NULL;
301
+ bri->pins_default = NULL;
302
+ bri->pins_gpio = NULL;
303
+ }
304
+}
305
+
306
+static int i2c_gpio_init_generic_recovery(struct i2c_adapter *adap)
307
+{
308
+ struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
309
+ struct device *dev = &adap->dev;
310
+ struct gpio_desc *gpiod;
311
+ int ret = 0;
312
+
313
+ /*
314
+ * don't touch the recovery information if the driver is not using
315
+ * generic SCL recovery
316
+ */
317
+ if (bri->recover_bus && bri->recover_bus != i2c_generic_scl_recovery)
318
+ return 0;
319
+
320
+ /*
321
+ * pins might be taken as GPIO, so we should inform pinctrl about
322
+ * this and move the state to GPIO
323
+ */
324
+ if (bri->pinctrl)
325
+ pinctrl_select_state(bri->pinctrl, bri->pins_gpio);
326
+
327
+ /*
328
+ * if there is incomplete or no recovery information, see if generic
329
+ * GPIO recovery is available
330
+ */
331
+ if (!bri->scl_gpiod) {
332
+ gpiod = devm_gpiod_get(dev, "scl", GPIOD_OUT_HIGH_OPEN_DRAIN);
333
+ if (PTR_ERR(gpiod) == -EPROBE_DEFER) {
334
+ ret = -EPROBE_DEFER;
335
+ goto cleanup_pinctrl_state;
336
+ }
337
+ if (!IS_ERR(gpiod)) {
338
+ bri->scl_gpiod = gpiod;
339
+ bri->recover_bus = i2c_generic_scl_recovery;
340
+ dev_info(dev, "using generic GPIOs for recovery\n");
341
+ }
342
+ }
343
+
344
+ /* SDA GPIOD line is optional, so we care about DEFER only */
345
+ if (!bri->sda_gpiod) {
346
+ /*
347
+ * We have SCL. Pull SCL low and wait a bit so that SDA glitches
348
+ * have no effect.
349
+ */
350
+ gpiod_direction_output(bri->scl_gpiod, 0);
351
+ udelay(10);
352
+ gpiod = devm_gpiod_get(dev, "sda", GPIOD_IN);
353
+
354
+ /* Wait a bit in case of a SDA glitch, and then release SCL. */
355
+ udelay(10);
356
+ gpiod_direction_output(bri->scl_gpiod, 1);
357
+
358
+ if (PTR_ERR(gpiod) == -EPROBE_DEFER) {
359
+ ret = -EPROBE_DEFER;
360
+ goto cleanup_pinctrl_state;
361
+ }
362
+ if (!IS_ERR(gpiod))
363
+ bri->sda_gpiod = gpiod;
364
+ }
365
+
366
+cleanup_pinctrl_state:
367
+ /* change the state of the pins back to their default state */
368
+ if (bri->pinctrl)
369
+ pinctrl_select_state(bri->pinctrl, bri->pins_default);
370
+
371
+ return ret;
372
+}
373
+
374
+static int i2c_gpio_init_recovery(struct i2c_adapter *adap)
375
+{
376
+ i2c_gpio_init_pinctrl_recovery(adap);
377
+ return i2c_gpio_init_generic_recovery(adap);
378
+}
379
+
380
+static int i2c_init_recovery(struct i2c_adapter *adap)
265381 {
266382 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
267383 char *err_str, *err_level = KERN_ERR;
268384
269385 if (!bri)
270
- return;
386
+ return 0;
387
+
388
+ if (i2c_gpio_init_recovery(adap) == -EPROBE_DEFER)
389
+ return -EPROBE_DEFER;
271390
272391 if (!bri->recover_bus) {
273392 err_str = "no suitable method provided";
....@@ -284,10 +403,7 @@
284403 if (gpiod_get_direction(bri->sda_gpiod) == 0)
285404 bri->set_sda = set_sda_gpio_value;
286405 }
287
- return;
288
- }
289
-
290
- if (bri->recover_bus == i2c_generic_scl_recovery) {
406
+ } else if (bri->recover_bus == i2c_generic_scl_recovery) {
291407 /* Generic SCL recovery */
292408 if (!bri->set_scl || !bri->get_scl) {
293409 err_str = "no {get|set}_scl() found";
....@@ -299,10 +415,12 @@
299415 }
300416 }
301417
302
- return;
418
+ return 0;
303419 err:
304420 dev_printk(err_level, &adap->dev, "Not using recovery: %s\n", err_str);
305421 adap->bus_recovery_info = NULL;
422
+
423
+ return -EINVAL;
306424 }
307425
308426 static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
....@@ -330,9 +448,9 @@
330448 if (!client)
331449 return 0;
332450
333
- driver = to_i2c_driver(dev->driver);
451
+ client->irq = client->init_irq;
334452
335
- if (!client->irq && !driver->disable_i2c_core_irq_mapping) {
453
+ if (!client->irq) {
336454 int irq = -ENOENT;
337455
338456 if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
....@@ -345,10 +463,12 @@
345463 if (irq == -EINVAL || irq == -ENODATA)
346464 irq = of_irq_get(dev->of_node, 0);
347465 } else if (ACPI_COMPANION(dev)) {
348
- irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0);
466
+ irq = i2c_acpi_get_irq(client);
349467 }
350
- if (irq == -EPROBE_DEFER)
351
- return irq;
468
+ if (irq == -EPROBE_DEFER) {
469
+ status = irq;
470
+ goto put_sync_adapter;
471
+ }
352472
353473 if (irq < 0)
354474 irq = 0;
....@@ -356,22 +476,26 @@
356476 client->irq = irq;
357477 }
358478
479
+ driver = to_i2c_driver(dev->driver);
480
+
359481 /*
360482 * An I2C ID table is not mandatory, if and only if, a suitable OF
361483 * or ACPI ID table is supplied for the probing device.
362484 */
363485 if (!driver->id_table &&
364
- !i2c_acpi_match_device(dev->driver->acpi_match_table, client) &&
365
- !i2c_of_match_device(dev->driver->of_match_table, client))
366
- return -ENODEV;
486
+ !acpi_driver_match_device(dev, dev->driver) &&
487
+ !i2c_of_match_device(dev->driver->of_match_table, client)) {
488
+ status = -ENODEV;
489
+ goto put_sync_adapter;
490
+ }
367491
368492 if (client->flags & I2C_CLIENT_WAKE) {
369
- int wakeirq = -ENOENT;
493
+ int wakeirq;
370494
371
- if (dev->of_node) {
372
- wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
373
- if (wakeirq == -EPROBE_DEFER)
374
- return wakeirq;
495
+ wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
496
+ if (wakeirq == -EPROBE_DEFER) {
497
+ status = wakeirq;
498
+ goto put_sync_adapter;
375499 }
376500
377501 device_init_wakeup(&client->dev, true);
....@@ -419,6 +543,10 @@
419543 err_clear_wakeup_irq:
420544 dev_pm_clear_wake_irq(&client->dev);
421545 device_init_wakeup(&client->dev, false);
546
+put_sync_adapter:
547
+ if (client->flags & I2C_CLIENT_HOST_NOTIFY)
548
+ pm_runtime_put_sync(&client->adapter->dev);
549
+
422550 return status;
423551 }
424552
....@@ -442,7 +570,7 @@
442570 dev_pm_clear_wake_irq(&client->dev);
443571 device_init_wakeup(&client->dev, false);
444572
445
- client->irq = client->init_irq;
573
+ client->irq = 0;
446574 if (client->flags & I2C_CLIENT_HOST_NOTIFY)
447575 pm_runtime_put(&client->adapter->dev);
448576
....@@ -469,15 +597,15 @@
469597 }
470598
471599 static ssize_t
472
-show_name(struct device *dev, struct device_attribute *attr, char *buf)
600
+name_show(struct device *dev, struct device_attribute *attr, char *buf)
473601 {
474602 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
475603 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
476604 }
477
-static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
605
+static DEVICE_ATTR_RO(name);
478606
479607 static ssize_t
480
-show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
608
+modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
481609 {
482610 struct i2c_client *client = to_i2c_client(dev);
483611 int len;
....@@ -492,7 +620,7 @@
492620
493621 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
494622 }
495
-static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
623
+static DEVICE_ATTR_RO(modalias);
496624
497625 static struct attribute *i2c_dev_attrs[] = {
498626 &dev_attr_name.attr,
....@@ -704,8 +832,8 @@
704832 i2c_encode_flags_to_addr(client), status);
705833 }
706834
707
-static int i2c_dev_irq_from_resources(const struct resource *resources,
708
- unsigned int num_resources)
835
+int i2c_dev_irq_from_resources(const struct resource *resources,
836
+ unsigned int num_resources)
709837 {
710838 struct irq_data *irqd;
711839 int i;
....@@ -731,7 +859,7 @@
731859 }
732860
733861 /**
734
- * i2c_new_device - instantiate an i2c device
862
+ * i2c_new_client_device - instantiate an i2c device
735863 * @adap: the adapter managing the device
736864 * @info: describes one I2C device; bus_num is ignored
737865 * Context: can sleep
....@@ -744,17 +872,17 @@
744872 * before any i2c_adapter could exist.
745873 *
746874 * This returns the new i2c client, which may be saved for later use with
747
- * i2c_unregister_device(); or NULL to indicate an error.
875
+ * i2c_unregister_device(); or an ERR_PTR to describe the error.
748876 */
749877 struct i2c_client *
750
-i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
878
+i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
751879 {
752880 struct i2c_client *client;
753881 int status;
754882
755883 client = kzalloc(sizeof *client, GFP_KERNEL);
756884 if (!client)
757
- return NULL;
885
+ return ERR_PTR(-ENOMEM);
758886
759887 client->adapter = adap;
760888
....@@ -766,7 +894,6 @@
766894 if (!client->init_irq)
767895 client->init_irq = i2c_dev_irq_from_resources(info->resources,
768896 info->num_resources);
769
- client->irq = client->init_irq;
770897
771898 strlcpy(client->name, info->type, sizeof(client->name));
772899
....@@ -818,19 +945,18 @@
818945 of_node_put(info->of_node);
819946 out_err_silent:
820947 kfree(client);
821
- return NULL;
948
+ return ERR_PTR(status);
822949 }
823
-EXPORT_SYMBOL_GPL(i2c_new_device);
824
-
950
+EXPORT_SYMBOL_GPL(i2c_new_client_device);
825951
826952 /**
827
- * i2c_unregister_device - reverse effect of i2c_new_device()
828
- * @client: value returned from i2c_new_device()
953
+ * i2c_unregister_device - reverse effect of i2c_new_*_device()
954
+ * @client: value returned from i2c_new_*_device()
829955 * Context: can sleep
830956 */
831957 void i2c_unregister_device(struct i2c_client *client)
832958 {
833
- if (!client)
959
+ if (IS_ERR_OR_NULL(client))
834960 return;
835961
836962 if (client->dev.of_node) {
....@@ -869,7 +995,7 @@
869995 };
870996
871997 /**
872
- * i2c_new_dummy - return a new i2c device bound to a dummy driver
998
+ * i2c_new_dummy_device - return a new i2c device bound to a dummy driver
873999 * @adapter: the adapter managing the device
8741000 * @address: seven bit address to be used
8751001 * Context: can sleep
....@@ -884,20 +1010,64 @@
8841010 * different driver.
8851011 *
8861012 * This returns the new i2c client, which should be saved for later use with
887
- * i2c_unregister_device(); or NULL to indicate an error.
1013
+ * i2c_unregister_device(); or an ERR_PTR to describe the error.
8881014 */
889
-struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
1015
+struct i2c_client *i2c_new_dummy_device(struct i2c_adapter *adapter, u16 address)
8901016 {
8911017 struct i2c_board_info info = {
8921018 I2C_BOARD_INFO("dummy", address),
8931019 };
8941020
895
- return i2c_new_device(adapter, &info);
1021
+ return i2c_new_client_device(adapter, &info);
8961022 }
897
-EXPORT_SYMBOL_GPL(i2c_new_dummy);
1023
+EXPORT_SYMBOL_GPL(i2c_new_dummy_device);
1024
+
1025
+struct i2c_dummy_devres {
1026
+ struct i2c_client *client;
1027
+};
1028
+
1029
+static void devm_i2c_release_dummy(struct device *dev, void *res)
1030
+{
1031
+ struct i2c_dummy_devres *this = res;
1032
+
1033
+ i2c_unregister_device(this->client);
1034
+}
8981035
8991036 /**
900
- * i2c_new_secondary_device - Helper to get the instantiated secondary address
1037
+ * devm_i2c_new_dummy_device - return a new i2c device bound to a dummy driver
1038
+ * @dev: device the managed resource is bound to
1039
+ * @adapter: the adapter managing the device
1040
+ * @address: seven bit address to be used
1041
+ * Context: can sleep
1042
+ *
1043
+ * This is the device-managed version of @i2c_new_dummy_device. It returns the
1044
+ * new i2c client or an ERR_PTR in case of an error.
1045
+ */
1046
+struct i2c_client *devm_i2c_new_dummy_device(struct device *dev,
1047
+ struct i2c_adapter *adapter,
1048
+ u16 address)
1049
+{
1050
+ struct i2c_dummy_devres *dr;
1051
+ struct i2c_client *client;
1052
+
1053
+ dr = devres_alloc(devm_i2c_release_dummy, sizeof(*dr), GFP_KERNEL);
1054
+ if (!dr)
1055
+ return ERR_PTR(-ENOMEM);
1056
+
1057
+ client = i2c_new_dummy_device(adapter, address);
1058
+ if (IS_ERR(client)) {
1059
+ devres_free(dr);
1060
+ } else {
1061
+ dr->client = client;
1062
+ devres_add(dev, dr);
1063
+ }
1064
+
1065
+ return client;
1066
+}
1067
+EXPORT_SYMBOL_GPL(devm_i2c_new_dummy_device);
1068
+
1069
+/**
1070
+ * i2c_new_ancillary_device - Helper to get the instantiated secondary address
9011071 * and create the associated device
9021072 * @client: Handle to the primary client
9031073 * @name: Handle to specify which secondary address to get
....@@ -916,9 +1086,9 @@
9161086 * cell whose "reg-names" value matches the slave name.
9171087 *
9181088 * This returns the new i2c client, which should be saved for later use with
919
- * i2c_unregister_device(); or NULL to indicate an error.
1089
+ * i2c_unregister_device(); or an ERR_PTR to describe the error.
9201090 */
921
-struct i2c_client *i2c_new_secondary_device(struct i2c_client *client,
1091
+struct i2c_client *i2c_new_ancillary_device(struct i2c_client *client,
9221092 const char *name,
9231093 u16 default_addr)
9241094 {
....@@ -933,9 +1103,9 @@
9331103 }
9341104
9351105 dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
936
- return i2c_new_dummy(client->adapter, addr);
1106
+ return i2c_new_dummy_device(client->adapter, addr);
9371107 }
938
-EXPORT_SYMBOL_GPL(i2c_new_secondary_device);
1108
+EXPORT_SYMBOL_GPL(i2c_new_ancillary_device);
9391109
9401110 /* ------------------------------------------------------------------------- */
9411111
....@@ -972,8 +1142,8 @@
9721142 * the user to provide incorrect parameters.
9731143 */
9741144 static ssize_t
975
-i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
976
- const char *buf, size_t count)
1145
+new_device_store(struct device *dev, struct device_attribute *attr,
1146
+ const char *buf, size_t count)
9771147 {
9781148 struct i2c_adapter *adap = to_i2c_adapter(dev);
9791149 struct i2c_board_info info;
....@@ -1015,9 +1185,9 @@
10151185 info.flags |= I2C_CLIENT_SLAVE;
10161186 }
10171187
1018
- client = i2c_new_device(adap, &info);
1019
- if (!client)
1020
- return -EINVAL;
1188
+ client = i2c_new_client_device(adap, &info);
1189
+ if (IS_ERR(client))
1190
+ return PTR_ERR(client);
10211191
10221192 /* Keep track of the added device */
10231193 mutex_lock(&adap->userspace_clients_lock);
....@@ -1028,7 +1198,7 @@
10281198
10291199 return count;
10301200 }
1031
-static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
1201
+static DEVICE_ATTR_WO(new_device);
10321202
10331203 /*
10341204 * And of course let the users delete the devices they instantiated, if
....@@ -1040,8 +1210,8 @@
10401210 * the user to delete the wrong device.
10411211 */
10421212 static ssize_t
1043
-i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
1044
- const char *buf, size_t count)
1213
+delete_device_store(struct device *dev, struct device_attribute *attr,
1214
+ const char *buf, size_t count)
10451215 {
10461216 struct i2c_adapter *adap = to_i2c_adapter(dev);
10471217 struct i2c_client *client, *next;
....@@ -1084,7 +1254,7 @@
10841254 return res;
10851255 }
10861256 static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
1087
- i2c_sysfs_delete_device);
1257
+ delete_device_store);
10881258
10891259 static struct attribute *i2c_adapter_attrs[] = {
10901260 &dev_attr_name.attr,
....@@ -1127,9 +1297,8 @@
11271297
11281298 down_read(&__i2c_board_lock);
11291299 list_for_each_entry(devinfo, &__i2c_board_list, list) {
1130
- if (devinfo->busnum == adapter->nr
1131
- && !i2c_new_device(adapter,
1132
- &devinfo->board_info))
1300
+ if (devinfo->busnum == adapter->nr &&
1301
+ IS_ERR(i2c_new_client_device(adapter, &devinfo->board_info)))
11331302 dev_err(&adapter->dev,
11341303 "Can't create device at 0x%02x\n",
11351304 devinfo->board_info.addr);
....@@ -1192,7 +1361,7 @@
11921361 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
11931362 return 0;
11941363
1195
- domain = irq_domain_create_linear(adap->dev.fwnode,
1364
+ domain = irq_domain_create_linear(adap->dev.parent->fwnode,
11961365 I2C_ADDR_7BITS_COUNT,
11971366 &i2c_host_notify_irq_ops, adap);
11981367 if (!domain)
....@@ -1252,6 +1421,7 @@
12521421 if (!adap->lock_ops)
12531422 adap->lock_ops = &i2c_adapter_lock_ops;
12541423
1424
+ adap->locked_flags = 0;
12551425 rt_mutex_init(&adap->bus_lock);
12561426 rt_mutex_init(&adap->mux_lock);
12571427 mutex_init(&adap->userspace_clients_lock);
....@@ -1282,11 +1452,15 @@
12821452 if (res)
12831453 goto out_reg;
12841454
1285
- dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1286
-
12871455 pm_runtime_no_callbacks(&adap->dev);
12881456 pm_suspend_ignore_children(&adap->dev, true);
12891457 pm_runtime_enable(&adap->dev);
1458
+
1459
+ res = i2c_init_recovery(adap);
1460
+ if (res == -EPROBE_DEFER)
1461
+ goto out_reg;
1462
+
1463
+ dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
12901464
12911465 #ifdef CONFIG_I2C_COMPAT
12921466 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
....@@ -1295,8 +1469,6 @@
12951469 dev_warn(&adap->dev,
12961470 "Failed to create compatibility class link\n");
12971471 #endif
1298
-
1299
- i2c_init_recovery(adap);
13001472
13011473 /* create pre-declared device nodes */
13021474 of_i2c_register_devices(adap);
....@@ -1537,63 +1709,64 @@
15371709 }
15381710 EXPORT_SYMBOL(i2c_del_adapter);
15391711
1712
+static void i2c_parse_timing(struct device *dev, char *prop_name, u32 *cur_val_p,
1713
+ u32 def_val, bool use_def)
1714
+{
1715
+ int ret;
1716
+
1717
+ ret = device_property_read_u32(dev, prop_name, cur_val_p);
1718
+ if (ret && use_def)
1719
+ *cur_val_p = def_val;
1720
+
1721
+ dev_dbg(dev, "%s: %u\n", prop_name, *cur_val_p);
1722
+}
1723
+
15401724 /**
15411725 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
15421726 * @dev: The device to scan for I2C timing properties
15431727 * @t: the i2c_timings struct to be filled with values
15441728 * @use_defaults: bool to use sane defaults derived from the I2C specification
1545
- * when properties are not found, otherwise use 0
1729
+ * when properties are not found, otherwise don't update
15461730 *
15471731 * Scan the device for the generic I2C properties describing timing parameters
15481732 * for the signal and fill the given struct with the results. If a property was
15491733 * not found and use_defaults was true, then maximum timings are assumed which
15501734 * are derived from the I2C specification. If use_defaults is not used, the
1551
- * results will be 0, so drivers can apply their own defaults later. The latter
1552
- * is mainly intended for avoiding regressions of existing drivers which want
1553
- * to switch to this function. New drivers almost always should use the defaults.
1735
+ * results will be as before, so drivers can apply their own defaults before
1736
+ * calling this helper. The latter is mainly intended for avoiding regressions
1737
+ * of existing drivers which want to switch to this function. New drivers
1738
+ * almost always should use the defaults.
15541739 */
1555
-
15561740 void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
15571741 {
1558
- int ret;
1742
+ bool u = use_defaults;
1743
+ u32 d;
15591744
1560
- memset(t, 0, sizeof(*t));
1745
+ i2c_parse_timing(dev, "clock-frequency", &t->bus_freq_hz,
1746
+ I2C_MAX_STANDARD_MODE_FREQ, u);
15611747
1562
- ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
1563
- if (ret && use_defaults)
1564
- t->bus_freq_hz = 100000;
1748
+ d = t->bus_freq_hz <= I2C_MAX_STANDARD_MODE_FREQ ? 1000 :
1749
+ t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ ? 300 : 120;
1750
+ i2c_parse_timing(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns, d, u);
15651751
1566
- ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
1567
- if (ret && use_defaults) {
1568
- if (t->bus_freq_hz <= 100000)
1569
- t->scl_rise_ns = 1000;
1570
- else if (t->bus_freq_hz <= 400000)
1571
- t->scl_rise_ns = 300;
1572
- else
1573
- t->scl_rise_ns = 120;
1574
- }
1752
+ d = t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ ? 300 : 120;
1753
+ i2c_parse_timing(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns, d, u);
15751754
1576
- ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
1577
- if (ret && use_defaults) {
1578
- if (t->bus_freq_hz <= 400000)
1579
- t->scl_fall_ns = 300;
1580
- else
1581
- t->scl_fall_ns = 120;
1582
- }
1583
-
1584
- device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
1585
-
1586
- ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
1587
- if (ret && use_defaults)
1588
- t->sda_fall_ns = t->scl_fall_ns;
1589
-
1590
- device_property_read_u32(dev, "i2c-sda-hold-time-ns", &t->sda_hold_ns);
1755
+ i2c_parse_timing(dev, "i2c-scl-internal-delay-ns",
1756
+ &t->scl_int_delay_ns, 0, u);
1757
+ i2c_parse_timing(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns,
1758
+ t->scl_fall_ns, u);
1759
+ i2c_parse_timing(dev, "i2c-sda-hold-time-ns", &t->sda_hold_ns, 0, u);
1760
+ i2c_parse_timing(dev, "i2c-digital-filter-width-ns",
1761
+ &t->digital_filter_width_ns, 0, u);
1762
+ i2c_parse_timing(dev, "i2c-analog-filter-cutoff-frequency",
1763
+ &t->analog_filter_cutoff_freq_hz, 0, u);
15911764 }
15921765 EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
15931766
15941767 /* ------------------------------------------------------------------------- */
15951768
1596
-int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1769
+int i2c_for_each_dev(void *data, int (*fn)(struct device *dev, void *data))
15971770 {
15981771 int res;
15991772
....@@ -1695,38 +1868,6 @@
16951868 device_for_each_child(&adapter->dev, &addrinfo, __i2c_check_addr_ex);
16961869 return addrinfo.cnt;
16971870 }
1698
-
1699
-/**
1700
- * i2c_use_client - increments the reference count of the i2c client structure
1701
- * @client: the client being referenced
1702
- *
1703
- * Each live reference to a client should be refcounted. The driver model does
1704
- * that automatically as part of driver binding, so that most drivers don't
1705
- * need to do this explicitly: they hold a reference until they're unbound
1706
- * from the device.
1707
- *
1708
- * A pointer to the client with the incremented reference counter is returned.
1709
- */
1710
-struct i2c_client *i2c_use_client(struct i2c_client *client)
1711
-{
1712
- if (client && get_device(&client->dev))
1713
- return client;
1714
- return NULL;
1715
-}
1716
-EXPORT_SYMBOL(i2c_use_client);
1717
-
1718
-/**
1719
- * i2c_release_client - release a use of the i2c client structure
1720
- * @client: the client being no longer referenced
1721
- *
1722
- * Must be called when a user of a client is finished with it.
1723
- */
1724
-void i2c_release_client(struct i2c_client *client)
1725
-{
1726
- if (client)
1727
- put_device(&client->dev);
1728
-}
1729
-EXPORT_SYMBOL(i2c_release_client);
17301871
17311872 struct i2c_cmd_arg {
17321873 unsigned cmd;
....@@ -1913,6 +2054,10 @@
19132054 if (WARN_ON(!msgs || num < 1))
19142055 return -EINVAL;
19152056
2057
+ ret = __i2c_check_suspended(adap);
2058
+ if (ret)
2059
+ return ret;
2060
+
19162061 if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
19172062 return -EOPNOTSUPP;
19182063
....@@ -1933,7 +2078,11 @@
19332078 /* Retry automatically on arbitration loss */
19342079 orig_jiffies = jiffies;
19352080 for (ret = 0, try = 0; try <= adap->retries; try++) {
1936
- ret = adap->algo->master_xfer(adap, msgs, num);
2081
+ if (i2c_in_atomic_xfer_mode() && adap->algo->master_xfer_atomic)
2082
+ ret = adap->algo->master_xfer_atomic(adap, msgs, num);
2083
+ else
2084
+ ret = adap->algo->master_xfer(adap, msgs, num);
2085
+
19372086 if (ret != -EAGAIN)
19382087 break;
19392088 if (time_after(jiffies, orig_jiffies + adap->timeout))
....@@ -1968,6 +2117,11 @@
19682117 {
19692118 int ret;
19702119
2120
+ if (!adap->algo->master_xfer) {
2121
+ dev_dbg(&adap->dev, "I2C level transfers not supported\n");
2122
+ return -EOPNOTSUPP;
2123
+ }
2124
+
19712125 /* REVISIT the fault reporting model here is weak:
19722126 *
19732127 * - When we get an error after receiving N bytes from a slave,
....@@ -1984,35 +2138,14 @@
19842138 * one (discarding status on the second message) or errno
19852139 * (discarding status on the first one).
19862140 */
1987
-
1988
- if (adap->algo->master_xfer) {
1989
-#ifdef DEBUG
1990
- for (ret = 0; ret < num; ret++) {
1991
- dev_dbg(&adap->dev,
1992
- "master_xfer[%d] %c, addr=0x%02x, len=%d%s\n",
1993
- ret, (msgs[ret].flags & I2C_M_RD) ? 'R' : 'W',
1994
- msgs[ret].addr, msgs[ret].len,
1995
- (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1996
- }
1997
-#endif
1998
-
1999
- if (in_atomic() || irqs_disabled()) {
2000
- ret = i2c_trylock_bus(adap, I2C_LOCK_SEGMENT);
2001
- if (!ret)
2002
- /* I2C activity is ongoing. */
2003
- return -EAGAIN;
2004
- } else {
2005
- i2c_lock_bus(adap, I2C_LOCK_SEGMENT);
2006
- }
2007
-
2008
- ret = __i2c_transfer(adap, msgs, num);
2009
- i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
2010
-
2141
+ ret = __i2c_lock_bus_helper(adap);
2142
+ if (ret)
20112143 return ret;
2012
- } else {
2013
- dev_dbg(&adap->dev, "I2C level transfers not supported\n");
2014
- return -EOPNOTSUPP;
2015
- }
2144
+
2145
+ ret = __i2c_transfer(adap, msgs, num);
2146
+ i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
2147
+
2148
+ return ret;
20162149 }
20172150 EXPORT_SYMBOL(i2c_transfer);
20182151
....@@ -2171,13 +2304,13 @@
21712304 dev_warn(&adapter->dev,
21722305 "This adapter will soon drop class based instantiation of devices. "
21732306 "Please make sure client 0x%02x gets instantiated by other means. "
2174
- "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2307
+ "Check 'Documentation/i2c/instantiating-devices.rst' for details.\n",
21752308 info.addr);
21762309
21772310 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
21782311 info.type, info.addr);
2179
- client = i2c_new_device(adapter, &info);
2180
- if (client)
2312
+ client = i2c_new_client_device(adapter, &info);
2313
+ if (!IS_ERR(client))
21812314 list_add_tail(&client->detected, &driver->clients);
21822315 else
21832316 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
....@@ -2191,7 +2324,6 @@
21912324 const unsigned short *address_list;
21922325 struct i2c_client *temp_client;
21932326 int i, err = 0;
2194
- int adap_id = i2c_adapter_id(adapter);
21952327
21962328 address_list = driver->address_list;
21972329 if (!driver->detect || !address_list)
....@@ -2201,7 +2333,7 @@
22012333 if (adapter->class == I2C_CLASS_DEPRECATED) {
22022334 dev_dbg(&adapter->dev,
22032335 "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
2204
- "If you need it, check 'Documentation/i2c/instantiating-devices' for alternatives.\n",
2336
+ "If you need it, check 'Documentation/i2c/instantiating-devices.rst' for alternatives.\n",
22052337 driver->driver.name);
22062338 return 0;
22072339 }
....@@ -2219,7 +2351,7 @@
22192351 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
22202352 dev_dbg(&adapter->dev,
22212353 "found normal entry for adapter %d, addr 0x%02x\n",
2222
- adap_id, address_list[i]);
2354
+ i2c_adapter_id(adapter), address_list[i]);
22232355 temp_client->addr = address_list[i];
22242356 err = i2c_detect_address(temp_client, driver);
22252357 if (unlikely(err))
....@@ -2238,10 +2370,10 @@
22382370 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
22392371
22402372 struct i2c_client *
2241
-i2c_new_probed_device(struct i2c_adapter *adap,
2242
- struct i2c_board_info *info,
2243
- unsigned short const *addr_list,
2244
- int (*probe)(struct i2c_adapter *, unsigned short addr))
2373
+i2c_new_scanned_device(struct i2c_adapter *adap,
2374
+ struct i2c_board_info *info,
2375
+ unsigned short const *addr_list,
2376
+ int (*probe)(struct i2c_adapter *adap, unsigned short addr))
22452377 {
22462378 int i;
22472379
....@@ -2271,13 +2403,13 @@
22712403
22722404 if (addr_list[i] == I2C_CLIENT_END) {
22732405 dev_dbg(&adap->dev, "Probing failed, no device found\n");
2274
- return NULL;
2406
+ return ERR_PTR(-ENODEV);
22752407 }
22762408
22772409 info->addr = addr_list[i];
2278
- return i2c_new_device(adap, info);
2410
+ return i2c_new_client_device(adap, info);
22792411 }
2280
-EXPORT_SYMBOL_GPL(i2c_new_probed_device);
2412
+EXPORT_SYMBOL_GPL(i2c_new_scanned_device);
22812413
22822414 struct i2c_adapter *i2c_get_adapter(int nr)
22832415 {
....@@ -2304,15 +2436,17 @@
23042436 if (!adap)
23052437 return;
23062438
2307
- put_device(&adap->dev);
23082439 module_put(adap->owner);
2440
+ /* Should be last, otherwise we risk use-after-free with 'adap' */
2441
+ put_device(&adap->dev);
23092442 }
23102443 EXPORT_SYMBOL(i2c_put_adapter);
23112444
23122445 /**
23132446 * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg
23142447 * @msg: the message to be checked
2315
- * @threshold: the minimum number of bytes for which using DMA makes sense
2448
+ * @threshold: the minimum number of bytes for which using DMA makes sense.
2449
+ * Should at least be 1.
23162450 *
23172451 * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.
23182452 * Or a valid pointer to be used with DMA. After use, release it by
....@@ -2322,7 +2456,11 @@
23222456 */
23232457 u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)
23242458 {
2325
- if (msg->len < threshold)
2459
+ /* also skip 0-length msgs for bogus thresholds of 0 */
2460
+ if (!threshold)
2461
+ pr_debug("DMA buffer for addr=0x%02x with length 0 is bogus\n",
2462
+ msg->addr);
2463
+ if (msg->len < threshold || msg->len == 0)
23262464 return NULL;
23272465
23282466 if (msg->flags & I2C_M_DMA_SAFE)