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