1 /* i2c-core.c - a device driver for the iic-bus interface */
2 /* ------------------------------------------------------------------------- */
3 /* Copyright (C) 1995-99 Simon G. Vogl
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details. */
14 /* ------------------------------------------------------------------------- */
16 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
17 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
18 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
19 Jean Delvare <jdelvare@suse.de>
20 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
21 Michael Lawnick <michael.lawnick.ext@nsn.com>
22 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
23 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
24 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
25 I2C ACPI code Copyright (C) 2014 Intel Corp
26 Author: Lan Tianyu <tianyu.lan@intel.com>
27 I2C slave support (c) 2014 by Wolfram Sang <wsa@sang-engineering.com>
30 #include <dt-bindings/i2c/i2c.h>
31 #include <asm/uaccess.h>
32 #include <linux/acpi.h>
33 #include <linux/clk/clk-conf.h>
34 #include <linux/completion.h>
35 #include <linux/delay.h>
36 #include <linux/err.h>
37 #include <linux/errno.h>
38 #include <linux/gpio.h>
39 #include <linux/hardirq.h>
40 #include <linux/i2c.h>
41 #include <linux/idr.h>
42 #include <linux/init.h>
43 #include <linux/irqflags.h>
44 #include <linux/jump_label.h>
45 #include <linux/kernel.h>
46 #include <linux/module.h>
47 #include <linux/mutex.h>
48 #include <linux/of_device.h>
50 #include <linux/of_irq.h>
51 #include <linux/pm_domain.h>
52 #include <linux/pm_runtime.h>
53 #include <linux/pm_wakeirq.h>
54 #include <linux/property.h>
55 #include <linux/rwsem.h>
56 #include <linux/slab.h>
60 #define CREATE_TRACE_POINTS
61 #include <trace/events/i2c.h>
63 #define I2C_ADDR_OFFSET_TEN_BIT 0xa000
64 #define I2C_ADDR_OFFSET_SLAVE 0x1000
66 /* core_lock protects i2c_adapter_idr, and guarantees
67 that device detection, deletion of detected devices, and attach_adapter
68 calls are serialized */
69 static DEFINE_MUTEX(core_lock
);
70 static DEFINE_IDR(i2c_adapter_idr
);
72 static struct device_type i2c_client_type
;
73 static int i2c_detect(struct i2c_adapter
*adapter
, struct i2c_driver
*driver
);
75 static struct static_key i2c_trace_msg
= STATIC_KEY_INIT_FALSE
;
76 static bool is_registered
;
78 void i2c_transfer_trace_reg(void)
80 static_key_slow_inc(&i2c_trace_msg
);
83 void i2c_transfer_trace_unreg(void)
85 static_key_slow_dec(&i2c_trace_msg
);
88 #if defined(CONFIG_ACPI)
89 struct acpi_i2c_handler_data
{
90 struct acpi_connection_info info
;
91 struct i2c_adapter
*adapter
;
104 struct acpi_i2c_lookup
{
105 struct i2c_board_info
*info
;
106 acpi_handle adapter_handle
;
107 acpi_handle device_handle
;
110 static int acpi_i2c_fill_info(struct acpi_resource
*ares
, void *data
)
112 struct acpi_i2c_lookup
*lookup
= data
;
113 struct i2c_board_info
*info
= lookup
->info
;
114 struct acpi_resource_i2c_serialbus
*sb
;
117 if (info
->addr
|| ares
->type
!= ACPI_RESOURCE_TYPE_SERIAL_BUS
)
120 sb
= &ares
->data
.i2c_serial_bus
;
121 if (sb
->type
!= ACPI_RESOURCE_SERIAL_TYPE_I2C
)
124 status
= acpi_get_handle(lookup
->device_handle
,
125 sb
->resource_source
.string_ptr
,
126 &lookup
->adapter_handle
);
127 if (!ACPI_SUCCESS(status
))
130 info
->addr
= sb
->slave_address
;
131 if (sb
->access_mode
== ACPI_I2C_10BIT_MODE
)
132 info
->flags
|= I2C_CLIENT_TEN
;
137 static int acpi_i2c_get_info(struct acpi_device
*adev
,
138 struct i2c_board_info
*info
,
139 acpi_handle
*adapter_handle
)
141 struct list_head resource_list
;
142 struct resource_entry
*entry
;
143 struct acpi_i2c_lookup lookup
;
146 if (acpi_bus_get_status(adev
) || !adev
->status
.present
||
147 acpi_device_enumerated(adev
))
150 memset(info
, 0, sizeof(*info
));
151 info
->fwnode
= acpi_fwnode_handle(adev
);
153 memset(&lookup
, 0, sizeof(lookup
));
154 lookup
.device_handle
= acpi_device_handle(adev
);
157 /* Look up for I2cSerialBus resource */
158 INIT_LIST_HEAD(&resource_list
);
159 ret
= acpi_dev_get_resources(adev
, &resource_list
,
160 acpi_i2c_fill_info
, &lookup
);
161 acpi_dev_free_resource_list(&resource_list
);
163 if (ret
< 0 || !info
->addr
)
166 *adapter_handle
= lookup
.adapter_handle
;
168 /* Then fill IRQ number if any */
169 ret
= acpi_dev_get_resources(adev
, &resource_list
, NULL
, NULL
);
173 resource_list_for_each_entry(entry
, &resource_list
) {
174 if (resource_type(entry
->res
) == IORESOURCE_IRQ
) {
175 info
->irq
= entry
->res
->start
;
180 acpi_dev_free_resource_list(&resource_list
);
182 strlcpy(info
->type
, dev_name(&adev
->dev
), sizeof(info
->type
));
187 static void acpi_i2c_register_device(struct i2c_adapter
*adapter
,
188 struct acpi_device
*adev
,
189 struct i2c_board_info
*info
)
191 adev
->power
.flags
.ignore_parent
= true;
192 acpi_device_set_enumerated(adev
);
194 if (!i2c_new_device(adapter
, info
)) {
195 adev
->power
.flags
.ignore_parent
= false;
196 dev_err(&adapter
->dev
,
197 "failed to add I2C device %s from ACPI\n",
198 dev_name(&adev
->dev
));
202 static acpi_status
acpi_i2c_add_device(acpi_handle handle
, u32 level
,
203 void *data
, void **return_value
)
205 struct i2c_adapter
*adapter
= data
;
206 struct acpi_device
*adev
;
207 acpi_handle adapter_handle
;
208 struct i2c_board_info info
;
210 if (acpi_bus_get_device(handle
, &adev
))
213 if (acpi_i2c_get_info(adev
, &info
, &adapter_handle
))
216 if (adapter_handle
!= ACPI_HANDLE(&adapter
->dev
))
219 acpi_i2c_register_device(adapter
, adev
, &info
);
224 #define ACPI_I2C_MAX_SCAN_DEPTH 32
227 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
228 * @adap: pointer to adapter
230 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
231 * namespace. When a device is found it will be added to the Linux device
232 * model and bound to the corresponding ACPI handle.
234 static void acpi_i2c_register_devices(struct i2c_adapter
*adap
)
238 if (!has_acpi_companion(&adap
->dev
))
241 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, ACPI_ROOT_OBJECT
,
242 ACPI_I2C_MAX_SCAN_DEPTH
,
243 acpi_i2c_add_device
, NULL
,
245 if (ACPI_FAILURE(status
))
246 dev_warn(&adap
->dev
, "failed to enumerate I2C slaves\n");
249 static int acpi_i2c_match_adapter(struct device
*dev
, void *data
)
251 struct i2c_adapter
*adapter
= i2c_verify_adapter(dev
);
256 return ACPI_HANDLE(dev
) == (acpi_handle
)data
;
259 static int acpi_i2c_match_device(struct device
*dev
, void *data
)
261 return ACPI_COMPANION(dev
) == data
;
264 static struct i2c_adapter
*acpi_i2c_find_adapter_by_handle(acpi_handle handle
)
268 dev
= bus_find_device(&i2c_bus_type
, NULL
, handle
,
269 acpi_i2c_match_adapter
);
270 return dev
? i2c_verify_adapter(dev
) : NULL
;
273 static struct i2c_client
*acpi_i2c_find_client_by_adev(struct acpi_device
*adev
)
277 dev
= bus_find_device(&i2c_bus_type
, NULL
, adev
, acpi_i2c_match_device
);
278 return dev
? i2c_verify_client(dev
) : NULL
;
281 static int acpi_i2c_notify(struct notifier_block
*nb
, unsigned long value
,
284 struct acpi_device
*adev
= arg
;
285 struct i2c_board_info info
;
286 acpi_handle adapter_handle
;
287 struct i2c_adapter
*adapter
;
288 struct i2c_client
*client
;
291 case ACPI_RECONFIG_DEVICE_ADD
:
292 if (acpi_i2c_get_info(adev
, &info
, &adapter_handle
))
295 adapter
= acpi_i2c_find_adapter_by_handle(adapter_handle
);
299 acpi_i2c_register_device(adapter
, adev
, &info
);
301 case ACPI_RECONFIG_DEVICE_REMOVE
:
302 if (!acpi_device_enumerated(adev
))
305 client
= acpi_i2c_find_client_by_adev(adev
);
309 i2c_unregister_device(client
);
310 put_device(&client
->dev
);
317 static struct notifier_block i2c_acpi_notifier
= {
318 .notifier_call
= acpi_i2c_notify
,
320 #else /* CONFIG_ACPI */
321 static inline void acpi_i2c_register_devices(struct i2c_adapter
*adap
) { }
322 extern struct notifier_block i2c_acpi_notifier
;
323 #endif /* CONFIG_ACPI */
325 #ifdef CONFIG_ACPI_I2C_OPREGION
326 static int acpi_gsb_i2c_read_bytes(struct i2c_client
*client
,
327 u8 cmd
, u8
*data
, u8 data_len
)
330 struct i2c_msg msgs
[2];
334 buffer
= kzalloc(data_len
, GFP_KERNEL
);
338 msgs
[0].addr
= client
->addr
;
339 msgs
[0].flags
= client
->flags
;
343 msgs
[1].addr
= client
->addr
;
344 msgs
[1].flags
= client
->flags
| I2C_M_RD
;
345 msgs
[1].len
= data_len
;
346 msgs
[1].buf
= buffer
;
348 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
350 dev_err(&client
->adapter
->dev
, "i2c read failed\n");
352 memcpy(data
, buffer
, data_len
);
358 static int acpi_gsb_i2c_write_bytes(struct i2c_client
*client
,
359 u8 cmd
, u8
*data
, u8 data_len
)
362 struct i2c_msg msgs
[1];
366 buffer
= kzalloc(data_len
+ 1, GFP_KERNEL
);
371 memcpy(buffer
+ 1, data
, data_len
);
373 msgs
[0].addr
= client
->addr
;
374 msgs
[0].flags
= client
->flags
;
375 msgs
[0].len
= data_len
+ 1;
376 msgs
[0].buf
= buffer
;
378 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
380 dev_err(&client
->adapter
->dev
, "i2c write failed\n");
387 acpi_i2c_space_handler(u32 function
, acpi_physical_address command
,
388 u32 bits
, u64
*value64
,
389 void *handler_context
, void *region_context
)
391 struct gsb_buffer
*gsb
= (struct gsb_buffer
*)value64
;
392 struct acpi_i2c_handler_data
*data
= handler_context
;
393 struct acpi_connection_info
*info
= &data
->info
;
394 struct acpi_resource_i2c_serialbus
*sb
;
395 struct i2c_adapter
*adapter
= data
->adapter
;
396 struct i2c_client
*client
;
397 struct acpi_resource
*ares
;
398 u32 accessor_type
= function
>> 16;
399 u8 action
= function
& ACPI_IO_MASK
;
403 ret
= acpi_buffer_to_resource(info
->connection
, info
->length
, &ares
);
404 if (ACPI_FAILURE(ret
))
407 client
= kzalloc(sizeof(*client
), GFP_KERNEL
);
413 if (!value64
|| ares
->type
!= ACPI_RESOURCE_TYPE_SERIAL_BUS
) {
414 ret
= AE_BAD_PARAMETER
;
418 sb
= &ares
->data
.i2c_serial_bus
;
419 if (sb
->type
!= ACPI_RESOURCE_SERIAL_TYPE_I2C
) {
420 ret
= AE_BAD_PARAMETER
;
424 client
->adapter
= adapter
;
425 client
->addr
= sb
->slave_address
;
427 if (sb
->access_mode
== ACPI_I2C_10BIT_MODE
)
428 client
->flags
|= I2C_CLIENT_TEN
;
430 switch (accessor_type
) {
431 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV
:
432 if (action
== ACPI_READ
) {
433 status
= i2c_smbus_read_byte(client
);
439 status
= i2c_smbus_write_byte(client
, gsb
->bdata
);
443 case ACPI_GSB_ACCESS_ATTRIB_BYTE
:
444 if (action
== ACPI_READ
) {
445 status
= i2c_smbus_read_byte_data(client
, command
);
451 status
= i2c_smbus_write_byte_data(client
, command
,
456 case ACPI_GSB_ACCESS_ATTRIB_WORD
:
457 if (action
== ACPI_READ
) {
458 status
= i2c_smbus_read_word_data(client
, command
);
464 status
= i2c_smbus_write_word_data(client
, command
,
469 case ACPI_GSB_ACCESS_ATTRIB_BLOCK
:
470 if (action
== ACPI_READ
) {
471 status
= i2c_smbus_read_block_data(client
, command
,
478 status
= i2c_smbus_write_block_data(client
, command
,
479 gsb
->len
, gsb
->data
);
483 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE
:
484 if (action
== ACPI_READ
) {
485 status
= acpi_gsb_i2c_read_bytes(client
, command
,
486 gsb
->data
, info
->access_length
);
490 status
= acpi_gsb_i2c_write_bytes(client
, command
,
491 gsb
->data
, info
->access_length
);
496 pr_info("protocol(0x%02x) is not supported.\n", accessor_type
);
497 ret
= AE_BAD_PARAMETER
;
501 gsb
->status
= status
;
510 static int acpi_i2c_install_space_handler(struct i2c_adapter
*adapter
)
513 struct acpi_i2c_handler_data
*data
;
516 if (!adapter
->dev
.parent
)
519 handle
= ACPI_HANDLE(adapter
->dev
.parent
);
524 data
= kzalloc(sizeof(struct acpi_i2c_handler_data
),
529 data
->adapter
= adapter
;
530 status
= acpi_bus_attach_private_data(handle
, (void *)data
);
531 if (ACPI_FAILURE(status
)) {
536 status
= acpi_install_address_space_handler(handle
,
537 ACPI_ADR_SPACE_GSBUS
,
538 &acpi_i2c_space_handler
,
541 if (ACPI_FAILURE(status
)) {
542 dev_err(&adapter
->dev
, "Error installing i2c space handler\n");
543 acpi_bus_detach_private_data(handle
);
548 acpi_walk_dep_device_list(handle
);
552 static void acpi_i2c_remove_space_handler(struct i2c_adapter
*adapter
)
555 struct acpi_i2c_handler_data
*data
;
558 if (!adapter
->dev
.parent
)
561 handle
= ACPI_HANDLE(adapter
->dev
.parent
);
566 acpi_remove_address_space_handler(handle
,
567 ACPI_ADR_SPACE_GSBUS
,
568 &acpi_i2c_space_handler
);
570 status
= acpi_bus_get_private_data(handle
, (void **)&data
);
571 if (ACPI_SUCCESS(status
))
574 acpi_bus_detach_private_data(handle
);
576 #else /* CONFIG_ACPI_I2C_OPREGION */
577 static inline void acpi_i2c_remove_space_handler(struct i2c_adapter
*adapter
)
580 static inline int acpi_i2c_install_space_handler(struct i2c_adapter
*adapter
)
582 #endif /* CONFIG_ACPI_I2C_OPREGION */
584 /* ------------------------------------------------------------------------- */
586 static const struct i2c_device_id
*i2c_match_id(const struct i2c_device_id
*id
,
587 const struct i2c_client
*client
)
589 while (id
->name
[0]) {
590 if (strcmp(client
->name
, id
->name
) == 0)
597 static int i2c_device_match(struct device
*dev
, struct device_driver
*drv
)
599 struct i2c_client
*client
= i2c_verify_client(dev
);
600 struct i2c_driver
*driver
;
605 /* Attempt an OF style match */
606 if (of_driver_match_device(dev
, drv
))
609 /* Then ACPI style match */
610 if (acpi_driver_match_device(dev
, drv
))
613 driver
= to_i2c_driver(drv
);
614 /* match on an id table if there is one */
615 if (driver
->id_table
)
616 return i2c_match_id(driver
->id_table
, client
) != NULL
;
621 static int i2c_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
623 struct i2c_client
*client
= to_i2c_client(dev
);
626 rc
= acpi_device_uevent_modalias(dev
, env
);
630 return add_uevent_var(env
, "MODALIAS=%s%s", I2C_MODULE_PREFIX
, client
->name
);
633 /* i2c bus recovery routines */
634 static int get_scl_gpio_value(struct i2c_adapter
*adap
)
636 return gpio_get_value(adap
->bus_recovery_info
->scl_gpio
);
639 static void set_scl_gpio_value(struct i2c_adapter
*adap
, int val
)
641 gpio_set_value(adap
->bus_recovery_info
->scl_gpio
, val
);
644 static int get_sda_gpio_value(struct i2c_adapter
*adap
)
646 return gpio_get_value(adap
->bus_recovery_info
->sda_gpio
);
649 static int i2c_get_gpios_for_recovery(struct i2c_adapter
*adap
)
651 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
652 struct device
*dev
= &adap
->dev
;
655 ret
= gpio_request_one(bri
->scl_gpio
, GPIOF_OPEN_DRAIN
|
656 GPIOF_OUT_INIT_HIGH
, "i2c-scl");
658 dev_warn(dev
, "Can't get SCL gpio: %d\n", bri
->scl_gpio
);
663 if (gpio_request_one(bri
->sda_gpio
, GPIOF_IN
, "i2c-sda")) {
664 /* work without SDA polling */
665 dev_warn(dev
, "Can't get SDA gpio: %d. Not using SDA polling\n",
674 static void i2c_put_gpios_for_recovery(struct i2c_adapter
*adap
)
676 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
679 gpio_free(bri
->sda_gpio
);
681 gpio_free(bri
->scl_gpio
);
685 * We are generating clock pulses. ndelay() determines durating of clk pulses.
686 * We will generate clock with rate 100 KHz and so duration of both clock levels
687 * is: delay in ns = (10^6 / 100) / 2
689 #define RECOVERY_NDELAY 5000
690 #define RECOVERY_CLK_CNT 9
692 static int i2c_generic_recovery(struct i2c_adapter
*adap
)
694 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
695 int i
= 0, val
= 1, ret
= 0;
697 if (bri
->prepare_recovery
)
698 bri
->prepare_recovery(adap
);
700 bri
->set_scl(adap
, val
);
701 ndelay(RECOVERY_NDELAY
);
704 * By this time SCL is high, as we need to give 9 falling-rising edges
706 while (i
++ < RECOVERY_CLK_CNT
* 2) {
708 /* Break if SDA is high */
709 if (bri
->get_sda
&& bri
->get_sda(adap
))
711 /* SCL shouldn't be low here */
712 if (!bri
->get_scl(adap
)) {
714 "SCL is stuck low, exit recovery\n");
721 bri
->set_scl(adap
, val
);
722 ndelay(RECOVERY_NDELAY
);
725 if (bri
->unprepare_recovery
)
726 bri
->unprepare_recovery(adap
);
731 int i2c_generic_scl_recovery(struct i2c_adapter
*adap
)
733 return i2c_generic_recovery(adap
);
735 EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery
);
737 int i2c_generic_gpio_recovery(struct i2c_adapter
*adap
)
741 ret
= i2c_get_gpios_for_recovery(adap
);
745 ret
= i2c_generic_recovery(adap
);
746 i2c_put_gpios_for_recovery(adap
);
750 EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery
);
752 int i2c_recover_bus(struct i2c_adapter
*adap
)
754 if (!adap
->bus_recovery_info
)
757 dev_dbg(&adap
->dev
, "Trying i2c bus recovery\n");
758 return adap
->bus_recovery_info
->recover_bus(adap
);
760 EXPORT_SYMBOL_GPL(i2c_recover_bus
);
762 static int i2c_device_probe(struct device
*dev
)
764 struct i2c_client
*client
= i2c_verify_client(dev
);
765 struct i2c_driver
*driver
;
775 irq
= of_irq_get_byname(dev
->of_node
, "irq");
776 if (irq
== -EINVAL
|| irq
== -ENODATA
)
777 irq
= of_irq_get(dev
->of_node
, 0);
778 } else if (ACPI_COMPANION(dev
)) {
779 irq
= acpi_dev_gpio_irq_get(ACPI_COMPANION(dev
), 0);
781 if (irq
== -EPROBE_DEFER
)
789 driver
= to_i2c_driver(dev
->driver
);
790 if (!driver
->probe
|| !driver
->id_table
)
793 if (client
->flags
& I2C_CLIENT_WAKE
) {
794 int wakeirq
= -ENOENT
;
797 wakeirq
= of_irq_get_byname(dev
->of_node
, "wakeup");
798 if (wakeirq
== -EPROBE_DEFER
)
802 device_init_wakeup(&client
->dev
, true);
804 if (wakeirq
> 0 && wakeirq
!= client
->irq
)
805 status
= dev_pm_set_dedicated_wake_irq(dev
, wakeirq
);
806 else if (client
->irq
> 0)
807 status
= dev_pm_set_wake_irq(dev
, client
->irq
);
812 dev_warn(&client
->dev
, "failed to set up wakeup irq");
815 dev_dbg(dev
, "probe\n");
817 status
= of_clk_set_defaults(dev
->of_node
, false);
819 goto err_clear_wakeup_irq
;
821 status
= dev_pm_domain_attach(&client
->dev
, true);
822 if (status
== -EPROBE_DEFER
)
823 goto err_clear_wakeup_irq
;
825 status
= driver
->probe(client
, i2c_match_id(driver
->id_table
, client
));
827 goto err_detach_pm_domain
;
831 err_detach_pm_domain
:
832 dev_pm_domain_detach(&client
->dev
, true);
833 err_clear_wakeup_irq
:
834 dev_pm_clear_wake_irq(&client
->dev
);
835 device_init_wakeup(&client
->dev
, false);
839 static int i2c_device_remove(struct device
*dev
)
841 struct i2c_client
*client
= i2c_verify_client(dev
);
842 struct i2c_driver
*driver
;
845 if (!client
|| !dev
->driver
)
848 driver
= to_i2c_driver(dev
->driver
);
849 if (driver
->remove
) {
850 dev_dbg(dev
, "remove\n");
851 status
= driver
->remove(client
);
854 dev_pm_domain_detach(&client
->dev
, true);
856 dev_pm_clear_wake_irq(&client
->dev
);
857 device_init_wakeup(&client
->dev
, false);
862 static void i2c_device_shutdown(struct device
*dev
)
864 struct i2c_client
*client
= i2c_verify_client(dev
);
865 struct i2c_driver
*driver
;
867 if (!client
|| !dev
->driver
)
869 driver
= to_i2c_driver(dev
->driver
);
870 if (driver
->shutdown
)
871 driver
->shutdown(client
);
874 static void i2c_client_dev_release(struct device
*dev
)
876 kfree(to_i2c_client(dev
));
880 show_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
882 return sprintf(buf
, "%s\n", dev
->type
== &i2c_client_type
?
883 to_i2c_client(dev
)->name
: to_i2c_adapter(dev
)->name
);
885 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
888 show_modalias(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
890 struct i2c_client
*client
= to_i2c_client(dev
);
893 len
= acpi_device_modalias(dev
, buf
, PAGE_SIZE
-1);
897 return sprintf(buf
, "%s%s\n", I2C_MODULE_PREFIX
, client
->name
);
899 static DEVICE_ATTR(modalias
, S_IRUGO
, show_modalias
, NULL
);
901 static struct attribute
*i2c_dev_attrs
[] = {
903 /* modalias helps coldplug: modprobe $(cat .../modalias) */
904 &dev_attr_modalias
.attr
,
907 ATTRIBUTE_GROUPS(i2c_dev
);
909 struct bus_type i2c_bus_type
= {
911 .match
= i2c_device_match
,
912 .probe
= i2c_device_probe
,
913 .remove
= i2c_device_remove
,
914 .shutdown
= i2c_device_shutdown
,
916 EXPORT_SYMBOL_GPL(i2c_bus_type
);
918 static struct device_type i2c_client_type
= {
919 .groups
= i2c_dev_groups
,
920 .uevent
= i2c_device_uevent
,
921 .release
= i2c_client_dev_release
,
926 * i2c_verify_client - return parameter as i2c_client, or NULL
927 * @dev: device, probably from some driver model iterator
929 * When traversing the driver model tree, perhaps using driver model
930 * iterators like @device_for_each_child(), you can't assume very much
931 * about the nodes you find. Use this function to avoid oopses caused
932 * by wrongly treating some non-I2C device as an i2c_client.
934 struct i2c_client
*i2c_verify_client(struct device
*dev
)
936 return (dev
->type
== &i2c_client_type
)
940 EXPORT_SYMBOL(i2c_verify_client
);
943 /* Return a unique address which takes the flags of the client into account */
944 static unsigned short i2c_encode_flags_to_addr(struct i2c_client
*client
)
946 unsigned short addr
= client
->addr
;
948 /* For some client flags, add an arbitrary offset to avoid collisions */
949 if (client
->flags
& I2C_CLIENT_TEN
)
950 addr
|= I2C_ADDR_OFFSET_TEN_BIT
;
952 if (client
->flags
& I2C_CLIENT_SLAVE
)
953 addr
|= I2C_ADDR_OFFSET_SLAVE
;
958 /* This is a permissive address validity check, I2C address map constraints
959 * are purposely not enforced, except for the general call address. */
960 static int i2c_check_addr_validity(unsigned addr
, unsigned short flags
)
962 if (flags
& I2C_CLIENT_TEN
) {
963 /* 10-bit address, all values are valid */
967 /* 7-bit address, reject the general call address */
968 if (addr
== 0x00 || addr
> 0x7f)
974 /* And this is a strict address validity check, used when probing. If a
975 * device uses a reserved address, then it shouldn't be probed. 7-bit
976 * addressing is assumed, 10-bit address devices are rare and should be
977 * explicitly enumerated. */
978 static int i2c_check_7bit_addr_validity_strict(unsigned short addr
)
981 * Reserved addresses per I2C specification:
982 * 0x00 General call address / START byte
984 * 0x02 Reserved for different bus format
985 * 0x03 Reserved for future purposes
986 * 0x04-0x07 Hs-mode master code
987 * 0x78-0x7b 10-bit slave addressing
988 * 0x7c-0x7f Reserved for future purposes
990 if (addr
< 0x08 || addr
> 0x77)
995 static int __i2c_check_addr_busy(struct device
*dev
, void *addrp
)
997 struct i2c_client
*client
= i2c_verify_client(dev
);
998 int addr
= *(int *)addrp
;
1000 if (client
&& i2c_encode_flags_to_addr(client
) == addr
)
1005 /* walk up mux tree */
1006 static int i2c_check_mux_parents(struct i2c_adapter
*adapter
, int addr
)
1008 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
1011 result
= device_for_each_child(&adapter
->dev
, &addr
,
1012 __i2c_check_addr_busy
);
1014 if (!result
&& parent
)
1015 result
= i2c_check_mux_parents(parent
, addr
);
1020 /* recurse down mux tree */
1021 static int i2c_check_mux_children(struct device
*dev
, void *addrp
)
1025 if (dev
->type
== &i2c_adapter_type
)
1026 result
= device_for_each_child(dev
, addrp
,
1027 i2c_check_mux_children
);
1029 result
= __i2c_check_addr_busy(dev
, addrp
);
1034 static int i2c_check_addr_busy(struct i2c_adapter
*adapter
, int addr
)
1036 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
1040 result
= i2c_check_mux_parents(parent
, addr
);
1043 result
= device_for_each_child(&adapter
->dev
, &addr
,
1044 i2c_check_mux_children
);
1050 * i2c_adapter_lock_bus - Get exclusive access to an I2C bus segment
1051 * @adapter: Target I2C bus segment
1052 * @flags: I2C_LOCK_ROOT_ADAPTER locks the root i2c adapter, I2C_LOCK_SEGMENT
1053 * locks only this branch in the adapter tree
1055 static void i2c_adapter_lock_bus(struct i2c_adapter
*adapter
,
1058 rt_mutex_lock(&adapter
->bus_lock
);
1062 * i2c_adapter_trylock_bus - Try to get exclusive access to an I2C bus segment
1063 * @adapter: Target I2C bus segment
1064 * @flags: I2C_LOCK_ROOT_ADAPTER trylocks the root i2c adapter, I2C_LOCK_SEGMENT
1065 * trylocks only this branch in the adapter tree
1067 static int i2c_adapter_trylock_bus(struct i2c_adapter
*adapter
,
1070 return rt_mutex_trylock(&adapter
->bus_lock
);
1074 * i2c_adapter_unlock_bus - Release exclusive access to an I2C bus segment
1075 * @adapter: Target I2C bus segment
1076 * @flags: I2C_LOCK_ROOT_ADAPTER unlocks the root i2c adapter, I2C_LOCK_SEGMENT
1077 * unlocks only this branch in the adapter tree
1079 static void i2c_adapter_unlock_bus(struct i2c_adapter
*adapter
,
1082 rt_mutex_unlock(&adapter
->bus_lock
);
1085 static void i2c_dev_set_name(struct i2c_adapter
*adap
,
1086 struct i2c_client
*client
)
1088 struct acpi_device
*adev
= ACPI_COMPANION(&client
->dev
);
1091 dev_set_name(&client
->dev
, "i2c-%s", acpi_dev_name(adev
));
1095 dev_set_name(&client
->dev
, "%d-%04x", i2c_adapter_id(adap
),
1096 i2c_encode_flags_to_addr(client
));
1100 * i2c_new_device - instantiate an i2c device
1101 * @adap: the adapter managing the device
1102 * @info: describes one I2C device; bus_num is ignored
1103 * Context: can sleep
1105 * Create an i2c device. Binding is handled through driver model
1106 * probe()/remove() methods. A driver may be bound to this device when we
1107 * return from this function, or any later moment (e.g. maybe hotplugging will
1108 * load the driver module). This call is not appropriate for use by mainboard
1109 * initialization logic, which usually runs during an arch_initcall() long
1110 * before any i2c_adapter could exist.
1112 * This returns the new i2c client, which may be saved for later use with
1113 * i2c_unregister_device(); or NULL to indicate an error.
1116 i2c_new_device(struct i2c_adapter
*adap
, struct i2c_board_info
const *info
)
1118 struct i2c_client
*client
;
1121 client
= kzalloc(sizeof *client
, GFP_KERNEL
);
1125 client
->adapter
= adap
;
1127 client
->dev
.platform_data
= info
->platform_data
;
1130 client
->dev
.archdata
= *info
->archdata
;
1132 client
->flags
= info
->flags
;
1133 client
->addr
= info
->addr
;
1134 client
->irq
= info
->irq
;
1136 strlcpy(client
->name
, info
->type
, sizeof(client
->name
));
1138 status
= i2c_check_addr_validity(client
->addr
, client
->flags
);
1140 dev_err(&adap
->dev
, "Invalid %d-bit I2C address 0x%02hx\n",
1141 client
->flags
& I2C_CLIENT_TEN
? 10 : 7, client
->addr
);
1142 goto out_err_silent
;
1145 /* Check for address business */
1146 status
= i2c_check_addr_busy(adap
, i2c_encode_flags_to_addr(client
));
1150 client
->dev
.parent
= &client
->adapter
->dev
;
1151 client
->dev
.bus
= &i2c_bus_type
;
1152 client
->dev
.type
= &i2c_client_type
;
1153 client
->dev
.of_node
= info
->of_node
;
1154 client
->dev
.fwnode
= info
->fwnode
;
1156 i2c_dev_set_name(adap
, client
);
1157 status
= device_register(&client
->dev
);
1161 dev_dbg(&adap
->dev
, "client [%s] registered with bus id %s\n",
1162 client
->name
, dev_name(&client
->dev
));
1167 dev_err(&adap
->dev
, "Failed to register i2c client %s at 0x%02x "
1168 "(%d)\n", client
->name
, client
->addr
, status
);
1173 EXPORT_SYMBOL_GPL(i2c_new_device
);
1177 * i2c_unregister_device - reverse effect of i2c_new_device()
1178 * @client: value returned from i2c_new_device()
1179 * Context: can sleep
1181 void i2c_unregister_device(struct i2c_client
*client
)
1183 if (client
->dev
.of_node
)
1184 of_node_clear_flag(client
->dev
.of_node
, OF_POPULATED
);
1185 if (ACPI_COMPANION(&client
->dev
))
1186 acpi_device_clear_enumerated(ACPI_COMPANION(&client
->dev
));
1187 device_unregister(&client
->dev
);
1189 EXPORT_SYMBOL_GPL(i2c_unregister_device
);
1192 static const struct i2c_device_id dummy_id
[] = {
1197 static int dummy_probe(struct i2c_client
*client
,
1198 const struct i2c_device_id
*id
)
1203 static int dummy_remove(struct i2c_client
*client
)
1208 static struct i2c_driver dummy_driver
= {
1209 .driver
.name
= "dummy",
1210 .probe
= dummy_probe
,
1211 .remove
= dummy_remove
,
1212 .id_table
= dummy_id
,
1216 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1217 * @adapter: the adapter managing the device
1218 * @address: seven bit address to be used
1219 * Context: can sleep
1221 * This returns an I2C client bound to the "dummy" driver, intended for use
1222 * with devices that consume multiple addresses. Examples of such chips
1223 * include various EEPROMS (like 24c04 and 24c08 models).
1225 * These dummy devices have two main uses. First, most I2C and SMBus calls
1226 * except i2c_transfer() need a client handle; the dummy will be that handle.
1227 * And second, this prevents the specified address from being bound to a
1230 * This returns the new i2c client, which should be saved for later use with
1231 * i2c_unregister_device(); or NULL to indicate an error.
1233 struct i2c_client
*i2c_new_dummy(struct i2c_adapter
*adapter
, u16 address
)
1235 struct i2c_board_info info
= {
1236 I2C_BOARD_INFO("dummy", address
),
1239 return i2c_new_device(adapter
, &info
);
1241 EXPORT_SYMBOL_GPL(i2c_new_dummy
);
1243 /* ------------------------------------------------------------------------- */
1245 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
1247 static void i2c_adapter_dev_release(struct device
*dev
)
1249 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
1250 complete(&adap
->dev_released
);
1254 * This function is only needed for mutex_lock_nested, so it is never
1255 * called unless locking correctness checking is enabled. Thus we
1256 * make it inline to avoid a compiler warning. That's what gcc ends up
1259 static inline unsigned int i2c_adapter_depth(struct i2c_adapter
*adapter
)
1261 unsigned int depth
= 0;
1263 while ((adapter
= i2c_parent_is_i2c_adapter(adapter
)))
1270 * Let users instantiate I2C devices through sysfs. This can be used when
1271 * platform initialization code doesn't contain the proper data for
1272 * whatever reason. Also useful for drivers that do device detection and
1273 * detection fails, either because the device uses an unexpected address,
1274 * or this is a compatible device with different ID register values.
1276 * Parameter checking may look overzealous, but we really don't want
1277 * the user to provide incorrect parameters.
1280 i2c_sysfs_new_device(struct device
*dev
, struct device_attribute
*attr
,
1281 const char *buf
, size_t count
)
1283 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
1284 struct i2c_board_info info
;
1285 struct i2c_client
*client
;
1289 memset(&info
, 0, sizeof(struct i2c_board_info
));
1291 blank
= strchr(buf
, ' ');
1293 dev_err(dev
, "%s: Missing parameters\n", "new_device");
1296 if (blank
- buf
> I2C_NAME_SIZE
- 1) {
1297 dev_err(dev
, "%s: Invalid device name\n", "new_device");
1300 memcpy(info
.type
, buf
, blank
- buf
);
1302 /* Parse remaining parameters, reject extra parameters */
1303 res
= sscanf(++blank
, "%hi%c", &info
.addr
, &end
);
1305 dev_err(dev
, "%s: Can't parse I2C address\n", "new_device");
1308 if (res
> 1 && end
!= '\n') {
1309 dev_err(dev
, "%s: Extra parameters\n", "new_device");
1313 if ((info
.addr
& I2C_ADDR_OFFSET_TEN_BIT
) == I2C_ADDR_OFFSET_TEN_BIT
) {
1314 info
.addr
&= ~I2C_ADDR_OFFSET_TEN_BIT
;
1315 info
.flags
|= I2C_CLIENT_TEN
;
1318 if (info
.addr
& I2C_ADDR_OFFSET_SLAVE
) {
1319 info
.addr
&= ~I2C_ADDR_OFFSET_SLAVE
;
1320 info
.flags
|= I2C_CLIENT_SLAVE
;
1323 client
= i2c_new_device(adap
, &info
);
1327 /* Keep track of the added device */
1328 mutex_lock(&adap
->userspace_clients_lock
);
1329 list_add_tail(&client
->detected
, &adap
->userspace_clients
);
1330 mutex_unlock(&adap
->userspace_clients_lock
);
1331 dev_info(dev
, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1332 info
.type
, info
.addr
);
1336 static DEVICE_ATTR(new_device
, S_IWUSR
, NULL
, i2c_sysfs_new_device
);
1339 * And of course let the users delete the devices they instantiated, if
1340 * they got it wrong. This interface can only be used to delete devices
1341 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1342 * don't delete devices to which some kernel code still has references.
1344 * Parameter checking may look overzealous, but we really don't want
1345 * the user to delete the wrong device.
1348 i2c_sysfs_delete_device(struct device
*dev
, struct device_attribute
*attr
,
1349 const char *buf
, size_t count
)
1351 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
1352 struct i2c_client
*client
, *next
;
1353 unsigned short addr
;
1357 /* Parse parameters, reject extra parameters */
1358 res
= sscanf(buf
, "%hi%c", &addr
, &end
);
1360 dev_err(dev
, "%s: Can't parse I2C address\n", "delete_device");
1363 if (res
> 1 && end
!= '\n') {
1364 dev_err(dev
, "%s: Extra parameters\n", "delete_device");
1368 /* Make sure the device was added through sysfs */
1370 mutex_lock_nested(&adap
->userspace_clients_lock
,
1371 i2c_adapter_depth(adap
));
1372 list_for_each_entry_safe(client
, next
, &adap
->userspace_clients
,
1374 if (i2c_encode_flags_to_addr(client
) == addr
) {
1375 dev_info(dev
, "%s: Deleting device %s at 0x%02hx\n",
1376 "delete_device", client
->name
, client
->addr
);
1378 list_del(&client
->detected
);
1379 i2c_unregister_device(client
);
1384 mutex_unlock(&adap
->userspace_clients_lock
);
1387 dev_err(dev
, "%s: Can't find device in list\n",
1391 static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device
, S_IWUSR
, NULL
,
1392 i2c_sysfs_delete_device
);
1394 static struct attribute
*i2c_adapter_attrs
[] = {
1395 &dev_attr_name
.attr
,
1396 &dev_attr_new_device
.attr
,
1397 &dev_attr_delete_device
.attr
,
1400 ATTRIBUTE_GROUPS(i2c_adapter
);
1402 struct device_type i2c_adapter_type
= {
1403 .groups
= i2c_adapter_groups
,
1404 .release
= i2c_adapter_dev_release
,
1406 EXPORT_SYMBOL_GPL(i2c_adapter_type
);
1409 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1410 * @dev: device, probably from some driver model iterator
1412 * When traversing the driver model tree, perhaps using driver model
1413 * iterators like @device_for_each_child(), you can't assume very much
1414 * about the nodes you find. Use this function to avoid oopses caused
1415 * by wrongly treating some non-I2C device as an i2c_adapter.
1417 struct i2c_adapter
*i2c_verify_adapter(struct device
*dev
)
1419 return (dev
->type
== &i2c_adapter_type
)
1420 ? to_i2c_adapter(dev
)
1423 EXPORT_SYMBOL(i2c_verify_adapter
);
1425 #ifdef CONFIG_I2C_COMPAT
1426 static struct class_compat
*i2c_adapter_compat_class
;
1429 static void i2c_scan_static_board_info(struct i2c_adapter
*adapter
)
1431 struct i2c_devinfo
*devinfo
;
1433 down_read(&__i2c_board_lock
);
1434 list_for_each_entry(devinfo
, &__i2c_board_list
, list
) {
1435 if (devinfo
->busnum
== adapter
->nr
1436 && !i2c_new_device(adapter
,
1437 &devinfo
->board_info
))
1438 dev_err(&adapter
->dev
,
1439 "Can't create device at 0x%02x\n",
1440 devinfo
->board_info
.addr
);
1442 up_read(&__i2c_board_lock
);
1445 /* OF support code */
1447 #if IS_ENABLED(CONFIG_OF)
1448 static struct i2c_client
*of_i2c_register_device(struct i2c_adapter
*adap
,
1449 struct device_node
*node
)
1451 struct i2c_client
*result
;
1452 struct i2c_board_info info
= {};
1453 struct dev_archdata dev_ad
= {};
1454 const __be32
*addr_be
;
1458 dev_dbg(&adap
->dev
, "of_i2c: register %s\n", node
->full_name
);
1460 if (of_modalias_node(node
, info
.type
, sizeof(info
.type
)) < 0) {
1461 dev_err(&adap
->dev
, "of_i2c: modalias failure on %s\n",
1463 return ERR_PTR(-EINVAL
);
1466 addr_be
= of_get_property(node
, "reg", &len
);
1467 if (!addr_be
|| (len
< sizeof(*addr_be
))) {
1468 dev_err(&adap
->dev
, "of_i2c: invalid reg on %s\n",
1470 return ERR_PTR(-EINVAL
);
1473 addr
= be32_to_cpup(addr_be
);
1474 if (addr
& I2C_TEN_BIT_ADDRESS
) {
1475 addr
&= ~I2C_TEN_BIT_ADDRESS
;
1476 info
.flags
|= I2C_CLIENT_TEN
;
1479 if (addr
& I2C_OWN_SLAVE_ADDRESS
) {
1480 addr
&= ~I2C_OWN_SLAVE_ADDRESS
;
1481 info
.flags
|= I2C_CLIENT_SLAVE
;
1484 if (i2c_check_addr_validity(addr
, info
.flags
)) {
1485 dev_err(&adap
->dev
, "of_i2c: invalid addr=%x on %s\n",
1486 info
.addr
, node
->full_name
);
1487 return ERR_PTR(-EINVAL
);
1491 info
.of_node
= of_node_get(node
);
1492 info
.archdata
= &dev_ad
;
1494 if (of_get_property(node
, "wakeup-source", NULL
))
1495 info
.flags
|= I2C_CLIENT_WAKE
;
1497 result
= i2c_new_device(adap
, &info
);
1498 if (result
== NULL
) {
1499 dev_err(&adap
->dev
, "of_i2c: Failure registering %s\n",
1502 return ERR_PTR(-EINVAL
);
1507 static void of_i2c_register_devices(struct i2c_adapter
*adap
)
1509 struct device_node
*node
;
1511 /* Only register child devices if the adapter has a node pointer set */
1512 if (!adap
->dev
.of_node
)
1515 dev_dbg(&adap
->dev
, "of_i2c: walking child nodes\n");
1517 for_each_available_child_of_node(adap
->dev
.of_node
, node
) {
1518 if (of_node_test_and_set_flag(node
, OF_POPULATED
))
1520 of_i2c_register_device(adap
, node
);
1524 static int of_dev_node_match(struct device
*dev
, void *data
)
1526 return dev
->of_node
== data
;
1529 /* must call put_device() when done with returned i2c_client device */
1530 struct i2c_client
*of_find_i2c_device_by_node(struct device_node
*node
)
1533 struct i2c_client
*client
;
1535 dev
= bus_find_device(&i2c_bus_type
, NULL
, node
, of_dev_node_match
);
1539 client
= i2c_verify_client(dev
);
1545 EXPORT_SYMBOL(of_find_i2c_device_by_node
);
1547 /* must call put_device() when done with returned i2c_adapter device */
1548 struct i2c_adapter
*of_find_i2c_adapter_by_node(struct device_node
*node
)
1551 struct i2c_adapter
*adapter
;
1553 dev
= bus_find_device(&i2c_bus_type
, NULL
, node
, of_dev_node_match
);
1557 adapter
= i2c_verify_adapter(dev
);
1563 EXPORT_SYMBOL(of_find_i2c_adapter_by_node
);
1565 /* must call i2c_put_adapter() when done with returned i2c_adapter device */
1566 struct i2c_adapter
*of_get_i2c_adapter_by_node(struct device_node
*node
)
1568 struct i2c_adapter
*adapter
;
1570 adapter
= of_find_i2c_adapter_by_node(node
);
1574 if (!try_module_get(adapter
->owner
)) {
1575 put_device(&adapter
->dev
);
1581 EXPORT_SYMBOL(of_get_i2c_adapter_by_node
);
1583 static void of_i2c_register_devices(struct i2c_adapter
*adap
) { }
1584 #endif /* CONFIG_OF */
1586 static int i2c_do_add_adapter(struct i2c_driver
*driver
,
1587 struct i2c_adapter
*adap
)
1589 /* Detect supported devices on that bus, and instantiate them */
1590 i2c_detect(adap
, driver
);
1592 /* Let legacy drivers scan this bus for matching devices */
1593 if (driver
->attach_adapter
) {
1594 dev_warn(&adap
->dev
, "%s: attach_adapter method is deprecated\n",
1595 driver
->driver
.name
);
1596 dev_warn(&adap
->dev
, "Please use another way to instantiate "
1597 "your i2c_client\n");
1598 /* We ignore the return code; if it fails, too bad */
1599 driver
->attach_adapter(adap
);
1604 static int __process_new_adapter(struct device_driver
*d
, void *data
)
1606 return i2c_do_add_adapter(to_i2c_driver(d
), data
);
1609 static int i2c_register_adapter(struct i2c_adapter
*adap
)
1613 /* Can't register until after driver model init */
1614 if (WARN_ON(!is_registered
)) {
1620 if (unlikely(adap
->name
[0] == '\0')) {
1621 pr_err("i2c-core: Attempt to register an adapter with "
1625 if (unlikely(!adap
->algo
)) {
1626 pr_err("i2c-core: Attempt to register adapter '%s' with "
1627 "no algo!\n", adap
->name
);
1631 if (!adap
->lock_bus
) {
1632 adap
->lock_bus
= i2c_adapter_lock_bus
;
1633 adap
->trylock_bus
= i2c_adapter_trylock_bus
;
1634 adap
->unlock_bus
= i2c_adapter_unlock_bus
;
1637 rt_mutex_init(&adap
->bus_lock
);
1638 rt_mutex_init(&adap
->mux_lock
);
1639 mutex_init(&adap
->userspace_clients_lock
);
1640 INIT_LIST_HEAD(&adap
->userspace_clients
);
1642 /* Set default timeout to 1 second if not already set */
1643 if (adap
->timeout
== 0)
1646 dev_set_name(&adap
->dev
, "i2c-%d", adap
->nr
);
1647 adap
->dev
.bus
= &i2c_bus_type
;
1648 adap
->dev
.type
= &i2c_adapter_type
;
1649 res
= device_register(&adap
->dev
);
1653 dev_dbg(&adap
->dev
, "adapter [%s] registered\n", adap
->name
);
1655 pm_runtime_no_callbacks(&adap
->dev
);
1656 pm_suspend_ignore_children(&adap
->dev
, true);
1657 pm_runtime_enable(&adap
->dev
);
1659 #ifdef CONFIG_I2C_COMPAT
1660 res
= class_compat_create_link(i2c_adapter_compat_class
, &adap
->dev
,
1663 dev_warn(&adap
->dev
,
1664 "Failed to create compatibility class link\n");
1667 /* bus recovery specific initialization */
1668 if (adap
->bus_recovery_info
) {
1669 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
1671 if (!bri
->recover_bus
) {
1672 dev_err(&adap
->dev
, "No recover_bus() found, not using recovery\n");
1673 adap
->bus_recovery_info
= NULL
;
1677 /* Generic GPIO recovery */
1678 if (bri
->recover_bus
== i2c_generic_gpio_recovery
) {
1679 if (!gpio_is_valid(bri
->scl_gpio
)) {
1680 dev_err(&adap
->dev
, "Invalid SCL gpio, not using recovery\n");
1681 adap
->bus_recovery_info
= NULL
;
1685 if (gpio_is_valid(bri
->sda_gpio
))
1686 bri
->get_sda
= get_sda_gpio_value
;
1688 bri
->get_sda
= NULL
;
1690 bri
->get_scl
= get_scl_gpio_value
;
1691 bri
->set_scl
= set_scl_gpio_value
;
1692 } else if (bri
->recover_bus
== i2c_generic_scl_recovery
) {
1693 /* Generic SCL recovery */
1694 if (!bri
->set_scl
|| !bri
->get_scl
) {
1695 dev_err(&adap
->dev
, "No {get|set}_scl() found, not using recovery\n");
1696 adap
->bus_recovery_info
= NULL
;
1702 /* create pre-declared device nodes */
1703 of_i2c_register_devices(adap
);
1704 acpi_i2c_register_devices(adap
);
1705 acpi_i2c_install_space_handler(adap
);
1707 if (adap
->nr
< __i2c_first_dynamic_bus_num
)
1708 i2c_scan_static_board_info(adap
);
1710 /* Notify drivers */
1711 mutex_lock(&core_lock
);
1712 bus_for_each_drv(&i2c_bus_type
, NULL
, adap
, __process_new_adapter
);
1713 mutex_unlock(&core_lock
);
1718 mutex_lock(&core_lock
);
1719 idr_remove(&i2c_adapter_idr
, adap
->nr
);
1720 mutex_unlock(&core_lock
);
1725 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1726 * @adap: the adapter to register (with adap->nr initialized)
1727 * Context: can sleep
1729 * See i2c_add_numbered_adapter() for details.
1731 static int __i2c_add_numbered_adapter(struct i2c_adapter
*adap
)
1735 mutex_lock(&core_lock
);
1736 id
= idr_alloc(&i2c_adapter_idr
, adap
, adap
->nr
, adap
->nr
+ 1,
1738 mutex_unlock(&core_lock
);
1740 return id
== -ENOSPC
? -EBUSY
: id
;
1742 return i2c_register_adapter(adap
);
1746 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1747 * @adapter: the adapter to add
1748 * Context: can sleep
1750 * This routine is used to declare an I2C adapter when its bus number
1751 * doesn't matter or when its bus number is specified by an dt alias.
1752 * Examples of bases when the bus number doesn't matter: I2C adapters
1753 * dynamically added by USB links or PCI plugin cards.
1755 * When this returns zero, a new bus number was allocated and stored
1756 * in adap->nr, and the specified adapter became available for clients.
1757 * Otherwise, a negative errno value is returned.
1759 int i2c_add_adapter(struct i2c_adapter
*adapter
)
1761 struct device
*dev
= &adapter
->dev
;
1765 id
= of_alias_get_id(dev
->of_node
, "i2c");
1768 return __i2c_add_numbered_adapter(adapter
);
1772 mutex_lock(&core_lock
);
1773 id
= idr_alloc(&i2c_adapter_idr
, adapter
,
1774 __i2c_first_dynamic_bus_num
, 0, GFP_KERNEL
);
1775 mutex_unlock(&core_lock
);
1781 return i2c_register_adapter(adapter
);
1783 EXPORT_SYMBOL(i2c_add_adapter
);
1786 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1787 * @adap: the adapter to register (with adap->nr initialized)
1788 * Context: can sleep
1790 * This routine is used to declare an I2C adapter when its bus number
1791 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1792 * or otherwise built in to the system's mainboard, and where i2c_board_info
1793 * is used to properly configure I2C devices.
1795 * If the requested bus number is set to -1, then this function will behave
1796 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1798 * If no devices have pre-been declared for this bus, then be sure to
1799 * register the adapter before any dynamically allocated ones. Otherwise
1800 * the required bus ID may not be available.
1802 * When this returns zero, the specified adapter became available for
1803 * clients using the bus number provided in adap->nr. Also, the table
1804 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1805 * and the appropriate driver model device nodes are created. Otherwise, a
1806 * negative errno value is returned.
1808 int i2c_add_numbered_adapter(struct i2c_adapter
*adap
)
1810 if (adap
->nr
== -1) /* -1 means dynamically assign bus id */
1811 return i2c_add_adapter(adap
);
1813 return __i2c_add_numbered_adapter(adap
);
1815 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter
);
1817 static void i2c_do_del_adapter(struct i2c_driver
*driver
,
1818 struct i2c_adapter
*adapter
)
1820 struct i2c_client
*client
, *_n
;
1822 /* Remove the devices we created ourselves as the result of hardware
1823 * probing (using a driver's detect method) */
1824 list_for_each_entry_safe(client
, _n
, &driver
->clients
, detected
) {
1825 if (client
->adapter
== adapter
) {
1826 dev_dbg(&adapter
->dev
, "Removing %s at 0x%x\n",
1827 client
->name
, client
->addr
);
1828 list_del(&client
->detected
);
1829 i2c_unregister_device(client
);
1834 static int __unregister_client(struct device
*dev
, void *dummy
)
1836 struct i2c_client
*client
= i2c_verify_client(dev
);
1837 if (client
&& strcmp(client
->name
, "dummy"))
1838 i2c_unregister_device(client
);
1842 static int __unregister_dummy(struct device
*dev
, void *dummy
)
1844 struct i2c_client
*client
= i2c_verify_client(dev
);
1846 i2c_unregister_device(client
);
1850 static int __process_removed_adapter(struct device_driver
*d
, void *data
)
1852 i2c_do_del_adapter(to_i2c_driver(d
), data
);
1857 * i2c_del_adapter - unregister I2C adapter
1858 * @adap: the adapter being unregistered
1859 * Context: can sleep
1861 * This unregisters an I2C adapter which was previously registered
1862 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1864 void i2c_del_adapter(struct i2c_adapter
*adap
)
1866 struct i2c_adapter
*found
;
1867 struct i2c_client
*client
, *next
;
1869 /* First make sure that this adapter was ever added */
1870 mutex_lock(&core_lock
);
1871 found
= idr_find(&i2c_adapter_idr
, adap
->nr
);
1872 mutex_unlock(&core_lock
);
1873 if (found
!= adap
) {
1874 pr_debug("i2c-core: attempting to delete unregistered "
1875 "adapter [%s]\n", adap
->name
);
1879 acpi_i2c_remove_space_handler(adap
);
1880 /* Tell drivers about this removal */
1881 mutex_lock(&core_lock
);
1882 bus_for_each_drv(&i2c_bus_type
, NULL
, adap
,
1883 __process_removed_adapter
);
1884 mutex_unlock(&core_lock
);
1886 /* Remove devices instantiated from sysfs */
1887 mutex_lock_nested(&adap
->userspace_clients_lock
,
1888 i2c_adapter_depth(adap
));
1889 list_for_each_entry_safe(client
, next
, &adap
->userspace_clients
,
1891 dev_dbg(&adap
->dev
, "Removing %s at 0x%x\n", client
->name
,
1893 list_del(&client
->detected
);
1894 i2c_unregister_device(client
);
1896 mutex_unlock(&adap
->userspace_clients_lock
);
1898 /* Detach any active clients. This can't fail, thus we do not
1899 * check the returned value. This is a two-pass process, because
1900 * we can't remove the dummy devices during the first pass: they
1901 * could have been instantiated by real devices wishing to clean
1902 * them up properly, so we give them a chance to do that first. */
1903 device_for_each_child(&adap
->dev
, NULL
, __unregister_client
);
1904 device_for_each_child(&adap
->dev
, NULL
, __unregister_dummy
);
1906 #ifdef CONFIG_I2C_COMPAT
1907 class_compat_remove_link(i2c_adapter_compat_class
, &adap
->dev
,
1911 /* device name is gone after device_unregister */
1912 dev_dbg(&adap
->dev
, "adapter [%s] unregistered\n", adap
->name
);
1914 pm_runtime_disable(&adap
->dev
);
1916 /* wait until all references to the device are gone
1918 * FIXME: This is old code and should ideally be replaced by an
1919 * alternative which results in decoupling the lifetime of the struct
1920 * device from the i2c_adapter, like spi or netdev do. Any solution
1921 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
1923 init_completion(&adap
->dev_released
);
1924 device_unregister(&adap
->dev
);
1925 wait_for_completion(&adap
->dev_released
);
1928 mutex_lock(&core_lock
);
1929 idr_remove(&i2c_adapter_idr
, adap
->nr
);
1930 mutex_unlock(&core_lock
);
1932 /* Clear the device structure in case this adapter is ever going to be
1934 memset(&adap
->dev
, 0, sizeof(adap
->dev
));
1936 EXPORT_SYMBOL(i2c_del_adapter
);
1939 * i2c_parse_fw_timings - get I2C related timing parameters from firmware
1940 * @dev: The device to scan for I2C timing properties
1941 * @t: the i2c_timings struct to be filled with values
1942 * @use_defaults: bool to use sane defaults derived from the I2C specification
1943 * when properties are not found, otherwise use 0
1945 * Scan the device for the generic I2C properties describing timing parameters
1946 * for the signal and fill the given struct with the results. If a property was
1947 * not found and use_defaults was true, then maximum timings are assumed which
1948 * are derived from the I2C specification. If use_defaults is not used, the
1949 * results will be 0, so drivers can apply their own defaults later. The latter
1950 * is mainly intended for avoiding regressions of existing drivers which want
1951 * to switch to this function. New drivers almost always should use the defaults.
1954 void i2c_parse_fw_timings(struct device
*dev
, struct i2c_timings
*t
, bool use_defaults
)
1958 memset(t
, 0, sizeof(*t
));
1960 ret
= device_property_read_u32(dev
, "clock-frequency", &t
->bus_freq_hz
);
1961 if (ret
&& use_defaults
)
1962 t
->bus_freq_hz
= 100000;
1964 ret
= device_property_read_u32(dev
, "i2c-scl-rising-time-ns", &t
->scl_rise_ns
);
1965 if (ret
&& use_defaults
) {
1966 if (t
->bus_freq_hz
<= 100000)
1967 t
->scl_rise_ns
= 1000;
1968 else if (t
->bus_freq_hz
<= 400000)
1969 t
->scl_rise_ns
= 300;
1971 t
->scl_rise_ns
= 120;
1974 ret
= device_property_read_u32(dev
, "i2c-scl-falling-time-ns", &t
->scl_fall_ns
);
1975 if (ret
&& use_defaults
) {
1976 if (t
->bus_freq_hz
<= 400000)
1977 t
->scl_fall_ns
= 300;
1979 t
->scl_fall_ns
= 120;
1982 device_property_read_u32(dev
, "i2c-scl-internal-delay-ns", &t
->scl_int_delay_ns
);
1984 ret
= device_property_read_u32(dev
, "i2c-sda-falling-time-ns", &t
->sda_fall_ns
);
1985 if (ret
&& use_defaults
)
1986 t
->sda_fall_ns
= t
->scl_fall_ns
;
1988 EXPORT_SYMBOL_GPL(i2c_parse_fw_timings
);
1990 /* ------------------------------------------------------------------------- */
1992 int i2c_for_each_dev(void *data
, int (*fn
)(struct device
*, void *))
1996 mutex_lock(&core_lock
);
1997 res
= bus_for_each_dev(&i2c_bus_type
, NULL
, data
, fn
);
1998 mutex_unlock(&core_lock
);
2002 EXPORT_SYMBOL_GPL(i2c_for_each_dev
);
2004 static int __process_new_driver(struct device
*dev
, void *data
)
2006 if (dev
->type
!= &i2c_adapter_type
)
2008 return i2c_do_add_adapter(data
, to_i2c_adapter(dev
));
2012 * An i2c_driver is used with one or more i2c_client (device) nodes to access
2013 * i2c slave chips, on a bus instance associated with some i2c_adapter.
2016 int i2c_register_driver(struct module
*owner
, struct i2c_driver
*driver
)
2020 /* Can't register until after driver model init */
2021 if (WARN_ON(!is_registered
))
2024 /* add the driver to the list of i2c drivers in the driver core */
2025 driver
->driver
.owner
= owner
;
2026 driver
->driver
.bus
= &i2c_bus_type
;
2028 /* When registration returns, the driver core
2029 * will have called probe() for all matching-but-unbound devices.
2031 res
= driver_register(&driver
->driver
);
2035 pr_debug("i2c-core: driver [%s] registered\n", driver
->driver
.name
);
2037 INIT_LIST_HEAD(&driver
->clients
);
2038 /* Walk the adapters that are already present */
2039 i2c_for_each_dev(driver
, __process_new_driver
);
2043 EXPORT_SYMBOL(i2c_register_driver
);
2045 static int __process_removed_driver(struct device
*dev
, void *data
)
2047 if (dev
->type
== &i2c_adapter_type
)
2048 i2c_do_del_adapter(data
, to_i2c_adapter(dev
));
2053 * i2c_del_driver - unregister I2C driver
2054 * @driver: the driver being unregistered
2055 * Context: can sleep
2057 void i2c_del_driver(struct i2c_driver
*driver
)
2059 i2c_for_each_dev(driver
, __process_removed_driver
);
2061 driver_unregister(&driver
->driver
);
2062 pr_debug("i2c-core: driver [%s] unregistered\n", driver
->driver
.name
);
2064 EXPORT_SYMBOL(i2c_del_driver
);
2066 /* ------------------------------------------------------------------------- */
2069 * i2c_use_client - increments the reference count of the i2c client structure
2070 * @client: the client being referenced
2072 * Each live reference to a client should be refcounted. The driver model does
2073 * that automatically as part of driver binding, so that most drivers don't
2074 * need to do this explicitly: they hold a reference until they're unbound
2077 * A pointer to the client with the incremented reference counter is returned.
2079 struct i2c_client
*i2c_use_client(struct i2c_client
*client
)
2081 if (client
&& get_device(&client
->dev
))
2085 EXPORT_SYMBOL(i2c_use_client
);
2088 * i2c_release_client - release a use of the i2c client structure
2089 * @client: the client being no longer referenced
2091 * Must be called when a user of a client is finished with it.
2093 void i2c_release_client(struct i2c_client
*client
)
2096 put_device(&client
->dev
);
2098 EXPORT_SYMBOL(i2c_release_client
);
2100 struct i2c_cmd_arg
{
2105 static int i2c_cmd(struct device
*dev
, void *_arg
)
2107 struct i2c_client
*client
= i2c_verify_client(dev
);
2108 struct i2c_cmd_arg
*arg
= _arg
;
2109 struct i2c_driver
*driver
;
2111 if (!client
|| !client
->dev
.driver
)
2114 driver
= to_i2c_driver(client
->dev
.driver
);
2115 if (driver
->command
)
2116 driver
->command(client
, arg
->cmd
, arg
->arg
);
2120 void i2c_clients_command(struct i2c_adapter
*adap
, unsigned int cmd
, void *arg
)
2122 struct i2c_cmd_arg cmd_arg
;
2126 device_for_each_child(&adap
->dev
, &cmd_arg
, i2c_cmd
);
2128 EXPORT_SYMBOL(i2c_clients_command
);
2130 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
2131 static int of_i2c_notify(struct notifier_block
*nb
, unsigned long action
,
2134 struct of_reconfig_data
*rd
= arg
;
2135 struct i2c_adapter
*adap
;
2136 struct i2c_client
*client
;
2138 switch (of_reconfig_get_state_change(action
, rd
)) {
2139 case OF_RECONFIG_CHANGE_ADD
:
2140 adap
= of_find_i2c_adapter_by_node(rd
->dn
->parent
);
2142 return NOTIFY_OK
; /* not for us */
2144 if (of_node_test_and_set_flag(rd
->dn
, OF_POPULATED
)) {
2145 put_device(&adap
->dev
);
2149 client
= of_i2c_register_device(adap
, rd
->dn
);
2150 put_device(&adap
->dev
);
2152 if (IS_ERR(client
)) {
2153 pr_err("%s: failed to create for '%s'\n",
2154 __func__
, rd
->dn
->full_name
);
2155 return notifier_from_errno(PTR_ERR(client
));
2158 case OF_RECONFIG_CHANGE_REMOVE
:
2159 /* already depopulated? */
2160 if (!of_node_check_flag(rd
->dn
, OF_POPULATED
))
2163 /* find our device by node */
2164 client
= of_find_i2c_device_by_node(rd
->dn
);
2166 return NOTIFY_OK
; /* no? not meant for us */
2168 /* unregister takes one ref away */
2169 i2c_unregister_device(client
);
2171 /* and put the reference of the find */
2172 put_device(&client
->dev
);
2178 static struct notifier_block i2c_of_notifier
= {
2179 .notifier_call
= of_i2c_notify
,
2182 extern struct notifier_block i2c_of_notifier
;
2183 #endif /* CONFIG_OF_DYNAMIC */
2185 static int __init
i2c_init(void)
2189 retval
= of_alias_get_highest_id("i2c");
2191 down_write(&__i2c_board_lock
);
2192 if (retval
>= __i2c_first_dynamic_bus_num
)
2193 __i2c_first_dynamic_bus_num
= retval
+ 1;
2194 up_write(&__i2c_board_lock
);
2196 retval
= bus_register(&i2c_bus_type
);
2200 is_registered
= true;
2202 #ifdef CONFIG_I2C_COMPAT
2203 i2c_adapter_compat_class
= class_compat_register("i2c-adapter");
2204 if (!i2c_adapter_compat_class
) {
2209 retval
= i2c_add_driver(&dummy_driver
);
2213 if (IS_ENABLED(CONFIG_OF_DYNAMIC
))
2214 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier
));
2215 if (IS_ENABLED(CONFIG_ACPI
))
2216 WARN_ON(acpi_reconfig_notifier_register(&i2c_acpi_notifier
));
2221 #ifdef CONFIG_I2C_COMPAT
2222 class_compat_unregister(i2c_adapter_compat_class
);
2225 is_registered
= false;
2226 bus_unregister(&i2c_bus_type
);
2230 static void __exit
i2c_exit(void)
2232 if (IS_ENABLED(CONFIG_ACPI
))
2233 WARN_ON(acpi_reconfig_notifier_unregister(&i2c_acpi_notifier
));
2234 if (IS_ENABLED(CONFIG_OF_DYNAMIC
))
2235 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier
));
2236 i2c_del_driver(&dummy_driver
);
2237 #ifdef CONFIG_I2C_COMPAT
2238 class_compat_unregister(i2c_adapter_compat_class
);
2240 bus_unregister(&i2c_bus_type
);
2241 tracepoint_synchronize_unregister();
2244 /* We must initialize early, because some subsystems register i2c drivers
2245 * in subsys_initcall() code, but are linked (and initialized) before i2c.
2247 postcore_initcall(i2c_init
);
2248 module_exit(i2c_exit
);
2250 /* ----------------------------------------------------
2251 * the functional interface to the i2c busses.
2252 * ----------------------------------------------------
2255 /* Check if val is exceeding the quirk IFF quirk is non 0 */
2256 #define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2258 static int i2c_quirk_error(struct i2c_adapter
*adap
, struct i2c_msg
*msg
, char *err_msg
)
2260 dev_err_ratelimited(&adap
->dev
, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2261 err_msg
, msg
->addr
, msg
->len
,
2262 msg
->flags
& I2C_M_RD
? "read" : "write");
2266 static int i2c_check_for_quirks(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
2268 const struct i2c_adapter_quirks
*q
= adap
->quirks
;
2269 int max_num
= q
->max_num_msgs
, i
;
2270 bool do_len_check
= true;
2272 if (q
->flags
& I2C_AQ_COMB
) {
2275 /* special checks for combined messages */
2277 if (q
->flags
& I2C_AQ_COMB_WRITE_FIRST
&& msgs
[0].flags
& I2C_M_RD
)
2278 return i2c_quirk_error(adap
, &msgs
[0], "1st comb msg must be write");
2280 if (q
->flags
& I2C_AQ_COMB_READ_SECOND
&& !(msgs
[1].flags
& I2C_M_RD
))
2281 return i2c_quirk_error(adap
, &msgs
[1], "2nd comb msg must be read");
2283 if (q
->flags
& I2C_AQ_COMB_SAME_ADDR
&& msgs
[0].addr
!= msgs
[1].addr
)
2284 return i2c_quirk_error(adap
, &msgs
[0], "comb msg only to same addr");
2286 if (i2c_quirk_exceeded(msgs
[0].len
, q
->max_comb_1st_msg_len
))
2287 return i2c_quirk_error(adap
, &msgs
[0], "msg too long");
2289 if (i2c_quirk_exceeded(msgs
[1].len
, q
->max_comb_2nd_msg_len
))
2290 return i2c_quirk_error(adap
, &msgs
[1], "msg too long");
2292 do_len_check
= false;
2296 if (i2c_quirk_exceeded(num
, max_num
))
2297 return i2c_quirk_error(adap
, &msgs
[0], "too many messages");
2299 for (i
= 0; i
< num
; i
++) {
2300 u16 len
= msgs
[i
].len
;
2302 if (msgs
[i
].flags
& I2C_M_RD
) {
2303 if (do_len_check
&& i2c_quirk_exceeded(len
, q
->max_read_len
))
2304 return i2c_quirk_error(adap
, &msgs
[i
], "msg too long");
2306 if (do_len_check
&& i2c_quirk_exceeded(len
, q
->max_write_len
))
2307 return i2c_quirk_error(adap
, &msgs
[i
], "msg too long");
2315 * __i2c_transfer - unlocked flavor of i2c_transfer
2316 * @adap: Handle to I2C bus
2317 * @msgs: One or more messages to execute before STOP is issued to
2318 * terminate the operation; each message begins with a START.
2319 * @num: Number of messages to be executed.
2321 * Returns negative errno, else the number of messages executed.
2323 * Adapter lock must be held when calling this function. No debug logging
2324 * takes place. adap->algo->master_xfer existence isn't checked.
2326 int __i2c_transfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
2328 unsigned long orig_jiffies
;
2331 if (adap
->quirks
&& i2c_check_for_quirks(adap
, msgs
, num
))
2334 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2335 * enabled. This is an efficient way of keeping the for-loop from
2336 * being executed when not needed.
2338 if (static_key_false(&i2c_trace_msg
)) {
2340 for (i
= 0; i
< num
; i
++)
2341 if (msgs
[i
].flags
& I2C_M_RD
)
2342 trace_i2c_read(adap
, &msgs
[i
], i
);
2344 trace_i2c_write(adap
, &msgs
[i
], i
);
2347 /* Retry automatically on arbitration loss */
2348 orig_jiffies
= jiffies
;
2349 for (ret
= 0, try = 0; try <= adap
->retries
; try++) {
2350 ret
= adap
->algo
->master_xfer(adap
, msgs
, num
);
2353 if (time_after(jiffies
, orig_jiffies
+ adap
->timeout
))
2357 if (static_key_false(&i2c_trace_msg
)) {
2359 for (i
= 0; i
< ret
; i
++)
2360 if (msgs
[i
].flags
& I2C_M_RD
)
2361 trace_i2c_reply(adap
, &msgs
[i
], i
);
2362 trace_i2c_result(adap
, i
, ret
);
2367 EXPORT_SYMBOL(__i2c_transfer
);
2370 * i2c_transfer - execute a single or combined I2C message
2371 * @adap: Handle to I2C bus
2372 * @msgs: One or more messages to execute before STOP is issued to
2373 * terminate the operation; each message begins with a START.
2374 * @num: Number of messages to be executed.
2376 * Returns negative errno, else the number of messages executed.
2378 * Note that there is no requirement that each message be sent to
2379 * the same slave address, although that is the most common model.
2381 int i2c_transfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
2385 /* REVISIT the fault reporting model here is weak:
2387 * - When we get an error after receiving N bytes from a slave,
2388 * there is no way to report "N".
2390 * - When we get a NAK after transmitting N bytes to a slave,
2391 * there is no way to report "N" ... or to let the master
2392 * continue executing the rest of this combined message, if
2393 * that's the appropriate response.
2395 * - When for example "num" is two and we successfully complete
2396 * the first message but get an error part way through the
2397 * second, it's unclear whether that should be reported as
2398 * one (discarding status on the second message) or errno
2399 * (discarding status on the first one).
2402 if (adap
->algo
->master_xfer
) {
2404 for (ret
= 0; ret
< num
; ret
++) {
2405 dev_dbg(&adap
->dev
, "master_xfer[%d] %c, addr=0x%02x, "
2406 "len=%d%s\n", ret
, (msgs
[ret
].flags
& I2C_M_RD
)
2407 ? 'R' : 'W', msgs
[ret
].addr
, msgs
[ret
].len
,
2408 (msgs
[ret
].flags
& I2C_M_RECV_LEN
) ? "+" : "");
2412 if (in_atomic() || irqs_disabled()) {
2413 ret
= adap
->trylock_bus(adap
, I2C_LOCK_SEGMENT
);
2415 /* I2C activity is ongoing. */
2418 i2c_lock_bus(adap
, I2C_LOCK_SEGMENT
);
2421 ret
= __i2c_transfer(adap
, msgs
, num
);
2422 i2c_unlock_bus(adap
, I2C_LOCK_SEGMENT
);
2426 dev_dbg(&adap
->dev
, "I2C level transfers not supported\n");
2430 EXPORT_SYMBOL(i2c_transfer
);
2433 * i2c_master_send - issue a single I2C message in master transmit mode
2434 * @client: Handle to slave device
2435 * @buf: Data that will be written to the slave
2436 * @count: How many bytes to write, must be less than 64k since msg.len is u16
2438 * Returns negative errno, or else the number of bytes written.
2440 int i2c_master_send(const struct i2c_client
*client
, const char *buf
, int count
)
2443 struct i2c_adapter
*adap
= client
->adapter
;
2446 msg
.addr
= client
->addr
;
2447 msg
.flags
= client
->flags
& I2C_M_TEN
;
2449 msg
.buf
= (char *)buf
;
2451 ret
= i2c_transfer(adap
, &msg
, 1);
2454 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2455 * transmitted, else error code.
2457 return (ret
== 1) ? count
: ret
;
2459 EXPORT_SYMBOL(i2c_master_send
);
2462 * i2c_master_recv - issue a single I2C message in master receive mode
2463 * @client: Handle to slave device
2464 * @buf: Where to store data read from slave
2465 * @count: How many bytes to read, must be less than 64k since msg.len is u16
2467 * Returns negative errno, or else the number of bytes read.
2469 int i2c_master_recv(const struct i2c_client
*client
, char *buf
, int count
)
2471 struct i2c_adapter
*adap
= client
->adapter
;
2475 msg
.addr
= client
->addr
;
2476 msg
.flags
= client
->flags
& I2C_M_TEN
;
2477 msg
.flags
|= I2C_M_RD
;
2481 ret
= i2c_transfer(adap
, &msg
, 1);
2484 * If everything went ok (i.e. 1 msg received), return #bytes received,
2487 return (ret
== 1) ? count
: ret
;
2489 EXPORT_SYMBOL(i2c_master_recv
);
2491 /* ----------------------------------------------------
2492 * the i2c address scanning function
2493 * Will not work for 10-bit addresses!
2494 * ----------------------------------------------------
2498 * Legacy default probe function, mostly relevant for SMBus. The default
2499 * probe method is a quick write, but it is known to corrupt the 24RF08
2500 * EEPROMs due to a state machine bug, and could also irreversibly
2501 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2502 * we use a short byte read instead. Also, some bus drivers don't implement
2503 * quick write, so we fallback to a byte read in that case too.
2504 * On x86, there is another special case for FSC hardware monitoring chips,
2505 * which want regular byte reads (address 0x73.) Fortunately, these are the
2506 * only known chips using this I2C address on PC hardware.
2507 * Returns 1 if probe succeeded, 0 if not.
2509 static int i2c_default_probe(struct i2c_adapter
*adap
, unsigned short addr
)
2512 union i2c_smbus_data dummy
;
2515 if (addr
== 0x73 && (adap
->class & I2C_CLASS_HWMON
)
2516 && i2c_check_functionality(adap
, I2C_FUNC_SMBUS_READ_BYTE_DATA
))
2517 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
2518 I2C_SMBUS_BYTE_DATA
, &dummy
);
2521 if (!((addr
& ~0x07) == 0x30 || (addr
& ~0x0f) == 0x50)
2522 && i2c_check_functionality(adap
, I2C_FUNC_SMBUS_QUICK
))
2523 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_WRITE
, 0,
2524 I2C_SMBUS_QUICK
, NULL
);
2525 else if (i2c_check_functionality(adap
, I2C_FUNC_SMBUS_READ_BYTE
))
2526 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
2527 I2C_SMBUS_BYTE
, &dummy
);
2529 dev_warn(&adap
->dev
, "No suitable probing method supported for address 0x%02X\n",
2537 static int i2c_detect_address(struct i2c_client
*temp_client
,
2538 struct i2c_driver
*driver
)
2540 struct i2c_board_info info
;
2541 struct i2c_adapter
*adapter
= temp_client
->adapter
;
2542 int addr
= temp_client
->addr
;
2545 /* Make sure the address is valid */
2546 err
= i2c_check_7bit_addr_validity_strict(addr
);
2548 dev_warn(&adapter
->dev
, "Invalid probe address 0x%02x\n",
2553 /* Skip if already in use (7 bit, no need to encode flags) */
2554 if (i2c_check_addr_busy(adapter
, addr
))
2557 /* Make sure there is something at this address */
2558 if (!i2c_default_probe(adapter
, addr
))
2561 /* Finally call the custom detection function */
2562 memset(&info
, 0, sizeof(struct i2c_board_info
));
2564 err
= driver
->detect(temp_client
, &info
);
2566 /* -ENODEV is returned if the detection fails. We catch it
2567 here as this isn't an error. */
2568 return err
== -ENODEV
? 0 : err
;
2571 /* Consistency check */
2572 if (info
.type
[0] == '\0') {
2573 dev_err(&adapter
->dev
, "%s detection function provided "
2574 "no name for 0x%x\n", driver
->driver
.name
,
2577 struct i2c_client
*client
;
2579 /* Detection succeeded, instantiate the device */
2580 if (adapter
->class & I2C_CLASS_DEPRECATED
)
2581 dev_warn(&adapter
->dev
,
2582 "This adapter will soon drop class based instantiation of devices. "
2583 "Please make sure client 0x%02x gets instantiated by other means. "
2584 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2587 dev_dbg(&adapter
->dev
, "Creating %s at 0x%02x\n",
2588 info
.type
, info
.addr
);
2589 client
= i2c_new_device(adapter
, &info
);
2591 list_add_tail(&client
->detected
, &driver
->clients
);
2593 dev_err(&adapter
->dev
, "Failed creating %s at 0x%02x\n",
2594 info
.type
, info
.addr
);
2599 static int i2c_detect(struct i2c_adapter
*adapter
, struct i2c_driver
*driver
)
2601 const unsigned short *address_list
;
2602 struct i2c_client
*temp_client
;
2604 int adap_id
= i2c_adapter_id(adapter
);
2606 address_list
= driver
->address_list
;
2607 if (!driver
->detect
|| !address_list
)
2610 /* Warn that the adapter lost class based instantiation */
2611 if (adapter
->class == I2C_CLASS_DEPRECATED
) {
2612 dev_dbg(&adapter
->dev
,
2613 "This adapter dropped support for I2C classes and "
2614 "won't auto-detect %s devices anymore. If you need it, check "
2615 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2616 driver
->driver
.name
);
2620 /* Stop here if the classes do not match */
2621 if (!(adapter
->class & driver
->class))
2624 /* Set up a temporary client to help detect callback */
2625 temp_client
= kzalloc(sizeof(struct i2c_client
), GFP_KERNEL
);
2628 temp_client
->adapter
= adapter
;
2630 for (i
= 0; address_list
[i
] != I2C_CLIENT_END
; i
+= 1) {
2631 dev_dbg(&adapter
->dev
, "found normal entry for adapter %d, "
2632 "addr 0x%02x\n", adap_id
, address_list
[i
]);
2633 temp_client
->addr
= address_list
[i
];
2634 err
= i2c_detect_address(temp_client
, driver
);
2643 int i2c_probe_func_quick_read(struct i2c_adapter
*adap
, unsigned short addr
)
2645 return i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
2646 I2C_SMBUS_QUICK
, NULL
) >= 0;
2648 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read
);
2651 i2c_new_probed_device(struct i2c_adapter
*adap
,
2652 struct i2c_board_info
*info
,
2653 unsigned short const *addr_list
,
2654 int (*probe
)(struct i2c_adapter
*, unsigned short addr
))
2659 probe
= i2c_default_probe
;
2661 for (i
= 0; addr_list
[i
] != I2C_CLIENT_END
; i
++) {
2662 /* Check address validity */
2663 if (i2c_check_7bit_addr_validity_strict(addr_list
[i
]) < 0) {
2664 dev_warn(&adap
->dev
, "Invalid 7-bit address "
2665 "0x%02x\n", addr_list
[i
]);
2669 /* Check address availability (7 bit, no need to encode flags) */
2670 if (i2c_check_addr_busy(adap
, addr_list
[i
])) {
2671 dev_dbg(&adap
->dev
, "Address 0x%02x already in "
2672 "use, not probing\n", addr_list
[i
]);
2676 /* Test address responsiveness */
2677 if (probe(adap
, addr_list
[i
]))
2681 if (addr_list
[i
] == I2C_CLIENT_END
) {
2682 dev_dbg(&adap
->dev
, "Probing failed, no device found\n");
2686 info
->addr
= addr_list
[i
];
2687 return i2c_new_device(adap
, info
);
2689 EXPORT_SYMBOL_GPL(i2c_new_probed_device
);
2691 struct i2c_adapter
*i2c_get_adapter(int nr
)
2693 struct i2c_adapter
*adapter
;
2695 mutex_lock(&core_lock
);
2696 adapter
= idr_find(&i2c_adapter_idr
, nr
);
2700 if (try_module_get(adapter
->owner
))
2701 get_device(&adapter
->dev
);
2706 mutex_unlock(&core_lock
);
2709 EXPORT_SYMBOL(i2c_get_adapter
);
2711 void i2c_put_adapter(struct i2c_adapter
*adap
)
2716 put_device(&adap
->dev
);
2717 module_put(adap
->owner
);
2719 EXPORT_SYMBOL(i2c_put_adapter
);
2721 /* The SMBus parts */
2723 #define POLY (0x1070U << 3)
2724 static u8
crc8(u16 data
)
2728 for (i
= 0; i
< 8; i
++) {
2733 return (u8
)(data
>> 8);
2736 /* Incremental CRC8 over count bytes in the array pointed to by p */
2737 static u8
i2c_smbus_pec(u8 crc
, u8
*p
, size_t count
)
2741 for (i
= 0; i
< count
; i
++)
2742 crc
= crc8((crc
^ p
[i
]) << 8);
2746 /* Assume a 7-bit address, which is reasonable for SMBus */
2747 static u8
i2c_smbus_msg_pec(u8 pec
, struct i2c_msg
*msg
)
2749 /* The address will be sent first */
2750 u8 addr
= i2c_8bit_addr_from_msg(msg
);
2751 pec
= i2c_smbus_pec(pec
, &addr
, 1);
2753 /* The data buffer follows */
2754 return i2c_smbus_pec(pec
, msg
->buf
, msg
->len
);
2757 /* Used for write only transactions */
2758 static inline void i2c_smbus_add_pec(struct i2c_msg
*msg
)
2760 msg
->buf
[msg
->len
] = i2c_smbus_msg_pec(0, msg
);
2764 /* Return <0 on CRC error
2765 If there was a write before this read (most cases) we need to take the
2766 partial CRC from the write part into account.
2767 Note that this function does modify the message (we need to decrease the
2768 message length to hide the CRC byte from the caller). */
2769 static int i2c_smbus_check_pec(u8 cpec
, struct i2c_msg
*msg
)
2771 u8 rpec
= msg
->buf
[--msg
->len
];
2772 cpec
= i2c_smbus_msg_pec(cpec
, msg
);
2775 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2783 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2784 * @client: Handle to slave device
2786 * This executes the SMBus "receive byte" protocol, returning negative errno
2787 * else the byte received from the device.
2789 s32
i2c_smbus_read_byte(const struct i2c_client
*client
)
2791 union i2c_smbus_data data
;
2794 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2796 I2C_SMBUS_BYTE
, &data
);
2797 return (status
< 0) ? status
: data
.byte
;
2799 EXPORT_SYMBOL(i2c_smbus_read_byte
);
2802 * i2c_smbus_write_byte - SMBus "send byte" protocol
2803 * @client: Handle to slave device
2804 * @value: Byte to be sent
2806 * This executes the SMBus "send byte" protocol, returning negative errno
2807 * else zero on success.
2809 s32
i2c_smbus_write_byte(const struct i2c_client
*client
, u8 value
)
2811 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2812 I2C_SMBUS_WRITE
, value
, I2C_SMBUS_BYTE
, NULL
);
2814 EXPORT_SYMBOL(i2c_smbus_write_byte
);
2817 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2818 * @client: Handle to slave device
2819 * @command: Byte interpreted by slave
2821 * This executes the SMBus "read byte" protocol, returning negative errno
2822 * else a data byte received from the device.
2824 s32
i2c_smbus_read_byte_data(const struct i2c_client
*client
, u8 command
)
2826 union i2c_smbus_data data
;
2829 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2830 I2C_SMBUS_READ
, command
,
2831 I2C_SMBUS_BYTE_DATA
, &data
);
2832 return (status
< 0) ? status
: data
.byte
;
2834 EXPORT_SYMBOL(i2c_smbus_read_byte_data
);
2837 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2838 * @client: Handle to slave device
2839 * @command: Byte interpreted by slave
2840 * @value: Byte being written
2842 * This executes the SMBus "write byte" protocol, returning negative errno
2843 * else zero on success.
2845 s32
i2c_smbus_write_byte_data(const struct i2c_client
*client
, u8 command
,
2848 union i2c_smbus_data data
;
2850 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2851 I2C_SMBUS_WRITE
, command
,
2852 I2C_SMBUS_BYTE_DATA
, &data
);
2854 EXPORT_SYMBOL(i2c_smbus_write_byte_data
);
2857 * i2c_smbus_read_word_data - SMBus "read word" protocol
2858 * @client: Handle to slave device
2859 * @command: Byte interpreted by slave
2861 * This executes the SMBus "read word" protocol, returning negative errno
2862 * else a 16-bit unsigned "word" received from the device.
2864 s32
i2c_smbus_read_word_data(const struct i2c_client
*client
, u8 command
)
2866 union i2c_smbus_data data
;
2869 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2870 I2C_SMBUS_READ
, command
,
2871 I2C_SMBUS_WORD_DATA
, &data
);
2872 return (status
< 0) ? status
: data
.word
;
2874 EXPORT_SYMBOL(i2c_smbus_read_word_data
);
2877 * i2c_smbus_write_word_data - SMBus "write word" protocol
2878 * @client: Handle to slave device
2879 * @command: Byte interpreted by slave
2880 * @value: 16-bit "word" being written
2882 * This executes the SMBus "write word" protocol, returning negative errno
2883 * else zero on success.
2885 s32
i2c_smbus_write_word_data(const struct i2c_client
*client
, u8 command
,
2888 union i2c_smbus_data data
;
2890 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2891 I2C_SMBUS_WRITE
, command
,
2892 I2C_SMBUS_WORD_DATA
, &data
);
2894 EXPORT_SYMBOL(i2c_smbus_write_word_data
);
2897 * i2c_smbus_read_block_data - SMBus "block read" protocol
2898 * @client: Handle to slave device
2899 * @command: Byte interpreted by slave
2900 * @values: Byte array into which data will be read; big enough to hold
2901 * the data returned by the slave. SMBus allows at most 32 bytes.
2903 * This executes the SMBus "block read" protocol, returning negative errno
2904 * else the number of data bytes in the slave's response.
2906 * Note that using this function requires that the client's adapter support
2907 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2908 * support this; its emulation through I2C messaging relies on a specific
2909 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2911 s32
i2c_smbus_read_block_data(const struct i2c_client
*client
, u8 command
,
2914 union i2c_smbus_data data
;
2917 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2918 I2C_SMBUS_READ
, command
,
2919 I2C_SMBUS_BLOCK_DATA
, &data
);
2923 memcpy(values
, &data
.block
[1], data
.block
[0]);
2924 return data
.block
[0];
2926 EXPORT_SYMBOL(i2c_smbus_read_block_data
);
2929 * i2c_smbus_write_block_data - SMBus "block write" protocol
2930 * @client: Handle to slave device
2931 * @command: Byte interpreted by slave
2932 * @length: Size of data block; SMBus allows at most 32 bytes
2933 * @values: Byte array which will be written.
2935 * This executes the SMBus "block write" protocol, returning negative errno
2936 * else zero on success.
2938 s32
i2c_smbus_write_block_data(const struct i2c_client
*client
, u8 command
,
2939 u8 length
, const u8
*values
)
2941 union i2c_smbus_data data
;
2943 if (length
> I2C_SMBUS_BLOCK_MAX
)
2944 length
= I2C_SMBUS_BLOCK_MAX
;
2945 data
.block
[0] = length
;
2946 memcpy(&data
.block
[1], values
, length
);
2947 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2948 I2C_SMBUS_WRITE
, command
,
2949 I2C_SMBUS_BLOCK_DATA
, &data
);
2951 EXPORT_SYMBOL(i2c_smbus_write_block_data
);
2953 /* Returns the number of read bytes */
2954 s32
i2c_smbus_read_i2c_block_data(const struct i2c_client
*client
, u8 command
,
2955 u8 length
, u8
*values
)
2957 union i2c_smbus_data data
;
2960 if (length
> I2C_SMBUS_BLOCK_MAX
)
2961 length
= I2C_SMBUS_BLOCK_MAX
;
2962 data
.block
[0] = length
;
2963 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2964 I2C_SMBUS_READ
, command
,
2965 I2C_SMBUS_I2C_BLOCK_DATA
, &data
);
2969 memcpy(values
, &data
.block
[1], data
.block
[0]);
2970 return data
.block
[0];
2972 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data
);
2974 s32
i2c_smbus_write_i2c_block_data(const struct i2c_client
*client
, u8 command
,
2975 u8 length
, const u8
*values
)
2977 union i2c_smbus_data data
;
2979 if (length
> I2C_SMBUS_BLOCK_MAX
)
2980 length
= I2C_SMBUS_BLOCK_MAX
;
2981 data
.block
[0] = length
;
2982 memcpy(data
.block
+ 1, values
, length
);
2983 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2984 I2C_SMBUS_WRITE
, command
,
2985 I2C_SMBUS_I2C_BLOCK_DATA
, &data
);
2987 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data
);
2989 /* Simulate a SMBus command using the i2c protocol
2990 No checking of parameters is done! */
2991 static s32
i2c_smbus_xfer_emulated(struct i2c_adapter
*adapter
, u16 addr
,
2992 unsigned short flags
,
2993 char read_write
, u8 command
, int size
,
2994 union i2c_smbus_data
*data
)
2996 /* So we need to generate a series of msgs. In the case of writing, we
2997 need to use only one message; when reading, we need two. We initialize
2998 most things with sane defaults, to keep the code below somewhat
3000 unsigned char msgbuf0
[I2C_SMBUS_BLOCK_MAX
+3];
3001 unsigned char msgbuf1
[I2C_SMBUS_BLOCK_MAX
+2];
3002 int num
= read_write
== I2C_SMBUS_READ
? 2 : 1;
3006 struct i2c_msg msg
[2] = {
3014 .flags
= flags
| I2C_M_RD
,
3020 msgbuf0
[0] = command
;
3022 case I2C_SMBUS_QUICK
:
3024 /* Special case: The read/write field is used as data */
3025 msg
[0].flags
= flags
| (read_write
== I2C_SMBUS_READ
?
3029 case I2C_SMBUS_BYTE
:
3030 if (read_write
== I2C_SMBUS_READ
) {
3031 /* Special case: only a read! */
3032 msg
[0].flags
= I2C_M_RD
| flags
;
3036 case I2C_SMBUS_BYTE_DATA
:
3037 if (read_write
== I2C_SMBUS_READ
)
3041 msgbuf0
[1] = data
->byte
;
3044 case I2C_SMBUS_WORD_DATA
:
3045 if (read_write
== I2C_SMBUS_READ
)
3049 msgbuf0
[1] = data
->word
& 0xff;
3050 msgbuf0
[2] = data
->word
>> 8;
3053 case I2C_SMBUS_PROC_CALL
:
3054 num
= 2; /* Special case */
3055 read_write
= I2C_SMBUS_READ
;
3058 msgbuf0
[1] = data
->word
& 0xff;
3059 msgbuf0
[2] = data
->word
>> 8;
3061 case I2C_SMBUS_BLOCK_DATA
:
3062 if (read_write
== I2C_SMBUS_READ
) {
3063 msg
[1].flags
|= I2C_M_RECV_LEN
;
3064 msg
[1].len
= 1; /* block length will be added by
3065 the underlying bus driver */
3067 msg
[0].len
= data
->block
[0] + 2;
3068 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 2) {
3069 dev_err(&adapter
->dev
,
3070 "Invalid block write size %d\n",
3074 for (i
= 1; i
< msg
[0].len
; i
++)
3075 msgbuf0
[i
] = data
->block
[i
-1];
3078 case I2C_SMBUS_BLOCK_PROC_CALL
:
3079 num
= 2; /* Another special case */
3080 read_write
= I2C_SMBUS_READ
;
3081 if (data
->block
[0] > I2C_SMBUS_BLOCK_MAX
) {
3082 dev_err(&adapter
->dev
,
3083 "Invalid block write size %d\n",
3087 msg
[0].len
= data
->block
[0] + 2;
3088 for (i
= 1; i
< msg
[0].len
; i
++)
3089 msgbuf0
[i
] = data
->block
[i
-1];
3090 msg
[1].flags
|= I2C_M_RECV_LEN
;
3091 msg
[1].len
= 1; /* block length will be added by
3092 the underlying bus driver */
3094 case I2C_SMBUS_I2C_BLOCK_DATA
:
3095 if (read_write
== I2C_SMBUS_READ
) {
3096 msg
[1].len
= data
->block
[0];
3098 msg
[0].len
= data
->block
[0] + 1;
3099 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 1) {
3100 dev_err(&adapter
->dev
,
3101 "Invalid block write size %d\n",
3105 for (i
= 1; i
<= data
->block
[0]; i
++)
3106 msgbuf0
[i
] = data
->block
[i
];
3110 dev_err(&adapter
->dev
, "Unsupported transaction %d\n", size
);
3114 i
= ((flags
& I2C_CLIENT_PEC
) && size
!= I2C_SMBUS_QUICK
3115 && size
!= I2C_SMBUS_I2C_BLOCK_DATA
);
3117 /* Compute PEC if first message is a write */
3118 if (!(msg
[0].flags
& I2C_M_RD
)) {
3119 if (num
== 1) /* Write only */
3120 i2c_smbus_add_pec(&msg
[0]);
3121 else /* Write followed by read */
3122 partial_pec
= i2c_smbus_msg_pec(0, &msg
[0]);
3124 /* Ask for PEC if last message is a read */
3125 if (msg
[num
-1].flags
& I2C_M_RD
)
3129 status
= i2c_transfer(adapter
, msg
, num
);
3133 /* Check PEC if last message is a read */
3134 if (i
&& (msg
[num
-1].flags
& I2C_M_RD
)) {
3135 status
= i2c_smbus_check_pec(partial_pec
, &msg
[num
-1]);
3140 if (read_write
== I2C_SMBUS_READ
)
3142 case I2C_SMBUS_BYTE
:
3143 data
->byte
= msgbuf0
[0];
3145 case I2C_SMBUS_BYTE_DATA
:
3146 data
->byte
= msgbuf1
[0];
3148 case I2C_SMBUS_WORD_DATA
:
3149 case I2C_SMBUS_PROC_CALL
:
3150 data
->word
= msgbuf1
[0] | (msgbuf1
[1] << 8);
3152 case I2C_SMBUS_I2C_BLOCK_DATA
:
3153 for (i
= 0; i
< data
->block
[0]; i
++)
3154 data
->block
[i
+1] = msgbuf1
[i
];
3156 case I2C_SMBUS_BLOCK_DATA
:
3157 case I2C_SMBUS_BLOCK_PROC_CALL
:
3158 for (i
= 0; i
< msgbuf1
[0] + 1; i
++)
3159 data
->block
[i
] = msgbuf1
[i
];
3166 * i2c_smbus_xfer - execute SMBus protocol operations
3167 * @adapter: Handle to I2C bus
3168 * @addr: Address of SMBus slave on that bus
3169 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
3170 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
3171 * @command: Byte interpreted by slave, for protocols which use such bytes
3172 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
3173 * @data: Data to be read or written
3175 * This executes an SMBus protocol operation, and returns a negative
3176 * errno code else zero on success.
3178 s32
i2c_smbus_xfer(struct i2c_adapter
*adapter
, u16 addr
, unsigned short flags
,
3179 char read_write
, u8 command
, int protocol
,
3180 union i2c_smbus_data
*data
)
3182 unsigned long orig_jiffies
;
3186 /* If enabled, the following two tracepoints are conditional on
3187 * read_write and protocol.
3189 trace_smbus_write(adapter
, addr
, flags
, read_write
,
3190 command
, protocol
, data
);
3191 trace_smbus_read(adapter
, addr
, flags
, read_write
,
3194 flags
&= I2C_M_TEN
| I2C_CLIENT_PEC
| I2C_CLIENT_SCCB
;
3196 if (adapter
->algo
->smbus_xfer
) {
3197 i2c_lock_bus(adapter
, I2C_LOCK_SEGMENT
);
3199 /* Retry automatically on arbitration loss */
3200 orig_jiffies
= jiffies
;
3201 for (res
= 0, try = 0; try <= adapter
->retries
; try++) {
3202 res
= adapter
->algo
->smbus_xfer(adapter
, addr
, flags
,
3203 read_write
, command
,
3207 if (time_after(jiffies
,
3208 orig_jiffies
+ adapter
->timeout
))
3211 i2c_unlock_bus(adapter
, I2C_LOCK_SEGMENT
);
3213 if (res
!= -EOPNOTSUPP
|| !adapter
->algo
->master_xfer
)
3216 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
3217 * implement native support for the SMBus operation.
3221 res
= i2c_smbus_xfer_emulated(adapter
, addr
, flags
, read_write
,
3222 command
, protocol
, data
);
3225 /* If enabled, the reply tracepoint is conditional on read_write. */
3226 trace_smbus_reply(adapter
, addr
, flags
, read_write
,
3227 command
, protocol
, data
);
3228 trace_smbus_result(adapter
, addr
, flags
, read_write
,
3229 command
, protocol
, res
);
3233 EXPORT_SYMBOL(i2c_smbus_xfer
);
3236 * i2c_smbus_read_i2c_block_data_or_emulated - read block or emulate
3237 * @client: Handle to slave device
3238 * @command: Byte interpreted by slave
3239 * @length: Size of data block; SMBus allows at most I2C_SMBUS_BLOCK_MAX bytes
3240 * @values: Byte array into which data will be read; big enough to hold
3241 * the data returned by the slave. SMBus allows at most
3242 * I2C_SMBUS_BLOCK_MAX bytes.
3244 * This executes the SMBus "block read" protocol if supported by the adapter.
3245 * If block read is not supported, it emulates it using either word or byte
3246 * read protocols depending on availability.
3248 * The addresses of the I2C slave device that are accessed with this function
3249 * must be mapped to a linear region, so that a block read will have the same
3250 * effect as a byte read. Before using this function you must double-check
3251 * if the I2C slave does support exchanging a block transfer with a byte
3254 s32
i2c_smbus_read_i2c_block_data_or_emulated(const struct i2c_client
*client
,
3255 u8 command
, u8 length
, u8
*values
)
3260 if (length
> I2C_SMBUS_BLOCK_MAX
)
3261 length
= I2C_SMBUS_BLOCK_MAX
;
3263 if (i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_READ_I2C_BLOCK
))
3264 return i2c_smbus_read_i2c_block_data(client
, command
, length
, values
);
3266 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_READ_BYTE_DATA
))
3269 if (i2c_check_functionality(client
->adapter
, I2C_FUNC_SMBUS_READ_WORD_DATA
)) {
3270 while ((i
+ 2) <= length
) {
3271 status
= i2c_smbus_read_word_data(client
, command
+ i
);
3274 values
[i
] = status
& 0xff;
3275 values
[i
+ 1] = status
>> 8;
3280 while (i
< length
) {
3281 status
= i2c_smbus_read_byte_data(client
, command
+ i
);
3290 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data_or_emulated
);
3292 #if IS_ENABLED(CONFIG_I2C_SLAVE)
3293 int i2c_slave_register(struct i2c_client
*client
, i2c_slave_cb_t slave_cb
)
3297 if (!client
|| !slave_cb
) {
3298 WARN(1, "insufficent data\n");
3302 if (!(client
->flags
& I2C_CLIENT_SLAVE
))
3303 dev_warn(&client
->dev
, "%s: client slave flag not set. You might see address collisions\n",
3306 if (!(client
->flags
& I2C_CLIENT_TEN
)) {
3307 /* Enforce stricter address checking */
3308 ret
= i2c_check_7bit_addr_validity_strict(client
->addr
);
3310 dev_err(&client
->dev
, "%s: invalid address\n", __func__
);
3315 if (!client
->adapter
->algo
->reg_slave
) {
3316 dev_err(&client
->dev
, "%s: not supported by adapter\n", __func__
);
3320 client
->slave_cb
= slave_cb
;
3322 i2c_lock_adapter(client
->adapter
);
3323 ret
= client
->adapter
->algo
->reg_slave(client
);
3324 i2c_unlock_adapter(client
->adapter
);
3327 client
->slave_cb
= NULL
;
3328 dev_err(&client
->dev
, "%s: adapter returned error %d\n", __func__
, ret
);
3333 EXPORT_SYMBOL_GPL(i2c_slave_register
);
3335 int i2c_slave_unregister(struct i2c_client
*client
)
3339 if (!client
->adapter
->algo
->unreg_slave
) {
3340 dev_err(&client
->dev
, "%s: not supported by adapter\n", __func__
);
3344 i2c_lock_adapter(client
->adapter
);
3345 ret
= client
->adapter
->algo
->unreg_slave(client
);
3346 i2c_unlock_adapter(client
->adapter
);
3349 client
->slave_cb
= NULL
;
3351 dev_err(&client
->dev
, "%s: adapter returned error %d\n", __func__
, ret
);
3355 EXPORT_SYMBOL_GPL(i2c_slave_unregister
);
3358 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3359 MODULE_DESCRIPTION("I2C-Bus main module");
3360 MODULE_LICENSE("GPL");