LCOV - code coverage report
Current view: top level - drivers/i2c - i2c-core-base.c (source / functions) Hit Total Coverage
Test: combined.info Lines: 33 873 3.8 %
Date: 2022-04-01 14:35:51 Functions: 3 81 3.7 %
Branches: 8 544 1.5 %

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-or-later
       2                 :            : /*
       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                 :            :  *
      10                 :            :  * Copyright (C) 2013-2017 Wolfram Sang <wsa@the-dreams.de>
      11                 :            :  */
      12                 :            : 
      13                 :            : #define pr_fmt(fmt) "i2c-core: " fmt
      14                 :            : 
      15                 :            : #include <dt-bindings/i2c/i2c.h>
      16                 :            : #include <linux/acpi.h>
      17                 :            : #include <linux/clk/clk-conf.h>
      18                 :            : #include <linux/completion.h>
      19                 :            : #include <linux/delay.h>
      20                 :            : #include <linux/err.h>
      21                 :            : #include <linux/errno.h>
      22                 :            : #include <linux/gpio/consumer.h>
      23                 :            : #include <linux/i2c.h>
      24                 :            : #include <linux/i2c-smbus.h>
      25                 :            : #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>
      38                 :            : #include <linux/property.h>
      39                 :            : #include <linux/rwsem.h>
      40                 :            : #include <linux/slab.h>
      41                 :            : 
      42                 :            : #include "i2c-core.h"
      43                 :            : 
      44                 :            : #define CREATE_TRACE_POINTS
      45                 :            : #include <trace/events/i2c.h>
      46                 :            : 
      47                 :            : #define I2C_ADDR_OFFSET_TEN_BIT 0xa000
      48                 :            : #define I2C_ADDR_OFFSET_SLAVE   0x1000
      49                 :            : 
      50                 :            : #define I2C_ADDR_7BITS_MAX      0x77
      51                 :            : #define I2C_ADDR_7BITS_COUNT    (I2C_ADDR_7BITS_MAX + 1)
      52                 :            : 
      53                 :            : #define I2C_ADDR_DEVICE_ID      0x7c
      54                 :            : 
      55                 :            : /*
      56                 :            :  * core_lock protects i2c_adapter_idr, and guarantees that device detection,
      57                 :            :  * deletion of detected devices are serialized
      58                 :            :  */
      59                 :            : static DEFINE_MUTEX(core_lock);
      60                 :            : static DEFINE_IDR(i2c_adapter_idr);
      61                 :            : 
      62                 :            : static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
      63                 :            : 
      64                 :            : static DEFINE_STATIC_KEY_FALSE(i2c_trace_msg_key);
      65                 :            : static bool is_registered;
      66                 :            : 
      67                 :          0 : int i2c_transfer_trace_reg(void)
      68                 :            : {
      69                 :          0 :         static_branch_inc(&i2c_trace_msg_key);
      70                 :          0 :         return 0;
      71                 :            : }
      72                 :            : 
      73                 :          0 : void i2c_transfer_trace_unreg(void)
      74                 :            : {
      75                 :          0 :         static_branch_dec(&i2c_trace_msg_key);
      76                 :          0 : }
      77                 :            : 
      78                 :          0 : const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
      79                 :            :                                                 const struct i2c_client *client)
      80                 :            : {
      81         [ #  # ]:          0 :         if (!(id && client))
      82                 :            :                 return NULL;
      83                 :            : 
      84         [ #  # ]:          0 :         while (id->name[0]) {
      85         [ #  # ]:          0 :                 if (strcmp(client->name, id->name) == 0)
      86                 :          0 :                         return id;
      87                 :          0 :                 id++;
      88                 :            :         }
      89                 :            :         return NULL;
      90                 :            : }
      91                 :            : EXPORT_SYMBOL_GPL(i2c_match_id);
      92                 :            : 
      93                 :          0 : static int i2c_device_match(struct device *dev, struct device_driver *drv)
      94                 :            : {
      95                 :          0 :         struct i2c_client       *client = i2c_verify_client(dev);
      96                 :          0 :         struct i2c_driver       *driver;
      97                 :            : 
      98                 :            : 
      99                 :            :         /* Attempt an OF style match */
     100                 :          0 :         if (i2c_of_match_device(drv->of_match_table, client))
     101                 :            :                 return 1;
     102                 :            : 
     103                 :            :         /* Then ACPI style match */
     104         [ #  # ]:          0 :         if (acpi_driver_match_device(dev, drv))
     105                 :            :                 return 1;
     106                 :            : 
     107                 :          0 :         driver = to_i2c_driver(drv);
     108                 :            : 
     109                 :            :         /* Finally an I2C match */
     110         [ #  # ]:          0 :         if (i2c_match_id(driver->id_table, client))
     111                 :          0 :                 return 1;
     112                 :            : 
     113                 :            :         return 0;
     114                 :            : }
     115                 :            : 
     116                 :          0 : static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
     117                 :            : {
     118                 :          0 :         struct i2c_client *client = to_i2c_client(dev);
     119                 :          0 :         int rc;
     120                 :            : 
     121                 :          0 :         rc = of_device_uevent_modalias(dev, env);
     122                 :          0 :         if (rc != -ENODEV)
     123                 :            :                 return rc;
     124                 :            : 
     125                 :          0 :         rc = acpi_device_uevent_modalias(dev, env);
     126         [ #  # ]:          0 :         if (rc != -ENODEV)
     127                 :            :                 return rc;
     128                 :            : 
     129                 :          0 :         return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name);
     130                 :            : }
     131                 :            : 
     132                 :            : /* i2c bus recovery routines */
     133                 :          0 : static int get_scl_gpio_value(struct i2c_adapter *adap)
     134                 :            : {
     135         [ #  # ]:          0 :         return gpiod_get_value_cansleep(adap->bus_recovery_info->scl_gpiod);
     136                 :            : }
     137                 :            : 
     138                 :          0 : static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
     139                 :            : {
     140         [ #  # ]:          0 :         gpiod_set_value_cansleep(adap->bus_recovery_info->scl_gpiod, val);
     141                 :          0 : }
     142                 :            : 
     143                 :          0 : static int get_sda_gpio_value(struct i2c_adapter *adap)
     144                 :            : {
     145         [ #  # ]:          0 :         return gpiod_get_value_cansleep(adap->bus_recovery_info->sda_gpiod);
     146                 :            : }
     147                 :            : 
     148                 :            : static void set_sda_gpio_value(struct i2c_adapter *adap, int val)
     149                 :            : {
     150                 :            :         gpiod_set_value_cansleep(adap->bus_recovery_info->sda_gpiod, val);
     151                 :            : }
     152                 :            : 
     153                 :          0 : static int i2c_generic_bus_free(struct i2c_adapter *adap)
     154                 :            : {
     155                 :          0 :         struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
     156                 :          0 :         int ret = -EOPNOTSUPP;
     157                 :            : 
     158         [ #  # ]:          0 :         if (bri->get_bus_free)
     159                 :          0 :                 ret = bri->get_bus_free(adap);
     160         [ #  # ]:          0 :         else if (bri->get_sda)
     161                 :          0 :                 ret = bri->get_sda(adap);
     162                 :            : 
     163         [ #  # ]:          0 :         if (ret < 0)
     164                 :          0 :                 return ret;
     165                 :            : 
     166         [ #  # ]:          0 :         return ret ? 0 : -EBUSY;
     167                 :            : }
     168                 :            : 
     169                 :            : /*
     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                 :            : 
     177                 :          0 : int i2c_generic_scl_recovery(struct i2c_adapter *adap)
     178                 :            : {
     179                 :          0 :         struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
     180                 :          0 :         int i = 0, scl = 1, ret = 0;
     181                 :            : 
     182         [ #  # ]:          0 :         if (bri->prepare_recovery)
     183                 :          0 :                 bri->prepare_recovery(adap);
     184                 :            : 
     185                 :            :         /*
     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
     189                 :            :          * for details. Note that we must honour tsu:sto, 4us, but lets use 5us
     190                 :            :          * here for simplicity.
     191                 :            :          */
     192                 :          0 :         bri->set_scl(adap, scl);
     193                 :          0 :         ndelay(RECOVERY_NDELAY);
     194         [ #  # ]:          0 :         if (bri->set_sda)
     195                 :          0 :                 bri->set_sda(adap, scl);
     196                 :          0 :         ndelay(RECOVERY_NDELAY / 2);
     197                 :            : 
     198                 :            :         /*
     199                 :            :          * By this time SCL is high, as we need to give 9 falling-rising edges
     200                 :            :          */
     201         [ #  # ]:          0 :         while (i++ < RECOVERY_CLK_CNT * 2) {
     202         [ #  # ]:          0 :                 if (scl) {
     203                 :            :                         /* SCL shouldn't be low here */
     204         [ #  # ]:          0 :                         if (!bri->get_scl(adap)) {
     205                 :          0 :                                 dev_err(&adap->dev,
     206                 :            :                                         "SCL is stuck low, exit recovery\n");
     207                 :          0 :                                 ret = -EBUSY;
     208                 :          0 :                                 break;
     209                 :            :                         }
     210                 :            :                 }
     211                 :            : 
     212                 :          0 :                 scl = !scl;
     213                 :          0 :                 bri->set_scl(adap, scl);
     214                 :            :                 /* Creating STOP again, see above */
     215         [ #  # ]:          0 :                 if (scl)  {
     216                 :            :                         /* Honour minimum tsu:sto */
     217                 :          0 :                         ndelay(RECOVERY_NDELAY);
     218                 :            :                 } else {
     219                 :            :                         /* Honour minimum tf and thd:dat */
     220                 :          0 :                         ndelay(RECOVERY_NDELAY / 2);
     221                 :            :                 }
     222         [ #  # ]:          0 :                 if (bri->set_sda)
     223                 :          0 :                         bri->set_sda(adap, scl);
     224                 :          0 :                 ndelay(RECOVERY_NDELAY / 2);
     225                 :            : 
     226         [ #  # ]:          0 :                 if (scl) {
     227                 :          0 :                         ret = i2c_generic_bus_free(adap);
     228         [ #  # ]:          0 :                         if (ret == 0)
     229                 :            :                                 break;
     230                 :            :                 }
     231                 :            :         }
     232                 :            : 
     233                 :            :         /* If we can't check bus status, assume recovery worked */
     234         [ #  # ]:          0 :         if (ret == -EOPNOTSUPP)
     235                 :          0 :                 ret = 0;
     236                 :            : 
     237         [ #  # ]:          0 :         if (bri->unprepare_recovery)
     238                 :          0 :                 bri->unprepare_recovery(adap);
     239                 :            : 
     240                 :          0 :         return ret;
     241                 :            : }
     242                 :            : EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery);
     243                 :            : 
     244                 :          0 : int i2c_recover_bus(struct i2c_adapter *adap)
     245                 :            : {
     246         [ #  # ]:          0 :         if (!adap->bus_recovery_info)
     247                 :            :                 return -EOPNOTSUPP;
     248                 :            : 
     249                 :          0 :         dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
     250                 :          0 :         return adap->bus_recovery_info->recover_bus(adap);
     251                 :            : }
     252                 :            : EXPORT_SYMBOL_GPL(i2c_recover_bus);
     253                 :            : 
     254                 :          0 : static void i2c_init_recovery(struct i2c_adapter *adap)
     255                 :            : {
     256                 :          0 :         struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
     257                 :          0 :         char *err_str;
     258                 :            : 
     259         [ #  # ]:          0 :         if (!bri)
     260                 :            :                 return;
     261                 :            : 
     262         [ #  # ]:          0 :         if (!bri->recover_bus) {
     263                 :          0 :                 err_str = "no recover_bus() found";
     264                 :          0 :                 goto err;
     265                 :            :         }
     266                 :            : 
     267   [ #  #  #  # ]:          0 :         if (bri->scl_gpiod && bri->recover_bus == i2c_generic_scl_recovery) {
     268                 :          0 :                 bri->get_scl = get_scl_gpio_value;
     269                 :          0 :                 bri->set_scl = set_scl_gpio_value;
     270         [ #  # ]:          0 :                 if (bri->sda_gpiod) {
     271                 :          0 :                         bri->get_sda = get_sda_gpio_value;
     272                 :            :                         /* FIXME: add proper flag instead of '0' once available */
     273                 :          0 :                         if (gpiod_get_direction(bri->sda_gpiod) == 0)
     274                 :            :                                 bri->set_sda = set_sda_gpio_value;
     275                 :            :                 }
     276                 :          0 :                 return;
     277                 :            :         }
     278                 :            : 
     279         [ #  # ]:          0 :         if (bri->recover_bus == i2c_generic_scl_recovery) {
     280                 :            :                 /* Generic SCL recovery */
     281   [ #  #  #  # ]:          0 :                 if (!bri->set_scl || !bri->get_scl) {
     282                 :          0 :                         err_str = "no {get|set}_scl() found";
     283                 :          0 :                         goto err;
     284                 :            :                 }
     285   [ #  #  #  # ]:          0 :                 if (!bri->set_sda && !bri->get_sda) {
     286                 :          0 :                         err_str = "either get_sda() or set_sda() needed";
     287                 :          0 :                         goto err;
     288                 :            :                 }
     289                 :            :         }
     290                 :            : 
     291                 :            :         return;
     292                 :          0 :  err:
     293                 :          0 :         dev_err(&adap->dev, "Not using recovery: %s\n", err_str);
     294                 :          0 :         adap->bus_recovery_info = NULL;
     295                 :            : }
     296                 :            : 
     297                 :          0 : static int i2c_smbus_host_notify_to_irq(const struct i2c_client *client)
     298                 :            : {
     299                 :          0 :         struct i2c_adapter *adap = client->adapter;
     300                 :          0 :         unsigned int irq;
     301                 :            : 
     302         [ #  # ]:          0 :         if (!adap->host_notify_domain)
     303                 :            :                 return -ENXIO;
     304                 :            : 
     305         [ #  # ]:          0 :         if (client->flags & I2C_CLIENT_TEN)
     306                 :            :                 return -EINVAL;
     307                 :            : 
     308                 :          0 :         irq = irq_create_mapping(adap->host_notify_domain, client->addr);
     309                 :            : 
     310         [ #  # ]:          0 :         return irq > 0 ? irq : -ENXIO;
     311                 :            : }
     312                 :            : 
     313                 :          0 : static int i2c_device_probe(struct device *dev)
     314                 :            : {
     315                 :          0 :         struct i2c_client       *client = i2c_verify_client(dev);
     316                 :          0 :         struct i2c_driver       *driver;
     317                 :          0 :         int status;
     318                 :            : 
     319                 :          0 :         if (!client)
     320                 :            :                 return 0;
     321                 :            : 
     322                 :          0 :         driver = to_i2c_driver(dev->driver);
     323                 :            : 
     324                 :          0 :         client->irq = client->init_irq;
     325                 :            : 
     326   [ #  #  #  # ]:          0 :         if (!client->irq && !driver->disable_i2c_core_irq_mapping) {
     327                 :          0 :                 int irq = -ENOENT;
     328                 :            : 
     329         [ #  # ]:          0 :                 if (client->flags & I2C_CLIENT_HOST_NOTIFY) {
     330                 :          0 :                         dev_dbg(dev, "Using Host Notify IRQ\n");
     331                 :            :                         /* Keep adapter active when Host Notify is required */
     332                 :          0 :                         pm_runtime_get_sync(&client->adapter->dev);
     333                 :          0 :                         irq = i2c_smbus_host_notify_to_irq(client);
     334         [ #  # ]:          0 :                 } else if (dev->of_node) {
     335                 :            :                         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   [ #  #  #  # ]:          0 :                 } else if (ACPI_COMPANION(dev)) {
     339                 :          0 :                         irq = i2c_acpi_get_irq(client);
     340                 :            :                 }
     341         [ #  # ]:          0 :                 if (irq == -EPROBE_DEFER)
     342                 :            :                         return irq;
     343                 :            : 
     344                 :          0 :                 if (irq < 0)
     345                 :            :                         irq = 0;
     346                 :            : 
     347                 :          0 :                 client->irq = irq;
     348                 :            :         }
     349                 :            : 
     350                 :            :         /*
     351                 :            :          * An I2C ID table is not mandatory, if and only if, a suitable OF
     352                 :            :          * or ACPI ID table is supplied for the probing device.
     353                 :            :          */
     354   [ #  #  #  # ]:          0 :         if (!driver->id_table &&
     355                 :          0 :             !i2c_acpi_match_device(dev->driver->acpi_match_table, client) &&
     356                 :            :             !i2c_of_match_device(dev->driver->of_match_table, client))
     357                 :            :                 return -ENODEV;
     358                 :            : 
     359         [ #  # ]:          0 :         if (client->flags & I2C_CLIENT_WAKE) {
     360                 :          0 :                 int wakeirq;
     361                 :            : 
     362                 :          0 :                 wakeirq = of_irq_get_byname(dev->of_node, "wakeup");
     363                 :          0 :                 if (wakeirq == -EPROBE_DEFER)
     364                 :            :                         return wakeirq;
     365                 :            : 
     366                 :          0 :                 device_init_wakeup(&client->dev, true);
     367                 :            : 
     368                 :          0 :                 if (wakeirq > 0 && wakeirq != client->irq)
     369                 :            :                         status = dev_pm_set_dedicated_wake_irq(dev, wakeirq);
     370         [ #  # ]:          0 :                 else if (client->irq > 0)
     371                 :          0 :                         status = dev_pm_set_wake_irq(dev, client->irq);
     372                 :            :                 else
     373                 :            :                         status = 0;
     374                 :            : 
     375         [ #  # ]:          0 :                 if (status)
     376                 :          0 :                         dev_warn(&client->dev, "failed to set up wakeup irq\n");
     377                 :            :         }
     378                 :            : 
     379                 :          0 :         dev_dbg(dev, "probe\n");
     380                 :            : 
     381                 :          0 :         status = of_clk_set_defaults(dev->of_node, false);
     382                 :          0 :         if (status < 0)
     383                 :            :                 goto err_clear_wakeup_irq;
     384                 :            : 
     385                 :          0 :         status = dev_pm_domain_attach(&client->dev, true);
     386         [ #  # ]:          0 :         if (status)
     387                 :          0 :                 goto err_clear_wakeup_irq;
     388                 :            : 
     389                 :            :         /*
     390                 :            :          * When there are no more users of probe(),
     391                 :            :          * rename probe_new to probe.
     392                 :            :          */
     393         [ #  # ]:          0 :         if (driver->probe_new)
     394                 :          0 :                 status = driver->probe_new(client);
     395         [ #  # ]:          0 :         else if (driver->probe)
     396                 :          0 :                 status = driver->probe(client,
     397                 :            :                                        i2c_match_id(driver->id_table, client));
     398                 :            :         else
     399                 :            :                 status = -EINVAL;
     400                 :            : 
     401         [ #  # ]:          0 :         if (status)
     402                 :          0 :                 goto err_detach_pm_domain;
     403                 :            : 
     404                 :            :         return 0;
     405                 :            : 
     406                 :            : err_detach_pm_domain:
     407                 :          0 :         dev_pm_domain_detach(&client->dev, true);
     408                 :          0 : err_clear_wakeup_irq:
     409                 :          0 :         dev_pm_clear_wake_irq(&client->dev);
     410                 :          0 :         device_init_wakeup(&client->dev, false);
     411                 :          0 :         return status;
     412                 :            : }
     413                 :            : 
     414                 :          0 : static int i2c_device_remove(struct device *dev)
     415                 :            : {
     416                 :          0 :         struct i2c_client       *client = i2c_verify_client(dev);
     417                 :          0 :         struct i2c_driver       *driver;
     418                 :          0 :         int status = 0;
     419                 :            : 
     420         [ #  # ]:          0 :         if (!client || !dev->driver)
     421                 :            :                 return 0;
     422                 :            : 
     423                 :          0 :         driver = to_i2c_driver(dev->driver);
     424         [ #  # ]:          0 :         if (driver->remove) {
     425                 :          0 :                 dev_dbg(dev, "remove\n");
     426                 :          0 :                 status = driver->remove(client);
     427                 :            :         }
     428                 :            : 
     429                 :          0 :         dev_pm_domain_detach(&client->dev, true);
     430                 :            : 
     431                 :          0 :         dev_pm_clear_wake_irq(&client->dev);
     432                 :          0 :         device_init_wakeup(&client->dev, false);
     433                 :            : 
     434                 :          0 :         client->irq = 0;
     435         [ #  # ]:          0 :         if (client->flags & I2C_CLIENT_HOST_NOTIFY)
     436                 :          0 :                 pm_runtime_put(&client->adapter->dev);
     437                 :            : 
     438                 :            :         return status;
     439                 :            : }
     440                 :            : 
     441                 :          0 : static void i2c_device_shutdown(struct device *dev)
     442                 :            : {
     443                 :          0 :         struct i2c_client *client = i2c_verify_client(dev);
     444                 :          0 :         struct i2c_driver *driver;
     445                 :            : 
     446         [ #  # ]:          0 :         if (!client || !dev->driver)
     447                 :            :                 return;
     448                 :          0 :         driver = to_i2c_driver(dev->driver);
     449         [ #  # ]:          0 :         if (driver->shutdown)
     450                 :          0 :                 driver->shutdown(client);
     451                 :            : }
     452                 :            : 
     453                 :          0 : static void i2c_client_dev_release(struct device *dev)
     454                 :            : {
     455                 :          0 :         kfree(to_i2c_client(dev));
     456                 :          0 : }
     457                 :            : 
     458                 :            : static ssize_t
     459                 :          0 : name_show(struct device *dev, struct device_attribute *attr, char *buf)
     460                 :            : {
     461         [ #  # ]:          0 :         return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
     462                 :          0 :                        to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
     463                 :            : }
     464                 :            : static DEVICE_ATTR_RO(name);
     465                 :            : 
     466                 :            : static ssize_t
     467                 :          0 : modalias_show(struct device *dev, struct device_attribute *attr, char *buf)
     468                 :            : {
     469                 :          0 :         struct i2c_client *client = to_i2c_client(dev);
     470                 :          0 :         int len;
     471                 :            : 
     472                 :          0 :         len = of_device_modalias(dev, buf, PAGE_SIZE);
     473                 :          0 :         if (len != -ENODEV)
     474                 :            :                 return len;
     475                 :            : 
     476                 :          0 :         len = acpi_device_modalias(dev, buf, PAGE_SIZE -1);
     477         [ #  # ]:          0 :         if (len != -ENODEV)
     478                 :          0 :                 return len;
     479                 :            : 
     480                 :          0 :         return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
     481                 :            : }
     482                 :            : static DEVICE_ATTR_RO(modalias);
     483                 :            : 
     484                 :            : static struct attribute *i2c_dev_attrs[] = {
     485                 :            :         &dev_attr_name.attr,
     486                 :            :         /* modalias helps coldplug:  modprobe $(cat .../modalias) */
     487                 :            :         &dev_attr_modalias.attr,
     488                 :            :         NULL
     489                 :            : };
     490                 :            : ATTRIBUTE_GROUPS(i2c_dev);
     491                 :            : 
     492                 :            : struct bus_type i2c_bus_type = {
     493                 :            :         .name           = "i2c",
     494                 :            :         .match          = i2c_device_match,
     495                 :            :         .probe          = i2c_device_probe,
     496                 :            :         .remove         = i2c_device_remove,
     497                 :            :         .shutdown       = i2c_device_shutdown,
     498                 :            : };
     499                 :            : EXPORT_SYMBOL_GPL(i2c_bus_type);
     500                 :            : 
     501                 :            : struct device_type i2c_client_type = {
     502                 :            :         .groups         = i2c_dev_groups,
     503                 :            :         .uevent         = i2c_device_uevent,
     504                 :            :         .release        = i2c_client_dev_release,
     505                 :            : };
     506                 :            : EXPORT_SYMBOL_GPL(i2c_client_type);
     507                 :            : 
     508                 :            : 
     509                 :            : /**
     510                 :            :  * i2c_verify_client - return parameter as i2c_client, or NULL
     511                 :            :  * @dev: device, probably from some driver model iterator
     512                 :            :  *
     513                 :            :  * When traversing the driver model tree, perhaps using driver model
     514                 :            :  * iterators like @device_for_each_child(), you can't assume very much
     515                 :            :  * about the nodes you find.  Use this function to avoid oopses caused
     516                 :            :  * by wrongly treating some non-I2C device as an i2c_client.
     517                 :            :  */
     518                 :          0 : struct i2c_client *i2c_verify_client(struct device *dev)
     519                 :            : {
     520                 :          0 :         return (dev->type == &i2c_client_type)
     521   [ #  #  #  #  :          0 :                         ? to_i2c_client(dev)
                   #  # ]
     522   [ #  #  #  #  :          0 :                         : NULL;
          #  #  #  #  #  
                      # ]
     523                 :            : }
     524                 :            : EXPORT_SYMBOL(i2c_verify_client);
     525                 :            : 
     526                 :            : 
     527                 :            : /* Return a unique address which takes the flags of the client into account */
     528                 :          0 : static unsigned short i2c_encode_flags_to_addr(struct i2c_client *client)
     529                 :            : {
     530                 :          0 :         unsigned short addr = client->addr;
     531                 :            : 
     532                 :            :         /* For some client flags, add an arbitrary offset to avoid collisions */
     533                 :          0 :         if (client->flags & I2C_CLIENT_TEN)
     534                 :          0 :                 addr |= I2C_ADDR_OFFSET_TEN_BIT;
     535                 :            : 
     536   [ #  #  #  #  :          0 :         if (client->flags & I2C_CLIENT_SLAVE)
             #  #  #  # ]
     537                 :          0 :                 addr |= I2C_ADDR_OFFSET_SLAVE;
     538                 :            : 
     539                 :          0 :         return addr;
     540                 :            : }
     541                 :            : 
     542                 :            : /* This is a permissive address validity check, I2C address map constraints
     543                 :            :  * are purposely not enforced, except for the general call address. */
     544                 :          0 : static int i2c_check_addr_validity(unsigned int addr, unsigned short flags)
     545                 :            : {
     546                 :          0 :         if (flags & I2C_CLIENT_TEN) {
     547                 :            :                 /* 10-bit address, all values are valid */
     548         [ #  # ]:          0 :                 if (addr > 0x3ff)
     549                 :            :                         return -EINVAL;
     550                 :            :         } else {
     551                 :            :                 /* 7-bit address, reject the general call address */
     552         [ #  # ]:          0 :                 if (addr == 0x00 || addr > 0x7f)
     553                 :            :                         return -EINVAL;
     554                 :            :         }
     555                 :            :         return 0;
     556                 :            : }
     557                 :            : 
     558                 :            : /* And this is a strict address validity check, used when probing. If a
     559                 :            :  * device uses a reserved address, then it shouldn't be probed. 7-bit
     560                 :            :  * addressing is assumed, 10-bit address devices are rare and should be
     561                 :            :  * explicitly enumerated. */
     562                 :          0 : int i2c_check_7bit_addr_validity_strict(unsigned short addr)
     563                 :            : {
     564                 :            :         /*
     565                 :            :          * Reserved addresses per I2C specification:
     566                 :            :          *  0x00       General call address / START byte
     567                 :            :          *  0x01       CBUS address
     568                 :            :          *  0x02       Reserved for different bus format
     569                 :            :          *  0x03       Reserved for future purposes
     570                 :            :          *  0x04-0x07  Hs-mode master code
     571                 :            :          *  0x78-0x7b  10-bit slave addressing
     572                 :            :          *  0x7c-0x7f  Reserved for future purposes
     573                 :            :          */
     574         [ #  # ]:          0 :         if (addr < 0x08 || addr > 0x77)
     575                 :          0 :                 return -EINVAL;
     576                 :            :         return 0;
     577                 :            : }
     578                 :            : 
     579                 :          0 : static int __i2c_check_addr_busy(struct device *dev, void *addrp)
     580                 :            : {
     581                 :          0 :         struct i2c_client       *client = i2c_verify_client(dev);
     582                 :          0 :         int                     addr = *(int *)addrp;
     583                 :            : 
     584   [ #  #  #  #  :          0 :         if (client && i2c_encode_flags_to_addr(client) == addr)
                   #  # ]
     585                 :          0 :                 return -EBUSY;
     586                 :            :         return 0;
     587                 :            : }
     588                 :            : 
     589                 :            : /* walk up mux tree */
     590                 :            : static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
     591                 :            : {
     592                 :            :         struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
     593                 :            :         int result;
     594                 :            : 
     595                 :            :         result = device_for_each_child(&adapter->dev, &addr,
     596                 :            :                                         __i2c_check_addr_busy);
     597                 :            : 
     598                 :            :         if (!result && parent)
     599                 :            :                 result = i2c_check_mux_parents(parent, addr);
     600                 :            : 
     601                 :            :         return result;
     602                 :            : }
     603                 :            : 
     604                 :            : /* recurse down mux tree */
     605                 :          0 : static int i2c_check_mux_children(struct device *dev, void *addrp)
     606                 :            : {
     607                 :          0 :         int result;
     608                 :            : 
     609         [ #  # ]:          0 :         if (dev->type == &i2c_adapter_type)
     610                 :          0 :                 result = device_for_each_child(dev, addrp,
     611                 :            :                                                 i2c_check_mux_children);
     612                 :            :         else
     613         [ #  # ]:          0 :                 result = __i2c_check_addr_busy(dev, addrp);
     614                 :            : 
     615                 :          0 :         return result;
     616                 :            : }
     617                 :            : 
     618                 :          0 : static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
     619                 :            : {
     620                 :          0 :         struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
     621                 :          0 :         int result = 0;
     622                 :            : 
     623                 :          0 :         if (parent)
     624                 :            :                 result = i2c_check_mux_parents(parent, addr);
     625                 :            : 
     626                 :          0 :         if (!result)
     627                 :          0 :                 result = device_for_each_child(&adapter->dev, &addr,
     628                 :            :                                                 i2c_check_mux_children);
     629                 :            : 
     630                 :          0 :         return result;
     631                 :            : }
     632                 :            : 
     633                 :            : /**
     634                 :            :  * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
     635                 :            :  * @adapter: Target I2C bus segment
     636                 :            :  * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
     637                 :            :  *      locks only this branch in the adapter tree
     638                 :            :  */
     639                 :          0 : static void i2c_adapter_lock_bus(struct i2c_adapter *adapter,
     640                 :            :                                  unsigned int flags)
     641                 :            : {
     642                 :          0 :         rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter));
     643                 :          0 : }
     644                 :            : 
     645                 :            : /**
     646                 :            :  * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
     647                 :            :  * @adapter: Target I2C bus segment
     648                 :            :  * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
     649                 :            :  *      trylocks only this branch in the adapter tree
     650                 :            :  */
     651                 :          0 : static int i2c_adapter_trylock_bus(struct i2c_adapter *adapter,
     652                 :            :                                    unsigned int flags)
     653                 :            : {
     654                 :          0 :         return rt_mutex_trylock(&adapter->bus_lock);
     655                 :            : }
     656                 :            : 
     657                 :            : /**
     658                 :            :  * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
     659                 :            :  * @adapter: Target I2C bus segment
     660                 :            :  * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
     661                 :            :  *      unlocks only this branch in the adapter tree
     662                 :            :  */
     663                 :          0 : static void i2c_adapter_unlock_bus(struct i2c_adapter *adapter,
     664                 :            :                                    unsigned int flags)
     665                 :            : {
     666                 :          0 :         rt_mutex_unlock(&adapter->bus_lock);
     667                 :          0 : }
     668                 :            : 
     669                 :          0 : static void i2c_dev_set_name(struct i2c_adapter *adap,
     670                 :            :                              struct i2c_client *client,
     671                 :            :                              struct i2c_board_info const *info)
     672                 :            : {
     673         [ #  # ]:          0 :         struct acpi_device *adev = ACPI_COMPANION(&client->dev);
     674                 :            : 
     675   [ #  #  #  # ]:          0 :         if (info && info->dev_name) {
     676                 :          0 :                 dev_set_name(&client->dev, "i2c-%s", info->dev_name);
     677                 :          0 :                 return;
     678                 :            :         }
     679                 :            : 
     680         [ #  # ]:          0 :         if (adev) {
     681         [ #  # ]:          0 :                 dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev));
     682                 :          0 :                 return;
     683                 :            :         }
     684                 :            : 
     685                 :          0 :         dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
     686         [ #  # ]:          0 :                      i2c_encode_flags_to_addr(client));
     687                 :            : }
     688                 :            : 
     689                 :          0 : int i2c_dev_irq_from_resources(const struct resource *resources,
     690                 :            :                                unsigned int num_resources)
     691                 :            : {
     692                 :          0 :         struct irq_data *irqd;
     693                 :          0 :         int i;
     694                 :            : 
     695         [ #  # ]:          0 :         for (i = 0; i < num_resources; i++) {
     696                 :          0 :                 const struct resource *r = &resources[i];
     697                 :            : 
     698         [ #  # ]:          0 :                 if (resource_type(r) != IORESOURCE_IRQ)
     699                 :          0 :                         continue;
     700                 :            : 
     701         [ #  # ]:          0 :                 if (r->flags & IORESOURCE_BITS) {
     702                 :          0 :                         irqd = irq_get_irq_data(r->start);
     703         [ #  # ]:          0 :                         if (!irqd)
     704                 :            :                                 break;
     705                 :            : 
     706                 :          0 :                         irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS);
     707                 :            :                 }
     708                 :            : 
     709                 :          0 :                 return r->start;
     710                 :            :         }
     711                 :            : 
     712                 :            :         return 0;
     713                 :            : }
     714                 :            : 
     715                 :            : /**
     716                 :            :  * i2c_new_client_device - instantiate an i2c device
     717                 :            :  * @adap: the adapter managing the device
     718                 :            :  * @info: describes one I2C device; bus_num is ignored
     719                 :            :  * Context: can sleep
     720                 :            :  *
     721                 :            :  * Create an i2c device. Binding is handled through driver model
     722                 :            :  * probe()/remove() methods.  A driver may be bound to this device when we
     723                 :            :  * return from this function, or any later moment (e.g. maybe hotplugging will
     724                 :            :  * load the driver module).  This call is not appropriate for use by mainboard
     725                 :            :  * initialization logic, which usually runs during an arch_initcall() long
     726                 :            :  * before any i2c_adapter could exist.
     727                 :            :  *
     728                 :            :  * This returns the new i2c client, which may be saved for later use with
     729                 :            :  * i2c_unregister_device(); or an ERR_PTR to describe the error.
     730                 :            :  */
     731                 :            : struct i2c_client *
     732                 :          0 : i2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
     733                 :            : {
     734                 :          0 :         struct i2c_client       *client;
     735                 :          0 :         int                     status;
     736                 :            : 
     737                 :          0 :         client = kzalloc(sizeof *client, GFP_KERNEL);
     738         [ #  # ]:          0 :         if (!client)
     739                 :            :                 return ERR_PTR(-ENOMEM);
     740                 :            : 
     741                 :          0 :         client->adapter = adap;
     742                 :            : 
     743                 :          0 :         client->dev.platform_data = info->platform_data;
     744                 :          0 :         client->flags = info->flags;
     745                 :          0 :         client->addr = info->addr;
     746                 :            : 
     747                 :          0 :         client->init_irq = info->irq;
     748         [ #  # ]:          0 :         if (!client->init_irq)
     749                 :          0 :                 client->init_irq = i2c_dev_irq_from_resources(info->resources,
     750                 :            :                                                          info->num_resources);
     751                 :            : 
     752                 :          0 :         strlcpy(client->name, info->type, sizeof(client->name));
     753                 :            : 
     754         [ #  # ]:          0 :         status = i2c_check_addr_validity(client->addr, client->flags);
     755                 :            :         if (status) {
     756         [ #  # ]:          0 :                 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
     757                 :            :                         client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
     758                 :          0 :                 goto out_err_silent;
     759                 :            :         }
     760                 :            : 
     761                 :            :         /* Check for address business */
     762         [ #  # ]:          0 :         status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client));
     763         [ #  # ]:          0 :         if (status)
     764                 :          0 :                 goto out_err;
     765                 :            : 
     766                 :          0 :         client->dev.parent = &client->adapter->dev;
     767                 :          0 :         client->dev.bus = &i2c_bus_type;
     768                 :          0 :         client->dev.type = &i2c_client_type;
     769                 :          0 :         client->dev.of_node = of_node_get(info->of_node);
     770                 :          0 :         client->dev.fwnode = info->fwnode;
     771                 :            : 
     772                 :          0 :         i2c_dev_set_name(adap, client, info);
     773                 :            : 
     774         [ #  # ]:          0 :         if (info->properties) {
     775                 :          0 :                 status = device_add_properties(&client->dev, info->properties);
     776         [ #  # ]:          0 :                 if (status) {
     777                 :          0 :                         dev_err(&adap->dev,
     778                 :            :                                 "Failed to add properties to client %s: %d\n",
     779                 :            :                                 client->name, status);
     780                 :          0 :                         goto out_err_put_of_node;
     781                 :            :                 }
     782                 :            :         }
     783                 :            : 
     784                 :          0 :         status = device_register(&client->dev);
     785         [ #  # ]:          0 :         if (status)
     786                 :          0 :                 goto out_free_props;
     787                 :            : 
     788                 :            :         dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
     789                 :            :                 client->name, dev_name(&client->dev));
     790                 :            : 
     791                 :            :         return client;
     792                 :            : 
     793                 :            : out_free_props:
     794         [ #  # ]:          0 :         if (info->properties)
     795                 :          0 :                 device_remove_properties(&client->dev);
     796                 :          0 : out_err_put_of_node:
     797                 :          0 :         of_node_put(info->of_node);
     798                 :          0 : out_err:
     799                 :          0 :         dev_err(&adap->dev,
     800                 :            :                 "Failed to register i2c client %s at 0x%02x (%d)\n",
     801                 :            :                 client->name, client->addr, status);
     802                 :          0 : out_err_silent:
     803                 :          0 :         kfree(client);
     804                 :          0 :         return ERR_PTR(status);
     805                 :            : }
     806                 :            : EXPORT_SYMBOL_GPL(i2c_new_client_device);
     807                 :            : 
     808                 :            : /**
     809                 :            :  * i2c_new_device - instantiate an i2c device
     810                 :            :  * @adap: the adapter managing the device
     811                 :            :  * @info: describes one I2C device; bus_num is ignored
     812                 :            :  * Context: can sleep
     813                 :            :  *
     814                 :            :  * This deprecated function has the same functionality as
     815                 :            :  * @i2c_new_client_device, it just returns NULL instead of an ERR_PTR in case of
     816                 :            :  * an error for compatibility with current I2C API. It will be removed once all
     817                 :            :  * users are converted.
     818                 :            :  *
     819                 :            :  * This returns the new i2c client, which may be saved for later use with
     820                 :            :  * i2c_unregister_device(); or NULL to indicate an error.
     821                 :            :  */
     822                 :            : struct i2c_client *
     823                 :          0 : i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
     824                 :            : {
     825                 :          0 :         struct i2c_client *ret;
     826                 :            : 
     827                 :          0 :         ret = i2c_new_client_device(adap, info);
     828         [ #  # ]:          0 :         return IS_ERR(ret) ? NULL : ret;
     829                 :            : }
     830                 :            : EXPORT_SYMBOL_GPL(i2c_new_device);
     831                 :            : 
     832                 :            : 
     833                 :            : /**
     834                 :            :  * i2c_unregister_device - reverse effect of i2c_new_*_device()
     835                 :            :  * @client: value returned from i2c_new_*_device()
     836                 :            :  * Context: can sleep
     837                 :            :  */
     838                 :          0 : void i2c_unregister_device(struct i2c_client *client)
     839                 :            : {
     840   [ #  #  #  # ]:          0 :         if (IS_ERR_OR_NULL(client))
     841                 :            :                 return;
     842                 :            : 
     843                 :          0 :         if (client->dev.of_node) {
     844                 :            :                 of_node_clear_flag(client->dev.of_node, OF_POPULATED);
     845                 :            :                 of_node_put(client->dev.of_node);
     846                 :            :         }
     847                 :            : 
     848   [ #  #  #  # ]:          0 :         if (ACPI_COMPANION(&client->dev))
     849         [ #  # ]:          0 :                 acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev));
     850                 :          0 :         device_unregister(&client->dev);
     851                 :            : }
     852                 :            : EXPORT_SYMBOL_GPL(i2c_unregister_device);
     853                 :            : 
     854                 :            : 
     855                 :            : static const struct i2c_device_id dummy_id[] = {
     856                 :            :         { "dummy", 0 },
     857                 :            :         { },
     858                 :            : };
     859                 :            : 
     860                 :          0 : static int dummy_probe(struct i2c_client *client,
     861                 :            :                        const struct i2c_device_id *id)
     862                 :            : {
     863                 :          0 :         return 0;
     864                 :            : }
     865                 :            : 
     866                 :          0 : static int dummy_remove(struct i2c_client *client)
     867                 :            : {
     868                 :          0 :         return 0;
     869                 :            : }
     870                 :            : 
     871                 :            : static struct i2c_driver dummy_driver = {
     872                 :            :         .driver.name    = "dummy",
     873                 :            :         .probe          = dummy_probe,
     874                 :            :         .remove         = dummy_remove,
     875                 :            :         .id_table       = dummy_id,
     876                 :            : };
     877                 :            : 
     878                 :            : /**
     879                 :            :  * i2c_new_dummy_device - return a new i2c device bound to a dummy driver
     880                 :            :  * @adapter: the adapter managing the device
     881                 :            :  * @address: seven bit address to be used
     882                 :            :  * Context: can sleep
     883                 :            :  *
     884                 :            :  * This returns an I2C client bound to the "dummy" driver, intended for use
     885                 :            :  * with devices that consume multiple addresses.  Examples of such chips
     886                 :            :  * include various EEPROMS (like 24c04 and 24c08 models).
     887                 :            :  *
     888                 :            :  * These dummy devices have two main uses.  First, most I2C and SMBus calls
     889                 :            :  * except i2c_transfer() need a client handle; the dummy will be that handle.
     890                 :            :  * And second, this prevents the specified address from being bound to a
     891                 :            :  * different driver.
     892                 :            :  *
     893                 :            :  * This returns the new i2c client, which should be saved for later use with
     894                 :            :  * i2c_unregister_device(); or an ERR_PTR to describe the error.
     895                 :            :  */
     896                 :          0 : struct i2c_client *i2c_new_dummy_device(struct i2c_adapter *adapter, u16 address)
     897                 :            : {
     898                 :          0 :         struct i2c_board_info info = {
     899                 :            :                 I2C_BOARD_INFO("dummy", address),
     900                 :            :         };
     901                 :            : 
     902                 :          0 :         return i2c_new_client_device(adapter, &info);
     903                 :            : }
     904                 :            : EXPORT_SYMBOL_GPL(i2c_new_dummy_device);
     905                 :            : 
     906                 :            : struct i2c_dummy_devres {
     907                 :            :         struct i2c_client *client;
     908                 :            : };
     909                 :            : 
     910                 :          0 : static void devm_i2c_release_dummy(struct device *dev, void *res)
     911                 :            : {
     912                 :          0 :         struct i2c_dummy_devres *this = res;
     913                 :            : 
     914                 :          0 :         i2c_unregister_device(this->client);
     915                 :          0 : }
     916                 :            : 
     917                 :            : /**
     918                 :            :  * devm_i2c_new_dummy_device - return a new i2c device bound to a dummy driver
     919                 :            :  * @dev: device the managed resource is bound to
     920                 :            :  * @adapter: the adapter managing the device
     921                 :            :  * @address: seven bit address to be used
     922                 :            :  * Context: can sleep
     923                 :            :  *
     924                 :            :  * This is the device-managed version of @i2c_new_dummy_device. It returns the
     925                 :            :  * new i2c client or an ERR_PTR in case of an error.
     926                 :            :  */
     927                 :          0 : struct i2c_client *devm_i2c_new_dummy_device(struct device *dev,
     928                 :            :                                              struct i2c_adapter *adapter,
     929                 :            :                                              u16 address)
     930                 :            : {
     931                 :          0 :         struct i2c_dummy_devres *dr;
     932                 :          0 :         struct i2c_client *client;
     933                 :            : 
     934                 :          0 :         dr = devres_alloc(devm_i2c_release_dummy, sizeof(*dr), GFP_KERNEL);
     935         [ #  # ]:          0 :         if (!dr)
     936                 :            :                 return ERR_PTR(-ENOMEM);
     937                 :            : 
     938                 :          0 :         client = i2c_new_dummy_device(adapter, address);
     939         [ #  # ]:          0 :         if (IS_ERR(client)) {
     940                 :          0 :                 devres_free(dr);
     941                 :            :         } else {
     942                 :          0 :                 dr->client = client;
     943                 :          0 :                 devres_add(dev, dr);
     944                 :            :         }
     945                 :            : 
     946                 :            :         return client;
     947                 :            : }
     948                 :            : EXPORT_SYMBOL_GPL(devm_i2c_new_dummy_device);
     949                 :            : 
     950                 :            : /**
     951                 :            :  * i2c_new_ancillary_device - Helper to get the instantiated secondary address
     952                 :            :  * and create the associated device
     953                 :            :  * @client: Handle to the primary client
     954                 :            :  * @name: Handle to specify which secondary address to get
     955                 :            :  * @default_addr: Used as a fallback if no secondary address was specified
     956                 :            :  * Context: can sleep
     957                 :            :  *
     958                 :            :  * I2C clients can be composed of multiple I2C slaves bound together in a single
     959                 :            :  * component. The I2C client driver then binds to the master I2C slave and needs
     960                 :            :  * to create I2C dummy clients to communicate with all the other slaves.
     961                 :            :  *
     962                 :            :  * This function creates and returns an I2C dummy client whose I2C address is
     963                 :            :  * retrieved from the platform firmware based on the given slave name. If no
     964                 :            :  * address is specified by the firmware default_addr is used.
     965                 :            :  *
     966                 :            :  * On DT-based platforms the address is retrieved from the "reg" property entry
     967                 :            :  * cell whose "reg-names" value matches the slave name.
     968                 :            :  *
     969                 :            :  * This returns the new i2c client, which should be saved for later use with
     970                 :            :  * i2c_unregister_device(); or an ERR_PTR to describe the error.
     971                 :            :  */
     972                 :          0 : struct i2c_client *i2c_new_ancillary_device(struct i2c_client *client,
     973                 :            :                                                 const char *name,
     974                 :            :                                                 u16 default_addr)
     975                 :            : {
     976                 :          0 :         struct device_node *np = client->dev.of_node;
     977                 :          0 :         u32 addr = default_addr;
     978                 :          0 :         int i;
     979                 :            : 
     980                 :          0 :         if (np) {
     981                 :            :                 i = of_property_match_string(np, "reg-names", name);
     982                 :            :                 if (i >= 0)
     983                 :            :                         of_property_read_u32_index(np, "reg", i, &addr);
     984                 :            :         }
     985                 :            : 
     986                 :          0 :         dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr);
     987                 :          0 :         return i2c_new_dummy_device(client->adapter, addr);
     988                 :            : }
     989                 :            : EXPORT_SYMBOL_GPL(i2c_new_ancillary_device);
     990                 :            : 
     991                 :            : /* ------------------------------------------------------------------------- */
     992                 :            : 
     993                 :            : /* I2C bus adapters -- one roots each I2C or SMBUS segment */
     994                 :            : 
     995                 :          0 : static void i2c_adapter_dev_release(struct device *dev)
     996                 :            : {
     997                 :          0 :         struct i2c_adapter *adap = to_i2c_adapter(dev);
     998                 :          0 :         complete(&adap->dev_released);
     999                 :          0 : }
    1000                 :            : 
    1001                 :          0 : unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
    1002                 :            : {
    1003                 :          0 :         unsigned int depth = 0;
    1004                 :            : 
    1005                 :          0 :         while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
    1006                 :            :                 depth++;
    1007                 :            : 
    1008                 :          0 :         WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES,
    1009                 :            :                   "adapter depth exceeds lockdep subclass limit\n");
    1010                 :            : 
    1011                 :          0 :         return depth;
    1012                 :            : }
    1013                 :            : EXPORT_SYMBOL_GPL(i2c_adapter_depth);
    1014                 :            : 
    1015                 :            : /*
    1016                 :            :  * Let users instantiate I2C devices through sysfs. This can be used when
    1017                 :            :  * platform initialization code doesn't contain the proper data for
    1018                 :            :  * whatever reason. Also useful for drivers that do device detection and
    1019                 :            :  * detection fails, either because the device uses an unexpected address,
    1020                 :            :  * or this is a compatible device with different ID register values.
    1021                 :            :  *
    1022                 :            :  * Parameter checking may look overzealous, but we really don't want
    1023                 :            :  * the user to provide incorrect parameters.
    1024                 :            :  */
    1025                 :            : static ssize_t
    1026                 :          0 : new_device_store(struct device *dev, struct device_attribute *attr,
    1027                 :            :                  const char *buf, size_t count)
    1028                 :            : {
    1029                 :          0 :         struct i2c_adapter *adap = to_i2c_adapter(dev);
    1030                 :          0 :         struct i2c_board_info info;
    1031                 :          0 :         struct i2c_client *client;
    1032                 :          0 :         char *blank, end;
    1033                 :          0 :         int res;
    1034                 :            : 
    1035                 :          0 :         memset(&info, 0, sizeof(struct i2c_board_info));
    1036                 :            : 
    1037                 :          0 :         blank = strchr(buf, ' ');
    1038         [ #  # ]:          0 :         if (!blank) {
    1039                 :          0 :                 dev_err(dev, "%s: Missing parameters\n", "new_device");
    1040                 :          0 :                 return -EINVAL;
    1041                 :            :         }
    1042         [ #  # ]:          0 :         if (blank - buf > I2C_NAME_SIZE - 1) {
    1043                 :          0 :                 dev_err(dev, "%s: Invalid device name\n", "new_device");
    1044                 :          0 :                 return -EINVAL;
    1045                 :            :         }
    1046                 :          0 :         memcpy(info.type, buf, blank - buf);
    1047                 :            : 
    1048                 :            :         /* Parse remaining parameters, reject extra parameters */
    1049                 :          0 :         res = sscanf(++blank, "%hi%c", &info.addr, &end);
    1050         [ #  # ]:          0 :         if (res < 1) {
    1051                 :          0 :                 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
    1052                 :          0 :                 return -EINVAL;
    1053                 :            :         }
    1054   [ #  #  #  # ]:          0 :         if (res > 1  && end != '\n') {
    1055                 :          0 :                 dev_err(dev, "%s: Extra parameters\n", "new_device");
    1056                 :          0 :                 return -EINVAL;
    1057                 :            :         }
    1058                 :            : 
    1059         [ #  # ]:          0 :         if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) {
    1060                 :          0 :                 info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT;
    1061                 :          0 :                 info.flags |= I2C_CLIENT_TEN;
    1062                 :            :         }
    1063                 :            : 
    1064         [ #  # ]:          0 :         if (info.addr & I2C_ADDR_OFFSET_SLAVE) {
    1065                 :          0 :                 info.addr &= ~I2C_ADDR_OFFSET_SLAVE;
    1066                 :          0 :                 info.flags |= I2C_CLIENT_SLAVE;
    1067                 :            :         }
    1068                 :            : 
    1069                 :          0 :         client = i2c_new_client_device(adap, &info);
    1070         [ #  # ]:          0 :         if (IS_ERR(client))
    1071                 :          0 :                 return PTR_ERR(client);
    1072                 :            : 
    1073                 :            :         /* Keep track of the added device */
    1074                 :          0 :         mutex_lock(&adap->userspace_clients_lock);
    1075                 :          0 :         list_add_tail(&client->detected, &adap->userspace_clients);
    1076                 :          0 :         mutex_unlock(&adap->userspace_clients_lock);
    1077                 :          0 :         dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
    1078                 :            :                  info.type, info.addr);
    1079                 :            : 
    1080                 :          0 :         return count;
    1081                 :            : }
    1082                 :            : static DEVICE_ATTR_WO(new_device);
    1083                 :            : 
    1084                 :            : /*
    1085                 :            :  * And of course let the users delete the devices they instantiated, if
    1086                 :            :  * they got it wrong. This interface can only be used to delete devices
    1087                 :            :  * instantiated by i2c_sysfs_new_device above. This guarantees that we
    1088                 :            :  * don't delete devices to which some kernel code still has references.
    1089                 :            :  *
    1090                 :            :  * Parameter checking may look overzealous, but we really don't want
    1091                 :            :  * the user to delete the wrong device.
    1092                 :            :  */
    1093                 :            : static ssize_t
    1094                 :          0 : delete_device_store(struct device *dev, struct device_attribute *attr,
    1095                 :            :                     const char *buf, size_t count)
    1096                 :            : {
    1097                 :          0 :         struct i2c_adapter *adap = to_i2c_adapter(dev);
    1098                 :          0 :         struct i2c_client *client, *next;
    1099                 :          0 :         unsigned short addr;
    1100                 :          0 :         char end;
    1101                 :          0 :         int res;
    1102                 :            : 
    1103                 :            :         /* Parse parameters, reject extra parameters */
    1104                 :          0 :         res = sscanf(buf, "%hi%c", &addr, &end);
    1105         [ #  # ]:          0 :         if (res < 1) {
    1106                 :          0 :                 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
    1107                 :          0 :                 return -EINVAL;
    1108                 :            :         }
    1109   [ #  #  #  # ]:          0 :         if (res > 1  && end != '\n') {
    1110                 :          0 :                 dev_err(dev, "%s: Extra parameters\n", "delete_device");
    1111                 :          0 :                 return -EINVAL;
    1112                 :            :         }
    1113                 :            : 
    1114                 :            :         /* Make sure the device was added through sysfs */
    1115                 :          0 :         res = -ENOENT;
    1116                 :          0 :         mutex_lock_nested(&adap->userspace_clients_lock,
    1117                 :            :                           i2c_adapter_depth(adap));
    1118         [ #  # ]:          0 :         list_for_each_entry_safe(client, next, &adap->userspace_clients,
    1119                 :            :                                  detected) {
    1120   [ #  #  #  # ]:          0 :                 if (i2c_encode_flags_to_addr(client) == addr) {
    1121                 :          0 :                         dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
    1122                 :            :                                  "delete_device", client->name, client->addr);
    1123                 :            : 
    1124                 :          0 :                         list_del(&client->detected);
    1125                 :          0 :                         i2c_unregister_device(client);
    1126                 :          0 :                         res = count;
    1127                 :          0 :                         break;
    1128                 :            :                 }
    1129                 :            :         }
    1130                 :          0 :         mutex_unlock(&adap->userspace_clients_lock);
    1131                 :            : 
    1132         [ #  # ]:          0 :         if (res < 0)
    1133                 :          0 :                 dev_err(dev, "%s: Can't find device in list\n",
    1134                 :            :                         "delete_device");
    1135                 :          0 :         return res;
    1136                 :            : }
    1137                 :            : static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
    1138                 :            :                                   delete_device_store);
    1139                 :            : 
    1140                 :            : static struct attribute *i2c_adapter_attrs[] = {
    1141                 :            :         &dev_attr_name.attr,
    1142                 :            :         &dev_attr_new_device.attr,
    1143                 :            :         &dev_attr_delete_device.attr,
    1144                 :            :         NULL
    1145                 :            : };
    1146                 :            : ATTRIBUTE_GROUPS(i2c_adapter);
    1147                 :            : 
    1148                 :            : struct device_type i2c_adapter_type = {
    1149                 :            :         .groups         = i2c_adapter_groups,
    1150                 :            :         .release        = i2c_adapter_dev_release,
    1151                 :            : };
    1152                 :            : EXPORT_SYMBOL_GPL(i2c_adapter_type);
    1153                 :            : 
    1154                 :            : /**
    1155                 :            :  * i2c_verify_adapter - return parameter as i2c_adapter or NULL
    1156                 :            :  * @dev: device, probably from some driver model iterator
    1157                 :            :  *
    1158                 :            :  * When traversing the driver model tree, perhaps using driver model
    1159                 :            :  * iterators like @device_for_each_child(), you can't assume very much
    1160                 :            :  * about the nodes you find.  Use this function to avoid oopses caused
    1161                 :            :  * by wrongly treating some non-I2C device as an i2c_adapter.
    1162                 :            :  */
    1163                 :          0 : struct i2c_adapter *i2c_verify_adapter(struct device *dev)
    1164                 :            : {
    1165                 :          0 :         return (dev->type == &i2c_adapter_type)
    1166                 :          0 :                         ? to_i2c_adapter(dev)
    1167         [ #  # ]:          0 :                         : NULL;
    1168                 :            : }
    1169                 :            : EXPORT_SYMBOL(i2c_verify_adapter);
    1170                 :            : 
    1171                 :            : #ifdef CONFIG_I2C_COMPAT
    1172                 :            : static struct class_compat *i2c_adapter_compat_class;
    1173                 :            : #endif
    1174                 :            : 
    1175                 :          0 : static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
    1176                 :            : {
    1177                 :          0 :         struct i2c_devinfo      *devinfo;
    1178                 :            : 
    1179                 :          0 :         down_read(&__i2c_board_lock);
    1180         [ #  # ]:          0 :         list_for_each_entry(devinfo, &__i2c_board_list, list) {
    1181   [ #  #  #  # ]:          0 :                 if (devinfo->busnum == adapter->nr &&
    1182                 :          0 :                     IS_ERR(i2c_new_client_device(adapter, &devinfo->board_info)))
    1183                 :          0 :                         dev_err(&adapter->dev,
    1184                 :            :                                 "Can't create device at 0x%02x\n",
    1185                 :            :                                 devinfo->board_info.addr);
    1186                 :            :         }
    1187                 :          0 :         up_read(&__i2c_board_lock);
    1188                 :          0 : }
    1189                 :            : 
    1190                 :          0 : static int i2c_do_add_adapter(struct i2c_driver *driver,
    1191                 :            :                               struct i2c_adapter *adap)
    1192                 :            : {
    1193                 :            :         /* Detect supported devices on that bus, and instantiate them */
    1194                 :          0 :         i2c_detect(adap, driver);
    1195                 :            : 
    1196                 :          0 :         return 0;
    1197                 :            : }
    1198                 :            : 
    1199                 :          0 : static int __process_new_adapter(struct device_driver *d, void *data)
    1200                 :            : {
    1201                 :          0 :         return i2c_do_add_adapter(to_i2c_driver(d), data);
    1202                 :            : }
    1203                 :            : 
    1204                 :            : static const struct i2c_lock_operations i2c_adapter_lock_ops = {
    1205                 :            :         .lock_bus =    i2c_adapter_lock_bus,
    1206                 :            :         .trylock_bus = i2c_adapter_trylock_bus,
    1207                 :            :         .unlock_bus =  i2c_adapter_unlock_bus,
    1208                 :            : };
    1209                 :            : 
    1210                 :            : static void i2c_host_notify_irq_teardown(struct i2c_adapter *adap)
    1211                 :            : {
    1212                 :            :         struct irq_domain *domain = adap->host_notify_domain;
    1213                 :            :         irq_hw_number_t hwirq;
    1214                 :            : 
    1215                 :            :         if (!domain)
    1216                 :            :                 return;
    1217                 :            : 
    1218                 :            :         for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++)
    1219                 :            :                 irq_dispose_mapping(irq_find_mapping(domain, hwirq));
    1220                 :            : 
    1221                 :            :         irq_domain_remove(domain);
    1222                 :            :         adap->host_notify_domain = NULL;
    1223                 :            : }
    1224                 :            : 
    1225                 :          0 : static int i2c_host_notify_irq_map(struct irq_domain *h,
    1226                 :            :                                           unsigned int virq,
    1227                 :            :                                           irq_hw_number_t hw_irq_num)
    1228                 :            : {
    1229                 :          0 :         irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq);
    1230                 :            : 
    1231                 :          0 :         return 0;
    1232                 :            : }
    1233                 :            : 
    1234                 :            : static const struct irq_domain_ops i2c_host_notify_irq_ops = {
    1235                 :            :         .map = i2c_host_notify_irq_map,
    1236                 :            : };
    1237                 :            : 
    1238                 :          0 : static int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap)
    1239                 :            : {
    1240                 :          0 :         struct irq_domain *domain;
    1241                 :            : 
    1242         [ #  # ]:          0 :         if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY))
    1243                 :            :                 return 0;
    1244                 :            : 
    1245                 :          0 :         domain = irq_domain_create_linear(adap->dev.fwnode,
    1246                 :            :                                           I2C_ADDR_7BITS_COUNT,
    1247                 :            :                                           &i2c_host_notify_irq_ops, adap);
    1248         [ #  # ]:          0 :         if (!domain)
    1249                 :            :                 return -ENOMEM;
    1250                 :            : 
    1251                 :          0 :         adap->host_notify_domain = domain;
    1252                 :            : 
    1253                 :          0 :         return 0;
    1254                 :            : }
    1255                 :            : 
    1256                 :            : /**
    1257                 :            :  * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct
    1258                 :            :  * I2C client.
    1259                 :            :  * @adap: the adapter
    1260                 :            :  * @addr: the I2C address of the notifying device
    1261                 :            :  * Context: can't sleep
    1262                 :            :  *
    1263                 :            :  * Helper function to be called from an I2C bus driver's interrupt
    1264                 :            :  * handler. It will schedule the Host Notify IRQ.
    1265                 :            :  */
    1266                 :          0 : int i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr)
    1267                 :            : {
    1268                 :          0 :         int irq;
    1269                 :            : 
    1270         [ #  # ]:          0 :         if (!adap)
    1271                 :            :                 return -EINVAL;
    1272                 :            : 
    1273                 :          0 :         irq = irq_find_mapping(adap->host_notify_domain, addr);
    1274         [ #  # ]:          0 :         if (irq <= 0)
    1275                 :            :                 return -ENXIO;
    1276                 :            : 
    1277                 :          0 :         generic_handle_irq(irq);
    1278                 :            : 
    1279                 :          0 :         return 0;
    1280                 :            : }
    1281                 :            : EXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify);
    1282                 :            : 
    1283                 :          0 : static int i2c_register_adapter(struct i2c_adapter *adap)
    1284                 :            : {
    1285                 :          0 :         int res = -EINVAL;
    1286                 :            : 
    1287                 :            :         /* Can't register until after driver model init */
    1288   [ #  #  #  # ]:          0 :         if (WARN_ON(!is_registered)) {
    1289                 :          0 :                 res = -EAGAIN;
    1290                 :          0 :                 goto out_list;
    1291                 :            :         }
    1292                 :            : 
    1293                 :            :         /* Sanity checks */
    1294   [ #  #  #  # ]:          0 :         if (WARN(!adap->name[0], "i2c adapter has no name"))
    1295                 :          0 :                 goto out_list;
    1296                 :            : 
    1297         [ #  # ]:          0 :         if (!adap->algo) {
    1298                 :          0 :                 pr_err("adapter '%s': no algo supplied!\n", adap->name);
    1299                 :          0 :                 goto out_list;
    1300                 :            :         }
    1301                 :            : 
    1302         [ #  # ]:          0 :         if (!adap->lock_ops)
    1303                 :          0 :                 adap->lock_ops = &i2c_adapter_lock_ops;
    1304                 :            : 
    1305                 :          0 :         adap->locked_flags = 0;
    1306                 :          0 :         rt_mutex_init(&adap->bus_lock);
    1307                 :          0 :         rt_mutex_init(&adap->mux_lock);
    1308                 :          0 :         mutex_init(&adap->userspace_clients_lock);
    1309         [ #  # ]:          0 :         INIT_LIST_HEAD(&adap->userspace_clients);
    1310                 :            : 
    1311                 :            :         /* Set default timeout to 1 second if not already set */
    1312         [ #  # ]:          0 :         if (adap->timeout == 0)
    1313                 :          0 :                 adap->timeout = HZ;
    1314                 :            : 
    1315                 :            :         /* register soft irqs for Host Notify */
    1316                 :          0 :         res = i2c_setup_host_notify_irq_domain(adap);
    1317         [ #  # ]:          0 :         if (res) {
    1318                 :          0 :                 pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n",
    1319                 :            :                        adap->name, res);
    1320                 :          0 :                 goto out_list;
    1321                 :            :         }
    1322                 :            : 
    1323                 :          0 :         dev_set_name(&adap->dev, "i2c-%d", adap->nr);
    1324                 :          0 :         adap->dev.bus = &i2c_bus_type;
    1325                 :          0 :         adap->dev.type = &i2c_adapter_type;
    1326                 :          0 :         res = device_register(&adap->dev);
    1327         [ #  # ]:          0 :         if (res) {
    1328                 :          0 :                 pr_err("adapter '%s': can't register device (%d)\n", adap->name, res);
    1329                 :          0 :                 goto out_list;
    1330                 :            :         }
    1331                 :            : 
    1332                 :          0 :         res = of_i2c_setup_smbus_alert(adap);
    1333                 :          0 :         if (res)
    1334                 :            :                 goto out_reg;
    1335                 :            : 
    1336                 :          0 :         dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
    1337                 :            : 
    1338                 :          0 :         pm_runtime_no_callbacks(&adap->dev);
    1339                 :          0 :         pm_suspend_ignore_children(&adap->dev, true);
    1340                 :          0 :         pm_runtime_enable(&adap->dev);
    1341                 :            : 
    1342                 :            : #ifdef CONFIG_I2C_COMPAT
    1343                 :          0 :         res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
    1344                 :            :                                        adap->dev.parent);
    1345         [ #  # ]:          0 :         if (res)
    1346                 :          0 :                 dev_warn(&adap->dev,
    1347                 :            :                          "Failed to create compatibility class link\n");
    1348                 :            : #endif
    1349                 :            : 
    1350                 :          0 :         i2c_init_recovery(adap);
    1351                 :            : 
    1352                 :            :         /* create pre-declared device nodes */
    1353                 :          0 :         of_i2c_register_devices(adap);
    1354                 :          0 :         i2c_acpi_register_devices(adap);
    1355                 :          0 :         i2c_acpi_install_space_handler(adap);
    1356                 :            : 
    1357         [ #  # ]:          0 :         if (adap->nr < __i2c_first_dynamic_bus_num)
    1358                 :          0 :                 i2c_scan_static_board_info(adap);
    1359                 :            : 
    1360                 :            :         /* Notify drivers */
    1361                 :          0 :         mutex_lock(&core_lock);
    1362                 :          0 :         bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
    1363                 :          0 :         mutex_unlock(&core_lock);
    1364                 :            : 
    1365                 :          0 :         return 0;
    1366                 :            : 
    1367                 :            : out_reg:
    1368                 :            :         init_completion(&adap->dev_released);
    1369                 :            :         device_unregister(&adap->dev);
    1370                 :            :         wait_for_completion(&adap->dev_released);
    1371                 :          0 : out_list:
    1372                 :          0 :         mutex_lock(&core_lock);
    1373                 :          0 :         idr_remove(&i2c_adapter_idr, adap->nr);
    1374                 :          0 :         mutex_unlock(&core_lock);
    1375                 :          0 :         return res;
    1376                 :            : }
    1377                 :            : 
    1378                 :            : /**
    1379                 :            :  * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
    1380                 :            :  * @adap: the adapter to register (with adap->nr initialized)
    1381                 :            :  * Context: can sleep
    1382                 :            :  *
    1383                 :            :  * See i2c_add_numbered_adapter() for details.
    1384                 :            :  */
    1385                 :          0 : static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
    1386                 :            : {
    1387                 :          0 :         int id;
    1388                 :            : 
    1389                 :          0 :         mutex_lock(&core_lock);
    1390                 :          0 :         id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL);
    1391                 :          0 :         mutex_unlock(&core_lock);
    1392   [ #  #  #  # ]:          0 :         if (WARN(id < 0, "couldn't get idr"))
    1393         [ #  # ]:          0 :                 return id == -ENOSPC ? -EBUSY : id;
    1394                 :            : 
    1395                 :          0 :         return i2c_register_adapter(adap);
    1396                 :            : }
    1397                 :            : 
    1398                 :            : /**
    1399                 :            :  * i2c_add_adapter - declare i2c adapter, use dynamic bus number
    1400                 :            :  * @adapter: the adapter to add
    1401                 :            :  * Context: can sleep
    1402                 :            :  *
    1403                 :            :  * This routine is used to declare an I2C adapter when its bus number
    1404                 :            :  * doesn't matter or when its bus number is specified by an dt alias.
    1405                 :            :  * Examples of bases when the bus number doesn't matter: I2C adapters
    1406                 :            :  * dynamically added by USB links or PCI plugin cards.
    1407                 :            :  *
    1408                 :            :  * When this returns zero, a new bus number was allocated and stored
    1409                 :            :  * in adap->nr, and the specified adapter became available for clients.
    1410                 :            :  * Otherwise, a negative errno value is returned.
    1411                 :            :  */
    1412                 :          0 : int i2c_add_adapter(struct i2c_adapter *adapter)
    1413                 :            : {
    1414                 :          0 :         struct device *dev = &adapter->dev;
    1415                 :          0 :         int id;
    1416                 :            : 
    1417                 :          0 :         if (dev->of_node) {
    1418                 :            :                 id = of_alias_get_id(dev->of_node, "i2c");
    1419                 :            :                 if (id >= 0) {
    1420                 :            :                         adapter->nr = id;
    1421                 :            :                         return __i2c_add_numbered_adapter(adapter);
    1422                 :            :                 }
    1423                 :            :         }
    1424                 :            : 
    1425                 :          0 :         mutex_lock(&core_lock);
    1426                 :          0 :         id = idr_alloc(&i2c_adapter_idr, adapter,
    1427                 :            :                        __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
    1428                 :          0 :         mutex_unlock(&core_lock);
    1429   [ #  #  #  # ]:          0 :         if (WARN(id < 0, "couldn't get idr"))
    1430                 :            :                 return id;
    1431                 :            : 
    1432                 :          0 :         adapter->nr = id;
    1433                 :            : 
    1434                 :          0 :         return i2c_register_adapter(adapter);
    1435                 :            : }
    1436                 :            : EXPORT_SYMBOL(i2c_add_adapter);
    1437                 :            : 
    1438                 :            : /**
    1439                 :            :  * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
    1440                 :            :  * @adap: the adapter to register (with adap->nr initialized)
    1441                 :            :  * Context: can sleep
    1442                 :            :  *
    1443                 :            :  * This routine is used to declare an I2C adapter when its bus number
    1444                 :            :  * matters.  For example, use it for I2C adapters from system-on-chip CPUs,
    1445                 :            :  * or otherwise built in to the system's mainboard, and where i2c_board_info
    1446                 :            :  * is used to properly configure I2C devices.
    1447                 :            :  *
    1448                 :            :  * If the requested bus number is set to -1, then this function will behave
    1449                 :            :  * identically to i2c_add_adapter, and will dynamically assign a bus number.
    1450                 :            :  *
    1451                 :            :  * If no devices have pre-been declared for this bus, then be sure to
    1452                 :            :  * register the adapter before any dynamically allocated ones.  Otherwise
    1453                 :            :  * the required bus ID may not be available.
    1454                 :            :  *
    1455                 :            :  * When this returns zero, the specified adapter became available for
    1456                 :            :  * clients using the bus number provided in adap->nr.  Also, the table
    1457                 :            :  * of I2C devices pre-declared using i2c_register_board_info() is scanned,
    1458                 :            :  * and the appropriate driver model device nodes are created.  Otherwise, a
    1459                 :            :  * negative errno value is returned.
    1460                 :            :  */
    1461                 :          0 : int i2c_add_numbered_adapter(struct i2c_adapter *adap)
    1462                 :            : {
    1463         [ #  # ]:          0 :         if (adap->nr == -1) /* -1 means dynamically assign bus id */
    1464                 :          0 :                 return i2c_add_adapter(adap);
    1465                 :            : 
    1466                 :          0 :         return __i2c_add_numbered_adapter(adap);
    1467                 :            : }
    1468                 :            : EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
    1469                 :            : 
    1470                 :          0 : static void i2c_do_del_adapter(struct i2c_driver *driver,
    1471                 :            :                               struct i2c_adapter *adapter)
    1472                 :            : {
    1473                 :          0 :         struct i2c_client *client, *_n;
    1474                 :            : 
    1475                 :            :         /* Remove the devices we created ourselves as the result of hardware
    1476                 :            :          * probing (using a driver's detect method) */
    1477         [ #  # ]:          0 :         list_for_each_entry_safe(client, _n, &driver->clients, detected) {
    1478         [ #  # ]:          0 :                 if (client->adapter == adapter) {
    1479                 :          0 :                         dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
    1480                 :            :                                 client->name, client->addr);
    1481                 :          0 :                         list_del(&client->detected);
    1482                 :          0 :                         i2c_unregister_device(client);
    1483                 :            :                 }
    1484                 :            :         }
    1485                 :          0 : }
    1486                 :            : 
    1487                 :          0 : static int __unregister_client(struct device *dev, void *dummy)
    1488                 :            : {
    1489         [ #  # ]:          0 :         struct i2c_client *client = i2c_verify_client(dev);
    1490   [ #  #  #  # ]:          0 :         if (client && strcmp(client->name, "dummy"))
    1491                 :          0 :                 i2c_unregister_device(client);
    1492                 :          0 :         return 0;
    1493                 :            : }
    1494                 :            : 
    1495                 :          0 : static int __unregister_dummy(struct device *dev, void *dummy)
    1496                 :            : {
    1497         [ #  # ]:          0 :         struct i2c_client *client = i2c_verify_client(dev);
    1498                 :          0 :         i2c_unregister_device(client);
    1499                 :          0 :         return 0;
    1500                 :            : }
    1501                 :            : 
    1502                 :          0 : static int __process_removed_adapter(struct device_driver *d, void *data)
    1503                 :            : {
    1504                 :          0 :         i2c_do_del_adapter(to_i2c_driver(d), data);
    1505                 :          0 :         return 0;
    1506                 :            : }
    1507                 :            : 
    1508                 :            : /**
    1509                 :            :  * i2c_del_adapter - unregister I2C adapter
    1510                 :            :  * @adap: the adapter being unregistered
    1511                 :            :  * Context: can sleep
    1512                 :            :  *
    1513                 :            :  * This unregisters an I2C adapter which was previously registered
    1514                 :            :  * by @i2c_add_adapter or @i2c_add_numbered_adapter.
    1515                 :            :  */
    1516                 :          0 : void i2c_del_adapter(struct i2c_adapter *adap)
    1517                 :            : {
    1518                 :          0 :         struct i2c_adapter *found;
    1519                 :          0 :         struct i2c_client *client, *next;
    1520                 :            : 
    1521                 :            :         /* First make sure that this adapter was ever added */
    1522                 :          0 :         mutex_lock(&core_lock);
    1523                 :          0 :         found = idr_find(&i2c_adapter_idr, adap->nr);
    1524                 :          0 :         mutex_unlock(&core_lock);
    1525         [ #  # ]:          0 :         if (found != adap) {
    1526                 :            :                 pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name);
    1527                 :            :                 return;
    1528                 :            :         }
    1529                 :            : 
    1530                 :          0 :         i2c_acpi_remove_space_handler(adap);
    1531                 :            :         /* Tell drivers about this removal */
    1532                 :          0 :         mutex_lock(&core_lock);
    1533                 :          0 :         bus_for_each_drv(&i2c_bus_type, NULL, adap,
    1534                 :            :                                __process_removed_adapter);
    1535                 :          0 :         mutex_unlock(&core_lock);
    1536                 :            : 
    1537                 :            :         /* Remove devices instantiated from sysfs */
    1538                 :          0 :         mutex_lock_nested(&adap->userspace_clients_lock,
    1539                 :            :                           i2c_adapter_depth(adap));
    1540         [ #  # ]:          0 :         list_for_each_entry_safe(client, next, &adap->userspace_clients,
    1541                 :            :                                  detected) {
    1542                 :          0 :                 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
    1543                 :            :                         client->addr);
    1544                 :          0 :                 list_del(&client->detected);
    1545                 :          0 :                 i2c_unregister_device(client);
    1546                 :            :         }
    1547                 :          0 :         mutex_unlock(&adap->userspace_clients_lock);
    1548                 :            : 
    1549                 :            :         /* Detach any active clients. This can't fail, thus we do not
    1550                 :            :          * check the returned value. This is a two-pass process, because
    1551                 :            :          * we can't remove the dummy devices during the first pass: they
    1552                 :            :          * could have been instantiated by real devices wishing to clean
    1553                 :            :          * them up properly, so we give them a chance to do that first. */
    1554                 :          0 :         device_for_each_child(&adap->dev, NULL, __unregister_client);
    1555                 :          0 :         device_for_each_child(&adap->dev, NULL, __unregister_dummy);
    1556                 :            : 
    1557                 :            : #ifdef CONFIG_I2C_COMPAT
    1558                 :          0 :         class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
    1559                 :            :                                  adap->dev.parent);
    1560                 :            : #endif
    1561                 :            : 
    1562                 :            :         /* device name is gone after device_unregister */
    1563                 :          0 :         dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
    1564                 :            : 
    1565                 :          0 :         pm_runtime_disable(&adap->dev);
    1566                 :            : 
    1567                 :          0 :         i2c_host_notify_irq_teardown(adap);
    1568                 :            : 
    1569                 :            :         /* wait until all references to the device are gone
    1570                 :            :          *
    1571                 :            :          * FIXME: This is old code and should ideally be replaced by an
    1572                 :            :          * alternative which results in decoupling the lifetime of the struct
    1573                 :            :          * device from the i2c_adapter, like spi or netdev do. Any solution
    1574                 :            :          * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
    1575                 :            :          */
    1576                 :          0 :         init_completion(&adap->dev_released);
    1577                 :          0 :         device_unregister(&adap->dev);
    1578                 :          0 :         wait_for_completion(&adap->dev_released);
    1579                 :            : 
    1580                 :            :         /* free bus id */
    1581                 :          0 :         mutex_lock(&core_lock);
    1582                 :          0 :         idr_remove(&i2c_adapter_idr, adap->nr);
    1583                 :          0 :         mutex_unlock(&core_lock);
    1584                 :            : 
    1585                 :            :         /* Clear the device structure in case this adapter is ever going to be
    1586                 :            :            added again */
    1587                 :          0 :         memset(&adap->dev, 0, sizeof(adap->dev));
    1588                 :            : }
    1589                 :            : EXPORT_SYMBOL(i2c_del_adapter);
    1590                 :            : 
    1591                 :            : /**
    1592                 :            :  * i2c_parse_fw_timings - get I2C related timing parameters from firmware
    1593                 :            :  * @dev: The device to scan for I2C timing properties
    1594                 :            :  * @t: the i2c_timings struct to be filled with values
    1595                 :            :  * @use_defaults: bool to use sane defaults derived from the I2C specification
    1596                 :            :  *                when properties are not found, otherwise use 0
    1597                 :            :  *
    1598                 :            :  * Scan the device for the generic I2C properties describing timing parameters
    1599                 :            :  * for the signal and fill the given struct with the results. If a property was
    1600                 :            :  * not found and use_defaults was true, then maximum timings are assumed which
    1601                 :            :  * are derived from the I2C specification. If use_defaults is not used, the
    1602                 :            :  * results will be 0, so drivers can apply their own defaults later. The latter
    1603                 :            :  * is mainly intended for avoiding regressions of existing drivers which want
    1604                 :            :  * to switch to this function. New drivers almost always should use the defaults.
    1605                 :            :  */
    1606                 :            : 
    1607                 :          0 : void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults)
    1608                 :            : {
    1609                 :          0 :         int ret;
    1610                 :            : 
    1611                 :          0 :         memset(t, 0, sizeof(*t));
    1612                 :            : 
    1613                 :          0 :         ret = device_property_read_u32(dev, "clock-frequency", &t->bus_freq_hz);
    1614         [ #  # ]:          0 :         if (ret && use_defaults)
    1615                 :          0 :                 t->bus_freq_hz = 100000;
    1616                 :            : 
    1617                 :          0 :         ret = device_property_read_u32(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns);
    1618         [ #  # ]:          0 :         if (ret && use_defaults) {
    1619         [ #  # ]:          0 :                 if (t->bus_freq_hz <= 100000)
    1620                 :          0 :                         t->scl_rise_ns = 1000;
    1621         [ #  # ]:          0 :                 else if (t->bus_freq_hz <= 400000)
    1622                 :          0 :                         t->scl_rise_ns = 300;
    1623                 :            :                 else
    1624                 :          0 :                         t->scl_rise_ns = 120;
    1625                 :            :         }
    1626                 :            : 
    1627                 :          0 :         ret = device_property_read_u32(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns);
    1628         [ #  # ]:          0 :         if (ret && use_defaults) {
    1629         [ #  # ]:          0 :                 if (t->bus_freq_hz <= 400000)
    1630                 :          0 :                         t->scl_fall_ns = 300;
    1631                 :            :                 else
    1632                 :          0 :                         t->scl_fall_ns = 120;
    1633                 :            :         }
    1634                 :            : 
    1635                 :          0 :         device_property_read_u32(dev, "i2c-scl-internal-delay-ns", &t->scl_int_delay_ns);
    1636                 :            : 
    1637                 :          0 :         ret = device_property_read_u32(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns);
    1638         [ #  # ]:          0 :         if (ret && use_defaults)
    1639                 :          0 :                 t->sda_fall_ns = t->scl_fall_ns;
    1640                 :            : 
    1641                 :          0 :         device_property_read_u32(dev, "i2c-sda-hold-time-ns", &t->sda_hold_ns);
    1642                 :            : 
    1643                 :          0 :         device_property_read_u32(dev, "i2c-digital-filter-width-ns",
    1644                 :            :                                  &t->digital_filter_width_ns);
    1645                 :            : 
    1646                 :          0 :         device_property_read_u32(dev, "i2c-analog-filter-cutoff-frequency",
    1647                 :            :                                  &t->analog_filter_cutoff_freq_hz);
    1648                 :          0 : }
    1649                 :            : EXPORT_SYMBOL_GPL(i2c_parse_fw_timings);
    1650                 :            : 
    1651                 :            : /* ------------------------------------------------------------------------- */
    1652                 :            : 
    1653                 :         42 : int i2c_for_each_dev(void *data, int (*fn)(struct device *dev, void *data))
    1654                 :            : {
    1655                 :         42 :         int res;
    1656                 :            : 
    1657                 :         42 :         mutex_lock(&core_lock);
    1658                 :         42 :         res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
    1659                 :         42 :         mutex_unlock(&core_lock);
    1660                 :            : 
    1661                 :         42 :         return res;
    1662                 :            : }
    1663                 :            : EXPORT_SYMBOL_GPL(i2c_for_each_dev);
    1664                 :            : 
    1665                 :          0 : static int __process_new_driver(struct device *dev, void *data)
    1666                 :            : {
    1667         [ #  # ]:          0 :         if (dev->type != &i2c_adapter_type)
    1668                 :            :                 return 0;
    1669                 :          0 :         return i2c_do_add_adapter(data, to_i2c_adapter(dev));
    1670                 :            : }
    1671                 :            : 
    1672                 :            : /*
    1673                 :            :  * An i2c_driver is used with one or more i2c_client (device) nodes to access
    1674                 :            :  * i2c slave chips, on a bus instance associated with some i2c_adapter.
    1675                 :            :  */
    1676                 :            : 
    1677                 :         42 : int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
    1678                 :            : {
    1679                 :         42 :         int res;
    1680                 :            : 
    1681                 :            :         /* Can't register until after driver model init */
    1682   [ -  +  +  - ]:         42 :         if (WARN_ON(!is_registered))
    1683                 :            :                 return -EAGAIN;
    1684                 :            : 
    1685                 :            :         /* add the driver to the list of i2c drivers in the driver core */
    1686                 :         42 :         driver->driver.owner = owner;
    1687                 :         42 :         driver->driver.bus = &i2c_bus_type;
    1688                 :         42 :         INIT_LIST_HEAD(&driver->clients);
    1689                 :            : 
    1690                 :            :         /* When registration returns, the driver core
    1691                 :            :          * will have called probe() for all matching-but-unbound devices.
    1692                 :            :          */
    1693                 :         42 :         res = driver_register(&driver->driver);
    1694         [ +  - ]:         42 :         if (res)
    1695                 :            :                 return res;
    1696                 :            : 
    1697                 :         42 :         pr_debug("driver [%s] registered\n", driver->driver.name);
    1698                 :            : 
    1699                 :            :         /* Walk the adapters that are already present */
    1700                 :         42 :         i2c_for_each_dev(driver, __process_new_driver);
    1701                 :            : 
    1702                 :         42 :         return 0;
    1703                 :            : }
    1704                 :            : EXPORT_SYMBOL(i2c_register_driver);
    1705                 :            : 
    1706                 :          0 : static int __process_removed_driver(struct device *dev, void *data)
    1707                 :            : {
    1708         [ #  # ]:          0 :         if (dev->type == &i2c_adapter_type)
    1709                 :          0 :                 i2c_do_del_adapter(data, to_i2c_adapter(dev));
    1710                 :          0 :         return 0;
    1711                 :            : }
    1712                 :            : 
    1713                 :            : /**
    1714                 :            :  * i2c_del_driver - unregister I2C driver
    1715                 :            :  * @driver: the driver being unregistered
    1716                 :            :  * Context: can sleep
    1717                 :            :  */
    1718                 :          0 : void i2c_del_driver(struct i2c_driver *driver)
    1719                 :            : {
    1720                 :          0 :         i2c_for_each_dev(driver, __process_removed_driver);
    1721                 :            : 
    1722                 :          0 :         driver_unregister(&driver->driver);
    1723                 :          0 :         pr_debug("driver [%s] unregistered\n", driver->driver.name);
    1724                 :          0 : }
    1725                 :            : EXPORT_SYMBOL(i2c_del_driver);
    1726                 :            : 
    1727                 :            : /* ------------------------------------------------------------------------- */
    1728                 :            : 
    1729                 :            : struct i2c_cmd_arg {
    1730                 :            :         unsigned        cmd;
    1731                 :            :         void            *arg;
    1732                 :            : };
    1733                 :            : 
    1734                 :          0 : static int i2c_cmd(struct device *dev, void *_arg)
    1735                 :            : {
    1736         [ #  # ]:          0 :         struct i2c_client       *client = i2c_verify_client(dev);
    1737                 :          0 :         struct i2c_cmd_arg      *arg = _arg;
    1738                 :          0 :         struct i2c_driver       *driver;
    1739                 :            : 
    1740   [ #  #  #  # ]:          0 :         if (!client || !client->dev.driver)
    1741                 :            :                 return 0;
    1742                 :            : 
    1743                 :          0 :         driver = to_i2c_driver(client->dev.driver);
    1744         [ #  # ]:          0 :         if (driver->command)
    1745                 :          0 :                 driver->command(client, arg->cmd, arg->arg);
    1746                 :            :         return 0;
    1747                 :            : }
    1748                 :            : 
    1749                 :          0 : void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
    1750                 :            : {
    1751                 :          0 :         struct i2c_cmd_arg      cmd_arg;
    1752                 :            : 
    1753                 :          0 :         cmd_arg.cmd = cmd;
    1754                 :          0 :         cmd_arg.arg = arg;
    1755                 :          0 :         device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
    1756                 :          0 : }
    1757                 :            : EXPORT_SYMBOL(i2c_clients_command);
    1758                 :            : 
    1759                 :         21 : static int __init i2c_init(void)
    1760                 :            : {
    1761                 :         21 :         int retval;
    1762                 :            : 
    1763                 :         21 :         retval = of_alias_get_highest_id("i2c");
    1764                 :            : 
    1765                 :         21 :         down_write(&__i2c_board_lock);
    1766         [ -  + ]:         21 :         if (retval >= __i2c_first_dynamic_bus_num)
    1767                 :          0 :                 __i2c_first_dynamic_bus_num = retval + 1;
    1768                 :         21 :         up_write(&__i2c_board_lock);
    1769                 :            : 
    1770                 :         21 :         retval = bus_register(&i2c_bus_type);
    1771         [ +  - ]:         21 :         if (retval)
    1772                 :            :                 return retval;
    1773                 :            : 
    1774                 :         21 :         is_registered = true;
    1775                 :            : 
    1776                 :            : #ifdef CONFIG_I2C_COMPAT
    1777                 :         21 :         i2c_adapter_compat_class = class_compat_register("i2c-adapter");
    1778         [ -  + ]:         21 :         if (!i2c_adapter_compat_class) {
    1779                 :          0 :                 retval = -ENOMEM;
    1780                 :          0 :                 goto bus_err;
    1781                 :            :         }
    1782                 :            : #endif
    1783                 :         21 :         retval = i2c_add_driver(&dummy_driver);
    1784         [ -  + ]:         21 :         if (retval)
    1785                 :          0 :                 goto class_err;
    1786                 :            : 
    1787                 :         21 :         if (IS_ENABLED(CONFIG_OF_DYNAMIC))
    1788                 :            :                 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier));
    1789                 :         21 :         if (IS_ENABLED(CONFIG_ACPI))
    1790         [ -  + ]:         21 :                 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier));
    1791                 :            : 
    1792                 :            :         return 0;
    1793                 :            : 
    1794                 :            : class_err:
    1795                 :            : #ifdef CONFIG_I2C_COMPAT
    1796                 :          0 :         class_compat_unregister(i2c_adapter_compat_class);
    1797                 :          0 : bus_err:
    1798                 :            : #endif
    1799                 :          0 :         is_registered = false;
    1800                 :          0 :         bus_unregister(&i2c_bus_type);
    1801                 :          0 :         return retval;
    1802                 :            : }
    1803                 :            : 
    1804                 :          0 : static void __exit i2c_exit(void)
    1805                 :            : {
    1806                 :          0 :         if (IS_ENABLED(CONFIG_ACPI))
    1807         [ #  # ]:          0 :                 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier));
    1808                 :          0 :         if (IS_ENABLED(CONFIG_OF_DYNAMIC))
    1809                 :            :                 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier));
    1810                 :          0 :         i2c_del_driver(&dummy_driver);
    1811                 :            : #ifdef CONFIG_I2C_COMPAT
    1812                 :          0 :         class_compat_unregister(i2c_adapter_compat_class);
    1813                 :            : #endif
    1814                 :          0 :         bus_unregister(&i2c_bus_type);
    1815                 :          0 :         tracepoint_synchronize_unregister();
    1816                 :          0 : }
    1817                 :            : 
    1818                 :            : /* We must initialize early, because some subsystems register i2c drivers
    1819                 :            :  * in subsys_initcall() code, but are linked (and initialized) before i2c.
    1820                 :            :  */
    1821                 :            : postcore_initcall(i2c_init);
    1822                 :            : module_exit(i2c_exit);
    1823                 :            : 
    1824                 :            : /* ----------------------------------------------------
    1825                 :            :  * the functional interface to the i2c busses.
    1826                 :            :  * ----------------------------------------------------
    1827                 :            :  */
    1828                 :            : 
    1829                 :            : /* Check if val is exceeding the quirk IFF quirk is non 0 */
    1830                 :            : #define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
    1831                 :            : 
    1832                 :          0 : static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg)
    1833                 :            : {
    1834   [ #  #  #  # ]:          0 :         dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
    1835                 :            :                             err_msg, msg->addr, msg->len,
    1836                 :            :                             msg->flags & I2C_M_RD ? "read" : "write");
    1837                 :          0 :         return -EOPNOTSUPP;
    1838                 :            : }
    1839                 :            : 
    1840                 :          0 : static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
    1841                 :            : {
    1842                 :          0 :         const struct i2c_adapter_quirks *q = adap->quirks;
    1843                 :          0 :         int max_num = q->max_num_msgs, i;
    1844                 :          0 :         bool do_len_check = true;
    1845                 :            : 
    1846         [ #  # ]:          0 :         if (q->flags & I2C_AQ_COMB) {
    1847                 :          0 :                 max_num = 2;
    1848                 :            : 
    1849                 :            :                 /* special checks for combined messages */
    1850         [ #  # ]:          0 :                 if (num == 2) {
    1851   [ #  #  #  # ]:          0 :                         if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD)
    1852                 :          0 :                                 return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write");
    1853                 :            : 
    1854   [ #  #  #  # ]:          0 :                         if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD))
    1855                 :          0 :                                 return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read");
    1856                 :            : 
    1857   [ #  #  #  # ]:          0 :                         if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr)
    1858                 :          0 :                                 return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr");
    1859                 :            : 
    1860   [ #  #  #  # ]:          0 :                         if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len))
    1861                 :          0 :                                 return i2c_quirk_error(adap, &msgs[0], "msg too long");
    1862                 :            : 
    1863   [ #  #  #  # ]:          0 :                         if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len))
    1864                 :          0 :                                 return i2c_quirk_error(adap, &msgs[1], "msg too long");
    1865                 :            : 
    1866                 :            :                         do_len_check = false;
    1867                 :            :                 }
    1868                 :            :         }
    1869                 :            : 
    1870         [ #  # ]:          0 :         if (i2c_quirk_exceeded(num, max_num))
    1871                 :          0 :                 return i2c_quirk_error(adap, &msgs[0], "too many messages");
    1872                 :            : 
    1873         [ #  # ]:          0 :         for (i = 0; i < num; i++) {
    1874                 :          0 :                 u16 len = msgs[i].len;
    1875                 :            : 
    1876         [ #  # ]:          0 :                 if (msgs[i].flags & I2C_M_RD) {
    1877   [ #  #  #  #  :          0 :                         if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len))
                   #  # ]
    1878                 :          0 :                                 return i2c_quirk_error(adap, &msgs[i], "msg too long");
    1879                 :            : 
    1880   [ #  #  #  # ]:          0 :                         if (q->flags & I2C_AQ_NO_ZERO_LEN_READ && len == 0)
    1881                 :          0 :                                 return i2c_quirk_error(adap, &msgs[i], "no zero length");
    1882                 :            :                 } else {
    1883   [ #  #  #  #  :          0 :                         if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len))
                   #  # ]
    1884                 :          0 :                                 return i2c_quirk_error(adap, &msgs[i], "msg too long");
    1885                 :            : 
    1886   [ #  #  #  # ]:          0 :                         if (q->flags & I2C_AQ_NO_ZERO_LEN_WRITE && len == 0)
    1887                 :          0 :                                 return i2c_quirk_error(adap, &msgs[i], "no zero length");
    1888                 :            :                 }
    1889                 :            :         }
    1890                 :            : 
    1891                 :            :         return 0;
    1892                 :            : }
    1893                 :            : 
    1894                 :            : /**
    1895                 :            :  * __i2c_transfer - unlocked flavor of i2c_transfer
    1896                 :            :  * @adap: Handle to I2C bus
    1897                 :            :  * @msgs: One or more messages to execute before STOP is issued to
    1898                 :            :  *      terminate the operation; each message begins with a START.
    1899                 :            :  * @num: Number of messages to be executed.
    1900                 :            :  *
    1901                 :            :  * Returns negative errno, else the number of messages executed.
    1902                 :            :  *
    1903                 :            :  * Adapter lock must be held when calling this function. No debug logging
    1904                 :            :  * takes place. adap->algo->master_xfer existence isn't checked.
    1905                 :            :  */
    1906                 :          0 : int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
    1907                 :            : {
    1908                 :          0 :         unsigned long orig_jiffies;
    1909                 :          0 :         int ret, try;
    1910                 :            : 
    1911   [ #  #  #  # ]:          0 :         if (WARN_ON(!msgs || num < 1))
    1912                 :            :                 return -EINVAL;
    1913                 :            : 
    1914                 :          0 :         ret = __i2c_check_suspended(adap);
    1915         [ #  # ]:          0 :         if (ret)
    1916                 :            :                 return ret;
    1917                 :            : 
    1918   [ #  #  #  # ]:          0 :         if (adap->quirks && i2c_check_for_quirks(adap, msgs, num))
    1919                 :            :                 return -EOPNOTSUPP;
    1920                 :            : 
    1921                 :            :         /*
    1922                 :            :          * i2c_trace_msg_key gets enabled when tracepoint i2c_transfer gets
    1923                 :            :          * enabled.  This is an efficient way of keeping the for-loop from
    1924                 :            :          * being executed when not needed.
    1925                 :            :          */
    1926   [ #  #  #  # ]:          0 :         if (static_branch_unlikely(&i2c_trace_msg_key)) {
    1927                 :            :                 int i;
    1928         [ #  # ]:          0 :                 for (i = 0; i < num; i++)
    1929         [ #  # ]:          0 :                         if (msgs[i].flags & I2C_M_RD)
    1930                 :          0 :                                 trace_i2c_read(adap, &msgs[i], i);
    1931                 :            :                         else
    1932                 :          0 :                                 trace_i2c_write(adap, &msgs[i], i);
    1933                 :            :         }
    1934                 :            : 
    1935                 :            :         /* Retry automatically on arbitration loss */
    1936                 :          0 :         orig_jiffies = jiffies;
    1937         [ #  # ]:          0 :         for (ret = 0, try = 0; try <= adap->retries; try++) {
    1938   [ #  #  #  #  :          0 :                 if (i2c_in_atomic_xfer_mode() && adap->algo->master_xfer_atomic)
                   #  # ]
    1939                 :          0 :                         ret = adap->algo->master_xfer_atomic(adap, msgs, num);
    1940                 :            :                 else
    1941                 :          0 :                         ret = adap->algo->master_xfer(adap, msgs, num);
    1942                 :            : 
    1943         [ #  # ]:          0 :                 if (ret != -EAGAIN)
    1944                 :            :                         break;
    1945         [ #  # ]:          0 :                 if (time_after(jiffies, orig_jiffies + adap->timeout))
    1946                 :            :                         break;
    1947                 :            :         }
    1948                 :            : 
    1949   [ #  #  #  # ]:          0 :         if (static_branch_unlikely(&i2c_trace_msg_key)) {
    1950                 :            :                 int i;
    1951         [ #  # ]:          0 :                 for (i = 0; i < ret; i++)
    1952         [ #  # ]:          0 :                         if (msgs[i].flags & I2C_M_RD)
    1953                 :          0 :                                 trace_i2c_reply(adap, &msgs[i], i);
    1954                 :          0 :                 trace_i2c_result(adap, num, ret);
    1955                 :            :         }
    1956                 :            : 
    1957                 :            :         return ret;
    1958                 :            : }
    1959                 :            : EXPORT_SYMBOL(__i2c_transfer);
    1960                 :            : 
    1961                 :            : /**
    1962                 :            :  * i2c_transfer - execute a single or combined I2C message
    1963                 :            :  * @adap: Handle to I2C bus
    1964                 :            :  * @msgs: One or more messages to execute before STOP is issued to
    1965                 :            :  *      terminate the operation; each message begins with a START.
    1966                 :            :  * @num: Number of messages to be executed.
    1967                 :            :  *
    1968                 :            :  * Returns negative errno, else the number of messages executed.
    1969                 :            :  *
    1970                 :            :  * Note that there is no requirement that each message be sent to
    1971                 :            :  * the same slave address, although that is the most common model.
    1972                 :            :  */
    1973                 :          0 : int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
    1974                 :            : {
    1975                 :          0 :         int ret;
    1976                 :            : 
    1977         [ #  # ]:          0 :         if (!adap->algo->master_xfer) {
    1978                 :            :                 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
    1979                 :            :                 return -EOPNOTSUPP;
    1980                 :            :         }
    1981                 :            : 
    1982                 :            :         /* REVISIT the fault reporting model here is weak:
    1983                 :            :          *
    1984                 :            :          *  - When we get an error after receiving N bytes from a slave,
    1985                 :            :          *    there is no way to report "N".
    1986                 :            :          *
    1987                 :            :          *  - When we get a NAK after transmitting N bytes to a slave,
    1988                 :            :          *    there is no way to report "N" ... or to let the master
    1989                 :            :          *    continue executing the rest of this combined message, if
    1990                 :            :          *    that's the appropriate response.
    1991                 :            :          *
    1992                 :            :          *  - When for example "num" is two and we successfully complete
    1993                 :            :          *    the first message but get an error part way through the
    1994                 :            :          *    second, it's unclear whether that should be reported as
    1995                 :            :          *    one (discarding status on the second message) or errno
    1996                 :            :          *    (discarding status on the first one).
    1997                 :            :          */
    1998                 :          0 :         ret = __i2c_lock_bus_helper(adap);
    1999         [ #  # ]:          0 :         if (ret)
    2000                 :            :                 return ret;
    2001                 :            : 
    2002                 :          0 :         ret = __i2c_transfer(adap, msgs, num);
    2003                 :          0 :         i2c_unlock_bus(adap, I2C_LOCK_SEGMENT);
    2004                 :            : 
    2005                 :          0 :         return ret;
    2006                 :            : }
    2007                 :            : EXPORT_SYMBOL(i2c_transfer);
    2008                 :            : 
    2009                 :            : /**
    2010                 :            :  * i2c_transfer_buffer_flags - issue a single I2C message transferring data
    2011                 :            :  *                             to/from a buffer
    2012                 :            :  * @client: Handle to slave device
    2013                 :            :  * @buf: Where the data is stored
    2014                 :            :  * @count: How many bytes to transfer, must be less than 64k since msg.len is u16
    2015                 :            :  * @flags: The flags to be used for the message, e.g. I2C_M_RD for reads
    2016                 :            :  *
    2017                 :            :  * Returns negative errno, or else the number of bytes transferred.
    2018                 :            :  */
    2019                 :          0 : int i2c_transfer_buffer_flags(const struct i2c_client *client, char *buf,
    2020                 :            :                               int count, u16 flags)
    2021                 :            : {
    2022                 :          0 :         int ret;
    2023                 :          0 :         struct i2c_msg msg = {
    2024                 :          0 :                 .addr = client->addr,
    2025                 :          0 :                 .flags = flags | (client->flags & I2C_M_TEN),
    2026                 :            :                 .len = count,
    2027                 :            :                 .buf = buf,
    2028                 :            :         };
    2029                 :            : 
    2030                 :          0 :         ret = i2c_transfer(client->adapter, &msg, 1);
    2031                 :            : 
    2032                 :            :         /*
    2033                 :            :          * If everything went ok (i.e. 1 msg transferred), return #bytes
    2034                 :            :          * transferred, else error code.
    2035                 :            :          */
    2036         [ #  # ]:          0 :         return (ret == 1) ? count : ret;
    2037                 :            : }
    2038                 :            : EXPORT_SYMBOL(i2c_transfer_buffer_flags);
    2039                 :            : 
    2040                 :            : /**
    2041                 :            :  * i2c_get_device_id - get manufacturer, part id and die revision of a device
    2042                 :            :  * @client: The device to query
    2043                 :            :  * @id: The queried information
    2044                 :            :  *
    2045                 :            :  * Returns negative errno on error, zero on success.
    2046                 :            :  */
    2047                 :          0 : int i2c_get_device_id(const struct i2c_client *client,
    2048                 :            :                       struct i2c_device_identity *id)
    2049                 :            : {
    2050                 :          0 :         struct i2c_adapter *adap = client->adapter;
    2051                 :          0 :         union i2c_smbus_data raw_id;
    2052                 :          0 :         int ret;
    2053                 :            : 
    2054         [ #  # ]:          0 :         if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_I2C_BLOCK))
    2055                 :            :                 return -EOPNOTSUPP;
    2056                 :            : 
    2057                 :          0 :         raw_id.block[0] = 3;
    2058                 :          0 :         ret = i2c_smbus_xfer(adap, I2C_ADDR_DEVICE_ID, 0,
    2059                 :          0 :                              I2C_SMBUS_READ, client->addr << 1,
    2060                 :            :                              I2C_SMBUS_I2C_BLOCK_DATA, &raw_id);
    2061         [ #  # ]:          0 :         if (ret)
    2062                 :            :                 return ret;
    2063                 :            : 
    2064                 :          0 :         id->manufacturer_id = (raw_id.block[1] << 4) | (raw_id.block[2] >> 4);
    2065                 :          0 :         id->part_id = ((raw_id.block[2] & 0xf) << 5) | (raw_id.block[3] >> 3);
    2066                 :          0 :         id->die_revision = raw_id.block[3] & 0x7;
    2067                 :          0 :         return 0;
    2068                 :            : }
    2069                 :            : EXPORT_SYMBOL_GPL(i2c_get_device_id);
    2070                 :            : 
    2071                 :            : /* ----------------------------------------------------
    2072                 :            :  * the i2c address scanning function
    2073                 :            :  * Will not work for 10-bit addresses!
    2074                 :            :  * ----------------------------------------------------
    2075                 :            :  */
    2076                 :            : 
    2077                 :            : /*
    2078                 :            :  * Legacy default probe function, mostly relevant for SMBus. The default
    2079                 :            :  * probe method is a quick write, but it is known to corrupt the 24RF08
    2080                 :            :  * EEPROMs due to a state machine bug, and could also irreversibly
    2081                 :            :  * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
    2082                 :            :  * we use a short byte read instead. Also, some bus drivers don't implement
    2083                 :            :  * quick write, so we fallback to a byte read in that case too.
    2084                 :            :  * On x86, there is another special case for FSC hardware monitoring chips,
    2085                 :            :  * which want regular byte reads (address 0x73.) Fortunately, these are the
    2086                 :            :  * only known chips using this I2C address on PC hardware.
    2087                 :            :  * Returns 1 if probe succeeded, 0 if not.
    2088                 :            :  */
    2089                 :          0 : static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
    2090                 :            : {
    2091                 :          0 :         int err;
    2092                 :          0 :         union i2c_smbus_data dummy;
    2093                 :            : 
    2094                 :            : #ifdef CONFIG_X86
    2095   [ #  #  #  # ]:          0 :         if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
    2096         [ #  # ]:          0 :          && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
    2097                 :          0 :                 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
    2098                 :            :                                      I2C_SMBUS_BYTE_DATA, &dummy);
    2099                 :            :         else
    2100                 :            : #endif
    2101   [ #  #  #  # ]:          0 :         if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
    2102         [ #  # ]:          0 :          && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
    2103                 :          0 :                 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
    2104                 :            :                                      I2C_SMBUS_QUICK, NULL);
    2105         [ #  # ]:          0 :         else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
    2106                 :          0 :                 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
    2107                 :            :                                      I2C_SMBUS_BYTE, &dummy);
    2108                 :            :         else {
    2109                 :          0 :                 dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n",
    2110                 :            :                          addr);
    2111                 :          0 :                 err = -EOPNOTSUPP;
    2112                 :            :         }
    2113                 :            : 
    2114                 :          0 :         return err >= 0;
    2115                 :            : }
    2116                 :            : 
    2117                 :          0 : static int i2c_detect_address(struct i2c_client *temp_client,
    2118                 :            :                               struct i2c_driver *driver)
    2119                 :            : {
    2120                 :          0 :         struct i2c_board_info info;
    2121                 :          0 :         struct i2c_adapter *adapter = temp_client->adapter;
    2122                 :          0 :         int addr = temp_client->addr;
    2123                 :          0 :         int err;
    2124                 :            : 
    2125                 :            :         /* Make sure the address is valid */
    2126         [ #  # ]:          0 :         err = i2c_check_7bit_addr_validity_strict(addr);
    2127                 :          0 :         if (err) {
    2128                 :          0 :                 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
    2129                 :            :                          addr);
    2130                 :          0 :                 return err;
    2131                 :            :         }
    2132                 :            : 
    2133                 :            :         /* Skip if already in use (7 bit, no need to encode flags) */
    2134         [ #  # ]:          0 :         if (i2c_check_addr_busy(adapter, addr))
    2135                 :            :                 return 0;
    2136                 :            : 
    2137                 :            :         /* Make sure there is something at this address */
    2138         [ #  # ]:          0 :         if (!i2c_default_probe(adapter, addr))
    2139                 :            :                 return 0;
    2140                 :            : 
    2141                 :            :         /* Finally call the custom detection function */
    2142                 :          0 :         memset(&info, 0, sizeof(struct i2c_board_info));
    2143                 :          0 :         info.addr = addr;
    2144                 :          0 :         err = driver->detect(temp_client, &info);
    2145         [ #  # ]:          0 :         if (err) {
    2146                 :            :                 /* -ENODEV is returned if the detection fails. We catch it
    2147                 :            :                    here as this isn't an error. */
    2148         [ #  # ]:          0 :                 return err == -ENODEV ? 0 : err;
    2149                 :            :         }
    2150                 :            : 
    2151                 :            :         /* Consistency check */
    2152         [ #  # ]:          0 :         if (info.type[0] == '\0') {
    2153                 :          0 :                 dev_err(&adapter->dev,
    2154                 :            :                         "%s detection function provided no name for 0x%x\n",
    2155                 :            :                         driver->driver.name, addr);
    2156                 :            :         } else {
    2157                 :          0 :                 struct i2c_client *client;
    2158                 :            : 
    2159                 :            :                 /* Detection succeeded, instantiate the device */
    2160         [ #  # ]:          0 :                 if (adapter->class & I2C_CLASS_DEPRECATED)
    2161                 :          0 :                         dev_warn(&adapter->dev,
    2162                 :            :                                 "This adapter will soon drop class based instantiation of devices. "
    2163                 :            :                                 "Please make sure client 0x%02x gets instantiated by other means. "
    2164                 :            :                                 "Check 'Documentation/i2c/instantiating-devices.rst' for details.\n",
    2165                 :            :                                 info.addr);
    2166                 :            : 
    2167                 :          0 :                 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
    2168                 :            :                         info.type, info.addr);
    2169                 :          0 :                 client = i2c_new_client_device(adapter, &info);
    2170         [ #  # ]:          0 :                 if (!IS_ERR(client))
    2171                 :          0 :                         list_add_tail(&client->detected, &driver->clients);
    2172                 :            :                 else
    2173                 :          0 :                         dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
    2174                 :            :                                 info.type, info.addr);
    2175                 :            :         }
    2176                 :            :         return 0;
    2177                 :            : }
    2178                 :            : 
    2179                 :          0 : static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
    2180                 :            : {
    2181                 :          0 :         const unsigned short *address_list;
    2182                 :          0 :         struct i2c_client *temp_client;
    2183                 :          0 :         int i, err = 0;
    2184         [ #  # ]:          0 :         int adap_id = i2c_adapter_id(adapter);
    2185                 :            : 
    2186                 :          0 :         address_list = driver->address_list;
    2187   [ #  #  #  # ]:          0 :         if (!driver->detect || !address_list)
    2188                 :            :                 return 0;
    2189                 :            : 
    2190                 :            :         /* Warn that the adapter lost class based instantiation */
    2191         [ #  # ]:          0 :         if (adapter->class == I2C_CLASS_DEPRECATED) {
    2192                 :            :                 dev_dbg(&adapter->dev,
    2193                 :            :                         "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. "
    2194                 :            :                         "If you need it, check 'Documentation/i2c/instantiating-devices.rst' for alternatives.\n",
    2195                 :            :                         driver->driver.name);
    2196                 :            :                 return 0;
    2197                 :            :         }
    2198                 :            : 
    2199                 :            :         /* Stop here if the classes do not match */
    2200         [ #  # ]:          0 :         if (!(adapter->class & driver->class))
    2201                 :            :                 return 0;
    2202                 :            : 
    2203                 :            :         /* Set up a temporary client to help detect callback */
    2204                 :          0 :         temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
    2205         [ #  # ]:          0 :         if (!temp_client)
    2206                 :            :                 return -ENOMEM;
    2207                 :          0 :         temp_client->adapter = adapter;
    2208                 :            : 
    2209         [ #  # ]:          0 :         for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
    2210                 :          0 :                 dev_dbg(&adapter->dev,
    2211                 :            :                         "found normal entry for adapter %d, addr 0x%02x\n",
    2212                 :            :                         adap_id, address_list[i]);
    2213                 :          0 :                 temp_client->addr = address_list[i];
    2214                 :          0 :                 err = i2c_detect_address(temp_client, driver);
    2215         [ #  # ]:          0 :                 if (unlikely(err))
    2216                 :            :                         break;
    2217                 :            :         }
    2218                 :            : 
    2219                 :          0 :         kfree(temp_client);
    2220                 :          0 :         return err;
    2221                 :            : }
    2222                 :            : 
    2223                 :          0 : int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
    2224                 :            : {
    2225                 :          0 :         return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
    2226                 :          0 :                               I2C_SMBUS_QUICK, NULL) >= 0;
    2227                 :            : }
    2228                 :            : EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
    2229                 :            : 
    2230                 :            : struct i2c_client *
    2231                 :          0 : i2c_new_scanned_device(struct i2c_adapter *adap,
    2232                 :            :                        struct i2c_board_info *info,
    2233                 :            :                        unsigned short const *addr_list,
    2234                 :            :                        int (*probe)(struct i2c_adapter *adap, unsigned short addr))
    2235                 :            : {
    2236                 :          0 :         int i;
    2237                 :            : 
    2238         [ #  # ]:          0 :         if (!probe)
    2239                 :          0 :                 probe = i2c_default_probe;
    2240                 :            : 
    2241         [ #  # ]:          0 :         for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
    2242                 :            :                 /* Check address validity */
    2243         [ #  # ]:          0 :                 if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) {
    2244                 :          0 :                         dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n",
    2245                 :            :                                  addr_list[i]);
    2246                 :          0 :                         continue;
    2247                 :            :                 }
    2248                 :            : 
    2249                 :            :                 /* Check address availability (7 bit, no need to encode flags) */
    2250         [ #  # ]:          0 :                 if (i2c_check_addr_busy(adap, addr_list[i])) {
    2251                 :          0 :                         dev_dbg(&adap->dev,
    2252                 :            :                                 "Address 0x%02x already in use, not probing\n",
    2253                 :            :                                 addr_list[i]);
    2254                 :          0 :                         continue;
    2255                 :            :                 }
    2256                 :            : 
    2257                 :            :                 /* Test address responsiveness */
    2258         [ #  # ]:          0 :                 if (probe(adap, addr_list[i]))
    2259                 :            :                         break;
    2260                 :            :         }
    2261                 :            : 
    2262         [ #  # ]:          0 :         if (addr_list[i] == I2C_CLIENT_END) {
    2263                 :            :                 dev_dbg(&adap->dev, "Probing failed, no device found\n");
    2264                 :            :                 return ERR_PTR(-ENODEV);
    2265                 :            :         }
    2266                 :            : 
    2267                 :          0 :         info->addr = addr_list[i];
    2268                 :          0 :         return i2c_new_client_device(adap, info);
    2269                 :            : }
    2270                 :            : EXPORT_SYMBOL_GPL(i2c_new_scanned_device);
    2271                 :            : 
    2272                 :            : struct i2c_client *
    2273                 :          0 : i2c_new_probed_device(struct i2c_adapter *adap,
    2274                 :            :                       struct i2c_board_info *info,
    2275                 :            :                       unsigned short const *addr_list,
    2276                 :            :                       int (*probe)(struct i2c_adapter *adap, unsigned short addr))
    2277                 :            : {
    2278                 :          0 :         struct i2c_client *client;
    2279                 :            : 
    2280                 :          0 :         client = i2c_new_scanned_device(adap, info, addr_list, probe);
    2281         [ #  # ]:          0 :         return IS_ERR(client) ? NULL : client;
    2282                 :            : }
    2283                 :            : EXPORT_SYMBOL_GPL(i2c_new_probed_device);
    2284                 :            : 
    2285                 :          0 : struct i2c_adapter *i2c_get_adapter(int nr)
    2286                 :            : {
    2287                 :          0 :         struct i2c_adapter *adapter;
    2288                 :            : 
    2289                 :          0 :         mutex_lock(&core_lock);
    2290                 :          0 :         adapter = idr_find(&i2c_adapter_idr, nr);
    2291         [ #  # ]:          0 :         if (!adapter)
    2292                 :          0 :                 goto exit;
    2293                 :            : 
    2294         [ #  # ]:          0 :         if (try_module_get(adapter->owner))
    2295                 :          0 :                 get_device(&adapter->dev);
    2296                 :            :         else
    2297                 :            :                 adapter = NULL;
    2298                 :            : 
    2299                 :          0 :  exit:
    2300                 :          0 :         mutex_unlock(&core_lock);
    2301                 :          0 :         return adapter;
    2302                 :            : }
    2303                 :            : EXPORT_SYMBOL(i2c_get_adapter);
    2304                 :            : 
    2305                 :          0 : void i2c_put_adapter(struct i2c_adapter *adap)
    2306                 :            : {
    2307         [ #  # ]:          0 :         if (!adap)
    2308                 :            :                 return;
    2309                 :            : 
    2310                 :          0 :         put_device(&adap->dev);
    2311                 :          0 :         module_put(adap->owner);
    2312                 :            : }
    2313                 :            : EXPORT_SYMBOL(i2c_put_adapter);
    2314                 :            : 
    2315                 :            : /**
    2316                 :            :  * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg
    2317                 :            :  * @msg: the message to be checked
    2318                 :            :  * @threshold: the minimum number of bytes for which using DMA makes sense.
    2319                 :            :  *             Should at least be 1.
    2320                 :            :  *
    2321                 :            :  * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO.
    2322                 :            :  *         Or a valid pointer to be used with DMA. After use, release it by
    2323                 :            :  *         calling i2c_put_dma_safe_msg_buf().
    2324                 :            :  *
    2325                 :            :  * This function must only be called from process context!
    2326                 :            :  */
    2327                 :          0 : u8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold)
    2328                 :            : {
    2329                 :            :         /* also skip 0-length msgs for bogus thresholds of 0 */
    2330                 :          0 :         if (!threshold)
    2331                 :            :                 pr_debug("DMA buffer for addr=0x%02x with length 0 is bogus\n",
    2332                 :            :                          msg->addr);
    2333   [ #  #  #  # ]:          0 :         if (msg->len < threshold || msg->len == 0)
    2334                 :            :                 return NULL;
    2335                 :            : 
    2336         [ #  # ]:          0 :         if (msg->flags & I2C_M_DMA_SAFE)
    2337                 :          0 :                 return msg->buf;
    2338                 :            : 
    2339                 :          0 :         pr_debug("using bounce buffer for addr=0x%02x, len=%d\n",
    2340                 :            :                  msg->addr, msg->len);
    2341                 :            : 
    2342         [ #  # ]:          0 :         if (msg->flags & I2C_M_RD)
    2343                 :          0 :                 return kzalloc(msg->len, GFP_KERNEL);
    2344                 :            :         else
    2345                 :          0 :                 return kmemdup(msg->buf, msg->len, GFP_KERNEL);
    2346                 :            : }
    2347                 :            : EXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf);
    2348                 :            : 
    2349                 :            : /**
    2350                 :            :  * i2c_put_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg
    2351                 :            :  * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL.
    2352                 :            :  * @msg: the message which the buffer corresponds to
    2353                 :            :  * @xferred: bool saying if the message was transferred
    2354                 :            :  */
    2355                 :          0 : void i2c_put_dma_safe_msg_buf(u8 *buf, struct i2c_msg *msg, bool xferred)
    2356                 :            : {
    2357   [ #  #  #  # ]:          0 :         if (!buf || buf == msg->buf)
    2358                 :            :                 return;
    2359                 :            : 
    2360   [ #  #  #  # ]:          0 :         if (xferred && msg->flags & I2C_M_RD)
    2361                 :          0 :                 memcpy(msg->buf, buf, msg->len);
    2362                 :            : 
    2363                 :          0 :         kfree(buf);
    2364                 :            : }
    2365                 :            : EXPORT_SYMBOL_GPL(i2c_put_dma_safe_msg_buf);
    2366                 :            : 
    2367                 :            : MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
    2368                 :            : MODULE_DESCRIPTION("I2C-Bus main module");
    2369                 :            : MODULE_LICENSE("GPL");

Generated by: LCOV version 1.14