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