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 #define I2C_ADDR_OFFSET_TEN_BIT 0xa000
61 #define I2C_ADDR_OFFSET_SLAVE 0x1000
63 /* core_lock protects i2c_adapter_idr, and guarantees
64 that device detection, deletion of detected devices, and attach_adapter
65 calls are serialized */
66 static DEFINE_MUTEX(core_lock
);
67 static DEFINE_IDR(i2c_adapter_idr
);
69 static struct device_type i2c_client_type
;
70 static int i2c_detect(struct i2c_adapter
*adapter
, struct i2c_driver
*driver
);
72 static struct static_key i2c_trace_msg
= STATIC_KEY_INIT_FALSE
;
74 void i2c_transfer_trace_reg(void)
76 static_key_slow_inc(&i2c_trace_msg
);
79 void i2c_transfer_trace_unreg(void)
81 static_key_slow_dec(&i2c_trace_msg
);
84 #if defined(CONFIG_ACPI)
85 struct acpi_i2c_handler_data
{
86 struct acpi_connection_info info
;
87 struct i2c_adapter
*adapter
;
100 static int acpi_i2c_add_resource(struct acpi_resource
*ares
, void *data
)
102 struct i2c_board_info
*info
= data
;
104 if (ares
->type
== ACPI_RESOURCE_TYPE_SERIAL_BUS
) {
105 struct acpi_resource_i2c_serialbus
*sb
;
107 sb
= &ares
->data
.i2c_serial_bus
;
108 if (!info
->addr
&& sb
->type
== ACPI_RESOURCE_SERIAL_TYPE_I2C
) {
109 info
->addr
= sb
->slave_address
;
110 if (sb
->access_mode
== ACPI_I2C_10BIT_MODE
)
111 info
->flags
|= I2C_CLIENT_TEN
;
113 } else if (!info
->irq
) {
116 if (acpi_dev_resource_interrupt(ares
, 0, &r
))
120 /* Tell the ACPI core to skip this resource */
124 static acpi_status
acpi_i2c_add_device(acpi_handle handle
, u32 level
,
125 void *data
, void **return_value
)
127 struct i2c_adapter
*adapter
= data
;
128 struct list_head resource_list
;
129 struct i2c_board_info info
;
130 struct acpi_device
*adev
;
133 if (acpi_bus_get_device(handle
, &adev
))
135 if (acpi_bus_get_status(adev
) || !adev
->status
.present
)
138 memset(&info
, 0, sizeof(info
));
139 info
.fwnode
= acpi_fwnode_handle(adev
);
141 INIT_LIST_HEAD(&resource_list
);
142 ret
= acpi_dev_get_resources(adev
, &resource_list
,
143 acpi_i2c_add_resource
, &info
);
144 acpi_dev_free_resource_list(&resource_list
);
146 if (ret
< 0 || !info
.addr
)
149 adev
->power
.flags
.ignore_parent
= true;
150 strlcpy(info
.type
, dev_name(&adev
->dev
), sizeof(info
.type
));
151 if (!i2c_new_device(adapter
, &info
)) {
152 adev
->power
.flags
.ignore_parent
= false;
153 dev_err(&adapter
->dev
,
154 "failed to add I2C device %s from ACPI\n",
155 dev_name(&adev
->dev
));
162 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
163 * @adap: pointer to adapter
165 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
166 * namespace. When a device is found it will be added to the Linux device
167 * model and bound to the corresponding ACPI handle.
169 static void acpi_i2c_register_devices(struct i2c_adapter
*adap
)
174 if (!adap
->dev
.parent
)
177 handle
= ACPI_HANDLE(adap
->dev
.parent
);
181 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, 1,
182 acpi_i2c_add_device
, NULL
,
184 if (ACPI_FAILURE(status
))
185 dev_warn(&adap
->dev
, "failed to enumerate I2C slaves\n");
188 #else /* CONFIG_ACPI */
189 static inline void acpi_i2c_register_devices(struct i2c_adapter
*adap
) { }
190 #endif /* CONFIG_ACPI */
192 #ifdef CONFIG_ACPI_I2C_OPREGION
193 static int acpi_gsb_i2c_read_bytes(struct i2c_client
*client
,
194 u8 cmd
, u8
*data
, u8 data_len
)
197 struct i2c_msg msgs
[2];
201 buffer
= kzalloc(data_len
, GFP_KERNEL
);
205 msgs
[0].addr
= client
->addr
;
206 msgs
[0].flags
= client
->flags
;
210 msgs
[1].addr
= client
->addr
;
211 msgs
[1].flags
= client
->flags
| I2C_M_RD
;
212 msgs
[1].len
= data_len
;
213 msgs
[1].buf
= buffer
;
215 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
217 dev_err(&client
->adapter
->dev
, "i2c read failed\n");
219 memcpy(data
, buffer
, data_len
);
225 static int acpi_gsb_i2c_write_bytes(struct i2c_client
*client
,
226 u8 cmd
, u8
*data
, u8 data_len
)
229 struct i2c_msg msgs
[1];
233 buffer
= kzalloc(data_len
+ 1, GFP_KERNEL
);
238 memcpy(buffer
+ 1, data
, data_len
);
240 msgs
[0].addr
= client
->addr
;
241 msgs
[0].flags
= client
->flags
;
242 msgs
[0].len
= data_len
+ 1;
243 msgs
[0].buf
= buffer
;
245 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
247 dev_err(&client
->adapter
->dev
, "i2c write failed\n");
254 acpi_i2c_space_handler(u32 function
, acpi_physical_address command
,
255 u32 bits
, u64
*value64
,
256 void *handler_context
, void *region_context
)
258 struct gsb_buffer
*gsb
= (struct gsb_buffer
*)value64
;
259 struct acpi_i2c_handler_data
*data
= handler_context
;
260 struct acpi_connection_info
*info
= &data
->info
;
261 struct acpi_resource_i2c_serialbus
*sb
;
262 struct i2c_adapter
*adapter
= data
->adapter
;
263 struct i2c_client
*client
;
264 struct acpi_resource
*ares
;
265 u32 accessor_type
= function
>> 16;
266 u8 action
= function
& ACPI_IO_MASK
;
270 ret
= acpi_buffer_to_resource(info
->connection
, info
->length
, &ares
);
271 if (ACPI_FAILURE(ret
))
274 client
= kzalloc(sizeof(*client
), GFP_KERNEL
);
280 if (!value64
|| ares
->type
!= ACPI_RESOURCE_TYPE_SERIAL_BUS
) {
281 ret
= AE_BAD_PARAMETER
;
285 sb
= &ares
->data
.i2c_serial_bus
;
286 if (sb
->type
!= ACPI_RESOURCE_SERIAL_TYPE_I2C
) {
287 ret
= AE_BAD_PARAMETER
;
291 client
->adapter
= adapter
;
292 client
->addr
= sb
->slave_address
;
294 if (sb
->access_mode
== ACPI_I2C_10BIT_MODE
)
295 client
->flags
|= I2C_CLIENT_TEN
;
297 switch (accessor_type
) {
298 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV
:
299 if (action
== ACPI_READ
) {
300 status
= i2c_smbus_read_byte(client
);
306 status
= i2c_smbus_write_byte(client
, gsb
->bdata
);
310 case ACPI_GSB_ACCESS_ATTRIB_BYTE
:
311 if (action
== ACPI_READ
) {
312 status
= i2c_smbus_read_byte_data(client
, command
);
318 status
= i2c_smbus_write_byte_data(client
, command
,
323 case ACPI_GSB_ACCESS_ATTRIB_WORD
:
324 if (action
== ACPI_READ
) {
325 status
= i2c_smbus_read_word_data(client
, command
);
331 status
= i2c_smbus_write_word_data(client
, command
,
336 case ACPI_GSB_ACCESS_ATTRIB_BLOCK
:
337 if (action
== ACPI_READ
) {
338 status
= i2c_smbus_read_block_data(client
, command
,
345 status
= i2c_smbus_write_block_data(client
, command
,
346 gsb
->len
, gsb
->data
);
350 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE
:
351 if (action
== ACPI_READ
) {
352 status
= acpi_gsb_i2c_read_bytes(client
, command
,
353 gsb
->data
, info
->access_length
);
357 status
= acpi_gsb_i2c_write_bytes(client
, command
,
358 gsb
->data
, info
->access_length
);
363 pr_info("protocol(0x%02x) is not supported.\n", accessor_type
);
364 ret
= AE_BAD_PARAMETER
;
368 gsb
->status
= status
;
377 static int acpi_i2c_install_space_handler(struct i2c_adapter
*adapter
)
380 struct acpi_i2c_handler_data
*data
;
383 if (!adapter
->dev
.parent
)
386 handle
= ACPI_HANDLE(adapter
->dev
.parent
);
391 data
= kzalloc(sizeof(struct acpi_i2c_handler_data
),
396 data
->adapter
= adapter
;
397 status
= acpi_bus_attach_private_data(handle
, (void *)data
);
398 if (ACPI_FAILURE(status
)) {
403 status
= acpi_install_address_space_handler(handle
,
404 ACPI_ADR_SPACE_GSBUS
,
405 &acpi_i2c_space_handler
,
408 if (ACPI_FAILURE(status
)) {
409 dev_err(&adapter
->dev
, "Error installing i2c space handler\n");
410 acpi_bus_detach_private_data(handle
);
415 acpi_walk_dep_device_list(handle
);
419 static void acpi_i2c_remove_space_handler(struct i2c_adapter
*adapter
)
422 struct acpi_i2c_handler_data
*data
;
425 if (!adapter
->dev
.parent
)
428 handle
= ACPI_HANDLE(adapter
->dev
.parent
);
433 acpi_remove_address_space_handler(handle
,
434 ACPI_ADR_SPACE_GSBUS
,
435 &acpi_i2c_space_handler
);
437 status
= acpi_bus_get_private_data(handle
, (void **)&data
);
438 if (ACPI_SUCCESS(status
))
441 acpi_bus_detach_private_data(handle
);
443 #else /* CONFIG_ACPI_I2C_OPREGION */
444 static inline void acpi_i2c_remove_space_handler(struct i2c_adapter
*adapter
)
447 static inline int acpi_i2c_install_space_handler(struct i2c_adapter
*adapter
)
449 #endif /* CONFIG_ACPI_I2C_OPREGION */
451 /* ------------------------------------------------------------------------- */
453 static const struct i2c_device_id
*i2c_match_id(const struct i2c_device_id
*id
,
454 const struct i2c_client
*client
)
456 while (id
->name
[0]) {
457 if (strcmp(client
->name
, id
->name
) == 0)
464 static int i2c_device_match(struct device
*dev
, struct device_driver
*drv
)
466 struct i2c_client
*client
= i2c_verify_client(dev
);
467 struct i2c_driver
*driver
;
472 /* Attempt an OF style match */
473 if (of_driver_match_device(dev
, drv
))
476 /* Then ACPI style match */
477 if (acpi_driver_match_device(dev
, drv
))
480 driver
= to_i2c_driver(drv
);
481 /* match on an id table if there is one */
482 if (driver
->id_table
)
483 return i2c_match_id(driver
->id_table
, client
) != NULL
;
489 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
490 static int i2c_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
492 struct i2c_client
*client
= to_i2c_client(dev
);
495 rc
= acpi_device_uevent_modalias(dev
, env
);
499 if (add_uevent_var(env
, "MODALIAS=%s%s",
500 I2C_MODULE_PREFIX
, client
->name
))
502 dev_dbg(dev
, "uevent\n");
506 /* i2c bus recovery routines */
507 static int get_scl_gpio_value(struct i2c_adapter
*adap
)
509 return gpio_get_value(adap
->bus_recovery_info
->scl_gpio
);
512 static void set_scl_gpio_value(struct i2c_adapter
*adap
, int val
)
514 gpio_set_value(adap
->bus_recovery_info
->scl_gpio
, val
);
517 static int get_sda_gpio_value(struct i2c_adapter
*adap
)
519 return gpio_get_value(adap
->bus_recovery_info
->sda_gpio
);
522 static int i2c_get_gpios_for_recovery(struct i2c_adapter
*adap
)
524 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
525 struct device
*dev
= &adap
->dev
;
528 ret
= gpio_request_one(bri
->scl_gpio
, GPIOF_OPEN_DRAIN
|
529 GPIOF_OUT_INIT_HIGH
, "i2c-scl");
531 dev_warn(dev
, "Can't get SCL gpio: %d\n", bri
->scl_gpio
);
536 if (gpio_request_one(bri
->sda_gpio
, GPIOF_IN
, "i2c-sda")) {
537 /* work without SDA polling */
538 dev_warn(dev
, "Can't get SDA gpio: %d. Not using SDA polling\n",
547 static void i2c_put_gpios_for_recovery(struct i2c_adapter
*adap
)
549 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
552 gpio_free(bri
->sda_gpio
);
554 gpio_free(bri
->scl_gpio
);
558 * We are generating clock pulses. ndelay() determines durating of clk pulses.
559 * We will generate clock with rate 100 KHz and so duration of both clock levels
560 * is: delay in ns = (10^6 / 100) / 2
562 #define RECOVERY_NDELAY 5000
563 #define RECOVERY_CLK_CNT 9
565 static int i2c_generic_recovery(struct i2c_adapter
*adap
)
567 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
568 int i
= 0, val
= 1, ret
= 0;
570 if (bri
->prepare_recovery
)
571 bri
->prepare_recovery(adap
);
573 bri
->set_scl(adap
, val
);
574 ndelay(RECOVERY_NDELAY
);
577 * By this time SCL is high, as we need to give 9 falling-rising edges
579 while (i
++ < RECOVERY_CLK_CNT
* 2) {
581 /* Break if SDA is high */
582 if (bri
->get_sda
&& bri
->get_sda(adap
))
584 /* SCL shouldn't be low here */
585 if (!bri
->get_scl(adap
)) {
587 "SCL is stuck low, exit recovery\n");
594 bri
->set_scl(adap
, val
);
595 ndelay(RECOVERY_NDELAY
);
598 if (bri
->unprepare_recovery
)
599 bri
->unprepare_recovery(adap
);
604 int i2c_generic_scl_recovery(struct i2c_adapter
*adap
)
606 return i2c_generic_recovery(adap
);
608 EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery
);
610 int i2c_generic_gpio_recovery(struct i2c_adapter
*adap
)
614 ret
= i2c_get_gpios_for_recovery(adap
);
618 ret
= i2c_generic_recovery(adap
);
619 i2c_put_gpios_for_recovery(adap
);
623 EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery
);
625 int i2c_recover_bus(struct i2c_adapter
*adap
)
627 if (!adap
->bus_recovery_info
)
630 dev_dbg(&adap
->dev
, "Trying i2c bus recovery\n");
631 return adap
->bus_recovery_info
->recover_bus(adap
);
633 EXPORT_SYMBOL_GPL(i2c_recover_bus
);
635 static int i2c_device_probe(struct device
*dev
)
637 struct i2c_client
*client
= i2c_verify_client(dev
);
638 struct i2c_driver
*driver
;
648 irq
= of_irq_get(dev
->of_node
, 0);
649 else if (ACPI_COMPANION(dev
))
650 irq
= acpi_dev_gpio_irq_get(ACPI_COMPANION(dev
), 0);
652 if (irq
== -EPROBE_DEFER
)
660 driver
= to_i2c_driver(dev
->driver
);
661 if (!driver
->probe
|| !driver
->id_table
)
664 if (!device_can_wakeup(&client
->dev
))
665 device_init_wakeup(&client
->dev
,
666 client
->flags
& I2C_CLIENT_WAKE
);
667 dev_dbg(dev
, "probe\n");
669 status
= of_clk_set_defaults(dev
->of_node
, false);
673 status
= dev_pm_domain_attach(&client
->dev
, true);
674 if (status
!= -EPROBE_DEFER
) {
675 status
= driver
->probe(client
, i2c_match_id(driver
->id_table
,
678 dev_pm_domain_detach(&client
->dev
, true);
684 static int i2c_device_remove(struct device
*dev
)
686 struct i2c_client
*client
= i2c_verify_client(dev
);
687 struct i2c_driver
*driver
;
690 if (!client
|| !dev
->driver
)
693 driver
= to_i2c_driver(dev
->driver
);
694 if (driver
->remove
) {
695 dev_dbg(dev
, "remove\n");
696 status
= driver
->remove(client
);
699 dev_pm_domain_detach(&client
->dev
, true);
703 static void i2c_device_shutdown(struct device
*dev
)
705 struct i2c_client
*client
= i2c_verify_client(dev
);
706 struct i2c_driver
*driver
;
708 if (!client
|| !dev
->driver
)
710 driver
= to_i2c_driver(dev
->driver
);
711 if (driver
->shutdown
)
712 driver
->shutdown(client
);
715 static void i2c_client_dev_release(struct device
*dev
)
717 kfree(to_i2c_client(dev
));
721 show_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
723 return sprintf(buf
, "%s\n", dev
->type
== &i2c_client_type
?
724 to_i2c_client(dev
)->name
: to_i2c_adapter(dev
)->name
);
726 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
729 show_modalias(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
731 struct i2c_client
*client
= to_i2c_client(dev
);
734 len
= acpi_device_modalias(dev
, buf
, PAGE_SIZE
-1);
738 return sprintf(buf
, "%s%s\n", I2C_MODULE_PREFIX
, client
->name
);
740 static DEVICE_ATTR(modalias
, S_IRUGO
, show_modalias
, NULL
);
742 static struct attribute
*i2c_dev_attrs
[] = {
744 /* modalias helps coldplug: modprobe $(cat .../modalias) */
745 &dev_attr_modalias
.attr
,
748 ATTRIBUTE_GROUPS(i2c_dev
);
750 struct bus_type i2c_bus_type
= {
752 .match
= i2c_device_match
,
753 .probe
= i2c_device_probe
,
754 .remove
= i2c_device_remove
,
755 .shutdown
= i2c_device_shutdown
,
757 EXPORT_SYMBOL_GPL(i2c_bus_type
);
759 static struct device_type i2c_client_type
= {
760 .groups
= i2c_dev_groups
,
761 .uevent
= i2c_device_uevent
,
762 .release
= i2c_client_dev_release
,
767 * i2c_verify_client - return parameter as i2c_client, or NULL
768 * @dev: device, probably from some driver model iterator
770 * When traversing the driver model tree, perhaps using driver model
771 * iterators like @device_for_each_child(), you can't assume very much
772 * about the nodes you find. Use this function to avoid oopses caused
773 * by wrongly treating some non-I2C device as an i2c_client.
775 struct i2c_client
*i2c_verify_client(struct device
*dev
)
777 return (dev
->type
== &i2c_client_type
)
781 EXPORT_SYMBOL(i2c_verify_client
);
784 /* Return a unique address which takes the flags of the client into account */
785 static unsigned short i2c_encode_flags_to_addr(struct i2c_client
*client
)
787 unsigned short addr
= client
->addr
;
789 /* For some client flags, add an arbitrary offset to avoid collisions */
790 if (client
->flags
& I2C_CLIENT_TEN
)
791 addr
|= I2C_ADDR_OFFSET_TEN_BIT
;
793 if (client
->flags
& I2C_CLIENT_SLAVE
)
794 addr
|= I2C_ADDR_OFFSET_SLAVE
;
799 /* This is a permissive address validity check, I2C address map constraints
800 * are purposely not enforced, except for the general call address. */
801 static int i2c_check_addr_validity(unsigned addr
, unsigned short flags
)
803 if (flags
& I2C_CLIENT_TEN
) {
804 /* 10-bit address, all values are valid */
808 /* 7-bit address, reject the general call address */
809 if (addr
== 0x00 || addr
> 0x7f)
815 /* And this is a strict address validity check, used when probing. If a
816 * device uses a reserved address, then it shouldn't be probed. 7-bit
817 * addressing is assumed, 10-bit address devices are rare and should be
818 * explicitly enumerated. */
819 static int i2c_check_7bit_addr_validity_strict(unsigned short addr
)
822 * Reserved addresses per I2C specification:
823 * 0x00 General call address / START byte
825 * 0x02 Reserved for different bus format
826 * 0x03 Reserved for future purposes
827 * 0x04-0x07 Hs-mode master code
828 * 0x78-0x7b 10-bit slave addressing
829 * 0x7c-0x7f Reserved for future purposes
831 if (addr
< 0x08 || addr
> 0x77)
836 static int __i2c_check_addr_busy(struct device
*dev
, void *addrp
)
838 struct i2c_client
*client
= i2c_verify_client(dev
);
839 int addr
= *(int *)addrp
;
841 if (client
&& client
->addr
== addr
)
846 /* walk up mux tree */
847 static int i2c_check_mux_parents(struct i2c_adapter
*adapter
, int addr
)
849 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
852 result
= device_for_each_child(&adapter
->dev
, &addr
,
853 __i2c_check_addr_busy
);
855 if (!result
&& parent
)
856 result
= i2c_check_mux_parents(parent
, addr
);
861 /* recurse down mux tree */
862 static int i2c_check_mux_children(struct device
*dev
, void *addrp
)
866 if (dev
->type
== &i2c_adapter_type
)
867 result
= device_for_each_child(dev
, addrp
,
868 i2c_check_mux_children
);
870 result
= __i2c_check_addr_busy(dev
, addrp
);
875 static int i2c_check_addr_busy(struct i2c_adapter
*adapter
, int addr
)
877 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
881 result
= i2c_check_mux_parents(parent
, addr
);
884 result
= device_for_each_child(&adapter
->dev
, &addr
,
885 i2c_check_mux_children
);
891 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
892 * @adapter: Target I2C bus segment
894 void i2c_lock_adapter(struct i2c_adapter
*adapter
)
896 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
899 i2c_lock_adapter(parent
);
901 rt_mutex_lock(&adapter
->bus_lock
);
903 EXPORT_SYMBOL_GPL(i2c_lock_adapter
);
906 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
907 * @adapter: Target I2C bus segment
909 static int i2c_trylock_adapter(struct i2c_adapter
*adapter
)
911 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
914 return i2c_trylock_adapter(parent
);
916 return rt_mutex_trylock(&adapter
->bus_lock
);
920 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
921 * @adapter: Target I2C bus segment
923 void i2c_unlock_adapter(struct i2c_adapter
*adapter
)
925 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
928 i2c_unlock_adapter(parent
);
930 rt_mutex_unlock(&adapter
->bus_lock
);
932 EXPORT_SYMBOL_GPL(i2c_unlock_adapter
);
934 static void i2c_dev_set_name(struct i2c_adapter
*adap
,
935 struct i2c_client
*client
)
937 struct acpi_device
*adev
= ACPI_COMPANION(&client
->dev
);
940 dev_set_name(&client
->dev
, "i2c-%s", acpi_dev_name(adev
));
944 dev_set_name(&client
->dev
, "%d-%04x", i2c_adapter_id(adap
),
945 i2c_encode_flags_to_addr(client
));
949 * i2c_new_device - instantiate an i2c device
950 * @adap: the adapter managing the device
951 * @info: describes one I2C device; bus_num is ignored
954 * Create an i2c device. Binding is handled through driver model
955 * probe()/remove() methods. A driver may be bound to this device when we
956 * return from this function, or any later moment (e.g. maybe hotplugging will
957 * load the driver module). This call is not appropriate for use by mainboard
958 * initialization logic, which usually runs during an arch_initcall() long
959 * before any i2c_adapter could exist.
961 * This returns the new i2c client, which may be saved for later use with
962 * i2c_unregister_device(); or NULL to indicate an error.
965 i2c_new_device(struct i2c_adapter
*adap
, struct i2c_board_info
const *info
)
967 struct i2c_client
*client
;
970 client
= kzalloc(sizeof *client
, GFP_KERNEL
);
974 client
->adapter
= adap
;
976 client
->dev
.platform_data
= info
->platform_data
;
979 client
->dev
.archdata
= *info
->archdata
;
981 client
->flags
= info
->flags
;
982 client
->addr
= info
->addr
;
983 client
->irq
= info
->irq
;
985 strlcpy(client
->name
, info
->type
, sizeof(client
->name
));
987 status
= i2c_check_addr_validity(client
->addr
, client
->flags
);
989 dev_err(&adap
->dev
, "Invalid %d-bit I2C address 0x%02hx\n",
990 client
->flags
& I2C_CLIENT_TEN
? 10 : 7, client
->addr
);
994 /* Check for address business */
995 status
= i2c_check_addr_busy(adap
, client
->addr
);
999 client
->dev
.parent
= &client
->adapter
->dev
;
1000 client
->dev
.bus
= &i2c_bus_type
;
1001 client
->dev
.type
= &i2c_client_type
;
1002 client
->dev
.of_node
= info
->of_node
;
1003 client
->dev
.fwnode
= info
->fwnode
;
1005 i2c_dev_set_name(adap
, client
);
1006 status
= device_register(&client
->dev
);
1010 dev_dbg(&adap
->dev
, "client [%s] registered with bus id %s\n",
1011 client
->name
, dev_name(&client
->dev
));
1016 dev_err(&adap
->dev
, "Failed to register i2c client %s at 0x%02x "
1017 "(%d)\n", client
->name
, client
->addr
, status
);
1022 EXPORT_SYMBOL_GPL(i2c_new_device
);
1026 * i2c_unregister_device - reverse effect of i2c_new_device()
1027 * @client: value returned from i2c_new_device()
1028 * Context: can sleep
1030 void i2c_unregister_device(struct i2c_client
*client
)
1032 if (client
->dev
.of_node
)
1033 of_node_clear_flag(client
->dev
.of_node
, OF_POPULATED
);
1034 device_unregister(&client
->dev
);
1036 EXPORT_SYMBOL_GPL(i2c_unregister_device
);
1039 static const struct i2c_device_id dummy_id
[] = {
1044 static int dummy_probe(struct i2c_client
*client
,
1045 const struct i2c_device_id
*id
)
1050 static int dummy_remove(struct i2c_client
*client
)
1055 static struct i2c_driver dummy_driver
= {
1056 .driver
.name
= "dummy",
1057 .probe
= dummy_probe
,
1058 .remove
= dummy_remove
,
1059 .id_table
= dummy_id
,
1063 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1064 * @adapter: the adapter managing the device
1065 * @address: seven bit address to be used
1066 * Context: can sleep
1068 * This returns an I2C client bound to the "dummy" driver, intended for use
1069 * with devices that consume multiple addresses. Examples of such chips
1070 * include various EEPROMS (like 24c04 and 24c08 models).
1072 * These dummy devices have two main uses. First, most I2C and SMBus calls
1073 * except i2c_transfer() need a client handle; the dummy will be that handle.
1074 * And second, this prevents the specified address from being bound to a
1077 * This returns the new i2c client, which should be saved for later use with
1078 * i2c_unregister_device(); or NULL to indicate an error.
1080 struct i2c_client
*i2c_new_dummy(struct i2c_adapter
*adapter
, u16 address
)
1082 struct i2c_board_info info
= {
1083 I2C_BOARD_INFO("dummy", address
),
1086 return i2c_new_device(adapter
, &info
);
1088 EXPORT_SYMBOL_GPL(i2c_new_dummy
);
1090 /* ------------------------------------------------------------------------- */
1092 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
1094 static void i2c_adapter_dev_release(struct device
*dev
)
1096 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
1097 complete(&adap
->dev_released
);
1101 * This function is only needed for mutex_lock_nested, so it is never
1102 * called unless locking correctness checking is enabled. Thus we
1103 * make it inline to avoid a compiler warning. That's what gcc ends up
1106 static inline unsigned int i2c_adapter_depth(struct i2c_adapter
*adapter
)
1108 unsigned int depth
= 0;
1110 while ((adapter
= i2c_parent_is_i2c_adapter(adapter
)))
1117 * Let users instantiate I2C devices through sysfs. This can be used when
1118 * platform initialization code doesn't contain the proper data for
1119 * whatever reason. Also useful for drivers that do device detection and
1120 * detection fails, either because the device uses an unexpected address,
1121 * or this is a compatible device with different ID register values.
1123 * Parameter checking may look overzealous, but we really don't want
1124 * the user to provide incorrect parameters.
1127 i2c_sysfs_new_device(struct device
*dev
, struct device_attribute
*attr
,
1128 const char *buf
, size_t count
)
1130 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
1131 struct i2c_board_info info
;
1132 struct i2c_client
*client
;
1136 memset(&info
, 0, sizeof(struct i2c_board_info
));
1138 blank
= strchr(buf
, ' ');
1140 dev_err(dev
, "%s: Missing parameters\n", "new_device");
1143 if (blank
- buf
> I2C_NAME_SIZE
- 1) {
1144 dev_err(dev
, "%s: Invalid device name\n", "new_device");
1147 memcpy(info
.type
, buf
, blank
- buf
);
1149 /* Parse remaining parameters, reject extra parameters */
1150 res
= sscanf(++blank
, "%hi%c", &info
.addr
, &end
);
1152 dev_err(dev
, "%s: Can't parse I2C address\n", "new_device");
1155 if (res
> 1 && end
!= '\n') {
1156 dev_err(dev
, "%s: Extra parameters\n", "new_device");
1160 client
= i2c_new_device(adap
, &info
);
1164 /* Keep track of the added device */
1165 mutex_lock(&adap
->userspace_clients_lock
);
1166 list_add_tail(&client
->detected
, &adap
->userspace_clients
);
1167 mutex_unlock(&adap
->userspace_clients_lock
);
1168 dev_info(dev
, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1169 info
.type
, info
.addr
);
1173 static DEVICE_ATTR(new_device
, S_IWUSR
, NULL
, i2c_sysfs_new_device
);
1176 * And of course let the users delete the devices they instantiated, if
1177 * they got it wrong. This interface can only be used to delete devices
1178 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1179 * don't delete devices to which some kernel code still has references.
1181 * Parameter checking may look overzealous, but we really don't want
1182 * the user to delete the wrong device.
1185 i2c_sysfs_delete_device(struct device
*dev
, struct device_attribute
*attr
,
1186 const char *buf
, size_t count
)
1188 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
1189 struct i2c_client
*client
, *next
;
1190 unsigned short addr
;
1194 /* Parse parameters, reject extra parameters */
1195 res
= sscanf(buf
, "%hi%c", &addr
, &end
);
1197 dev_err(dev
, "%s: Can't parse I2C address\n", "delete_device");
1200 if (res
> 1 && end
!= '\n') {
1201 dev_err(dev
, "%s: Extra parameters\n", "delete_device");
1205 /* Make sure the device was added through sysfs */
1207 mutex_lock_nested(&adap
->userspace_clients_lock
,
1208 i2c_adapter_depth(adap
));
1209 list_for_each_entry_safe(client
, next
, &adap
->userspace_clients
,
1211 if (client
->addr
== addr
) {
1212 dev_info(dev
, "%s: Deleting device %s at 0x%02hx\n",
1213 "delete_device", client
->name
, client
->addr
);
1215 list_del(&client
->detected
);
1216 i2c_unregister_device(client
);
1221 mutex_unlock(&adap
->userspace_clients_lock
);
1224 dev_err(dev
, "%s: Can't find device in list\n",
1228 static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device
, S_IWUSR
, NULL
,
1229 i2c_sysfs_delete_device
);
1231 static struct attribute
*i2c_adapter_attrs
[] = {
1232 &dev_attr_name
.attr
,
1233 &dev_attr_new_device
.attr
,
1234 &dev_attr_delete_device
.attr
,
1237 ATTRIBUTE_GROUPS(i2c_adapter
);
1239 struct device_type i2c_adapter_type
= {
1240 .groups
= i2c_adapter_groups
,
1241 .release
= i2c_adapter_dev_release
,
1243 EXPORT_SYMBOL_GPL(i2c_adapter_type
);
1246 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1247 * @dev: device, probably from some driver model iterator
1249 * When traversing the driver model tree, perhaps using driver model
1250 * iterators like @device_for_each_child(), you can't assume very much
1251 * about the nodes you find. Use this function to avoid oopses caused
1252 * by wrongly treating some non-I2C device as an i2c_adapter.
1254 struct i2c_adapter
*i2c_verify_adapter(struct device
*dev
)
1256 return (dev
->type
== &i2c_adapter_type
)
1257 ? to_i2c_adapter(dev
)
1260 EXPORT_SYMBOL(i2c_verify_adapter
);
1262 #ifdef CONFIG_I2C_COMPAT
1263 static struct class_compat
*i2c_adapter_compat_class
;
1266 static void i2c_scan_static_board_info(struct i2c_adapter
*adapter
)
1268 struct i2c_devinfo
*devinfo
;
1270 down_read(&__i2c_board_lock
);
1271 list_for_each_entry(devinfo
, &__i2c_board_list
, list
) {
1272 if (devinfo
->busnum
== adapter
->nr
1273 && !i2c_new_device(adapter
,
1274 &devinfo
->board_info
))
1275 dev_err(&adapter
->dev
,
1276 "Can't create device at 0x%02x\n",
1277 devinfo
->board_info
.addr
);
1279 up_read(&__i2c_board_lock
);
1282 /* OF support code */
1284 #if IS_ENABLED(CONFIG_OF)
1285 static struct i2c_client
*of_i2c_register_device(struct i2c_adapter
*adap
,
1286 struct device_node
*node
)
1288 struct i2c_client
*result
;
1289 struct i2c_board_info info
= {};
1290 struct dev_archdata dev_ad
= {};
1294 dev_dbg(&adap
->dev
, "of_i2c: register %s\n", node
->full_name
);
1296 if (of_modalias_node(node
, info
.type
, sizeof(info
.type
)) < 0) {
1297 dev_err(&adap
->dev
, "of_i2c: modalias failure on %s\n",
1299 return ERR_PTR(-EINVAL
);
1302 addr
= of_get_property(node
, "reg", &len
);
1303 if (!addr
|| (len
< sizeof(*addr
))) {
1304 dev_err(&adap
->dev
, "of_i2c: invalid reg on %s\n",
1306 return ERR_PTR(-EINVAL
);
1309 info
.addr
= be32_to_cpup(addr
);
1310 if (info
.addr
> (1 << 10) - 1) {
1311 dev_err(&adap
->dev
, "of_i2c: invalid addr=%x on %s\n",
1312 info
.addr
, node
->full_name
);
1313 return ERR_PTR(-EINVAL
);
1316 info
.of_node
= of_node_get(node
);
1317 info
.archdata
= &dev_ad
;
1319 if (of_get_property(node
, "wakeup-source", NULL
))
1320 info
.flags
|= I2C_CLIENT_WAKE
;
1322 result
= i2c_new_device(adap
, &info
);
1323 if (result
== NULL
) {
1324 dev_err(&adap
->dev
, "of_i2c: Failure registering %s\n",
1327 return ERR_PTR(-EINVAL
);
1332 static void of_i2c_register_devices(struct i2c_adapter
*adap
)
1334 struct device_node
*node
;
1336 /* Only register child devices if the adapter has a node pointer set */
1337 if (!adap
->dev
.of_node
)
1340 dev_dbg(&adap
->dev
, "of_i2c: walking child nodes\n");
1342 for_each_available_child_of_node(adap
->dev
.of_node
, node
) {
1343 if (of_node_test_and_set_flag(node
, OF_POPULATED
))
1345 of_i2c_register_device(adap
, node
);
1349 static int of_dev_node_match(struct device
*dev
, void *data
)
1351 return dev
->of_node
== data
;
1354 /* must call put_device() when done with returned i2c_client device */
1355 struct i2c_client
*of_find_i2c_device_by_node(struct device_node
*node
)
1358 struct i2c_client
*client
;
1360 dev
= bus_find_device(&i2c_bus_type
, NULL
, node
, of_dev_node_match
);
1364 client
= i2c_verify_client(dev
);
1370 EXPORT_SYMBOL(of_find_i2c_device_by_node
);
1372 /* must call put_device() when done with returned i2c_adapter device */
1373 struct i2c_adapter
*of_find_i2c_adapter_by_node(struct device_node
*node
)
1376 struct i2c_adapter
*adapter
;
1378 dev
= bus_find_device(&i2c_bus_type
, NULL
, node
, of_dev_node_match
);
1382 adapter
= i2c_verify_adapter(dev
);
1388 EXPORT_SYMBOL(of_find_i2c_adapter_by_node
);
1390 /* must call i2c_put_adapter() when done with returned i2c_adapter device */
1391 struct i2c_adapter
*of_get_i2c_adapter_by_node(struct device_node
*node
)
1393 struct i2c_adapter
*adapter
;
1395 adapter
= of_find_i2c_adapter_by_node(node
);
1399 if (!try_module_get(adapter
->owner
)) {
1400 put_device(&adapter
->dev
);
1406 EXPORT_SYMBOL(of_get_i2c_adapter_by_node
);
1408 static void of_i2c_register_devices(struct i2c_adapter
*adap
) { }
1409 #endif /* CONFIG_OF */
1411 static int i2c_do_add_adapter(struct i2c_driver
*driver
,
1412 struct i2c_adapter
*adap
)
1414 /* Detect supported devices on that bus, and instantiate them */
1415 i2c_detect(adap
, driver
);
1417 /* Let legacy drivers scan this bus for matching devices */
1418 if (driver
->attach_adapter
) {
1419 dev_warn(&adap
->dev
, "%s: attach_adapter method is deprecated\n",
1420 driver
->driver
.name
);
1421 dev_warn(&adap
->dev
, "Please use another way to instantiate "
1422 "your i2c_client\n");
1423 /* We ignore the return code; if it fails, too bad */
1424 driver
->attach_adapter(adap
);
1429 static int __process_new_adapter(struct device_driver
*d
, void *data
)
1431 return i2c_do_add_adapter(to_i2c_driver(d
), data
);
1434 static int i2c_register_adapter(struct i2c_adapter
*adap
)
1438 /* Can't register until after driver model init */
1439 if (unlikely(WARN_ON(!i2c_bus_type
.p
))) {
1445 if (unlikely(adap
->name
[0] == '\0')) {
1446 pr_err("i2c-core: Attempt to register an adapter with "
1450 if (unlikely(!adap
->algo
)) {
1451 pr_err("i2c-core: Attempt to register adapter '%s' with "
1452 "no algo!\n", adap
->name
);
1456 rt_mutex_init(&adap
->bus_lock
);
1457 mutex_init(&adap
->userspace_clients_lock
);
1458 INIT_LIST_HEAD(&adap
->userspace_clients
);
1460 /* Set default timeout to 1 second if not already set */
1461 if (adap
->timeout
== 0)
1464 dev_set_name(&adap
->dev
, "i2c-%d", adap
->nr
);
1465 adap
->dev
.bus
= &i2c_bus_type
;
1466 adap
->dev
.type
= &i2c_adapter_type
;
1467 res
= device_register(&adap
->dev
);
1471 dev_dbg(&adap
->dev
, "adapter [%s] registered\n", adap
->name
);
1473 pm_runtime_no_callbacks(&adap
->dev
);
1475 #ifdef CONFIG_I2C_COMPAT
1476 res
= class_compat_create_link(i2c_adapter_compat_class
, &adap
->dev
,
1479 dev_warn(&adap
->dev
,
1480 "Failed to create compatibility class link\n");
1483 /* bus recovery specific initialization */
1484 if (adap
->bus_recovery_info
) {
1485 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
1487 if (!bri
->recover_bus
) {
1488 dev_err(&adap
->dev
, "No recover_bus() found, not using recovery\n");
1489 adap
->bus_recovery_info
= NULL
;
1493 /* Generic GPIO recovery */
1494 if (bri
->recover_bus
== i2c_generic_gpio_recovery
) {
1495 if (!gpio_is_valid(bri
->scl_gpio
)) {
1496 dev_err(&adap
->dev
, "Invalid SCL gpio, not using recovery\n");
1497 adap
->bus_recovery_info
= NULL
;
1501 if (gpio_is_valid(bri
->sda_gpio
))
1502 bri
->get_sda
= get_sda_gpio_value
;
1504 bri
->get_sda
= NULL
;
1506 bri
->get_scl
= get_scl_gpio_value
;
1507 bri
->set_scl
= set_scl_gpio_value
;
1508 } else if (!bri
->set_scl
|| !bri
->get_scl
) {
1509 /* Generic SCL recovery */
1510 dev_err(&adap
->dev
, "No {get|set}_gpio() found, not using recovery\n");
1511 adap
->bus_recovery_info
= NULL
;
1516 /* create pre-declared device nodes */
1517 of_i2c_register_devices(adap
);
1518 acpi_i2c_register_devices(adap
);
1519 acpi_i2c_install_space_handler(adap
);
1521 if (adap
->nr
< __i2c_first_dynamic_bus_num
)
1522 i2c_scan_static_board_info(adap
);
1524 /* Notify drivers */
1525 mutex_lock(&core_lock
);
1526 bus_for_each_drv(&i2c_bus_type
, NULL
, adap
, __process_new_adapter
);
1527 mutex_unlock(&core_lock
);
1532 mutex_lock(&core_lock
);
1533 idr_remove(&i2c_adapter_idr
, adap
->nr
);
1534 mutex_unlock(&core_lock
);
1539 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1540 * @adap: the adapter to register (with adap->nr initialized)
1541 * Context: can sleep
1543 * See i2c_add_numbered_adapter() for details.
1545 static int __i2c_add_numbered_adapter(struct i2c_adapter
*adap
)
1549 mutex_lock(&core_lock
);
1550 id
= idr_alloc(&i2c_adapter_idr
, adap
, adap
->nr
, adap
->nr
+ 1,
1552 mutex_unlock(&core_lock
);
1554 return id
== -ENOSPC
? -EBUSY
: id
;
1556 return i2c_register_adapter(adap
);
1560 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1561 * @adapter: the adapter to add
1562 * Context: can sleep
1564 * This routine is used to declare an I2C adapter when its bus number
1565 * doesn't matter or when its bus number is specified by an dt alias.
1566 * Examples of bases when the bus number doesn't matter: I2C adapters
1567 * dynamically added by USB links or PCI plugin cards.
1569 * When this returns zero, a new bus number was allocated and stored
1570 * in adap->nr, and the specified adapter became available for clients.
1571 * Otherwise, a negative errno value is returned.
1573 int i2c_add_adapter(struct i2c_adapter
*adapter
)
1575 struct device
*dev
= &adapter
->dev
;
1579 id
= of_alias_get_id(dev
->of_node
, "i2c");
1582 return __i2c_add_numbered_adapter(adapter
);
1586 mutex_lock(&core_lock
);
1587 id
= idr_alloc(&i2c_adapter_idr
, adapter
,
1588 __i2c_first_dynamic_bus_num
, 0, GFP_KERNEL
);
1589 mutex_unlock(&core_lock
);
1595 return i2c_register_adapter(adapter
);
1597 EXPORT_SYMBOL(i2c_add_adapter
);
1600 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1601 * @adap: the adapter to register (with adap->nr initialized)
1602 * Context: can sleep
1604 * This routine is used to declare an I2C adapter when its bus number
1605 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1606 * or otherwise built in to the system's mainboard, and where i2c_board_info
1607 * is used to properly configure I2C devices.
1609 * If the requested bus number is set to -1, then this function will behave
1610 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1612 * If no devices have pre-been declared for this bus, then be sure to
1613 * register the adapter before any dynamically allocated ones. Otherwise
1614 * the required bus ID may not be available.
1616 * When this returns zero, the specified adapter became available for
1617 * clients using the bus number provided in adap->nr. Also, the table
1618 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1619 * and the appropriate driver model device nodes are created. Otherwise, a
1620 * negative errno value is returned.
1622 int i2c_add_numbered_adapter(struct i2c_adapter
*adap
)
1624 if (adap
->nr
== -1) /* -1 means dynamically assign bus id */
1625 return i2c_add_adapter(adap
);
1627 return __i2c_add_numbered_adapter(adap
);
1629 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter
);
1631 static void i2c_do_del_adapter(struct i2c_driver
*driver
,
1632 struct i2c_adapter
*adapter
)
1634 struct i2c_client
*client
, *_n
;
1636 /* Remove the devices we created ourselves as the result of hardware
1637 * probing (using a driver's detect method) */
1638 list_for_each_entry_safe(client
, _n
, &driver
->clients
, detected
) {
1639 if (client
->adapter
== adapter
) {
1640 dev_dbg(&adapter
->dev
, "Removing %s at 0x%x\n",
1641 client
->name
, client
->addr
);
1642 list_del(&client
->detected
);
1643 i2c_unregister_device(client
);
1648 static int __unregister_client(struct device
*dev
, void *dummy
)
1650 struct i2c_client
*client
= i2c_verify_client(dev
);
1651 if (client
&& strcmp(client
->name
, "dummy"))
1652 i2c_unregister_device(client
);
1656 static int __unregister_dummy(struct device
*dev
, void *dummy
)
1658 struct i2c_client
*client
= i2c_verify_client(dev
);
1660 i2c_unregister_device(client
);
1664 static int __process_removed_adapter(struct device_driver
*d
, void *data
)
1666 i2c_do_del_adapter(to_i2c_driver(d
), data
);
1671 * i2c_del_adapter - unregister I2C adapter
1672 * @adap: the adapter being unregistered
1673 * Context: can sleep
1675 * This unregisters an I2C adapter which was previously registered
1676 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1678 void i2c_del_adapter(struct i2c_adapter
*adap
)
1680 struct i2c_adapter
*found
;
1681 struct i2c_client
*client
, *next
;
1683 /* First make sure that this adapter was ever added */
1684 mutex_lock(&core_lock
);
1685 found
= idr_find(&i2c_adapter_idr
, adap
->nr
);
1686 mutex_unlock(&core_lock
);
1687 if (found
!= adap
) {
1688 pr_debug("i2c-core: attempting to delete unregistered "
1689 "adapter [%s]\n", adap
->name
);
1693 acpi_i2c_remove_space_handler(adap
);
1694 /* Tell drivers about this removal */
1695 mutex_lock(&core_lock
);
1696 bus_for_each_drv(&i2c_bus_type
, NULL
, adap
,
1697 __process_removed_adapter
);
1698 mutex_unlock(&core_lock
);
1700 /* Remove devices instantiated from sysfs */
1701 mutex_lock_nested(&adap
->userspace_clients_lock
,
1702 i2c_adapter_depth(adap
));
1703 list_for_each_entry_safe(client
, next
, &adap
->userspace_clients
,
1705 dev_dbg(&adap
->dev
, "Removing %s at 0x%x\n", client
->name
,
1707 list_del(&client
->detected
);
1708 i2c_unregister_device(client
);
1710 mutex_unlock(&adap
->userspace_clients_lock
);
1712 /* Detach any active clients. This can't fail, thus we do not
1713 * check the returned value. This is a two-pass process, because
1714 * we can't remove the dummy devices during the first pass: they
1715 * could have been instantiated by real devices wishing to clean
1716 * them up properly, so we give them a chance to do that first. */
1717 device_for_each_child(&adap
->dev
, NULL
, __unregister_client
);
1718 device_for_each_child(&adap
->dev
, NULL
, __unregister_dummy
);
1720 #ifdef CONFIG_I2C_COMPAT
1721 class_compat_remove_link(i2c_adapter_compat_class
, &adap
->dev
,
1725 /* device name is gone after device_unregister */
1726 dev_dbg(&adap
->dev
, "adapter [%s] unregistered\n", adap
->name
);
1728 /* wait until all references to the device are gone
1730 * FIXME: This is old code and should ideally be replaced by an
1731 * alternative which results in decoupling the lifetime of the struct
1732 * device from the i2c_adapter, like spi or netdev do. Any solution
1733 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
1735 init_completion(&adap
->dev_released
);
1736 device_unregister(&adap
->dev
);
1737 wait_for_completion(&adap
->dev_released
);
1740 mutex_lock(&core_lock
);
1741 idr_remove(&i2c_adapter_idr
, adap
->nr
);
1742 mutex_unlock(&core_lock
);
1744 /* Clear the device structure in case this adapter is ever going to be
1746 memset(&adap
->dev
, 0, sizeof(adap
->dev
));
1748 EXPORT_SYMBOL(i2c_del_adapter
);
1750 /* ------------------------------------------------------------------------- */
1752 int i2c_for_each_dev(void *data
, int (*fn
)(struct device
*, void *))
1756 mutex_lock(&core_lock
);
1757 res
= bus_for_each_dev(&i2c_bus_type
, NULL
, data
, fn
);
1758 mutex_unlock(&core_lock
);
1762 EXPORT_SYMBOL_GPL(i2c_for_each_dev
);
1764 static int __process_new_driver(struct device
*dev
, void *data
)
1766 if (dev
->type
!= &i2c_adapter_type
)
1768 return i2c_do_add_adapter(data
, to_i2c_adapter(dev
));
1772 * An i2c_driver is used with one or more i2c_client (device) nodes to access
1773 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1776 int i2c_register_driver(struct module
*owner
, struct i2c_driver
*driver
)
1780 /* Can't register until after driver model init */
1781 if (unlikely(WARN_ON(!i2c_bus_type
.p
)))
1784 /* add the driver to the list of i2c drivers in the driver core */
1785 driver
->driver
.owner
= owner
;
1786 driver
->driver
.bus
= &i2c_bus_type
;
1788 /* When registration returns, the driver core
1789 * will have called probe() for all matching-but-unbound devices.
1791 res
= driver_register(&driver
->driver
);
1795 pr_debug("i2c-core: driver [%s] registered\n", driver
->driver
.name
);
1797 INIT_LIST_HEAD(&driver
->clients
);
1798 /* Walk the adapters that are already present */
1799 i2c_for_each_dev(driver
, __process_new_driver
);
1803 EXPORT_SYMBOL(i2c_register_driver
);
1805 static int __process_removed_driver(struct device
*dev
, void *data
)
1807 if (dev
->type
== &i2c_adapter_type
)
1808 i2c_do_del_adapter(data
, to_i2c_adapter(dev
));
1813 * i2c_del_driver - unregister I2C driver
1814 * @driver: the driver being unregistered
1815 * Context: can sleep
1817 void i2c_del_driver(struct i2c_driver
*driver
)
1819 i2c_for_each_dev(driver
, __process_removed_driver
);
1821 driver_unregister(&driver
->driver
);
1822 pr_debug("i2c-core: driver [%s] unregistered\n", driver
->driver
.name
);
1824 EXPORT_SYMBOL(i2c_del_driver
);
1826 /* ------------------------------------------------------------------------- */
1829 * i2c_use_client - increments the reference count of the i2c client structure
1830 * @client: the client being referenced
1832 * Each live reference to a client should be refcounted. The driver model does
1833 * that automatically as part of driver binding, so that most drivers don't
1834 * need to do this explicitly: they hold a reference until they're unbound
1837 * A pointer to the client with the incremented reference counter is returned.
1839 struct i2c_client
*i2c_use_client(struct i2c_client
*client
)
1841 if (client
&& get_device(&client
->dev
))
1845 EXPORT_SYMBOL(i2c_use_client
);
1848 * i2c_release_client - release a use of the i2c client structure
1849 * @client: the client being no longer referenced
1851 * Must be called when a user of a client is finished with it.
1853 void i2c_release_client(struct i2c_client
*client
)
1856 put_device(&client
->dev
);
1858 EXPORT_SYMBOL(i2c_release_client
);
1860 struct i2c_cmd_arg
{
1865 static int i2c_cmd(struct device
*dev
, void *_arg
)
1867 struct i2c_client
*client
= i2c_verify_client(dev
);
1868 struct i2c_cmd_arg
*arg
= _arg
;
1869 struct i2c_driver
*driver
;
1871 if (!client
|| !client
->dev
.driver
)
1874 driver
= to_i2c_driver(client
->dev
.driver
);
1875 if (driver
->command
)
1876 driver
->command(client
, arg
->cmd
, arg
->arg
);
1880 void i2c_clients_command(struct i2c_adapter
*adap
, unsigned int cmd
, void *arg
)
1882 struct i2c_cmd_arg cmd_arg
;
1886 device_for_each_child(&adap
->dev
, &cmd_arg
, i2c_cmd
);
1888 EXPORT_SYMBOL(i2c_clients_command
);
1890 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
1891 static int of_i2c_notify(struct notifier_block
*nb
, unsigned long action
,
1894 struct of_reconfig_data
*rd
= arg
;
1895 struct i2c_adapter
*adap
;
1896 struct i2c_client
*client
;
1898 switch (of_reconfig_get_state_change(action
, rd
)) {
1899 case OF_RECONFIG_CHANGE_ADD
:
1900 adap
= of_find_i2c_adapter_by_node(rd
->dn
->parent
);
1902 return NOTIFY_OK
; /* not for us */
1904 if (of_node_test_and_set_flag(rd
->dn
, OF_POPULATED
)) {
1905 put_device(&adap
->dev
);
1909 client
= of_i2c_register_device(adap
, rd
->dn
);
1910 put_device(&adap
->dev
);
1912 if (IS_ERR(client
)) {
1913 pr_err("%s: failed to create for '%s'\n",
1914 __func__
, rd
->dn
->full_name
);
1915 return notifier_from_errno(PTR_ERR(client
));
1918 case OF_RECONFIG_CHANGE_REMOVE
:
1919 /* already depopulated? */
1920 if (!of_node_check_flag(rd
->dn
, OF_POPULATED
))
1923 /* find our device by node */
1924 client
= of_find_i2c_device_by_node(rd
->dn
);
1926 return NOTIFY_OK
; /* no? not meant for us */
1928 /* unregister takes one ref away */
1929 i2c_unregister_device(client
);
1931 /* and put the reference of the find */
1932 put_device(&client
->dev
);
1938 static struct notifier_block i2c_of_notifier
= {
1939 .notifier_call
= of_i2c_notify
,
1942 extern struct notifier_block i2c_of_notifier
;
1943 #endif /* CONFIG_OF_DYNAMIC */
1945 static int __init
i2c_init(void)
1949 retval
= of_alias_get_highest_id("i2c");
1951 down_write(&__i2c_board_lock
);
1952 if (retval
>= __i2c_first_dynamic_bus_num
)
1953 __i2c_first_dynamic_bus_num
= retval
+ 1;
1954 up_write(&__i2c_board_lock
);
1956 retval
= bus_register(&i2c_bus_type
);
1959 #ifdef CONFIG_I2C_COMPAT
1960 i2c_adapter_compat_class
= class_compat_register("i2c-adapter");
1961 if (!i2c_adapter_compat_class
) {
1966 retval
= i2c_add_driver(&dummy_driver
);
1970 if (IS_ENABLED(CONFIG_OF_DYNAMIC
))
1971 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier
));
1976 #ifdef CONFIG_I2C_COMPAT
1977 class_compat_unregister(i2c_adapter_compat_class
);
1980 bus_unregister(&i2c_bus_type
);
1984 static void __exit
i2c_exit(void)
1986 if (IS_ENABLED(CONFIG_OF_DYNAMIC
))
1987 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier
));
1988 i2c_del_driver(&dummy_driver
);
1989 #ifdef CONFIG_I2C_COMPAT
1990 class_compat_unregister(i2c_adapter_compat_class
);
1992 bus_unregister(&i2c_bus_type
);
1993 tracepoint_synchronize_unregister();
1996 /* We must initialize early, because some subsystems register i2c drivers
1997 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1999 postcore_initcall(i2c_init
);
2000 module_exit(i2c_exit
);
2002 /* ----------------------------------------------------
2003 * the functional interface to the i2c busses.
2004 * ----------------------------------------------------
2007 /* Check if val is exceeding the quirk IFF quirk is non 0 */
2008 #define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
2010 static int i2c_quirk_error(struct i2c_adapter
*adap
, struct i2c_msg
*msg
, char *err_msg
)
2012 dev_err_ratelimited(&adap
->dev
, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
2013 err_msg
, msg
->addr
, msg
->len
,
2014 msg
->flags
& I2C_M_RD
? "read" : "write");
2018 static int i2c_check_for_quirks(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
2020 const struct i2c_adapter_quirks
*q
= adap
->quirks
;
2021 int max_num
= q
->max_num_msgs
, i
;
2022 bool do_len_check
= true;
2024 if (q
->flags
& I2C_AQ_COMB
) {
2027 /* special checks for combined messages */
2029 if (q
->flags
& I2C_AQ_COMB_WRITE_FIRST
&& msgs
[0].flags
& I2C_M_RD
)
2030 return i2c_quirk_error(adap
, &msgs
[0], "1st comb msg must be write");
2032 if (q
->flags
& I2C_AQ_COMB_READ_SECOND
&& !(msgs
[1].flags
& I2C_M_RD
))
2033 return i2c_quirk_error(adap
, &msgs
[1], "2nd comb msg must be read");
2035 if (q
->flags
& I2C_AQ_COMB_SAME_ADDR
&& msgs
[0].addr
!= msgs
[1].addr
)
2036 return i2c_quirk_error(adap
, &msgs
[0], "comb msg only to same addr");
2038 if (i2c_quirk_exceeded(msgs
[0].len
, q
->max_comb_1st_msg_len
))
2039 return i2c_quirk_error(adap
, &msgs
[0], "msg too long");
2041 if (i2c_quirk_exceeded(msgs
[1].len
, q
->max_comb_2nd_msg_len
))
2042 return i2c_quirk_error(adap
, &msgs
[1], "msg too long");
2044 do_len_check
= false;
2048 if (i2c_quirk_exceeded(num
, max_num
))
2049 return i2c_quirk_error(adap
, &msgs
[0], "too many messages");
2051 for (i
= 0; i
< num
; i
++) {
2052 u16 len
= msgs
[i
].len
;
2054 if (msgs
[i
].flags
& I2C_M_RD
) {
2055 if (do_len_check
&& i2c_quirk_exceeded(len
, q
->max_read_len
))
2056 return i2c_quirk_error(adap
, &msgs
[i
], "msg too long");
2058 if (do_len_check
&& i2c_quirk_exceeded(len
, q
->max_write_len
))
2059 return i2c_quirk_error(adap
, &msgs
[i
], "msg too long");
2067 * __i2c_transfer - unlocked flavor of i2c_transfer
2068 * @adap: Handle to I2C bus
2069 * @msgs: One or more messages to execute before STOP is issued to
2070 * terminate the operation; each message begins with a START.
2071 * @num: Number of messages to be executed.
2073 * Returns negative errno, else the number of messages executed.
2075 * Adapter lock must be held when calling this function. No debug logging
2076 * takes place. adap->algo->master_xfer existence isn't checked.
2078 int __i2c_transfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
2080 unsigned long orig_jiffies
;
2083 if (adap
->quirks
&& i2c_check_for_quirks(adap
, msgs
, num
))
2086 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2087 * enabled. This is an efficient way of keeping the for-loop from
2088 * being executed when not needed.
2090 if (static_key_false(&i2c_trace_msg
)) {
2092 for (i
= 0; i
< num
; i
++)
2093 if (msgs
[i
].flags
& I2C_M_RD
)
2094 trace_i2c_read(adap
, &msgs
[i
], i
);
2096 trace_i2c_write(adap
, &msgs
[i
], i
);
2099 /* Retry automatically on arbitration loss */
2100 orig_jiffies
= jiffies
;
2101 for (ret
= 0, try = 0; try <= adap
->retries
; try++) {
2102 ret
= adap
->algo
->master_xfer(adap
, msgs
, num
);
2105 if (time_after(jiffies
, orig_jiffies
+ adap
->timeout
))
2109 if (static_key_false(&i2c_trace_msg
)) {
2111 for (i
= 0; i
< ret
; i
++)
2112 if (msgs
[i
].flags
& I2C_M_RD
)
2113 trace_i2c_reply(adap
, &msgs
[i
], i
);
2114 trace_i2c_result(adap
, i
, ret
);
2119 EXPORT_SYMBOL(__i2c_transfer
);
2122 * i2c_transfer - execute a single or combined I2C message
2123 * @adap: Handle to I2C bus
2124 * @msgs: One or more messages to execute before STOP is issued to
2125 * terminate the operation; each message begins with a START.
2126 * @num: Number of messages to be executed.
2128 * Returns negative errno, else the number of messages executed.
2130 * Note that there is no requirement that each message be sent to
2131 * the same slave address, although that is the most common model.
2133 int i2c_transfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
2137 /* REVISIT the fault reporting model here is weak:
2139 * - When we get an error after receiving N bytes from a slave,
2140 * there is no way to report "N".
2142 * - When we get a NAK after transmitting N bytes to a slave,
2143 * there is no way to report "N" ... or to let the master
2144 * continue executing the rest of this combined message, if
2145 * that's the appropriate response.
2147 * - When for example "num" is two and we successfully complete
2148 * the first message but get an error part way through the
2149 * second, it's unclear whether that should be reported as
2150 * one (discarding status on the second message) or errno
2151 * (discarding status on the first one).
2154 if (adap
->algo
->master_xfer
) {
2156 for (ret
= 0; ret
< num
; ret
++) {
2157 dev_dbg(&adap
->dev
, "master_xfer[%d] %c, addr=0x%02x, "
2158 "len=%d%s\n", ret
, (msgs
[ret
].flags
& I2C_M_RD
)
2159 ? 'R' : 'W', msgs
[ret
].addr
, msgs
[ret
].len
,
2160 (msgs
[ret
].flags
& I2C_M_RECV_LEN
) ? "+" : "");
2164 if (in_atomic() || irqs_disabled()) {
2165 ret
= i2c_trylock_adapter(adap
);
2167 /* I2C activity is ongoing. */
2170 i2c_lock_adapter(adap
);
2173 ret
= __i2c_transfer(adap
, msgs
, num
);
2174 i2c_unlock_adapter(adap
);
2178 dev_dbg(&adap
->dev
, "I2C level transfers not supported\n");
2182 EXPORT_SYMBOL(i2c_transfer
);
2185 * i2c_master_send - issue a single I2C message in master transmit mode
2186 * @client: Handle to slave device
2187 * @buf: Data that will be written to the slave
2188 * @count: How many bytes to write, must be less than 64k since msg.len is u16
2190 * Returns negative errno, or else the number of bytes written.
2192 int i2c_master_send(const struct i2c_client
*client
, const char *buf
, int count
)
2195 struct i2c_adapter
*adap
= client
->adapter
;
2198 msg
.addr
= client
->addr
;
2199 msg
.flags
= client
->flags
& I2C_M_TEN
;
2201 msg
.buf
= (char *)buf
;
2203 ret
= i2c_transfer(adap
, &msg
, 1);
2206 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2207 * transmitted, else error code.
2209 return (ret
== 1) ? count
: ret
;
2211 EXPORT_SYMBOL(i2c_master_send
);
2214 * i2c_master_recv - issue a single I2C message in master receive mode
2215 * @client: Handle to slave device
2216 * @buf: Where to store data read from slave
2217 * @count: How many bytes to read, must be less than 64k since msg.len is u16
2219 * Returns negative errno, or else the number of bytes read.
2221 int i2c_master_recv(const struct i2c_client
*client
, char *buf
, int count
)
2223 struct i2c_adapter
*adap
= client
->adapter
;
2227 msg
.addr
= client
->addr
;
2228 msg
.flags
= client
->flags
& I2C_M_TEN
;
2229 msg
.flags
|= I2C_M_RD
;
2233 ret
= i2c_transfer(adap
, &msg
, 1);
2236 * If everything went ok (i.e. 1 msg received), return #bytes received,
2239 return (ret
== 1) ? count
: ret
;
2241 EXPORT_SYMBOL(i2c_master_recv
);
2243 /* ----------------------------------------------------
2244 * the i2c address scanning function
2245 * Will not work for 10-bit addresses!
2246 * ----------------------------------------------------
2250 * Legacy default probe function, mostly relevant for SMBus. The default
2251 * probe method is a quick write, but it is known to corrupt the 24RF08
2252 * EEPROMs due to a state machine bug, and could also irreversibly
2253 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2254 * we use a short byte read instead. Also, some bus drivers don't implement
2255 * quick write, so we fallback to a byte read in that case too.
2256 * On x86, there is another special case for FSC hardware monitoring chips,
2257 * which want regular byte reads (address 0x73.) Fortunately, these are the
2258 * only known chips using this I2C address on PC hardware.
2259 * Returns 1 if probe succeeded, 0 if not.
2261 static int i2c_default_probe(struct i2c_adapter
*adap
, unsigned short addr
)
2264 union i2c_smbus_data dummy
;
2267 if (addr
== 0x73 && (adap
->class & I2C_CLASS_HWMON
)
2268 && i2c_check_functionality(adap
, I2C_FUNC_SMBUS_READ_BYTE_DATA
))
2269 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
2270 I2C_SMBUS_BYTE_DATA
, &dummy
);
2273 if (!((addr
& ~0x07) == 0x30 || (addr
& ~0x0f) == 0x50)
2274 && i2c_check_functionality(adap
, I2C_FUNC_SMBUS_QUICK
))
2275 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_WRITE
, 0,
2276 I2C_SMBUS_QUICK
, NULL
);
2277 else if (i2c_check_functionality(adap
, I2C_FUNC_SMBUS_READ_BYTE
))
2278 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
2279 I2C_SMBUS_BYTE
, &dummy
);
2281 dev_warn(&adap
->dev
, "No suitable probing method supported for address 0x%02X\n",
2289 static int i2c_detect_address(struct i2c_client
*temp_client
,
2290 struct i2c_driver
*driver
)
2292 struct i2c_board_info info
;
2293 struct i2c_adapter
*adapter
= temp_client
->adapter
;
2294 int addr
= temp_client
->addr
;
2297 /* Make sure the address is valid */
2298 err
= i2c_check_7bit_addr_validity_strict(addr
);
2300 dev_warn(&adapter
->dev
, "Invalid probe address 0x%02x\n",
2305 /* Skip if already in use */
2306 if (i2c_check_addr_busy(adapter
, addr
))
2309 /* Make sure there is something at this address */
2310 if (!i2c_default_probe(adapter
, addr
))
2313 /* Finally call the custom detection function */
2314 memset(&info
, 0, sizeof(struct i2c_board_info
));
2316 err
= driver
->detect(temp_client
, &info
);
2318 /* -ENODEV is returned if the detection fails. We catch it
2319 here as this isn't an error. */
2320 return err
== -ENODEV
? 0 : err
;
2323 /* Consistency check */
2324 if (info
.type
[0] == '\0') {
2325 dev_err(&adapter
->dev
, "%s detection function provided "
2326 "no name for 0x%x\n", driver
->driver
.name
,
2329 struct i2c_client
*client
;
2331 /* Detection succeeded, instantiate the device */
2332 if (adapter
->class & I2C_CLASS_DEPRECATED
)
2333 dev_warn(&adapter
->dev
,
2334 "This adapter will soon drop class based instantiation of devices. "
2335 "Please make sure client 0x%02x gets instantiated by other means. "
2336 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2339 dev_dbg(&adapter
->dev
, "Creating %s at 0x%02x\n",
2340 info
.type
, info
.addr
);
2341 client
= i2c_new_device(adapter
, &info
);
2343 list_add_tail(&client
->detected
, &driver
->clients
);
2345 dev_err(&adapter
->dev
, "Failed creating %s at 0x%02x\n",
2346 info
.type
, info
.addr
);
2351 static int i2c_detect(struct i2c_adapter
*adapter
, struct i2c_driver
*driver
)
2353 const unsigned short *address_list
;
2354 struct i2c_client
*temp_client
;
2356 int adap_id
= i2c_adapter_id(adapter
);
2358 address_list
= driver
->address_list
;
2359 if (!driver
->detect
|| !address_list
)
2362 /* Warn that the adapter lost class based instantiation */
2363 if (adapter
->class == I2C_CLASS_DEPRECATED
) {
2364 dev_dbg(&adapter
->dev
,
2365 "This adapter dropped support for I2C classes and "
2366 "won't auto-detect %s devices anymore. If you need it, check "
2367 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2368 driver
->driver
.name
);
2372 /* Stop here if the classes do not match */
2373 if (!(adapter
->class & driver
->class))
2376 /* Set up a temporary client to help detect callback */
2377 temp_client
= kzalloc(sizeof(struct i2c_client
), GFP_KERNEL
);
2380 temp_client
->adapter
= adapter
;
2382 for (i
= 0; address_list
[i
] != I2C_CLIENT_END
; i
+= 1) {
2383 dev_dbg(&adapter
->dev
, "found normal entry for adapter %d, "
2384 "addr 0x%02x\n", adap_id
, address_list
[i
]);
2385 temp_client
->addr
= address_list
[i
];
2386 err
= i2c_detect_address(temp_client
, driver
);
2395 int i2c_probe_func_quick_read(struct i2c_adapter
*adap
, unsigned short addr
)
2397 return i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
2398 I2C_SMBUS_QUICK
, NULL
) >= 0;
2400 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read
);
2403 i2c_new_probed_device(struct i2c_adapter
*adap
,
2404 struct i2c_board_info
*info
,
2405 unsigned short const *addr_list
,
2406 int (*probe
)(struct i2c_adapter
*, unsigned short addr
))
2411 probe
= i2c_default_probe
;
2413 for (i
= 0; addr_list
[i
] != I2C_CLIENT_END
; i
++) {
2414 /* Check address validity */
2415 if (i2c_check_7bit_addr_validity_strict(addr_list
[i
]) < 0) {
2416 dev_warn(&adap
->dev
, "Invalid 7-bit address "
2417 "0x%02x\n", addr_list
[i
]);
2421 /* Check address availability */
2422 if (i2c_check_addr_busy(adap
, addr_list
[i
])) {
2423 dev_dbg(&adap
->dev
, "Address 0x%02x already in "
2424 "use, not probing\n", addr_list
[i
]);
2428 /* Test address responsiveness */
2429 if (probe(adap
, addr_list
[i
]))
2433 if (addr_list
[i
] == I2C_CLIENT_END
) {
2434 dev_dbg(&adap
->dev
, "Probing failed, no device found\n");
2438 info
->addr
= addr_list
[i
];
2439 return i2c_new_device(adap
, info
);
2441 EXPORT_SYMBOL_GPL(i2c_new_probed_device
);
2443 struct i2c_adapter
*i2c_get_adapter(int nr
)
2445 struct i2c_adapter
*adapter
;
2447 mutex_lock(&core_lock
);
2448 adapter
= idr_find(&i2c_adapter_idr
, nr
);
2452 if (try_module_get(adapter
->owner
))
2453 get_device(&adapter
->dev
);
2458 mutex_unlock(&core_lock
);
2461 EXPORT_SYMBOL(i2c_get_adapter
);
2463 void i2c_put_adapter(struct i2c_adapter
*adap
)
2468 put_device(&adap
->dev
);
2469 module_put(adap
->owner
);
2471 EXPORT_SYMBOL(i2c_put_adapter
);
2473 /* The SMBus parts */
2475 #define POLY (0x1070U << 3)
2476 static u8
crc8(u16 data
)
2480 for (i
= 0; i
< 8; i
++) {
2485 return (u8
)(data
>> 8);
2488 /* Incremental CRC8 over count bytes in the array pointed to by p */
2489 static u8
i2c_smbus_pec(u8 crc
, u8
*p
, size_t count
)
2493 for (i
= 0; i
< count
; i
++)
2494 crc
= crc8((crc
^ p
[i
]) << 8);
2498 /* Assume a 7-bit address, which is reasonable for SMBus */
2499 static u8
i2c_smbus_msg_pec(u8 pec
, struct i2c_msg
*msg
)
2501 /* The address will be sent first */
2502 u8 addr
= (msg
->addr
<< 1) | !!(msg
->flags
& I2C_M_RD
);
2503 pec
= i2c_smbus_pec(pec
, &addr
, 1);
2505 /* The data buffer follows */
2506 return i2c_smbus_pec(pec
, msg
->buf
, msg
->len
);
2509 /* Used for write only transactions */
2510 static inline void i2c_smbus_add_pec(struct i2c_msg
*msg
)
2512 msg
->buf
[msg
->len
] = i2c_smbus_msg_pec(0, msg
);
2516 /* Return <0 on CRC error
2517 If there was a write before this read (most cases) we need to take the
2518 partial CRC from the write part into account.
2519 Note that this function does modify the message (we need to decrease the
2520 message length to hide the CRC byte from the caller). */
2521 static int i2c_smbus_check_pec(u8 cpec
, struct i2c_msg
*msg
)
2523 u8 rpec
= msg
->buf
[--msg
->len
];
2524 cpec
= i2c_smbus_msg_pec(cpec
, msg
);
2527 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2535 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2536 * @client: Handle to slave device
2538 * This executes the SMBus "receive byte" protocol, returning negative errno
2539 * else the byte received from the device.
2541 s32
i2c_smbus_read_byte(const struct i2c_client
*client
)
2543 union i2c_smbus_data data
;
2546 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2548 I2C_SMBUS_BYTE
, &data
);
2549 return (status
< 0) ? status
: data
.byte
;
2551 EXPORT_SYMBOL(i2c_smbus_read_byte
);
2554 * i2c_smbus_write_byte - SMBus "send byte" protocol
2555 * @client: Handle to slave device
2556 * @value: Byte to be sent
2558 * This executes the SMBus "send byte" protocol, returning negative errno
2559 * else zero on success.
2561 s32
i2c_smbus_write_byte(const struct i2c_client
*client
, u8 value
)
2563 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2564 I2C_SMBUS_WRITE
, value
, I2C_SMBUS_BYTE
, NULL
);
2566 EXPORT_SYMBOL(i2c_smbus_write_byte
);
2569 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2570 * @client: Handle to slave device
2571 * @command: Byte interpreted by slave
2573 * This executes the SMBus "read byte" protocol, returning negative errno
2574 * else a data byte received from the device.
2576 s32
i2c_smbus_read_byte_data(const struct i2c_client
*client
, u8 command
)
2578 union i2c_smbus_data data
;
2581 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2582 I2C_SMBUS_READ
, command
,
2583 I2C_SMBUS_BYTE_DATA
, &data
);
2584 return (status
< 0) ? status
: data
.byte
;
2586 EXPORT_SYMBOL(i2c_smbus_read_byte_data
);
2589 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2590 * @client: Handle to slave device
2591 * @command: Byte interpreted by slave
2592 * @value: Byte being written
2594 * This executes the SMBus "write byte" protocol, returning negative errno
2595 * else zero on success.
2597 s32
i2c_smbus_write_byte_data(const struct i2c_client
*client
, u8 command
,
2600 union i2c_smbus_data data
;
2602 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2603 I2C_SMBUS_WRITE
, command
,
2604 I2C_SMBUS_BYTE_DATA
, &data
);
2606 EXPORT_SYMBOL(i2c_smbus_write_byte_data
);
2609 * i2c_smbus_read_word_data - SMBus "read word" protocol
2610 * @client: Handle to slave device
2611 * @command: Byte interpreted by slave
2613 * This executes the SMBus "read word" protocol, returning negative errno
2614 * else a 16-bit unsigned "word" received from the device.
2616 s32
i2c_smbus_read_word_data(const struct i2c_client
*client
, u8 command
)
2618 union i2c_smbus_data data
;
2621 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2622 I2C_SMBUS_READ
, command
,
2623 I2C_SMBUS_WORD_DATA
, &data
);
2624 return (status
< 0) ? status
: data
.word
;
2626 EXPORT_SYMBOL(i2c_smbus_read_word_data
);
2629 * i2c_smbus_write_word_data - SMBus "write word" protocol
2630 * @client: Handle to slave device
2631 * @command: Byte interpreted by slave
2632 * @value: 16-bit "word" being written
2634 * This executes the SMBus "write word" protocol, returning negative errno
2635 * else zero on success.
2637 s32
i2c_smbus_write_word_data(const struct i2c_client
*client
, u8 command
,
2640 union i2c_smbus_data data
;
2642 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2643 I2C_SMBUS_WRITE
, command
,
2644 I2C_SMBUS_WORD_DATA
, &data
);
2646 EXPORT_SYMBOL(i2c_smbus_write_word_data
);
2649 * i2c_smbus_read_block_data - SMBus "block read" protocol
2650 * @client: Handle to slave device
2651 * @command: Byte interpreted by slave
2652 * @values: Byte array into which data will be read; big enough to hold
2653 * the data returned by the slave. SMBus allows at most 32 bytes.
2655 * This executes the SMBus "block read" protocol, returning negative errno
2656 * else the number of data bytes in the slave's response.
2658 * Note that using this function requires that the client's adapter support
2659 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2660 * support this; its emulation through I2C messaging relies on a specific
2661 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2663 s32
i2c_smbus_read_block_data(const struct i2c_client
*client
, u8 command
,
2666 union i2c_smbus_data data
;
2669 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2670 I2C_SMBUS_READ
, command
,
2671 I2C_SMBUS_BLOCK_DATA
, &data
);
2675 memcpy(values
, &data
.block
[1], data
.block
[0]);
2676 return data
.block
[0];
2678 EXPORT_SYMBOL(i2c_smbus_read_block_data
);
2681 * i2c_smbus_write_block_data - SMBus "block write" protocol
2682 * @client: Handle to slave device
2683 * @command: Byte interpreted by slave
2684 * @length: Size of data block; SMBus allows at most 32 bytes
2685 * @values: Byte array which will be written.
2687 * This executes the SMBus "block write" protocol, returning negative errno
2688 * else zero on success.
2690 s32
i2c_smbus_write_block_data(const struct i2c_client
*client
, u8 command
,
2691 u8 length
, const u8
*values
)
2693 union i2c_smbus_data data
;
2695 if (length
> I2C_SMBUS_BLOCK_MAX
)
2696 length
= I2C_SMBUS_BLOCK_MAX
;
2697 data
.block
[0] = length
;
2698 memcpy(&data
.block
[1], values
, length
);
2699 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2700 I2C_SMBUS_WRITE
, command
,
2701 I2C_SMBUS_BLOCK_DATA
, &data
);
2703 EXPORT_SYMBOL(i2c_smbus_write_block_data
);
2705 /* Returns the number of read bytes */
2706 s32
i2c_smbus_read_i2c_block_data(const struct i2c_client
*client
, u8 command
,
2707 u8 length
, u8
*values
)
2709 union i2c_smbus_data data
;
2712 if (length
> I2C_SMBUS_BLOCK_MAX
)
2713 length
= I2C_SMBUS_BLOCK_MAX
;
2714 data
.block
[0] = length
;
2715 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2716 I2C_SMBUS_READ
, command
,
2717 I2C_SMBUS_I2C_BLOCK_DATA
, &data
);
2721 memcpy(values
, &data
.block
[1], data
.block
[0]);
2722 return data
.block
[0];
2724 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data
);
2726 s32
i2c_smbus_write_i2c_block_data(const struct i2c_client
*client
, u8 command
,
2727 u8 length
, const u8
*values
)
2729 union i2c_smbus_data data
;
2731 if (length
> I2C_SMBUS_BLOCK_MAX
)
2732 length
= I2C_SMBUS_BLOCK_MAX
;
2733 data
.block
[0] = length
;
2734 memcpy(data
.block
+ 1, values
, length
);
2735 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2736 I2C_SMBUS_WRITE
, command
,
2737 I2C_SMBUS_I2C_BLOCK_DATA
, &data
);
2739 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data
);
2741 /* Simulate a SMBus command using the i2c protocol
2742 No checking of parameters is done! */
2743 static s32
i2c_smbus_xfer_emulated(struct i2c_adapter
*adapter
, u16 addr
,
2744 unsigned short flags
,
2745 char read_write
, u8 command
, int size
,
2746 union i2c_smbus_data
*data
)
2748 /* So we need to generate a series of msgs. In the case of writing, we
2749 need to use only one message; when reading, we need two. We initialize
2750 most things with sane defaults, to keep the code below somewhat
2752 unsigned char msgbuf0
[I2C_SMBUS_BLOCK_MAX
+3];
2753 unsigned char msgbuf1
[I2C_SMBUS_BLOCK_MAX
+2];
2754 int num
= read_write
== I2C_SMBUS_READ
? 2 : 1;
2758 struct i2c_msg msg
[2] = {
2766 .flags
= flags
| I2C_M_RD
,
2772 msgbuf0
[0] = command
;
2774 case I2C_SMBUS_QUICK
:
2776 /* Special case: The read/write field is used as data */
2777 msg
[0].flags
= flags
| (read_write
== I2C_SMBUS_READ
?
2781 case I2C_SMBUS_BYTE
:
2782 if (read_write
== I2C_SMBUS_READ
) {
2783 /* Special case: only a read! */
2784 msg
[0].flags
= I2C_M_RD
| flags
;
2788 case I2C_SMBUS_BYTE_DATA
:
2789 if (read_write
== I2C_SMBUS_READ
)
2793 msgbuf0
[1] = data
->byte
;
2796 case I2C_SMBUS_WORD_DATA
:
2797 if (read_write
== I2C_SMBUS_READ
)
2801 msgbuf0
[1] = data
->word
& 0xff;
2802 msgbuf0
[2] = data
->word
>> 8;
2805 case I2C_SMBUS_PROC_CALL
:
2806 num
= 2; /* Special case */
2807 read_write
= I2C_SMBUS_READ
;
2810 msgbuf0
[1] = data
->word
& 0xff;
2811 msgbuf0
[2] = data
->word
>> 8;
2813 case I2C_SMBUS_BLOCK_DATA
:
2814 if (read_write
== I2C_SMBUS_READ
) {
2815 msg
[1].flags
|= I2C_M_RECV_LEN
;
2816 msg
[1].len
= 1; /* block length will be added by
2817 the underlying bus driver */
2819 msg
[0].len
= data
->block
[0] + 2;
2820 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 2) {
2821 dev_err(&adapter
->dev
,
2822 "Invalid block write size %d\n",
2826 for (i
= 1; i
< msg
[0].len
; i
++)
2827 msgbuf0
[i
] = data
->block
[i
-1];
2830 case I2C_SMBUS_BLOCK_PROC_CALL
:
2831 num
= 2; /* Another special case */
2832 read_write
= I2C_SMBUS_READ
;
2833 if (data
->block
[0] > I2C_SMBUS_BLOCK_MAX
) {
2834 dev_err(&adapter
->dev
,
2835 "Invalid block write size %d\n",
2839 msg
[0].len
= data
->block
[0] + 2;
2840 for (i
= 1; i
< msg
[0].len
; i
++)
2841 msgbuf0
[i
] = data
->block
[i
-1];
2842 msg
[1].flags
|= I2C_M_RECV_LEN
;
2843 msg
[1].len
= 1; /* block length will be added by
2844 the underlying bus driver */
2846 case I2C_SMBUS_I2C_BLOCK_DATA
:
2847 if (read_write
== I2C_SMBUS_READ
) {
2848 msg
[1].len
= data
->block
[0];
2850 msg
[0].len
= data
->block
[0] + 1;
2851 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 1) {
2852 dev_err(&adapter
->dev
,
2853 "Invalid block write size %d\n",
2857 for (i
= 1; i
<= data
->block
[0]; i
++)
2858 msgbuf0
[i
] = data
->block
[i
];
2862 dev_err(&adapter
->dev
, "Unsupported transaction %d\n", size
);
2866 i
= ((flags
& I2C_CLIENT_PEC
) && size
!= I2C_SMBUS_QUICK
2867 && size
!= I2C_SMBUS_I2C_BLOCK_DATA
);
2869 /* Compute PEC if first message is a write */
2870 if (!(msg
[0].flags
& I2C_M_RD
)) {
2871 if (num
== 1) /* Write only */
2872 i2c_smbus_add_pec(&msg
[0]);
2873 else /* Write followed by read */
2874 partial_pec
= i2c_smbus_msg_pec(0, &msg
[0]);
2876 /* Ask for PEC if last message is a read */
2877 if (msg
[num
-1].flags
& I2C_M_RD
)
2881 status
= i2c_transfer(adapter
, msg
, num
);
2885 /* Check PEC if last message is a read */
2886 if (i
&& (msg
[num
-1].flags
& I2C_M_RD
)) {
2887 status
= i2c_smbus_check_pec(partial_pec
, &msg
[num
-1]);
2892 if (read_write
== I2C_SMBUS_READ
)
2894 case I2C_SMBUS_BYTE
:
2895 data
->byte
= msgbuf0
[0];
2897 case I2C_SMBUS_BYTE_DATA
:
2898 data
->byte
= msgbuf1
[0];
2900 case I2C_SMBUS_WORD_DATA
:
2901 case I2C_SMBUS_PROC_CALL
:
2902 data
->word
= msgbuf1
[0] | (msgbuf1
[1] << 8);
2904 case I2C_SMBUS_I2C_BLOCK_DATA
:
2905 for (i
= 0; i
< data
->block
[0]; i
++)
2906 data
->block
[i
+1] = msgbuf1
[i
];
2908 case I2C_SMBUS_BLOCK_DATA
:
2909 case I2C_SMBUS_BLOCK_PROC_CALL
:
2910 for (i
= 0; i
< msgbuf1
[0] + 1; i
++)
2911 data
->block
[i
] = msgbuf1
[i
];
2918 * i2c_smbus_xfer - execute SMBus protocol operations
2919 * @adapter: Handle to I2C bus
2920 * @addr: Address of SMBus slave on that bus
2921 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2922 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2923 * @command: Byte interpreted by slave, for protocols which use such bytes
2924 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2925 * @data: Data to be read or written
2927 * This executes an SMBus protocol operation, and returns a negative
2928 * errno code else zero on success.
2930 s32
i2c_smbus_xfer(struct i2c_adapter
*adapter
, u16 addr
, unsigned short flags
,
2931 char read_write
, u8 command
, int protocol
,
2932 union i2c_smbus_data
*data
)
2934 unsigned long orig_jiffies
;
2938 /* If enabled, the following two tracepoints are conditional on
2939 * read_write and protocol.
2941 trace_smbus_write(adapter
, addr
, flags
, read_write
,
2942 command
, protocol
, data
);
2943 trace_smbus_read(adapter
, addr
, flags
, read_write
,
2946 flags
&= I2C_M_TEN
| I2C_CLIENT_PEC
| I2C_CLIENT_SCCB
;
2948 if (adapter
->algo
->smbus_xfer
) {
2949 i2c_lock_adapter(adapter
);
2951 /* Retry automatically on arbitration loss */
2952 orig_jiffies
= jiffies
;
2953 for (res
= 0, try = 0; try <= adapter
->retries
; try++) {
2954 res
= adapter
->algo
->smbus_xfer(adapter
, addr
, flags
,
2955 read_write
, command
,
2959 if (time_after(jiffies
,
2960 orig_jiffies
+ adapter
->timeout
))
2963 i2c_unlock_adapter(adapter
);
2965 if (res
!= -EOPNOTSUPP
|| !adapter
->algo
->master_xfer
)
2968 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2969 * implement native support for the SMBus operation.
2973 res
= i2c_smbus_xfer_emulated(adapter
, addr
, flags
, read_write
,
2974 command
, protocol
, data
);
2977 /* If enabled, the reply tracepoint is conditional on read_write. */
2978 trace_smbus_reply(adapter
, addr
, flags
, read_write
,
2979 command
, protocol
, data
);
2980 trace_smbus_result(adapter
, addr
, flags
, read_write
,
2981 command
, protocol
, res
);
2985 EXPORT_SYMBOL(i2c_smbus_xfer
);
2987 #if IS_ENABLED(CONFIG_I2C_SLAVE)
2988 int i2c_slave_register(struct i2c_client
*client
, i2c_slave_cb_t slave_cb
)
2992 if (!client
|| !slave_cb
) {
2993 WARN(1, "insufficent data\n");
2997 if (!(client
->flags
& I2C_CLIENT_TEN
)) {
2998 /* Enforce stricter address checking */
2999 ret
= i2c_check_7bit_addr_validity_strict(client
->addr
);
3001 dev_err(&client
->dev
, "%s: invalid address\n", __func__
);
3006 if (!client
->adapter
->algo
->reg_slave
) {
3007 dev_err(&client
->dev
, "%s: not supported by adapter\n", __func__
);
3011 client
->slave_cb
= slave_cb
;
3013 i2c_lock_adapter(client
->adapter
);
3014 ret
= client
->adapter
->algo
->reg_slave(client
);
3015 i2c_unlock_adapter(client
->adapter
);
3018 client
->slave_cb
= NULL
;
3019 dev_err(&client
->dev
, "%s: adapter returned error %d\n", __func__
, ret
);
3024 EXPORT_SYMBOL_GPL(i2c_slave_register
);
3026 int i2c_slave_unregister(struct i2c_client
*client
)
3030 if (!client
->adapter
->algo
->unreg_slave
) {
3031 dev_err(&client
->dev
, "%s: not supported by adapter\n", __func__
);
3035 i2c_lock_adapter(client
->adapter
);
3036 ret
= client
->adapter
->algo
->unreg_slave(client
);
3037 i2c_unlock_adapter(client
->adapter
);
3040 client
->slave_cb
= NULL
;
3042 dev_err(&client
->dev
, "%s: adapter returned error %d\n", __func__
, ret
);
3046 EXPORT_SYMBOL_GPL(i2c_slave_unregister
);
3049 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3050 MODULE_DESCRIPTION("I2C-Bus main module");
3051 MODULE_LICENSE("GPL");