aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/i2c/i2c-core.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/i2c/i2c-core.c')
-rw-r--r--drivers/i2c/i2c-core.c256
1 files changed, 142 insertions, 114 deletions
diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c
index 4a9ead277596..dda472e5e8be 100644
--- a/drivers/i2c/i2c-core.c
+++ b/drivers/i2c/i2c-core.c
@@ -61,7 +61,7 @@ static int i2c_bus_resume(struct device * dev)
61 return rc; 61 return rc;
62} 62}
63 63
64static struct bus_type i2c_bus_type = { 64struct bus_type i2c_bus_type = {
65 .name = "i2c", 65 .name = "i2c",
66 .match = i2c_device_match, 66 .match = i2c_device_match,
67 .suspend = i2c_bus_suspend, 67 .suspend = i2c_bus_suspend,
@@ -78,13 +78,13 @@ static int i2c_device_remove(struct device *dev)
78 return 0; 78 return 0;
79} 79}
80 80
81static void i2c_adapter_dev_release(struct device *dev) 81void i2c_adapter_dev_release(struct device *dev)
82{ 82{
83 struct i2c_adapter *adap = dev_to_i2c_adapter(dev); 83 struct i2c_adapter *adap = dev_to_i2c_adapter(dev);
84 complete(&adap->dev_released); 84 complete(&adap->dev_released);
85} 85}
86 86
87static struct device_driver i2c_adapter_driver = { 87struct device_driver i2c_adapter_driver = {
88 .name = "i2c_adapter", 88 .name = "i2c_adapter",
89 .bus = &i2c_bus_type, 89 .bus = &i2c_bus_type,
90 .probe = i2c_device_probe, 90 .probe = i2c_device_probe,
@@ -97,7 +97,7 @@ static void i2c_adapter_class_dev_release(struct class_device *dev)
97 complete(&adap->class_dev_released); 97 complete(&adap->class_dev_released);
98} 98}
99 99
100static struct class i2c_adapter_class = { 100struct class i2c_adapter_class = {
101 .name = "i2c-adapter", 101 .name = "i2c-adapter",
102 .release = &i2c_adapter_class_dev_release, 102 .release = &i2c_adapter_class_dev_release,
103}; 103};
@@ -188,6 +188,8 @@ int i2c_add_adapter(struct i2c_adapter *adap)
188 strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE); 188 strlcpy(adap->class_dev.class_id, adap->dev.bus_id, BUS_ID_SIZE);
189 class_device_register(&adap->class_dev); 189 class_device_register(&adap->class_dev);
190 190
191 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
192
191 /* inform drivers of new adapters */ 193 /* inform drivers of new adapters */
192 list_for_each(item,&drivers) { 194 list_for_each(item,&drivers) {
193 driver = list_entry(item, struct i2c_driver, list); 195 driver = list_entry(item, struct i2c_driver, list);
@@ -196,8 +198,6 @@ int i2c_add_adapter(struct i2c_adapter *adap)
196 driver->attach_adapter(adap); 198 driver->attach_adapter(adap);
197 } 199 }
198 200
199 dev_dbg(&adap->dev, "registered as adapter #%d\n", adap->nr);
200
201out_unlock: 201out_unlock:
202 up(&core_lists); 202 up(&core_lists);
203 return res; 203 return res;
@@ -220,8 +220,8 @@ int i2c_del_adapter(struct i2c_adapter *adap)
220 break; 220 break;
221 } 221 }
222 if (adap_from_list != adap) { 222 if (adap_from_list != adap) {
223 pr_debug("I2C: Attempting to delete an unregistered " 223 pr_debug("i2c-core: attempting to delete unregistered "
224 "adapter\n"); 224 "adapter [%s]\n", adap->name);
225 res = -EINVAL; 225 res = -EINVAL;
226 goto out_unlock; 226 goto out_unlock;
227 } 227 }
@@ -230,9 +230,8 @@ int i2c_del_adapter(struct i2c_adapter *adap)
230 driver = list_entry(item, struct i2c_driver, list); 230 driver = list_entry(item, struct i2c_driver, list);
231 if (driver->detach_adapter) 231 if (driver->detach_adapter)
232 if ((res = driver->detach_adapter(adap))) { 232 if ((res = driver->detach_adapter(adap))) {
233 dev_warn(&adap->dev, "can't detach adapter " 233 dev_err(&adap->dev, "detach_adapter failed "
234 "while detaching driver %s: driver " 234 "for driver [%s]\n", driver->name);
235 "not detached!\n", driver->name);
236 goto out_unlock; 235 goto out_unlock;
237 } 236 }
238 } 237 }
@@ -247,9 +246,8 @@ int i2c_del_adapter(struct i2c_adapter *adap)
247 * must be deleted, as this would cause invalid states. 246 * must be deleted, as this would cause invalid states.
248 */ 247 */
249 if ((res=client->driver->detach_client(client))) { 248 if ((res=client->driver->detach_client(client))) {
250 dev_err(&adap->dev, "adapter not " 249 dev_err(&adap->dev, "detach_client failed for client "
251 "unregistered, because client at " 250 "[%s] at address 0x%02x\n", client->name,
252 "address %02x can't be detached. ",
253 client->addr); 251 client->addr);
254 goto out_unlock; 252 goto out_unlock;
255 } 253 }
@@ -270,7 +268,7 @@ int i2c_del_adapter(struct i2c_adapter *adap)
270 /* free dynamically allocated bus id */ 268 /* free dynamically allocated bus id */
271 idr_remove(&i2c_adapter_idr, adap->nr); 269 idr_remove(&i2c_adapter_idr, adap->nr);
272 270
273 dev_dbg(&adap->dev, "adapter unregistered\n"); 271 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
274 272
275 out_unlock: 273 out_unlock:
276 up(&core_lists); 274 up(&core_lists);
@@ -303,7 +301,7 @@ int i2c_add_driver(struct i2c_driver *driver)
303 goto out_unlock; 301 goto out_unlock;
304 302
305 list_add_tail(&driver->list,&drivers); 303 list_add_tail(&driver->list,&drivers);
306 pr_debug("i2c-core: driver %s registered.\n", driver->name); 304 pr_debug("i2c-core: driver [%s] registered\n", driver->name);
307 305
308 /* now look for instances of driver on our adapters */ 306 /* now look for instances of driver on our adapters */
309 if (driver->flags & I2C_DF_NOTIFY) { 307 if (driver->flags & I2C_DF_NOTIFY) {
@@ -331,21 +329,17 @@ int i2c_del_driver(struct i2c_driver *driver)
331 /* Have a look at each adapter, if clients of this driver are still 329 /* Have a look at each adapter, if clients of this driver are still
332 * attached. If so, detach them to be able to kill the driver 330 * attached. If so, detach them to be able to kill the driver
333 * afterwards. 331 * afterwards.
334 */ 332 *
335 pr_debug("i2c-core: unregister_driver - looking for clients.\n"); 333 * Removing clients does not depend on the notify flag, else
336 /* removing clients does not depend on the notify flag, else
337 * invalid operation might (will!) result, when using stale client 334 * invalid operation might (will!) result, when using stale client
338 * pointers. 335 * pointers.
339 */ 336 */
340 list_for_each(item1,&adapters) { 337 list_for_each(item1,&adapters) {
341 adap = list_entry(item1, struct i2c_adapter, list); 338 adap = list_entry(item1, struct i2c_adapter, list);
342 dev_dbg(&adap->dev, "examining adapter\n");
343 if (driver->detach_adapter) { 339 if (driver->detach_adapter) {
344 if ((res = driver->detach_adapter(adap))) { 340 if ((res = driver->detach_adapter(adap))) {
345 dev_warn(&adap->dev, "while unregistering " 341 dev_err(&adap->dev, "detach_adapter failed "
346 "dummy driver %s, adapter could " 342 "for driver [%s]\n", driver->name);
347 "not be detached properly; driver "
348 "not unloaded!",driver->name);
349 goto out_unlock; 343 goto out_unlock;
350 } 344 }
351 } else { 345 } else {
@@ -353,16 +347,13 @@ int i2c_del_driver(struct i2c_driver *driver)
353 client = list_entry(item2, struct i2c_client, list); 347 client = list_entry(item2, struct i2c_client, list);
354 if (client->driver != driver) 348 if (client->driver != driver)
355 continue; 349 continue;
356 pr_debug("i2c-core.o: detaching client %s:\n", client->name); 350 dev_dbg(&adap->dev, "detaching client [%s] "
351 "at 0x%02x\n", client->name,
352 client->addr);
357 if ((res = driver->detach_client(client))) { 353 if ((res = driver->detach_client(client))) {
358 dev_err(&adap->dev, "while " 354 dev_err(&adap->dev, "detach_client "
359 "unregistering driver " 355 "failed for client [%s] at "
360 "`%s', the client at " 356 "0x%02x\n", client->name,
361 "address %02x of "
362 "adapter could not "
363 "be detached; driver "
364 "not unloaded!",
365 driver->name,
366 client->addr); 357 client->addr);
367 goto out_unlock; 358 goto out_unlock;
368 } 359 }
@@ -372,7 +363,7 @@ int i2c_del_driver(struct i2c_driver *driver)
372 363
373 driver_unregister(&driver->driver); 364 driver_unregister(&driver->driver);
374 list_del(&driver->list); 365 list_del(&driver->list);
375 pr_debug("i2c-core: driver unregistered: %s\n", driver->name); 366 pr_debug("i2c-core: driver [%s] unregistered\n", driver->name);
376 367
377 out_unlock: 368 out_unlock:
378 up(&core_lists); 369 up(&core_lists);
@@ -417,15 +408,12 @@ int i2c_attach_client(struct i2c_client *client)
417 408
418 if (adapter->client_register) { 409 if (adapter->client_register) {
419 if (adapter->client_register(client)) { 410 if (adapter->client_register(client)) {
420 dev_warn(&adapter->dev, "warning: client_register " 411 dev_dbg(&adapter->dev, "client_register "
421 "seems to have failed for client %02x\n", 412 "failed for client [%s] at 0x%02x\n",
422 client->addr); 413 client->name, client->addr);
423 } 414 }
424 } 415 }
425 416
426 dev_dbg(&adapter->dev, "client [%s] registered to adapter\n",
427 client->name);
428
429 if (client->flags & I2C_CLIENT_ALLOW_USE) 417 if (client->flags & I2C_CLIENT_ALLOW_USE)
430 client->usage_count = 0; 418 client->usage_count = 0;
431 419
@@ -436,7 +424,8 @@ int i2c_attach_client(struct i2c_client *client)
436 424
437 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id), 425 snprintf(&client->dev.bus_id[0], sizeof(client->dev.bus_id),
438 "%d-%04x", i2c_adapter_id(adapter), client->addr); 426 "%d-%04x", i2c_adapter_id(adapter), client->addr);
439 pr_debug("registering %s\n", client->dev.bus_id); 427 dev_dbg(&adapter->dev, "client [%s] registered with bus id %s\n",
428 client->name, client->dev.bus_id);
440 device_register(&client->dev); 429 device_register(&client->dev);
441 device_create_file(&client->dev, &dev_attr_client_name); 430 device_create_file(&client->dev, &dev_attr_client_name);
442 431
@@ -449,8 +438,12 @@ int i2c_detach_client(struct i2c_client *client)
449 struct i2c_adapter *adapter = client->adapter; 438 struct i2c_adapter *adapter = client->adapter;
450 int res = 0; 439 int res = 0;
451 440
452 if ((client->flags & I2C_CLIENT_ALLOW_USE) && (client->usage_count > 0)) 441 if ((client->flags & I2C_CLIENT_ALLOW_USE)
442 && (client->usage_count > 0)) {
443 dev_warn(&client->dev, "Client [%s] still busy, "
444 "can't detach\n", client->name);
453 return -EBUSY; 445 return -EBUSY;
446 }
454 447
455 if (adapter->client_unregister) { 448 if (adapter->client_unregister) {
456 res = adapter->client_unregister(client); 449 res = adapter->client_unregister(client);
@@ -669,98 +662,128 @@ int i2c_control(struct i2c_client *client,
669 * Will not work for 10-bit addresses! 662 * Will not work for 10-bit addresses!
670 * ---------------------------------------------------- 663 * ----------------------------------------------------
671 */ 664 */
665static int i2c_probe_address(struct i2c_adapter *adapter, int addr, int kind,
666 int (*found_proc) (struct i2c_adapter *, int, int))
667{
668 int err;
669
670 /* Make sure the address is valid */
671 if (addr < 0x03 || addr > 0x77) {
672 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
673 addr);
674 return -EINVAL;
675 }
676
677 /* Skip if already in use */
678 if (i2c_check_addr(adapter, addr))
679 return 0;
680
681 /* Make sure there is something at this address, unless forced */
682 if (kind < 0) {
683 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
684 I2C_SMBUS_QUICK, NULL) < 0)
685 return 0;
686
687 /* prevent 24RF08 corruption */
688 if ((addr & ~0x0f) == 0x50)
689 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
690 I2C_SMBUS_QUICK, NULL);
691 }
692
693 /* Finally call the custom detection function */
694 err = found_proc(adapter, addr, kind);
695
696 /* -ENODEV can be returned if there is a chip at the given address
697 but it isn't supported by this chip driver. We catch it here as
698 this isn't an error. */
699 return (err == -ENODEV) ? 0 : err;
700}
701
672int i2c_probe(struct i2c_adapter *adapter, 702int i2c_probe(struct i2c_adapter *adapter,
673 struct i2c_client_address_data *address_data, 703 struct i2c_client_address_data *address_data,
674 int (*found_proc) (struct i2c_adapter *, int, int)) 704 int (*found_proc) (struct i2c_adapter *, int, int))
675{ 705{
676 int addr,i,found,err; 706 int i, err;
677 int adap_id = i2c_adapter_id(adapter); 707 int adap_id = i2c_adapter_id(adapter);
678 708
679 /* Forget it if we can't probe using SMBUS_QUICK */ 709 /* Forget it if we can't probe using SMBUS_QUICK */
680 if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK)) 710 if (! i2c_check_functionality(adapter,I2C_FUNC_SMBUS_QUICK))
681 return -1; 711 return -1;
682 712
683 for (addr = 0x00; addr <= 0x7f; addr++) { 713 /* Force entries are done first, and are not affected by ignore
684 714 entries */
685 /* Skip if already in use */ 715 if (address_data->forces) {
686 if (i2c_check_addr(adapter,addr)) 716 unsigned short **forces = address_data->forces;
687 continue; 717 int kind;
688 718
689 /* If it is in one of the force entries, we don't do any detection 719 for (kind = 0; forces[kind]; kind++) {
690 at all */ 720 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
691 found = 0; 721 i += 2) {
692 722 if (forces[kind][i] == adap_id
693 for (i = 0; !found && (address_data->force[i] != I2C_CLIENT_END); i += 2) { 723 || forces[kind][i] == ANY_I2C_BUS) {
694 if (((adap_id == address_data->force[i]) || 724 dev_dbg(&adapter->dev, "found force "
695 (address_data->force[i] == ANY_I2C_BUS)) && 725 "parameter for adapter %d, "
696 (addr == address_data->force[i+1])) { 726 "addr 0x%02x, kind %d\n",
697 dev_dbg(&adapter->dev, "found force parameter for adapter %d, addr %04x\n", 727 adap_id, forces[kind][i + 1],
698 adap_id, addr); 728 kind);
699 if ((err = found_proc(adapter,addr,0))) 729 err = i2c_probe_address(adapter,
700 return err; 730 forces[kind][i + 1],
701 found = 1; 731 kind, found_proc);
702 } 732 if (err)
703 } 733 return err;
704 if (found) 734 }
705 continue;
706
707 /* If this address is in one of the ignores, we can forget about
708 it right now */
709 for (i = 0;
710 !found && (address_data->ignore[i] != I2C_CLIENT_END);
711 i += 2) {
712 if (((adap_id == address_data->ignore[i]) ||
713 ((address_data->ignore[i] == ANY_I2C_BUS))) &&
714 (addr == address_data->ignore[i+1])) {
715 dev_dbg(&adapter->dev, "found ignore parameter for adapter %d, "
716 "addr %04x\n", adap_id ,addr);
717 found = 1;
718 } 735 }
719 } 736 }
720 if (found) 737 }
721 continue;
722 738
723 /* Now, we will do a detection, but only if it is in the normal or 739 /* Probe entries are done second, and are not affected by ignore
724 probe entries */ 740 entries either */
725 for (i = 0; 741 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
726 !found && (address_data->normal_i2c[i] != I2C_CLIENT_END); 742 if (address_data->probe[i] == adap_id
727 i += 1) { 743 || address_data->probe[i] == ANY_I2C_BUS) {
728 if (addr == address_data->normal_i2c[i]) { 744 dev_dbg(&adapter->dev, "found probe parameter for "
729 found = 1; 745 "adapter %d, addr 0x%02x\n", adap_id,
730 dev_dbg(&adapter->dev, "found normal i2c entry for adapter %d, " 746 address_data->probe[i + 1]);
731 "addr %02x\n", adap_id, addr); 747 err = i2c_probe_address(adapter,
732 } 748 address_data->probe[i + 1],
749 -1, found_proc);
750 if (err)
751 return err;
733 } 752 }
753 }
734 754
735 for (i = 0; 755 /* Normal entries are done last, unless shadowed by an ignore entry */
736 !found && (address_data->probe[i] != I2C_CLIENT_END); 756 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
737 i += 2) { 757 int j, ignore;
738 if (((adap_id == address_data->probe[i]) || 758
739 ((address_data->probe[i] == ANY_I2C_BUS))) && 759 ignore = 0;
740 (addr == address_data->probe[i+1])) { 760 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
741 found = 1; 761 j += 2) {
742 dev_dbg(&adapter->dev, "found probe parameter for adapter %d, " 762 if ((address_data->ignore[j] == adap_id ||
743 "addr %04x\n", adap_id,addr); 763 address_data->ignore[j] == ANY_I2C_BUS)
764 && address_data->ignore[j + 1]
765 == address_data->normal_i2c[i]) {
766 dev_dbg(&adapter->dev, "found ignore "
767 "parameter for adapter %d, "
768 "addr 0x%02x\n", adap_id,
769 address_data->ignore[j + 1]);
744 } 770 }
771 ignore = 1;
772 break;
745 } 773 }
746 if (!found) 774 if (ignore)
747 continue; 775 continue;
748 776
749 /* OK, so we really should examine this address. First check 777 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
750 whether there is some client here at all! */ 778 "addr 0x%02x\n", adap_id,
751 if (i2c_smbus_xfer(adapter,addr,0,0,0,I2C_SMBUS_QUICK,NULL) >= 0) 779 address_data->normal_i2c[i]);
752 if ((err = found_proc(adapter,addr,-1))) 780 err = i2c_probe_address(adapter, address_data->normal_i2c[i],
753 return err; 781 -1, found_proc);
782 if (err)
783 return err;
754 } 784 }
755 return 0;
756}
757 785
758/* 786 return 0;
759 * return id number for a specific adapter
760 */
761int i2c_adapter_id(struct i2c_adapter *adap)
762{
763 return adap->nr;
764} 787}
765 788
766struct i2c_adapter* i2c_get_adapter(int id) 789struct i2c_adapter* i2c_get_adapter(int id)
@@ -1171,6 +1194,12 @@ s32 i2c_smbus_xfer(struct i2c_adapter * adapter, u16 addr, unsigned short flags,
1171} 1194}
1172 1195
1173 1196
1197/* Next four are needed by i2c-isa */
1198EXPORT_SYMBOL_GPL(i2c_adapter_dev_release);
1199EXPORT_SYMBOL_GPL(i2c_adapter_driver);
1200EXPORT_SYMBOL_GPL(i2c_adapter_class);
1201EXPORT_SYMBOL_GPL(i2c_bus_type);
1202
1174EXPORT_SYMBOL(i2c_add_adapter); 1203EXPORT_SYMBOL(i2c_add_adapter);
1175EXPORT_SYMBOL(i2c_del_adapter); 1204EXPORT_SYMBOL(i2c_del_adapter);
1176EXPORT_SYMBOL(i2c_add_driver); 1205EXPORT_SYMBOL(i2c_add_driver);
@@ -1186,7 +1215,6 @@ EXPORT_SYMBOL(i2c_master_send);
1186EXPORT_SYMBOL(i2c_master_recv); 1215EXPORT_SYMBOL(i2c_master_recv);
1187EXPORT_SYMBOL(i2c_control); 1216EXPORT_SYMBOL(i2c_control);
1188EXPORT_SYMBOL(i2c_transfer); 1217EXPORT_SYMBOL(i2c_transfer);
1189EXPORT_SYMBOL(i2c_adapter_id);
1190EXPORT_SYMBOL(i2c_get_adapter); 1218EXPORT_SYMBOL(i2c_get_adapter);
1191EXPORT_SYMBOL(i2c_put_adapter); 1219EXPORT_SYMBOL(i2c_put_adapter);
1192EXPORT_SYMBOL(i2c_probe); 1220EXPORT_SYMBOL(i2c_probe);