blob: 1e173f3576743a702bef6125d2fc5d70bf612d24 [file] [log] [blame]
Jochen Friedrich612212a2008-04-12 05:22:35 +10001/*
2 * OF helpers for the I2C API
3 *
4 * Copyright (c) 2008 Jochen Friedrich <jochen@scram.de>
5 *
6 * Based on a previous patch from Jon Smirl <jonsmirl@gmail.com>
7 *
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.
12 */
13
14#include <linux/i2c.h>
David Daney4c600712010-10-19 15:50:31 -070015#include <linux/irq.h>
Jochen Friedrich612212a2008-04-12 05:22:35 +100016#include <linux/of.h>
Robert P. J. Dayf40987b2008-07-08 06:38:24 -040017#include <linux/of_i2c.h>
Grant Likely9fd04992010-06-08 07:48:18 -060018#include <linux/of_irq.h>
Adrian Bunk138decf2008-04-23 19:51:34 +100019#include <linux/module.h>
Jochen Friedrich612212a2008-04-12 05:22:35 +100020
Grant Likely9fd04992010-06-08 07:48:18 -060021void of_i2c_register_devices(struct i2c_adapter *adap)
Jochen Friedrich612212a2008-04-12 05:22:35 +100022{
23 void *result;
24 struct device_node *node;
25
Grant Likely9fd04992010-06-08 07:48:18 -060026 /* Only register child devices if the adapter has a node pointer set */
27 if (!adap->dev.of_node)
28 return;
29
30 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
31
32 for_each_child_of_node(adap->dev.of_node, node) {
Jochen Friedrich612212a2008-04-12 05:22:35 +100033 struct i2c_board_info info = {};
Anton Vorontsove6a437e2008-11-28 09:13:45 +000034 struct dev_archdata dev_ad = {};
Jeremy Kerr33714882010-01-30 01:45:26 -070035 const __be32 *addr;
Jochen Friedrich612212a2008-04-12 05:22:35 +100036 int len;
37
Grant Likely9fd04992010-06-08 07:48:18 -060038 dev_dbg(&adap->dev, "of_i2c: register %s\n", node->full_name);
39
40 if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
41 dev_err(&adap->dev, "of_i2c: modalias failure on %s\n",
42 node->full_name);
Grant Likely3f07af42008-07-25 22:25:13 -040043 continue;
Grant Likely9fd04992010-06-08 07:48:18 -060044 }
Grant Likely3f07af42008-07-25 22:25:13 -040045
Jochen Friedrich612212a2008-04-12 05:22:35 +100046 addr = of_get_property(node, "reg", &len);
Grant Likely9fd04992010-06-08 07:48:18 -060047 if (!addr || (len < sizeof(int))) {
48 dev_err(&adap->dev, "of_i2c: invalid reg on %s\n",
49 node->full_name);
50 continue;
51 }
52
53 info.addr = be32_to_cpup(addr);
54 if (info.addr > (1 << 10) - 1) {
55 dev_err(&adap->dev, "of_i2c: invalid addr=%x on %s\n",
56 info.addr, node->full_name);
Jochen Friedrich612212a2008-04-12 05:22:35 +100057 continue;
58 }
59
60 info.irq = irq_of_parse_and_map(node, 0);
Grant Likely9fd04992010-06-08 07:48:18 -060061 info.of_node = of_node_get(node);
Anton Vorontsove6a437e2008-11-28 09:13:45 +000062 info.archdata = &dev_ad;
63
David Daney02086262010-11-16 14:42:14 -080064 request_module("%s%s", I2C_MODULE_PREFIX, info.type);
Jochen Friedrich612212a2008-04-12 05:22:35 +100065
66 result = i2c_new_device(adap, &info);
67 if (result == NULL) {
Grant Likely9fd04992010-06-08 07:48:18 -060068 dev_err(&adap->dev, "of_i2c: Failure registering %s\n",
69 node->full_name);
70 of_node_put(node);
Jochen Friedrich612212a2008-04-12 05:22:35 +100071 irq_dispose_mapping(info.irq);
72 continue;
73 }
74 }
75}
Grant Likely9fd04992010-06-08 07:48:18 -060076EXPORT_SYMBOL(of_i2c_register_devices);
Adrian Bunk138decf2008-04-23 19:51:34 +100077
Jon Smirl2526c152009-01-09 15:49:06 -070078static int of_dev_node_match(struct device *dev, void *data)
79{
Grant Likely61c7a082010-04-13 16:12:29 -070080 return dev->of_node == data;
Jon Smirl2526c152009-01-09 15:49:06 -070081}
82
83/* must call put_device() when done with returned i2c_client device */
84struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
85{
86 struct device *dev;
87
88 dev = bus_find_device(&i2c_bus_type, NULL, node,
89 of_dev_node_match);
90 if (!dev)
91 return NULL;
92
Stephen Warrend9afca32012-04-17 12:42:15 -060093 return i2c_verify_client(dev);
Jon Smirl2526c152009-01-09 15:49:06 -070094}
95EXPORT_SYMBOL(of_find_i2c_device_by_node);
96
Stephen Warren09386432012-04-17 12:43:34 -060097/* must call put_device() when done with returned i2c_adapter device */
98struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
99{
100 struct device *dev;
101
102 dev = bus_find_device(&i2c_bus_type, NULL, node,
103 of_dev_node_match);
104 if (!dev)
105 return NULL;
106
107 return i2c_verify_adapter(dev);
108}
109EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
110
Adrian Bunk138decf2008-04-23 19:51:34 +1000111MODULE_LICENSE("GPL");