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 <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/delay.h>
33 #include <linux/errno.h>
34 #include <linux/gpio.h>
35 #include <linux/slab.h>
36 #include <linux/i2c.h>
37 #include <linux/init.h>
38 #include <linux/idr.h>
39 #include <linux/mutex.h>
41 #include <linux/of_device.h>
42 #include <linux/of_irq.h>
43 #include <linux/clk/clk-conf.h>
44 #include <linux/completion.h>
45 #include <linux/hardirq.h>
46 #include <linux/irqflags.h>
47 #include <linux/rwsem.h>
48 #include <linux/pm_runtime.h>
49 #include <linux/pm_domain.h>
50 #include <linux/acpi.h>
51 #include <linux/jump_label.h>
52 #include <asm/uaccess.h>
53 #include <linux/err.h>
57 #define CREATE_TRACE_POINTS
58 #include <trace/events/i2c.h>
60 /* core_lock protects i2c_adapter_idr, and guarantees
61 that device detection, deletion of detected devices, and attach_adapter
62 calls are serialized */
63 static DEFINE_MUTEX(core_lock
);
64 static DEFINE_IDR(i2c_adapter_idr
);
66 static struct device_type i2c_client_type
;
67 static int i2c_detect(struct i2c_adapter
*adapter
, struct i2c_driver
*driver
);
69 static struct static_key i2c_trace_msg
= STATIC_KEY_INIT_FALSE
;
71 void i2c_transfer_trace_reg(void)
73 static_key_slow_inc(&i2c_trace_msg
);
76 void i2c_transfer_trace_unreg(void)
78 static_key_slow_dec(&i2c_trace_msg
);
81 #if defined(CONFIG_ACPI)
82 struct acpi_i2c_handler_data
{
83 struct acpi_connection_info info
;
84 struct i2c_adapter
*adapter
;
97 static int acpi_i2c_add_resource(struct acpi_resource
*ares
, void *data
)
99 struct i2c_board_info
*info
= data
;
101 if (ares
->type
== ACPI_RESOURCE_TYPE_SERIAL_BUS
) {
102 struct acpi_resource_i2c_serialbus
*sb
;
104 sb
= &ares
->data
.i2c_serial_bus
;
105 if (!info
->addr
&& sb
->type
== ACPI_RESOURCE_SERIAL_TYPE_I2C
) {
106 info
->addr
= sb
->slave_address
;
107 if (sb
->access_mode
== ACPI_I2C_10BIT_MODE
)
108 info
->flags
|= I2C_CLIENT_TEN
;
110 } else if (info
->irq
< 0) {
113 if (acpi_dev_resource_interrupt(ares
, 0, &r
))
117 /* Tell the ACPI core to skip this resource */
121 static acpi_status
acpi_i2c_add_device(acpi_handle handle
, u32 level
,
122 void *data
, void **return_value
)
124 struct i2c_adapter
*adapter
= data
;
125 struct list_head resource_list
;
126 struct i2c_board_info info
;
127 struct acpi_device
*adev
;
130 if (acpi_bus_get_device(handle
, &adev
))
132 if (acpi_bus_get_status(adev
) || !adev
->status
.present
)
135 memset(&info
, 0, sizeof(info
));
136 info
.fwnode
= acpi_fwnode_handle(adev
);
139 INIT_LIST_HEAD(&resource_list
);
140 ret
= acpi_dev_get_resources(adev
, &resource_list
,
141 acpi_i2c_add_resource
, &info
);
142 acpi_dev_free_resource_list(&resource_list
);
144 if (ret
< 0 || !info
.addr
)
147 adev
->power
.flags
.ignore_parent
= true;
148 strlcpy(info
.type
, dev_name(&adev
->dev
), sizeof(info
.type
));
149 if (!i2c_new_device(adapter
, &info
)) {
150 adev
->power
.flags
.ignore_parent
= false;
151 dev_err(&adapter
->dev
,
152 "failed to add I2C device %s from ACPI\n",
153 dev_name(&adev
->dev
));
160 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
161 * @adap: pointer to adapter
163 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
164 * namespace. When a device is found it will be added to the Linux device
165 * model and bound to the corresponding ACPI handle.
167 static void acpi_i2c_register_devices(struct i2c_adapter
*adap
)
172 if (!adap
->dev
.parent
)
175 handle
= ACPI_HANDLE(adap
->dev
.parent
);
179 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, 1,
180 acpi_i2c_add_device
, NULL
,
182 if (ACPI_FAILURE(status
))
183 dev_warn(&adap
->dev
, "failed to enumerate I2C slaves\n");
186 #else /* CONFIG_ACPI */
187 static inline void acpi_i2c_register_devices(struct i2c_adapter
*adap
) { }
188 #endif /* CONFIG_ACPI */
190 #ifdef CONFIG_ACPI_I2C_OPREGION
191 static int acpi_gsb_i2c_read_bytes(struct i2c_client
*client
,
192 u8 cmd
, u8
*data
, u8 data_len
)
195 struct i2c_msg msgs
[2];
199 buffer
= kzalloc(data_len
, GFP_KERNEL
);
203 msgs
[0].addr
= client
->addr
;
204 msgs
[0].flags
= client
->flags
;
208 msgs
[1].addr
= client
->addr
;
209 msgs
[1].flags
= client
->flags
| I2C_M_RD
;
210 msgs
[1].len
= data_len
;
211 msgs
[1].buf
= buffer
;
213 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
215 dev_err(&client
->adapter
->dev
, "i2c read failed\n");
217 memcpy(data
, buffer
, data_len
);
223 static int acpi_gsb_i2c_write_bytes(struct i2c_client
*client
,
224 u8 cmd
, u8
*data
, u8 data_len
)
227 struct i2c_msg msgs
[1];
231 buffer
= kzalloc(data_len
+ 1, GFP_KERNEL
);
236 memcpy(buffer
+ 1, data
, data_len
);
238 msgs
[0].addr
= client
->addr
;
239 msgs
[0].flags
= client
->flags
;
240 msgs
[0].len
= data_len
+ 1;
241 msgs
[0].buf
= buffer
;
243 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
245 dev_err(&client
->adapter
->dev
, "i2c write failed\n");
252 acpi_i2c_space_handler(u32 function
, acpi_physical_address command
,
253 u32 bits
, u64
*value64
,
254 void *handler_context
, void *region_context
)
256 struct gsb_buffer
*gsb
= (struct gsb_buffer
*)value64
;
257 struct acpi_i2c_handler_data
*data
= handler_context
;
258 struct acpi_connection_info
*info
= &data
->info
;
259 struct acpi_resource_i2c_serialbus
*sb
;
260 struct i2c_adapter
*adapter
= data
->adapter
;
261 struct i2c_client client
;
262 struct acpi_resource
*ares
;
263 u32 accessor_type
= function
>> 16;
264 u8 action
= function
& ACPI_IO_MASK
;
268 ret
= acpi_buffer_to_resource(info
->connection
, info
->length
, &ares
);
269 if (ACPI_FAILURE(ret
))
272 if (!value64
|| ares
->type
!= ACPI_RESOURCE_TYPE_SERIAL_BUS
) {
273 ret
= AE_BAD_PARAMETER
;
277 sb
= &ares
->data
.i2c_serial_bus
;
278 if (sb
->type
!= ACPI_RESOURCE_SERIAL_TYPE_I2C
) {
279 ret
= AE_BAD_PARAMETER
;
283 memset(&client
, 0, sizeof(client
));
284 client
.adapter
= adapter
;
285 client
.addr
= sb
->slave_address
;
288 if (sb
->access_mode
== ACPI_I2C_10BIT_MODE
)
289 client
.flags
|= I2C_CLIENT_TEN
;
291 switch (accessor_type
) {
292 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV
:
293 if (action
== ACPI_READ
) {
294 status
= i2c_smbus_read_byte(&client
);
300 status
= i2c_smbus_write_byte(&client
, gsb
->bdata
);
304 case ACPI_GSB_ACCESS_ATTRIB_BYTE
:
305 if (action
== ACPI_READ
) {
306 status
= i2c_smbus_read_byte_data(&client
, command
);
312 status
= i2c_smbus_write_byte_data(&client
, command
,
317 case ACPI_GSB_ACCESS_ATTRIB_WORD
:
318 if (action
== ACPI_READ
) {
319 status
= i2c_smbus_read_word_data(&client
, command
);
325 status
= i2c_smbus_write_word_data(&client
, command
,
330 case ACPI_GSB_ACCESS_ATTRIB_BLOCK
:
331 if (action
== ACPI_READ
) {
332 status
= i2c_smbus_read_block_data(&client
, command
,
339 status
= i2c_smbus_write_block_data(&client
, command
,
340 gsb
->len
, gsb
->data
);
344 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE
:
345 if (action
== ACPI_READ
) {
346 status
= acpi_gsb_i2c_read_bytes(&client
, command
,
347 gsb
->data
, info
->access_length
);
351 status
= acpi_gsb_i2c_write_bytes(&client
, command
,
352 gsb
->data
, info
->access_length
);
357 pr_info("protocol(0x%02x) is not supported.\n", accessor_type
);
358 ret
= AE_BAD_PARAMETER
;
362 gsb
->status
= status
;
370 static int acpi_i2c_install_space_handler(struct i2c_adapter
*adapter
)
373 struct acpi_i2c_handler_data
*data
;
376 if (!adapter
->dev
.parent
)
379 handle
= ACPI_HANDLE(adapter
->dev
.parent
);
384 data
= kzalloc(sizeof(struct acpi_i2c_handler_data
),
389 data
->adapter
= adapter
;
390 status
= acpi_bus_attach_private_data(handle
, (void *)data
);
391 if (ACPI_FAILURE(status
)) {
396 status
= acpi_install_address_space_handler(handle
,
397 ACPI_ADR_SPACE_GSBUS
,
398 &acpi_i2c_space_handler
,
401 if (ACPI_FAILURE(status
)) {
402 dev_err(&adapter
->dev
, "Error installing i2c space handler\n");
403 acpi_bus_detach_private_data(handle
);
408 acpi_walk_dep_device_list(handle
);
412 static void acpi_i2c_remove_space_handler(struct i2c_adapter
*adapter
)
415 struct acpi_i2c_handler_data
*data
;
418 if (!adapter
->dev
.parent
)
421 handle
= ACPI_HANDLE(adapter
->dev
.parent
);
426 acpi_remove_address_space_handler(handle
,
427 ACPI_ADR_SPACE_GSBUS
,
428 &acpi_i2c_space_handler
);
430 status
= acpi_bus_get_private_data(handle
, (void **)&data
);
431 if (ACPI_SUCCESS(status
))
434 acpi_bus_detach_private_data(handle
);
436 #else /* CONFIG_ACPI_I2C_OPREGION */
437 static inline void acpi_i2c_remove_space_handler(struct i2c_adapter
*adapter
)
440 static inline int acpi_i2c_install_space_handler(struct i2c_adapter
*adapter
)
442 #endif /* CONFIG_ACPI_I2C_OPREGION */
444 /* ------------------------------------------------------------------------- */
446 static const struct i2c_device_id
*i2c_match_id(const struct i2c_device_id
*id
,
447 const struct i2c_client
*client
)
449 while (id
->name
[0]) {
450 if (strcmp(client
->name
, id
->name
) == 0)
457 static int i2c_device_match(struct device
*dev
, struct device_driver
*drv
)
459 struct i2c_client
*client
= i2c_verify_client(dev
);
460 struct i2c_driver
*driver
;
465 /* Attempt an OF style match */
466 if (of_driver_match_device(dev
, drv
))
469 /* Then ACPI style match */
470 if (acpi_driver_match_device(dev
, drv
))
473 driver
= to_i2c_driver(drv
);
474 /* match on an id table if there is one */
475 if (driver
->id_table
)
476 return i2c_match_id(driver
->id_table
, client
) != NULL
;
482 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
483 static int i2c_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
485 struct i2c_client
*client
= to_i2c_client(dev
);
488 rc
= acpi_device_uevent_modalias(dev
, env
);
492 if (add_uevent_var(env
, "MODALIAS=%s%s",
493 I2C_MODULE_PREFIX
, client
->name
))
495 dev_dbg(dev
, "uevent\n");
499 /* i2c bus recovery routines */
500 static int get_scl_gpio_value(struct i2c_adapter
*adap
)
502 return gpio_get_value(adap
->bus_recovery_info
->scl_gpio
);
505 static void set_scl_gpio_value(struct i2c_adapter
*adap
, int val
)
507 gpio_set_value(adap
->bus_recovery_info
->scl_gpio
, val
);
510 static int get_sda_gpio_value(struct i2c_adapter
*adap
)
512 return gpio_get_value(adap
->bus_recovery_info
->sda_gpio
);
515 static int i2c_get_gpios_for_recovery(struct i2c_adapter
*adap
)
517 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
518 struct device
*dev
= &adap
->dev
;
521 ret
= gpio_request_one(bri
->scl_gpio
, GPIOF_OPEN_DRAIN
|
522 GPIOF_OUT_INIT_HIGH
, "i2c-scl");
524 dev_warn(dev
, "Can't get SCL gpio: %d\n", bri
->scl_gpio
);
529 if (gpio_request_one(bri
->sda_gpio
, GPIOF_IN
, "i2c-sda")) {
530 /* work without SDA polling */
531 dev_warn(dev
, "Can't get SDA gpio: %d. Not using SDA polling\n",
540 static void i2c_put_gpios_for_recovery(struct i2c_adapter
*adap
)
542 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
545 gpio_free(bri
->sda_gpio
);
547 gpio_free(bri
->scl_gpio
);
551 * We are generating clock pulses. ndelay() determines durating of clk pulses.
552 * We will generate clock with rate 100 KHz and so duration of both clock levels
553 * is: delay in ns = (10^6 / 100) / 2
555 #define RECOVERY_NDELAY 5000
556 #define RECOVERY_CLK_CNT 9
558 static int i2c_generic_recovery(struct i2c_adapter
*adap
)
560 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
561 int i
= 0, val
= 1, ret
= 0;
563 if (bri
->prepare_recovery
)
564 bri
->prepare_recovery(adap
);
567 * By this time SCL is high, as we need to give 9 falling-rising edges
569 while (i
++ < RECOVERY_CLK_CNT
* 2) {
571 /* Break if SDA is high */
572 if (bri
->get_sda
&& bri
->get_sda(adap
))
574 /* SCL shouldn't be low here */
575 if (!bri
->get_scl(adap
)) {
577 "SCL is stuck low, exit recovery\n");
584 bri
->set_scl(adap
, val
);
585 ndelay(RECOVERY_NDELAY
);
588 if (bri
->unprepare_recovery
)
589 bri
->unprepare_recovery(adap
);
594 int i2c_generic_scl_recovery(struct i2c_adapter
*adap
)
596 adap
->bus_recovery_info
->set_scl(adap
, 1);
597 return i2c_generic_recovery(adap
);
599 EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery
);
601 int i2c_generic_gpio_recovery(struct i2c_adapter
*adap
)
605 ret
= i2c_get_gpios_for_recovery(adap
);
609 ret
= i2c_generic_recovery(adap
);
610 i2c_put_gpios_for_recovery(adap
);
614 EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery
);
616 int i2c_recover_bus(struct i2c_adapter
*adap
)
618 if (!adap
->bus_recovery_info
)
621 dev_dbg(&adap
->dev
, "Trying i2c bus recovery\n");
622 return adap
->bus_recovery_info
->recover_bus(adap
);
624 EXPORT_SYMBOL_GPL(i2c_recover_bus
);
626 static int i2c_device_probe(struct device
*dev
)
628 struct i2c_client
*client
= i2c_verify_client(dev
);
629 struct i2c_driver
*driver
;
635 if (!client
->irq
&& dev
->of_node
) {
636 int irq
= of_irq_get(dev
->of_node
, 0);
638 if (irq
== -EPROBE_DEFER
)
646 driver
= to_i2c_driver(dev
->driver
);
647 if (!driver
->probe
|| !driver
->id_table
)
650 if (!device_can_wakeup(&client
->dev
))
651 device_init_wakeup(&client
->dev
,
652 client
->flags
& I2C_CLIENT_WAKE
);
653 dev_dbg(dev
, "probe\n");
655 status
= of_clk_set_defaults(dev
->of_node
, false);
659 status
= dev_pm_domain_attach(&client
->dev
, true);
660 if (status
!= -EPROBE_DEFER
) {
661 status
= driver
->probe(client
, i2c_match_id(driver
->id_table
,
664 dev_pm_domain_detach(&client
->dev
, true);
670 static int i2c_device_remove(struct device
*dev
)
672 struct i2c_client
*client
= i2c_verify_client(dev
);
673 struct i2c_driver
*driver
;
676 if (!client
|| !dev
->driver
)
679 driver
= to_i2c_driver(dev
->driver
);
680 if (driver
->remove
) {
681 dev_dbg(dev
, "remove\n");
682 status
= driver
->remove(client
);
685 dev_pm_domain_detach(&client
->dev
, true);
689 static void i2c_device_shutdown(struct device
*dev
)
691 struct i2c_client
*client
= i2c_verify_client(dev
);
692 struct i2c_driver
*driver
;
694 if (!client
|| !dev
->driver
)
696 driver
= to_i2c_driver(dev
->driver
);
697 if (driver
->shutdown
)
698 driver
->shutdown(client
);
701 static void i2c_client_dev_release(struct device
*dev
)
703 kfree(to_i2c_client(dev
));
707 show_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
709 return sprintf(buf
, "%s\n", dev
->type
== &i2c_client_type
?
710 to_i2c_client(dev
)->name
: to_i2c_adapter(dev
)->name
);
712 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
715 show_modalias(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
717 struct i2c_client
*client
= to_i2c_client(dev
);
720 len
= acpi_device_modalias(dev
, buf
, PAGE_SIZE
-1);
724 return sprintf(buf
, "%s%s\n", I2C_MODULE_PREFIX
, client
->name
);
726 static DEVICE_ATTR(modalias
, S_IRUGO
, show_modalias
, NULL
);
728 static struct attribute
*i2c_dev_attrs
[] = {
730 /* modalias helps coldplug: modprobe $(cat .../modalias) */
731 &dev_attr_modalias
.attr
,
734 ATTRIBUTE_GROUPS(i2c_dev
);
736 struct bus_type i2c_bus_type
= {
738 .match
= i2c_device_match
,
739 .probe
= i2c_device_probe
,
740 .remove
= i2c_device_remove
,
741 .shutdown
= i2c_device_shutdown
,
743 EXPORT_SYMBOL_GPL(i2c_bus_type
);
745 static struct device_type i2c_client_type
= {
746 .groups
= i2c_dev_groups
,
747 .uevent
= i2c_device_uevent
,
748 .release
= i2c_client_dev_release
,
753 * i2c_verify_client - return parameter as i2c_client, or NULL
754 * @dev: device, probably from some driver model iterator
756 * When traversing the driver model tree, perhaps using driver model
757 * iterators like @device_for_each_child(), you can't assume very much
758 * about the nodes you find. Use this function to avoid oopses caused
759 * by wrongly treating some non-I2C device as an i2c_client.
761 struct i2c_client
*i2c_verify_client(struct device
*dev
)
763 return (dev
->type
== &i2c_client_type
)
767 EXPORT_SYMBOL(i2c_verify_client
);
770 /* This is a permissive address validity check, I2C address map constraints
771 * are purposely not enforced, except for the general call address. */
772 static int i2c_check_client_addr_validity(const struct i2c_client
*client
)
774 if (client
->flags
& I2C_CLIENT_TEN
) {
775 /* 10-bit address, all values are valid */
776 if (client
->addr
> 0x3ff)
779 /* 7-bit address, reject the general call address */
780 if (client
->addr
== 0x00 || client
->addr
> 0x7f)
786 /* And this is a strict address validity check, used when probing. If a
787 * device uses a reserved address, then it shouldn't be probed. 7-bit
788 * addressing is assumed, 10-bit address devices are rare and should be
789 * explicitly enumerated. */
790 static int i2c_check_addr_validity(unsigned short addr
)
793 * Reserved addresses per I2C specification:
794 * 0x00 General call address / START byte
796 * 0x02 Reserved for different bus format
797 * 0x03 Reserved for future purposes
798 * 0x04-0x07 Hs-mode master code
799 * 0x78-0x7b 10-bit slave addressing
800 * 0x7c-0x7f Reserved for future purposes
802 if (addr
< 0x08 || addr
> 0x77)
807 static int __i2c_check_addr_busy(struct device
*dev
, void *addrp
)
809 struct i2c_client
*client
= i2c_verify_client(dev
);
810 int addr
= *(int *)addrp
;
812 if (client
&& client
->addr
== addr
)
817 /* walk up mux tree */
818 static int i2c_check_mux_parents(struct i2c_adapter
*adapter
, int addr
)
820 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
823 result
= device_for_each_child(&adapter
->dev
, &addr
,
824 __i2c_check_addr_busy
);
826 if (!result
&& parent
)
827 result
= i2c_check_mux_parents(parent
, addr
);
832 /* recurse down mux tree */
833 static int i2c_check_mux_children(struct device
*dev
, void *addrp
)
837 if (dev
->type
== &i2c_adapter_type
)
838 result
= device_for_each_child(dev
, addrp
,
839 i2c_check_mux_children
);
841 result
= __i2c_check_addr_busy(dev
, addrp
);
846 static int i2c_check_addr_busy(struct i2c_adapter
*adapter
, int addr
)
848 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
852 result
= i2c_check_mux_parents(parent
, addr
);
855 result
= device_for_each_child(&adapter
->dev
, &addr
,
856 i2c_check_mux_children
);
862 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
863 * @adapter: Target I2C bus segment
865 void i2c_lock_adapter(struct i2c_adapter
*adapter
)
867 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
870 i2c_lock_adapter(parent
);
872 rt_mutex_lock(&adapter
->bus_lock
);
874 EXPORT_SYMBOL_GPL(i2c_lock_adapter
);
877 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
878 * @adapter: Target I2C bus segment
880 static int i2c_trylock_adapter(struct i2c_adapter
*adapter
)
882 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
885 return i2c_trylock_adapter(parent
);
887 return rt_mutex_trylock(&adapter
->bus_lock
);
891 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
892 * @adapter: Target I2C bus segment
894 void i2c_unlock_adapter(struct i2c_adapter
*adapter
)
896 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
899 i2c_unlock_adapter(parent
);
901 rt_mutex_unlock(&adapter
->bus_lock
);
903 EXPORT_SYMBOL_GPL(i2c_unlock_adapter
);
905 static void i2c_dev_set_name(struct i2c_adapter
*adap
,
906 struct i2c_client
*client
)
908 struct acpi_device
*adev
= ACPI_COMPANION(&client
->dev
);
911 dev_set_name(&client
->dev
, "i2c-%s", acpi_dev_name(adev
));
915 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
916 dev_set_name(&client
->dev
, "%d-%04x", i2c_adapter_id(adap
),
917 client
->addr
| ((client
->flags
& I2C_CLIENT_TEN
)
922 * i2c_new_device - instantiate an i2c device
923 * @adap: the adapter managing the device
924 * @info: describes one I2C device; bus_num is ignored
927 * Create an i2c device. Binding is handled through driver model
928 * probe()/remove() methods. A driver may be bound to this device when we
929 * return from this function, or any later moment (e.g. maybe hotplugging will
930 * load the driver module). This call is not appropriate for use by mainboard
931 * initialization logic, which usually runs during an arch_initcall() long
932 * before any i2c_adapter could exist.
934 * This returns the new i2c client, which may be saved for later use with
935 * i2c_unregister_device(); or NULL to indicate an error.
938 i2c_new_device(struct i2c_adapter
*adap
, struct i2c_board_info
const *info
)
940 struct i2c_client
*client
;
943 client
= kzalloc(sizeof *client
, GFP_KERNEL
);
947 client
->adapter
= adap
;
949 client
->dev
.platform_data
= info
->platform_data
;
952 client
->dev
.archdata
= *info
->archdata
;
954 client
->flags
= info
->flags
;
955 client
->addr
= info
->addr
;
956 client
->irq
= info
->irq
;
958 strlcpy(client
->name
, info
->type
, sizeof(client
->name
));
960 /* Check for address validity */
961 status
= i2c_check_client_addr_validity(client
);
963 dev_err(&adap
->dev
, "Invalid %d-bit I2C address 0x%02hx\n",
964 client
->flags
& I2C_CLIENT_TEN
? 10 : 7, client
->addr
);
968 /* Check for address business */
969 status
= i2c_check_addr_busy(adap
, client
->addr
);
973 client
->dev
.parent
= &client
->adapter
->dev
;
974 client
->dev
.bus
= &i2c_bus_type
;
975 client
->dev
.type
= &i2c_client_type
;
976 client
->dev
.of_node
= info
->of_node
;
977 client
->dev
.fwnode
= info
->fwnode
;
979 i2c_dev_set_name(adap
, client
);
980 status
= device_register(&client
->dev
);
984 dev_dbg(&adap
->dev
, "client [%s] registered with bus id %s\n",
985 client
->name
, dev_name(&client
->dev
));
990 dev_err(&adap
->dev
, "Failed to register i2c client %s at 0x%02x "
991 "(%d)\n", client
->name
, client
->addr
, status
);
996 EXPORT_SYMBOL_GPL(i2c_new_device
);
1000 * i2c_unregister_device - reverse effect of i2c_new_device()
1001 * @client: value returned from i2c_new_device()
1002 * Context: can sleep
1004 void i2c_unregister_device(struct i2c_client
*client
)
1006 device_unregister(&client
->dev
);
1008 EXPORT_SYMBOL_GPL(i2c_unregister_device
);
1011 static const struct i2c_device_id dummy_id
[] = {
1016 static int dummy_probe(struct i2c_client
*client
,
1017 const struct i2c_device_id
*id
)
1022 static int dummy_remove(struct i2c_client
*client
)
1027 static struct i2c_driver dummy_driver
= {
1028 .driver
.name
= "dummy",
1029 .probe
= dummy_probe
,
1030 .remove
= dummy_remove
,
1031 .id_table
= dummy_id
,
1035 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1036 * @adapter: the adapter managing the device
1037 * @address: seven bit address to be used
1038 * Context: can sleep
1040 * This returns an I2C client bound to the "dummy" driver, intended for use
1041 * with devices that consume multiple addresses. Examples of such chips
1042 * include various EEPROMS (like 24c04 and 24c08 models).
1044 * These dummy devices have two main uses. First, most I2C and SMBus calls
1045 * except i2c_transfer() need a client handle; the dummy will be that handle.
1046 * And second, this prevents the specified address from being bound to a
1049 * This returns the new i2c client, which should be saved for later use with
1050 * i2c_unregister_device(); or NULL to indicate an error.
1052 struct i2c_client
*i2c_new_dummy(struct i2c_adapter
*adapter
, u16 address
)
1054 struct i2c_board_info info
= {
1055 I2C_BOARD_INFO("dummy", address
),
1058 return i2c_new_device(adapter
, &info
);
1060 EXPORT_SYMBOL_GPL(i2c_new_dummy
);
1062 /* ------------------------------------------------------------------------- */
1064 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
1066 static void i2c_adapter_dev_release(struct device
*dev
)
1068 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
1069 complete(&adap
->dev_released
);
1073 * This function is only needed for mutex_lock_nested, so it is never
1074 * called unless locking correctness checking is enabled. Thus we
1075 * make it inline to avoid a compiler warning. That's what gcc ends up
1078 static inline unsigned int i2c_adapter_depth(struct i2c_adapter
*adapter
)
1080 unsigned int depth
= 0;
1082 while ((adapter
= i2c_parent_is_i2c_adapter(adapter
)))
1089 * Let users instantiate I2C devices through sysfs. This can be used when
1090 * platform initialization code doesn't contain the proper data for
1091 * whatever reason. Also useful for drivers that do device detection and
1092 * detection fails, either because the device uses an unexpected address,
1093 * or this is a compatible device with different ID register values.
1095 * Parameter checking may look overzealous, but we really don't want
1096 * the user to provide incorrect parameters.
1099 i2c_sysfs_new_device(struct device
*dev
, struct device_attribute
*attr
,
1100 const char *buf
, size_t count
)
1102 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
1103 struct i2c_board_info info
;
1104 struct i2c_client
*client
;
1108 memset(&info
, 0, sizeof(struct i2c_board_info
));
1110 blank
= strchr(buf
, ' ');
1112 dev_err(dev
, "%s: Missing parameters\n", "new_device");
1115 if (blank
- buf
> I2C_NAME_SIZE
- 1) {
1116 dev_err(dev
, "%s: Invalid device name\n", "new_device");
1119 memcpy(info
.type
, buf
, blank
- buf
);
1121 /* Parse remaining parameters, reject extra parameters */
1122 res
= sscanf(++blank
, "%hi%c", &info
.addr
, &end
);
1124 dev_err(dev
, "%s: Can't parse I2C address\n", "new_device");
1127 if (res
> 1 && end
!= '\n') {
1128 dev_err(dev
, "%s: Extra parameters\n", "new_device");
1132 client
= i2c_new_device(adap
, &info
);
1136 /* Keep track of the added device */
1137 mutex_lock(&adap
->userspace_clients_lock
);
1138 list_add_tail(&client
->detected
, &adap
->userspace_clients
);
1139 mutex_unlock(&adap
->userspace_clients_lock
);
1140 dev_info(dev
, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1141 info
.type
, info
.addr
);
1145 static DEVICE_ATTR(new_device
, S_IWUSR
, NULL
, i2c_sysfs_new_device
);
1148 * And of course let the users delete the devices they instantiated, if
1149 * they got it wrong. This interface can only be used to delete devices
1150 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1151 * don't delete devices to which some kernel code still has references.
1153 * Parameter checking may look overzealous, but we really don't want
1154 * the user to delete the wrong device.
1157 i2c_sysfs_delete_device(struct device
*dev
, struct device_attribute
*attr
,
1158 const char *buf
, size_t count
)
1160 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
1161 struct i2c_client
*client
, *next
;
1162 unsigned short addr
;
1166 /* Parse parameters, reject extra parameters */
1167 res
= sscanf(buf
, "%hi%c", &addr
, &end
);
1169 dev_err(dev
, "%s: Can't parse I2C address\n", "delete_device");
1172 if (res
> 1 && end
!= '\n') {
1173 dev_err(dev
, "%s: Extra parameters\n", "delete_device");
1177 /* Make sure the device was added through sysfs */
1179 mutex_lock_nested(&adap
->userspace_clients_lock
,
1180 i2c_adapter_depth(adap
));
1181 list_for_each_entry_safe(client
, next
, &adap
->userspace_clients
,
1183 if (client
->addr
== addr
) {
1184 dev_info(dev
, "%s: Deleting device %s at 0x%02hx\n",
1185 "delete_device", client
->name
, client
->addr
);
1187 list_del(&client
->detected
);
1188 i2c_unregister_device(client
);
1193 mutex_unlock(&adap
->userspace_clients_lock
);
1196 dev_err(dev
, "%s: Can't find device in list\n",
1200 static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device
, S_IWUSR
, NULL
,
1201 i2c_sysfs_delete_device
);
1203 static struct attribute
*i2c_adapter_attrs
[] = {
1204 &dev_attr_name
.attr
,
1205 &dev_attr_new_device
.attr
,
1206 &dev_attr_delete_device
.attr
,
1209 ATTRIBUTE_GROUPS(i2c_adapter
);
1211 struct device_type i2c_adapter_type
= {
1212 .groups
= i2c_adapter_groups
,
1213 .release
= i2c_adapter_dev_release
,
1215 EXPORT_SYMBOL_GPL(i2c_adapter_type
);
1218 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1219 * @dev: device, probably from some driver model iterator
1221 * When traversing the driver model tree, perhaps using driver model
1222 * iterators like @device_for_each_child(), you can't assume very much
1223 * about the nodes you find. Use this function to avoid oopses caused
1224 * by wrongly treating some non-I2C device as an i2c_adapter.
1226 struct i2c_adapter
*i2c_verify_adapter(struct device
*dev
)
1228 return (dev
->type
== &i2c_adapter_type
)
1229 ? to_i2c_adapter(dev
)
1232 EXPORT_SYMBOL(i2c_verify_adapter
);
1234 #ifdef CONFIG_I2C_COMPAT
1235 static struct class_compat
*i2c_adapter_compat_class
;
1238 static void i2c_scan_static_board_info(struct i2c_adapter
*adapter
)
1240 struct i2c_devinfo
*devinfo
;
1242 down_read(&__i2c_board_lock
);
1243 list_for_each_entry(devinfo
, &__i2c_board_list
, list
) {
1244 if (devinfo
->busnum
== adapter
->nr
1245 && !i2c_new_device(adapter
,
1246 &devinfo
->board_info
))
1247 dev_err(&adapter
->dev
,
1248 "Can't create device at 0x%02x\n",
1249 devinfo
->board_info
.addr
);
1251 up_read(&__i2c_board_lock
);
1254 /* OF support code */
1256 #if IS_ENABLED(CONFIG_OF)
1257 static struct i2c_client
*of_i2c_register_device(struct i2c_adapter
*adap
,
1258 struct device_node
*node
)
1260 struct i2c_client
*result
;
1261 struct i2c_board_info info
= {};
1262 struct dev_archdata dev_ad
= {};
1266 dev_dbg(&adap
->dev
, "of_i2c: register %s\n", node
->full_name
);
1268 if (of_modalias_node(node
, info
.type
, sizeof(info
.type
)) < 0) {
1269 dev_err(&adap
->dev
, "of_i2c: modalias failure on %s\n",
1271 return ERR_PTR(-EINVAL
);
1274 addr
= of_get_property(node
, "reg", &len
);
1275 if (!addr
|| (len
< sizeof(int))) {
1276 dev_err(&adap
->dev
, "of_i2c: invalid reg on %s\n",
1278 return ERR_PTR(-EINVAL
);
1281 info
.addr
= be32_to_cpup(addr
);
1282 if (info
.addr
> (1 << 10) - 1) {
1283 dev_err(&adap
->dev
, "of_i2c: invalid addr=%x on %s\n",
1284 info
.addr
, node
->full_name
);
1285 return ERR_PTR(-EINVAL
);
1288 info
.of_node
= of_node_get(node
);
1289 info
.archdata
= &dev_ad
;
1291 if (of_get_property(node
, "wakeup-source", NULL
))
1292 info
.flags
|= I2C_CLIENT_WAKE
;
1294 result
= i2c_new_device(adap
, &info
);
1295 if (result
== NULL
) {
1296 dev_err(&adap
->dev
, "of_i2c: Failure registering %s\n",
1299 return ERR_PTR(-EINVAL
);
1304 static void of_i2c_register_devices(struct i2c_adapter
*adap
)
1306 struct device_node
*node
;
1308 /* Only register child devices if the adapter has a node pointer set */
1309 if (!adap
->dev
.of_node
)
1312 dev_dbg(&adap
->dev
, "of_i2c: walking child nodes\n");
1314 for_each_available_child_of_node(adap
->dev
.of_node
, node
)
1315 of_i2c_register_device(adap
, node
);
1318 static int of_dev_node_match(struct device
*dev
, void *data
)
1320 return dev
->of_node
== data
;
1323 /* must call put_device() when done with returned i2c_client device */
1324 struct i2c_client
*of_find_i2c_device_by_node(struct device_node
*node
)
1328 dev
= bus_find_device(&i2c_bus_type
, NULL
, node
,
1333 return i2c_verify_client(dev
);
1335 EXPORT_SYMBOL(of_find_i2c_device_by_node
);
1337 /* must call put_device() when done with returned i2c_adapter device */
1338 struct i2c_adapter
*of_find_i2c_adapter_by_node(struct device_node
*node
)
1342 dev
= bus_find_device(&i2c_bus_type
, NULL
, node
,
1347 return i2c_verify_adapter(dev
);
1349 EXPORT_SYMBOL(of_find_i2c_adapter_by_node
);
1351 static void of_i2c_register_devices(struct i2c_adapter
*adap
) { }
1352 #endif /* CONFIG_OF */
1354 static int i2c_do_add_adapter(struct i2c_driver
*driver
,
1355 struct i2c_adapter
*adap
)
1357 /* Detect supported devices on that bus, and instantiate them */
1358 i2c_detect(adap
, driver
);
1360 /* Let legacy drivers scan this bus for matching devices */
1361 if (driver
->attach_adapter
) {
1362 dev_warn(&adap
->dev
, "%s: attach_adapter method is deprecated\n",
1363 driver
->driver
.name
);
1364 dev_warn(&adap
->dev
, "Please use another way to instantiate "
1365 "your i2c_client\n");
1366 /* We ignore the return code; if it fails, too bad */
1367 driver
->attach_adapter(adap
);
1372 static int __process_new_adapter(struct device_driver
*d
, void *data
)
1374 return i2c_do_add_adapter(to_i2c_driver(d
), data
);
1377 static int i2c_register_adapter(struct i2c_adapter
*adap
)
1381 /* Can't register until after driver model init */
1382 if (unlikely(WARN_ON(!i2c_bus_type
.p
))) {
1388 if (unlikely(adap
->name
[0] == '\0')) {
1389 pr_err("i2c-core: Attempt to register an adapter with "
1393 if (unlikely(!adap
->algo
)) {
1394 pr_err("i2c-core: Attempt to register adapter '%s' with "
1395 "no algo!\n", adap
->name
);
1399 rt_mutex_init(&adap
->bus_lock
);
1400 mutex_init(&adap
->userspace_clients_lock
);
1401 INIT_LIST_HEAD(&adap
->userspace_clients
);
1403 /* Set default timeout to 1 second if not already set */
1404 if (adap
->timeout
== 0)
1407 dev_set_name(&adap
->dev
, "i2c-%d", adap
->nr
);
1408 adap
->dev
.bus
= &i2c_bus_type
;
1409 adap
->dev
.type
= &i2c_adapter_type
;
1410 res
= device_register(&adap
->dev
);
1414 dev_dbg(&adap
->dev
, "adapter [%s] registered\n", adap
->name
);
1416 #ifdef CONFIG_I2C_COMPAT
1417 res
= class_compat_create_link(i2c_adapter_compat_class
, &adap
->dev
,
1420 dev_warn(&adap
->dev
,
1421 "Failed to create compatibility class link\n");
1424 /* bus recovery specific initialization */
1425 if (adap
->bus_recovery_info
) {
1426 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
1428 if (!bri
->recover_bus
) {
1429 dev_err(&adap
->dev
, "No recover_bus() found, not using recovery\n");
1430 adap
->bus_recovery_info
= NULL
;
1434 /* Generic GPIO recovery */
1435 if (bri
->recover_bus
== i2c_generic_gpio_recovery
) {
1436 if (!gpio_is_valid(bri
->scl_gpio
)) {
1437 dev_err(&adap
->dev
, "Invalid SCL gpio, not using recovery\n");
1438 adap
->bus_recovery_info
= NULL
;
1442 if (gpio_is_valid(bri
->sda_gpio
))
1443 bri
->get_sda
= get_sda_gpio_value
;
1445 bri
->get_sda
= NULL
;
1447 bri
->get_scl
= get_scl_gpio_value
;
1448 bri
->set_scl
= set_scl_gpio_value
;
1449 } else if (!bri
->set_scl
|| !bri
->get_scl
) {
1450 /* Generic SCL recovery */
1451 dev_err(&adap
->dev
, "No {get|set}_gpio() found, not using recovery\n");
1452 adap
->bus_recovery_info
= NULL
;
1457 /* create pre-declared device nodes */
1458 of_i2c_register_devices(adap
);
1459 acpi_i2c_register_devices(adap
);
1460 acpi_i2c_install_space_handler(adap
);
1462 if (adap
->nr
< __i2c_first_dynamic_bus_num
)
1463 i2c_scan_static_board_info(adap
);
1465 /* Notify drivers */
1466 mutex_lock(&core_lock
);
1467 bus_for_each_drv(&i2c_bus_type
, NULL
, adap
, __process_new_adapter
);
1468 mutex_unlock(&core_lock
);
1473 mutex_lock(&core_lock
);
1474 idr_remove(&i2c_adapter_idr
, adap
->nr
);
1475 mutex_unlock(&core_lock
);
1480 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1481 * @adap: the adapter to register (with adap->nr initialized)
1482 * Context: can sleep
1484 * See i2c_add_numbered_adapter() for details.
1486 static int __i2c_add_numbered_adapter(struct i2c_adapter
*adap
)
1490 mutex_lock(&core_lock
);
1491 id
= idr_alloc(&i2c_adapter_idr
, adap
, adap
->nr
, adap
->nr
+ 1,
1493 mutex_unlock(&core_lock
);
1495 return id
== -ENOSPC
? -EBUSY
: id
;
1497 return i2c_register_adapter(adap
);
1501 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1502 * @adapter: the adapter to add
1503 * Context: can sleep
1505 * This routine is used to declare an I2C adapter when its bus number
1506 * doesn't matter or when its bus number is specified by an dt alias.
1507 * Examples of bases when the bus number doesn't matter: I2C adapters
1508 * dynamically added by USB links or PCI plugin cards.
1510 * When this returns zero, a new bus number was allocated and stored
1511 * in adap->nr, and the specified adapter became available for clients.
1512 * Otherwise, a negative errno value is returned.
1514 int i2c_add_adapter(struct i2c_adapter
*adapter
)
1516 struct device
*dev
= &adapter
->dev
;
1520 id
= of_alias_get_id(dev
->of_node
, "i2c");
1523 return __i2c_add_numbered_adapter(adapter
);
1527 mutex_lock(&core_lock
);
1528 id
= idr_alloc(&i2c_adapter_idr
, adapter
,
1529 __i2c_first_dynamic_bus_num
, 0, GFP_KERNEL
);
1530 mutex_unlock(&core_lock
);
1536 return i2c_register_adapter(adapter
);
1538 EXPORT_SYMBOL(i2c_add_adapter
);
1541 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1542 * @adap: the adapter to register (with adap->nr initialized)
1543 * Context: can sleep
1545 * This routine is used to declare an I2C adapter when its bus number
1546 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1547 * or otherwise built in to the system's mainboard, and where i2c_board_info
1548 * is used to properly configure I2C devices.
1550 * If the requested bus number is set to -1, then this function will behave
1551 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1553 * If no devices have pre-been declared for this bus, then be sure to
1554 * register the adapter before any dynamically allocated ones. Otherwise
1555 * the required bus ID may not be available.
1557 * When this returns zero, the specified adapter became available for
1558 * clients using the bus number provided in adap->nr. Also, the table
1559 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1560 * and the appropriate driver model device nodes are created. Otherwise, a
1561 * negative errno value is returned.
1563 int i2c_add_numbered_adapter(struct i2c_adapter
*adap
)
1565 if (adap
->nr
== -1) /* -1 means dynamically assign bus id */
1566 return i2c_add_adapter(adap
);
1568 return __i2c_add_numbered_adapter(adap
);
1570 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter
);
1572 static void i2c_do_del_adapter(struct i2c_driver
*driver
,
1573 struct i2c_adapter
*adapter
)
1575 struct i2c_client
*client
, *_n
;
1577 /* Remove the devices we created ourselves as the result of hardware
1578 * probing (using a driver's detect method) */
1579 list_for_each_entry_safe(client
, _n
, &driver
->clients
, detected
) {
1580 if (client
->adapter
== adapter
) {
1581 dev_dbg(&adapter
->dev
, "Removing %s at 0x%x\n",
1582 client
->name
, client
->addr
);
1583 list_del(&client
->detected
);
1584 i2c_unregister_device(client
);
1589 static int __unregister_client(struct device
*dev
, void *dummy
)
1591 struct i2c_client
*client
= i2c_verify_client(dev
);
1592 if (client
&& strcmp(client
->name
, "dummy"))
1593 i2c_unregister_device(client
);
1597 static int __unregister_dummy(struct device
*dev
, void *dummy
)
1599 struct i2c_client
*client
= i2c_verify_client(dev
);
1601 i2c_unregister_device(client
);
1605 static int __process_removed_adapter(struct device_driver
*d
, void *data
)
1607 i2c_do_del_adapter(to_i2c_driver(d
), data
);
1612 * i2c_del_adapter - unregister I2C adapter
1613 * @adap: the adapter being unregistered
1614 * Context: can sleep
1616 * This unregisters an I2C adapter which was previously registered
1617 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1619 void i2c_del_adapter(struct i2c_adapter
*adap
)
1621 struct i2c_adapter
*found
;
1622 struct i2c_client
*client
, *next
;
1624 /* First make sure that this adapter was ever added */
1625 mutex_lock(&core_lock
);
1626 found
= idr_find(&i2c_adapter_idr
, adap
->nr
);
1627 mutex_unlock(&core_lock
);
1628 if (found
!= adap
) {
1629 pr_debug("i2c-core: attempting to delete unregistered "
1630 "adapter [%s]\n", adap
->name
);
1634 acpi_i2c_remove_space_handler(adap
);
1635 /* Tell drivers about this removal */
1636 mutex_lock(&core_lock
);
1637 bus_for_each_drv(&i2c_bus_type
, NULL
, adap
,
1638 __process_removed_adapter
);
1639 mutex_unlock(&core_lock
);
1641 /* Remove devices instantiated from sysfs */
1642 mutex_lock_nested(&adap
->userspace_clients_lock
,
1643 i2c_adapter_depth(adap
));
1644 list_for_each_entry_safe(client
, next
, &adap
->userspace_clients
,
1646 dev_dbg(&adap
->dev
, "Removing %s at 0x%x\n", client
->name
,
1648 list_del(&client
->detected
);
1649 i2c_unregister_device(client
);
1651 mutex_unlock(&adap
->userspace_clients_lock
);
1653 /* Detach any active clients. This can't fail, thus we do not
1654 * check the returned value. This is a two-pass process, because
1655 * we can't remove the dummy devices during the first pass: they
1656 * could have been instantiated by real devices wishing to clean
1657 * them up properly, so we give them a chance to do that first. */
1658 device_for_each_child(&adap
->dev
, NULL
, __unregister_client
);
1659 device_for_each_child(&adap
->dev
, NULL
, __unregister_dummy
);
1661 #ifdef CONFIG_I2C_COMPAT
1662 class_compat_remove_link(i2c_adapter_compat_class
, &adap
->dev
,
1666 /* device name is gone after device_unregister */
1667 dev_dbg(&adap
->dev
, "adapter [%s] unregistered\n", adap
->name
);
1669 /* wait until all references to the device are gone
1671 * FIXME: This is old code and should ideally be replaced by an
1672 * alternative which results in decoupling the lifetime of the struct
1673 * device from the i2c_adapter, like spi or netdev do. Any solution
1674 * should be throughly tested with DEBUG_KOBJECT_RELEASE enabled!
1676 init_completion(&adap
->dev_released
);
1677 device_unregister(&adap
->dev
);
1678 wait_for_completion(&adap
->dev_released
);
1681 mutex_lock(&core_lock
);
1682 idr_remove(&i2c_adapter_idr
, adap
->nr
);
1683 mutex_unlock(&core_lock
);
1685 /* Clear the device structure in case this adapter is ever going to be
1687 memset(&adap
->dev
, 0, sizeof(adap
->dev
));
1689 EXPORT_SYMBOL(i2c_del_adapter
);
1691 /* ------------------------------------------------------------------------- */
1693 int i2c_for_each_dev(void *data
, int (*fn
)(struct device
*, void *))
1697 mutex_lock(&core_lock
);
1698 res
= bus_for_each_dev(&i2c_bus_type
, NULL
, data
, fn
);
1699 mutex_unlock(&core_lock
);
1703 EXPORT_SYMBOL_GPL(i2c_for_each_dev
);
1705 static int __process_new_driver(struct device
*dev
, void *data
)
1707 if (dev
->type
!= &i2c_adapter_type
)
1709 return i2c_do_add_adapter(data
, to_i2c_adapter(dev
));
1713 * An i2c_driver is used with one or more i2c_client (device) nodes to access
1714 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1717 int i2c_register_driver(struct module
*owner
, struct i2c_driver
*driver
)
1721 /* Can't register until after driver model init */
1722 if (unlikely(WARN_ON(!i2c_bus_type
.p
)))
1725 /* add the driver to the list of i2c drivers in the driver core */
1726 driver
->driver
.owner
= owner
;
1727 driver
->driver
.bus
= &i2c_bus_type
;
1729 /* When registration returns, the driver core
1730 * will have called probe() for all matching-but-unbound devices.
1732 res
= driver_register(&driver
->driver
);
1736 pr_debug("i2c-core: driver [%s] registered\n", driver
->driver
.name
);
1738 INIT_LIST_HEAD(&driver
->clients
);
1739 /* Walk the adapters that are already present */
1740 i2c_for_each_dev(driver
, __process_new_driver
);
1744 EXPORT_SYMBOL(i2c_register_driver
);
1746 static int __process_removed_driver(struct device
*dev
, void *data
)
1748 if (dev
->type
== &i2c_adapter_type
)
1749 i2c_do_del_adapter(data
, to_i2c_adapter(dev
));
1754 * i2c_del_driver - unregister I2C driver
1755 * @driver: the driver being unregistered
1756 * Context: can sleep
1758 void i2c_del_driver(struct i2c_driver
*driver
)
1760 i2c_for_each_dev(driver
, __process_removed_driver
);
1762 driver_unregister(&driver
->driver
);
1763 pr_debug("i2c-core: driver [%s] unregistered\n", driver
->driver
.name
);
1765 EXPORT_SYMBOL(i2c_del_driver
);
1767 /* ------------------------------------------------------------------------- */
1770 * i2c_use_client - increments the reference count of the i2c client structure
1771 * @client: the client being referenced
1773 * Each live reference to a client should be refcounted. The driver model does
1774 * that automatically as part of driver binding, so that most drivers don't
1775 * need to do this explicitly: they hold a reference until they're unbound
1778 * A pointer to the client with the incremented reference counter is returned.
1780 struct i2c_client
*i2c_use_client(struct i2c_client
*client
)
1782 if (client
&& get_device(&client
->dev
))
1786 EXPORT_SYMBOL(i2c_use_client
);
1789 * i2c_release_client - release a use of the i2c client structure
1790 * @client: the client being no longer referenced
1792 * Must be called when a user of a client is finished with it.
1794 void i2c_release_client(struct i2c_client
*client
)
1797 put_device(&client
->dev
);
1799 EXPORT_SYMBOL(i2c_release_client
);
1801 struct i2c_cmd_arg
{
1806 static int i2c_cmd(struct device
*dev
, void *_arg
)
1808 struct i2c_client
*client
= i2c_verify_client(dev
);
1809 struct i2c_cmd_arg
*arg
= _arg
;
1810 struct i2c_driver
*driver
;
1812 if (!client
|| !client
->dev
.driver
)
1815 driver
= to_i2c_driver(client
->dev
.driver
);
1816 if (driver
->command
)
1817 driver
->command(client
, arg
->cmd
, arg
->arg
);
1821 void i2c_clients_command(struct i2c_adapter
*adap
, unsigned int cmd
, void *arg
)
1823 struct i2c_cmd_arg cmd_arg
;
1827 device_for_each_child(&adap
->dev
, &cmd_arg
, i2c_cmd
);
1829 EXPORT_SYMBOL(i2c_clients_command
);
1831 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
1832 static int of_i2c_notify(struct notifier_block
*nb
, unsigned long action
,
1835 struct of_reconfig_data
*rd
= arg
;
1836 struct i2c_adapter
*adap
;
1837 struct i2c_client
*client
;
1839 switch (of_reconfig_get_state_change(action
, rd
)) {
1840 case OF_RECONFIG_CHANGE_ADD
:
1841 adap
= of_find_i2c_adapter_by_node(rd
->dn
->parent
);
1843 return NOTIFY_OK
; /* not for us */
1845 client
= of_i2c_register_device(adap
, rd
->dn
);
1846 put_device(&adap
->dev
);
1848 if (IS_ERR(client
)) {
1849 pr_err("%s: failed to create for '%s'\n",
1850 __func__
, rd
->dn
->full_name
);
1851 return notifier_from_errno(PTR_ERR(client
));
1854 case OF_RECONFIG_CHANGE_REMOVE
:
1855 /* find our device by node */
1856 client
= of_find_i2c_device_by_node(rd
->dn
);
1858 return NOTIFY_OK
; /* no? not meant for us */
1860 /* unregister takes one ref away */
1861 i2c_unregister_device(client
);
1863 /* and put the reference of the find */
1864 put_device(&client
->dev
);
1870 static struct notifier_block i2c_of_notifier
= {
1871 .notifier_call
= of_i2c_notify
,
1874 extern struct notifier_block i2c_of_notifier
;
1875 #endif /* CONFIG_OF_DYNAMIC */
1877 static int __init
i2c_init(void)
1881 retval
= of_alias_get_highest_id("i2c");
1883 down_write(&__i2c_board_lock
);
1884 if (retval
>= __i2c_first_dynamic_bus_num
)
1885 __i2c_first_dynamic_bus_num
= retval
+ 1;
1886 up_write(&__i2c_board_lock
);
1888 retval
= bus_register(&i2c_bus_type
);
1891 #ifdef CONFIG_I2C_COMPAT
1892 i2c_adapter_compat_class
= class_compat_register("i2c-adapter");
1893 if (!i2c_adapter_compat_class
) {
1898 retval
= i2c_add_driver(&dummy_driver
);
1902 if (IS_ENABLED(CONFIG_OF_DYNAMIC
))
1903 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier
));
1908 #ifdef CONFIG_I2C_COMPAT
1909 class_compat_unregister(i2c_adapter_compat_class
);
1912 bus_unregister(&i2c_bus_type
);
1916 static void __exit
i2c_exit(void)
1918 if (IS_ENABLED(CONFIG_OF_DYNAMIC
))
1919 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier
));
1920 i2c_del_driver(&dummy_driver
);
1921 #ifdef CONFIG_I2C_COMPAT
1922 class_compat_unregister(i2c_adapter_compat_class
);
1924 bus_unregister(&i2c_bus_type
);
1925 tracepoint_synchronize_unregister();
1928 /* We must initialize early, because some subsystems register i2c drivers
1929 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1931 postcore_initcall(i2c_init
);
1932 module_exit(i2c_exit
);
1934 /* ----------------------------------------------------
1935 * the functional interface to the i2c busses.
1936 * ----------------------------------------------------
1939 /* Check if val is exceeding the quirk IFF quirk is non 0 */
1940 #define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1942 static int i2c_quirk_error(struct i2c_adapter
*adap
, struct i2c_msg
*msg
, char *err_msg
)
1944 dev_err_ratelimited(&adap
->dev
, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
1945 err_msg
, msg
->addr
, msg
->len
,
1946 msg
->flags
& I2C_M_RD
? "read" : "write");
1950 static int i2c_check_for_quirks(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
1952 const struct i2c_adapter_quirks
*q
= adap
->quirks
;
1953 int max_num
= q
->max_num_msgs
, i
;
1954 bool do_len_check
= true;
1956 if (q
->flags
& I2C_AQ_COMB
) {
1959 /* special checks for combined messages */
1961 if (q
->flags
& I2C_AQ_COMB_WRITE_FIRST
&& msgs
[0].flags
& I2C_M_RD
)
1962 return i2c_quirk_error(adap
, &msgs
[0], "1st comb msg must be write");
1964 if (q
->flags
& I2C_AQ_COMB_READ_SECOND
&& !(msgs
[1].flags
& I2C_M_RD
))
1965 return i2c_quirk_error(adap
, &msgs
[1], "2nd comb msg must be read");
1967 if (q
->flags
& I2C_AQ_COMB_SAME_ADDR
&& msgs
[0].addr
!= msgs
[1].addr
)
1968 return i2c_quirk_error(adap
, &msgs
[0], "comb msg only to same addr");
1970 if (i2c_quirk_exceeded(msgs
[0].len
, q
->max_comb_1st_msg_len
))
1971 return i2c_quirk_error(adap
, &msgs
[0], "msg too long");
1973 if (i2c_quirk_exceeded(msgs
[1].len
, q
->max_comb_2nd_msg_len
))
1974 return i2c_quirk_error(adap
, &msgs
[1], "msg too long");
1976 do_len_check
= false;
1980 if (i2c_quirk_exceeded(num
, max_num
))
1981 return i2c_quirk_error(adap
, &msgs
[0], "too many messages");
1983 for (i
= 0; i
< num
; i
++) {
1984 u16 len
= msgs
[i
].len
;
1986 if (msgs
[i
].flags
& I2C_M_RD
) {
1987 if (do_len_check
&& i2c_quirk_exceeded(len
, q
->max_read_len
))
1988 return i2c_quirk_error(adap
, &msgs
[i
], "msg too long");
1990 if (do_len_check
&& i2c_quirk_exceeded(len
, q
->max_write_len
))
1991 return i2c_quirk_error(adap
, &msgs
[i
], "msg too long");
1999 * __i2c_transfer - unlocked flavor of i2c_transfer
2000 * @adap: Handle to I2C bus
2001 * @msgs: One or more messages to execute before STOP is issued to
2002 * terminate the operation; each message begins with a START.
2003 * @num: Number of messages to be executed.
2005 * Returns negative errno, else the number of messages executed.
2007 * Adapter lock must be held when calling this function. No debug logging
2008 * takes place. adap->algo->master_xfer existence isn't checked.
2010 int __i2c_transfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
2012 unsigned long orig_jiffies
;
2015 if (adap
->quirks
&& i2c_check_for_quirks(adap
, msgs
, num
))
2018 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2019 * enabled. This is an efficient way of keeping the for-loop from
2020 * being executed when not needed.
2022 if (static_key_false(&i2c_trace_msg
)) {
2024 for (i
= 0; i
< num
; i
++)
2025 if (msgs
[i
].flags
& I2C_M_RD
)
2026 trace_i2c_read(adap
, &msgs
[i
], i
);
2028 trace_i2c_write(adap
, &msgs
[i
], i
);
2031 /* Retry automatically on arbitration loss */
2032 orig_jiffies
= jiffies
;
2033 for (ret
= 0, try = 0; try <= adap
->retries
; try++) {
2034 ret
= adap
->algo
->master_xfer(adap
, msgs
, num
);
2037 if (time_after(jiffies
, orig_jiffies
+ adap
->timeout
))
2041 if (static_key_false(&i2c_trace_msg
)) {
2043 for (i
= 0; i
< ret
; i
++)
2044 if (msgs
[i
].flags
& I2C_M_RD
)
2045 trace_i2c_reply(adap
, &msgs
[i
], i
);
2046 trace_i2c_result(adap
, i
, ret
);
2051 EXPORT_SYMBOL(__i2c_transfer
);
2054 * i2c_transfer - execute a single or combined I2C message
2055 * @adap: Handle to I2C bus
2056 * @msgs: One or more messages to execute before STOP is issued to
2057 * terminate the operation; each message begins with a START.
2058 * @num: Number of messages to be executed.
2060 * Returns negative errno, else the number of messages executed.
2062 * Note that there is no requirement that each message be sent to
2063 * the same slave address, although that is the most common model.
2065 int i2c_transfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
2069 /* REVISIT the fault reporting model here is weak:
2071 * - When we get an error after receiving N bytes from a slave,
2072 * there is no way to report "N".
2074 * - When we get a NAK after transmitting N bytes to a slave,
2075 * there is no way to report "N" ... or to let the master
2076 * continue executing the rest of this combined message, if
2077 * that's the appropriate response.
2079 * - When for example "num" is two and we successfully complete
2080 * the first message but get an error part way through the
2081 * second, it's unclear whether that should be reported as
2082 * one (discarding status on the second message) or errno
2083 * (discarding status on the first one).
2086 if (adap
->algo
->master_xfer
) {
2088 for (ret
= 0; ret
< num
; ret
++) {
2089 dev_dbg(&adap
->dev
, "master_xfer[%d] %c, addr=0x%02x, "
2090 "len=%d%s\n", ret
, (msgs
[ret
].flags
& I2C_M_RD
)
2091 ? 'R' : 'W', msgs
[ret
].addr
, msgs
[ret
].len
,
2092 (msgs
[ret
].flags
& I2C_M_RECV_LEN
) ? "+" : "");
2096 if (in_atomic() || irqs_disabled()) {
2097 ret
= i2c_trylock_adapter(adap
);
2099 /* I2C activity is ongoing. */
2102 i2c_lock_adapter(adap
);
2105 ret
= __i2c_transfer(adap
, msgs
, num
);
2106 i2c_unlock_adapter(adap
);
2110 dev_dbg(&adap
->dev
, "I2C level transfers not supported\n");
2114 EXPORT_SYMBOL(i2c_transfer
);
2117 * i2c_master_send - issue a single I2C message in master transmit mode
2118 * @client: Handle to slave device
2119 * @buf: Data that will be written to the slave
2120 * @count: How many bytes to write, must be less than 64k since msg.len is u16
2122 * Returns negative errno, or else the number of bytes written.
2124 int i2c_master_send(const struct i2c_client
*client
, const char *buf
, int count
)
2127 struct i2c_adapter
*adap
= client
->adapter
;
2130 msg
.addr
= client
->addr
;
2131 msg
.flags
= client
->flags
& I2C_M_TEN
;
2133 msg
.buf
= (char *)buf
;
2135 ret
= i2c_transfer(adap
, &msg
, 1);
2138 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2139 * transmitted, else error code.
2141 return (ret
== 1) ? count
: ret
;
2143 EXPORT_SYMBOL(i2c_master_send
);
2146 * i2c_master_recv - issue a single I2C message in master receive mode
2147 * @client: Handle to slave device
2148 * @buf: Where to store data read from slave
2149 * @count: How many bytes to read, must be less than 64k since msg.len is u16
2151 * Returns negative errno, or else the number of bytes read.
2153 int i2c_master_recv(const struct i2c_client
*client
, char *buf
, int count
)
2155 struct i2c_adapter
*adap
= client
->adapter
;
2159 msg
.addr
= client
->addr
;
2160 msg
.flags
= client
->flags
& I2C_M_TEN
;
2161 msg
.flags
|= I2C_M_RD
;
2165 ret
= i2c_transfer(adap
, &msg
, 1);
2168 * If everything went ok (i.e. 1 msg received), return #bytes received,
2171 return (ret
== 1) ? count
: ret
;
2173 EXPORT_SYMBOL(i2c_master_recv
);
2175 /* ----------------------------------------------------
2176 * the i2c address scanning function
2177 * Will not work for 10-bit addresses!
2178 * ----------------------------------------------------
2182 * Legacy default probe function, mostly relevant for SMBus. The default
2183 * probe method is a quick write, but it is known to corrupt the 24RF08
2184 * EEPROMs due to a state machine bug, and could also irreversibly
2185 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2186 * we use a short byte read instead. Also, some bus drivers don't implement
2187 * quick write, so we fallback to a byte read in that case too.
2188 * On x86, there is another special case for FSC hardware monitoring chips,
2189 * which want regular byte reads (address 0x73.) Fortunately, these are the
2190 * only known chips using this I2C address on PC hardware.
2191 * Returns 1 if probe succeeded, 0 if not.
2193 static int i2c_default_probe(struct i2c_adapter
*adap
, unsigned short addr
)
2196 union i2c_smbus_data dummy
;
2199 if (addr
== 0x73 && (adap
->class & I2C_CLASS_HWMON
)
2200 && i2c_check_functionality(adap
, I2C_FUNC_SMBUS_READ_BYTE_DATA
))
2201 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
2202 I2C_SMBUS_BYTE_DATA
, &dummy
);
2205 if (!((addr
& ~0x07) == 0x30 || (addr
& ~0x0f) == 0x50)
2206 && i2c_check_functionality(adap
, I2C_FUNC_SMBUS_QUICK
))
2207 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_WRITE
, 0,
2208 I2C_SMBUS_QUICK
, NULL
);
2209 else if (i2c_check_functionality(adap
, I2C_FUNC_SMBUS_READ_BYTE
))
2210 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
2211 I2C_SMBUS_BYTE
, &dummy
);
2213 dev_warn(&adap
->dev
, "No suitable probing method supported for address 0x%02X\n",
2221 static int i2c_detect_address(struct i2c_client
*temp_client
,
2222 struct i2c_driver
*driver
)
2224 struct i2c_board_info info
;
2225 struct i2c_adapter
*adapter
= temp_client
->adapter
;
2226 int addr
= temp_client
->addr
;
2229 /* Make sure the address is valid */
2230 err
= i2c_check_addr_validity(addr
);
2232 dev_warn(&adapter
->dev
, "Invalid probe address 0x%02x\n",
2237 /* Skip if already in use */
2238 if (i2c_check_addr_busy(adapter
, addr
))
2241 /* Make sure there is something at this address */
2242 if (!i2c_default_probe(adapter
, addr
))
2245 /* Finally call the custom detection function */
2246 memset(&info
, 0, sizeof(struct i2c_board_info
));
2248 err
= driver
->detect(temp_client
, &info
);
2250 /* -ENODEV is returned if the detection fails. We catch it
2251 here as this isn't an error. */
2252 return err
== -ENODEV
? 0 : err
;
2255 /* Consistency check */
2256 if (info
.type
[0] == '\0') {
2257 dev_err(&adapter
->dev
, "%s detection function provided "
2258 "no name for 0x%x\n", driver
->driver
.name
,
2261 struct i2c_client
*client
;
2263 /* Detection succeeded, instantiate the device */
2264 if (adapter
->class & I2C_CLASS_DEPRECATED
)
2265 dev_warn(&adapter
->dev
,
2266 "This adapter will soon drop class based instantiation of devices. "
2267 "Please make sure client 0x%02x gets instantiated by other means. "
2268 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2271 dev_dbg(&adapter
->dev
, "Creating %s at 0x%02x\n",
2272 info
.type
, info
.addr
);
2273 client
= i2c_new_device(adapter
, &info
);
2275 list_add_tail(&client
->detected
, &driver
->clients
);
2277 dev_err(&adapter
->dev
, "Failed creating %s at 0x%02x\n",
2278 info
.type
, info
.addr
);
2283 static int i2c_detect(struct i2c_adapter
*adapter
, struct i2c_driver
*driver
)
2285 const unsigned short *address_list
;
2286 struct i2c_client
*temp_client
;
2288 int adap_id
= i2c_adapter_id(adapter
);
2290 address_list
= driver
->address_list
;
2291 if (!driver
->detect
|| !address_list
)
2294 /* Warn that the adapter lost class based instantiation */
2295 if (adapter
->class == I2C_CLASS_DEPRECATED
) {
2296 dev_dbg(&adapter
->dev
,
2297 "This adapter dropped support for I2C classes and "
2298 "won't auto-detect %s devices anymore. If you need it, check "
2299 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2300 driver
->driver
.name
);
2304 /* Stop here if the classes do not match */
2305 if (!(adapter
->class & driver
->class))
2308 /* Set up a temporary client to help detect callback */
2309 temp_client
= kzalloc(sizeof(struct i2c_client
), GFP_KERNEL
);
2312 temp_client
->adapter
= adapter
;
2314 for (i
= 0; address_list
[i
] != I2C_CLIENT_END
; i
+= 1) {
2315 dev_dbg(&adapter
->dev
, "found normal entry for adapter %d, "
2316 "addr 0x%02x\n", adap_id
, address_list
[i
]);
2317 temp_client
->addr
= address_list
[i
];
2318 err
= i2c_detect_address(temp_client
, driver
);
2327 int i2c_probe_func_quick_read(struct i2c_adapter
*adap
, unsigned short addr
)
2329 return i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
2330 I2C_SMBUS_QUICK
, NULL
) >= 0;
2332 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read
);
2335 i2c_new_probed_device(struct i2c_adapter
*adap
,
2336 struct i2c_board_info
*info
,
2337 unsigned short const *addr_list
,
2338 int (*probe
)(struct i2c_adapter
*, unsigned short addr
))
2343 probe
= i2c_default_probe
;
2345 for (i
= 0; addr_list
[i
] != I2C_CLIENT_END
; i
++) {
2346 /* Check address validity */
2347 if (i2c_check_addr_validity(addr_list
[i
]) < 0) {
2348 dev_warn(&adap
->dev
, "Invalid 7-bit address "
2349 "0x%02x\n", addr_list
[i
]);
2353 /* Check address availability */
2354 if (i2c_check_addr_busy(adap
, addr_list
[i
])) {
2355 dev_dbg(&adap
->dev
, "Address 0x%02x already in "
2356 "use, not probing\n", addr_list
[i
]);
2360 /* Test address responsiveness */
2361 if (probe(adap
, addr_list
[i
]))
2365 if (addr_list
[i
] == I2C_CLIENT_END
) {
2366 dev_dbg(&adap
->dev
, "Probing failed, no device found\n");
2370 info
->addr
= addr_list
[i
];
2371 return i2c_new_device(adap
, info
);
2373 EXPORT_SYMBOL_GPL(i2c_new_probed_device
);
2375 struct i2c_adapter
*i2c_get_adapter(int nr
)
2377 struct i2c_adapter
*adapter
;
2379 mutex_lock(&core_lock
);
2380 adapter
= idr_find(&i2c_adapter_idr
, nr
);
2381 if (adapter
&& !try_module_get(adapter
->owner
))
2384 mutex_unlock(&core_lock
);
2387 EXPORT_SYMBOL(i2c_get_adapter
);
2389 void i2c_put_adapter(struct i2c_adapter
*adap
)
2392 module_put(adap
->owner
);
2394 EXPORT_SYMBOL(i2c_put_adapter
);
2396 /* The SMBus parts */
2398 #define POLY (0x1070U << 3)
2399 static u8
crc8(u16 data
)
2403 for (i
= 0; i
< 8; i
++) {
2408 return (u8
)(data
>> 8);
2411 /* Incremental CRC8 over count bytes in the array pointed to by p */
2412 static u8
i2c_smbus_pec(u8 crc
, u8
*p
, size_t count
)
2416 for (i
= 0; i
< count
; i
++)
2417 crc
= crc8((crc
^ p
[i
]) << 8);
2421 /* Assume a 7-bit address, which is reasonable for SMBus */
2422 static u8
i2c_smbus_msg_pec(u8 pec
, struct i2c_msg
*msg
)
2424 /* The address will be sent first */
2425 u8 addr
= (msg
->addr
<< 1) | !!(msg
->flags
& I2C_M_RD
);
2426 pec
= i2c_smbus_pec(pec
, &addr
, 1);
2428 /* The data buffer follows */
2429 return i2c_smbus_pec(pec
, msg
->buf
, msg
->len
);
2432 /* Used for write only transactions */
2433 static inline void i2c_smbus_add_pec(struct i2c_msg
*msg
)
2435 msg
->buf
[msg
->len
] = i2c_smbus_msg_pec(0, msg
);
2439 /* Return <0 on CRC error
2440 If there was a write before this read (most cases) we need to take the
2441 partial CRC from the write part into account.
2442 Note that this function does modify the message (we need to decrease the
2443 message length to hide the CRC byte from the caller). */
2444 static int i2c_smbus_check_pec(u8 cpec
, struct i2c_msg
*msg
)
2446 u8 rpec
= msg
->buf
[--msg
->len
];
2447 cpec
= i2c_smbus_msg_pec(cpec
, msg
);
2450 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2458 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2459 * @client: Handle to slave device
2461 * This executes the SMBus "receive byte" protocol, returning negative errno
2462 * else the byte received from the device.
2464 s32
i2c_smbus_read_byte(const struct i2c_client
*client
)
2466 union i2c_smbus_data data
;
2469 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2471 I2C_SMBUS_BYTE
, &data
);
2472 return (status
< 0) ? status
: data
.byte
;
2474 EXPORT_SYMBOL(i2c_smbus_read_byte
);
2477 * i2c_smbus_write_byte - SMBus "send byte" protocol
2478 * @client: Handle to slave device
2479 * @value: Byte to be sent
2481 * This executes the SMBus "send byte" protocol, returning negative errno
2482 * else zero on success.
2484 s32
i2c_smbus_write_byte(const struct i2c_client
*client
, u8 value
)
2486 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2487 I2C_SMBUS_WRITE
, value
, I2C_SMBUS_BYTE
, NULL
);
2489 EXPORT_SYMBOL(i2c_smbus_write_byte
);
2492 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2493 * @client: Handle to slave device
2494 * @command: Byte interpreted by slave
2496 * This executes the SMBus "read byte" protocol, returning negative errno
2497 * else a data byte received from the device.
2499 s32
i2c_smbus_read_byte_data(const struct i2c_client
*client
, u8 command
)
2501 union i2c_smbus_data data
;
2504 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2505 I2C_SMBUS_READ
, command
,
2506 I2C_SMBUS_BYTE_DATA
, &data
);
2507 return (status
< 0) ? status
: data
.byte
;
2509 EXPORT_SYMBOL(i2c_smbus_read_byte_data
);
2512 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2513 * @client: Handle to slave device
2514 * @command: Byte interpreted by slave
2515 * @value: Byte being written
2517 * This executes the SMBus "write byte" protocol, returning negative errno
2518 * else zero on success.
2520 s32
i2c_smbus_write_byte_data(const struct i2c_client
*client
, u8 command
,
2523 union i2c_smbus_data data
;
2525 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2526 I2C_SMBUS_WRITE
, command
,
2527 I2C_SMBUS_BYTE_DATA
, &data
);
2529 EXPORT_SYMBOL(i2c_smbus_write_byte_data
);
2532 * i2c_smbus_read_word_data - SMBus "read word" protocol
2533 * @client: Handle to slave device
2534 * @command: Byte interpreted by slave
2536 * This executes the SMBus "read word" protocol, returning negative errno
2537 * else a 16-bit unsigned "word" received from the device.
2539 s32
i2c_smbus_read_word_data(const struct i2c_client
*client
, u8 command
)
2541 union i2c_smbus_data data
;
2544 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2545 I2C_SMBUS_READ
, command
,
2546 I2C_SMBUS_WORD_DATA
, &data
);
2547 return (status
< 0) ? status
: data
.word
;
2549 EXPORT_SYMBOL(i2c_smbus_read_word_data
);
2552 * i2c_smbus_write_word_data - SMBus "write word" protocol
2553 * @client: Handle to slave device
2554 * @command: Byte interpreted by slave
2555 * @value: 16-bit "word" being written
2557 * This executes the SMBus "write word" protocol, returning negative errno
2558 * else zero on success.
2560 s32
i2c_smbus_write_word_data(const struct i2c_client
*client
, u8 command
,
2563 union i2c_smbus_data data
;
2565 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2566 I2C_SMBUS_WRITE
, command
,
2567 I2C_SMBUS_WORD_DATA
, &data
);
2569 EXPORT_SYMBOL(i2c_smbus_write_word_data
);
2572 * i2c_smbus_read_block_data - SMBus "block read" protocol
2573 * @client: Handle to slave device
2574 * @command: Byte interpreted by slave
2575 * @values: Byte array into which data will be read; big enough to hold
2576 * the data returned by the slave. SMBus allows at most 32 bytes.
2578 * This executes the SMBus "block read" protocol, returning negative errno
2579 * else the number of data bytes in the slave's response.
2581 * Note that using this function requires that the client's adapter support
2582 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2583 * support this; its emulation through I2C messaging relies on a specific
2584 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2586 s32
i2c_smbus_read_block_data(const struct i2c_client
*client
, u8 command
,
2589 union i2c_smbus_data data
;
2592 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2593 I2C_SMBUS_READ
, command
,
2594 I2C_SMBUS_BLOCK_DATA
, &data
);
2598 memcpy(values
, &data
.block
[1], data
.block
[0]);
2599 return data
.block
[0];
2601 EXPORT_SYMBOL(i2c_smbus_read_block_data
);
2604 * i2c_smbus_write_block_data - SMBus "block write" protocol
2605 * @client: Handle to slave device
2606 * @command: Byte interpreted by slave
2607 * @length: Size of data block; SMBus allows at most 32 bytes
2608 * @values: Byte array which will be written.
2610 * This executes the SMBus "block write" protocol, returning negative errno
2611 * else zero on success.
2613 s32
i2c_smbus_write_block_data(const struct i2c_client
*client
, u8 command
,
2614 u8 length
, const u8
*values
)
2616 union i2c_smbus_data data
;
2618 if (length
> I2C_SMBUS_BLOCK_MAX
)
2619 length
= I2C_SMBUS_BLOCK_MAX
;
2620 data
.block
[0] = length
;
2621 memcpy(&data
.block
[1], values
, length
);
2622 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2623 I2C_SMBUS_WRITE
, command
,
2624 I2C_SMBUS_BLOCK_DATA
, &data
);
2626 EXPORT_SYMBOL(i2c_smbus_write_block_data
);
2628 /* Returns the number of read bytes */
2629 s32
i2c_smbus_read_i2c_block_data(const struct i2c_client
*client
, u8 command
,
2630 u8 length
, u8
*values
)
2632 union i2c_smbus_data data
;
2635 if (length
> I2C_SMBUS_BLOCK_MAX
)
2636 length
= I2C_SMBUS_BLOCK_MAX
;
2637 data
.block
[0] = length
;
2638 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2639 I2C_SMBUS_READ
, command
,
2640 I2C_SMBUS_I2C_BLOCK_DATA
, &data
);
2644 memcpy(values
, &data
.block
[1], data
.block
[0]);
2645 return data
.block
[0];
2647 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data
);
2649 s32
i2c_smbus_write_i2c_block_data(const struct i2c_client
*client
, u8 command
,
2650 u8 length
, const u8
*values
)
2652 union i2c_smbus_data data
;
2654 if (length
> I2C_SMBUS_BLOCK_MAX
)
2655 length
= I2C_SMBUS_BLOCK_MAX
;
2656 data
.block
[0] = length
;
2657 memcpy(data
.block
+ 1, values
, length
);
2658 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2659 I2C_SMBUS_WRITE
, command
,
2660 I2C_SMBUS_I2C_BLOCK_DATA
, &data
);
2662 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data
);
2664 /* Simulate a SMBus command using the i2c protocol
2665 No checking of parameters is done! */
2666 static s32
i2c_smbus_xfer_emulated(struct i2c_adapter
*adapter
, u16 addr
,
2667 unsigned short flags
,
2668 char read_write
, u8 command
, int size
,
2669 union i2c_smbus_data
*data
)
2671 /* So we need to generate a series of msgs. In the case of writing, we
2672 need to use only one message; when reading, we need two. We initialize
2673 most things with sane defaults, to keep the code below somewhat
2675 unsigned char msgbuf0
[I2C_SMBUS_BLOCK_MAX
+3];
2676 unsigned char msgbuf1
[I2C_SMBUS_BLOCK_MAX
+2];
2677 int num
= read_write
== I2C_SMBUS_READ
? 2 : 1;
2681 struct i2c_msg msg
[2] = {
2689 .flags
= flags
| I2C_M_RD
,
2695 msgbuf0
[0] = command
;
2697 case I2C_SMBUS_QUICK
:
2699 /* Special case: The read/write field is used as data */
2700 msg
[0].flags
= flags
| (read_write
== I2C_SMBUS_READ
?
2704 case I2C_SMBUS_BYTE
:
2705 if (read_write
== I2C_SMBUS_READ
) {
2706 /* Special case: only a read! */
2707 msg
[0].flags
= I2C_M_RD
| flags
;
2711 case I2C_SMBUS_BYTE_DATA
:
2712 if (read_write
== I2C_SMBUS_READ
)
2716 msgbuf0
[1] = data
->byte
;
2719 case I2C_SMBUS_WORD_DATA
:
2720 if (read_write
== I2C_SMBUS_READ
)
2724 msgbuf0
[1] = data
->word
& 0xff;
2725 msgbuf0
[2] = data
->word
>> 8;
2728 case I2C_SMBUS_PROC_CALL
:
2729 num
= 2; /* Special case */
2730 read_write
= I2C_SMBUS_READ
;
2733 msgbuf0
[1] = data
->word
& 0xff;
2734 msgbuf0
[2] = data
->word
>> 8;
2736 case I2C_SMBUS_BLOCK_DATA
:
2737 if (read_write
== I2C_SMBUS_READ
) {
2738 msg
[1].flags
|= I2C_M_RECV_LEN
;
2739 msg
[1].len
= 1; /* block length will be added by
2740 the underlying bus driver */
2742 msg
[0].len
= data
->block
[0] + 2;
2743 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 2) {
2744 dev_err(&adapter
->dev
,
2745 "Invalid block write size %d\n",
2749 for (i
= 1; i
< msg
[0].len
; i
++)
2750 msgbuf0
[i
] = data
->block
[i
-1];
2753 case I2C_SMBUS_BLOCK_PROC_CALL
:
2754 num
= 2; /* Another special case */
2755 read_write
= I2C_SMBUS_READ
;
2756 if (data
->block
[0] > I2C_SMBUS_BLOCK_MAX
) {
2757 dev_err(&adapter
->dev
,
2758 "Invalid block write size %d\n",
2762 msg
[0].len
= data
->block
[0] + 2;
2763 for (i
= 1; i
< msg
[0].len
; i
++)
2764 msgbuf0
[i
] = data
->block
[i
-1];
2765 msg
[1].flags
|= I2C_M_RECV_LEN
;
2766 msg
[1].len
= 1; /* block length will be added by
2767 the underlying bus driver */
2769 case I2C_SMBUS_I2C_BLOCK_DATA
:
2770 if (read_write
== I2C_SMBUS_READ
) {
2771 msg
[1].len
= data
->block
[0];
2773 msg
[0].len
= data
->block
[0] + 1;
2774 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 1) {
2775 dev_err(&adapter
->dev
,
2776 "Invalid block write size %d\n",
2780 for (i
= 1; i
<= data
->block
[0]; i
++)
2781 msgbuf0
[i
] = data
->block
[i
];
2785 dev_err(&adapter
->dev
, "Unsupported transaction %d\n", size
);
2789 i
= ((flags
& I2C_CLIENT_PEC
) && size
!= I2C_SMBUS_QUICK
2790 && size
!= I2C_SMBUS_I2C_BLOCK_DATA
);
2792 /* Compute PEC if first message is a write */
2793 if (!(msg
[0].flags
& I2C_M_RD
)) {
2794 if (num
== 1) /* Write only */
2795 i2c_smbus_add_pec(&msg
[0]);
2796 else /* Write followed by read */
2797 partial_pec
= i2c_smbus_msg_pec(0, &msg
[0]);
2799 /* Ask for PEC if last message is a read */
2800 if (msg
[num
-1].flags
& I2C_M_RD
)
2804 status
= i2c_transfer(adapter
, msg
, num
);
2808 /* Check PEC if last message is a read */
2809 if (i
&& (msg
[num
-1].flags
& I2C_M_RD
)) {
2810 status
= i2c_smbus_check_pec(partial_pec
, &msg
[num
-1]);
2815 if (read_write
== I2C_SMBUS_READ
)
2817 case I2C_SMBUS_BYTE
:
2818 data
->byte
= msgbuf0
[0];
2820 case I2C_SMBUS_BYTE_DATA
:
2821 data
->byte
= msgbuf1
[0];
2823 case I2C_SMBUS_WORD_DATA
:
2824 case I2C_SMBUS_PROC_CALL
:
2825 data
->word
= msgbuf1
[0] | (msgbuf1
[1] << 8);
2827 case I2C_SMBUS_I2C_BLOCK_DATA
:
2828 for (i
= 0; i
< data
->block
[0]; i
++)
2829 data
->block
[i
+1] = msgbuf1
[i
];
2831 case I2C_SMBUS_BLOCK_DATA
:
2832 case I2C_SMBUS_BLOCK_PROC_CALL
:
2833 for (i
= 0; i
< msgbuf1
[0] + 1; i
++)
2834 data
->block
[i
] = msgbuf1
[i
];
2841 * i2c_smbus_xfer - execute SMBus protocol operations
2842 * @adapter: Handle to I2C bus
2843 * @addr: Address of SMBus slave on that bus
2844 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2845 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2846 * @command: Byte interpreted by slave, for protocols which use such bytes
2847 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2848 * @data: Data to be read or written
2850 * This executes an SMBus protocol operation, and returns a negative
2851 * errno code else zero on success.
2853 s32
i2c_smbus_xfer(struct i2c_adapter
*adapter
, u16 addr
, unsigned short flags
,
2854 char read_write
, u8 command
, int protocol
,
2855 union i2c_smbus_data
*data
)
2857 unsigned long orig_jiffies
;
2861 /* If enabled, the following two tracepoints are conditional on
2862 * read_write and protocol.
2864 trace_smbus_write(adapter
, addr
, flags
, read_write
,
2865 command
, protocol
, data
);
2866 trace_smbus_read(adapter
, addr
, flags
, read_write
,
2869 flags
&= I2C_M_TEN
| I2C_CLIENT_PEC
| I2C_CLIENT_SCCB
;
2871 if (adapter
->algo
->smbus_xfer
) {
2872 i2c_lock_adapter(adapter
);
2874 /* Retry automatically on arbitration loss */
2875 orig_jiffies
= jiffies
;
2876 for (res
= 0, try = 0; try <= adapter
->retries
; try++) {
2877 res
= adapter
->algo
->smbus_xfer(adapter
, addr
, flags
,
2878 read_write
, command
,
2882 if (time_after(jiffies
,
2883 orig_jiffies
+ adapter
->timeout
))
2886 i2c_unlock_adapter(adapter
);
2888 if (res
!= -EOPNOTSUPP
|| !adapter
->algo
->master_xfer
)
2891 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2892 * implement native support for the SMBus operation.
2896 res
= i2c_smbus_xfer_emulated(adapter
, addr
, flags
, read_write
,
2897 command
, protocol
, data
);
2900 /* If enabled, the reply tracepoint is conditional on read_write. */
2901 trace_smbus_reply(adapter
, addr
, flags
, read_write
,
2902 command
, protocol
, data
);
2903 trace_smbus_result(adapter
, addr
, flags
, read_write
,
2904 command
, protocol
, res
);
2908 EXPORT_SYMBOL(i2c_smbus_xfer
);
2910 #if IS_ENABLED(CONFIG_I2C_SLAVE)
2911 int i2c_slave_register(struct i2c_client
*client
, i2c_slave_cb_t slave_cb
)
2915 if (!client
|| !slave_cb
)
2918 if (!(client
->flags
& I2C_CLIENT_TEN
)) {
2919 /* Enforce stricter address checking */
2920 ret
= i2c_check_addr_validity(client
->addr
);
2925 if (!client
->adapter
->algo
->reg_slave
)
2928 client
->slave_cb
= slave_cb
;
2930 i2c_lock_adapter(client
->adapter
);
2931 ret
= client
->adapter
->algo
->reg_slave(client
);
2932 i2c_unlock_adapter(client
->adapter
);
2935 client
->slave_cb
= NULL
;
2939 EXPORT_SYMBOL_GPL(i2c_slave_register
);
2941 int i2c_slave_unregister(struct i2c_client
*client
)
2945 if (!client
->adapter
->algo
->unreg_slave
)
2948 i2c_lock_adapter(client
->adapter
);
2949 ret
= client
->adapter
->algo
->unreg_slave(client
);
2950 i2c_unlock_adapter(client
->adapter
);
2953 client
->slave_cb
= NULL
;
2957 EXPORT_SYMBOL_GPL(i2c_slave_unregister
);
2960 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2961 MODULE_DESCRIPTION("I2C-Bus main module");
2962 MODULE_LICENSE("GPL");