acpi_pad: build only on X86
[linux-2.6/linux-acpi-2.6.git] / drivers / i2c / i2c-core.c
blob0e45c296d3d22b3230f044a9a75bf360515102b0
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 int i2c_check_addr(struct i2c_adapter *adapter, int addr);
50 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
52 /* ------------------------------------------------------------------------- */
54 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
55 const struct i2c_client *client)
57 while (id->name[0]) {
58 if (strcmp(client->name, id->name) == 0)
59 return id;
60 id++;
62 return NULL;
65 static int i2c_device_match(struct device *dev, struct device_driver *drv)
67 struct i2c_client *client = to_i2c_client(dev);
68 struct i2c_driver *driver = to_i2c_driver(drv);
70 /* match on an id table if there is one */
71 if (driver->id_table)
72 return i2c_match_id(driver->id_table, client) != NULL;
74 return 0;
77 #ifdef CONFIG_HOTPLUG
79 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
80 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
82 struct i2c_client *client = to_i2c_client(dev);
84 if (add_uevent_var(env, "MODALIAS=%s%s",
85 I2C_MODULE_PREFIX, client->name))
86 return -ENOMEM;
87 dev_dbg(dev, "uevent\n");
88 return 0;
91 #else
92 #define i2c_device_uevent NULL
93 #endif /* CONFIG_HOTPLUG */
95 static int i2c_device_probe(struct device *dev)
97 struct i2c_client *client = to_i2c_client(dev);
98 struct i2c_driver *driver = to_i2c_driver(dev->driver);
99 int status;
101 if (!driver->probe || !driver->id_table)
102 return -ENODEV;
103 client->driver = driver;
104 if (!device_can_wakeup(&client->dev))
105 device_init_wakeup(&client->dev,
106 client->flags & I2C_CLIENT_WAKE);
107 dev_dbg(dev, "probe\n");
109 status = driver->probe(client, i2c_match_id(driver->id_table, client));
110 if (status)
111 client->driver = NULL;
112 return status;
115 static int i2c_device_remove(struct device *dev)
117 struct i2c_client *client = to_i2c_client(dev);
118 struct i2c_driver *driver;
119 int status;
121 if (!dev->driver)
122 return 0;
124 driver = to_i2c_driver(dev->driver);
125 if (driver->remove) {
126 dev_dbg(dev, "remove\n");
127 status = driver->remove(client);
128 } else {
129 dev->driver = NULL;
130 status = 0;
132 if (status == 0)
133 client->driver = NULL;
134 return status;
137 static void i2c_device_shutdown(struct device *dev)
139 struct i2c_driver *driver;
141 if (!dev->driver)
142 return;
143 driver = to_i2c_driver(dev->driver);
144 if (driver->shutdown)
145 driver->shutdown(to_i2c_client(dev));
148 static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
150 struct i2c_driver *driver;
152 if (!dev->driver)
153 return 0;
154 driver = to_i2c_driver(dev->driver);
155 if (!driver->suspend)
156 return 0;
157 return driver->suspend(to_i2c_client(dev), mesg);
160 static int i2c_device_resume(struct device *dev)
162 struct i2c_driver *driver;
164 if (!dev->driver)
165 return 0;
166 driver = to_i2c_driver(dev->driver);
167 if (!driver->resume)
168 return 0;
169 return driver->resume(to_i2c_client(dev));
172 static void i2c_client_dev_release(struct device *dev)
174 kfree(to_i2c_client(dev));
177 static ssize_t
178 show_client_name(struct device *dev, struct device_attribute *attr, char *buf)
180 struct i2c_client *client = to_i2c_client(dev);
181 return sprintf(buf, "%s\n", client->name);
184 static ssize_t
185 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
187 struct i2c_client *client = to_i2c_client(dev);
188 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
191 static struct device_attribute i2c_dev_attrs[] = {
192 __ATTR(name, S_IRUGO, show_client_name, NULL),
193 /* modalias helps coldplug: modprobe $(cat .../modalias) */
194 __ATTR(modalias, S_IRUGO, show_modalias, NULL),
195 { },
198 struct bus_type i2c_bus_type = {
199 .name = "i2c",
200 .dev_attrs = i2c_dev_attrs,
201 .match = i2c_device_match,
202 .uevent = i2c_device_uevent,
203 .probe = i2c_device_probe,
204 .remove = i2c_device_remove,
205 .shutdown = i2c_device_shutdown,
206 .suspend = i2c_device_suspend,
207 .resume = i2c_device_resume,
209 EXPORT_SYMBOL_GPL(i2c_bus_type);
213 * i2c_verify_client - return parameter as i2c_client, or NULL
214 * @dev: device, probably from some driver model iterator
216 * When traversing the driver model tree, perhaps using driver model
217 * iterators like @device_for_each_child(), you can't assume very much
218 * about the nodes you find. Use this function to avoid oopses caused
219 * by wrongly treating some non-I2C device as an i2c_client.
221 struct i2c_client *i2c_verify_client(struct device *dev)
223 return (dev->bus == &i2c_bus_type)
224 ? to_i2c_client(dev)
225 : NULL;
227 EXPORT_SYMBOL(i2c_verify_client);
231 * i2c_new_device - instantiate an i2c device
232 * @adap: the adapter managing the device
233 * @info: describes one I2C device; bus_num is ignored
234 * Context: can sleep
236 * Create an i2c device. Binding is handled through driver model
237 * probe()/remove() methods. A driver may be bound to this device when we
238 * return from this function, or any later moment (e.g. maybe hotplugging will
239 * load the driver module). This call is not appropriate for use by mainboard
240 * initialization logic, which usually runs during an arch_initcall() long
241 * before any i2c_adapter could exist.
243 * This returns the new i2c client, which may be saved for later use with
244 * i2c_unregister_device(); or NULL to indicate an error.
246 struct i2c_client *
247 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
249 struct i2c_client *client;
250 int status;
252 client = kzalloc(sizeof *client, GFP_KERNEL);
253 if (!client)
254 return NULL;
256 client->adapter = adap;
258 client->dev.platform_data = info->platform_data;
260 if (info->archdata)
261 client->dev.archdata = *info->archdata;
263 client->flags = info->flags;
264 client->addr = info->addr;
265 client->irq = info->irq;
267 strlcpy(client->name, info->type, sizeof(client->name));
269 /* Check for address business */
270 status = i2c_check_addr(adap, client->addr);
271 if (status)
272 goto out_err;
274 client->dev.parent = &client->adapter->dev;
275 client->dev.bus = &i2c_bus_type;
276 client->dev.release = i2c_client_dev_release;
278 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
279 client->addr);
280 status = device_register(&client->dev);
281 if (status)
282 goto out_err;
284 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
285 client->name, dev_name(&client->dev));
287 return client;
289 out_err:
290 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
291 "(%d)\n", client->name, client->addr, status);
292 kfree(client);
293 return NULL;
295 EXPORT_SYMBOL_GPL(i2c_new_device);
299 * i2c_unregister_device - reverse effect of i2c_new_device()
300 * @client: value returned from i2c_new_device()
301 * Context: can sleep
303 void i2c_unregister_device(struct i2c_client *client)
305 device_unregister(&client->dev);
307 EXPORT_SYMBOL_GPL(i2c_unregister_device);
310 static const struct i2c_device_id dummy_id[] = {
311 { "dummy", 0 },
312 { },
315 static int dummy_probe(struct i2c_client *client,
316 const struct i2c_device_id *id)
318 return 0;
321 static int dummy_remove(struct i2c_client *client)
323 return 0;
326 static struct i2c_driver dummy_driver = {
327 .driver.name = "dummy",
328 .probe = dummy_probe,
329 .remove = dummy_remove,
330 .id_table = dummy_id,
334 * i2c_new_dummy - return a new i2c device bound to a dummy driver
335 * @adapter: the adapter managing the device
336 * @address: seven bit address to be used
337 * Context: can sleep
339 * This returns an I2C client bound to the "dummy" driver, intended for use
340 * with devices that consume multiple addresses. Examples of such chips
341 * include various EEPROMS (like 24c04 and 24c08 models).
343 * These dummy devices have two main uses. First, most I2C and SMBus calls
344 * except i2c_transfer() need a client handle; the dummy will be that handle.
345 * And second, this prevents the specified address from being bound to a
346 * different driver.
348 * This returns the new i2c client, which should be saved for later use with
349 * i2c_unregister_device(); or NULL to indicate an error.
351 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
353 struct i2c_board_info info = {
354 I2C_BOARD_INFO("dummy", address),
357 return i2c_new_device(adapter, &info);
359 EXPORT_SYMBOL_GPL(i2c_new_dummy);
361 /* ------------------------------------------------------------------------- */
363 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
365 static void i2c_adapter_dev_release(struct device *dev)
367 struct i2c_adapter *adap = to_i2c_adapter(dev);
368 complete(&adap->dev_released);
371 static ssize_t
372 show_adapter_name(struct device *dev, struct device_attribute *attr, char *buf)
374 struct i2c_adapter *adap = to_i2c_adapter(dev);
375 return sprintf(buf, "%s\n", adap->name);
379 * Let users instantiate I2C devices through sysfs. This can be used when
380 * platform initialization code doesn't contain the proper data for
381 * whatever reason. Also useful for drivers that do device detection and
382 * detection fails, either because the device uses an unexpected address,
383 * or this is a compatible device with different ID register values.
385 * Parameter checking may look overzealous, but we really don't want
386 * the user to provide incorrect parameters.
388 static ssize_t
389 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
390 const char *buf, size_t count)
392 struct i2c_adapter *adap = to_i2c_adapter(dev);
393 struct i2c_board_info info;
394 struct i2c_client *client;
395 char *blank, end;
396 int res;
398 dev_warn(dev, "The new_device interface is still experimental "
399 "and may change in a near future\n");
400 memset(&info, 0, sizeof(struct i2c_board_info));
402 blank = strchr(buf, ' ');
403 if (!blank) {
404 dev_err(dev, "%s: Missing parameters\n", "new_device");
405 return -EINVAL;
407 if (blank - buf > I2C_NAME_SIZE - 1) {
408 dev_err(dev, "%s: Invalid device name\n", "new_device");
409 return -EINVAL;
411 memcpy(info.type, buf, blank - buf);
413 /* Parse remaining parameters, reject extra parameters */
414 res = sscanf(++blank, "%hi%c", &info.addr, &end);
415 if (res < 1) {
416 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
417 return -EINVAL;
419 if (res > 1 && end != '\n') {
420 dev_err(dev, "%s: Extra parameters\n", "new_device");
421 return -EINVAL;
424 if (info.addr < 0x03 || info.addr > 0x77) {
425 dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
426 info.addr);
427 return -EINVAL;
430 client = i2c_new_device(adap, &info);
431 if (!client)
432 return -EEXIST;
434 /* Keep track of the added device */
435 mutex_lock(&core_lock);
436 list_add_tail(&client->detected, &userspace_devices);
437 mutex_unlock(&core_lock);
438 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
439 info.type, info.addr);
441 return count;
445 * And of course let the users delete the devices they instantiated, if
446 * they got it wrong. This interface can only be used to delete devices
447 * instantiated by i2c_sysfs_new_device above. This guarantees that we
448 * don't delete devices to which some kernel code still has references.
450 * Parameter checking may look overzealous, but we really don't want
451 * the user to delete the wrong device.
453 static ssize_t
454 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
455 const char *buf, size_t count)
457 struct i2c_adapter *adap = to_i2c_adapter(dev);
458 struct i2c_client *client, *next;
459 unsigned short addr;
460 char end;
461 int res;
463 /* Parse parameters, reject extra parameters */
464 res = sscanf(buf, "%hi%c", &addr, &end);
465 if (res < 1) {
466 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
467 return -EINVAL;
469 if (res > 1 && end != '\n') {
470 dev_err(dev, "%s: Extra parameters\n", "delete_device");
471 return -EINVAL;
474 /* Make sure the device was added through sysfs */
475 res = -ENOENT;
476 mutex_lock(&core_lock);
477 list_for_each_entry_safe(client, next, &userspace_devices, detected) {
478 if (client->addr == addr && client->adapter == adap) {
479 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
480 "delete_device", client->name, client->addr);
482 list_del(&client->detected);
483 i2c_unregister_device(client);
484 res = count;
485 break;
488 mutex_unlock(&core_lock);
490 if (res < 0)
491 dev_err(dev, "%s: Can't find device in list\n",
492 "delete_device");
493 return res;
496 static struct device_attribute i2c_adapter_attrs[] = {
497 __ATTR(name, S_IRUGO, show_adapter_name, NULL),
498 __ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device),
499 __ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device),
500 { },
503 static struct class i2c_adapter_class = {
504 .owner = THIS_MODULE,
505 .name = "i2c-adapter",
506 .dev_attrs = i2c_adapter_attrs,
509 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
511 struct i2c_devinfo *devinfo;
513 down_read(&__i2c_board_lock);
514 list_for_each_entry(devinfo, &__i2c_board_list, list) {
515 if (devinfo->busnum == adapter->nr
516 && !i2c_new_device(adapter,
517 &devinfo->board_info))
518 dev_err(&adapter->dev,
519 "Can't create device at 0x%02x\n",
520 devinfo->board_info.addr);
522 up_read(&__i2c_board_lock);
525 static int i2c_do_add_adapter(struct device_driver *d, void *data)
527 struct i2c_driver *driver = to_i2c_driver(d);
528 struct i2c_adapter *adap = data;
530 /* Detect supported devices on that bus, and instantiate them */
531 i2c_detect(adap, driver);
533 /* Let legacy drivers scan this bus for matching devices */
534 if (driver->attach_adapter) {
535 /* We ignore the return code; if it fails, too bad */
536 driver->attach_adapter(adap);
538 return 0;
541 static int i2c_register_adapter(struct i2c_adapter *adap)
543 int res = 0, dummy;
545 /* Can't register until after driver model init */
546 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
547 res = -EAGAIN;
548 goto out_list;
551 mutex_init(&adap->bus_lock);
553 /* Set default timeout to 1 second if not already set */
554 if (adap->timeout == 0)
555 adap->timeout = HZ;
557 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
558 adap->dev.release = &i2c_adapter_dev_release;
559 adap->dev.class = &i2c_adapter_class;
560 res = device_register(&adap->dev);
561 if (res)
562 goto out_list;
564 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
566 /* create pre-declared device nodes */
567 if (adap->nr < __i2c_first_dynamic_bus_num)
568 i2c_scan_static_board_info(adap);
570 /* Notify drivers */
571 mutex_lock(&core_lock);
572 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
573 i2c_do_add_adapter);
574 mutex_unlock(&core_lock);
576 return 0;
578 out_list:
579 mutex_lock(&core_lock);
580 idr_remove(&i2c_adapter_idr, adap->nr);
581 mutex_unlock(&core_lock);
582 return res;
586 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
587 * @adapter: the adapter to add
588 * Context: can sleep
590 * This routine is used to declare an I2C adapter when its bus number
591 * doesn't matter. Examples: for I2C adapters dynamically added by
592 * USB links or PCI plugin cards.
594 * When this returns zero, a new bus number was allocated and stored
595 * in adap->nr, and the specified adapter became available for clients.
596 * Otherwise, a negative errno value is returned.
598 int i2c_add_adapter(struct i2c_adapter *adapter)
600 int id, res = 0;
602 retry:
603 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
604 return -ENOMEM;
606 mutex_lock(&core_lock);
607 /* "above" here means "above or equal to", sigh */
608 res = idr_get_new_above(&i2c_adapter_idr, adapter,
609 __i2c_first_dynamic_bus_num, &id);
610 mutex_unlock(&core_lock);
612 if (res < 0) {
613 if (res == -EAGAIN)
614 goto retry;
615 return res;
618 adapter->nr = id;
619 return i2c_register_adapter(adapter);
621 EXPORT_SYMBOL(i2c_add_adapter);
624 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
625 * @adap: the adapter to register (with adap->nr initialized)
626 * Context: can sleep
628 * This routine is used to declare an I2C adapter when its bus number
629 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
630 * or otherwise built in to the system's mainboard, and where i2c_board_info
631 * is used to properly configure I2C devices.
633 * If no devices have pre-been declared for this bus, then be sure to
634 * register the adapter before any dynamically allocated ones. Otherwise
635 * the required bus ID may not be available.
637 * When this returns zero, the specified adapter became available for
638 * clients using the bus number provided in adap->nr. Also, the table
639 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
640 * and the appropriate driver model device nodes are created. Otherwise, a
641 * negative errno value is returned.
643 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
645 int id;
646 int status;
648 if (adap->nr & ~MAX_ID_MASK)
649 return -EINVAL;
651 retry:
652 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
653 return -ENOMEM;
655 mutex_lock(&core_lock);
656 /* "above" here means "above or equal to", sigh;
657 * we need the "equal to" result to force the result
659 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
660 if (status == 0 && id != adap->nr) {
661 status = -EBUSY;
662 idr_remove(&i2c_adapter_idr, id);
664 mutex_unlock(&core_lock);
665 if (status == -EAGAIN)
666 goto retry;
668 if (status == 0)
669 status = i2c_register_adapter(adap);
670 return status;
672 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
674 static int i2c_do_del_adapter(struct device_driver *d, void *data)
676 struct i2c_driver *driver = to_i2c_driver(d);
677 struct i2c_adapter *adapter = data;
678 struct i2c_client *client, *_n;
679 int res;
681 /* Remove the devices we created ourselves as the result of hardware
682 * probing (using a driver's detect method) */
683 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
684 if (client->adapter == adapter) {
685 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
686 client->name, client->addr);
687 list_del(&client->detected);
688 i2c_unregister_device(client);
692 if (!driver->detach_adapter)
693 return 0;
694 res = driver->detach_adapter(adapter);
695 if (res)
696 dev_err(&adapter->dev, "detach_adapter failed (%d) "
697 "for driver [%s]\n", res, driver->driver.name);
698 return res;
701 static int __unregister_client(struct device *dev, void *dummy)
703 struct i2c_client *client = i2c_verify_client(dev);
704 if (client)
705 i2c_unregister_device(client);
706 return 0;
710 * i2c_del_adapter - unregister I2C adapter
711 * @adap: the adapter being unregistered
712 * Context: can sleep
714 * This unregisters an I2C adapter which was previously registered
715 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
717 int i2c_del_adapter(struct i2c_adapter *adap)
719 int res = 0;
720 struct i2c_adapter *found;
722 /* First make sure that this adapter was ever added */
723 mutex_lock(&core_lock);
724 found = idr_find(&i2c_adapter_idr, adap->nr);
725 mutex_unlock(&core_lock);
726 if (found != adap) {
727 pr_debug("i2c-core: attempting to delete unregistered "
728 "adapter [%s]\n", adap->name);
729 return -EINVAL;
732 /* Tell drivers about this removal */
733 mutex_lock(&core_lock);
734 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
735 i2c_do_del_adapter);
736 mutex_unlock(&core_lock);
737 if (res)
738 return res;
740 /* Detach any active clients. This can't fail, thus we do not
741 checking the returned value. */
742 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
744 /* clean up the sysfs representation */
745 init_completion(&adap->dev_released);
746 device_unregister(&adap->dev);
748 /* wait for sysfs to drop all references */
749 wait_for_completion(&adap->dev_released);
751 /* free bus id */
752 mutex_lock(&core_lock);
753 idr_remove(&i2c_adapter_idr, adap->nr);
754 mutex_unlock(&core_lock);
756 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
758 /* Clear the device structure in case this adapter is ever going to be
759 added again */
760 memset(&adap->dev, 0, sizeof(adap->dev));
762 return 0;
764 EXPORT_SYMBOL(i2c_del_adapter);
767 /* ------------------------------------------------------------------------- */
769 static int __attach_adapter(struct device *dev, void *data)
771 struct i2c_adapter *adapter = to_i2c_adapter(dev);
772 struct i2c_driver *driver = data;
774 i2c_detect(adapter, driver);
776 /* Legacy drivers scan i2c busses directly */
777 if (driver->attach_adapter)
778 driver->attach_adapter(adapter);
780 return 0;
784 * An i2c_driver is used with one or more i2c_client (device) nodes to access
785 * i2c slave chips, on a bus instance associated with some i2c_adapter.
788 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
790 int res;
792 /* Can't register until after driver model init */
793 if (unlikely(WARN_ON(!i2c_bus_type.p)))
794 return -EAGAIN;
796 /* add the driver to the list of i2c drivers in the driver core */
797 driver->driver.owner = owner;
798 driver->driver.bus = &i2c_bus_type;
800 /* When registration returns, the driver core
801 * will have called probe() for all matching-but-unbound devices.
803 res = driver_register(&driver->driver);
804 if (res)
805 return res;
807 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
809 INIT_LIST_HEAD(&driver->clients);
810 /* Walk the adapters that are already present */
811 mutex_lock(&core_lock);
812 class_for_each_device(&i2c_adapter_class, NULL, driver,
813 __attach_adapter);
814 mutex_unlock(&core_lock);
816 return 0;
818 EXPORT_SYMBOL(i2c_register_driver);
820 static int __detach_adapter(struct device *dev, void *data)
822 struct i2c_adapter *adapter = to_i2c_adapter(dev);
823 struct i2c_driver *driver = data;
824 struct i2c_client *client, *_n;
826 /* Remove the devices we created ourselves as the result of hardware
827 * probing (using a driver's detect method) */
828 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
829 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
830 client->name, client->addr);
831 list_del(&client->detected);
832 i2c_unregister_device(client);
835 if (driver->detach_adapter) {
836 if (driver->detach_adapter(adapter))
837 dev_err(&adapter->dev,
838 "detach_adapter failed for driver [%s]\n",
839 driver->driver.name);
842 return 0;
846 * i2c_del_driver - unregister I2C driver
847 * @driver: the driver being unregistered
848 * Context: can sleep
850 void i2c_del_driver(struct i2c_driver *driver)
852 mutex_lock(&core_lock);
853 class_for_each_device(&i2c_adapter_class, NULL, driver,
854 __detach_adapter);
855 mutex_unlock(&core_lock);
857 driver_unregister(&driver->driver);
858 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
860 EXPORT_SYMBOL(i2c_del_driver);
862 /* ------------------------------------------------------------------------- */
864 static int __i2c_check_addr(struct device *dev, void *addrp)
866 struct i2c_client *client = i2c_verify_client(dev);
867 int addr = *(int *)addrp;
869 if (client && client->addr == addr)
870 return -EBUSY;
871 return 0;
874 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
876 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
880 * i2c_use_client - increments the reference count of the i2c client structure
881 * @client: the client being referenced
883 * Each live reference to a client should be refcounted. The driver model does
884 * that automatically as part of driver binding, so that most drivers don't
885 * need to do this explicitly: they hold a reference until they're unbound
886 * from the device.
888 * A pointer to the client with the incremented reference counter is returned.
890 struct i2c_client *i2c_use_client(struct i2c_client *client)
892 if (client && get_device(&client->dev))
893 return client;
894 return NULL;
896 EXPORT_SYMBOL(i2c_use_client);
899 * i2c_release_client - release a use of the i2c client structure
900 * @client: the client being no longer referenced
902 * Must be called when a user of a client is finished with it.
904 void i2c_release_client(struct i2c_client *client)
906 if (client)
907 put_device(&client->dev);
909 EXPORT_SYMBOL(i2c_release_client);
911 struct i2c_cmd_arg {
912 unsigned cmd;
913 void *arg;
916 static int i2c_cmd(struct device *dev, void *_arg)
918 struct i2c_client *client = i2c_verify_client(dev);
919 struct i2c_cmd_arg *arg = _arg;
921 if (client && client->driver && client->driver->command)
922 client->driver->command(client, arg->cmd, arg->arg);
923 return 0;
926 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
928 struct i2c_cmd_arg cmd_arg;
930 cmd_arg.cmd = cmd;
931 cmd_arg.arg = arg;
932 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
934 EXPORT_SYMBOL(i2c_clients_command);
936 static int __init i2c_init(void)
938 int retval;
940 retval = bus_register(&i2c_bus_type);
941 if (retval)
942 return retval;
943 retval = class_register(&i2c_adapter_class);
944 if (retval)
945 goto bus_err;
946 retval = i2c_add_driver(&dummy_driver);
947 if (retval)
948 goto class_err;
949 return 0;
951 class_err:
952 class_unregister(&i2c_adapter_class);
953 bus_err:
954 bus_unregister(&i2c_bus_type);
955 return retval;
958 static void __exit i2c_exit(void)
960 i2c_del_driver(&dummy_driver);
961 class_unregister(&i2c_adapter_class);
962 bus_unregister(&i2c_bus_type);
965 /* We must initialize early, because some subsystems register i2c drivers
966 * in subsys_initcall() code, but are linked (and initialized) before i2c.
968 postcore_initcall(i2c_init);
969 module_exit(i2c_exit);
971 /* ----------------------------------------------------
972 * the functional interface to the i2c busses.
973 * ----------------------------------------------------
977 * i2c_transfer - execute a single or combined I2C message
978 * @adap: Handle to I2C bus
979 * @msgs: One or more messages to execute before STOP is issued to
980 * terminate the operation; each message begins with a START.
981 * @num: Number of messages to be executed.
983 * Returns negative errno, else the number of messages executed.
985 * Note that there is no requirement that each message be sent to
986 * the same slave address, although that is the most common model.
988 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
990 unsigned long orig_jiffies;
991 int ret, try;
993 /* REVISIT the fault reporting model here is weak:
995 * - When we get an error after receiving N bytes from a slave,
996 * there is no way to report "N".
998 * - When we get a NAK after transmitting N bytes to a slave,
999 * there is no way to report "N" ... or to let the master
1000 * continue executing the rest of this combined message, if
1001 * that's the appropriate response.
1003 * - When for example "num" is two and we successfully complete
1004 * the first message but get an error part way through the
1005 * second, it's unclear whether that should be reported as
1006 * one (discarding status on the second message) or errno
1007 * (discarding status on the first one).
1010 if (adap->algo->master_xfer) {
1011 #ifdef DEBUG
1012 for (ret = 0; ret < num; ret++) {
1013 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1014 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1015 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1016 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1018 #endif
1020 if (in_atomic() || irqs_disabled()) {
1021 ret = mutex_trylock(&adap->bus_lock);
1022 if (!ret)
1023 /* I2C activity is ongoing. */
1024 return -EAGAIN;
1025 } else {
1026 mutex_lock_nested(&adap->bus_lock, adap->level);
1029 /* Retry automatically on arbitration loss */
1030 orig_jiffies = jiffies;
1031 for (ret = 0, try = 0; try <= adap->retries; try++) {
1032 ret = adap->algo->master_xfer(adap, msgs, num);
1033 if (ret != -EAGAIN)
1034 break;
1035 if (time_after(jiffies, orig_jiffies + adap->timeout))
1036 break;
1038 mutex_unlock(&adap->bus_lock);
1040 return ret;
1041 } else {
1042 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1043 return -EOPNOTSUPP;
1046 EXPORT_SYMBOL(i2c_transfer);
1049 * i2c_master_send - issue a single I2C message in master transmit mode
1050 * @client: Handle to slave device
1051 * @buf: Data that will be written to the slave
1052 * @count: How many bytes to write
1054 * Returns negative errno, or else the number of bytes written.
1056 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1058 int ret;
1059 struct i2c_adapter *adap=client->adapter;
1060 struct i2c_msg msg;
1062 msg.addr = client->addr;
1063 msg.flags = client->flags & I2C_M_TEN;
1064 msg.len = count;
1065 msg.buf = (char *)buf;
1067 ret = i2c_transfer(adap, &msg, 1);
1069 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1070 transmitted, else error code. */
1071 return (ret == 1) ? count : ret;
1073 EXPORT_SYMBOL(i2c_master_send);
1076 * i2c_master_recv - issue a single I2C message in master receive mode
1077 * @client: Handle to slave device
1078 * @buf: Where to store data read from slave
1079 * @count: How many bytes to read
1081 * Returns negative errno, or else the number of bytes read.
1083 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1085 struct i2c_adapter *adap=client->adapter;
1086 struct i2c_msg msg;
1087 int ret;
1089 msg.addr = client->addr;
1090 msg.flags = client->flags & I2C_M_TEN;
1091 msg.flags |= I2C_M_RD;
1092 msg.len = count;
1093 msg.buf = buf;
1095 ret = i2c_transfer(adap, &msg, 1);
1097 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1098 transmitted, else error code. */
1099 return (ret == 1) ? count : ret;
1101 EXPORT_SYMBOL(i2c_master_recv);
1103 /* ----------------------------------------------------
1104 * the i2c address scanning function
1105 * Will not work for 10-bit addresses!
1106 * ----------------------------------------------------
1109 static int i2c_detect_address(struct i2c_client *temp_client, int kind,
1110 struct i2c_driver *driver)
1112 struct i2c_board_info info;
1113 struct i2c_adapter *adapter = temp_client->adapter;
1114 int addr = temp_client->addr;
1115 int err;
1117 /* Make sure the address is valid */
1118 if (addr < 0x03 || addr > 0x77) {
1119 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1120 addr);
1121 return -EINVAL;
1124 /* Skip if already in use */
1125 if (i2c_check_addr(adapter, addr))
1126 return 0;
1128 /* Make sure there is something at this address, unless forced */
1129 if (kind < 0) {
1130 if (i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1131 I2C_SMBUS_QUICK, NULL) < 0)
1132 return 0;
1134 /* prevent 24RF08 corruption */
1135 if ((addr & ~0x0f) == 0x50)
1136 i2c_smbus_xfer(adapter, addr, 0, 0, 0,
1137 I2C_SMBUS_QUICK, NULL);
1140 /* Finally call the custom detection function */
1141 memset(&info, 0, sizeof(struct i2c_board_info));
1142 info.addr = addr;
1143 err = driver->detect(temp_client, kind, &info);
1144 if (err) {
1145 /* -ENODEV is returned if the detection fails. We catch it
1146 here as this isn't an error. */
1147 return err == -ENODEV ? 0 : err;
1150 /* Consistency check */
1151 if (info.type[0] == '\0') {
1152 dev_err(&adapter->dev, "%s detection function provided "
1153 "no name for 0x%x\n", driver->driver.name,
1154 addr);
1155 } else {
1156 struct i2c_client *client;
1158 /* Detection succeeded, instantiate the device */
1159 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1160 info.type, info.addr);
1161 client = i2c_new_device(adapter, &info);
1162 if (client)
1163 list_add_tail(&client->detected, &driver->clients);
1164 else
1165 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1166 info.type, info.addr);
1168 return 0;
1171 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1173 const struct i2c_client_address_data *address_data;
1174 struct i2c_client *temp_client;
1175 int i, err = 0;
1176 int adap_id = i2c_adapter_id(adapter);
1178 address_data = driver->address_data;
1179 if (!driver->detect || !address_data)
1180 return 0;
1182 /* Set up a temporary client to help detect callback */
1183 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1184 if (!temp_client)
1185 return -ENOMEM;
1186 temp_client->adapter = adapter;
1188 /* Force entries are done first, and are not affected by ignore
1189 entries */
1190 if (address_data->forces) {
1191 const unsigned short * const *forces = address_data->forces;
1192 int kind;
1194 for (kind = 0; forces[kind]; kind++) {
1195 for (i = 0; forces[kind][i] != I2C_CLIENT_END;
1196 i += 2) {
1197 if (forces[kind][i] == adap_id
1198 || forces[kind][i] == ANY_I2C_BUS) {
1199 dev_dbg(&adapter->dev, "found force "
1200 "parameter for adapter %d, "
1201 "addr 0x%02x, kind %d\n",
1202 adap_id, forces[kind][i + 1],
1203 kind);
1204 temp_client->addr = forces[kind][i + 1];
1205 err = i2c_detect_address(temp_client,
1206 kind, driver);
1207 if (err)
1208 goto exit_free;
1214 /* Stop here if the classes do not match */
1215 if (!(adapter->class & driver->class))
1216 goto exit_free;
1218 /* Stop here if we can't use SMBUS_QUICK */
1219 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1220 if (address_data->probe[0] == I2C_CLIENT_END
1221 && address_data->normal_i2c[0] == I2C_CLIENT_END)
1222 goto exit_free;
1224 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1225 "can't probe for chips\n");
1226 err = -EOPNOTSUPP;
1227 goto exit_free;
1230 /* Probe entries are done second, and are not affected by ignore
1231 entries either */
1232 for (i = 0; address_data->probe[i] != I2C_CLIENT_END; i += 2) {
1233 if (address_data->probe[i] == adap_id
1234 || address_data->probe[i] == ANY_I2C_BUS) {
1235 dev_dbg(&adapter->dev, "found probe parameter for "
1236 "adapter %d, addr 0x%02x\n", adap_id,
1237 address_data->probe[i + 1]);
1238 temp_client->addr = address_data->probe[i + 1];
1239 err = i2c_detect_address(temp_client, -1, driver);
1240 if (err)
1241 goto exit_free;
1245 /* Normal entries are done last, unless shadowed by an ignore entry */
1246 for (i = 0; address_data->normal_i2c[i] != I2C_CLIENT_END; i += 1) {
1247 int j, ignore;
1249 ignore = 0;
1250 for (j = 0; address_data->ignore[j] != I2C_CLIENT_END;
1251 j += 2) {
1252 if ((address_data->ignore[j] == adap_id ||
1253 address_data->ignore[j] == ANY_I2C_BUS)
1254 && address_data->ignore[j + 1]
1255 == address_data->normal_i2c[i]) {
1256 dev_dbg(&adapter->dev, "found ignore "
1257 "parameter for adapter %d, "
1258 "addr 0x%02x\n", adap_id,
1259 address_data->ignore[j + 1]);
1260 ignore = 1;
1261 break;
1264 if (ignore)
1265 continue;
1267 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1268 "addr 0x%02x\n", adap_id,
1269 address_data->normal_i2c[i]);
1270 temp_client->addr = address_data->normal_i2c[i];
1271 err = i2c_detect_address(temp_client, -1, driver);
1272 if (err)
1273 goto exit_free;
1276 exit_free:
1277 kfree(temp_client);
1278 return err;
1281 struct i2c_client *
1282 i2c_new_probed_device(struct i2c_adapter *adap,
1283 struct i2c_board_info *info,
1284 unsigned short const *addr_list)
1286 int i;
1288 /* Stop here if the bus doesn't support probing */
1289 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1290 dev_err(&adap->dev, "Probing not supported\n");
1291 return NULL;
1294 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1295 /* Check address validity */
1296 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1297 dev_warn(&adap->dev, "Invalid 7-bit address "
1298 "0x%02x\n", addr_list[i]);
1299 continue;
1302 /* Check address availability */
1303 if (i2c_check_addr(adap, addr_list[i])) {
1304 dev_dbg(&adap->dev, "Address 0x%02x already in "
1305 "use, not probing\n", addr_list[i]);
1306 continue;
1309 /* Test address responsiveness
1310 The default probe method is a quick write, but it is known
1311 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1312 and could also irreversibly write-protect some EEPROMs, so
1313 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1314 read instead. Also, some bus drivers don't implement
1315 quick write, so we fallback to a byte read it that case
1316 too. */
1317 if ((addr_list[i] & ~0x07) == 0x30
1318 || (addr_list[i] & ~0x0f) == 0x50
1319 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1320 union i2c_smbus_data data;
1322 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1323 I2C_SMBUS_READ, 0,
1324 I2C_SMBUS_BYTE, &data) >= 0)
1325 break;
1326 } else {
1327 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1328 I2C_SMBUS_WRITE, 0,
1329 I2C_SMBUS_QUICK, NULL) >= 0)
1330 break;
1334 if (addr_list[i] == I2C_CLIENT_END) {
1335 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1336 return NULL;
1339 info->addr = addr_list[i];
1340 return i2c_new_device(adap, info);
1342 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1344 struct i2c_adapter* i2c_get_adapter(int id)
1346 struct i2c_adapter *adapter;
1348 mutex_lock(&core_lock);
1349 adapter = idr_find(&i2c_adapter_idr, id);
1350 if (adapter && !try_module_get(adapter->owner))
1351 adapter = NULL;
1353 mutex_unlock(&core_lock);
1354 return adapter;
1356 EXPORT_SYMBOL(i2c_get_adapter);
1358 void i2c_put_adapter(struct i2c_adapter *adap)
1360 module_put(adap->owner);
1362 EXPORT_SYMBOL(i2c_put_adapter);
1364 /* The SMBus parts */
1366 #define POLY (0x1070U << 3)
1367 static u8 crc8(u16 data)
1369 int i;
1371 for(i = 0; i < 8; i++) {
1372 if (data & 0x8000)
1373 data = data ^ POLY;
1374 data = data << 1;
1376 return (u8)(data >> 8);
1379 /* Incremental CRC8 over count bytes in the array pointed to by p */
1380 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1382 int i;
1384 for(i = 0; i < count; i++)
1385 crc = crc8((crc ^ p[i]) << 8);
1386 return crc;
1389 /* Assume a 7-bit address, which is reasonable for SMBus */
1390 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1392 /* The address will be sent first */
1393 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1394 pec = i2c_smbus_pec(pec, &addr, 1);
1396 /* The data buffer follows */
1397 return i2c_smbus_pec(pec, msg->buf, msg->len);
1400 /* Used for write only transactions */
1401 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1403 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1404 msg->len++;
1407 /* Return <0 on CRC error
1408 If there was a write before this read (most cases) we need to take the
1409 partial CRC from the write part into account.
1410 Note that this function does modify the message (we need to decrease the
1411 message length to hide the CRC byte from the caller). */
1412 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1414 u8 rpec = msg->buf[--msg->len];
1415 cpec = i2c_smbus_msg_pec(cpec, msg);
1417 if (rpec != cpec) {
1418 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1419 rpec, cpec);
1420 return -EBADMSG;
1422 return 0;
1426 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1427 * @client: Handle to slave device
1429 * This executes the SMBus "receive byte" protocol, returning negative errno
1430 * else the byte received from the device.
1432 s32 i2c_smbus_read_byte(struct i2c_client *client)
1434 union i2c_smbus_data data;
1435 int status;
1437 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1438 I2C_SMBUS_READ, 0,
1439 I2C_SMBUS_BYTE, &data);
1440 return (status < 0) ? status : data.byte;
1442 EXPORT_SYMBOL(i2c_smbus_read_byte);
1445 * i2c_smbus_write_byte - SMBus "send byte" protocol
1446 * @client: Handle to slave device
1447 * @value: Byte to be sent
1449 * This executes the SMBus "send byte" protocol, returning negative errno
1450 * else zero on success.
1452 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1454 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1455 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1457 EXPORT_SYMBOL(i2c_smbus_write_byte);
1460 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1461 * @client: Handle to slave device
1462 * @command: Byte interpreted by slave
1464 * This executes the SMBus "read byte" protocol, returning negative errno
1465 * else a data byte received from the device.
1467 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1469 union i2c_smbus_data data;
1470 int status;
1472 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1473 I2C_SMBUS_READ, command,
1474 I2C_SMBUS_BYTE_DATA, &data);
1475 return (status < 0) ? status : data.byte;
1477 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1480 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1481 * @client: Handle to slave device
1482 * @command: Byte interpreted by slave
1483 * @value: Byte being written
1485 * This executes the SMBus "write byte" protocol, returning negative errno
1486 * else zero on success.
1488 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1490 union i2c_smbus_data data;
1491 data.byte = value;
1492 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1493 I2C_SMBUS_WRITE,command,
1494 I2C_SMBUS_BYTE_DATA,&data);
1496 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1499 * i2c_smbus_read_word_data - SMBus "read word" protocol
1500 * @client: Handle to slave device
1501 * @command: Byte interpreted by slave
1503 * This executes the SMBus "read word" protocol, returning negative errno
1504 * else a 16-bit unsigned "word" received from the device.
1506 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1508 union i2c_smbus_data data;
1509 int status;
1511 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1512 I2C_SMBUS_READ, command,
1513 I2C_SMBUS_WORD_DATA, &data);
1514 return (status < 0) ? status : data.word;
1516 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1519 * i2c_smbus_write_word_data - SMBus "write word" protocol
1520 * @client: Handle to slave device
1521 * @command: Byte interpreted by slave
1522 * @value: 16-bit "word" being written
1524 * This executes the SMBus "write word" protocol, returning negative errno
1525 * else zero on success.
1527 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1529 union i2c_smbus_data data;
1530 data.word = value;
1531 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1532 I2C_SMBUS_WRITE,command,
1533 I2C_SMBUS_WORD_DATA,&data);
1535 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1538 * i2c_smbus_process_call - SMBus "process call" protocol
1539 * @client: Handle to slave device
1540 * @command: Byte interpreted by slave
1541 * @value: 16-bit "word" being written
1543 * This executes the SMBus "process call" protocol, returning negative errno
1544 * else a 16-bit unsigned "word" received from the device.
1546 s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1548 union i2c_smbus_data data;
1549 int status;
1550 data.word = value;
1552 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1553 I2C_SMBUS_WRITE, command,
1554 I2C_SMBUS_PROC_CALL, &data);
1555 return (status < 0) ? status : data.word;
1557 EXPORT_SYMBOL(i2c_smbus_process_call);
1560 * i2c_smbus_read_block_data - SMBus "block read" protocol
1561 * @client: Handle to slave device
1562 * @command: Byte interpreted by slave
1563 * @values: Byte array into which data will be read; big enough to hold
1564 * the data returned by the slave. SMBus allows at most 32 bytes.
1566 * This executes the SMBus "block read" protocol, returning negative errno
1567 * else the number of data bytes in the slave's response.
1569 * Note that using this function requires that the client's adapter support
1570 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1571 * support this; its emulation through I2C messaging relies on a specific
1572 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1574 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1575 u8 *values)
1577 union i2c_smbus_data data;
1578 int status;
1580 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1581 I2C_SMBUS_READ, command,
1582 I2C_SMBUS_BLOCK_DATA, &data);
1583 if (status)
1584 return status;
1586 memcpy(values, &data.block[1], data.block[0]);
1587 return data.block[0];
1589 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1592 * i2c_smbus_write_block_data - SMBus "block write" protocol
1593 * @client: Handle to slave device
1594 * @command: Byte interpreted by slave
1595 * @length: Size of data block; SMBus allows at most 32 bytes
1596 * @values: Byte array which will be written.
1598 * This executes the SMBus "block write" protocol, returning negative errno
1599 * else zero on success.
1601 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1602 u8 length, const u8 *values)
1604 union i2c_smbus_data data;
1606 if (length > I2C_SMBUS_BLOCK_MAX)
1607 length = I2C_SMBUS_BLOCK_MAX;
1608 data.block[0] = length;
1609 memcpy(&data.block[1], values, length);
1610 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1611 I2C_SMBUS_WRITE,command,
1612 I2C_SMBUS_BLOCK_DATA,&data);
1614 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1616 /* Returns the number of read bytes */
1617 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1618 u8 length, u8 *values)
1620 union i2c_smbus_data data;
1621 int status;
1623 if (length > I2C_SMBUS_BLOCK_MAX)
1624 length = I2C_SMBUS_BLOCK_MAX;
1625 data.block[0] = length;
1626 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1627 I2C_SMBUS_READ, command,
1628 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1629 if (status < 0)
1630 return status;
1632 memcpy(values, &data.block[1], data.block[0]);
1633 return data.block[0];
1635 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1637 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1638 u8 length, const u8 *values)
1640 union i2c_smbus_data data;
1642 if (length > I2C_SMBUS_BLOCK_MAX)
1643 length = I2C_SMBUS_BLOCK_MAX;
1644 data.block[0] = length;
1645 memcpy(data.block + 1, values, length);
1646 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1647 I2C_SMBUS_WRITE, command,
1648 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1650 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1652 /* Simulate a SMBus command using the i2c protocol
1653 No checking of parameters is done! */
1654 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1655 unsigned short flags,
1656 char read_write, u8 command, int size,
1657 union i2c_smbus_data * data)
1659 /* So we need to generate a series of msgs. In the case of writing, we
1660 need to use only one message; when reading, we need two. We initialize
1661 most things with sane defaults, to keep the code below somewhat
1662 simpler. */
1663 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1664 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1665 int num = read_write == I2C_SMBUS_READ?2:1;
1666 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1667 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1669 int i;
1670 u8 partial_pec = 0;
1671 int status;
1673 msgbuf0[0] = command;
1674 switch(size) {
1675 case I2C_SMBUS_QUICK:
1676 msg[0].len = 0;
1677 /* Special case: The read/write field is used as data */
1678 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1679 I2C_M_RD : 0);
1680 num = 1;
1681 break;
1682 case I2C_SMBUS_BYTE:
1683 if (read_write == I2C_SMBUS_READ) {
1684 /* Special case: only a read! */
1685 msg[0].flags = I2C_M_RD | flags;
1686 num = 1;
1688 break;
1689 case I2C_SMBUS_BYTE_DATA:
1690 if (read_write == I2C_SMBUS_READ)
1691 msg[1].len = 1;
1692 else {
1693 msg[0].len = 2;
1694 msgbuf0[1] = data->byte;
1696 break;
1697 case I2C_SMBUS_WORD_DATA:
1698 if (read_write == I2C_SMBUS_READ)
1699 msg[1].len = 2;
1700 else {
1701 msg[0].len=3;
1702 msgbuf0[1] = data->word & 0xff;
1703 msgbuf0[2] = data->word >> 8;
1705 break;
1706 case I2C_SMBUS_PROC_CALL:
1707 num = 2; /* Special case */
1708 read_write = I2C_SMBUS_READ;
1709 msg[0].len = 3;
1710 msg[1].len = 2;
1711 msgbuf0[1] = data->word & 0xff;
1712 msgbuf0[2] = data->word >> 8;
1713 break;
1714 case I2C_SMBUS_BLOCK_DATA:
1715 if (read_write == I2C_SMBUS_READ) {
1716 msg[1].flags |= I2C_M_RECV_LEN;
1717 msg[1].len = 1; /* block length will be added by
1718 the underlying bus driver */
1719 } else {
1720 msg[0].len = data->block[0] + 2;
1721 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1722 dev_err(&adapter->dev,
1723 "Invalid block write size %d\n",
1724 data->block[0]);
1725 return -EINVAL;
1727 for (i = 1; i < msg[0].len; i++)
1728 msgbuf0[i] = data->block[i-1];
1730 break;
1731 case I2C_SMBUS_BLOCK_PROC_CALL:
1732 num = 2; /* Another special case */
1733 read_write = I2C_SMBUS_READ;
1734 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1735 dev_err(&adapter->dev,
1736 "Invalid block write size %d\n",
1737 data->block[0]);
1738 return -EINVAL;
1740 msg[0].len = data->block[0] + 2;
1741 for (i = 1; i < msg[0].len; i++)
1742 msgbuf0[i] = data->block[i-1];
1743 msg[1].flags |= I2C_M_RECV_LEN;
1744 msg[1].len = 1; /* block length will be added by
1745 the underlying bus driver */
1746 break;
1747 case I2C_SMBUS_I2C_BLOCK_DATA:
1748 if (read_write == I2C_SMBUS_READ) {
1749 msg[1].len = data->block[0];
1750 } else {
1751 msg[0].len = data->block[0] + 1;
1752 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1753 dev_err(&adapter->dev,
1754 "Invalid block write size %d\n",
1755 data->block[0]);
1756 return -EINVAL;
1758 for (i = 1; i <= data->block[0]; i++)
1759 msgbuf0[i] = data->block[i];
1761 break;
1762 default:
1763 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1764 return -EOPNOTSUPP;
1767 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1768 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1769 if (i) {
1770 /* Compute PEC if first message is a write */
1771 if (!(msg[0].flags & I2C_M_RD)) {
1772 if (num == 1) /* Write only */
1773 i2c_smbus_add_pec(&msg[0]);
1774 else /* Write followed by read */
1775 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1777 /* Ask for PEC if last message is a read */
1778 if (msg[num-1].flags & I2C_M_RD)
1779 msg[num-1].len++;
1782 status = i2c_transfer(adapter, msg, num);
1783 if (status < 0)
1784 return status;
1786 /* Check PEC if last message is a read */
1787 if (i && (msg[num-1].flags & I2C_M_RD)) {
1788 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1789 if (status < 0)
1790 return status;
1793 if (read_write == I2C_SMBUS_READ)
1794 switch(size) {
1795 case I2C_SMBUS_BYTE:
1796 data->byte = msgbuf0[0];
1797 break;
1798 case I2C_SMBUS_BYTE_DATA:
1799 data->byte = msgbuf1[0];
1800 break;
1801 case I2C_SMBUS_WORD_DATA:
1802 case I2C_SMBUS_PROC_CALL:
1803 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1804 break;
1805 case I2C_SMBUS_I2C_BLOCK_DATA:
1806 for (i = 0; i < data->block[0]; i++)
1807 data->block[i+1] = msgbuf1[i];
1808 break;
1809 case I2C_SMBUS_BLOCK_DATA:
1810 case I2C_SMBUS_BLOCK_PROC_CALL:
1811 for (i = 0; i < msgbuf1[0] + 1; i++)
1812 data->block[i] = msgbuf1[i];
1813 break;
1815 return 0;
1819 * i2c_smbus_xfer - execute SMBus protocol operations
1820 * @adapter: Handle to I2C bus
1821 * @addr: Address of SMBus slave on that bus
1822 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1823 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1824 * @command: Byte interpreted by slave, for protocols which use such bytes
1825 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1826 * @data: Data to be read or written
1828 * This executes an SMBus protocol operation, and returns a negative
1829 * errno code else zero on success.
1831 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1832 char read_write, u8 command, int protocol,
1833 union i2c_smbus_data *data)
1835 unsigned long orig_jiffies;
1836 int try;
1837 s32 res;
1839 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1841 if (adapter->algo->smbus_xfer) {
1842 mutex_lock(&adapter->bus_lock);
1844 /* Retry automatically on arbitration loss */
1845 orig_jiffies = jiffies;
1846 for (res = 0, try = 0; try <= adapter->retries; try++) {
1847 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1848 read_write, command,
1849 protocol, data);
1850 if (res != -EAGAIN)
1851 break;
1852 if (time_after(jiffies,
1853 orig_jiffies + adapter->timeout))
1854 break;
1856 mutex_unlock(&adapter->bus_lock);
1857 } else
1858 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1859 command, protocol, data);
1861 return res;
1863 EXPORT_SYMBOL(i2c_smbus_xfer);
1865 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1866 MODULE_DESCRIPTION("I2C-Bus main module");
1867 MODULE_LICENSE("GPL");