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>
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
)
59 if (strcmp(client
->name
, id
->name
) == 0)
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
;
74 driver
= to_i2c_driver(drv
);
75 /* match on an id table if there is one */
77 return i2c_match_id(driver
->id_table
, client
) != NULL
;
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
))
92 dev_dbg(dev
, "uevent\n");
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
;
109 driver
= to_i2c_driver(dev
->driver
);
110 if (!driver
->probe
|| !driver
->id_table
)
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
));
120 client
->driver
= NULL
;
121 i2c_set_clientdata(client
, NULL
);
126 static int i2c_device_remove(struct device
*dev
)
128 struct i2c_client
*client
= i2c_verify_client(dev
);
129 struct i2c_driver
*driver
;
132 if (!client
|| !dev
->driver
)
135 driver
= to_i2c_driver(dev
->driver
);
136 if (driver
->remove
) {
137 dev_dbg(dev
, "remove\n");
138 status
= driver
->remove(client
);
144 client
->driver
= NULL
;
145 i2c_set_clientdata(client
, NULL
);
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
)
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
;
169 pm
= dev
->driver
->pm
;
170 if (!pm
|| !pm
->suspend
)
172 return pm
->suspend(dev
);
175 static int i2c_device_pm_resume(struct device
*dev
)
177 const struct dev_pm_ops
*pm
;
181 pm
= dev
->driver
->pm
;
182 if (!pm
|| !pm
->resume
)
184 return pm
->resume(dev
);
187 #define i2c_device_pm_suspend NULL
188 #define i2c_device_pm_resume NULL
191 #ifdef CONFIG_PM_RUNTIME
192 static int i2c_device_runtime_suspend(struct device
*dev
)
194 const struct dev_pm_ops
*pm
;
198 pm
= dev
->driver
->pm
;
199 if (!pm
|| !pm
->runtime_suspend
)
201 return pm
->runtime_suspend(dev
);
204 static int i2c_device_runtime_resume(struct device
*dev
)
206 const struct dev_pm_ops
*pm
;
210 pm
= dev
->driver
->pm
;
211 if (!pm
|| !pm
->runtime_resume
)
213 return pm
->runtime_resume(dev
);
216 static int i2c_device_runtime_idle(struct device
*dev
)
218 const struct dev_pm_ops
*pm
= NULL
;
222 pm
= dev
->driver
->pm
;
223 if (pm
&& pm
->runtime_idle
) {
224 ret
= pm
->runtime_idle(dev
);
229 return pm_runtime_suspend(dev
);
232 #define i2c_device_runtime_suspend NULL
233 #define i2c_device_runtime_resume NULL
234 #define i2c_device_runtime_idle NULL
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
)
244 driver
= to_i2c_driver(dev
->driver
);
245 if (!driver
->suspend
)
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
)
257 driver
= to_i2c_driver(dev
->driver
);
260 return driver
->resume(client
);
263 static void i2c_client_dev_release(struct device
*dev
)
265 kfree(to_i2c_client(dev
));
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
);
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
[] = {
287 /* modalias helps coldplug: modprobe $(cat .../modalias) */
288 &dev_attr_modalias
.attr
,
292 static struct attribute_group i2c_dev_attr_group
= {
293 .attrs
= i2c_dev_attrs
,
296 static const struct attribute_group
*i2c_dev_attr_groups
[] = {
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
= {
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
)
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
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.
363 i2c_new_device(struct i2c_adapter
*adap
, struct i2c_board_info
const *info
)
365 struct i2c_client
*client
;
368 client
= kzalloc(sizeof *client
, GFP_KERNEL
);
372 client
->adapter
= adap
;
374 client
->dev
.platform_data
= info
->platform_data
;
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
);
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
),
396 status
= device_register(&client
->dev
);
400 dev_dbg(&adap
->dev
, "client [%s] registered with bus id %s\n",
401 client
->name
, dev_name(&client
->dev
));
406 dev_err(&adap
->dev
, "Failed to register i2c client %s at 0x%02x "
407 "(%d)\n", client
->name
, client
->addr
, status
);
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()
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
[] = {
431 static int dummy_probe(struct i2c_client
*client
,
432 const struct i2c_device_id
*id
)
437 static int dummy_remove(struct i2c_client
*client
)
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
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
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.
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
;
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
, ' ');
513 dev_err(dev
, "%s: Missing parameters\n", "new_device");
516 if (blank
- buf
> I2C_NAME_SIZE
- 1) {
517 dev_err(dev
, "%s: Invalid device name\n", "new_device");
520 memcpy(info
.type
, buf
, blank
- buf
);
522 /* Parse remaining parameters, reject extra parameters */
523 res
= sscanf(++blank
, "%hi%c", &info
.addr
, &end
);
525 dev_err(dev
, "%s: Can't parse I2C address\n", "new_device");
528 if (res
> 1 && end
!= '\n') {
529 dev_err(dev
, "%s: Extra parameters\n", "new_device");
533 if (info
.addr
< 0x03 || info
.addr
> 0x77) {
534 dev_err(dev
, "%s: Invalid I2C address 0x%hx\n", "new_device",
539 client
= i2c_new_device(adap
, &info
);
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
);
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.
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
;
572 /* Parse parameters, reject extra parameters */
573 res
= sscanf(buf
, "%hi%c", &addr
, &end
);
575 dev_err(dev
, "%s: Can't parse I2C address\n", "delete_device");
578 if (res
> 1 && end
!= '\n') {
579 dev_err(dev
, "%s: Extra parameters\n", "delete_device");
583 /* Make sure the device was added through sysfs */
585 i2c_lock_adapter(adap
);
586 list_for_each_entry_safe(client
, next
, &adap
->userspace_clients
,
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
);
598 i2c_unlock_adapter(adap
);
601 dev_err(dev
, "%s: Can't find device in list\n",
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
[] = {
611 &dev_attr_new_device
.attr
,
612 &dev_attr_delete_device
.attr
,
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
,
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
;
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
);
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
)
673 /* Can't register until after driver model init */
674 if (unlikely(WARN_ON(!i2c_bus_type
.p
))) {
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)
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
);
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
,
700 "Failed to create compatibility class link\n");
703 /* create pre-declared device nodes */
704 if (adap
->nr
< __i2c_first_dynamic_bus_num
)
705 i2c_scan_static_board_info(adap
);
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
);
716 mutex_lock(&core_lock
);
717 idr_remove(&i2c_adapter_idr
, adap
->nr
);
718 mutex_unlock(&core_lock
);
723 * i2c_add_adapter - declare i2c adapter, use dynamic bus number
724 * @adapter: the adapter to add
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
)
740 if (idr_pre_get(&i2c_adapter_idr
, GFP_KERNEL
) == 0)
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
);
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)
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
)
785 if (adap
->nr
& ~MAX_ID_MASK
)
789 if (idr_pre_get(&i2c_adapter_idr
, GFP_KERNEL
) == 0)
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
) {
799 idr_remove(&i2c_adapter_idr
, id
);
801 mutex_unlock(&core_lock
);
802 if (status
== -EAGAIN
)
806 status
= i2c_register_adapter(adap
);
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
;
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
)
830 res
= driver
->detach_adapter(adapter
);
832 dev_err(&adapter
->dev
, "detach_adapter failed (%d) "
833 "for driver [%s]\n", res
, driver
->driver
.name
);
837 static int __unregister_client(struct device
*dev
, void *dummy
)
839 struct i2c_client
*client
= i2c_verify_client(dev
);
841 i2c_unregister_device(client
);
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
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
)
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
);
869 pr_debug("i2c-core: attempting to delete unregistered "
870 "adapter [%s]\n", adap
->name
);
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
);
882 /* Remove devices instantiated from sysfs */
883 i2c_lock_adapter(adap
);
884 list_for_each_entry_safe(client
, next
, &adap
->userspace_clients
,
886 dev_dbg(&adap
->dev
, "Removing %s at 0x%x\n", client
->name
,
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
,
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
);
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
919 memset(&adap
->dev
, 0, sizeof(adap
->dev
));
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
)
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
)
944 /* Can't register until after driver model init */
945 if (unlikely(WARN_ON(!i2c_bus_type
.p
)))
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
);
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
);
969 EXPORT_SYMBOL(i2c_register_driver
);
971 static int __process_removed_driver(struct device
*dev
, void *data
)
973 if (dev
->type
!= &i2c_adapter_type
)
975 return i2c_do_del_adapter(data
, to_i2c_adapter(dev
));
979 * i2c_del_driver - unregister I2C driver
980 * @driver: the driver being unregistered
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
)
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
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
))
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
)
1039 put_device(&client
->dev
);
1041 EXPORT_SYMBOL(i2c_release_client
);
1043 struct i2c_cmd_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
);
1058 void i2c_clients_command(struct i2c_adapter
*adap
, unsigned int cmd
, void *arg
)
1060 struct i2c_cmd_arg cmd_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)
1072 retval
= bus_register(&i2c_bus_type
);
1075 #ifdef CONFIG_I2C_COMPAT
1076 i2c_adapter_compat_class
= class_compat_register("i2c-adapter");
1077 if (!i2c_adapter_compat_class
) {
1082 retval
= i2c_add_driver(&dummy_driver
);
1088 #ifdef CONFIG_I2C_COMPAT
1089 class_compat_unregister(i2c_adapter_compat_class
);
1092 bus_unregister(&i2c_bus_type
);
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
);
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
;
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
) {
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
) ? "+" : "");
1160 if (in_atomic() || irqs_disabled()) {
1161 ret
= rt_mutex_trylock(&adap
->bus_lock
);
1163 /* I2C activity is ongoing. */
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
);
1175 if (time_after(jiffies
, orig_jiffies
+ adap
->timeout
))
1178 rt_mutex_unlock(&adap
->bus_lock
);
1182 dev_dbg(&adap
->dev
, "I2C level transfers not supported\n");
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
)
1199 struct i2c_adapter
*adap
=client
->adapter
;
1202 msg
.addr
= client
->addr
;
1203 msg
.flags
= client
->flags
& I2C_M_TEN
;
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
;
1229 msg
.addr
= client
->addr
;
1230 msg
.flags
= client
->flags
& I2C_M_TEN
;
1231 msg
.flags
|= I2C_M_RD
;
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
;
1257 /* Make sure the address is valid */
1258 if (addr
< 0x03 || addr
> 0x77) {
1259 dev_warn(&adapter
->dev
, "Invalid probe address 0x%02x\n",
1264 /* Skip if already in use */
1265 if (i2c_check_addr(adapter
, addr
))
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)
1277 if (i2c_smbus_xfer(adapter
, addr
, 0, I2C_SMBUS_WRITE
, 0,
1278 I2C_SMBUS_QUICK
, NULL
) < 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
));
1290 err
= driver
->detect(temp_client
, &info
);
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
,
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
);
1310 list_add_tail(&client
->detected
, &driver
->clients
);
1312 dev_err(&adapter
->dev
, "Failed creating %s at 0x%02x\n",
1313 info
.type
, info
.addr
);
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
;
1323 int adap_id
= i2c_adapter_id(adapter
);
1325 address_list
= driver
->address_list
;
1326 if (!driver
->detect
|| !address_list
)
1329 /* Set up a temporary client to help detect callback */
1330 temp_client
= kzalloc(sizeof(struct i2c_client
), GFP_KERNEL
);
1333 temp_client
->adapter
= adapter
;
1335 /* Stop here if the classes do not match */
1336 if (!(adapter
->class & driver
->class))
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
)
1344 dev_warn(&adapter
->dev
, "SMBus Quick command not supported, "
1345 "can't probe for chips\n");
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
);
1365 i2c_new_probed_device(struct i2c_adapter
*adap
,
1366 struct i2c_board_info
*info
,
1367 unsigned short const *addr_list
)
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");
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
]);
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
]);
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
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,
1407 I2C_SMBUS_BYTE
, &data
) >= 0)
1410 if (i2c_smbus_xfer(adap
, addr_list
[i
], 0,
1412 I2C_SMBUS_QUICK
, NULL
) >= 0)
1417 if (addr_list
[i
] == I2C_CLIENT_END
) {
1418 dev_dbg(&adap
->dev
, "Probing failed, no device found\n");
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
))
1436 mutex_unlock(&core_lock
);
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
)
1454 for(i
= 0; i
< 8; i
++) {
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
)
1467 for(i
= 0; i
< count
; i
++)
1468 crc
= crc8((crc
^ p
[i
]) << 8);
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
);
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
);
1501 pr_debug("i2c-core: Bad PEC 0x%02x vs. 0x%02x\n",
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
;
1520 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
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
;
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
;
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
;
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
;
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
;
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
,
1660 union i2c_smbus_data data
;
1663 status
= i2c_smbus_xfer(client
->adapter
, client
->addr
, client
->flags
,
1664 I2C_SMBUS_READ
, command
,
1665 I2C_SMBUS_BLOCK_DATA
, &data
);
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
;
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
);
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
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
}
1756 msgbuf0
[0] = command
;
1758 case I2C_SMBUS_QUICK
:
1760 /* Special case: The read/write field is used as data */
1761 msg
[0].flags
= flags
| (read_write
== I2C_SMBUS_READ
?
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
;
1772 case I2C_SMBUS_BYTE_DATA
:
1773 if (read_write
== I2C_SMBUS_READ
)
1777 msgbuf0
[1] = data
->byte
;
1780 case I2C_SMBUS_WORD_DATA
:
1781 if (read_write
== I2C_SMBUS_READ
)
1785 msgbuf0
[1] = data
->word
& 0xff;
1786 msgbuf0
[2] = data
->word
>> 8;
1789 case I2C_SMBUS_PROC_CALL
:
1790 num
= 2; /* Special case */
1791 read_write
= I2C_SMBUS_READ
;
1794 msgbuf0
[1] = data
->word
& 0xff;
1795 msgbuf0
[2] = data
->word
>> 8;
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 */
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",
1810 for (i
= 1; i
< msg
[0].len
; i
++)
1811 msgbuf0
[i
] = data
->block
[i
-1];
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",
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 */
1830 case I2C_SMBUS_I2C_BLOCK_DATA
:
1831 if (read_write
== I2C_SMBUS_READ
) {
1832 msg
[1].len
= data
->block
[0];
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",
1841 for (i
= 1; i
<= data
->block
[0]; i
++)
1842 msgbuf0
[i
] = data
->block
[i
];
1846 dev_err(&adapter
->dev
, "Unsupported transaction %d\n", size
);
1850 i
= ((flags
& I2C_CLIENT_PEC
) && size
!= I2C_SMBUS_QUICK
1851 && size
!= I2C_SMBUS_I2C_BLOCK_DATA
);
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
)
1865 status
= i2c_transfer(adapter
, msg
, num
);
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]);
1876 if (read_write
== I2C_SMBUS_READ
)
1878 case I2C_SMBUS_BYTE
:
1879 data
->byte
= msgbuf0
[0];
1881 case I2C_SMBUS_BYTE_DATA
:
1882 data
->byte
= msgbuf1
[0];
1884 case I2C_SMBUS_WORD_DATA
:
1885 case I2C_SMBUS_PROC_CALL
:
1886 data
->word
= msgbuf1
[0] | (msgbuf1
[1] << 8);
1888 case I2C_SMBUS_I2C_BLOCK_DATA
:
1889 for (i
= 0; i
< data
->block
[0]; i
++)
1890 data
->block
[i
+1] = msgbuf1
[i
];
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
];
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
;
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
,
1935 if (time_after(jiffies
,
1936 orig_jiffies
+ adapter
->timeout
))
1939 rt_mutex_unlock(&adapter
->bus_lock
);
1941 res
= i2c_smbus_xfer_emulated(adapter
,addr
,flags
,read_write
,
1942 command
, protocol
, data
);
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");