Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * I2C multiplexer using a single register
  3 *
  4 * Copyright 2015 Freescale Semiconductor
  5 * York Sun  <yorksun@freescale.com>
  6 *
  7 * This program is free software; you can redistribute it and/or modify it
  8 * under the terms of the GNU General Public License as published by the
  9 * Free Software Foundation; either version 2 of the License, or (at your
 10 * option) any later version.
 11 */
 12
 13#include <linux/i2c.h>
 14#include <linux/i2c-mux.h>
 15#include <linux/init.h>
 16#include <linux/io.h>
 17#include <linux/module.h>
 18#include <linux/of_address.h>
 19#include <linux/platform_data/i2c-mux-reg.h>
 20#include <linux/platform_device.h>
 21#include <linux/slab.h>
 22
 23struct regmux {
 24	struct i2c_adapter *parent;
 25	struct i2c_adapter **adap; /* child busses */
 26	struct i2c_mux_reg_platform_data data;
 27};
 28
 29static int i2c_mux_reg_set(const struct regmux *mux, unsigned int chan_id)
 30{
 31	if (!mux->data.reg)
 32		return -EINVAL;
 33
 34	/*
 35	 * Write to the register, followed by a read to ensure the write is
 36	 * completed on a "posted" bus, for example PCI or write buffers.
 37	 * The endianness of reading doesn't matter and the return data
 38	 * is not used.
 39	 */
 40	switch (mux->data.reg_size) {
 41	case 4:
 42		if (mux->data.little_endian)
 43			iowrite32(chan_id, mux->data.reg);
 44		else
 45			iowrite32be(chan_id, mux->data.reg);
 46		if (!mux->data.write_only)
 47			ioread32(mux->data.reg);
 48		break;
 49	case 2:
 50		if (mux->data.little_endian)
 51			iowrite16(chan_id, mux->data.reg);
 52		else
 53			iowrite16be(chan_id, mux->data.reg);
 54		if (!mux->data.write_only)
 55			ioread16(mux->data.reg);
 56		break;
 57	case 1:
 58		iowrite8(chan_id, mux->data.reg);
 59		if (!mux->data.write_only)
 60			ioread8(mux->data.reg);
 61		break;
 62	}
 63
 64	return 0;
 65}
 66
 67static int i2c_mux_reg_select(struct i2c_adapter *adap, void *data,
 68			      unsigned int chan)
 69{
 70	struct regmux *mux = data;
 71
 72	return i2c_mux_reg_set(mux, chan);
 73}
 74
 75static int i2c_mux_reg_deselect(struct i2c_adapter *adap, void *data,
 76				unsigned int chan)
 77{
 78	struct regmux *mux = data;
 79
 80	if (mux->data.idle_in_use)
 81		return i2c_mux_reg_set(mux, mux->data.idle);
 82
 83	return 0;
 84}
 85
 86#ifdef CONFIG_OF
 87static int i2c_mux_reg_probe_dt(struct regmux *mux,
 88					struct platform_device *pdev)
 89{
 90	struct device_node *np = pdev->dev.of_node;
 91	struct device_node *adapter_np, *child;
 92	struct i2c_adapter *adapter;
 93	struct resource res;
 94	unsigned *values;
 95	int i = 0;
 96
 97	if (!np)
 98		return -ENODEV;
 99
100	adapter_np = of_parse_phandle(np, "i2c-parent", 0);
101	if (!adapter_np) {
102		dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
103		return -ENODEV;
104	}
105	adapter = of_find_i2c_adapter_by_node(adapter_np);
106	of_node_put(adapter_np);
107	if (!adapter)
108		return -EPROBE_DEFER;
109
110	mux->parent = adapter;
111	mux->data.parent = i2c_adapter_id(adapter);
112	put_device(&adapter->dev);
113
114	mux->data.n_values = of_get_child_count(np);
115	if (of_find_property(np, "little-endian", NULL)) {
116		mux->data.little_endian = true;
117	} else if (of_find_property(np, "big-endian", NULL)) {
118		mux->data.little_endian = false;
119	} else {
120#if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : \
121	defined(__LITTLE_ENDIAN)
122		mux->data.little_endian = true;
123#elif defined(__BYTE_ORDER) ? __BYTE_ORDER == __BIG_ENDIAN : \
124	defined(__BIG_ENDIAN)
125		mux->data.little_endian = false;
126#else
127#error Endianness not defined?
128#endif
129	}
130	if (of_find_property(np, "write-only", NULL))
131		mux->data.write_only = true;
132	else
133		mux->data.write_only = false;
134
135	values = devm_kzalloc(&pdev->dev,
136			      sizeof(*mux->data.values) * mux->data.n_values,
137			      GFP_KERNEL);
138	if (!values) {
139		dev_err(&pdev->dev, "Cannot allocate values array");
140		return -ENOMEM;
141	}
142
143	for_each_child_of_node(np, child) {
144		of_property_read_u32(child, "reg", values + i);
145		i++;
146	}
147	mux->data.values = values;
148
149	if (!of_property_read_u32(np, "idle-state", &mux->data.idle))
150		mux->data.idle_in_use = true;
151
152	/* map address from "reg" if exists */
153	if (of_address_to_resource(np, 0, &res)) {
154		mux->data.reg_size = resource_size(&res);
155		mux->data.reg = devm_ioremap_resource(&pdev->dev, &res);
156		if (IS_ERR(mux->data.reg))
157			return PTR_ERR(mux->data.reg);
158	}
159
160	return 0;
161}
162#else
163static int i2c_mux_reg_probe_dt(struct regmux *mux,
164					struct platform_device *pdev)
165{
166	return 0;
167}
168#endif
169
170static int i2c_mux_reg_probe(struct platform_device *pdev)
171{
 
172	struct regmux *mux;
173	struct i2c_adapter *parent;
174	struct resource *res;
175	int (*deselect)(struct i2c_adapter *, void *, u32);
176	unsigned int class;
177	int i, ret, nr;
178
179	mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
180	if (!mux)
181		return -ENOMEM;
182
183	platform_set_drvdata(pdev, mux);
184
185	if (dev_get_platdata(&pdev->dev)) {
186		memcpy(&mux->data, dev_get_platdata(&pdev->dev),
187			sizeof(mux->data));
188
189		parent = i2c_get_adapter(mux->data.parent);
190		if (!parent)
191			return -EPROBE_DEFER;
192
193		mux->parent = parent;
194	} else {
195		ret = i2c_mux_reg_probe_dt(mux, pdev);
 
 
 
196		if (ret < 0) {
197			dev_err(&pdev->dev, "Error parsing device tree");
198			return ret;
199		}
200	}
201
 
 
 
 
202	if (!mux->data.reg) {
203		dev_info(&pdev->dev,
204			"Register not set, using platform resource\n");
205		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
206		mux->data.reg_size = resource_size(res);
207		mux->data.reg = devm_ioremap_resource(&pdev->dev, res);
208		if (IS_ERR(mux->data.reg))
209			return PTR_ERR(mux->data.reg);
 
 
210	}
211
212	if (mux->data.reg_size != 4 && mux->data.reg_size != 2 &&
213	    mux->data.reg_size != 1) {
214		dev_err(&pdev->dev, "Invalid register size\n");
215		return -EINVAL;
 
216	}
217
218	mux->adap = devm_kzalloc(&pdev->dev,
219				 sizeof(*mux->adap) * mux->data.n_values,
220				 GFP_KERNEL);
221	if (!mux->adap) {
222		dev_err(&pdev->dev, "Cannot allocate i2c_adapter structure");
223		return -ENOMEM;
224	}
 
 
 
225
226	if (mux->data.idle_in_use)
227		deselect = i2c_mux_reg_deselect;
228	else
229		deselect = NULL;
230
231	for (i = 0; i < mux->data.n_values; i++) {
232		nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
233		class = mux->data.classes ? mux->data.classes[i] : 0;
234
235		mux->adap[i] = i2c_add_mux_adapter(mux->parent, &pdev->dev, mux,
236						   nr, mux->data.values[i],
237						   class, i2c_mux_reg_select,
238						   deselect);
239		if (!mux->adap[i]) {
240			ret = -ENODEV;
241			dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
242			goto add_adapter_failed;
243		}
244	}
245
246	dev_dbg(&pdev->dev, "%d port mux on %s adapter\n",
247		 mux->data.n_values, mux->parent->name);
248
249	return 0;
250
251add_adapter_failed:
252	for (; i > 0; i--)
253		i2c_del_mux_adapter(mux->adap[i - 1]);
 
254
255	return ret;
256}
257
258static int i2c_mux_reg_remove(struct platform_device *pdev)
259{
260	struct regmux *mux = platform_get_drvdata(pdev);
261	int i;
262
263	for (i = 0; i < mux->data.n_values; i++)
264		i2c_del_mux_adapter(mux->adap[i]);
265
266	i2c_put_adapter(mux->parent);
 
267
268	return 0;
269}
270
271static const struct of_device_id i2c_mux_reg_of_match[] = {
272	{ .compatible = "i2c-mux-reg", },
273	{},
274};
275MODULE_DEVICE_TABLE(of, i2c_mux_reg_of_match);
276
277static struct platform_driver i2c_mux_reg_driver = {
278	.probe	= i2c_mux_reg_probe,
279	.remove	= i2c_mux_reg_remove,
280	.driver	= {
281		.name	= "i2c-mux-reg",
 
282	},
283};
284
285module_platform_driver(i2c_mux_reg_driver);
286
287MODULE_DESCRIPTION("Register-based I2C multiplexer driver");
288MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
289MODULE_LICENSE("GPL");
290MODULE_ALIAS("platform:i2c-mux-reg");
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * I2C multiplexer using a single register
  4 *
  5 * Copyright 2015 Freescale Semiconductor
  6 * York Sun  <yorksun@freescale.com>
 
 
 
 
 
  7 */
  8
  9#include <linux/i2c.h>
 10#include <linux/i2c-mux.h>
 11#include <linux/init.h>
 12#include <linux/io.h>
 13#include <linux/module.h>
 14#include <linux/of_address.h>
 15#include <linux/platform_data/i2c-mux-reg.h>
 16#include <linux/platform_device.h>
 17#include <linux/slab.h>
 18
 19struct regmux {
 
 
 20	struct i2c_mux_reg_platform_data data;
 21};
 22
 23static int i2c_mux_reg_set(const struct regmux *mux, unsigned int chan_id)
 24{
 25	if (!mux->data.reg)
 26		return -EINVAL;
 27
 28	/*
 29	 * Write to the register, followed by a read to ensure the write is
 30	 * completed on a "posted" bus, for example PCI or write buffers.
 31	 * The endianness of reading doesn't matter and the return data
 32	 * is not used.
 33	 */
 34	switch (mux->data.reg_size) {
 35	case 4:
 36		if (mux->data.little_endian)
 37			iowrite32(chan_id, mux->data.reg);
 38		else
 39			iowrite32be(chan_id, mux->data.reg);
 40		if (!mux->data.write_only)
 41			ioread32(mux->data.reg);
 42		break;
 43	case 2:
 44		if (mux->data.little_endian)
 45			iowrite16(chan_id, mux->data.reg);
 46		else
 47			iowrite16be(chan_id, mux->data.reg);
 48		if (!mux->data.write_only)
 49			ioread16(mux->data.reg);
 50		break;
 51	case 1:
 52		iowrite8(chan_id, mux->data.reg);
 53		if (!mux->data.write_only)
 54			ioread8(mux->data.reg);
 55		break;
 56	}
 57
 58	return 0;
 59}
 60
 61static int i2c_mux_reg_select(struct i2c_mux_core *muxc, u32 chan)
 
 62{
 63	struct regmux *mux = i2c_mux_priv(muxc);
 64
 65	return i2c_mux_reg_set(mux, chan);
 66}
 67
 68static int i2c_mux_reg_deselect(struct i2c_mux_core *muxc, u32 chan)
 
 69{
 70	struct regmux *mux = i2c_mux_priv(muxc);
 71
 72	if (mux->data.idle_in_use)
 73		return i2c_mux_reg_set(mux, mux->data.idle);
 74
 75	return 0;
 76}
 77
 78#ifdef CONFIG_OF
 79static int i2c_mux_reg_probe_dt(struct regmux *mux,
 80				struct platform_device *pdev)
 81{
 82	struct device_node *np = pdev->dev.of_node;
 83	struct device_node *adapter_np, *child;
 84	struct i2c_adapter *adapter;
 85	struct resource res;
 86	unsigned *values;
 87	int i = 0;
 88
 89	if (!np)
 90		return -ENODEV;
 91
 92	adapter_np = of_parse_phandle(np, "i2c-parent", 0);
 93	if (!adapter_np) {
 94		dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
 95		return -ENODEV;
 96	}
 97	adapter = of_find_i2c_adapter_by_node(adapter_np);
 98	of_node_put(adapter_np);
 99	if (!adapter)
100		return -EPROBE_DEFER;
101
 
102	mux->data.parent = i2c_adapter_id(adapter);
103	put_device(&adapter->dev);
104
105	mux->data.n_values = of_get_child_count(np);
106	if (of_property_read_bool(np, "little-endian")) {
107		mux->data.little_endian = true;
108	} else if (of_property_read_bool(np, "big-endian")) {
109		mux->data.little_endian = false;
110	} else {
111#if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : \
112	defined(__LITTLE_ENDIAN)
113		mux->data.little_endian = true;
114#elif defined(__BYTE_ORDER) ? __BYTE_ORDER == __BIG_ENDIAN : \
115	defined(__BIG_ENDIAN)
116		mux->data.little_endian = false;
117#else
118#error Endianness not defined?
119#endif
120	}
121	mux->data.write_only = of_property_read_bool(np, "write-only");
 
 
 
122
123	values = devm_kcalloc(&pdev->dev,
124			      mux->data.n_values, sizeof(*mux->data.values),
125			      GFP_KERNEL);
126	if (!values)
 
127		return -ENOMEM;
 
128
129	for_each_child_of_node(np, child) {
130		of_property_read_u32(child, "reg", values + i);
131		i++;
132	}
133	mux->data.values = values;
134
135	if (!of_property_read_u32(np, "idle-state", &mux->data.idle))
136		mux->data.idle_in_use = true;
137
138	/* map address from "reg" if exists */
139	if (of_address_to_resource(np, 0, &res) == 0) {
140		mux->data.reg_size = resource_size(&res);
141		mux->data.reg = devm_ioremap_resource(&pdev->dev, &res);
142		if (IS_ERR(mux->data.reg))
143			return PTR_ERR(mux->data.reg);
144	}
145
146	return 0;
147}
148#else
149static int i2c_mux_reg_probe_dt(struct regmux *mux,
150				struct platform_device *pdev)
151{
152	return 0;
153}
154#endif
155
156static int i2c_mux_reg_probe(struct platform_device *pdev)
157{
158	struct i2c_mux_core *muxc;
159	struct regmux *mux;
160	struct i2c_adapter *parent;
161	struct resource *res;
 
162	unsigned int class;
163	int i, ret, nr;
164
165	mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
166	if (!mux)
167		return -ENOMEM;
168
 
 
169	if (dev_get_platdata(&pdev->dev)) {
170		memcpy(&mux->data, dev_get_platdata(&pdev->dev),
171			sizeof(mux->data));
 
 
 
 
 
 
172	} else {
173		ret = i2c_mux_reg_probe_dt(mux, pdev);
174		if (ret == -EPROBE_DEFER)
175			return ret;
176
177		if (ret < 0) {
178			dev_err(&pdev->dev, "Error parsing device tree");
179			return ret;
180		}
181	}
182
183	parent = i2c_get_adapter(mux->data.parent);
184	if (!parent)
185		return -EPROBE_DEFER;
186
187	if (!mux->data.reg) {
188		dev_info(&pdev->dev,
189			"Register not set, using platform resource\n");
190		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
191		mux->data.reg_size = resource_size(res);
192		mux->data.reg = devm_ioremap_resource(&pdev->dev, res);
193		if (IS_ERR(mux->data.reg)) {
194			ret = PTR_ERR(mux->data.reg);
195			goto err_put_parent;
196		}
197	}
198
199	if (mux->data.reg_size != 4 && mux->data.reg_size != 2 &&
200	    mux->data.reg_size != 1) {
201		dev_err(&pdev->dev, "Invalid register size\n");
202		ret = -EINVAL;
203		goto err_put_parent;
204	}
205
206	muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values, 0, 0,
207			     i2c_mux_reg_select, NULL);
208	if (!muxc) {
209		ret = -ENOMEM;
210		goto err_put_parent;
 
211	}
212	muxc->priv = mux;
213
214	platform_set_drvdata(pdev, muxc);
215
216	if (mux->data.idle_in_use)
217		muxc->deselect = i2c_mux_reg_deselect;
 
 
218
219	for (i = 0; i < mux->data.n_values; i++) {
220		nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
221		class = mux->data.classes ? mux->data.classes[i] : 0;
222
223		ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class);
224		if (ret)
225			goto err_del_mux_adapters;
 
 
 
 
 
 
226	}
227
228	dev_dbg(&pdev->dev, "%d port mux on %s adapter\n",
229		 mux->data.n_values, muxc->parent->name);
230
231	return 0;
232
233err_del_mux_adapters:
234	i2c_mux_del_adapters(muxc);
235err_put_parent:
236	i2c_put_adapter(parent);
237
238	return ret;
239}
240
241static int i2c_mux_reg_remove(struct platform_device *pdev)
242{
243	struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
 
 
 
 
244
245	i2c_mux_del_adapters(muxc);
246	i2c_put_adapter(muxc->parent);
247
248	return 0;
249}
250
251static const struct of_device_id i2c_mux_reg_of_match[] = {
252	{ .compatible = "i2c-mux-reg", },
253	{},
254};
255MODULE_DEVICE_TABLE(of, i2c_mux_reg_of_match);
256
257static struct platform_driver i2c_mux_reg_driver = {
258	.probe	= i2c_mux_reg_probe,
259	.remove	= i2c_mux_reg_remove,
260	.driver	= {
261		.name	= "i2c-mux-reg",
262		.of_match_table = of_match_ptr(i2c_mux_reg_of_match),
263	},
264};
265
266module_platform_driver(i2c_mux_reg_driver);
267
268MODULE_DESCRIPTION("Register-based I2C multiplexer driver");
269MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
270MODULE_LICENSE("GPL");
271MODULE_ALIAS("platform:i2c-mux-reg");