18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Linux I2C core 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 1995-99 Simon G. Vogl 68c2ecf20Sopenharmony_ci * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> 78c2ecf20Sopenharmony_ci * Mux support by Rodolfo Giometti <giometti@enneenne.com> and 88c2ecf20Sopenharmony_ci * Michael Lawnick <michael.lawnick.ext@nsn.com> 98c2ecf20Sopenharmony_ci * 108c2ecf20Sopenharmony_ci * Copyright (C) 2013-2017 Wolfram Sang <wsa@kernel.org> 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "i2c-core: " fmt 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include <dt-bindings/i2c/i2c.h> 168c2ecf20Sopenharmony_ci#include <linux/acpi.h> 178c2ecf20Sopenharmony_ci#include <linux/clk/clk-conf.h> 188c2ecf20Sopenharmony_ci#include <linux/completion.h> 198c2ecf20Sopenharmony_ci#include <linux/delay.h> 208c2ecf20Sopenharmony_ci#include <linux/err.h> 218c2ecf20Sopenharmony_ci#include <linux/errno.h> 228c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h> 238c2ecf20Sopenharmony_ci#include <linux/i2c.h> 248c2ecf20Sopenharmony_ci#include <linux/i2c-smbus.h> 258c2ecf20Sopenharmony_ci#include <linux/idr.h> 268c2ecf20Sopenharmony_ci#include <linux/init.h> 278c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 288c2ecf20Sopenharmony_ci#include <linux/irqflags.h> 298c2ecf20Sopenharmony_ci#include <linux/jump_label.h> 308c2ecf20Sopenharmony_ci#include <linux/kernel.h> 318c2ecf20Sopenharmony_ci#include <linux/module.h> 328c2ecf20Sopenharmony_ci#include <linux/mutex.h> 338c2ecf20Sopenharmony_ci#include <linux/of_device.h> 348c2ecf20Sopenharmony_ci#include <linux/of.h> 358c2ecf20Sopenharmony_ci#include <linux/of_irq.h> 368c2ecf20Sopenharmony_ci#include <linux/pinctrl/consumer.h> 378c2ecf20Sopenharmony_ci#include <linux/pm_domain.h> 388c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 398c2ecf20Sopenharmony_ci#include <linux/pm_wakeirq.h> 408c2ecf20Sopenharmony_ci#include <linux/property.h> 418c2ecf20Sopenharmony_ci#include <linux/rwsem.h> 428c2ecf20Sopenharmony_ci#include <linux/slab.h> 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci#include "i2c-core.h" 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci#define CREATE_TRACE_POINTS 478c2ecf20Sopenharmony_ci#include <trace/events/i2c.h> 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci#define I2C_ADDR_OFFSET_TEN_BIT 0xa000 508c2ecf20Sopenharmony_ci#define I2C_ADDR_OFFSET_SLAVE 0x1000 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci#define I2C_ADDR_7BITS_MAX 0x77 538c2ecf20Sopenharmony_ci#define I2C_ADDR_7BITS_COUNT (I2C_ADDR_7BITS_MAX + 1) 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci#define I2C_ADDR_DEVICE_ID 0x7c 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci/* 588c2ecf20Sopenharmony_ci * core_lock protects i2c_adapter_idr, and guarantees that device detection, 598c2ecf20Sopenharmony_ci * deletion of detected devices are serialized 608c2ecf20Sopenharmony_ci */ 618c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(core_lock); 628c2ecf20Sopenharmony_cistatic DEFINE_IDR(i2c_adapter_idr); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistatic int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver); 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_cistatic DEFINE_STATIC_KEY_FALSE(i2c_trace_msg_key); 678c2ecf20Sopenharmony_cistatic bool is_registered; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ciint i2c_transfer_trace_reg(void) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci static_branch_inc(&i2c_trace_msg_key); 728c2ecf20Sopenharmony_ci return 0; 738c2ecf20Sopenharmony_ci} 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_civoid i2c_transfer_trace_unreg(void) 768c2ecf20Sopenharmony_ci{ 778c2ecf20Sopenharmony_ci static_branch_dec(&i2c_trace_msg_key); 788c2ecf20Sopenharmony_ci} 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ciconst struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, 818c2ecf20Sopenharmony_ci const struct i2c_client *client) 828c2ecf20Sopenharmony_ci{ 838c2ecf20Sopenharmony_ci if (!(id && client)) 848c2ecf20Sopenharmony_ci return NULL; 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci while (id->name[0]) { 878c2ecf20Sopenharmony_ci if (strcmp(client->name, id->name) == 0) 888c2ecf20Sopenharmony_ci return id; 898c2ecf20Sopenharmony_ci id++; 908c2ecf20Sopenharmony_ci } 918c2ecf20Sopenharmony_ci return NULL; 928c2ecf20Sopenharmony_ci} 938c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_match_id); 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistatic int i2c_device_match(struct device *dev, struct device_driver *drv) 968c2ecf20Sopenharmony_ci{ 978c2ecf20Sopenharmony_ci struct i2c_client *client = i2c_verify_client(dev); 988c2ecf20Sopenharmony_ci struct i2c_driver *driver; 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci /* Attempt an OF style match */ 1028c2ecf20Sopenharmony_ci if (i2c_of_match_device(drv->of_match_table, client)) 1038c2ecf20Sopenharmony_ci return 1; 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci /* Then ACPI style match */ 1068c2ecf20Sopenharmony_ci if (acpi_driver_match_device(dev, drv)) 1078c2ecf20Sopenharmony_ci return 1; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci driver = to_i2c_driver(drv); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci /* Finally an I2C match */ 1128c2ecf20Sopenharmony_ci if (i2c_match_id(driver->id_table, client)) 1138c2ecf20Sopenharmony_ci return 1; 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci return 0; 1168c2ecf20Sopenharmony_ci} 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_cistatic int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 1218c2ecf20Sopenharmony_ci int rc; 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci rc = of_device_uevent_modalias(dev, env); 1248c2ecf20Sopenharmony_ci if (rc != -ENODEV) 1258c2ecf20Sopenharmony_ci return rc; 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci rc = acpi_device_uevent_modalias(dev, env); 1288c2ecf20Sopenharmony_ci if (rc != -ENODEV) 1298c2ecf20Sopenharmony_ci return rc; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci return add_uevent_var(env, "MODALIAS=%s%s", I2C_MODULE_PREFIX, client->name); 1328c2ecf20Sopenharmony_ci} 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci/* i2c bus recovery routines */ 1358c2ecf20Sopenharmony_cistatic int get_scl_gpio_value(struct i2c_adapter *adap) 1368c2ecf20Sopenharmony_ci{ 1378c2ecf20Sopenharmony_ci return gpiod_get_value_cansleep(adap->bus_recovery_info->scl_gpiod); 1388c2ecf20Sopenharmony_ci} 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_cistatic void set_scl_gpio_value(struct i2c_adapter *adap, int val) 1418c2ecf20Sopenharmony_ci{ 1428c2ecf20Sopenharmony_ci gpiod_set_value_cansleep(adap->bus_recovery_info->scl_gpiod, val); 1438c2ecf20Sopenharmony_ci} 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_cistatic int get_sda_gpio_value(struct i2c_adapter *adap) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci return gpiod_get_value_cansleep(adap->bus_recovery_info->sda_gpiod); 1488c2ecf20Sopenharmony_ci} 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_cistatic void set_sda_gpio_value(struct i2c_adapter *adap, int val) 1518c2ecf20Sopenharmony_ci{ 1528c2ecf20Sopenharmony_ci gpiod_set_value_cansleep(adap->bus_recovery_info->sda_gpiod, val); 1538c2ecf20Sopenharmony_ci} 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_cistatic int i2c_generic_bus_free(struct i2c_adapter *adap) 1568c2ecf20Sopenharmony_ci{ 1578c2ecf20Sopenharmony_ci struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; 1588c2ecf20Sopenharmony_ci int ret = -EOPNOTSUPP; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci if (bri->get_bus_free) 1618c2ecf20Sopenharmony_ci ret = bri->get_bus_free(adap); 1628c2ecf20Sopenharmony_ci else if (bri->get_sda) 1638c2ecf20Sopenharmony_ci ret = bri->get_sda(adap); 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci if (ret < 0) 1668c2ecf20Sopenharmony_ci return ret; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci return ret ? 0 : -EBUSY; 1698c2ecf20Sopenharmony_ci} 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci/* 1728c2ecf20Sopenharmony_ci * We are generating clock pulses. ndelay() determines durating of clk pulses. 1738c2ecf20Sopenharmony_ci * We will generate clock with rate 100 KHz and so duration of both clock levels 1748c2ecf20Sopenharmony_ci * is: delay in ns = (10^6 / 100) / 2 1758c2ecf20Sopenharmony_ci */ 1768c2ecf20Sopenharmony_ci#define RECOVERY_NDELAY 5000 1778c2ecf20Sopenharmony_ci#define RECOVERY_CLK_CNT 9 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ciint i2c_generic_scl_recovery(struct i2c_adapter *adap) 1808c2ecf20Sopenharmony_ci{ 1818c2ecf20Sopenharmony_ci struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; 1828c2ecf20Sopenharmony_ci int i = 0, scl = 1, ret = 0; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci if (bri->prepare_recovery) 1858c2ecf20Sopenharmony_ci bri->prepare_recovery(adap); 1868c2ecf20Sopenharmony_ci if (bri->pinctrl) 1878c2ecf20Sopenharmony_ci pinctrl_select_state(bri->pinctrl, bri->pins_gpio); 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_ci /* 1908c2ecf20Sopenharmony_ci * If we can set SDA, we will always create a STOP to ensure additional 1918c2ecf20Sopenharmony_ci * pulses will do no harm. This is achieved by letting SDA follow SCL 1928c2ecf20Sopenharmony_ci * half a cycle later. Check the 'incomplete_write_byte' fault injector 1938c2ecf20Sopenharmony_ci * for details. Note that we must honour tsu:sto, 4us, but lets use 5us 1948c2ecf20Sopenharmony_ci * here for simplicity. 1958c2ecf20Sopenharmony_ci */ 1968c2ecf20Sopenharmony_ci bri->set_scl(adap, scl); 1978c2ecf20Sopenharmony_ci ndelay(RECOVERY_NDELAY); 1988c2ecf20Sopenharmony_ci if (bri->set_sda) 1998c2ecf20Sopenharmony_ci bri->set_sda(adap, scl); 2008c2ecf20Sopenharmony_ci ndelay(RECOVERY_NDELAY / 2); 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci /* 2038c2ecf20Sopenharmony_ci * By this time SCL is high, as we need to give 9 falling-rising edges 2048c2ecf20Sopenharmony_ci */ 2058c2ecf20Sopenharmony_ci while (i++ < RECOVERY_CLK_CNT * 2) { 2068c2ecf20Sopenharmony_ci if (scl) { 2078c2ecf20Sopenharmony_ci /* SCL shouldn't be low here */ 2088c2ecf20Sopenharmony_ci if (!bri->get_scl(adap)) { 2098c2ecf20Sopenharmony_ci dev_err(&adap->dev, 2108c2ecf20Sopenharmony_ci "SCL is stuck low, exit recovery\n"); 2118c2ecf20Sopenharmony_ci ret = -EBUSY; 2128c2ecf20Sopenharmony_ci break; 2138c2ecf20Sopenharmony_ci } 2148c2ecf20Sopenharmony_ci } 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci scl = !scl; 2178c2ecf20Sopenharmony_ci bri->set_scl(adap, scl); 2188c2ecf20Sopenharmony_ci /* Creating STOP again, see above */ 2198c2ecf20Sopenharmony_ci if (scl) { 2208c2ecf20Sopenharmony_ci /* Honour minimum tsu:sto */ 2218c2ecf20Sopenharmony_ci ndelay(RECOVERY_NDELAY); 2228c2ecf20Sopenharmony_ci } else { 2238c2ecf20Sopenharmony_ci /* Honour minimum tf and thd:dat */ 2248c2ecf20Sopenharmony_ci ndelay(RECOVERY_NDELAY / 2); 2258c2ecf20Sopenharmony_ci } 2268c2ecf20Sopenharmony_ci if (bri->set_sda) 2278c2ecf20Sopenharmony_ci bri->set_sda(adap, scl); 2288c2ecf20Sopenharmony_ci ndelay(RECOVERY_NDELAY / 2); 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci if (scl) { 2318c2ecf20Sopenharmony_ci ret = i2c_generic_bus_free(adap); 2328c2ecf20Sopenharmony_ci if (ret == 0) 2338c2ecf20Sopenharmony_ci break; 2348c2ecf20Sopenharmony_ci } 2358c2ecf20Sopenharmony_ci } 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci /* If we can't check bus status, assume recovery worked */ 2388c2ecf20Sopenharmony_ci if (ret == -EOPNOTSUPP) 2398c2ecf20Sopenharmony_ci ret = 0; 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci if (bri->unprepare_recovery) 2428c2ecf20Sopenharmony_ci bri->unprepare_recovery(adap); 2438c2ecf20Sopenharmony_ci if (bri->pinctrl) 2448c2ecf20Sopenharmony_ci pinctrl_select_state(bri->pinctrl, bri->pins_default); 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci return ret; 2478c2ecf20Sopenharmony_ci} 2488c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_generic_scl_recovery); 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ciint i2c_recover_bus(struct i2c_adapter *adap) 2518c2ecf20Sopenharmony_ci{ 2528c2ecf20Sopenharmony_ci if (!adap->bus_recovery_info) 2538c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci dev_dbg(&adap->dev, "Trying i2c bus recovery\n"); 2568c2ecf20Sopenharmony_ci return adap->bus_recovery_info->recover_bus(adap); 2578c2ecf20Sopenharmony_ci} 2588c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_recover_bus); 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_cistatic void i2c_gpio_init_pinctrl_recovery(struct i2c_adapter *adap) 2618c2ecf20Sopenharmony_ci{ 2628c2ecf20Sopenharmony_ci struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; 2638c2ecf20Sopenharmony_ci struct device *dev = &adap->dev; 2648c2ecf20Sopenharmony_ci struct pinctrl *p = bri->pinctrl; 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci /* 2678c2ecf20Sopenharmony_ci * we can't change states without pinctrl, so remove the states if 2688c2ecf20Sopenharmony_ci * populated 2698c2ecf20Sopenharmony_ci */ 2708c2ecf20Sopenharmony_ci if (!p) { 2718c2ecf20Sopenharmony_ci bri->pins_default = NULL; 2728c2ecf20Sopenharmony_ci bri->pins_gpio = NULL; 2738c2ecf20Sopenharmony_ci return; 2748c2ecf20Sopenharmony_ci } 2758c2ecf20Sopenharmony_ci 2768c2ecf20Sopenharmony_ci if (!bri->pins_default) { 2778c2ecf20Sopenharmony_ci bri->pins_default = pinctrl_lookup_state(p, 2788c2ecf20Sopenharmony_ci PINCTRL_STATE_DEFAULT); 2798c2ecf20Sopenharmony_ci if (IS_ERR(bri->pins_default)) { 2808c2ecf20Sopenharmony_ci dev_dbg(dev, PINCTRL_STATE_DEFAULT " state not found for GPIO recovery\n"); 2818c2ecf20Sopenharmony_ci bri->pins_default = NULL; 2828c2ecf20Sopenharmony_ci } 2838c2ecf20Sopenharmony_ci } 2848c2ecf20Sopenharmony_ci if (!bri->pins_gpio) { 2858c2ecf20Sopenharmony_ci bri->pins_gpio = pinctrl_lookup_state(p, "gpio"); 2868c2ecf20Sopenharmony_ci if (IS_ERR(bri->pins_gpio)) 2878c2ecf20Sopenharmony_ci bri->pins_gpio = pinctrl_lookup_state(p, "recovery"); 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci if (IS_ERR(bri->pins_gpio)) { 2908c2ecf20Sopenharmony_ci dev_dbg(dev, "no gpio or recovery state found for GPIO recovery\n"); 2918c2ecf20Sopenharmony_ci bri->pins_gpio = NULL; 2928c2ecf20Sopenharmony_ci } 2938c2ecf20Sopenharmony_ci } 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci /* for pinctrl state changes, we need all the information */ 2968c2ecf20Sopenharmony_ci if (bri->pins_default && bri->pins_gpio) { 2978c2ecf20Sopenharmony_ci dev_info(dev, "using pinctrl states for GPIO recovery"); 2988c2ecf20Sopenharmony_ci } else { 2998c2ecf20Sopenharmony_ci bri->pinctrl = NULL; 3008c2ecf20Sopenharmony_ci bri->pins_default = NULL; 3018c2ecf20Sopenharmony_ci bri->pins_gpio = NULL; 3028c2ecf20Sopenharmony_ci } 3038c2ecf20Sopenharmony_ci} 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_cistatic int i2c_gpio_init_generic_recovery(struct i2c_adapter *adap) 3068c2ecf20Sopenharmony_ci{ 3078c2ecf20Sopenharmony_ci struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; 3088c2ecf20Sopenharmony_ci struct device *dev = &adap->dev; 3098c2ecf20Sopenharmony_ci struct gpio_desc *gpiod; 3108c2ecf20Sopenharmony_ci int ret = 0; 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci /* 3138c2ecf20Sopenharmony_ci * don't touch the recovery information if the driver is not using 3148c2ecf20Sopenharmony_ci * generic SCL recovery 3158c2ecf20Sopenharmony_ci */ 3168c2ecf20Sopenharmony_ci if (bri->recover_bus && bri->recover_bus != i2c_generic_scl_recovery) 3178c2ecf20Sopenharmony_ci return 0; 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci /* 3208c2ecf20Sopenharmony_ci * pins might be taken as GPIO, so we should inform pinctrl about 3218c2ecf20Sopenharmony_ci * this and move the state to GPIO 3228c2ecf20Sopenharmony_ci */ 3238c2ecf20Sopenharmony_ci if (bri->pinctrl) 3248c2ecf20Sopenharmony_ci pinctrl_select_state(bri->pinctrl, bri->pins_gpio); 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci /* 3278c2ecf20Sopenharmony_ci * if there is incomplete or no recovery information, see if generic 3288c2ecf20Sopenharmony_ci * GPIO recovery is available 3298c2ecf20Sopenharmony_ci */ 3308c2ecf20Sopenharmony_ci if (!bri->scl_gpiod) { 3318c2ecf20Sopenharmony_ci gpiod = devm_gpiod_get(dev, "scl", GPIOD_OUT_HIGH_OPEN_DRAIN); 3328c2ecf20Sopenharmony_ci if (PTR_ERR(gpiod) == -EPROBE_DEFER) { 3338c2ecf20Sopenharmony_ci ret = -EPROBE_DEFER; 3348c2ecf20Sopenharmony_ci goto cleanup_pinctrl_state; 3358c2ecf20Sopenharmony_ci } 3368c2ecf20Sopenharmony_ci if (!IS_ERR(gpiod)) { 3378c2ecf20Sopenharmony_ci bri->scl_gpiod = gpiod; 3388c2ecf20Sopenharmony_ci bri->recover_bus = i2c_generic_scl_recovery; 3398c2ecf20Sopenharmony_ci dev_info(dev, "using generic GPIOs for recovery\n"); 3408c2ecf20Sopenharmony_ci } 3418c2ecf20Sopenharmony_ci } 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci /* SDA GPIOD line is optional, so we care about DEFER only */ 3448c2ecf20Sopenharmony_ci if (!bri->sda_gpiod) { 3458c2ecf20Sopenharmony_ci /* 3468c2ecf20Sopenharmony_ci * We have SCL. Pull SCL low and wait a bit so that SDA glitches 3478c2ecf20Sopenharmony_ci * have no effect. 3488c2ecf20Sopenharmony_ci */ 3498c2ecf20Sopenharmony_ci gpiod_direction_output(bri->scl_gpiod, 0); 3508c2ecf20Sopenharmony_ci udelay(10); 3518c2ecf20Sopenharmony_ci gpiod = devm_gpiod_get(dev, "sda", GPIOD_IN); 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci /* Wait a bit in case of a SDA glitch, and then release SCL. */ 3548c2ecf20Sopenharmony_ci udelay(10); 3558c2ecf20Sopenharmony_ci gpiod_direction_output(bri->scl_gpiod, 1); 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_ci if (PTR_ERR(gpiod) == -EPROBE_DEFER) { 3588c2ecf20Sopenharmony_ci ret = -EPROBE_DEFER; 3598c2ecf20Sopenharmony_ci goto cleanup_pinctrl_state; 3608c2ecf20Sopenharmony_ci } 3618c2ecf20Sopenharmony_ci if (!IS_ERR(gpiod)) 3628c2ecf20Sopenharmony_ci bri->sda_gpiod = gpiod; 3638c2ecf20Sopenharmony_ci } 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_cicleanup_pinctrl_state: 3668c2ecf20Sopenharmony_ci /* change the state of the pins back to their default state */ 3678c2ecf20Sopenharmony_ci if (bri->pinctrl) 3688c2ecf20Sopenharmony_ci pinctrl_select_state(bri->pinctrl, bri->pins_default); 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci return ret; 3718c2ecf20Sopenharmony_ci} 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_cistatic int i2c_gpio_init_recovery(struct i2c_adapter *adap) 3748c2ecf20Sopenharmony_ci{ 3758c2ecf20Sopenharmony_ci i2c_gpio_init_pinctrl_recovery(adap); 3768c2ecf20Sopenharmony_ci return i2c_gpio_init_generic_recovery(adap); 3778c2ecf20Sopenharmony_ci} 3788c2ecf20Sopenharmony_ci 3798c2ecf20Sopenharmony_cistatic int i2c_init_recovery(struct i2c_adapter *adap) 3808c2ecf20Sopenharmony_ci{ 3818c2ecf20Sopenharmony_ci struct i2c_bus_recovery_info *bri = adap->bus_recovery_info; 3828c2ecf20Sopenharmony_ci char *err_str, *err_level = KERN_ERR; 3838c2ecf20Sopenharmony_ci 3848c2ecf20Sopenharmony_ci if (!bri) 3858c2ecf20Sopenharmony_ci return 0; 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci if (i2c_gpio_init_recovery(adap) == -EPROBE_DEFER) 3888c2ecf20Sopenharmony_ci return -EPROBE_DEFER; 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ci if (!bri->recover_bus) { 3918c2ecf20Sopenharmony_ci err_str = "no suitable method provided"; 3928c2ecf20Sopenharmony_ci err_level = KERN_DEBUG; 3938c2ecf20Sopenharmony_ci goto err; 3948c2ecf20Sopenharmony_ci } 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_ci if (bri->scl_gpiod && bri->recover_bus == i2c_generic_scl_recovery) { 3978c2ecf20Sopenharmony_ci bri->get_scl = get_scl_gpio_value; 3988c2ecf20Sopenharmony_ci bri->set_scl = set_scl_gpio_value; 3998c2ecf20Sopenharmony_ci if (bri->sda_gpiod) { 4008c2ecf20Sopenharmony_ci bri->get_sda = get_sda_gpio_value; 4018c2ecf20Sopenharmony_ci /* FIXME: add proper flag instead of '0' once available */ 4028c2ecf20Sopenharmony_ci if (gpiod_get_direction(bri->sda_gpiod) == 0) 4038c2ecf20Sopenharmony_ci bri->set_sda = set_sda_gpio_value; 4048c2ecf20Sopenharmony_ci } 4058c2ecf20Sopenharmony_ci } else if (bri->recover_bus == i2c_generic_scl_recovery) { 4068c2ecf20Sopenharmony_ci /* Generic SCL recovery */ 4078c2ecf20Sopenharmony_ci if (!bri->set_scl || !bri->get_scl) { 4088c2ecf20Sopenharmony_ci err_str = "no {get|set}_scl() found"; 4098c2ecf20Sopenharmony_ci goto err; 4108c2ecf20Sopenharmony_ci } 4118c2ecf20Sopenharmony_ci if (!bri->set_sda && !bri->get_sda) { 4128c2ecf20Sopenharmony_ci err_str = "either get_sda() or set_sda() needed"; 4138c2ecf20Sopenharmony_ci goto err; 4148c2ecf20Sopenharmony_ci } 4158c2ecf20Sopenharmony_ci } 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_ci return 0; 4188c2ecf20Sopenharmony_ci err: 4198c2ecf20Sopenharmony_ci dev_printk(err_level, &adap->dev, "Not using recovery: %s\n", err_str); 4208c2ecf20Sopenharmony_ci adap->bus_recovery_info = NULL; 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci return -EINVAL; 4238c2ecf20Sopenharmony_ci} 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_cistatic int i2c_smbus_host_notify_to_irq(const struct i2c_client *client) 4268c2ecf20Sopenharmony_ci{ 4278c2ecf20Sopenharmony_ci struct i2c_adapter *adap = client->adapter; 4288c2ecf20Sopenharmony_ci unsigned int irq; 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci if (!adap->host_notify_domain) 4318c2ecf20Sopenharmony_ci return -ENXIO; 4328c2ecf20Sopenharmony_ci 4338c2ecf20Sopenharmony_ci if (client->flags & I2C_CLIENT_TEN) 4348c2ecf20Sopenharmony_ci return -EINVAL; 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci irq = irq_create_mapping(adap->host_notify_domain, client->addr); 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci return irq > 0 ? irq : -ENXIO; 4398c2ecf20Sopenharmony_ci} 4408c2ecf20Sopenharmony_ci 4418c2ecf20Sopenharmony_cistatic int i2c_device_probe(struct device *dev) 4428c2ecf20Sopenharmony_ci{ 4438c2ecf20Sopenharmony_ci struct i2c_client *client = i2c_verify_client(dev); 4448c2ecf20Sopenharmony_ci struct i2c_driver *driver; 4458c2ecf20Sopenharmony_ci int status; 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci if (!client) 4488c2ecf20Sopenharmony_ci return 0; 4498c2ecf20Sopenharmony_ci 4508c2ecf20Sopenharmony_ci client->irq = client->init_irq; 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci if (!client->irq) { 4538c2ecf20Sopenharmony_ci int irq = -ENOENT; 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci if (client->flags & I2C_CLIENT_HOST_NOTIFY) { 4568c2ecf20Sopenharmony_ci dev_dbg(dev, "Using Host Notify IRQ\n"); 4578c2ecf20Sopenharmony_ci /* Keep adapter active when Host Notify is required */ 4588c2ecf20Sopenharmony_ci pm_runtime_get_sync(&client->adapter->dev); 4598c2ecf20Sopenharmony_ci irq = i2c_smbus_host_notify_to_irq(client); 4608c2ecf20Sopenharmony_ci } else if (dev->of_node) { 4618c2ecf20Sopenharmony_ci irq = of_irq_get_byname(dev->of_node, "irq"); 4628c2ecf20Sopenharmony_ci if (irq == -EINVAL || irq == -ENODATA) 4638c2ecf20Sopenharmony_ci irq = of_irq_get(dev->of_node, 0); 4648c2ecf20Sopenharmony_ci } else if (ACPI_COMPANION(dev)) { 4658c2ecf20Sopenharmony_ci irq = i2c_acpi_get_irq(client); 4668c2ecf20Sopenharmony_ci } 4678c2ecf20Sopenharmony_ci if (irq == -EPROBE_DEFER) { 4688c2ecf20Sopenharmony_ci status = irq; 4698c2ecf20Sopenharmony_ci goto put_sync_adapter; 4708c2ecf20Sopenharmony_ci } 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ci if (irq < 0) 4738c2ecf20Sopenharmony_ci irq = 0; 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci client->irq = irq; 4768c2ecf20Sopenharmony_ci } 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ci driver = to_i2c_driver(dev->driver); 4798c2ecf20Sopenharmony_ci 4808c2ecf20Sopenharmony_ci /* 4818c2ecf20Sopenharmony_ci * An I2C ID table is not mandatory, if and only if, a suitable OF 4828c2ecf20Sopenharmony_ci * or ACPI ID table is supplied for the probing device. 4838c2ecf20Sopenharmony_ci */ 4848c2ecf20Sopenharmony_ci if (!driver->id_table && 4858c2ecf20Sopenharmony_ci !acpi_driver_match_device(dev, dev->driver) && 4868c2ecf20Sopenharmony_ci !i2c_of_match_device(dev->driver->of_match_table, client)) { 4878c2ecf20Sopenharmony_ci status = -ENODEV; 4888c2ecf20Sopenharmony_ci goto put_sync_adapter; 4898c2ecf20Sopenharmony_ci } 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci if (client->flags & I2C_CLIENT_WAKE) { 4928c2ecf20Sopenharmony_ci int wakeirq; 4938c2ecf20Sopenharmony_ci 4948c2ecf20Sopenharmony_ci wakeirq = of_irq_get_byname(dev->of_node, "wakeup"); 4958c2ecf20Sopenharmony_ci if (wakeirq == -EPROBE_DEFER) { 4968c2ecf20Sopenharmony_ci status = wakeirq; 4978c2ecf20Sopenharmony_ci goto put_sync_adapter; 4988c2ecf20Sopenharmony_ci } 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_ci device_init_wakeup(&client->dev, true); 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ci if (wakeirq > 0 && wakeirq != client->irq) 5038c2ecf20Sopenharmony_ci status = dev_pm_set_dedicated_wake_irq(dev, wakeirq); 5048c2ecf20Sopenharmony_ci else if (client->irq > 0) 5058c2ecf20Sopenharmony_ci status = dev_pm_set_wake_irq(dev, client->irq); 5068c2ecf20Sopenharmony_ci else 5078c2ecf20Sopenharmony_ci status = 0; 5088c2ecf20Sopenharmony_ci 5098c2ecf20Sopenharmony_ci if (status) 5108c2ecf20Sopenharmony_ci dev_warn(&client->dev, "failed to set up wakeup irq\n"); 5118c2ecf20Sopenharmony_ci } 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci dev_dbg(dev, "probe\n"); 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci status = of_clk_set_defaults(dev->of_node, false); 5168c2ecf20Sopenharmony_ci if (status < 0) 5178c2ecf20Sopenharmony_ci goto err_clear_wakeup_irq; 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_ci status = dev_pm_domain_attach(&client->dev, true); 5208c2ecf20Sopenharmony_ci if (status) 5218c2ecf20Sopenharmony_ci goto err_clear_wakeup_irq; 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_ci /* 5248c2ecf20Sopenharmony_ci * When there are no more users of probe(), 5258c2ecf20Sopenharmony_ci * rename probe_new to probe. 5268c2ecf20Sopenharmony_ci */ 5278c2ecf20Sopenharmony_ci if (driver->probe_new) 5288c2ecf20Sopenharmony_ci status = driver->probe_new(client); 5298c2ecf20Sopenharmony_ci else if (driver->probe) 5308c2ecf20Sopenharmony_ci status = driver->probe(client, 5318c2ecf20Sopenharmony_ci i2c_match_id(driver->id_table, client)); 5328c2ecf20Sopenharmony_ci else 5338c2ecf20Sopenharmony_ci status = -EINVAL; 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_ci if (status) 5368c2ecf20Sopenharmony_ci goto err_detach_pm_domain; 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci return 0; 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_cierr_detach_pm_domain: 5418c2ecf20Sopenharmony_ci dev_pm_domain_detach(&client->dev, true); 5428c2ecf20Sopenharmony_cierr_clear_wakeup_irq: 5438c2ecf20Sopenharmony_ci dev_pm_clear_wake_irq(&client->dev); 5448c2ecf20Sopenharmony_ci device_init_wakeup(&client->dev, false); 5458c2ecf20Sopenharmony_ciput_sync_adapter: 5468c2ecf20Sopenharmony_ci if (client->flags & I2C_CLIENT_HOST_NOTIFY) 5478c2ecf20Sopenharmony_ci pm_runtime_put_sync(&client->adapter->dev); 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_ci return status; 5508c2ecf20Sopenharmony_ci} 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_cistatic int i2c_device_remove(struct device *dev) 5538c2ecf20Sopenharmony_ci{ 5548c2ecf20Sopenharmony_ci struct i2c_client *client = i2c_verify_client(dev); 5558c2ecf20Sopenharmony_ci struct i2c_driver *driver; 5568c2ecf20Sopenharmony_ci int status = 0; 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_ci if (!client || !dev->driver) 5598c2ecf20Sopenharmony_ci return 0; 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci driver = to_i2c_driver(dev->driver); 5628c2ecf20Sopenharmony_ci if (driver->remove) { 5638c2ecf20Sopenharmony_ci dev_dbg(dev, "remove\n"); 5648c2ecf20Sopenharmony_ci status = driver->remove(client); 5658c2ecf20Sopenharmony_ci } 5668c2ecf20Sopenharmony_ci 5678c2ecf20Sopenharmony_ci dev_pm_domain_detach(&client->dev, true); 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ci dev_pm_clear_wake_irq(&client->dev); 5708c2ecf20Sopenharmony_ci device_init_wakeup(&client->dev, false); 5718c2ecf20Sopenharmony_ci 5728c2ecf20Sopenharmony_ci client->irq = 0; 5738c2ecf20Sopenharmony_ci if (client->flags & I2C_CLIENT_HOST_NOTIFY) 5748c2ecf20Sopenharmony_ci pm_runtime_put(&client->adapter->dev); 5758c2ecf20Sopenharmony_ci 5768c2ecf20Sopenharmony_ci return status; 5778c2ecf20Sopenharmony_ci} 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_cistatic void i2c_device_shutdown(struct device *dev) 5808c2ecf20Sopenharmony_ci{ 5818c2ecf20Sopenharmony_ci struct i2c_client *client = i2c_verify_client(dev); 5828c2ecf20Sopenharmony_ci struct i2c_driver *driver; 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci if (!client || !dev->driver) 5858c2ecf20Sopenharmony_ci return; 5868c2ecf20Sopenharmony_ci driver = to_i2c_driver(dev->driver); 5878c2ecf20Sopenharmony_ci if (driver->shutdown) 5888c2ecf20Sopenharmony_ci driver->shutdown(client); 5898c2ecf20Sopenharmony_ci else if (client->irq > 0) 5908c2ecf20Sopenharmony_ci disable_irq(client->irq); 5918c2ecf20Sopenharmony_ci} 5928c2ecf20Sopenharmony_ci 5938c2ecf20Sopenharmony_cistatic void i2c_client_dev_release(struct device *dev) 5948c2ecf20Sopenharmony_ci{ 5958c2ecf20Sopenharmony_ci kfree(to_i2c_client(dev)); 5968c2ecf20Sopenharmony_ci} 5978c2ecf20Sopenharmony_ci 5988c2ecf20Sopenharmony_cistatic ssize_t 5998c2ecf20Sopenharmony_ciname_show(struct device *dev, struct device_attribute *attr, char *buf) 6008c2ecf20Sopenharmony_ci{ 6018c2ecf20Sopenharmony_ci return sprintf(buf, "%s\n", dev->type == &i2c_client_type ? 6028c2ecf20Sopenharmony_ci to_i2c_client(dev)->name : to_i2c_adapter(dev)->name); 6038c2ecf20Sopenharmony_ci} 6048c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(name); 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_cistatic ssize_t 6078c2ecf20Sopenharmony_cimodalias_show(struct device *dev, struct device_attribute *attr, char *buf) 6088c2ecf20Sopenharmony_ci{ 6098c2ecf20Sopenharmony_ci struct i2c_client *client = to_i2c_client(dev); 6108c2ecf20Sopenharmony_ci int len; 6118c2ecf20Sopenharmony_ci 6128c2ecf20Sopenharmony_ci len = of_device_modalias(dev, buf, PAGE_SIZE); 6138c2ecf20Sopenharmony_ci if (len != -ENODEV) 6148c2ecf20Sopenharmony_ci return len; 6158c2ecf20Sopenharmony_ci 6168c2ecf20Sopenharmony_ci len = acpi_device_modalias(dev, buf, PAGE_SIZE -1); 6178c2ecf20Sopenharmony_ci if (len != -ENODEV) 6188c2ecf20Sopenharmony_ci return len; 6198c2ecf20Sopenharmony_ci 6208c2ecf20Sopenharmony_ci return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name); 6218c2ecf20Sopenharmony_ci} 6228c2ecf20Sopenharmony_cistatic DEVICE_ATTR_RO(modalias); 6238c2ecf20Sopenharmony_ci 6248c2ecf20Sopenharmony_cistatic struct attribute *i2c_dev_attrs[] = { 6258c2ecf20Sopenharmony_ci &dev_attr_name.attr, 6268c2ecf20Sopenharmony_ci /* modalias helps coldplug: modprobe $(cat .../modalias) */ 6278c2ecf20Sopenharmony_ci &dev_attr_modalias.attr, 6288c2ecf20Sopenharmony_ci NULL 6298c2ecf20Sopenharmony_ci}; 6308c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(i2c_dev); 6318c2ecf20Sopenharmony_ci 6328c2ecf20Sopenharmony_cistruct bus_type i2c_bus_type = { 6338c2ecf20Sopenharmony_ci .name = "i2c", 6348c2ecf20Sopenharmony_ci .match = i2c_device_match, 6358c2ecf20Sopenharmony_ci .probe = i2c_device_probe, 6368c2ecf20Sopenharmony_ci .remove = i2c_device_remove, 6378c2ecf20Sopenharmony_ci .shutdown = i2c_device_shutdown, 6388c2ecf20Sopenharmony_ci}; 6398c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_bus_type); 6408c2ecf20Sopenharmony_ci 6418c2ecf20Sopenharmony_cistruct device_type i2c_client_type = { 6428c2ecf20Sopenharmony_ci .groups = i2c_dev_groups, 6438c2ecf20Sopenharmony_ci .uevent = i2c_device_uevent, 6448c2ecf20Sopenharmony_ci .release = i2c_client_dev_release, 6458c2ecf20Sopenharmony_ci}; 6468c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_client_type); 6478c2ecf20Sopenharmony_ci 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_ci/** 6508c2ecf20Sopenharmony_ci * i2c_verify_client - return parameter as i2c_client, or NULL 6518c2ecf20Sopenharmony_ci * @dev: device, probably from some driver model iterator 6528c2ecf20Sopenharmony_ci * 6538c2ecf20Sopenharmony_ci * When traversing the driver model tree, perhaps using driver model 6548c2ecf20Sopenharmony_ci * iterators like @device_for_each_child(), you can't assume very much 6558c2ecf20Sopenharmony_ci * about the nodes you find. Use this function to avoid oopses caused 6568c2ecf20Sopenharmony_ci * by wrongly treating some non-I2C device as an i2c_client. 6578c2ecf20Sopenharmony_ci */ 6588c2ecf20Sopenharmony_cistruct i2c_client *i2c_verify_client(struct device *dev) 6598c2ecf20Sopenharmony_ci{ 6608c2ecf20Sopenharmony_ci return (dev->type == &i2c_client_type) 6618c2ecf20Sopenharmony_ci ? to_i2c_client(dev) 6628c2ecf20Sopenharmony_ci : NULL; 6638c2ecf20Sopenharmony_ci} 6648c2ecf20Sopenharmony_ciEXPORT_SYMBOL(i2c_verify_client); 6658c2ecf20Sopenharmony_ci 6668c2ecf20Sopenharmony_ci 6678c2ecf20Sopenharmony_ci/* Return a unique address which takes the flags of the client into account */ 6688c2ecf20Sopenharmony_cistatic unsigned short i2c_encode_flags_to_addr(struct i2c_client *client) 6698c2ecf20Sopenharmony_ci{ 6708c2ecf20Sopenharmony_ci unsigned short addr = client->addr; 6718c2ecf20Sopenharmony_ci 6728c2ecf20Sopenharmony_ci /* For some client flags, add an arbitrary offset to avoid collisions */ 6738c2ecf20Sopenharmony_ci if (client->flags & I2C_CLIENT_TEN) 6748c2ecf20Sopenharmony_ci addr |= I2C_ADDR_OFFSET_TEN_BIT; 6758c2ecf20Sopenharmony_ci 6768c2ecf20Sopenharmony_ci if (client->flags & I2C_CLIENT_SLAVE) 6778c2ecf20Sopenharmony_ci addr |= I2C_ADDR_OFFSET_SLAVE; 6788c2ecf20Sopenharmony_ci 6798c2ecf20Sopenharmony_ci return addr; 6808c2ecf20Sopenharmony_ci} 6818c2ecf20Sopenharmony_ci 6828c2ecf20Sopenharmony_ci/* This is a permissive address validity check, I2C address map constraints 6838c2ecf20Sopenharmony_ci * are purposely not enforced, except for the general call address. */ 6848c2ecf20Sopenharmony_cistatic int i2c_check_addr_validity(unsigned int addr, unsigned short flags) 6858c2ecf20Sopenharmony_ci{ 6868c2ecf20Sopenharmony_ci if (flags & I2C_CLIENT_TEN) { 6878c2ecf20Sopenharmony_ci /* 10-bit address, all values are valid */ 6888c2ecf20Sopenharmony_ci if (addr > 0x3ff) 6898c2ecf20Sopenharmony_ci return -EINVAL; 6908c2ecf20Sopenharmony_ci } else { 6918c2ecf20Sopenharmony_ci /* 7-bit address, reject the general call address */ 6928c2ecf20Sopenharmony_ci if (addr == 0x00 || addr > 0x7f) 6938c2ecf20Sopenharmony_ci return -EINVAL; 6948c2ecf20Sopenharmony_ci } 6958c2ecf20Sopenharmony_ci return 0; 6968c2ecf20Sopenharmony_ci} 6978c2ecf20Sopenharmony_ci 6988c2ecf20Sopenharmony_ci/* And this is a strict address validity check, used when probing. If a 6998c2ecf20Sopenharmony_ci * device uses a reserved address, then it shouldn't be probed. 7-bit 7008c2ecf20Sopenharmony_ci * addressing is assumed, 10-bit address devices are rare and should be 7018c2ecf20Sopenharmony_ci * explicitly enumerated. */ 7028c2ecf20Sopenharmony_ciint i2c_check_7bit_addr_validity_strict(unsigned short addr) 7038c2ecf20Sopenharmony_ci{ 7048c2ecf20Sopenharmony_ci /* 7058c2ecf20Sopenharmony_ci * Reserved addresses per I2C specification: 7068c2ecf20Sopenharmony_ci * 0x00 General call address / START byte 7078c2ecf20Sopenharmony_ci * 0x01 CBUS address 7088c2ecf20Sopenharmony_ci * 0x02 Reserved for different bus format 7098c2ecf20Sopenharmony_ci * 0x03 Reserved for future purposes 7108c2ecf20Sopenharmony_ci * 0x04-0x07 Hs-mode master code 7118c2ecf20Sopenharmony_ci * 0x78-0x7b 10-bit slave addressing 7128c2ecf20Sopenharmony_ci * 0x7c-0x7f Reserved for future purposes 7138c2ecf20Sopenharmony_ci */ 7148c2ecf20Sopenharmony_ci if (addr < 0x08 || addr > 0x77) 7158c2ecf20Sopenharmony_ci return -EINVAL; 7168c2ecf20Sopenharmony_ci return 0; 7178c2ecf20Sopenharmony_ci} 7188c2ecf20Sopenharmony_ci 7198c2ecf20Sopenharmony_cistatic int __i2c_check_addr_busy(struct device *dev, void *addrp) 7208c2ecf20Sopenharmony_ci{ 7218c2ecf20Sopenharmony_ci struct i2c_client *client = i2c_verify_client(dev); 7228c2ecf20Sopenharmony_ci int addr = *(int *)addrp; 7238c2ecf20Sopenharmony_ci 7248c2ecf20Sopenharmony_ci if (client && i2c_encode_flags_to_addr(client) == addr) 7258c2ecf20Sopenharmony_ci return -EBUSY; 7268c2ecf20Sopenharmony_ci return 0; 7278c2ecf20Sopenharmony_ci} 7288c2ecf20Sopenharmony_ci 7298c2ecf20Sopenharmony_ci/* walk up mux tree */ 7308c2ecf20Sopenharmony_cistatic int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr) 7318c2ecf20Sopenharmony_ci{ 7328c2ecf20Sopenharmony_ci struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); 7338c2ecf20Sopenharmony_ci int result; 7348c2ecf20Sopenharmony_ci 7358c2ecf20Sopenharmony_ci result = device_for_each_child(&adapter->dev, &addr, 7368c2ecf20Sopenharmony_ci __i2c_check_addr_busy); 7378c2ecf20Sopenharmony_ci 7388c2ecf20Sopenharmony_ci if (!result && parent) 7398c2ecf20Sopenharmony_ci result = i2c_check_mux_parents(parent, addr); 7408c2ecf20Sopenharmony_ci 7418c2ecf20Sopenharmony_ci return result; 7428c2ecf20Sopenharmony_ci} 7438c2ecf20Sopenharmony_ci 7448c2ecf20Sopenharmony_ci/* recurse down mux tree */ 7458c2ecf20Sopenharmony_cistatic int i2c_check_mux_children(struct device *dev, void *addrp) 7468c2ecf20Sopenharmony_ci{ 7478c2ecf20Sopenharmony_ci int result; 7488c2ecf20Sopenharmony_ci 7498c2ecf20Sopenharmony_ci if (dev->type == &i2c_adapter_type) 7508c2ecf20Sopenharmony_ci result = device_for_each_child(dev, addrp, 7518c2ecf20Sopenharmony_ci i2c_check_mux_children); 7528c2ecf20Sopenharmony_ci else 7538c2ecf20Sopenharmony_ci result = __i2c_check_addr_busy(dev, addrp); 7548c2ecf20Sopenharmony_ci 7558c2ecf20Sopenharmony_ci return result; 7568c2ecf20Sopenharmony_ci} 7578c2ecf20Sopenharmony_ci 7588c2ecf20Sopenharmony_cistatic int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr) 7598c2ecf20Sopenharmony_ci{ 7608c2ecf20Sopenharmony_ci struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter); 7618c2ecf20Sopenharmony_ci int result = 0; 7628c2ecf20Sopenharmony_ci 7638c2ecf20Sopenharmony_ci if (parent) 7648c2ecf20Sopenharmony_ci result = i2c_check_mux_parents(parent, addr); 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_ci if (!result) 7678c2ecf20Sopenharmony_ci result = device_for_each_child(&adapter->dev, &addr, 7688c2ecf20Sopenharmony_ci i2c_check_mux_children); 7698c2ecf20Sopenharmony_ci 7708c2ecf20Sopenharmony_ci return result; 7718c2ecf20Sopenharmony_ci} 7728c2ecf20Sopenharmony_ci 7738c2ecf20Sopenharmony_ci/** 7748c2ecf20Sopenharmony_ci * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment 7758c2ecf20Sopenharmony_ci * @adapter: Target I2C bus segment 7768c2ecf20Sopenharmony_ci * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT 7778c2ecf20Sopenharmony_ci * locks only this branch in the adapter tree 7788c2ecf20Sopenharmony_ci */ 7798c2ecf20Sopenharmony_cistatic void i2c_adapter_lock_bus(struct i2c_adapter *adapter, 7808c2ecf20Sopenharmony_ci unsigned int flags) 7818c2ecf20Sopenharmony_ci{ 7828c2ecf20Sopenharmony_ci rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter)); 7838c2ecf20Sopenharmony_ci} 7848c2ecf20Sopenharmony_ci 7858c2ecf20Sopenharmony_ci/** 7868c2ecf20Sopenharmony_ci * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment 7878c2ecf20Sopenharmony_ci * @adapter: Target I2C bus segment 7888c2ecf20Sopenharmony_ci * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT 7898c2ecf20Sopenharmony_ci * trylocks only this branch in the adapter tree 7908c2ecf20Sopenharmony_ci */ 7918c2ecf20Sopenharmony_cistatic int i2c_adapter_trylock_bus(struct i2c_adapter *adapter, 7928c2ecf20Sopenharmony_ci unsigned int flags) 7938c2ecf20Sopenharmony_ci{ 7948c2ecf20Sopenharmony_ci return rt_mutex_trylock(&adapter->bus_lock); 7958c2ecf20Sopenharmony_ci} 7968c2ecf20Sopenharmony_ci 7978c2ecf20Sopenharmony_ci/** 7988c2ecf20Sopenharmony_ci * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment 7998c2ecf20Sopenharmony_ci * @adapter: Target I2C bus segment 8008c2ecf20Sopenharmony_ci * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT 8018c2ecf20Sopenharmony_ci * unlocks only this branch in the adapter tree 8028c2ecf20Sopenharmony_ci */ 8038c2ecf20Sopenharmony_cistatic void i2c_adapter_unlock_bus(struct i2c_adapter *adapter, 8048c2ecf20Sopenharmony_ci unsigned int flags) 8058c2ecf20Sopenharmony_ci{ 8068c2ecf20Sopenharmony_ci rt_mutex_unlock(&adapter->bus_lock); 8078c2ecf20Sopenharmony_ci} 8088c2ecf20Sopenharmony_ci 8098c2ecf20Sopenharmony_cistatic void i2c_dev_set_name(struct i2c_adapter *adap, 8108c2ecf20Sopenharmony_ci struct i2c_client *client, 8118c2ecf20Sopenharmony_ci struct i2c_board_info const *info) 8128c2ecf20Sopenharmony_ci{ 8138c2ecf20Sopenharmony_ci struct acpi_device *adev = ACPI_COMPANION(&client->dev); 8148c2ecf20Sopenharmony_ci 8158c2ecf20Sopenharmony_ci if (info && info->dev_name) { 8168c2ecf20Sopenharmony_ci dev_set_name(&client->dev, "i2c-%s", info->dev_name); 8178c2ecf20Sopenharmony_ci return; 8188c2ecf20Sopenharmony_ci } 8198c2ecf20Sopenharmony_ci 8208c2ecf20Sopenharmony_ci if (adev) { 8218c2ecf20Sopenharmony_ci dev_set_name(&client->dev, "i2c-%s", acpi_dev_name(adev)); 8228c2ecf20Sopenharmony_ci return; 8238c2ecf20Sopenharmony_ci } 8248c2ecf20Sopenharmony_ci 8258c2ecf20Sopenharmony_ci dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), 8268c2ecf20Sopenharmony_ci i2c_encode_flags_to_addr(client)); 8278c2ecf20Sopenharmony_ci} 8288c2ecf20Sopenharmony_ci 8298c2ecf20Sopenharmony_ciint i2c_dev_irq_from_resources(const struct resource *resources, 8308c2ecf20Sopenharmony_ci unsigned int num_resources) 8318c2ecf20Sopenharmony_ci{ 8328c2ecf20Sopenharmony_ci struct irq_data *irqd; 8338c2ecf20Sopenharmony_ci int i; 8348c2ecf20Sopenharmony_ci 8358c2ecf20Sopenharmony_ci for (i = 0; i < num_resources; i++) { 8368c2ecf20Sopenharmony_ci const struct resource *r = &resources[i]; 8378c2ecf20Sopenharmony_ci 8388c2ecf20Sopenharmony_ci if (resource_type(r) != IORESOURCE_IRQ) 8398c2ecf20Sopenharmony_ci continue; 8408c2ecf20Sopenharmony_ci 8418c2ecf20Sopenharmony_ci if (r->flags & IORESOURCE_BITS) { 8428c2ecf20Sopenharmony_ci irqd = irq_get_irq_data(r->start); 8438c2ecf20Sopenharmony_ci if (!irqd) 8448c2ecf20Sopenharmony_ci break; 8458c2ecf20Sopenharmony_ci 8468c2ecf20Sopenharmony_ci irqd_set_trigger_type(irqd, r->flags & IORESOURCE_BITS); 8478c2ecf20Sopenharmony_ci } 8488c2ecf20Sopenharmony_ci 8498c2ecf20Sopenharmony_ci return r->start; 8508c2ecf20Sopenharmony_ci } 8518c2ecf20Sopenharmony_ci 8528c2ecf20Sopenharmony_ci return 0; 8538c2ecf20Sopenharmony_ci} 8548c2ecf20Sopenharmony_ci 8558c2ecf20Sopenharmony_ci/** 8568c2ecf20Sopenharmony_ci * i2c_new_client_device - instantiate an i2c device 8578c2ecf20Sopenharmony_ci * @adap: the adapter managing the device 8588c2ecf20Sopenharmony_ci * @info: describes one I2C device; bus_num is ignored 8598c2ecf20Sopenharmony_ci * Context: can sleep 8608c2ecf20Sopenharmony_ci * 8618c2ecf20Sopenharmony_ci * Create an i2c device. Binding is handled through driver model 8628c2ecf20Sopenharmony_ci * probe()/remove() methods. A driver may be bound to this device when we 8638c2ecf20Sopenharmony_ci * return from this function, or any later moment (e.g. maybe hotplugging will 8648c2ecf20Sopenharmony_ci * load the driver module). This call is not appropriate for use by mainboard 8658c2ecf20Sopenharmony_ci * initialization logic, which usually runs during an arch_initcall() long 8668c2ecf20Sopenharmony_ci * before any i2c_adapter could exist. 8678c2ecf20Sopenharmony_ci * 8688c2ecf20Sopenharmony_ci * This returns the new i2c client, which may be saved for later use with 8698c2ecf20Sopenharmony_ci * i2c_unregister_device(); or an ERR_PTR to describe the error. 8708c2ecf20Sopenharmony_ci */ 8718c2ecf20Sopenharmony_cistruct i2c_client * 8728c2ecf20Sopenharmony_cii2c_new_client_device(struct i2c_adapter *adap, struct i2c_board_info const *info) 8738c2ecf20Sopenharmony_ci{ 8748c2ecf20Sopenharmony_ci struct i2c_client *client; 8758c2ecf20Sopenharmony_ci int status; 8768c2ecf20Sopenharmony_ci 8778c2ecf20Sopenharmony_ci client = kzalloc(sizeof *client, GFP_KERNEL); 8788c2ecf20Sopenharmony_ci if (!client) 8798c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 8808c2ecf20Sopenharmony_ci 8818c2ecf20Sopenharmony_ci client->adapter = adap; 8828c2ecf20Sopenharmony_ci 8838c2ecf20Sopenharmony_ci client->dev.platform_data = info->platform_data; 8848c2ecf20Sopenharmony_ci client->flags = info->flags; 8858c2ecf20Sopenharmony_ci client->addr = info->addr; 8868c2ecf20Sopenharmony_ci 8878c2ecf20Sopenharmony_ci client->init_irq = info->irq; 8888c2ecf20Sopenharmony_ci if (!client->init_irq) 8898c2ecf20Sopenharmony_ci client->init_irq = i2c_dev_irq_from_resources(info->resources, 8908c2ecf20Sopenharmony_ci info->num_resources); 8918c2ecf20Sopenharmony_ci 8928c2ecf20Sopenharmony_ci strlcpy(client->name, info->type, sizeof(client->name)); 8938c2ecf20Sopenharmony_ci 8948c2ecf20Sopenharmony_ci status = i2c_check_addr_validity(client->addr, client->flags); 8958c2ecf20Sopenharmony_ci if (status) { 8968c2ecf20Sopenharmony_ci dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n", 8978c2ecf20Sopenharmony_ci client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr); 8988c2ecf20Sopenharmony_ci goto out_err_silent; 8998c2ecf20Sopenharmony_ci } 9008c2ecf20Sopenharmony_ci 9018c2ecf20Sopenharmony_ci /* Check for address business */ 9028c2ecf20Sopenharmony_ci status = i2c_check_addr_busy(adap, i2c_encode_flags_to_addr(client)); 9038c2ecf20Sopenharmony_ci if (status) 9048c2ecf20Sopenharmony_ci goto out_err; 9058c2ecf20Sopenharmony_ci 9068c2ecf20Sopenharmony_ci client->dev.parent = &client->adapter->dev; 9078c2ecf20Sopenharmony_ci client->dev.bus = &i2c_bus_type; 9088c2ecf20Sopenharmony_ci client->dev.type = &i2c_client_type; 9098c2ecf20Sopenharmony_ci client->dev.of_node = of_node_get(info->of_node); 9108c2ecf20Sopenharmony_ci client->dev.fwnode = info->fwnode; 9118c2ecf20Sopenharmony_ci 9128c2ecf20Sopenharmony_ci i2c_dev_set_name(adap, client, info); 9138c2ecf20Sopenharmony_ci 9148c2ecf20Sopenharmony_ci if (info->properties) { 9158c2ecf20Sopenharmony_ci status = device_add_properties(&client->dev, info->properties); 9168c2ecf20Sopenharmony_ci if (status) { 9178c2ecf20Sopenharmony_ci dev_err(&adap->dev, 9188c2ecf20Sopenharmony_ci "Failed to add properties to client %s: %d\n", 9198c2ecf20Sopenharmony_ci client->name, status); 9208c2ecf20Sopenharmony_ci goto out_err_put_of_node; 9218c2ecf20Sopenharmony_ci } 9228c2ecf20Sopenharmony_ci } 9238c2ecf20Sopenharmony_ci 9248c2ecf20Sopenharmony_ci status = device_register(&client->dev); 9258c2ecf20Sopenharmony_ci if (status) 9268c2ecf20Sopenharmony_ci goto out_free_props; 9278c2ecf20Sopenharmony_ci 9288c2ecf20Sopenharmony_ci dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n", 9298c2ecf20Sopenharmony_ci client->name, dev_name(&client->dev)); 9308c2ecf20Sopenharmony_ci 9318c2ecf20Sopenharmony_ci return client; 9328c2ecf20Sopenharmony_ci 9338c2ecf20Sopenharmony_ciout_free_props: 9348c2ecf20Sopenharmony_ci if (info->properties) 9358c2ecf20Sopenharmony_ci device_remove_properties(&client->dev); 9368c2ecf20Sopenharmony_ciout_err_put_of_node: 9378c2ecf20Sopenharmony_ci of_node_put(info->of_node); 9388c2ecf20Sopenharmony_ciout_err: 9398c2ecf20Sopenharmony_ci dev_err(&adap->dev, 9408c2ecf20Sopenharmony_ci "Failed to register i2c client %s at 0x%02x (%d)\n", 9418c2ecf20Sopenharmony_ci client->name, client->addr, status); 9428c2ecf20Sopenharmony_ciout_err_silent: 9438c2ecf20Sopenharmony_ci kfree(client); 9448c2ecf20Sopenharmony_ci return ERR_PTR(status); 9458c2ecf20Sopenharmony_ci} 9468c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_new_client_device); 9478c2ecf20Sopenharmony_ci 9488c2ecf20Sopenharmony_ci/** 9498c2ecf20Sopenharmony_ci * i2c_unregister_device - reverse effect of i2c_new_*_device() 9508c2ecf20Sopenharmony_ci * @client: value returned from i2c_new_*_device() 9518c2ecf20Sopenharmony_ci * Context: can sleep 9528c2ecf20Sopenharmony_ci */ 9538c2ecf20Sopenharmony_civoid i2c_unregister_device(struct i2c_client *client) 9548c2ecf20Sopenharmony_ci{ 9558c2ecf20Sopenharmony_ci if (IS_ERR_OR_NULL(client)) 9568c2ecf20Sopenharmony_ci return; 9578c2ecf20Sopenharmony_ci 9588c2ecf20Sopenharmony_ci if (client->dev.of_node) { 9598c2ecf20Sopenharmony_ci of_node_clear_flag(client->dev.of_node, OF_POPULATED); 9608c2ecf20Sopenharmony_ci of_node_put(client->dev.of_node); 9618c2ecf20Sopenharmony_ci } 9628c2ecf20Sopenharmony_ci 9638c2ecf20Sopenharmony_ci if (ACPI_COMPANION(&client->dev)) 9648c2ecf20Sopenharmony_ci acpi_device_clear_enumerated(ACPI_COMPANION(&client->dev)); 9658c2ecf20Sopenharmony_ci device_unregister(&client->dev); 9668c2ecf20Sopenharmony_ci} 9678c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_unregister_device); 9688c2ecf20Sopenharmony_ci 9698c2ecf20Sopenharmony_ci 9708c2ecf20Sopenharmony_cistatic const struct i2c_device_id dummy_id[] = { 9718c2ecf20Sopenharmony_ci { "dummy", 0 }, 9728c2ecf20Sopenharmony_ci { }, 9738c2ecf20Sopenharmony_ci}; 9748c2ecf20Sopenharmony_ci 9758c2ecf20Sopenharmony_cistatic int dummy_probe(struct i2c_client *client, 9768c2ecf20Sopenharmony_ci const struct i2c_device_id *id) 9778c2ecf20Sopenharmony_ci{ 9788c2ecf20Sopenharmony_ci return 0; 9798c2ecf20Sopenharmony_ci} 9808c2ecf20Sopenharmony_ci 9818c2ecf20Sopenharmony_cistatic int dummy_remove(struct i2c_client *client) 9828c2ecf20Sopenharmony_ci{ 9838c2ecf20Sopenharmony_ci return 0; 9848c2ecf20Sopenharmony_ci} 9858c2ecf20Sopenharmony_ci 9868c2ecf20Sopenharmony_cistatic struct i2c_driver dummy_driver = { 9878c2ecf20Sopenharmony_ci .driver.name = "dummy", 9888c2ecf20Sopenharmony_ci .probe = dummy_probe, 9898c2ecf20Sopenharmony_ci .remove = dummy_remove, 9908c2ecf20Sopenharmony_ci .id_table = dummy_id, 9918c2ecf20Sopenharmony_ci}; 9928c2ecf20Sopenharmony_ci 9938c2ecf20Sopenharmony_ci/** 9948c2ecf20Sopenharmony_ci * i2c_new_dummy_device - return a new i2c device bound to a dummy driver 9958c2ecf20Sopenharmony_ci * @adapter: the adapter managing the device 9968c2ecf20Sopenharmony_ci * @address: seven bit address to be used 9978c2ecf20Sopenharmony_ci * Context: can sleep 9988c2ecf20Sopenharmony_ci * 9998c2ecf20Sopenharmony_ci * This returns an I2C client bound to the "dummy" driver, intended for use 10008c2ecf20Sopenharmony_ci * with devices that consume multiple addresses. Examples of such chips 10018c2ecf20Sopenharmony_ci * include various EEPROMS (like 24c04 and 24c08 models). 10028c2ecf20Sopenharmony_ci * 10038c2ecf20Sopenharmony_ci * These dummy devices have two main uses. First, most I2C and SMBus calls 10048c2ecf20Sopenharmony_ci * except i2c_transfer() need a client handle; the dummy will be that handle. 10058c2ecf20Sopenharmony_ci * And second, this prevents the specified address from being bound to a 10068c2ecf20Sopenharmony_ci * different driver. 10078c2ecf20Sopenharmony_ci * 10088c2ecf20Sopenharmony_ci * This returns the new i2c client, which should be saved for later use with 10098c2ecf20Sopenharmony_ci * i2c_unregister_device(); or an ERR_PTR to describe the error. 10108c2ecf20Sopenharmony_ci */ 10118c2ecf20Sopenharmony_cistruct i2c_client *i2c_new_dummy_device(struct i2c_adapter *adapter, u16 address) 10128c2ecf20Sopenharmony_ci{ 10138c2ecf20Sopenharmony_ci struct i2c_board_info info = { 10148c2ecf20Sopenharmony_ci I2C_BOARD_INFO("dummy", address), 10158c2ecf20Sopenharmony_ci }; 10168c2ecf20Sopenharmony_ci 10178c2ecf20Sopenharmony_ci return i2c_new_client_device(adapter, &info); 10188c2ecf20Sopenharmony_ci} 10198c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_new_dummy_device); 10208c2ecf20Sopenharmony_ci 10218c2ecf20Sopenharmony_cistruct i2c_dummy_devres { 10228c2ecf20Sopenharmony_ci struct i2c_client *client; 10238c2ecf20Sopenharmony_ci}; 10248c2ecf20Sopenharmony_ci 10258c2ecf20Sopenharmony_cistatic void devm_i2c_release_dummy(struct device *dev, void *res) 10268c2ecf20Sopenharmony_ci{ 10278c2ecf20Sopenharmony_ci struct i2c_dummy_devres *this = res; 10288c2ecf20Sopenharmony_ci 10298c2ecf20Sopenharmony_ci i2c_unregister_device(this->client); 10308c2ecf20Sopenharmony_ci} 10318c2ecf20Sopenharmony_ci 10328c2ecf20Sopenharmony_ci/** 10338c2ecf20Sopenharmony_ci * devm_i2c_new_dummy_device - return a new i2c device bound to a dummy driver 10348c2ecf20Sopenharmony_ci * @dev: device the managed resource is bound to 10358c2ecf20Sopenharmony_ci * @adapter: the adapter managing the device 10368c2ecf20Sopenharmony_ci * @address: seven bit address to be used 10378c2ecf20Sopenharmony_ci * Context: can sleep 10388c2ecf20Sopenharmony_ci * 10398c2ecf20Sopenharmony_ci * This is the device-managed version of @i2c_new_dummy_device. It returns the 10408c2ecf20Sopenharmony_ci * new i2c client or an ERR_PTR in case of an error. 10418c2ecf20Sopenharmony_ci */ 10428c2ecf20Sopenharmony_cistruct i2c_client *devm_i2c_new_dummy_device(struct device *dev, 10438c2ecf20Sopenharmony_ci struct i2c_adapter *adapter, 10448c2ecf20Sopenharmony_ci u16 address) 10458c2ecf20Sopenharmony_ci{ 10468c2ecf20Sopenharmony_ci struct i2c_dummy_devres *dr; 10478c2ecf20Sopenharmony_ci struct i2c_client *client; 10488c2ecf20Sopenharmony_ci 10498c2ecf20Sopenharmony_ci dr = devres_alloc(devm_i2c_release_dummy, sizeof(*dr), GFP_KERNEL); 10508c2ecf20Sopenharmony_ci if (!dr) 10518c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 10528c2ecf20Sopenharmony_ci 10538c2ecf20Sopenharmony_ci client = i2c_new_dummy_device(adapter, address); 10548c2ecf20Sopenharmony_ci if (IS_ERR(client)) { 10558c2ecf20Sopenharmony_ci devres_free(dr); 10568c2ecf20Sopenharmony_ci } else { 10578c2ecf20Sopenharmony_ci dr->client = client; 10588c2ecf20Sopenharmony_ci devres_add(dev, dr); 10598c2ecf20Sopenharmony_ci } 10608c2ecf20Sopenharmony_ci 10618c2ecf20Sopenharmony_ci return client; 10628c2ecf20Sopenharmony_ci} 10638c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(devm_i2c_new_dummy_device); 10648c2ecf20Sopenharmony_ci 10658c2ecf20Sopenharmony_ci/** 10668c2ecf20Sopenharmony_ci * i2c_new_ancillary_device - Helper to get the instantiated secondary address 10678c2ecf20Sopenharmony_ci * and create the associated device 10688c2ecf20Sopenharmony_ci * @client: Handle to the primary client 10698c2ecf20Sopenharmony_ci * @name: Handle to specify which secondary address to get 10708c2ecf20Sopenharmony_ci * @default_addr: Used as a fallback if no secondary address was specified 10718c2ecf20Sopenharmony_ci * Context: can sleep 10728c2ecf20Sopenharmony_ci * 10738c2ecf20Sopenharmony_ci * I2C clients can be composed of multiple I2C slaves bound together in a single 10748c2ecf20Sopenharmony_ci * component. The I2C client driver then binds to the master I2C slave and needs 10758c2ecf20Sopenharmony_ci * to create I2C dummy clients to communicate with all the other slaves. 10768c2ecf20Sopenharmony_ci * 10778c2ecf20Sopenharmony_ci * This function creates and returns an I2C dummy client whose I2C address is 10788c2ecf20Sopenharmony_ci * retrieved from the platform firmware based on the given slave name. If no 10798c2ecf20Sopenharmony_ci * address is specified by the firmware default_addr is used. 10808c2ecf20Sopenharmony_ci * 10818c2ecf20Sopenharmony_ci * On DT-based platforms the address is retrieved from the "reg" property entry 10828c2ecf20Sopenharmony_ci * cell whose "reg-names" value matches the slave name. 10838c2ecf20Sopenharmony_ci * 10848c2ecf20Sopenharmony_ci * This returns the new i2c client, which should be saved for later use with 10858c2ecf20Sopenharmony_ci * i2c_unregister_device(); or an ERR_PTR to describe the error. 10868c2ecf20Sopenharmony_ci */ 10878c2ecf20Sopenharmony_cistruct i2c_client *i2c_new_ancillary_device(struct i2c_client *client, 10888c2ecf20Sopenharmony_ci const char *name, 10898c2ecf20Sopenharmony_ci u16 default_addr) 10908c2ecf20Sopenharmony_ci{ 10918c2ecf20Sopenharmony_ci struct device_node *np = client->dev.of_node; 10928c2ecf20Sopenharmony_ci u32 addr = default_addr; 10938c2ecf20Sopenharmony_ci int i; 10948c2ecf20Sopenharmony_ci 10958c2ecf20Sopenharmony_ci if (np) { 10968c2ecf20Sopenharmony_ci i = of_property_match_string(np, "reg-names", name); 10978c2ecf20Sopenharmony_ci if (i >= 0) 10988c2ecf20Sopenharmony_ci of_property_read_u32_index(np, "reg", i, &addr); 10998c2ecf20Sopenharmony_ci } 11008c2ecf20Sopenharmony_ci 11018c2ecf20Sopenharmony_ci dev_dbg(&client->adapter->dev, "Address for %s : 0x%x\n", name, addr); 11028c2ecf20Sopenharmony_ci return i2c_new_dummy_device(client->adapter, addr); 11038c2ecf20Sopenharmony_ci} 11048c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_new_ancillary_device); 11058c2ecf20Sopenharmony_ci 11068c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------------- */ 11078c2ecf20Sopenharmony_ci 11088c2ecf20Sopenharmony_ci/* I2C bus adapters -- one roots each I2C or SMBUS segment */ 11098c2ecf20Sopenharmony_ci 11108c2ecf20Sopenharmony_cistatic void i2c_adapter_dev_release(struct device *dev) 11118c2ecf20Sopenharmony_ci{ 11128c2ecf20Sopenharmony_ci struct i2c_adapter *adap = to_i2c_adapter(dev); 11138c2ecf20Sopenharmony_ci complete(&adap->dev_released); 11148c2ecf20Sopenharmony_ci} 11158c2ecf20Sopenharmony_ci 11168c2ecf20Sopenharmony_ciunsigned int i2c_adapter_depth(struct i2c_adapter *adapter) 11178c2ecf20Sopenharmony_ci{ 11188c2ecf20Sopenharmony_ci unsigned int depth = 0; 11198c2ecf20Sopenharmony_ci 11208c2ecf20Sopenharmony_ci while ((adapter = i2c_parent_is_i2c_adapter(adapter))) 11218c2ecf20Sopenharmony_ci depth++; 11228c2ecf20Sopenharmony_ci 11238c2ecf20Sopenharmony_ci WARN_ONCE(depth >= MAX_LOCKDEP_SUBCLASSES, 11248c2ecf20Sopenharmony_ci "adapter depth exceeds lockdep subclass limit\n"); 11258c2ecf20Sopenharmony_ci 11268c2ecf20Sopenharmony_ci return depth; 11278c2ecf20Sopenharmony_ci} 11288c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_adapter_depth); 11298c2ecf20Sopenharmony_ci 11308c2ecf20Sopenharmony_ci/* 11318c2ecf20Sopenharmony_ci * Let users instantiate I2C devices through sysfs. This can be used when 11328c2ecf20Sopenharmony_ci * platform initialization code doesn't contain the proper data for 11338c2ecf20Sopenharmony_ci * whatever reason. Also useful for drivers that do device detection and 11348c2ecf20Sopenharmony_ci * detection fails, either because the device uses an unexpected address, 11358c2ecf20Sopenharmony_ci * or this is a compatible device with different ID register values. 11368c2ecf20Sopenharmony_ci * 11378c2ecf20Sopenharmony_ci * Parameter checking may look overzealous, but we really don't want 11388c2ecf20Sopenharmony_ci * the user to provide incorrect parameters. 11398c2ecf20Sopenharmony_ci */ 11408c2ecf20Sopenharmony_cistatic ssize_t 11418c2ecf20Sopenharmony_cinew_device_store(struct device *dev, struct device_attribute *attr, 11428c2ecf20Sopenharmony_ci const char *buf, size_t count) 11438c2ecf20Sopenharmony_ci{ 11448c2ecf20Sopenharmony_ci struct i2c_adapter *adap = to_i2c_adapter(dev); 11458c2ecf20Sopenharmony_ci struct i2c_board_info info; 11468c2ecf20Sopenharmony_ci struct i2c_client *client; 11478c2ecf20Sopenharmony_ci char *blank, end; 11488c2ecf20Sopenharmony_ci int res; 11498c2ecf20Sopenharmony_ci 11508c2ecf20Sopenharmony_ci memset(&info, 0, sizeof(struct i2c_board_info)); 11518c2ecf20Sopenharmony_ci 11528c2ecf20Sopenharmony_ci blank = strchr(buf, ' '); 11538c2ecf20Sopenharmony_ci if (!blank) { 11548c2ecf20Sopenharmony_ci dev_err(dev, "%s: Missing parameters\n", "new_device"); 11558c2ecf20Sopenharmony_ci return -EINVAL; 11568c2ecf20Sopenharmony_ci } 11578c2ecf20Sopenharmony_ci if (blank - buf > I2C_NAME_SIZE - 1) { 11588c2ecf20Sopenharmony_ci dev_err(dev, "%s: Invalid device name\n", "new_device"); 11598c2ecf20Sopenharmony_ci return -EINVAL; 11608c2ecf20Sopenharmony_ci } 11618c2ecf20Sopenharmony_ci memcpy(info.type, buf, blank - buf); 11628c2ecf20Sopenharmony_ci 11638c2ecf20Sopenharmony_ci /* Parse remaining parameters, reject extra parameters */ 11648c2ecf20Sopenharmony_ci res = sscanf(++blank, "%hi%c", &info.addr, &end); 11658c2ecf20Sopenharmony_ci if (res < 1) { 11668c2ecf20Sopenharmony_ci dev_err(dev, "%s: Can't parse I2C address\n", "new_device"); 11678c2ecf20Sopenharmony_ci return -EINVAL; 11688c2ecf20Sopenharmony_ci } 11698c2ecf20Sopenharmony_ci if (res > 1 && end != '\n') { 11708c2ecf20Sopenharmony_ci dev_err(dev, "%s: Extra parameters\n", "new_device"); 11718c2ecf20Sopenharmony_ci return -EINVAL; 11728c2ecf20Sopenharmony_ci } 11738c2ecf20Sopenharmony_ci 11748c2ecf20Sopenharmony_ci if ((info.addr & I2C_ADDR_OFFSET_TEN_BIT) == I2C_ADDR_OFFSET_TEN_BIT) { 11758c2ecf20Sopenharmony_ci info.addr &= ~I2C_ADDR_OFFSET_TEN_BIT; 11768c2ecf20Sopenharmony_ci info.flags |= I2C_CLIENT_TEN; 11778c2ecf20Sopenharmony_ci } 11788c2ecf20Sopenharmony_ci 11798c2ecf20Sopenharmony_ci if (info.addr & I2C_ADDR_OFFSET_SLAVE) { 11808c2ecf20Sopenharmony_ci info.addr &= ~I2C_ADDR_OFFSET_SLAVE; 11818c2ecf20Sopenharmony_ci info.flags |= I2C_CLIENT_SLAVE; 11828c2ecf20Sopenharmony_ci } 11838c2ecf20Sopenharmony_ci 11848c2ecf20Sopenharmony_ci client = i2c_new_client_device(adap, &info); 11858c2ecf20Sopenharmony_ci if (IS_ERR(client)) 11868c2ecf20Sopenharmony_ci return PTR_ERR(client); 11878c2ecf20Sopenharmony_ci 11888c2ecf20Sopenharmony_ci /* Keep track of the added device */ 11898c2ecf20Sopenharmony_ci mutex_lock(&adap->userspace_clients_lock); 11908c2ecf20Sopenharmony_ci list_add_tail(&client->detected, &adap->userspace_clients); 11918c2ecf20Sopenharmony_ci mutex_unlock(&adap->userspace_clients_lock); 11928c2ecf20Sopenharmony_ci dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device", 11938c2ecf20Sopenharmony_ci info.type, info.addr); 11948c2ecf20Sopenharmony_ci 11958c2ecf20Sopenharmony_ci return count; 11968c2ecf20Sopenharmony_ci} 11978c2ecf20Sopenharmony_cistatic DEVICE_ATTR_WO(new_device); 11988c2ecf20Sopenharmony_ci 11998c2ecf20Sopenharmony_ci/* 12008c2ecf20Sopenharmony_ci * And of course let the users delete the devices they instantiated, if 12018c2ecf20Sopenharmony_ci * they got it wrong. This interface can only be used to delete devices 12028c2ecf20Sopenharmony_ci * instantiated by i2c_sysfs_new_device above. This guarantees that we 12038c2ecf20Sopenharmony_ci * don't delete devices to which some kernel code still has references. 12048c2ecf20Sopenharmony_ci * 12058c2ecf20Sopenharmony_ci * Parameter checking may look overzealous, but we really don't want 12068c2ecf20Sopenharmony_ci * the user to delete the wrong device. 12078c2ecf20Sopenharmony_ci */ 12088c2ecf20Sopenharmony_cistatic ssize_t 12098c2ecf20Sopenharmony_cidelete_device_store(struct device *dev, struct device_attribute *attr, 12108c2ecf20Sopenharmony_ci const char *buf, size_t count) 12118c2ecf20Sopenharmony_ci{ 12128c2ecf20Sopenharmony_ci struct i2c_adapter *adap = to_i2c_adapter(dev); 12138c2ecf20Sopenharmony_ci struct i2c_client *client, *next; 12148c2ecf20Sopenharmony_ci unsigned short addr; 12158c2ecf20Sopenharmony_ci char end; 12168c2ecf20Sopenharmony_ci int res; 12178c2ecf20Sopenharmony_ci 12188c2ecf20Sopenharmony_ci /* Parse parameters, reject extra parameters */ 12198c2ecf20Sopenharmony_ci res = sscanf(buf, "%hi%c", &addr, &end); 12208c2ecf20Sopenharmony_ci if (res < 1) { 12218c2ecf20Sopenharmony_ci dev_err(dev, "%s: Can't parse I2C address\n", "delete_device"); 12228c2ecf20Sopenharmony_ci return -EINVAL; 12238c2ecf20Sopenharmony_ci } 12248c2ecf20Sopenharmony_ci if (res > 1 && end != '\n') { 12258c2ecf20Sopenharmony_ci dev_err(dev, "%s: Extra parameters\n", "delete_device"); 12268c2ecf20Sopenharmony_ci return -EINVAL; 12278c2ecf20Sopenharmony_ci } 12288c2ecf20Sopenharmony_ci 12298c2ecf20Sopenharmony_ci /* Make sure the device was added through sysfs */ 12308c2ecf20Sopenharmony_ci res = -ENOENT; 12318c2ecf20Sopenharmony_ci mutex_lock_nested(&adap->userspace_clients_lock, 12328c2ecf20Sopenharmony_ci i2c_adapter_depth(adap)); 12338c2ecf20Sopenharmony_ci list_for_each_entry_safe(client, next, &adap->userspace_clients, 12348c2ecf20Sopenharmony_ci detected) { 12358c2ecf20Sopenharmony_ci if (i2c_encode_flags_to_addr(client) == addr) { 12368c2ecf20Sopenharmony_ci dev_info(dev, "%s: Deleting device %s at 0x%02hx\n", 12378c2ecf20Sopenharmony_ci "delete_device", client->name, client->addr); 12388c2ecf20Sopenharmony_ci 12398c2ecf20Sopenharmony_ci list_del(&client->detected); 12408c2ecf20Sopenharmony_ci i2c_unregister_device(client); 12418c2ecf20Sopenharmony_ci res = count; 12428c2ecf20Sopenharmony_ci break; 12438c2ecf20Sopenharmony_ci } 12448c2ecf20Sopenharmony_ci } 12458c2ecf20Sopenharmony_ci mutex_unlock(&adap->userspace_clients_lock); 12468c2ecf20Sopenharmony_ci 12478c2ecf20Sopenharmony_ci if (res < 0) 12488c2ecf20Sopenharmony_ci dev_err(dev, "%s: Can't find device in list\n", 12498c2ecf20Sopenharmony_ci "delete_device"); 12508c2ecf20Sopenharmony_ci return res; 12518c2ecf20Sopenharmony_ci} 12528c2ecf20Sopenharmony_cistatic DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL, 12538c2ecf20Sopenharmony_ci delete_device_store); 12548c2ecf20Sopenharmony_ci 12558c2ecf20Sopenharmony_cistatic struct attribute *i2c_adapter_attrs[] = { 12568c2ecf20Sopenharmony_ci &dev_attr_name.attr, 12578c2ecf20Sopenharmony_ci &dev_attr_new_device.attr, 12588c2ecf20Sopenharmony_ci &dev_attr_delete_device.attr, 12598c2ecf20Sopenharmony_ci NULL 12608c2ecf20Sopenharmony_ci}; 12618c2ecf20Sopenharmony_ciATTRIBUTE_GROUPS(i2c_adapter); 12628c2ecf20Sopenharmony_ci 12638c2ecf20Sopenharmony_cistruct device_type i2c_adapter_type = { 12648c2ecf20Sopenharmony_ci .groups = i2c_adapter_groups, 12658c2ecf20Sopenharmony_ci .release = i2c_adapter_dev_release, 12668c2ecf20Sopenharmony_ci}; 12678c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_adapter_type); 12688c2ecf20Sopenharmony_ci 12698c2ecf20Sopenharmony_ci/** 12708c2ecf20Sopenharmony_ci * i2c_verify_adapter - return parameter as i2c_adapter or NULL 12718c2ecf20Sopenharmony_ci * @dev: device, probably from some driver model iterator 12728c2ecf20Sopenharmony_ci * 12738c2ecf20Sopenharmony_ci * When traversing the driver model tree, perhaps using driver model 12748c2ecf20Sopenharmony_ci * iterators like @device_for_each_child(), you can't assume very much 12758c2ecf20Sopenharmony_ci * about the nodes you find. Use this function to avoid oopses caused 12768c2ecf20Sopenharmony_ci * by wrongly treating some non-I2C device as an i2c_adapter. 12778c2ecf20Sopenharmony_ci */ 12788c2ecf20Sopenharmony_cistruct i2c_adapter *i2c_verify_adapter(struct device *dev) 12798c2ecf20Sopenharmony_ci{ 12808c2ecf20Sopenharmony_ci return (dev->type == &i2c_adapter_type) 12818c2ecf20Sopenharmony_ci ? to_i2c_adapter(dev) 12828c2ecf20Sopenharmony_ci : NULL; 12838c2ecf20Sopenharmony_ci} 12848c2ecf20Sopenharmony_ciEXPORT_SYMBOL(i2c_verify_adapter); 12858c2ecf20Sopenharmony_ci 12868c2ecf20Sopenharmony_ci#ifdef CONFIG_I2C_COMPAT 12878c2ecf20Sopenharmony_cistatic struct class_compat *i2c_adapter_compat_class; 12888c2ecf20Sopenharmony_ci#endif 12898c2ecf20Sopenharmony_ci 12908c2ecf20Sopenharmony_cistatic void i2c_scan_static_board_info(struct i2c_adapter *adapter) 12918c2ecf20Sopenharmony_ci{ 12928c2ecf20Sopenharmony_ci struct i2c_devinfo *devinfo; 12938c2ecf20Sopenharmony_ci 12948c2ecf20Sopenharmony_ci down_read(&__i2c_board_lock); 12958c2ecf20Sopenharmony_ci list_for_each_entry(devinfo, &__i2c_board_list, list) { 12968c2ecf20Sopenharmony_ci if (devinfo->busnum == adapter->nr && 12978c2ecf20Sopenharmony_ci IS_ERR(i2c_new_client_device(adapter, &devinfo->board_info))) 12988c2ecf20Sopenharmony_ci dev_err(&adapter->dev, 12998c2ecf20Sopenharmony_ci "Can't create device at 0x%02x\n", 13008c2ecf20Sopenharmony_ci devinfo->board_info.addr); 13018c2ecf20Sopenharmony_ci } 13028c2ecf20Sopenharmony_ci up_read(&__i2c_board_lock); 13038c2ecf20Sopenharmony_ci} 13048c2ecf20Sopenharmony_ci 13058c2ecf20Sopenharmony_cistatic int i2c_do_add_adapter(struct i2c_driver *driver, 13068c2ecf20Sopenharmony_ci struct i2c_adapter *adap) 13078c2ecf20Sopenharmony_ci{ 13088c2ecf20Sopenharmony_ci /* Detect supported devices on that bus, and instantiate them */ 13098c2ecf20Sopenharmony_ci i2c_detect(adap, driver); 13108c2ecf20Sopenharmony_ci 13118c2ecf20Sopenharmony_ci return 0; 13128c2ecf20Sopenharmony_ci} 13138c2ecf20Sopenharmony_ci 13148c2ecf20Sopenharmony_cistatic int __process_new_adapter(struct device_driver *d, void *data) 13158c2ecf20Sopenharmony_ci{ 13168c2ecf20Sopenharmony_ci return i2c_do_add_adapter(to_i2c_driver(d), data); 13178c2ecf20Sopenharmony_ci} 13188c2ecf20Sopenharmony_ci 13198c2ecf20Sopenharmony_cistatic const struct i2c_lock_operations i2c_adapter_lock_ops = { 13208c2ecf20Sopenharmony_ci .lock_bus = i2c_adapter_lock_bus, 13218c2ecf20Sopenharmony_ci .trylock_bus = i2c_adapter_trylock_bus, 13228c2ecf20Sopenharmony_ci .unlock_bus = i2c_adapter_unlock_bus, 13238c2ecf20Sopenharmony_ci}; 13248c2ecf20Sopenharmony_ci 13258c2ecf20Sopenharmony_cistatic void i2c_host_notify_irq_teardown(struct i2c_adapter *adap) 13268c2ecf20Sopenharmony_ci{ 13278c2ecf20Sopenharmony_ci struct irq_domain *domain = adap->host_notify_domain; 13288c2ecf20Sopenharmony_ci irq_hw_number_t hwirq; 13298c2ecf20Sopenharmony_ci 13308c2ecf20Sopenharmony_ci if (!domain) 13318c2ecf20Sopenharmony_ci return; 13328c2ecf20Sopenharmony_ci 13338c2ecf20Sopenharmony_ci for (hwirq = 0 ; hwirq < I2C_ADDR_7BITS_COUNT ; hwirq++) 13348c2ecf20Sopenharmony_ci irq_dispose_mapping(irq_find_mapping(domain, hwirq)); 13358c2ecf20Sopenharmony_ci 13368c2ecf20Sopenharmony_ci irq_domain_remove(domain); 13378c2ecf20Sopenharmony_ci adap->host_notify_domain = NULL; 13388c2ecf20Sopenharmony_ci} 13398c2ecf20Sopenharmony_ci 13408c2ecf20Sopenharmony_cistatic int i2c_host_notify_irq_map(struct irq_domain *h, 13418c2ecf20Sopenharmony_ci unsigned int virq, 13428c2ecf20Sopenharmony_ci irq_hw_number_t hw_irq_num) 13438c2ecf20Sopenharmony_ci{ 13448c2ecf20Sopenharmony_ci irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_simple_irq); 13458c2ecf20Sopenharmony_ci 13468c2ecf20Sopenharmony_ci return 0; 13478c2ecf20Sopenharmony_ci} 13488c2ecf20Sopenharmony_ci 13498c2ecf20Sopenharmony_cistatic const struct irq_domain_ops i2c_host_notify_irq_ops = { 13508c2ecf20Sopenharmony_ci .map = i2c_host_notify_irq_map, 13518c2ecf20Sopenharmony_ci}; 13528c2ecf20Sopenharmony_ci 13538c2ecf20Sopenharmony_cistatic int i2c_setup_host_notify_irq_domain(struct i2c_adapter *adap) 13548c2ecf20Sopenharmony_ci{ 13558c2ecf20Sopenharmony_ci struct irq_domain *domain; 13568c2ecf20Sopenharmony_ci 13578c2ecf20Sopenharmony_ci if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_HOST_NOTIFY)) 13588c2ecf20Sopenharmony_ci return 0; 13598c2ecf20Sopenharmony_ci 13608c2ecf20Sopenharmony_ci domain = irq_domain_create_linear(adap->dev.parent->fwnode, 13618c2ecf20Sopenharmony_ci I2C_ADDR_7BITS_COUNT, 13628c2ecf20Sopenharmony_ci &i2c_host_notify_irq_ops, adap); 13638c2ecf20Sopenharmony_ci if (!domain) 13648c2ecf20Sopenharmony_ci return -ENOMEM; 13658c2ecf20Sopenharmony_ci 13668c2ecf20Sopenharmony_ci adap->host_notify_domain = domain; 13678c2ecf20Sopenharmony_ci 13688c2ecf20Sopenharmony_ci return 0; 13698c2ecf20Sopenharmony_ci} 13708c2ecf20Sopenharmony_ci 13718c2ecf20Sopenharmony_ci/** 13728c2ecf20Sopenharmony_ci * i2c_handle_smbus_host_notify - Forward a Host Notify event to the correct 13738c2ecf20Sopenharmony_ci * I2C client. 13748c2ecf20Sopenharmony_ci * @adap: the adapter 13758c2ecf20Sopenharmony_ci * @addr: the I2C address of the notifying device 13768c2ecf20Sopenharmony_ci * Context: can't sleep 13778c2ecf20Sopenharmony_ci * 13788c2ecf20Sopenharmony_ci * Helper function to be called from an I2C bus driver's interrupt 13798c2ecf20Sopenharmony_ci * handler. It will schedule the Host Notify IRQ. 13808c2ecf20Sopenharmony_ci */ 13818c2ecf20Sopenharmony_ciint i2c_handle_smbus_host_notify(struct i2c_adapter *adap, unsigned short addr) 13828c2ecf20Sopenharmony_ci{ 13838c2ecf20Sopenharmony_ci int irq; 13848c2ecf20Sopenharmony_ci 13858c2ecf20Sopenharmony_ci if (!adap) 13868c2ecf20Sopenharmony_ci return -EINVAL; 13878c2ecf20Sopenharmony_ci 13888c2ecf20Sopenharmony_ci irq = irq_find_mapping(adap->host_notify_domain, addr); 13898c2ecf20Sopenharmony_ci if (irq <= 0) 13908c2ecf20Sopenharmony_ci return -ENXIO; 13918c2ecf20Sopenharmony_ci 13928c2ecf20Sopenharmony_ci generic_handle_irq(irq); 13938c2ecf20Sopenharmony_ci 13948c2ecf20Sopenharmony_ci return 0; 13958c2ecf20Sopenharmony_ci} 13968c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_handle_smbus_host_notify); 13978c2ecf20Sopenharmony_ci 13988c2ecf20Sopenharmony_cistatic int i2c_register_adapter(struct i2c_adapter *adap) 13998c2ecf20Sopenharmony_ci{ 14008c2ecf20Sopenharmony_ci int res = -EINVAL; 14018c2ecf20Sopenharmony_ci 14028c2ecf20Sopenharmony_ci /* Can't register until after driver model init */ 14038c2ecf20Sopenharmony_ci if (WARN_ON(!is_registered)) { 14048c2ecf20Sopenharmony_ci res = -EAGAIN; 14058c2ecf20Sopenharmony_ci goto out_list; 14068c2ecf20Sopenharmony_ci } 14078c2ecf20Sopenharmony_ci 14088c2ecf20Sopenharmony_ci /* Sanity checks */ 14098c2ecf20Sopenharmony_ci if (WARN(!adap->name[0], "i2c adapter has no name")) 14108c2ecf20Sopenharmony_ci goto out_list; 14118c2ecf20Sopenharmony_ci 14128c2ecf20Sopenharmony_ci if (!adap->algo) { 14138c2ecf20Sopenharmony_ci pr_err("adapter '%s': no algo supplied!\n", adap->name); 14148c2ecf20Sopenharmony_ci goto out_list; 14158c2ecf20Sopenharmony_ci } 14168c2ecf20Sopenharmony_ci 14178c2ecf20Sopenharmony_ci if (!adap->lock_ops) 14188c2ecf20Sopenharmony_ci adap->lock_ops = &i2c_adapter_lock_ops; 14198c2ecf20Sopenharmony_ci 14208c2ecf20Sopenharmony_ci adap->locked_flags = 0; 14218c2ecf20Sopenharmony_ci rt_mutex_init(&adap->bus_lock); 14228c2ecf20Sopenharmony_ci rt_mutex_init(&adap->mux_lock); 14238c2ecf20Sopenharmony_ci mutex_init(&adap->userspace_clients_lock); 14248c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&adap->userspace_clients); 14258c2ecf20Sopenharmony_ci 14268c2ecf20Sopenharmony_ci /* Set default timeout to 1 second if not already set */ 14278c2ecf20Sopenharmony_ci if (adap->timeout == 0) 14288c2ecf20Sopenharmony_ci adap->timeout = HZ; 14298c2ecf20Sopenharmony_ci 14308c2ecf20Sopenharmony_ci /* register soft irqs for Host Notify */ 14318c2ecf20Sopenharmony_ci res = i2c_setup_host_notify_irq_domain(adap); 14328c2ecf20Sopenharmony_ci if (res) { 14338c2ecf20Sopenharmony_ci pr_err("adapter '%s': can't create Host Notify IRQs (%d)\n", 14348c2ecf20Sopenharmony_ci adap->name, res); 14358c2ecf20Sopenharmony_ci goto out_list; 14368c2ecf20Sopenharmony_ci } 14378c2ecf20Sopenharmony_ci 14388c2ecf20Sopenharmony_ci dev_set_name(&adap->dev, "i2c-%d", adap->nr); 14398c2ecf20Sopenharmony_ci adap->dev.bus = &i2c_bus_type; 14408c2ecf20Sopenharmony_ci adap->dev.type = &i2c_adapter_type; 14418c2ecf20Sopenharmony_ci res = device_register(&adap->dev); 14428c2ecf20Sopenharmony_ci if (res) { 14438c2ecf20Sopenharmony_ci pr_err("adapter '%s': can't register device (%d)\n", adap->name, res); 14448c2ecf20Sopenharmony_ci goto out_list; 14458c2ecf20Sopenharmony_ci } 14468c2ecf20Sopenharmony_ci 14478c2ecf20Sopenharmony_ci res = of_i2c_setup_smbus_alert(adap); 14488c2ecf20Sopenharmony_ci if (res) 14498c2ecf20Sopenharmony_ci goto out_reg; 14508c2ecf20Sopenharmony_ci 14518c2ecf20Sopenharmony_ci pm_runtime_no_callbacks(&adap->dev); 14528c2ecf20Sopenharmony_ci pm_suspend_ignore_children(&adap->dev, true); 14538c2ecf20Sopenharmony_ci pm_runtime_enable(&adap->dev); 14548c2ecf20Sopenharmony_ci 14558c2ecf20Sopenharmony_ci res = i2c_init_recovery(adap); 14568c2ecf20Sopenharmony_ci if (res == -EPROBE_DEFER) 14578c2ecf20Sopenharmony_ci goto out_reg; 14588c2ecf20Sopenharmony_ci 14598c2ecf20Sopenharmony_ci dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name); 14608c2ecf20Sopenharmony_ci 14618c2ecf20Sopenharmony_ci#ifdef CONFIG_I2C_COMPAT 14628c2ecf20Sopenharmony_ci res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev, 14638c2ecf20Sopenharmony_ci adap->dev.parent); 14648c2ecf20Sopenharmony_ci if (res) 14658c2ecf20Sopenharmony_ci dev_warn(&adap->dev, 14668c2ecf20Sopenharmony_ci "Failed to create compatibility class link\n"); 14678c2ecf20Sopenharmony_ci#endif 14688c2ecf20Sopenharmony_ci 14698c2ecf20Sopenharmony_ci /* create pre-declared device nodes */ 14708c2ecf20Sopenharmony_ci of_i2c_register_devices(adap); 14718c2ecf20Sopenharmony_ci i2c_acpi_install_space_handler(adap); 14728c2ecf20Sopenharmony_ci i2c_acpi_register_devices(adap); 14738c2ecf20Sopenharmony_ci 14748c2ecf20Sopenharmony_ci if (adap->nr < __i2c_first_dynamic_bus_num) 14758c2ecf20Sopenharmony_ci i2c_scan_static_board_info(adap); 14768c2ecf20Sopenharmony_ci 14778c2ecf20Sopenharmony_ci /* Notify drivers */ 14788c2ecf20Sopenharmony_ci mutex_lock(&core_lock); 14798c2ecf20Sopenharmony_ci bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter); 14808c2ecf20Sopenharmony_ci mutex_unlock(&core_lock); 14818c2ecf20Sopenharmony_ci 14828c2ecf20Sopenharmony_ci return 0; 14838c2ecf20Sopenharmony_ci 14848c2ecf20Sopenharmony_ciout_reg: 14858c2ecf20Sopenharmony_ci init_completion(&adap->dev_released); 14868c2ecf20Sopenharmony_ci device_unregister(&adap->dev); 14878c2ecf20Sopenharmony_ci wait_for_completion(&adap->dev_released); 14888c2ecf20Sopenharmony_ciout_list: 14898c2ecf20Sopenharmony_ci mutex_lock(&core_lock); 14908c2ecf20Sopenharmony_ci idr_remove(&i2c_adapter_idr, adap->nr); 14918c2ecf20Sopenharmony_ci mutex_unlock(&core_lock); 14928c2ecf20Sopenharmony_ci return res; 14938c2ecf20Sopenharmony_ci} 14948c2ecf20Sopenharmony_ci 14958c2ecf20Sopenharmony_ci/** 14968c2ecf20Sopenharmony_ci * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1 14978c2ecf20Sopenharmony_ci * @adap: the adapter to register (with adap->nr initialized) 14988c2ecf20Sopenharmony_ci * Context: can sleep 14998c2ecf20Sopenharmony_ci * 15008c2ecf20Sopenharmony_ci * See i2c_add_numbered_adapter() for details. 15018c2ecf20Sopenharmony_ci */ 15028c2ecf20Sopenharmony_cistatic int __i2c_add_numbered_adapter(struct i2c_adapter *adap) 15038c2ecf20Sopenharmony_ci{ 15048c2ecf20Sopenharmony_ci int id; 15058c2ecf20Sopenharmony_ci 15068c2ecf20Sopenharmony_ci mutex_lock(&core_lock); 15078c2ecf20Sopenharmony_ci id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1, GFP_KERNEL); 15088c2ecf20Sopenharmony_ci mutex_unlock(&core_lock); 15098c2ecf20Sopenharmony_ci if (WARN(id < 0, "couldn't get idr")) 15108c2ecf20Sopenharmony_ci return id == -ENOSPC ? -EBUSY : id; 15118c2ecf20Sopenharmony_ci 15128c2ecf20Sopenharmony_ci return i2c_register_adapter(adap); 15138c2ecf20Sopenharmony_ci} 15148c2ecf20Sopenharmony_ci 15158c2ecf20Sopenharmony_ci/** 15168c2ecf20Sopenharmony_ci * i2c_add_adapter - declare i2c adapter, use dynamic bus number 15178c2ecf20Sopenharmony_ci * @adapter: the adapter to add 15188c2ecf20Sopenharmony_ci * Context: can sleep 15198c2ecf20Sopenharmony_ci * 15208c2ecf20Sopenharmony_ci * This routine is used to declare an I2C adapter when its bus number 15218c2ecf20Sopenharmony_ci * doesn't matter or when its bus number is specified by an dt alias. 15228c2ecf20Sopenharmony_ci * Examples of bases when the bus number doesn't matter: I2C adapters 15238c2ecf20Sopenharmony_ci * dynamically added by USB links or PCI plugin cards. 15248c2ecf20Sopenharmony_ci * 15258c2ecf20Sopenharmony_ci * When this returns zero, a new bus number was allocated and stored 15268c2ecf20Sopenharmony_ci * in adap->nr, and the specified adapter became available for clients. 15278c2ecf20Sopenharmony_ci * Otherwise, a negative errno value is returned. 15288c2ecf20Sopenharmony_ci */ 15298c2ecf20Sopenharmony_ciint i2c_add_adapter(struct i2c_adapter *adapter) 15308c2ecf20Sopenharmony_ci{ 15318c2ecf20Sopenharmony_ci struct device *dev = &adapter->dev; 15328c2ecf20Sopenharmony_ci int id; 15338c2ecf20Sopenharmony_ci 15348c2ecf20Sopenharmony_ci if (dev->of_node) { 15358c2ecf20Sopenharmony_ci id = of_alias_get_id(dev->of_node, "i2c"); 15368c2ecf20Sopenharmony_ci if (id >= 0) { 15378c2ecf20Sopenharmony_ci adapter->nr = id; 15388c2ecf20Sopenharmony_ci return __i2c_add_numbered_adapter(adapter); 15398c2ecf20Sopenharmony_ci } 15408c2ecf20Sopenharmony_ci } 15418c2ecf20Sopenharmony_ci 15428c2ecf20Sopenharmony_ci mutex_lock(&core_lock); 15438c2ecf20Sopenharmony_ci id = idr_alloc(&i2c_adapter_idr, adapter, 15448c2ecf20Sopenharmony_ci __i2c_first_dynamic_bus_num, 0, GFP_KERNEL); 15458c2ecf20Sopenharmony_ci mutex_unlock(&core_lock); 15468c2ecf20Sopenharmony_ci if (WARN(id < 0, "couldn't get idr")) 15478c2ecf20Sopenharmony_ci return id; 15488c2ecf20Sopenharmony_ci 15498c2ecf20Sopenharmony_ci adapter->nr = id; 15508c2ecf20Sopenharmony_ci 15518c2ecf20Sopenharmony_ci return i2c_register_adapter(adapter); 15528c2ecf20Sopenharmony_ci} 15538c2ecf20Sopenharmony_ciEXPORT_SYMBOL(i2c_add_adapter); 15548c2ecf20Sopenharmony_ci 15558c2ecf20Sopenharmony_ci/** 15568c2ecf20Sopenharmony_ci * i2c_add_numbered_adapter - declare i2c adapter, use static bus number 15578c2ecf20Sopenharmony_ci * @adap: the adapter to register (with adap->nr initialized) 15588c2ecf20Sopenharmony_ci * Context: can sleep 15598c2ecf20Sopenharmony_ci * 15608c2ecf20Sopenharmony_ci * This routine is used to declare an I2C adapter when its bus number 15618c2ecf20Sopenharmony_ci * matters. For example, use it for I2C adapters from system-on-chip CPUs, 15628c2ecf20Sopenharmony_ci * or otherwise built in to the system's mainboard, and where i2c_board_info 15638c2ecf20Sopenharmony_ci * is used to properly configure I2C devices. 15648c2ecf20Sopenharmony_ci * 15658c2ecf20Sopenharmony_ci * If the requested bus number is set to -1, then this function will behave 15668c2ecf20Sopenharmony_ci * identically to i2c_add_adapter, and will dynamically assign a bus number. 15678c2ecf20Sopenharmony_ci * 15688c2ecf20Sopenharmony_ci * If no devices have pre-been declared for this bus, then be sure to 15698c2ecf20Sopenharmony_ci * register the adapter before any dynamically allocated ones. Otherwise 15708c2ecf20Sopenharmony_ci * the required bus ID may not be available. 15718c2ecf20Sopenharmony_ci * 15728c2ecf20Sopenharmony_ci * When this returns zero, the specified adapter became available for 15738c2ecf20Sopenharmony_ci * clients using the bus number provided in adap->nr. Also, the table 15748c2ecf20Sopenharmony_ci * of I2C devices pre-declared using i2c_register_board_info() is scanned, 15758c2ecf20Sopenharmony_ci * and the appropriate driver model device nodes are created. Otherwise, a 15768c2ecf20Sopenharmony_ci * negative errno value is returned. 15778c2ecf20Sopenharmony_ci */ 15788c2ecf20Sopenharmony_ciint i2c_add_numbered_adapter(struct i2c_adapter *adap) 15798c2ecf20Sopenharmony_ci{ 15808c2ecf20Sopenharmony_ci if (adap->nr == -1) /* -1 means dynamically assign bus id */ 15818c2ecf20Sopenharmony_ci return i2c_add_adapter(adap); 15828c2ecf20Sopenharmony_ci 15838c2ecf20Sopenharmony_ci return __i2c_add_numbered_adapter(adap); 15848c2ecf20Sopenharmony_ci} 15858c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_add_numbered_adapter); 15868c2ecf20Sopenharmony_ci 15878c2ecf20Sopenharmony_cistatic void i2c_do_del_adapter(struct i2c_driver *driver, 15888c2ecf20Sopenharmony_ci struct i2c_adapter *adapter) 15898c2ecf20Sopenharmony_ci{ 15908c2ecf20Sopenharmony_ci struct i2c_client *client, *_n; 15918c2ecf20Sopenharmony_ci 15928c2ecf20Sopenharmony_ci /* Remove the devices we created ourselves as the result of hardware 15938c2ecf20Sopenharmony_ci * probing (using a driver's detect method) */ 15948c2ecf20Sopenharmony_ci list_for_each_entry_safe(client, _n, &driver->clients, detected) { 15958c2ecf20Sopenharmony_ci if (client->adapter == adapter) { 15968c2ecf20Sopenharmony_ci dev_dbg(&adapter->dev, "Removing %s at 0x%x\n", 15978c2ecf20Sopenharmony_ci client->name, client->addr); 15988c2ecf20Sopenharmony_ci list_del(&client->detected); 15998c2ecf20Sopenharmony_ci i2c_unregister_device(client); 16008c2ecf20Sopenharmony_ci } 16018c2ecf20Sopenharmony_ci } 16028c2ecf20Sopenharmony_ci} 16038c2ecf20Sopenharmony_ci 16048c2ecf20Sopenharmony_cistatic int __unregister_client(struct device *dev, void *dummy) 16058c2ecf20Sopenharmony_ci{ 16068c2ecf20Sopenharmony_ci struct i2c_client *client = i2c_verify_client(dev); 16078c2ecf20Sopenharmony_ci if (client && strcmp(client->name, "dummy")) 16088c2ecf20Sopenharmony_ci i2c_unregister_device(client); 16098c2ecf20Sopenharmony_ci return 0; 16108c2ecf20Sopenharmony_ci} 16118c2ecf20Sopenharmony_ci 16128c2ecf20Sopenharmony_cistatic int __unregister_dummy(struct device *dev, void *dummy) 16138c2ecf20Sopenharmony_ci{ 16148c2ecf20Sopenharmony_ci struct i2c_client *client = i2c_verify_client(dev); 16158c2ecf20Sopenharmony_ci i2c_unregister_device(client); 16168c2ecf20Sopenharmony_ci return 0; 16178c2ecf20Sopenharmony_ci} 16188c2ecf20Sopenharmony_ci 16198c2ecf20Sopenharmony_cistatic int __process_removed_adapter(struct device_driver *d, void *data) 16208c2ecf20Sopenharmony_ci{ 16218c2ecf20Sopenharmony_ci i2c_do_del_adapter(to_i2c_driver(d), data); 16228c2ecf20Sopenharmony_ci return 0; 16238c2ecf20Sopenharmony_ci} 16248c2ecf20Sopenharmony_ci 16258c2ecf20Sopenharmony_ci/** 16268c2ecf20Sopenharmony_ci * i2c_del_adapter - unregister I2C adapter 16278c2ecf20Sopenharmony_ci * @adap: the adapter being unregistered 16288c2ecf20Sopenharmony_ci * Context: can sleep 16298c2ecf20Sopenharmony_ci * 16308c2ecf20Sopenharmony_ci * This unregisters an I2C adapter which was previously registered 16318c2ecf20Sopenharmony_ci * by @i2c_add_adapter or @i2c_add_numbered_adapter. 16328c2ecf20Sopenharmony_ci */ 16338c2ecf20Sopenharmony_civoid i2c_del_adapter(struct i2c_adapter *adap) 16348c2ecf20Sopenharmony_ci{ 16358c2ecf20Sopenharmony_ci struct i2c_adapter *found; 16368c2ecf20Sopenharmony_ci struct i2c_client *client, *next; 16378c2ecf20Sopenharmony_ci 16388c2ecf20Sopenharmony_ci /* First make sure that this adapter was ever added */ 16398c2ecf20Sopenharmony_ci mutex_lock(&core_lock); 16408c2ecf20Sopenharmony_ci found = idr_find(&i2c_adapter_idr, adap->nr); 16418c2ecf20Sopenharmony_ci mutex_unlock(&core_lock); 16428c2ecf20Sopenharmony_ci if (found != adap) { 16438c2ecf20Sopenharmony_ci pr_debug("attempting to delete unregistered adapter [%s]\n", adap->name); 16448c2ecf20Sopenharmony_ci return; 16458c2ecf20Sopenharmony_ci } 16468c2ecf20Sopenharmony_ci 16478c2ecf20Sopenharmony_ci i2c_acpi_remove_space_handler(adap); 16488c2ecf20Sopenharmony_ci /* Tell drivers about this removal */ 16498c2ecf20Sopenharmony_ci mutex_lock(&core_lock); 16508c2ecf20Sopenharmony_ci bus_for_each_drv(&i2c_bus_type, NULL, adap, 16518c2ecf20Sopenharmony_ci __process_removed_adapter); 16528c2ecf20Sopenharmony_ci mutex_unlock(&core_lock); 16538c2ecf20Sopenharmony_ci 16548c2ecf20Sopenharmony_ci /* Remove devices instantiated from sysfs */ 16558c2ecf20Sopenharmony_ci mutex_lock_nested(&adap->userspace_clients_lock, 16568c2ecf20Sopenharmony_ci i2c_adapter_depth(adap)); 16578c2ecf20Sopenharmony_ci list_for_each_entry_safe(client, next, &adap->userspace_clients, 16588c2ecf20Sopenharmony_ci detected) { 16598c2ecf20Sopenharmony_ci dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name, 16608c2ecf20Sopenharmony_ci client->addr); 16618c2ecf20Sopenharmony_ci list_del(&client->detected); 16628c2ecf20Sopenharmony_ci i2c_unregister_device(client); 16638c2ecf20Sopenharmony_ci } 16648c2ecf20Sopenharmony_ci mutex_unlock(&adap->userspace_clients_lock); 16658c2ecf20Sopenharmony_ci 16668c2ecf20Sopenharmony_ci /* Detach any active clients. This can't fail, thus we do not 16678c2ecf20Sopenharmony_ci * check the returned value. This is a two-pass process, because 16688c2ecf20Sopenharmony_ci * we can't remove the dummy devices during the first pass: they 16698c2ecf20Sopenharmony_ci * could have been instantiated by real devices wishing to clean 16708c2ecf20Sopenharmony_ci * them up properly, so we give them a chance to do that first. */ 16718c2ecf20Sopenharmony_ci device_for_each_child(&adap->dev, NULL, __unregister_client); 16728c2ecf20Sopenharmony_ci device_for_each_child(&adap->dev, NULL, __unregister_dummy); 16738c2ecf20Sopenharmony_ci 16748c2ecf20Sopenharmony_ci#ifdef CONFIG_I2C_COMPAT 16758c2ecf20Sopenharmony_ci class_compat_remove_link(i2c_adapter_compat_class, &adap->dev, 16768c2ecf20Sopenharmony_ci adap->dev.parent); 16778c2ecf20Sopenharmony_ci#endif 16788c2ecf20Sopenharmony_ci 16798c2ecf20Sopenharmony_ci /* device name is gone after device_unregister */ 16808c2ecf20Sopenharmony_ci dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name); 16818c2ecf20Sopenharmony_ci 16828c2ecf20Sopenharmony_ci pm_runtime_disable(&adap->dev); 16838c2ecf20Sopenharmony_ci 16848c2ecf20Sopenharmony_ci i2c_host_notify_irq_teardown(adap); 16858c2ecf20Sopenharmony_ci 16868c2ecf20Sopenharmony_ci /* wait until all references to the device are gone 16878c2ecf20Sopenharmony_ci * 16888c2ecf20Sopenharmony_ci * FIXME: This is old code and should ideally be replaced by an 16898c2ecf20Sopenharmony_ci * alternative which results in decoupling the lifetime of the struct 16908c2ecf20Sopenharmony_ci * device from the i2c_adapter, like spi or netdev do. Any solution 16918c2ecf20Sopenharmony_ci * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled! 16928c2ecf20Sopenharmony_ci */ 16938c2ecf20Sopenharmony_ci init_completion(&adap->dev_released); 16948c2ecf20Sopenharmony_ci device_unregister(&adap->dev); 16958c2ecf20Sopenharmony_ci wait_for_completion(&adap->dev_released); 16968c2ecf20Sopenharmony_ci 16978c2ecf20Sopenharmony_ci /* free bus id */ 16988c2ecf20Sopenharmony_ci mutex_lock(&core_lock); 16998c2ecf20Sopenharmony_ci idr_remove(&i2c_adapter_idr, adap->nr); 17008c2ecf20Sopenharmony_ci mutex_unlock(&core_lock); 17018c2ecf20Sopenharmony_ci 17028c2ecf20Sopenharmony_ci /* Clear the device structure in case this adapter is ever going to be 17038c2ecf20Sopenharmony_ci added again */ 17048c2ecf20Sopenharmony_ci memset(&adap->dev, 0, sizeof(adap->dev)); 17058c2ecf20Sopenharmony_ci} 17068c2ecf20Sopenharmony_ciEXPORT_SYMBOL(i2c_del_adapter); 17078c2ecf20Sopenharmony_ci 17088c2ecf20Sopenharmony_cistatic void i2c_parse_timing(struct device *dev, char *prop_name, u32 *cur_val_p, 17098c2ecf20Sopenharmony_ci u32 def_val, bool use_def) 17108c2ecf20Sopenharmony_ci{ 17118c2ecf20Sopenharmony_ci int ret; 17128c2ecf20Sopenharmony_ci 17138c2ecf20Sopenharmony_ci ret = device_property_read_u32(dev, prop_name, cur_val_p); 17148c2ecf20Sopenharmony_ci if (ret && use_def) 17158c2ecf20Sopenharmony_ci *cur_val_p = def_val; 17168c2ecf20Sopenharmony_ci 17178c2ecf20Sopenharmony_ci dev_dbg(dev, "%s: %u\n", prop_name, *cur_val_p); 17188c2ecf20Sopenharmony_ci} 17198c2ecf20Sopenharmony_ci 17208c2ecf20Sopenharmony_ci/** 17218c2ecf20Sopenharmony_ci * i2c_parse_fw_timings - get I2C related timing parameters from firmware 17228c2ecf20Sopenharmony_ci * @dev: The device to scan for I2C timing properties 17238c2ecf20Sopenharmony_ci * @t: the i2c_timings struct to be filled with values 17248c2ecf20Sopenharmony_ci * @use_defaults: bool to use sane defaults derived from the I2C specification 17258c2ecf20Sopenharmony_ci * when properties are not found, otherwise don't update 17268c2ecf20Sopenharmony_ci * 17278c2ecf20Sopenharmony_ci * Scan the device for the generic I2C properties describing timing parameters 17288c2ecf20Sopenharmony_ci * for the signal and fill the given struct with the results. If a property was 17298c2ecf20Sopenharmony_ci * not found and use_defaults was true, then maximum timings are assumed which 17308c2ecf20Sopenharmony_ci * are derived from the I2C specification. If use_defaults is not used, the 17318c2ecf20Sopenharmony_ci * results will be as before, so drivers can apply their own defaults before 17328c2ecf20Sopenharmony_ci * calling this helper. The latter is mainly intended for avoiding regressions 17338c2ecf20Sopenharmony_ci * of existing drivers which want to switch to this function. New drivers 17348c2ecf20Sopenharmony_ci * almost always should use the defaults. 17358c2ecf20Sopenharmony_ci */ 17368c2ecf20Sopenharmony_civoid i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults) 17378c2ecf20Sopenharmony_ci{ 17388c2ecf20Sopenharmony_ci bool u = use_defaults; 17398c2ecf20Sopenharmony_ci u32 d; 17408c2ecf20Sopenharmony_ci 17418c2ecf20Sopenharmony_ci i2c_parse_timing(dev, "clock-frequency", &t->bus_freq_hz, 17428c2ecf20Sopenharmony_ci I2C_MAX_STANDARD_MODE_FREQ, u); 17438c2ecf20Sopenharmony_ci 17448c2ecf20Sopenharmony_ci d = t->bus_freq_hz <= I2C_MAX_STANDARD_MODE_FREQ ? 1000 : 17458c2ecf20Sopenharmony_ci t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ ? 300 : 120; 17468c2ecf20Sopenharmony_ci i2c_parse_timing(dev, "i2c-scl-rising-time-ns", &t->scl_rise_ns, d, u); 17478c2ecf20Sopenharmony_ci 17488c2ecf20Sopenharmony_ci d = t->bus_freq_hz <= I2C_MAX_FAST_MODE_FREQ ? 300 : 120; 17498c2ecf20Sopenharmony_ci i2c_parse_timing(dev, "i2c-scl-falling-time-ns", &t->scl_fall_ns, d, u); 17508c2ecf20Sopenharmony_ci 17518c2ecf20Sopenharmony_ci i2c_parse_timing(dev, "i2c-scl-internal-delay-ns", 17528c2ecf20Sopenharmony_ci &t->scl_int_delay_ns, 0, u); 17538c2ecf20Sopenharmony_ci i2c_parse_timing(dev, "i2c-sda-falling-time-ns", &t->sda_fall_ns, 17548c2ecf20Sopenharmony_ci t->scl_fall_ns, u); 17558c2ecf20Sopenharmony_ci i2c_parse_timing(dev, "i2c-sda-hold-time-ns", &t->sda_hold_ns, 0, u); 17568c2ecf20Sopenharmony_ci i2c_parse_timing(dev, "i2c-digital-filter-width-ns", 17578c2ecf20Sopenharmony_ci &t->digital_filter_width_ns, 0, u); 17588c2ecf20Sopenharmony_ci i2c_parse_timing(dev, "i2c-analog-filter-cutoff-frequency", 17598c2ecf20Sopenharmony_ci &t->analog_filter_cutoff_freq_hz, 0, u); 17608c2ecf20Sopenharmony_ci} 17618c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_parse_fw_timings); 17628c2ecf20Sopenharmony_ci 17638c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------------- */ 17648c2ecf20Sopenharmony_ci 17658c2ecf20Sopenharmony_ciint i2c_for_each_dev(void *data, int (*fn)(struct device *dev, void *data)) 17668c2ecf20Sopenharmony_ci{ 17678c2ecf20Sopenharmony_ci int res; 17688c2ecf20Sopenharmony_ci 17698c2ecf20Sopenharmony_ci mutex_lock(&core_lock); 17708c2ecf20Sopenharmony_ci res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn); 17718c2ecf20Sopenharmony_ci mutex_unlock(&core_lock); 17728c2ecf20Sopenharmony_ci 17738c2ecf20Sopenharmony_ci return res; 17748c2ecf20Sopenharmony_ci} 17758c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_for_each_dev); 17768c2ecf20Sopenharmony_ci 17778c2ecf20Sopenharmony_cistatic int __process_new_driver(struct device *dev, void *data) 17788c2ecf20Sopenharmony_ci{ 17798c2ecf20Sopenharmony_ci if (dev->type != &i2c_adapter_type) 17808c2ecf20Sopenharmony_ci return 0; 17818c2ecf20Sopenharmony_ci return i2c_do_add_adapter(data, to_i2c_adapter(dev)); 17828c2ecf20Sopenharmony_ci} 17838c2ecf20Sopenharmony_ci 17848c2ecf20Sopenharmony_ci/* 17858c2ecf20Sopenharmony_ci * An i2c_driver is used with one or more i2c_client (device) nodes to access 17868c2ecf20Sopenharmony_ci * i2c slave chips, on a bus instance associated with some i2c_adapter. 17878c2ecf20Sopenharmony_ci */ 17888c2ecf20Sopenharmony_ci 17898c2ecf20Sopenharmony_ciint i2c_register_driver(struct module *owner, struct i2c_driver *driver) 17908c2ecf20Sopenharmony_ci{ 17918c2ecf20Sopenharmony_ci int res; 17928c2ecf20Sopenharmony_ci 17938c2ecf20Sopenharmony_ci /* Can't register until after driver model init */ 17948c2ecf20Sopenharmony_ci if (WARN_ON(!is_registered)) 17958c2ecf20Sopenharmony_ci return -EAGAIN; 17968c2ecf20Sopenharmony_ci 17978c2ecf20Sopenharmony_ci /* add the driver to the list of i2c drivers in the driver core */ 17988c2ecf20Sopenharmony_ci driver->driver.owner = owner; 17998c2ecf20Sopenharmony_ci driver->driver.bus = &i2c_bus_type; 18008c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&driver->clients); 18018c2ecf20Sopenharmony_ci 18028c2ecf20Sopenharmony_ci /* When registration returns, the driver core 18038c2ecf20Sopenharmony_ci * will have called probe() for all matching-but-unbound devices. 18048c2ecf20Sopenharmony_ci */ 18058c2ecf20Sopenharmony_ci res = driver_register(&driver->driver); 18068c2ecf20Sopenharmony_ci if (res) 18078c2ecf20Sopenharmony_ci return res; 18088c2ecf20Sopenharmony_ci 18098c2ecf20Sopenharmony_ci pr_debug("driver [%s] registered\n", driver->driver.name); 18108c2ecf20Sopenharmony_ci 18118c2ecf20Sopenharmony_ci /* Walk the adapters that are already present */ 18128c2ecf20Sopenharmony_ci i2c_for_each_dev(driver, __process_new_driver); 18138c2ecf20Sopenharmony_ci 18148c2ecf20Sopenharmony_ci return 0; 18158c2ecf20Sopenharmony_ci} 18168c2ecf20Sopenharmony_ciEXPORT_SYMBOL(i2c_register_driver); 18178c2ecf20Sopenharmony_ci 18188c2ecf20Sopenharmony_cistatic int __process_removed_driver(struct device *dev, void *data) 18198c2ecf20Sopenharmony_ci{ 18208c2ecf20Sopenharmony_ci if (dev->type == &i2c_adapter_type) 18218c2ecf20Sopenharmony_ci i2c_do_del_adapter(data, to_i2c_adapter(dev)); 18228c2ecf20Sopenharmony_ci return 0; 18238c2ecf20Sopenharmony_ci} 18248c2ecf20Sopenharmony_ci 18258c2ecf20Sopenharmony_ci/** 18268c2ecf20Sopenharmony_ci * i2c_del_driver - unregister I2C driver 18278c2ecf20Sopenharmony_ci * @driver: the driver being unregistered 18288c2ecf20Sopenharmony_ci * Context: can sleep 18298c2ecf20Sopenharmony_ci */ 18308c2ecf20Sopenharmony_civoid i2c_del_driver(struct i2c_driver *driver) 18318c2ecf20Sopenharmony_ci{ 18328c2ecf20Sopenharmony_ci i2c_for_each_dev(driver, __process_removed_driver); 18338c2ecf20Sopenharmony_ci 18348c2ecf20Sopenharmony_ci driver_unregister(&driver->driver); 18358c2ecf20Sopenharmony_ci pr_debug("driver [%s] unregistered\n", driver->driver.name); 18368c2ecf20Sopenharmony_ci} 18378c2ecf20Sopenharmony_ciEXPORT_SYMBOL(i2c_del_driver); 18388c2ecf20Sopenharmony_ci 18398c2ecf20Sopenharmony_ci/* ------------------------------------------------------------------------- */ 18408c2ecf20Sopenharmony_ci 18418c2ecf20Sopenharmony_cistruct i2c_cmd_arg { 18428c2ecf20Sopenharmony_ci unsigned cmd; 18438c2ecf20Sopenharmony_ci void *arg; 18448c2ecf20Sopenharmony_ci}; 18458c2ecf20Sopenharmony_ci 18468c2ecf20Sopenharmony_cistatic int i2c_cmd(struct device *dev, void *_arg) 18478c2ecf20Sopenharmony_ci{ 18488c2ecf20Sopenharmony_ci struct i2c_client *client = i2c_verify_client(dev); 18498c2ecf20Sopenharmony_ci struct i2c_cmd_arg *arg = _arg; 18508c2ecf20Sopenharmony_ci struct i2c_driver *driver; 18518c2ecf20Sopenharmony_ci 18528c2ecf20Sopenharmony_ci if (!client || !client->dev.driver) 18538c2ecf20Sopenharmony_ci return 0; 18548c2ecf20Sopenharmony_ci 18558c2ecf20Sopenharmony_ci driver = to_i2c_driver(client->dev.driver); 18568c2ecf20Sopenharmony_ci if (driver->command) 18578c2ecf20Sopenharmony_ci driver->command(client, arg->cmd, arg->arg); 18588c2ecf20Sopenharmony_ci return 0; 18598c2ecf20Sopenharmony_ci} 18608c2ecf20Sopenharmony_ci 18618c2ecf20Sopenharmony_civoid i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg) 18628c2ecf20Sopenharmony_ci{ 18638c2ecf20Sopenharmony_ci struct i2c_cmd_arg cmd_arg; 18648c2ecf20Sopenharmony_ci 18658c2ecf20Sopenharmony_ci cmd_arg.cmd = cmd; 18668c2ecf20Sopenharmony_ci cmd_arg.arg = arg; 18678c2ecf20Sopenharmony_ci device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd); 18688c2ecf20Sopenharmony_ci} 18698c2ecf20Sopenharmony_ciEXPORT_SYMBOL(i2c_clients_command); 18708c2ecf20Sopenharmony_ci 18718c2ecf20Sopenharmony_cistatic int __init i2c_init(void) 18728c2ecf20Sopenharmony_ci{ 18738c2ecf20Sopenharmony_ci int retval; 18748c2ecf20Sopenharmony_ci 18758c2ecf20Sopenharmony_ci retval = of_alias_get_highest_id("i2c"); 18768c2ecf20Sopenharmony_ci 18778c2ecf20Sopenharmony_ci down_write(&__i2c_board_lock); 18788c2ecf20Sopenharmony_ci if (retval >= __i2c_first_dynamic_bus_num) 18798c2ecf20Sopenharmony_ci __i2c_first_dynamic_bus_num = retval + 1; 18808c2ecf20Sopenharmony_ci up_write(&__i2c_board_lock); 18818c2ecf20Sopenharmony_ci 18828c2ecf20Sopenharmony_ci retval = bus_register(&i2c_bus_type); 18838c2ecf20Sopenharmony_ci if (retval) 18848c2ecf20Sopenharmony_ci return retval; 18858c2ecf20Sopenharmony_ci 18868c2ecf20Sopenharmony_ci is_registered = true; 18878c2ecf20Sopenharmony_ci 18888c2ecf20Sopenharmony_ci#ifdef CONFIG_I2C_COMPAT 18898c2ecf20Sopenharmony_ci i2c_adapter_compat_class = class_compat_register("i2c-adapter"); 18908c2ecf20Sopenharmony_ci if (!i2c_adapter_compat_class) { 18918c2ecf20Sopenharmony_ci retval = -ENOMEM; 18928c2ecf20Sopenharmony_ci goto bus_err; 18938c2ecf20Sopenharmony_ci } 18948c2ecf20Sopenharmony_ci#endif 18958c2ecf20Sopenharmony_ci retval = i2c_add_driver(&dummy_driver); 18968c2ecf20Sopenharmony_ci if (retval) 18978c2ecf20Sopenharmony_ci goto class_err; 18988c2ecf20Sopenharmony_ci 18998c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 19008c2ecf20Sopenharmony_ci WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier)); 19018c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_ACPI)) 19028c2ecf20Sopenharmony_ci WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier)); 19038c2ecf20Sopenharmony_ci 19048c2ecf20Sopenharmony_ci return 0; 19058c2ecf20Sopenharmony_ci 19068c2ecf20Sopenharmony_ciclass_err: 19078c2ecf20Sopenharmony_ci#ifdef CONFIG_I2C_COMPAT 19088c2ecf20Sopenharmony_ci class_compat_unregister(i2c_adapter_compat_class); 19098c2ecf20Sopenharmony_cibus_err: 19108c2ecf20Sopenharmony_ci#endif 19118c2ecf20Sopenharmony_ci is_registered = false; 19128c2ecf20Sopenharmony_ci bus_unregister(&i2c_bus_type); 19138c2ecf20Sopenharmony_ci return retval; 19148c2ecf20Sopenharmony_ci} 19158c2ecf20Sopenharmony_ci 19168c2ecf20Sopenharmony_cistatic void __exit i2c_exit(void) 19178c2ecf20Sopenharmony_ci{ 19188c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_ACPI)) 19198c2ecf20Sopenharmony_ci WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier)); 19208c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_OF_DYNAMIC)) 19218c2ecf20Sopenharmony_ci WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier)); 19228c2ecf20Sopenharmony_ci i2c_del_driver(&dummy_driver); 19238c2ecf20Sopenharmony_ci#ifdef CONFIG_I2C_COMPAT 19248c2ecf20Sopenharmony_ci class_compat_unregister(i2c_adapter_compat_class); 19258c2ecf20Sopenharmony_ci#endif 19268c2ecf20Sopenharmony_ci bus_unregister(&i2c_bus_type); 19278c2ecf20Sopenharmony_ci tracepoint_synchronize_unregister(); 19288c2ecf20Sopenharmony_ci} 19298c2ecf20Sopenharmony_ci 19308c2ecf20Sopenharmony_ci/* We must initialize early, because some subsystems register i2c drivers 19318c2ecf20Sopenharmony_ci * in subsys_initcall() code, but are linked (and initialized) before i2c. 19328c2ecf20Sopenharmony_ci */ 19338c2ecf20Sopenharmony_cipostcore_initcall(i2c_init); 19348c2ecf20Sopenharmony_cimodule_exit(i2c_exit); 19358c2ecf20Sopenharmony_ci 19368c2ecf20Sopenharmony_ci/* ---------------------------------------------------- 19378c2ecf20Sopenharmony_ci * the functional interface to the i2c busses. 19388c2ecf20Sopenharmony_ci * ---------------------------------------------------- 19398c2ecf20Sopenharmony_ci */ 19408c2ecf20Sopenharmony_ci 19418c2ecf20Sopenharmony_ci/* Check if val is exceeding the quirk IFF quirk is non 0 */ 19428c2ecf20Sopenharmony_ci#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk))) 19438c2ecf20Sopenharmony_ci 19448c2ecf20Sopenharmony_cistatic int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg) 19458c2ecf20Sopenharmony_ci{ 19468c2ecf20Sopenharmony_ci dev_err_ratelimited(&adap->dev, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n", 19478c2ecf20Sopenharmony_ci err_msg, msg->addr, msg->len, 19488c2ecf20Sopenharmony_ci msg->flags & I2C_M_RD ? "read" : "write"); 19498c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 19508c2ecf20Sopenharmony_ci} 19518c2ecf20Sopenharmony_ci 19528c2ecf20Sopenharmony_cistatic int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) 19538c2ecf20Sopenharmony_ci{ 19548c2ecf20Sopenharmony_ci const struct i2c_adapter_quirks *q = adap->quirks; 19558c2ecf20Sopenharmony_ci int max_num = q->max_num_msgs, i; 19568c2ecf20Sopenharmony_ci bool do_len_check = true; 19578c2ecf20Sopenharmony_ci 19588c2ecf20Sopenharmony_ci if (q->flags & I2C_AQ_COMB) { 19598c2ecf20Sopenharmony_ci max_num = 2; 19608c2ecf20Sopenharmony_ci 19618c2ecf20Sopenharmony_ci /* special checks for combined messages */ 19628c2ecf20Sopenharmony_ci if (num == 2) { 19638c2ecf20Sopenharmony_ci if (q->flags & I2C_AQ_COMB_WRITE_FIRST && msgs[0].flags & I2C_M_RD) 19648c2ecf20Sopenharmony_ci return i2c_quirk_error(adap, &msgs[0], "1st comb msg must be write"); 19658c2ecf20Sopenharmony_ci 19668c2ecf20Sopenharmony_ci if (q->flags & I2C_AQ_COMB_READ_SECOND && !(msgs[1].flags & I2C_M_RD)) 19678c2ecf20Sopenharmony_ci return i2c_quirk_error(adap, &msgs[1], "2nd comb msg must be read"); 19688c2ecf20Sopenharmony_ci 19698c2ecf20Sopenharmony_ci if (q->flags & I2C_AQ_COMB_SAME_ADDR && msgs[0].addr != msgs[1].addr) 19708c2ecf20Sopenharmony_ci return i2c_quirk_error(adap, &msgs[0], "comb msg only to same addr"); 19718c2ecf20Sopenharmony_ci 19728c2ecf20Sopenharmony_ci if (i2c_quirk_exceeded(msgs[0].len, q->max_comb_1st_msg_len)) 19738c2ecf20Sopenharmony_ci return i2c_quirk_error(adap, &msgs[0], "msg too long"); 19748c2ecf20Sopenharmony_ci 19758c2ecf20Sopenharmony_ci if (i2c_quirk_exceeded(msgs[1].len, q->max_comb_2nd_msg_len)) 19768c2ecf20Sopenharmony_ci return i2c_quirk_error(adap, &msgs[1], "msg too long"); 19778c2ecf20Sopenharmony_ci 19788c2ecf20Sopenharmony_ci do_len_check = false; 19798c2ecf20Sopenharmony_ci } 19808c2ecf20Sopenharmony_ci } 19818c2ecf20Sopenharmony_ci 19828c2ecf20Sopenharmony_ci if (i2c_quirk_exceeded(num, max_num)) 19838c2ecf20Sopenharmony_ci return i2c_quirk_error(adap, &msgs[0], "too many messages"); 19848c2ecf20Sopenharmony_ci 19858c2ecf20Sopenharmony_ci for (i = 0; i < num; i++) { 19868c2ecf20Sopenharmony_ci u16 len = msgs[i].len; 19878c2ecf20Sopenharmony_ci 19888c2ecf20Sopenharmony_ci if (msgs[i].flags & I2C_M_RD) { 19898c2ecf20Sopenharmony_ci if (do_len_check && i2c_quirk_exceeded(len, q->max_read_len)) 19908c2ecf20Sopenharmony_ci return i2c_quirk_error(adap, &msgs[i], "msg too long"); 19918c2ecf20Sopenharmony_ci 19928c2ecf20Sopenharmony_ci if (q->flags & I2C_AQ_NO_ZERO_LEN_READ && len == 0) 19938c2ecf20Sopenharmony_ci return i2c_quirk_error(adap, &msgs[i], "no zero length"); 19948c2ecf20Sopenharmony_ci } else { 19958c2ecf20Sopenharmony_ci if (do_len_check && i2c_quirk_exceeded(len, q->max_write_len)) 19968c2ecf20Sopenharmony_ci return i2c_quirk_error(adap, &msgs[i], "msg too long"); 19978c2ecf20Sopenharmony_ci 19988c2ecf20Sopenharmony_ci if (q->flags & I2C_AQ_NO_ZERO_LEN_WRITE && len == 0) 19998c2ecf20Sopenharmony_ci return i2c_quirk_error(adap, &msgs[i], "no zero length"); 20008c2ecf20Sopenharmony_ci } 20018c2ecf20Sopenharmony_ci } 20028c2ecf20Sopenharmony_ci 20038c2ecf20Sopenharmony_ci return 0; 20048c2ecf20Sopenharmony_ci} 20058c2ecf20Sopenharmony_ci 20068c2ecf20Sopenharmony_ci/** 20078c2ecf20Sopenharmony_ci * __i2c_transfer - unlocked flavor of i2c_transfer 20088c2ecf20Sopenharmony_ci * @adap: Handle to I2C bus 20098c2ecf20Sopenharmony_ci * @msgs: One or more messages to execute before STOP is issued to 20108c2ecf20Sopenharmony_ci * terminate the operation; each message begins with a START. 20118c2ecf20Sopenharmony_ci * @num: Number of messages to be executed. 20128c2ecf20Sopenharmony_ci * 20138c2ecf20Sopenharmony_ci * Returns negative errno, else the number of messages executed. 20148c2ecf20Sopenharmony_ci * 20158c2ecf20Sopenharmony_ci * Adapter lock must be held when calling this function. No debug logging 20168c2ecf20Sopenharmony_ci * takes place. 20178c2ecf20Sopenharmony_ci */ 20188c2ecf20Sopenharmony_ciint __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) 20198c2ecf20Sopenharmony_ci{ 20208c2ecf20Sopenharmony_ci unsigned long orig_jiffies; 20218c2ecf20Sopenharmony_ci int ret, try; 20228c2ecf20Sopenharmony_ci 20238c2ecf20Sopenharmony_ci if (!adap->algo->master_xfer) { 20248c2ecf20Sopenharmony_ci dev_dbg(&adap->dev, "I2C level transfers not supported\n"); 20258c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 20268c2ecf20Sopenharmony_ci } 20278c2ecf20Sopenharmony_ci 20288c2ecf20Sopenharmony_ci if (WARN_ON(!msgs || num < 1)) 20298c2ecf20Sopenharmony_ci return -EINVAL; 20308c2ecf20Sopenharmony_ci 20318c2ecf20Sopenharmony_ci ret = __i2c_check_suspended(adap); 20328c2ecf20Sopenharmony_ci if (ret) 20338c2ecf20Sopenharmony_ci return ret; 20348c2ecf20Sopenharmony_ci 20358c2ecf20Sopenharmony_ci if (adap->quirks && i2c_check_for_quirks(adap, msgs, num)) 20368c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 20378c2ecf20Sopenharmony_ci 20388c2ecf20Sopenharmony_ci /* 20398c2ecf20Sopenharmony_ci * i2c_trace_msg_key gets enabled when tracepoint i2c_transfer gets 20408c2ecf20Sopenharmony_ci * enabled. This is an efficient way of keeping the for-loop from 20418c2ecf20Sopenharmony_ci * being executed when not needed. 20428c2ecf20Sopenharmony_ci */ 20438c2ecf20Sopenharmony_ci if (static_branch_unlikely(&i2c_trace_msg_key)) { 20448c2ecf20Sopenharmony_ci int i; 20458c2ecf20Sopenharmony_ci for (i = 0; i < num; i++) 20468c2ecf20Sopenharmony_ci if (msgs[i].flags & I2C_M_RD) 20478c2ecf20Sopenharmony_ci trace_i2c_read(adap, &msgs[i], i); 20488c2ecf20Sopenharmony_ci else 20498c2ecf20Sopenharmony_ci trace_i2c_write(adap, &msgs[i], i); 20508c2ecf20Sopenharmony_ci } 20518c2ecf20Sopenharmony_ci 20528c2ecf20Sopenharmony_ci /* Retry automatically on arbitration loss */ 20538c2ecf20Sopenharmony_ci orig_jiffies = jiffies; 20548c2ecf20Sopenharmony_ci for (ret = 0, try = 0; try <= adap->retries; try++) { 20558c2ecf20Sopenharmony_ci if (i2c_in_atomic_xfer_mode() && adap->algo->master_xfer_atomic) 20568c2ecf20Sopenharmony_ci ret = adap->algo->master_xfer_atomic(adap, msgs, num); 20578c2ecf20Sopenharmony_ci else 20588c2ecf20Sopenharmony_ci ret = adap->algo->master_xfer(adap, msgs, num); 20598c2ecf20Sopenharmony_ci 20608c2ecf20Sopenharmony_ci if (ret != -EAGAIN) 20618c2ecf20Sopenharmony_ci break; 20628c2ecf20Sopenharmony_ci if (time_after(jiffies, orig_jiffies + adap->timeout)) 20638c2ecf20Sopenharmony_ci break; 20648c2ecf20Sopenharmony_ci } 20658c2ecf20Sopenharmony_ci 20668c2ecf20Sopenharmony_ci if (static_branch_unlikely(&i2c_trace_msg_key)) { 20678c2ecf20Sopenharmony_ci int i; 20688c2ecf20Sopenharmony_ci for (i = 0; i < ret; i++) 20698c2ecf20Sopenharmony_ci if (msgs[i].flags & I2C_M_RD) 20708c2ecf20Sopenharmony_ci trace_i2c_reply(adap, &msgs[i], i); 20718c2ecf20Sopenharmony_ci trace_i2c_result(adap, num, ret); 20728c2ecf20Sopenharmony_ci } 20738c2ecf20Sopenharmony_ci 20748c2ecf20Sopenharmony_ci return ret; 20758c2ecf20Sopenharmony_ci} 20768c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__i2c_transfer); 20778c2ecf20Sopenharmony_ci 20788c2ecf20Sopenharmony_ci/** 20798c2ecf20Sopenharmony_ci * i2c_transfer - execute a single or combined I2C message 20808c2ecf20Sopenharmony_ci * @adap: Handle to I2C bus 20818c2ecf20Sopenharmony_ci * @msgs: One or more messages to execute before STOP is issued to 20828c2ecf20Sopenharmony_ci * terminate the operation; each message begins with a START. 20838c2ecf20Sopenharmony_ci * @num: Number of messages to be executed. 20848c2ecf20Sopenharmony_ci * 20858c2ecf20Sopenharmony_ci * Returns negative errno, else the number of messages executed. 20868c2ecf20Sopenharmony_ci * 20878c2ecf20Sopenharmony_ci * Note that there is no requirement that each message be sent to 20888c2ecf20Sopenharmony_ci * the same slave address, although that is the most common model. 20898c2ecf20Sopenharmony_ci */ 20908c2ecf20Sopenharmony_ciint i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) 20918c2ecf20Sopenharmony_ci{ 20928c2ecf20Sopenharmony_ci int ret; 20938c2ecf20Sopenharmony_ci 20948c2ecf20Sopenharmony_ci /* REVISIT the fault reporting model here is weak: 20958c2ecf20Sopenharmony_ci * 20968c2ecf20Sopenharmony_ci * - When we get an error after receiving N bytes from a slave, 20978c2ecf20Sopenharmony_ci * there is no way to report "N". 20988c2ecf20Sopenharmony_ci * 20998c2ecf20Sopenharmony_ci * - When we get a NAK after transmitting N bytes to a slave, 21008c2ecf20Sopenharmony_ci * there is no way to report "N" ... or to let the master 21018c2ecf20Sopenharmony_ci * continue executing the rest of this combined message, if 21028c2ecf20Sopenharmony_ci * that's the appropriate response. 21038c2ecf20Sopenharmony_ci * 21048c2ecf20Sopenharmony_ci * - When for example "num" is two and we successfully complete 21058c2ecf20Sopenharmony_ci * the first message but get an error part way through the 21068c2ecf20Sopenharmony_ci * second, it's unclear whether that should be reported as 21078c2ecf20Sopenharmony_ci * one (discarding status on the second message) or errno 21088c2ecf20Sopenharmony_ci * (discarding status on the first one). 21098c2ecf20Sopenharmony_ci */ 21108c2ecf20Sopenharmony_ci ret = __i2c_lock_bus_helper(adap); 21118c2ecf20Sopenharmony_ci if (ret) 21128c2ecf20Sopenharmony_ci return ret; 21138c2ecf20Sopenharmony_ci 21148c2ecf20Sopenharmony_ci ret = __i2c_transfer(adap, msgs, num); 21158c2ecf20Sopenharmony_ci i2c_unlock_bus(adap, I2C_LOCK_SEGMENT); 21168c2ecf20Sopenharmony_ci 21178c2ecf20Sopenharmony_ci return ret; 21188c2ecf20Sopenharmony_ci} 21198c2ecf20Sopenharmony_ciEXPORT_SYMBOL(i2c_transfer); 21208c2ecf20Sopenharmony_ci 21218c2ecf20Sopenharmony_ci/** 21228c2ecf20Sopenharmony_ci * i2c_transfer_buffer_flags - issue a single I2C message transferring data 21238c2ecf20Sopenharmony_ci * to/from a buffer 21248c2ecf20Sopenharmony_ci * @client: Handle to slave device 21258c2ecf20Sopenharmony_ci * @buf: Where the data is stored 21268c2ecf20Sopenharmony_ci * @count: How many bytes to transfer, must be less than 64k since msg.len is u16 21278c2ecf20Sopenharmony_ci * @flags: The flags to be used for the message, e.g. I2C_M_RD for reads 21288c2ecf20Sopenharmony_ci * 21298c2ecf20Sopenharmony_ci * Returns negative errno, or else the number of bytes transferred. 21308c2ecf20Sopenharmony_ci */ 21318c2ecf20Sopenharmony_ciint i2c_transfer_buffer_flags(const struct i2c_client *client, char *buf, 21328c2ecf20Sopenharmony_ci int count, u16 flags) 21338c2ecf20Sopenharmony_ci{ 21348c2ecf20Sopenharmony_ci int ret; 21358c2ecf20Sopenharmony_ci struct i2c_msg msg = { 21368c2ecf20Sopenharmony_ci .addr = client->addr, 21378c2ecf20Sopenharmony_ci .flags = flags | (client->flags & I2C_M_TEN), 21388c2ecf20Sopenharmony_ci .len = count, 21398c2ecf20Sopenharmony_ci .buf = buf, 21408c2ecf20Sopenharmony_ci }; 21418c2ecf20Sopenharmony_ci 21428c2ecf20Sopenharmony_ci ret = i2c_transfer(client->adapter, &msg, 1); 21438c2ecf20Sopenharmony_ci 21448c2ecf20Sopenharmony_ci /* 21458c2ecf20Sopenharmony_ci * If everything went ok (i.e. 1 msg transferred), return #bytes 21468c2ecf20Sopenharmony_ci * transferred, else error code. 21478c2ecf20Sopenharmony_ci */ 21488c2ecf20Sopenharmony_ci return (ret == 1) ? count : ret; 21498c2ecf20Sopenharmony_ci} 21508c2ecf20Sopenharmony_ciEXPORT_SYMBOL(i2c_transfer_buffer_flags); 21518c2ecf20Sopenharmony_ci 21528c2ecf20Sopenharmony_ci/** 21538c2ecf20Sopenharmony_ci * i2c_get_device_id - get manufacturer, part id and die revision of a device 21548c2ecf20Sopenharmony_ci * @client: The device to query 21558c2ecf20Sopenharmony_ci * @id: The queried information 21568c2ecf20Sopenharmony_ci * 21578c2ecf20Sopenharmony_ci * Returns negative errno on error, zero on success. 21588c2ecf20Sopenharmony_ci */ 21598c2ecf20Sopenharmony_ciint i2c_get_device_id(const struct i2c_client *client, 21608c2ecf20Sopenharmony_ci struct i2c_device_identity *id) 21618c2ecf20Sopenharmony_ci{ 21628c2ecf20Sopenharmony_ci struct i2c_adapter *adap = client->adapter; 21638c2ecf20Sopenharmony_ci union i2c_smbus_data raw_id; 21648c2ecf20Sopenharmony_ci int ret; 21658c2ecf20Sopenharmony_ci 21668c2ecf20Sopenharmony_ci if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_I2C_BLOCK)) 21678c2ecf20Sopenharmony_ci return -EOPNOTSUPP; 21688c2ecf20Sopenharmony_ci 21698c2ecf20Sopenharmony_ci raw_id.block[0] = 3; 21708c2ecf20Sopenharmony_ci ret = i2c_smbus_xfer(adap, I2C_ADDR_DEVICE_ID, 0, 21718c2ecf20Sopenharmony_ci I2C_SMBUS_READ, client->addr << 1, 21728c2ecf20Sopenharmony_ci I2C_SMBUS_I2C_BLOCK_DATA, &raw_id); 21738c2ecf20Sopenharmony_ci if (ret) 21748c2ecf20Sopenharmony_ci return ret; 21758c2ecf20Sopenharmony_ci 21768c2ecf20Sopenharmony_ci id->manufacturer_id = (raw_id.block[1] << 4) | (raw_id.block[2] >> 4); 21778c2ecf20Sopenharmony_ci id->part_id = ((raw_id.block[2] & 0xf) << 5) | (raw_id.block[3] >> 3); 21788c2ecf20Sopenharmony_ci id->die_revision = raw_id.block[3] & 0x7; 21798c2ecf20Sopenharmony_ci return 0; 21808c2ecf20Sopenharmony_ci} 21818c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_get_device_id); 21828c2ecf20Sopenharmony_ci 21838c2ecf20Sopenharmony_ci/* ---------------------------------------------------- 21848c2ecf20Sopenharmony_ci * the i2c address scanning function 21858c2ecf20Sopenharmony_ci * Will not work for 10-bit addresses! 21868c2ecf20Sopenharmony_ci * ---------------------------------------------------- 21878c2ecf20Sopenharmony_ci */ 21888c2ecf20Sopenharmony_ci 21898c2ecf20Sopenharmony_ci/* 21908c2ecf20Sopenharmony_ci * Legacy default probe function, mostly relevant for SMBus. The default 21918c2ecf20Sopenharmony_ci * probe method is a quick write, but it is known to corrupt the 24RF08 21928c2ecf20Sopenharmony_ci * EEPROMs due to a state machine bug, and could also irreversibly 21938c2ecf20Sopenharmony_ci * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f, 21948c2ecf20Sopenharmony_ci * we use a short byte read instead. Also, some bus drivers don't implement 21958c2ecf20Sopenharmony_ci * quick write, so we fallback to a byte read in that case too. 21968c2ecf20Sopenharmony_ci * On x86, there is another special case for FSC hardware monitoring chips, 21978c2ecf20Sopenharmony_ci * which want regular byte reads (address 0x73.) Fortunately, these are the 21988c2ecf20Sopenharmony_ci * only known chips using this I2C address on PC hardware. 21998c2ecf20Sopenharmony_ci * Returns 1 if probe succeeded, 0 if not. 22008c2ecf20Sopenharmony_ci */ 22018c2ecf20Sopenharmony_cistatic int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr) 22028c2ecf20Sopenharmony_ci{ 22038c2ecf20Sopenharmony_ci int err; 22048c2ecf20Sopenharmony_ci union i2c_smbus_data dummy; 22058c2ecf20Sopenharmony_ci 22068c2ecf20Sopenharmony_ci#ifdef CONFIG_X86 22078c2ecf20Sopenharmony_ci if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON) 22088c2ecf20Sopenharmony_ci && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA)) 22098c2ecf20Sopenharmony_ci err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, 22108c2ecf20Sopenharmony_ci I2C_SMBUS_BYTE_DATA, &dummy); 22118c2ecf20Sopenharmony_ci else 22128c2ecf20Sopenharmony_ci#endif 22138c2ecf20Sopenharmony_ci if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50) 22148c2ecf20Sopenharmony_ci && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) 22158c2ecf20Sopenharmony_ci err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0, 22168c2ecf20Sopenharmony_ci I2C_SMBUS_QUICK, NULL); 22178c2ecf20Sopenharmony_ci else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) 22188c2ecf20Sopenharmony_ci err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, 22198c2ecf20Sopenharmony_ci I2C_SMBUS_BYTE, &dummy); 22208c2ecf20Sopenharmony_ci else { 22218c2ecf20Sopenharmony_ci dev_warn(&adap->dev, "No suitable probing method supported for address 0x%02X\n", 22228c2ecf20Sopenharmony_ci addr); 22238c2ecf20Sopenharmony_ci err = -EOPNOTSUPP; 22248c2ecf20Sopenharmony_ci } 22258c2ecf20Sopenharmony_ci 22268c2ecf20Sopenharmony_ci return err >= 0; 22278c2ecf20Sopenharmony_ci} 22288c2ecf20Sopenharmony_ci 22298c2ecf20Sopenharmony_cistatic int i2c_detect_address(struct i2c_client *temp_client, 22308c2ecf20Sopenharmony_ci struct i2c_driver *driver) 22318c2ecf20Sopenharmony_ci{ 22328c2ecf20Sopenharmony_ci struct i2c_board_info info; 22338c2ecf20Sopenharmony_ci struct i2c_adapter *adapter = temp_client->adapter; 22348c2ecf20Sopenharmony_ci int addr = temp_client->addr; 22358c2ecf20Sopenharmony_ci int err; 22368c2ecf20Sopenharmony_ci 22378c2ecf20Sopenharmony_ci /* Make sure the address is valid */ 22388c2ecf20Sopenharmony_ci err = i2c_check_7bit_addr_validity_strict(addr); 22398c2ecf20Sopenharmony_ci if (err) { 22408c2ecf20Sopenharmony_ci dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n", 22418c2ecf20Sopenharmony_ci addr); 22428c2ecf20Sopenharmony_ci return err; 22438c2ecf20Sopenharmony_ci } 22448c2ecf20Sopenharmony_ci 22458c2ecf20Sopenharmony_ci /* Skip if already in use (7 bit, no need to encode flags) */ 22468c2ecf20Sopenharmony_ci if (i2c_check_addr_busy(adapter, addr)) 22478c2ecf20Sopenharmony_ci return 0; 22488c2ecf20Sopenharmony_ci 22498c2ecf20Sopenharmony_ci /* Make sure there is something at this address */ 22508c2ecf20Sopenharmony_ci if (!i2c_default_probe(adapter, addr)) 22518c2ecf20Sopenharmony_ci return 0; 22528c2ecf20Sopenharmony_ci 22538c2ecf20Sopenharmony_ci /* Finally call the custom detection function */ 22548c2ecf20Sopenharmony_ci memset(&info, 0, sizeof(struct i2c_board_info)); 22558c2ecf20Sopenharmony_ci info.addr = addr; 22568c2ecf20Sopenharmony_ci err = driver->detect(temp_client, &info); 22578c2ecf20Sopenharmony_ci if (err) { 22588c2ecf20Sopenharmony_ci /* -ENODEV is returned if the detection fails. We catch it 22598c2ecf20Sopenharmony_ci here as this isn't an error. */ 22608c2ecf20Sopenharmony_ci return err == -ENODEV ? 0 : err; 22618c2ecf20Sopenharmony_ci } 22628c2ecf20Sopenharmony_ci 22638c2ecf20Sopenharmony_ci /* Consistency check */ 22648c2ecf20Sopenharmony_ci if (info.type[0] == '\0') { 22658c2ecf20Sopenharmony_ci dev_err(&adapter->dev, 22668c2ecf20Sopenharmony_ci "%s detection function provided no name for 0x%x\n", 22678c2ecf20Sopenharmony_ci driver->driver.name, addr); 22688c2ecf20Sopenharmony_ci } else { 22698c2ecf20Sopenharmony_ci struct i2c_client *client; 22708c2ecf20Sopenharmony_ci 22718c2ecf20Sopenharmony_ci /* Detection succeeded, instantiate the device */ 22728c2ecf20Sopenharmony_ci if (adapter->class & I2C_CLASS_DEPRECATED) 22738c2ecf20Sopenharmony_ci dev_warn(&adapter->dev, 22748c2ecf20Sopenharmony_ci "This adapter will soon drop class based instantiation of devices. " 22758c2ecf20Sopenharmony_ci "Please make sure client 0x%02x gets instantiated by other means. " 22768c2ecf20Sopenharmony_ci "Check 'Documentation/i2c/instantiating-devices.rst' for details.\n", 22778c2ecf20Sopenharmony_ci info.addr); 22788c2ecf20Sopenharmony_ci 22798c2ecf20Sopenharmony_ci dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n", 22808c2ecf20Sopenharmony_ci info.type, info.addr); 22818c2ecf20Sopenharmony_ci client = i2c_new_client_device(adapter, &info); 22828c2ecf20Sopenharmony_ci if (!IS_ERR(client)) 22838c2ecf20Sopenharmony_ci list_add_tail(&client->detected, &driver->clients); 22848c2ecf20Sopenharmony_ci else 22858c2ecf20Sopenharmony_ci dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n", 22868c2ecf20Sopenharmony_ci info.type, info.addr); 22878c2ecf20Sopenharmony_ci } 22888c2ecf20Sopenharmony_ci return 0; 22898c2ecf20Sopenharmony_ci} 22908c2ecf20Sopenharmony_ci 22918c2ecf20Sopenharmony_cistatic int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver) 22928c2ecf20Sopenharmony_ci{ 22938c2ecf20Sopenharmony_ci const unsigned short *address_list; 22948c2ecf20Sopenharmony_ci struct i2c_client *temp_client; 22958c2ecf20Sopenharmony_ci int i, err = 0; 22968c2ecf20Sopenharmony_ci 22978c2ecf20Sopenharmony_ci address_list = driver->address_list; 22988c2ecf20Sopenharmony_ci if (!driver->detect || !address_list) 22998c2ecf20Sopenharmony_ci return 0; 23008c2ecf20Sopenharmony_ci 23018c2ecf20Sopenharmony_ci /* Warn that the adapter lost class based instantiation */ 23028c2ecf20Sopenharmony_ci if (adapter->class == I2C_CLASS_DEPRECATED) { 23038c2ecf20Sopenharmony_ci dev_dbg(&adapter->dev, 23048c2ecf20Sopenharmony_ci "This adapter dropped support for I2C classes and won't auto-detect %s devices anymore. " 23058c2ecf20Sopenharmony_ci "If you need it, check 'Documentation/i2c/instantiating-devices.rst' for alternatives.\n", 23068c2ecf20Sopenharmony_ci driver->driver.name); 23078c2ecf20Sopenharmony_ci return 0; 23088c2ecf20Sopenharmony_ci } 23098c2ecf20Sopenharmony_ci 23108c2ecf20Sopenharmony_ci /* Stop here if the classes do not match */ 23118c2ecf20Sopenharmony_ci if (!(adapter->class & driver->class)) 23128c2ecf20Sopenharmony_ci return 0; 23138c2ecf20Sopenharmony_ci 23148c2ecf20Sopenharmony_ci /* Set up a temporary client to help detect callback */ 23158c2ecf20Sopenharmony_ci temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL); 23168c2ecf20Sopenharmony_ci if (!temp_client) 23178c2ecf20Sopenharmony_ci return -ENOMEM; 23188c2ecf20Sopenharmony_ci temp_client->adapter = adapter; 23198c2ecf20Sopenharmony_ci 23208c2ecf20Sopenharmony_ci for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) { 23218c2ecf20Sopenharmony_ci dev_dbg(&adapter->dev, 23228c2ecf20Sopenharmony_ci "found normal entry for adapter %d, addr 0x%02x\n", 23238c2ecf20Sopenharmony_ci i2c_adapter_id(adapter), address_list[i]); 23248c2ecf20Sopenharmony_ci temp_client->addr = address_list[i]; 23258c2ecf20Sopenharmony_ci err = i2c_detect_address(temp_client, driver); 23268c2ecf20Sopenharmony_ci if (unlikely(err)) 23278c2ecf20Sopenharmony_ci break; 23288c2ecf20Sopenharmony_ci } 23298c2ecf20Sopenharmony_ci 23308c2ecf20Sopenharmony_ci kfree(temp_client); 23318c2ecf20Sopenharmony_ci return err; 23328c2ecf20Sopenharmony_ci} 23338c2ecf20Sopenharmony_ci 23348c2ecf20Sopenharmony_ciint i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr) 23358c2ecf20Sopenharmony_ci{ 23368c2ecf20Sopenharmony_ci return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0, 23378c2ecf20Sopenharmony_ci I2C_SMBUS_QUICK, NULL) >= 0; 23388c2ecf20Sopenharmony_ci} 23398c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_probe_func_quick_read); 23408c2ecf20Sopenharmony_ci 23418c2ecf20Sopenharmony_cistruct i2c_client * 23428c2ecf20Sopenharmony_cii2c_new_scanned_device(struct i2c_adapter *adap, 23438c2ecf20Sopenharmony_ci struct i2c_board_info *info, 23448c2ecf20Sopenharmony_ci unsigned short const *addr_list, 23458c2ecf20Sopenharmony_ci int (*probe)(struct i2c_adapter *adap, unsigned short addr)) 23468c2ecf20Sopenharmony_ci{ 23478c2ecf20Sopenharmony_ci int i; 23488c2ecf20Sopenharmony_ci 23498c2ecf20Sopenharmony_ci if (!probe) 23508c2ecf20Sopenharmony_ci probe = i2c_default_probe; 23518c2ecf20Sopenharmony_ci 23528c2ecf20Sopenharmony_ci for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) { 23538c2ecf20Sopenharmony_ci /* Check address validity */ 23548c2ecf20Sopenharmony_ci if (i2c_check_7bit_addr_validity_strict(addr_list[i]) < 0) { 23558c2ecf20Sopenharmony_ci dev_warn(&adap->dev, "Invalid 7-bit address 0x%02x\n", 23568c2ecf20Sopenharmony_ci addr_list[i]); 23578c2ecf20Sopenharmony_ci continue; 23588c2ecf20Sopenharmony_ci } 23598c2ecf20Sopenharmony_ci 23608c2ecf20Sopenharmony_ci /* Check address availability (7 bit, no need to encode flags) */ 23618c2ecf20Sopenharmony_ci if (i2c_check_addr_busy(adap, addr_list[i])) { 23628c2ecf20Sopenharmony_ci dev_dbg(&adap->dev, 23638c2ecf20Sopenharmony_ci "Address 0x%02x already in use, not probing\n", 23648c2ecf20Sopenharmony_ci addr_list[i]); 23658c2ecf20Sopenharmony_ci continue; 23668c2ecf20Sopenharmony_ci } 23678c2ecf20Sopenharmony_ci 23688c2ecf20Sopenharmony_ci /* Test address responsiveness */ 23698c2ecf20Sopenharmony_ci if (probe(adap, addr_list[i])) 23708c2ecf20Sopenharmony_ci break; 23718c2ecf20Sopenharmony_ci } 23728c2ecf20Sopenharmony_ci 23738c2ecf20Sopenharmony_ci if (addr_list[i] == I2C_CLIENT_END) { 23748c2ecf20Sopenharmony_ci dev_dbg(&adap->dev, "Probing failed, no device found\n"); 23758c2ecf20Sopenharmony_ci return ERR_PTR(-ENODEV); 23768c2ecf20Sopenharmony_ci } 23778c2ecf20Sopenharmony_ci 23788c2ecf20Sopenharmony_ci info->addr = addr_list[i]; 23798c2ecf20Sopenharmony_ci return i2c_new_client_device(adap, info); 23808c2ecf20Sopenharmony_ci} 23818c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_new_scanned_device); 23828c2ecf20Sopenharmony_ci 23838c2ecf20Sopenharmony_cistruct i2c_adapter *i2c_get_adapter(int nr) 23848c2ecf20Sopenharmony_ci{ 23858c2ecf20Sopenharmony_ci struct i2c_adapter *adapter; 23868c2ecf20Sopenharmony_ci 23878c2ecf20Sopenharmony_ci mutex_lock(&core_lock); 23888c2ecf20Sopenharmony_ci adapter = idr_find(&i2c_adapter_idr, nr); 23898c2ecf20Sopenharmony_ci if (!adapter) 23908c2ecf20Sopenharmony_ci goto exit; 23918c2ecf20Sopenharmony_ci 23928c2ecf20Sopenharmony_ci if (try_module_get(adapter->owner)) 23938c2ecf20Sopenharmony_ci get_device(&adapter->dev); 23948c2ecf20Sopenharmony_ci else 23958c2ecf20Sopenharmony_ci adapter = NULL; 23968c2ecf20Sopenharmony_ci 23978c2ecf20Sopenharmony_ci exit: 23988c2ecf20Sopenharmony_ci mutex_unlock(&core_lock); 23998c2ecf20Sopenharmony_ci return adapter; 24008c2ecf20Sopenharmony_ci} 24018c2ecf20Sopenharmony_ciEXPORT_SYMBOL(i2c_get_adapter); 24028c2ecf20Sopenharmony_ci 24038c2ecf20Sopenharmony_civoid i2c_put_adapter(struct i2c_adapter *adap) 24048c2ecf20Sopenharmony_ci{ 24058c2ecf20Sopenharmony_ci if (!adap) 24068c2ecf20Sopenharmony_ci return; 24078c2ecf20Sopenharmony_ci 24088c2ecf20Sopenharmony_ci module_put(adap->owner); 24098c2ecf20Sopenharmony_ci /* Should be last, otherwise we risk use-after-free with 'adap' */ 24108c2ecf20Sopenharmony_ci put_device(&adap->dev); 24118c2ecf20Sopenharmony_ci} 24128c2ecf20Sopenharmony_ciEXPORT_SYMBOL(i2c_put_adapter); 24138c2ecf20Sopenharmony_ci 24148c2ecf20Sopenharmony_ci/** 24158c2ecf20Sopenharmony_ci * i2c_get_dma_safe_msg_buf() - get a DMA safe buffer for the given i2c_msg 24168c2ecf20Sopenharmony_ci * @msg: the message to be checked 24178c2ecf20Sopenharmony_ci * @threshold: the minimum number of bytes for which using DMA makes sense. 24188c2ecf20Sopenharmony_ci * Should at least be 1. 24198c2ecf20Sopenharmony_ci * 24208c2ecf20Sopenharmony_ci * Return: NULL if a DMA safe buffer was not obtained. Use msg->buf with PIO. 24218c2ecf20Sopenharmony_ci * Or a valid pointer to be used with DMA. After use, release it by 24228c2ecf20Sopenharmony_ci * calling i2c_put_dma_safe_msg_buf(). 24238c2ecf20Sopenharmony_ci * 24248c2ecf20Sopenharmony_ci * This function must only be called from process context! 24258c2ecf20Sopenharmony_ci */ 24268c2ecf20Sopenharmony_ciu8 *i2c_get_dma_safe_msg_buf(struct i2c_msg *msg, unsigned int threshold) 24278c2ecf20Sopenharmony_ci{ 24288c2ecf20Sopenharmony_ci /* also skip 0-length msgs for bogus thresholds of 0 */ 24298c2ecf20Sopenharmony_ci if (!threshold) 24308c2ecf20Sopenharmony_ci pr_debug("DMA buffer for addr=0x%02x with length 0 is bogus\n", 24318c2ecf20Sopenharmony_ci msg->addr); 24328c2ecf20Sopenharmony_ci if (msg->len < threshold || msg->len == 0) 24338c2ecf20Sopenharmony_ci return NULL; 24348c2ecf20Sopenharmony_ci 24358c2ecf20Sopenharmony_ci if (msg->flags & I2C_M_DMA_SAFE) 24368c2ecf20Sopenharmony_ci return msg->buf; 24378c2ecf20Sopenharmony_ci 24388c2ecf20Sopenharmony_ci pr_debug("using bounce buffer for addr=0x%02x, len=%d\n", 24398c2ecf20Sopenharmony_ci msg->addr, msg->len); 24408c2ecf20Sopenharmony_ci 24418c2ecf20Sopenharmony_ci if (msg->flags & I2C_M_RD) 24428c2ecf20Sopenharmony_ci return kzalloc(msg->len, GFP_KERNEL); 24438c2ecf20Sopenharmony_ci else 24448c2ecf20Sopenharmony_ci return kmemdup(msg->buf, msg->len, GFP_KERNEL); 24458c2ecf20Sopenharmony_ci} 24468c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_get_dma_safe_msg_buf); 24478c2ecf20Sopenharmony_ci 24488c2ecf20Sopenharmony_ci/** 24498c2ecf20Sopenharmony_ci * i2c_put_dma_safe_msg_buf - release DMA safe buffer and sync with i2c_msg 24508c2ecf20Sopenharmony_ci * @buf: the buffer obtained from i2c_get_dma_safe_msg_buf(). May be NULL. 24518c2ecf20Sopenharmony_ci * @msg: the message which the buffer corresponds to 24528c2ecf20Sopenharmony_ci * @xferred: bool saying if the message was transferred 24538c2ecf20Sopenharmony_ci */ 24548c2ecf20Sopenharmony_civoid i2c_put_dma_safe_msg_buf(u8 *buf, struct i2c_msg *msg, bool xferred) 24558c2ecf20Sopenharmony_ci{ 24568c2ecf20Sopenharmony_ci if (!buf || buf == msg->buf) 24578c2ecf20Sopenharmony_ci return; 24588c2ecf20Sopenharmony_ci 24598c2ecf20Sopenharmony_ci if (xferred && msg->flags & I2C_M_RD) 24608c2ecf20Sopenharmony_ci memcpy(msg->buf, buf, msg->len); 24618c2ecf20Sopenharmony_ci 24628c2ecf20Sopenharmony_ci kfree(buf); 24638c2ecf20Sopenharmony_ci} 24648c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(i2c_put_dma_safe_msg_buf); 24658c2ecf20Sopenharmony_ci 24668c2ecf20Sopenharmony_ciMODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>"); 24678c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("I2C-Bus main module"); 24688c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 2469