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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 /* ------------------------------------------------------------------------- */
21 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
22 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
23 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
24 Jean Delvare <jdelvare@suse.de>
25 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
26 Michael Lawnick <michael.lawnick.ext@nsn.com>
27 OF support is copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
28 (based on a previous patch from Jon Smirl <jonsmirl@gmail.com>) and
29 (c) 2013 Wolfram Sang <wsa@the-dreams.de>
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/delay.h>
35 #include <linux/errno.h>
36 #include <linux/gpio.h>
37 #include <linux/slab.h>
38 #include <linux/i2c.h>
39 #include <linux/init.h>
40 #include <linux/idr.h>
41 #include <linux/mutex.h>
43 #include <linux/of_device.h>
44 #include <linux/of_irq.h>
45 #include <linux/clk/clk-conf.h>
46 #include <linux/completion.h>
47 #include <linux/hardirq.h>
48 #include <linux/irqflags.h>
49 #include <linux/rwsem.h>
50 #include <linux/pm_runtime.h>
51 #include <linux/acpi.h>
52 #include <linux/jump_label.h>
53 #include <asm/uaccess.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 /* ------------------------------------------------------------------------- */
83 static const struct i2c_device_id
*i2c_match_id(const struct i2c_device_id
*id
,
84 const struct i2c_client
*client
)
87 if (strcmp(client
->name
, id
->name
) == 0)
94 static int i2c_device_match(struct device
*dev
, struct device_driver
*drv
)
96 struct i2c_client
*client
= i2c_verify_client(dev
);
97 struct i2c_driver
*driver
;
102 /* Attempt an OF style match */
103 if (of_driver_match_device(dev
, drv
))
106 /* Then ACPI style match */
107 if (acpi_driver_match_device(dev
, drv
))
110 driver
= to_i2c_driver(drv
);
111 /* match on an id table if there is one */
112 if (driver
->id_table
)
113 return i2c_match_id(driver
->id_table
, client
) != NULL
;
119 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
120 static int i2c_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
122 struct i2c_client
*client
= to_i2c_client(dev
);
125 rc
= acpi_device_uevent_modalias(dev
, env
);
129 if (add_uevent_var(env
, "MODALIAS=%s%s",
130 I2C_MODULE_PREFIX
, client
->name
))
132 dev_dbg(dev
, "uevent\n");
136 /* i2c bus recovery routines */
137 static int get_scl_gpio_value(struct i2c_adapter
*adap
)
139 return gpio_get_value(adap
->bus_recovery_info
->scl_gpio
);
142 static void set_scl_gpio_value(struct i2c_adapter
*adap
, int val
)
144 gpio_set_value(adap
->bus_recovery_info
->scl_gpio
, val
);
147 static int get_sda_gpio_value(struct i2c_adapter
*adap
)
149 return gpio_get_value(adap
->bus_recovery_info
->sda_gpio
);
152 static int i2c_get_gpios_for_recovery(struct i2c_adapter
*adap
)
154 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
155 struct device
*dev
= &adap
->dev
;
158 ret
= gpio_request_one(bri
->scl_gpio
, GPIOF_OPEN_DRAIN
|
159 GPIOF_OUT_INIT_HIGH
, "i2c-scl");
161 dev_warn(dev
, "Can't get SCL gpio: %d\n", bri
->scl_gpio
);
166 if (gpio_request_one(bri
->sda_gpio
, GPIOF_IN
, "i2c-sda")) {
167 /* work without SDA polling */
168 dev_warn(dev
, "Can't get SDA gpio: %d. Not using SDA polling\n",
177 static void i2c_put_gpios_for_recovery(struct i2c_adapter
*adap
)
179 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
182 gpio_free(bri
->sda_gpio
);
184 gpio_free(bri
->scl_gpio
);
188 * We are generating clock pulses. ndelay() determines durating of clk pulses.
189 * We will generate clock with rate 100 KHz and so duration of both clock levels
190 * is: delay in ns = (10^6 / 100) / 2
192 #define RECOVERY_NDELAY 5000
193 #define RECOVERY_CLK_CNT 9
195 static int i2c_generic_recovery(struct i2c_adapter
*adap
)
197 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
198 int i
= 0, val
= 1, ret
= 0;
200 if (bri
->prepare_recovery
)
201 bri
->prepare_recovery(bri
);
204 * By this time SCL is high, as we need to give 9 falling-rising edges
206 while (i
++ < RECOVERY_CLK_CNT
* 2) {
208 /* Break if SDA is high */
209 if (bri
->get_sda
&& bri
->get_sda(adap
))
211 /* SCL shouldn't be low here */
212 if (!bri
->get_scl(adap
)) {
214 "SCL is stuck low, exit recovery\n");
221 bri
->set_scl(adap
, val
);
222 ndelay(RECOVERY_NDELAY
);
225 if (bri
->unprepare_recovery
)
226 bri
->unprepare_recovery(bri
);
231 int i2c_generic_scl_recovery(struct i2c_adapter
*adap
)
233 adap
->bus_recovery_info
->set_scl(adap
, 1);
234 return i2c_generic_recovery(adap
);
237 int i2c_generic_gpio_recovery(struct i2c_adapter
*adap
)
241 ret
= i2c_get_gpios_for_recovery(adap
);
245 ret
= i2c_generic_recovery(adap
);
246 i2c_put_gpios_for_recovery(adap
);
251 int i2c_recover_bus(struct i2c_adapter
*adap
)
253 if (!adap
->bus_recovery_info
)
256 dev_dbg(&adap
->dev
, "Trying i2c bus recovery\n");
257 return adap
->bus_recovery_info
->recover_bus(adap
);
260 static int i2c_device_probe(struct device
*dev
)
262 struct i2c_client
*client
= i2c_verify_client(dev
);
263 struct i2c_driver
*driver
;
269 driver
= to_i2c_driver(dev
->driver
);
270 if (!driver
->probe
|| !driver
->id_table
)
273 if (!device_can_wakeup(&client
->dev
))
274 device_init_wakeup(&client
->dev
,
275 client
->flags
& I2C_CLIENT_WAKE
);
276 dev_dbg(dev
, "probe\n");
278 status
= of_clk_set_defaults(dev
->of_node
, false);
282 acpi_dev_pm_attach(&client
->dev
, true);
283 status
= driver
->probe(client
, i2c_match_id(driver
->id_table
, client
));
285 acpi_dev_pm_detach(&client
->dev
, true);
290 static int i2c_device_remove(struct device
*dev
)
292 struct i2c_client
*client
= i2c_verify_client(dev
);
293 struct i2c_driver
*driver
;
296 if (!client
|| !dev
->driver
)
299 driver
= to_i2c_driver(dev
->driver
);
300 if (driver
->remove
) {
301 dev_dbg(dev
, "remove\n");
302 status
= driver
->remove(client
);
305 acpi_dev_pm_detach(&client
->dev
, true);
309 static void i2c_device_shutdown(struct device
*dev
)
311 struct i2c_client
*client
= i2c_verify_client(dev
);
312 struct i2c_driver
*driver
;
314 if (!client
|| !dev
->driver
)
316 driver
= to_i2c_driver(dev
->driver
);
317 if (driver
->shutdown
)
318 driver
->shutdown(client
);
321 #ifdef CONFIG_PM_SLEEP
322 static int i2c_legacy_suspend(struct device
*dev
, pm_message_t mesg
)
324 struct i2c_client
*client
= i2c_verify_client(dev
);
325 struct i2c_driver
*driver
;
327 if (!client
|| !dev
->driver
)
329 driver
= to_i2c_driver(dev
->driver
);
330 if (!driver
->suspend
)
332 return driver
->suspend(client
, mesg
);
335 static int i2c_legacy_resume(struct device
*dev
)
337 struct i2c_client
*client
= i2c_verify_client(dev
);
338 struct i2c_driver
*driver
;
340 if (!client
|| !dev
->driver
)
342 driver
= to_i2c_driver(dev
->driver
);
345 return driver
->resume(client
);
348 static int i2c_device_pm_suspend(struct device
*dev
)
350 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
353 return pm_generic_suspend(dev
);
355 return i2c_legacy_suspend(dev
, PMSG_SUSPEND
);
358 static int i2c_device_pm_resume(struct device
*dev
)
360 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
363 return pm_generic_resume(dev
);
365 return i2c_legacy_resume(dev
);
368 static int i2c_device_pm_freeze(struct device
*dev
)
370 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
373 return pm_generic_freeze(dev
);
375 return i2c_legacy_suspend(dev
, PMSG_FREEZE
);
378 static int i2c_device_pm_thaw(struct device
*dev
)
380 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
383 return pm_generic_thaw(dev
);
385 return i2c_legacy_resume(dev
);
388 static int i2c_device_pm_poweroff(struct device
*dev
)
390 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
393 return pm_generic_poweroff(dev
);
395 return i2c_legacy_suspend(dev
, PMSG_HIBERNATE
);
398 static int i2c_device_pm_restore(struct device
*dev
)
400 const struct dev_pm_ops
*pm
= dev
->driver
? dev
->driver
->pm
: NULL
;
403 return pm_generic_restore(dev
);
405 return i2c_legacy_resume(dev
);
407 #else /* !CONFIG_PM_SLEEP */
408 #define i2c_device_pm_suspend NULL
409 #define i2c_device_pm_resume NULL
410 #define i2c_device_pm_freeze NULL
411 #define i2c_device_pm_thaw NULL
412 #define i2c_device_pm_poweroff NULL
413 #define i2c_device_pm_restore NULL
414 #endif /* !CONFIG_PM_SLEEP */
416 static void i2c_client_dev_release(struct device
*dev
)
418 kfree(to_i2c_client(dev
));
422 show_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
424 return sprintf(buf
, "%s\n", dev
->type
== &i2c_client_type
?
425 to_i2c_client(dev
)->name
: to_i2c_adapter(dev
)->name
);
429 show_modalias(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
431 struct i2c_client
*client
= to_i2c_client(dev
);
434 len
= acpi_device_modalias(dev
, buf
, PAGE_SIZE
-1);
438 return sprintf(buf
, "%s%s\n", I2C_MODULE_PREFIX
, client
->name
);
441 static DEVICE_ATTR(name
, S_IRUGO
, show_name
, NULL
);
442 static DEVICE_ATTR(modalias
, S_IRUGO
, show_modalias
, NULL
);
444 static struct attribute
*i2c_dev_attrs
[] = {
446 /* modalias helps coldplug: modprobe $(cat .../modalias) */
447 &dev_attr_modalias
.attr
,
451 static struct attribute_group i2c_dev_attr_group
= {
452 .attrs
= i2c_dev_attrs
,
455 static const struct attribute_group
*i2c_dev_attr_groups
[] = {
460 static const struct dev_pm_ops i2c_device_pm_ops
= {
461 .suspend
= i2c_device_pm_suspend
,
462 .resume
= i2c_device_pm_resume
,
463 .freeze
= i2c_device_pm_freeze
,
464 .thaw
= i2c_device_pm_thaw
,
465 .poweroff
= i2c_device_pm_poweroff
,
466 .restore
= i2c_device_pm_restore
,
468 pm_generic_runtime_suspend
,
469 pm_generic_runtime_resume
,
474 struct bus_type i2c_bus_type
= {
476 .match
= i2c_device_match
,
477 .probe
= i2c_device_probe
,
478 .remove
= i2c_device_remove
,
479 .shutdown
= i2c_device_shutdown
,
480 .pm
= &i2c_device_pm_ops
,
482 EXPORT_SYMBOL_GPL(i2c_bus_type
);
484 static struct device_type i2c_client_type
= {
485 .groups
= i2c_dev_attr_groups
,
486 .uevent
= i2c_device_uevent
,
487 .release
= i2c_client_dev_release
,
492 * i2c_verify_client - return parameter as i2c_client, or NULL
493 * @dev: device, probably from some driver model iterator
495 * When traversing the driver model tree, perhaps using driver model
496 * iterators like @device_for_each_child(), you can't assume very much
497 * about the nodes you find. Use this function to avoid oopses caused
498 * by wrongly treating some non-I2C device as an i2c_client.
500 struct i2c_client
*i2c_verify_client(struct device
*dev
)
502 return (dev
->type
== &i2c_client_type
)
506 EXPORT_SYMBOL(i2c_verify_client
);
509 /* This is a permissive address validity check, I2C address map constraints
510 * are purposely not enforced, except for the general call address. */
511 static int i2c_check_client_addr_validity(const struct i2c_client
*client
)
513 if (client
->flags
& I2C_CLIENT_TEN
) {
514 /* 10-bit address, all values are valid */
515 if (client
->addr
> 0x3ff)
518 /* 7-bit address, reject the general call address */
519 if (client
->addr
== 0x00 || client
->addr
> 0x7f)
525 /* And this is a strict address validity check, used when probing. If a
526 * device uses a reserved address, then it shouldn't be probed. 7-bit
527 * addressing is assumed, 10-bit address devices are rare and should be
528 * explicitly enumerated. */
529 static int i2c_check_addr_validity(unsigned short addr
)
532 * Reserved addresses per I2C specification:
533 * 0x00 General call address / START byte
535 * 0x02 Reserved for different bus format
536 * 0x03 Reserved for future purposes
537 * 0x04-0x07 Hs-mode master code
538 * 0x78-0x7b 10-bit slave addressing
539 * 0x7c-0x7f Reserved for future purposes
541 if (addr
< 0x08 || addr
> 0x77)
546 static int __i2c_check_addr_busy(struct device
*dev
, void *addrp
)
548 struct i2c_client
*client
= i2c_verify_client(dev
);
549 int addr
= *(int *)addrp
;
551 if (client
&& client
->addr
== addr
)
556 /* walk up mux tree */
557 static int i2c_check_mux_parents(struct i2c_adapter
*adapter
, int addr
)
559 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
562 result
= device_for_each_child(&adapter
->dev
, &addr
,
563 __i2c_check_addr_busy
);
565 if (!result
&& parent
)
566 result
= i2c_check_mux_parents(parent
, addr
);
571 /* recurse down mux tree */
572 static int i2c_check_mux_children(struct device
*dev
, void *addrp
)
576 if (dev
->type
== &i2c_adapter_type
)
577 result
= device_for_each_child(dev
, addrp
,
578 i2c_check_mux_children
);
580 result
= __i2c_check_addr_busy(dev
, addrp
);
585 static int i2c_check_addr_busy(struct i2c_adapter
*adapter
, int addr
)
587 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
591 result
= i2c_check_mux_parents(parent
, addr
);
594 result
= device_for_each_child(&adapter
->dev
, &addr
,
595 i2c_check_mux_children
);
601 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
602 * @adapter: Target I2C bus segment
604 void i2c_lock_adapter(struct i2c_adapter
*adapter
)
606 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
609 i2c_lock_adapter(parent
);
611 rt_mutex_lock(&adapter
->bus_lock
);
613 EXPORT_SYMBOL_GPL(i2c_lock_adapter
);
616 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
617 * @adapter: Target I2C bus segment
619 static int i2c_trylock_adapter(struct i2c_adapter
*adapter
)
621 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
624 return i2c_trylock_adapter(parent
);
626 return rt_mutex_trylock(&adapter
->bus_lock
);
630 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
631 * @adapter: Target I2C bus segment
633 void i2c_unlock_adapter(struct i2c_adapter
*adapter
)
635 struct i2c_adapter
*parent
= i2c_parent_is_i2c_adapter(adapter
);
638 i2c_unlock_adapter(parent
);
640 rt_mutex_unlock(&adapter
->bus_lock
);
642 EXPORT_SYMBOL_GPL(i2c_unlock_adapter
);
644 static void i2c_dev_set_name(struct i2c_adapter
*adap
,
645 struct i2c_client
*client
)
647 struct acpi_device
*adev
= ACPI_COMPANION(&client
->dev
);
650 dev_set_name(&client
->dev
, "i2c-%s", acpi_dev_name(adev
));
654 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
655 dev_set_name(&client
->dev
, "%d-%04x", i2c_adapter_id(adap
),
656 client
->addr
| ((client
->flags
& I2C_CLIENT_TEN
)
661 * i2c_new_device - instantiate an i2c device
662 * @adap: the adapter managing the device
663 * @info: describes one I2C device; bus_num is ignored
666 * Create an i2c device. Binding is handled through driver model
667 * probe()/remove() methods. A driver may be bound to this device when we
668 * return from this function, or any later moment (e.g. maybe hotplugging will
669 * load the driver module). This call is not appropriate for use by mainboard
670 * initialization logic, which usually runs during an arch_initcall() long
671 * before any i2c_adapter could exist.
673 * This returns the new i2c client, which may be saved for later use with
674 * i2c_unregister_device(); or NULL to indicate an error.
677 i2c_new_device(struct i2c_adapter
*adap
, struct i2c_board_info
const *info
)
679 struct i2c_client
*client
;
682 client
= kzalloc(sizeof *client
, GFP_KERNEL
);
686 client
->adapter
= adap
;
688 client
->dev
.platform_data
= info
->platform_data
;
691 client
->dev
.archdata
= *info
->archdata
;
693 client
->flags
= info
->flags
;
694 client
->addr
= info
->addr
;
695 client
->irq
= info
->irq
;
697 strlcpy(client
->name
, info
->type
, sizeof(client
->name
));
699 /* Check for address validity */
700 status
= i2c_check_client_addr_validity(client
);
702 dev_err(&adap
->dev
, "Invalid %d-bit I2C address 0x%02hx\n",
703 client
->flags
& I2C_CLIENT_TEN
? 10 : 7, client
->addr
);
707 /* Check for address business */
708 status
= i2c_check_addr_busy(adap
, client
->addr
);
712 client
->dev
.parent
= &client
->adapter
->dev
;
713 client
->dev
.bus
= &i2c_bus_type
;
714 client
->dev
.type
= &i2c_client_type
;
715 client
->dev
.of_node
= info
->of_node
;
716 ACPI_COMPANION_SET(&client
->dev
, info
->acpi_node
.companion
);
718 i2c_dev_set_name(adap
, client
);
719 status
= device_register(&client
->dev
);
723 dev_dbg(&adap
->dev
, "client [%s] registered with bus id %s\n",
724 client
->name
, dev_name(&client
->dev
));
729 dev_err(&adap
->dev
, "Failed to register i2c client %s at 0x%02x "
730 "(%d)\n", client
->name
, client
->addr
, status
);
735 EXPORT_SYMBOL_GPL(i2c_new_device
);
739 * i2c_unregister_device - reverse effect of i2c_new_device()
740 * @client: value returned from i2c_new_device()
743 void i2c_unregister_device(struct i2c_client
*client
)
745 device_unregister(&client
->dev
);
747 EXPORT_SYMBOL_GPL(i2c_unregister_device
);
750 static const struct i2c_device_id dummy_id
[] = {
755 static int dummy_probe(struct i2c_client
*client
,
756 const struct i2c_device_id
*id
)
761 static int dummy_remove(struct i2c_client
*client
)
766 static struct i2c_driver dummy_driver
= {
767 .driver
.name
= "dummy",
768 .probe
= dummy_probe
,
769 .remove
= dummy_remove
,
770 .id_table
= dummy_id
,
774 * i2c_new_dummy - return a new i2c device bound to a dummy driver
775 * @adapter: the adapter managing the device
776 * @address: seven bit address to be used
779 * This returns an I2C client bound to the "dummy" driver, intended for use
780 * with devices that consume multiple addresses. Examples of such chips
781 * include various EEPROMS (like 24c04 and 24c08 models).
783 * These dummy devices have two main uses. First, most I2C and SMBus calls
784 * except i2c_transfer() need a client handle; the dummy will be that handle.
785 * And second, this prevents the specified address from being bound to a
788 * This returns the new i2c client, which should be saved for later use with
789 * i2c_unregister_device(); or NULL to indicate an error.
791 struct i2c_client
*i2c_new_dummy(struct i2c_adapter
*adapter
, u16 address
)
793 struct i2c_board_info info
= {
794 I2C_BOARD_INFO("dummy", address
),
797 return i2c_new_device(adapter
, &info
);
799 EXPORT_SYMBOL_GPL(i2c_new_dummy
);
801 /* ------------------------------------------------------------------------- */
803 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
805 static void i2c_adapter_dev_release(struct device
*dev
)
807 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
808 complete(&adap
->dev_released
);
812 * This function is only needed for mutex_lock_nested, so it is never
813 * called unless locking correctness checking is enabled. Thus we
814 * make it inline to avoid a compiler warning. That's what gcc ends up
817 static inline unsigned int i2c_adapter_depth(struct i2c_adapter
*adapter
)
819 unsigned int depth
= 0;
821 while ((adapter
= i2c_parent_is_i2c_adapter(adapter
)))
828 * Let users instantiate I2C devices through sysfs. This can be used when
829 * platform initialization code doesn't contain the proper data for
830 * whatever reason. Also useful for drivers that do device detection and
831 * detection fails, either because the device uses an unexpected address,
832 * or this is a compatible device with different ID register values.
834 * Parameter checking may look overzealous, but we really don't want
835 * the user to provide incorrect parameters.
838 i2c_sysfs_new_device(struct device
*dev
, struct device_attribute
*attr
,
839 const char *buf
, size_t count
)
841 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
842 struct i2c_board_info info
;
843 struct i2c_client
*client
;
847 memset(&info
, 0, sizeof(struct i2c_board_info
));
849 blank
= strchr(buf
, ' ');
851 dev_err(dev
, "%s: Missing parameters\n", "new_device");
854 if (blank
- buf
> I2C_NAME_SIZE
- 1) {
855 dev_err(dev
, "%s: Invalid device name\n", "new_device");
858 memcpy(info
.type
, buf
, blank
- buf
);
860 /* Parse remaining parameters, reject extra parameters */
861 res
= sscanf(++blank
, "%hi%c", &info
.addr
, &end
);
863 dev_err(dev
, "%s: Can't parse I2C address\n", "new_device");
866 if (res
> 1 && end
!= '\n') {
867 dev_err(dev
, "%s: Extra parameters\n", "new_device");
871 client
= i2c_new_device(adap
, &info
);
875 /* Keep track of the added device */
876 mutex_lock(&adap
->userspace_clients_lock
);
877 list_add_tail(&client
->detected
, &adap
->userspace_clients
);
878 mutex_unlock(&adap
->userspace_clients_lock
);
879 dev_info(dev
, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
880 info
.type
, info
.addr
);
886 * And of course let the users delete the devices they instantiated, if
887 * they got it wrong. This interface can only be used to delete devices
888 * instantiated by i2c_sysfs_new_device above. This guarantees that we
889 * don't delete devices to which some kernel code still has references.
891 * Parameter checking may look overzealous, but we really don't want
892 * the user to delete the wrong device.
895 i2c_sysfs_delete_device(struct device
*dev
, struct device_attribute
*attr
,
896 const char *buf
, size_t count
)
898 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
899 struct i2c_client
*client
, *next
;
904 /* Parse parameters, reject extra parameters */
905 res
= sscanf(buf
, "%hi%c", &addr
, &end
);
907 dev_err(dev
, "%s: Can't parse I2C address\n", "delete_device");
910 if (res
> 1 && end
!= '\n') {
911 dev_err(dev
, "%s: Extra parameters\n", "delete_device");
915 /* Make sure the device was added through sysfs */
917 mutex_lock_nested(&adap
->userspace_clients_lock
,
918 i2c_adapter_depth(adap
));
919 list_for_each_entry_safe(client
, next
, &adap
->userspace_clients
,
921 if (client
->addr
== addr
) {
922 dev_info(dev
, "%s: Deleting device %s at 0x%02hx\n",
923 "delete_device", client
->name
, client
->addr
);
925 list_del(&client
->detected
);
926 i2c_unregister_device(client
);
931 mutex_unlock(&adap
->userspace_clients_lock
);
934 dev_err(dev
, "%s: Can't find device in list\n",
939 static DEVICE_ATTR(new_device
, S_IWUSR
, NULL
, i2c_sysfs_new_device
);
940 static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device
, S_IWUSR
, NULL
,
941 i2c_sysfs_delete_device
);
943 static struct attribute
*i2c_adapter_attrs
[] = {
945 &dev_attr_new_device
.attr
,
946 &dev_attr_delete_device
.attr
,
950 static struct attribute_group i2c_adapter_attr_group
= {
951 .attrs
= i2c_adapter_attrs
,
954 static const struct attribute_group
*i2c_adapter_attr_groups
[] = {
955 &i2c_adapter_attr_group
,
959 struct device_type i2c_adapter_type
= {
960 .groups
= i2c_adapter_attr_groups
,
961 .release
= i2c_adapter_dev_release
,
963 EXPORT_SYMBOL_GPL(i2c_adapter_type
);
966 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
967 * @dev: device, probably from some driver model iterator
969 * When traversing the driver model tree, perhaps using driver model
970 * iterators like @device_for_each_child(), you can't assume very much
971 * about the nodes you find. Use this function to avoid oopses caused
972 * by wrongly treating some non-I2C device as an i2c_adapter.
974 struct i2c_adapter
*i2c_verify_adapter(struct device
*dev
)
976 return (dev
->type
== &i2c_adapter_type
)
977 ? to_i2c_adapter(dev
)
980 EXPORT_SYMBOL(i2c_verify_adapter
);
982 #ifdef CONFIG_I2C_COMPAT
983 static struct class_compat
*i2c_adapter_compat_class
;
986 static void i2c_scan_static_board_info(struct i2c_adapter
*adapter
)
988 struct i2c_devinfo
*devinfo
;
990 down_read(&__i2c_board_lock
);
991 list_for_each_entry(devinfo
, &__i2c_board_list
, list
) {
992 if (devinfo
->busnum
== adapter
->nr
993 && !i2c_new_device(adapter
,
994 &devinfo
->board_info
))
995 dev_err(&adapter
->dev
,
996 "Can't create device at 0x%02x\n",
997 devinfo
->board_info
.addr
);
999 up_read(&__i2c_board_lock
);
1002 /* OF support code */
1004 #if IS_ENABLED(CONFIG_OF)
1005 static void of_i2c_register_devices(struct i2c_adapter
*adap
)
1008 struct device_node
*node
;
1010 /* Only register child devices if the adapter has a node pointer set */
1011 if (!adap
->dev
.of_node
)
1014 dev_dbg(&adap
->dev
, "of_i2c: walking child nodes\n");
1016 for_each_available_child_of_node(adap
->dev
.of_node
, node
) {
1017 struct i2c_board_info info
= {};
1018 struct dev_archdata dev_ad
= {};
1022 dev_dbg(&adap
->dev
, "of_i2c: register %s\n", node
->full_name
);
1024 if (of_modalias_node(node
, info
.type
, sizeof(info
.type
)) < 0) {
1025 dev_err(&adap
->dev
, "of_i2c: modalias failure on %s\n",
1030 addr
= of_get_property(node
, "reg", &len
);
1031 if (!addr
|| (len
< sizeof(int))) {
1032 dev_err(&adap
->dev
, "of_i2c: invalid reg on %s\n",
1037 info
.addr
= be32_to_cpup(addr
);
1038 if (info
.addr
> (1 << 10) - 1) {
1039 dev_err(&adap
->dev
, "of_i2c: invalid addr=%x on %s\n",
1040 info
.addr
, node
->full_name
);
1044 info
.irq
= irq_of_parse_and_map(node
, 0);
1045 info
.of_node
= of_node_get(node
);
1046 info
.archdata
= &dev_ad
;
1048 if (of_get_property(node
, "wakeup-source", NULL
))
1049 info
.flags
|= I2C_CLIENT_WAKE
;
1051 request_module("%s%s", I2C_MODULE_PREFIX
, info
.type
);
1053 result
= i2c_new_device(adap
, &info
);
1054 if (result
== NULL
) {
1055 dev_err(&adap
->dev
, "of_i2c: Failure registering %s\n",
1058 irq_dispose_mapping(info
.irq
);
1064 static int of_dev_node_match(struct device
*dev
, void *data
)
1066 return dev
->of_node
== data
;
1069 /* must call put_device() when done with returned i2c_client device */
1070 struct i2c_client
*of_find_i2c_device_by_node(struct device_node
*node
)
1074 dev
= bus_find_device(&i2c_bus_type
, NULL
, node
,
1079 return i2c_verify_client(dev
);
1081 EXPORT_SYMBOL(of_find_i2c_device_by_node
);
1083 /* must call put_device() when done with returned i2c_adapter device */
1084 struct i2c_adapter
*of_find_i2c_adapter_by_node(struct device_node
*node
)
1088 dev
= bus_find_device(&i2c_bus_type
, NULL
, node
,
1093 return i2c_verify_adapter(dev
);
1095 EXPORT_SYMBOL(of_find_i2c_adapter_by_node
);
1097 static void of_i2c_register_devices(struct i2c_adapter
*adap
) { }
1098 #endif /* CONFIG_OF */
1100 static int i2c_do_add_adapter(struct i2c_driver
*driver
,
1101 struct i2c_adapter
*adap
)
1103 /* Detect supported devices on that bus, and instantiate them */
1104 i2c_detect(adap
, driver
);
1106 /* Let legacy drivers scan this bus for matching devices */
1107 if (driver
->attach_adapter
) {
1108 dev_warn(&adap
->dev
, "%s: attach_adapter method is deprecated\n",
1109 driver
->driver
.name
);
1110 dev_warn(&adap
->dev
, "Please use another way to instantiate "
1111 "your i2c_client\n");
1112 /* We ignore the return code; if it fails, too bad */
1113 driver
->attach_adapter(adap
);
1118 static int __process_new_adapter(struct device_driver
*d
, void *data
)
1120 return i2c_do_add_adapter(to_i2c_driver(d
), data
);
1123 static int i2c_register_adapter(struct i2c_adapter
*adap
)
1127 /* Can't register until after driver model init */
1128 if (unlikely(WARN_ON(!i2c_bus_type
.p
))) {
1134 if (unlikely(adap
->name
[0] == '\0')) {
1135 pr_err("i2c-core: Attempt to register an adapter with "
1139 if (unlikely(!adap
->algo
)) {
1140 pr_err("i2c-core: Attempt to register adapter '%s' with "
1141 "no algo!\n", adap
->name
);
1145 rt_mutex_init(&adap
->bus_lock
);
1146 mutex_init(&adap
->userspace_clients_lock
);
1147 INIT_LIST_HEAD(&adap
->userspace_clients
);
1149 /* Set default timeout to 1 second if not already set */
1150 if (adap
->timeout
== 0)
1153 dev_set_name(&adap
->dev
, "i2c-%d", adap
->nr
);
1154 adap
->dev
.bus
= &i2c_bus_type
;
1155 adap
->dev
.type
= &i2c_adapter_type
;
1156 res
= device_register(&adap
->dev
);
1160 dev_dbg(&adap
->dev
, "adapter [%s] registered\n", adap
->name
);
1162 #ifdef CONFIG_I2C_COMPAT
1163 res
= class_compat_create_link(i2c_adapter_compat_class
, &adap
->dev
,
1166 dev_warn(&adap
->dev
,
1167 "Failed to create compatibility class link\n");
1170 /* bus recovery specific initialization */
1171 if (adap
->bus_recovery_info
) {
1172 struct i2c_bus_recovery_info
*bri
= adap
->bus_recovery_info
;
1174 if (!bri
->recover_bus
) {
1175 dev_err(&adap
->dev
, "No recover_bus() found, not using recovery\n");
1176 adap
->bus_recovery_info
= NULL
;
1180 /* Generic GPIO recovery */
1181 if (bri
->recover_bus
== i2c_generic_gpio_recovery
) {
1182 if (!gpio_is_valid(bri
->scl_gpio
)) {
1183 dev_err(&adap
->dev
, "Invalid SCL gpio, not using recovery\n");
1184 adap
->bus_recovery_info
= NULL
;
1188 if (gpio_is_valid(bri
->sda_gpio
))
1189 bri
->get_sda
= get_sda_gpio_value
;
1191 bri
->get_sda
= NULL
;
1193 bri
->get_scl
= get_scl_gpio_value
;
1194 bri
->set_scl
= set_scl_gpio_value
;
1195 } else if (!bri
->set_scl
|| !bri
->get_scl
) {
1196 /* Generic SCL recovery */
1197 dev_err(&adap
->dev
, "No {get|set}_gpio() found, not using recovery\n");
1198 adap
->bus_recovery_info
= NULL
;
1203 /* create pre-declared device nodes */
1204 of_i2c_register_devices(adap
);
1205 acpi_i2c_register_devices(adap
);
1206 acpi_i2c_install_space_handler(adap
);
1208 if (adap
->nr
< __i2c_first_dynamic_bus_num
)
1209 i2c_scan_static_board_info(adap
);
1211 /* Notify drivers */
1212 mutex_lock(&core_lock
);
1213 bus_for_each_drv(&i2c_bus_type
, NULL
, adap
, __process_new_adapter
);
1214 mutex_unlock(&core_lock
);
1219 mutex_lock(&core_lock
);
1220 idr_remove(&i2c_adapter_idr
, adap
->nr
);
1221 mutex_unlock(&core_lock
);
1226 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1227 * @adap: the adapter to register (with adap->nr initialized)
1228 * Context: can sleep
1230 * See i2c_add_numbered_adapter() for details.
1232 static int __i2c_add_numbered_adapter(struct i2c_adapter
*adap
)
1236 mutex_lock(&core_lock
);
1237 id
= idr_alloc(&i2c_adapter_idr
, adap
, adap
->nr
, adap
->nr
+ 1,
1239 mutex_unlock(&core_lock
);
1241 return id
== -ENOSPC
? -EBUSY
: id
;
1243 return i2c_register_adapter(adap
);
1247 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1248 * @adapter: the adapter to add
1249 * Context: can sleep
1251 * This routine is used to declare an I2C adapter when its bus number
1252 * doesn't matter or when its bus number is specified by an dt alias.
1253 * Examples of bases when the bus number doesn't matter: I2C adapters
1254 * dynamically added by USB links or PCI plugin cards.
1256 * When this returns zero, a new bus number was allocated and stored
1257 * in adap->nr, and the specified adapter became available for clients.
1258 * Otherwise, a negative errno value is returned.
1260 int i2c_add_adapter(struct i2c_adapter
*adapter
)
1262 struct device
*dev
= &adapter
->dev
;
1266 id
= of_alias_get_id(dev
->of_node
, "i2c");
1269 return __i2c_add_numbered_adapter(adapter
);
1273 mutex_lock(&core_lock
);
1274 id
= idr_alloc(&i2c_adapter_idr
, adapter
,
1275 __i2c_first_dynamic_bus_num
, 0, GFP_KERNEL
);
1276 mutex_unlock(&core_lock
);
1282 return i2c_register_adapter(adapter
);
1284 EXPORT_SYMBOL(i2c_add_adapter
);
1287 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1288 * @adap: the adapter to register (with adap->nr initialized)
1289 * Context: can sleep
1291 * This routine is used to declare an I2C adapter when its bus number
1292 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1293 * or otherwise built in to the system's mainboard, and where i2c_board_info
1294 * is used to properly configure I2C devices.
1296 * If the requested bus number is set to -1, then this function will behave
1297 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1299 * If no devices have pre-been declared for this bus, then be sure to
1300 * register the adapter before any dynamically allocated ones. Otherwise
1301 * the required bus ID may not be available.
1303 * When this returns zero, the specified adapter became available for
1304 * clients using the bus number provided in adap->nr. Also, the table
1305 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1306 * and the appropriate driver model device nodes are created. Otherwise, a
1307 * negative errno value is returned.
1309 int i2c_add_numbered_adapter(struct i2c_adapter
*adap
)
1311 if (adap
->nr
== -1) /* -1 means dynamically assign bus id */
1312 return i2c_add_adapter(adap
);
1314 return __i2c_add_numbered_adapter(adap
);
1316 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter
);
1318 static void i2c_do_del_adapter(struct i2c_driver
*driver
,
1319 struct i2c_adapter
*adapter
)
1321 struct i2c_client
*client
, *_n
;
1323 /* Remove the devices we created ourselves as the result of hardware
1324 * probing (using a driver's detect method) */
1325 list_for_each_entry_safe(client
, _n
, &driver
->clients
, detected
) {
1326 if (client
->adapter
== adapter
) {
1327 dev_dbg(&adapter
->dev
, "Removing %s at 0x%x\n",
1328 client
->name
, client
->addr
);
1329 list_del(&client
->detected
);
1330 i2c_unregister_device(client
);
1335 static int __unregister_client(struct device
*dev
, void *dummy
)
1337 struct i2c_client
*client
= i2c_verify_client(dev
);
1338 if (client
&& strcmp(client
->name
, "dummy"))
1339 i2c_unregister_device(client
);
1343 static int __unregister_dummy(struct device
*dev
, void *dummy
)
1345 struct i2c_client
*client
= i2c_verify_client(dev
);
1347 i2c_unregister_device(client
);
1351 static int __process_removed_adapter(struct device_driver
*d
, void *data
)
1353 i2c_do_del_adapter(to_i2c_driver(d
), data
);
1358 * i2c_del_adapter - unregister I2C adapter
1359 * @adap: the adapter being unregistered
1360 * Context: can sleep
1362 * This unregisters an I2C adapter which was previously registered
1363 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1365 void i2c_del_adapter(struct i2c_adapter
*adap
)
1367 struct i2c_adapter
*found
;
1368 struct i2c_client
*client
, *next
;
1370 /* First make sure that this adapter was ever added */
1371 mutex_lock(&core_lock
);
1372 found
= idr_find(&i2c_adapter_idr
, adap
->nr
);
1373 mutex_unlock(&core_lock
);
1374 if (found
!= adap
) {
1375 pr_debug("i2c-core: attempting to delete unregistered "
1376 "adapter [%s]\n", adap
->name
);
1380 acpi_i2c_remove_space_handler(adap
);
1381 /* Tell drivers about this removal */
1382 mutex_lock(&core_lock
);
1383 bus_for_each_drv(&i2c_bus_type
, NULL
, adap
,
1384 __process_removed_adapter
);
1385 mutex_unlock(&core_lock
);
1387 /* Remove devices instantiated from sysfs */
1388 mutex_lock_nested(&adap
->userspace_clients_lock
,
1389 i2c_adapter_depth(adap
));
1390 list_for_each_entry_safe(client
, next
, &adap
->userspace_clients
,
1392 dev_dbg(&adap
->dev
, "Removing %s at 0x%x\n", client
->name
,
1394 list_del(&client
->detected
);
1395 i2c_unregister_device(client
);
1397 mutex_unlock(&adap
->userspace_clients_lock
);
1399 /* Detach any active clients. This can't fail, thus we do not
1400 * check the returned value. This is a two-pass process, because
1401 * we can't remove the dummy devices during the first pass: they
1402 * could have been instantiated by real devices wishing to clean
1403 * them up properly, so we give them a chance to do that first. */
1404 device_for_each_child(&adap
->dev
, NULL
, __unregister_client
);
1405 device_for_each_child(&adap
->dev
, NULL
, __unregister_dummy
);
1407 #ifdef CONFIG_I2C_COMPAT
1408 class_compat_remove_link(i2c_adapter_compat_class
, &adap
->dev
,
1412 /* device name is gone after device_unregister */
1413 dev_dbg(&adap
->dev
, "adapter [%s] unregistered\n", adap
->name
);
1415 /* clean up the sysfs representation */
1416 init_completion(&adap
->dev_released
);
1417 device_unregister(&adap
->dev
);
1419 /* wait for sysfs to drop all references */
1420 wait_for_completion(&adap
->dev_released
);
1423 mutex_lock(&core_lock
);
1424 idr_remove(&i2c_adapter_idr
, adap
->nr
);
1425 mutex_unlock(&core_lock
);
1427 /* Clear the device structure in case this adapter is ever going to be
1429 memset(&adap
->dev
, 0, sizeof(adap
->dev
));
1431 EXPORT_SYMBOL(i2c_del_adapter
);
1433 /* ------------------------------------------------------------------------- */
1435 int i2c_for_each_dev(void *data
, int (*fn
)(struct device
*, void *))
1439 mutex_lock(&core_lock
);
1440 res
= bus_for_each_dev(&i2c_bus_type
, NULL
, data
, fn
);
1441 mutex_unlock(&core_lock
);
1445 EXPORT_SYMBOL_GPL(i2c_for_each_dev
);
1447 static int __process_new_driver(struct device
*dev
, void *data
)
1449 if (dev
->type
!= &i2c_adapter_type
)
1451 return i2c_do_add_adapter(data
, to_i2c_adapter(dev
));
1455 * An i2c_driver is used with one or more i2c_client (device) nodes to access
1456 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1459 int i2c_register_driver(struct module
*owner
, struct i2c_driver
*driver
)
1463 /* Can't register until after driver model init */
1464 if (unlikely(WARN_ON(!i2c_bus_type
.p
)))
1467 /* add the driver to the list of i2c drivers in the driver core */
1468 driver
->driver
.owner
= owner
;
1469 driver
->driver
.bus
= &i2c_bus_type
;
1471 /* When registration returns, the driver core
1472 * will have called probe() for all matching-but-unbound devices.
1474 res
= driver_register(&driver
->driver
);
1478 /* Drivers should switch to dev_pm_ops instead. */
1479 if (driver
->suspend
)
1480 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1481 driver
->driver
.name
);
1483 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1484 driver
->driver
.name
);
1486 pr_debug("i2c-core: driver [%s] registered\n", driver
->driver
.name
);
1488 INIT_LIST_HEAD(&driver
->clients
);
1489 /* Walk the adapters that are already present */
1490 i2c_for_each_dev(driver
, __process_new_driver
);
1494 EXPORT_SYMBOL(i2c_register_driver
);
1496 static int __process_removed_driver(struct device
*dev
, void *data
)
1498 if (dev
->type
== &i2c_adapter_type
)
1499 i2c_do_del_adapter(data
, to_i2c_adapter(dev
));
1504 * i2c_del_driver - unregister I2C driver
1505 * @driver: the driver being unregistered
1506 * Context: can sleep
1508 void i2c_del_driver(struct i2c_driver
*driver
)
1510 i2c_for_each_dev(driver
, __process_removed_driver
);
1512 driver_unregister(&driver
->driver
);
1513 pr_debug("i2c-core: driver [%s] unregistered\n", driver
->driver
.name
);
1515 EXPORT_SYMBOL(i2c_del_driver
);
1517 /* ------------------------------------------------------------------------- */
1520 * i2c_use_client - increments the reference count of the i2c client structure
1521 * @client: the client being referenced
1523 * Each live reference to a client should be refcounted. The driver model does
1524 * that automatically as part of driver binding, so that most drivers don't
1525 * need to do this explicitly: they hold a reference until they're unbound
1528 * A pointer to the client with the incremented reference counter is returned.
1530 struct i2c_client
*i2c_use_client(struct i2c_client
*client
)
1532 if (client
&& get_device(&client
->dev
))
1536 EXPORT_SYMBOL(i2c_use_client
);
1539 * i2c_release_client - release a use of the i2c client structure
1540 * @client: the client being no longer referenced
1542 * Must be called when a user of a client is finished with it.
1544 void i2c_release_client(struct i2c_client
*client
)
1547 put_device(&client
->dev
);
1549 EXPORT_SYMBOL(i2c_release_client
);
1551 struct i2c_cmd_arg
{
1556 static int i2c_cmd(struct device
*dev
, void *_arg
)
1558 struct i2c_client
*client
= i2c_verify_client(dev
);
1559 struct i2c_cmd_arg
*arg
= _arg
;
1560 struct i2c_driver
*driver
;
1562 if (!client
|| !client
->dev
.driver
)
1565 driver
= to_i2c_driver(client
->dev
.driver
);
1566 if (driver
->command
)
1567 driver
->command(client
, arg
->cmd
, arg
->arg
);
1571 void i2c_clients_command(struct i2c_adapter
*adap
, unsigned int cmd
, void *arg
)
1573 struct i2c_cmd_arg cmd_arg
;
1577 device_for_each_child(&adap
->dev
, &cmd_arg
, i2c_cmd
);
1579 EXPORT_SYMBOL(i2c_clients_command
);
1581 static int __init
i2c_init(void)
1585 retval
= bus_register(&i2c_bus_type
);
1588 #ifdef CONFIG_I2C_COMPAT
1589 i2c_adapter_compat_class
= class_compat_register("i2c-adapter");
1590 if (!i2c_adapter_compat_class
) {
1595 retval
= i2c_add_driver(&dummy_driver
);
1601 #ifdef CONFIG_I2C_COMPAT
1602 class_compat_unregister(i2c_adapter_compat_class
);
1605 bus_unregister(&i2c_bus_type
);
1609 static void __exit
i2c_exit(void)
1611 i2c_del_driver(&dummy_driver
);
1612 #ifdef CONFIG_I2C_COMPAT
1613 class_compat_unregister(i2c_adapter_compat_class
);
1615 bus_unregister(&i2c_bus_type
);
1616 tracepoint_synchronize_unregister();
1619 /* We must initialize early, because some subsystems register i2c drivers
1620 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1622 postcore_initcall(i2c_init
);
1623 module_exit(i2c_exit
);
1625 /* ----------------------------------------------------
1626 * the functional interface to the i2c busses.
1627 * ----------------------------------------------------
1631 * __i2c_transfer - unlocked flavor of i2c_transfer
1632 * @adap: Handle to I2C bus
1633 * @msgs: One or more messages to execute before STOP is issued to
1634 * terminate the operation; each message begins with a START.
1635 * @num: Number of messages to be executed.
1637 * Returns negative errno, else the number of messages executed.
1639 * Adapter lock must be held when calling this function. No debug logging
1640 * takes place. adap->algo->master_xfer existence isn't checked.
1642 int __i2c_transfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
1644 unsigned long orig_jiffies
;
1647 /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets
1648 * enabled. This is an efficient way of keeping the for-loop from
1649 * being executed when not needed.
1651 if (static_key_false(&i2c_trace_msg
)) {
1653 for (i
= 0; i
< num
; i
++)
1654 if (msgs
[i
].flags
& I2C_M_RD
)
1655 trace_i2c_read(adap
, &msgs
[i
], i
);
1657 trace_i2c_write(adap
, &msgs
[i
], i
);
1660 /* Retry automatically on arbitration loss */
1661 orig_jiffies
= jiffies
;
1662 for (ret
= 0, try = 0; try <= adap
->retries
; try++) {
1663 ret
= adap
->algo
->master_xfer(adap
, msgs
, num
);
1666 if (time_after(jiffies
, orig_jiffies
+ adap
->timeout
))
1670 if (static_key_false(&i2c_trace_msg
)) {
1672 for (i
= 0; i
< ret
; i
++)
1673 if (msgs
[i
].flags
& I2C_M_RD
)
1674 trace_i2c_reply(adap
, &msgs
[i
], i
);
1675 trace_i2c_result(adap
, i
, ret
);
1680 EXPORT_SYMBOL(__i2c_transfer
);
1683 * i2c_transfer - execute a single or combined I2C message
1684 * @adap: Handle to I2C bus
1685 * @msgs: One or more messages to execute before STOP is issued to
1686 * terminate the operation; each message begins with a START.
1687 * @num: Number of messages to be executed.
1689 * Returns negative errno, else the number of messages executed.
1691 * Note that there is no requirement that each message be sent to
1692 * the same slave address, although that is the most common model.
1694 int i2c_transfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
1698 /* REVISIT the fault reporting model here is weak:
1700 * - When we get an error after receiving N bytes from a slave,
1701 * there is no way to report "N".
1703 * - When we get a NAK after transmitting N bytes to a slave,
1704 * there is no way to report "N" ... or to let the master
1705 * continue executing the rest of this combined message, if
1706 * that's the appropriate response.
1708 * - When for example "num" is two and we successfully complete
1709 * the first message but get an error part way through the
1710 * second, it's unclear whether that should be reported as
1711 * one (discarding status on the second message) or errno
1712 * (discarding status on the first one).
1715 if (adap
->algo
->master_xfer
) {
1717 for (ret
= 0; ret
< num
; ret
++) {
1718 dev_dbg(&adap
->dev
, "master_xfer[%d] %c, addr=0x%02x, "
1719 "len=%d%s\n", ret
, (msgs
[ret
].flags
& I2C_M_RD
)
1720 ? 'R' : 'W', msgs
[ret
].addr
, msgs
[ret
].len
,
1721 (msgs
[ret
].flags
& I2C_M_RECV_LEN
) ? "+" : "");
1725 if (in_atomic() || irqs_disabled()) {
1726 ret
= i2c_trylock_adapter(adap
);
1728 /* I2C activity is ongoing. */
1731 i2c_lock_adapter(adap
);
1734 ret
= __i2c_transfer(adap
, msgs
, num
);
1735 i2c_unlock_adapter(adap
);
1739 dev_dbg(&adap
->dev
, "I2C level transfers not supported\n");
1743 EXPORT_SYMBOL(i2c_transfer
);
1746 * i2c_master_send - issue a single I2C message in master transmit mode
1747 * @client: Handle to slave device
1748 * @buf: Data that will be written to the slave
1749 * @count: How many bytes to write, must be less than 64k since msg.len is u16
1751 * Returns negative errno, or else the number of bytes written.
1753 int i2c_master_send(const struct i2c_client
*client
, const char *buf
, int count
)
1756 struct i2c_adapter
*adap
= client
->adapter
;
1759 msg
.addr
= client
->addr
;
1760 msg
.flags
= client
->flags
& I2C_M_TEN
;
1762 msg
.buf
= (char *)buf
;
1764 ret
= i2c_transfer(adap
, &msg
, 1);
1767 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1768 * transmitted, else error code.
1770 return (ret
== 1) ? count
: ret
;
1772 EXPORT_SYMBOL(i2c_master_send
);
1775 * i2c_master_recv - issue a single I2C message in master receive mode
1776 * @client: Handle to slave device
1777 * @buf: Where to store data read from slave
1778 * @count: How many bytes to read, must be less than 64k since msg.len is u16
1780 * Returns negative errno, or else the number of bytes read.
1782 int i2c_master_recv(const struct i2c_client
*client
, char *buf
, int count
)
1784 struct i2c_adapter
*adap
= client
->adapter
;
1788 msg
.addr
= client
->addr
;
1789 msg
.flags
= client
->flags
& I2C_M_TEN
;
1790 msg
.flags
|= I2C_M_RD
;
1794 ret
= i2c_transfer(adap
, &msg
, 1);
1797 * If everything went ok (i.e. 1 msg received), return #bytes received,
1800 return (ret
== 1) ? count
: ret
;
1802 EXPORT_SYMBOL(i2c_master_recv
);
1804 /* ----------------------------------------------------
1805 * the i2c address scanning function
1806 * Will not work for 10-bit addresses!
1807 * ----------------------------------------------------
1811 * Legacy default probe function, mostly relevant for SMBus. The default
1812 * probe method is a quick write, but it is known to corrupt the 24RF08
1813 * EEPROMs due to a state machine bug, and could also irreversibly
1814 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1815 * we use a short byte read instead. Also, some bus drivers don't implement
1816 * quick write, so we fallback to a byte read in that case too.
1817 * On x86, there is another special case for FSC hardware monitoring chips,
1818 * which want regular byte reads (address 0x73.) Fortunately, these are the
1819 * only known chips using this I2C address on PC hardware.
1820 * Returns 1 if probe succeeded, 0 if not.
1822 static int i2c_default_probe(struct i2c_adapter
*adap
, unsigned short addr
)
1825 union i2c_smbus_data dummy
;
1828 if (addr
== 0x73 && (adap
->class & I2C_CLASS_HWMON
)
1829 && i2c_check_functionality(adap
, I2C_FUNC_SMBUS_READ_BYTE_DATA
))
1830 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
1831 I2C_SMBUS_BYTE_DATA
, &dummy
);
1834 if (!((addr
& ~0x07) == 0x30 || (addr
& ~0x0f) == 0x50)
1835 && i2c_check_functionality(adap
, I2C_FUNC_SMBUS_QUICK
))
1836 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_WRITE
, 0,
1837 I2C_SMBUS_QUICK
, NULL
);
1838 else if (i2c_check_functionality(adap
, I2C_FUNC_SMBUS_READ_BYTE
))
1839 err
= i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
1840 I2C_SMBUS_BYTE
, &dummy
);
1842 dev_warn(&adap
->dev
, "No suitable probing method supported for address 0x%02X\n",
1850 static int i2c_detect_address(struct i2c_client
*temp_client
,
1851 struct i2c_driver
*driver
)
1853 struct i2c_board_info info
;
1854 struct i2c_adapter
*adapter
= temp_client
->adapter
;
1855 int addr
= temp_client
->addr
;
1858 /* Make sure the address is valid */
1859 err
= i2c_check_addr_validity(addr
);
1861 dev_warn(&adapter
->dev
, "Invalid probe address 0x%02x\n",
1866 /* Skip if already in use */
1867 if (i2c_check_addr_busy(adapter
, addr
))
1870 /* Make sure there is something at this address */
1871 if (!i2c_default_probe(adapter
, addr
))
1874 /* Finally call the custom detection function */
1875 memset(&info
, 0, sizeof(struct i2c_board_info
));
1877 err
= driver
->detect(temp_client
, &info
);
1879 /* -ENODEV is returned if the detection fails. We catch it
1880 here as this isn't an error. */
1881 return err
== -ENODEV
? 0 : err
;
1884 /* Consistency check */
1885 if (info
.type
[0] == '\0') {
1886 dev_err(&adapter
->dev
, "%s detection function provided "
1887 "no name for 0x%x\n", driver
->driver
.name
,
1890 struct i2c_client
*client
;
1892 /* Detection succeeded, instantiate the device */
1893 if (adapter
->class & I2C_CLASS_DEPRECATED
)
1894 dev_warn(&adapter
->dev
,
1895 "This adapter will soon drop class based instantiation of devices. "
1896 "Please make sure client 0x%02x gets instantiated by other means. "
1897 "Check 'Documentation/i2c/instantiating-devices' for details.\n",
1900 dev_dbg(&adapter
->dev
, "Creating %s at 0x%02x\n",
1901 info
.type
, info
.addr
);
1902 client
= i2c_new_device(adapter
, &info
);
1904 list_add_tail(&client
->detected
, &driver
->clients
);
1906 dev_err(&adapter
->dev
, "Failed creating %s at 0x%02x\n",
1907 info
.type
, info
.addr
);
1912 static int i2c_detect(struct i2c_adapter
*adapter
, struct i2c_driver
*driver
)
1914 const unsigned short *address_list
;
1915 struct i2c_client
*temp_client
;
1917 int adap_id
= i2c_adapter_id(adapter
);
1919 address_list
= driver
->address_list
;
1920 if (!driver
->detect
|| !address_list
)
1923 /* Warn that the adapter lost class based instantiation */
1924 if (adapter
->class == I2C_CLASS_DEPRECATED
) {
1925 dev_dbg(&adapter
->dev
,
1926 "This adapter dropped support for I2C classes and "
1927 "won't auto-detect %s devices anymore. If you need it, check "
1928 "'Documentation/i2c/instantiating-devices' for alternatives.\n",
1929 driver
->driver
.name
);
1933 /* Stop here if the classes do not match */
1934 if (!(adapter
->class & driver
->class))
1937 /* Set up a temporary client to help detect callback */
1938 temp_client
= kzalloc(sizeof(struct i2c_client
), GFP_KERNEL
);
1941 temp_client
->adapter
= adapter
;
1943 for (i
= 0; address_list
[i
] != I2C_CLIENT_END
; i
+= 1) {
1944 dev_dbg(&adapter
->dev
, "found normal entry for adapter %d, "
1945 "addr 0x%02x\n", adap_id
, address_list
[i
]);
1946 temp_client
->addr
= address_list
[i
];
1947 err
= i2c_detect_address(temp_client
, driver
);
1956 int i2c_probe_func_quick_read(struct i2c_adapter
*adap
, unsigned short addr
)
1958 return i2c_smbus_xfer(adap
, addr
, 0, I2C_SMBUS_READ
, 0,
1959 I2C_SMBUS_QUICK
, NULL
) >= 0;
1961 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read
);
1964 i2c_new_probed_device(struct i2c_adapter
*adap
,
1965 struct i2c_board_info
*info
,
1966 unsigned short const *addr_list
,
1967 int (*probe
)(struct i2c_adapter
*, unsigned short addr
))
1972 probe
= i2c_default_probe
;
1974 for (i
= 0; addr_list
[i
] != I2C_CLIENT_END
; i
++) {
1975 /* Check address validity */
1976 if (i2c_check_addr_validity(addr_list
[i
]) < 0) {
1977 dev_warn(&adap
->dev
, "Invalid 7-bit address "
1978 "0x%02x\n", addr_list
[i
]);
1982 /* Check address availability */
1983 if (i2c_check_addr_busy(adap
, addr_list
[i
])) {
1984 dev_dbg(&adap
->dev
, "Address 0x%02x already in "
1985 "use, not probing\n", addr_list
[i
]);
1989 /* Test address responsiveness */
1990 if (probe(adap
, addr_list
[i
]))
1994 if (addr_list
[i
] == I2C_CLIENT_END
) {
1995 dev_dbg(&adap
->dev
, "Probing failed, no device found\n");
1999 info
->addr
= addr_list
[i
];
2000 return i2c_new_device(adap
, info
);
2002 EXPORT_SYMBOL_GPL(i2c_new_probed_device
);
2004 struct i2c_adapter
*i2c_get_adapter(int nr
)
2006 struct i2c_adapter
*adapter
;
2008 mutex_lock(&core_lock
);
2009 adapter
= idr_find(&i2c_adapter_idr
, nr
);
2010 if (adapter
&& !try_module_get(adapter
->owner
))
2013 mutex_unlock(&core_lock
);
2016 EXPORT_SYMBOL(i2c_get_adapter
);
2018 void i2c_put_adapter(struct i2c_adapter
*adap
)
2021 module_put(adap
->owner
);
2023 EXPORT_SYMBOL(i2c_put_adapter
);
2025 /* The SMBus parts */
2027 #define POLY (0x1070U << 3)
2028 static u8
crc8(u16 data
)
2032 for (i
= 0; i
< 8; i
++) {
2037 return (u8
)(data
>> 8);
2040 /* Incremental CRC8 over count bytes in the array pointed to by p */
2041 static u8
i2c_smbus_pec(u8 crc
, u8
*p
, size_t count
)
2045 for (i
= 0; i
< count
; i
++)
2046 crc
= crc8((crc
^ p
[i
]) << 8);
2050 /* Assume a 7-bit address, which is reasonable for SMBus */
2051 static u8
i2c_smbus_msg_pec(u8 pec
, struct i2c_msg
*msg
)
2053 /* The address will be sent first */
2054 u8 addr
= (msg
->addr
<< 1) | !!(msg
->flags
& I2C_M_RD
);
2055 pec
= i2c_smbus_pec(pec
, &addr
, 1);
2057 /* The data buffer follows */
2058 return i2c_smbus_pec(pec
, msg
->buf
, msg
->len
);
2061 /* Used for write only transactions */
2062 static inline void i2c_smbus_add_pec(struct i2c_msg
*msg
)
2064 msg
->buf
[msg
->len
] = i2c_smbus_msg_pec(0, msg
);
2068 /* Return <0 on CRC error
2069 If there was a write before this read (most cases) we need to take the
2070 partial CRC from the write part into account.
2071 Note that this function does modify the message (we need to decrease the
2072 message length to hide the CRC byte from the caller). */
2073 static int i2c_smbus_check_pec(u8 cpec
, struct i2c_msg
*msg
)
2075 u8 rpec
= msg
->buf
[--msg
->len
];
2076 cpec
= i2c_smbus_msg_pec(cpec
, msg
);
2079 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
2087 * i2c_smbus_read_byte - SMBus "receive byte" protocol
2088 * @client: Handle to slave device
2090 * This executes the SMBus "receive byte" protocol, returning negative errno
2091 * else the byte received from the device.
2093 s32
i2c_smbus_read_byte(const struct i2c_client
*client
)
2095 union i2c_smbus_data data
;
2098 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2100 I2C_SMBUS_BYTE
, &data
);
2101 return (status
< 0) ? status
: data
.byte
;
2103 EXPORT_SYMBOL(i2c_smbus_read_byte
);
2106 * i2c_smbus_write_byte - SMBus "send byte" protocol
2107 * @client: Handle to slave device
2108 * @value: Byte to be sent
2110 * This executes the SMBus "send byte" protocol, returning negative errno
2111 * else zero on success.
2113 s32
i2c_smbus_write_byte(const struct i2c_client
*client
, u8 value
)
2115 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2116 I2C_SMBUS_WRITE
, value
, I2C_SMBUS_BYTE
, NULL
);
2118 EXPORT_SYMBOL(i2c_smbus_write_byte
);
2121 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
2122 * @client: Handle to slave device
2123 * @command: Byte interpreted by slave
2125 * This executes the SMBus "read byte" protocol, returning negative errno
2126 * else a data byte received from the device.
2128 s32
i2c_smbus_read_byte_data(const struct i2c_client
*client
, u8 command
)
2130 union i2c_smbus_data data
;
2133 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2134 I2C_SMBUS_READ
, command
,
2135 I2C_SMBUS_BYTE_DATA
, &data
);
2136 return (status
< 0) ? status
: data
.byte
;
2138 EXPORT_SYMBOL(i2c_smbus_read_byte_data
);
2141 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2142 * @client: Handle to slave device
2143 * @command: Byte interpreted by slave
2144 * @value: Byte being written
2146 * This executes the SMBus "write byte" protocol, returning negative errno
2147 * else zero on success.
2149 s32
i2c_smbus_write_byte_data(const struct i2c_client
*client
, u8 command
,
2152 union i2c_smbus_data data
;
2154 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2155 I2C_SMBUS_WRITE
, command
,
2156 I2C_SMBUS_BYTE_DATA
, &data
);
2158 EXPORT_SYMBOL(i2c_smbus_write_byte_data
);
2161 * i2c_smbus_read_word_data - SMBus "read word" protocol
2162 * @client: Handle to slave device
2163 * @command: Byte interpreted by slave
2165 * This executes the SMBus "read word" protocol, returning negative errno
2166 * else a 16-bit unsigned "word" received from the device.
2168 s32
i2c_smbus_read_word_data(const struct i2c_client
*client
, u8 command
)
2170 union i2c_smbus_data data
;
2173 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2174 I2C_SMBUS_READ
, command
,
2175 I2C_SMBUS_WORD_DATA
, &data
);
2176 return (status
< 0) ? status
: data
.word
;
2178 EXPORT_SYMBOL(i2c_smbus_read_word_data
);
2181 * i2c_smbus_write_word_data - SMBus "write word" protocol
2182 * @client: Handle to slave device
2183 * @command: Byte interpreted by slave
2184 * @value: 16-bit "word" being written
2186 * This executes the SMBus "write word" protocol, returning negative errno
2187 * else zero on success.
2189 s32
i2c_smbus_write_word_data(const struct i2c_client
*client
, u8 command
,
2192 union i2c_smbus_data data
;
2194 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2195 I2C_SMBUS_WRITE
, command
,
2196 I2C_SMBUS_WORD_DATA
, &data
);
2198 EXPORT_SYMBOL(i2c_smbus_write_word_data
);
2201 * i2c_smbus_read_block_data - SMBus "block read" protocol
2202 * @client: Handle to slave device
2203 * @command: Byte interpreted by slave
2204 * @values: Byte array into which data will be read; big enough to hold
2205 * the data returned by the slave. SMBus allows at most 32 bytes.
2207 * This executes the SMBus "block read" protocol, returning negative errno
2208 * else the number of data bytes in the slave's response.
2210 * Note that using this function requires that the client's adapter support
2211 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2212 * support this; its emulation through I2C messaging relies on a specific
2213 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2215 s32
i2c_smbus_read_block_data(const struct i2c_client
*client
, u8 command
,
2218 union i2c_smbus_data data
;
2221 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2222 I2C_SMBUS_READ
, command
,
2223 I2C_SMBUS_BLOCK_DATA
, &data
);
2227 memcpy(values
, &data
.block
[1], data
.block
[0]);
2228 return data
.block
[0];
2230 EXPORT_SYMBOL(i2c_smbus_read_block_data
);
2233 * i2c_smbus_write_block_data - SMBus "block write" protocol
2234 * @client: Handle to slave device
2235 * @command: Byte interpreted by slave
2236 * @length: Size of data block; SMBus allows at most 32 bytes
2237 * @values: Byte array which will be written.
2239 * This executes the SMBus "block write" protocol, returning negative errno
2240 * else zero on success.
2242 s32
i2c_smbus_write_block_data(const struct i2c_client
*client
, u8 command
,
2243 u8 length
, const u8
*values
)
2245 union i2c_smbus_data data
;
2247 if (length
> I2C_SMBUS_BLOCK_MAX
)
2248 length
= I2C_SMBUS_BLOCK_MAX
;
2249 data
.block
[0] = length
;
2250 memcpy(&data
.block
[1], values
, length
);
2251 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2252 I2C_SMBUS_WRITE
, command
,
2253 I2C_SMBUS_BLOCK_DATA
, &data
);
2255 EXPORT_SYMBOL(i2c_smbus_write_block_data
);
2257 /* Returns the number of read bytes */
2258 s32
i2c_smbus_read_i2c_block_data(const struct i2c_client
*client
, u8 command
,
2259 u8 length
, u8
*values
)
2261 union i2c_smbus_data data
;
2264 if (length
> I2C_SMBUS_BLOCK_MAX
)
2265 length
= I2C_SMBUS_BLOCK_MAX
;
2266 data
.block
[0] = length
;
2267 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2268 I2C_SMBUS_READ
, command
,
2269 I2C_SMBUS_I2C_BLOCK_DATA
, &data
);
2273 memcpy(values
, &data
.block
[1], data
.block
[0]);
2274 return data
.block
[0];
2276 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data
);
2278 s32
i2c_smbus_write_i2c_block_data(const struct i2c_client
*client
, u8 command
,
2279 u8 length
, const u8
*values
)
2281 union i2c_smbus_data data
;
2283 if (length
> I2C_SMBUS_BLOCK_MAX
)
2284 length
= I2C_SMBUS_BLOCK_MAX
;
2285 data
.block
[0] = length
;
2286 memcpy(data
.block
+ 1, values
, length
);
2287 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
2288 I2C_SMBUS_WRITE
, command
,
2289 I2C_SMBUS_I2C_BLOCK_DATA
, &data
);
2291 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data
);
2293 /* Simulate a SMBus command using the i2c protocol
2294 No checking of parameters is done! */
2295 static s32
i2c_smbus_xfer_emulated(struct i2c_adapter
*adapter
, u16 addr
,
2296 unsigned short flags
,
2297 char read_write
, u8 command
, int size
,
2298 union i2c_smbus_data
*data
)
2300 /* So we need to generate a series of msgs. In the case of writing, we
2301 need to use only one message; when reading, we need two. We initialize
2302 most things with sane defaults, to keep the code below somewhat
2304 unsigned char msgbuf0
[I2C_SMBUS_BLOCK_MAX
+3];
2305 unsigned char msgbuf1
[I2C_SMBUS_BLOCK_MAX
+2];
2306 int num
= read_write
== I2C_SMBUS_READ
? 2 : 1;
2310 struct i2c_msg msg
[2] = {
2318 .flags
= flags
| I2C_M_RD
,
2324 msgbuf0
[0] = command
;
2326 case I2C_SMBUS_QUICK
:
2328 /* Special case: The read/write field is used as data */
2329 msg
[0].flags
= flags
| (read_write
== I2C_SMBUS_READ
?
2333 case I2C_SMBUS_BYTE
:
2334 if (read_write
== I2C_SMBUS_READ
) {
2335 /* Special case: only a read! */
2336 msg
[0].flags
= I2C_M_RD
| flags
;
2340 case I2C_SMBUS_BYTE_DATA
:
2341 if (read_write
== I2C_SMBUS_READ
)
2345 msgbuf0
[1] = data
->byte
;
2348 case I2C_SMBUS_WORD_DATA
:
2349 if (read_write
== I2C_SMBUS_READ
)
2353 msgbuf0
[1] = data
->word
& 0xff;
2354 msgbuf0
[2] = data
->word
>> 8;
2357 case I2C_SMBUS_PROC_CALL
:
2358 num
= 2; /* Special case */
2359 read_write
= I2C_SMBUS_READ
;
2362 msgbuf0
[1] = data
->word
& 0xff;
2363 msgbuf0
[2] = data
->word
>> 8;
2365 case I2C_SMBUS_BLOCK_DATA
:
2366 if (read_write
== I2C_SMBUS_READ
) {
2367 msg
[1].flags
|= I2C_M_RECV_LEN
;
2368 msg
[1].len
= 1; /* block length will be added by
2369 the underlying bus driver */
2371 msg
[0].len
= data
->block
[0] + 2;
2372 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 2) {
2373 dev_err(&adapter
->dev
,
2374 "Invalid block write size %d\n",
2378 for (i
= 1; i
< msg
[0].len
; i
++)
2379 msgbuf0
[i
] = data
->block
[i
-1];
2382 case I2C_SMBUS_BLOCK_PROC_CALL
:
2383 num
= 2; /* Another special case */
2384 read_write
= I2C_SMBUS_READ
;
2385 if (data
->block
[0] > I2C_SMBUS_BLOCK_MAX
) {
2386 dev_err(&adapter
->dev
,
2387 "Invalid block write size %d\n",
2391 msg
[0].len
= data
->block
[0] + 2;
2392 for (i
= 1; i
< msg
[0].len
; i
++)
2393 msgbuf0
[i
] = data
->block
[i
-1];
2394 msg
[1].flags
|= I2C_M_RECV_LEN
;
2395 msg
[1].len
= 1; /* block length will be added by
2396 the underlying bus driver */
2398 case I2C_SMBUS_I2C_BLOCK_DATA
:
2399 if (read_write
== I2C_SMBUS_READ
) {
2400 msg
[1].len
= data
->block
[0];
2402 msg
[0].len
= data
->block
[0] + 1;
2403 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 1) {
2404 dev_err(&adapter
->dev
,
2405 "Invalid block write size %d\n",
2409 for (i
= 1; i
<= data
->block
[0]; i
++)
2410 msgbuf0
[i
] = data
->block
[i
];
2414 dev_err(&adapter
->dev
, "Unsupported transaction %d\n", size
);
2418 i
= ((flags
& I2C_CLIENT_PEC
) && size
!= I2C_SMBUS_QUICK
2419 && size
!= I2C_SMBUS_I2C_BLOCK_DATA
);
2421 /* Compute PEC if first message is a write */
2422 if (!(msg
[0].flags
& I2C_M_RD
)) {
2423 if (num
== 1) /* Write only */
2424 i2c_smbus_add_pec(&msg
[0]);
2425 else /* Write followed by read */
2426 partial_pec
= i2c_smbus_msg_pec(0, &msg
[0]);
2428 /* Ask for PEC if last message is a read */
2429 if (msg
[num
-1].flags
& I2C_M_RD
)
2433 status
= i2c_transfer(adapter
, msg
, num
);
2437 /* Check PEC if last message is a read */
2438 if (i
&& (msg
[num
-1].flags
& I2C_M_RD
)) {
2439 status
= i2c_smbus_check_pec(partial_pec
, &msg
[num
-1]);
2444 if (read_write
== I2C_SMBUS_READ
)
2446 case I2C_SMBUS_BYTE
:
2447 data
->byte
= msgbuf0
[0];
2449 case I2C_SMBUS_BYTE_DATA
:
2450 data
->byte
= msgbuf1
[0];
2452 case I2C_SMBUS_WORD_DATA
:
2453 case I2C_SMBUS_PROC_CALL
:
2454 data
->word
= msgbuf1
[0] | (msgbuf1
[1] << 8);
2456 case I2C_SMBUS_I2C_BLOCK_DATA
:
2457 for (i
= 0; i
< data
->block
[0]; i
++)
2458 data
->block
[i
+1] = msgbuf1
[i
];
2460 case I2C_SMBUS_BLOCK_DATA
:
2461 case I2C_SMBUS_BLOCK_PROC_CALL
:
2462 for (i
= 0; i
< msgbuf1
[0] + 1; i
++)
2463 data
->block
[i
] = msgbuf1
[i
];
2470 * i2c_smbus_xfer - execute SMBus protocol operations
2471 * @adapter: Handle to I2C bus
2472 * @addr: Address of SMBus slave on that bus
2473 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2474 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2475 * @command: Byte interpreted by slave, for protocols which use such bytes
2476 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2477 * @data: Data to be read or written
2479 * This executes an SMBus protocol operation, and returns a negative
2480 * errno code else zero on success.
2482 s32
i2c_smbus_xfer(struct i2c_adapter
*adapter
, u16 addr
, unsigned short flags
,
2483 char read_write
, u8 command
, int protocol
,
2484 union i2c_smbus_data
*data
)
2486 unsigned long orig_jiffies
;
2490 /* If enabled, the following two tracepoints are conditional on
2491 * read_write and protocol.
2493 trace_smbus_write(adapter
, addr
, flags
, read_write
,
2494 command
, protocol
, data
);
2495 trace_smbus_read(adapter
, addr
, flags
, read_write
,
2498 flags
&= I2C_M_TEN
| I2C_CLIENT_PEC
| I2C_CLIENT_SCCB
;
2500 if (adapter
->algo
->smbus_xfer
) {
2501 i2c_lock_adapter(adapter
);
2503 /* Retry automatically on arbitration loss */
2504 orig_jiffies
= jiffies
;
2505 for (res
= 0, try = 0; try <= adapter
->retries
; try++) {
2506 res
= adapter
->algo
->smbus_xfer(adapter
, addr
, flags
,
2507 read_write
, command
,
2511 if (time_after(jiffies
,
2512 orig_jiffies
+ adapter
->timeout
))
2515 i2c_unlock_adapter(adapter
);
2517 if (res
!= -EOPNOTSUPP
|| !adapter
->algo
->master_xfer
)
2520 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2521 * implement native support for the SMBus operation.
2525 res
= i2c_smbus_xfer_emulated(adapter
, addr
, flags
, read_write
,
2526 command
, protocol
, data
);
2529 /* If enabled, the reply tracepoint is conditional on read_write. */
2530 trace_smbus_reply(adapter
, addr
, flags
, read_write
,
2531 command
, protocol
, data
);
2532 trace_smbus_result(adapter
, addr
, flags
, read_write
,
2533 command
, protocol
, res
);
2537 EXPORT_SYMBOL(i2c_smbus_xfer
);
2539 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2540 MODULE_DESCRIPTION("I2C-Bus main module");
2541 MODULE_LICENSE("GPL");