Merge commit 'refs/merge-requests/1' of git://gitorious.org/linux-on-wince-htc/linux_...
[htc-linux.git] / drivers / i2c / i2c-core.c
blob8066db706e6cffdc6b9131dc08899c6a836a06b4
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 <asm/uaccess.h>
39 #include "i2c-core.h"
42 /* core_lock protects i2c_adapter_idr, userspace_devices, and guarantees
43 that device detection, deletion of detected devices, and attach_adapter
44 and detach_adapter calls are serialized */
45 static DEFINE_MUTEX(core_lock);
46 static DEFINE_IDR(i2c_adapter_idr);
47 static LIST_HEAD(userspace_devices);
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 return status;
124 static int i2c_device_remove(struct device *dev)
126 struct i2c_client *client = i2c_verify_client(dev);
127 struct i2c_driver *driver;
128 int status;
130 if (!client || !dev->driver)
131 return 0;
133 driver = to_i2c_driver(dev->driver);
134 if (driver->remove) {
135 dev_dbg(dev, "remove\n");
136 status = driver->remove(client);
137 } else {
138 dev->driver = NULL;
139 status = 0;
141 if (status == 0)
142 client->driver = NULL;
143 return status;
146 static void i2c_device_shutdown(struct device *dev)
148 struct i2c_client *client = i2c_verify_client(dev);
149 struct i2c_driver *driver;
151 if (!client || !dev->driver)
152 return;
153 driver = to_i2c_driver(dev->driver);
154 if (driver->shutdown)
155 driver->shutdown(client);
158 static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
160 struct i2c_client *client = i2c_verify_client(dev);
161 struct i2c_driver *driver;
163 if (!client || !dev->driver)
164 return 0;
165 driver = to_i2c_driver(dev->driver);
166 if (!driver->suspend)
167 return 0;
168 return driver->suspend(client, mesg);
171 static int i2c_device_resume(struct device *dev)
173 struct i2c_client *client = i2c_verify_client(dev);
174 struct i2c_driver *driver;
176 if (!client || !dev->driver)
177 return 0;
178 driver = to_i2c_driver(dev->driver);
179 if (!driver->resume)
180 return 0;
181 return driver->resume(client);
184 static void i2c_client_dev_release(struct device *dev)
186 kfree(to_i2c_client(dev));
189 static ssize_t
190 show_name(struct device *dev, struct device_attribute *attr, char *buf)
192 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
193 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
196 static ssize_t
197 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
199 struct i2c_client *client = to_i2c_client(dev);
200 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
203 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
204 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
206 static struct attribute *i2c_dev_attrs[] = {
207 &dev_attr_name.attr,
208 /* modalias helps coldplug: modprobe $(cat .../modalias) */
209 &dev_attr_modalias.attr,
210 NULL
213 static struct attribute_group i2c_dev_attr_group = {
214 .attrs = i2c_dev_attrs,
217 static const struct attribute_group *i2c_dev_attr_groups[] = {
218 &i2c_dev_attr_group,
219 NULL
222 struct bus_type i2c_bus_type = {
223 .name = "i2c",
224 .match = i2c_device_match,
225 .probe = i2c_device_probe,
226 .remove = i2c_device_remove,
227 .shutdown = i2c_device_shutdown,
228 .suspend = i2c_device_suspend,
229 .resume = i2c_device_resume,
231 EXPORT_SYMBOL_GPL(i2c_bus_type);
233 static struct device_type i2c_client_type = {
234 .groups = i2c_dev_attr_groups,
235 .uevent = i2c_device_uevent,
236 .release = i2c_client_dev_release,
241 * i2c_verify_client - return parameter as i2c_client, or NULL
242 * @dev: device, probably from some driver model iterator
244 * When traversing the driver model tree, perhaps using driver model
245 * iterators like @device_for_each_child(), you can't assume very much
246 * about the nodes you find. Use this function to avoid oopses caused
247 * by wrongly treating some non-I2C device as an i2c_client.
249 struct i2c_client *i2c_verify_client(struct device *dev)
251 return (dev->type == &i2c_client_type)
252 ? to_i2c_client(dev)
253 : NULL;
255 EXPORT_SYMBOL(i2c_verify_client);
259 * i2c_new_device - instantiate an i2c device
260 * @adap: the adapter managing the device
261 * @info: describes one I2C device; bus_num is ignored
262 * Context: can sleep
264 * Create an i2c device. Binding is handled through driver model
265 * probe()/remove() methods. A driver may be bound to this device when we
266 * return from this function, or any later moment (e.g. maybe hotplugging will
267 * load the driver module). This call is not appropriate for use by mainboard
268 * initialization logic, which usually runs during an arch_initcall() long
269 * before any i2c_adapter could exist.
271 * This returns the new i2c client, which may be saved for later use with
272 * i2c_unregister_device(); or NULL to indicate an error.
274 struct i2c_client *
275 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
277 struct i2c_client *client;
278 int status;
280 client = kzalloc(sizeof *client, GFP_KERNEL);
281 if (!client)
282 return NULL;
284 client->adapter = adap;
286 client->dev.platform_data = info->platform_data;
288 if (info->archdata)
289 client->dev.archdata = *info->archdata;
291 client->flags = info->flags;
292 client->addr = info->addr;
293 client->irq = info->irq;
295 strlcpy(client->name, info->type, sizeof(client->name));
297 /* Check for address business */
298 status = i2c_check_addr(adap, client->addr);
299 if (status)
300 goto out_err;
302 client->dev.parent = &client->adapter->dev;
303 client->dev.bus = &i2c_bus_type;
304 client->dev.type = &i2c_client_type;
306 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
307 client->addr);
308 status = device_register(&client->dev);
309 if (status)
310 goto out_err;
312 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
313 client->name, dev_name(&client->dev));
315 return client;
317 out_err:
318 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
319 "(%d)\n", client->name, client->addr, status);
320 kfree(client);
321 return NULL;
323 EXPORT_SYMBOL_GPL(i2c_new_device);
327 * i2c_unregister_device - reverse effect of i2c_new_device()
328 * @client: value returned from i2c_new_device()
329 * Context: can sleep
331 void i2c_unregister_device(struct i2c_client *client)
333 device_unregister(&client->dev);
335 EXPORT_SYMBOL_GPL(i2c_unregister_device);
338 static const struct i2c_device_id dummy_id[] = {
339 { "dummy", 0 },
340 { },
343 static int dummy_probe(struct i2c_client *client,
344 const struct i2c_device_id *id)
346 return 0;
349 static int dummy_remove(struct i2c_client *client)
351 return 0;
354 static struct i2c_driver dummy_driver = {
355 .driver.name = "dummy",
356 .probe = dummy_probe,
357 .remove = dummy_remove,
358 .id_table = dummy_id,
362 * i2c_new_dummy - return a new i2c device bound to a dummy driver
363 * @adapter: the adapter managing the device
364 * @address: seven bit address to be used
365 * Context: can sleep
367 * This returns an I2C client bound to the "dummy" driver, intended for use
368 * with devices that consume multiple addresses. Examples of such chips
369 * include various EEPROMS (like 24c04 and 24c08 models).
371 * These dummy devices have two main uses. First, most I2C and SMBus calls
372 * except i2c_transfer() need a client handle; the dummy will be that handle.
373 * And second, this prevents the specified address from being bound to a
374 * different driver.
376 * This returns the new i2c client, which should be saved for later use with
377 * i2c_unregister_device(); or NULL to indicate an error.
379 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
381 struct i2c_board_info info = {
382 I2C_BOARD_INFO("dummy", address),
385 return i2c_new_device(adapter, &info);
387 EXPORT_SYMBOL_GPL(i2c_new_dummy);
389 /* ------------------------------------------------------------------------- */
391 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
393 static void i2c_adapter_dev_release(struct device *dev)
395 struct i2c_adapter *adap = to_i2c_adapter(dev);
396 complete(&adap->dev_released);
400 * Let users instantiate I2C devices through sysfs. This can be used when
401 * platform initialization code doesn't contain the proper data for
402 * whatever reason. Also useful for drivers that do device detection and
403 * detection fails, either because the device uses an unexpected address,
404 * or this is a compatible device with different ID register values.
406 * Parameter checking may look overzealous, but we really don't want
407 * the user to provide incorrect parameters.
409 static ssize_t
410 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
411 const char *buf, size_t count)
413 struct i2c_adapter *adap = to_i2c_adapter(dev);
414 struct i2c_board_info info;
415 struct i2c_client *client;
416 char *blank, end;
417 int res;
419 dev_warn(dev, "The new_device interface is still experimental "
420 "and may change in a near future\n");
421 memset(&info, 0, sizeof(struct i2c_board_info));
423 blank = strchr(buf, ' ');
424 if (!blank) {
425 dev_err(dev, "%s: Missing parameters\n", "new_device");
426 return -EINVAL;
428 if (blank - buf > I2C_NAME_SIZE - 1) {
429 dev_err(dev, "%s: Invalid device name\n", "new_device");
430 return -EINVAL;
432 memcpy(info.type, buf, blank - buf);
434 /* Parse remaining parameters, reject extra parameters */
435 res = sscanf(++blank, "%hi%c", &info.addr, &end);
436 if (res < 1) {
437 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
438 return -EINVAL;
440 if (res > 1 && end != '\n') {
441 dev_err(dev, "%s: Extra parameters\n", "new_device");
442 return -EINVAL;
445 if (info.addr < 0x03 || info.addr > 0x77) {
446 dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
447 info.addr);
448 return -EINVAL;
451 client = i2c_new_device(adap, &info);
452 if (!client)
453 return -EEXIST;
455 /* Keep track of the added device */
456 mutex_lock(&core_lock);
457 list_add_tail(&client->detected, &userspace_devices);
458 mutex_unlock(&core_lock);
459 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
460 info.type, info.addr);
462 return count;
466 * And of course let the users delete the devices they instantiated, if
467 * they got it wrong. This interface can only be used to delete devices
468 * instantiated by i2c_sysfs_new_device above. This guarantees that we
469 * don't delete devices to which some kernel code still has references.
471 * Parameter checking may look overzealous, but we really don't want
472 * the user to delete the wrong device.
474 static ssize_t
475 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
476 const char *buf, size_t count)
478 struct i2c_adapter *adap = to_i2c_adapter(dev);
479 struct i2c_client *client, *next;
480 unsigned short addr;
481 char end;
482 int res;
484 /* Parse parameters, reject extra parameters */
485 res = sscanf(buf, "%hi%c", &addr, &end);
486 if (res < 1) {
487 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
488 return -EINVAL;
490 if (res > 1 && end != '\n') {
491 dev_err(dev, "%s: Extra parameters\n", "delete_device");
492 return -EINVAL;
495 /* Make sure the device was added through sysfs */
496 res = -ENOENT;
497 mutex_lock(&core_lock);
498 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
499 if (client->addr == addr && client->adapter == adap) {
500 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
501 "delete_device", client->name, client->addr);
503 list_del(&client->detected);
504 i2c_unregister_device(client);
505 res = count;
506 break;
509 mutex_unlock(&core_lock);
511 if (res < 0)
512 dev_err(dev, "%s: Can't find device in list\n",
513 "delete_device");
514 return res;
517 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
518 static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
520 static struct attribute *i2c_adapter_attrs[] = {
521 &dev_attr_name.attr,
522 &dev_attr_new_device.attr,
523 &dev_attr_delete_device.attr,
524 NULL
527 static struct attribute_group i2c_adapter_attr_group = {
528 .attrs = i2c_adapter_attrs,
531 static const struct attribute_group *i2c_adapter_attr_groups[] = {
532 &i2c_adapter_attr_group,
533 NULL
536 static struct device_type i2c_adapter_type = {
537 .groups = i2c_adapter_attr_groups,
538 .release = i2c_adapter_dev_release,
541 #ifdef CONFIG_I2C_COMPAT
542 static struct class_compat *i2c_adapter_compat_class;
543 #endif
545 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
547 struct i2c_devinfo *devinfo;
549 down_read(&__i2c_board_lock);
550 list_for_each_entry(devinfo, &__i2c_board_list, list) {
551 if (devinfo->busnum == adapter->nr
552 && !i2c_new_device(adapter,
553 &devinfo->board_info))
554 dev_err(&adapter->dev,
555 "Can't create device at 0x%02x\n",
556 devinfo->board_info.addr);
558 up_read(&__i2c_board_lock);
561 static int i2c_do_add_adapter(struct device_driver *d, void *data)
563 struct i2c_driver *driver = to_i2c_driver(d);
564 struct i2c_adapter *adap = data;
566 /* Detect supported devices on that bus, and instantiate them */
567 i2c_detect(adap, driver);
569 /* Let legacy drivers scan this bus for matching devices */
570 if (driver->attach_adapter) {
571 /* We ignore the return code; if it fails, too bad */
572 driver->attach_adapter(adap);
574 return 0;
577 static int i2c_register_adapter(struct i2c_adapter *adap)
579 int res = 0, dummy;
581 /* Can't register until after driver model init */
582 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
583 res = -EAGAIN;
584 goto out_list;
587 mutex_init(&adap->bus_lock);
589 /* Set default timeout to 1 second if not already set */
590 if (adap->timeout == 0)
591 adap->timeout = HZ;
593 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
594 adap->dev.bus = &i2c_bus_type;
595 adap->dev.type = &i2c_adapter_type;
596 res = device_register(&adap->dev);
597 if (res)
598 goto out_list;
600 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
602 #ifdef CONFIG_I2C_COMPAT
603 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
604 adap->dev.parent);
605 if (res)
606 dev_warn(&adap->dev,
607 "Failed to create compatibility class link\n");
608 #endif
610 /* create pre-declared device nodes */
611 if (adap->nr < __i2c_first_dynamic_bus_num)
612 i2c_scan_static_board_info(adap);
614 /* Notify drivers */
615 mutex_lock(&core_lock);
616 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
617 i2c_do_add_adapter);
618 mutex_unlock(&core_lock);
620 return 0;
622 out_list:
623 mutex_lock(&core_lock);
624 idr_remove(&i2c_adapter_idr, adap->nr);
625 mutex_unlock(&core_lock);
626 return res;
630 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
631 * @adapter: the adapter to add
632 * Context: can sleep
634 * This routine is used to declare an I2C adapter when its bus number
635 * doesn't matter. Examples: for I2C adapters dynamically added by
636 * USB links or PCI plugin cards.
638 * When this returns zero, a new bus number was allocated and stored
639 * in adap->nr, and the specified adapter became available for clients.
640 * Otherwise, a negative errno value is returned.
642 int i2c_add_adapter(struct i2c_adapter *adapter)
644 int id, res = 0;
646 retry:
647 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
648 return -ENOMEM;
650 mutex_lock(&core_lock);
651 /* "above" here means "above or equal to", sigh */
652 res = idr_get_new_above(&i2c_adapter_idr, adapter,
653 __i2c_first_dynamic_bus_num, &id);
654 mutex_unlock(&core_lock);
656 if (res < 0) {
657 if (res == -EAGAIN)
658 goto retry;
659 return res;
662 adapter->nr = id;
663 return i2c_register_adapter(adapter);
665 EXPORT_SYMBOL(i2c_add_adapter);
668 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
669 * @adap: the adapter to register (with adap->nr initialized)
670 * Context: can sleep
672 * This routine is used to declare an I2C adapter when its bus number
673 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
674 * or otherwise built in to the system's mainboard, and where i2c_board_info
675 * is used to properly configure I2C devices.
677 * If no devices have pre-been declared for this bus, then be sure to
678 * register the adapter before any dynamically allocated ones. Otherwise
679 * the required bus ID may not be available.
681 * When this returns zero, the specified adapter became available for
682 * clients using the bus number provided in adap->nr. Also, the table
683 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
684 * and the appropriate driver model device nodes are created. Otherwise, a
685 * negative errno value is returned.
687 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
689 int id;
690 int status;
692 if (adap->nr & ~MAX_ID_MASK)
693 return -EINVAL;
695 retry:
696 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
697 return -ENOMEM;
699 mutex_lock(&core_lock);
700 /* "above" here means "above or equal to", sigh;
701 * we need the "equal to" result to force the result
703 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
704 if (status == 0 && id != adap->nr) {
705 status = -EBUSY;
706 idr_remove(&i2c_adapter_idr, id);
708 mutex_unlock(&core_lock);
709 if (status == -EAGAIN)
710 goto retry;
712 if (status == 0)
713 status = i2c_register_adapter(adap);
714 return status;
716 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
718 static int i2c_do_del_adapter(struct device_driver *d, void *data)
720 struct i2c_driver *driver = to_i2c_driver(d);
721 struct i2c_adapter *adapter = data;
722 struct i2c_client *client, *_n;
723 int res;
725 /* Remove the devices we created ourselves as the result of hardware
726 * probing (using a driver's detect method) */
727 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
728 if (client->adapter == adapter) {
729 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
730 client->name, client->addr);
731 list_del(&client->detected);
732 i2c_unregister_device(client);
736 if (!driver->detach_adapter)
737 return 0;
738 res = driver->detach_adapter(adapter);
739 if (res)
740 dev_err(&adapter->dev, "detach_adapter failed (%d) "
741 "for driver [%s]\n", res, driver->driver.name);
742 return res;
745 static int __unregister_client(struct device *dev, void *dummy)
747 struct i2c_client *client = i2c_verify_client(dev);
748 if (client)
749 i2c_unregister_device(client);
750 return 0;
754 * i2c_del_adapter - unregister I2C adapter
755 * @adap: the adapter being unregistered
756 * Context: can sleep
758 * This unregisters an I2C adapter which was previously registered
759 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
761 int i2c_del_adapter(struct i2c_adapter *adap)
763 int res = 0;
764 struct i2c_adapter *found;
765 struct i2c_client *client, *next;
767 /* First make sure that this adapter was ever added */
768 mutex_lock(&core_lock);
769 found = idr_find(&i2c_adapter_idr, adap->nr);
770 mutex_unlock(&core_lock);
771 if (found != adap) {
772 pr_debug("i2c-core: attempting to delete unregistered "
773 "adapter [%s]\n", adap->name);
774 return -EINVAL;
777 /* Tell drivers about this removal */
778 mutex_lock(&core_lock);
779 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
780 i2c_do_del_adapter);
781 mutex_unlock(&core_lock);
782 if (res)
783 return res;
785 /* Remove devices instantiated from sysfs */
786 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
787 if (client->adapter == adap) {
788 dev_dbg(&adap->dev, "Removing %s at 0x%x\n",
789 client->name, client->addr);
790 list_del(&client->detected);
791 i2c_unregister_device(client);
795 /* Detach any active clients. This can't fail, thus we do not
796 checking the returned value. */
797 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
799 #ifdef CONFIG_I2C_COMPAT
800 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
801 adap->dev.parent);
802 #endif
804 /* device name is gone after device_unregister */
805 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
807 /* clean up the sysfs representation */
808 init_completion(&adap->dev_released);
809 device_unregister(&adap->dev);
811 /* wait for sysfs to drop all references */
812 wait_for_completion(&adap->dev_released);
814 /* free bus id */
815 mutex_lock(&core_lock);
816 idr_remove(&i2c_adapter_idr, adap->nr);
817 mutex_unlock(&core_lock);
819 /* Clear the device structure in case this adapter is ever going to be
820 added again */
821 memset(&adap->dev, 0, sizeof(adap->dev));
823 return 0;
825 EXPORT_SYMBOL(i2c_del_adapter);
828 /* ------------------------------------------------------------------------- */
830 static int __attach_adapter(struct device *dev, void *data)
832 struct i2c_adapter *adapter;
833 struct i2c_driver *driver = data;
835 if (dev->type != &i2c_adapter_type)
836 return 0;
837 adapter = to_i2c_adapter(dev);
839 i2c_detect(adapter, driver);
841 /* Legacy drivers scan i2c busses directly */
842 if (driver->attach_adapter)
843 driver->attach_adapter(adapter);
845 return 0;
849 * An i2c_driver is used with one or more i2c_client (device) nodes to access
850 * i2c slave chips, on a bus instance associated with some i2c_adapter.
853 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
855 int res;
857 /* Can't register until after driver model init */
858 if (unlikely(WARN_ON(!i2c_bus_type.p)))
859 return -EAGAIN;
861 /* add the driver to the list of i2c drivers in the driver core */
862 driver->driver.owner = owner;
863 driver->driver.bus = &i2c_bus_type;
865 /* When registration returns, the driver core
866 * will have called probe() for all matching-but-unbound devices.
868 res = driver_register(&driver->driver);
869 if (res)
870 return res;
872 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
874 INIT_LIST_HEAD(&driver->clients);
875 /* Walk the adapters that are already present */
876 mutex_lock(&core_lock);
877 bus_for_each_dev(&i2c_bus_type, NULL, driver, __attach_adapter);
878 mutex_unlock(&core_lock);
880 return 0;
882 EXPORT_SYMBOL(i2c_register_driver);
884 static int __detach_adapter(struct device *dev, void *data)
886 struct i2c_adapter *adapter;
887 struct i2c_driver *driver = data;
888 struct i2c_client *client, *_n;
890 if (dev->type != &i2c_adapter_type)
891 return 0;
892 adapter = to_i2c_adapter(dev);
894 /* Remove the devices we created ourselves as the result of hardware
895 * probing (using a driver's detect method) */
896 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
897 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
898 client->name, client->addr);
899 list_del(&client->detected);
900 i2c_unregister_device(client);
903 if (driver->detach_adapter) {
904 if (driver->detach_adapter(adapter))
905 dev_err(&adapter->dev,
906 "detach_adapter failed for driver [%s]\n",
907 driver->driver.name);
910 return 0;
914 * i2c_del_driver - unregister I2C driver
915 * @driver: the driver being unregistered
916 * Context: can sleep
918 void i2c_del_driver(struct i2c_driver *driver)
920 mutex_lock(&core_lock);
921 bus_for_each_dev(&i2c_bus_type, NULL, driver, __detach_adapter);
922 mutex_unlock(&core_lock);
924 driver_unregister(&driver->driver);
925 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
927 EXPORT_SYMBOL(i2c_del_driver);
929 /* ------------------------------------------------------------------------- */
931 static int __i2c_check_addr(struct device *dev, void *addrp)
933 struct i2c_client *client = i2c_verify_client(dev);
934 int addr = *(int *)addrp;
936 if (client && client->addr == addr)
937 return -EBUSY;
938 return 0;
941 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
943 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
947 * i2c_use_client - increments the reference count of the i2c client structure
948 * @client: the client being referenced
950 * Each live reference to a client should be refcounted. The driver model does
951 * that automatically as part of driver binding, so that most drivers don't
952 * need to do this explicitly: they hold a reference until they're unbound
953 * from the device.
955 * A pointer to the client with the incremented reference counter is returned.
957 struct i2c_client *i2c_use_client(struct i2c_client *client)
959 if (client && get_device(&client->dev))
960 return client;
961 return NULL;
963 EXPORT_SYMBOL(i2c_use_client);
966 * i2c_release_client - release a use of the i2c client structure
967 * @client: the client being no longer referenced
969 * Must be called when a user of a client is finished with it.
971 void i2c_release_client(struct i2c_client *client)
973 if (client)
974 put_device(&client->dev);
976 EXPORT_SYMBOL(i2c_release_client);
978 struct i2c_cmd_arg {
979 unsigned cmd;
980 void *arg;
983 static int i2c_cmd(struct device *dev, void *_arg)
985 struct i2c_client *client = i2c_verify_client(dev);
986 struct i2c_cmd_arg *arg = _arg;
988 if (client && client->driver && client->driver->command)
989 client->driver->command(client, arg->cmd, arg->arg);
990 return 0;
993 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
995 struct i2c_cmd_arg cmd_arg;
997 cmd_arg.cmd = cmd;
998 cmd_arg.arg = arg;
999 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1001 EXPORT_SYMBOL(i2c_clients_command);
1003 static int __init i2c_init(void)
1005 int retval;
1007 retval = bus_register(&i2c_bus_type);
1008 if (retval)
1009 return retval;
1010 #ifdef CONFIG_I2C_COMPAT
1011 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1012 if (!i2c_adapter_compat_class) {
1013 retval = -ENOMEM;
1014 goto bus_err;
1016 #endif
1017 retval = i2c_add_driver(&dummy_driver);
1018 if (retval)
1019 goto class_err;
1020 return 0;
1022 class_err:
1023 #ifdef CONFIG_I2C_COMPAT
1024 class_compat_unregister(i2c_adapter_compat_class);
1025 bus_err:
1026 #endif
1027 bus_unregister(&i2c_bus_type);
1028 return retval;
1031 static void __exit i2c_exit(void)
1033 i2c_del_driver(&dummy_driver);
1034 #ifdef CONFIG_I2C_COMPAT
1035 class_compat_unregister(i2c_adapter_compat_class);
1036 #endif
1037 bus_unregister(&i2c_bus_type);
1040 /* We must initialize early, because some subsystems register i2c drivers
1041 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1043 postcore_initcall(i2c_init);
1044 module_exit(i2c_exit);
1046 /* ----------------------------------------------------
1047 * the functional interface to the i2c busses.
1048 * ----------------------------------------------------
1052 * i2c_transfer - execute a single or combined I2C message
1053 * @adap: Handle to I2C bus
1054 * @msgs: One or more messages to execute before STOP is issued to
1055 * terminate the operation; each message begins with a START.
1056 * @num: Number of messages to be executed.
1058 * Returns negative errno, else the number of messages executed.
1060 * Note that there is no requirement that each message be sent to
1061 * the same slave address, although that is the most common model.
1063 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1065 unsigned long orig_jiffies;
1066 int ret, try;
1068 /* REVISIT the fault reporting model here is weak:
1070 * - When we get an error after receiving N bytes from a slave,
1071 * there is no way to report "N".
1073 * - When we get a NAK after transmitting N bytes to a slave,
1074 * there is no way to report "N" ... or to let the master
1075 * continue executing the rest of this combined message, if
1076 * that's the appropriate response.
1078 * - When for example "num" is two and we successfully complete
1079 * the first message but get an error part way through the
1080 * second, it's unclear whether that should be reported as
1081 * one (discarding status on the second message) or errno
1082 * (discarding status on the first one).
1085 if (adap->algo->master_xfer) {
1086 #ifdef DEBUG
1087 for (ret = 0; ret < num; ret++) {
1088 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1089 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1090 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1091 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1093 #endif
1095 if (in_atomic() || irqs_disabled()) {
1096 ret = mutex_trylock(&adap->bus_lock);
1097 if (!ret)
1098 /* I2C activity is ongoing. */
1099 return -EAGAIN;
1100 } else {
1101 mutex_lock_nested(&adap->bus_lock, adap->level);
1104 /* Retry automatically on arbitration loss */
1105 orig_jiffies = jiffies;
1106 for (ret = 0, try = 0; try <= adap->retries; try++) {
1107 ret = adap->algo->master_xfer(adap, msgs, num);
1108 if (ret != -EAGAIN)
1109 break;
1110 if (time_after(jiffies, orig_jiffies + adap->timeout))
1111 break;
1113 mutex_unlock(&adap->bus_lock);
1115 return ret;
1116 } else {
1117 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1118 return -EOPNOTSUPP;
1121 EXPORT_SYMBOL(i2c_transfer);
1124 * i2c_master_send - issue a single I2C message in master transmit mode
1125 * @client: Handle to slave device
1126 * @buf: Data that will be written to the slave
1127 * @count: How many bytes to write
1129 * Returns negative errno, or else the number of bytes written.
1131 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1133 int ret;
1134 struct i2c_adapter *adap=client->adapter;
1135 struct i2c_msg msg;
1137 msg.addr = client->addr;
1138 msg.flags = client->flags & I2C_M_TEN;
1139 msg.len = count;
1140 msg.buf = (char *)buf;
1142 ret = i2c_transfer(adap, &msg, 1);
1144 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1145 transmitted, else error code. */
1146 return (ret == 1) ? count : ret;
1148 EXPORT_SYMBOL(i2c_master_send);
1151 * i2c_master_recv - issue a single I2C message in master receive mode
1152 * @client: Handle to slave device
1153 * @buf: Where to store data read from slave
1154 * @count: How many bytes to read
1156 * Returns negative errno, or else the number of bytes read.
1158 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1160 struct i2c_adapter *adap=client->adapter;
1161 struct i2c_msg msg;
1162 int ret;
1164 msg.addr = client->addr;
1165 msg.flags = client->flags & I2C_M_TEN;
1166 msg.flags |= I2C_M_RD;
1167 msg.len = count;
1168 msg.buf = buf;
1170 ret = i2c_transfer(adap, &msg, 1);
1172 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1173 transmitted, else error code. */
1174 return (ret == 1) ? count : ret;
1176 EXPORT_SYMBOL(i2c_master_recv);
1178 /* ----------------------------------------------------
1179 * the i2c address scanning function
1180 * Will not work for 10-bit addresses!
1181 * ----------------------------------------------------
1184 static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1185 struct i2c_driver *driver)
1187 struct i2c_board_info info;
1188 struct i2c_adapter *adapter = temp_client->adapter;
1189 int addr = temp_client->addr;
1190 int err;
1192 /* Make sure the address is valid */
1193 if (addr < 0x03 || addr > 0x77) {
1194 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1195 addr);
1196 return -EINVAL;
1199 /* Skip if already in use */
1200 if (i2c_check_addr(adapter, addr))
1201 return 0;
1203 /* Make sure there is something at this address, unless forced */
1204 if (kind < 0) {
1205 if (addr == 0x73 && (adapter->class & I2C_CLASS_HWMON)) {
1206 /* Special probe for FSC hwmon chips */
1207 union i2c_smbus_data dummy;
1209 if (i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_READ, 0,
1210 I2C_SMBUS_BYTE_DATA, &dummy) < 0)
1211 return 0;
1212 } else {
1213 if (i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_WRITE, 0,
1214 I2C_SMBUS_QUICK, NULL) < 0)
1215 return 0;
1217 /* Prevent 24RF08 corruption */
1218 if ((addr & ~0x0f) == 0x50)
1219 i2c_smbus_xfer(adapter, addr, 0,
1220 I2C_SMBUS_WRITE, 0,
1221 I2C_SMBUS_QUICK, NULL);
1225 /* Finally call the custom detection function */
1226 memset(&info, 0, sizeof(struct i2c_board_info));
1227 info.addr = addr;
1228 err = driver->detect(temp_client, kind, &info);
1229 if (err) {
1230 /* -ENODEV is returned if the detection fails. We catch it
1231 here as this isn't an error. */
1232 return err == -ENODEV ? 0 : err;
1235 /* Consistency check */
1236 if (info.type[0] == '\0') {
1237 dev_err(&adapter->dev, "%s detection function provided "
1238 "no name for 0x%x\n", driver->driver.name,
1239 addr);
1240 } else {
1241 struct i2c_client *client;
1243 /* Detection succeeded, instantiate the device */
1244 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1245 info.type, info.addr);
1246 client = i2c_new_device(adapter, &info);
1247 if (client)
1248 list_add_tail(&client->detected, &driver->clients);
1249 else
1250 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1251 info.type, info.addr);
1253 return 0;
1256 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1258 const struct i2c_client_address_data *address_data;
1259 struct i2c_client *temp_client;
1260 int i, err = 0;
1261 int adap_id = i2c_adapter_id(adapter);
1263 address_data = driver->address_data;
1264 if (!driver->detect || !address_data)
1265 return 0;
1267 /* Set up a temporary client to help detect callback */
1268 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1269 if (!temp_client)
1270 return -ENOMEM;
1271 temp_client->adapter = adapter;
1273 /* Force entries are done first, and are not affected by ignore
1274 entries */
1275 if (address_data->forces) {
1276 const unsigned short * const *forces = address_data->forces;
1277 int kind;
1279 for (kind = 0; forces[kind]; kind++) {
1280 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1281 i += 2) {
1282 if (forces[kind][i] == adap_id
1283 || forces[kind][i] == ANY_I2C_BUS) {
1284 dev_dbg(&adapter->dev, "found force "
1285 "parameter for adapter %d, "
1286 "addr 0x%02x, kind %d\n",
1287 adap_id, forces[kind][i + 1],
1288 kind);
1289 temp_client->addr = forces[kind][i + 1];
1290 err = i2c_detect_address(temp_client,
1291 kind, driver);
1292 if (err)
1293 goto exit_free;
1299 /* Stop here if the classes do not match */
1300 if (!(adapter->class & driver->class))
1301 goto exit_free;
1303 /* Stop here if we can't use SMBUS_QUICK */
1304 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1305 if (address_data->probe[0] == I2C_CLIENT_END
1306 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1307 goto exit_free;
1309 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1310 "can't probe for chips\n");
1311 err = -EOPNOTSUPP;
1312 goto exit_free;
1315 /* Probe entries are done second, and are not affected by ignore
1316 entries either */
1317 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1318 if (address_data->probe[i] == adap_id
1319 || address_data->probe[i] == ANY_I2C_BUS) {
1320 dev_dbg(&adapter->dev, "found probe parameter for "
1321 "adapter %d, addr 0x%02x\n", adap_id,
1322 address_data->probe[i + 1]);
1323 temp_client->addr = address_data->probe[i + 1];
1324 err = i2c_detect_address(temp_client, -1, driver);
1325 if (err)
1326 goto exit_free;
1330 /* Normal entries are done last, unless shadowed by an ignore entry */
1331 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1332 int j, ignore;
1334 ignore = 0;
1335 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1336 j += 2) {
1337 if ((address_data->ignore[j] == adap_id ||
1338 address_data->ignore[j] == ANY_I2C_BUS)
1339 && address_data->ignore[j + 1]
1340 == address_data->normal_i2c[i]) {
1341 dev_dbg(&adapter->dev, "found ignore "
1342 "parameter for adapter %d, "
1343 "addr 0x%02x\n", adap_id,
1344 address_data->ignore[j + 1]);
1345 ignore = 1;
1346 break;
1349 if (ignore)
1350 continue;
1352 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1353 "addr 0x%02x\n", adap_id,
1354 address_data->normal_i2c[i]);
1355 temp_client->addr = address_data->normal_i2c[i];
1356 err = i2c_detect_address(temp_client, -1, driver);
1357 if (err)
1358 goto exit_free;
1361 exit_free:
1362 kfree(temp_client);
1363 return err;
1366 struct i2c_client *
1367 i2c_new_probed_device(struct i2c_adapter *adap,
1368 struct i2c_board_info *info,
1369 unsigned short const *addr_list)
1371 int i;
1373 /* Stop here if the bus doesn't support probing */
1374 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1375 dev_err(&adap->dev, "Probing not supported\n");
1376 return NULL;
1379 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1380 /* Check address validity */
1381 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1382 dev_warn(&adap->dev, "Invalid 7-bit address "
1383 "0x%02x\n", addr_list[i]);
1384 continue;
1387 /* Check address availability */
1388 if (i2c_check_addr(adap, addr_list[i])) {
1389 dev_dbg(&adap->dev, "Address 0x%02x already in "
1390 "use, not probing\n", addr_list[i]);
1391 continue;
1394 /* Test address responsiveness
1395 The default probe method is a quick write, but it is known
1396 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1397 and could also irreversibly write-protect some EEPROMs, so
1398 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1399 read instead. Also, some bus drivers don't implement
1400 quick write, so we fallback to a byte read it that case
1401 too. */
1402 if ((addr_list[i] & ~0x07) == 0x30
1403 || (addr_list[i] & ~0x0f) == 0x50
1404 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1405 union i2c_smbus_data data;
1407 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1408 I2C_SMBUS_READ, 0,
1409 I2C_SMBUS_BYTE, &data) >= 0)
1410 break;
1411 } else {
1412 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1413 I2C_SMBUS_WRITE, 0,
1414 I2C_SMBUS_QUICK, NULL) >= 0)
1415 break;
1419 if (addr_list[i] == I2C_CLIENT_END) {
1420 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1421 return NULL;
1424 info->addr = addr_list[i];
1425 return i2c_new_device(adap, info);
1427 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1429 struct i2c_adapter* i2c_get_adapter(int id)
1431 struct i2c_adapter *adapter;
1433 mutex_lock(&core_lock);
1434 adapter = idr_find(&i2c_adapter_idr, id);
1435 if (adapter && !try_module_get(adapter->owner))
1436 adapter = NULL;
1438 mutex_unlock(&core_lock);
1439 return adapter;
1441 EXPORT_SYMBOL(i2c_get_adapter);
1443 void i2c_put_adapter(struct i2c_adapter *adap)
1445 module_put(adap->owner);
1447 EXPORT_SYMBOL(i2c_put_adapter);
1449 /* The SMBus parts */
1451 #define POLY (0x1070U << 3)
1452 static u8 crc8(u16 data)
1454 int i;
1456 for(i = 0; i < 8; i++) {
1457 if (data & 0x8000)
1458 data = data ^ POLY;
1459 data = data << 1;
1461 return (u8)(data >> 8);
1464 /* Incremental CRC8 over count bytes in the array pointed to by p */
1465 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1467 int i;
1469 for(i = 0; i < count; i++)
1470 crc = crc8((crc ^ p[i]) << 8);
1471 return crc;
1474 /* Assume a 7-bit address, which is reasonable for SMBus */
1475 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1477 /* The address will be sent first */
1478 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1479 pec = i2c_smbus_pec(pec, &addr, 1);
1481 /* The data buffer follows */
1482 return i2c_smbus_pec(pec, msg->buf, msg->len);
1485 /* Used for write only transactions */
1486 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1488 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1489 msg->len++;
1492 /* Return <0 on CRC error
1493 If there was a write before this read (most cases) we need to take the
1494 partial CRC from the write part into account.
1495 Note that this function does modify the message (we need to decrease the
1496 message length to hide the CRC byte from the caller). */
1497 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1499 u8 rpec = msg->buf[--msg->len];
1500 cpec = i2c_smbus_msg_pec(cpec, msg);
1502 if (rpec != cpec) {
1503 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1504 rpec, cpec);
1505 return -EBADMSG;
1507 return 0;
1511 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1512 * @client: Handle to slave device
1514 * This executes the SMBus "receive byte" protocol, returning negative errno
1515 * else the byte received from the device.
1517 s32 i2c_smbus_read_byte(struct i2c_client *client)
1519 union i2c_smbus_data data;
1520 int status;
1522 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1523 I2C_SMBUS_READ, 0,
1524 I2C_SMBUS_BYTE, &data);
1525 return (status < 0) ? status : data.byte;
1527 EXPORT_SYMBOL(i2c_smbus_read_byte);
1530 * i2c_smbus_write_byte - SMBus "send byte" protocol
1531 * @client: Handle to slave device
1532 * @value: Byte to be sent
1534 * This executes the SMBus "send byte" protocol, returning negative errno
1535 * else zero on success.
1537 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1539 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1540 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1542 EXPORT_SYMBOL(i2c_smbus_write_byte);
1545 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1546 * @client: Handle to slave device
1547 * @command: Byte interpreted by slave
1549 * This executes the SMBus "read byte" protocol, returning negative errno
1550 * else a data byte received from the device.
1552 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1554 union i2c_smbus_data data;
1555 int status;
1557 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1558 I2C_SMBUS_READ, command,
1559 I2C_SMBUS_BYTE_DATA, &data);
1560 return (status < 0) ? status : data.byte;
1562 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1565 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1566 * @client: Handle to slave device
1567 * @command: Byte interpreted by slave
1568 * @value: Byte being written
1570 * This executes the SMBus "write byte" protocol, returning negative errno
1571 * else zero on success.
1573 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1575 union i2c_smbus_data data;
1576 data.byte = value;
1577 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1578 I2C_SMBUS_WRITE,command,
1579 I2C_SMBUS_BYTE_DATA,&data);
1581 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1584 * i2c_smbus_read_word_data - SMBus "read word" protocol
1585 * @client: Handle to slave device
1586 * @command: Byte interpreted by slave
1588 * This executes the SMBus "read word" protocol, returning negative errno
1589 * else a 16-bit unsigned "word" received from the device.
1591 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1593 union i2c_smbus_data data;
1594 int status;
1596 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1597 I2C_SMBUS_READ, command,
1598 I2C_SMBUS_WORD_DATA, &data);
1599 return (status < 0) ? status : data.word;
1601 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1604 * i2c_smbus_write_word_data - SMBus "write word" protocol
1605 * @client: Handle to slave device
1606 * @command: Byte interpreted by slave
1607 * @value: 16-bit "word" being written
1609 * This executes the SMBus "write word" protocol, returning negative errno
1610 * else zero on success.
1612 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1614 union i2c_smbus_data data;
1615 data.word = value;
1616 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1617 I2C_SMBUS_WRITE,command,
1618 I2C_SMBUS_WORD_DATA,&data);
1620 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1623 * i2c_smbus_process_call - SMBus "process call" protocol
1624 * @client: Handle to slave device
1625 * @command: Byte interpreted by slave
1626 * @value: 16-bit "word" being written
1628 * This executes the SMBus "process call" protocol, returning negative errno
1629 * else a 16-bit unsigned "word" received from the device.
1631 s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1633 union i2c_smbus_data data;
1634 int status;
1635 data.word = value;
1637 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1638 I2C_SMBUS_WRITE, command,
1639 I2C_SMBUS_PROC_CALL, &data);
1640 return (status < 0) ? status : data.word;
1642 EXPORT_SYMBOL(i2c_smbus_process_call);
1645 * i2c_smbus_read_block_data - SMBus "block read" protocol
1646 * @client: Handle to slave device
1647 * @command: Byte interpreted by slave
1648 * @values: Byte array into which data will be read; big enough to hold
1649 * the data returned by the slave. SMBus allows at most 32 bytes.
1651 * This executes the SMBus "block read" protocol, returning negative errno
1652 * else the number of data bytes in the slave's response.
1654 * Note that using this function requires that the client's adapter support
1655 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1656 * support this; its emulation through I2C messaging relies on a specific
1657 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1659 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1660 u8 *values)
1662 union i2c_smbus_data data;
1663 int status;
1665 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1666 I2C_SMBUS_READ, command,
1667 I2C_SMBUS_BLOCK_DATA, &data);
1668 if (status)
1669 return status;
1671 memcpy(values, &data.block[1], data.block[0]);
1672 return data.block[0];
1674 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1677 * i2c_smbus_write_block_data - SMBus "block write" protocol
1678 * @client: Handle to slave device
1679 * @command: Byte interpreted by slave
1680 * @length: Size of data block; SMBus allows at most 32 bytes
1681 * @values: Byte array which will be written.
1683 * This executes the SMBus "block write" protocol, returning negative errno
1684 * else zero on success.
1686 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1687 u8 length, const u8 *values)
1689 union i2c_smbus_data data;
1691 if (length > I2C_SMBUS_BLOCK_MAX)
1692 length = I2C_SMBUS_BLOCK_MAX;
1693 data.block[0] = length;
1694 memcpy(&data.block[1], values, length);
1695 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1696 I2C_SMBUS_WRITE,command,
1697 I2C_SMBUS_BLOCK_DATA,&data);
1699 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1701 /* Returns the number of read bytes */
1702 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1703 u8 length, u8 *values)
1705 union i2c_smbus_data data;
1706 int status;
1708 if (length > I2C_SMBUS_BLOCK_MAX)
1709 length = I2C_SMBUS_BLOCK_MAX;
1710 data.block[0] = length;
1711 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1712 I2C_SMBUS_READ, command,
1713 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1714 if (status < 0)
1715 return status;
1717 memcpy(values, &data.block[1], data.block[0]);
1718 return data.block[0];
1720 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1722 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1723 u8 length, const u8 *values)
1725 union i2c_smbus_data data;
1727 if (length > I2C_SMBUS_BLOCK_MAX)
1728 length = I2C_SMBUS_BLOCK_MAX;
1729 data.block[0] = length;
1730 memcpy(data.block + 1, values, length);
1731 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1732 I2C_SMBUS_WRITE, command,
1733 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1735 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1737 /* Simulate a SMBus command using the i2c protocol
1738 No checking of parameters is done! */
1739 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1740 unsigned short flags,
1741 char read_write, u8 command, int size,
1742 union i2c_smbus_data * data)
1744 /* So we need to generate a series of msgs. In the case of writing, we
1745 need to use only one message; when reading, we need two. We initialize
1746 most things with sane defaults, to keep the code below somewhat
1747 simpler. */
1748 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1749 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1750 int num = read_write == I2C_SMBUS_READ?2:1;
1751 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1752 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1754 int i;
1755 u8 partial_pec = 0;
1756 int status;
1758 msgbuf0[0] = command;
1759 switch(size) {
1760 case I2C_SMBUS_QUICK:
1761 msg[0].len = 0;
1762 /* Special case: The read/write field is used as data */
1763 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1764 I2C_M_RD : 0);
1765 num = 1;
1766 break;
1767 case I2C_SMBUS_BYTE:
1768 if (read_write == I2C_SMBUS_READ) {
1769 /* Special case: only a read! */
1770 msg[0].flags = I2C_M_RD | flags;
1771 num = 1;
1773 break;
1774 case I2C_SMBUS_BYTE_DATA:
1775 if (read_write == I2C_SMBUS_READ)
1776 msg[1].len = 1;
1777 else {
1778 msg[0].len = 2;
1779 msgbuf0[1] = data->byte;
1781 break;
1782 case I2C_SMBUS_WORD_DATA:
1783 if (read_write == I2C_SMBUS_READ)
1784 msg[1].len = 2;
1785 else {
1786 msg[0].len=3;
1787 msgbuf0[1] = data->word & 0xff;
1788 msgbuf0[2] = data->word >> 8;
1790 break;
1791 case I2C_SMBUS_PROC_CALL:
1792 num = 2; /* Special case */
1793 read_write = I2C_SMBUS_READ;
1794 msg[0].len = 3;
1795 msg[1].len = 2;
1796 msgbuf0[1] = data->word & 0xff;
1797 msgbuf0[2] = data->word >> 8;
1798 break;
1799 case I2C_SMBUS_BLOCK_DATA:
1800 if (read_write == I2C_SMBUS_READ) {
1801 msg[1].flags |= I2C_M_RECV_LEN;
1802 msg[1].len = 1; /* block length will be added by
1803 the underlying bus driver */
1804 } else {
1805 msg[0].len = data->block[0] + 2;
1806 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1807 dev_err(&adapter->dev,
1808 "Invalid block write size %d\n",
1809 data->block[0]);
1810 return -EINVAL;
1812 for (i = 1; i < msg[0].len; i++)
1813 msgbuf0[i] = data->block[i-1];
1815 break;
1816 case I2C_SMBUS_BLOCK_PROC_CALL:
1817 num = 2; /* Another special case */
1818 read_write = I2C_SMBUS_READ;
1819 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1820 dev_err(&adapter->dev,
1821 "Invalid block write size %d\n",
1822 data->block[0]);
1823 return -EINVAL;
1825 msg[0].len = data->block[0] + 2;
1826 for (i = 1; i < msg[0].len; i++)
1827 msgbuf0[i] = data->block[i-1];
1828 msg[1].flags |= I2C_M_RECV_LEN;
1829 msg[1].len = 1; /* block length will be added by
1830 the underlying bus driver */
1831 break;
1832 case I2C_SMBUS_I2C_BLOCK_DATA:
1833 if (read_write == I2C_SMBUS_READ) {
1834 msg[1].len = data->block[0];
1835 } else {
1836 msg[0].len = data->block[0] + 1;
1837 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1838 dev_err(&adapter->dev,
1839 "Invalid block write size %d\n",
1840 data->block[0]);
1841 return -EINVAL;
1843 for (i = 1; i <= data->block[0]; i++)
1844 msgbuf0[i] = data->block[i];
1846 break;
1847 default:
1848 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1849 return -EOPNOTSUPP;
1852 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1853 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1854 if (i) {
1855 /* Compute PEC if first message is a write */
1856 if (!(msg[0].flags & I2C_M_RD)) {
1857 if (num == 1) /* Write only */
1858 i2c_smbus_add_pec(&msg[0]);
1859 else /* Write followed by read */
1860 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1862 /* Ask for PEC if last message is a read */
1863 if (msg[num-1].flags & I2C_M_RD)
1864 msg[num-1].len++;
1867 status = i2c_transfer(adapter, msg, num);
1868 if (status < 0)
1869 return status;
1871 /* Check PEC if last message is a read */
1872 if (i && (msg[num-1].flags & I2C_M_RD)) {
1873 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1874 if (status < 0)
1875 return status;
1878 if (read_write == I2C_SMBUS_READ)
1879 switch(size) {
1880 case I2C_SMBUS_BYTE:
1881 data->byte = msgbuf0[0];
1882 break;
1883 case I2C_SMBUS_BYTE_DATA:
1884 data->byte = msgbuf1[0];
1885 break;
1886 case I2C_SMBUS_WORD_DATA:
1887 case I2C_SMBUS_PROC_CALL:
1888 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1889 break;
1890 case I2C_SMBUS_I2C_BLOCK_DATA:
1891 for (i = 0; i < data->block[0]; i++)
1892 data->block[i+1] = msgbuf1[i];
1893 break;
1894 case I2C_SMBUS_BLOCK_DATA:
1895 case I2C_SMBUS_BLOCK_PROC_CALL:
1896 for (i = 0; i < msgbuf1[0] + 1; i++)
1897 data->block[i] = msgbuf1[i];
1898 break;
1900 return 0;
1904 * i2c_smbus_xfer - execute SMBus protocol operations
1905 * @adapter: Handle to I2C bus
1906 * @addr: Address of SMBus slave on that bus
1907 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1908 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1909 * @command: Byte interpreted by slave, for protocols which use such bytes
1910 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1911 * @data: Data to be read or written
1913 * This executes an SMBus protocol operation, and returns a negative
1914 * errno code else zero on success.
1916 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1917 char read_write, u8 command, int protocol,
1918 union i2c_smbus_data *data)
1920 unsigned long orig_jiffies;
1921 int try;
1922 s32 res;
1924 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1926 if (adapter->algo->smbus_xfer) {
1927 mutex_lock(&adapter->bus_lock);
1929 /* Retry automatically on arbitration loss */
1930 orig_jiffies = jiffies;
1931 for (res = 0, try = 0; try <= adapter->retries; try++) {
1932 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1933 read_write, command,
1934 protocol, data);
1935 if (res != -EAGAIN)
1936 break;
1937 if (time_after(jiffies,
1938 orig_jiffies + adapter->timeout))
1939 break;
1941 mutex_unlock(&adapter->bus_lock);
1942 } else
1943 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1944 command, protocol, data);
1946 return res;
1948 EXPORT_SYMBOL(i2c_smbus_xfer);
1950 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1951 MODULE_DESCRIPTION("I2C-Bus main module");
1952 MODULE_LICENSE("GPL");