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