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
) {
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
);
138 INIT_LIST_HEAD(&resource_list
);
139 ret
= acpi_dev_get_resources(adev
, &resource_list
,
140 acpi_i2c_add_resource
, &info
);
141 acpi_dev_free_resource_list(&resource_list
);
143 if (ret
< 0 || !info
.addr
)
146 adev
->power
.flags
.ignore_parent
= true;
147 strlcpy(info
.type
, dev_name(&adev
->dev
), sizeof(info
.type
));
148 if (!i2c_new_device(adapter
, &info
)) {
149 adev
->power
.flags
.ignore_parent
= false;
150 dev_err(&adapter
->dev
,
151 "failed to add I2C device %s from ACPI\n",
152 dev_name(&adev
->dev
));
159 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
160 * @adap: pointer to adapter
162 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
163 * namespace. When a device is found it will be added to the Linux device
164 * model and bound to the corresponding ACPI handle.
166 static void acpi_i2c_register_devices(struct i2c_adapter
*adap
)
171 if (!adap
->dev
.parent
)
174 handle
= ACPI_HANDLE(adap
->dev
.parent
);
178 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, 1,
179 acpi_i2c_add_device
, NULL
,
181 if (ACPI_FAILURE(status
))
182 dev_warn(&adap
->dev
, "failed to enumerate I2C slaves\n");
185 #else /* CONFIG_ACPI */
186 static inline void acpi_i2c_register_devices(struct i2c_adapter
*adap
) { }
187 #endif /* CONFIG_ACPI */
189 #ifdef CONFIG_ACPI_I2C_OPREGION
190 static int acpi_gsb_i2c_read_bytes(struct i2c_client
*client
,
191 u8 cmd
, u8
*data
, u8 data_len
)
194 struct i2c_msg msgs
[2];
198 buffer
= kzalloc(data_len
, GFP_KERNEL
);
202 msgs
[0].addr
= client
->addr
;
203 msgs
[0].flags
= client
->flags
;
207 msgs
[1].addr
= client
->addr
;
208 msgs
[1].flags
= client
->flags
| I2C_M_RD
;
209 msgs
[1].len
= data_len
;
210 msgs
[1].buf
= buffer
;
212 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
214 dev_err(&client
->adapter
->dev
, "i2c read failed\n");
216 memcpy(data
, buffer
, data_len
);
222 static int acpi_gsb_i2c_write_bytes(struct i2c_client
*client
,
223 u8 cmd
, u8
*data
, u8 data_len
)
226 struct i2c_msg msgs
[1];
230 buffer
= kzalloc(data_len
+ 1, GFP_KERNEL
);
235 memcpy(buffer
+ 1, data
, data_len
);
237 msgs
[0].addr
= client
->addr
;
238 msgs
[0].flags
= client
->flags
;
239 msgs
[0].len
= data_len
+ 1;
240 msgs
[0].buf
= buffer
;
242 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
244 dev_err(&client
->adapter
->dev
, "i2c write failed\n");
251 acpi_i2c_space_handler(u32 function
, acpi_physical_address command
,
252 u32 bits
, u64
*value64
,
253 void *handler_context
, void *region_context
)
255 struct gsb_buffer
*gsb
= (struct gsb_buffer
*)value64
;
256 struct acpi_i2c_handler_data
*data
= handler_context
;
257 struct acpi_connection_info
*info
= &data
->info
;
258 struct acpi_resource_i2c_serialbus
*sb
;
259 struct i2c_adapter
*adapter
= data
->adapter
;
260 struct i2c_client
*client
;
261 struct acpi_resource
*ares
;
262 u32 accessor_type
= function
>> 16;
263 u8 action
= function
& ACPI_IO_MASK
;
267 ret
= acpi_buffer_to_resource(info
->connection
, info
->length
, &ares
);
268 if (ACPI_FAILURE(ret
))
271 client
= kzalloc(sizeof(*client
), GFP_KERNEL
);
277 if (!value64
|| ares
->type
!= ACPI_RESOURCE_TYPE_SERIAL_BUS
) {
278 ret
= AE_BAD_PARAMETER
;
282 sb
= &ares
->data
.i2c_serial_bus
;
283 if (sb
->type
!= ACPI_RESOURCE_SERIAL_TYPE_I2C
) {
284 ret
= AE_BAD_PARAMETER
;
288 client
->adapter
= adapter
;
289 client
->addr
= sb
->slave_address
;
291 if (sb
->access_mode
== ACPI_I2C_10BIT_MODE
)
292 client
->flags
|= I2C_CLIENT_TEN
;
294 switch (accessor_type
) {
295 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV
:
296 if (action
== ACPI_READ
) {
297 status
= i2c_smbus_read_byte(client
);
303 status
= i2c_smbus_write_byte(client
, gsb
->bdata
);
307 case ACPI_GSB_ACCESS_ATTRIB_BYTE
:
308 if (action
== ACPI_READ
) {
309 status
= i2c_smbus_read_byte_data(client
, command
);
315 status
= i2c_smbus_write_byte_data(client
, command
,
320 case ACPI_GSB_ACCESS_ATTRIB_WORD
:
321 if (action
== ACPI_READ
) {
322 status
= i2c_smbus_read_word_data(client
, command
);
328 status
= i2c_smbus_write_word_data(client
, command
,
333 case ACPI_GSB_ACCESS_ATTRIB_BLOCK
:
334 if (action
== ACPI_READ
) {
335 status
= i2c_smbus_read_block_data(client
, command
,
342 status
= i2c_smbus_write_block_data(client
, command
,
343 gsb
->len
, gsb
->data
);
347 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE
:
348 if (action
== ACPI_READ
) {
349 status
= acpi_gsb_i2c_read_bytes(client
, command
,
350 gsb
->data
, info
->access_length
);
354 status
= acpi_gsb_i2c_write_bytes(client
, command
,
355 gsb
->data
, info
->access_length
);
360 pr_info("protocol(0x%02x) is not supported.\n", accessor_type
);
361 ret
= AE_BAD_PARAMETER
;
365 gsb
->status
= status
;
374 static int acpi_i2c_install_space_handler(struct i2c_adapter
*adapter
)
377 struct acpi_i2c_handler_data
*data
;
380 if (!adapter
->dev
.parent
)
383 handle
= ACPI_HANDLE(adapter
->dev
.parent
);
388 data
= kzalloc(sizeof(struct acpi_i2c_handler_data
),
393 data
->adapter
= adapter
;
394 status
= acpi_bus_attach_private_data(handle
, (void *)data
);
395 if (ACPI_FAILURE(status
)) {
400 status
= acpi_install_address_space_handler(handle
,
401 ACPI_ADR_SPACE_GSBUS
,
402 &acpi_i2c_space_handler
,
405 if (ACPI_FAILURE(status
)) {
406 dev_err(&adapter
->dev
, "Error installing i2c space handler\n");
407 acpi_bus_detach_private_data(handle
);
412 acpi_walk_dep_device_list(handle
);
416 static void acpi_i2c_remove_space_handler(struct i2c_adapter
*adapter
)
419 struct acpi_i2c_handler_data
*data
;
422 if (!adapter
->dev
.parent
)
425 handle
= ACPI_HANDLE(adapter
->dev
.parent
);
430 acpi_remove_address_space_handler(handle
,
431 ACPI_ADR_SPACE_GSBUS
,
432 &acpi_i2c_space_handler
);
434 status
= acpi_bus_get_private_data(handle
, (void **)&data
);
435 if (ACPI_SUCCESS(status
))
438 acpi_bus_detach_private_data(handle
);
440 #else /* CONFIG_ACPI_I2C_OPREGION */
441 static inline void acpi_i2c_remove_space_handler(struct i2c_adapter
*adapter
)
444 static inline int acpi_i2c_install_space_handler(struct i2c_adapter
*adapter
)
446 #endif /* CONFIG_ACPI_I2C_OPREGION */
448 /* ------------------------------------------------------------------------- */
450 static const struct i2c_device_id
*i2c_match_id(const struct i2c_device_id
*id
,
451 const struct i2c_client
*client
)
453 while (id
->name
[0]) {
454 if (strcmp(client
->name
, id
->name
) == 0)
461 static int i2c_device_match(struct device
*dev
, struct device_driver
*drv
)
463 struct i2c_client
*client
= i2c_verify_client(dev
);
464 struct i2c_driver
*driver
;
469 /* Attempt an OF style match */
470 if (of_driver_match_device(dev
, drv
))
473 /* Then ACPI style match */
474 if (acpi_driver_match_device(dev
, drv
))
477 driver
= to_i2c_driver(drv
);
478 /* match on an id table if there is one */
479 if (driver
->id_table
)
480 return i2c_match_id(driver
->id_table
, client
) != NULL
;
486 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
487 static int i2c_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
489 struct i2c_client
*client
= to_i2c_client(dev
);
492 rc
= acpi_device_uevent_modalias(dev
, env
);
496 if (add_uevent_var(env
, "MODALIAS=%s%s",
497 I2C_MODULE_PREFIX
, client
->name
))
499 dev_dbg(dev
, "uevent\n");
503 /* i2c bus recovery routines */
504 static int get_scl_gpio_value(struct i2c_adapter
*adap
)
506 return gpio_get_value(adap
->bus_recovery_info
->scl_gpio
);
509 static void set_scl_gpio_value(struct i2c_adapter
*adap
, int val
)
511 gpio_set_value(adap
->bus_recovery_info
->scl_gpio
, val
);
514 static int get_sda_gpio_value(struct i2c_adapter
*adap
)
516 return gpio_get_value(adap
->bus_recovery_info
->sda_gpio
);
519 static int i2c_get_gpios_for_recovery(struct i2c_adapter
*adap
)
521 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
522 struct device
*dev
= &adap
->dev
;
525 ret
= gpio_request_one(bri
->scl_gpio
, GPIOF_OPEN_DRAIN
|
526 GPIOF_OUT_INIT_HIGH
, "i2c-scl");
528 dev_warn(dev
, "Can't get SCL gpio: %d\n", bri
->scl_gpio
);
533 if (gpio_request_one(bri
->sda_gpio
, GPIOF_IN
, "i2c-sda")) {
534 /* work without SDA polling */
535 dev_warn(dev
, "Can't get SDA gpio: %d. Not using SDA polling\n",
544 static void i2c_put_gpios_for_recovery(struct i2c_adapter
*adap
)
546 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
549 gpio_free(bri
->sda_gpio
);
551 gpio_free(bri
->scl_gpio
);
555 * We are generating clock pulses. ndelay() determines durating of clk pulses.
556 * We will generate clock with rate 100 KHz and so duration of both clock levels
557 * is: delay in ns = (10^6 / 100) / 2
559 #define RECOVERY_NDELAY 5000
560 #define RECOVERY_CLK_CNT 9
562 static int i2c_generic_recovery(struct i2c_adapter
*adap
)
564 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
565 int i
= 0, val
= 1, ret
= 0;
567 if (bri
->prepare_recovery
)
568 bri
->prepare_recovery(adap
);
570 bri
->set_scl(adap
, val
);
571 ndelay(RECOVERY_NDELAY
);
574 * By this time SCL is high, as we need to give 9 falling-rising edges
576 while (i
++ < RECOVERY_CLK_CNT
* 2) {
578 /* Break if SDA is high */
579 if (bri
->get_sda
&& bri
->get_sda(adap
))
581 /* SCL shouldn't be low here */
582 if (!bri
->get_scl(adap
)) {
584 "SCL is stuck low, exit recovery\n");
591 bri
->set_scl(adap
, val
);
592 ndelay(RECOVERY_NDELAY
);
595 if (bri
->unprepare_recovery
)
596 bri
->unprepare_recovery(adap
);
601 int i2c_generic_scl_recovery(struct i2c_adapter
*adap
)
603 return i2c_generic_recovery(adap
);
605 EXPORT_SYMBOL_GPL(i2c_generic_scl_recovery
);
607 int i2c_generic_gpio_recovery(struct i2c_adapter
*adap
)
611 ret
= i2c_get_gpios_for_recovery(adap
);
615 ret
= i2c_generic_recovery(adap
);
616 i2c_put_gpios_for_recovery(adap
);
620 EXPORT_SYMBOL_GPL(i2c_generic_gpio_recovery
);
622 int i2c_recover_bus(struct i2c_adapter
*adap
)
624 if (!adap
->bus_recovery_info
)
627 dev_dbg(&adap
->dev
, "Trying i2c bus recovery\n");
628 return adap
->bus_recovery_info
->recover_bus(adap
);
630 EXPORT_SYMBOL_GPL(i2c_recover_bus
);
632 static int i2c_device_probe(struct device
*dev
)
634 struct i2c_client
*client
= i2c_verify_client(dev
);
635 struct i2c_driver
*driver
;
645 irq
= of_irq_get(dev
->of_node
, 0);
646 else if (ACPI_COMPANION(dev
))
647 irq
= acpi_dev_gpio_irq_get(ACPI_COMPANION(dev
), 0);
649 if (irq
== -EPROBE_DEFER
)
657 driver
= to_i2c_driver(dev
->driver
);
658 if (!driver
->probe
|| !driver
->id_table
)
661 if (!device_can_wakeup(&client
->dev
))
662 device_init_wakeup(&client
->dev
,
663 client
->flags
& I2C_CLIENT_WAKE
);
664 dev_dbg(dev
, "probe\n");
666 status
= of_clk_set_defaults(dev
->of_node
, false);
670 status
= dev_pm_domain_attach(&client
->dev
, true);
671 if (status
!= -EPROBE_DEFER
) {
672 status
= driver
->probe(client
, i2c_match_id(driver
->id_table
,
675 dev_pm_domain_detach(&client
->dev
, true);
681 static int i2c_device_remove(struct device
*dev
)
683 struct i2c_client
*client
= i2c_verify_client(dev
);
684 struct i2c_driver
*driver
;
687 if (!client
|| !dev
->driver
)
690 driver
= to_i2c_driver(dev
->driver
);
691 if (driver
->remove
) {
692 dev_dbg(dev
, "remove\n");
693 status
= driver
->remove(client
);
696 dev_pm_domain_detach(&client
->dev
, true);
700 static void i2c_device_shutdown(struct device
*dev
)
702 struct i2c_client
*client
= i2c_verify_client(dev
);
703 struct i2c_driver
*driver
;
705 if (!client
|| !dev
->driver
)
707 driver
= to_i2c_driver(dev
->driver
);
708 if (driver
->shutdown
)
709 driver
->shutdown(client
);
712 static void i2c_client_dev_release(struct device
*dev
)
714 kfree(to_i2c_client(dev
));
718 show_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
720 return sprintf(buf
, "%s\n", dev
->type
== &i2c_client_type
?
721 to_i2c_client(dev
)->name
: to_i2c_adapter(dev
)->name
);
723 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
726 show_modalias(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
728 struct i2c_client
*client
= to_i2c_client(dev
);
731 len
= acpi_device_modalias(dev
, buf
, PAGE_SIZE
-1);
735 return sprintf(buf
, "%s%s\n", I2C_MODULE_PREFIX
, client
->name
);
737 static DEVICE_ATTR(modalias
, S_IRUGO
, show_modalias
, NULL
);
739 static struct attribute
*i2c_dev_attrs
[] = {
741 /* modalias helps coldplug: modprobe $(cat .../modalias) */
742 &dev_attr_modalias
.attr
,
745 ATTRIBUTE_GROUPS(i2c_dev
);
747 struct bus_type i2c_bus_type
= {
749 .match
= i2c_device_match
,
750 .probe
= i2c_device_probe
,
751 .remove
= i2c_device_remove
,
752 .shutdown
= i2c_device_shutdown
,
754 EXPORT_SYMBOL_GPL(i2c_bus_type
);
756 static struct device_type i2c_client_type
= {
757 .groups
= i2c_dev_groups
,
758 .uevent
= i2c_device_uevent
,
759 .release
= i2c_client_dev_release
,
764 * i2c_verify_client - return parameter as i2c_client, or NULL
765 * @dev: device, probably from some driver model iterator
767 * When traversing the driver model tree, perhaps using driver model
768 * iterators like @device_for_each_child(), you can't assume very much
769 * about the nodes you find. Use this function to avoid oopses caused
770 * by wrongly treating some non-I2C device as an i2c_client.
772 struct i2c_client
*i2c_verify_client(struct device
*dev
)
774 return (dev
->type
== &i2c_client_type
)
778 EXPORT_SYMBOL(i2c_verify_client
);
781 /* This is a permissive address validity check, I2C address map constraints
782 * are purposely not enforced, except for the general call address. */
783 static int i2c_check_client_addr_validity(const struct i2c_client
*client
)
785 if (client
->flags
& I2C_CLIENT_TEN
) {
786 /* 10-bit address, all values are valid */
787 if (client
->addr
> 0x3ff)
790 /* 7-bit address, reject the general call address */
791 if (client
->addr
== 0x00 || client
->addr
> 0x7f)
797 /* And this is a strict address validity check, used when probing. If a
798 * device uses a reserved address, then it shouldn't be probed. 7-bit
799 * addressing is assumed, 10-bit address devices are rare and should be
800 * explicitly enumerated. */
801 static int i2c_check_addr_validity(unsigned short addr
)
804 * Reserved addresses per I2C specification:
805 * 0x00 General call address / START byte
807 * 0x02 Reserved for different bus format
808 * 0x03 Reserved for future purposes
809 * 0x04-0x07 Hs-mode master code
810 * 0x78-0x7b 10-bit slave addressing
811 * 0x7c-0x7f Reserved for future purposes
813 if (addr
< 0x08 || addr
> 0x77)
818 static int __i2c_check_addr_busy(struct device
*dev
, void *addrp
)
820 struct i2c_client
*client
= i2c_verify_client(dev
);
821 int addr
= *(int *)addrp
;
823 if (client
&& client
->addr
== addr
)
828 /* walk up mux tree */
829 static int i2c_check_mux_parents(struct i2c_adapter
*adapter
, int addr
)
831 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
834 result
= device_for_each_child(&adapter
->dev
, &addr
,
835 __i2c_check_addr_busy
);
837 if (!result
&& parent
)
838 result
= i2c_check_mux_parents(parent
, addr
);
843 /* recurse down mux tree */
844 static int i2c_check_mux_children(struct device
*dev
, void *addrp
)
848 if (dev
->type
== &i2c_adapter_type
)
849 result
= device_for_each_child(dev
, addrp
,
850 i2c_check_mux_children
);
852 result
= __i2c_check_addr_busy(dev
, addrp
);
857 static int i2c_check_addr_busy(struct i2c_adapter
*adapter
, int addr
)
859 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
863 result
= i2c_check_mux_parents(parent
, addr
);
866 result
= device_for_each_child(&adapter
->dev
, &addr
,
867 i2c_check_mux_children
);
873 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
874 * @adapter: Target I2C bus segment
876 void i2c_lock_adapter(struct i2c_adapter
*adapter
)
878 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
881 i2c_lock_adapter(parent
);
883 rt_mutex_lock(&adapter
->bus_lock
);
885 EXPORT_SYMBOL_GPL(i2c_lock_adapter
);
888 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
889 * @adapter: Target I2C bus segment
891 static int i2c_trylock_adapter(struct i2c_adapter
*adapter
)
893 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
896 return i2c_trylock_adapter(parent
);
898 return rt_mutex_trylock(&adapter
->bus_lock
);
902 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
903 * @adapter: Target I2C bus segment
905 void i2c_unlock_adapter(struct i2c_adapter
*adapter
)
907 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
910 i2c_unlock_adapter(parent
);
912 rt_mutex_unlock(&adapter
->bus_lock
);
914 EXPORT_SYMBOL_GPL(i2c_unlock_adapter
);
916 static void i2c_dev_set_name(struct i2c_adapter
*adap
,
917 struct i2c_client
*client
)
919 struct acpi_device
*adev
= ACPI_COMPANION(&client
->dev
);
922 dev_set_name(&client
->dev
, "i2c-%s", acpi_dev_name(adev
));
926 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
927 dev_set_name(&client
->dev
, "%d-%04x", i2c_adapter_id(adap
),
928 client
->addr
| ((client
->flags
& I2C_CLIENT_TEN
)
933 * i2c_new_device - instantiate an i2c device
934 * @adap: the adapter managing the device
935 * @info: describes one I2C device; bus_num is ignored
938 * Create an i2c device. Binding is handled through driver model
939 * probe()/remove() methods. A driver may be bound to this device when we
940 * return from this function, or any later moment (e.g. maybe hotplugging will
941 * load the driver module). This call is not appropriate for use by mainboard
942 * initialization logic, which usually runs during an arch_initcall() long
943 * before any i2c_adapter could exist.
945 * This returns the new i2c client, which may be saved for later use with
946 * i2c_unregister_device(); or NULL to indicate an error.
949 i2c_new_device(struct i2c_adapter
*adap
, struct i2c_board_info
const *info
)
951 struct i2c_client
*client
;
954 client
= kzalloc(sizeof *client
, GFP_KERNEL
);
958 client
->adapter
= adap
;
960 client
->dev
.platform_data
= info
->platform_data
;
963 client
->dev
.archdata
= *info
->archdata
;
965 client
->flags
= info
->flags
;
966 client
->addr
= info
->addr
;
967 client
->irq
= info
->irq
;
969 strlcpy(client
->name
, info
->type
, sizeof(client
->name
));
971 /* Check for address validity */
972 status
= i2c_check_client_addr_validity(client
);
974 dev_err(&adap
->dev
, "Invalid %d-bit I2C address 0x%02hx\n",
975 client
->flags
& I2C_CLIENT_TEN
? 10 : 7, client
->addr
);
979 /* Check for address business */
980 status
= i2c_check_addr_busy(adap
, client
->addr
);
984 client
->dev
.parent
= &client
->adapter
->dev
;
985 client
->dev
.bus
= &i2c_bus_type
;
986 client
->dev
.type
= &i2c_client_type
;
987 client
->dev
.of_node
= info
->of_node
;
988 client
->dev
.fwnode
= info
->fwnode
;
990 i2c_dev_set_name(adap
, client
);
991 status
= device_register(&client
->dev
);
995 dev_dbg(&adap
->dev
, "client [%s] registered with bus id %s\n",
996 client
->name
, dev_name(&client
->dev
));
1001 dev_err(&adap
->dev
, "Failed to register i2c client %s at 0x%02x "
1002 "(%d)\n", client
->name
, client
->addr
, status
);
1007 EXPORT_SYMBOL_GPL(i2c_new_device
);
1011 * i2c_unregister_device - reverse effect of i2c_new_device()
1012 * @client: value returned from i2c_new_device()
1013 * Context: can sleep
1015 void i2c_unregister_device(struct i2c_client
*client
)
1017 if (client
->dev
.of_node
)
1018 of_node_clear_flag(client
->dev
.of_node
, OF_POPULATED
);
1019 device_unregister(&client
->dev
);
1021 EXPORT_SYMBOL_GPL(i2c_unregister_device
);
1024 static const struct i2c_device_id dummy_id
[] = {
1029 static int dummy_probe(struct i2c_client
*client
,
1030 const struct i2c_device_id
*id
)
1035 static int dummy_remove(struct i2c_client
*client
)
1040 static struct i2c_driver dummy_driver
= {
1041 .driver
.name
= "dummy",
1042 .probe
= dummy_probe
,
1043 .remove
= dummy_remove
,
1044 .id_table
= dummy_id
,
1048 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1049 * @adapter: the adapter managing the device
1050 * @address: seven bit address to be used
1051 * Context: can sleep
1053 * This returns an I2C client bound to the "dummy" driver, intended for use
1054 * with devices that consume multiple addresses. Examples of such chips
1055 * include various EEPROMS (like 24c04 and 24c08 models).
1057 * These dummy devices have two main uses. First, most I2C and SMBus calls
1058 * except i2c_transfer() need a client handle; the dummy will be that handle.
1059 * And second, this prevents the specified address from being bound to a
1062 * This returns the new i2c client, which should be saved for later use with
1063 * i2c_unregister_device(); or NULL to indicate an error.
1065 struct i2c_client
*i2c_new_dummy(struct i2c_adapter
*adapter
, u16 address
)
1067 struct i2c_board_info info
= {
1068 I2C_BOARD_INFO("dummy", address
),
1071 return i2c_new_device(adapter
, &info
);
1073 EXPORT_SYMBOL_GPL(i2c_new_dummy
);
1075 /* ------------------------------------------------------------------------- */
1077 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
1079 static void i2c_adapter_dev_release(struct device
*dev
)
1081 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
1082 complete(&adap
->dev_released
);
1086 * This function is only needed for mutex_lock_nested, so it is never
1087 * called unless locking correctness checking is enabled. Thus we
1088 * make it inline to avoid a compiler warning. That's what gcc ends up
1091 static inline unsigned int i2c_adapter_depth(struct i2c_adapter
*adapter
)
1093 unsigned int depth
= 0;
1095 while ((adapter
= i2c_parent_is_i2c_adapter(adapter
)))
1102 * Let users instantiate I2C devices through sysfs. This can be used when
1103 * platform initialization code doesn't contain the proper data for
1104 * whatever reason. Also useful for drivers that do device detection and
1105 * detection fails, either because the device uses an unexpected address,
1106 * or this is a compatible device with different ID register values.
1108 * Parameter checking may look overzealous, but we really don't want
1109 * the user to provide incorrect parameters.
1112 i2c_sysfs_new_device(struct device
*dev
, struct device_attribute
*attr
,
1113 const char *buf
, size_t count
)
1115 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
1116 struct i2c_board_info info
;
1117 struct i2c_client
*client
;
1121 memset(&info
, 0, sizeof(struct i2c_board_info
));
1123 blank
= strchr(buf
, ' ');
1125 dev_err(dev
, "%s: Missing parameters\n", "new_device");
1128 if (blank
- buf
> I2C_NAME_SIZE
- 1) {
1129 dev_err(dev
, "%s: Invalid device name\n", "new_device");
1132 memcpy(info
.type
, buf
, blank
- buf
);
1134 /* Parse remaining parameters, reject extra parameters */
1135 res
= sscanf(++blank
, "%hi%c", &info
.addr
, &end
);
1137 dev_err(dev
, "%s: Can't parse I2C address\n", "new_device");
1140 if (res
> 1 && end
!= '\n') {
1141 dev_err(dev
, "%s: Extra parameters\n", "new_device");
1145 client
= i2c_new_device(adap
, &info
);
1149 /* Keep track of the added device */
1150 mutex_lock(&adap
->userspace_clients_lock
);
1151 list_add_tail(&client
->detected
, &adap
->userspace_clients
);
1152 mutex_unlock(&adap
->userspace_clients_lock
);
1153 dev_info(dev
, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1154 info
.type
, info
.addr
);
1158 static DEVICE_ATTR(new_device
, S_IWUSR
, NULL
, i2c_sysfs_new_device
);
1161 * And of course let the users delete the devices they instantiated, if
1162 * they got it wrong. This interface can only be used to delete devices
1163 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1164 * don't delete devices to which some kernel code still has references.
1166 * Parameter checking may look overzealous, but we really don't want
1167 * the user to delete the wrong device.
1170 i2c_sysfs_delete_device(struct device
*dev
, struct device_attribute
*attr
,
1171 const char *buf
, size_t count
)
1173 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
1174 struct i2c_client
*client
, *next
;
1175 unsigned short addr
;
1179 /* Parse parameters, reject extra parameters */
1180 res
= sscanf(buf
, "%hi%c", &addr
, &end
);
1182 dev_err(dev
, "%s: Can't parse I2C address\n", "delete_device");
1185 if (res
> 1 && end
!= '\n') {
1186 dev_err(dev
, "%s: Extra parameters\n", "delete_device");
1190 /* Make sure the device was added through sysfs */
1192 mutex_lock_nested(&adap
->userspace_clients_lock
,
1193 i2c_adapter_depth(adap
));
1194 list_for_each_entry_safe(client
, next
, &adap
->userspace_clients
,
1196 if (client
->addr
== addr
) {
1197 dev_info(dev
, "%s: Deleting device %s at 0x%02hx\n",
1198 "delete_device", client
->name
, client
->addr
);
1200 list_del(&client
->detected
);
1201 i2c_unregister_device(client
);
1206 mutex_unlock(&adap
->userspace_clients_lock
);
1209 dev_err(dev
, "%s: Can't find device in list\n",
1213 static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device
, S_IWUSR
, NULL
,
1214 i2c_sysfs_delete_device
);
1216 static struct attribute
*i2c_adapter_attrs
[] = {
1217 &dev_attr_name
.attr
,
1218 &dev_attr_new_device
.attr
,
1219 &dev_attr_delete_device
.attr
,
1222 ATTRIBUTE_GROUPS(i2c_adapter
);
1224 struct device_type i2c_adapter_type
= {
1225 .groups
= i2c_adapter_groups
,
1226 .release
= i2c_adapter_dev_release
,
1228 EXPORT_SYMBOL_GPL(i2c_adapter_type
);
1231 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1232 * @dev: device, probably from some driver model iterator
1234 * When traversing the driver model tree, perhaps using driver model
1235 * iterators like @device_for_each_child(), you can't assume very much
1236 * about the nodes you find. Use this function to avoid oopses caused
1237 * by wrongly treating some non-I2C device as an i2c_adapter.
1239 struct i2c_adapter
*i2c_verify_adapter(struct device
*dev
)
1241 return (dev
->type
== &i2c_adapter_type
)
1242 ? to_i2c_adapter(dev
)
1245 EXPORT_SYMBOL(i2c_verify_adapter
);
1247 #ifdef CONFIG_I2C_COMPAT
1248 static struct class_compat
*i2c_adapter_compat_class
;
1251 static void i2c_scan_static_board_info(struct i2c_adapter
*adapter
)
1253 struct i2c_devinfo
*devinfo
;
1255 down_read(&__i2c_board_lock
);
1256 list_for_each_entry(devinfo
, &__i2c_board_list
, list
) {
1257 if (devinfo
->busnum
== adapter
->nr
1258 && !i2c_new_device(adapter
,
1259 &devinfo
->board_info
))
1260 dev_err(&adapter
->dev
,
1261 "Can't create device at 0x%02x\n",
1262 devinfo
->board_info
.addr
);
1264 up_read(&__i2c_board_lock
);
1267 /* OF support code */
1269 #if IS_ENABLED(CONFIG_OF)
1270 static struct i2c_client
*of_i2c_register_device(struct i2c_adapter
*adap
,
1271 struct device_node
*node
)
1273 struct i2c_client
*result
;
1274 struct i2c_board_info info
= {};
1275 struct dev_archdata dev_ad
= {};
1279 dev_dbg(&adap
->dev
, "of_i2c: register %s\n", node
->full_name
);
1281 if (of_modalias_node(node
, info
.type
, sizeof(info
.type
)) < 0) {
1282 dev_err(&adap
->dev
, "of_i2c: modalias failure on %s\n",
1284 return ERR_PTR(-EINVAL
);
1287 addr
= of_get_property(node
, "reg", &len
);
1288 if (!addr
|| (len
< sizeof(*addr
))) {
1289 dev_err(&adap
->dev
, "of_i2c: invalid reg on %s\n",
1291 return ERR_PTR(-EINVAL
);
1294 info
.addr
= be32_to_cpup(addr
);
1295 if (info
.addr
> (1 << 10) - 1) {
1296 dev_err(&adap
->dev
, "of_i2c: invalid addr=%x on %s\n",
1297 info
.addr
, node
->full_name
);
1298 return ERR_PTR(-EINVAL
);
1301 info
.of_node
= of_node_get(node
);
1302 info
.archdata
= &dev_ad
;
1304 if (of_get_property(node
, "wakeup-source", NULL
))
1305 info
.flags
|= I2C_CLIENT_WAKE
;
1307 result
= i2c_new_device(adap
, &info
);
1308 if (result
== NULL
) {
1309 dev_err(&adap
->dev
, "of_i2c: Failure registering %s\n",
1312 return ERR_PTR(-EINVAL
);
1317 static void of_i2c_register_devices(struct i2c_adapter
*adap
)
1319 struct device_node
*node
;
1321 /* Only register child devices if the adapter has a node pointer set */
1322 if (!adap
->dev
.of_node
)
1325 dev_dbg(&adap
->dev
, "of_i2c: walking child nodes\n");
1327 for_each_available_child_of_node(adap
->dev
.of_node
, node
) {
1328 if (of_node_test_and_set_flag(node
, OF_POPULATED
))
1330 of_i2c_register_device(adap
, node
);
1334 static int of_dev_node_match(struct device
*dev
, void *data
)
1336 return dev
->of_node
== data
;
1339 /* must call put_device() when done with returned i2c_client device */
1340 struct i2c_client
*of_find_i2c_device_by_node(struct device_node
*node
)
1343 struct i2c_client
*client
;
1345 dev
= bus_find_device(&i2c_bus_type
, NULL
, node
, of_dev_node_match
);
1349 client
= i2c_verify_client(dev
);
1355 EXPORT_SYMBOL(of_find_i2c_device_by_node
);
1357 /* must call put_device() when done with returned i2c_adapter device */
1358 struct i2c_adapter
*of_find_i2c_adapter_by_node(struct device_node
*node
)
1361 struct i2c_adapter
*adapter
;
1363 dev
= bus_find_device(&i2c_bus_type
, NULL
, node
, of_dev_node_match
);
1367 adapter
= i2c_verify_adapter(dev
);
1373 EXPORT_SYMBOL(of_find_i2c_adapter_by_node
);
1375 static void of_i2c_register_devices(struct i2c_adapter
*adap
) { }
1376 #endif /* CONFIG_OF */
1378 static int i2c_do_add_adapter(struct i2c_driver
*driver
,
1379 struct i2c_adapter
*adap
)
1381 /* Detect supported devices on that bus, and instantiate them */
1382 i2c_detect(adap
, driver
);
1384 /* Let legacy drivers scan this bus for matching devices */
1385 if (driver
->attach_adapter
) {
1386 dev_warn(&adap
->dev
, "%s: attach_adapter method is deprecated\n",
1387 driver
->driver
.name
);
1388 dev_warn(&adap
->dev
, "Please use another way to instantiate "
1389 "your i2c_client\n");
1390 /* We ignore the return code; if it fails, too bad */
1391 driver
->attach_adapter(adap
);
1396 static int __process_new_adapter(struct device_driver
*d
, void *data
)
1398 return i2c_do_add_adapter(to_i2c_driver(d
), data
);
1401 static int i2c_register_adapter(struct i2c_adapter
*adap
)
1405 /* Can't register until after driver model init */
1406 if (unlikely(WARN_ON(!i2c_bus_type
.p
))) {
1412 if (unlikely(adap
->name
[0] == '\0')) {
1413 pr_err("i2c-core: Attempt to register an adapter with "
1417 if (unlikely(!adap
->algo
)) {
1418 pr_err("i2c-core: Attempt to register adapter '%s' with "
1419 "no algo!\n", adap
->name
);
1423 rt_mutex_init(&adap
->bus_lock
);
1424 mutex_init(&adap
->userspace_clients_lock
);
1425 INIT_LIST_HEAD(&adap
->userspace_clients
);
1427 /* Set default timeout to 1 second if not already set */
1428 if (adap
->timeout
== 0)
1431 dev_set_name(&adap
->dev
, "i2c-%d", adap
->nr
);
1432 adap
->dev
.bus
= &i2c_bus_type
;
1433 adap
->dev
.type
= &i2c_adapter_type
;
1434 res
= device_register(&adap
->dev
);
1438 dev_dbg(&adap
->dev
, "adapter [%s] registered\n", adap
->name
);
1440 pm_runtime_no_callbacks(&adap
->dev
);
1442 #ifdef CONFIG_I2C_COMPAT
1443 res
= class_compat_create_link(i2c_adapter_compat_class
, &adap
->dev
,
1446 dev_warn(&adap
->dev
,
1447 "Failed to create compatibility class link\n");
1450 /* bus recovery specific initialization */
1451 if (adap
->bus_recovery_info
) {
1452 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
1454 if (!bri
->recover_bus
) {
1455 dev_err(&adap
->dev
, "No recover_bus() found, not using recovery\n");
1456 adap
->bus_recovery_info
= NULL
;
1460 /* Generic GPIO recovery */
1461 if (bri
->recover_bus
== i2c_generic_gpio_recovery
) {
1462 if (!gpio_is_valid(bri
->scl_gpio
)) {
1463 dev_err(&adap
->dev
, "Invalid SCL gpio, not using recovery\n");
1464 adap
->bus_recovery_info
= NULL
;
1468 if (gpio_is_valid(bri
->sda_gpio
))
1469 bri
->get_sda
= get_sda_gpio_value
;
1471 bri
->get_sda
= NULL
;
1473 bri
->get_scl
= get_scl_gpio_value
;
1474 bri
->set_scl
= set_scl_gpio_value
;
1475 } else if (!bri
->set_scl
|| !bri
->get_scl
) {
1476 /* Generic SCL recovery */
1477 dev_err(&adap
->dev
, "No {get|set}_gpio() found, not using recovery\n");
1478 adap
->bus_recovery_info
= NULL
;
1483 /* create pre-declared device nodes */
1484 of_i2c_register_devices(adap
);
1485 acpi_i2c_register_devices(adap
);
1486 acpi_i2c_install_space_handler(adap
);
1488 if (adap
->nr
< __i2c_first_dynamic_bus_num
)
1489 i2c_scan_static_board_info(adap
);
1491 /* Notify drivers */
1492 mutex_lock(&core_lock
);
1493 bus_for_each_drv(&i2c_bus_type
, NULL
, adap
, __process_new_adapter
);
1494 mutex_unlock(&core_lock
);
1499 mutex_lock(&core_lock
);
1500 idr_remove(&i2c_adapter_idr
, adap
->nr
);
1501 mutex_unlock(&core_lock
);
1506 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1507 * @adap: the adapter to register (with adap->nr initialized)
1508 * Context: can sleep
1510 * See i2c_add_numbered_adapter() for details.
1512 static int __i2c_add_numbered_adapter(struct i2c_adapter
*adap
)
1516 mutex_lock(&core_lock
);
1517 id
= idr_alloc(&i2c_adapter_idr
, adap
, adap
->nr
, adap
->nr
+ 1,
1519 mutex_unlock(&core_lock
);
1521 return id
== -ENOSPC
? -EBUSY
: id
;
1523 return i2c_register_adapter(adap
);
1527 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1528 * @adapter: the adapter to add
1529 * Context: can sleep
1531 * This routine is used to declare an I2C adapter when its bus number
1532 * doesn't matter or when its bus number is specified by an dt alias.
1533 * Examples of bases when the bus number doesn't matter: I2C adapters
1534 * dynamically added by USB links or PCI plugin cards.
1536 * When this returns zero, a new bus number was allocated and stored
1537 * in adap->nr, and the specified adapter became available for clients.
1538 * Otherwise, a negative errno value is returned.
1540 int i2c_add_adapter(struct i2c_adapter
*adapter
)
1542 struct device
*dev
= &adapter
->dev
;
1546 id
= of_alias_get_id(dev
->of_node
, "i2c");
1549 return __i2c_add_numbered_adapter(adapter
);
1553 mutex_lock(&core_lock
);
1554 id
= idr_alloc(&i2c_adapter_idr
, adapter
,
1555 __i2c_first_dynamic_bus_num
, 0, GFP_KERNEL
);
1556 mutex_unlock(&core_lock
);
1562 return i2c_register_adapter(adapter
);
1564 EXPORT_SYMBOL(i2c_add_adapter
);
1567 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1568 * @adap: the adapter to register (with adap->nr initialized)
1569 * Context: can sleep
1571 * This routine is used to declare an I2C adapter when its bus number
1572 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1573 * or otherwise built in to the system's mainboard, and where i2c_board_info
1574 * is used to properly configure I2C devices.
1576 * If the requested bus number is set to -1, then this function will behave
1577 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1579 * If no devices have pre-been declared for this bus, then be sure to
1580 * register the adapter before any dynamically allocated ones. Otherwise
1581 * the required bus ID may not be available.
1583 * When this returns zero, the specified adapter became available for
1584 * clients using the bus number provided in adap->nr. Also, the table
1585 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1586 * and the appropriate driver model device nodes are created. Otherwise, a
1587 * negative errno value is returned.
1589 int i2c_add_numbered_adapter(struct i2c_adapter
*adap
)
1591 if (adap
->nr
== -1) /* -1 means dynamically assign bus id */
1592 return i2c_add_adapter(adap
);
1594 return __i2c_add_numbered_adapter(adap
);
1596 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter
);
1598 static void i2c_do_del_adapter(struct i2c_driver
*driver
,
1599 struct i2c_adapter
*adapter
)
1601 struct i2c_client
*client
, *_n
;
1603 /* Remove the devices we created ourselves as the result of hardware
1604 * probing (using a driver's detect method) */
1605 list_for_each_entry_safe(client
, _n
, &driver
->clients
, detected
) {
1606 if (client
->adapter
== adapter
) {
1607 dev_dbg(&adapter
->dev
, "Removing %s at 0x%x\n",
1608 client
->name
, client
->addr
);
1609 list_del(&client
->detected
);
1610 i2c_unregister_device(client
);
1615 static int __unregister_client(struct device
*dev
, void *dummy
)
1617 struct i2c_client
*client
= i2c_verify_client(dev
);
1618 if (client
&& strcmp(client
->name
, "dummy"))
1619 i2c_unregister_device(client
);
1623 static int __unregister_dummy(struct device
*dev
, void *dummy
)
1625 struct i2c_client
*client
= i2c_verify_client(dev
);
1627 i2c_unregister_device(client
);
1631 static int __process_removed_adapter(struct device_driver
*d
, void *data
)
1633 i2c_do_del_adapter(to_i2c_driver(d
), data
);
1638 * i2c_del_adapter - unregister I2C adapter
1639 * @adap: the adapter being unregistered
1640 * Context: can sleep
1642 * This unregisters an I2C adapter which was previously registered
1643 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1645 void i2c_del_adapter(struct i2c_adapter
*adap
)
1647 struct i2c_adapter
*found
;
1648 struct i2c_client
*client
, *next
;
1650 /* First make sure that this adapter was ever added */
1651 mutex_lock(&core_lock
);
1652 found
= idr_find(&i2c_adapter_idr
, adap
->nr
);
1653 mutex_unlock(&core_lock
);
1654 if (found
!= adap
) {
1655 pr_debug("i2c-core: attempting to delete unregistered "
1656 "adapter [%s]\n", adap
->name
);
1660 acpi_i2c_remove_space_handler(adap
);
1661 /* Tell drivers about this removal */
1662 mutex_lock(&core_lock
);
1663 bus_for_each_drv(&i2c_bus_type
, NULL
, adap
,
1664 __process_removed_adapter
);
1665 mutex_unlock(&core_lock
);
1667 /* Remove devices instantiated from sysfs */
1668 mutex_lock_nested(&adap
->userspace_clients_lock
,
1669 i2c_adapter_depth(adap
));
1670 list_for_each_entry_safe(client
, next
, &adap
->userspace_clients
,
1672 dev_dbg(&adap
->dev
, "Removing %s at 0x%x\n", client
->name
,
1674 list_del(&client
->detected
);
1675 i2c_unregister_device(client
);
1677 mutex_unlock(&adap
->userspace_clients_lock
);
1679 /* Detach any active clients. This can't fail, thus we do not
1680 * check the returned value. This is a two-pass process, because
1681 * we can't remove the dummy devices during the first pass: they
1682 * could have been instantiated by real devices wishing to clean
1683 * them up properly, so we give them a chance to do that first. */
1684 device_for_each_child(&adap
->dev
, NULL
, __unregister_client
);
1685 device_for_each_child(&adap
->dev
, NULL
, __unregister_dummy
);
1687 #ifdef CONFIG_I2C_COMPAT
1688 class_compat_remove_link(i2c_adapter_compat_class
, &adap
->dev
,
1692 /* device name is gone after device_unregister */
1693 dev_dbg(&adap
->dev
, "adapter [%s] unregistered\n", adap
->name
);
1695 /* wait until all references to the device are gone
1697 * FIXME: This is old code and should ideally be replaced by an
1698 * alternative which results in decoupling the lifetime of the struct
1699 * device from the i2c_adapter, like spi or netdev do. Any solution
1700 * should be thoroughly tested with DEBUG_KOBJECT_RELEASE enabled!
1702 init_completion(&adap
->dev_released
);
1703 device_unregister(&adap
->dev
);
1704 wait_for_completion(&adap
->dev_released
);
1707 mutex_lock(&core_lock
);
1708 idr_remove(&i2c_adapter_idr
, adap
->nr
);
1709 mutex_unlock(&core_lock
);
1711 /* Clear the device structure in case this adapter is ever going to be
1713 memset(&adap
->dev
, 0, sizeof(adap
->dev
));
1715 EXPORT_SYMBOL(i2c_del_adapter
);
1717 /* ------------------------------------------------------------------------- */
1719 int i2c_for_each_dev(void *data
, int (*fn
)(struct device
*, void *))
1723 mutex_lock(&core_lock
);
1724 res
= bus_for_each_dev(&i2c_bus_type
, NULL
, data
, fn
);
1725 mutex_unlock(&core_lock
);
1729 EXPORT_SYMBOL_GPL(i2c_for_each_dev
);
1731 static int __process_new_driver(struct device
*dev
, void *data
)
1733 if (dev
->type
!= &i2c_adapter_type
)
1735 return i2c_do_add_adapter(data
, to_i2c_adapter(dev
));
1739 * An i2c_driver is used with one or more i2c_client (device) nodes to access
1740 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1743 int i2c_register_driver(struct module
*owner
, struct i2c_driver
*driver
)
1747 /* Can't register until after driver model init */
1748 if (unlikely(WARN_ON(!i2c_bus_type
.p
)))
1751 /* add the driver to the list of i2c drivers in the driver core */
1752 driver
->driver
.owner
= owner
;
1753 driver
->driver
.bus
= &i2c_bus_type
;
1755 /* When registration returns, the driver core
1756 * will have called probe() for all matching-but-unbound devices.
1758 res
= driver_register(&driver
->driver
);
1762 pr_debug("i2c-core: driver [%s] registered\n", driver
->driver
.name
);
1764 INIT_LIST_HEAD(&driver
->clients
);
1765 /* Walk the adapters that are already present */
1766 i2c_for_each_dev(driver
, __process_new_driver
);
1770 EXPORT_SYMBOL(i2c_register_driver
);
1772 static int __process_removed_driver(struct device
*dev
, void *data
)
1774 if (dev
->type
== &i2c_adapter_type
)
1775 i2c_do_del_adapter(data
, to_i2c_adapter(dev
));
1780 * i2c_del_driver - unregister I2C driver
1781 * @driver: the driver being unregistered
1782 * Context: can sleep
1784 void i2c_del_driver(struct i2c_driver
*driver
)
1786 i2c_for_each_dev(driver
, __process_removed_driver
);
1788 driver_unregister(&driver
->driver
);
1789 pr_debug("i2c-core: driver [%s] unregistered\n", driver
->driver
.name
);
1791 EXPORT_SYMBOL(i2c_del_driver
);
1793 /* ------------------------------------------------------------------------- */
1796 * i2c_use_client - increments the reference count of the i2c client structure
1797 * @client: the client being referenced
1799 * Each live reference to a client should be refcounted. The driver model does
1800 * that automatically as part of driver binding, so that most drivers don't
1801 * need to do this explicitly: they hold a reference until they're unbound
1804 * A pointer to the client with the incremented reference counter is returned.
1806 struct i2c_client
*i2c_use_client(struct i2c_client
*client
)
1808 if (client
&& get_device(&client
->dev
))
1812 EXPORT_SYMBOL(i2c_use_client
);
1815 * i2c_release_client - release a use of the i2c client structure
1816 * @client: the client being no longer referenced
1818 * Must be called when a user of a client is finished with it.
1820 void i2c_release_client(struct i2c_client
*client
)
1823 put_device(&client
->dev
);
1825 EXPORT_SYMBOL(i2c_release_client
);
1827 struct i2c_cmd_arg
{
1832 static int i2c_cmd(struct device
*dev
, void *_arg
)
1834 struct i2c_client
*client
= i2c_verify_client(dev
);
1835 struct i2c_cmd_arg
*arg
= _arg
;
1836 struct i2c_driver
*driver
;
1838 if (!client
|| !client
->dev
.driver
)
1841 driver
= to_i2c_driver(client
->dev
.driver
);
1842 if (driver
->command
)
1843 driver
->command(client
, arg
->cmd
, arg
->arg
);
1847 void i2c_clients_command(struct i2c_adapter
*adap
, unsigned int cmd
, void *arg
)
1849 struct i2c_cmd_arg cmd_arg
;
1853 device_for_each_child(&adap
->dev
, &cmd_arg
, i2c_cmd
);
1855 EXPORT_SYMBOL(i2c_clients_command
);
1857 #if IS_ENABLED(CONFIG_OF_DYNAMIC)
1858 static int of_i2c_notify(struct notifier_block
*nb
, unsigned long action
,
1861 struct of_reconfig_data
*rd
= arg
;
1862 struct i2c_adapter
*adap
;
1863 struct i2c_client
*client
;
1865 switch (of_reconfig_get_state_change(action
, rd
)) {
1866 case OF_RECONFIG_CHANGE_ADD
:
1867 adap
= of_find_i2c_adapter_by_node(rd
->dn
->parent
);
1869 return NOTIFY_OK
; /* not for us */
1871 if (of_node_test_and_set_flag(rd
->dn
, OF_POPULATED
)) {
1872 put_device(&adap
->dev
);
1876 client
= of_i2c_register_device(adap
, rd
->dn
);
1877 put_device(&adap
->dev
);
1879 if (IS_ERR(client
)) {
1880 pr_err("%s: failed to create for '%s'\n",
1881 __func__
, rd
->dn
->full_name
);
1882 return notifier_from_errno(PTR_ERR(client
));
1885 case OF_RECONFIG_CHANGE_REMOVE
:
1886 /* already depopulated? */
1887 if (!of_node_check_flag(rd
->dn
, OF_POPULATED
))
1890 /* find our device by node */
1891 client
= of_find_i2c_device_by_node(rd
->dn
);
1893 return NOTIFY_OK
; /* no? not meant for us */
1895 /* unregister takes one ref away */
1896 i2c_unregister_device(client
);
1898 /* and put the reference of the find */
1899 put_device(&client
->dev
);
1905 static struct notifier_block i2c_of_notifier
= {
1906 .notifier_call
= of_i2c_notify
,
1909 extern struct notifier_block i2c_of_notifier
;
1910 #endif /* CONFIG_OF_DYNAMIC */
1912 static int __init
i2c_init(void)
1916 retval
= of_alias_get_highest_id("i2c");
1918 down_write(&__i2c_board_lock
);
1919 if (retval
>= __i2c_first_dynamic_bus_num
)
1920 __i2c_first_dynamic_bus_num
= retval
+ 1;
1921 up_write(&__i2c_board_lock
);
1923 retval
= bus_register(&i2c_bus_type
);
1926 #ifdef CONFIG_I2C_COMPAT
1927 i2c_adapter_compat_class
= class_compat_register("i2c-adapter");
1928 if (!i2c_adapter_compat_class
) {
1933 retval
= i2c_add_driver(&dummy_driver
);
1937 if (IS_ENABLED(CONFIG_OF_DYNAMIC
))
1938 WARN_ON(of_reconfig_notifier_register(&i2c_of_notifier
));
1943 #ifdef CONFIG_I2C_COMPAT
1944 class_compat_unregister(i2c_adapter_compat_class
);
1947 bus_unregister(&i2c_bus_type
);
1951 static void __exit
i2c_exit(void)
1953 if (IS_ENABLED(CONFIG_OF_DYNAMIC
))
1954 WARN_ON(of_reconfig_notifier_unregister(&i2c_of_notifier
));
1955 i2c_del_driver(&dummy_driver
);
1956 #ifdef CONFIG_I2C_COMPAT
1957 class_compat_unregister(i2c_adapter_compat_class
);
1959 bus_unregister(&i2c_bus_type
);
1960 tracepoint_synchronize_unregister();
1963 /* We must initialize early, because some subsystems register i2c drivers
1964 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1966 postcore_initcall(i2c_init
);
1967 module_exit(i2c_exit
);
1969 /* ----------------------------------------------------
1970 * the functional interface to the i2c busses.
1971 * ----------------------------------------------------
1974 /* Check if val is exceeding the quirk IFF quirk is non 0 */
1975 #define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk)))
1977 static int i2c_quirk_error(struct i2c_adapter
*adap
, struct i2c_msg
*msg
, char *err_msg
)
1979 dev_err_ratelimited(&adap
->dev
, "adapter quirk: %s (addr 0x%04x, size %u, %s)\n",
1980 err_msg
, msg
->addr
, msg
->len
,
1981 msg
->flags
& I2C_M_RD
? "read" : "write");
1985 static int i2c_check_for_quirks(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
1987 const struct i2c_adapter_quirks
*q
= adap
->quirks
;
1988 int max_num
= q
->max_num_msgs
, i
;
1989 bool do_len_check
= true;
1991 if (q
->flags
& I2C_AQ_COMB
) {
1994 /* special checks for combined messages */
1996 if (q
->flags
& I2C_AQ_COMB_WRITE_FIRST
&& msgs
[0].flags
& I2C_M_RD
)
1997 return i2c_quirk_error(adap
, &msgs
[0], "1st comb msg must be write");
1999 if (q
->flags
& I2C_AQ_COMB_READ_SECOND
&& !(msgs
[1].flags
& I2C_M_RD
))
2000 return i2c_quirk_error(adap
, &msgs
[1], "2nd comb msg must be read");
2002 if (q
->flags
& I2C_AQ_COMB_SAME_ADDR
&& msgs
[0].addr
!= msgs
[1].addr
)
2003 return i2c_quirk_error(adap
, &msgs
[0], "comb msg only to same addr");
2005 if (i2c_quirk_exceeded(msgs
[0].len
, q
->max_comb_1st_msg_len
))
2006 return i2c_quirk_error(adap
, &msgs
[0], "msg too long");
2008 if (i2c_quirk_exceeded(msgs
[1].len
, q
->max_comb_2nd_msg_len
))
2009 return i2c_quirk_error(adap
, &msgs
[1], "msg too long");
2011 do_len_check
= false;
2015 if (i2c_quirk_exceeded(num
, max_num
))
2016 return i2c_quirk_error(adap
, &msgs
[0], "too many messages");
2018 for (i
= 0; i
< num
; i
++) {
2019 u16 len
= msgs
[i
].len
;
2021 if (msgs
[i
].flags
& I2C_M_RD
) {
2022 if (do_len_check
&& i2c_quirk_exceeded(len
, q
->max_read_len
))
2023 return i2c_quirk_error(adap
, &msgs
[i
], "msg too long");
2025 if (do_len_check
&& i2c_quirk_exceeded(len
, q
->max_write_len
))
2026 return i2c_quirk_error(adap
, &msgs
[i
], "msg too long");
2034 * __i2c_transfer - unlocked flavor of i2c_transfer
2035 * @adap: Handle to I2C bus
2036 * @msgs: One or more messages to execute before STOP is issued to
2037 * terminate the operation; each message begins with a START.
2038 * @num: Number of messages to be executed.
2040 * Returns negative errno, else the number of messages executed.
2042 * Adapter lock must be held when calling this function. No debug logging
2043 * takes place. adap->algo->master_xfer existence isn't checked.
2045 int __i2c_transfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
2047 unsigned long orig_jiffies
;
2050 if (adap
->quirks
&& i2c_check_for_quirks(adap
, msgs
, num
))
2053 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2054 * enabled. This is an efficient way of keeping the for-loop from
2055 * being executed when not needed.
2057 if (static_key_false(&i2c_trace_msg
)) {
2059 for (i
= 0; i
< num
; i
++)
2060 if (msgs
[i
].flags
& I2C_M_RD
)
2061 trace_i2c_read(adap
, &msgs
[i
], i
);
2063 trace_i2c_write(adap
, &msgs
[i
], i
);
2066 /* Retry automatically on arbitration loss */
2067 orig_jiffies
= jiffies
;
2068 for (ret
= 0, try = 0; try <= adap
->retries
; try++) {
2069 ret
= adap
->algo
->master_xfer(adap
, msgs
, num
);
2072 if (time_after(jiffies
, orig_jiffies
+ adap
->timeout
))
2076 if (static_key_false(&i2c_trace_msg
)) {
2078 for (i
= 0; i
< ret
; i
++)
2079 if (msgs
[i
].flags
& I2C_M_RD
)
2080 trace_i2c_reply(adap
, &msgs
[i
], i
);
2081 trace_i2c_result(adap
, i
, ret
);
2086 EXPORT_SYMBOL(__i2c_transfer
);
2089 * i2c_transfer - execute a single or combined I2C message
2090 * @adap: Handle to I2C bus
2091 * @msgs: One or more messages to execute before STOP is issued to
2092 * terminate the operation; each message begins with a START.
2093 * @num: Number of messages to be executed.
2095 * Returns negative errno, else the number of messages executed.
2097 * Note that there is no requirement that each message be sent to
2098 * the same slave address, although that is the most common model.
2100 int i2c_transfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
2104 /* REVISIT the fault reporting model here is weak:
2106 * - When we get an error after receiving N bytes from a slave,
2107 * there is no way to report "N".
2109 * - When we get a NAK after transmitting N bytes to a slave,
2110 * there is no way to report "N" ... or to let the master
2111 * continue executing the rest of this combined message, if
2112 * that's the appropriate response.
2114 * - When for example "num" is two and we successfully complete
2115 * the first message but get an error part way through the
2116 * second, it's unclear whether that should be reported as
2117 * one (discarding status on the second message) or errno
2118 * (discarding status on the first one).
2121 if (adap
->algo
->master_xfer
) {
2123 for (ret
= 0; ret
< num
; ret
++) {
2124 dev_dbg(&adap
->dev
, "master_xfer[%d] %c, addr=0x%02x, "
2125 "len=%d%s\n", ret
, (msgs
[ret
].flags
& I2C_M_RD
)
2126 ? 'R' : 'W', msgs
[ret
].addr
, msgs
[ret
].len
,
2127 (msgs
[ret
].flags
& I2C_M_RECV_LEN
) ? "+" : "");
2131 if (in_atomic() || irqs_disabled()) {
2132 ret
= i2c_trylock_adapter(adap
);
2134 /* I2C activity is ongoing. */
2137 i2c_lock_adapter(adap
);
2140 ret
= __i2c_transfer(adap
, msgs
, num
);
2141 i2c_unlock_adapter(adap
);
2145 dev_dbg(&adap
->dev
, "I2C level transfers not supported\n");
2149 EXPORT_SYMBOL(i2c_transfer
);
2152 * i2c_master_send - issue a single I2C message in master transmit mode
2153 * @client: Handle to slave device
2154 * @buf: Data that will be written to the slave
2155 * @count: How many bytes to write, must be less than 64k since msg.len is u16
2157 * Returns negative errno, or else the number of bytes written.
2159 int i2c_master_send(const struct i2c_client
*client
, const char *buf
, int count
)
2162 struct i2c_adapter
*adap
= client
->adapter
;
2165 msg
.addr
= client
->addr
;
2166 msg
.flags
= client
->flags
& I2C_M_TEN
;
2168 msg
.buf
= (char *)buf
;
2170 ret
= i2c_transfer(adap
, &msg
, 1);
2173 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2174 * transmitted, else error code.
2176 return (ret
== 1) ? count
: ret
;
2178 EXPORT_SYMBOL(i2c_master_send
);
2181 * i2c_master_recv - issue a single I2C message in master receive mode
2182 * @client: Handle to slave device
2183 * @buf: Where to store data read from slave
2184 * @count: How many bytes to read, must be less than 64k since msg.len is u16
2186 * Returns negative errno, or else the number of bytes read.
2188 int i2c_master_recv(const struct i2c_client
*client
, char *buf
, int count
)
2190 struct i2c_adapter
*adap
= client
->adapter
;
2194 msg
.addr
= client
->addr
;
2195 msg
.flags
= client
->flags
& I2C_M_TEN
;
2196 msg
.flags
|= I2C_M_RD
;
2200 ret
= i2c_transfer(adap
, &msg
, 1);
2203 * If everything went ok (i.e. 1 msg received), return #bytes received,
2206 return (ret
== 1) ? count
: ret
;
2208 EXPORT_SYMBOL(i2c_master_recv
);
2210 /* ----------------------------------------------------
2211 * the i2c address scanning function
2212 * Will not work for 10-bit addresses!
2213 * ----------------------------------------------------
2217 * Legacy default probe function, mostly relevant for SMBus. The default
2218 * probe method is a quick write, but it is known to corrupt the 24RF08
2219 * EEPROMs due to a state machine bug, and could also irreversibly
2220 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2221 * we use a short byte read instead. Also, some bus drivers don't implement
2222 * quick write, so we fallback to a byte read in that case too.
2223 * On x86, there is another special case for FSC hardware monitoring chips,
2224 * which want regular byte reads (address 0x73.) Fortunately, these are the
2225 * only known chips using this I2C address on PC hardware.
2226 * Returns 1 if probe succeeded, 0 if not.
2228 static int i2c_default_probe(struct i2c_adapter
*adap
, unsigned short addr
)
2231 union i2c_smbus_data dummy
;
2234 if (addr
== 0x73 && (adap
->class & I2C_CLASS_HWMON
)
2235 && i2c_check_functionality(adap
, I2C_FUNC_SMBUS_READ_BYTE_DATA
))
2236 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
2237 I2C_SMBUS_BYTE_DATA
, &dummy
);
2240 if (!((addr
& ~0x07) == 0x30 || (addr
& ~0x0f) == 0x50)
2241 && i2c_check_functionality(adap
, I2C_FUNC_SMBUS_QUICK
))
2242 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_WRITE
, 0,
2243 I2C_SMBUS_QUICK
, NULL
);
2244 else if (i2c_check_functionality(adap
, I2C_FUNC_SMBUS_READ_BYTE
))
2245 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
2246 I2C_SMBUS_BYTE
, &dummy
);
2248 dev_warn(&adap
->dev
, "No suitable probing method supported for address 0x%02X\n",
2256 static int i2c_detect_address(struct i2c_client
*temp_client
,
2257 struct i2c_driver
*driver
)
2259 struct i2c_board_info info
;
2260 struct i2c_adapter
*adapter
= temp_client
->adapter
;
2261 int addr
= temp_client
->addr
;
2264 /* Make sure the address is valid */
2265 err
= i2c_check_addr_validity(addr
);
2267 dev_warn(&adapter
->dev
, "Invalid probe address 0x%02x\n",
2272 /* Skip if already in use */
2273 if (i2c_check_addr_busy(adapter
, addr
))
2276 /* Make sure there is something at this address */
2277 if (!i2c_default_probe(adapter
, addr
))
2280 /* Finally call the custom detection function */
2281 memset(&info
, 0, sizeof(struct i2c_board_info
));
2283 err
= driver
->detect(temp_client
, &info
);
2285 /* -ENODEV is returned if the detection fails. We catch it
2286 here as this isn't an error. */
2287 return err
== -ENODEV
? 0 : err
;
2290 /* Consistency check */
2291 if (info
.type
[0] == '\0') {
2292 dev_err(&adapter
->dev
, "%s detection function provided "
2293 "no name for 0x%x\n", driver
->driver
.name
,
2296 struct i2c_client
*client
;
2298 /* Detection succeeded, instantiate the device */
2299 if (adapter
->class & I2C_CLASS_DEPRECATED
)
2300 dev_warn(&adapter
->dev
,
2301 "This adapter will soon drop class based instantiation of devices. "
2302 "Please make sure client 0x%02x gets instantiated by other means. "
2303 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2306 dev_dbg(&adapter
->dev
, "Creating %s at 0x%02x\n",
2307 info
.type
, info
.addr
);
2308 client
= i2c_new_device(adapter
, &info
);
2310 list_add_tail(&client
->detected
, &driver
->clients
);
2312 dev_err(&adapter
->dev
, "Failed creating %s at 0x%02x\n",
2313 info
.type
, info
.addr
);
2318 static int i2c_detect(struct i2c_adapter
*adapter
, struct i2c_driver
*driver
)
2320 const unsigned short *address_list
;
2321 struct i2c_client
*temp_client
;
2323 int adap_id
= i2c_adapter_id(adapter
);
2325 address_list
= driver
->address_list
;
2326 if (!driver
->detect
|| !address_list
)
2329 /* Warn that the adapter lost class based instantiation */
2330 if (adapter
->class == I2C_CLASS_DEPRECATED
) {
2331 dev_dbg(&adapter
->dev
,
2332 "This adapter dropped support for I2C classes and "
2333 "won't auto-detect %s devices anymore. If you need it, check "
2334 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2335 driver
->driver
.name
);
2339 /* Stop here if the classes do not match */
2340 if (!(adapter
->class & driver
->class))
2343 /* Set up a temporary client to help detect callback */
2344 temp_client
= kzalloc(sizeof(struct i2c_client
), GFP_KERNEL
);
2347 temp_client
->adapter
= adapter
;
2349 for (i
= 0; address_list
[i
] != I2C_CLIENT_END
; i
+= 1) {
2350 dev_dbg(&adapter
->dev
, "found normal entry for adapter %d, "
2351 "addr 0x%02x\n", adap_id
, address_list
[i
]);
2352 temp_client
->addr
= address_list
[i
];
2353 err
= i2c_detect_address(temp_client
, driver
);
2362 int i2c_probe_func_quick_read(struct i2c_adapter
*adap
, unsigned short addr
)
2364 return i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
2365 I2C_SMBUS_QUICK
, NULL
) >= 0;
2367 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read
);
2370 i2c_new_probed_device(struct i2c_adapter
*adap
,
2371 struct i2c_board_info
*info
,
2372 unsigned short const *addr_list
,
2373 int (*probe
)(struct i2c_adapter
*, unsigned short addr
))
2378 probe
= i2c_default_probe
;
2380 for (i
= 0; addr_list
[i
] != I2C_CLIENT_END
; i
++) {
2381 /* Check address validity */
2382 if (i2c_check_addr_validity(addr_list
[i
]) < 0) {
2383 dev_warn(&adap
->dev
, "Invalid 7-bit address "
2384 "0x%02x\n", addr_list
[i
]);
2388 /* Check address availability */
2389 if (i2c_check_addr_busy(adap
, addr_list
[i
])) {
2390 dev_dbg(&adap
->dev
, "Address 0x%02x already in "
2391 "use, not probing\n", addr_list
[i
]);
2395 /* Test address responsiveness */
2396 if (probe(adap
, addr_list
[i
]))
2400 if (addr_list
[i
] == I2C_CLIENT_END
) {
2401 dev_dbg(&adap
->dev
, "Probing failed, no device found\n");
2405 info
->addr
= addr_list
[i
];
2406 return i2c_new_device(adap
, info
);
2408 EXPORT_SYMBOL_GPL(i2c_new_probed_device
);
2410 struct i2c_adapter
*i2c_get_adapter(int nr
)
2412 struct i2c_adapter
*adapter
;
2414 mutex_lock(&core_lock
);
2415 adapter
= idr_find(&i2c_adapter_idr
, nr
);
2416 if (adapter
&& !try_module_get(adapter
->owner
))
2419 mutex_unlock(&core_lock
);
2422 EXPORT_SYMBOL(i2c_get_adapter
);
2424 void i2c_put_adapter(struct i2c_adapter
*adap
)
2427 module_put(adap
->owner
);
2429 EXPORT_SYMBOL(i2c_put_adapter
);
2431 /* The SMBus parts */
2433 #define POLY (0x1070U << 3)
2434 static u8
crc8(u16 data
)
2438 for (i
= 0; i
< 8; i
++) {
2443 return (u8
)(data
>> 8);
2446 /* Incremental CRC8 over count bytes in the array pointed to by p */
2447 static u8
i2c_smbus_pec(u8 crc
, u8
*p
, size_t count
)
2451 for (i
= 0; i
< count
; i
++)
2452 crc
= crc8((crc
^ p
[i
]) << 8);
2456 /* Assume a 7-bit address, which is reasonable for SMBus */
2457 static u8
i2c_smbus_msg_pec(u8 pec
, struct i2c_msg
*msg
)
2459 /* The address will be sent first */
2460 u8 addr
= (msg
->addr
<< 1) | !!(msg
->flags
& I2C_M_RD
);
2461 pec
= i2c_smbus_pec(pec
, &addr
, 1);
2463 /* The data buffer follows */
2464 return i2c_smbus_pec(pec
, msg
->buf
, msg
->len
);
2467 /* Used for write only transactions */
2468 static inline void i2c_smbus_add_pec(struct i2c_msg
*msg
)
2470 msg
->buf
[msg
->len
] = i2c_smbus_msg_pec(0, msg
);
2474 /* Return <0 on CRC error
2475 If there was a write before this read (most cases) we need to take the
2476 partial CRC from the write part into account.
2477 Note that this function does modify the message (we need to decrease the
2478 message length to hide the CRC byte from the caller). */
2479 static int i2c_smbus_check_pec(u8 cpec
, struct i2c_msg
*msg
)
2481 u8 rpec
= msg
->buf
[--msg
->len
];
2482 cpec
= i2c_smbus_msg_pec(cpec
, msg
);
2485 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2493 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2494 * @client: Handle to slave device
2496 * This executes the SMBus "receive byte" protocol, returning negative errno
2497 * else the byte received from the device.
2499 s32
i2c_smbus_read_byte(const struct i2c_client
*client
)
2501 union i2c_smbus_data data
;
2504 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2506 I2C_SMBUS_BYTE
, &data
);
2507 return (status
< 0) ? status
: data
.byte
;
2509 EXPORT_SYMBOL(i2c_smbus_read_byte
);
2512 * i2c_smbus_write_byte - SMBus "send byte" protocol
2513 * @client: Handle to slave device
2514 * @value: Byte to be sent
2516 * This executes the SMBus "send byte" protocol, returning negative errno
2517 * else zero on success.
2519 s32
i2c_smbus_write_byte(const struct i2c_client
*client
, u8 value
)
2521 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2522 I2C_SMBUS_WRITE
, value
, I2C_SMBUS_BYTE
, NULL
);
2524 EXPORT_SYMBOL(i2c_smbus_write_byte
);
2527 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2528 * @client: Handle to slave device
2529 * @command: Byte interpreted by slave
2531 * This executes the SMBus "read byte" protocol, returning negative errno
2532 * else a data byte received from the device.
2534 s32
i2c_smbus_read_byte_data(const struct i2c_client
*client
, u8 command
)
2536 union i2c_smbus_data data
;
2539 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2540 I2C_SMBUS_READ
, command
,
2541 I2C_SMBUS_BYTE_DATA
, &data
);
2542 return (status
< 0) ? status
: data
.byte
;
2544 EXPORT_SYMBOL(i2c_smbus_read_byte_data
);
2547 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2548 * @client: Handle to slave device
2549 * @command: Byte interpreted by slave
2550 * @value: Byte being written
2552 * This executes the SMBus "write byte" protocol, returning negative errno
2553 * else zero on success.
2555 s32
i2c_smbus_write_byte_data(const struct i2c_client
*client
, u8 command
,
2558 union i2c_smbus_data data
;
2560 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2561 I2C_SMBUS_WRITE
, command
,
2562 I2C_SMBUS_BYTE_DATA
, &data
);
2564 EXPORT_SYMBOL(i2c_smbus_write_byte_data
);
2567 * i2c_smbus_read_word_data - SMBus "read word" protocol
2568 * @client: Handle to slave device
2569 * @command: Byte interpreted by slave
2571 * This executes the SMBus "read word" protocol, returning negative errno
2572 * else a 16-bit unsigned "word" received from the device.
2574 s32
i2c_smbus_read_word_data(const struct i2c_client
*client
, u8 command
)
2576 union i2c_smbus_data data
;
2579 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2580 I2C_SMBUS_READ
, command
,
2581 I2C_SMBUS_WORD_DATA
, &data
);
2582 return (status
< 0) ? status
: data
.word
;
2584 EXPORT_SYMBOL(i2c_smbus_read_word_data
);
2587 * i2c_smbus_write_word_data - SMBus "write word" protocol
2588 * @client: Handle to slave device
2589 * @command: Byte interpreted by slave
2590 * @value: 16-bit "word" being written
2592 * This executes the SMBus "write word" protocol, returning negative errno
2593 * else zero on success.
2595 s32
i2c_smbus_write_word_data(const struct i2c_client
*client
, u8 command
,
2598 union i2c_smbus_data data
;
2600 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2601 I2C_SMBUS_WRITE
, command
,
2602 I2C_SMBUS_WORD_DATA
, &data
);
2604 EXPORT_SYMBOL(i2c_smbus_write_word_data
);
2607 * i2c_smbus_read_block_data - SMBus "block read" protocol
2608 * @client: Handle to slave device
2609 * @command: Byte interpreted by slave
2610 * @values: Byte array into which data will be read; big enough to hold
2611 * the data returned by the slave. SMBus allows at most 32 bytes.
2613 * This executes the SMBus "block read" protocol, returning negative errno
2614 * else the number of data bytes in the slave's response.
2616 * Note that using this function requires that the client's adapter support
2617 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2618 * support this; its emulation through I2C messaging relies on a specific
2619 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2621 s32
i2c_smbus_read_block_data(const struct i2c_client
*client
, u8 command
,
2624 union i2c_smbus_data data
;
2627 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2628 I2C_SMBUS_READ
, command
,
2629 I2C_SMBUS_BLOCK_DATA
, &data
);
2633 memcpy(values
, &data
.block
[1], data
.block
[0]);
2634 return data
.block
[0];
2636 EXPORT_SYMBOL(i2c_smbus_read_block_data
);
2639 * i2c_smbus_write_block_data - SMBus "block write" protocol
2640 * @client: Handle to slave device
2641 * @command: Byte interpreted by slave
2642 * @length: Size of data block; SMBus allows at most 32 bytes
2643 * @values: Byte array which will be written.
2645 * This executes the SMBus "block write" protocol, returning negative errno
2646 * else zero on success.
2648 s32
i2c_smbus_write_block_data(const struct i2c_client
*client
, u8 command
,
2649 u8 length
, const u8
*values
)
2651 union i2c_smbus_data data
;
2653 if (length
> I2C_SMBUS_BLOCK_MAX
)
2654 length
= I2C_SMBUS_BLOCK_MAX
;
2655 data
.block
[0] = length
;
2656 memcpy(&data
.block
[1], values
, length
);
2657 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2658 I2C_SMBUS_WRITE
, command
,
2659 I2C_SMBUS_BLOCK_DATA
, &data
);
2661 EXPORT_SYMBOL(i2c_smbus_write_block_data
);
2663 /* Returns the number of read bytes */
2664 s32
i2c_smbus_read_i2c_block_data(const struct i2c_client
*client
, u8 command
,
2665 u8 length
, u8
*values
)
2667 union i2c_smbus_data data
;
2670 if (length
> I2C_SMBUS_BLOCK_MAX
)
2671 length
= I2C_SMBUS_BLOCK_MAX
;
2672 data
.block
[0] = length
;
2673 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2674 I2C_SMBUS_READ
, command
,
2675 I2C_SMBUS_I2C_BLOCK_DATA
, &data
);
2679 memcpy(values
, &data
.block
[1], data
.block
[0]);
2680 return data
.block
[0];
2682 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data
);
2684 s32
i2c_smbus_write_i2c_block_data(const struct i2c_client
*client
, u8 command
,
2685 u8 length
, const u8
*values
)
2687 union i2c_smbus_data data
;
2689 if (length
> I2C_SMBUS_BLOCK_MAX
)
2690 length
= I2C_SMBUS_BLOCK_MAX
;
2691 data
.block
[0] = length
;
2692 memcpy(data
.block
+ 1, values
, length
);
2693 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2694 I2C_SMBUS_WRITE
, command
,
2695 I2C_SMBUS_I2C_BLOCK_DATA
, &data
);
2697 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data
);
2699 /* Simulate a SMBus command using the i2c protocol
2700 No checking of parameters is done! */
2701 static s32
i2c_smbus_xfer_emulated(struct i2c_adapter
*adapter
, u16 addr
,
2702 unsigned short flags
,
2703 char read_write
, u8 command
, int size
,
2704 union i2c_smbus_data
*data
)
2706 /* So we need to generate a series of msgs. In the case of writing, we
2707 need to use only one message; when reading, we need two. We initialize
2708 most things with sane defaults, to keep the code below somewhat
2710 unsigned char msgbuf0
[I2C_SMBUS_BLOCK_MAX
+3];
2711 unsigned char msgbuf1
[I2C_SMBUS_BLOCK_MAX
+2];
2712 int num
= read_write
== I2C_SMBUS_READ
? 2 : 1;
2716 struct i2c_msg msg
[2] = {
2724 .flags
= flags
| I2C_M_RD
,
2730 msgbuf0
[0] = command
;
2732 case I2C_SMBUS_QUICK
:
2734 /* Special case: The read/write field is used as data */
2735 msg
[0].flags
= flags
| (read_write
== I2C_SMBUS_READ
?
2739 case I2C_SMBUS_BYTE
:
2740 if (read_write
== I2C_SMBUS_READ
) {
2741 /* Special case: only a read! */
2742 msg
[0].flags
= I2C_M_RD
| flags
;
2746 case I2C_SMBUS_BYTE_DATA
:
2747 if (read_write
== I2C_SMBUS_READ
)
2751 msgbuf0
[1] = data
->byte
;
2754 case I2C_SMBUS_WORD_DATA
:
2755 if (read_write
== I2C_SMBUS_READ
)
2759 msgbuf0
[1] = data
->word
& 0xff;
2760 msgbuf0
[2] = data
->word
>> 8;
2763 case I2C_SMBUS_PROC_CALL
:
2764 num
= 2; /* Special case */
2765 read_write
= I2C_SMBUS_READ
;
2768 msgbuf0
[1] = data
->word
& 0xff;
2769 msgbuf0
[2] = data
->word
>> 8;
2771 case I2C_SMBUS_BLOCK_DATA
:
2772 if (read_write
== I2C_SMBUS_READ
) {
2773 msg
[1].flags
|= I2C_M_RECV_LEN
;
2774 msg
[1].len
= 1; /* block length will be added by
2775 the underlying bus driver */
2777 msg
[0].len
= data
->block
[0] + 2;
2778 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 2) {
2779 dev_err(&adapter
->dev
,
2780 "Invalid block write size %d\n",
2784 for (i
= 1; i
< msg
[0].len
; i
++)
2785 msgbuf0
[i
] = data
->block
[i
-1];
2788 case I2C_SMBUS_BLOCK_PROC_CALL
:
2789 num
= 2; /* Another special case */
2790 read_write
= I2C_SMBUS_READ
;
2791 if (data
->block
[0] > I2C_SMBUS_BLOCK_MAX
) {
2792 dev_err(&adapter
->dev
,
2793 "Invalid block write size %d\n",
2797 msg
[0].len
= data
->block
[0] + 2;
2798 for (i
= 1; i
< msg
[0].len
; i
++)
2799 msgbuf0
[i
] = data
->block
[i
-1];
2800 msg
[1].flags
|= I2C_M_RECV_LEN
;
2801 msg
[1].len
= 1; /* block length will be added by
2802 the underlying bus driver */
2804 case I2C_SMBUS_I2C_BLOCK_DATA
:
2805 if (read_write
== I2C_SMBUS_READ
) {
2806 msg
[1].len
= data
->block
[0];
2808 msg
[0].len
= data
->block
[0] + 1;
2809 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 1) {
2810 dev_err(&adapter
->dev
,
2811 "Invalid block write size %d\n",
2815 for (i
= 1; i
<= data
->block
[0]; i
++)
2816 msgbuf0
[i
] = data
->block
[i
];
2820 dev_err(&adapter
->dev
, "Unsupported transaction %d\n", size
);
2824 i
= ((flags
& I2C_CLIENT_PEC
) && size
!= I2C_SMBUS_QUICK
2825 && size
!= I2C_SMBUS_I2C_BLOCK_DATA
);
2827 /* Compute PEC if first message is a write */
2828 if (!(msg
[0].flags
& I2C_M_RD
)) {
2829 if (num
== 1) /* Write only */
2830 i2c_smbus_add_pec(&msg
[0]);
2831 else /* Write followed by read */
2832 partial_pec
= i2c_smbus_msg_pec(0, &msg
[0]);
2834 /* Ask for PEC if last message is a read */
2835 if (msg
[num
-1].flags
& I2C_M_RD
)
2839 status
= i2c_transfer(adapter
, msg
, num
);
2843 /* Check PEC if last message is a read */
2844 if (i
&& (msg
[num
-1].flags
& I2C_M_RD
)) {
2845 status
= i2c_smbus_check_pec(partial_pec
, &msg
[num
-1]);
2850 if (read_write
== I2C_SMBUS_READ
)
2852 case I2C_SMBUS_BYTE
:
2853 data
->byte
= msgbuf0
[0];
2855 case I2C_SMBUS_BYTE_DATA
:
2856 data
->byte
= msgbuf1
[0];
2858 case I2C_SMBUS_WORD_DATA
:
2859 case I2C_SMBUS_PROC_CALL
:
2860 data
->word
= msgbuf1
[0] | (msgbuf1
[1] << 8);
2862 case I2C_SMBUS_I2C_BLOCK_DATA
:
2863 for (i
= 0; i
< data
->block
[0]; i
++)
2864 data
->block
[i
+1] = msgbuf1
[i
];
2866 case I2C_SMBUS_BLOCK_DATA
:
2867 case I2C_SMBUS_BLOCK_PROC_CALL
:
2868 for (i
= 0; i
< msgbuf1
[0] + 1; i
++)
2869 data
->block
[i
] = msgbuf1
[i
];
2876 * i2c_smbus_xfer - execute SMBus protocol operations
2877 * @adapter: Handle to I2C bus
2878 * @addr: Address of SMBus slave on that bus
2879 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2880 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2881 * @command: Byte interpreted by slave, for protocols which use such bytes
2882 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2883 * @data: Data to be read or written
2885 * This executes an SMBus protocol operation, and returns a negative
2886 * errno code else zero on success.
2888 s32
i2c_smbus_xfer(struct i2c_adapter
*adapter
, u16 addr
, unsigned short flags
,
2889 char read_write
, u8 command
, int protocol
,
2890 union i2c_smbus_data
*data
)
2892 unsigned long orig_jiffies
;
2896 /* If enabled, the following two tracepoints are conditional on
2897 * read_write and protocol.
2899 trace_smbus_write(adapter
, addr
, flags
, read_write
,
2900 command
, protocol
, data
);
2901 trace_smbus_read(adapter
, addr
, flags
, read_write
,
2904 flags
&= I2C_M_TEN
| I2C_CLIENT_PEC
| I2C_CLIENT_SCCB
;
2906 if (adapter
->algo
->smbus_xfer
) {
2907 i2c_lock_adapter(adapter
);
2909 /* Retry automatically on arbitration loss */
2910 orig_jiffies
= jiffies
;
2911 for (res
= 0, try = 0; try <= adapter
->retries
; try++) {
2912 res
= adapter
->algo
->smbus_xfer(adapter
, addr
, flags
,
2913 read_write
, command
,
2917 if (time_after(jiffies
,
2918 orig_jiffies
+ adapter
->timeout
))
2921 i2c_unlock_adapter(adapter
);
2923 if (res
!= -EOPNOTSUPP
|| !adapter
->algo
->master_xfer
)
2926 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2927 * implement native support for the SMBus operation.
2931 res
= i2c_smbus_xfer_emulated(adapter
, addr
, flags
, read_write
,
2932 command
, protocol
, data
);
2935 /* If enabled, the reply tracepoint is conditional on read_write. */
2936 trace_smbus_reply(adapter
, addr
, flags
, read_write
,
2937 command
, protocol
, data
);
2938 trace_smbus_result(adapter
, addr
, flags
, read_write
,
2939 command
, protocol
, res
);
2943 EXPORT_SYMBOL(i2c_smbus_xfer
);
2945 #if IS_ENABLED(CONFIG_I2C_SLAVE)
2946 int i2c_slave_register(struct i2c_client
*client
, i2c_slave_cb_t slave_cb
)
2950 if (!client
|| !slave_cb
) {
2951 WARN(1, "insufficent data\n");
2955 if (!(client
->flags
& I2C_CLIENT_TEN
)) {
2956 /* Enforce stricter address checking */
2957 ret
= i2c_check_addr_validity(client
->addr
);
2959 dev_err(&client
->dev
, "%s: invalid address\n", __func__
);
2964 if (!client
->adapter
->algo
->reg_slave
) {
2965 dev_err(&client
->dev
, "%s: not supported by adapter\n", __func__
);
2969 client
->slave_cb
= slave_cb
;
2971 i2c_lock_adapter(client
->adapter
);
2972 ret
= client
->adapter
->algo
->reg_slave(client
);
2973 i2c_unlock_adapter(client
->adapter
);
2976 client
->slave_cb
= NULL
;
2977 dev_err(&client
->dev
, "%s: adapter returned error %d\n", __func__
, ret
);
2982 EXPORT_SYMBOL_GPL(i2c_slave_register
);
2984 int i2c_slave_unregister(struct i2c_client
*client
)
2988 if (!client
->adapter
->algo
->unreg_slave
) {
2989 dev_err(&client
->dev
, "%s: not supported by adapter\n", __func__
);
2993 i2c_lock_adapter(client
->adapter
);
2994 ret
= client
->adapter
->algo
->unreg_slave(client
);
2995 i2c_unlock_adapter(client
->adapter
);
2998 client
->slave_cb
= NULL
;
3000 dev_err(&client
->dev
, "%s: adapter returned error %d\n", __func__
, ret
);
3004 EXPORT_SYMBOL_GPL(i2c_slave_unregister
);
3007 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
3008 MODULE_DESCRIPTION("I2C-Bus main module");
3009 MODULE_LICENSE("GPL");