LCOV - code coverage report
Current view: top level - drivers/i2c - i2c-core-base.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 28 737 3.8 %
Date: 2020-09-30 20:25:40 Functions: 3 88 3.4 %
Branches: 8 564 1.4 %

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

Generated by: LCOV version 1.14