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 <khali@linux-fr.org>
25 Mux support by Rodolfo Giometti <giometti@enneenne.com> and
26 Michael Lawnick <michael.lawnick.ext@nsn.com> */
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/delay.h>
31 #include <linux/errno.h>
32 #include <linux/gpio.h>
33 #include <linux/slab.h>
34 #include <linux/i2c.h>
35 #include <linux/init.h>
36 #include <linux/idr.h>
37 #include <linux/mutex.h>
38 #include <linux/of_device.h>
39 #include <linux/completion.h>
40 #include <linux/hardirq.h>
41 #include <linux/irqflags.h>
42 #include <linux/rwsem.h>
43 #include <linux/pm_runtime.h>
44 #include <linux/acpi.h>
45 #include <asm/uaccess.h>
50 /* core_lock protects i2c_adapter_idr, and guarantees
51 that device detection, deletion of detected devices, and attach_adapter
52 calls are serialized */
53 static DEFINE_MUTEX(core_lock);
54 static DEFINE_IDR(i2c_adapter_idr);
56 static struct device_type i2c_client_type;
57 static int i2c_check_addr_ex(struct i2c_adapter *adapter, int addr);
58 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
60 /* ------------------------------------------------------------------------- */
62 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
63 const struct i2c_client *client)
66 if (strcmp(client->name, id->name) == 0)
73 static int i2c_device_match(struct device *dev, struct device_driver *drv)
75 struct i2c_client *client = i2c_verify_client(dev);
76 struct i2c_driver *driver;
81 /* Attempt an OF style match */
82 if (of_driver_match_device(dev, drv))
85 /* Then ACPI style match */
86 if (acpi_driver_match_device(dev, drv))
89 driver = to_i2c_driver(drv);
90 /* match on an id table if there is one */
92 return i2c_match_id(driver->id_table, client) != NULL;
98 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
99 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
101 struct i2c_client *client = to_i2c_client(dev);
103 if (add_uevent_var(env, "MODALIAS=%s%s",
104 I2C_MODULE_PREFIX, client->name))
106 dev_dbg(dev, "uevent\n");
110 /* i2c bus recovery routines */
111 static int get_scl_gpio_value(struct i2c_adapter *adap)
113 return gpio_get_value(adap->bus_recovery_info->scl_gpio);
116 static void set_scl_gpio_value(struct i2c_adapter *adap, int val)
118 gpio_set_value(adap->bus_recovery_info->scl_gpio, val);
121 static int get_sda_gpio_value(struct i2c_adapter *adap)
123 return gpio_get_value(adap->bus_recovery_info->sda_gpio);
126 static int i2c_get_gpios_for_recovery(struct i2c_adapter *adap)
128 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
129 struct device *dev = &adap->dev;
132 ret = gpio_request_one(bri->scl_gpio, GPIOF_OPEN_DRAIN |
133 GPIOF_OUT_INIT_HIGH, "i2c-scl");
135 dev_warn(dev, "Can't get SCL gpio: %d\n", bri->scl_gpio);
140 if (gpio_request_one(bri->sda_gpio, GPIOF_IN, "i2c-sda")) {
141 /* work without SDA polling */
142 dev_warn(dev, "Can't get SDA gpio: %d. Not using SDA polling\n",
151 static void i2c_put_gpios_for_recovery(struct i2c_adapter *adap)
153 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
156 gpio_free(bri->sda_gpio);
158 gpio_free(bri->scl_gpio);
162 * We are generating clock pulses. ndelay() determines durating of clk pulses.
163 * We will generate clock with rate 100 KHz and so duration of both clock levels
164 * is: delay in ns = (10^6 / 100) / 2
166 #define RECOVERY_NDELAY 5000
167 #define RECOVERY_CLK_CNT 9
169 static int i2c_generic_recovery(struct i2c_adapter *adap)
171 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
172 int i = 0, val = 1, ret = 0;
174 if (bri->prepare_recovery)
175 bri->prepare_recovery(bri);
178 * By this time SCL is high, as we need to give 9 falling-rising edges
180 while (i++ < RECOVERY_CLK_CNT * 2) {
182 /* Break if SDA is high */
183 if (bri->get_sda && bri->get_sda(adap))
185 /* SCL shouldn't be low here */
186 if (!bri->get_scl(adap)) {
188 "SCL is stuck low, exit recovery\n");
195 bri->set_scl(adap, val);
196 ndelay(RECOVERY_NDELAY);
199 if (bri->unprepare_recovery)
200 bri->unprepare_recovery(bri);
205 int i2c_generic_scl_recovery(struct i2c_adapter *adap)
207 adap->bus_recovery_info->set_scl(adap, 1);
208 return i2c_generic_recovery(adap);
211 int i2c_generic_gpio_recovery(struct i2c_adapter *adap)
215 ret = i2c_get_gpios_for_recovery(adap);
219 ret = i2c_generic_recovery(adap);
220 i2c_put_gpios_for_recovery(adap);
225 int i2c_recover_bus(struct i2c_adapter *adap)
227 if (!adap->bus_recovery_info)
230 dev_dbg(&adap->dev, "Trying i2c bus recovery\n");
231 return adap->bus_recovery_info->recover_bus(adap);
234 static int i2c_device_probe(struct device *dev)
236 struct i2c_client *client = i2c_verify_client(dev);
237 struct i2c_driver *driver;
243 driver = to_i2c_driver(dev->driver);
244 if (!driver->probe || !driver->id_table)
246 client->driver = driver;
247 if (!device_can_wakeup(&client->dev))
248 device_init_wakeup(&client->dev,
249 client->flags & I2C_CLIENT_WAKE);
250 dev_dbg(dev, "probe\n");
252 status = driver->probe(client, i2c_match_id(driver->id_table, client));
254 client->driver = NULL;
255 i2c_set_clientdata(client, NULL);
260 static int i2c_device_remove(struct device *dev)
262 struct i2c_client *client = i2c_verify_client(dev);
263 struct i2c_driver *driver;
266 if (!client || !dev->driver)
269 driver = to_i2c_driver(dev->driver);
270 if (driver->remove) {
271 dev_dbg(dev, "remove\n");
272 status = driver->remove(client);
278 client->driver = NULL;
279 i2c_set_clientdata(client, NULL);
284 static void i2c_device_shutdown(struct device *dev)
286 struct i2c_client *client = i2c_verify_client(dev);
287 struct i2c_driver *driver;
289 if (!client || !dev->driver)
291 driver = to_i2c_driver(dev->driver);
292 if (driver->shutdown)
293 driver->shutdown(client);
296 #ifdef CONFIG_PM_SLEEP
297 static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
299 struct i2c_client *client = i2c_verify_client(dev);
300 struct i2c_driver *driver;
302 if (!client || !dev->driver)
304 driver = to_i2c_driver(dev->driver);
305 if (!driver->suspend)
307 return driver->suspend(client, mesg);
310 static int i2c_legacy_resume(struct device *dev)
312 struct i2c_client *client = i2c_verify_client(dev);
313 struct i2c_driver *driver;
315 if (!client || !dev->driver)
317 driver = to_i2c_driver(dev->driver);
320 return driver->resume(client);
323 static int i2c_device_pm_suspend(struct device *dev)
325 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
328 return pm_generic_suspend(dev);
330 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
333 static int i2c_device_pm_resume(struct device *dev)
335 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
338 return pm_generic_resume(dev);
340 return i2c_legacy_resume(dev);
343 static int i2c_device_pm_freeze(struct device *dev)
345 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
348 return pm_generic_freeze(dev);
350 return i2c_legacy_suspend(dev, PMSG_FREEZE);
353 static int i2c_device_pm_thaw(struct device *dev)
355 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
358 return pm_generic_thaw(dev);
360 return i2c_legacy_resume(dev);
363 static int i2c_device_pm_poweroff(struct device *dev)
365 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
368 return pm_generic_poweroff(dev);
370 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
373 static int i2c_device_pm_restore(struct device *dev)
375 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
378 return pm_generic_restore(dev);
380 return i2c_legacy_resume(dev);
382 #else /* !CONFIG_PM_SLEEP */
383 #define i2c_device_pm_suspend NULL
384 #define i2c_device_pm_resume NULL
385 #define i2c_device_pm_freeze NULL
386 #define i2c_device_pm_thaw NULL
387 #define i2c_device_pm_poweroff NULL
388 #define i2c_device_pm_restore NULL
389 #endif /* !CONFIG_PM_SLEEP */
391 static void i2c_client_dev_release(struct device *dev)
393 kfree(to_i2c_client(dev));
397 show_name(struct device *dev, struct device_attribute *attr, char *buf)
399 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
400 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
404 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
406 struct i2c_client *client = to_i2c_client(dev);
407 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
410 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
411 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
413 static struct attribute *i2c_dev_attrs[] = {
415 /* modalias helps coldplug: modprobe $(cat .../modalias) */
416 &dev_attr_modalias.attr,
420 static struct attribute_group i2c_dev_attr_group = {
421 .attrs = i2c_dev_attrs,
424 static const struct attribute_group *i2c_dev_attr_groups[] = {
429 static const struct dev_pm_ops i2c_device_pm_ops = {
430 .suspend = i2c_device_pm_suspend,
431 .resume = i2c_device_pm_resume,
432 .freeze = i2c_device_pm_freeze,
433 .thaw = i2c_device_pm_thaw,
434 .poweroff = i2c_device_pm_poweroff,
435 .restore = i2c_device_pm_restore,
437 pm_generic_runtime_suspend,
438 pm_generic_runtime_resume,
443 struct bus_type i2c_bus_type = {
445 .match = i2c_device_match,
446 .probe = i2c_device_probe,
447 .remove = i2c_device_remove,
448 .shutdown = i2c_device_shutdown,
449 .pm = &i2c_device_pm_ops,
451 EXPORT_SYMBOL_GPL(i2c_bus_type);
453 static struct device_type i2c_client_type = {
454 .groups = i2c_dev_attr_groups,
455 .uevent = i2c_device_uevent,
456 .release = i2c_client_dev_release,
461 * i2c_verify_client - return parameter as i2c_client, or NULL
462 * @dev: device, probably from some driver model iterator
464 * When traversing the driver model tree, perhaps using driver model
465 * iterators like @device_for_each_child(), you can't assume very much
466 * about the nodes you find. Use this function to avoid oopses caused
467 * by wrongly treating some non-I2C device as an i2c_client.
469 struct i2c_client *i2c_verify_client(struct device *dev)
471 return (dev->type == &i2c_client_type)
475 EXPORT_SYMBOL(i2c_verify_client);
478 /* This is a permissive address validity check, I2C address map constraints
479 * are purposely not enforced, except for the general call address. */
480 static int i2c_check_client_addr_validity(const struct i2c_client *client)
482 if (client->flags & I2C_CLIENT_TEN) {
483 /* 10-bit address, all values are valid */
484 if (client->addr > 0x3ff)
487 /* 7-bit address, reject the general call address */
488 if (client->addr == 0x00 || client->addr > 0x7f)
494 /* And this is a strict address validity check, used when probing. If a
495 * device uses a reserved address, then it shouldn't be probed. 7-bit
496 * addressing is assumed, 10-bit address devices are rare and should be
497 * explicitly enumerated. */
498 static int i2c_check_addr_validity(unsigned short addr)
501 * Reserved addresses per I2C specification:
502 * 0x00 General call address / START byte
504 * 0x02 Reserved for different bus format
505 * 0x03 Reserved for future purposes
506 * 0x04-0x07 Hs-mode master code
507 * 0x78-0x7b 10-bit slave addressing
508 * 0x7c-0x7f Reserved for future purposes
510 if (addr < 0x08 || addr > 0x77)
515 static int __i2c_check_addr_busy(struct device *dev, void *addrp)
517 struct i2c_client *client = i2c_verify_client(dev);
518 int addr = *(int *)addrp;
520 if (client && client->addr == addr)
525 /* walk up mux tree */
526 static int i2c_check_mux_parents(struct i2c_adapter *adapter, int addr)
528 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
531 result = device_for_each_child(&adapter->dev, &addr,
532 __i2c_check_addr_busy);
534 if (!result && parent)
535 result = i2c_check_mux_parents(parent, addr);
540 /* recurse down mux tree */
541 static int i2c_check_mux_children(struct device *dev, void *addrp)
545 if (dev->type == &i2c_adapter_type)
546 result = device_for_each_child(dev, addrp,
547 i2c_check_mux_children);
549 result = __i2c_check_addr_busy(dev, addrp);
554 static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
556 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
560 result = i2c_check_mux_parents(parent, addr);
563 result = device_for_each_child(&adapter->dev, &addr,
564 i2c_check_mux_children);
570 * i2c_lock_adapter - Get exclusive access to an I2C bus segment
571 * @adapter: Target I2C bus segment
573 void i2c_lock_adapter(struct i2c_adapter *adapter)
575 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
578 i2c_lock_adapter(parent);
580 rt_mutex_lock(&adapter->bus_lock);
582 EXPORT_SYMBOL_GPL(i2c_lock_adapter);
585 * i2c_trylock_adapter - Try to get exclusive access to an I2C bus segment
586 * @adapter: Target I2C bus segment
588 static int i2c_trylock_adapter(struct i2c_adapter *adapter)
590 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
593 return i2c_trylock_adapter(parent);
595 return rt_mutex_trylock(&adapter->bus_lock);
599 * i2c_unlock_adapter - Release exclusive access to an I2C bus segment
600 * @adapter: Target I2C bus segment
602 void i2c_unlock_adapter(struct i2c_adapter *adapter)
604 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
607 i2c_unlock_adapter(parent);
609 rt_mutex_unlock(&adapter->bus_lock);
611 EXPORT_SYMBOL_GPL(i2c_unlock_adapter);
614 * i2c_new_device - instantiate an i2c device
615 * @adap: the adapter managing the device
616 * @info: describes one I2C device; bus_num is ignored
619 * Create an i2c device. Binding is handled through driver model
620 * probe()/remove() methods. A driver may be bound to this device when we
621 * return from this function, or any later moment (e.g. maybe hotplugging will
622 * load the driver module). This call is not appropriate for use by mainboard
623 * initialization logic, which usually runs during an arch_initcall() long
624 * before any i2c_adapter could exist.
626 * This returns the new i2c client, which may be saved for later use with
627 * i2c_unregister_device(); or NULL to indicate an error.
630 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
632 struct i2c_client *client;
635 client = kzalloc(sizeof *client, GFP_KERNEL);
639 client->adapter = adap;
641 client->dev.platform_data = info->platform_data;
644 client->dev.archdata = *info->archdata;
646 client->flags = info->flags;
647 client->addr = info->addr;
648 client->irq = info->irq;
650 strlcpy(client->name, info->type, sizeof(client->name));
652 /* Check for address validity */
653 status = i2c_check_client_addr_validity(client);
655 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
656 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
660 /* Check for address business */
662 status = i2c_check_addr_busy(adap, client->addr);
666 /* ddl@rock-chips.com : Devices which have some i2c addr can work in same i2c bus,
667 if devices havn't work at the same time.*/
668 status = i2c_check_addr_ex(adap, client->addr);
670 dev_err(&adap->dev, "%d i2c clients have been registered at 0x%02x",
671 status, client->addr);
674 client->dev.parent = &client->adapter->dev;
675 client->dev.bus = &i2c_bus_type;
676 client->dev.type = &i2c_client_type;
677 client->dev.of_node = info->of_node;
678 ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle);
680 /* For 10-bit clients, add an arbitrary offset to avoid collisions */
682 /* ddl@rock-chips.com : Devices which have some i2c addr can work in same i2c bus,
683 if devices havn't work at the same time.*/
685 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
686 client->addr | ((client->flags & I2C_CLIENT_TEN)
690 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
693 dev_set_name(&client->dev, "%d-%04x-%01x", i2c_adapter_id(adap),
694 client->addr,status);
697 status = device_register(&client->dev);
701 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
702 client->name, dev_name(&client->dev));
707 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
708 "(%d)\n", client->name, client->addr, status);
713 EXPORT_SYMBOL_GPL(i2c_new_device);
717 * i2c_unregister_device - reverse effect of i2c_new_device()
718 * @client: value returned from i2c_new_device()
721 void i2c_unregister_device(struct i2c_client *client)
723 device_unregister(&client->dev);
725 EXPORT_SYMBOL_GPL(i2c_unregister_device);
728 static const struct i2c_device_id dummy_id[] = {
733 static int dummy_probe(struct i2c_client *client,
734 const struct i2c_device_id *id)
739 static int dummy_remove(struct i2c_client *client)
744 static struct i2c_driver dummy_driver = {
745 .driver.name = "dummy",
746 .probe = dummy_probe,
747 .remove = dummy_remove,
748 .id_table = dummy_id,
752 * i2c_new_dummy - return a new i2c device bound to a dummy driver
753 * @adapter: the adapter managing the device
754 * @address: seven bit address to be used
757 * This returns an I2C client bound to the "dummy" driver, intended for use
758 * with devices that consume multiple addresses. Examples of such chips
759 * include various EEPROMS (like 24c04 and 24c08 models).
761 * These dummy devices have two main uses. First, most I2C and SMBus calls
762 * except i2c_transfer() need a client handle; the dummy will be that handle.
763 * And second, this prevents the specified address from being bound to a
766 * This returns the new i2c client, which should be saved for later use with
767 * i2c_unregister_device(); or NULL to indicate an error.
769 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
771 struct i2c_board_info info = {
772 I2C_BOARD_INFO("dummy", address),
775 return i2c_new_device(adapter, &info);
777 EXPORT_SYMBOL_GPL(i2c_new_dummy);
779 /* ------------------------------------------------------------------------- */
781 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
783 static void i2c_adapter_dev_release(struct device *dev)
785 struct i2c_adapter *adap = to_i2c_adapter(dev);
786 complete(&adap->dev_released);
790 * This function is only needed for mutex_lock_nested, so it is never
791 * called unless locking correctness checking is enabled. Thus we
792 * make it inline to avoid a compiler warning. That's what gcc ends up
795 static inline unsigned int i2c_adapter_depth(struct i2c_adapter *adapter)
797 unsigned int depth = 0;
799 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
806 * Let users instantiate I2C devices through sysfs. This can be used when
807 * platform initialization code doesn't contain the proper data for
808 * whatever reason. Also useful for drivers that do device detection and
809 * detection fails, either because the device uses an unexpected address,
810 * or this is a compatible device with different ID register values.
812 * Parameter checking may look overzealous, but we really don't want
813 * the user to provide incorrect parameters.
816 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
817 const char *buf, size_t count)
819 struct i2c_adapter *adap = to_i2c_adapter(dev);
820 struct i2c_board_info info;
821 struct i2c_client *client;
825 memset(&info, 0, sizeof(struct i2c_board_info));
827 blank = strchr(buf, ' ');
829 dev_err(dev, "%s: Missing parameters\n", "new_device");
832 if (blank - buf > I2C_NAME_SIZE - 1) {
833 dev_err(dev, "%s: Invalid device name\n", "new_device");
836 memcpy(info.type, buf, blank - buf);
838 /* Parse remaining parameters, reject extra parameters */
839 res = sscanf(++blank, "%hi%c", &info.addr, &end);
841 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
844 if (res > 1 && end != '\n') {
845 dev_err(dev, "%s: Extra parameters\n", "new_device");
849 client = i2c_new_device(adap, &info);
853 /* Keep track of the added device */
854 mutex_lock(&adap->userspace_clients_lock);
855 list_add_tail(&client->detected, &adap->userspace_clients);
856 mutex_unlock(&adap->userspace_clients_lock);
857 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
858 info.type, info.addr);
864 * And of course let the users delete the devices they instantiated, if
865 * they got it wrong. This interface can only be used to delete devices
866 * instantiated by i2c_sysfs_new_device above. This guarantees that we
867 * don't delete devices to which some kernel code still has references.
869 * Parameter checking may look overzealous, but we really don't want
870 * the user to delete the wrong device.
873 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
874 const char *buf, size_t count)
876 struct i2c_adapter *adap = to_i2c_adapter(dev);
877 struct i2c_client *client, *next;
882 /* Parse parameters, reject extra parameters */
883 res = sscanf(buf, "%hi%c", &addr, &end);
885 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
888 if (res > 1 && end != '\n') {
889 dev_err(dev, "%s: Extra parameters\n", "delete_device");
893 /* Make sure the device was added through sysfs */
895 mutex_lock_nested(&adap->userspace_clients_lock,
896 i2c_adapter_depth(adap));
897 list_for_each_entry_safe(client, next, &adap->userspace_clients,
899 if (client->addr == addr) {
900 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
901 "delete_device", client->name, client->addr);
903 list_del(&client->detected);
904 i2c_unregister_device(client);
909 mutex_unlock(&adap->userspace_clients_lock);
912 dev_err(dev, "%s: Can't find device in list\n",
917 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
918 static DEVICE_ATTR_IGNORE_LOCKDEP(delete_device, S_IWUSR, NULL,
919 i2c_sysfs_delete_device);
921 static struct attribute *i2c_adapter_attrs[] = {
923 &dev_attr_new_device.attr,
924 &dev_attr_delete_device.attr,
928 static struct attribute_group i2c_adapter_attr_group = {
929 .attrs = i2c_adapter_attrs,
932 static const struct attribute_group *i2c_adapter_attr_groups[] = {
933 &i2c_adapter_attr_group,
937 struct device_type i2c_adapter_type = {
938 .groups = i2c_adapter_attr_groups,
939 .release = i2c_adapter_dev_release,
941 EXPORT_SYMBOL_GPL(i2c_adapter_type);
944 * i2c_verify_adapter - return parameter as i2c_adapter or NULL
945 * @dev: device, probably from some driver model iterator
947 * When traversing the driver model tree, perhaps using driver model
948 * iterators like @device_for_each_child(), you can't assume very much
949 * about the nodes you find. Use this function to avoid oopses caused
950 * by wrongly treating some non-I2C device as an i2c_adapter.
952 struct i2c_adapter *i2c_verify_adapter(struct device *dev)
954 return (dev->type == &i2c_adapter_type)
955 ? to_i2c_adapter(dev)
958 EXPORT_SYMBOL(i2c_verify_adapter);
960 #ifdef CONFIG_I2C_COMPAT
961 static struct class_compat *i2c_adapter_compat_class;
964 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
966 struct i2c_devinfo *devinfo;
968 down_read(&__i2c_board_lock);
969 list_for_each_entry(devinfo, &__i2c_board_list, list) {
970 if (devinfo->busnum == adapter->nr
971 && !i2c_new_device(adapter,
972 &devinfo->board_info))
973 dev_err(&adapter->dev,
974 "Can't create device at 0x%02x\n",
975 devinfo->board_info.addr);
977 up_read(&__i2c_board_lock);
980 static int i2c_do_add_adapter(struct i2c_driver *driver,
981 struct i2c_adapter *adap)
983 /* Detect supported devices on that bus, and instantiate them */
984 i2c_detect(adap, driver);
986 /* Let legacy drivers scan this bus for matching devices */
987 if (driver->attach_adapter) {
988 dev_warn(&adap->dev, "%s: attach_adapter method is deprecated\n",
989 driver->driver.name);
990 dev_warn(&adap->dev, "Please use another way to instantiate "
991 "your i2c_client\n");
992 /* We ignore the return code; if it fails, too bad */
993 driver->attach_adapter(adap);
998 static int __process_new_adapter(struct device_driver *d, void *data)
1000 return i2c_do_add_adapter(to_i2c_driver(d), data);
1003 static int i2c_register_adapter(struct i2c_adapter *adap)
1007 /* Can't register until after driver model init */
1008 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
1014 if (unlikely(adap->name[0] == '\0')) {
1015 pr_err("i2c-core: Attempt to register an adapter with "
1019 if (unlikely(!adap->algo)) {
1020 pr_err("i2c-core: Attempt to register adapter '%s' with "
1021 "no algo!\n", adap->name);
1025 rt_mutex_init(&adap->bus_lock);
1026 mutex_init(&adap->userspace_clients_lock);
1027 INIT_LIST_HEAD(&adap->userspace_clients);
1029 /* Set default timeout to 1 second if not already set */
1030 if (adap->timeout == 0)
1033 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
1034 adap->dev.bus = &i2c_bus_type;
1035 adap->dev.type = &i2c_adapter_type;
1036 res = device_register(&adap->dev);
1040 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
1042 #ifdef CONFIG_I2C_COMPAT
1043 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
1046 dev_warn(&adap->dev,
1047 "Failed to create compatibility class link\n");
1050 /* bus recovery specific initialization */
1051 if (adap->bus_recovery_info) {
1052 struct i2c_bus_recovery_info *bri = adap->bus_recovery_info;
1054 if (!bri->recover_bus) {
1055 dev_err(&adap->dev, "No recover_bus() found, not using recovery\n");
1056 adap->bus_recovery_info = NULL;
1060 /* Generic GPIO recovery */
1061 if (bri->recover_bus == i2c_generic_gpio_recovery) {
1062 if (!gpio_is_valid(bri->scl_gpio)) {
1063 dev_err(&adap->dev, "Invalid SCL gpio, not using recovery\n");
1064 adap->bus_recovery_info = NULL;
1068 if (gpio_is_valid(bri->sda_gpio))
1069 bri->get_sda = get_sda_gpio_value;
1071 bri->get_sda = NULL;
1073 bri->get_scl = get_scl_gpio_value;
1074 bri->set_scl = set_scl_gpio_value;
1075 } else if (!bri->set_scl || !bri->get_scl) {
1076 /* Generic SCL recovery */
1077 dev_err(&adap->dev, "No {get|set}_gpio() found, not using recovery\n");
1078 adap->bus_recovery_info = NULL;
1083 /* create pre-declared device nodes */
1084 if (adap->nr < __i2c_first_dynamic_bus_num)
1085 i2c_scan_static_board_info(adap);
1087 /* Notify drivers */
1088 mutex_lock(&core_lock);
1089 bus_for_each_drv(&i2c_bus_type, NULL, adap, __process_new_adapter);
1090 mutex_unlock(&core_lock);
1095 mutex_lock(&core_lock);
1096 idr_remove(&i2c_adapter_idr, adap->nr);
1097 mutex_unlock(&core_lock);
1102 * __i2c_add_numbered_adapter - i2c_add_numbered_adapter where nr is never -1
1103 * @adap: the adapter to register (with adap->nr initialized)
1104 * Context: can sleep
1106 * See i2c_add_numbered_adapter() for details.
1108 static int __i2c_add_numbered_adapter(struct i2c_adapter *adap)
1112 mutex_lock(&core_lock);
1113 id = idr_alloc(&i2c_adapter_idr, adap, adap->nr, adap->nr + 1,
1115 mutex_unlock(&core_lock);
1117 return id == -ENOSPC ? -EBUSY : id;
1119 return i2c_register_adapter(adap);
1123 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
1124 * @adapter: the adapter to add
1125 * Context: can sleep
1127 * This routine is used to declare an I2C adapter when its bus number
1128 * doesn't matter or when its bus number is specified by an dt alias.
1129 * Examples of bases when the bus number doesn't matter: I2C adapters
1130 * dynamically added by USB links or PCI plugin cards.
1132 * When this returns zero, a new bus number was allocated and stored
1133 * in adap->nr, and the specified adapter became available for clients.
1134 * Otherwise, a negative errno value is returned.
1136 int i2c_add_adapter(struct i2c_adapter *adapter)
1138 struct device *dev = &adapter->dev;
1142 id = of_alias_get_id(dev->of_node, "i2c");
1145 return __i2c_add_numbered_adapter(adapter);
1149 mutex_lock(&core_lock);
1150 id = idr_alloc(&i2c_adapter_idr, adapter,
1151 __i2c_first_dynamic_bus_num, 0, GFP_KERNEL);
1152 mutex_unlock(&core_lock);
1158 return i2c_register_adapter(adapter);
1160 EXPORT_SYMBOL(i2c_add_adapter);
1163 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
1164 * @adap: the adapter to register (with adap->nr initialized)
1165 * Context: can sleep
1167 * This routine is used to declare an I2C adapter when its bus number
1168 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
1169 * or otherwise built in to the system's mainboard, and where i2c_board_info
1170 * is used to properly configure I2C devices.
1172 * If the requested bus number is set to -1, then this function will behave
1173 * identically to i2c_add_adapter, and will dynamically assign a bus number.
1175 * If no devices have pre-been declared for this bus, then be sure to
1176 * register the adapter before any dynamically allocated ones. Otherwise
1177 * the required bus ID may not be available.
1179 * When this returns zero, the specified adapter became available for
1180 * clients using the bus number provided in adap->nr. Also, the table
1181 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
1182 * and the appropriate driver model device nodes are created. Otherwise, a
1183 * negative errno value is returned.
1185 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
1187 if (adap->nr == -1) /* -1 means dynamically assign bus id */
1188 return i2c_add_adapter(adap);
1190 return __i2c_add_numbered_adapter(adap);
1192 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
1194 static void i2c_do_del_adapter(struct i2c_driver *driver,
1195 struct i2c_adapter *adapter)
1197 struct i2c_client *client, *_n;
1199 /* Remove the devices we created ourselves as the result of hardware
1200 * probing (using a driver's detect method) */
1201 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
1202 if (client->adapter == adapter) {
1203 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
1204 client->name, client->addr);
1205 list_del(&client->detected);
1206 i2c_unregister_device(client);
1211 static int __unregister_client(struct device *dev, void *dummy)
1213 struct i2c_client *client = i2c_verify_client(dev);
1214 if (client && strcmp(client->name, "dummy"))
1215 i2c_unregister_device(client);
1219 static int __unregister_dummy(struct device *dev, void *dummy)
1221 struct i2c_client *client = i2c_verify_client(dev);
1223 i2c_unregister_device(client);
1227 static int __process_removed_adapter(struct device_driver *d, void *data)
1229 i2c_do_del_adapter(to_i2c_driver(d), data);
1234 * i2c_del_adapter - unregister I2C adapter
1235 * @adap: the adapter being unregistered
1236 * Context: can sleep
1238 * This unregisters an I2C adapter which was previously registered
1239 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
1241 void i2c_del_adapter(struct i2c_adapter *adap)
1243 struct i2c_adapter *found;
1244 struct i2c_client *client, *next;
1246 /* First make sure that this adapter was ever added */
1247 mutex_lock(&core_lock);
1248 found = idr_find(&i2c_adapter_idr, adap->nr);
1249 mutex_unlock(&core_lock);
1250 if (found != adap) {
1251 pr_debug("i2c-core: attempting to delete unregistered "
1252 "adapter [%s]\n", adap->name);
1256 /* Tell drivers about this removal */
1257 mutex_lock(&core_lock);
1258 bus_for_each_drv(&i2c_bus_type, NULL, adap,
1259 __process_removed_adapter);
1260 mutex_unlock(&core_lock);
1262 /* Remove devices instantiated from sysfs */
1263 mutex_lock_nested(&adap->userspace_clients_lock,
1264 i2c_adapter_depth(adap));
1265 list_for_each_entry_safe(client, next, &adap->userspace_clients,
1267 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
1269 list_del(&client->detected);
1270 i2c_unregister_device(client);
1272 mutex_unlock(&adap->userspace_clients_lock);
1274 /* Detach any active clients. This can't fail, thus we do not
1275 * check the returned value. This is a two-pass process, because
1276 * we can't remove the dummy devices during the first pass: they
1277 * could have been instantiated by real devices wishing to clean
1278 * them up properly, so we give them a chance to do that first. */
1279 device_for_each_child(&adap->dev, NULL, __unregister_client);
1280 device_for_each_child(&adap->dev, NULL, __unregister_dummy);
1282 #ifdef CONFIG_I2C_COMPAT
1283 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
1287 /* device name is gone after device_unregister */
1288 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1290 /* clean up the sysfs representation */
1291 init_completion(&adap->dev_released);
1292 device_unregister(&adap->dev);
1294 /* wait for sysfs to drop all references */
1295 wait_for_completion(&adap->dev_released);
1298 mutex_lock(&core_lock);
1299 idr_remove(&i2c_adapter_idr, adap->nr);
1300 mutex_unlock(&core_lock);
1302 /* Clear the device structure in case this adapter is ever going to be
1304 memset(&adap->dev, 0, sizeof(adap->dev));
1306 EXPORT_SYMBOL(i2c_del_adapter);
1309 /* ------------------------------------------------------------------------- */
1311 int i2c_for_each_dev(void *data, int (*fn)(struct device *, void *))
1315 mutex_lock(&core_lock);
1316 res = bus_for_each_dev(&i2c_bus_type, NULL, data, fn);
1317 mutex_unlock(&core_lock);
1321 EXPORT_SYMBOL_GPL(i2c_for_each_dev);
1323 static int __process_new_driver(struct device *dev, void *data)
1325 if (dev->type != &i2c_adapter_type)
1327 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
1331 * An i2c_driver is used with one or more i2c_client (device) nodes to access
1332 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1335 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
1339 /* Can't register until after driver model init */
1340 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1343 /* add the driver to the list of i2c drivers in the driver core */
1344 driver->driver.owner = owner;
1345 driver->driver.bus = &i2c_bus_type;
1347 /* When registration returns, the driver core
1348 * will have called probe() for all matching-but-unbound devices.
1350 res = driver_register(&driver->driver);
1354 /* Drivers should switch to dev_pm_ops instead. */
1355 if (driver->suspend)
1356 pr_warn("i2c-core: driver [%s] using legacy suspend method\n",
1357 driver->driver.name);
1359 pr_warn("i2c-core: driver [%s] using legacy resume method\n",
1360 driver->driver.name);
1362 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
1364 INIT_LIST_HEAD(&driver->clients);
1365 /* Walk the adapters that are already present */
1366 i2c_for_each_dev(driver, __process_new_driver);
1370 EXPORT_SYMBOL(i2c_register_driver);
1372 static int __process_removed_driver(struct device *dev, void *data)
1374 if (dev->type == &i2c_adapter_type)
1375 i2c_do_del_adapter(data, to_i2c_adapter(dev));
1380 * i2c_del_driver - unregister I2C driver
1381 * @driver: the driver being unregistered
1382 * Context: can sleep
1384 void i2c_del_driver(struct i2c_driver *driver)
1386 i2c_for_each_dev(driver, __process_removed_driver);
1388 driver_unregister(&driver->driver);
1389 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
1391 EXPORT_SYMBOL(i2c_del_driver);
1393 /* ------------------------------------------------------------------------- */
1394 /* ddl@rock-chips.com : Devices which have some i2c addr can work in same i2c bus,
1395 if devices havn't work at the same time.*/
1401 static int __i2c_check_addr_ex(struct device *dev, void *addrp)
1403 struct i2c_client *client = i2c_verify_client(dev);
1404 struct i2c_addr_cnt *addrinfo = (struct i2c_addr_cnt *)addrp;
1405 int addr = addrinfo->addr;
1407 if (client && client->addr == addr) {
1412 static int i2c_check_addr_ex(struct i2c_adapter *adapter, int addr)
1414 struct i2c_addr_cnt addrinfo;
1416 addrinfo.addr = addr;
1418 device_for_each_child(&adapter->dev, &addrinfo, __i2c_check_addr_ex);
1419 return addrinfo.cnt;
1423 * i2c_use_client - increments the reference count of the i2c client structure
1424 * @client: the client being referenced
1426 * Each live reference to a client should be refcounted. The driver model does
1427 * that automatically as part of driver binding, so that most drivers don't
1428 * need to do this explicitly: they hold a reference until they're unbound
1431 * A pointer to the client with the incremented reference counter is returned.
1433 struct i2c_client *i2c_use_client(struct i2c_client *client)
1435 if (client && get_device(&client->dev))
1439 EXPORT_SYMBOL(i2c_use_client);
1442 * i2c_release_client - release a use of the i2c client structure
1443 * @client: the client being no longer referenced
1445 * Must be called when a user of a client is finished with it.
1447 void i2c_release_client(struct i2c_client *client)
1450 put_device(&client->dev);
1452 EXPORT_SYMBOL(i2c_release_client);
1454 struct i2c_cmd_arg {
1459 static int i2c_cmd(struct device *dev, void *_arg)
1461 struct i2c_client *client = i2c_verify_client(dev);
1462 struct i2c_cmd_arg *arg = _arg;
1464 if (client && client->driver && client->driver->command)
1465 client->driver->command(client, arg->cmd, arg->arg);
1469 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1471 struct i2c_cmd_arg cmd_arg;
1475 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1477 EXPORT_SYMBOL(i2c_clients_command);
1479 static int __init i2c_init(void)
1483 retval = bus_register(&i2c_bus_type);
1486 #ifdef CONFIG_I2C_COMPAT
1487 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1488 if (!i2c_adapter_compat_class) {
1493 retval = i2c_add_driver(&dummy_driver);
1499 #ifdef CONFIG_I2C_COMPAT
1500 class_compat_unregister(i2c_adapter_compat_class);
1503 bus_unregister(&i2c_bus_type);
1507 static void __exit i2c_exit(void)
1509 i2c_del_driver(&dummy_driver);
1510 #ifdef CONFIG_I2C_COMPAT
1511 class_compat_unregister(i2c_adapter_compat_class);
1513 bus_unregister(&i2c_bus_type);
1516 /* We must initialize early, because some subsystems register i2c drivers
1517 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1519 postcore_initcall(i2c_init);
1520 module_exit(i2c_exit);
1522 /* ----------------------------------------------------
1523 * the functional interface to the i2c busses.
1524 * ----------------------------------------------------
1528 * __i2c_transfer - unlocked flavor of i2c_transfer
1529 * @adap: Handle to I2C bus
1530 * @msgs: One or more messages to execute before STOP is issued to
1531 * terminate the operation; each message begins with a START.
1532 * @num: Number of messages to be executed.
1534 * Returns negative errno, else the number of messages executed.
1536 * Adapter lock must be held when calling this function. No debug logging
1537 * takes place. adap->algo->master_xfer existence isn't checked.
1539 int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1541 unsigned long orig_jiffies;
1544 /* Retry automatically on arbitration loss */
1545 orig_jiffies = jiffies;
1546 for (ret = 0, try = 0; try <= adap->retries; try++) {
1547 ret = adap->algo->master_xfer(adap, msgs, num);
1550 if (time_after(jiffies, orig_jiffies + adap->timeout))
1556 EXPORT_SYMBOL(__i2c_transfer);
1559 * i2c_transfer - execute a single or combined I2C message
1560 * @adap: Handle to I2C bus
1561 * @msgs: One or more messages to execute before STOP is issued to
1562 * terminate the operation; each message begins with a START.
1563 * @num: Number of messages to be executed.
1565 * Returns negative errno, else the number of messages executed.
1567 * Note that there is no requirement that each message be sent to
1568 * the same slave address, although that is the most common model.
1570 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1574 /* REVISIT the fault reporting model here is weak:
1576 * - When we get an error after receiving N bytes from a slave,
1577 * there is no way to report "N".
1579 * - When we get a NAK after transmitting N bytes to a slave,
1580 * there is no way to report "N" ... or to let the master
1581 * continue executing the rest of this combined message, if
1582 * that's the appropriate response.
1584 * - When for example "num" is two and we successfully complete
1585 * the first message but get an error part way through the
1586 * second, it's unclear whether that should be reported as
1587 * one (discarding status on the second message) or errno
1588 * (discarding status on the first one).
1591 if (adap->algo->master_xfer) {
1593 for (ret = 0; ret < num; ret++) {
1594 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1595 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1596 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1597 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1601 if (in_atomic() || irqs_disabled()) {
1602 ret = i2c_trylock_adapter(adap);
1604 /* I2C activity is ongoing. */
1607 i2c_lock_adapter(adap);
1610 ret = __i2c_transfer(adap, msgs, num);
1611 i2c_unlock_adapter(adap);
1615 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1619 EXPORT_SYMBOL(i2c_transfer);
1622 * i2c_master_send - issue a single I2C message in master transmit mode
1623 * @client: Handle to slave device
1624 * @buf: Data that will be written to the slave
1625 * @count: How many bytes to write, must be less than 64k since msg.len is u16
1627 * Returns negative errno, or else the number of bytes written.
1629 int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
1632 struct i2c_adapter *adap = client->adapter;
1635 msg.addr = client->addr;
1636 msg.flags = client->flags & I2C_M_TEN;
1638 msg.buf = (char *)buf;
1639 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
1640 msg.scl_rate = 100 * 1000;
1643 ret = i2c_transfer(adap, &msg, 1);
1646 * If everything went ok (i.e. 1 msg transmitted), return #bytes
1647 * transmitted, else error code.
1649 return (ret == 1) ? count : ret;
1651 EXPORT_SYMBOL(i2c_master_send);
1654 * i2c_master_recv - issue a single I2C message in master receive mode
1655 * @client: Handle to slave device
1656 * @buf: Where to store data read from slave
1657 * @count: How many bytes to read, must be less than 64k since msg.len is u16
1659 * Returns negative errno, or else the number of bytes read.
1661 int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
1663 struct i2c_adapter *adap = client->adapter;
1667 msg.addr = client->addr;
1668 msg.flags = client->flags & I2C_M_TEN;
1669 msg.flags |= I2C_M_RD;
1672 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
1673 msg.scl_rate = 100 * 1000;
1676 ret = i2c_transfer(adap, &msg, 1);
1679 * If everything went ok (i.e. 1 msg received), return #bytes received,
1682 return (ret == 1) ? count : ret;
1684 EXPORT_SYMBOL(i2c_master_recv);
1686 /* ----------------------------------------------------
1687 * the i2c address scanning function
1688 * Will not work for 10-bit addresses!
1689 * ----------------------------------------------------
1693 * Legacy default probe function, mostly relevant for SMBus. The default
1694 * probe method is a quick write, but it is known to corrupt the 24RF08
1695 * EEPROMs due to a state machine bug, and could also irreversibly
1696 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1697 * we use a short byte read instead. Also, some bus drivers don't implement
1698 * quick write, so we fallback to a byte read in that case too.
1699 * On x86, there is another special case for FSC hardware monitoring chips,
1700 * which want regular byte reads (address 0x73.) Fortunately, these are the
1701 * only known chips using this I2C address on PC hardware.
1702 * Returns 1 if probe succeeded, 0 if not.
1704 static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1707 union i2c_smbus_data dummy;
1710 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1711 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1712 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1713 I2C_SMBUS_BYTE_DATA, &dummy);
1716 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1717 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
1718 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1719 I2C_SMBUS_QUICK, NULL);
1720 else if (i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE))
1721 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1722 I2C_SMBUS_BYTE, &dummy);
1724 dev_warn(&adap->dev, "No suitable probing method supported\n");
1731 static int i2c_detect_address(struct i2c_client *temp_client,
1732 struct i2c_driver *driver)
1734 struct i2c_board_info info;
1735 struct i2c_adapter *adapter = temp_client->adapter;
1736 int addr = temp_client->addr;
1739 /* Make sure the address is valid */
1740 err = i2c_check_addr_validity(addr);
1742 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1747 /* Skip if already in use */
1748 if (i2c_check_addr_busy(adapter, addr))
1751 /* Make sure there is something at this address */
1752 if (!i2c_default_probe(adapter, addr))
1755 /* Finally call the custom detection function */
1756 memset(&info, 0, sizeof(struct i2c_board_info));
1758 err = driver->detect(temp_client, &info);
1760 /* -ENODEV is returned if the detection fails. We catch it
1761 here as this isn't an error. */
1762 return err == -ENODEV ? 0 : err;
1765 /* Consistency check */
1766 if (info.type[0] == '\0') {
1767 dev_err(&adapter->dev, "%s detection function provided "
1768 "no name for 0x%x\n", driver->driver.name,
1771 struct i2c_client *client;
1773 /* Detection succeeded, instantiate the device */
1774 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1775 info.type, info.addr);
1776 client = i2c_new_device(adapter, &info);
1778 list_add_tail(&client->detected, &driver->clients);
1780 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1781 info.type, info.addr);
1786 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1788 const unsigned short *address_list;
1789 struct i2c_client *temp_client;
1791 int adap_id = i2c_adapter_id(adapter);
1793 address_list = driver->address_list;
1794 if (!driver->detect || !address_list)
1797 /* Stop here if the classes do not match */
1798 if (!(adapter->class & driver->class))
1801 /* Set up a temporary client to help detect callback */
1802 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1805 temp_client->adapter = adapter;
1807 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
1808 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1809 "addr 0x%02x\n", adap_id, address_list[i]);
1810 temp_client->addr = address_list[i];
1811 err = i2c_detect_address(temp_client, driver);
1820 int i2c_probe_func_quick_read(struct i2c_adapter *adap, unsigned short addr)
1822 return i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1823 I2C_SMBUS_QUICK, NULL) >= 0;
1825 EXPORT_SYMBOL_GPL(i2c_probe_func_quick_read);
1828 i2c_new_probed_device(struct i2c_adapter *adap,
1829 struct i2c_board_info *info,
1830 unsigned short const *addr_list,
1831 int (*probe)(struct i2c_adapter *, unsigned short addr))
1836 probe = i2c_default_probe;
1838 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1839 /* Check address validity */
1840 if (i2c_check_addr_validity(addr_list[i]) < 0) {
1841 dev_warn(&adap->dev, "Invalid 7-bit address "
1842 "0x%02x\n", addr_list[i]);
1846 /* Check address availability */
1847 if (i2c_check_addr_busy(adap, addr_list[i])) {
1848 dev_dbg(&adap->dev, "Address 0x%02x already in "
1849 "use, not probing\n", addr_list[i]);
1853 /* Test address responsiveness */
1854 if (probe(adap, addr_list[i]))
1858 if (addr_list[i] == I2C_CLIENT_END) {
1859 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1863 info->addr = addr_list[i];
1864 return i2c_new_device(adap, info);
1866 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1868 struct i2c_adapter *i2c_get_adapter(int nr)
1870 struct i2c_adapter *adapter;
1872 mutex_lock(&core_lock);
1873 adapter = idr_find(&i2c_adapter_idr, nr);
1874 if (adapter && !try_module_get(adapter->owner))
1877 mutex_unlock(&core_lock);
1880 EXPORT_SYMBOL(i2c_get_adapter);
1882 void i2c_put_adapter(struct i2c_adapter *adap)
1884 module_put(adap->owner);
1886 EXPORT_SYMBOL(i2c_put_adapter);
1888 /* The SMBus parts */
1890 #define POLY (0x1070U << 3)
1891 static u8 crc8(u16 data)
1895 for (i = 0; i < 8; i++) {
1900 return (u8)(data >> 8);
1903 /* Incremental CRC8 over count bytes in the array pointed to by p */
1904 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1908 for (i = 0; i < count; i++)
1909 crc = crc8((crc ^ p[i]) << 8);
1913 /* Assume a 7-bit address, which is reasonable for SMBus */
1914 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1916 /* The address will be sent first */
1917 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1918 pec = i2c_smbus_pec(pec, &addr, 1);
1920 /* The data buffer follows */
1921 return i2c_smbus_pec(pec, msg->buf, msg->len);
1924 /* Used for write only transactions */
1925 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1927 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1931 /* Return <0 on CRC error
1932 If there was a write before this read (most cases) we need to take the
1933 partial CRC from the write part into account.
1934 Note that this function does modify the message (we need to decrease the
1935 message length to hide the CRC byte from the caller). */
1936 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1938 u8 rpec = msg->buf[--msg->len];
1939 cpec = i2c_smbus_msg_pec(cpec, msg);
1942 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1950 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1951 * @client: Handle to slave device
1953 * This executes the SMBus "receive byte" protocol, returning negative errno
1954 * else the byte received from the device.
1956 s32 i2c_smbus_read_byte(const struct i2c_client *client)
1958 union i2c_smbus_data data;
1961 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1963 I2C_SMBUS_BYTE, &data);
1964 return (status < 0) ? status : data.byte;
1966 EXPORT_SYMBOL(i2c_smbus_read_byte);
1969 * i2c_smbus_write_byte - SMBus "send byte" protocol
1970 * @client: Handle to slave device
1971 * @value: Byte to be sent
1973 * This executes the SMBus "send byte" protocol, returning negative errno
1974 * else zero on success.
1976 s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
1978 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1979 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1981 EXPORT_SYMBOL(i2c_smbus_write_byte);
1984 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1985 * @client: Handle to slave device
1986 * @command: Byte interpreted by slave
1988 * This executes the SMBus "read byte" protocol, returning negative errno
1989 * else a data byte received from the device.
1991 s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
1993 union i2c_smbus_data data;
1996 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1997 I2C_SMBUS_READ, command,
1998 I2C_SMBUS_BYTE_DATA, &data);
1999 return (status < 0) ? status : data.byte;
2001 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
2004 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
2005 * @client: Handle to slave device
2006 * @command: Byte interpreted by slave
2007 * @value: Byte being written
2009 * This executes the SMBus "write byte" protocol, returning negative errno
2010 * else zero on success.
2012 s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command,
2015 union i2c_smbus_data data;
2017 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2018 I2C_SMBUS_WRITE, command,
2019 I2C_SMBUS_BYTE_DATA, &data);
2021 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
2024 * i2c_smbus_read_word_data - SMBus "read word" protocol
2025 * @client: Handle to slave device
2026 * @command: Byte interpreted by slave
2028 * This executes the SMBus "read word" protocol, returning negative errno
2029 * else a 16-bit unsigned "word" received from the device.
2031 s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
2033 union i2c_smbus_data data;
2036 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2037 I2C_SMBUS_READ, command,
2038 I2C_SMBUS_WORD_DATA, &data);
2039 return (status < 0) ? status : data.word;
2041 EXPORT_SYMBOL(i2c_smbus_read_word_data);
2044 * i2c_smbus_write_word_data - SMBus "write word" protocol
2045 * @client: Handle to slave device
2046 * @command: Byte interpreted by slave
2047 * @value: 16-bit "word" being written
2049 * This executes the SMBus "write word" protocol, returning negative errno
2050 * else zero on success.
2052 s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command,
2055 union i2c_smbus_data data;
2057 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2058 I2C_SMBUS_WRITE, command,
2059 I2C_SMBUS_WORD_DATA, &data);
2061 EXPORT_SYMBOL(i2c_smbus_write_word_data);
2064 * i2c_smbus_read_block_data - SMBus "block read" protocol
2065 * @client: Handle to slave device
2066 * @command: Byte interpreted by slave
2067 * @values: Byte array into which data will be read; big enough to hold
2068 * the data returned by the slave. SMBus allows at most 32 bytes.
2070 * This executes the SMBus "block read" protocol, returning negative errno
2071 * else the number of data bytes in the slave's response.
2073 * Note that using this function requires that the client's adapter support
2074 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
2075 * support this; its emulation through I2C messaging relies on a specific
2076 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
2078 s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,
2081 union i2c_smbus_data data;
2084 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2085 I2C_SMBUS_READ, command,
2086 I2C_SMBUS_BLOCK_DATA, &data);
2090 memcpy(values, &data.block[1], data.block[0]);
2091 return data.block[0];
2093 EXPORT_SYMBOL(i2c_smbus_read_block_data);
2096 * i2c_smbus_write_block_data - SMBus "block write" protocol
2097 * @client: Handle to slave device
2098 * @command: Byte interpreted by slave
2099 * @length: Size of data block; SMBus allows at most 32 bytes
2100 * @values: Byte array which will be written.
2102 * This executes the SMBus "block write" protocol, returning negative errno
2103 * else zero on success.
2105 s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
2106 u8 length, const u8 *values)
2108 union i2c_smbus_data data;
2110 if (length > I2C_SMBUS_BLOCK_MAX)
2111 length = I2C_SMBUS_BLOCK_MAX;
2112 data.block[0] = length;
2113 memcpy(&data.block[1], values, length);
2114 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2115 I2C_SMBUS_WRITE, command,
2116 I2C_SMBUS_BLOCK_DATA, &data);
2118 EXPORT_SYMBOL(i2c_smbus_write_block_data);
2120 /* Returns the number of read bytes */
2121 s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
2122 u8 length, u8 *values)
2124 union i2c_smbus_data data;
2127 if (length > I2C_SMBUS_BLOCK_MAX)
2128 length = I2C_SMBUS_BLOCK_MAX;
2129 data.block[0] = length;
2130 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2131 I2C_SMBUS_READ, command,
2132 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2136 memcpy(values, &data.block[1], data.block[0]);
2137 return data.block[0];
2139 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
2141 s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
2142 u8 length, const u8 *values)
2144 union i2c_smbus_data data;
2146 if (length > I2C_SMBUS_BLOCK_MAX)
2147 length = I2C_SMBUS_BLOCK_MAX;
2148 data.block[0] = length;
2149 memcpy(data.block + 1, values, length);
2150 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
2151 I2C_SMBUS_WRITE, command,
2152 I2C_SMBUS_I2C_BLOCK_DATA, &data);
2154 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
2156 /* Simulate a SMBus command using the i2c protocol
2157 No checking of parameters is done! */
2158 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
2159 unsigned short flags,
2160 char read_write, u8 command, int size,
2161 union i2c_smbus_data *data)
2163 /* So we need to generate a series of msgs. In the case of writing, we
2164 need to use only one message; when reading, we need two. We initialize
2165 most things with sane defaults, to keep the code below somewhat
2167 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
2168 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
2169 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
2173 struct i2c_msg msg[2] = {
2179 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
2180 .scl_rate = 100 * 1000,
2184 .flags = flags | I2C_M_RD,
2187 #ifdef CONFIG_I2C_ROCKCHIP_COMPAT
2188 .scl_rate = 100 * 1000,
2193 msgbuf0[0] = command;
2195 case I2C_SMBUS_QUICK:
2197 /* Special case: The read/write field is used as data */
2198 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
2202 case I2C_SMBUS_BYTE:
2203 if (read_write == I2C_SMBUS_READ) {
2204 /* Special case: only a read! */
2205 msg[0].flags = I2C_M_RD | flags;
2209 case I2C_SMBUS_BYTE_DATA:
2210 if (read_write == I2C_SMBUS_READ)
2214 msgbuf0[1] = data->byte;
2217 case I2C_SMBUS_WORD_DATA:
2218 if (read_write == I2C_SMBUS_READ)
2222 msgbuf0[1] = data->word & 0xff;
2223 msgbuf0[2] = data->word >> 8;
2226 case I2C_SMBUS_PROC_CALL:
2227 num = 2; /* Special case */
2228 read_write = I2C_SMBUS_READ;
2231 msgbuf0[1] = data->word & 0xff;
2232 msgbuf0[2] = data->word >> 8;
2234 case I2C_SMBUS_BLOCK_DATA:
2235 if (read_write == I2C_SMBUS_READ) {
2236 msg[1].flags |= I2C_M_RECV_LEN;
2237 msg[1].len = 1; /* block length will be added by
2238 the underlying bus driver */
2240 msg[0].len = data->block[0] + 2;
2241 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
2242 dev_err(&adapter->dev,
2243 "Invalid block write size %d\n",
2247 for (i = 1; i < msg[0].len; i++)
2248 msgbuf0[i] = data->block[i-1];
2251 case I2C_SMBUS_BLOCK_PROC_CALL:
2252 num = 2; /* Another special case */
2253 read_write = I2C_SMBUS_READ;
2254 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
2255 dev_err(&adapter->dev,
2256 "Invalid block write size %d\n",
2260 msg[0].len = data->block[0] + 2;
2261 for (i = 1; i < msg[0].len; i++)
2262 msgbuf0[i] = data->block[i-1];
2263 msg[1].flags |= I2C_M_RECV_LEN;
2264 msg[1].len = 1; /* block length will be added by
2265 the underlying bus driver */
2267 case I2C_SMBUS_I2C_BLOCK_DATA:
2268 if (read_write == I2C_SMBUS_READ) {
2269 msg[1].len = data->block[0];
2271 msg[0].len = data->block[0] + 1;
2272 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
2273 dev_err(&adapter->dev,
2274 "Invalid block write size %d\n",
2278 for (i = 1; i <= data->block[0]; i++)
2279 msgbuf0[i] = data->block[i];
2283 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
2287 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
2288 && size != I2C_SMBUS_I2C_BLOCK_DATA);
2290 /* Compute PEC if first message is a write */
2291 if (!(msg[0].flags & I2C_M_RD)) {
2292 if (num == 1) /* Write only */
2293 i2c_smbus_add_pec(&msg[0]);
2294 else /* Write followed by read */
2295 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2297 /* Ask for PEC if last message is a read */
2298 if (msg[num-1].flags & I2C_M_RD)
2302 status = i2c_transfer(adapter, msg, num);
2306 /* Check PEC if last message is a read */
2307 if (i && (msg[num-1].flags & I2C_M_RD)) {
2308 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2313 if (read_write == I2C_SMBUS_READ)
2315 case I2C_SMBUS_BYTE:
2316 data->byte = msgbuf0[0];
2318 case I2C_SMBUS_BYTE_DATA:
2319 data->byte = msgbuf1[0];
2321 case I2C_SMBUS_WORD_DATA:
2322 case I2C_SMBUS_PROC_CALL:
2323 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
2325 case I2C_SMBUS_I2C_BLOCK_DATA:
2326 for (i = 0; i < data->block[0]; i++)
2327 data->block[i+1] = msgbuf1[i];
2329 case I2C_SMBUS_BLOCK_DATA:
2330 case I2C_SMBUS_BLOCK_PROC_CALL:
2331 for (i = 0; i < msgbuf1[0] + 1; i++)
2332 data->block[i] = msgbuf1[i];
2339 * i2c_smbus_xfer - execute SMBus protocol operations
2340 * @adapter: Handle to I2C bus
2341 * @addr: Address of SMBus slave on that bus
2342 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
2343 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
2344 * @command: Byte interpreted by slave, for protocols which use such bytes
2345 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
2346 * @data: Data to be read or written
2348 * This executes an SMBus protocol operation, and returns a negative
2349 * errno code else zero on success.
2351 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
2352 char read_write, u8 command, int protocol,
2353 union i2c_smbus_data *data)
2355 unsigned long orig_jiffies;
2359 flags &= I2C_M_TEN | I2C_CLIENT_PEC | I2C_CLIENT_SCCB;
2361 if (adapter->algo->smbus_xfer) {
2362 i2c_lock_adapter(adapter);
2364 /* Retry automatically on arbitration loss */
2365 orig_jiffies = jiffies;
2366 for (res = 0, try = 0; try <= adapter->retries; try++) {
2367 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2368 read_write, command,
2372 if (time_after(jiffies,
2373 orig_jiffies + adapter->timeout))
2376 i2c_unlock_adapter(adapter);
2378 if (res != -EOPNOTSUPP || !adapter->algo->master_xfer)
2381 * Fall back to i2c_smbus_xfer_emulated if the adapter doesn't
2382 * implement native support for the SMBus operation.
2386 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2387 command, protocol, data);
2389 EXPORT_SYMBOL(i2c_smbus_xfer);
2391 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2392 MODULE_DESCRIPTION("I2C-Bus main module");
2393 MODULE_LICENSE("GPL");