.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * Linux I2C core |
---|
3 | 4 | * |
---|
.. | .. |
---|
6 | 7 | * Mux support by Rodolfo Giometti <giometti@enneenne.com> and |
---|
7 | 8 | * Michael Lawnick <michael.lawnick.ext@nsn.com> |
---|
8 | 9 | * |
---|
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> |
---|
19 | 11 | */ |
---|
20 | 12 | |
---|
21 | 13 | #define pr_fmt(fmt) "i2c-core: " fmt |
---|
.. | .. |
---|
41 | 33 | #include <linux/of_device.h> |
---|
42 | 34 | #include <linux/of.h> |
---|
43 | 35 | #include <linux/of_irq.h> |
---|
| 36 | +#include <linux/pinctrl/consumer.h> |
---|
44 | 37 | #include <linux/pm_domain.h> |
---|
45 | 38 | #include <linux/pm_runtime.h> |
---|
46 | 39 | #include <linux/pm_wakeirq.h> |
---|
.. | .. |
---|
191 | 184 | |
---|
192 | 185 | if (bri->prepare_recovery) |
---|
193 | 186 | bri->prepare_recovery(adap); |
---|
| 187 | + if (bri->pinctrl) |
---|
| 188 | + pinctrl_select_state(bri->pinctrl, bri->pins_gpio); |
---|
194 | 189 | |
---|
195 | 190 | /* |
---|
196 | 191 | * If we can set SDA, we will always create a STOP to ensure additional |
---|
.. | .. |
---|
246 | 241 | |
---|
247 | 242 | if (bri->unprepare_recovery) |
---|
248 | 243 | bri->unprepare_recovery(adap); |
---|
| 244 | + if (bri->pinctrl) |
---|
| 245 | + pinctrl_select_state(bri->pinctrl, bri->pins_default); |
---|
249 | 246 | |
---|
250 | 247 | return ret; |
---|
251 | 248 | } |
---|
.. | .. |
---|
261 | 258 | } |
---|
262 | 259 | EXPORT_SYMBOL_GPL(i2c_recover_bus); |
---|
263 | 260 | |
---|
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) |
---|
265 | 381 | { |
---|
266 | 382 | struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; |
---|
267 | 383 | char *err_str, *err_level = KERN_ERR; |
---|
268 | 384 | |
---|
269 | 385 | if (!bri) |
---|
270 | | - return; |
---|
| 386 | + return 0; |
---|
| 387 | + |
---|
| 388 | + if (i2c_gpio_init_recovery(adap) == -EPROBE_DEFER) |
---|
| 389 | + return -EPROBE_DEFER; |
---|
271 | 390 | |
---|
272 | 391 | if (!bri->recover_bus) { |
---|
273 | 392 | err_str = "no suitable method provided"; |
---|
.. | .. |
---|
284 | 403 | if (gpiod_get_direction(bri->sda_gpiod) == 0) |
---|
285 | 404 | bri->set_sda = set_sda_gpio_value; |
---|
286 | 405 | } |
---|
287 | | - return; |
---|
288 | | - } |
---|
289 | | - |
---|
290 | | - if (bri->recover_bus == i2c_generic_scl_recovery) { |
---|
| 406 | + } else if (bri->recover_bus == i2c_generic_scl_recovery) { |
---|
291 | 407 | /* Generic SCL recovery */ |
---|
292 | 408 | if (!bri->set_scl || !bri->get_scl) { |
---|
293 | 409 | err_str = "no {get|set}_scl() found"; |
---|
.. | .. |
---|
299 | 415 | } |
---|
300 | 416 | } |
---|
301 | 417 | |
---|
302 | | - return; |
---|
| 418 | + return 0; |
---|
303 | 419 | err: |
---|
304 | 420 | dev_printk(err_level, &adap->dev, "Not using recovery: %s\n", err_str); |
---|
305 | 421 | adap->bus_recovery_info = NULL; |
---|
| 422 | + |
---|
| 423 | + return -EINVAL; |
---|
306 | 424 | } |
---|
307 | 425 | |
---|
308 | 426 | static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client) |
---|
.. | .. |
---|
330 | 448 | if (!client) |
---|
331 | 449 | return 0; |
---|
332 | 450 | |
---|
333 | | - driver = to_i2c_driver(dev->driver); |
---|
| 451 | + client->irq = client->init_irq; |
---|
334 | 452 | |
---|
335 | | - if (!client->irq && !driver->disable_i2c_core_irq_mapping) { |
---|
| 453 | + if (!client->irq) { |
---|
336 | 454 | int irq = -ENOENT; |
---|
337 | 455 | |
---|
338 | 456 | if (client->flags & I2C_CLIENT_HOST_NOTIFY) { |
---|
.. | .. |
---|
345 | 463 | if (irq == -EINVAL || irq == -ENODATA) |
---|
346 | 464 | irq = of_irq_get(dev->of_node, 0); |
---|
347 | 465 | } else if (ACPI_COMPANION(dev)) { |
---|
348 | | - irq = acpi_dev_gpio_irq_get(ACPI_COMPANION(dev), 0); |
---|
| 466 | + irq = i2c_acpi_get_irq(client); |
---|
349 | 467 | } |
---|
350 | | - if (irq == -EPROBE_DEFER) |
---|
351 | | - return irq; |
---|
| 468 | + if (irq == -EPROBE_DEFER) { |
---|
| 469 | + status = irq; |
---|
| 470 | + goto put_sync_adapter; |
---|
| 471 | + } |
---|
352 | 472 | |
---|
353 | 473 | if (irq < 0) |
---|
354 | 474 | irq = 0; |
---|
.. | .. |
---|
356 | 476 | client->irq = irq; |
---|
357 | 477 | } |
---|
358 | 478 | |
---|
| 479 | + driver = to_i2c_driver(dev->driver); |
---|
| 480 | + |
---|
359 | 481 | /* |
---|
360 | 482 | * An I2C ID table is not mandatory, if and only if, a suitable OF |
---|
361 | 483 | * or ACPI ID table is supplied for the probing device. |
---|
362 | 484 | */ |
---|
363 | 485 | 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 | + } |
---|
367 | 491 | |
---|
368 | 492 | if (client->flags & I2C_CLIENT_WAKE) { |
---|
369 | | - int wakeirq = -ENOENT; |
---|
| 493 | + int wakeirq; |
---|
370 | 494 | |
---|
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; |
---|
375 | 499 | } |
---|
376 | 500 | |
---|
377 | 501 | device_init_wakeup(&client->dev, true); |
---|
.. | .. |
---|
419 | 543 | err_clear_wakeup_irq: |
---|
420 | 544 | dev_pm_clear_wake_irq(&client->dev); |
---|
421 | 545 | 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 | + |
---|
422 | 550 | return status; |
---|
423 | 551 | } |
---|
424 | 552 | |
---|
.. | .. |
---|
442 | 570 | dev_pm_clear_wake_irq(&client->dev); |
---|
443 | 571 | device_init_wakeup(&client->dev, false); |
---|
444 | 572 | |
---|
445 | | - client->irq = client->init_irq; |
---|
| 573 | + client->irq = 0; |
---|
446 | 574 | if (client->flags & I2C_CLIENT_HOST_NOTIFY) |
---|
447 | 575 | pm_runtime_put(&client->adapter->dev); |
---|
448 | 576 | |
---|
.. | .. |
---|
469 | 597 | } |
---|
470 | 598 | |
---|
471 | 599 | 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) |
---|
473 | 601 | { |
---|
474 | 602 | return sprintf(buf, "%s\n", dev->type == &i2c_client_type ? |
---|
475 | 603 | to_i2c_client(dev)->name : to_i2c_adapter(dev)->name); |
---|
476 | 604 | } |
---|
477 | | -static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
---|
| 605 | +static DEVICE_ATTR_RO(name); |
---|
478 | 606 | |
---|
479 | 607 | 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) |
---|
481 | 609 | { |
---|
482 | 610 | struct i2c_client *client = to_i2c_client(dev); |
---|
483 | 611 | int len; |
---|
.. | .. |
---|
492 | 620 | |
---|
493 | 621 | return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name); |
---|
494 | 622 | } |
---|
495 | | -static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL); |
---|
| 623 | +static DEVICE_ATTR_RO(modalias); |
---|
496 | 624 | |
---|
497 | 625 | static struct attribute *i2c_dev_attrs[] = { |
---|
498 | 626 | &dev_attr_name.attr, |
---|
.. | .. |
---|
704 | 832 | i2c_encode_flags_to_addr(client), status); |
---|
705 | 833 | } |
---|
706 | 834 | |
---|
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) |
---|
709 | 837 | { |
---|
710 | 838 | struct irq_data *irqd; |
---|
711 | 839 | int i; |
---|
.. | .. |
---|
731 | 859 | } |
---|
732 | 860 | |
---|
733 | 861 | /** |
---|
734 | | - * i2c_new_device - instantiate an i2c device |
---|
| 862 | + * i2c_new_client_device - instantiate an i2c device |
---|
735 | 863 | * @adap: the adapter managing the device |
---|
736 | 864 | * @info: describes one I2C device; bus_num is ignored |
---|
737 | 865 | * Context: can sleep |
---|
.. | .. |
---|
744 | 872 | * before any i2c_adapter could exist. |
---|
745 | 873 | * |
---|
746 | 874 | * 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. |
---|
748 | 876 | */ |
---|
749 | 877 | 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) |
---|
751 | 879 | { |
---|
752 | 880 | struct i2c_client *client; |
---|
753 | 881 | int status; |
---|
754 | 882 | |
---|
755 | 883 | client = kzalloc(sizeof *client, GFP_KERNEL); |
---|
756 | 884 | if (!client) |
---|
757 | | - return NULL; |
---|
| 885 | + return ERR_PTR(-ENOMEM); |
---|
758 | 886 | |
---|
759 | 887 | client->adapter = adap; |
---|
760 | 888 | |
---|
.. | .. |
---|
766 | 894 | if (!client->init_irq) |
---|
767 | 895 | client->init_irq = i2c_dev_irq_from_resources(info->resources, |
---|
768 | 896 | info->num_resources); |
---|
769 | | - client->irq = client->init_irq; |
---|
770 | 897 | |
---|
771 | 898 | strlcpy(client->name, info->type, sizeof(client->name)); |
---|
772 | 899 | |
---|
.. | .. |
---|
818 | 945 | of_node_put(info->of_node); |
---|
819 | 946 | out_err_silent: |
---|
820 | 947 | kfree(client); |
---|
821 | | - return NULL; |
---|
| 948 | + return ERR_PTR(status); |
---|
822 | 949 | } |
---|
823 | | -EXPORT_SYMBOL_GPL(i2c_new_device); |
---|
824 | | - |
---|
| 950 | +EXPORT_SYMBOL_GPL(i2c_new_client_device); |
---|
825 | 951 | |
---|
826 | 952 | /** |
---|
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() |
---|
829 | 955 | * Context: can sleep |
---|
830 | 956 | */ |
---|
831 | 957 | void i2c_unregister_device(struct i2c_client *client) |
---|
832 | 958 | { |
---|
833 | | - if (!client) |
---|
| 959 | + if (IS_ERR_OR_NULL(client)) |
---|
834 | 960 | return; |
---|
835 | 961 | |
---|
836 | 962 | if (client->dev.of_node) { |
---|
.. | .. |
---|
869 | 995 | }; |
---|
870 | 996 | |
---|
871 | 997 | /** |
---|
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 |
---|
873 | 999 | * @adapter: the adapter managing the device |
---|
874 | 1000 | * @address: seven bit address to be used |
---|
875 | 1001 | * Context: can sleep |
---|
.. | .. |
---|
884 | 1010 | * different driver. |
---|
885 | 1011 | * |
---|
886 | 1012 | * 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. |
---|
888 | 1014 | */ |
---|
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) |
---|
890 | 1016 | { |
---|
891 | 1017 | struct i2c_board_info info = { |
---|
892 | 1018 | I2C_BOARD_INFO("dummy", address), |
---|
893 | 1019 | }; |
---|
894 | 1020 | |
---|
895 | | - return i2c_new_device(adapter, &info); |
---|
| 1021 | + return i2c_new_client_device(adapter, &info); |
---|
896 | 1022 | } |
---|
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 | +} |
---|
898 | 1035 | |
---|
899 | 1036 | /** |
---|
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 |
---|
901 | 1071 | * and create the associated device |
---|
902 | 1072 | * @client: Handle to the primary client |
---|
903 | 1073 | * @name: Handle to specify which secondary address to get |
---|
.. | .. |
---|
916 | 1086 | * cell whose "reg-names" value matches the slave name. |
---|
917 | 1087 | * |
---|
918 | 1088 | * 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. |
---|
920 | 1090 | */ |
---|
921 | | -struct i2c_client *i2c_new_secondary_device(struct i2c_client *client, |
---|
| 1091 | +struct i2c_client *i2c_new_ancillary_device(struct i2c_client *client, |
---|
922 | 1092 | const char *name, |
---|
923 | 1093 | u16 default_addr) |
---|
924 | 1094 | { |
---|
.. | .. |
---|
933 | 1103 | } |
---|
934 | 1104 | |
---|
935 | 1105 | 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); |
---|
937 | 1107 | } |
---|
938 | | -EXPORT_SYMBOL_GPL(i2c_new_secondary_device); |
---|
| 1108 | +EXPORT_SYMBOL_GPL(i2c_new_ancillary_device); |
---|
939 | 1109 | |
---|
940 | 1110 | /* ------------------------------------------------------------------------- */ |
---|
941 | 1111 | |
---|
.. | .. |
---|
972 | 1142 | * the user to provide incorrect parameters. |
---|
973 | 1143 | */ |
---|
974 | 1144 | 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) |
---|
977 | 1147 | { |
---|
978 | 1148 | struct i2c_adapter *adap = to_i2c_adapter(dev); |
---|
979 | 1149 | struct i2c_board_info info; |
---|
.. | .. |
---|
1015 | 1185 | info.flags |= I2C_CLIENT_SLAVE; |
---|
1016 | 1186 | } |
---|
1017 | 1187 | |
---|
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); |
---|
1021 | 1191 | |
---|
1022 | 1192 | /* Keep track of the added device */ |
---|
1023 | 1193 | mutex_lock(&adap->userspace_clients_lock); |
---|
.. | .. |
---|
1028 | 1198 | |
---|
1029 | 1199 | return count; |
---|
1030 | 1200 | } |
---|
1031 | | -static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device); |
---|
| 1201 | +static DEVICE_ATTR_WO(new_device); |
---|
1032 | 1202 | |
---|
1033 | 1203 | /* |
---|
1034 | 1204 | * And of course let the users delete the devices they instantiated, if |
---|
.. | .. |
---|
1040 | 1210 | * the user to delete the wrong device. |
---|
1041 | 1211 | */ |
---|
1042 | 1212 | 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) |
---|
1045 | 1215 | { |
---|
1046 | 1216 | struct i2c_adapter *adap = to_i2c_adapter(dev); |
---|
1047 | 1217 | struct i2c_client *client, *next; |
---|
.. | .. |
---|
1084 | 1254 | return res; |
---|
1085 | 1255 | } |
---|
1086 | 1256 | static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL, |
---|
1087 | | - i2c_sysfs_delete_device); |
---|
| 1257 | + delete_device_store); |
---|
1088 | 1258 | |
---|
1089 | 1259 | static struct attribute *i2c_adapter_attrs[] = { |
---|
1090 | 1260 | &dev_attr_name.attr, |
---|
.. | .. |
---|
1127 | 1297 | |
---|
1128 | 1298 | down_read(&__i2c_board_lock); |
---|
1129 | 1299 | 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))) |
---|
1133 | 1302 | dev_err(&adapter->dev, |
---|
1134 | 1303 | "Can't create device at 0x%02x\n", |
---|
1135 | 1304 | devinfo->board_info.addr); |
---|
.. | .. |
---|
1192 | 1361 | if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY)) |
---|
1193 | 1362 | return 0; |
---|
1194 | 1363 | |
---|
1195 | | - domain = irq_domain_create_linear(adap->dev.fwnode, |
---|
| 1364 | + domain = irq_domain_create_linear(adap->dev.parent->fwnode, |
---|
1196 | 1365 | I2C_ADDR_7BITS_COUNT, |
---|
1197 | 1366 | &i2c_host_notify_irq_ops, adap); |
---|
1198 | 1367 | if (!domain) |
---|
.. | .. |
---|
1252 | 1421 | if (!adap->lock_ops) |
---|
1253 | 1422 | adap->lock_ops = &i2c_adapter_lock_ops; |
---|
1254 | 1423 | |
---|
| 1424 | + adap->locked_flags = 0; |
---|
1255 | 1425 | rt_mutex_init(&adap->bus_lock); |
---|
1256 | 1426 | rt_mutex_init(&adap->mux_lock); |
---|
1257 | 1427 | mutex_init(&adap->userspace_clients_lock); |
---|
.. | .. |
---|
1282 | 1452 | if (res) |
---|
1283 | 1453 | goto out_reg; |
---|
1284 | 1454 | |
---|
1285 | | - dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); |
---|
1286 | | - |
---|
1287 | 1455 | pm_runtime_no_callbacks(&adap->dev); |
---|
1288 | 1456 | pm_suspend_ignore_children(&adap->dev, true); |
---|
1289 | 1457 | 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); |
---|
1290 | 1464 | |
---|
1291 | 1465 | #ifdef CONFIG_I2C_COMPAT |
---|
1292 | 1466 | res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev, |
---|
.. | .. |
---|
1295 | 1469 | dev_warn(&adap->dev, |
---|
1296 | 1470 | "Failed to create compatibility class link\n"); |
---|
1297 | 1471 | #endif |
---|
1298 | | - |
---|
1299 | | - i2c_init_recovery(adap); |
---|
1300 | 1472 | |
---|
1301 | 1473 | /* create pre-declared device nodes */ |
---|
1302 | 1474 | of_i2c_register_devices(adap); |
---|
.. | .. |
---|
1537 | 1709 | } |
---|
1538 | 1710 | EXPORT_SYMBOL(i2c_del_adapter); |
---|
1539 | 1711 | |
---|
| 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 | + |
---|
1540 | 1724 | /** |
---|
1541 | 1725 | * i2c_parse_fw_timings - get I2C related timing parameters from firmware |
---|
1542 | 1726 | * @dev: The device to scan for I2C timing properties |
---|
1543 | 1727 | * @t: the i2c_timings struct to be filled with values |
---|
1544 | 1728 | * @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 |
---|
1546 | 1730 | * |
---|
1547 | 1731 | * Scan the device for the generic I2C properties describing timing parameters |
---|
1548 | 1732 | * for the signal and fill the given struct with the results. If a property was |
---|
1549 | 1733 | * not found and use_defaults was true, then maximum timings are assumed which |
---|
1550 | 1734 | * 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. |
---|
1554 | 1739 | */ |
---|
1555 | | - |
---|
1556 | 1740 | void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults) |
---|
1557 | 1741 | { |
---|
1558 | | - int ret; |
---|
| 1742 | + bool u = use_defaults; |
---|
| 1743 | + u32 d; |
---|
1559 | 1744 | |
---|
1560 | | - memset(t, 0, sizeof(*t)); |
---|
| 1745 | + i2c_parse_timing(dev, "clock-frequency", &t->bus_freq_hz, |
---|
| 1746 | + I2C_MAX_STANDARD_MODE_FREQ, u); |
---|
1561 | 1747 | |
---|
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); |
---|
1565 | 1751 | |
---|
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); |
---|
1575 | 1754 | |
---|
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); |
---|
1591 | 1764 | } |
---|
1592 | 1765 | EXPORT_SYMBOL_GPL(i2c_parse_fw_timings); |
---|
1593 | 1766 | |
---|
1594 | 1767 | /* ------------------------------------------------------------------------- */ |
---|
1595 | 1768 | |
---|
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)) |
---|
1597 | 1770 | { |
---|
1598 | 1771 | int res; |
---|
1599 | 1772 | |
---|
.. | .. |
---|
1695 | 1868 | device_for_each_child(&adapter->dev, &addrinfo, __i2c_check_addr_ex); |
---|
1696 | 1869 | return addrinfo.cnt; |
---|
1697 | 1870 | } |
---|
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); |
---|
1730 | 1871 | |
---|
1731 | 1872 | struct i2c_cmd_arg { |
---|
1732 | 1873 | unsigned cmd; |
---|
.. | .. |
---|
1913 | 2054 | if (WARN_ON(!msgs || num < 1)) |
---|
1914 | 2055 | return -EINVAL; |
---|
1915 | 2056 | |
---|
| 2057 | + ret = __i2c_check_suspended(adap); |
---|
| 2058 | + if (ret) |
---|
| 2059 | + return ret; |
---|
| 2060 | + |
---|
1916 | 2061 | if (adap->quirks && i2c_check_for_quirks(adap, msgs, num)) |
---|
1917 | 2062 | return -EOPNOTSUPP; |
---|
1918 | 2063 | |
---|
.. | .. |
---|
1933 | 2078 | /* Retry automatically on arbitration loss */ |
---|
1934 | 2079 | orig_jiffies = jiffies; |
---|
1935 | 2080 | 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 | + |
---|
1937 | 2086 | if (ret != -EAGAIN) |
---|
1938 | 2087 | break; |
---|
1939 | 2088 | if (time_after(jiffies, orig_jiffies + adap->timeout)) |
---|
.. | .. |
---|
1968 | 2117 | { |
---|
1969 | 2118 | int ret; |
---|
1970 | 2119 | |
---|
| 2120 | + if (!adap->algo->master_xfer) { |
---|
| 2121 | + dev_dbg(&adap->dev, "I2C level transfers not supported\n"); |
---|
| 2122 | + return -EOPNOTSUPP; |
---|
| 2123 | + } |
---|
| 2124 | + |
---|
1971 | 2125 | /* REVISIT the fault reporting model here is weak: |
---|
1972 | 2126 | * |
---|
1973 | 2127 | * - When we get an error after receiving N bytes from a slave, |
---|
.. | .. |
---|
1984 | 2138 | * one (discarding status on the second message) or errno |
---|
1985 | 2139 | * (discarding status on the first one). |
---|
1986 | 2140 | */ |
---|
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) |
---|
2011 | 2143 | 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; |
---|
2016 | 2149 | } |
---|
2017 | 2150 | EXPORT_SYMBOL(i2c_transfer); |
---|
2018 | 2151 | |
---|
.. | .. |
---|
2171 | 2304 | dev_warn(&adapter->dev, |
---|
2172 | 2305 | "This adapter will soon drop class based instantiation of devices. " |
---|
2173 | 2306 | "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", |
---|
2175 | 2308 | info.addr); |
---|
2176 | 2309 | |
---|
2177 | 2310 | dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n", |
---|
2178 | 2311 | 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)) |
---|
2181 | 2314 | list_add_tail(&client->detected, &driver->clients); |
---|
2182 | 2315 | else |
---|
2183 | 2316 | dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n", |
---|
.. | .. |
---|
2191 | 2324 | const unsigned short *address_list; |
---|
2192 | 2325 | struct i2c_client *temp_client; |
---|
2193 | 2326 | int i, err = 0; |
---|
2194 | | - int adap_id = i2c_adapter_id(adapter); |
---|
2195 | 2327 | |
---|
2196 | 2328 | address_list = driver->address_list; |
---|
2197 | 2329 | if (!driver->detect || !address_list) |
---|
.. | .. |
---|
2201 | 2333 | if (adapter->class == I2C_CLASS_DEPRECATED) { |
---|
2202 | 2334 | dev_dbg(&adapter->dev, |
---|
2203 | 2335 | "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", |
---|
2205 | 2337 | driver->driver.name); |
---|
2206 | 2338 | return 0; |
---|
2207 | 2339 | } |
---|
.. | .. |
---|
2219 | 2351 | for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) { |
---|
2220 | 2352 | dev_dbg(&adapter->dev, |
---|
2221 | 2353 | "found normal entry for adapter %d, addr 0x%02x\n", |
---|
2222 | | - adap_id, address_list[i]); |
---|
| 2354 | + i2c_adapter_id(adapter), address_list[i]); |
---|
2223 | 2355 | temp_client->addr = address_list[i]; |
---|
2224 | 2356 | err = i2c_detect_address(temp_client, driver); |
---|
2225 | 2357 | if (unlikely(err)) |
---|
.. | .. |
---|
2238 | 2370 | EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read); |
---|
2239 | 2371 | |
---|
2240 | 2372 | 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)) |
---|
2245 | 2377 | { |
---|
2246 | 2378 | int i; |
---|
2247 | 2379 | |
---|
.. | .. |
---|
2271 | 2403 | |
---|
2272 | 2404 | if (addr_list[i] == I2C_CLIENT_END) { |
---|
2273 | 2405 | dev_dbg(&adap->dev, "Probing failed, no device found\n"); |
---|
2274 | | - return NULL; |
---|
| 2406 | + return ERR_PTR(-ENODEV); |
---|
2275 | 2407 | } |
---|
2276 | 2408 | |
---|
2277 | 2409 | info->addr = addr_list[i]; |
---|
2278 | | - return i2c_new_device(adap, info); |
---|
| 2410 | + return i2c_new_client_device(adap, info); |
---|
2279 | 2411 | } |
---|
2280 | | -EXPORT_SYMBOL_GPL(i2c_new_probed_device); |
---|
| 2412 | +EXPORT_SYMBOL_GPL(i2c_new_scanned_device); |
---|
2281 | 2413 | |
---|
2282 | 2414 | struct i2c_adapter *i2c_get_adapter(int nr) |
---|
2283 | 2415 | { |
---|
.. | .. |
---|
2304 | 2436 | if (!adap) |
---|
2305 | 2437 | return; |
---|
2306 | 2438 | |
---|
2307 | | - put_device(&adap->dev); |
---|
2308 | 2439 | module_put(adap->owner); |
---|
| 2440 | + /* Should be last, otherwise we risk use-after-free with 'adap' */ |
---|
| 2441 | + put_device(&adap->dev); |
---|
2309 | 2442 | } |
---|
2310 | 2443 | EXPORT_SYMBOL(i2c_put_adapter); |
---|
2311 | 2444 | |
---|
2312 | 2445 | /** |
---|
2313 | 2446 | * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg |
---|
2314 | 2447 | * @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. |
---|
2316 | 2450 | * |
---|
2317 | 2451 | * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO. |
---|
2318 | 2452 | * Or a valid pointer to be used with DMA. After use, release it by |
---|
.. | .. |
---|
2322 | 2456 | */ |
---|
2323 | 2457 | u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold) |
---|
2324 | 2458 | { |
---|
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) |
---|
2326 | 2464 | return NULL; |
---|
2327 | 2465 | |
---|
2328 | 2466 | if (msg->flags & I2C_M_DMA_SAFE) |
---|