Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * I2C multiplexer using GPIO API
  4 *
  5 * Peter Korsgaard <peter.korsgaard@barco.com>
  6 */
  7
  8#include <linux/i2c.h>
  9#include <linux/i2c-mux.h>
 10#include <linux/platform_data/i2c-mux-gpio.h>
 11#include <linux/platform_device.h>
 12#include <linux/module.h>
 13#include <linux/slab.h>
 14#include <linux/bits.h>
 15#include <linux/gpio/consumer.h>
 16/* FIXME: stop poking around inside gpiolib */
 17#include "../../gpio/gpiolib.h"
 18
 19struct gpiomux {
 20	struct i2c_mux_gpio_platform_data data;
 21	int ngpios;
 22	struct gpio_desc **gpios;
 23};
 24
 25static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
 26{
 27	DECLARE_BITMAP(values, BITS_PER_TYPE(val));
 28
 29	values[0] = val;
 30
 31	gpiod_set_array_value_cansleep(mux->ngpios, mux->gpios, NULL, values);
 32}
 33
 34static int i2c_mux_gpio_select(struct i2c_mux_core *muxc, u32 chan)
 35{
 36	struct gpiomux *mux = i2c_mux_priv(muxc);
 37
 38	i2c_mux_gpio_set(mux, chan);
 39
 40	return 0;
 41}
 42
 43static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan)
 44{
 45	struct gpiomux *mux = i2c_mux_priv(muxc);
 46
 47	i2c_mux_gpio_set(mux, mux->data.idle);
 48
 49	return 0;
 50}
 51
 52#ifdef CONFIG_OF
 53static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
 54					struct platform_device *pdev)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 55{
 56	struct device_node *np = pdev->dev.of_node;
 57	struct device_node *adapter_np, *child;
 58	struct i2c_adapter *adapter;
 
 
 
 
 
 
 
 
 
 
 59	unsigned *values;
 60	int i = 0;
 61
 62	if (!np)
 63		return -ENODEV;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 64
 65	adapter_np = of_parse_phandle(np, "i2c-parent", 0);
 66	if (!adapter_np) {
 67		dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
 68		return -ENODEV;
 69	}
 70	adapter = of_find_i2c_adapter_by_node(adapter_np);
 71	of_node_put(adapter_np);
 72	if (!adapter)
 73		return -EPROBE_DEFER;
 74
 75	mux->data.parent = i2c_adapter_id(adapter);
 76	put_device(&adapter->dev);
 77
 78	mux->data.n_values = of_get_child_count(np);
 79
 80	values = devm_kcalloc(&pdev->dev,
 81			      mux->data.n_values, sizeof(*mux->data.values),
 82			      GFP_KERNEL);
 83	if (!values) {
 84		dev_err(&pdev->dev, "Cannot allocate values array");
 85		return -ENOMEM;
 86	}
 87
 88	for_each_child_of_node(np, child) {
 89		of_property_read_u32(child, "reg", values + i);
 
 
 
 
 
 
 
 
 90		i++;
 91	}
 92	mux->data.values = values;
 93
 94	if (of_property_read_u32(np, "idle-state", &mux->data.idle))
 95		mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
 96
 97	return 0;
 98}
 99#else
