USB: xhci: Set DMA mask for host.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / i2c / i2c-core.c
blobdf937df845ebf3b50b3941c9aea569ef3bb7f22b
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* ------------------------------------------------------------------------- */
20 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
21 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
22 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/init.h>
31 #include <linux/idr.h>
32 #include <linux/mutex.h>
33 #include <linux/of_i2c.h>
34 #include <linux/of_device.h>
35 #include <linux/completion.h>
36 #include <linux/hardirq.h>
37 #include <linux/irqflags.h>
38 #include <linux/rwsem.h>
39 #include <linux/pm_runtime.h>
40 #include <asm/uaccess.h>
42 #include "i2c-core.h"
45 /* core_lock protects i2c_adapter_idr, and guarantees
46 that device detection, deletion of detected devices, and attach_adapter
47 and detach_adapter calls are serialized */
48 static DEFINE_MUTEX(core_lock);
49 static DEFINE_IDR(i2c_adapter_idr);
51 static struct device_type i2c_client_type;
52 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
54 /* ------------------------------------------------------------------------- */
56 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
57 const struct i2c_client *client)
59 while (id->name[0]) {
60 if (strcmp(client->name, id->name) == 0)
61 return id;
62 id++;
64 return NULL;
67 static int i2c_device_match(struct device *dev, struct device_driver *drv)
69 struct i2c_client *client = i2c_verify_client(dev);
70 struct i2c_driver *driver;
72 if (!client)
73 return 0;
75 /* Attempt an OF style match */
76 if (of_driver_match_device(dev, drv))
77 return 1;
79 driver = to_i2c_driver(drv);
80 /* match on an id table if there is one */
81 if (driver->id_table)
82 return i2c_match_id(driver->id_table, client) != NULL;
84 return 0;
87 #ifdef CONFIG_HOTPLUG
89 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
90 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
92 struct i2c_client *client = to_i2c_client(dev);
94 if (add_uevent_var(env, "MODALIAS=%s%s",
95 I2C_MODULE_PREFIX, client->name))
96 return -ENOMEM;
97 dev_dbg(dev, "uevent\n");
98 return 0;
101 #else
102 #define i2c_device_uevent NULL
103 #endif /* CONFIG_HOTPLUG */
105 static int i2c_device_probe(struct device *dev)
107 struct i2c_client *client = i2c_verify_client(dev);
108 struct i2c_driver *driver;
109 int status;
111 if (!client)
112 return 0;
114 driver = to_i2c_driver(dev->driver);
115 if (!driver->probe || !driver->id_table)
116 return -ENODEV;
117 client->driver = driver;
118 if (!device_can_wakeup(&client->dev))
119 device_init_wakeup(&client->dev,
120 client->flags & I2C_CLIENT_WAKE);
121 dev_dbg(dev, "probe\n");
123 status = driver->probe(client, i2c_match_id(driver->id_table, client));
124 if (status) {
125 client->driver = NULL;
126 i2c_set_clientdata(client, NULL);
128 return status;
131 static int i2c_device_remove(struct device *dev)
133 struct i2c_client *client = i2c_verify_client(dev);
134 struct i2c_driver *driver;
135 int status;
137 if (!client || !dev->driver)
138 return 0;
140 driver = to_i2c_driver(dev->driver);
141 if (driver->remove) {
142 dev_dbg(dev, "remove\n");
143 status = driver->remove(client);
144 } else {
145 dev->driver = NULL;
146 status = 0;
148 if (status == 0) {
149 client->driver = NULL;
150 i2c_set_clientdata(client, NULL);
152 return status;
155 static void i2c_device_shutdown(struct device *dev)
157 struct i2c_client *client = i2c_verify_client(dev);
158 struct i2c_driver *driver;
160 if (!client || !dev->driver)
161 return;
162 driver = to_i2c_driver(dev->driver);
163 if (driver->shutdown)
164 driver->shutdown(client);
167 #ifdef CONFIG_PM_SLEEP
168 static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
170 struct i2c_client *client = i2c_verify_client(dev);
171 struct i2c_driver *driver;
173 if (!client || !dev->driver)
174 return 0;
175 driver = to_i2c_driver(dev->driver);
176 if (!driver->suspend)
177 return 0;
178 return driver->suspend(client, mesg);
181 static int i2c_legacy_resume(struct device *dev)
183 struct i2c_client *client = i2c_verify_client(dev);
184 struct i2c_driver *driver;
186 if (!client || !dev->driver)
187 return 0;
188 driver = to_i2c_driver(dev->driver);
189 if (!driver->resume)
190 return 0;
191 return driver->resume(client);
194 static int i2c_device_pm_suspend(struct device *dev)
196 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
198 if (pm_runtime_suspended(dev))
199 return 0;
201 if (pm)
202 return pm->suspend ? pm->suspend(dev) : 0;
204 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
207 static int i2c_device_pm_resume(struct device *dev)
209 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
210 int ret;
212 if (pm)
213 ret = pm->resume ? pm->resume(dev) : 0;
214 else
215 ret = i2c_legacy_resume(dev);
217 if (!ret) {
218 pm_runtime_disable(dev);
219 pm_runtime_set_active(dev);
220 pm_runtime_enable(dev);
223 return ret;
226 static int i2c_device_pm_freeze(struct device *dev)
228 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
230 if (pm_runtime_suspended(dev))
231 return 0;
233 if (pm)
234 return pm->freeze ? pm->freeze(dev) : 0;
236 return i2c_legacy_suspend(dev, PMSG_FREEZE);
239 static int i2c_device_pm_thaw(struct device *dev)
241 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
243 if (pm_runtime_suspended(dev))
244 return 0;
246 if (pm)
247 return pm->thaw ? pm->thaw(dev) : 0;
249 return i2c_legacy_resume(dev);
252 static int i2c_device_pm_poweroff(struct device *dev)
254 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
256 if (pm_runtime_suspended(dev))
257 return 0;
259 if (pm)
260 return pm->poweroff ? pm->poweroff(dev) : 0;
262 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
265 static int i2c_device_pm_restore(struct device *dev)
267 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
268 int ret;
270 if (pm)
271 ret = pm->restore ? pm->restore(dev) : 0;
272 else
273 ret = i2c_legacy_resume(dev);
275 if (!ret) {
276 pm_runtime_disable(dev);
277 pm_runtime_set_active(dev);
278 pm_runtime_enable(dev);
281 return ret;
283 #else /* !CONFIG_PM_SLEEP */
284 #define i2c_device_pm_suspend NULL
285 #define i2c_device_pm_resume NULL
286 #define i2c_device_pm_freeze NULL
287 #define i2c_device_pm_thaw NULL
288 #define i2c_device_pm_poweroff NULL
289 #define i2c_device_pm_restore NULL
290 #endif /* !CONFIG_PM_SLEEP */
292 static void i2c_client_dev_release(struct device *dev)
294 kfree(to_i2c_client(dev));
297 static ssize_t
298 show_name(struct device *dev, struct device_attribute *attr, char *buf)
300 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
301 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
304 static ssize_t
305 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
307 struct i2c_client *client = to_i2c_client(dev);
308 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
311 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
312 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
314 static struct attribute *i2c_dev_attrs[] = {
315 &dev_attr_name.attr,
316 /* modalias helps coldplug: modprobe $(cat .../modalias) */
317 &dev_attr_modalias.attr,
318 NULL
321 static struct attribute_group i2c_dev_attr_group = {
322 .attrs = i2c_dev_attrs,
325 static const struct attribute_group *i2c_dev_attr_groups[] = {
326 &i2c_dev_attr_group,
327 NULL
330 static const struct dev_pm_ops i2c_device_pm_ops = {
331 .suspend = i2c_device_pm_suspend,
332 .resume = i2c_device_pm_resume,
333 .freeze = i2c_device_pm_freeze,
334 .thaw = i2c_device_pm_thaw,
335 .poweroff = i2c_device_pm_poweroff,
336 .restore = i2c_device_pm_restore,
337 SET_RUNTIME_PM_OPS(
338 pm_generic_runtime_suspend,
339 pm_generic_runtime_resume,
340 pm_generic_runtime_idle
344 struct bus_type i2c_bus_type = {
345 .name = "i2c",
346 .match = i2c_device_match,
347 .probe = i2c_device_probe,
348 .remove = i2c_device_remove,
349 .shutdown = i2c_device_shutdown,
350 .pm = &i2c_device_pm_ops,
352 EXPORT_SYMBOL_GPL(i2c_bus_type);
354 static struct device_type i2c_client_type = {
355 .groups = i2c_dev_attr_groups,
356 .uevent = i2c_device_uevent,
357 .release = i2c_client_dev_release,
362 * i2c_verify_client - return parameter as i2c_client, or NULL
363 * @dev: device, probably from some driver model iterator
365 * When traversing the driver model tree, perhaps using driver model
366 * iterators like @device_for_each_child(), you can't assume very much
367 * about the nodes you find. Use this function to avoid oopses caused
368 * by wrongly treating some non-I2C device as an i2c_client.
370 struct i2c_client *i2c_verify_client(struct device *dev)
372 return (dev->type == &i2c_client_type)
373 ? to_i2c_client(dev)
374 : NULL;
376 EXPORT_SYMBOL(i2c_verify_client);
379 /* This is a permissive address validity check, I2C address map constraints
380 * are purposedly not enforced, except for the general call address. */
381 static int i2c_check_client_addr_validity(const struct i2c_client *client)
383 if (client->flags & I2C_CLIENT_TEN) {
384 /* 10-bit address, all values are valid */
385 if (client->addr > 0x3ff)
386 return -EINVAL;
387 } else {
388 /* 7-bit address, reject the general call address */
389 if (client->addr == 0x00 || client->addr > 0x7f)
390 return -EINVAL;
392 return 0;
395 /* And this is a strict address validity check, used when probing. If a
396 * device uses a reserved address, then it shouldn't be probed. 7-bit
397 * addressing is assumed, 10-bit address devices are rare and should be
398 * explicitly enumerated. */
399 static int i2c_check_addr_validity(unsigned short addr)
402 * Reserved addresses per I2C specification:
403 * 0x00 General call address / START byte
404 * 0x01 CBUS address
405 * 0x02 Reserved for different bus format
406 * 0x03 Reserved for future purposes
407 * 0x04-0x07 Hs-mode master code
408 * 0x78-0x7b 10-bit slave addressing
409 * 0x7c-0x7f Reserved for future purposes
411 if (addr < 0x08 || addr > 0x77)
412 return -EINVAL;
413 return 0;
416 static int __i2c_check_addr_busy(struct device *dev, void *addrp)
418 struct i2c_client *client = i2c_verify_client(dev);
419 int addr = *(int *)addrp;
421 if (client && client->addr == addr)
422 return -EBUSY;
423 return 0;
426 static int i2c_check_addr_busy(struct i2c_adapter *adapter, int addr)
428 return device_for_each_child(&adapter->dev, &addr,
429 __i2c_check_addr_busy);
433 * i2c_new_device - instantiate an i2c device
434 * @adap: the adapter managing the device
435 * @info: describes one I2C device; bus_num is ignored
436 * Context: can sleep
438 * Create an i2c device. Binding is handled through driver model
439 * probe()/remove() methods. A driver may be bound to this device when we
440 * return from this function, or any later moment (e.g. maybe hotplugging will
441 * load the driver module). This call is not appropriate for use by mainboard
442 * initialization logic, which usually runs during an arch_initcall() long
443 * before any i2c_adapter could exist.
445 * This returns the new i2c client, which may be saved for later use with
446 * i2c_unregister_device(); or NULL to indicate an error.
448 struct i2c_client *
449 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
451 struct i2c_client *client;
452 int status;
454 client = kzalloc(sizeof *client, GFP_KERNEL);
455 if (!client)
456 return NULL;
458 client->adapter = adap;
460 client->dev.platform_data = info->platform_data;
462 if (info->archdata)
463 client->dev.archdata = *info->archdata;
465 client->flags = info->flags;
466 client->addr = info->addr;
467 client->irq = info->irq;
469 strlcpy(client->name, info->type, sizeof(client->name));
471 /* Check for address validity */
472 status = i2c_check_client_addr_validity(client);
473 if (status) {
474 dev_err(&adap->dev, "Invalid %d-bit I2C address 0x%02hx\n",
475 client->flags & I2C_CLIENT_TEN ? 10 : 7, client->addr);
476 goto out_err_silent;
479 /* Check for address business */
480 status = i2c_check_addr_busy(adap, client->addr);
481 if (status)
482 goto out_err;
484 client->dev.parent = &client->adapter->dev;
485 client->dev.bus = &i2c_bus_type;
486 client->dev.type = &i2c_client_type;
487 #ifdef CONFIG_OF
488 client->dev.of_node = info->of_node;
489 #endif
491 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
492 client->addr);
493 status = device_register(&client->dev);
494 if (status)
495 goto out_err;
497 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
498 client->name, dev_name(&client->dev));
500 return client;
502 out_err:
503 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
504 "(%d)\n", client->name, client->addr, status);
505 out_err_silent:
506 kfree(client);
507 return NULL;
509 EXPORT_SYMBOL_GPL(i2c_new_device);
513 * i2c_unregister_device - reverse effect of i2c_new_device()
514 * @client: value returned from i2c_new_device()
515 * Context: can sleep
517 void i2c_unregister_device(struct i2c_client *client)
519 device_unregister(&client->dev);
521 EXPORT_SYMBOL_GPL(i2c_unregister_device);
524 static const struct i2c_device_id dummy_id[] = {
525 { "dummy", 0 },
526 { },
529 static int dummy_probe(struct i2c_client *client,
530 const struct i2c_device_id *id)
532 return 0;
535 static int dummy_remove(struct i2c_client *client)
537 return 0;
540 static struct i2c_driver dummy_driver = {
541 .driver.name = "dummy",
542 .probe = dummy_probe,
543 .remove = dummy_remove,
544 .id_table = dummy_id,
548 * i2c_new_dummy - return a new i2c device bound to a dummy driver
549 * @adapter: the adapter managing the device
550 * @address: seven bit address to be used
551 * Context: can sleep
553 * This returns an I2C client bound to the "dummy" driver, intended for use
554 * with devices that consume multiple addresses. Examples of such chips
555 * include various EEPROMS (like 24c04 and 24c08 models).
557 * These dummy devices have two main uses. First, most I2C and SMBus calls
558 * except i2c_transfer() need a client handle; the dummy will be that handle.
559 * And second, this prevents the specified address from being bound to a
560 * different driver.
562 * This returns the new i2c client, which should be saved for later use with
563 * i2c_unregister_device(); or NULL to indicate an error.
565 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
567 struct i2c_board_info info = {
568 I2C_BOARD_INFO("dummy", address),
571 return i2c_new_device(adapter, &info);
573 EXPORT_SYMBOL_GPL(i2c_new_dummy);
575 /* ------------------------------------------------------------------------- */
577 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
579 static void i2c_adapter_dev_release(struct device *dev)
581 struct i2c_adapter *adap = to_i2c_adapter(dev);
582 complete(&adap->dev_released);
586 * Let users instantiate I2C devices through sysfs. This can be used when
587 * platform initialization code doesn't contain the proper data for
588 * whatever reason. Also useful for drivers that do device detection and
589 * detection fails, either because the device uses an unexpected address,
590 * or this is a compatible device with different ID register values.
592 * Parameter checking may look overzealous, but we really don't want
593 * the user to provide incorrect parameters.
595 static ssize_t
596 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
597 const char *buf, size_t count)
599 struct i2c_adapter *adap = to_i2c_adapter(dev);
600 struct i2c_board_info info;
601 struct i2c_client *client;
602 char *blank, end;
603 int res;
605 dev_warn(dev, "The new_device interface is still experimental "
606 "and may change in a near future\n");
607 memset(&info, 0, sizeof(struct i2c_board_info));
609 blank = strchr(buf, ' ');
610 if (!blank) {
611 dev_err(dev, "%s: Missing parameters\n", "new_device");
612 return -EINVAL;
614 if (blank - buf > I2C_NAME_SIZE - 1) {
615 dev_err(dev, "%s: Invalid device name\n", "new_device");
616 return -EINVAL;
618 memcpy(info.type, buf, blank - buf);
620 /* Parse remaining parameters, reject extra parameters */
621 res = sscanf(++blank, "%hi%c", &info.addr, &end);
622 if (res < 1) {
623 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
624 return -EINVAL;
626 if (res > 1 && end != '\n') {
627 dev_err(dev, "%s: Extra parameters\n", "new_device");
628 return -EINVAL;
631 client = i2c_new_device(adap, &info);
632 if (!client)
633 return -EINVAL;
635 /* Keep track of the added device */
636 i2c_lock_adapter(adap);
637 list_add_tail(&client->detected, &adap->userspace_clients);
638 i2c_unlock_adapter(adap);
639 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
640 info.type, info.addr);
642 return count;
646 * And of course let the users delete the devices they instantiated, if
647 * they got it wrong. This interface can only be used to delete devices
648 * instantiated by i2c_sysfs_new_device above. This guarantees that we
649 * don't delete devices to which some kernel code still has references.
651 * Parameter checking may look overzealous, but we really don't want
652 * the user to delete the wrong device.
654 static ssize_t
655 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
656 const char *buf, size_t count)
658 struct i2c_adapter *adap = to_i2c_adapter(dev);
659 struct i2c_client *client, *next;
660 unsigned short addr;
661 char end;
662 int res;
664 /* Parse parameters, reject extra parameters */
665 res = sscanf(buf, "%hi%c", &addr, &end);
666 if (res < 1) {
667 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
668 return -EINVAL;
670 if (res > 1 && end != '\n') {
671 dev_err(dev, "%s: Extra parameters\n", "delete_device");
672 return -EINVAL;
675 /* Make sure the device was added through sysfs */
676 res = -ENOENT;
677 i2c_lock_adapter(adap);
678 list_for_each_entry_safe(client, next, &adap->userspace_clients,
679 detected) {
680 if (client->addr == addr) {
681 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
682 "delete_device", client->name, client->addr);
684 list_del(&client->detected);
685 i2c_unregister_device(client);
686 res = count;
687 break;
690 i2c_unlock_adapter(adap);
692 if (res < 0)
693 dev_err(dev, "%s: Can't find device in list\n",
694 "delete_device");
695 return res;
698 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
699 static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
701 static struct attribute *i2c_adapter_attrs[] = {
702 &dev_attr_name.attr,
703 &dev_attr_new_device.attr,
704 &dev_attr_delete_device.attr,
705 NULL
708 static struct attribute_group i2c_adapter_attr_group = {
709 .attrs = i2c_adapter_attrs,
712 static const struct attribute_group *i2c_adapter_attr_groups[] = {
713 &i2c_adapter_attr_group,
714 NULL
717 static struct device_type i2c_adapter_type = {
718 .groups = i2c_adapter_attr_groups,
719 .release = i2c_adapter_dev_release,
722 #ifdef CONFIG_I2C_COMPAT
723 static struct class_compat *i2c_adapter_compat_class;
724 #endif
726 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
728 struct i2c_devinfo *devinfo;
730 down_read(&__i2c_board_lock);
731 list_for_each_entry(devinfo, &__i2c_board_list, list) {
732 if (devinfo->busnum == adapter->nr
733 && !i2c_new_device(adapter,
734 &devinfo->board_info))
735 dev_err(&adapter->dev,
736 "Can't create device at 0x%02x\n",
737 devinfo->board_info.addr);
739 up_read(&__i2c_board_lock);
742 static int i2c_do_add_adapter(struct i2c_driver *driver,
743 struct i2c_adapter *adap)
745 /* Detect supported devices on that bus, and instantiate them */
746 i2c_detect(adap, driver);
748 /* Let legacy drivers scan this bus for matching devices */
749 if (driver->attach_adapter) {
750 /* We ignore the return code; if it fails, too bad */
751 driver->attach_adapter(adap);
753 return 0;
756 static int __process_new_adapter(struct device_driver *d, void *data)
758 return i2c_do_add_adapter(to_i2c_driver(d), data);
761 static int i2c_register_adapter(struct i2c_adapter *adap)
763 int res = 0, dummy;
765 /* Can't register until after driver model init */
766 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
767 res = -EAGAIN;
768 goto out_list;
771 rt_mutex_init(&adap->bus_lock);
772 INIT_LIST_HEAD(&adap->userspace_clients);
774 /* Set default timeout to 1 second if not already set */
775 if (adap->timeout == 0)
776 adap->timeout = HZ;
778 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
779 adap->dev.bus = &i2c_bus_type;
780 adap->dev.type = &i2c_adapter_type;
781 res = device_register(&adap->dev);
782 if (res)
783 goto out_list;
785 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
787 #ifdef CONFIG_I2C_COMPAT
788 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
789 adap->dev.parent);
790 if (res)
791 dev_warn(&adap->dev,
792 "Failed to create compatibility class link\n");
793 #endif
795 /* create pre-declared device nodes */
796 if (adap->nr < __i2c_first_dynamic_bus_num)
797 i2c_scan_static_board_info(adap);
799 /* Register devices from the device tree */
800 of_i2c_register_devices(adap);
802 /* Notify drivers */
803 mutex_lock(&core_lock);
804 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
805 __process_new_adapter);
806 mutex_unlock(&core_lock);
808 return 0;
810 out_list:
811 mutex_lock(&core_lock);
812 idr_remove(&i2c_adapter_idr, adap->nr);
813 mutex_unlock(&core_lock);
814 return res;
818 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
819 * @adapter: the adapter to add
820 * Context: can sleep
822 * This routine is used to declare an I2C adapter when its bus number
823 * doesn't matter. Examples: for I2C adapters dynamically added by
824 * USB links or PCI plugin cards.
826 * When this returns zero, a new bus number was allocated and stored
827 * in adap->nr, and the specified adapter became available for clients.
828 * Otherwise, a negative errno value is returned.
830 int i2c_add_adapter(struct i2c_adapter *adapter)
832 int id, res = 0;
834 retry:
835 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
836 return -ENOMEM;
838 mutex_lock(&core_lock);
839 /* "above" here means "above or equal to", sigh */
840 res = idr_get_new_above(&i2c_adapter_idr, adapter,
841 __i2c_first_dynamic_bus_num, &id);
842 mutex_unlock(&core_lock);
844 if (res < 0) {
845 if (res == -EAGAIN)
846 goto retry;
847 return res;
850 adapter->nr = id;
851 return i2c_register_adapter(adapter);
853 EXPORT_SYMBOL(i2c_add_adapter);
856 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
857 * @adap: the adapter to register (with adap->nr initialized)
858 * Context: can sleep
860 * This routine is used to declare an I2C adapter when its bus number
861 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
862 * or otherwise built in to the system's mainboard, and where i2c_board_info
863 * is used to properly configure I2C devices.
865 * If no devices have pre-been declared for this bus, then be sure to
866 * register the adapter before any dynamically allocated ones. Otherwise
867 * the required bus ID may not be available.
869 * When this returns zero, the specified adapter became available for
870 * clients using the bus number provided in adap->nr. Also, the table
871 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
872 * and the appropriate driver model device nodes are created. Otherwise, a
873 * negative errno value is returned.
875 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
877 int id;
878 int status;
880 if (adap->nr & ~MAX_ID_MASK)
881 return -EINVAL;
883 retry:
884 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
885 return -ENOMEM;
887 mutex_lock(&core_lock);
888 /* "above" here means "above or equal to", sigh;
889 * we need the "equal to" result to force the result
891 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
892 if (status == 0 && id != adap->nr) {
893 status = -EBUSY;
894 idr_remove(&i2c_adapter_idr, id);
896 mutex_unlock(&core_lock);
897 if (status == -EAGAIN)
898 goto retry;
900 if (status == 0)
901 status = i2c_register_adapter(adap);
902 return status;
904 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
906 static int i2c_do_del_adapter(struct i2c_driver *driver,
907 struct i2c_adapter *adapter)
909 struct i2c_client *client, *_n;
910 int res;
912 /* Remove the devices we created ourselves as the result of hardware
913 * probing (using a driver's detect method) */
914 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
915 if (client->adapter == adapter) {
916 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
917 client->name, client->addr);
918 list_del(&client->detected);
919 i2c_unregister_device(client);
923 if (!driver->detach_adapter)
924 return 0;
925 res = driver->detach_adapter(adapter);
926 if (res)
927 dev_err(&adapter->dev, "detach_adapter failed (%d) "
928 "for driver [%s]\n", res, driver->driver.name);
929 return res;
932 static int __unregister_client(struct device *dev, void *dummy)
934 struct i2c_client *client = i2c_verify_client(dev);
935 if (client)
936 i2c_unregister_device(client);
937 return 0;
940 static int __process_removed_adapter(struct device_driver *d, void *data)
942 return i2c_do_del_adapter(to_i2c_driver(d), data);
946 * i2c_del_adapter - unregister I2C adapter
947 * @adap: the adapter being unregistered
948 * Context: can sleep
950 * This unregisters an I2C adapter which was previously registered
951 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
953 int i2c_del_adapter(struct i2c_adapter *adap)
955 int res = 0;
956 struct i2c_adapter *found;
957 struct i2c_client *client, *next;
959 /* First make sure that this adapter was ever added */
960 mutex_lock(&core_lock);
961 found = idr_find(&i2c_adapter_idr, adap->nr);
962 mutex_unlock(&core_lock);
963 if (found != adap) {
964 pr_debug("i2c-core: attempting to delete unregistered "
965 "adapter [%s]\n", adap->name);
966 return -EINVAL;
969 /* Tell drivers about this removal */
970 mutex_lock(&core_lock);
971 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
972 __process_removed_adapter);
973 mutex_unlock(&core_lock);
974 if (res)
975 return res;
977 /* Remove devices instantiated from sysfs */
978 i2c_lock_adapter(adap);
979 list_for_each_entry_safe(client, next, &adap->userspace_clients,
980 detected) {
981 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
982 client->addr);
983 list_del(&client->detected);
984 i2c_unregister_device(client);
986 i2c_unlock_adapter(adap);
988 /* Detach any active clients. This can't fail, thus we do not
989 checking the returned value. */
990 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
992 #ifdef CONFIG_I2C_COMPAT
993 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
994 adap->dev.parent);
995 #endif
997 /* device name is gone after device_unregister */
998 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
1000 /* clean up the sysfs representation */
1001 init_completion(&adap->dev_released);
1002 device_unregister(&adap->dev);
1004 /* wait for sysfs to drop all references */
1005 wait_for_completion(&adap->dev_released);
1007 /* free bus id */
1008 mutex_lock(&core_lock);
1009 idr_remove(&i2c_adapter_idr, adap->nr);
1010 mutex_unlock(&core_lock);
1012 /* Clear the device structure in case this adapter is ever going to be
1013 added again */
1014 memset(&adap->dev, 0, sizeof(adap->dev));
1016 return 0;
1018 EXPORT_SYMBOL(i2c_del_adapter);
1021 /* ------------------------------------------------------------------------- */
1023 static int __process_new_driver(struct device *dev, void *data)
1025 if (dev->type != &i2c_adapter_type)
1026 return 0;
1027 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
1031 * An i2c_driver is used with one or more i2c_client (device) nodes to access
1032 * i2c slave chips, on a bus instance associated with some i2c_adapter.
1035 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
1037 int res;
1039 /* Can't register until after driver model init */
1040 if (unlikely(WARN_ON(!i2c_bus_type.p)))
1041 return -EAGAIN;
1043 /* add the driver to the list of i2c drivers in the driver core */
1044 driver->driver.owner = owner;
1045 driver->driver.bus = &i2c_bus_type;
1047 /* When registration returns, the driver core
1048 * will have called probe() for all matching-but-unbound devices.
1050 res = driver_register(&driver->driver);
1051 if (res)
1052 return res;
1054 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
1056 INIT_LIST_HEAD(&driver->clients);
1057 /* Walk the adapters that are already present */
1058 mutex_lock(&core_lock);
1059 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
1060 mutex_unlock(&core_lock);
1062 return 0;
1064 EXPORT_SYMBOL(i2c_register_driver);
1066 static int __process_removed_driver(struct device *dev, void *data)
1068 if (dev->type != &i2c_adapter_type)
1069 return 0;
1070 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
1074 * i2c_del_driver - unregister I2C driver
1075 * @driver: the driver being unregistered
1076 * Context: can sleep
1078 void i2c_del_driver(struct i2c_driver *driver)
1080 mutex_lock(&core_lock);
1081 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
1082 mutex_unlock(&core_lock);
1084 driver_unregister(&driver->driver);
1085 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
1087 EXPORT_SYMBOL(i2c_del_driver);
1089 /* ------------------------------------------------------------------------- */
1092 * i2c_use_client - increments the reference count of the i2c client structure
1093 * @client: the client being referenced
1095 * Each live reference to a client should be refcounted. The driver model does
1096 * that automatically as part of driver binding, so that most drivers don't
1097 * need to do this explicitly: they hold a reference until they're unbound
1098 * from the device.
1100 * A pointer to the client with the incremented reference counter is returned.
1102 struct i2c_client *i2c_use_client(struct i2c_client *client)
1104 if (client && get_device(&client->dev))
1105 return client;
1106 return NULL;
1108 EXPORT_SYMBOL(i2c_use_client);
1111 * i2c_release_client - release a use of the i2c client structure
1112 * @client: the client being no longer referenced
1114 * Must be called when a user of a client is finished with it.
1116 void i2c_release_client(struct i2c_client *client)
1118 if (client)
1119 put_device(&client->dev);
1121 EXPORT_SYMBOL(i2c_release_client);
1123 struct i2c_cmd_arg {
1124 unsigned cmd;
1125 void *arg;
1128 static int i2c_cmd(struct device *dev, void *_arg)
1130 struct i2c_client *client = i2c_verify_client(dev);
1131 struct i2c_cmd_arg *arg = _arg;
1133 if (client && client->driver && client->driver->command)
1134 client->driver->command(client, arg->cmd, arg->arg);
1135 return 0;
1138 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1140 struct i2c_cmd_arg cmd_arg;
1142 cmd_arg.cmd = cmd;
1143 cmd_arg.arg = arg;
1144 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1146 EXPORT_SYMBOL(i2c_clients_command);
1148 static int __init i2c_init(void)
1150 int retval;
1152 retval = bus_register(&i2c_bus_type);
1153 if (retval)
1154 return retval;
1155 #ifdef CONFIG_I2C_COMPAT
1156 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1157 if (!i2c_adapter_compat_class) {
1158 retval = -ENOMEM;
1159 goto bus_err;
1161 #endif
1162 retval = i2c_add_driver(&dummy_driver);
1163 if (retval)
1164 goto class_err;
1165 return 0;
1167 class_err:
1168 #ifdef CONFIG_I2C_COMPAT
1169 class_compat_unregister(i2c_adapter_compat_class);
1170 bus_err:
1171 #endif
1172 bus_unregister(&i2c_bus_type);
1173 return retval;
1176 static void __exit i2c_exit(void)
1178 i2c_del_driver(&dummy_driver);
1179 #ifdef CONFIG_I2C_COMPAT
1180 class_compat_unregister(i2c_adapter_compat_class);
1181 #endif
1182 bus_unregister(&i2c_bus_type);
1185 /* We must initialize early, because some subsystems register i2c drivers
1186 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1188 postcore_initcall(i2c_init);
1189 module_exit(i2c_exit);
1191 /* ----------------------------------------------------
1192 * the functional interface to the i2c busses.
1193 * ----------------------------------------------------
1197 * i2c_transfer - execute a single or combined I2C message
1198 * @adap: Handle to I2C bus
1199 * @msgs: One or more messages to execute before STOP is issued to
1200 * terminate the operation; each message begins with a START.
1201 * @num: Number of messages to be executed.
1203 * Returns negative errno, else the number of messages executed.
1205 * Note that there is no requirement that each message be sent to
1206 * the same slave address, although that is the most common model.
1208 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1210 unsigned long orig_jiffies;
1211 int ret, try;
1213 /* REVISIT the fault reporting model here is weak:
1215 * - When we get an error after receiving N bytes from a slave,
1216 * there is no way to report "N".
1218 * - When we get a NAK after transmitting N bytes to a slave,
1219 * there is no way to report "N" ... or to let the master
1220 * continue executing the rest of this combined message, if
1221 * that's the appropriate response.
1223 * - When for example "num" is two and we successfully complete
1224 * the first message but get an error part way through the
1225 * second, it's unclear whether that should be reported as
1226 * one (discarding status on the second message) or errno
1227 * (discarding status on the first one).
1230 if (adap->algo->master_xfer) {
1231 #ifdef DEBUG
1232 for (ret = 0; ret < num; ret++) {
1233 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1234 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1235 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1236 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1238 #endif
1240 if (in_atomic() || irqs_disabled()) {
1241 ret = rt_mutex_trylock(&adap->bus_lock);
1242 if (!ret)
1243 /* I2C activity is ongoing. */
1244 return -EAGAIN;
1245 } else {
1246 rt_mutex_lock(&adap->bus_lock);
1249 /* Retry automatically on arbitration loss */
1250 orig_jiffies = jiffies;
1251 for (ret = 0, try = 0; try <= adap->retries; try++) {
1252 ret = adap->algo->master_xfer(adap, msgs, num);
1253 if (ret != -EAGAIN)
1254 break;
1255 if (time_after(jiffies, orig_jiffies + adap->timeout))
1256 break;
1258 rt_mutex_unlock(&adap->bus_lock);
1260 return ret;
1261 } else {
1262 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1263 return -EOPNOTSUPP;
1266 EXPORT_SYMBOL(i2c_transfer);
1269 * i2c_master_send - issue a single I2C message in master transmit mode
1270 * @client: Handle to slave device
1271 * @buf: Data that will be written to the slave
1272 * @count: How many bytes to write, must be less than 64k since msg.len is u16
1274 * Returns negative errno, or else the number of bytes written.
1276 int i2c_master_send(struct i2c_client *client, const char *buf, int count)
1278 int ret;
1279 struct i2c_adapter *adap = client->adapter;
1280 struct i2c_msg msg;
1282 msg.addr = client->addr;
1283 msg.flags = client->flags & I2C_M_TEN;
1284 msg.len = count;
1285 msg.buf = (char *)buf;
1287 ret = i2c_transfer(adap, &msg, 1);
1289 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1290 transmitted, else error code. */
1291 return (ret == 1) ? count : ret;
1293 EXPORT_SYMBOL(i2c_master_send);
1296 * i2c_master_recv - issue a single I2C message in master receive mode
1297 * @client: Handle to slave device
1298 * @buf: Where to store data read from slave
1299 * @count: How many bytes to read, must be less than 64k since msg.len is u16
1301 * Returns negative errno, or else the number of bytes read.
1303 int i2c_master_recv(struct i2c_client *client, char *buf, int count)
1305 struct i2c_adapter *adap = client->adapter;
1306 struct i2c_msg msg;
1307 int ret;
1309 msg.addr = client->addr;
1310 msg.flags = client->flags & I2C_M_TEN;
1311 msg.flags |= I2C_M_RD;
1312 msg.len = count;
1313 msg.buf = buf;
1315 ret = i2c_transfer(adap, &msg, 1);
1317 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1318 transmitted, else error code. */
1319 return (ret == 1) ? count : ret;
1321 EXPORT_SYMBOL(i2c_master_recv);
1323 /* ----------------------------------------------------
1324 * the i2c address scanning function
1325 * Will not work for 10-bit addresses!
1326 * ----------------------------------------------------
1330 * Legacy default probe function, mostly relevant for SMBus. The default
1331 * probe method is a quick write, but it is known to corrupt the 24RF08
1332 * EEPROMs due to a state machine bug, and could also irreversibly
1333 * write-protect some EEPROMs, so for address ranges 0x30-0x37 and 0x50-0x5f,
1334 * we use a short byte read instead. Also, some bus drivers don't implement
1335 * quick write, so we fallback to a byte read in that case too.
1336 * On x86, there is another special case for FSC hardware monitoring chips,
1337 * which want regular byte reads (address 0x73.) Fortunately, these are the
1338 * only known chips using this I2C address on PC hardware.
1339 * Returns 1 if probe succeeded, 0 if not.
1341 static int i2c_default_probe(struct i2c_adapter *adap, unsigned short addr)
1343 int err;
1344 union i2c_smbus_data dummy;
1346 #ifdef CONFIG_X86
1347 if (addr == 0x73 && (adap->class & I2C_CLASS_HWMON)
1348 && i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE_DATA))
1349 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1350 I2C_SMBUS_BYTE_DATA, &dummy);
1351 else
1352 #endif
1353 if ((addr & ~0x07) == 0x30 || (addr & ~0x0f) == 0x50
1354 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK))
1355 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_READ, 0,
1356 I2C_SMBUS_BYTE, &dummy);
1357 else
1358 err = i2c_smbus_xfer(adap, addr, 0, I2C_SMBUS_WRITE, 0,
1359 I2C_SMBUS_QUICK, NULL);
1361 return err >= 0;
1364 static int i2c_detect_address(struct i2c_client *temp_client,
1365 struct i2c_driver *driver)
1367 struct i2c_board_info info;
1368 struct i2c_adapter *adapter = temp_client->adapter;
1369 int addr = temp_client->addr;
1370 int err;
1372 /* Make sure the address is valid */
1373 err = i2c_check_addr_validity(addr);
1374 if (err) {
1375 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1376 addr);
1377 return err;
1380 /* Skip if already in use */
1381 if (i2c_check_addr_busy(adapter, addr))
1382 return 0;
1384 /* Make sure there is something at this address */
1385 if (!i2c_default_probe(adapter, addr))
1386 return 0;
1388 /* Finally call the custom detection function */
1389 memset(&info, 0, sizeof(struct i2c_board_info));
1390 info.addr = addr;
1391 err = driver->detect(temp_client, &info);
1392 if (err) {
1393 /* -ENODEV is returned if the detection fails. We catch it
1394 here as this isn't an error. */
1395 return err == -ENODEV ? 0 : err;
1398 /* Consistency check */
1399 if (info.type[0] == '\0') {
1400 dev_err(&adapter->dev, "%s detection function provided "
1401 "no name for 0x%x\n", driver->driver.name,
1402 addr);
1403 } else {
1404 struct i2c_client *client;
1406 /* Detection succeeded, instantiate the device */
1407 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1408 info.type, info.addr);
1409 client = i2c_new_device(adapter, &info);
1410 if (client)
1411 list_add_tail(&client->detected, &driver->clients);
1412 else
1413 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1414 info.type, info.addr);
1416 return 0;
1419 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1421 const unsigned short *address_list;
1422 struct i2c_client *temp_client;
1423 int i, err = 0;
1424 int adap_id = i2c_adapter_id(adapter);
1426 address_list = driver->address_list;
1427 if (!driver->detect || !address_list)
1428 return 0;
1430 /* Set up a temporary client to help detect callback */
1431 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1432 if (!temp_client)
1433 return -ENOMEM;
1434 temp_client->adapter = adapter;
1436 /* Stop here if the classes do not match */
1437 if (!(adapter->class & driver->class))
1438 goto exit_free;
1440 /* Stop here if the bus doesn't support probing */
1441 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_READ_BYTE)) {
1442 if (address_list[0] == I2C_CLIENT_END)
1443 goto exit_free;
1445 dev_warn(&adapter->dev, "Probing not supported\n");
1446 err = -EOPNOTSUPP;
1447 goto exit_free;
1450 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
1451 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1452 "addr 0x%02x\n", adap_id, address_list[i]);
1453 temp_client->addr = address_list[i];
1454 err = i2c_detect_address(temp_client, driver);
1455 if (err)
1456 goto exit_free;
1459 exit_free:
1460 kfree(temp_client);
1461 return err;
1464 struct i2c_client *
1465 i2c_new_probed_device(struct i2c_adapter *adap,
1466 struct i2c_board_info *info,
1467 unsigned short const *addr_list)
1469 int i;
1471 /* Stop here if the bus doesn't support probing */
1472 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1473 dev_err(&adap->dev, "Probing not supported\n");
1474 return NULL;
1477 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1478 /* Check address validity */
1479 if (i2c_check_addr_validity(addr_list[i]) < 0) {
1480 dev_warn(&adap->dev, "Invalid 7-bit address "
1481 "0x%02x\n", addr_list[i]);
1482 continue;
1485 /* Check address availability */
1486 if (i2c_check_addr_busy(adap, addr_list[i])) {
1487 dev_dbg(&adap->dev, "Address 0x%02x already in "
1488 "use, not probing\n", addr_list[i]);
1489 continue;
1492 /* Test address responsiveness */
1493 if (i2c_default_probe(adap, addr_list[i]))
1494 break;
1497 if (addr_list[i] == I2C_CLIENT_END) {
1498 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1499 return NULL;
1502 info->addr = addr_list[i];
1503 return i2c_new_device(adap, info);
1505 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1507 struct i2c_adapter *i2c_get_adapter(int id)
1509 struct i2c_adapter *adapter;
1511 mutex_lock(&core_lock);
1512 adapter = idr_find(&i2c_adapter_idr, id);
1513 if (adapter && !try_module_get(adapter->owner))
1514 adapter = NULL;
1516 mutex_unlock(&core_lock);
1517 return adapter;
1519 EXPORT_SYMBOL(i2c_get_adapter);
1521 void i2c_put_adapter(struct i2c_adapter *adap)
1523 module_put(adap->owner);
1525 EXPORT_SYMBOL(i2c_put_adapter);
1527 /* The SMBus parts */
1529 #define POLY (0x1070U << 3)
1530 static u8 crc8(u16 data)
1532 int i;
1534 for (i = 0; i < 8; i++) {
1535 if (data & 0x8000)
1536 data = data ^ POLY;
1537 data = data << 1;
1539 return (u8)(data >> 8);
1542 /* Incremental CRC8 over count bytes in the array pointed to by p */
1543 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1545 int i;
1547 for (i = 0; i < count; i++)
1548 crc = crc8((crc ^ p[i]) << 8);
1549 return crc;
1552 /* Assume a 7-bit address, which is reasonable for SMBus */
1553 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1555 /* The address will be sent first */
1556 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1557 pec = i2c_smbus_pec(pec, &addr, 1);
1559 /* The data buffer follows */
1560 return i2c_smbus_pec(pec, msg->buf, msg->len);
1563 /* Used for write only transactions */
1564 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1566 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1567 msg->len++;
1570 /* Return <0 on CRC error
1571 If there was a write before this read (most cases) we need to take the
1572 partial CRC from the write part into account.
1573 Note that this function does modify the message (we need to decrease the
1574 message length to hide the CRC byte from the caller). */
1575 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1577 u8 rpec = msg->buf[--msg->len];
1578 cpec = i2c_smbus_msg_pec(cpec, msg);
1580 if (rpec != cpec) {
1581 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1582 rpec, cpec);
1583 return -EBADMSG;
1585 return 0;
1589 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1590 * @client: Handle to slave device
1592 * This executes the SMBus "receive byte" protocol, returning negative errno
1593 * else the byte received from the device.
1595 s32 i2c_smbus_read_byte(struct i2c_client *client)
1597 union i2c_smbus_data data;
1598 int status;
1600 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1601 I2C_SMBUS_READ, 0,
1602 I2C_SMBUS_BYTE, &data);
1603 return (status < 0) ? status : data.byte;
1605 EXPORT_SYMBOL(i2c_smbus_read_byte);
1608 * i2c_smbus_write_byte - SMBus "send byte" protocol
1609 * @client: Handle to slave device
1610 * @value: Byte to be sent
1612 * This executes the SMBus "send byte" protocol, returning negative errno
1613 * else zero on success.
1615 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1617 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1618 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1620 EXPORT_SYMBOL(i2c_smbus_write_byte);
1623 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1624 * @client: Handle to slave device
1625 * @command: Byte interpreted by slave
1627 * This executes the SMBus "read byte" protocol, returning negative errno
1628 * else a data byte received from the device.
1630 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1632 union i2c_smbus_data data;
1633 int status;
1635 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1636 I2C_SMBUS_READ, command,
1637 I2C_SMBUS_BYTE_DATA, &data);
1638 return (status < 0) ? status : data.byte;
1640 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1643 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1644 * @client: Handle to slave device
1645 * @command: Byte interpreted by slave
1646 * @value: Byte being written
1648 * This executes the SMBus "write byte" protocol, returning negative errno
1649 * else zero on success.
1651 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1653 union i2c_smbus_data data;
1654 data.byte = value;
1655 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1656 I2C_SMBUS_WRITE, command,
1657 I2C_SMBUS_BYTE_DATA, &data);
1659 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1662 * i2c_smbus_read_word_data - SMBus "read word" protocol
1663 * @client: Handle to slave device
1664 * @command: Byte interpreted by slave
1666 * This executes the SMBus "read word" protocol, returning negative errno
1667 * else a 16-bit unsigned "word" received from the device.
1669 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1671 union i2c_smbus_data data;
1672 int status;
1674 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1675 I2C_SMBUS_READ, command,
1676 I2C_SMBUS_WORD_DATA, &data);
1677 return (status < 0) ? status : data.word;
1679 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1682 * i2c_smbus_write_word_data - SMBus "write word" protocol
1683 * @client: Handle to slave device
1684 * @command: Byte interpreted by slave
1685 * @value: 16-bit "word" being written
1687 * This executes the SMBus "write word" protocol, returning negative errno
1688 * else zero on success.
1690 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1692 union i2c_smbus_data data;
1693 data.word = value;
1694 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1695 I2C_SMBUS_WRITE, command,
1696 I2C_SMBUS_WORD_DATA, &data);
1698 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1701 * i2c_smbus_process_call - SMBus "process call" protocol
1702 * @client: Handle to slave device
1703 * @command: Byte interpreted by slave
1704 * @value: 16-bit "word" being written
1706 * This executes the SMBus "process call" protocol, returning negative errno
1707 * else a 16-bit unsigned "word" received from the device.
1709 s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1711 union i2c_smbus_data data;
1712 int status;
1713 data.word = value;
1715 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1716 I2C_SMBUS_WRITE, command,
1717 I2C_SMBUS_PROC_CALL, &data);
1718 return (status < 0) ? status : data.word;
1720 EXPORT_SYMBOL(i2c_smbus_process_call);
1723 * i2c_smbus_read_block_data - SMBus "block read" protocol
1724 * @client: Handle to slave device
1725 * @command: Byte interpreted by slave
1726 * @values: Byte array into which data will be read; big enough to hold
1727 * the data returned by the slave. SMBus allows at most 32 bytes.
1729 * This executes the SMBus "block read" protocol, returning negative errno
1730 * else the number of data bytes in the slave's response.
1732 * Note that using this function requires that the client's adapter support
1733 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1734 * support this; its emulation through I2C messaging relies on a specific
1735 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1737 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1738 u8 *values)
1740 union i2c_smbus_data data;
1741 int status;
1743 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1744 I2C_SMBUS_READ, command,
1745 I2C_SMBUS_BLOCK_DATA, &data);
1746 if (status)
1747 return status;
1749 memcpy(values, &data.block[1], data.block[0]);
1750 return data.block[0];
1752 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1755 * i2c_smbus_write_block_data - SMBus "block write" protocol
1756 * @client: Handle to slave device
1757 * @command: Byte interpreted by slave
1758 * @length: Size of data block; SMBus allows at most 32 bytes
1759 * @values: Byte array which will be written.
1761 * This executes the SMBus "block write" protocol, returning negative errno
1762 * else zero on success.
1764 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1765 u8 length, const u8 *values)
1767 union i2c_smbus_data data;
1769 if (length > I2C_SMBUS_BLOCK_MAX)
1770 length = I2C_SMBUS_BLOCK_MAX;
1771 data.block[0] = length;
1772 memcpy(&data.block[1], values, length);
1773 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1774 I2C_SMBUS_WRITE, command,
1775 I2C_SMBUS_BLOCK_DATA, &data);
1777 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1779 /* Returns the number of read bytes */
1780 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1781 u8 length, u8 *values)
1783 union i2c_smbus_data data;
1784 int status;
1786 if (length > I2C_SMBUS_BLOCK_MAX)
1787 length = I2C_SMBUS_BLOCK_MAX;
1788 data.block[0] = length;
1789 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1790 I2C_SMBUS_READ, command,
1791 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1792 if (status < 0)
1793 return status;
1795 memcpy(values, &data.block[1], data.block[0]);
1796 return data.block[0];
1798 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1800 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1801 u8 length, const u8 *values)
1803 union i2c_smbus_data data;
1805 if (length > I2C_SMBUS_BLOCK_MAX)
1806 length = I2C_SMBUS_BLOCK_MAX;
1807 data.block[0] = length;
1808 memcpy(data.block + 1, values, length);
1809 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1810 I2C_SMBUS_WRITE, command,
1811 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1813 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1815 /* Simulate a SMBus command using the i2c protocol
1816 No checking of parameters is done! */
1817 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
1818 unsigned short flags,
1819 char read_write, u8 command, int size,
1820 union i2c_smbus_data *data)
1822 /* So we need to generate a series of msgs. In the case of writing, we
1823 need to use only one message; when reading, we need two. We initialize
1824 most things with sane defaults, to keep the code below somewhat
1825 simpler. */
1826 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1827 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1828 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
1829 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1830 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1832 int i;
1833 u8 partial_pec = 0;
1834 int status;
1836 msgbuf0[0] = command;
1837 switch (size) {
1838 case I2C_SMBUS_QUICK:
1839 msg[0].len = 0;
1840 /* Special case: The read/write field is used as data */
1841 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1842 I2C_M_RD : 0);
1843 num = 1;
1844 break;
1845 case I2C_SMBUS_BYTE:
1846 if (read_write == I2C_SMBUS_READ) {
1847 /* Special case: only a read! */
1848 msg[0].flags = I2C_M_RD | flags;
1849 num = 1;
1851 break;
1852 case I2C_SMBUS_BYTE_DATA:
1853 if (read_write == I2C_SMBUS_READ)
1854 msg[1].len = 1;
1855 else {
1856 msg[0].len = 2;
1857 msgbuf0[1] = data->byte;
1859 break;
1860 case I2C_SMBUS_WORD_DATA:
1861 if (read_write == I2C_SMBUS_READ)
1862 msg[1].len = 2;
1863 else {
1864 msg[0].len = 3;
1865 msgbuf0[1] = data->word & 0xff;
1866 msgbuf0[2] = data->word >> 8;
1868 break;
1869 case I2C_SMBUS_PROC_CALL:
1870 num = 2; /* Special case */
1871 read_write = I2C_SMBUS_READ;
1872 msg[0].len = 3;
1873 msg[1].len = 2;
1874 msgbuf0[1] = data->word & 0xff;
1875 msgbuf0[2] = data->word >> 8;
1876 break;
1877 case I2C_SMBUS_BLOCK_DATA:
1878 if (read_write == I2C_SMBUS_READ) {
1879 msg[1].flags |= I2C_M_RECV_LEN;
1880 msg[1].len = 1; /* block length will be added by
1881 the underlying bus driver */
1882 } else {
1883 msg[0].len = data->block[0] + 2;
1884 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1885 dev_err(&adapter->dev,
1886 "Invalid block write size %d\n",
1887 data->block[0]);
1888 return -EINVAL;
1890 for (i = 1; i < msg[0].len; i++)
1891 msgbuf0[i] = data->block[i-1];
1893 break;
1894 case I2C_SMBUS_BLOCK_PROC_CALL:
1895 num = 2; /* Another special case */
1896 read_write = I2C_SMBUS_READ;
1897 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1898 dev_err(&adapter->dev,
1899 "Invalid block write size %d\n",
1900 data->block[0]);
1901 return -EINVAL;
1903 msg[0].len = data->block[0] + 2;
1904 for (i = 1; i < msg[0].len; i++)
1905 msgbuf0[i] = data->block[i-1];
1906 msg[1].flags |= I2C_M_RECV_LEN;
1907 msg[1].len = 1; /* block length will be added by
1908 the underlying bus driver */
1909 break;
1910 case I2C_SMBUS_I2C_BLOCK_DATA:
1911 if (read_write == I2C_SMBUS_READ) {
1912 msg[1].len = data->block[0];
1913 } else {
1914 msg[0].len = data->block[0] + 1;
1915 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1916 dev_err(&adapter->dev,
1917 "Invalid block write size %d\n",
1918 data->block[0]);
1919 return -EINVAL;
1921 for (i = 1; i <= data->block[0]; i++)
1922 msgbuf0[i] = data->block[i];
1924 break;
1925 default:
1926 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1927 return -EOPNOTSUPP;
1930 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1931 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1932 if (i) {
1933 /* Compute PEC if first message is a write */
1934 if (!(msg[0].flags & I2C_M_RD)) {
1935 if (num == 1) /* Write only */
1936 i2c_smbus_add_pec(&msg[0]);
1937 else /* Write followed by read */
1938 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1940 /* Ask for PEC if last message is a read */
1941 if (msg[num-1].flags & I2C_M_RD)
1942 msg[num-1].len++;
1945 status = i2c_transfer(adapter, msg, num);
1946 if (status < 0)
1947 return status;
1949 /* Check PEC if last message is a read */
1950 if (i && (msg[num-1].flags & I2C_M_RD)) {
1951 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1952 if (status < 0)
1953 return status;
1956 if (read_write == I2C_SMBUS_READ)
1957 switch (size) {
1958 case I2C_SMBUS_BYTE:
1959 data->byte = msgbuf0[0];
1960 break;
1961 case I2C_SMBUS_BYTE_DATA:
1962 data->byte = msgbuf1[0];
1963 break;
1964 case I2C_SMBUS_WORD_DATA:
1965 case I2C_SMBUS_PROC_CALL:
1966 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1967 break;
1968 case I2C_SMBUS_I2C_BLOCK_DATA:
1969 for (i = 0; i < data->block[0]; i++)
1970 data->block[i+1] = msgbuf1[i];
1971 break;
1972 case I2C_SMBUS_BLOCK_DATA:
1973 case I2C_SMBUS_BLOCK_PROC_CALL:
1974 for (i = 0; i < msgbuf1[0] + 1; i++)
1975 data->block[i] = msgbuf1[i];
1976 break;
1978 return 0;
1982 * i2c_smbus_xfer - execute SMBus protocol operations
1983 * @adapter: Handle to I2C bus
1984 * @addr: Address of SMBus slave on that bus
1985 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1986 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1987 * @command: Byte interpreted by slave, for protocols which use such bytes
1988 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1989 * @data: Data to be read or written
1991 * This executes an SMBus protocol operation, and returns a negative
1992 * errno code else zero on success.
1994 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1995 char read_write, u8 command, int protocol,
1996 union i2c_smbus_data *data)
1998 unsigned long orig_jiffies;
1999 int try;
2000 s32 res;
2002 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
2004 if (adapter->algo->smbus_xfer) {
2005 rt_mutex_lock(&adapter->bus_lock);
2007 /* Retry automatically on arbitration loss */
2008 orig_jiffies = jiffies;
2009 for (res = 0, try = 0; try <= adapter->retries; try++) {
2010 res = adapter->algo->smbus_xfer(adapter, addr, flags,
2011 read_write, command,
2012 protocol, data);
2013 if (res != -EAGAIN)
2014 break;
2015 if (time_after(jiffies,
2016 orig_jiffies + adapter->timeout))
2017 break;
2019 rt_mutex_unlock(&adapter->bus_lock);
2020 } else
2021 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
2022 command, protocol, data);
2024 return res;
2026 EXPORT_SYMBOL(i2c_smbus_xfer);
2028 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
2029 MODULE_DESCRIPTION("I2C-Bus main module");
2030 MODULE_LICENSE("GPL");