28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/errno.h>
31 #include <linux/slab.h>
32 #include <linux/i2c.h>
42 #include <asm/uaccess.h>
78 if (of_driver_match_device(dev, drv))
104 #define i2c_device_uevent NULL
107 static int i2c_device_probe(
struct device *dev)
120 if (!device_can_wakeup(&client->
dev))
125 status = driver->
probe(client, i2c_match_id(driver->
id_table, client));
128 i2c_set_clientdata(client,
NULL);
133 static int i2c_device_remove(
struct device *dev)
139 if (!client || !dev->
driver)
145 status = driver->
remove(client);
152 i2c_set_clientdata(client,
NULL);
157 static void i2c_device_shutdown(
struct device *dev)
162 if (!client || !dev->
driver)
169 #ifdef CONFIG_PM_SLEEP
175 if (!client || !dev->
driver)
180 return driver->
suspend(client, mesg);
183 static int i2c_legacy_resume(
struct device *dev)
188 if (!client || !dev->
driver)
193 return driver->
resume(client);
213 return i2c_legacy_resume(dev);
233 return i2c_legacy_resume(dev);
253 return i2c_legacy_resume(dev);
256 #define i2c_device_pm_suspend NULL
257 #define i2c_device_pm_resume NULL
258 #define i2c_device_pm_freeze NULL
259 #define i2c_device_pm_thaw NULL
260 #define i2c_device_pm_poweroff NULL
261 #define i2c_device_pm_restore NULL
264 static void i2c_client_dev_release(
struct device *dev)
272 return sprintf(buf,
"%s\n", dev->
type == &i2c_client_type ?
286 static struct attribute *i2c_dev_attrs[] = {
289 &dev_attr_modalias.attr,
294 .attrs = i2c_dev_attrs,
302 static const struct dev_pm_ops i2c_device_pm_ops = {
310 pm_generic_runtime_suspend,
311 pm_generic_runtime_resume,
312 pm_generic_runtime_idle
318 .match = i2c_device_match,
319 .probe = i2c_device_probe,
320 .remove = i2c_device_remove,
321 .shutdown = i2c_device_shutdown,
322 .pm = &i2c_device_pm_ops,
327 .groups = i2c_dev_attr_groups,
329 .release = i2c_client_dev_release,
344 return (dev->
type == &i2c_client_type)
353 static int i2c_check_client_addr_validity(
const struct i2c_client *client)
357 if (client->
addr > 0x3ff)
361 if (client->
addr == 0x00 || client->
addr > 0x7f)
371 static int i2c_check_addr_validity(
unsigned short addr)
383 if (addr < 0x08 || addr > 0x77)
388 static int __i2c_check_addr_busy(
struct device *dev,
void *addrp)
391 int addr = *(
int *)addrp;
393 if (client && client->
addr == addr)
405 __i2c_check_addr_busy);
407 if (!result && parent)
408 result = i2c_check_mux_parents(parent, addr);
414 static int i2c_check_mux_children(
struct device *dev,
void *addrp)
420 i2c_check_mux_children);
422 result = __i2c_check_addr_busy(dev, addrp);
427 static int i2c_check_addr_busy(
struct i2c_adapter *adapter,
int addr)
429 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
433 result = i2c_check_mux_parents(parent, addr);
437 i2c_check_mux_children);
448 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
461 static int i2c_trylock_adapter(
struct i2c_adapter *adapter)
463 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
466 return i2c_trylock_adapter(parent);
477 struct i2c_adapter *parent = i2c_parent_is_i2c_adapter(adapter);
526 status = i2c_check_client_addr_validity(client);
528 dev_err(&adap->
dev,
"Invalid %d-bit I2C address 0x%02hx\n",
534 status = i2c_check_addr_busy(adap, client->
addr);
540 client->
dev.type = &i2c_client_type;
551 dev_dbg(&adap->
dev,
"client [%s] registered with bus id %s\n",
552 client->
name, dev_name(&client->
dev));
557 dev_err(&adap->
dev,
"Failed to register i2c client %s at 0x%02x "
558 "(%d)\n", client->
name, client->
addr, status);
583 static int dummy_probe(
struct i2c_client *client,
589 static int dummy_remove(
struct i2c_client *client)
595 .driver.name =
"dummy",
596 .probe = dummy_probe,
597 .remove = dummy_remove,
598 .id_table = dummy_id,
633 static void i2c_adapter_dev_release(
struct device *dev)
645 static inline unsigned int i2c_adapter_depth(
struct i2c_adapter *adapter)
647 unsigned int depth = 0;
649 while ((adapter = i2c_parent_is_i2c_adapter(adapter)))
667 const char *buf,
size_t count)
679 dev_err(dev,
"%s: Missing parameters\n",
"new_device");
683 dev_err(dev,
"%s: Invalid device name\n",
"new_device");
689 res =
sscanf(++blank,
"%hi%c", &
info.addr, &end);
691 dev_err(dev,
"%s: Can't parse I2C address\n",
"new_device");
694 if (res > 1 && end !=
'\n') {
695 dev_err(dev,
"%s: Extra parameters\n",
"new_device");
707 dev_info(dev,
"%s: Instantiated device %s at 0x%02hx\n",
"new_device",
724 const char *buf,
size_t count)
733 res =
sscanf(buf,
"%hi%c", &addr, &end);
735 dev_err(dev,
"%s: Can't parse I2C address\n",
"delete_device");
738 if (res > 1 && end !=
'\n') {
739 dev_err(dev,
"%s: Extra parameters\n",
"delete_device");
746 i2c_adapter_depth(adap));
749 if (client->
addr == addr) {
750 dev_info(dev,
"%s: Deleting device %s at 0x%02hx\n",
751 "delete_device", client->
name, client->
addr);
762 dev_err(dev,
"%s: Can't find device in list\n",
770 static struct attribute *i2c_adapter_attrs[] = {
772 &dev_attr_new_device.attr,
773 &dev_attr_delete_device.attr,
778 .attrs = i2c_adapter_attrs,
782 &i2c_adapter_attr_group,
787 .groups = i2c_adapter_attr_groups,
788 .release = i2c_adapter_dev_release,
803 return (dev->
type == &i2c_adapter_type)
809 #ifdef CONFIG_I2C_COMPAT
813 static void i2c_scan_static_board_info(
struct i2c_adapter *adapter)
823 "Can't create device at 0x%02x\n",
829 static int i2c_do_add_adapter(
struct i2c_driver *driver,
833 i2c_detect(adap, driver);
837 dev_warn(&adap->
dev,
"%s: attach_adapter method is deprecated\n",
839 dev_warn(&adap->
dev,
"Please use another way to instantiate "
840 "your i2c_client\n");
852 static int i2c_register_adapter(
struct i2c_adapter *adap)
864 pr_err(
"i2c-core: Attempt to register an adapter with "
869 pr_err(
"i2c-core: Attempt to register adapter '%s' with "
870 "no algo!\n", adap->
name);
891 #ifdef CONFIG_I2C_COMPAT
896 "Failed to create compatibility class link\n");
901 i2c_scan_static_board_info(adap);
951 return i2c_register_adapter(adapter);
997 if (status == 0 &&
id != adap->
nr) {
1006 status = i2c_register_adapter(adap);
1011 static int i2c_do_del_adapter(
struct i2c_driver *driver,
1020 if (client->
adapter == adapter) {
1021 dev_dbg(&adapter->
dev,
"Removing %s at 0x%x\n",
1030 dev_warn(&adapter->
dev,
"%s: detach_adapter method is deprecated\n",
1034 dev_err(&adapter->
dev,
"detach_adapter failed (%d) "
1035 "for driver [%s]\n", res, driver->
driver.name);
1039 static int __unregister_client(
struct device *dev,
void *
dummy)
1047 static int __unregister_dummy(
struct device *dev,
void *dummy)
1055 static int __process_removed_adapter(
struct device_driver *d,
void *data)
1076 found =
idr_find(&i2c_adapter_idr, adap->
nr);
1078 if (found != adap) {
1079 pr_debug(
"i2c-core: attempting to delete unregistered "
1080 "adapter [%s]\n", adap->
name);
1087 __process_removed_adapter);
1094 i2c_adapter_depth(adap));
1112 #ifdef CONFIG_I2C_COMPAT
1155 static int __process_new_driver(
struct device *dev,
void *data)
1157 if (dev->
type != &i2c_adapter_type)
1188 pr_warn(
"i2c-core: driver [%s] using legacy suspend method\n",
1191 pr_warn(
"i2c-core: driver [%s] using legacy resume method\n",
1194 pr_debug(
"i2c-core: driver [%s] registered\n", driver->
driver.name);
1196 INIT_LIST_HEAD(&driver->
clients);
1204 static int __process_removed_driver(
struct device *dev,
void *data)
1206 if (dev->
type != &i2c_adapter_type)
1221 pr_debug(
"i2c-core: driver [%s] unregistered\n", driver->
driver.name);
1264 static int i2c_cmd(
struct device *dev,
void *_arg)
1269 if (client && client->
driver && client->
driver->command)
1291 #ifdef CONFIG_I2C_COMPAT
1293 if (!i2c_adapter_compat_class) {
1298 retval = i2c_add_driver(&dummy_driver);
1304 #ifdef CONFIG_I2C_COMPAT
1312 static void __exit i2c_exit(
void)
1315 #ifdef CONFIG_I2C_COMPAT
1346 unsigned long orig_jiffies;
1351 for (ret = 0,
try = 0;
try <= adap->
retries;
try++) {
1352 ret = adap->
algo->master_xfer(adap, msgs, num);
1396 if (adap->
algo->master_xfer) {
1398 for (ret = 0; ret < num; ret++) {
1399 dev_dbg(&adap->
dev,
"master_xfer[%d] %c, addr=0x%02x, "
1401 ?
'R' :
'W', msgs[ret].
addr, msgs[ret].
len,
1407 ret = i2c_trylock_adapter(adap);
1420 dev_dbg(&adap->
dev,
"I2C level transfers not supported\n");
1443 msg.
buf = (
char *)buf;
1451 return (ret == 1) ? count :
ret;
1481 return (ret == 1) ? count :
ret;
1503 static int i2c_default_probe(
struct i2c_adapter *adap,
unsigned short addr)
1515 if (!((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50)
1523 dev_warn(&adap->
dev,
"No suitable probing method supported\n");
1530 static int i2c_detect_address(
struct i2c_client *temp_client,
1535 int addr = temp_client->
addr;
1539 err = i2c_check_addr_validity(addr);
1541 dev_warn(&adapter->
dev,
"Invalid probe address 0x%02x\n",
1547 if (i2c_check_addr_busy(adapter, addr))
1551 if (!i2c_default_probe(adapter, addr))
1565 if (
info.type[0] ==
'\0') {
1566 dev_err(&adapter->
dev,
"%s detection function provided "
1567 "no name for 0x%x\n", driver->
driver.name,
1573 dev_dbg(&adapter->
dev,
"Creating %s at 0x%02x\n",
1579 dev_err(&adapter->
dev,
"Failed creating %s at 0x%02x\n",
1587 const unsigned short *address_list;
1590 int adap_id = i2c_adapter_id(adapter);
1593 if (!driver->
detect || !address_list)
1607 dev_dbg(&adapter->
dev,
"found normal entry for adapter %d, "
1608 "addr 0x%02x\n", adap_id, address_list[i]);
1609 temp_client->
addr = address_list[
i];
1610 err = i2c_detect_address(temp_client, driver);
1629 unsigned short const *addr_list,
1630 int (*probe)(
struct i2c_adapter *,
unsigned short addr))
1635 probe = i2c_default_probe;
1639 if (i2c_check_addr_validity(addr_list[i]) < 0) {
1641 "0x%02x\n", addr_list[i]);
1646 if (i2c_check_addr_busy(adap, addr_list[i])) {
1647 dev_dbg(&adap->
dev,
"Address 0x%02x already in "
1648 "use, not probing\n", addr_list[i]);
1653 if (probe(adap, addr_list[i]))
1658 dev_dbg(&adap->
dev,
"Probing failed, no device found\n");
1662 info->
addr = addr_list[
i];
1672 adapter =
idr_find(&i2c_adapter_idr, nr);
1673 if (adapter && !try_module_get(adapter->
owner))
1683 module_put(adap->
owner);
1689 #define POLY (0x1070U << 3)
1690 static u8 crc8(
u16 data)
1694 for (i = 0; i < 8; i++) {
1699 return (
u8)(data >> 8);
1703 static u8 i2c_smbus_pec(
u8 crc,
u8 *
p,
size_t count)
1707 for (i = 0; i <
count; i++)
1708 crc = crc8((crc ^ p[i]) << 8);
1717 pec = i2c_smbus_pec(pec, &addr, 1);
1720 return i2c_smbus_pec(pec, msg->
buf, msg->
len);
1724 static inline void i2c_smbus_add_pec(
struct i2c_msg *msg)
1726 msg->
buf[msg->
len] = i2c_smbus_msg_pec(0, msg);
1735 static int i2c_smbus_check_pec(
u8 cpec,
struct i2c_msg *msg)
1738 cpec = i2c_smbus_msg_pec(cpec, msg);
1741 pr_debug(
"i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1763 return (status < 0) ? status : data.
byte;
1798 return (status < 0) ? status : data.
byte;
1838 return (status < 0) ? status : data.
word;
1881 return (status < 0) ? status : data.
word;
1913 return data.
block[0];
1959 return data.
block[0];
1981 unsigned short flags,
2030 msgbuf0[1] = data->
byte;
2038 msgbuf0[1] = data->
word & 0xff;
2039 msgbuf0[2] = data->
word >> 8;
2047 msgbuf0[1] = data->
word & 0xff;
2048 msgbuf0[2] = data->
word >> 8;
2059 "Invalid block write size %d\n",
2063 for (i = 1; i < msg[0].
len; i++)
2064 msgbuf0[i] = data->
block[i-1];
2072 "Invalid block write size %d\n",
2077 for (i = 1; i < msg[0].
len; i++)
2078 msgbuf0[i] = data->
block[i-1];
2090 "Invalid block write size %d\n",
2094 for (i = 1; i <= data->
block[0]; i++)
2095 msgbuf0[i] = data->
block[i];
2099 dev_err(&adapter->
dev,
"Unsupported transaction %d\n", size);
2109 i2c_smbus_add_pec(&msg[0]);
2111 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
2114 if (msg[num-1].flags & I2C_M_RD)
2123 if (i && (msg[num-1].flags & I2C_M_RD)) {
2124 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
2132 data->
byte = msgbuf0[0];
2135 data->
byte = msgbuf1[0];
2139 data->
word = msgbuf1[0] | (msgbuf1[1] << 8);
2142 for (i = 0; i < data->
block[0]; i++)
2143 data->
block[i+1] = msgbuf1[i];
2147 for (i = 0; i < msgbuf1[0] + 1; i++)
2148 data->
block[i] = msgbuf1[i];
2171 unsigned long orig_jiffies;
2177 if (adapter->
algo->smbus_xfer) {
2182 for (res = 0,
try = 0;
try <= adapter->
retries;
try++) {
2183 res = adapter->
algo->smbus_xfer(adapter, addr, flags,
2184 read_write, command,
2189 orig_jiffies + adapter->
timeout))
2202 return i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2203 command, protocol, data);