100static int i2c_mux_gpio_probe_dt(struct gpiomux *mux,
101					struct platform_device *pdev)
102{
103	return 0;
104}
105#endif
106
107static int i2c_mux_gpio_probe(struct platform_device *pdev)
108{
109	struct i2c_mux_core *muxc;
110	struct gpiomux *mux;
111	struct i2c_adapter *parent;
112	struct i2c_adapter *root;
113	unsigned initial_state;
114	int i, ngpios, ret;
115
116	mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
117	if (!mux)
118		return -ENOMEM;
119
120	if (!dev_get_platdata(&pdev->dev)) {
121		ret = i2c_mux_gpio_probe_dt(mux, pdev);
122		if (ret < 0)
123			return ret;
124	} else {
125		memcpy(&mux->data, dev_get_platdata(&pdev->dev),
126			sizeof(mux->data));
127	}
128
129	ngpios = gpiod_count(&pdev->dev, "mux");
130	if (ngpios <= 0) {
131		dev_err(&pdev->dev, "no valid gpios provided\n");
132		return ngpios ?: -EINVAL;
133	}
134	mux->ngpios = ngpios;
135
136	parent = i2c_get_adapter(mux->data.parent);
137	if (!parent)
138		return -EPROBE_DEFER;
139
140	muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values,
141			     ngpios * sizeof(*mux->gpios), 0,
142			     i2c_mux_gpio_select, NULL);
143	if (!muxc) {
144		ret = -ENOMEM;
145		goto alloc_failed;
146	}
147	mux->gpios = muxc->priv;
148	muxc->priv = mux;
149
150	platform_set_drvdata(pdev, muxc);
151
152	root = i2c_root_adapter(&parent->dev);
153
154	muxc->mux_locked = true;
155
156	if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) {
157		initial_state = mux->data.idle;
158		muxc->deselect = i2c_mux_gpio_deselect;
159	} else {
160		initial_state = mux->data.values[0];
161	}
162
163	for (i = 0; i < ngpios; i++) {
164		struct device *gpio_dev;
165		struct gpio_desc *gpiod;
166		enum gpiod_flags flag;
167
168		if (initial_state & BIT(i))
169			flag = GPIOD_OUT_HIGH;
170		else
171			flag = GPIOD_OUT_LOW;
172		gpiod = devm_gpiod_get_index(&pdev->dev, "mux", i, flag);
173		if (IS_ERR(gpiod)) {
174			ret = PTR_ERR(gpiod);
175			goto alloc_failed;
176		}
177
178		mux->gpios[i] = gpiod;
179
180		if (!muxc->mux_locked)
181			continue;
182
183		/* FIXME: find a proper way to access the GPIO device */
184		gpio_dev = &gpiod->gdev->dev;
185		muxc->mux_locked = i2c_root_adapter(gpio_dev) == root;
186	}
187
188	if (muxc->mux_locked)
189		dev_info(&pdev->dev, "mux-locked i2c mux\n");
190
191	for (i = 0; i < mux->data.n_values; i++) {
192		u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
193		unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
194
195		ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class);
196		if (ret)
197			goto add_adapter_failed;
198	}
199
200	dev_info(&pdev->dev, "%d port mux on %s adapter\n",
201		 mux->data.n_values, parent->name);
202
203	return 0;
204
205add_adapter_failed:
206	i2c_mux_del_adapters(muxc);
207alloc_failed:
208	i2c_put_adapter(parent);
209
210	return ret;
211}
212
213static int i2c_mux_gpio_remove(struct platform_device *pdev)
214{
215	struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
216
217	i2c_mux_del_adapters(muxc);
218	i2c_put_adapter(muxc->parent);
219
220	return 0;
221}
222
223static const struct of_device_id i2c_mux_gpio_of_match[] = {
224	{ .compatible = "i2c-mux-gpio", },
225	{},
226};
227MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match);
228
229static struct platform_driver i2c_mux_gpio_driver = {
230	.probe	= i2c_mux_gpio_probe,
231	.remove	= i2c_mux_gpio_remove,
232	.driver	= {
233		.name	= "i2c-mux-gpio",
234		.of_match_table = i2c_mux_gpio_of_match,
235	},
236};
237
238module_platform_driver(i2c_mux_gpio_driver);
239
240MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
241MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
242MODULE_LICENSE("GPL");
243MODULE_ALIAS("platform:i2c-mux-gpio");
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * I2C multiplexer using GPIO API
  4 *
  5 * Peter Korsgaard <peter.korsgaard@barco.com>
  6 */
  7
  8#include <linux/i2c.h>
  9#include <linux/i2c-mux.h>
 10#include <linux/platform_data/i2c-mux-gpio.h>
 11#include <linux/platform_device.h>
 12#include <linux/module.h>
 13#include <linux/slab.h>
 14#include <linux/bits.h>
 15#include <linux/gpio/consumer.h>
 16/* FIXME: stop poking around inside gpiolib */
 17#include "../../gpio/gpiolib.h"
 18
 19struct gpiomux {
 20	struct i2c_mux_gpio_platform_data data;
 21	int ngpios;
 22	struct gpio_desc **gpios;
 23};
 24
 25static void i2c_mux_gpio_set(const struct gpiomux *mux, unsigned val)
 26{
 27	DECLARE_BITMAP(values, BITS_PER_TYPE(val));
 28
 29	values[0] = val;
 30
 31	gpiod_set_array_value_cansleep(mux->ngpios, mux->gpios, NULL, values);
 32}
 33
 34static int i2c_mux_gpio_select(struct i2c_mux_core *muxc, u32 chan)
 35{
 36	struct gpiomux *mux = i2c_mux_priv(muxc);
 37
 38	i2c_mux_gpio_set(mux, chan);
 39
 40	return 0;
 41}
 42
 43static int i2c_mux_gpio_deselect(struct i2c_mux_core *muxc, u32 chan)
 44{
 45	struct gpiomux *mux = i2c_mux_priv(muxc);
 46
 47	i2c_mux_gpio_set(mux, mux->data.idle);
 48
 49	return 0;
 50}
 51
 52#ifdef CONFIG_ACPI
 53
 54static int i2c_mux_gpio_get_acpi_adr(struct device *dev,
 55				     struct fwnode_handle *fwdev,
 56				     unsigned int *adr)
 57
 58{
 59	unsigned long long adr64;
 60	acpi_status status;
 61
 62	status = acpi_evaluate_integer(ACPI_HANDLE_FWNODE(fwdev),
 63				       METHOD_NAME__ADR,
 64				       NULL, &adr64);
 65
 66	if (!ACPI_SUCCESS(status)) {
 67		dev_err(dev, "Cannot get address\n");
 68		return -EINVAL;
 69	}
 70
 71	*adr = adr64;
 72	if (*adr != adr64) {
 73		dev_err(dev, "Address out of range\n");
 74		return -ERANGE;
 75	}
 76
 77	return 0;
 78}
 79
 80#else
 81
 82static int i2c_mux_gpio_get_acpi_adr(struct device *dev,
 83				     struct fwnode_handle *fwdev,
 84				     unsigned int *adr)
 85{
 86	return -EINVAL;
 87}
 88
 89#endif
 90
 91static int i2c_mux_gpio_probe_fw(struct gpiomux *mux,
 92				 struct platform_device *pdev)
 93{
 94	struct device *dev = &pdev->dev;
 95	struct device_node *np = dev->of_node;
 96	struct device_node *adapter_np;
 97	struct i2c_adapter *adapter = NULL;
 98	struct fwnode_handle *child;
 99	unsigned *values;
100	int rc, i = 0;
101
102	if (is_of_node(dev->fwnode)) {
103		if (!np)
104			return -ENODEV;
105
106		adapter_np = of_parse_phandle(np, "i2c-parent", 0);
107		if (!adapter_np) {
108			dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
109			return -ENODEV;
110		}
111		adapter = of_find_i2c_adapter_by_node(adapter_np);
112		of_node_put(adapter_np);
113
114	} else if (is_acpi_node(dev->fwnode)) {
115		/*
116		 * In ACPI land the mux should be a direct child of the i2c
117		 * bus it muxes.
118		 */
119		acpi_handle dev_handle = ACPI_HANDLE(dev->parent);
120
121		adapter = i2c_acpi_find_adapter_by_handle(dev_handle);
 
 
 
122	}
123
 
124	if (!adapter)
125		return -EPROBE_DEFER;
126
127	mux->data.parent = i2c_adapter_id(adapter);
128	put_device(&adapter->dev);
129
130	mux->data.n_values = device_get_child_node_count(dev);
131	values = devm_kcalloc(dev,
 
132			      mux->data.n_values, sizeof(*mux->data.values),
133			      GFP_KERNEL);
134	if (!values) {
135		dev_err(dev, "Cannot allocate values array");
136		return -ENOMEM;
137	}
138
139	device_for_each_child_node(dev, child) {
140		if (is_of_node(child)) {
141			fwnode_property_read_u32(child, "reg", values + i);
142
143		} else if (is_acpi_node(child)) {
144			rc = i2c_mux_gpio_get_acpi_adr(dev, child, values + i);
145			if (rc)
146				return rc;
147		}
148
149		i++;
150	}
151	mux->data.values = values;
152
153	if (fwnode_property_read_u32(dev->fwnode, "idle-state", &mux->data.idle))
154		mux->data.idle = I2C_MUX_GPIO_NO_IDLE;
155
156	return 0;
157}
 
 
 
 
 
 
 
