xref: /linux/drivers/i2c/muxes/i2c-mux-reg.c (revision 4b660dbd9ee2059850fd30e0df420ca7a38a1856)
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 
19 struct regmux {
20 	struct i2c_mux_reg_platform_data data;
21 };
22 
23 static 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 
61 static 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 
68 static 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
79 static 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
149 static int i2c_mux_reg_probe_dt(struct regmux *mux,
150 				struct platform_device *pdev)
151 {
152 	return 0;
153 }
154 #endif
155 
156 static 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 	int i, ret, nr;
163 
164 	mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
165 	if (!mux)
166 		return -ENOMEM;
167 
168 	if (dev_get_platdata(&pdev->dev)) {
169 		memcpy(&mux->data, dev_get_platdata(&pdev->dev),
170 			sizeof(mux->data));
171 	} else {
172 		ret = i2c_mux_reg_probe_dt(mux, pdev);
173 		if (ret < 0)
174 			return dev_err_probe(&pdev->dev, ret,
175 					     "Error parsing device tree");
176 	}
177 
178 	parent = i2c_get_adapter(mux->data.parent);
179 	if (!parent)
180 		return -EPROBE_DEFER;
181 
182 	if (!mux->data.reg) {
183 		dev_info(&pdev->dev,
184 			"Register not set, using platform resource\n");
185 		mux->data.reg = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
186 		if (IS_ERR(mux->data.reg)) {
187 			ret = PTR_ERR(mux->data.reg);
188 			goto err_put_parent;
189 		}
190 		mux->data.reg_size = resource_size(res);
191 	}
192 
193 	if (mux->data.reg_size != 4 && mux->data.reg_size != 2 &&
194 	    mux->data.reg_size != 1) {
195 		dev_err(&pdev->dev, "Invalid register size\n");
196 		ret = -EINVAL;
197 		goto err_put_parent;
198 	}
199 
200 	muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values, 0, 0,
201 			     i2c_mux_reg_select, NULL);
202 	if (!muxc) {
203 		ret = -ENOMEM;
204 		goto err_put_parent;
205 	}
206 	muxc->priv = mux;
207 
208 	platform_set_drvdata(pdev, muxc);
209 
210 	if (mux->data.idle_in_use)
211 		muxc->deselect = i2c_mux_reg_deselect;
212 
213 	for (i = 0; i < mux->data.n_values; i++) {
214 		nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
215 
216 		ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], 0);
217 		if (ret)
218 			goto err_del_mux_adapters;
219 	}
220 
221 	dev_dbg(&pdev->dev, "%d port mux on %s adapter\n",
222 		 mux->data.n_values, muxc->parent->name);
223 
224 	return 0;
225 
226 err_del_mux_adapters:
227 	i2c_mux_del_adapters(muxc);
228 err_put_parent:
229 	i2c_put_adapter(parent);
230 
231 	return ret;
232 }
233 
234 static void i2c_mux_reg_remove(struct platform_device *pdev)
235 {
236 	struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
237 
238 	i2c_mux_del_adapters(muxc);
239 	i2c_put_adapter(muxc->parent);
240 }
241 
242 static const struct of_device_id i2c_mux_reg_of_match[] = {
243 	{ .compatible = "i2c-mux-reg", },
244 	{},
245 };
246 MODULE_DEVICE_TABLE(of, i2c_mux_reg_of_match);
247 
248 static struct platform_driver i2c_mux_reg_driver = {
249 	.probe	= i2c_mux_reg_probe,
250 	.remove_new = i2c_mux_reg_remove,
251 	.driver	= {
252 		.name	= "i2c-mux-reg",
253 		.of_match_table = of_match_ptr(i2c_mux_reg_of_match),
254 	},
255 };
256 
257 module_platform_driver(i2c_mux_reg_driver);
258 
259 MODULE_DESCRIPTION("Register-based I2C multiplexer driver");
260 MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
261 MODULE_LICENSE("GPL");
262 MODULE_ALIAS("platform:i2c-mux-reg");
263