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