2 * OF helpers for the I2C API
4 * Copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
6 * Based on a previous patch from Jon Smirl <jonsmirl@gmail.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/i2c.h>
16 #include <linux/module.h>
18 struct i2c_driver_device {
23 static struct i2c_driver_device i2c_devices[] = {
26 static int of_find_i2c_driver(struct device_node *node,
27 struct i2c_board_info *info)
30 const char *compatible;
33 /* 1. search for exception list entry */
34 for (i = 0; i < ARRAY_SIZE(i2c_devices); i++) {
35 if (!of_device_is_compatible(node, i2c_devices[i].of_device))
37 if (strlcpy(info->type, i2c_devices[i].i2c_type,
38 I2C_NAME_SIZE) >= I2C_NAME_SIZE)
44 compatible = of_get_property(node, "compatible", &cplen);
48 /* 2. search for linux,<i2c-type> entry */
51 if (!strncmp(p, "linux,", 6)) {
53 if (strlcpy(info->type, p,
54 I2C_NAME_SIZE) >= I2C_NAME_SIZE)
64 /* 3. take fist compatible entry and strip manufacturer */
65 p = strchr(compatible, ',');
69 if (strlcpy(info->type, p, I2C_NAME_SIZE) >= I2C_NAME_SIZE)
74 void of_register_i2c_devices(struct i2c_adapter *adap,
75 struct device_node *adap_node)
78 struct device_node *node;
80 for_each_child_of_node(adap_node, node) {
81 struct i2c_board_info info = {};
85 addr = of_get_property(node, "reg", &len);
86 if (!addr || len < sizeof(int) || *addr > (1 << 10) - 1) {
88 "of-i2c: invalid i2c device entry\n");
92 info.irq = irq_of_parse_and_map(node, 0);
93 if (info.irq == NO_IRQ)
96 if (of_find_i2c_driver(node, &info) < 0) {
97 irq_dispose_mapping(info.irq);
103 request_module(info.type);
105 result = i2c_new_device(adap, &info);
106 if (result == NULL) {
108 "of-i2c: Failed to load driver for %s\n",
110 irq_dispose_mapping(info.irq);
115 EXPORT_SYMBOL(of_register_i2c_devices);
117 MODULE_LICENSE("GPL");