Blame view

drivers/i2c/i2c-core-of.c 6.45 KB
2874c5fd2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-or-later
5bf4fa7da   Wolfram Sang   i2c: break out OF...
2
3
4
5
6
7
  /*
   * Linux I2C core OF support code
   *
   * Copyright (C) 2008 Jochen Friedrich <jochen@scram.de>
   * based on a previous patch from Jon Smirl <jonsmirl@gmail.com>
   *
a1671af28   Wolfram Sang   i2c: of: simplify...
8
   * Copyright (C) 2013, 2018 Wolfram Sang <wsa@the-dreams.de>
5bf4fa7da   Wolfram Sang   i2c: break out OF...
9
10
11
12
13
14
15
16
17
   */
  
  #include <dt-bindings/i2c/i2c.h>
  #include <linux/device.h>
  #include <linux/err.h>
  #include <linux/i2c.h>
  #include <linux/module.h>
  #include <linux/of.h>
  #include <linux/of_device.h>
a8023e66e   Wolfram Sang   i2c: core: add sy...
18
  #include <linux/sysfs.h>
5bf4fa7da   Wolfram Sang   i2c: break out OF...
19
20
  
  #include "i2c-core.h"
da0086d01   Boris Brezillon   i2c: Export of_i2...
21
22
  int of_i2c_get_board_info(struct device *dev, struct device_node *node,
  			  struct i2c_board_info *info)
