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