Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0-or-later
  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 *
  8 * Copyright (C) 2013, 2018 Wolfram Sang <wsa@kernel.org>
 
 
 
 
 
  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>
 18#include <linux/sysfs.h>
 19
 20#include "i2c-core.h"
 21
 22int of_i2c_get_board_info(struct device *dev, struct device_node *node,
 23			  struct i2c_board_info *info)
 24{
 
 
 
 25	u32 addr;
 26	int ret;
 27
 28	memset(info, 0, sizeof(*info));
 29
 30	if (of_alias_from_compatible(node, info->type, sizeof(info->type)) < 0) {
 31		dev_err(dev, "of_i2c: modalias failure on %pOF\n", node);
 32		return -EINVAL;
 
 33	}
 34
 35	ret = of_property_read_u32(node, "reg", &addr);
 36	if (ret) {
 37		dev_err(dev, "of_i2c: invalid reg on %pOF\n", node);
 38		return ret;
 39	}
 40
 41	if (addr & I2C_TEN_BIT_ADDRESS) {
 42		addr &= ~I2C_TEN_BIT_ADDRESS;
 43		info->flags |= I2C_CLIENT_TEN;
 44	}
 45
 46	if (addr & I2C_OWN_SLAVE_ADDRESS) {
 47		addr &= ~I2C_OWN_SLAVE_ADDRESS;
 48		info->flags |= I2C_CLIENT_SLAVE;
 49	}
 50
 51	info->addr = addr;
 52	info->of_node = node;
 53	info->fwnode = of_fwnode_handle(node);
 54
 55	if (of_property_read_bool(node, "host-notify"))
 56		info->flags |= I2C_CLIENT_HOST_NOTIFY;
 57
 58	if (of_property_read_bool(node, "wakeup-source"))
 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);
 77
 78	client = i2c_new_client_device(adap, &info);
 79	if (IS_ERR(client))
 80		dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
 81
 
 
 82	return client;
 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)) {
106			dev_err(&adap->dev,
107				 "Failed to create I2C device for %pOF\n",
108				 node);
109			of_node_clear_flag(node, OF_POPULATED);
110		}
111	}
112
113	of_node_put(bus);
114}
115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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		/*
182		 * Clear the flag before adding the device so that fw_devlink
183		 * doesn't skip adding consumers to this device.
184		 */
185		rd->dn->fwnode.flags &= ~FWNODE_FLAG_NOT_DEVICE;
186		client = of_i2c_register_device(adap, rd->dn);
 
 
187		if (IS_ERR(client)) {
188			dev_err(&adap->dev, "failed to create client for '%pOF'\n",
189				 rd->dn);
190			put_device(&adap->dev);
191			of_node_clear_flag(rd->dn, OF_POPULATED);
192			return notifier_from_errno(PTR_ERR(client));
193		}
194		put_device(&adap->dev);
195		break;
196	case OF_RECONFIG_CHANGE_REMOVE:
197		/* already depopulated? */
198		if (!of_node_check_flag(rd->dn, OF_POPULATED))
199			return NOTIFY_OK;
200
201		/* find our device by node */
202		client = of_find_i2c_device_by_node(rd->dn);
203		if (client == NULL)
204			return NOTIFY_OK;	/* no? not meant for us */
205
206		/* unregister takes one ref away */
207		i2c_unregister_device(client);
208
209		/* and put the reference of the find */
210		put_device(&client->dev);
211		break;
212	}
213
214	return NOTIFY_OK;
215}
216
217struct notifier_block i2c_of_notifier = {
218	.notifier_call = of_i2c_notify,
219};
220#endif /* CONFIG_OF_DYNAMIC */
v4.17
 
  1/*
  2 * Linux I2C core OF support code
  3 *
  4 * Copyright (C) 2008 Jochen Friedrich <jochen@scram.de>
  5 * based on a previous patch from Jon Smirl <jonsmirl@gmail.com>
  6 *
  7 * Copyright (C) 2013, 2018 Wolfram Sang <wsa@the-dreams.de>
  8 *
  9 * This program is free software; you can redistribute it and/or modify it
 10 * under the terms of the GNU General Public License as published by the Free
 11 * Software Foundation; either version 2 of the License, or (at your option)
 12 * any later version.
 13 */
 14
 15#include <dt-bindings/i2c/i2c.h>
 16#include <linux/device.h>
 17#include <linux/err.h>
 18#include <linux/i2c.h>
 19#include <linux/module.h>
 20#include <linux/of.h>
 21#include <linux/of_device.h>
 
 22
 23#include "i2c-core.h"
 24
 25static struct i2c_client *of_i2c_register_device(struct i2c_adapter *adap,
 26						 struct device_node *node)
 27{
 28	struct i2c_client *client;
 29	struct i2c_board_info info = {};
 30	struct dev_archdata dev_ad = {};
 31	u32 addr;
 32	int ret;
 33
 34	dev_dbg(&adap->dev, "of_i2c: register %pOF\n", node);
 35
 36	if (of_modalias_node(node, info.type, sizeof(info.type)) < 0) {
 37		dev_err(&adap->dev, "of_i2c: modalias failure on %pOF\n",
 38			node);
 39		return ERR_PTR(-EINVAL);
 40	}
 41
 42	ret = of_property_read_u32(node, "reg", &addr);
 43	if (ret) {
 44		dev_err(&adap->dev, "of_i2c: invalid reg on %pOF\n", node);
 45		return ERR_PTR(ret);
 46	}
 47
 48	if (addr & I2C_TEN_BIT_ADDRESS) {
 49		addr &= ~I2C_TEN_BIT_ADDRESS;
 50		info.flags |= I2C_CLIENT_TEN;
 51	}
 52
 53	if (addr & I2C_OWN_SLAVE_ADDRESS) {
 54		addr &= ~I2C_OWN_SLAVE_ADDRESS;
 55		info.flags |= I2C_CLIENT_SLAVE;
 56	}
 57
 58	info.addr = addr;
 59	info.archdata = &dev_ad;
 60	info.of_node = of_node_get(node);
 61
 62	if (of_property_read_bool(node, "host-notify"))
 63		info.flags |= I2C_CLIENT_HOST_NOTIFY;
 
 
 
 64
 65	if (of_get_property(node, "wakeup-source", NULL))
 66		info.flags |= I2C_CLIENT_WAKE;
 
 
 
 
 
 
 
 
 67
 68	client = i2c_new_device(adap, &info);
 69	if (!client) {
 
 
 
 
 
 
 70		dev_err(&adap->dev, "of_i2c: Failure registering %pOF\n", node);
 71		of_node_put(node);
 72		return ERR_PTR(-EINVAL);
 73	}
 74	return client;
 75}
 76
 77void of_i2c_register_devices(struct i2c_adapter *adap)
 78{
 79	struct device_node *bus, *node;
 80	struct i2c_client *client;
 81
 82	/* Only register child devices if the adapter has a node pointer set */
 83	if (!adap->dev.of_node)
 84		return;
 85
 86	dev_dbg(&adap->dev, "of_i2c: walking child nodes\n");
 87
 88	bus = of_get_child_by_name(adap->dev.of_node, "i2c-bus");
 89	if (!bus)
 90		bus = of_node_get(adap->dev.of_node);
 91
 92	for_each_available_child_of_node(bus, node) {
 93		if (of_node_test_and_set_flag(node, OF_POPULATED))
 94			continue;
 95
 96		client = of_i2c_register_device(adap, node);
 97		if (IS_ERR(client)) {
 98			dev_err(&adap->dev,
 99				 "Failed to create I2C device for %pOF\n",
100				 node);
101			of_node_clear_flag(node, OF_POPULATED);
102		}
103	}
104
105	of_node_put(bus);
106}
107
108static int of_dev_node_match(struct device *dev, void *data)
109{
110	return dev->of_node == data;
111}
112
113/* must call put_device() when done with returned i2c_client device */
114struct i2c_client *of_find_i2c_device_by_node(struct device_node *node)
115{
116	struct device *dev;
117	struct i2c_client *client;
118
119	dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
120	if (!dev)
121		return NULL;
122
123	client = i2c_verify_client(dev);
124	if (!client)
125		put_device(dev);
126
127	return client;
128}
129EXPORT_SYMBOL(of_find_i2c_device_by_node);
130
131/* must call put_device() when done with returned i2c_adapter device */
132struct i2c_adapter *of_find_i2c_adapter_by_node(struct device_node *node)
133{
134	struct device *dev;
135	struct i2c_adapter *adapter;
136
137	dev = bus_find_device(&i2c_bus_type, NULL, node, of_dev_node_match);
138	if (!dev)
139		return NULL;
140
141	adapter = i2c_verify_adapter(dev);
142	if (!adapter)
143		put_device(dev);
144
145	return adapter;
146}
147EXPORT_SYMBOL(of_find_i2c_adapter_by_node);
148
149/* must call i2c_put_adapter() when done with returned i2c_adapter device */
150struct i2c_adapter *of_get_i2c_adapter_by_node(struct device_node *node)
151{
152	struct i2c_adapter *adapter;
153
154	adapter = of_find_i2c_adapter_by_node(node);
155	if (!adapter)
156		return NULL;
157
158	if (!try_module_get(adapter->owner)) {
159		put_device(&adapter->dev);
160		adapter = NULL;
161	}
162
163	return adapter;
164}
165EXPORT_SYMBOL(of_get_i2c_adapter_by_node);
166
167static const struct of_device_id*
168i2c_of_match_device_sysfs(const struct of_device_id *matches,
169				  struct i2c_client *client)
170{
171	const char *name;
172
173	for (; matches->compatible[0]; matches++) {
174		/*
175		 * Adding devices through the i2c sysfs interface provides us
176		 * a string to match which may be compatible with the device
177		 * tree compatible strings, however with no actual of_node the
178		 * of_match_device() will not match
179		 */
180		if (sysfs_streq(client->name, matches->compatible))
181			return matches;
182
183		name = strchr(matches->compatible, ',');
184		if (!name)
185			name = matches->compatible;
186		else
187			name++;
188
189		if (sysfs_streq(client->name, name))
190			return matches;
191	}
192
193	return NULL;
194}
195
196const struct of_device_id
197*i2c_of_match_device(const struct of_device_id *matches,
198		     struct i2c_client *client)
199{
200	const struct of_device_id *match;
201
202	if (!(client && matches))
203		return NULL;
204
205	match = of_match_device(matches, &client->dev);
206	if (match)
207		return match;
208
209	return i2c_of_match_device_sysfs(matches, client);
210}
211EXPORT_SYMBOL_GPL(i2c_of_match_device);
212
213#if IS_ENABLED(CONFIG_OF_DYNAMIC)
214static int of_i2c_notify(struct notifier_block *nb, unsigned long action,
215			 void *arg)
216{
217	struct of_reconfig_data *rd = arg;
218	struct i2c_adapter *adap;
219	struct i2c_client *client;
220
221	switch (of_reconfig_get_state_change(action, rd)) {
222	case OF_RECONFIG_CHANGE_ADD:
223		adap = of_find_i2c_adapter_by_node(rd->dn->parent);
224		if (adap == NULL)
225			return NOTIFY_OK;	/* not for us */
226
227		if (of_node_test_and_set_flag(rd->dn, OF_POPULATED)) {
228			put_device(&adap->dev);
229			return NOTIFY_OK;
230		}
231
 
 
 
 
 
232		client = of_i2c_register_device(adap, rd->dn);
233		put_device(&adap->dev);
234
235		if (IS_ERR(client)) {
236			dev_err(&adap->dev, "failed to create client for '%pOF'\n",
237				 rd->dn);
 
238			of_node_clear_flag(rd->dn, OF_POPULATED);
239			return notifier_from_errno(PTR_ERR(client));
240		}
 
241		break;
242	case OF_RECONFIG_CHANGE_REMOVE:
243		/* already depopulated? */
244		if (!of_node_check_flag(rd->dn, OF_POPULATED))
245			return NOTIFY_OK;
246
247		/* find our device by node */
248		client = of_find_i2c_device_by_node(rd->dn);
249		if (client == NULL)
250			return NOTIFY_OK;	/* no? not meant for us */
251
252		/* unregister takes one ref away */
253		i2c_unregister_device(client);
254
255		/* and put the reference of the find */
256		put_device(&client->dev);
257		break;
258	}
259
260	return NOTIFY_OK;
261}
262
263struct notifier_block i2c_of_notifier = {
264	.notifier_call = of_i2c_notify,
265};
266#endif /* CONFIG_OF_DYNAMIC */