Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-block.git] / drivers / i2c / i2c-core-of.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
5bf4fa7d
WS
2/*
3 * Linux I2C core OF support code
4 *
5 * Copyright (C) 2008 Jochen Friedrich <jochen@scram.de>
6 * based on a previous patch from Jon Smirl <jonsmirl@gmail.com>
7 *
2f5a55c5 8 * Copyright (C) 2013, 2018 Wolfram Sang <wsa@kernel.org>
5bf4fa7d
WS
9 */
10
11#include <dt-bindings/i2c/i2c.h>
12#include <linux/device.h>
13#include <linux/err.h>
14#include <linux/i2c.h>
15#include <linux/module.h>
16#include <linux/of.h>
17#include <linux/of_device.h>
a8023e66 18#include <linux/sysfs.h>
5bf4fa7d
WS
19
20#include "i2c-core.h"
21
da0086d0
BB
22int of_i2c_get_board_info(struct device *dev, struct device_node *node,
23 struct i2c_board_info *info)
5bf4fa7d 24{
5bf4fa7d 25 u32 addr;
a1671af2 26 int ret;
5bf4fa7d 27
da0086d0 28 memset(info, 0, sizeof(*info));
5bf4fa7d 29
da0086d0
BB
30 if (of_modalias_node(node, info->type, sizeof(info->type)) < 0) {
31 dev_err(dev, "of_i2c: modalias failure on %pOF\n", node);
32 return -EINVAL;
5bf4fa7d
WS
33 }
34
a1671af2
WS
35 ret = of_property_read_u32(node, "reg", &addr);
36 if (ret) {
da0086d0
BB
37 dev_err(dev, "of_i2c: invalid reg on %pOF\n", node);
38 return ret;
5bf4fa7d
WS
39 }
40
5bf4fa7d
WS
41 if (addr & I2C_TEN_BIT_ADDRESS) {
42 addr &= ~I2C_TEN_BIT_ADDRESS;
da0086d0 43 info->flags |= I2C_CLIENT_TEN;
5bf4fa7d
WS
44 }
45
46 if (addr & I2C_OWN_SLAVE_ADDRESS) {
47 addr &= ~I2C_OWN_SLAVE_ADDRESS;
da0086d0 48 info->flags |= I2C_CLIENT_SLAVE;
5bf4fa7d
WS
49 }
50
da0086d0
BB
51 info->addr = addr;
52 info->of_node = node;
60774d2a 53 info->fwnode = of_fwnode_handle(node);
5bf4fa7d
WS
54
55 if (of_property_read_bool(node, "host-notify"))
da0086d0 56 info->flags |= I2C_CLIENT_HOST_NOTIFY;
5bf4fa7d
WS
57
58 if (of_get_property(node, "wakeup-source", NULL))
da0086d0
BB
59 info->flags |= I2C_CLIENT_WAKE;
60
61 return 0;
62}
63EXPORT_SYMBOL_GPL(of_i2c_get_board_info);
64
65static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
66 struct device_node *node)
67{
68 struct i2c_client *client;
69 struct i2c_board_info info;
70 int ret;
71
72 dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
73
74 ret = of_i2c_get_board_info(&adap->dev, node, &info);
75 if (ret)
76 return ERR_PTR(ret);
5bf4fa7d 77
5f0155b4
WS
78 client = i2c_new_client_device(adap, &info);
79 if (IS_ERR(client))
453a237c 80 dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
5f0155b4 81
c49b0e07 82 return client;
5bf4fa7d
WS
83}
84
85void of_i2c_register_devices(struct i2c_adapter *adap)
86{
87 struct device_node *bus, *node;
88 struct i2c_client *client;
89
90 /* Only register child devices if the adapter has a node pointer set */
91 if (!adap->dev.of_node)
92 return;
93
94 dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
95
96 bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
97 if (!bus)
98 bus = of_node_get(adap->dev.of_node);
99
100 for_each_available_child_of_node(bus, node) {
101 if (of_node_test_and_set_flag(node, OF_POPULATED))
102 continue;
103
104 client = of_i2c_register_device(adap, node);
105 if (IS_ERR(client)) {
f1c87ceb 106 dev_err(&adap->dev,
453a237c
RH
107 "Failed to create I2C device for %pOF\n",
108 node);
5bf4fa7d
WS
109 of_node_clear_flag(node, OF_POPULATED);
110 }
111 }
112
113 of_node_put(bus);
114}
115
5bf4fa7d
WS
116static const struct of_device_id*
117i2c_of_match_device_sysfs(const struct of_device_id *matches,
118 struct i2c_client *client)
119{
120 const char *name;
121
122 for (; matches->compatible[0]; matches++) {
123 /*
124 * Adding devices through the i2c sysfs interface provides us
125 * a string to match which may be compatible with the device
126 * tree compatible strings, however with no actual of_node the
127 * of_match_device() will not match
128 */
129 if (sysfs_streq(client->name, matches->compatible))
130 return matches;
131
132 name = strchr(matches->compatible, ',');
133 if (!name)
134 name = matches->compatible;
135 else
136 name++;
137
138 if (sysfs_streq(client->name, name))
139 return matches;
140 }
141
142 return NULL;
143}
144
145const struct of_device_id
146*i2c_of_match_device(const struct of_device_id *matches,
147 struct i2c_client *client)
148{
149 const struct of_device_id *match;
150
151 if (!(client && matches))
152 return NULL;
153
154 match = of_match_device(matches, &client->dev);
155 if (match)
156 return match;
157
158 return i2c_of_match_device_sysfs(matches, client);
159}
160EXPORT_SYMBOL_GPL(i2c_of_match_device);
161
162#if IS_ENABLED(CONFIG_OF_DYNAMIC)
163static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
164 void *arg)
165{
166 struct of_reconfig_data *rd = arg;
167 struct i2c_adapter *adap;
168 struct i2c_client *client;
169
170 switch (of_reconfig_get_state_change(action, rd)) {
171 case OF_RECONFIG_CHANGE_ADD:
172 adap = of_find_i2c_adapter_by_node(rd->dn->parent);
173 if (adap == NULL)
174 return NOTIFY_OK; /* not for us */
175
176 if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
177 put_device(&adap->dev);
178 return NOTIFY_OK;
179 }
180
181 client = of_i2c_register_device(adap, rd->dn);
5bf4fa7d 182 if (IS_ERR(client)) {
453a237c
RH
183 dev_err(&adap->dev, "failed to create client for '%pOF'\n",
184 rd->dn);
a4c2fec1 185 put_device(&adap->dev);
5bf4fa7d
WS
186 of_node_clear_flag(rd->dn, OF_POPULATED);
187 return notifier_from_errno(PTR_ERR(client));
188 }
a4c2fec1 189 put_device(&adap->dev);
5bf4fa7d
WS
190 break;
191 case OF_RECONFIG_CHANGE_REMOVE:
192 /* already depopulated? */
193 if (!of_node_check_flag(rd->dn, OF_POPULATED))
194 return NOTIFY_OK;
195
196 /* find our device by node */
197 client = of_find_i2c_device_by_node(rd->dn);
198 if (client == NULL)
199 return NOTIFY_OK; /* no? not meant for us */
200
201 /* unregister takes one ref away */
202 i2c_unregister_device(client);
203
204 /* and put the reference of the find */
205 put_device(&client->dev);
206 break;
207 }
208
209 return NOTIFY_OK;
210}
211
212struct notifier_block i2c_of_notifier = {
213 .notifier_call = of_i2c_notify,
214};
215#endif /* CONFIG_OF_DYNAMIC */