smbfs: Push down BKL into ioctl function
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / i2c / i2c-core.c
blobc2258a51fe0ce3bb09055220c645602ab467c876
1 /* i2c-core.c - a device driver for the iic-bus interface */
2 /* ------------------------------------------------------------------------- */
3 /* Copyright (C) 1995-99 Simon G. Vogl
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* ------------------------------------------------------------------------- */
20 /* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>.
21 All SMBus-related things are written by Frodo Looijaard <frodol@dds.nl>
22 SMBus 2.0 support by Mark Studebaker <mdsxyz123@yahoo.com> and
23 Jean Delvare <khali@linux-fr.org> */
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/errno.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30 #include <linux/init.h>
31 #include <linux/idr.h>
32 #include <linux/mutex.h>
33 #include <linux/completion.h>
34 #include <linux/hardirq.h>
35 #include <linux/irqflags.h>
36 #include <linux/rwsem.h>
37 #include <linux/pm_runtime.h>
38 #include <asm/uaccess.h>
40 #include "i2c-core.h"
43 /* core_lock protects i2c_adapter_idr, and guarantees
44 that device detection, deletion of detected devices, and attach_adapter
45 and detach_adapter calls are serialized */
46 static DEFINE_MUTEX(core_lock);
47 static DEFINE_IDR(i2c_adapter_idr);
49 static struct device_type i2c_client_type;
50 static int i2c_check_addr(struct i2c_adapter *adapter, int addr);
51 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver);
53 /* ------------------------------------------------------------------------- */
55 static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id,
56 const struct i2c_client *client)
58 while (id->name[0]) {
59 if (strcmp(client->name, id->name) == 0)
60 return id;
61 id++;
63 return NULL;
66 static int i2c_device_match(struct device *dev, struct device_driver *drv)
68 struct i2c_client *client = i2c_verify_client(dev);
69 struct i2c_driver *driver;
71 if (!client)
72 return 0;
74 driver = to_i2c_driver(drv);
75 /* match on an id table if there is one */
76 if (driver->id_table)
77 return i2c_match_id(driver->id_table, client) != NULL;
79 return 0;
82 #ifdef CONFIG_HOTPLUG
84 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
85 static int i2c_device_uevent(struct device *dev, struct kobj_uevent_env *env)
87 struct i2c_client *client = to_i2c_client(dev);
89 if (add_uevent_var(env, "MODALIAS=%s%s",
90 I2C_MODULE_PREFIX, client->name))
91 return -ENOMEM;
92 dev_dbg(dev, "uevent\n");
93 return 0;
96 #else
97 #define i2c_device_uevent NULL
98 #endif /* CONFIG_HOTPLUG */
100 static int i2c_device_probe(struct device *dev)
102 struct i2c_client *client = i2c_verify_client(dev);
103 struct i2c_driver *driver;
104 int status;
106 if (!client)
107 return 0;
109 driver = to_i2c_driver(dev->driver);
110 if (!driver->probe || !driver->id_table)
111 return -ENODEV;
112 client->driver = driver;
113 if (!device_can_wakeup(&client->dev))
114 device_init_wakeup(&client->dev,
115 client->flags & I2C_CLIENT_WAKE);
116 dev_dbg(dev, "probe\n");
118 status = driver->probe(client, i2c_match_id(driver->id_table, client));
119 if (status) {
120 client->driver = NULL;
121 i2c_set_clientdata(client, NULL);
123 return status;
126 static int i2c_device_remove(struct device *dev)
128 struct i2c_client *client = i2c_verify_client(dev);
129 struct i2c_driver *driver;
130 int status;
132 if (!client || !dev->driver)
133 return 0;
135 driver = to_i2c_driver(dev->driver);
136 if (driver->remove) {
137 dev_dbg(dev, "remove\n");
138 status = driver->remove(client);
139 } else {
140 dev->driver = NULL;
141 status = 0;
143 if (status == 0) {
144 client->driver = NULL;
145 i2c_set_clientdata(client, NULL);
147 return status;
150 static void i2c_device_shutdown(struct device *dev)
152 struct i2c_client *client = i2c_verify_client(dev);
153 struct i2c_driver *driver;
155 if (!client || !dev->driver)
156 return;
157 driver = to_i2c_driver(dev->driver);
158 if (driver->shutdown)
159 driver->shutdown(client);
162 #ifdef CONFIG_SUSPEND
163 static int i2c_device_pm_suspend(struct device *dev)
165 const struct dev_pm_ops *pm;
167 if (!dev->driver)
168 return 0;
169 pm = dev->driver->pm;
170 if (!pm || !pm->suspend)
171 return 0;
172 return pm->suspend(dev);
175 static int i2c_device_pm_resume(struct device *dev)
177 const struct dev_pm_ops *pm;
179 if (!dev->driver)
180 return 0;
181 pm = dev->driver->pm;
182 if (!pm || !pm->resume)
183 return 0;
184 return pm->resume(dev);
186 #else
187 #define i2c_device_pm_suspend NULL
188 #define i2c_device_pm_resume NULL
189 #endif
191 #ifdef CONFIG_PM_RUNTIME
192 static int i2c_device_runtime_suspend(struct device *dev)
194 const struct dev_pm_ops *pm;
196 if (!dev->driver)
197 return 0;
198 pm = dev->driver->pm;
199 if (!pm || !pm->runtime_suspend)
200 return 0;
201 return pm->runtime_suspend(dev);
204 static int i2c_device_runtime_resume(struct device *dev)
206 const struct dev_pm_ops *pm;
208 if (!dev->driver)
209 return 0;
210 pm = dev->driver->pm;
211 if (!pm || !pm->runtime_resume)
212 return 0;
213 return pm->runtime_resume(dev);
216 static int i2c_device_runtime_idle(struct device *dev)
218 const struct dev_pm_ops *pm = NULL;
219 int ret;
221 if (dev->driver)
222 pm = dev->driver->pm;
223 if (pm && pm->runtime_idle) {
224 ret = pm->runtime_idle(dev);
225 if (ret)
226 return ret;
229 return pm_runtime_suspend(dev);
231 #else
232 #define i2c_device_runtime_suspend NULL
233 #define i2c_device_runtime_resume NULL
234 #define i2c_device_runtime_idle NULL
235 #endif
237 static int i2c_device_suspend(struct device *dev, pm_message_t mesg)
239 struct i2c_client *client = i2c_verify_client(dev);
240 struct i2c_driver *driver;
242 if (!client || !dev->driver)
243 return 0;
244 driver = to_i2c_driver(dev->driver);
245 if (!driver->suspend)
246 return 0;
247 return driver->suspend(client, mesg);
250 static int i2c_device_resume(struct device *dev)
252 struct i2c_client *client = i2c_verify_client(dev);
253 struct i2c_driver *driver;
255 if (!client || !dev->driver)
256 return 0;
257 driver = to_i2c_driver(dev->driver);
258 if (!driver->resume)
259 return 0;
260 return driver->resume(client);
263 static void i2c_client_dev_release(struct device *dev)
265 kfree(to_i2c_client(dev));
268 static ssize_t
269 show_name(struct device *dev, struct device_attribute *attr, char *buf)
271 return sprintf(buf, "%s\n", dev->type == &i2c_client_type ?
272 to_i2c_client(dev)->name : to_i2c_adapter(dev)->name);
275 static ssize_t
276 show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
278 struct i2c_client *client = to_i2c_client(dev);
279 return sprintf(buf, "%s%s\n", I2C_MODULE_PREFIX, client->name);
282 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
283 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
285 static struct attribute *i2c_dev_attrs[] = {
286 &dev_attr_name.attr,
287 /* modalias helps coldplug: modprobe $(cat .../modalias) */
288 &dev_attr_modalias.attr,
289 NULL
292 static struct attribute_group i2c_dev_attr_group = {
293 .attrs = i2c_dev_attrs,
296 static const struct attribute_group *i2c_dev_attr_groups[] = {
297 &i2c_dev_attr_group,
298 NULL
301 static const struct dev_pm_ops i2c_device_pm_ops = {
302 .suspend = i2c_device_pm_suspend,
303 .resume = i2c_device_pm_resume,
304 .runtime_suspend = i2c_device_runtime_suspend,
305 .runtime_resume = i2c_device_runtime_resume,
306 .runtime_idle = i2c_device_runtime_idle,
309 struct bus_type i2c_bus_type = {
310 .name = "i2c",
311 .match = i2c_device_match,
312 .probe = i2c_device_probe,
313 .remove = i2c_device_remove,
314 .shutdown = i2c_device_shutdown,
315 .suspend = i2c_device_suspend,
316 .resume = i2c_device_resume,
317 .pm = &i2c_device_pm_ops,
319 EXPORT_SYMBOL_GPL(i2c_bus_type);
321 static struct device_type i2c_client_type = {
322 .groups = i2c_dev_attr_groups,
323 .uevent = i2c_device_uevent,
324 .release = i2c_client_dev_release,
329 * i2c_verify_client - return parameter as i2c_client, or NULL
330 * @dev: device, probably from some driver model iterator
332 * When traversing the driver model tree, perhaps using driver model
333 * iterators like @device_for_each_child(), you can't assume very much
334 * about the nodes you find. Use this function to avoid oopses caused
335 * by wrongly treating some non-I2C device as an i2c_client.
337 struct i2c_client *i2c_verify_client(struct device *dev)
339 return (dev->type == &i2c_client_type)
340 ? to_i2c_client(dev)
341 : NULL;
343 EXPORT_SYMBOL(i2c_verify_client);
347 * i2c_new_device - instantiate an i2c device
348 * @adap: the adapter managing the device
349 * @info: describes one I2C device; bus_num is ignored
350 * Context: can sleep
352 * Create an i2c device. Binding is handled through driver model
353 * probe()/remove() methods. A driver may be bound to this device when we
354 * return from this function, or any later moment (e.g. maybe hotplugging will
355 * load the driver module). This call is not appropriate for use by mainboard
356 * initialization logic, which usually runs during an arch_initcall() long
357 * before any i2c_adapter could exist.
359 * This returns the new i2c client, which may be saved for later use with
360 * i2c_unregister_device(); or NULL to indicate an error.
362 struct i2c_client *
363 i2c_new_device(struct i2c_adapter *adap, struct i2c_board_info const *info)
365 struct i2c_client *client;
366 int status;
368 client = kzalloc(sizeof *client, GFP_KERNEL);
369 if (!client)
370 return NULL;
372 client->adapter = adap;
374 client->dev.platform_data = info->platform_data;
376 if (info->archdata)
377 client->dev.archdata = *info->archdata;
379 client->flags = info->flags;
380 client->addr = info->addr;
381 client->irq = info->irq;
383 strlcpy(client->name, info->type, sizeof(client->name));
385 /* Check for address business */
386 status = i2c_check_addr(adap, client->addr);
387 if (status)
388 goto out_err;
390 client->dev.parent = &client->adapter->dev;
391 client->dev.bus = &i2c_bus_type;
392 client->dev.type = &i2c_client_type;
394 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap),
395 client->addr);
396 status = device_register(&client->dev);
397 if (status)
398 goto out_err;
400 dev_dbg(&adap->dev, "client [%s] registered with bus id %s\n",
401 client->name, dev_name(&client->dev));
403 return client;
405 out_err:
406 dev_err(&adap->dev, "Failed to register i2c client %s at 0x%02x "
407 "(%d)\n", client->name, client->addr, status);
408 kfree(client);
409 return NULL;
411 EXPORT_SYMBOL_GPL(i2c_new_device);
415 * i2c_unregister_device - reverse effect of i2c_new_device()
416 * @client: value returned from i2c_new_device()
417 * Context: can sleep
419 void i2c_unregister_device(struct i2c_client *client)
421 device_unregister(&client->dev);
423 EXPORT_SYMBOL_GPL(i2c_unregister_device);
426 static const struct i2c_device_id dummy_id[] = {
427 { "dummy", 0 },
428 { },
431 static int dummy_probe(struct i2c_client *client,
432 const struct i2c_device_id *id)
434 return 0;
437 static int dummy_remove(struct i2c_client *client)
439 return 0;
442 static struct i2c_driver dummy_driver = {
443 .driver.name = "dummy",
444 .probe = dummy_probe,
445 .remove = dummy_remove,
446 .id_table = dummy_id,
450 * i2c_new_dummy - return a new i2c device bound to a dummy driver
451 * @adapter: the adapter managing the device
452 * @address: seven bit address to be used
453 * Context: can sleep
455 * This returns an I2C client bound to the "dummy" driver, intended for use
456 * with devices that consume multiple addresses. Examples of such chips
457 * include various EEPROMS (like 24c04 and 24c08 models).
459 * These dummy devices have two main uses. First, most I2C and SMBus calls
460 * except i2c_transfer() need a client handle; the dummy will be that handle.
461 * And second, this prevents the specified address from being bound to a
462 * different driver.
464 * This returns the new i2c client, which should be saved for later use with
465 * i2c_unregister_device(); or NULL to indicate an error.
467 struct i2c_client *i2c_new_dummy(struct i2c_adapter *adapter, u16 address)
469 struct i2c_board_info info = {
470 I2C_BOARD_INFO("dummy", address),
473 return i2c_new_device(adapter, &info);
475 EXPORT_SYMBOL_GPL(i2c_new_dummy);
477 /* ------------------------------------------------------------------------- */
479 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
481 static void i2c_adapter_dev_release(struct device *dev)
483 struct i2c_adapter *adap = to_i2c_adapter(dev);
484 complete(&adap->dev_released);
488 * Let users instantiate I2C devices through sysfs. This can be used when
489 * platform initialization code doesn't contain the proper data for
490 * whatever reason. Also useful for drivers that do device detection and
491 * detection fails, either because the device uses an unexpected address,
492 * or this is a compatible device with different ID register values.
494 * Parameter checking may look overzealous, but we really don't want
495 * the user to provide incorrect parameters.
497 static ssize_t
498 i2c_sysfs_new_device(struct device *dev, struct device_attribute *attr,
499 const char *buf, size_t count)
501 struct i2c_adapter *adap = to_i2c_adapter(dev);
502 struct i2c_board_info info;
503 struct i2c_client *client;
504 char *blank, end;
505 int res;
507 dev_warn(dev, "The new_device interface is still experimental "
508 "and may change in a near future\n");
509 memset(&info, 0, sizeof(struct i2c_board_info));
511 blank = strchr(buf, ' ');
512 if (!blank) {
513 dev_err(dev, "%s: Missing parameters\n", "new_device");
514 return -EINVAL;
516 if (blank - buf > I2C_NAME_SIZE - 1) {
517 dev_err(dev, "%s: Invalid device name\n", "new_device");
518 return -EINVAL;
520 memcpy(info.type, buf, blank - buf);
522 /* Parse remaining parameters, reject extra parameters */
523 res = sscanf(++blank, "%hi%c", &info.addr, &end);
524 if (res < 1) {
525 dev_err(dev, "%s: Can't parse I2C address\n", "new_device");
526 return -EINVAL;
528 if (res > 1 && end != '\n') {
529 dev_err(dev, "%s: Extra parameters\n", "new_device");
530 return -EINVAL;
533 if (info.addr < 0x03 || info.addr > 0x77) {
534 dev_err(dev, "%s: Invalid I2C address 0x%hx\n", "new_device",
535 info.addr);
536 return -EINVAL;
539 client = i2c_new_device(adap, &info);
540 if (!client)
541 return -EEXIST;
543 /* Keep track of the added device */
544 i2c_lock_adapter(adap);
545 list_add_tail(&client->detected, &adap->userspace_clients);
546 i2c_unlock_adapter(adap);
547 dev_info(dev, "%s: Instantiated device %s at 0x%02hx\n", "new_device",
548 info.type, info.addr);
550 return count;
554 * And of course let the users delete the devices they instantiated, if
555 * they got it wrong. This interface can only be used to delete devices
556 * instantiated by i2c_sysfs_new_device above. This guarantees that we
557 * don't delete devices to which some kernel code still has references.
559 * Parameter checking may look overzealous, but we really don't want
560 * the user to delete the wrong device.
562 static ssize_t
563 i2c_sysfs_delete_device(struct device *dev, struct device_attribute *attr,
564 const char *buf, size_t count)
566 struct i2c_adapter *adap = to_i2c_adapter(dev);
567 struct i2c_client *client, *next;
568 unsigned short addr;
569 char end;
570 int res;
572 /* Parse parameters, reject extra parameters */
573 res = sscanf(buf, "%hi%c", &addr, &end);
574 if (res < 1) {
575 dev_err(dev, "%s: Can't parse I2C address\n", "delete_device");
576 return -EINVAL;
578 if (res > 1 && end != '\n') {
579 dev_err(dev, "%s: Extra parameters\n", "delete_device");
580 return -EINVAL;
583 /* Make sure the device was added through sysfs */
584 res = -ENOENT;
585 i2c_lock_adapter(adap);
586 list_for_each_entry_safe(client, next, &adap->userspace_clients,
587 detected) {
588 if (client->addr == addr) {
589 dev_info(dev, "%s: Deleting device %s at 0x%02hx\n",
590 "delete_device", client->name, client->addr);
592 list_del(&client->detected);
593 i2c_unregister_device(client);
594 res = count;
595 break;
598 i2c_unlock_adapter(adap);
600 if (res < 0)
601 dev_err(dev, "%s: Can't find device in list\n",
602 "delete_device");
603 return res;
606 static DEVICE_ATTR(new_device, S_IWUSR, NULL, i2c_sysfs_new_device);
607 static DEVICE_ATTR(delete_device, S_IWUSR, NULL, i2c_sysfs_delete_device);
609 static struct attribute *i2c_adapter_attrs[] = {
610 &dev_attr_name.attr,
611 &dev_attr_new_device.attr,
612 &dev_attr_delete_device.attr,
613 NULL
616 static struct attribute_group i2c_adapter_attr_group = {
617 .attrs = i2c_adapter_attrs,
620 static const struct attribute_group *i2c_adapter_attr_groups[] = {
621 &i2c_adapter_attr_group,
622 NULL
625 static struct device_type i2c_adapter_type = {
626 .groups = i2c_adapter_attr_groups,
627 .release = i2c_adapter_dev_release,
630 #ifdef CONFIG_I2C_COMPAT
631 static struct class_compat *i2c_adapter_compat_class;
632 #endif
634 static void i2c_scan_static_board_info(struct i2c_adapter *adapter)
636 struct i2c_devinfo *devinfo;
638 down_read(&__i2c_board_lock);
639 list_for_each_entry(devinfo, &__i2c_board_list, list) {
640 if (devinfo->busnum == adapter->nr
641 && !i2c_new_device(adapter,
642 &devinfo->board_info))
643 dev_err(&adapter->dev,
644 "Can't create device at 0x%02x\n",
645 devinfo->board_info.addr);
647 up_read(&__i2c_board_lock);
650 static int i2c_do_add_adapter(struct i2c_driver *driver,
651 struct i2c_adapter *adap)
653 /* Detect supported devices on that bus, and instantiate them */
654 i2c_detect(adap, driver);
656 /* Let legacy drivers scan this bus for matching devices */
657 if (driver->attach_adapter) {
658 /* We ignore the return code; if it fails, too bad */
659 driver->attach_adapter(adap);
661 return 0;
664 static int __process_new_adapter(struct device_driver *d, void *data)
666 return i2c_do_add_adapter(to_i2c_driver(d), data);
669 static int i2c_register_adapter(struct i2c_adapter *adap)
671 int res = 0, dummy;
673 /* Can't register until after driver model init */
674 if (unlikely(WARN_ON(!i2c_bus_type.p))) {
675 res = -EAGAIN;
676 goto out_list;
679 rt_mutex_init(&adap->bus_lock);
680 INIT_LIST_HEAD(&adap->userspace_clients);
682 /* Set default timeout to 1 second if not already set */
683 if (adap->timeout == 0)
684 adap->timeout = HZ;
686 dev_set_name(&adap->dev, "i2c-%d", adap->nr);
687 adap->dev.bus = &i2c_bus_type;
688 adap->dev.type = &i2c_adapter_type;
689 res = device_register(&adap->dev);
690 if (res)
691 goto out_list;
693 dev_dbg(&adap->dev, "adapter [%s] registered\n", adap->name);
695 #ifdef CONFIG_I2C_COMPAT
696 res = class_compat_create_link(i2c_adapter_compat_class, &adap->dev,
697 adap->dev.parent);
698 if (res)
699 dev_warn(&adap->dev,
700 "Failed to create compatibility class link\n");
701 #endif
703 /* create pre-declared device nodes */
704 if (adap->nr < __i2c_first_dynamic_bus_num)
705 i2c_scan_static_board_info(adap);
707 /* Notify drivers */
708 mutex_lock(&core_lock);
709 dummy = bus_for_each_drv(&i2c_bus_type, NULL, adap,
710 __process_new_adapter);
711 mutex_unlock(&core_lock);
713 return 0;
715 out_list:
716 mutex_lock(&core_lock);
717 idr_remove(&i2c_adapter_idr, adap->nr);
718 mutex_unlock(&core_lock);
719 return res;
723 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
724 * @adapter: the adapter to add
725 * Context: can sleep
727 * This routine is used to declare an I2C adapter when its bus number
728 * doesn't matter. Examples: for I2C adapters dynamically added by
729 * USB links or PCI plugin cards.
731 * When this returns zero, a new bus number was allocated and stored
732 * in adap->nr, and the specified adapter became available for clients.
733 * Otherwise, a negative errno value is returned.
735 int i2c_add_adapter(struct i2c_adapter *adapter)
737 int id, res = 0;
739 retry:
740 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
741 return -ENOMEM;
743 mutex_lock(&core_lock);
744 /* "above" here means "above or equal to", sigh */
745 res = idr_get_new_above(&i2c_adapter_idr, adapter,
746 __i2c_first_dynamic_bus_num, &id);
747 mutex_unlock(&core_lock);
749 if (res < 0) {
750 if (res == -EAGAIN)
751 goto retry;
752 return res;
755 adapter->nr = id;
756 return i2c_register_adapter(adapter);
758 EXPORT_SYMBOL(i2c_add_adapter);
761 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
762 * @adap: the adapter to register (with adap->nr initialized)
763 * Context: can sleep
765 * This routine is used to declare an I2C adapter when its bus number
766 * matters. For example, use it for I2C adapters from system-on-chip CPUs,
767 * or otherwise built in to the system's mainboard, and where i2c_board_info
768 * is used to properly configure I2C devices.
770 * If no devices have pre-been declared for this bus, then be sure to
771 * register the adapter before any dynamically allocated ones. Otherwise
772 * the required bus ID may not be available.
774 * When this returns zero, the specified adapter became available for
775 * clients using the bus number provided in adap->nr. Also, the table
776 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
777 * and the appropriate driver model device nodes are created. Otherwise, a
778 * negative errno value is returned.
780 int i2c_add_numbered_adapter(struct i2c_adapter *adap)
782 int id;
783 int status;
785 if (adap->nr & ~MAX_ID_MASK)
786 return -EINVAL;
788 retry:
789 if (idr_pre_get(&i2c_adapter_idr, GFP_KERNEL) == 0)
790 return -ENOMEM;
792 mutex_lock(&core_lock);
793 /* "above" here means "above or equal to", sigh;
794 * we need the "equal to" result to force the result
796 status = idr_get_new_above(&i2c_adapter_idr, adap, adap->nr, &id);
797 if (status == 0 && id != adap->nr) {
798 status = -EBUSY;
799 idr_remove(&i2c_adapter_idr, id);
801 mutex_unlock(&core_lock);
802 if (status == -EAGAIN)
803 goto retry;
805 if (status == 0)
806 status = i2c_register_adapter(adap);
807 return status;
809 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter);
811 static int i2c_do_del_adapter(struct i2c_driver *driver,
812 struct i2c_adapter *adapter)
814 struct i2c_client *client, *_n;
815 int res;
817 /* Remove the devices we created ourselves as the result of hardware
818 * probing (using a driver's detect method) */
819 list_for_each_entry_safe(client, _n, &driver->clients, detected) {
820 if (client->adapter == adapter) {
821 dev_dbg(&adapter->dev, "Removing %s at 0x%x\n",
822 client->name, client->addr);
823 list_del(&client->detected);
824 i2c_unregister_device(client);
828 if (!driver->detach_adapter)
829 return 0;
830 res = driver->detach_adapter(adapter);
831 if (res)
832 dev_err(&adapter->dev, "detach_adapter failed (%d) "
833 "for driver [%s]\n", res, driver->driver.name);
834 return res;
837 static int __unregister_client(struct device *dev, void *dummy)
839 struct i2c_client *client = i2c_verify_client(dev);
840 if (client)
841 i2c_unregister_device(client);
842 return 0;
845 static int __process_removed_adapter(struct device_driver *d, void *data)
847 return i2c_do_del_adapter(to_i2c_driver(d), data);
851 * i2c_del_adapter - unregister I2C adapter
852 * @adap: the adapter being unregistered
853 * Context: can sleep
855 * This unregisters an I2C adapter which was previously registered
856 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
858 int i2c_del_adapter(struct i2c_adapter *adap)
860 int res = 0;
861 struct i2c_adapter *found;
862 struct i2c_client *client, *next;
864 /* First make sure that this adapter was ever added */
865 mutex_lock(&core_lock);
866 found = idr_find(&i2c_adapter_idr, adap->nr);
867 mutex_unlock(&core_lock);
868 if (found != adap) {
869 pr_debug("i2c-core: attempting to delete unregistered "
870 "adapter [%s]\n", adap->name);
871 return -EINVAL;
874 /* Tell drivers about this removal */
875 mutex_lock(&core_lock);
876 res = bus_for_each_drv(&i2c_bus_type, NULL, adap,
877 __process_removed_adapter);
878 mutex_unlock(&core_lock);
879 if (res)
880 return res;
882 /* Remove devices instantiated from sysfs */
883 i2c_lock_adapter(adap);
884 list_for_each_entry_safe(client, next, &adap->userspace_clients,
885 detected) {
886 dev_dbg(&adap->dev, "Removing %s at 0x%x\n", client->name,
887 client->addr);
888 list_del(&client->detected);
889 i2c_unregister_device(client);
891 i2c_unlock_adapter(adap);
893 /* Detach any active clients. This can't fail, thus we do not
894 checking the returned value. */
895 res = device_for_each_child(&adap->dev, NULL, __unregister_client);
897 #ifdef CONFIG_I2C_COMPAT
898 class_compat_remove_link(i2c_adapter_compat_class, &adap->dev,
899 adap->dev.parent);
900 #endif
902 /* device name is gone after device_unregister */
903 dev_dbg(&adap->dev, "adapter [%s] unregistered\n", adap->name);
905 /* clean up the sysfs representation */
906 init_completion(&adap->dev_released);
907 device_unregister(&adap->dev);
909 /* wait for sysfs to drop all references */
910 wait_for_completion(&adap->dev_released);
912 /* free bus id */
913 mutex_lock(&core_lock);
914 idr_remove(&i2c_adapter_idr, adap->nr);
915 mutex_unlock(&core_lock);
917 /* Clear the device structure in case this adapter is ever going to be
918 added again */
919 memset(&adap->dev, 0, sizeof(adap->dev));
921 return 0;
923 EXPORT_SYMBOL(i2c_del_adapter);
926 /* ------------------------------------------------------------------------- */
928 static int __process_new_driver(struct device *dev, void *data)
930 if (dev->type != &i2c_adapter_type)
931 return 0;
932 return i2c_do_add_adapter(data, to_i2c_adapter(dev));
936 * An i2c_driver is used with one or more i2c_client (device) nodes to access
937 * i2c slave chips, on a bus instance associated with some i2c_adapter.
940 int i2c_register_driver(struct module *owner, struct i2c_driver *driver)
942 int res;
944 /* Can't register until after driver model init */
945 if (unlikely(WARN_ON(!i2c_bus_type.p)))
946 return -EAGAIN;
948 /* add the driver to the list of i2c drivers in the driver core */
949 driver->driver.owner = owner;
950 driver->driver.bus = &i2c_bus_type;
952 /* When registration returns, the driver core
953 * will have called probe() for all matching-but-unbound devices.
955 res = driver_register(&driver->driver);
956 if (res)
957 return res;
959 pr_debug("i2c-core: driver [%s] registered\n", driver->driver.name);
961 INIT_LIST_HEAD(&driver->clients);
962 /* Walk the adapters that are already present */
963 mutex_lock(&core_lock);
964 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_new_driver);
965 mutex_unlock(&core_lock);
967 return 0;
969 EXPORT_SYMBOL(i2c_register_driver);
971 static int __process_removed_driver(struct device *dev, void *data)
973 if (dev->type != &i2c_adapter_type)
974 return 0;
975 return i2c_do_del_adapter(data, to_i2c_adapter(dev));
979 * i2c_del_driver - unregister I2C driver
980 * @driver: the driver being unregistered
981 * Context: can sleep
983 void i2c_del_driver(struct i2c_driver *driver)
985 mutex_lock(&core_lock);
986 bus_for_each_dev(&i2c_bus_type, NULL, driver, __process_removed_driver);
987 mutex_unlock(&core_lock);
989 driver_unregister(&driver->driver);
990 pr_debug("i2c-core: driver [%s] unregistered\n", driver->driver.name);
992 EXPORT_SYMBOL(i2c_del_driver);
994 /* ------------------------------------------------------------------------- */
996 static int __i2c_check_addr(struct device *dev, void *addrp)
998 struct i2c_client *client = i2c_verify_client(dev);
999 int addr = *(int *)addrp;
1001 if (client && client->addr == addr)
1002 return -EBUSY;
1003 return 0;
1006 static int i2c_check_addr(struct i2c_adapter *adapter, int addr)
1008 return device_for_each_child(&adapter->dev, &addr, __i2c_check_addr);
1012 * i2c_use_client - increments the reference count of the i2c client structure
1013 * @client: the client being referenced
1015 * Each live reference to a client should be refcounted. The driver model does
1016 * that automatically as part of driver binding, so that most drivers don't
1017 * need to do this explicitly: they hold a reference until they're unbound
1018 * from the device.
1020 * A pointer to the client with the incremented reference counter is returned.
1022 struct i2c_client *i2c_use_client(struct i2c_client *client)
1024 if (client && get_device(&client->dev))
1025 return client;
1026 return NULL;
1028 EXPORT_SYMBOL(i2c_use_client);
1031 * i2c_release_client - release a use of the i2c client structure
1032 * @client: the client being no longer referenced
1034 * Must be called when a user of a client is finished with it.
1036 void i2c_release_client(struct i2c_client *client)
1038 if (client)
1039 put_device(&client->dev);
1041 EXPORT_SYMBOL(i2c_release_client);
1043 struct i2c_cmd_arg {
1044 unsigned cmd;
1045 void *arg;
1048 static int i2c_cmd(struct device *dev, void *_arg)
1050 struct i2c_client *client = i2c_verify_client(dev);
1051 struct i2c_cmd_arg *arg = _arg;
1053 if (client && client->driver && client->driver->command)
1054 client->driver->command(client, arg->cmd, arg->arg);
1055 return 0;
1058 void i2c_clients_command(struct i2c_adapter *adap, unsigned int cmd, void *arg)
1060 struct i2c_cmd_arg cmd_arg;
1062 cmd_arg.cmd = cmd;
1063 cmd_arg.arg = arg;
1064 device_for_each_child(&adap->dev, &cmd_arg, i2c_cmd);
1066 EXPORT_SYMBOL(i2c_clients_command);
1068 static int __init i2c_init(void)
1070 int retval;
1072 retval = bus_register(&i2c_bus_type);
1073 if (retval)
1074 return retval;
1075 #ifdef CONFIG_I2C_COMPAT
1076 i2c_adapter_compat_class = class_compat_register("i2c-adapter");
1077 if (!i2c_adapter_compat_class) {
1078 retval = -ENOMEM;
1079 goto bus_err;
1081 #endif
1082 retval = i2c_add_driver(&dummy_driver);
1083 if (retval)
1084 goto class_err;
1085 return 0;
1087 class_err:
1088 #ifdef CONFIG_I2C_COMPAT
1089 class_compat_unregister(i2c_adapter_compat_class);
1090 bus_err:
1091 #endif
1092 bus_unregister(&i2c_bus_type);
1093 return retval;
1096 static void __exit i2c_exit(void)
1098 i2c_del_driver(&dummy_driver);
1099 #ifdef CONFIG_I2C_COMPAT
1100 class_compat_unregister(i2c_adapter_compat_class);
1101 #endif
1102 bus_unregister(&i2c_bus_type);
1105 /* We must initialize early, because some subsystems register i2c drivers
1106 * in subsys_initcall() code, but are linked (and initialized) before i2c.
1108 postcore_initcall(i2c_init);
1109 module_exit(i2c_exit);
1111 /* ----------------------------------------------------
1112 * the functional interface to the i2c busses.
1113 * ----------------------------------------------------
1117 * i2c_transfer - execute a single or combined I2C message
1118 * @adap: Handle to I2C bus
1119 * @msgs: One or more messages to execute before STOP is issued to
1120 * terminate the operation; each message begins with a START.
1121 * @num: Number of messages to be executed.
1123 * Returns negative errno, else the number of messages executed.
1125 * Note that there is no requirement that each message be sent to
1126 * the same slave address, although that is the most common model.
1128 int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
1130 unsigned long orig_jiffies;
1131 int ret, try;
1133 /* REVISIT the fault reporting model here is weak:
1135 * - When we get an error after receiving N bytes from a slave,
1136 * there is no way to report "N".
1138 * - When we get a NAK after transmitting N bytes to a slave,
1139 * there is no way to report "N" ... or to let the master
1140 * continue executing the rest of this combined message, if
1141 * that's the appropriate response.
1143 * - When for example "num" is two and we successfully complete
1144 * the first message but get an error part way through the
1145 * second, it's unclear whether that should be reported as
1146 * one (discarding status on the second message) or errno
1147 * (discarding status on the first one).
1150 if (adap->algo->master_xfer) {
1151 #ifdef DEBUG
1152 for (ret = 0; ret < num; ret++) {
1153 dev_dbg(&adap->dev, "master_xfer[%d] %c, addr=0x%02x, "
1154 "len=%d%s\n", ret, (msgs[ret].flags & I2C_M_RD)
1155 ? 'R' : 'W', msgs[ret].addr, msgs[ret].len,
1156 (msgs[ret].flags & I2C_M_RECV_LEN) ? "+" : "");
1158 #endif
1160 if (in_atomic() || irqs_disabled()) {
1161 ret = rt_mutex_trylock(&adap->bus_lock);
1162 if (!ret)
1163 /* I2C activity is ongoing. */
1164 return -EAGAIN;
1165 } else {
1166 rt_mutex_lock(&adap->bus_lock);
1169 /* Retry automatically on arbitration loss */
1170 orig_jiffies = jiffies;
1171 for (ret = 0, try = 0; try <= adap->retries; try++) {
1172 ret = adap->algo->master_xfer(adap, msgs, num);
1173 if (ret != -EAGAIN)
1174 break;
1175 if (time_after(jiffies, orig_jiffies + adap->timeout))
1176 break;
1178 rt_mutex_unlock(&adap->bus_lock);
1180 return ret;
1181 } else {
1182 dev_dbg(&adap->dev, "I2C level transfers not supported\n");
1183 return -EOPNOTSUPP;
1186 EXPORT_SYMBOL(i2c_transfer);
1189 * i2c_master_send - issue a single I2C message in master transmit mode
1190 * @client: Handle to slave device
1191 * @buf: Data that will be written to the slave
1192 * @count: How many bytes to write, must be less than 64k since msg.len is u16
1194 * Returns negative errno, or else the number of bytes written.
1196 int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
1198 int ret;
1199 struct i2c_adapter *adap=client->adapter;
1200 struct i2c_msg msg;
1202 msg.addr = client->addr;
1203 msg.flags = client->flags & I2C_M_TEN;
1204 msg.len = count;
1205 msg.buf = (char *)buf;
1207 ret = i2c_transfer(adap, &msg, 1);
1209 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1210 transmitted, else error code. */
1211 return (ret == 1) ? count : ret;
1213 EXPORT_SYMBOL(i2c_master_send);
1216 * i2c_master_recv - issue a single I2C message in master receive mode
1217 * @client: Handle to slave device
1218 * @buf: Where to store data read from slave
1219 * @count: How many bytes to read, must be less than 64k since msg.len is u16
1221 * Returns negative errno, or else the number of bytes read.
1223 int i2c_master_recv(struct i2c_client *client, char *buf ,int count)
1225 struct i2c_adapter *adap=client->adapter;
1226 struct i2c_msg msg;
1227 int ret;
1229 msg.addr = client->addr;
1230 msg.flags = client->flags & I2C_M_TEN;
1231 msg.flags |= I2C_M_RD;
1232 msg.len = count;
1233 msg.buf = buf;
1235 ret = i2c_transfer(adap, &msg, 1);
1237 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
1238 transmitted, else error code. */
1239 return (ret == 1) ? count : ret;
1241 EXPORT_SYMBOL(i2c_master_recv);
1243 /* ----------------------------------------------------
1244 * the i2c address scanning function
1245 * Will not work for 10-bit addresses!
1246 * ----------------------------------------------------
1249 static int i2c_detect_address(struct i2c_client *temp_client,
1250 struct i2c_driver *driver)
1252 struct i2c_board_info info;
1253 struct i2c_adapter *adapter = temp_client->adapter;
1254 int addr = temp_client->addr;
1255 int err;
1257 /* Make sure the address is valid */
1258 if (addr < 0x03 || addr > 0x77) {
1259 dev_warn(&adapter->dev, "Invalid probe address 0x%02x\n",
1260 addr);
1261 return -EINVAL;
1264 /* Skip if already in use */
1265 if (i2c_check_addr(adapter, addr))
1266 return 0;
1268 /* Make sure there is something at this address */
1269 if (addr == 0x73 && (adapter->class & I2C_CLASS_HWMON)) {
1270 /* Special probe for FSC hwmon chips */
1271 union i2c_smbus_data dummy;
1273 if (i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_READ, 0,
1274 I2C_SMBUS_BYTE_DATA, &dummy) < 0)
1275 return 0;
1276 } else {
1277 if (i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_WRITE, 0,
1278 I2C_SMBUS_QUICK, NULL) < 0)
1279 return 0;
1281 /* Prevent 24RF08 corruption */
1282 if ((addr & ~0x0f) == 0x50)
1283 i2c_smbus_xfer(adapter, addr, 0, I2C_SMBUS_WRITE, 0,
1284 I2C_SMBUS_QUICK, NULL);
1287 /* Finally call the custom detection function */
1288 memset(&info, 0, sizeof(struct i2c_board_info));
1289 info.addr = addr;
1290 err = driver->detect(temp_client, &info);
1291 if (err) {
1292 /* -ENODEV is returned if the detection fails. We catch it
1293 here as this isn't an error. */
1294 return err == -ENODEV ? 0 : err;
1297 /* Consistency check */
1298 if (info.type[0] == '\0') {
1299 dev_err(&adapter->dev, "%s detection function provided "
1300 "no name for 0x%x\n", driver->driver.name,
1301 addr);
1302 } else {
1303 struct i2c_client *client;
1305 /* Detection succeeded, instantiate the device */
1306 dev_dbg(&adapter->dev, "Creating %s at 0x%02x\n",
1307 info.type, info.addr);
1308 client = i2c_new_device(adapter, &info);
1309 if (client)
1310 list_add_tail(&client->detected, &driver->clients);
1311 else
1312 dev_err(&adapter->dev, "Failed creating %s at 0x%02x\n",
1313 info.type, info.addr);
1315 return 0;
1318 static int i2c_detect(struct i2c_adapter *adapter, struct i2c_driver *driver)
1320 const unsigned short *address_list;
1321 struct i2c_client *temp_client;
1322 int i, err = 0;
1323 int adap_id = i2c_adapter_id(adapter);
1325 address_list = driver->address_list;
1326 if (!driver->detect || !address_list)
1327 return 0;
1329 /* Set up a temporary client to help detect callback */
1330 temp_client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
1331 if (!temp_client)
1332 return -ENOMEM;
1333 temp_client->adapter = adapter;
1335 /* Stop here if the classes do not match */
1336 if (!(adapter->class & driver->class))
1337 goto exit_free;
1339 /* Stop here if we can't use SMBUS_QUICK */
1340 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_QUICK)) {
1341 if (address_list[0] == I2C_CLIENT_END)
1342 goto exit_free;
1344 dev_warn(&adapter->dev, "SMBus Quick command not supported, "
1345 "can't probe for chips\n");
1346 err = -EOPNOTSUPP;
1347 goto exit_free;
1350 for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
1351 dev_dbg(&adapter->dev, "found normal entry for adapter %d, "
1352 "addr 0x%02x\n", adap_id, address_list[i]);
1353 temp_client->addr = address_list[i];
1354 err = i2c_detect_address(temp_client, driver);
1355 if (err)
1356 goto exit_free;
1359 exit_free:
1360 kfree(temp_client);
1361 return err;
1364 struct i2c_client *
1365 i2c_new_probed_device(struct i2c_adapter *adap,
1366 struct i2c_board_info *info,
1367 unsigned short const *addr_list)
1369 int i;
1371 /* Stop here if the bus doesn't support probing */
1372 if (!i2c_check_functionality(adap, I2C_FUNC_SMBUS_READ_BYTE)) {
1373 dev_err(&adap->dev, "Probing not supported\n");
1374 return NULL;
1377 for (i = 0; addr_list[i] != I2C_CLIENT_END; i++) {
1378 /* Check address validity */
1379 if (addr_list[i] < 0x03 || addr_list[i] > 0x77) {
1380 dev_warn(&adap->dev, "Invalid 7-bit address "
1381 "0x%02x\n", addr_list[i]);
1382 continue;
1385 /* Check address availability */
1386 if (i2c_check_addr(adap, addr_list[i])) {
1387 dev_dbg(&adap->dev, "Address 0x%02x already in "
1388 "use, not probing\n", addr_list[i]);
1389 continue;
1392 /* Test address responsiveness
1393 The default probe method is a quick write, but it is known
1394 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1395 and could also irreversibly write-protect some EEPROMs, so
1396 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1397 read instead. Also, some bus drivers don't implement
1398 quick write, so we fallback to a byte read it that case
1399 too. */
1400 if ((addr_list[i] & ~0x07) == 0x30
1401 || (addr_list[i] & ~0x0f) == 0x50
1402 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) {
1403 union i2c_smbus_data data;
1405 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1406 I2C_SMBUS_READ, 0,
1407 I2C_SMBUS_BYTE, &data) >= 0)
1408 break;
1409 } else {
1410 if (i2c_smbus_xfer(adap, addr_list[i], 0,
1411 I2C_SMBUS_WRITE, 0,
1412 I2C_SMBUS_QUICK, NULL) >= 0)
1413 break;
1417 if (addr_list[i] == I2C_CLIENT_END) {
1418 dev_dbg(&adap->dev, "Probing failed, no device found\n");
1419 return NULL;
1422 info->addr = addr_list[i];
1423 return i2c_new_device(adap, info);
1425 EXPORT_SYMBOL_GPL(i2c_new_probed_device);
1427 struct i2c_adapter* i2c_get_adapter(int id)
1429 struct i2c_adapter *adapter;
1431 mutex_lock(&core_lock);
1432 adapter = idr_find(&i2c_adapter_idr, id);
1433 if (adapter && !try_module_get(adapter->owner))
1434 adapter = NULL;
1436 mutex_unlock(&core_lock);
1437 return adapter;
1439 EXPORT_SYMBOL(i2c_get_adapter);
1441 void i2c_put_adapter(struct i2c_adapter *adap)
1443 module_put(adap->owner);
1445 EXPORT_SYMBOL(i2c_put_adapter);
1447 /* The SMBus parts */
1449 #define POLY (0x1070U << 3)
1450 static u8 crc8(u16 data)
1452 int i;
1454 for(i = 0; i < 8; i++) {
1455 if (data & 0x8000)
1456 data = data ^ POLY;
1457 data = data << 1;
1459 return (u8)(data >> 8);
1462 /* Incremental CRC8 over count bytes in the array pointed to by p */
1463 static u8 i2c_smbus_pec(u8 crc, u8 *p, size_t count)
1465 int i;
1467 for(i = 0; i < count; i++)
1468 crc = crc8((crc ^ p[i]) << 8);
1469 return crc;
1472 /* Assume a 7-bit address, which is reasonable for SMBus */
1473 static u8 i2c_smbus_msg_pec(u8 pec, struct i2c_msg *msg)
1475 /* The address will be sent first */
1476 u8 addr = (msg->addr << 1) | !!(msg->flags & I2C_M_RD);
1477 pec = i2c_smbus_pec(pec, &addr, 1);
1479 /* The data buffer follows */
1480 return i2c_smbus_pec(pec, msg->buf, msg->len);
1483 /* Used for write only transactions */
1484 static inline void i2c_smbus_add_pec(struct i2c_msg *msg)
1486 msg->buf[msg->len] = i2c_smbus_msg_pec(0, msg);
1487 msg->len++;
1490 /* Return <0 on CRC error
1491 If there was a write before this read (most cases) we need to take the
1492 partial CRC from the write part into account.
1493 Note that this function does modify the message (we need to decrease the
1494 message length to hide the CRC byte from the caller). */
1495 static int i2c_smbus_check_pec(u8 cpec, struct i2c_msg *msg)
1497 u8 rpec = msg->buf[--msg->len];
1498 cpec = i2c_smbus_msg_pec(cpec, msg);
1500 if (rpec != cpec) {
1501 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1502 rpec, cpec);
1503 return -EBADMSG;
1505 return 0;
1509 * i2c_smbus_read_byte - SMBus "receive byte" protocol
1510 * @client: Handle to slave device
1512 * This executes the SMBus "receive byte" protocol, returning negative errno
1513 * else the byte received from the device.
1515 s32 i2c_smbus_read_byte(struct i2c_client *client)
1517 union i2c_smbus_data data;
1518 int status;
1520 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1521 I2C_SMBUS_READ, 0,
1522 I2C_SMBUS_BYTE, &data);
1523 return (status < 0) ? status : data.byte;
1525 EXPORT_SYMBOL(i2c_smbus_read_byte);
1528 * i2c_smbus_write_byte - SMBus "send byte" protocol
1529 * @client: Handle to slave device
1530 * @value: Byte to be sent
1532 * This executes the SMBus "send byte" protocol, returning negative errno
1533 * else zero on success.
1535 s32 i2c_smbus_write_byte(struct i2c_client *client, u8 value)
1537 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1538 I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
1540 EXPORT_SYMBOL(i2c_smbus_write_byte);
1543 * i2c_smbus_read_byte_data - SMBus "read byte" protocol
1544 * @client: Handle to slave device
1545 * @command: Byte interpreted by slave
1547 * This executes the SMBus "read byte" protocol, returning negative errno
1548 * else a data byte received from the device.
1550 s32 i2c_smbus_read_byte_data(struct i2c_client *client, u8 command)
1552 union i2c_smbus_data data;
1553 int status;
1555 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1556 I2C_SMBUS_READ, command,
1557 I2C_SMBUS_BYTE_DATA, &data);
1558 return (status < 0) ? status : data.byte;
1560 EXPORT_SYMBOL(i2c_smbus_read_byte_data);
1563 * i2c_smbus_write_byte_data - SMBus "write byte" protocol
1564 * @client: Handle to slave device
1565 * @command: Byte interpreted by slave
1566 * @value: Byte being written
1568 * This executes the SMBus "write byte" protocol, returning negative errno
1569 * else zero on success.
1571 s32 i2c_smbus_write_byte_data(struct i2c_client *client, u8 command, u8 value)
1573 union i2c_smbus_data data;
1574 data.byte = value;
1575 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1576 I2C_SMBUS_WRITE,command,
1577 I2C_SMBUS_BYTE_DATA,&data);
1579 EXPORT_SYMBOL(i2c_smbus_write_byte_data);
1582 * i2c_smbus_read_word_data - SMBus "read word" protocol
1583 * @client: Handle to slave device
1584 * @command: Byte interpreted by slave
1586 * This executes the SMBus "read word" protocol, returning negative errno
1587 * else a 16-bit unsigned "word" received from the device.
1589 s32 i2c_smbus_read_word_data(struct i2c_client *client, u8 command)
1591 union i2c_smbus_data data;
1592 int status;
1594 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1595 I2C_SMBUS_READ, command,
1596 I2C_SMBUS_WORD_DATA, &data);
1597 return (status < 0) ? status : data.word;
1599 EXPORT_SYMBOL(i2c_smbus_read_word_data);
1602 * i2c_smbus_write_word_data - SMBus "write word" protocol
1603 * @client: Handle to slave device
1604 * @command: Byte interpreted by slave
1605 * @value: 16-bit "word" being written
1607 * This executes the SMBus "write word" protocol, returning negative errno
1608 * else zero on success.
1610 s32 i2c_smbus_write_word_data(struct i2c_client *client, u8 command, u16 value)
1612 union i2c_smbus_data data;
1613 data.word = value;
1614 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1615 I2C_SMBUS_WRITE,command,
1616 I2C_SMBUS_WORD_DATA,&data);
1618 EXPORT_SYMBOL(i2c_smbus_write_word_data);
1621 * i2c_smbus_process_call - SMBus "process call" protocol
1622 * @client: Handle to slave device
1623 * @command: Byte interpreted by slave
1624 * @value: 16-bit "word" being written
1626 * This executes the SMBus "process call" protocol, returning negative errno
1627 * else a 16-bit unsigned "word" received from the device.
1629 s32 i2c_smbus_process_call(struct i2c_client *client, u8 command, u16 value)
1631 union i2c_smbus_data data;
1632 int status;
1633 data.word = value;
1635 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1636 I2C_SMBUS_WRITE, command,
1637 I2C_SMBUS_PROC_CALL, &data);
1638 return (status < 0) ? status : data.word;
1640 EXPORT_SYMBOL(i2c_smbus_process_call);
1643 * i2c_smbus_read_block_data - SMBus "block read" protocol
1644 * @client: Handle to slave device
1645 * @command: Byte interpreted by slave
1646 * @values: Byte array into which data will be read; big enough to hold
1647 * the data returned by the slave. SMBus allows at most 32 bytes.
1649 * This executes the SMBus "block read" protocol, returning negative errno
1650 * else the number of data bytes in the slave's response.
1652 * Note that using this function requires that the client's adapter support
1653 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1654 * support this; its emulation through I2C messaging relies on a specific
1655 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1657 s32 i2c_smbus_read_block_data(struct i2c_client *client, u8 command,
1658 u8 *values)
1660 union i2c_smbus_data data;
1661 int status;
1663 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1664 I2C_SMBUS_READ, command,
1665 I2C_SMBUS_BLOCK_DATA, &data);
1666 if (status)
1667 return status;
1669 memcpy(values, &data.block[1], data.block[0]);
1670 return data.block[0];
1672 EXPORT_SYMBOL(i2c_smbus_read_block_data);
1675 * i2c_smbus_write_block_data - SMBus "block write" protocol
1676 * @client: Handle to slave device
1677 * @command: Byte interpreted by slave
1678 * @length: Size of data block; SMBus allows at most 32 bytes
1679 * @values: Byte array which will be written.
1681 * This executes the SMBus "block write" protocol, returning negative errno
1682 * else zero on success.
1684 s32 i2c_smbus_write_block_data(struct i2c_client *client, u8 command,
1685 u8 length, const u8 *values)
1687 union i2c_smbus_data data;
1689 if (length > I2C_SMBUS_BLOCK_MAX)
1690 length = I2C_SMBUS_BLOCK_MAX;
1691 data.block[0] = length;
1692 memcpy(&data.block[1], values, length);
1693 return i2c_smbus_xfer(client->adapter,client->addr,client->flags,
1694 I2C_SMBUS_WRITE,command,
1695 I2C_SMBUS_BLOCK_DATA,&data);
1697 EXPORT_SYMBOL(i2c_smbus_write_block_data);
1699 /* Returns the number of read bytes */
1700 s32 i2c_smbus_read_i2c_block_data(struct i2c_client *client, u8 command,
1701 u8 length, u8 *values)
1703 union i2c_smbus_data data;
1704 int status;
1706 if (length > I2C_SMBUS_BLOCK_MAX)
1707 length = I2C_SMBUS_BLOCK_MAX;
1708 data.block[0] = length;
1709 status = i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1710 I2C_SMBUS_READ, command,
1711 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1712 if (status < 0)
1713 return status;
1715 memcpy(values, &data.block[1], data.block[0]);
1716 return data.block[0];
1718 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data);
1720 s32 i2c_smbus_write_i2c_block_data(struct i2c_client *client, u8 command,
1721 u8 length, const u8 *values)
1723 union i2c_smbus_data data;
1725 if (length > I2C_SMBUS_BLOCK_MAX)
1726 length = I2C_SMBUS_BLOCK_MAX;
1727 data.block[0] = length;
1728 memcpy(data.block + 1, values, length);
1729 return i2c_smbus_xfer(client->adapter, client->addr, client->flags,
1730 I2C_SMBUS_WRITE, command,
1731 I2C_SMBUS_I2C_BLOCK_DATA, &data);
1733 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data);
1735 /* Simulate a SMBus command using the i2c protocol
1736 No checking of parameters is done! */
1737 static s32 i2c_smbus_xfer_emulated(struct i2c_adapter * adapter, u16 addr,
1738 unsigned short flags,
1739 char read_write, u8 command, int size,
1740 union i2c_smbus_data * data)
1742 /* So we need to generate a series of msgs. In the case of writing, we
1743 need to use only one message; when reading, we need two. We initialize
1744 most things with sane defaults, to keep the code below somewhat
1745 simpler. */
1746 unsigned char msgbuf0[I2C_SMBUS_BLOCK_MAX+3];
1747 unsigned char msgbuf1[I2C_SMBUS_BLOCK_MAX+2];
1748 int num = read_write == I2C_SMBUS_READ?2:1;
1749 struct i2c_msg msg[2] = { { addr, flags, 1, msgbuf0 },
1750 { addr, flags | I2C_M_RD, 0, msgbuf1 }
1752 int i;
1753 u8 partial_pec = 0;
1754 int status;
1756 msgbuf0[0] = command;
1757 switch(size) {
1758 case I2C_SMBUS_QUICK:
1759 msg[0].len = 0;
1760 /* Special case: The read/write field is used as data */
1761 msg[0].flags = flags | (read_write == I2C_SMBUS_READ ?
1762 I2C_M_RD : 0);
1763 num = 1;
1764 break;
1765 case I2C_SMBUS_BYTE:
1766 if (read_write == I2C_SMBUS_READ) {
1767 /* Special case: only a read! */
1768 msg[0].flags = I2C_M_RD | flags;
1769 num = 1;
1771 break;
1772 case I2C_SMBUS_BYTE_DATA:
1773 if (read_write == I2C_SMBUS_READ)
1774 msg[1].len = 1;
1775 else {
1776 msg[0].len = 2;
1777 msgbuf0[1] = data->byte;
1779 break;
1780 case I2C_SMBUS_WORD_DATA:
1781 if (read_write == I2C_SMBUS_READ)
1782 msg[1].len = 2;
1783 else {
1784 msg[0].len=3;
1785 msgbuf0[1] = data->word & 0xff;
1786 msgbuf0[2] = data->word >> 8;
1788 break;
1789 case I2C_SMBUS_PROC_CALL:
1790 num = 2; /* Special case */
1791 read_write = I2C_SMBUS_READ;
1792 msg[0].len = 3;
1793 msg[1].len = 2;
1794 msgbuf0[1] = data->word & 0xff;
1795 msgbuf0[2] = data->word >> 8;
1796 break;
1797 case I2C_SMBUS_BLOCK_DATA:
1798 if (read_write == I2C_SMBUS_READ) {
1799 msg[1].flags |= I2C_M_RECV_LEN;
1800 msg[1].len = 1; /* block length will be added by
1801 the underlying bus driver */
1802 } else {
1803 msg[0].len = data->block[0] + 2;
1804 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 2) {
1805 dev_err(&adapter->dev,
1806 "Invalid block write size %d\n",
1807 data->block[0]);
1808 return -EINVAL;
1810 for (i = 1; i < msg[0].len; i++)
1811 msgbuf0[i] = data->block[i-1];
1813 break;
1814 case I2C_SMBUS_BLOCK_PROC_CALL:
1815 num = 2; /* Another special case */
1816 read_write = I2C_SMBUS_READ;
1817 if (data->block[0] > I2C_SMBUS_BLOCK_MAX) {
1818 dev_err(&adapter->dev,
1819 "Invalid block write size %d\n",
1820 data->block[0]);
1821 return -EINVAL;
1823 msg[0].len = data->block[0] + 2;
1824 for (i = 1; i < msg[0].len; i++)
1825 msgbuf0[i] = data->block[i-1];
1826 msg[1].flags |= I2C_M_RECV_LEN;
1827 msg[1].len = 1; /* block length will be added by
1828 the underlying bus driver */
1829 break;
1830 case I2C_SMBUS_I2C_BLOCK_DATA:
1831 if (read_write == I2C_SMBUS_READ) {
1832 msg[1].len = data->block[0];
1833 } else {
1834 msg[0].len = data->block[0] + 1;
1835 if (msg[0].len > I2C_SMBUS_BLOCK_MAX + 1) {
1836 dev_err(&adapter->dev,
1837 "Invalid block write size %d\n",
1838 data->block[0]);
1839 return -EINVAL;
1841 for (i = 1; i <= data->block[0]; i++)
1842 msgbuf0[i] = data->block[i];
1844 break;
1845 default:
1846 dev_err(&adapter->dev, "Unsupported transaction %d\n", size);
1847 return -EOPNOTSUPP;
1850 i = ((flags & I2C_CLIENT_PEC) && size != I2C_SMBUS_QUICK
1851 && size != I2C_SMBUS_I2C_BLOCK_DATA);
1852 if (i) {
1853 /* Compute PEC if first message is a write */
1854 if (!(msg[0].flags & I2C_M_RD)) {
1855 if (num == 1) /* Write only */
1856 i2c_smbus_add_pec(&msg[0]);
1857 else /* Write followed by read */
1858 partial_pec = i2c_smbus_msg_pec(0, &msg[0]);
1860 /* Ask for PEC if last message is a read */
1861 if (msg[num-1].flags & I2C_M_RD)
1862 msg[num-1].len++;
1865 status = i2c_transfer(adapter, msg, num);
1866 if (status < 0)
1867 return status;
1869 /* Check PEC if last message is a read */
1870 if (i && (msg[num-1].flags & I2C_M_RD)) {
1871 status = i2c_smbus_check_pec(partial_pec, &msg[num-1]);
1872 if (status < 0)
1873 return status;
1876 if (read_write == I2C_SMBUS_READ)
1877 switch(size) {
1878 case I2C_SMBUS_BYTE:
1879 data->byte = msgbuf0[0];
1880 break;
1881 case I2C_SMBUS_BYTE_DATA:
1882 data->byte = msgbuf1[0];
1883 break;
1884 case I2C_SMBUS_WORD_DATA:
1885 case I2C_SMBUS_PROC_CALL:
1886 data->word = msgbuf1[0] | (msgbuf1[1] << 8);
1887 break;
1888 case I2C_SMBUS_I2C_BLOCK_DATA:
1889 for (i = 0; i < data->block[0]; i++)
1890 data->block[i+1] = msgbuf1[i];
1891 break;
1892 case I2C_SMBUS_BLOCK_DATA:
1893 case I2C_SMBUS_BLOCK_PROC_CALL:
1894 for (i = 0; i < msgbuf1[0] + 1; i++)
1895 data->block[i] = msgbuf1[i];
1896 break;
1898 return 0;
1902 * i2c_smbus_xfer - execute SMBus protocol operations
1903 * @adapter: Handle to I2C bus
1904 * @addr: Address of SMBus slave on that bus
1905 * @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
1906 * @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
1907 * @command: Byte interpreted by slave, for protocols which use such bytes
1908 * @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
1909 * @data: Data to be read or written
1911 * This executes an SMBus protocol operation, and returns a negative
1912 * errno code else zero on success.
1914 s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
1915 char read_write, u8 command, int protocol,
1916 union i2c_smbus_data *data)
1918 unsigned long orig_jiffies;
1919 int try;
1920 s32 res;
1922 flags &= I2C_M_TEN | I2C_CLIENT_PEC;
1924 if (adapter->algo->smbus_xfer) {
1925 rt_mutex_lock(&adapter->bus_lock);
1927 /* Retry automatically on arbitration loss */
1928 orig_jiffies = jiffies;
1929 for (res = 0, try = 0; try <= adapter->retries; try++) {
1930 res = adapter->algo->smbus_xfer(adapter, addr, flags,
1931 read_write, command,
1932 protocol, data);
1933 if (res != -EAGAIN)
1934 break;
1935 if (time_after(jiffies,
1936 orig_jiffies + adapter->timeout))
1937 break;
1939 rt_mutex_unlock(&adapter->bus_lock);
1940 } else
1941 res = i2c_smbus_xfer_emulated(adapter,addr,flags,read_write,
1942 command, protocol, data);
1944 return res;
1946 EXPORT_SYMBOL(i2c_smbus_xfer);
1948 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1949 MODULE_DESCRIPTION("I2C-Bus main module");
1950 MODULE_LICENSE("GPL");