1
2
3
4
5
6
7
8
9
10
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_mux_reg_platform_data data;
25};
26
27static int i2c_mux_reg_set(const struct regmux *mux, unsigned int chan_id)
28{
29 if (!mux->data.reg)
30 return -EINVAL;
31
32
33
34
35
36
37
38 switch (mux->data.reg_size) {
39 case 4:
40 if (mux->data.little_endian)
41 iowrite32(chan_id, mux->data.reg);
42 else
43 iowrite32be(chan_id, mux->data.reg);
44 if (!mux->data.write_only)
45 ioread32(mux->data.reg);
46 break;
47 case 2:
48 if (mux->data.little_endian)
49 iowrite16(chan_id, mux->data.reg);
50 else
51 iowrite16be(chan_id, mux->data.reg);
52 if (!mux->data.write_only)
53 ioread16(mux->data.reg);
54 break;
55 case 1:
56 iowrite8(chan_id, mux->data.reg);
57 if (!mux->data.write_only)
58 ioread8(mux->data.reg);
59 break;
60 }
61
62 return 0;
63}
64
65static int i2c_mux_reg_select(struct i2c_mux_core *muxc, u32 chan)
66{
67 struct regmux *mux = i2c_mux_priv(muxc);
68
69 return i2c_mux_reg_set(mux, chan);
70}
71
72static int i2c_mux_reg_deselect(struct i2c_mux_core *muxc, u32 chan)
73{
74 struct regmux *mux = i2c_mux_priv(muxc);
75
76 if (mux->data.idle_in_use)
77 return i2c_mux_reg_set(mux, mux->data.idle);
78
79 return 0;
80}
81
82#ifdef CONFIG_OF
83static int i2c_mux_reg_probe_dt(struct regmux *mux,
84 struct platform_device *pdev)
85{
86 struct device_node *np = pdev->dev.of_node;
87 struct device_node *adapter_np, *child;
88 struct i2c_adapter *adapter;
89 struct resource res;
90 unsigned *values;
91 int i = 0;
92
93 if (!np)
94 return -ENODEV;
95
96 adapter_np = of_parse_phandle(np, "i2c-parent", 0);
97 if (!adapter_np) {
98 dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
99 return -ENODEV;
100 }
101 adapter = of_find_i2c_adapter_by_node(adapter_np);
102 of_node_put(adapter_np);
103 if (!adapter)
104 return -EPROBE_DEFER;
105
106 mux->data.parent = i2c_adapter_id(adapter);
107 put_device(&adapter->dev);
108
109 mux->data.n_values = of_get_child_count(np);
110 if (of_property_read_bool(np, "little-endian")) {
111 mux->data.little_endian = true;
112 } else if (of_property_read_bool(np, "big-endian")) {
113 mux->data.little_endian = false;
114 } else {
115#if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : \
116 defined(__LITTLE_ENDIAN)
117 mux->data.little_endian = true;
118#elif defined(__BYTE_ORDER) ? __BYTE_ORDER == __BIG_ENDIAN : \
119 defined(__BIG_ENDIAN)
120 mux->data.little_endian = false;
121#else
122#error Endianness not defined?
123#endif
124 }
125 mux->data.write_only = of_property_read_bool(np, "write-only");
126
127 values = devm_kzalloc(&pdev->dev,
128 sizeof(*mux->data.values) * mux->data.n_values,
129 GFP_KERNEL);
130 if (!values) {
131 dev_err(&pdev->dev, "Cannot allocate values array");
132 return -ENOMEM;
133 }
134
135 for_each_child_of_node(np, child) {
136 of_property_read_u32(child, "reg", values + i);
137 i++;
138 }
139 mux->data.values = values;
140
141 if (!of_property_read_u32(np, "idle-state", &mux->data.idle))
142 mux->data.idle_in_use = true;
143
144
145 if (of_address_to_resource(np, 0, &res) == 0) {
146 mux->data.reg_size = resource_size(&res);
147 mux->data.reg = devm_ioremap_resource(&pdev->dev, &res);
148 if (IS_ERR(mux->data.reg))
149 return PTR_ERR(mux->data.reg);
150 }
151
152 return 0;
153}
154#else
155static int i2c_mux_reg_probe_dt(struct regmux *mux,
156 struct platform_device *pdev)
157{
158 return 0;
159}
160#endif
161
162static int i2c_mux_reg_probe(struct platform_device *pdev)
163{
164 struct i2c_mux_core *muxc;
165 struct regmux *mux;
166 struct i2c_adapter *parent;
167 struct resource *res;
168 unsigned int class;
169 int i, ret, nr;
170
171 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
172 if (!mux)
173 return -ENOMEM;
174
175 if (dev_get_platdata(&pdev->dev)) {
176 memcpy(&mux->data, dev_get_platdata(&pdev->dev),
177 sizeof(mux->data));
178 } else {
179 ret = i2c_mux_reg_probe_dt(mux, pdev);
180 if (ret == -EPROBE_DEFER)
181 return ret;
182
183 if (ret < 0) {
184 dev_err(&pdev->dev, "Error parsing device tree");
185 return ret;
186 }
187 }
188
189 parent = i2c_get_adapter(mux->data.parent);
190 if (!parent)
191 return -EPROBE_DEFER;
192
193 if (!mux->data.reg) {
194 dev_info(&pdev->dev,
195 "Register not set, using platform resource\n");
196 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
197 mux->data.reg_size = resource_size(res);
198 mux->data.reg = devm_ioremap_resource(&pdev->dev, res);
199 if (IS_ERR(mux->data.reg)) {
200 ret = PTR_ERR(mux->data.reg);
201 goto err_put_parent;
202 }
203 }
204
205 if (mux->data.reg_size != 4 && mux->data.reg_size != 2 &&
206 mux->data.reg_size != 1) {
207 dev_err(&pdev->dev, "Invalid register size\n");
208 ret = -EINVAL;
209 goto err_put_parent;
210 }
211
212 muxc = i2c_mux_alloc(parent, &pdev->dev, mux->data.n_values, 0, 0,
213 i2c_mux_reg_select, NULL);
214 if (!muxc) {
215 ret = -ENOMEM;
216 goto err_put_parent;
217 }
218 muxc->priv = mux;
219
220 platform_set_drvdata(pdev, muxc);
221
222 if (mux->data.idle_in_use)
223 muxc->deselect = i2c_mux_reg_deselect;
224
225 for (i = 0; i < mux->data.n_values; i++) {
226 nr = mux->data.base_nr ? (mux->data.base_nr + i) : 0;
227 class = mux->data.classes ? mux->data.classes[i] : 0;
228
229 ret = i2c_mux_add_adapter(muxc, nr, mux->data.values[i], class);
230 if (ret)
231 goto err_del_mux_adapters;
232 }
233
234 dev_dbg(&pdev->dev, "%d port mux on %s adapter\n",
235 mux->data.n_values, muxc->parent->name);
236
237 return 0;
238
239err_del_mux_adapters:
240 i2c_mux_del_adapters(muxc);
241err_put_parent:
242 i2c_put_adapter(parent);
243
244 return ret;
245}
246
247static int i2c_mux_reg_remove(struct platform_device *pdev)
248{
249 struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
250
251 i2c_mux_del_adapters(muxc);
252 i2c_put_adapter(muxc->parent);
253
254 return 0;
255}
256
257static const struct of_device_id i2c_mux_reg_of_match[] = {
258 { .compatible = "i2c-mux-reg", },
259 {},
260};
261MODULE_DEVICE_TABLE(of, i2c_mux_reg_of_match);
262
263static struct platform_driver i2c_mux_reg_driver = {
264 .probe = i2c_mux_reg_probe,
265 .remove = i2c_mux_reg_remove,
266 .driver = {
267 .name = "i2c-mux-reg",
268 .of_match_table = of_match_ptr(i2c_mux_reg_of_match),
269 },
270};
271
272module_platform_driver(i2c_mux_reg_driver);
273
274MODULE_DESCRIPTION("Register-based I2C multiplexer driver");
275MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
276MODULE_LICENSE("GPL");
277MODULE_ALIAS("platform:i2c-mux-reg");
278