netfilter: xtables: stackptr should be percpu
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / i2c / i2c-core.c
blobe0f833cca3f1193c24480c4efc446a7568a9a3af
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/completion.h>
34 #include <linux/hardirq.h>
35 #include <linux/irqflags.h>
36 #include <linux/rwsem.h>
37 #include <linux/pm_runtime.h>
38 #include <asm/uaccess.h>
40 #include "i2c-core.h"
43 /* core_lock protects i2c_adapter_idr, and guarantees
44 that device detection, deletion of detected devices, and attach_adapter
45 and detach_adapter calls are serialized */
46 static DEFINE_MUTEX(core_lock);
47 static DEFINE_IDR(i2c_adapter_idr);
49 static struct device_type i2c_client_type;
50 static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
51 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
53 /* ------------------------------------------------------------------------- */
55 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
56 const struct i2c_client *client)
58 while (id->name[0]) {
59 if (strcmp(client->name, id->name) == 0)
60 return id;
61 id++;
63 return NULL;
66 static int i2c_device_match(struct device *dev, struct device_driver *drv)
68 struct i2c_client *client = i2c_verify_client(dev);
69 struct i2c_driver *driver;
71 if (!client)
72 return 0;
74 driver = to_i2c_driver(drv);
75 /* match on an id table if there is one */
76 if (driver->id_table)
77 return i2c_match_id(driver->id_table, client) != NULL;
79 return 0;
82 #ifdef CONFIG_HOTPLUG
84 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
85 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
87 struct i2c_client *client = to_i2c_client(dev);
89 if (add_uevent_var(env, "MODALIAS=%s%s",
90 I2C_MODULE_PREFIX, client->name))
91 return -ENOMEM;
92 dev_dbg(dev, "uevent\n");
93 return 0;
96 #else
97 #define i2c_device_uevent NULL
98 #endif /* CONFIG_HOTPLUG */
100 static int i2c_device_probe(struct device *dev)
102 struct i2c_client *client = i2c_verify_client(dev);
103 struct i2c_driver *driver;
104 int status;
106 if (!client)
107 return 0;
109 driver = to_i2c_driver(dev->driver);
110 if (!driver->probe || !driver->id_table)
111 return -ENODEV;
112 client->driver = driver;
113 if (!device_can_wakeup(&client->dev))
114 device_init_wakeup(&client->dev,
115 client->flags & I2C_CLIENT_WAKE);
116 dev_dbg(dev, "probe\n");
118 status = driver->probe(client, i2c_match_id(driver->id_table, client));
119 if (status) {
120 client->driver = NULL;
121 i2c_set_clientdata(client, NULL);
123 return status;
126 static int i2c_device_remove(struct device *dev)
128 struct i2c_client *client = i2c_verify_client(dev);
129 struct i2c_driver *driver;
130 int status;
132 if (!client || !dev->driver)
133 return 0;
135 driver = to_i2c_driver(dev->driver);
136 if (driver->remove) {
137 dev_dbg(dev, "remove\n");
138 status = driver->remove(client);
139 } else {
140 dev->driver = NULL;
141 status = 0;
143 if (status == 0) {
144 client->driver = NULL;
145 i2c_set_clientdata(client, NULL);
147 return status;
150 static void i2c_device_shutdown(struct device *dev)
152 struct i2c_client *client = i2c_verify_client(dev);
153 struct i2c_driver *driver;
155 if (!client || !dev->driver)
156 return;
157 driver = to_i2c_driver(dev->driver);
158 if (driver->shutdown)
159 driver->shutdown(client);
162 #ifdef CONFIG_PM_SLEEP
163 static int i2c_legacy_suspend(struct device *dev, pm_message_t mesg)
165 struct i2c_client *client = i2c_verify_client(dev);
166 struct i2c_driver *driver;
168 if (!client || !dev->driver)
169 return 0;
170 driver = to_i2c_driver(dev->driver);
171 if (!driver->suspend)
172 return 0;
173 return driver->suspend(client, mesg);
176 static int i2c_legacy_resume(struct device *dev)
178 struct i2c_client *client = i2c_verify_client(dev);
179 struct i2c_driver *driver;
181 if (!client || !dev->driver)
182 return 0;
183 driver = to_i2c_driver(dev->driver);
184 if (!driver->resume)
185 return 0;
186 return driver->resume(client);
189 static int i2c_device_pm_suspend(struct device *dev)
191 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
193 if (pm_runtime_suspended(dev))
194 return 0;
196 if (pm)
197 return pm->suspend ? pm->suspend(dev) : 0;
199 return i2c_legacy_suspend(dev, PMSG_SUSPEND);
202 static int i2c_device_pm_resume(struct device *dev)
204 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
205 int ret;
207 if (pm)
208 ret = pm->resume ? pm->resume(dev) : 0;
209 else
210 ret = i2c_legacy_resume(dev);
212 if (!ret) {
213 pm_runtime_disable(dev);
214 pm_runtime_set_active(dev);
215 pm_runtime_enable(dev);
218 return ret;
221 static int i2c_device_pm_freeze(struct device *dev)
223 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
225 if (pm_runtime_suspended(dev))
226 return 0;
228 if (pm)
229 return pm->freeze ? pm->freeze(dev) : 0;
231 return i2c_legacy_suspend(dev, PMSG_FREEZE);
234 static int i2c_device_pm_thaw(struct device *dev)
236 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
238 if (pm_runtime_suspended(dev))
239 return 0;
241 if (pm)
242 return pm->thaw ? pm->thaw(dev) : 0;
244 return i2c_legacy_resume(dev);
247 static int i2c_device_pm_poweroff(struct device *dev)
249 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
251 if (pm_runtime_suspended(dev))
252 return 0;
254 if (pm)
255 return pm->poweroff ? pm->poweroff(dev) : 0;
257 return i2c_legacy_suspend(dev, PMSG_HIBERNATE);
260 static int i2c_device_pm_restore(struct device *dev)
262 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
263 int ret;
265 if (pm)
266 ret = pm->restore ? pm->restore(dev) : 0;
267 else
268 ret = i2c_legacy_resume(dev);
270 if (!ret) {
271 pm_runtime_disable(dev);
272 pm_runtime_set_active(dev);
273 pm_runtime_enable(dev);
276 return ret;
278 #else /* !CONFIG_PM_SLEEP */
279 #define i2c_device_pm_suspend NULL
280 #define i2c_device_pm_resume NULL
281 #define i2c_device_pm_freeze NULL
282 #define i2c_device_pm_thaw NULL
283 #define i2c_device_pm_poweroff NULL
284 #define i2c_device_pm_restore NULL
285 #endif /* !CONFIG_PM_SLEEP */
287 static void i2c_client_dev_release(struct device *dev)
289 kfree(to_i2c_client(dev));
292 static ssize_t
293 show_name(struct device *dev, struct device_attribute *attr, char *buf)
295 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
296 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
299 static ssize_t
300 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
302 struct i2c_client *client = to_i2c_client(dev);
303 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
306 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
307 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
309 static struct attribute *i2c_dev_attrs[] = {
310 &dev_attr_name.attr,
311 /* modalias helps coldplug: modprobe $(cat .../modalias) */
312 &dev_attr_modalias.attr,
313 NULL
316 static struct attribute_group i2c_dev_attr_group = {
317 .attrs = i2c_dev_attrs,
320 static const struct attribute_group *i2c_dev_attr_groups[] = {
321 &i2c_dev_attr_group,
322 NULL
325 static const struct dev_pm_ops i2c_device_pm_ops = {
326 .suspend = i2c_device_pm_suspend,
327 .resume = i2c_device_pm_resume,
328 .freeze = i2c_device_pm_freeze,
329 .thaw = i2c_device_pm_thaw,
330 .poweroff = i2c_device_pm_poweroff,
331 .restore = i2c_device_pm_restore,
332 SET_RUNTIME_PM_OPS(
333 pm_generic_runtime_suspend,
334 pm_generic_runtime_resume,
335 pm_generic_runtime_idle
339 struct bus_type i2c_bus_type = {
340 .name = "i2c",
341 .match = i2c_device_match,
342 .probe = i2c_device_probe,
343 .remove = i2c_device_remove,
344 .shutdown = i2c_device_shutdown,
345 .pm = &i2c_device_pm_ops,
347 EXPORT_SYMBOL_GPL(i2c_bus_type);
349 static struct device_type i2c_client_type = {
350 .groups = i2c_dev_attr_groups,
351 .uevent = i2c_device_uevent,
352 .release = i2c_client_dev_release,
357 * i2c_verify_client - return parameter as i2c_client, or NULL
358 * @dev: device, probably from some driver model iterator
360 * When traversing the driver model tree, perhaps using driver model
361 * iterators like @device_for_each_child(), you can't assume very much
362 * about the nodes you find. Use this function to avoid oopses caused
363 * by wrongly treating some non-I2C device as an i2c_client.
365 struct i2c_client *i2c_verify_client(struct device *dev)
367 return (dev->type == &i2c_client_type)
368 ? to_i2c_client(dev)
369 : NULL;
371 EXPORT_SYMBOL(i2c_verify_client);
375 * i2c_new_device - instantiate an i2c device
376 * @adap: the adapter managing the device
377 * @info: describes one I2C device; bus_num is ignored
378 * Context: can sleep
380 * Create an i2c device. Binding is handled through driver model
381 * probe()/remove() methods. A driver may be bound to this device when we
382 * return from this function, or any later moment (e.g. maybe hotplugging will
383 * load the driver module). This call is not appropriate for use by mainboard
384 * initialization logic, which usually runs during an arch_initcall() long
385 * before any i2c_adapter could exist.
387 * This returns the new i2c client, which may be saved for later use with
388 * i2c_unregister_device(); or NULL to indicate an error.
390 struct i2c_client *
391 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
393 struct i2c_client *client;
394 int status;
396 client = kzalloc(sizeof *client, GFP_KERNEL);
397 if (!client)
398 return NULL;
400 client->adapter = adap;
402 client->dev.platform_data = info->platform_data;
404 if (info->archdata)
405 client->dev.archdata = *info->archdata;
407 client->flags = info->flags;
408 client->addr = info->addr;
409 client->irq = info->irq;
411 strlcpy(client->name, info->type, sizeof(client->name));
413 /* Check for address business */
414 status = i2c_check_addr(adap, client->addr);
415 if (status)
416 goto out_err;
418 client->dev.parent = &client->adapter->dev;
419 client->dev.bus = &i2c_bus_type;
420 client->dev.type = &i2c_client_type;
421 #ifdef CONFIG_OF
422 client->dev.of_node = info->of_node;
423 #endif
425 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
426 client->addr);
427 status = device_register(&client->dev);
428 if (status)
429 goto out_err;
431 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
432 client->name, dev_name(&client->dev));
434 return client;
436 out_err:
437 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
438 "(%d)\n", client->name, client->addr, status);
439 kfree(client);
440 return NULL;
442 EXPORT_SYMBOL_GPL(i2c_new_device);
446 * i2c_unregister_device - reverse effect of i2c_new_device()
447 * @client: value returned from i2c_new_device()
448 * Context: can sleep
450 void i2c_unregister_device(struct i2c_client *client)
452 device_unregister(&client->dev);
454 EXPORT_SYMBOL_GPL(i2c_unregister_device);
457 static const struct i2c_device_id dummy_id[] = {
458 { "dummy", 0 },
459 { },
462 static int dummy_probe(struct i2c_client *client,
463 const struct i2c_device_id *id)
465 return 0;
468 static int dummy_remove(struct i2c_client *client)
470 return 0;
473 static struct i2c_driver dummy_driver = {
474 .driver.name = "dummy",
475 .probe = dummy_probe,
476 .remove = dummy_remove,
477 .id_table = dummy_id,
481 * i2c_new_dummy - return a new i2c device bound to a dummy driver
482 * @adapter: the adapter managing the device
483 * @address: seven bit address to be used
484 * Context: can sleep
486 * This returns an I2C client bound to the "dummy" driver, intended for use
487 * with devices that consume multiple addresses. Examples of such chips
488 * include various EEPROMS (like 24c04 and 24c08 models).
490 * These dummy devices have two main uses. First, most I2C and SMBus calls
491 * except i2c_transfer() need a client handle; the dummy will be that handle.
492 * And second, this prevents the specified address from being bound to a
493 * different driver.
495 * This returns the new i2c client, which should be saved for later use with
496 * i2c_unregister_device(); or NULL to indicate an error.
498 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
500 struct i2c_board_info info = {
501 I2C_BOARD_INFO("dummy", address),
504 return i2c_new_device(adapter, &info);
506 EXPORT_SYMBOL_GPL(i2c_new_dummy);
508 /* ------------------------------------------------------------------------- */
510 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
512 static void i2c_adapter_dev_release(struct device *dev)
514 struct i2c_adapter *adap = to_i2c_adapter(dev);
515 complete(&adap->dev_released);
519 * Let users instantiate I2C devices through sysfs. This can be used when
520 * platform initialization code doesn't contain the proper data for
521 * whatever reason. Also useful for drivers that do device detection and
522 * detection fails, either because the device uses an unexpected address,
523 * or this is a compatible device with different ID register values.
525 * Parameter checking may look overzealous, but we really don't want
526 * the user to provide incorrect parameters.
528 static ssize_t
529 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
530 const char *buf, size_t count)
532 struct i2c_adapter *adap = to_i2c_adapter(dev);
533 struct i2c_board_info info;
534 struct i2c_client *client;
535 char *blank, end;
536 int res;
538 dev_warn(dev, "The new_device interface is still experimental "
539 "and may change in a near future\n");
540 memset(&info, 0, sizeof(struct i2c_board_info));
542 blank = strchr(buf, ' ');
543 if (!blank) {
544 dev_err(dev, "%s: Missing parameters\n", "new_device");
545 return -EINVAL;
547 if (blank - buf > I2C_NAME_SIZE - 1) {
548 dev_err(dev, "%s: Invalid device name\n", "new_device");
549 return -EINVAL;
551 memcpy(info.type, buf, blank - buf);
553 /* Parse remaining parameters, reject extra parameters */
554 res = sscanf(++blank, "%hi%c", &info.addr, &end);
555 if (res < 1) {
556 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
557 return -EINVAL;
559 if (res > 1 && end != '\n') {
560 dev_err(dev, "%s: Extra parameters\n", "new_device");
561 return -EINVAL;
564 if (info.addr < 0x03 || info.addr > 0x77) {
565 dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
566 info.addr);
567 return -EINVAL;
570 client = i2c_new_device(adap, &info);
571 if (!client)
572 return -EEXIST;
574 /* Keep track of the added device */
575 i2c_lock_adapter(adap);
576 list_add_tail(&client->detected, &adap->userspace_clients);
577 i2c_unlock_adapter(adap);
578 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
579 info.type, info.addr);
581 return count;
585 * And of course let the users delete the devices they instantiated, if
586 * they got it wrong. This interface can only be used to delete devices
587 * instantiated by i2c_sysfs_new_device above. This guarantees that we
588 * don't delete devices to which some kernel code still has references.
590 * Parameter checking may look overzealous, but we really don't want
591 * the user to delete the wrong device.
593 static ssize_t
594 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
595 const char *buf, size_t count)
597 struct i2c_adapter *adap = to_i2c_adapter(dev);
598 struct i2c_client *client, *next;
599 unsigned short addr;
600 char end;
601 int res;
603 /* Parse parameters, reject extra parameters */
604 res = sscanf(buf, "%hi%c", &addr, &end);
605 if (res < 1) {
606 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
607 return -EINVAL;
609 if (res > 1 && end != '\n') {
610 dev_err(dev, "%s: Extra parameters\n", "delete_device");
611 return -EINVAL;
614 /* Make sure the device was added through sysfs */
615 res = -ENOENT;
616 i2c_lock_adapter(adap);
617 list_for_each_entry_safe(client, next, &adap->userspace_clients,
618 detected) {
619 if (client->addr == addr) {
620 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
621 "delete_device", client->name, client->addr);
623 list_del(&client->detected);
624 i2c_unregister_device(client);
625 res = count;
626 break;
629 i2c_unlock_adapter(adap);
631 if (res < 0)
632 dev_err(dev, "%s: Can't find device in list\n",
633 "delete_device");
634 return res;
637 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
638 static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
640 static struct attribute *i2c_adapter_attrs[] = {
641 &dev_attr_name.attr,
642 &dev_attr_new_device.attr,
643 &dev_attr_delete_device.attr,
644 NULL
647 static struct attribute_group i2c_adapter_attr_group = {
648 .attrs = i2c_adapter_attrs,
651 static const struct attribute_group *i2c_adapter_attr_groups[] = {
652 &i2c_adapter_attr_group,
653 NULL
656 static struct device_type i2c_adapter_type = {
657 .groups = i2c_adapter_attr_groups,
658 .release = i2c_adapter_dev_release,
661 #ifdef CONFIG_I2C_COMPAT
662 static struct class_compat *i2c_adapter_compat_class;
663 #endif
665 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
667 struct i2c_devinfo *devinfo;
669 down_read(&__i2c_board_lock);
670 list_for_each_entry(devinfo, &__i2c_board_list, list) {
671 if (devinfo->busnum == adapter->nr
672 && !i2c_new_device(adapter,
673 &devinfo->board_info))
674 dev_err(&adapter->dev,
675 "Can't create device at 0x%02x\n",
676 devinfo->board_info.addr);
678 up_read(&__i2c_board_lock);
681 static int i2c_do_add_adapter(struct i2c_driver *driver,
682 struct i2c_adapter *adap)
684 /* Detect supported devices on that bus, and instantiate them */
685 i2c_detect(adap, driver);
687 /* Let legacy drivers scan this bus for matching devices */
688 if (driver->attach_adapter) {
689 /* We ignore the return code; if it fails, too bad */
690 driver->attach_adapter(adap);
692 return 0;
695 static int __process_new_adapter(struct device_driver *d, void *data)
697 return i2c_do_add_adapter(to_i2c_driver(d), data);
700 static int i2c_register_adapter(struct i2c_adapter *adap)
702 int res = 0, dummy;
704 /* Can't register until after driver model init */
705 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
706 res = -EAGAIN;
707 goto out_list;
710 rt_mutex_init(&adap->bus_lock);
711 INIT_LIST_HEAD(&adap->userspace_clients);
713 /* Set default timeout to 1 second if not already set */
714 if (adap->timeout == 0)
715 adap->timeout = HZ;
717 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
718 adap->dev.bus = &i2c_bus_type;
719 adap->dev.type = &i2c_adapter_type;
720 res = device_register(&adap->dev);
721 if (res)
722 goto out_list;
724 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
726 #ifdef CONFIG_I2C_COMPAT
727 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
728 adap->dev.parent);
729 if (res)
730 dev_warn(&adap->dev,
731 "Failed to create compatibility class link\n");
732 #endif
734 /* create pre-declared device nodes */
735 if (adap->nr < __i2c_first_dynamic_bus_num)
736 i2c_scan_static_board_info(adap);
738 /* Notify drivers */
739 mutex_lock(&core_lock);
740 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
741 __process_new_adapter);
742 mutex_unlock(&core_lock);
744 return 0;
746 out_list:
747 mutex_lock(&core_lock);
748 idr_remove(&i2c_adapter_idr, adap->nr);
749 mutex_unlock(&core_lock);
750 return res;
754 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
755 * @adapter: the adapter to add
756 * Context: can sleep
758 * This routine is used to declare an I2C adapter when its bus number
759 * doesn't matter. Examples: for I2C adapters dynamically added by
760 * USB links or PCI plugin cards.
762 * When this returns zero, a new bus number was allocated and stored
763 * in adap->nr, and the specified adapter became available for clients.
764 * Otherwise, a negative errno value is returned.
766 int i2c_add_adapter(struct i2c_adapter *adapter)
768 int id, res = 0;
770 retry:
771 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
772 return -ENOMEM;
774 mutex_lock(&core_lock);
775 /* "above" here means "above or equal to", sigh */
776 res = idr_get_new_above(&i2c_adapter_idr, adapter,
777 __i2c_first_dynamic_bus_num, &id);
778 mutex_unlock(&core_lock);
780 if (res < 0) {
781 if (res == -EAGAIN)
782 goto retry;
783 return res;
786 adapter->nr = id;
787 return i2c_register_adapter(adapter);
789 EXPORT_SYMBOL(i2c_add_adapter);
792 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
793 * @adap: the adapter to register (with adap->nr initialized)
794 * Context: can sleep
796 * This routine is used to declare an I2C adapter when its bus number
797 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
798 * or otherwise built in to the system's mainboard, and where i2c_board_info
799 * is used to properly configure I2C devices.
801 * If no devices have pre-been declared for this bus, then be sure to
802 * register the adapter before any dynamically allocated ones. Otherwise
803 * the required bus ID may not be available.
805 * When this returns zero, the specified adapter became available for
806 * clients using the bus number provided in adap->nr. Also, the table
807 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
808 * and the appropriate driver model device nodes are created. Otherwise, a
809 * negative errno value is returned.
811 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
813 int id;
814 int status;
816 if (adap->nr & ~MAX_ID_MASK)
817 return -EINVAL;
819 retry:
820 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
821 return -ENOMEM;
823 mutex_lock(&core_lock);
824 /* "above" here means "above or equal to", sigh;
825 * we need the "equal to" result to force the result
827 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
828 if (status == 0 && id != adap->nr) {
829 status = -EBUSY;
830 idr_remove(&i2c_adapter_idr, id);
832 mutex_unlock(&core_lock);
833 if (status == -EAGAIN)
834 goto retry;
836 if (status == 0)
837 status = i2c_register_adapter(adap);
838 return status;
840 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
842 static int i2c_do_del_adapter(struct i2c_driver *driver,
843 struct i2c_adapter *adapter)
845 struct i2c_client *client, *_n;
846 int res;
848 /* Remove the devices we created ourselves as the result of hardware
849 * probing (using a driver's detect method) */
850 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
851 if (client->adapter == adapter) {
852 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
853 client->name, client->addr);
854 list_del(&client->detected);
855 i2c_unregister_device(client);
859 if (!driver->detach_adapter)
860 return 0;
861 res = driver->detach_adapter(adapter);
862 if (res)
863 dev_err(&adapter->dev, "detach_adapter failed (%d) "
864 "for driver [%s]\n", res, driver->driver.name);
865 return res;
868 static int __unregister_client(struct device *dev, void *dummy)
870 struct i2c_client *client = i2c_verify_client(dev);
871 if (client)
872 i2c_unregister_device(client);
873 return 0;
876 static int __process_removed_adapter(struct device_driver *d, void *data)
878 return i2c_do_del_adapter(to_i2c_driver(d), data);
882 * i2c_del_adapter - unregister I2C adapter
883 * @adap: the adapter being unregistered
884 * Context: can sleep
886 * This unregisters an I2C adapter which was previously registered
887 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
889 int i2c_del_adapter(struct i2c_adapter *adap)
891 int res = 0;
892 struct i2c_adapter *found;
893 struct i2c_client *client, *next;
895 /* First make sure that this adapter was ever added */
896 mutex_lock(&core_lock);
897 found = idr_find(&i2c_adapter_idr, adap->nr);
898 mutex_unlock(&core_lock);
899 if (found != adap) {
900 pr_debug("i2c-core: attempting to delete unregistered "
901 "adapter [%s]\n", adap->name);
902 return -EINVAL;
905 /* Tell drivers about this removal */
906 mutex_lock(&core_lock);
907 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
908 __process_removed_adapter);
909 mutex_unlock(&core_lock);
910 if (res)
911 return res;
913 /* Remove devices instantiated from sysfs */
914 i2c_lock_adapter(adap);
915 list_for_each_entry_safe(client, next, &adap->userspace_clients,
916 detected) {
917 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
918 client->addr);
919 list_del(&client->detected);
920 i2c_unregister_device(client);
922 i2c_unlock_adapter(adap);
924 /* Detach any active clients. This can't fail, thus we do not
925 checking the returned value. */
926 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
928 #ifdef CONFIG_I2C_COMPAT
929 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
930 adap->dev.parent);
931 #endif
933 /* device name is gone after device_unregister */
934 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
936 /* clean up the sysfs representation */
937 init_completion(&adap->dev_released);
938 device_unregister(&adap->dev);
940 /* wait for sysfs to drop all references */
941 wait_for_completion(&adap->dev_released);
943 /* free bus id */
944 mutex_lock(&core_lock);
945 idr_remove(&i2c_adapter_idr, adap->nr);
946 mutex_unlock(&core_lock);
948 /* Clear the device structure in case this adapter is ever going to be
949 added again */
950 memset(&adap->dev, 0, sizeof(adap->dev));
952 return 0;
954 EXPORT_SYMBOL(i2c_del_adapter);
957 /* ------------------------------------------------------------------------- */
959 static int __process_new_driver(struct device *dev, void *data)
961 if (dev->type != &i2c_adapter_type)
962 return 0;
963 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
967 * An i2c_driver is used with one or more i2c_client (device) nodes to access
968 * i2c slave chips, on a bus instance associated with some i2c_adapter.
971 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
973 int res;
975 /* Can't register until after driver model init */
976 if (unlikely(WARN_ON(!i2c_bus_type.p)))
977 return -EAGAIN;
979 /* add the driver to the list of i2c drivers in the driver core */
980 driver->driver.owner = owner;
981 driver->driver.bus = &i2c_bus_type;
983 /* When registration returns, the driver core
984 * will have called probe() for all matching-but-unbound devices.
986 res = driver_register(&driver->driver);
987 if (res)
988 return res;
990 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
992 INIT_LIST_HEAD(&driver->clients);
993 /* Walk the adapters that are already present */
994 mutex_lock(&core_lock);
995 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
996 mutex_unlock(&core_lock);
998 return 0;
1000 EXPORT_SYMBOL(i2c_register_driver);
1002 static int __process_removed_driver(struct device *dev, void *data)
1004 if (dev->type != &i2c_adapter_type)
1005 return 0;
1006 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
1010 * i2c_del_driver - unregister I2C driver
1011 * @driver: the driver being unregistered
1012 * Context: can sleep
1014 void i2c_del_driver(struct i2c_driver *driver)
1016 mutex_lock(&core_lock);
1017 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
1018 mutex_unlock(&core_lock);
1020 driver_unregister(&driver->driver);
1021 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
1023 EXPORT_SYMBOL(i2c_del_driver);
1025 /* ------------------------------------------------------------------------- */
1027 static int __i2c_check_addr(struct device *dev, void *addrp)
1029 struct i2c_client *client = i2c_verify_client(dev);
1030 int addr = *(int *)addrp;
1032 if (client && client->addr == addr)
1033 return -EBUSY;
1034 return 0;
1037 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
1039 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
1043 * i2c_use_client - increments the reference count of the i2c client structure
1044 * @client: the client being referenced
1046 * Each live reference to a client should be refcounted. The driver model does
1047 * that automatically as part of driver binding, so that most drivers don't
1048 * need to do this explicitly: they hold a reference until they're unbound
1049 * from the device.
1051 * A pointer to the client with the incremented reference counter is returned.
1053 struct i2c_client *i2c_use_client(struct i2c_client *client)
1055 if (client && get_device(&client->dev))
1056 return client;
1057 return NULL;
1059 EXPORT_SYMBOL(i2c_use_client);
1062 * i2c_release_client - release a use of the i2c client structure
1063 * @client: the client being no longer referenced
1065 * Must be called when a user of a client is finished with it.
1067 void i2c_release_client(struct i2c_client *client)
1069 if (client)
1070 put_device(&client->dev);
1072 EXPORT_SYMBOL(i2c_release_client);
1074 struct i2c_cmd_arg {
1075 unsigned cmd;
1076 void *arg;
1079 static int i2c_cmd(struct device *dev, void *_arg)
1081 struct i2c_client *client = i2c_verify_client(dev);
1082 struct i2c_cmd_arg *arg = _arg;
1084 if (client && client->driver && client->driver->command)
1085 client->driver->command(client, arg->cmd, arg->arg);
1086 return 0;
1089 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1091 struct i2c_cmd_arg cmd_arg;
1093 cmd_arg.cmd = cmd;
1094 cmd_arg.arg = arg;
1095 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1097 EXPORT_SYMBOL(i2c_clients_command);
1099 static int __init i2c_init(void)
1101 int retval;
1103 retval = bus_register(&i2c_bus_type);
1104 if (retval)
1105 return retval;
1106 #ifdef CONFIG_I2C_COMPAT
1107 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1108 if (!i2c_adapter_compat_class) {
1109 retval = -ENOMEM;
1110 goto bus_err;
1112 #endif
1113 retval = i2c_add_driver(&dummy_driver);
1114 if (retval)
1115 goto class_err;
1116 return 0;
1118 class_err:
1119 #ifdef CONFIG_I2C_COMPAT
1120 class_compat_unregister(i2c_adapter_compat_class);
1121 bus_err:
1122 #endif
1123 bus_unregister(&i2c_bus_type);
1124 return retval;
1127 static void __exit i2c_exit(void)
1129 i2c_del_driver(&dummy_driver);
1130 #ifdef CONFIG_I2C_COMPAT
1131 class_compat_unregister(i2c_adapter_compat_class);
1132 #endif
1133 bus_unregister(&i2c_bus_type);
1136 /* We must initialize early, because some subsystems register i2c drivers
1137 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1139 postcore_initcall(i2c_init);
1140 module_exit(i2c_exit);
1142 /* ----------------------------------------------------
1143 * the functional interface to the i2c busses.
1144 * ----------------------------------------------------
1148 * i2c_transfer - execute a single or combined I2C message
1149 * @adap: Handle to I2C bus
1150 * @msgs: One or more messages to execute before STOP is issued to
1151 * terminate the operation; each message begins with a START.
1152 * @num: Number of messages to be executed.
1154 * Returns negative errno, else the number of messages executed.
1156 * Note that there is no requirement that each message be sent to
1157 * the same slave address, although that is the most common model.
1159 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1161 unsigned long orig_jiffies;
1162 int ret, try;
1164 /* REVISIT the fault reporting model here is weak:
1166 * - When we get an error after receiving N bytes from a slave,
1167 * there is no way to report "N".
1169 * - When we get a NAK after transmitting N bytes to a slave,
1170 * there is no way to report "N" ... or to let the master
1171 * continue executing the rest of this combined message, if
1172 * that's the appropriate response.
1174 * - When for example "num" is two and we successfully complete
1175 * the first message but get an error part way through the
1176 * second, it's unclear whether that should be reported as
1177 * one (discarding status on the second message) or errno
1178 * (discarding status on the first one).
1181 if (adap->algo->master_xfer) {
1182 #ifdef DEBUG
1183 for (ret = 0; ret < num; ret++) {
1184 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1185 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1186 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1187 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1189 #endif
1191 if (in_atomic() || irqs_disabled()) {
1192 ret = rt_mutex_trylock(&adap->bus_lock);
1193 if (!ret)
1194 /* I2C activity is ongoing. */
1195 return -EAGAIN;
1196 } else {
1197 rt_mutex_lock(&adap->bus_lock);
1200 /* Retry automatically on arbitration loss */
1201 orig_jiffies = jiffies;
1202 for (ret = 0, try = 0; try <= adap->retries; try++) {
1203 ret = adap->algo->master_xfer(adap, msgs, num);
1204 if (ret != -EAGAIN)
1205 break;
1206 if (time_after(jiffies, orig_jiffies + adap->timeout))
1207 break;
1209 rt_mutex_unlock(&adap->bus_lock);
1211 return ret;
1212 } else {
1213 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1214 return -EOPNOTSUPP;
1217 EXPORT_SYMBOL(i2c_transfer);
1220 * i2c_master_send - issue a single I2C message in master transmit mode
1221 * @client: Handle to slave device
1222 * @buf: Data that will be written to the slave
1223 * @count: How many bytes to write, must be less than 64k since msg.len is u16
1225 * Returns negative errno, or else the number of bytes written.
1227 int i2c_master_send(struct i2c_client *client, const char *buf, int count)
1229 int ret;
1230 struct i2c_adapter *adap = client->adapter;
1231 struct i2c_msg msg;
1233 msg.addr = client->addr;
1234 msg.flags = client->flags & I2C_M_TEN;
1235 msg.len = count;
1236 msg.buf = (char *)buf;
1238 ret = i2c_transfer(adap, &msg, 1);
1240 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1241 transmitted, else error code. */
1242 return (ret == 1) ? count : ret;
1244 EXPORT_SYMBOL(i2c_master_send);
1247 * i2c_master_recv - issue a single I2C message in master receive mode
1248 * @client: Handle to slave device
1249 * @buf: Where to store data read from slave
1250 * @count: How many bytes to read, must be less than 64k since msg.len is u16
1252 * Returns negative errno, or else the number of bytes read.
1254 int i2c_master_recv(struct i2c_client *client, char *buf, int count)
1256 struct i2c_adapter *adap = client->adapter;
1257 struct i2c_msg msg;
1258 int ret;
1260 msg.addr = client->addr;
1261 msg.flags = client->flags & I2C_M_TEN;
1262 msg.flags |= I2C_M_RD;
1263 msg.len = count;
1264 msg.buf = buf;
1266 ret = i2c_transfer(adap, &msg, 1);
1268 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1269 transmitted, else error code. */
1270 return (ret == 1) ? count : ret;
1272 EXPORT_SYMBOL(i2c_master_recv);
1274 /* ----------------------------------------------------
1275 * the i2c address scanning function
1276 * Will not work for 10-bit addresses!
1277 * ----------------------------------------------------
1280 static int i2c_detect_address(struct i2c_client *temp_client,
1281 struct i2c_driver *driver)
1283 struct i2c_board_info info;
1284 struct i2c_adapter *adapter = temp_client->adapter;
1285 int addr = temp_client->addr;
1286 int err;
1288 /* Make sure the address is valid */
1289 if (addr < 0x03 || addr > 0x77) {
1290 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1291 addr);
1292 return -EINVAL;
1295 /* Skip if already in use */
1296 if (i2c_check_addr(adapter, addr))
1297 return 0;
1299 /* Make sure there is something at this address */
1300 if (addr == 0x73 && (adapter->class & I2C_CLASS_HWMON)) {
1301 /* Special probe for FSC hwmon chips */
1302 union i2c_smbus_data dummy;
1304 if (i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_READ, 0,
1305 I2C_SMBUS_BYTE_DATA, &dummy) < 0)
1306 return 0;
1307 } else {
1308 if (i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_WRITE, 0,
1309 I2C_SMBUS_QUICK, NULL) < 0)
1310 return 0;
1312 /* Prevent 24RF08 corruption */
1313 if ((addr & ~0x0f) == 0x50)
1314 i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_WRITE, 0,
1315 I2C_SMBUS_QUICK, NULL);
1318 /* Finally call the custom detection function */
1319 memset(&info, 0, sizeof(struct i2c_board_info));
1320 info.addr = addr;
1321 err = driver->detect(temp_client, &info);
1322 if (err) {
1323 /* -ENODEV is returned if the detection fails. We catch it
1324 here as this isn't an error. */
1325 return err == -ENODEV ? 0 : err;
1328 /* Consistency check */
1329 if (info.type[0] == '\0') {
1330 dev_err(&adapter->dev, "%s detection function provided "
1331 "no name for 0x%x\n", driver->driver.name,
1332 addr);
1333 } else {
1334 struct i2c_client *client;
1336 /* Detection succeeded, instantiate the device */
1337 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1338 info.type, info.addr);
1339 client = i2c_new_device(adapter, &info);
1340 if (client)
1341 list_add_tail(&client->detected, &driver->clients);
1342 else
1343 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1344 info.type, info.addr);
1346 return 0;
1349 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1351 const unsigned short *address_list;
1352 struct i2c_client *temp_client;
1353 int i, err = 0;
1354 int adap_id = i2c_adapter_id(adapter);
1356 address_list = driver->address_list;
1357 if (!driver->detect || !address_list)
1358 return 0;
1360 /* Set up a temporary client to help detect callback */
1361 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1362 if (!temp_client)
1363 return -ENOMEM;
1364 temp_client->adapter = adapter;
1366 /* Stop here if the classes do not match */
1367 if (!(adapter->class & driver->class))
1368 goto exit_free;
1370 /* Stop here if we can't use SMBUS_QUICK */
1371 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1372 if (address_list[0] == I2C_CLIENT_END)
1373 goto exit_free;
1375 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1376 "can't probe for chips\n");
1377 err = -EOPNOTSUPP;
1378 goto exit_free;
1381 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
1382 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1383 "addr 0x%02x\n", adap_id, address_list[i]);
1384 temp_client->addr = address_list[i];
1385 err = i2c_detect_address(temp_client, driver);
1386 if (err)
1387 goto exit_free;
1390 exit_free:
1391 kfree(temp_client);
1392 return err;
1395 struct i2c_client *
1396 i2c_new_probed_device(struct i2c_adapter *adap,
1397 struct i2c_board_info *info,
1398 unsigned short const *addr_list)
1400 int i;
1402 /* Stop here if the bus doesn't support probing */
1403 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1404 dev_err(&adap->dev, "Probing not supported\n");
1405 return NULL;
1408 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1409 /* Check address validity */
1410 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1411 dev_warn(&adap->dev, "Invalid 7-bit address "
1412 "0x%02x\n", addr_list[i]);
1413 continue;
1416 /* Check address availability */
1417 if (i2c_check_addr(adap, addr_list[i])) {
1418 dev_dbg(&adap->dev, "Address 0x%02x already in "
1419 "use, not probing\n", addr_list[i]);
1420 continue;
1423 /* Test address responsiveness
1424 The default probe method is a quick write, but it is known
1425 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1426 and could also irreversibly write-protect some EEPROMs, so
1427 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1428 read instead. Also, some bus drivers don't implement
1429 quick write, so we fallback to a byte read it that case
1430 too. */
1431 if ((addr_list[i] & ~0x07) == 0x30
1432 || (addr_list[i] & ~0x0f) == 0x50
1433 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1434 union i2c_smbus_data data;
1436 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1437 I2C_SMBUS_READ, 0,
1438 I2C_SMBUS_BYTE, &data) >= 0)
1439 break;
1440 } else {
1441 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1442 I2C_SMBUS_WRITE, 0,
1443 I2C_SMBUS_QUICK, NULL) >= 0)
1444 break;
1448 if (addr_list[i] == I2C_CLIENT_END) {
1449 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1450 return NULL;
1453 info->addr = addr_list[i];
1454 return i2c_new_device(adap, info);
1456 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1458 struct i2c_adapter *i2c_get_adapter(int id)
1460 struct i2c_adapter *adapter;
1462 mutex_lock(&core_lock);
1463 adapter = idr_find(&i2c_adapter_idr, id);
1464 if (adapter && !try_module_get(adapter->owner))
1465 adapter = NULL;
1467 mutex_unlock(&core_lock);
1468 return adapter;
1470 EXPORT_SYMBOL(i2c_get_adapter);
1472 void i2c_put_adapter(struct i2c_adapter *adap)
1474 module_put(adap->owner);
1476 EXPORT_SYMBOL(i2c_put_adapter);
1478 /* The SMBus parts */
1480 #define POLY (0x1070U << 3)
1481 static u8 crc8(u16 data)
1483 int i;
1485 for (i = 0; i < 8; i++) {
1486 if (data & 0x8000)
1487 data = data ^ POLY;
1488 data = data << 1;
1490 return (u8)(data >> 8);
1493 /* Incremental CRC8 over count bytes in the array pointed to by p */
1494 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1496 int i;
1498 for (i = 0; i < count; i++)
1499 crc = crc8((crc ^ p[i]) << 8);
1500 return crc;
1503 /* Assume a 7-bit address, which is reasonable for SMBus */
1504 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1506 /* The address will be sent first */
1507 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1508 pec = i2c_smbus_pec(pec, &addr, 1);
1510 /* The data buffer follows */
1511 return i2c_smbus_pec(pec, msg->buf, msg->len);
1514 /* Used for write only transactions */
1515 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1517 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1518 msg->len++;
1521 /* Return <0 on CRC error
1522 If there was a write before this read (most cases) we need to take the
1523 partial CRC from the write part into account.
1524 Note that this function does modify the message (we need to decrease the
1525 message length to hide the CRC byte from the caller). */
1526 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1528 u8 rpec = msg->buf[--msg->len];
1529 cpec = i2c_smbus_msg_pec(cpec, msg);
1531 if (rpec != cpec) {
1532 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1533 rpec, cpec);
1534 return -EBADMSG;
1536 return 0;
1540 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1541 * @client: Handle to slave device
1543 * This executes the SMBus "receive byte" protocol, returning negative errno
1544 * else the byte received from the device.
1546 s32 i2c_smbus_read_byte(struct i2c_client *client)
1548 union i2c_smbus_data data;
1549 int status;
1551 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1552 I2C_SMBUS_READ, 0,
1553 I2C_SMBUS_BYTE, &data);
1554 return (status < 0) ? status : data.byte;
1556 EXPORT_SYMBOL(i2c_smbus_read_byte);
1559 * i2c_smbus_write_byte - SMBus "send byte" protocol
1560 * @client: Handle to slave device
1561 * @value: Byte to be sent
1563 * This executes the SMBus "send byte" protocol, returning negative errno
1564 * else zero on success.
1566 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1568 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1569 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1571 EXPORT_SYMBOL(i2c_smbus_write_byte);
1574 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1575 * @client: Handle to slave device
1576 * @command: Byte interpreted by slave
1578 * This executes the SMBus "read byte" protocol, returning negative errno
1579 * else a data byte received from the device.
1581 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1583 union i2c_smbus_data data;
1584 int status;
1586 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1587 I2C_SMBUS_READ, command,
1588 I2C_SMBUS_BYTE_DATA, &data);
1589 return (status < 0) ? status : data.byte;
1591 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1594 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1595 * @client: Handle to slave device
1596 * @command: Byte interpreted by slave
1597 * @value: Byte being written
1599 * This executes the SMBus "write byte" protocol, returning negative errno
1600 * else zero on success.
1602 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1604 union i2c_smbus_data data;
1605 data.byte = value;
1606 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1607 I2C_SMBUS_WRITE, command,
1608 I2C_SMBUS_BYTE_DATA, &data);
1610 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1613 * i2c_smbus_read_word_data - SMBus "read word" protocol
1614 * @client: Handle to slave device
1615 * @command: Byte interpreted by slave
1617 * This executes the SMBus "read word" protocol, returning negative errno
1618 * else a 16-bit unsigned "word" received from the device.
1620 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1622 union i2c_smbus_data data;
1623 int status;
1625 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1626 I2C_SMBUS_READ, command,
1627 I2C_SMBUS_WORD_DATA, &data);
1628 return (status < 0) ? status : data.word;
1630 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1633 * i2c_smbus_write_word_data - SMBus "write word" protocol
1634 * @client: Handle to slave device
1635 * @command: Byte interpreted by slave
1636 * @value: 16-bit "word" being written
1638 * This executes the SMBus "write word" protocol, returning negative errno
1639 * else zero on success.
1641 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1643 union i2c_smbus_data data;
1644 data.word = value;
1645 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1646 I2C_SMBUS_WRITE, command,
1647 I2C_SMBUS_WORD_DATA, &data);
1649 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1652 * i2c_smbus_process_call - SMBus "process call" protocol
1653 * @client: Handle to slave device
1654 * @command: Byte interpreted by slave
1655 * @value: 16-bit "word" being written
1657 * This executes the SMBus "process call" protocol, returning negative errno
1658 * else a 16-bit unsigned "word" received from the device.
1660 s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1662 union i2c_smbus_data data;
1663 int status;
1664 data.word = value;
1666 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1667 I2C_SMBUS_WRITE, command,
1668 I2C_SMBUS_PROC_CALL, &data);
1669 return (status < 0) ? status : data.word;
1671 EXPORT_SYMBOL(i2c_smbus_process_call);
1674 * i2c_smbus_read_block_data - SMBus "block read" protocol
1675 * @client: Handle to slave device
1676 * @command: Byte interpreted by slave
1677 * @values: Byte array into which data will be read; big enough to hold
1678 * the data returned by the slave. SMBus allows at most 32 bytes.
1680 * This executes the SMBus "block read" protocol, returning negative errno
1681 * else the number of data bytes in the slave's response.
1683 * Note that using this function requires that the client's adapter support
1684 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1685 * support this; its emulation through I2C messaging relies on a specific
1686 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1688 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1689 u8 *values)
1691 union i2c_smbus_data data;
1692 int status;
1694 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1695 I2C_SMBUS_READ, command,
1696 I2C_SMBUS_BLOCK_DATA, &data);
1697 if (status)
1698 return status;
1700 memcpy(values, &data.block[1], data.block[0]);
1701 return data.block[0];
1703 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1706 * i2c_smbus_write_block_data - SMBus "block write" protocol
1707 * @client: Handle to slave device
1708 * @command: Byte interpreted by slave
1709 * @length: Size of data block; SMBus allows at most 32 bytes
1710 * @values: Byte array which will be written.
1712 * This executes the SMBus "block write" protocol, returning negative errno
1713 * else zero on success.
1715 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1716 u8 length, const u8 *values)
1718 union i2c_smbus_data data;
1720 if (length > I2C_SMBUS_BLOCK_MAX)
1721 length = I2C_SMBUS_BLOCK_MAX;
1722 data.block[0] = length;
1723 memcpy(&data.block[1], values, length);
1724 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1725 I2C_SMBUS_WRITE, command,
1726 I2C_SMBUS_BLOCK_DATA, &data);
1728 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1730 /* Returns the number of read bytes */
1731 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1732 u8 length, u8 *values)
1734 union i2c_smbus_data data;
1735 int status;
1737 if (length > I2C_SMBUS_BLOCK_MAX)
1738 length = I2C_SMBUS_BLOCK_MAX;
1739 data.block[0] = length;
1740 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1741 I2C_SMBUS_READ, command,
1742 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1743 if (status < 0)
1744 return status;
1746 memcpy(values, &data.block[1], data.block[0]);
1747 return data.block[0];
1749 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1751 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1752 u8 length, const u8 *values)
1754 union i2c_smbus_data data;
1756 if (length > I2C_SMBUS_BLOCK_MAX)
1757 length = I2C_SMBUS_BLOCK_MAX;
1758 data.block[0] = length;
1759 memcpy(data.block + 1, values, length);
1760 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1761 I2C_SMBUS_WRITE, command,
1762 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1764 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1766 /* Simulate a SMBus command using the i2c protocol
1767 No checking of parameters is done! */
1768 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter *adapter, u16 addr,
1769 unsigned short flags,
1770 char read_write, u8 command, int size,
1771 union i2c_smbus_data *data)
1773 /* So we need to generate a series of msgs. In the case of writing, we
1774 need to use only one message; when reading, we need two. We initialize
1775 most things with sane defaults, to keep the code below somewhat
1776 simpler. */
1777 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1778 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1779 int num = read_write == I2C_SMBUS_READ ? 2 : 1;
1780 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1781 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1783 int i;
1784 u8 partial_pec = 0;
1785 int status;
1787 msgbuf0[0] = command;
1788 switch (size) {
1789 case I2C_SMBUS_QUICK:
1790 msg[0].len = 0;
1791 /* Special case: The read/write field is used as data */
1792 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1793 I2C_M_RD : 0);
1794 num = 1;
1795 break;
1796 case I2C_SMBUS_BYTE:
1797 if (read_write == I2C_SMBUS_READ) {
1798 /* Special case: only a read! */
1799 msg[0].flags = I2C_M_RD | flags;
1800 num = 1;
1802 break;
1803 case I2C_SMBUS_BYTE_DATA:
1804 if (read_write == I2C_SMBUS_READ)
1805 msg[1].len = 1;
1806 else {
1807 msg[0].len = 2;
1808 msgbuf0[1] = data->byte;
1810 break;
1811 case I2C_SMBUS_WORD_DATA:
1812 if (read_write == I2C_SMBUS_READ)
1813 msg[1].len = 2;
1814 else {
1815 msg[0].len = 3;
1816 msgbuf0[1] = data->word & 0xff;
1817 msgbuf0[2] = data->word >> 8;
1819 break;
1820 case I2C_SMBUS_PROC_CALL:
1821 num = 2; /* Special case */
1822 read_write = I2C_SMBUS_READ;
1823 msg[0].len = 3;
1824 msg[1].len = 2;
1825 msgbuf0[1] = data->word & 0xff;
1826 msgbuf0[2] = data->word >> 8;
1827 break;
1828 case I2C_SMBUS_BLOCK_DATA:
1829 if (read_write == I2C_SMBUS_READ) {
1830 msg[1].flags |= I2C_M_RECV_LEN;
1831 msg[1].len = 1; /* block length will be added by
1832 the underlying bus driver */
1833 } else {
1834 msg[0].len = data->block[0] + 2;
1835 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1836 dev_err(&adapter->dev,
1837 "Invalid block write size %d\n",
1838 data->block[0]);
1839 return -EINVAL;
1841 for (i = 1; i < msg[0].len; i++)
1842 msgbuf0[i] = data->block[i-1];
1844 break;
1845 case I2C_SMBUS_BLOCK_PROC_CALL:
1846 num = 2; /* Another special case */
1847 read_write = I2C_SMBUS_READ;
1848 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1849 dev_err(&adapter->dev,
1850 "Invalid block write size %d\n",
1851 data->block[0]);
1852 return -EINVAL;
1854 msg[0].len = data->block[0] + 2;
1855 for (i = 1; i < msg[0].len; i++)
1856 msgbuf0[i] = data->block[i-1];
1857 msg[1].flags |= I2C_M_RECV_LEN;
1858 msg[1].len = 1; /* block length will be added by
1859 the underlying bus driver */
1860 break;
1861 case I2C_SMBUS_I2C_BLOCK_DATA:
1862 if (read_write == I2C_SMBUS_READ) {
1863 msg[1].len = data->block[0];
1864 } else {
1865 msg[0].len = data->block[0] + 1;
1866 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1867 dev_err(&adapter->dev,
1868 "Invalid block write size %d\n",
1869 data->block[0]);
1870 return -EINVAL;
1872 for (i = 1; i <= data->block[0]; i++)
1873 msgbuf0[i] = data->block[i];
1875 break;
1876 default:
1877 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1878 return -EOPNOTSUPP;
1881 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1882 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1883 if (i) {
1884 /* Compute PEC if first message is a write */
1885 if (!(msg[0].flags & I2C_M_RD)) {
1886 if (num == 1) /* Write only */
1887 i2c_smbus_add_pec(&msg[0]);
1888 else /* Write followed by read */
1889 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1891 /* Ask for PEC if last message is a read */
1892 if (msg[num-1].flags & I2C_M_RD)
1893 msg[num-1].len++;
1896 status = i2c_transfer(adapter, msg, num);
1897 if (status < 0)
1898 return status;
1900 /* Check PEC if last message is a read */
1901 if (i && (msg[num-1].flags & I2C_M_RD)) {
1902 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1903 if (status < 0)
1904 return status;
1907 if (read_write == I2C_SMBUS_READ)
1908 switch (size) {
1909 case I2C_SMBUS_BYTE:
1910 data->byte = msgbuf0[0];
1911 break;
1912 case I2C_SMBUS_BYTE_DATA:
1913 data->byte = msgbuf1[0];
1914 break;
1915 case I2C_SMBUS_WORD_DATA:
1916 case I2C_SMBUS_PROC_CALL:
1917 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1918 break;
1919 case I2C_SMBUS_I2C_BLOCK_DATA:
1920 for (i = 0; i < data->block[0]; i++)
1921 data->block[i+1] = msgbuf1[i];
1922 break;
1923 case I2C_SMBUS_BLOCK_DATA:
1924 case I2C_SMBUS_BLOCK_PROC_CALL:
1925 for (i = 0; i < msgbuf1[0] + 1; i++)
1926 data->block[i] = msgbuf1[i];
1927 break;
1929 return 0;
1933 * i2c_smbus_xfer - execute SMBus protocol operations
1934 * @adapter: Handle to I2C bus
1935 * @addr: Address of SMBus slave on that bus
1936 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1937 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1938 * @command: Byte interpreted by slave, for protocols which use such bytes
1939 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1940 * @data: Data to be read or written
1942 * This executes an SMBus protocol operation, and returns a negative
1943 * errno code else zero on success.
1945 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1946 char read_write, u8 command, int protocol,
1947 union i2c_smbus_data *data)
1949 unsigned long orig_jiffies;
1950 int try;
1951 s32 res;
1953 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1955 if (adapter->algo->smbus_xfer) {
1956 rt_mutex_lock(&adapter->bus_lock);
1958 /* Retry automatically on arbitration loss */
1959 orig_jiffies = jiffies;
1960 for (res = 0, try = 0; try <= adapter->retries; try++) {
1961 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1962 read_write, command,
1963 protocol, data);
1964 if (res != -EAGAIN)
1965 break;
1966 if (time_after(jiffies,
1967 orig_jiffies + adapter->timeout))
1968 break;
1970 rt_mutex_unlock(&adapter->bus_lock);
1971 } else
1972 res = i2c_smbus_xfer_emulated(adapter, addr, flags, read_write,
1973 command, protocol, data);
1975 return res;
1977 EXPORT_SYMBOL(i2c_smbus_xfer);
1979 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1980 MODULE_DESCRIPTION("I2C-Bus main module");
1981 MODULE_LICENSE("GPL");