5bf4fa7da   Wolfram Sang   i2c: break out OF...
23
  {
5bf4fa7da   Wolfram Sang   i2c: break out OF...
24
  	u32 addr;
a1671af28   Wolfram Sang   i2c: of: simplify...
25
  	int ret;
5bf4fa7da   Wolfram Sang   i2c: break out OF...
26

da0086d01   Boris Brezillon   i2c: Export of_i2...
27
  	memset(info, 0, sizeof(*info));
5bf4fa7da   Wolfram Sang   i2c: break out OF...
28

da0086d01   Boris Brezillon   i2c: Export of_i2...
29
30
31
32
  	if (of_modalias_node(node, info->type, sizeof(info->type)) < 0) {
  		dev_err(dev, "of_i2c: modalias failure on %pOF
  ", node);
  		return -EINVAL;
5bf4fa7da   Wolfram Sang   i2c: break out OF...
33
  	}
a1671af28   Wolfram Sang   i2c: of: simplify...
34
35
  	ret = of_property_read_u32(node, "reg", &addr);
  	if (ret) {
da0086d01   Boris Brezillon   i2c: Export of_i2...
36
37
38
  		dev_err(dev, "of_i2c: invalid reg on %pOF
  ", node);
  		return ret;
5bf4fa7da   Wolfram Sang   i2c: break out OF...
39
  	}
5bf4fa7da   Wolfram Sang   i2c: break out OF...
40
41
  	if (addr & I2C_TEN_BIT_ADDRESS) {
  		addr &= ~I2C_TEN_BIT_ADDRESS;
da0086d01   Boris Brezillon   i2c: Export of_i2...
42
  		info->flags |= I2C_CLIENT_TEN;
5bf4fa7da   Wolfram Sang   i2c: break out OF...
43
44
45
46
  	}
  
  	if (addr & I2C_OWN_SLAVE_ADDRESS) {
  		addr &= ~I2C_OWN_SLAVE_ADDRESS;
da0086d01   Boris Brezillon   i2c: Export of_i2...
47
  		info->flags |= I2C_CLIENT_SLAVE;
5bf4fa7da   Wolfram Sang   i2c: break out OF...
48
  	}
da0086d01   Boris Brezillon   i2c: Export of_i2...
49
50
  	info->addr = addr;
  	info->of_node = node;
5bf4fa7da   Wolfram Sang   i2c: break out OF...
51
52
  
  	if (of_property_read_bool(node, "host-notify"))
da0086d01   Boris Brezillon   i2c: Export of_i2...
53
  		info->flags |= I2C_CLIENT_HOST_NOTIFY;
5bf4fa7da   Wolfram Sang   i2c: break out OF...
54
55
  
  	if (of_get_property(node, "wakeup-source", NULL))
da0086d01   Boris Brezillon   i2c: Export of_i2...
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  		info->flags |= I2C_CLIENT_WAKE;
  
  	return 0;
  }
  EXPORT_SYMBOL_GPL(of_i2c_get_board_info);
  
  static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
  						 struct device_node *node)
  {
  	struct i2c_client *client;
  	struct i2c_board_info info;
  	int ret;
  
  	dev_dbg(&adap->dev, "of_i2c: register %pOF
  ", node);
  
  	ret = of_i2c_get_board_info(&adap->dev, node, &info);
  	if (ret)
  		return ERR_PTR(ret);
5bf4fa7da   Wolfram Sang   i2c: break out OF...
75

c49b0e077   Wolfram Sang   i2c: of: rename v...
76
77
  	client = i2c_new_device(adap, &info);
  	if (!client) {
453a237cc   Rob Herring   i2c: Convert to u...
78
79
  		dev_err(&adap->dev, "of_i2c: Failure registering %pOF
  ", node);
5bf4fa7da   Wolfram Sang   i2c: break out OF...
80
81
  		return ERR_PTR(-EINVAL);
  	}
c49b0e077   Wolfram Sang   i2c: of: rename v...
82
  	return client;
5bf4fa7da   Wolfram Sang   i2c: break out OF...
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  }
  
  void of_i2c_register_devices(struct i2c_adapter *adap)
  {
  	struct device_node *bus, *node;
  	struct i2c_client *client;
  
  	/* Only register child devices if the adapter has a node pointer set */
  	if (!adap->dev.of_node)
  		return;
  
  	dev_dbg(&adap->dev, "of_i2c: walking child nodes
  ");
  
  	bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
  	if (!bus)
  		bus = of_node_get(adap->dev.of_node);
  
  	for_each_available_child_of_node(bus, node) {
  		if (of_node_test_and_set_flag(node, OF_POPULATED))
  			continue;
  
  		client = of_i2c_register_device(adap, node);
  		if (IS_ERR(client)) {
f1c87ceb0   Wolfram Sang   i2c: of: change l...
107
  			dev_err(&adap->dev,
453a237cc   Rob Herring   i2c: Convert to u...
108
109
110
  				 "Failed to create I2C device for %pOF
  ",
  				 node);
5bf4fa7da   Wolfram Sang   i2c: break out OF...
111
112
113
114
115
116
  			of_node_clear_flag(node, OF_POPULATED);
  		}
  	}
  
  	of_node_put(bus);
  }
418e3ea15   Suzuki K Poulose   bus_find_device: ...
117
  static int of_dev_or_parent_node_match(struct device *dev, const void *data)
e814e6884   Thierry Reding   i2c: of: Try to f...
118
119
120
121
122
123
124
125
126
  {
  	if (dev->of_node == data)
  		return 1;
  
  	if (dev->parent)
  		return dev->parent->of_node == data;
  
  	return 0;
  }
5bf4fa7da   Wolfram Sang   i2c: break out OF...
127
128
129
130
131
  /* must call put_device() when done with returned i2c_client device */
  struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
  {
  	struct device *dev;
  	struct i2c_client *client;
cfba5de9b   Suzuki K Poulose   drivers: Introduc...
132
  	dev = bus_find_device_by_of_node(&i2c_bus_type, node);
5bf4fa7da   Wolfram Sang   i2c: break out OF...
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  	if (!dev)
  		return NULL;
  
  	client = i2c_verify_client(dev);
  	if (!client)
  		put_device(dev);
  
  	return client;
  }
  EXPORT_SYMBOL(of_find_i2c_device_by_node);
  
  /* must call put_device() when done with returned i2c_adapter device */
  struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
  {
  	struct device *dev;
  	struct i2c_adapter *adapter;
e814e6884   Thierry Reding   i2c: of: Try to f...
149
150
  	dev = bus_find_device(&i2c_bus_type, NULL, node,
  			      of_dev_or_parent_node_match);
5bf4fa7da   Wolfram Sang   i2c: break out OF...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
  	if (!dev)
  		return NULL;
  
  	adapter = i2c_verify_adapter(dev);
  	if (!adapter)
  		put_device(dev);
  
  	return adapter;
  }
  EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
  
  /* must call i2c_put_adapter() when done with returned i2c_adapter device */
  struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
  {
  	struct i2c_adapter *adapter;
  
  	adapter = of_find_i2c_adapter_by_node(node);
  	if (!adapter)
  		return NULL;
  
  	if (!try_module_get(adapter->owner)) {
  		put_device(&adapter->dev);
  		adapter = NULL;
  	}
  
  	return adapter;
  }
  EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
  
  static const struct of_device_id*
  i2c_of_match_device_sysfs(const struct of_device_id *matches,
  				  struct i2c_client *client)
  {
  	const char *name;
  
  	for (; matches->compatible[0]; matches++) {
  		/*
  		 * Adding devices through the i2c sysfs interface provides us
  		 * a string to match which may be compatible with the device
  		 * tree compatible strings, however with no actual of_node the
  		 * of_match_device() will not match
  		 */
  		if (sysfs_streq(client->name, matches->compatible))
  			return matches;
  
  		name = strchr(matches->compatible, ',');
  		if (!name)
  			name = matches->compatible;
  		else
  			name++;
  
  		if (sysfs_streq(client->name, name))
  			return matches;
  	}
  
  	return NULL;
  }
  
  const struct of_device_id
  *i2c_of_match_device(const struct of_device_id *matches,
  		     struct i2c_client *client)
  {
  	const struct of_device_id *match;
  
  	if (!(client && matches))
  		return NULL;
  
  	match = of_match_device(matches, &client->dev);
  	if (match)
  		return match;
  
  	return i2c_of_match_device_sysfs(matches, client);
  }
  EXPORT_SYMBOL_GPL(i2c_of_match_device);
  
  #if IS_ENABLED(CONFIG_OF_DYNAMIC)
  static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
  			 void *arg)
  {
  	struct of_reconfig_data *rd = arg;
  	struct i2c_adapter *adap;
  	struct i2c_client *client;
  
  	switch (of_reconfig_get_state_change(action, rd)) {
  	case OF_RECONFIG_CHANGE_ADD:
  		adap = of_find_i2c_adapter_by_node(rd->dn->parent);
  		if (adap == NULL)
  			return NOTIFY_OK;	/* not for us */
  
  		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
  			put_device(&adap->dev);
  			return NOTIFY_OK;
  		}
  
  		client = of_i2c_register_device(adap, rd->dn);
5bf4fa7da   Wolfram Sang   i2c: break out OF...
246
  		if (IS_ERR(client)) {
453a237cc   Rob Herring   i2c: Convert to u...
247
248
249
  			dev_err(&adap->dev, "failed to create client for '%pOF'
  ",
  				 rd->dn);
a4c2fec16   Wen Yang   i2c: core: fix us...
250
  			put_device(&adap->dev);
5bf4fa7da   Wolfram Sang   i2c: break out OF...
251
252
253
  			of_node_clear_flag(rd->dn, OF_POPULATED);
  			return notifier_from_errno(PTR_ERR(client));
  		}
a4c2fec16   Wen Yang   i2c: core: fix us...
254
  		put_device(&adap->dev);
5bf4fa7da   Wolfram Sang   i2c: break out OF...
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
  		break;
  	case OF_RECONFIG_CHANGE_REMOVE:
  		/* already depopulated? */
  		if (!of_node_check_flag(rd->dn, OF_POPULATED))
  			return NOTIFY_OK;
  
  		/* find our device by node */
  		client = of_find_i2c_device_by_node(rd->dn);
  		if (client == NULL)
  			return NOTIFY_OK;	/* no? not meant for us */
  
  		/* unregister takes one ref away */
  		i2c_unregister_device(client);
  
  		/* and put the reference of the find */
  		put_device(&client->dev);
  		break;
  	}
  
  	return NOTIFY_OK;
  }
  
  struct notifier_block i2c_of_notifier = {
  	.notifier_call = of_i2c_notify,
  };
  #endif /* CONFIG_OF_DYNAMIC */