158
159static int i2c_mux_gpio_probe(struct platform_device *pdev)
160{
161	struct i2c_mux_core *muxc;
162	struct gpiomux *mux;
163	struct i2c_adapter *parent;
164	struct i2c_adapter *root;
165	unsigned initial_state;
166	int i, ngpios, ret;
167
168	mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
169	if (!mux)
170		return -ENOMEM;
171
172	if (!dev_get_platdata(&pdev->dev)) {
173		ret = i2c_mux_gpio_probe_fw(mux, pdev);
174		if (ret < 0)
175			return ret;
176	} else {
177		memcpy(&mux->data, dev_get_platdata(&pdev->dev),
178			sizeof(mux->data));
179	}
180
181	ngpios = gpiod_count(&pdev->dev, "mux");
182	if (ngpios <= 0) {
183		dev_err(&pdev->dev, "no valid gpios provided\n");
184		return ngpios ?: -EINVAL;
185	}
186	mux->ngpios = ngpios;
187
188	parent = i2c_get_adapter(mux->data.parent);
189	if (!parent)
190		return -EPROBE_DEFER;
191
192	muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values,
193			     ngpios * sizeof(*mux->gpios), 0,
194			     i2c_mux_gpio_select, NULL);
195	if (!muxc) {
196		ret = -ENOMEM;
197		goto alloc_failed;
198	}
199	mux->gpios = muxc->priv;
200	muxc->priv = mux;
201
202	platform_set_drvdata(pdev, muxc);
203
204	root = i2c_root_adapter(&parent->dev);
205
206	muxc->mux_locked = true;
207
208	if (mux->data.idle != I2C_MUX_GPIO_NO_IDLE) {
209		initial_state = mux->data.idle;
210		muxc->deselect = i2c_mux_gpio_deselect;
211	} else {
212		initial_state = mux->data.values[0];
213	}
214
215	for (i = 0; i < ngpios; i++) {
216		struct device *gpio_dev;
217		struct gpio_desc *gpiod;
218		enum gpiod_flags flag;
219
220		if (initial_state & BIT(i))
221			flag = GPIOD_OUT_HIGH;
222		else
223			flag = GPIOD_OUT_LOW;
224		gpiod = devm_gpiod_get_index(&pdev->dev, "mux", i, flag);
225		if (IS_ERR(gpiod)) {
226			ret = PTR_ERR(gpiod);
227			goto alloc_failed;
228		}
229
230		mux->gpios[i] = gpiod;
231
232		if (!muxc->mux_locked)
233			continue;
234
235		/* FIXME: find a proper way to access the GPIO device */
236		gpio_dev = &gpiod->gdev->dev;
237		muxc->mux_locked = i2c_root_adapter(gpio_dev) == root;
238	}
239
240	if (muxc->mux_locked)
241		dev_info(&pdev->dev, "mux-locked i2c mux\n");
242
243	for (i = 0; i < mux->data.n_values; i++) {
244		u32 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
245		unsigned int class = mux->data.classes ? mux->data.classes[i] : 0;
246
247		ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class);
248		if (ret)
249			goto add_adapter_failed;
250	}
251
252	dev_info(&pdev->dev, "%d port mux on %s adapter\n",
253		 mux->data.n_values, parent->name);
254
255	return 0;
256
257add_adapter_failed:
258	i2c_mux_del_adapters(muxc);
259alloc_failed:
260	i2c_put_adapter(parent);
261
262	return ret;
263}
264
265static int i2c_mux_gpio_remove(struct platform_device *pdev)
266{
267	struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
268
269	i2c_mux_del_adapters(muxc);
270	i2c_put_adapter(muxc->parent);
271
272	return 0;
273}
274
275static const struct of_device_id i2c_mux_gpio_of_match[] = {
276	{ .compatible = "i2c-mux-gpio", },
277	{},
278};
279MODULE_DEVICE_TABLE(of, i2c_mux_gpio_of_match);
280
281static struct platform_driver i2c_mux_gpio_driver = {
282	.probe	= i2c_mux_gpio_probe,
283	.remove	= i2c_mux_gpio_remove,
284	.driver	= {
285		.name	= "i2c-mux-gpio",
286		.of_match_table = i2c_mux_gpio_of_match,
287	},
288};
289
290module_platform_driver(i2c_mux_gpio_driver);
291
292MODULE_DESCRIPTION("GPIO-based I2C multiplexer driver");
293MODULE_AUTHOR("Peter Korsgaard <peter.korsgaard@barco.com>");
294MODULE_LICENSE("GPL");
295MODULE_ALIAS("platform:i2c-mux-gpio");