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>
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/delay.h>
32 #include <linux/errno.h>
33 #include <linux/gpio.h>
34 #include <linux/slab.h>
35 #include <linux/i2c.h>
36 #include <linux/init.h>
37 #include <linux/idr.h>
38 #include <linux/mutex.h>
40 #include <linux/of_device.h>
41 #include <linux/of_irq.h>
42 #include <linux/clk/clk-conf.h>
43 #include <linux/completion.h>
44 #include <linux/hardirq.h>
45 #include <linux/irqflags.h>
46 #include <linux/rwsem.h>
47 #include <linux/pm_runtime.h>
48 #include <linux/pm_domain.h>
49 #include <linux/acpi.h>
50 #include <linux/jump_label.h>
51 #include <asm/uaccess.h>
55 #define CREATE_TRACE_POINTS
56 #include <trace/events/i2c.h>
58 /* core_lock protects i2c_adapter_idr, and guarantees
59 that device detection, deletion of detected devices, and attach_adapter
60 calls are serialized */
61 static DEFINE_MUTEX(core_lock
);
62 static DEFINE_IDR(i2c_adapter_idr
);
64 static struct device_type i2c_client_type
;
65 static int i2c_detect(struct i2c_adapter
*adapter
, struct i2c_driver
*driver
);
67 static struct static_key i2c_trace_msg
= STATIC_KEY_INIT_FALSE
;
69 void i2c_transfer_trace_reg(void)
71 static_key_slow_inc(&i2c_trace_msg
);
74 void i2c_transfer_trace_unreg(void)
76 static_key_slow_dec(&i2c_trace_msg
);
79 #if defined(CONFIG_ACPI)
80 struct acpi_i2c_handler_data
{
81 struct acpi_connection_info info
;
82 struct i2c_adapter
*adapter
;
95 static int acpi_i2c_add_resource(struct acpi_resource
*ares
, void *data
)
97 struct i2c_board_info
*info
= data
;
99 if (ares
->type
== ACPI_RESOURCE_TYPE_SERIAL_BUS
) {
100 struct acpi_resource_i2c_serialbus
*sb
;
102 sb
= &ares
->data
.i2c_serial_bus
;
103 if (sb
->type
== ACPI_RESOURCE_SERIAL_TYPE_I2C
) {
104 info
->addr
= sb
->slave_address
;
105 if (sb
->access_mode
== ACPI_I2C_10BIT_MODE
)
106 info
->flags
|= I2C_CLIENT_TEN
;
108 } else if (info
->irq
< 0) {
111 if (acpi_dev_resource_interrupt(ares
, 0, &r
))
115 /* Tell the ACPI core to skip this resource */
119 static acpi_status
acpi_i2c_add_device(acpi_handle handle
, u32 level
,
120 void *data
, void **return_value
)
122 struct i2c_adapter
*adapter
= data
;
123 struct list_head resource_list
;
124 struct i2c_board_info info
;
125 struct acpi_device
*adev
;
128 if (acpi_bus_get_device(handle
, &adev
))
130 if (acpi_bus_get_status(adev
) || !adev
->status
.present
)
133 memset(&info
, 0, sizeof(info
));
134 info
.acpi_node
.companion
= adev
;
137 INIT_LIST_HEAD(&resource_list
);
138 ret
= acpi_dev_get_resources(adev
, &resource_list
,
139 acpi_i2c_add_resource
, &info
);
140 acpi_dev_free_resource_list(&resource_list
);
142 if (ret
< 0 || !info
.addr
)
145 adev
->power
.flags
.ignore_parent
= true;
146 strlcpy(info
.type
, dev_name(&adev
->dev
), sizeof(info
.type
));
147 if (!i2c_new_device(adapter
, &info
)) {
148 adev
->power
.flags
.ignore_parent
= false;
149 dev_err(&adapter
->dev
,
150 "failed to add I2C device %s from ACPI\n",
151 dev_name(&adev
->dev
));
158 * acpi_i2c_register_devices - enumerate I2C slave devices behind adapter
159 * @adap: pointer to adapter
161 * Enumerate all I2C slave devices behind this adapter by walking the ACPI
162 * namespace. When a device is found it will be added to the Linux device
163 * model and bound to the corresponding ACPI handle.
165 static void acpi_i2c_register_devices(struct i2c_adapter
*adap
)
170 if (!adap
->dev
.parent
)
173 handle
= ACPI_HANDLE(adap
->dev
.parent
);
177 status
= acpi_walk_namespace(ACPI_TYPE_DEVICE
, handle
, 1,
178 acpi_i2c_add_device
, NULL
,
180 if (ACPI_FAILURE(status
))
181 dev_warn(&adap
->dev
, "failed to enumerate I2C slaves\n");
184 #else /* CONFIG_ACPI */
185 static inline void acpi_i2c_register_devices(struct i2c_adapter
*adap
) { }
186 #endif /* CONFIG_ACPI */
188 #ifdef CONFIG_ACPI_I2C_OPREGION
189 static int acpi_gsb_i2c_read_bytes(struct i2c_client
*client
,
190 u8 cmd
, u8
*data
, u8 data_len
)
193 struct i2c_msg msgs
[2];
197 buffer
= kzalloc(data_len
, GFP_KERNEL
);
201 msgs
[0].addr
= client
->addr
;
202 msgs
[0].flags
= client
->flags
;
206 msgs
[1].addr
= client
->addr
;
207 msgs
[1].flags
= client
->flags
| I2C_M_RD
;
208 msgs
[1].len
= data_len
;
209 msgs
[1].buf
= buffer
;
211 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
213 dev_err(&client
->adapter
->dev
, "i2c read failed\n");
215 memcpy(data
, buffer
, data_len
);
221 static int acpi_gsb_i2c_write_bytes(struct i2c_client
*client
,
222 u8 cmd
, u8
*data
, u8 data_len
)
225 struct i2c_msg msgs
[1];
229 buffer
= kzalloc(data_len
+ 1, GFP_KERNEL
);
234 memcpy(buffer
+ 1, data
, data_len
);
236 msgs
[0].addr
= client
->addr
;
237 msgs
[0].flags
= client
->flags
;
238 msgs
[0].len
= data_len
+ 1;
239 msgs
[0].buf
= buffer
;
241 ret
= i2c_transfer(client
->adapter
, msgs
, ARRAY_SIZE(msgs
));
243 dev_err(&client
->adapter
->dev
, "i2c write failed\n");
250 acpi_i2c_space_handler(u32 function
, acpi_physical_address command
,
251 u32 bits
, u64
*value64
,
252 void *handler_context
, void *region_context
)
254 struct gsb_buffer
*gsb
= (struct gsb_buffer
*)value64
;
255 struct acpi_i2c_handler_data
*data
= handler_context
;
256 struct acpi_connection_info
*info
= &data
->info
;
257 struct acpi_resource_i2c_serialbus
*sb
;
258 struct i2c_adapter
*adapter
= data
->adapter
;
259 struct i2c_client client
;
260 struct acpi_resource
*ares
;
261 u32 accessor_type
= function
>> 16;
262 u8 action
= function
& ACPI_IO_MASK
;
263 acpi_status ret
= AE_OK
;
266 ret
= acpi_buffer_to_resource(info
->connection
, info
->length
, &ares
);
267 if (ACPI_FAILURE(ret
))
270 if (!value64
|| ares
->type
!= ACPI_RESOURCE_TYPE_SERIAL_BUS
) {
271 ret
= AE_BAD_PARAMETER
;
275 sb
= &ares
->data
.i2c_serial_bus
;
276 if (sb
->type
!= ACPI_RESOURCE_SERIAL_TYPE_I2C
) {
277 ret
= AE_BAD_PARAMETER
;
281 memset(&client
, 0, sizeof(client
));
282 client
.adapter
= adapter
;
283 client
.addr
= sb
->slave_address
;
286 if (sb
->access_mode
== ACPI_I2C_10BIT_MODE
)
287 client
.flags
|= I2C_CLIENT_TEN
;
289 switch (accessor_type
) {
290 case ACPI_GSB_ACCESS_ATTRIB_SEND_RCV
:
291 if (action
== ACPI_READ
) {
292 status
= i2c_smbus_read_byte(&client
);
298 status
= i2c_smbus_write_byte(&client
, gsb
->bdata
);
302 case ACPI_GSB_ACCESS_ATTRIB_BYTE
:
303 if (action
== ACPI_READ
) {
304 status
= i2c_smbus_read_byte_data(&client
, command
);
310 status
= i2c_smbus_write_byte_data(&client
, command
,
315 case ACPI_GSB_ACCESS_ATTRIB_WORD
:
316 if (action
== ACPI_READ
) {
317 status
= i2c_smbus_read_word_data(&client
, command
);
323 status
= i2c_smbus_write_word_data(&client
, command
,
328 case ACPI_GSB_ACCESS_ATTRIB_BLOCK
:
329 if (action
== ACPI_READ
) {
330 status
= i2c_smbus_read_block_data(&client
, command
,
337 status
= i2c_smbus_write_block_data(&client
, command
,
338 gsb
->len
, gsb
->data
);
342 case ACPI_GSB_ACCESS_ATTRIB_MULTIBYTE
:
343 if (action
== ACPI_READ
) {
344 status
= acpi_gsb_i2c_read_bytes(&client
, command
,
345 gsb
->data
, info
->access_length
);
349 status
= acpi_gsb_i2c_write_bytes(&client
, command
,
350 gsb
->data
, info
->access_length
);
355 pr_info("protocol(0x%02x) is not supported.\n", accessor_type
);
356 ret
= AE_BAD_PARAMETER
;
360 gsb
->status
= status
;
368 static int acpi_i2c_install_space_handler(struct i2c_adapter
*adapter
)
371 struct acpi_i2c_handler_data
*data
;
374 if (!adapter
->dev
.parent
)
377 handle
= ACPI_HANDLE(adapter
->dev
.parent
);
382 data
= kzalloc(sizeof(struct acpi_i2c_handler_data
),
387 data
->adapter
= adapter
;
388 status
= acpi_bus_attach_private_data(handle
, (void *)data
);
389 if (ACPI_FAILURE(status
)) {
394 status
= acpi_install_address_space_handler(handle
,
395 ACPI_ADR_SPACE_GSBUS
,
396 &acpi_i2c_space_handler
,
399 if (ACPI_FAILURE(status
)) {
400 dev_err(&adapter
->dev
, "Error installing i2c space handler\n");
401 acpi_bus_detach_private_data(handle
);
409 static void acpi_i2c_remove_space_handler(struct i2c_adapter
*adapter
)
412 struct acpi_i2c_handler_data
*data
;
415 if (!adapter
->dev
.parent
)
418 handle
= ACPI_HANDLE(adapter
->dev
.parent
);
423 acpi_remove_address_space_handler(handle
,
424 ACPI_ADR_SPACE_GSBUS
,
425 &acpi_i2c_space_handler
);
427 status
= acpi_bus_get_private_data(handle
, (void **)&data
);
428 if (ACPI_SUCCESS(status
))
431 acpi_bus_detach_private_data(handle
);
433 #else /* CONFIG_ACPI_I2C_OPREGION */
434 static inline void acpi_i2c_remove_space_handler(struct i2c_adapter
*adapter
)
437 static inline int acpi_i2c_install_space_handler(struct i2c_adapter
*adapter
)
439 #endif /* CONFIG_ACPI_I2C_OPREGION */
441 /* ------------------------------------------------------------------------- */
443 static const struct i2c_device_id
*i2c_match_id(const struct i2c_device_id
*id
,
444 const struct i2c_client
*client
)
446 while (id
->name
[0]) {
447 if (strcmp(client
->name
, id
->name
) == 0)
454 static int i2c_device_match(struct device
*dev
, struct device_driver
*drv
)
456 struct i2c_client
*client
= i2c_verify_client(dev
);
457 struct i2c_driver
*driver
;
462 /* Attempt an OF style match */
463 if (of_driver_match_device(dev
, drv
))
466 /* Then ACPI style match */
467 if (acpi_driver_match_device(dev
, drv
))
470 driver
= to_i2c_driver(drv
);
471 /* match on an id table if there is one */
472 if (driver
->id_table
)
473 return i2c_match_id(driver
->id_table
, client
) != NULL
;
479 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
480 static int i2c_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
482 struct i2c_client
*client
= to_i2c_client(dev
);
485 rc
= acpi_device_uevent_modalias(dev
, env
);
489 if (add_uevent_var(env
, "MODALIAS=%s%s",
490 I2C_MODULE_PREFIX
, client
->name
))
492 dev_dbg(dev
, "uevent\n");
496 /* i2c bus recovery routines */
497 static int get_scl_gpio_value(struct i2c_adapter
*adap
)
499 return gpio_get_value(adap
->bus_recovery_info
->scl_gpio
);
502 static void set_scl_gpio_value(struct i2c_adapter
*adap
, int val
)
504 gpio_set_value(adap
->bus_recovery_info
->scl_gpio
, val
);
507 static int get_sda_gpio_value(struct i2c_adapter
*adap
)
509 return gpio_get_value(adap
->bus_recovery_info
->sda_gpio
);
512 static int i2c_get_gpios_for_recovery(struct i2c_adapter
*adap
)
514 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
515 struct device
*dev
= &adap
->dev
;
518 ret
= gpio_request_one(bri
->scl_gpio
, GPIOF_OPEN_DRAIN
|
519 GPIOF_OUT_INIT_HIGH
, "i2c-scl");
521 dev_warn(dev
, "Can't get SCL gpio: %d\n", bri
->scl_gpio
);
526 if (gpio_request_one(bri
->sda_gpio
, GPIOF_IN
, "i2c-sda")) {
527 /* work without SDA polling */
528 dev_warn(dev
, "Can't get SDA gpio: %d. Not using SDA polling\n",
537 static void i2c_put_gpios_for_recovery(struct i2c_adapter
*adap
)
539 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
542 gpio_free(bri
->sda_gpio
);
544 gpio_free(bri
->scl_gpio
);
548 * We are generating clock pulses. ndelay() determines durating of clk pulses.
549 * We will generate clock with rate 100 KHz and so duration of both clock levels
550 * is: delay in ns = (10^6 / 100) / 2
552 #define RECOVERY_NDELAY 5000
553 #define RECOVERY_CLK_CNT 9
555 static int i2c_generic_recovery(struct i2c_adapter
*adap
)
557 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
558 int i
= 0, val
= 1, ret
= 0;
560 if (bri
->prepare_recovery
)
561 bri
->prepare_recovery(bri
);
564 * By this time SCL is high, as we need to give 9 falling-rising edges
566 while (i
++ < RECOVERY_CLK_CNT
* 2) {
568 /* Break if SDA is high */
569 if (bri
->get_sda
&& bri
->get_sda(adap
))
571 /* SCL shouldn't be low here */
572 if (!bri
->get_scl(adap
)) {
574 "SCL is stuck low, exit recovery\n");
581 bri
->set_scl(adap
, val
);
582 ndelay(RECOVERY_NDELAY
);
585 if (bri
->unprepare_recovery
)
586 bri
->unprepare_recovery(bri
);
591 int i2c_generic_scl_recovery(struct i2c_adapter
*adap
)
593 adap
->bus_recovery_info
->set_scl(adap
, 1);
594 return i2c_generic_recovery(adap
);
597 int i2c_generic_gpio_recovery(struct i2c_adapter
*adap
)
601 ret
= i2c_get_gpios_for_recovery(adap
);
605 ret
= i2c_generic_recovery(adap
);
606 i2c_put_gpios_for_recovery(adap
);
611 int i2c_recover_bus(struct i2c_adapter
*adap
)
613 if (!adap
->bus_recovery_info
)
616 dev_dbg(&adap
->dev
, "Trying i2c bus recovery\n");
617 return adap
->bus_recovery_info
->recover_bus(adap
);
620 static int i2c_device_probe(struct device
*dev
)
622 struct i2c_client
*client
= i2c_verify_client(dev
);
623 struct i2c_driver
*driver
;
629 driver
= to_i2c_driver(dev
->driver
);
630 if (!driver
->probe
|| !driver
->id_table
)
633 if (!device_can_wakeup(&client
->dev
))
634 device_init_wakeup(&client
->dev
,
635 client
->flags
& I2C_CLIENT_WAKE
);
636 dev_dbg(dev
, "probe\n");
638 status
= of_clk_set_defaults(dev
->of_node
, false);
642 status
= dev_pm_domain_attach(&client
->dev
, true);
643 if (status
!= -EPROBE_DEFER
) {
644 status
= driver
->probe(client
, i2c_match_id(driver
->id_table
,
647 dev_pm_domain_detach(&client
->dev
, true);
653 static int i2c_device_remove(struct device
*dev
)
655 struct i2c_client
*client
= i2c_verify_client(dev
);
656 struct i2c_driver
*driver
;
659 if (!client
|| !dev
->driver
)
662 driver
= to_i2c_driver(dev
->driver
);
663 if (driver
->remove
) {
664 dev_dbg(dev
, "remove\n");
665 status
= driver
->remove(client
);
669 irq_dispose_mapping(client
->irq
);
671 dev_pm_domain_detach(&client
->dev
, true);
675 static void i2c_device_shutdown(struct device
*dev
)
677 struct i2c_client
*client
= i2c_verify_client(dev
);
678 struct i2c_driver
*driver
;
680 if (!client
|| !dev
->driver
)
682 driver
= to_i2c_driver(dev
->driver
);
683 if (driver
->shutdown
)
684 driver
->shutdown(client
);
687 #ifdef CONFIG_PM_SLEEP
688 static int i2c_legacy_suspend(struct device
*dev
, pm_message_t mesg
)
690 struct i2c_client
*client
= i2c_verify_client(dev
);
691 struct i2c_driver
*driver
;
693 if (!client
|| !dev
->driver
)
695 driver
= to_i2c_driver(dev
->driver
);
696 if (!driver
->suspend
)
698 return driver
->suspend(client
, mesg
);
701 static int i2c_legacy_resume(struct device
*dev
)
703 struct i2c_client
*client
= i2c_verify_client(dev
);
704 struct i2c_driver
*driver
;
706 if (!client
|| !dev
->driver
)
708 driver
= to_i2c_driver(dev
->driver
);
711 return driver
->resume(client
);
714 static int i2c_device_pm_suspend(struct device
*dev
)
716 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
719 return pm_generic_suspend(dev
);
721 return i2c_legacy_suspend(dev
, PMSG_SUSPEND
);
724 static int i2c_device_pm_resume(struct device
*dev
)
726 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
729 return pm_generic_resume(dev
);
731 return i2c_legacy_resume(dev
);
734 static int i2c_device_pm_freeze(struct device
*dev
)
736 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
739 return pm_generic_freeze(dev
);
741 return i2c_legacy_suspend(dev
, PMSG_FREEZE
);
744 static int i2c_device_pm_thaw(struct device
*dev
)
746 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
749 return pm_generic_thaw(dev
);
751 return i2c_legacy_resume(dev
);
754 static int i2c_device_pm_poweroff(struct device
*dev
)
756 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
759 return pm_generic_poweroff(dev
);
761 return i2c_legacy_suspend(dev
, PMSG_HIBERNATE
);
764 static int i2c_device_pm_restore(struct device
*dev
)
766 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
769 return pm_generic_restore(dev
);
771 return i2c_legacy_resume(dev
);
773 #else /* !CONFIG_PM_SLEEP */
774 #define i2c_device_pm_suspend NULL
775 #define i2c_device_pm_resume NULL
776 #define i2c_device_pm_freeze NULL
777 #define i2c_device_pm_thaw NULL
778 #define i2c_device_pm_poweroff NULL
779 #define i2c_device_pm_restore NULL
780 #endif /* !CONFIG_PM_SLEEP */
782 static void i2c_client_dev_release(struct device
*dev
)
784 kfree(to_i2c_client(dev
));
788 show_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
790 return sprintf(buf
, "%s\n", dev
->type
== &i2c_client_type
?
791 to_i2c_client(dev
)->name
: to_i2c_adapter(dev
)->name
);
795 show_modalias(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
797 struct i2c_client
*client
= to_i2c_client(dev
);
800 len
= acpi_device_modalias(dev
, buf
, PAGE_SIZE
-1);
804 return sprintf(buf
, "%s%s\n", I2C_MODULE_PREFIX
, client
->name
);
807 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
808 static DEVICE_ATTR(modalias
, S_IRUGO
, show_modalias
, NULL
);
810 static struct attribute
*i2c_dev_attrs
[] = {
812 /* modalias helps coldplug: modprobe $(cat .../modalias) */
813 &dev_attr_modalias
.attr
,
817 static struct attribute_group i2c_dev_attr_group
= {
818 .attrs
= i2c_dev_attrs
,
821 static const struct attribute_group
*i2c_dev_attr_groups
[] = {
826 static const struct dev_pm_ops i2c_device_pm_ops
= {
827 .suspend
= i2c_device_pm_suspend
,
828 .resume
= i2c_device_pm_resume
,
829 .freeze
= i2c_device_pm_freeze
,
830 .thaw
= i2c_device_pm_thaw
,
831 .poweroff
= i2c_device_pm_poweroff
,
832 .restore
= i2c_device_pm_restore
,
834 pm_generic_runtime_suspend
,
835 pm_generic_runtime_resume
,
840 struct bus_type i2c_bus_type
= {
842 .match
= i2c_device_match
,
843 .probe
= i2c_device_probe
,
844 .remove
= i2c_device_remove
,
845 .shutdown
= i2c_device_shutdown
,
846 .pm
= &i2c_device_pm_ops
,
848 EXPORT_SYMBOL_GPL(i2c_bus_type
);
850 static struct device_type i2c_client_type
= {
851 .groups
= i2c_dev_attr_groups
,
852 .uevent
= i2c_device_uevent
,
853 .release
= i2c_client_dev_release
,
858 * i2c_verify_client - return parameter as i2c_client, or NULL
859 * @dev: device, probably from some driver model iterator
861 * When traversing the driver model tree, perhaps using driver model
862 * iterators like @device_for_each_child(), you can't assume very much
863 * about the nodes you find. Use this function to avoid oopses caused
864 * by wrongly treating some non-I2C device as an i2c_client.
866 struct i2c_client
*i2c_verify_client(struct device
*dev
)
868 return (dev
->type
== &i2c_client_type
)
872 EXPORT_SYMBOL(i2c_verify_client
);
875 /* This is a permissive address validity check, I2C address map constraints
876 * are purposely not enforced, except for the general call address. */
877 static int i2c_check_client_addr_validity(const struct i2c_client
*client
)
879 if (client
->flags
& I2C_CLIENT_TEN
) {
880 /* 10-bit address, all values are valid */
881 if (client
->addr
> 0x3ff)
884 /* 7-bit address, reject the general call address */
885 if (client
->addr
== 0x00 || client
->addr
> 0x7f)
891 /* And this is a strict address validity check, used when probing. If a
892 * device uses a reserved address, then it shouldn't be probed. 7-bit
893 * addressing is assumed, 10-bit address devices are rare and should be
894 * explicitly enumerated. */
895 static int i2c_check_addr_validity(unsigned short addr
)
898 * Reserved addresses per I2C specification:
899 * 0x00 General call address / START byte
901 * 0x02 Reserved for different bus format
902 * 0x03 Reserved for future purposes
903 * 0x04-0x07 Hs-mode master code
904 * 0x78-0x7b 10-bit slave addressing
905 * 0x7c-0x7f Reserved for future purposes
907 if (addr
< 0x08 || addr
> 0x77)
912 static int __i2c_check_addr_busy(struct device
*dev
, void *addrp
)
914 struct i2c_client
*client
= i2c_verify_client(dev
);
915 int addr
= *(int *)addrp
;
917 if (client
&& client
->addr
== addr
)
922 /* walk up mux tree */
923 static int i2c_check_mux_parents(struct i2c_adapter
*adapter
, int addr
)
925 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
928 result
= device_for_each_child(&adapter
->dev
, &addr
,
929 __i2c_check_addr_busy
);
931 if (!result
&& parent
)
932 result
= i2c_check_mux_parents(parent
, addr
);
937 /* recurse down mux tree */
938 static int i2c_check_mux_children(struct device
*dev
, void *addrp
)
942 if (dev
->type
== &i2c_adapter_type
)
943 result
= device_for_each_child(dev
, addrp
,
944 i2c_check_mux_children
);
946 result
= __i2c_check_addr_busy(dev
, addrp
);
951 static int i2c_check_addr_busy(struct i2c_adapter
*adapter
, int addr
)
953 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
957 result
= i2c_check_mux_parents(parent
, addr
);
960 result
= device_for_each_child(&adapter
->dev
, &addr
,
961 i2c_check_mux_children
);
967 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
968 * @adapter: Target I2C bus segment
970 void i2c_lock_adapter(struct i2c_adapter
*adapter
)
972 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
975 i2c_lock_adapter(parent
);
977 rt_mutex_lock(&adapter
->bus_lock
);
979 EXPORT_SYMBOL_GPL(i2c_lock_adapter
);
982 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
983 * @adapter: Target I2C bus segment
985 static int i2c_trylock_adapter(struct i2c_adapter
*adapter
)
987 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
990 return i2c_trylock_adapter(parent
);
992 return rt_mutex_trylock(&adapter
->bus_lock
);
996 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
997 * @adapter: Target I2C bus segment
999 void i2c_unlock_adapter(struct i2c_adapter
*adapter
)
1001 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
1004 i2c_unlock_adapter(parent
);
1006 rt_mutex_unlock(&adapter
->bus_lock
);
1008 EXPORT_SYMBOL_GPL(i2c_unlock_adapter
);
1010 static void i2c_dev_set_name(struct i2c_adapter
*adap
,
1011 struct i2c_client
*client
)
1013 struct acpi_device
*adev
= ACPI_COMPANION(&client
->dev
);
1016 dev_set_name(&client
->dev
, "i2c-%s", acpi_dev_name(adev
));
1020 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
1021 dev_set_name(&client
->dev
, "%d-%04x", i2c_adapter_id(adap
),
1022 client
->addr
| ((client
->flags
& I2C_CLIENT_TEN
)
1027 * i2c_new_device - instantiate an i2c device
1028 * @adap: the adapter managing the device
1029 * @info: describes one I2C device; bus_num is ignored
1030 * Context: can sleep
1032 * Create an i2c device. Binding is handled through driver model
1033 * probe()/remove() methods. A driver may be bound to this device when we
1034 * return from this function, or any later moment (e.g. maybe hotplugging will
1035 * load the driver module). This call is not appropriate for use by mainboard
1036 * initialization logic, which usually runs during an arch_initcall() long
1037 * before any i2c_adapter could exist.
1039 * This returns the new i2c client, which may be saved for later use with
1040 * i2c_unregister_device(); or NULL to indicate an error.
1043 i2c_new_device(struct i2c_adapter
*adap
, struct i2c_board_info
const *info
)
1045 struct i2c_client
*client
;
1048 client
= kzalloc(sizeof *client
, GFP_KERNEL
);
1052 client
->adapter
= adap
;
1054 client
->dev
.platform_data
= info
->platform_data
;
1057 client
->dev
.archdata
= *info
->archdata
;
1059 client
->flags
= info
->flags
;
1060 client
->addr
= info
->addr
;
1061 client
->irq
= info
->irq
;
1063 strlcpy(client
->name
, info
->type
, sizeof(client
->name
));
1065 /* Check for address validity */
1066 status
= i2c_check_client_addr_validity(client
);
1068 dev_err(&adap
->dev
, "Invalid %d-bit I2C address 0x%02hx\n",
1069 client
->flags
& I2C_CLIENT_TEN
? 10 : 7, client
->addr
);
1070 goto out_err_silent
;
1073 /* Check for address business */
1074 status
= i2c_check_addr_busy(adap
, client
->addr
);
1078 client
->dev
.parent
= &client
->adapter
->dev
;
1079 client
->dev
.bus
= &i2c_bus_type
;
1080 client
->dev
.type
= &i2c_client_type
;
1081 client
->dev
.of_node
= info
->of_node
;
1082 ACPI_COMPANION_SET(&client
->dev
, info
->acpi_node
.companion
);
1084 i2c_dev_set_name(adap
, client
);
1085 status
= device_register(&client
->dev
);
1089 dev_dbg(&adap
->dev
, "client [%s] registered with bus id %s\n",
1090 client
->name
, dev_name(&client
->dev
));
1095 dev_err(&adap
->dev
, "Failed to register i2c client %s at 0x%02x "
1096 "(%d)\n", client
->name
, client
->addr
, status
);
1101 EXPORT_SYMBOL_GPL(i2c_new_device
);
1105 * i2c_unregister_device - reverse effect of i2c_new_device()
1106 * @client: value returned from i2c_new_device()
1107 * Context: can sleep
1109 void i2c_unregister_device(struct i2c_client
*client
)
1111 device_unregister(&client
->dev
);
1113 EXPORT_SYMBOL_GPL(i2c_unregister_device
);
1116 static const struct i2c_device_id dummy_id
[] = {
1121 static int dummy_probe(struct i2c_client
*client
,
1122 const struct i2c_device_id
*id
)
1127 static int dummy_remove(struct i2c_client
*client
)
1132 static struct i2c_driver dummy_driver
= {
1133 .driver
.name
= "dummy",
1134 .probe
= dummy_probe
,
1135 .remove
= dummy_remove
,
1136 .id_table
= dummy_id
,
1140 * i2c_new_dummy - return a new i2c device bound to a dummy driver
1141 * @adapter: the adapter managing the device
1142 * @address: seven bit address to be used
1143 * Context: can sleep
1145 * This returns an I2C client bound to the "dummy" driver, intended for use
1146 * with devices that consume multiple addresses. Examples of such chips
1147 * include various EEPROMS (like 24c04 and 24c08 models).
1149 * These dummy devices have two main uses. First, most I2C and SMBus calls
1150 * except i2c_transfer() need a client handle; the dummy will be that handle.
1151 * And second, this prevents the specified address from being bound to a
1154 * This returns the new i2c client, which should be saved for later use with
1155 * i2c_unregister_device(); or NULL to indicate an error.
1157 struct i2c_client
*i2c_new_dummy(struct i2c_adapter
*adapter
, u16 address
)
1159 struct i2c_board_info info
= {
1160 I2C_BOARD_INFO("dummy", address
),
1163 return i2c_new_device(adapter
, &info
);
1165 EXPORT_SYMBOL_GPL(i2c_new_dummy
);
1167 /* ------------------------------------------------------------------------- */
1169 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
1171 static void i2c_adapter_dev_release(struct device
*dev
)
1173 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
1174 complete(&adap
->dev_released
);
1178 * This function is only needed for mutex_lock_nested, so it is never
1179 * called unless locking correctness checking is enabled. Thus we
1180 * make it inline to avoid a compiler warning. That's what gcc ends up
1183 static inline unsigned int i2c_adapter_depth(struct i2c_adapter
*adapter
)
1185 unsigned int depth
= 0;
1187 while ((adapter
= i2c_parent_is_i2c_adapter(adapter
)))
1194 * Let users instantiate I2C devices through sysfs. This can be used when
1195 * platform initialization code doesn't contain the proper data for
1196 * whatever reason. Also useful for drivers that do device detection and
1197 * detection fails, either because the device uses an unexpected address,
1198 * or this is a compatible device with different ID register values.
1200 * Parameter checking may look overzealous, but we really don't want
1201 * the user to provide incorrect parameters.
1204 i2c_sysfs_new_device(struct device
*dev
, struct device_attribute
*attr
,
1205 const char *buf
, size_t count
)
1207 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
1208 struct i2c_board_info info
;
1209 struct i2c_client
*client
;
1213 memset(&info
, 0, sizeof(struct i2c_board_info
));
1215 blank
= strchr(buf
, ' ');
1217 dev_err(dev
, "%s: Missing parameters\n", "new_device");
1220 if (blank
- buf
> I2C_NAME_SIZE
- 1) {
1221 dev_err(dev
, "%s: Invalid device name\n", "new_device");
1224 memcpy(info
.type
, buf
, blank
- buf
);
1226 /* Parse remaining parameters, reject extra parameters */
1227 res
= sscanf(++blank
, "%hi%c", &info
.addr
, &end
);
1229 dev_err(dev
, "%s: Can't parse I2C address\n", "new_device");
1232 if (res
> 1 && end
!= '\n') {
1233 dev_err(dev
, "%s: Extra parameters\n", "new_device");
1237 client
= i2c_new_device(adap
, &info
);
1241 /* Keep track of the added device */
1242 mutex_lock(&adap
->userspace_clients_lock
);
1243 list_add_tail(&client
->detected
, &adap
->userspace_clients
);
1244 mutex_unlock(&adap
->userspace_clients_lock
);
1245 dev_info(dev
, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
1246 info
.type
, info
.addr
);
1252 * And of course let the users delete the devices they instantiated, if
1253 * they got it wrong. This interface can only be used to delete devices
1254 * instantiated by i2c_sysfs_new_device above. This guarantees that we
1255 * don't delete devices to which some kernel code still has references.
1257 * Parameter checking may look overzealous, but we really don't want
1258 * the user to delete the wrong device.
1261 i2c_sysfs_delete_device(struct device
*dev
, struct device_attribute
*attr
,
1262 const char *buf
, size_t count
)
1264 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
1265 struct i2c_client
*client
, *next
;
1266 unsigned short addr
;
1270 /* Parse parameters, reject extra parameters */
1271 res
= sscanf(buf
, "%hi%c", &addr
, &end
);
1273 dev_err(dev
, "%s: Can't parse I2C address\n", "delete_device");
1276 if (res
> 1 && end
!= '\n') {
1277 dev_err(dev
, "%s: Extra parameters\n", "delete_device");
1281 /* Make sure the device was added through sysfs */
1283 mutex_lock_nested(&adap
->userspace_clients_lock
,
1284 i2c_adapter_depth(adap
));
1285 list_for_each_entry_safe(client
, next
, &adap
->userspace_clients
,
1287 if (client
->addr
== addr
) {
1288 dev_info(dev
, "%s: Deleting device %s at 0x%02hx\n",
1289 "delete_device", client
->name
, client
->addr
);
1291 list_del(&client
->detected
);
1292 i2c_unregister_device(client
);
1297 mutex_unlock(&adap
->userspace_clients_lock
);
1300 dev_err(dev
, "%s: Can't find device in list\n",
1305 static DEVICE_ATTR(new_device
, S_IWUSR
, NULL
, i2c_sysfs_new_device
);
1306 static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device
, S_IWUSR
, NULL
,
1307 i2c_sysfs_delete_device
);
1309 static struct attribute
*i2c_adapter_attrs
[] = {
1310 &dev_attr_name
.attr
,
1311 &dev_attr_new_device
.attr
,
1312 &dev_attr_delete_device
.attr
,
1316 static struct attribute_group i2c_adapter_attr_group
= {
1317 .attrs
= i2c_adapter_attrs
,
1320 static const struct attribute_group
*i2c_adapter_attr_groups
[] = {
1321 &i2c_adapter_attr_group
,
1325 struct device_type i2c_adapter_type
= {
1326 .groups
= i2c_adapter_attr_groups
,
1327 .release
= i2c_adapter_dev_release
,
1329 EXPORT_SYMBOL_GPL(i2c_adapter_type
);
1332 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
1333 * @dev: device, probably from some driver model iterator
1335 * When traversing the driver model tree, perhaps using driver model
1336 * iterators like @device_for_each_child(), you can't assume very much
1337 * about the nodes you find. Use this function to avoid oopses caused
1338 * by wrongly treating some non-I2C device as an i2c_adapter.
1340 struct i2c_adapter
*i2c_verify_adapter(struct device
*dev
)
1342 return (dev
->type
== &i2c_adapter_type
)
1343 ? to_i2c_adapter(dev
)
1346 EXPORT_SYMBOL(i2c_verify_adapter
);
1348 #ifdef CONFIG_I2C_COMPAT
1349 static struct class_compat
*i2c_adapter_compat_class
;
1352 static void i2c_scan_static_board_info(struct i2c_adapter
*adapter
)
1354 struct i2c_devinfo
*devinfo
;
1356 down_read(&__i2c_board_lock
);
1357 list_for_each_entry(devinfo
, &__i2c_board_list
, list
) {
1358 if (devinfo
->busnum
== adapter
->nr
1359 && !i2c_new_device(adapter
,
1360 &devinfo
->board_info
))
1361 dev_err(&adapter
->dev
,
1362 "Can't create device at 0x%02x\n",
1363 devinfo
->board_info
.addr
);
1365 up_read(&__i2c_board_lock
);
1368 /* OF support code */
1370 #if IS_ENABLED(CONFIG_OF)
1371 static void of_i2c_register_devices(struct i2c_adapter
*adap
)
1374 struct device_node
*node
;
1376 /* Only register child devices if the adapter has a node pointer set */
1377 if (!adap
->dev
.of_node
)
1380 dev_dbg(&adap
->dev
, "of_i2c: walking child nodes\n");
1382 for_each_available_child_of_node(adap
->dev
.of_node
, node
) {
1383 struct i2c_board_info info
= {};
1384 struct dev_archdata dev_ad
= {};
1388 dev_dbg(&adap
->dev
, "of_i2c: register %s\n", node
->full_name
);
1390 if (of_modalias_node(node
, info
.type
, sizeof(info
.type
)) < 0) {
1391 dev_err(&adap
->dev
, "of_i2c: modalias failure on %s\n",
1396 addr
= of_get_property(node
, "reg", &len
);
1397 if (!addr
|| (len
< sizeof(int))) {
1398 dev_err(&adap
->dev
, "of_i2c: invalid reg on %s\n",
1403 info
.addr
= be32_to_cpup(addr
);
1404 if (info
.addr
> (1 << 10) - 1) {
1405 dev_err(&adap
->dev
, "of_i2c: invalid addr=%x on %s\n",
1406 info
.addr
, node
->full_name
);
1410 info
.irq
= irq_of_parse_and_map(node
, 0);
1411 info
.of_node
= of_node_get(node
);
1412 info
.archdata
= &dev_ad
;
1414 if (of_get_property(node
, "wakeup-source", NULL
))
1415 info
.flags
|= I2C_CLIENT_WAKE
;
1417 request_module("%s%s", I2C_MODULE_PREFIX
, info
.type
);
1419 result
= i2c_new_device(adap
, &info
);
1420 if (result
== NULL
) {
1421 dev_err(&adap
->dev
, "of_i2c: Failure registering %s\n",
1424 irq_dispose_mapping(info
.irq
);
1430 static int of_dev_node_match(struct device
*dev
, void *data
)
1432 return dev
->of_node
== data
;
1435 /* must call put_device() when done with returned i2c_client device */
1436 struct i2c_client
*of_find_i2c_device_by_node(struct device_node
*node
)
1440 dev
= bus_find_device(&i2c_bus_type
, NULL
, node
,
1445 return i2c_verify_client(dev
);
1447 EXPORT_SYMBOL(of_find_i2c_device_by_node
);
1449 /* must call put_device() when done with returned i2c_adapter device */
1450 struct i2c_adapter
*of_find_i2c_adapter_by_node(struct device_node
*node
)
1454 dev
= bus_find_device(&i2c_bus_type
, NULL
, node
,
1459 return i2c_verify_adapter(dev
);
1461 EXPORT_SYMBOL(of_find_i2c_adapter_by_node
);
1463 static void of_i2c_register_devices(struct i2c_adapter
*adap
) { }
1464 #endif /* CONFIG_OF */
1466 static int i2c_do_add_adapter(struct i2c_driver
*driver
,
1467 struct i2c_adapter
*adap
)
1469 /* Detect supported devices on that bus, and instantiate them */
1470 i2c_detect(adap
, driver
);
1472 /* Let legacy drivers scan this bus for matching devices */
1473 if (driver
->attach_adapter
) {
1474 dev_warn(&adap
->dev
, "%s: attach_adapter method is deprecated\n",
1475 driver
->driver
.name
);
1476 dev_warn(&adap
->dev
, "Please use another way to instantiate "
1477 "your i2c_client\n");
1478 /* We ignore the return code; if it fails, too bad */
1479 driver
->attach_adapter(adap
);
1484 static int __process_new_adapter(struct device_driver
*d
, void *data
)
1486 return i2c_do_add_adapter(to_i2c_driver(d
), data
);
1489 static int i2c_register_adapter(struct i2c_adapter
*adap
)
1493 /* Can't register until after driver model init */
1494 if (unlikely(WARN_ON(!i2c_bus_type
.p
))) {
1500 if (unlikely(adap
->name
[0] == '\0')) {
1501 pr_err("i2c-core: Attempt to register an adapter with "
1505 if (unlikely(!adap
->algo
)) {
1506 pr_err("i2c-core: Attempt to register adapter '%s' with "
1507 "no algo!\n", adap
->name
);
1511 rt_mutex_init(&adap
->bus_lock
);
1512 mutex_init(&adap
->userspace_clients_lock
);
1513 INIT_LIST_HEAD(&adap
->userspace_clients
);
1515 /* Set default timeout to 1 second if not already set */
1516 if (adap
->timeout
== 0)
1519 dev_set_name(&adap
->dev
, "i2c-%d", adap
->nr
);
1520 adap
->dev
.bus
= &i2c_bus_type
;
1521 adap
->dev
.type
= &i2c_adapter_type
;
1522 res
= device_register(&adap
->dev
);
1526 dev_dbg(&adap
->dev
, "adapter [%s] registered\n", adap
->name
);
1528 #ifdef CONFIG_I2C_COMPAT
1529 res
= class_compat_create_link(i2c_adapter_compat_class
, &adap
->dev
,
1532 dev_warn(&adap
->dev
,
1533 "Failed to create compatibility class link\n");
1536 /* bus recovery specific initialization */
1537 if (adap
->bus_recovery_info
) {
1538 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
1540 if (!bri
->recover_bus
) {
1541 dev_err(&adap
->dev
, "No recover_bus() found, not using recovery\n");
1542 adap
->bus_recovery_info
= NULL
;
1546 /* Generic GPIO recovery */
1547 if (bri
->recover_bus
== i2c_generic_gpio_recovery
) {
1548 if (!gpio_is_valid(bri
->scl_gpio
)) {
1549 dev_err(&adap
->dev
, "Invalid SCL gpio, not using recovery\n");
1550 adap
->bus_recovery_info
= NULL
;
1554 if (gpio_is_valid(bri
->sda_gpio
))
1555 bri
->get_sda
= get_sda_gpio_value
;
1557 bri
->get_sda
= NULL
;
1559 bri
->get_scl
= get_scl_gpio_value
;
1560 bri
->set_scl
= set_scl_gpio_value
;
1561 } else if (!bri
->set_scl
|| !bri
->get_scl
) {
1562 /* Generic SCL recovery */
1563 dev_err(&adap
->dev
, "No {get|set}_gpio() found, not using recovery\n");
1564 adap
->bus_recovery_info
= NULL
;
1569 /* create pre-declared device nodes */
1570 of_i2c_register_devices(adap
);
1571 acpi_i2c_register_devices(adap
);
1572 acpi_i2c_install_space_handler(adap
);
1574 if (adap
->nr
< __i2c_first_dynamic_bus_num
)
1575 i2c_scan_static_board_info(adap
);
1577 /* Notify drivers */
1578 mutex_lock(&core_lock
);
1579 bus_for_each_drv(&i2c_bus_type
, NULL
, adap
, __process_new_adapter
);
1580 mutex_unlock(&core_lock
);
1585 mutex_lock(&core_lock
);
1586 idr_remove(&i2c_adapter_idr
, adap
->nr
);
1587 mutex_unlock(&core_lock
);
1592 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1593 * @adap: the adapter to register (with adap->nr initialized)
1594 * Context: can sleep
1596 * See i2c_add_numbered_adapter() for details.
1598 static int __i2c_add_numbered_adapter(struct i2c_adapter
*adap
)
1602 mutex_lock(&core_lock
);
1603 id
= idr_alloc(&i2c_adapter_idr
, adap
, adap
->nr
, adap
->nr
+ 1,
1605 mutex_unlock(&core_lock
);
1607 return id
== -ENOSPC
? -EBUSY
: id
;
1609 return i2c_register_adapter(adap
);
1613 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1614 * @adapter: the adapter to add
1615 * Context: can sleep
1617 * This routine is used to declare an I2C adapter when its bus number
1618 * doesn't matter or when its bus number is specified by an dt alias.
1619 * Examples of bases when the bus number doesn't matter: I2C adapters
1620 * dynamically added by USB links or PCI plugin cards.
1622 * When this returns zero, a new bus number was allocated and stored
1623 * in adap->nr, and the specified adapter became available for clients.
1624 * Otherwise, a negative errno value is returned.
1626 int i2c_add_adapter(struct i2c_adapter
*adapter
)
1628 struct device
*dev
= &adapter
->dev
;
1632 id
= of_alias_get_id(dev
->of_node
, "i2c");
1635 return __i2c_add_numbered_adapter(adapter
);
1639 mutex_lock(&core_lock
);
1640 id
= idr_alloc(&i2c_adapter_idr
, adapter
,
1641 __i2c_first_dynamic_bus_num
, 0, GFP_KERNEL
);
1642 mutex_unlock(&core_lock
);
1648 return i2c_register_adapter(adapter
);
1650 EXPORT_SYMBOL(i2c_add_adapter
);
1653 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1654 * @adap: the adapter to register (with adap->nr initialized)
1655 * Context: can sleep
1657 * This routine is used to declare an I2C adapter when its bus number
1658 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1659 * or otherwise built in to the system's mainboard, and where i2c_board_info
1660 * is used to properly configure I2C devices.
1662 * If the requested bus number is set to -1, then this function will behave
1663 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1665 * If no devices have pre-been declared for this bus, then be sure to
1666 * register the adapter before any dynamically allocated ones. Otherwise
1667 * the required bus ID may not be available.
1669 * When this returns zero, the specified adapter became available for
1670 * clients using the bus number provided in adap->nr. Also, the table
1671 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1672 * and the appropriate driver model device nodes are created. Otherwise, a
1673 * negative errno value is returned.
1675 int i2c_add_numbered_adapter(struct i2c_adapter
*adap
)
1677 if (adap
->nr
== -1) /* -1 means dynamically assign bus id */
1678 return i2c_add_adapter(adap
);
1680 return __i2c_add_numbered_adapter(adap
);
1682 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter
);
1684 static void i2c_do_del_adapter(struct i2c_driver
*driver
,
1685 struct i2c_adapter
*adapter
)
1687 struct i2c_client
*client
, *_n
;
1689 /* Remove the devices we created ourselves as the result of hardware
1690 * probing (using a driver's detect method) */
1691 list_for_each_entry_safe(client
, _n
, &driver
->clients
, detected
) {
1692 if (client
->adapter
== adapter
) {
1693 dev_dbg(&adapter
->dev
, "Removing %s at 0x%x\n",
1694 client
->name
, client
->addr
);
1695 list_del(&client
->detected
);
1696 i2c_unregister_device(client
);
1701 static int __unregister_client(struct device
*dev
, void *dummy
)
1703 struct i2c_client
*client
= i2c_verify_client(dev
);
1704 if (client
&& strcmp(client
->name
, "dummy"))
1705 i2c_unregister_device(client
);
1709 static int __unregister_dummy(struct device
*dev
, void *dummy
)
1711 struct i2c_client
*client
= i2c_verify_client(dev
);
1713 i2c_unregister_device(client
);
1717 static int __process_removed_adapter(struct device_driver
*d
, void *data
)
1719 i2c_do_del_adapter(to_i2c_driver(d
), data
);
1724 * i2c_del_adapter - unregister I2C adapter
1725 * @adap: the adapter being unregistered
1726 * Context: can sleep
1728 * This unregisters an I2C adapter which was previously registered
1729 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1731 void i2c_del_adapter(struct i2c_adapter
*adap
)
1733 struct i2c_adapter
*found
;
1734 struct i2c_client
*client
, *next
;
1736 /* First make sure that this adapter was ever added */
1737 mutex_lock(&core_lock
);
1738 found
= idr_find(&i2c_adapter_idr
, adap
->nr
);
1739 mutex_unlock(&core_lock
);
1740 if (found
!= adap
) {
1741 pr_debug("i2c-core: attempting to delete unregistered "
1742 "adapter [%s]\n", adap
->name
);
1746 acpi_i2c_remove_space_handler(adap
);
1747 /* Tell drivers about this removal */
1748 mutex_lock(&core_lock
);
1749 bus_for_each_drv(&i2c_bus_type
, NULL
, adap
,
1750 __process_removed_adapter
);
1751 mutex_unlock(&core_lock
);
1753 /* Remove devices instantiated from sysfs */
1754 mutex_lock_nested(&adap
->userspace_clients_lock
,
1755 i2c_adapter_depth(adap
));
1756 list_for_each_entry_safe(client
, next
, &adap
->userspace_clients
,
1758 dev_dbg(&adap
->dev
, "Removing %s at 0x%x\n", client
->name
,
1760 list_del(&client
->detected
);
1761 i2c_unregister_device(client
);
1763 mutex_unlock(&adap
->userspace_clients_lock
);
1765 /* Detach any active clients. This can't fail, thus we do not
1766 * check the returned value. This is a two-pass process, because
1767 * we can't remove the dummy devices during the first pass: they
1768 * could have been instantiated by real devices wishing to clean
1769 * them up properly, so we give them a chance to do that first. */
1770 device_for_each_child(&adap
->dev
, NULL
, __unregister_client
);
1771 device_for_each_child(&adap
->dev
, NULL
, __unregister_dummy
);
1773 #ifdef CONFIG_I2C_COMPAT
1774 class_compat_remove_link(i2c_adapter_compat_class
, &adap
->dev
,
1778 /* device name is gone after device_unregister */
1779 dev_dbg(&adap
->dev
, "adapter [%s] unregistered\n", adap
->name
);
1781 /* clean up the sysfs representation */
1782 init_completion(&adap
->dev_released
);
1783 device_unregister(&adap
->dev
);
1785 /* wait for sysfs to drop all references */
1786 wait_for_completion(&adap
->dev_released
);
1789 mutex_lock(&core_lock
);
1790 idr_remove(&i2c_adapter_idr
, adap
->nr
);
1791 mutex_unlock(&core_lock
);
1793 /* Clear the device structure in case this adapter is ever going to be
1795 memset(&adap
->dev
, 0, sizeof(adap
->dev
));
1797 EXPORT_SYMBOL(i2c_del_adapter
);
1799 /* ------------------------------------------------------------------------- */
1801 int i2c_for_each_dev(void *data
, int (*fn
)(struct device
*, void *))
1805 mutex_lock(&core_lock
);
1806 res
= bus_for_each_dev(&i2c_bus_type
, NULL
, data
, fn
);
1807 mutex_unlock(&core_lock
);
1811 EXPORT_SYMBOL_GPL(i2c_for_each_dev
);
1813 static int __process_new_driver(struct device
*dev
, void *data
)
1815 if (dev
->type
!= &i2c_adapter_type
)
1817 return i2c_do_add_adapter(data
, to_i2c_adapter(dev
));
1821 * An i2c_driver is used with one or more i2c_client (device) nodes to access
1822 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1825 int i2c_register_driver(struct module
*owner
, struct i2c_driver
*driver
)
1829 /* Can't register until after driver model init */
1830 if (unlikely(WARN_ON(!i2c_bus_type
.p
)))
1833 /* add the driver to the list of i2c drivers in the driver core */
1834 driver
->driver
.owner
= owner
;
1835 driver
->driver
.bus
= &i2c_bus_type
;
1837 /* When registration returns, the driver core
1838 * will have called probe() for all matching-but-unbound devices.
1840 res
= driver_register(&driver
->driver
);
1844 /* Drivers should switch to dev_pm_ops instead. */
1845 if (driver
->suspend
)
1846 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1847 driver
->driver
.name
);
1849 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1850 driver
->driver
.name
);
1852 pr_debug("i2c-core: driver [%s] registered\n", driver
->driver
.name
);
1854 INIT_LIST_HEAD(&driver
->clients
);
1855 /* Walk the adapters that are already present */
1856 i2c_for_each_dev(driver
, __process_new_driver
);
1860 EXPORT_SYMBOL(i2c_register_driver
);
1862 static int __process_removed_driver(struct device
*dev
, void *data
)
1864 if (dev
->type
== &i2c_adapter_type
)
1865 i2c_do_del_adapter(data
, to_i2c_adapter(dev
));
1870 * i2c_del_driver - unregister I2C driver
1871 * @driver: the driver being unregistered
1872 * Context: can sleep
1874 void i2c_del_driver(struct i2c_driver
*driver
)
1876 i2c_for_each_dev(driver
, __process_removed_driver
);
1878 driver_unregister(&driver
->driver
);
1879 pr_debug("i2c-core: driver [%s] unregistered\n", driver
->driver
.name
);
1881 EXPORT_SYMBOL(i2c_del_driver
);
1883 /* ------------------------------------------------------------------------- */
1886 * i2c_use_client - increments the reference count of the i2c client structure
1887 * @client: the client being referenced
1889 * Each live reference to a client should be refcounted. The driver model does
1890 * that automatically as part of driver binding, so that most drivers don't
1891 * need to do this explicitly: they hold a reference until they're unbound
1894 * A pointer to the client with the incremented reference counter is returned.
1896 struct i2c_client
*i2c_use_client(struct i2c_client
*client
)
1898 if (client
&& get_device(&client
->dev
))
1902 EXPORT_SYMBOL(i2c_use_client
);
1905 * i2c_release_client - release a use of the i2c client structure
1906 * @client: the client being no longer referenced
1908 * Must be called when a user of a client is finished with it.
1910 void i2c_release_client(struct i2c_client
*client
)
1913 put_device(&client
->dev
);
1915 EXPORT_SYMBOL(i2c_release_client
);
1917 struct i2c_cmd_arg
{
1922 static int i2c_cmd(struct device
*dev
, void *_arg
)
1924 struct i2c_client
*client
= i2c_verify_client(dev
);
1925 struct i2c_cmd_arg
*arg
= _arg
;
1926 struct i2c_driver
*driver
;
1928 if (!client
|| !client
->dev
.driver
)
1931 driver
= to_i2c_driver(client
->dev
.driver
);
1932 if (driver
->command
)
1933 driver
->command(client
, arg
->cmd
, arg
->arg
);
1937 void i2c_clients_command(struct i2c_adapter
*adap
, unsigned int cmd
, void *arg
)
1939 struct i2c_cmd_arg cmd_arg
;
1943 device_for_each_child(&adap
->dev
, &cmd_arg
, i2c_cmd
);
1945 EXPORT_SYMBOL(i2c_clients_command
);
1947 static int __init
i2c_init(void)
1951 retval
= bus_register(&i2c_bus_type
);
1954 #ifdef CONFIG_I2C_COMPAT
1955 i2c_adapter_compat_class
= class_compat_register("i2c-adapter");
1956 if (!i2c_adapter_compat_class
) {
1961 retval
= i2c_add_driver(&dummy_driver
);
1967 #ifdef CONFIG_I2C_COMPAT
1968 class_compat_unregister(i2c_adapter_compat_class
);
1971 bus_unregister(&i2c_bus_type
);
1975 static void __exit
i2c_exit(void)
1977 i2c_del_driver(&dummy_driver
);
1978 #ifdef CONFIG_I2C_COMPAT
1979 class_compat_unregister(i2c_adapter_compat_class
);
1981 bus_unregister(&i2c_bus_type
);
1982 tracepoint_synchronize_unregister();
1985 /* We must initialize early, because some subsystems register i2c drivers
1986 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1988 postcore_initcall(i2c_init
);
1989 module_exit(i2c_exit
);
1991 /* ----------------------------------------------------
1992 * the functional interface to the i2c busses.
1993 * ----------------------------------------------------
1997 * __i2c_transfer - unlocked flavor of i2c_transfer
1998 * @adap: Handle to I2C bus
1999 * @msgs: One or more messages to execute before STOP is issued to
2000 * terminate the operation; each message begins with a START.
2001 * @num: Number of messages to be executed.
2003 * Returns negative errno, else the number of messages executed.
2005 * Adapter lock must be held when calling this function. No debug logging
2006 * takes place. adap->algo->master_xfer existence isn't checked.
2008 int __i2c_transfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
2010 unsigned long orig_jiffies
;
2013 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
2014 * enabled. This is an efficient way of keeping the for-loop from
2015 * being executed when not needed.
2017 if (static_key_false(&i2c_trace_msg
)) {
2019 for (i
= 0; i
< num
; i
++)
2020 if (msgs
[i
].flags
& I2C_M_RD
)
2021 trace_i2c_read(adap
, &msgs
[i
], i
);
2023 trace_i2c_write(adap
, &msgs
[i
], i
);
2026 /* Retry automatically on arbitration loss */
2027 orig_jiffies
= jiffies
;
2028 for (ret
= 0, try = 0; try <= adap
->retries
; try++) {
2029 ret
= adap
->algo
->master_xfer(adap
, msgs
, num
);
2032 if (time_after(jiffies
, orig_jiffies
+ adap
->timeout
))
2036 if (static_key_false(&i2c_trace_msg
)) {
2038 for (i
= 0; i
< ret
; i
++)
2039 if (msgs
[i
].flags
& I2C_M_RD
)
2040 trace_i2c_reply(adap
, &msgs
[i
], i
);
2041 trace_i2c_result(adap
, i
, ret
);
2046 EXPORT_SYMBOL(__i2c_transfer
);
2049 * i2c_transfer - execute a single or combined I2C message
2050 * @adap: Handle to I2C bus
2051 * @msgs: One or more messages to execute before STOP is issued to
2052 * terminate the operation; each message begins with a START.
2053 * @num: Number of messages to be executed.
2055 * Returns negative errno, else the number of messages executed.
2057 * Note that there is no requirement that each message be sent to
2058 * the same slave address, although that is the most common model.
2060 int i2c_transfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
2064 /* REVISIT the fault reporting model here is weak:
2066 * - When we get an error after receiving N bytes from a slave,
2067 * there is no way to report "N".
2069 * - When we get a NAK after transmitting N bytes to a slave,
2070 * there is no way to report "N" ... or to let the master
2071 * continue executing the rest of this combined message, if
2072 * that's the appropriate response.
2074 * - When for example "num" is two and we successfully complete
2075 * the first message but get an error part way through the
2076 * second, it's unclear whether that should be reported as
2077 * one (discarding status on the second message) or errno
2078 * (discarding status on the first one).
2081 if (adap
->algo
->master_xfer
) {
2083 for (ret
= 0; ret
< num
; ret
++) {
2084 dev_dbg(&adap
->dev
, "master_xfer[%d] %c, addr=0x%02x, "
2085 "len=%d%s\n", ret
, (msgs
[ret
].flags
& I2C_M_RD
)
2086 ? 'R' : 'W', msgs
[ret
].addr
, msgs
[ret
].len
,
2087 (msgs
[ret
].flags
& I2C_M_RECV_LEN
) ? "+" : "");
2091 if (in_atomic() || irqs_disabled()) {
2092 ret
= i2c_trylock_adapter(adap
);
2094 /* I2C activity is ongoing. */
2097 i2c_lock_adapter(adap
);
2100 ret
= __i2c_transfer(adap
, msgs
, num
);
2101 i2c_unlock_adapter(adap
);
2105 dev_dbg(&adap
->dev
, "I2C level transfers not supported\n");
2109 EXPORT_SYMBOL(i2c_transfer
);
2112 * i2c_master_send - issue a single I2C message in master transmit mode
2113 * @client: Handle to slave device
2114 * @buf: Data that will be written to the slave
2115 * @count: How many bytes to write, must be less than 64k since msg.len is u16
2117 * Returns negative errno, or else the number of bytes written.
2119 int i2c_master_send(const struct i2c_client
*client
, const char *buf
, int count
)
2122 struct i2c_adapter
*adap
= client
->adapter
;
2125 msg
.addr
= client
->addr
;
2126 msg
.flags
= client
->flags
& I2C_M_TEN
;
2128 msg
.buf
= (char *)buf
;
2130 ret
= i2c_transfer(adap
, &msg
, 1);
2133 * If everything went ok (i.e. 1 msg transmitted), return #bytes
2134 * transmitted, else error code.
2136 return (ret
== 1) ? count
: ret
;
2138 EXPORT_SYMBOL(i2c_master_send
);
2141 * i2c_master_recv - issue a single I2C message in master receive mode
2142 * @client: Handle to slave device
2143 * @buf: Where to store data read from slave
2144 * @count: How many bytes to read, must be less than 64k since msg.len is u16
2146 * Returns negative errno, or else the number of bytes read.
2148 int i2c_master_recv(const struct i2c_client
*client
, char *buf
, int count
)
2150 struct i2c_adapter
*adap
= client
->adapter
;
2154 msg
.addr
= client
->addr
;
2155 msg
.flags
= client
->flags
& I2C_M_TEN
;
2156 msg
.flags
|= I2C_M_RD
;
2160 ret
= i2c_transfer(adap
, &msg
, 1);
2163 * If everything went ok (i.e. 1 msg received), return #bytes received,
2166 return (ret
== 1) ? count
: ret
;
2168 EXPORT_SYMBOL(i2c_master_recv
);
2170 /* ----------------------------------------------------
2171 * the i2c address scanning function
2172 * Will not work for 10-bit addresses!
2173 * ----------------------------------------------------
2177 * Legacy default probe function, mostly relevant for SMBus. The default
2178 * probe method is a quick write, but it is known to corrupt the 24RF08
2179 * EEPROMs due to a state machine bug, and could also irreversibly
2180 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
2181 * we use a short byte read instead. Also, some bus drivers don't implement
2182 * quick write, so we fallback to a byte read in that case too.
2183 * On x86, there is another special case for FSC hardware monitoring chips,
2184 * which want regular byte reads (address 0x73.) Fortunately, these are the
2185 * only known chips using this I2C address on PC hardware.
2186 * Returns 1 if probe succeeded, 0 if not.
2188 static int i2c_default_probe(struct i2c_adapter
*adap
, unsigned short addr
)
2191 union i2c_smbus_data dummy
;
2194 if (addr
== 0x73 && (adap
->class & I2C_CLASS_HWMON
)
2195 && i2c_check_functionality(adap
, I2C_FUNC_SMBUS_READ_BYTE_DATA
))
2196 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
2197 I2C_SMBUS_BYTE_DATA
, &dummy
);
2200 if (!((addr
& ~0x07) == 0x30 || (addr
& ~0x0f) == 0x50)
2201 && i2c_check_functionality(adap
, I2C_FUNC_SMBUS_QUICK
))
2202 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_WRITE
, 0,
2203 I2C_SMBUS_QUICK
, NULL
);
2204 else if (i2c_check_functionality(adap
, I2C_FUNC_SMBUS_READ_BYTE
))
2205 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
2206 I2C_SMBUS_BYTE
, &dummy
);
2208 dev_warn(&adap
->dev
, "No suitable probing method supported for address 0x%02X\n",
2216 static int i2c_detect_address(struct i2c_client
*temp_client
,
2217 struct i2c_driver
*driver
)
2219 struct i2c_board_info info
;
2220 struct i2c_adapter
*adapter
= temp_client
->adapter
;
2221 int addr
= temp_client
->addr
;
2224 /* Make sure the address is valid */
2225 err
= i2c_check_addr_validity(addr
);
2227 dev_warn(&adapter
->dev
, "Invalid probe address 0x%02x\n",
2232 /* Skip if already in use */
2233 if (i2c_check_addr_busy(adapter
, addr
))
2236 /* Make sure there is something at this address */
2237 if (!i2c_default_probe(adapter
, addr
))
2240 /* Finally call the custom detection function */
2241 memset(&info
, 0, sizeof(struct i2c_board_info
));
2243 err
= driver
->detect(temp_client
, &info
);
2245 /* -ENODEV is returned if the detection fails. We catch it
2246 here as this isn't an error. */
2247 return err
== -ENODEV
? 0 : err
;
2250 /* Consistency check */
2251 if (info
.type
[0] == '\0') {
2252 dev_err(&adapter
->dev
, "%s detection function provided "
2253 "no name for 0x%x\n", driver
->driver
.name
,
2256 struct i2c_client
*client
;
2258 /* Detection succeeded, instantiate the device */
2259 if (adapter
->class & I2C_CLASS_DEPRECATED
)
2260 dev_warn(&adapter
->dev
,
2261 "This adapter will soon drop class based instantiation of devices. "
2262 "Please make sure client 0x%02x gets instantiated by other means. "
2263 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
2266 dev_dbg(&adapter
->dev
, "Creating %s at 0x%02x\n",
2267 info
.type
, info
.addr
);
2268 client
= i2c_new_device(adapter
, &info
);
2270 list_add_tail(&client
->detected
, &driver
->clients
);
2272 dev_err(&adapter
->dev
, "Failed creating %s at 0x%02x\n",
2273 info
.type
, info
.addr
);
2278 static int i2c_detect(struct i2c_adapter
*adapter
, struct i2c_driver
*driver
)
2280 const unsigned short *address_list
;
2281 struct i2c_client
*temp_client
;
2283 int adap_id
= i2c_adapter_id(adapter
);
2285 address_list
= driver
->address_list
;
2286 if (!driver
->detect
|| !address_list
)
2289 /* Warn that the adapter lost class based instantiation */
2290 if (adapter
->class == I2C_CLASS_DEPRECATED
) {
2291 dev_dbg(&adapter
->dev
,
2292 "This adapter dropped support for I2C classes and "
2293 "won't auto-detect %s devices anymore. If you need it, check "
2294 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
2295 driver
->driver
.name
);
2299 /* Stop here if the classes do not match */
2300 if (!(adapter
->class & driver
->class))
2303 /* Set up a temporary client to help detect callback */
2304 temp_client
= kzalloc(sizeof(struct i2c_client
), GFP_KERNEL
);
2307 temp_client
->adapter
= adapter
;
2309 for (i
= 0; address_list
[i
] != I2C_CLIENT_END
; i
+= 1) {
2310 dev_dbg(&adapter
->dev
, "found normal entry for adapter %d, "
2311 "addr 0x%02x\n", adap_id
, address_list
[i
]);
2312 temp_client
->addr
= address_list
[i
];
2313 err
= i2c_detect_address(temp_client
, driver
);
2322 int i2c_probe_func_quick_read(struct i2c_adapter
*adap
, unsigned short addr
)
2324 return i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
2325 I2C_SMBUS_QUICK
, NULL
) >= 0;
2327 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read
);
2330 i2c_new_probed_device(struct i2c_adapter
*adap
,
2331 struct i2c_board_info
*info
,
2332 unsigned short const *addr_list
,
2333 int (*probe
)(struct i2c_adapter
*, unsigned short addr
))
2338 probe
= i2c_default_probe
;
2340 for (i
= 0; addr_list
[i
] != I2C_CLIENT_END
; i
++) {
2341 /* Check address validity */
2342 if (i2c_check_addr_validity(addr_list
[i
]) < 0) {
2343 dev_warn(&adap
->dev
, "Invalid 7-bit address "
2344 "0x%02x\n", addr_list
[i
]);
2348 /* Check address availability */
2349 if (i2c_check_addr_busy(adap
, addr_list
[i
])) {
2350 dev_dbg(&adap
->dev
, "Address 0x%02x already in "
2351 "use, not probing\n", addr_list
[i
]);
2355 /* Test address responsiveness */
2356 if (probe(adap
, addr_list
[i
]))
2360 if (addr_list
[i
] == I2C_CLIENT_END
) {
2361 dev_dbg(&adap
->dev
, "Probing failed, no device found\n");
2365 info
->addr
= addr_list
[i
];
2366 return i2c_new_device(adap
, info
);
2368 EXPORT_SYMBOL_GPL(i2c_new_probed_device
);
2370 struct i2c_adapter
*i2c_get_adapter(int nr
)
2372 struct i2c_adapter
*adapter
;
2374 mutex_lock(&core_lock
);
2375 adapter
= idr_find(&i2c_adapter_idr
, nr
);
2376 if (adapter
&& !try_module_get(adapter
->owner
))
2379 mutex_unlock(&core_lock
);
2382 EXPORT_SYMBOL(i2c_get_adapter
);
2384 void i2c_put_adapter(struct i2c_adapter
*adap
)
2387 module_put(adap
->owner
);
2389 EXPORT_SYMBOL(i2c_put_adapter
);
2391 /* The SMBus parts */
2393 #define POLY (0x1070U << 3)
2394 static u8
crc8(u16 data
)
2398 for (i
= 0; i
< 8; i
++) {
2403 return (u8
)(data
>> 8);
2406 /* Incremental CRC8 over count bytes in the array pointed to by p */
2407 static u8
i2c_smbus_pec(u8 crc
, u8
*p
, size_t count
)
2411 for (i
= 0; i
< count
; i
++)
2412 crc
= crc8((crc
^ p
[i
]) << 8);
2416 /* Assume a 7-bit address, which is reasonable for SMBus */
2417 static u8
i2c_smbus_msg_pec(u8 pec
, struct i2c_msg
*msg
)
2419 /* The address will be sent first */
2420 u8 addr
= (msg
->addr
<< 1) | !!(msg
->flags
& I2C_M_RD
);
2421 pec
= i2c_smbus_pec(pec
, &addr
, 1);
2423 /* The data buffer follows */
2424 return i2c_smbus_pec(pec
, msg
->buf
, msg
->len
);
2427 /* Used for write only transactions */
2428 static inline void i2c_smbus_add_pec(struct i2c_msg
*msg
)
2430 msg
->buf
[msg
->len
] = i2c_smbus_msg_pec(0, msg
);
2434 /* Return <0 on CRC error
2435 If there was a write before this read (most cases) we need to take the
2436 partial CRC from the write part into account.
2437 Note that this function does modify the message (we need to decrease the
2438 message length to hide the CRC byte from the caller). */
2439 static int i2c_smbus_check_pec(u8 cpec
, struct i2c_msg
*msg
)
2441 u8 rpec
= msg
->buf
[--msg
->len
];
2442 cpec
= i2c_smbus_msg_pec(cpec
, msg
);
2445 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2453 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2454 * @client: Handle to slave device
2456 * This executes the SMBus "receive byte" protocol, returning negative errno
2457 * else the byte received from the device.
2459 s32
i2c_smbus_read_byte(const struct i2c_client
*client
)
2461 union i2c_smbus_data data
;
2464 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2466 I2C_SMBUS_BYTE
, &data
);
2467 return (status
< 0) ? status
: data
.byte
;
2469 EXPORT_SYMBOL(i2c_smbus_read_byte
);
2472 * i2c_smbus_write_byte - SMBus "send byte" protocol
2473 * @client: Handle to slave device
2474 * @value: Byte to be sent
2476 * This executes the SMBus "send byte" protocol, returning negative errno
2477 * else zero on success.
2479 s32
i2c_smbus_write_byte(const struct i2c_client
*client
, u8 value
)
2481 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2482 I2C_SMBUS_WRITE
, value
, I2C_SMBUS_BYTE
, NULL
);
2484 EXPORT_SYMBOL(i2c_smbus_write_byte
);
2487 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2488 * @client: Handle to slave device
2489 * @command: Byte interpreted by slave
2491 * This executes the SMBus "read byte" protocol, returning negative errno
2492 * else a data byte received from the device.
2494 s32
i2c_smbus_read_byte_data(const struct i2c_client
*client
, u8 command
)
2496 union i2c_smbus_data data
;
2499 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2500 I2C_SMBUS_READ
, command
,
2501 I2C_SMBUS_BYTE_DATA
, &data
);
2502 return (status
< 0) ? status
: data
.byte
;
2504 EXPORT_SYMBOL(i2c_smbus_read_byte_data
);
2507 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2508 * @client: Handle to slave device
2509 * @command: Byte interpreted by slave
2510 * @value: Byte being written
2512 * This executes the SMBus "write byte" protocol, returning negative errno
2513 * else zero on success.
2515 s32
i2c_smbus_write_byte_data(const struct i2c_client
*client
, u8 command
,
2518 union i2c_smbus_data data
;
2520 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2521 I2C_SMBUS_WRITE
, command
,
2522 I2C_SMBUS_BYTE_DATA
, &data
);
2524 EXPORT_SYMBOL(i2c_smbus_write_byte_data
);
2527 * i2c_smbus_read_word_data - SMBus "read word" protocol
2528 * @client: Handle to slave device
2529 * @command: Byte interpreted by slave
2531 * This executes the SMBus "read word" protocol, returning negative errno
2532 * else a 16-bit unsigned "word" received from the device.
2534 s32
i2c_smbus_read_word_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_WORD_DATA
, &data
);
2542 return (status
< 0) ? status
: data
.word
;
2544 EXPORT_SYMBOL(i2c_smbus_read_word_data
);
2547 * i2c_smbus_write_word_data - SMBus "write word" protocol
2548 * @client: Handle to slave device
2549 * @command: Byte interpreted by slave
2550 * @value: 16-bit "word" being written
2552 * This executes the SMBus "write word" protocol, returning negative errno
2553 * else zero on success.
2555 s32
i2c_smbus_write_word_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_WORD_DATA
, &data
);
2564 EXPORT_SYMBOL(i2c_smbus_write_word_data
);
2567 * i2c_smbus_read_block_data - SMBus "block read" protocol
2568 * @client: Handle to slave device
2569 * @command: Byte interpreted by slave
2570 * @values: Byte array into which data will be read; big enough to hold
2571 * the data returned by the slave. SMBus allows at most 32 bytes.
2573 * This executes the SMBus "block read" protocol, returning negative errno
2574 * else the number of data bytes in the slave's response.
2576 * Note that using this function requires that the client's adapter support
2577 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2578 * support this; its emulation through I2C messaging relies on a specific
2579 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2581 s32
i2c_smbus_read_block_data(const struct i2c_client
*client
, u8 command
,
2584 union i2c_smbus_data data
;
2587 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2588 I2C_SMBUS_READ
, command
,
2589 I2C_SMBUS_BLOCK_DATA
, &data
);
2593 memcpy(values
, &data
.block
[1], data
.block
[0]);
2594 return data
.block
[0];
2596 EXPORT_SYMBOL(i2c_smbus_read_block_data
);
2599 * i2c_smbus_write_block_data - SMBus "block write" protocol
2600 * @client: Handle to slave device
2601 * @command: Byte interpreted by slave
2602 * @length: Size of data block; SMBus allows at most 32 bytes
2603 * @values: Byte array which will be written.
2605 * This executes the SMBus "block write" protocol, returning negative errno
2606 * else zero on success.
2608 s32
i2c_smbus_write_block_data(const struct i2c_client
*client
, u8 command
,
2609 u8 length
, const u8
*values
)
2611 union i2c_smbus_data data
;
2613 if (length
> I2C_SMBUS_BLOCK_MAX
)
2614 length
= I2C_SMBUS_BLOCK_MAX
;
2615 data
.block
[0] = length
;
2616 memcpy(&data
.block
[1], values
, length
);
2617 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2618 I2C_SMBUS_WRITE
, command
,
2619 I2C_SMBUS_BLOCK_DATA
, &data
);
2621 EXPORT_SYMBOL(i2c_smbus_write_block_data
);
2623 /* Returns the number of read bytes */
2624 s32
i2c_smbus_read_i2c_block_data(const struct i2c_client
*client
, u8 command
,
2625 u8 length
, u8
*values
)
2627 union i2c_smbus_data data
;
2630 if (length
> I2C_SMBUS_BLOCK_MAX
)
2631 length
= I2C_SMBUS_BLOCK_MAX
;
2632 data
.block
[0] = length
;
2633 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2634 I2C_SMBUS_READ
, command
,
2635 I2C_SMBUS_I2C_BLOCK_DATA
, &data
);
2639 memcpy(values
, &data
.block
[1], data
.block
[0]);
2640 return data
.block
[0];
2642 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data
);
2644 s32
i2c_smbus_write_i2c_block_data(const struct i2c_client
*client
, u8 command
,
2645 u8 length
, const u8
*values
)
2647 union i2c_smbus_data data
;
2649 if (length
> I2C_SMBUS_BLOCK_MAX
)
2650 length
= I2C_SMBUS_BLOCK_MAX
;
2651 data
.block
[0] = length
;
2652 memcpy(data
.block
+ 1, values
, length
);
2653 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2654 I2C_SMBUS_WRITE
, command
,
2655 I2C_SMBUS_I2C_BLOCK_DATA
, &data
);
2657 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data
);
2659 /* Simulate a SMBus command using the i2c protocol
2660 No checking of parameters is done! */
2661 static s32
i2c_smbus_xfer_emulated(struct i2c_adapter
*adapter
, u16 addr
,
2662 unsigned short flags
,
2663 char read_write
, u8 command
, int size
,
2664 union i2c_smbus_data
*data
)
2666 /* So we need to generate a series of msgs. In the case of writing, we
2667 need to use only one message; when reading, we need two. We initialize
2668 most things with sane defaults, to keep the code below somewhat
2670 unsigned char msgbuf0
[I2C_SMBUS_BLOCK_MAX
+3];
2671 unsigned char msgbuf1
[I2C_SMBUS_BLOCK_MAX
+2];
2672 int num
= read_write
== I2C_SMBUS_READ
? 2 : 1;
2676 struct i2c_msg msg
[2] = {
2684 .flags
= flags
| I2C_M_RD
,
2690 msgbuf0
[0] = command
;
2692 case I2C_SMBUS_QUICK
:
2694 /* Special case: The read/write field is used as data */
2695 msg
[0].flags
= flags
| (read_write
== I2C_SMBUS_READ
?
2699 case I2C_SMBUS_BYTE
:
2700 if (read_write
== I2C_SMBUS_READ
) {
2701 /* Special case: only a read! */
2702 msg
[0].flags
= I2C_M_RD
| flags
;
2706 case I2C_SMBUS_BYTE_DATA
:
2707 if (read_write
== I2C_SMBUS_READ
)
2711 msgbuf0
[1] = data
->byte
;
2714 case I2C_SMBUS_WORD_DATA
:
2715 if (read_write
== I2C_SMBUS_READ
)
2719 msgbuf0
[1] = data
->word
& 0xff;
2720 msgbuf0
[2] = data
->word
>> 8;
2723 case I2C_SMBUS_PROC_CALL
:
2724 num
= 2; /* Special case */
2725 read_write
= I2C_SMBUS_READ
;
2728 msgbuf0
[1] = data
->word
& 0xff;
2729 msgbuf0
[2] = data
->word
>> 8;
2731 case I2C_SMBUS_BLOCK_DATA
:
2732 if (read_write
== I2C_SMBUS_READ
) {
2733 msg
[1].flags
|= I2C_M_RECV_LEN
;
2734 msg
[1].len
= 1; /* block length will be added by
2735 the underlying bus driver */
2737 msg
[0].len
= data
->block
[0] + 2;
2738 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 2) {
2739 dev_err(&adapter
->dev
,
2740 "Invalid block write size %d\n",
2744 for (i
= 1; i
< msg
[0].len
; i
++)
2745 msgbuf0
[i
] = data
->block
[i
-1];
2748 case I2C_SMBUS_BLOCK_PROC_CALL
:
2749 num
= 2; /* Another special case */
2750 read_write
= I2C_SMBUS_READ
;
2751 if (data
->block
[0] > I2C_SMBUS_BLOCK_MAX
) {
2752 dev_err(&adapter
->dev
,
2753 "Invalid block write size %d\n",
2757 msg
[0].len
= data
->block
[0] + 2;
2758 for (i
= 1; i
< msg
[0].len
; i
++)
2759 msgbuf0
[i
] = data
->block
[i
-1];
2760 msg
[1].flags
|= I2C_M_RECV_LEN
;
2761 msg
[1].len
= 1; /* block length will be added by
2762 the underlying bus driver */
2764 case I2C_SMBUS_I2C_BLOCK_DATA
:
2765 if (read_write
== I2C_SMBUS_READ
) {
2766 msg
[1].len
= data
->block
[0];
2768 msg
[0].len
= data
->block
[0] + 1;
2769 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 1) {
2770 dev_err(&adapter
->dev
,
2771 "Invalid block write size %d\n",
2775 for (i
= 1; i
<= data
->block
[0]; i
++)
2776 msgbuf0
[i
] = data
->block
[i
];
2780 dev_err(&adapter
->dev
, "Unsupported transaction %d\n", size
);
2784 i
= ((flags
& I2C_CLIENT_PEC
) && size
!= I2C_SMBUS_QUICK
2785 && size
!= I2C_SMBUS_I2C_BLOCK_DATA
);
2787 /* Compute PEC if first message is a write */
2788 if (!(msg
[0].flags
& I2C_M_RD
)) {
2789 if (num
== 1) /* Write only */
2790 i2c_smbus_add_pec(&msg
[0]);
2791 else /* Write followed by read */
2792 partial_pec
= i2c_smbus_msg_pec(0, &msg
[0]);
2794 /* Ask for PEC if last message is a read */
2795 if (msg
[num
-1].flags
& I2C_M_RD
)
2799 status
= i2c_transfer(adapter
, msg
, num
);
2803 /* Check PEC if last message is a read */
2804 if (i
&& (msg
[num
-1].flags
& I2C_M_RD
)) {
2805 status
= i2c_smbus_check_pec(partial_pec
, &msg
[num
-1]);
2810 if (read_write
== I2C_SMBUS_READ
)
2812 case I2C_SMBUS_BYTE
:
2813 data
->byte
= msgbuf0
[0];
2815 case I2C_SMBUS_BYTE_DATA
:
2816 data
->byte
= msgbuf1
[0];
2818 case I2C_SMBUS_WORD_DATA
:
2819 case I2C_SMBUS_PROC_CALL
:
2820 data
->word
= msgbuf1
[0] | (msgbuf1
[1] << 8);
2822 case I2C_SMBUS_I2C_BLOCK_DATA
:
2823 for (i
= 0; i
< data
->block
[0]; i
++)
2824 data
->block
[i
+1] = msgbuf1
[i
];
2826 case I2C_SMBUS_BLOCK_DATA
:
2827 case I2C_SMBUS_BLOCK_PROC_CALL
:
2828 for (i
= 0; i
< msgbuf1
[0] + 1; i
++)
2829 data
->block
[i
] = msgbuf1
[i
];
2836 * i2c_smbus_xfer - execute SMBus protocol operations
2837 * @adapter: Handle to I2C bus
2838 * @addr: Address of SMBus slave on that bus
2839 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2840 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2841 * @command: Byte interpreted by slave, for protocols which use such bytes
2842 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2843 * @data: Data to be read or written
2845 * This executes an SMBus protocol operation, and returns a negative
2846 * errno code else zero on success.
2848 s32
i2c_smbus_xfer(struct i2c_adapter
*adapter
, u16 addr
, unsigned short flags
,
2849 char read_write
, u8 command
, int protocol
,
2850 union i2c_smbus_data
*data
)
2852 unsigned long orig_jiffies
;
2856 /* If enabled, the following two tracepoints are conditional on
2857 * read_write and protocol.
2859 trace_smbus_write(adapter
, addr
, flags
, read_write
,
2860 command
, protocol
, data
);
2861 trace_smbus_read(adapter
, addr
, flags
, read_write
,
2864 flags
&= I2C_M_TEN
| I2C_CLIENT_PEC
| I2C_CLIENT_SCCB
;
2866 if (adapter
->algo
->smbus_xfer
) {
2867 i2c_lock_adapter(adapter
);
2869 /* Retry automatically on arbitration loss */
2870 orig_jiffies
= jiffies
;
2871 for (res
= 0, try = 0; try <= adapter
->retries
; try++) {
2872 res
= adapter
->algo
->smbus_xfer(adapter
, addr
, flags
,
2873 read_write
, command
,
2877 if (time_after(jiffies
,
2878 orig_jiffies
+ adapter
->timeout
))
2881 i2c_unlock_adapter(adapter
);
2883 if (res
!= -EOPNOTSUPP
|| !adapter
->algo
->master_xfer
)
2886 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2887 * implement native support for the SMBus operation.
2891 res
= i2c_smbus_xfer_emulated(adapter
, addr
, flags
, read_write
,
2892 command
, protocol
, data
);
2895 /* If enabled, the reply tracepoint is conditional on read_write. */
2896 trace_smbus_reply(adapter
, addr
, flags
, read_write
,
2897 command
, protocol
, data
);
2898 trace_smbus_result(adapter
, addr
, flags
, read_write
,
2899 command
, protocol
, res
);
2903 EXPORT_SYMBOL(i2c_smbus_xfer
);
2905 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2906 MODULE_DESCRIPTION("I2C-Bus main module");
2907 MODULE_LICENSE("GPL");