OpenFirmware: Include <linux/of_i2c.h> from of_i2c.c.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / of / of_i2c.c
blob5c015d310d4a9a92b493cbd7d34691c85dd6b992
1 /*
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>
15 #include <linux/of.h>
16 #include <linux/of_i2c.h>
17 #include <linux/module.h>
19 struct i2c_driver_device {
20 char *of_device;
21 char *i2c_type;
24 static struct i2c_driver_device i2c_devices[] = {
27 static int of_find_i2c_driver(struct device_node *node,
28 struct i2c_board_info *info)
30 int i, cplen;
31 const char *compatible;
32 const char *p;
34 /* 1. search for exception list entry */
35 for (i = 0; i < ARRAY_SIZE(i2c_devices); i++) {
36 if (!of_device_is_compatible(node, i2c_devices[i].of_device))
37 continue;
38 if (strlcpy(info->type, i2c_devices[i].i2c_type,
39 I2C_NAME_SIZE) >= I2C_NAME_SIZE)
40 return -ENOMEM;
42 return 0;
45 compatible = of_get_property(node, "compatible", &cplen);
46 if (!compatible)
47 return -ENODEV;
49 /* 2. search for linux,<i2c-type> entry */
50 p = compatible;
51 while (cplen > 0) {
52 if (!strncmp(p, "linux,", 6)) {
53 p += 6;
54 if (strlcpy(info->type, p,
55 I2C_NAME_SIZE) >= I2C_NAME_SIZE)
56 return -ENOMEM;
57 return 0;
60 i = strlen(p) + 1;
61 p += i;
62 cplen -= i;
65 /* 3. take fist compatible entry and strip manufacturer */
66 p = strchr(compatible, ',');
67 if (!p)
68 return -ENODEV;
69 p++;
70 if (strlcpy(info->type, p, I2C_NAME_SIZE) >= I2C_NAME_SIZE)
71 return -ENOMEM;
72 return 0;
75 void of_register_i2c_devices(struct i2c_adapter *adap,
76 struct device_node *adap_node)
78 void *result;
79 struct device_node *node;
81 for_each_child_of_node(adap_node, node) {
82 struct i2c_board_info info = {};
83 const u32 *addr;
84 int len;
86 addr = of_get_property(node, "reg", &len);
87 if (!addr || len < sizeof(int) || *addr > (1 << 10) - 1) {
88 printk(KERN_ERR
89 "of-i2c: invalid i2c device entry\n");
90 continue;
93 info.irq = irq_of_parse_and_map(node, 0);
94 if (info.irq == NO_IRQ)
95 info.irq = -1;
97 if (of_find_i2c_driver(node, &info) < 0) {
98 irq_dispose_mapping(info.irq);
99 continue;
102 info.addr = *addr;
104 request_module(info.type);
106 result = i2c_new_device(adap, &info);
107 if (result == NULL) {
108 printk(KERN_ERR
109 "of-i2c: Failed to load driver for %s\n",
110 info.type);
111 irq_dispose_mapping(info.irq);
112 continue;
116 EXPORT_SYMBOL(of_register_i2c_devices);
118 MODULE_LICENSE("GPL");