Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * I2C multiplexer using pinctrl API
  3 *
  4 * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
  5 *
  6 * This program is free software; you can redistribute it and/or modify it
  7 * under the terms and conditions of the GNU General Public License,
  8 * version 2, as published by the Free Software Foundation.
  9 *
 10 * This program is distributed in the hope it will be useful, but WITHOUT
 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 13 * more details.
 14 *
 15 * You should have received a copy of the GNU General Public License
 16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 17 */
 18
 19#include <linux/i2c.h>
 20#include <linux/i2c-mux.h>
 21#include <linux/module.h>
 22#include <linux/pinctrl/consumer.h>
 23#include <linux/i2c-mux-pinctrl.h>
 24#include <linux/platform_device.h>
 25#include <linux/slab.h>
 26#include <linux/of.h>
 
 27
 28struct i2c_mux_pinctrl {
 29	struct device *dev;
 30	struct i2c_mux_pinctrl_platform_data *pdata;
 31	struct pinctrl *pinctrl;
 32	struct pinctrl_state **states;
 33	struct pinctrl_state *state_idle;
 34	struct i2c_adapter *parent;
 35	struct i2c_adapter **busses;
 36};
 37
 38static int i2c_mux_pinctrl_select(struct i2c_adapter *adap, void *data,
 39				  u32 chan)
 40{
 41	struct i2c_mux_pinctrl *mux = data;
 42
 43	return pinctrl_select_state(mux->pinctrl, mux->states[chan]);
 44}
 45
 46static int i2c_mux_pinctrl_deselect(struct i2c_adapter *adap, void *data,
 47				    u32 chan)
 48{
 49	struct i2c_mux_pinctrl *mux = data;
 50
 51	return pinctrl_select_state(mux->pinctrl, mux->state_idle);
 52}
 53
 54#ifdef CONFIG_OF
 55static int i2c_mux_pinctrl_parse_dt(struct i2c_mux_pinctrl *mux,
 56				struct platform_device *pdev)
 57{
 58	struct device_node *np = pdev->dev.of_node;
 59	int num_names, i, ret;
 60	struct device_node *adapter_np;
 61	struct i2c_adapter *adapter;
 62
 63	if (!np)
 64		return 0;
 65
 66	mux->pdata = devm_kzalloc(&pdev->dev, sizeof(*mux->pdata), GFP_KERNEL);
 67	if (!mux->pdata) {
 68		dev_err(mux->dev,
 69			"Cannot allocate i2c_mux_pinctrl_platform_data\n");
 70		return -ENOMEM;
 71	}
 72
 73	num_names = of_property_count_strings(np, "pinctrl-names");
 74	if (num_names < 0) {
 75		dev_err(mux->dev, "Cannot parse pinctrl-names: %d\n",
 76			num_names);
 77		return num_names;
 78	}
 79
 80	mux->pdata->pinctrl_states = devm_kzalloc(&pdev->dev,
 81		sizeof(*mux->pdata->pinctrl_states) * num_names,
 82		GFP_KERNEL);
 83	if (!mux->pdata->pinctrl_states) {
 84		dev_err(mux->dev, "Cannot allocate pinctrl_states\n");
 85		return -ENOMEM;
 86	}
 87
 88	for (i = 0; i < num_names; i++) {
 89		ret = of_property_read_string_index(np, "pinctrl-names", i,
 90			&mux->pdata->pinctrl_states[mux->pdata->bus_count]);
 91		if (ret < 0) {
 92			dev_err(mux->dev, "Cannot parse pinctrl-names: %d\n",
 93				ret);
 94			return ret;
 95		}
 96		if (!strcmp(mux->pdata->pinctrl_states[mux->pdata->bus_count],
 97			    "idle")) {
 98			if (i != num_names - 1) {
 99				dev_err(mux->dev, "idle state must be last\n");
100				return -EINVAL;
101			}
102			mux->pdata->pinctrl_state_idle = "idle";
103		} else {
104			mux->pdata->bus_count++;
105		}
106	}
107
108	adapter_np = of_parse_phandle(np, "i2c-parent", 0);
109	if (!adapter_np) {
110		dev_err(mux->dev, "Cannot parse i2c-parent\n");
111		return -ENODEV;
112	}
113	adapter = of_find_i2c_adapter_by_node(adapter_np);
114	of_node_put(adapter_np);
115	if (!adapter) {
116		dev_err(mux->dev, "Cannot find parent bus\n");
117		return -EPROBE_DEFER;
118	}
119	mux->pdata->parent_bus_num = i2c_adapter_id(adapter);
120	put_device(&adapter->dev);
121
122	return 0;
123}
124#else
125static inline int i2c_mux_pinctrl_parse_dt(struct i2c_mux_pinctrl *mux,
126					   struct platform_device *pdev)
127{
128	return 0;
129}
130#endif
131
132static int i2c_mux_pinctrl_probe(struct platform_device *pdev)
133{
 
 
 
134	struct i2c_mux_pinctrl *mux;
135	int (*deselect)(struct i2c_adapter *, void *, u32);
136	int i, ret;
137
138	mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
139	if (!mux) {
140		dev_err(&pdev->dev, "Cannot allocate i2c_mux_pinctrl\n");
141		ret = -ENOMEM;
142		goto err;
143	}
144	platform_set_drvdata(pdev, mux);
145
146	mux->dev = &pdev->dev;
147
148	mux->pdata = dev_get_platdata(&pdev->dev);
149	if (!mux->pdata) {
150		ret = i2c_mux_pinctrl_parse_dt(mux, pdev);
151		if (ret < 0)
152			goto err;
153	}
154	if (!mux->pdata) {
155		dev_err(&pdev->dev, "Missing platform data\n");
156		ret = -ENODEV;
157		goto err;
158	}
159
160	mux->states = devm_kzalloc(&pdev->dev,
161				   sizeof(*mux->states) * mux->pdata->bus_count,
162				   GFP_KERNEL);
163	if (!mux->states) {
164		dev_err(&pdev->dev, "Cannot allocate states\n");
 
 
 
165		ret = -ENOMEM;
166		goto err;
167	}
 
168
169	mux->busses = devm_kzalloc(&pdev->dev,
170				   sizeof(*mux->busses) * mux->pdata->bus_count,
171				   GFP_KERNEL);
172	if (!mux->busses) {
173		dev_err(&pdev->dev, "Cannot allocate busses\n");
174		ret = -ENOMEM;
175		goto err;
176	}
177
178	mux->pinctrl = devm_pinctrl_get(&pdev->dev);
179	if (IS_ERR(mux->pinctrl)) {
180		ret = PTR_ERR(mux->pinctrl);
181		dev_err(&pdev->dev, "Cannot get pinctrl: %d\n", ret);
182		goto err;
183	}
184	for (i = 0; i < mux->pdata->bus_count; i++) {
185		mux->states[i] = pinctrl_lookup_state(mux->pinctrl,
186						mux->pdata->pinctrl_states[i]);
187			if (IS_ERR(mux->states[i])) {
188				ret = PTR_ERR(mux->states[i]);
189				dev_err(&pdev->dev,
190					"Cannot look up pinctrl state %s: %d\n",
191					mux->pdata->pinctrl_states[i], ret);
192				goto err;
193			}
194	}
195	if (mux->pdata->pinctrl_state_idle) {
196		mux->state_idle = pinctrl_lookup_state(mux->pinctrl,
197						mux->pdata->pinctrl_state_idle);
198		if (IS_ERR(mux->state_idle)) {
199			ret = PTR_ERR(mux->state_idle);
200			dev_err(&pdev->dev,
201				"Cannot look up pinctrl state %s: %d\n",
202				mux->pdata->pinctrl_state_idle, ret);
203			goto err;
204		}
205
206		deselect = i2c_mux_pinctrl_deselect;
207	} else {
208		deselect = NULL;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209	}
210
211	mux->parent = i2c_get_adapter(mux->pdata->parent_bus_num);
212	if (!mux->parent) {
213		dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
214			mux->pdata->parent_bus_num);
215		ret = -EPROBE_DEFER;
216		goto err;
 
 
217	}
 
 
218
219	for (i = 0; i < mux->pdata->bus_count; i++) {
220		u32 bus = mux->pdata->base_bus_num ?
221				(mux->pdata->base_bus_num + i) : 0;
222
223		mux->busses[i] = i2c_add_mux_adapter(mux->parent, &pdev->dev,
224						     mux, bus, i, 0,
225						     i2c_mux_pinctrl_select,
226						     deselect);
227		if (!mux->busses[i]) {
228			ret = -ENODEV;
229			dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
230			goto err_del_adapter;
231		}
232	}
233
234	return 0;
235
236err_del_adapter:
237	for (; i > 0; i--)
238		i2c_del_mux_adapter(mux->busses[i - 1]);
239	i2c_put_adapter(mux->parent);
240err:
241	return ret;
242}
243
244static int i2c_mux_pinctrl_remove(struct platform_device *pdev)
245{
246	struct i2c_mux_pinctrl *mux = platform_get_drvdata(pdev);
247	int i;
248
249	for (i = 0; i < mux->pdata->bus_count; i++)
250		i2c_del_mux_adapter(mux->busses[i]);
251
252	i2c_put_adapter(mux->parent);
 
253
254	return 0;
255}
256
257#ifdef CONFIG_OF
258static const struct of_device_id i2c_mux_pinctrl_of_match[] = {
259	{ .compatible = "i2c-mux-pinctrl", },
260	{},
261};
262MODULE_DEVICE_TABLE(of, i2c_mux_pinctrl_of_match);
263#endif
264
265static struct platform_driver i2c_mux_pinctrl_driver = {
266	.driver	= {
267		.name	= "i2c-mux-pinctrl",
268		.of_match_table = of_match_ptr(i2c_mux_pinctrl_of_match),
269	},
270	.probe	= i2c_mux_pinctrl_probe,
271	.remove	= i2c_mux_pinctrl_remove,
272};
273module_platform_driver(i2c_mux_pinctrl_driver);
274
275MODULE_DESCRIPTION("pinctrl-based I2C multiplexer driver");
276MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
277MODULE_LICENSE("GPL v2");
278MODULE_ALIAS("platform:i2c-mux-pinctrl");
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * I2C multiplexer using pinctrl API
  4 *
  5 * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
 
 
 
 
 
 
 
 
 
 
 
 
  6 */
  7
  8#include <linux/i2c.h>
  9#include <linux/i2c-mux.h>
 10#include <linux/module.h>
 11#include <linux/pinctrl/consumer.h>
 
 12#include <linux/platform_device.h>
 13#include <linux/slab.h>
 14#include <linux/of.h>
 15#include "../../pinctrl/core.h"
 16
 17struct i2c_mux_pinctrl {
 
 
 18	struct pinctrl *pinctrl;
 19	struct pinctrl_state *states[];
 
 
 
 20};
 21
 22static int i2c_mux_pinctrl_select(struct i2c_mux_core *muxc, u32 chan)
 
 23{
 24	struct i2c_mux_pinctrl *mux = i2c_mux_priv(muxc);
 25
 26	return pinctrl_select_state(mux->pinctrl, mux->states[chan]);
 27}
 28
 29static int i2c_mux_pinctrl_deselect(struct i2c_mux_core *muxc, u32 chan)
 
 30{
 31	return i2c_mux_pinctrl_select(muxc, muxc->num_adapters);
 
 
 32}
 33
 34static struct i2c_adapter *i2c_mux_pinctrl_root_adapter(
 35	struct pinctrl_state *state)
 
 36{
 37	struct i2c_adapter *root = NULL;
 38	struct pinctrl_setting *setting;
 39	struct i2c_adapter *pin_root;
 
 40
 41	list_for_each_entry(setting, &state->settings, node) {
 42		pin_root = i2c_root_adapter(setting->pctldev->dev);
 43		if (!pin_root)
 44			return NULL;
 45		if (!root)
 46			root = pin_root;
 47		else if (root != pin_root)
 48			return NULL;
 49	}
 50
 51	return root;
 52}
 
 
 
 
 
 
 
 
 
 
 
 
 53
 54static struct i2c_adapter *i2c_mux_pinctrl_parent_adapter(struct device *dev)
 55{
 56	struct device_node *np = dev->of_node;
 57	struct device_node *parent_np;
 58	struct i2c_adapter *parent;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 59
 60	parent_np = of_parse_phandle(np, "i2c-parent", 0);
 61	if (!parent_np) {
 62		dev_err(dev, "Cannot parse i2c-parent\n");
 63		return ERR_PTR(-ENODEV);
 64	}
 65	parent = of_find_i2c_adapter_by_node(parent_np);
 66	of_node_put(parent_np);
 67	if (!parent)
 68		return ERR_PTR(-EPROBE_DEFER);
 
 
 
 
 69
 70	return parent;
 
 
 
 
 
 
 71}
 
 72
 73static int i2c_mux_pinctrl_probe(struct platform_device *pdev)
 74{
 75	struct device *dev = &pdev->dev;
 76	struct device_node *np = dev->of_node;
 77	struct i2c_mux_core *muxc;
 78	struct i2c_mux_pinctrl *mux;
 79	struct i2c_adapter *parent;
 80	struct i2c_adapter *root;
 81	int num_names, i, ret;
 82	const char *name;
 
 
 
 
 
 
 
 
 83
 84	num_names = of_property_count_strings(np, "pinctrl-names");
 85	if (num_names < 0) {
 86		dev_err(dev, "Cannot parse pinctrl-names: %d\n",
 87			num_names);
 88		return num_names;
 
 
 
 
 
 89	}
 90
 91	parent = i2c_mux_pinctrl_parent_adapter(dev);
 92	if (IS_ERR(parent))
 93		return PTR_ERR(parent);
 94
 95	muxc = i2c_mux_alloc(parent, dev, num_names,
 96			     struct_size(mux, states, num_names),
 97			     0, i2c_mux_pinctrl_select, NULL);
 98	if (!muxc) {
 99		ret = -ENOMEM;
100		goto err_put_parent;
101	}
102	mux = i2c_mux_priv(muxc);
103
104	platform_set_drvdata(pdev, muxc);
 
 
 
 
 
 
 
105
106	mux->pinctrl = devm_pinctrl_get(dev);
107	if (IS_ERR(mux->pinctrl)) {
108		ret = PTR_ERR(mux->pinctrl);
109		dev_err(dev, "Cannot get pinctrl: %d\n", ret);
110		goto err_put_parent;
 
 
 
 
 
 
 
 
 
 
 
111	}
112
113	for (i = 0; i < num_names; i++) {
114		ret = of_property_read_string_index(np, "pinctrl-names", i,
115						    &name);
116		if (ret < 0) {
117			dev_err(dev, "Cannot parse pinctrl-names: %d\n", ret);
118			goto err_put_parent;
 
 
119		}
120
121		mux->states[i] = pinctrl_lookup_state(mux->pinctrl, name);
122		if (IS_ERR(mux->states[i])) {
123			ret = PTR_ERR(mux->states[i]);
124			dev_err(dev, "Cannot look up pinctrl state %s: %d\n",
125				name, ret);
126			goto err_put_parent;
127		}
128
129		if (strcmp(name, "idle"))
130			continue;
131
132		if (i != num_names - 1) {
133			dev_err(dev, "idle state must be last\n");
134			ret = -EINVAL;
135			goto err_put_parent;
136		}
137		muxc->deselect = i2c_mux_pinctrl_deselect;
138	}
139
140	root = i2c_root_adapter(&muxc->parent->dev);
141
142	muxc->mux_locked = true;
143	for (i = 0; i < num_names; i++) {
144		if (root != i2c_mux_pinctrl_root_adapter(mux->states[i])) {
145			muxc->mux_locked = false;
146			break;
147		}
148	}
149	if (muxc->mux_locked)
150		dev_info(dev, "mux-locked i2c mux\n");
151
152	/* Do not add any adapter for the idle state (if it's there at all). */
153	for (i = 0; i < num_names - !!muxc->deselect; i++) {
154		ret = i2c_mux_add_adapter(muxc, 0, i, 0);
155		if (ret)
 
 
 
 
 
 
 
156			goto err_del_adapter;
 
157	}
158
159	return 0;
160
161err_del_adapter:
162	i2c_mux_del_adapters(muxc);
163err_put_parent:
164	i2c_put_adapter(parent);
165
166	return ret;
167}
168
169static int i2c_mux_pinctrl_remove(struct platform_device *pdev)
170{
171	struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
 
 
 
 
172
173	i2c_mux_del_adapters(muxc);
174	i2c_put_adapter(muxc->parent);
175
176	return 0;
177}
178
 
179static const struct of_device_id i2c_mux_pinctrl_of_match[] = {
180	{ .compatible = "i2c-mux-pinctrl", },
181	{},
182};
183MODULE_DEVICE_TABLE(of, i2c_mux_pinctrl_of_match);
 
184
185static struct platform_driver i2c_mux_pinctrl_driver = {
186	.driver	= {
187		.name	= "i2c-mux-pinctrl",
188		.of_match_table = of_match_ptr(i2c_mux_pinctrl_of_match),
189	},
190	.probe	= i2c_mux_pinctrl_probe,
191	.remove	= i2c_mux_pinctrl_remove,
192};
193module_platform_driver(i2c_mux_pinctrl_driver);
194
195MODULE_DESCRIPTION("pinctrl-based I2C multiplexer driver");
196MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
197MODULE_LICENSE("GPL v2");
198MODULE_ALIAS("platform:i2c-mux-pinctrl");