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/seq_file.h>
33 #include <linux/platform_device.h>
34 #include <linux/mutex.h>
35 #include <linux/completion.h>
36 #include <linux/hardirq.h>
37 #include <linux/irqflags.h>
38 #include <asm/uaccess.h>
39 #include <asm/semaphore.h>
44 static DEFINE_MUTEX(core_lock
);
45 static DEFINE_IDR(i2c_adapter_idr
);
47 #define is_newstyle_driver(d) ((d)->probe || (d)->remove)
49 /* ------------------------------------------------------------------------- */
51 static int i2c_device_match(struct device
*dev
, struct device_driver
*drv
)
53 struct i2c_client
*client
= to_i2c_client(dev
);
54 struct i2c_driver
*driver
= to_i2c_driver(drv
);
56 /* make legacy i2c drivers bypass driver model probing entirely;
57 * such drivers scan each i2c adapter/bus themselves.
59 if (!is_newstyle_driver(driver
))
62 /* new style drivers use the same kind of driver matching policy
63 * as platform devices or SPI: compare device and driver IDs.
65 return strcmp(client
->driver_name
, drv
->name
) == 0;
70 /* uevent helps with hotplug: modprobe -q $(MODALIAS) */
71 static int i2c_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
73 struct i2c_client
*client
= to_i2c_client(dev
);
75 /* by definition, legacy drivers can't hotplug */
76 if (dev
->driver
|| !client
->driver_name
)
79 if (add_uevent_var(env
, "MODALIAS=%s", client
->driver_name
))
81 dev_dbg(dev
, "uevent\n");
86 #define i2c_device_uevent NULL
87 #endif /* CONFIG_HOTPLUG */
89 static int i2c_device_probe(struct device
*dev
)
91 struct i2c_client
*client
= to_i2c_client(dev
);
92 struct i2c_driver
*driver
= to_i2c_driver(dev
->driver
);
93 <<<<<<< HEAD
:drivers
/i2c
/i2c
-core
.c
96 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/i2c
/i2c
-core
.c
100 client
->driver
= driver
;
101 dev_dbg(dev
, "probe\n");
102 <<<<<<< HEAD
:drivers
/i2c
/i2c
-core
.c
103 return driver
->probe(client
);
105 status
= driver
->probe(client
);
107 client
->driver
= NULL
;
109 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:drivers
/i2c
/i2c
-core
.c
112 static int i2c_device_remove(struct device
*dev
)
114 struct i2c_client
*client
= to_i2c_client(dev
);
115 struct i2c_driver
*driver
;
121 driver
= to_i2c_driver(dev
->driver
);
122 if (driver
->remove
) {
123 dev_dbg(dev
, "remove\n");
124 status
= driver
->remove(client
);
130 client
->driver
= NULL
;
134 static void i2c_device_shutdown(struct device
*dev
)
136 struct i2c_driver
*driver
;
140 driver
= to_i2c_driver(dev
->driver
);
141 if (driver
->shutdown
)
142 driver
->shutdown(to_i2c_client(dev
));
145 static int i2c_device_suspend(struct device
* dev
, pm_message_t mesg
)
147 struct i2c_driver
*driver
;
151 driver
= to_i2c_driver(dev
->driver
);
152 if (!driver
->suspend
)
154 return driver
->suspend(to_i2c_client(dev
), mesg
);
157 static int i2c_device_resume(struct device
* dev
)
159 struct i2c_driver
*driver
;
163 driver
= to_i2c_driver(dev
->driver
);
166 return driver
->resume(to_i2c_client(dev
));
169 static void i2c_client_release(struct device
*dev
)
171 struct i2c_client
*client
= to_i2c_client(dev
);
172 complete(&client
->released
);
175 static void i2c_client_dev_release(struct device
*dev
)
177 kfree(to_i2c_client(dev
));
180 static ssize_t
show_client_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
182 struct i2c_client
*client
= to_i2c_client(dev
);
183 return sprintf(buf
, "%s\n", client
->name
);
186 static ssize_t
show_modalias(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
188 struct i2c_client
*client
= to_i2c_client(dev
);
189 return client
->driver_name
190 ? sprintf(buf
, "%s\n", client
->driver_name
)
194 static struct device_attribute i2c_dev_attrs
[] = {
195 __ATTR(name
, S_IRUGO
, show_client_name
, NULL
),
196 /* modalias helps coldplug: modprobe $(cat .../modalias) */
197 __ATTR(modalias
, S_IRUGO
, show_modalias
, NULL
),
201 static struct bus_type i2c_bus_type
= {
203 .dev_attrs
= i2c_dev_attrs
,
204 .match
= i2c_device_match
,
205 .uevent
= i2c_device_uevent
,
206 .probe
= i2c_device_probe
,
207 .remove
= i2c_device_remove
,
208 .shutdown
= i2c_device_shutdown
,
209 .suspend
= i2c_device_suspend
,
210 .resume
= i2c_device_resume
,
215 * i2c_verify_client - return parameter as i2c_client, or NULL
216 * @dev: device, probably from some driver model iterator
218 * When traversing the driver model tree, perhaps using driver model
219 * iterators like @device_for_each_child(), you can't assume very much
220 * about the nodes you find. Use this function to avoid oopses caused
221 * by wrongly treating some non-I2C device as an i2c_client.
223 struct i2c_client
*i2c_verify_client(struct device
*dev
)
225 return (dev
->bus
== &i2c_bus_type
)
229 EXPORT_SYMBOL(i2c_verify_client
);
233 * i2c_new_device - instantiate an i2c device for use with a new style driver
234 * @adap: the adapter managing the device
235 * @info: describes one I2C device; bus_num is ignored
238 * Create a device to work with a new style i2c driver, where binding is
239 * handled through driver model probe()/remove() methods. This call is not
240 * appropriate for use by mainboad initialization logic, which usually runs
241 * during an arch_initcall() long 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.
247 i2c_new_device(struct i2c_adapter
*adap
, struct i2c_board_info
const *info
)
249 struct i2c_client
*client
;
252 client
= kzalloc(sizeof *client
, GFP_KERNEL
);
256 client
->adapter
= adap
;
258 client
->dev
.platform_data
= info
->platform_data
;
259 device_init_wakeup(&client
->dev
, info
->flags
& I2C_CLIENT_WAKE
);
261 client
->flags
= info
->flags
& ~I2C_CLIENT_WAKE
;
262 client
->addr
= info
->addr
;
263 client
->irq
= info
->irq
;
265 strlcpy(client
->driver_name
, info
->driver_name
,
266 sizeof(client
->driver_name
));
267 strlcpy(client
->name
, info
->type
, sizeof(client
->name
));
269 /* a new style driver may be bound to this device when we
270 * return from this function, or any later moment (e.g. maybe
271 * hotplugging will load the driver module). and the device
272 * refcount model is the standard driver model one.
274 status
= i2c_attach_client(client
);
281 EXPORT_SYMBOL_GPL(i2c_new_device
);
285 * i2c_unregister_device - reverse effect of i2c_new_device()
286 * @client: value returned from i2c_new_device()
289 void i2c_unregister_device(struct i2c_client
*client
)
291 struct i2c_adapter
*adapter
= client
->adapter
;
292 struct i2c_driver
*driver
= client
->driver
;
294 if (driver
&& !is_newstyle_driver(driver
)) {
295 dev_err(&client
->dev
, "can't unregister devices "
296 "with legacy drivers\n");
301 mutex_lock(&adapter
->clist_lock
);
302 list_del(&client
->list
);
303 mutex_unlock(&adapter
->clist_lock
);
305 device_unregister(&client
->dev
);
307 EXPORT_SYMBOL_GPL(i2c_unregister_device
);
310 static int dummy_nop(struct i2c_client
*client
)
315 static struct i2c_driver dummy_driver
= {
316 .driver
.name
= "dummy",
322 * i2c_new_dummy - return a new i2c device bound to a dummy driver
323 * @adapter: the adapter managing the device
324 * @address: seven bit address to be used
325 * @type: optional label used for i2c_client.name
328 * This returns an I2C client bound to the "dummy" driver, intended for use
329 * with devices that consume multiple addresses. Examples of such chips
330 * include various EEPROMS (like 24c04 and 24c08 models).
332 * These dummy devices have two main uses. First, most I2C and SMBus calls
333 * except i2c_transfer() need a client handle; the dummy will be that handle.
334 * And second, this prevents the specified address from being bound to a
337 * This returns the new i2c client, which should be saved for later use with
338 * i2c_unregister_device(); or NULL to indicate an error.
341 i2c_new_dummy(struct i2c_adapter
*adapter
, u16 address
, const char *type
)
343 struct i2c_board_info info
= {
344 .driver_name
= "dummy",
349 strlcpy(info
.type
, type
, sizeof info
.type
);
350 return i2c_new_device(adapter
, &info
);
352 EXPORT_SYMBOL_GPL(i2c_new_dummy
);
354 /* ------------------------------------------------------------------------- */
356 /* I2C bus adapters -- one roots each I2C or SMBUS segment */
358 static void i2c_adapter_dev_release(struct device
*dev
)
360 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
361 complete(&adap
->dev_released
);
365 show_adapter_name(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
367 struct i2c_adapter
*adap
= to_i2c_adapter(dev
);
368 return sprintf(buf
, "%s\n", adap
->name
);
371 static struct device_attribute i2c_adapter_attrs
[] = {
372 __ATTR(name
, S_IRUGO
, show_adapter_name
, NULL
),
376 static struct class i2c_adapter_class
= {
377 .owner
= THIS_MODULE
,
378 .name
= "i2c-adapter",
379 .dev_attrs
= i2c_adapter_attrs
,
382 static void i2c_scan_static_board_info(struct i2c_adapter
*adapter
)
384 struct i2c_devinfo
*devinfo
;
386 mutex_lock(&__i2c_board_lock
);
387 list_for_each_entry(devinfo
, &__i2c_board_list
, list
) {
388 if (devinfo
->busnum
== adapter
->nr
389 && !i2c_new_device(adapter
,
390 &devinfo
->board_info
))
391 printk(KERN_ERR
"i2c-core: can't create i2c%d-%04x\n",
392 i2c_adapter_id(adapter
),
393 devinfo
->board_info
.addr
);
395 mutex_unlock(&__i2c_board_lock
);
398 static int i2c_do_add_adapter(struct device_driver
*d
, void *data
)
400 struct i2c_driver
*driver
= to_i2c_driver(d
);
401 struct i2c_adapter
*adap
= data
;
403 if (driver
->attach_adapter
) {
404 /* We ignore the return code; if it fails, too bad */
405 driver
->attach_adapter(adap
);
410 static int i2c_register_adapter(struct i2c_adapter
*adap
)
414 mutex_init(&adap
->bus_lock
);
415 mutex_init(&adap
->clist_lock
);
416 INIT_LIST_HEAD(&adap
->clients
);
418 mutex_lock(&core_lock
);
420 /* Add the adapter to the driver core.
421 * If the parent pointer is not set up,
422 * we add this adapter to the host bus.
424 if (adap
->dev
.parent
== NULL
) {
425 adap
->dev
.parent
= &platform_bus
;
426 pr_debug("I2C adapter driver [%s] forgot to specify "
427 "physical device\n", adap
->name
);
429 sprintf(adap
->dev
.bus_id
, "i2c-%d", adap
->nr
);
430 adap
->dev
.release
= &i2c_adapter_dev_release
;
431 adap
->dev
.class = &i2c_adapter_class
;
432 res
= device_register(&adap
->dev
);
436 dev_dbg(&adap
->dev
, "adapter [%s] registered\n", adap
->name
);
438 /* create pre-declared device nodes for new-style drivers */
439 if (adap
->nr
< __i2c_first_dynamic_bus_num
)
440 i2c_scan_static_board_info(adap
);
442 /* let legacy drivers scan this bus for matching devices */
443 dummy
= bus_for_each_drv(&i2c_bus_type
, NULL
, adap
,
447 mutex_unlock(&core_lock
);
451 idr_remove(&i2c_adapter_idr
, adap
->nr
);
456 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
457 * @adapter: the adapter to add
460 * This routine is used to declare an I2C adapter when its bus number
461 * doesn't matter. Examples: for I2C adapters dynamically added by
462 * USB links or PCI plugin cards.
464 * When this returns zero, a new bus number was allocated and stored
465 * in adap->nr, and the specified adapter became available for clients.
466 * Otherwise, a negative errno value is returned.
468 int i2c_add_adapter(struct i2c_adapter
*adapter
)
473 if (idr_pre_get(&i2c_adapter_idr
, GFP_KERNEL
) == 0)
476 mutex_lock(&core_lock
);
477 /* "above" here means "above or equal to", sigh */
478 res
= idr_get_new_above(&i2c_adapter_idr
, adapter
,
479 __i2c_first_dynamic_bus_num
, &id
);
480 mutex_unlock(&core_lock
);
489 return i2c_register_adapter(adapter
);
491 EXPORT_SYMBOL(i2c_add_adapter
);
494 * i2c_add_numbered_adapter - declare i2c adapter, use static bus number
495 * @adap: the adapter to register (with adap->nr initialized)
498 * This routine is used to declare an I2C adapter when its bus number
499 * matters. Example: for I2C adapters from system-on-chip CPUs, or
500 * otherwise built in to the system's mainboard, and where i2c_board_info
501 * is used to properly configure I2C devices.
503 * If no devices have pre-been declared for this bus, then be sure to
504 * register the adapter before any dynamically allocated ones. Otherwise
505 * the required bus ID may not be available.
507 * When this returns zero, the specified adapter became available for
508 * clients using the bus number provided in adap->nr. Also, the table
509 * of I2C devices pre-declared using i2c_register_board_info() is scanned,
510 * and the appropriate driver model device nodes are created. Otherwise, a
511 * negative errno value is returned.
513 int i2c_add_numbered_adapter(struct i2c_adapter
*adap
)
518 if (adap
->nr
& ~MAX_ID_MASK
)
522 if (idr_pre_get(&i2c_adapter_idr
, GFP_KERNEL
) == 0)
525 mutex_lock(&core_lock
);
526 /* "above" here means "above or equal to", sigh;
527 * we need the "equal to" result to force the result
529 status
= idr_get_new_above(&i2c_adapter_idr
, adap
, adap
->nr
, &id
);
530 if (status
== 0 && id
!= adap
->nr
) {
532 idr_remove(&i2c_adapter_idr
, id
);
534 mutex_unlock(&core_lock
);
535 if (status
== -EAGAIN
)
539 status
= i2c_register_adapter(adap
);
542 EXPORT_SYMBOL_GPL(i2c_add_numbered_adapter
);
544 static int i2c_do_del_adapter(struct device_driver
*d
, void *data
)
546 struct i2c_driver
*driver
= to_i2c_driver(d
);
547 struct i2c_adapter
*adapter
= data
;
550 if (!driver
->detach_adapter
)
552 res
= driver
->detach_adapter(adapter
);
554 dev_err(&adapter
->dev
, "detach_adapter failed (%d) "
555 "for driver [%s]\n", res
, driver
->driver
.name
);
560 * i2c_del_adapter - unregister I2C adapter
561 * @adap: the adapter being unregistered
564 * This unregisters an I2C adapter which was previously registered
565 * by @i2c_add_adapter or @i2c_add_numbered_adapter.
567 int i2c_del_adapter(struct i2c_adapter
*adap
)
569 struct list_head
*item
, *_n
;
570 struct i2c_client
*client
;
573 mutex_lock(&core_lock
);
575 /* First make sure that this adapter was ever added */
576 if (idr_find(&i2c_adapter_idr
, adap
->nr
) != adap
) {
577 pr_debug("i2c-core: attempting to delete unregistered "
578 "adapter [%s]\n", adap
->name
);
583 /* Tell drivers about this removal */
584 res
= bus_for_each_drv(&i2c_bus_type
, NULL
, adap
,
589 /* detach any active clients. This must be done first, because
590 * it can fail; in which case we give up. */
591 list_for_each_safe(item
, _n
, &adap
->clients
) {
592 struct i2c_driver
*driver
;
594 client
= list_entry(item
, struct i2c_client
, list
);
595 driver
= client
->driver
;
597 /* new style, follow standard driver model */
598 if (!driver
|| is_newstyle_driver(driver
)) {
599 i2c_unregister_device(client
);
603 /* legacy drivers create and remove clients themselves */
604 if ((res
= driver
->detach_client(client
))) {
605 dev_err(&adap
->dev
, "detach_client failed for client "
606 "[%s] at address 0x%02x\n", client
->name
,
612 /* clean up the sysfs representation */
613 init_completion(&adap
->dev_released
);
614 device_unregister(&adap
->dev
);
616 /* wait for sysfs to drop all references */
617 wait_for_completion(&adap
->dev_released
);
620 idr_remove(&i2c_adapter_idr
, adap
->nr
);
622 dev_dbg(&adap
->dev
, "adapter [%s] unregistered\n", adap
->name
);
625 mutex_unlock(&core_lock
);
628 EXPORT_SYMBOL(i2c_del_adapter
);
631 /* ------------------------------------------------------------------------- */
634 * An i2c_driver is used with one or more i2c_client (device) nodes to access
635 * i2c slave chips, on a bus instance associated with some i2c_adapter. There
636 * are two models for binding the driver to its device: "new style" drivers
637 * follow the standard Linux driver model and just respond to probe() calls
638 * issued if the driver core sees they match(); "legacy" drivers create device
642 int i2c_register_driver(struct module
*owner
, struct i2c_driver
*driver
)
646 /* new style driver methods can't mix with legacy ones */
647 if (is_newstyle_driver(driver
)) {
648 if (driver
->attach_adapter
|| driver
->detach_adapter
649 || driver
->detach_client
) {
651 "i2c-core: driver [%s] is confused\n",
652 driver
->driver
.name
);
657 /* add the driver to the list of i2c drivers in the driver core */
658 driver
->driver
.owner
= owner
;
659 driver
->driver
.bus
= &i2c_bus_type
;
661 /* for new style drivers, when registration returns the driver core
662 * will have called probe() for all matching-but-unbound devices.
664 res
= driver_register(&driver
->driver
);
668 mutex_lock(&core_lock
);
670 pr_debug("i2c-core: driver [%s] registered\n", driver
->driver
.name
);
672 /* legacy drivers scan i2c busses directly */
673 if (driver
->attach_adapter
) {
674 struct i2c_adapter
*adapter
;
676 down(&i2c_adapter_class
.sem
);
677 list_for_each_entry(adapter
, &i2c_adapter_class
.devices
,
679 driver
->attach_adapter(adapter
);
681 up(&i2c_adapter_class
.sem
);
684 mutex_unlock(&core_lock
);
687 EXPORT_SYMBOL(i2c_register_driver
);
690 * i2c_del_driver - unregister I2C driver
691 * @driver: the driver being unregistered
694 void i2c_del_driver(struct i2c_driver
*driver
)
696 struct list_head
*item2
, *_n
;
697 struct i2c_client
*client
;
698 struct i2c_adapter
*adap
;
700 mutex_lock(&core_lock
);
702 /* new-style driver? */
703 if (is_newstyle_driver(driver
))
706 /* Have a look at each adapter, if clients of this driver are still
707 * attached. If so, detach them to be able to kill the driver
710 down(&i2c_adapter_class
.sem
);
711 list_for_each_entry(adap
, &i2c_adapter_class
.devices
, dev
.node
) {
712 if (driver
->detach_adapter
) {
713 if (driver
->detach_adapter(adap
)) {
714 dev_err(&adap
->dev
, "detach_adapter failed "
716 driver
->driver
.name
);
719 list_for_each_safe(item2
, _n
, &adap
->clients
) {
720 client
= list_entry(item2
, struct i2c_client
, list
);
721 if (client
->driver
!= driver
)
723 dev_dbg(&adap
->dev
, "detaching client [%s] "
724 "at 0x%02x\n", client
->name
,
726 if (driver
->detach_client(client
)) {
727 dev_err(&adap
->dev
, "detach_client "
728 "failed for client [%s] at "
729 "0x%02x\n", client
->name
,
735 up(&i2c_adapter_class
.sem
);
738 driver_unregister(&driver
->driver
);
739 pr_debug("i2c-core: driver [%s] unregistered\n", driver
->driver
.name
);
741 mutex_unlock(&core_lock
);
743 EXPORT_SYMBOL(i2c_del_driver
);
745 /* ------------------------------------------------------------------------- */
747 static int __i2c_check_addr(struct device
*dev
, void *addrp
)
749 struct i2c_client
*client
= i2c_verify_client(dev
);
750 int addr
= *(int *)addrp
;
752 if (client
&& client
->addr
== addr
)
757 static int i2c_check_addr(struct i2c_adapter
*adapter
, int addr
)
759 return device_for_each_child(&adapter
->dev
, &addr
, __i2c_check_addr
);
762 int i2c_attach_client(struct i2c_client
*client
)
764 struct i2c_adapter
*adapter
= client
->adapter
;
767 client
->dev
.parent
= &client
->adapter
->dev
;
768 client
->dev
.bus
= &i2c_bus_type
;
771 client
->dev
.driver
= &client
->driver
->driver
;
773 if (client
->driver
&& !is_newstyle_driver(client
->driver
)) {
774 client
->dev
.release
= i2c_client_release
;
775 client
->dev
.uevent_suppress
= 1;
777 client
->dev
.release
= i2c_client_dev_release
;
779 snprintf(&client
->dev
.bus_id
[0], sizeof(client
->dev
.bus_id
),
780 "%d-%04x", i2c_adapter_id(adapter
), client
->addr
);
781 res
= device_register(&client
->dev
);
785 mutex_lock(&adapter
->clist_lock
);
786 list_add_tail(&client
->list
, &adapter
->clients
);
787 mutex_unlock(&adapter
->clist_lock
);
789 dev_dbg(&adapter
->dev
, "client [%s] registered with bus id %s\n",
790 client
->name
, client
->dev
.bus_id
);
792 if (adapter
->client_register
) {
793 if (adapter
->client_register(client
)) {
794 dev_dbg(&adapter
->dev
, "client_register "
795 "failed for client [%s] at 0x%02x\n",
796 client
->name
, client
->addr
);
803 dev_err(&adapter
->dev
, "Failed to attach i2c client %s at 0x%02x "
804 "(%d)\n", client
->name
, client
->addr
, res
);
807 EXPORT_SYMBOL(i2c_attach_client
);
809 int i2c_detach_client(struct i2c_client
*client
)
811 struct i2c_adapter
*adapter
= client
->adapter
;
814 if (adapter
->client_unregister
) {
815 res
= adapter
->client_unregister(client
);
817 dev_err(&client
->dev
,
818 "client_unregister [%s] failed, "
819 "client not detached\n", client
->name
);
824 mutex_lock(&adapter
->clist_lock
);
825 list_del(&client
->list
);
826 mutex_unlock(&adapter
->clist_lock
);
828 init_completion(&client
->released
);
829 device_unregister(&client
->dev
);
830 wait_for_completion(&client
->released
);
835 EXPORT_SYMBOL(i2c_detach_client
);
838 * i2c_use_client - increments the reference count of the i2c client structure
839 * @client: the client being referenced
841 * Each live reference to a client should be refcounted. The driver model does
842 * that automatically as part of driver binding, so that most drivers don't
843 * need to do this explicitly: they hold a reference until they're unbound
846 * A pointer to the client with the incremented reference counter is returned.
848 struct i2c_client
*i2c_use_client(struct i2c_client
*client
)
850 get_device(&client
->dev
);
853 EXPORT_SYMBOL(i2c_use_client
);
856 * i2c_release_client - release a use of the i2c client structure
857 * @client: the client being no longer referenced
859 * Must be called when a user of a client is finished with it.
861 void i2c_release_client(struct i2c_client
*client
)
863 put_device(&client
->dev
);
865 EXPORT_SYMBOL(i2c_release_client
);
872 static int i2c_cmd(struct device
*dev
, void *_arg
)
874 struct i2c_client
*client
= i2c_verify_client(dev
);
875 struct i2c_cmd_arg
*arg
= _arg
;
877 if (client
&& client
->driver
&& client
->driver
->command
)
878 client
->driver
->command(client
, arg
->cmd
, arg
->arg
);
882 void i2c_clients_command(struct i2c_adapter
*adap
, unsigned int cmd
, void *arg
)
884 struct i2c_cmd_arg cmd_arg
;
888 device_for_each_child(&adap
->dev
, &cmd_arg
, i2c_cmd
);
890 EXPORT_SYMBOL(i2c_clients_command
);
892 static int __init
i2c_init(void)
896 retval
= bus_register(&i2c_bus_type
);
899 retval
= class_register(&i2c_adapter_class
);
902 retval
= i2c_add_driver(&dummy_driver
);
908 class_unregister(&i2c_adapter_class
);
910 bus_unregister(&i2c_bus_type
);
914 static void __exit
i2c_exit(void)
916 i2c_del_driver(&dummy_driver
);
917 class_unregister(&i2c_adapter_class
);
918 bus_unregister(&i2c_bus_type
);
921 subsys_initcall(i2c_init
);
922 module_exit(i2c_exit
);
924 /* ----------------------------------------------------
925 * the functional interface to the i2c busses.
926 * ----------------------------------------------------
929 int i2c_transfer(struct i2c_adapter
* adap
, struct i2c_msg
*msgs
, int num
)
933 if (adap
->algo
->master_xfer
) {
935 for (ret
= 0; ret
< num
; ret
++) {
936 dev_dbg(&adap
->dev
, "master_xfer[%d] %c, addr=0x%02x, "
937 "len=%d%s\n", ret
, (msgs
[ret
].flags
& I2C_M_RD
)
938 ? 'R' : 'W', msgs
[ret
].addr
, msgs
[ret
].len
,
939 (msgs
[ret
].flags
& I2C_M_RECV_LEN
) ? "+" : "");
943 if (in_atomic() || irqs_disabled()) {
944 ret
= mutex_trylock(&adap
->bus_lock
);
946 /* I2C activity is ongoing. */
949 mutex_lock_nested(&adap
->bus_lock
, adap
->level
);
952 ret
= adap
->algo
->master_xfer(adap
,msgs
,num
);
953 mutex_unlock(&adap
->bus_lock
);
957 dev_dbg(&adap
->dev
, "I2C level transfers not supported\n");
961 EXPORT_SYMBOL(i2c_transfer
);
963 int i2c_master_send(struct i2c_client
*client
,const char *buf
,int count
)
966 struct i2c_adapter
*adap
=client
->adapter
;
969 msg
.addr
= client
->addr
;
970 msg
.flags
= client
->flags
& I2C_M_TEN
;
972 msg
.buf
= (char *)buf
;
974 ret
= i2c_transfer(adap
, &msg
, 1);
976 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
977 transmitted, else error code. */
978 return (ret
== 1) ? count
: ret
;
980 EXPORT_SYMBOL(i2c_master_send
);
982 int i2c_master_recv(struct i2c_client
*client
, char *buf
,int count
)
984 struct i2c_adapter
*adap
=client
->adapter
;
988 msg
.addr
= client
->addr
;
989 msg
.flags
= client
->flags
& I2C_M_TEN
;
990 msg
.flags
|= I2C_M_RD
;
994 ret
= i2c_transfer(adap
, &msg
, 1);
996 /* If everything went ok (i.e. 1 msg transmitted), return #bytes
997 transmitted, else error code. */
998 return (ret
== 1) ? count
: ret
;
1000 EXPORT_SYMBOL(i2c_master_recv
);
1002 /* ----------------------------------------------------
1003 * the i2c address scanning function
1004 * Will not work for 10-bit addresses!
1005 * ----------------------------------------------------
1007 static int i2c_probe_address(struct i2c_adapter
*adapter
, int addr
, int kind
,
1008 int (*found_proc
) (struct i2c_adapter
*, int, int))
1012 /* Make sure the address is valid */
1013 if (addr
< 0x03 || addr
> 0x77) {
1014 dev_warn(&adapter
->dev
, "Invalid probe address 0x%02x\n",
1019 /* Skip if already in use */
1020 if (i2c_check_addr(adapter
, addr
))
1023 /* Make sure there is something at this address, unless forced */
1025 if (i2c_smbus_xfer(adapter
, addr
, 0, 0, 0,
1026 I2C_SMBUS_QUICK
, NULL
) < 0)
1029 /* prevent 24RF08 corruption */
1030 if ((addr
& ~0x0f) == 0x50)
1031 i2c_smbus_xfer(adapter
, addr
, 0, 0, 0,
1032 I2C_SMBUS_QUICK
, NULL
);
1035 /* Finally call the custom detection function */
1036 err
= found_proc(adapter
, addr
, kind
);
1037 /* -ENODEV can be returned if there is a chip at the given address
1038 but it isn't supported by this chip driver. We catch it here as
1039 this isn't an error. */
1044 dev_warn(&adapter
->dev
, "Client creation failed at 0x%x (%d)\n",
1049 int i2c_probe(struct i2c_adapter
*adapter
,
1050 const struct i2c_client_address_data
*address_data
,
1051 int (*found_proc
) (struct i2c_adapter
*, int, int))
1054 int adap_id
= i2c_adapter_id(adapter
);
1056 /* Force entries are done first, and are not affected by ignore
1058 if (address_data
->forces
) {
1059 const unsigned short * const *forces
= address_data
->forces
;
1062 for (kind
= 0; forces
[kind
]; kind
++) {
1063 for (i
= 0; forces
[kind
][i
] != I2C_CLIENT_END
;
1065 if (forces
[kind
][i
] == adap_id
1066 || forces
[kind
][i
] == ANY_I2C_BUS
) {
1067 dev_dbg(&adapter
->dev
, "found force "
1068 "parameter for adapter %d, "
1069 "addr 0x%02x, kind %d\n",
1070 adap_id
, forces
[kind
][i
+ 1],
1072 err
= i2c_probe_address(adapter
,
1073 forces
[kind
][i
+ 1],
1082 /* Stop here if we can't use SMBUS_QUICK */
1083 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_QUICK
)) {
1084 if (address_data
->probe
[0] == I2C_CLIENT_END
1085 && address_data
->normal_i2c
[0] == I2C_CLIENT_END
)
1088 dev_warn(&adapter
->dev
, "SMBus Quick command not supported, "
1089 "can't probe for chips\n");
1093 /* Probe entries are done second, and are not affected by ignore
1095 for (i
= 0; address_data
->probe
[i
] != I2C_CLIENT_END
; i
+= 2) {
1096 if (address_data
->probe
[i
] == adap_id
1097 || address_data
->probe
[i
] == ANY_I2C_BUS
) {
1098 dev_dbg(&adapter
->dev
, "found probe parameter for "
1099 "adapter %d, addr 0x%02x\n", adap_id
,
1100 address_data
->probe
[i
+ 1]);
1101 err
= i2c_probe_address(adapter
,
1102 address_data
->probe
[i
+ 1],
1109 /* Normal entries are done last, unless shadowed by an ignore entry */
1110 for (i
= 0; address_data
->normal_i2c
[i
] != I2C_CLIENT_END
; i
+= 1) {
1114 for (j
= 0; address_data
->ignore
[j
] != I2C_CLIENT_END
;
1116 if ((address_data
->ignore
[j
] == adap_id
||
1117 address_data
->ignore
[j
] == ANY_I2C_BUS
)
1118 && address_data
->ignore
[j
+ 1]
1119 == address_data
->normal_i2c
[i
]) {
1120 dev_dbg(&adapter
->dev
, "found ignore "
1121 "parameter for adapter %d, "
1122 "addr 0x%02x\n", adap_id
,
1123 address_data
->ignore
[j
+ 1]);
1131 dev_dbg(&adapter
->dev
, "found normal entry for adapter %d, "
1132 "addr 0x%02x\n", adap_id
,
1133 address_data
->normal_i2c
[i
]);
1134 err
= i2c_probe_address(adapter
, address_data
->normal_i2c
[i
],
1142 EXPORT_SYMBOL(i2c_probe
);
1145 i2c_new_probed_device(struct i2c_adapter
*adap
,
1146 struct i2c_board_info
*info
,
1147 unsigned short const *addr_list
)
1151 /* Stop here if the bus doesn't support probing */
1152 if (!i2c_check_functionality(adap
, I2C_FUNC_SMBUS_READ_BYTE
)) {
1153 dev_err(&adap
->dev
, "Probing not supported\n");
1157 for (i
= 0; addr_list
[i
] != I2C_CLIENT_END
; i
++) {
1158 /* Check address validity */
1159 if (addr_list
[i
] < 0x03 || addr_list
[i
] > 0x77) {
1160 dev_warn(&adap
->dev
, "Invalid 7-bit address "
1161 "0x%02x\n", addr_list
[i
]);
1165 /* Check address availability */
1166 if (i2c_check_addr(adap
, addr_list
[i
])) {
1167 dev_dbg(&adap
->dev
, "Address 0x%02x already in "
1168 "use, not probing\n", addr_list
[i
]);
1172 /* Test address responsiveness
1173 The default probe method is a quick write, but it is known
1174 to corrupt the 24RF08 EEPROMs due to a state machine bug,
1175 and could also irreversibly write-protect some EEPROMs, so
1176 for address ranges 0x30-0x37 and 0x50-0x5f, we use a byte
1177 read instead. Also, some bus drivers don't implement
1178 quick write, so we fallback to a byte read it that case
1180 if ((addr_list
[i
] & ~0x07) == 0x30
1181 || (addr_list
[i
] & ~0x0f) == 0x50
1182 || !i2c_check_functionality(adap
, I2C_FUNC_SMBUS_QUICK
)) {
1183 if (i2c_smbus_xfer(adap
, addr_list
[i
], 0,
1185 I2C_SMBUS_BYTE
, NULL
) >= 0)
1188 if (i2c_smbus_xfer(adap
, addr_list
[i
], 0,
1190 I2C_SMBUS_QUICK
, NULL
) >= 0)
1195 if (addr_list
[i
] == I2C_CLIENT_END
) {
1196 dev_dbg(&adap
->dev
, "Probing failed, no device found\n");
1200 info
->addr
= addr_list
[i
];
1201 return i2c_new_device(adap
, info
);
1203 EXPORT_SYMBOL_GPL(i2c_new_probed_device
);
1205 struct i2c_adapter
* i2c_get_adapter(int id
)
1207 struct i2c_adapter
*adapter
;
1209 mutex_lock(&core_lock
);
1210 adapter
= (struct i2c_adapter
*)idr_find(&i2c_adapter_idr
, id
);
1211 if (adapter
&& !try_module_get(adapter
->owner
))
1214 mutex_unlock(&core_lock
);
1217 EXPORT_SYMBOL(i2c_get_adapter
);
1219 void i2c_put_adapter(struct i2c_adapter
*adap
)
1221 module_put(adap
->owner
);
1223 EXPORT_SYMBOL(i2c_put_adapter
);
1225 /* The SMBus parts */
1227 #define POLY (0x1070U << 3)
1233 for(i
= 0; i
< 8; i
++) {
1238 return (u8
)(data
>> 8);
1241 /* Incremental CRC8 over count bytes in the array pointed to by p */
1242 static u8
i2c_smbus_pec(u8 crc
, u8
*p
, size_t count
)
1246 for(i
= 0; i
< count
; i
++)
1247 crc
= crc8((crc
^ p
[i
]) << 8);
1251 /* Assume a 7-bit address, which is reasonable for SMBus */
1252 static u8
i2c_smbus_msg_pec(u8 pec
, struct i2c_msg
*msg
)
1254 /* The address will be sent first */
1255 u8 addr
= (msg
->addr
<< 1) | !!(msg
->flags
& I2C_M_RD
);
1256 pec
= i2c_smbus_pec(pec
, &addr
, 1);
1258 /* The data buffer follows */
1259 return i2c_smbus_pec(pec
, msg
->buf
, msg
->len
);
1262 /* Used for write only transactions */
1263 static inline void i2c_smbus_add_pec(struct i2c_msg
*msg
)
1265 msg
->buf
[msg
->len
] = i2c_smbus_msg_pec(0, msg
);
1269 /* Return <0 on CRC error
1270 If there was a write before this read (most cases) we need to take the
1271 partial CRC from the write part into account.
1272 Note that this function does modify the message (we need to decrease the
1273 message length to hide the CRC byte from the caller). */
1274 static int i2c_smbus_check_pec(u8 cpec
, struct i2c_msg
*msg
)
1276 u8 rpec
= msg
->buf
[--msg
->len
];
1277 cpec
= i2c_smbus_msg_pec(cpec
, msg
);
1280 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
1287 s32
i2c_smbus_write_quick(struct i2c_client
*client
, u8 value
)
1289 return i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
1290 value
,0,I2C_SMBUS_QUICK
,NULL
);
1292 EXPORT_SYMBOL(i2c_smbus_write_quick
);
1294 s32
i2c_smbus_read_byte(struct i2c_client
*client
)
1296 union i2c_smbus_data data
;
1297 if (i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
1298 I2C_SMBUS_READ
,0,I2C_SMBUS_BYTE
, &data
))
1303 EXPORT_SYMBOL(i2c_smbus_read_byte
);
1305 s32
i2c_smbus_write_byte(struct i2c_client
*client
, u8 value
)
1307 return i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
1308 I2C_SMBUS_WRITE
, value
, I2C_SMBUS_BYTE
, NULL
);
1310 EXPORT_SYMBOL(i2c_smbus_write_byte
);
1312 s32
i2c_smbus_read_byte_data(struct i2c_client
*client
, u8 command
)
1314 union i2c_smbus_data data
;
1315 if (i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
1316 I2C_SMBUS_READ
,command
, I2C_SMBUS_BYTE_DATA
,&data
))
1321 EXPORT_SYMBOL(i2c_smbus_read_byte_data
);
1323 s32
i2c_smbus_write_byte_data(struct i2c_client
*client
, u8 command
, u8 value
)
1325 union i2c_smbus_data data
;
1327 return i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
1328 I2C_SMBUS_WRITE
,command
,
1329 I2C_SMBUS_BYTE_DATA
,&data
);
1331 EXPORT_SYMBOL(i2c_smbus_write_byte_data
);
1333 s32
i2c_smbus_read_word_data(struct i2c_client
*client
, u8 command
)
1335 union i2c_smbus_data data
;
1336 if (i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
1337 I2C_SMBUS_READ
,command
, I2C_SMBUS_WORD_DATA
, &data
))
1342 EXPORT_SYMBOL(i2c_smbus_read_word_data
);
1344 s32
i2c_smbus_write_word_data(struct i2c_client
*client
, u8 command
, u16 value
)
1346 union i2c_smbus_data data
;
1348 return i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
1349 I2C_SMBUS_WRITE
,command
,
1350 I2C_SMBUS_WORD_DATA
,&data
);
1352 EXPORT_SYMBOL(i2c_smbus_write_word_data
);
1355 * i2c_smbus_read_block_data - SMBus block read request
1356 * @client: Handle to slave device
1357 * @command: Command byte issued to let the slave know what data should
1359 * @values: Byte array into which data will be read; big enough to hold
1360 * the data returned by the slave. SMBus allows at most 32 bytes.
1362 * Returns the number of bytes read in the slave's response, else a
1363 * negative number to indicate some kind of error.
1365 * Note that using this function requires that the client's adapter support
1366 * the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
1367 * support this; its emulation through I2C messaging relies on a specific
1368 * mechanism (I2C_M_RECV_LEN) which may not be implemented.
1370 s32
i2c_smbus_read_block_data(struct i2c_client
*client
, u8 command
,
1373 union i2c_smbus_data data
;
1375 if (i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
1376 I2C_SMBUS_READ
, command
,
1377 I2C_SMBUS_BLOCK_DATA
, &data
))
1380 memcpy(values
, &data
.block
[1], data
.block
[0]);
1381 return data
.block
[0];
1383 EXPORT_SYMBOL(i2c_smbus_read_block_data
);
1385 s32
i2c_smbus_write_block_data(struct i2c_client
*client
, u8 command
,
1386 u8 length
, const u8
*values
)
1388 union i2c_smbus_data data
;
1390 if (length
> I2C_SMBUS_BLOCK_MAX
)
1391 length
= I2C_SMBUS_BLOCK_MAX
;
1392 data
.block
[0] = length
;
1393 memcpy(&data
.block
[1], values
, length
);
1394 return i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
1395 I2C_SMBUS_WRITE
,command
,
1396 I2C_SMBUS_BLOCK_DATA
,&data
);
1398 EXPORT_SYMBOL(i2c_smbus_write_block_data
);
1400 /* Returns the number of read bytes */
1401 s32
i2c_smbus_read_i2c_block_data(struct i2c_client
*client
, u8 command
,
1402 u8 length
, u8
*values
)
1404 union i2c_smbus_data data
;
1406 if (length
> I2C_SMBUS_BLOCK_MAX
)
1407 length
= I2C_SMBUS_BLOCK_MAX
;
1408 data
.block
[0] = length
;
1409 if (i2c_smbus_xfer(client
->adapter
,client
->addr
,client
->flags
,
1410 I2C_SMBUS_READ
,command
,
1411 I2C_SMBUS_I2C_BLOCK_DATA
,&data
))
1414 memcpy(values
, &data
.block
[1], data
.block
[0]);
1415 return data
.block
[0];
1417 EXPORT_SYMBOL(i2c_smbus_read_i2c_block_data
);
1419 s32
i2c_smbus_write_i2c_block_data(struct i2c_client
*client
, u8 command
,
1420 u8 length
, const u8
*values
)
1422 union i2c_smbus_data data
;
1424 if (length
> I2C_SMBUS_BLOCK_MAX
)
1425 length
= I2C_SMBUS_BLOCK_MAX
;
1426 data
.block
[0] = length
;
1427 memcpy(data
.block
+ 1, values
, length
);
1428 return i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
1429 I2C_SMBUS_WRITE
, command
,
1430 I2C_SMBUS_I2C_BLOCK_DATA
, &data
);
1432 EXPORT_SYMBOL(i2c_smbus_write_i2c_block_data
);
1434 /* Simulate a SMBus command using the i2c protocol
1435 No checking of parameters is done! */
1436 static s32
i2c_smbus_xfer_emulated(struct i2c_adapter
* adapter
, u16 addr
,
1437 unsigned short flags
,
1438 char read_write
, u8 command
, int size
,
1439 union i2c_smbus_data
* data
)
1441 /* So we need to generate a series of msgs. In the case of writing, we
1442 need to use only one message; when reading, we need two. We initialize
1443 most things with sane defaults, to keep the code below somewhat
1445 unsigned char msgbuf0
[I2C_SMBUS_BLOCK_MAX
+3];
1446 unsigned char msgbuf1
[I2C_SMBUS_BLOCK_MAX
+2];
1447 int num
= read_write
== I2C_SMBUS_READ
?2:1;
1448 struct i2c_msg msg
[2] = { { addr
, flags
, 1, msgbuf0
},
1449 { addr
, flags
| I2C_M_RD
, 0, msgbuf1
}
1454 msgbuf0
[0] = command
;
1456 case I2C_SMBUS_QUICK
:
1458 /* Special case: The read/write field is used as data */
1459 msg
[0].flags
= flags
| (read_write
==I2C_SMBUS_READ
)?I2C_M_RD
:0;
1462 case I2C_SMBUS_BYTE
:
1463 if (read_write
== I2C_SMBUS_READ
) {
1464 /* Special case: only a read! */
1465 msg
[0].flags
= I2C_M_RD
| flags
;
1469 case I2C_SMBUS_BYTE_DATA
:
1470 if (read_write
== I2C_SMBUS_READ
)
1474 msgbuf0
[1] = data
->byte
;
1477 case I2C_SMBUS_WORD_DATA
:
1478 if (read_write
== I2C_SMBUS_READ
)
1482 msgbuf0
[1] = data
->word
& 0xff;
1483 msgbuf0
[2] = data
->word
>> 8;
1486 case I2C_SMBUS_PROC_CALL
:
1487 num
= 2; /* Special case */
1488 read_write
= I2C_SMBUS_READ
;
1491 msgbuf0
[1] = data
->word
& 0xff;
1492 msgbuf0
[2] = data
->word
>> 8;
1494 case I2C_SMBUS_BLOCK_DATA
:
1495 if (read_write
== I2C_SMBUS_READ
) {
1496 msg
[1].flags
|= I2C_M_RECV_LEN
;
1497 msg
[1].len
= 1; /* block length will be added by
1498 the underlying bus driver */
1500 msg
[0].len
= data
->block
[0] + 2;
1501 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 2) {
1502 dev_err(&adapter
->dev
, "smbus_access called with "
1503 "invalid block write size (%d)\n",
1507 for (i
= 1; i
< msg
[0].len
; i
++)
1508 msgbuf0
[i
] = data
->block
[i
-1];
1511 case I2C_SMBUS_BLOCK_PROC_CALL
:
1512 num
= 2; /* Another special case */
1513 read_write
= I2C_SMBUS_READ
;
1514 if (data
->block
[0] > I2C_SMBUS_BLOCK_MAX
) {
1515 dev_err(&adapter
->dev
, "%s called with invalid "
1516 "block proc call size (%d)\n", __FUNCTION__
,
1520 msg
[0].len
= data
->block
[0] + 2;
1521 for (i
= 1; i
< msg
[0].len
; i
++)
1522 msgbuf0
[i
] = data
->block
[i
-1];
1523 msg
[1].flags
|= I2C_M_RECV_LEN
;
1524 msg
[1].len
= 1; /* block length will be added by
1525 the underlying bus driver */
1527 case I2C_SMBUS_I2C_BLOCK_DATA
:
1528 if (read_write
== I2C_SMBUS_READ
) {
1529 msg
[1].len
= data
->block
[0];
1531 msg
[0].len
= data
->block
[0] + 1;
1532 if (msg
[0].len
> I2C_SMBUS_BLOCK_MAX
+ 1) {
1533 dev_err(&adapter
->dev
, "i2c_smbus_xfer_emulated called with "
1534 "invalid block write size (%d)\n",
1538 for (i
= 1; i
<= data
->block
[0]; i
++)
1539 msgbuf0
[i
] = data
->block
[i
];
1543 dev_err(&adapter
->dev
, "smbus_access called with invalid size (%d)\n",
1548 i
= ((flags
& I2C_CLIENT_PEC
) && size
!= I2C_SMBUS_QUICK
1549 && size
!= I2C_SMBUS_I2C_BLOCK_DATA
);
1551 /* Compute PEC if first message is a write */
1552 if (!(msg
[0].flags
& I2C_M_RD
)) {
1553 if (num
== 1) /* Write only */
1554 i2c_smbus_add_pec(&msg
[0]);
1555 else /* Write followed by read */
1556 partial_pec
= i2c_smbus_msg_pec(0, &msg
[0]);
1558 /* Ask for PEC if last message is a read */
1559 if (msg
[num
-1].flags
& I2C_M_RD
)
1563 if (i2c_transfer(adapter
, msg
, num
) < 0)
1566 /* Check PEC if last message is a read */
1567 if (i
&& (msg
[num
-1].flags
& I2C_M_RD
)) {
1568 if (i2c_smbus_check_pec(partial_pec
, &msg
[num
-1]) < 0)
1572 if (read_write
== I2C_SMBUS_READ
)
1574 case I2C_SMBUS_BYTE
:
1575 data
->byte
= msgbuf0
[0];
1577 case I2C_SMBUS_BYTE_DATA
:
1578 data
->byte
= msgbuf1
[0];
1580 case I2C_SMBUS_WORD_DATA
:
1581 case I2C_SMBUS_PROC_CALL
:
1582 data
->word
= msgbuf1
[0] | (msgbuf1
[1] << 8);
1584 case I2C_SMBUS_I2C_BLOCK_DATA
:
1585 for (i
= 0; i
< data
->block
[0]; i
++)
1586 data
->block
[i
+1] = msgbuf1
[i
];
1588 case I2C_SMBUS_BLOCK_DATA
:
1589 case I2C_SMBUS_BLOCK_PROC_CALL
:
1590 for (i
= 0; i
< msgbuf1
[0] + 1; i
++)
1591 data
->block
[i
] = msgbuf1
[i
];
1598 s32
i2c_smbus_xfer(struct i2c_adapter
* adapter
, u16 addr
, unsigned short flags
,
1599 char read_write
, u8 command
, int size
,
1600 union i2c_smbus_data
* data
)
1604 flags
&= I2C_M_TEN
| I2C_CLIENT_PEC
;
1606 if (adapter
->algo
->smbus_xfer
) {
1607 mutex_lock(&adapter
->bus_lock
);
1608 res
= adapter
->algo
->smbus_xfer(adapter
,addr
,flags
,read_write
,
1610 mutex_unlock(&adapter
->bus_lock
);
1612 res
= i2c_smbus_xfer_emulated(adapter
,addr
,flags
,read_write
,
1617 EXPORT_SYMBOL(i2c_smbus_xfer
);
1619 MODULE_AUTHOR("Simon G. Vogl <simon@tk.uni-linz.ac.at>");
1620 MODULE_DESCRIPTION("I2C-Bus main module");
1621 MODULE_LICENSE("GPL");