2 * I2C multiplexer using a single register
4 * Copyright 2015 Freescale Semiconductor
5 * York Sun <yorksun@freescale.com>
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.
13 #include <linux/i2c.h>
14 #include <linux/i2c-mux.h>
15 #include <linux/init.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>
24 struct i2c_mux_reg_platform_data data
;
27 static int i2c_mux_reg_set(const struct regmux
*mux
, unsigned int chan_id
)
33 * Write to the register, followed by a read to ensure the write is
34 * completed on a "posted" bus, for example PCI or write buffers.
35 * The endianness of reading doesn't matter and the return data
38 switch (mux
->data
.reg_size
) {
40 if (mux
->data
.little_endian
)
41 iowrite32(chan_id
, mux
->data
.reg
);
43 iowrite32be(chan_id
, mux
->data
.reg
);
44 if (!mux
->data
.write_only
)
45 ioread32(mux
->data
.reg
);
48 if (mux
->data
.little_endian
)
49 iowrite16(chan_id
, mux
->data
.reg
);
51 iowrite16be(chan_id
, mux
->data
.reg
);
52 if (!mux
->data
.write_only
)
53 ioread16(mux
->data
.reg
);
56 iowrite8(chan_id
, mux
->data
.reg
);
57 if (!mux
->data
.write_only
)
58 ioread8(mux
->data
.reg
);
65 static int i2c_mux_reg_select(struct i2c_mux_core
*muxc
, u32 chan
)
67 struct regmux
*mux
= i2c_mux_priv(muxc
);
69 return i2c_mux_reg_set(mux
, chan
);
72 static int i2c_mux_reg_deselect(struct i2c_mux_core
*muxc
, u32 chan
)
74 struct regmux
*mux
= i2c_mux_priv(muxc
);
76 if (mux
->data
.idle_in_use
)
77 return i2c_mux_reg_set(mux
, mux
->data
.idle
);
83 static int i2c_mux_reg_probe_dt(struct regmux
*mux
,
84 struct platform_device
*pdev
)
86 struct device_node
*np
= pdev
->dev
.of_node
;
87 struct device_node
*adapter_np
, *child
;
88 struct i2c_adapter
*adapter
;
96 adapter_np
= of_parse_phandle(np
, "i2c-parent", 0);
98 dev_err(&pdev
->dev
, "Cannot parse i2c-parent\n");
101 adapter
= of_find_i2c_adapter_by_node(adapter_np
);
102 of_node_put(adapter_np
);
104 return -EPROBE_DEFER
;
106 mux
->data
.parent
= i2c_adapter_id(adapter
);
107 put_device(&adapter
->dev
);
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;
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;
122 #error Endianness not defined?
125 mux
->data
.write_only
= of_property_read_bool(np
, "write-only");
127 values
= devm_kcalloc(&pdev
->dev
,
128 mux
->data
.n_values
, sizeof(*mux
->data
.values
),
133 for_each_child_of_node(np
, child
) {
134 of_property_read_u32(child
, "reg", values
+ i
);
137 mux
->data
.values
= values
;
139 if (!of_property_read_u32(np
, "idle-state", &mux
->data
.idle
))
140 mux
->data
.idle_in_use
= true;
142 /* map address from "reg" if exists */
143 if (of_address_to_resource(np
, 0, &res
) == 0) {
144 mux
->data
.reg_size
= resource_size(&res
);
145 mux
->data
.reg
= devm_ioremap_resource(&pdev
->dev
, &res
);
146 if (IS_ERR(mux
->data
.reg
))
147 return PTR_ERR(mux
->data
.reg
);
153 static int i2c_mux_reg_probe_dt(struct regmux
*mux
,
154 struct platform_device
*pdev
)
160 static int i2c_mux_reg_probe(struct platform_device
*pdev
)
162 struct i2c_mux_core
*muxc
;
164 struct i2c_adapter
*parent
;
165 struct resource
*res
;
169 mux
= devm_kzalloc(&pdev
->dev
, sizeof(*mux
), GFP_KERNEL
);
173 if (dev_get_platdata(&pdev
->dev
)) {
174 memcpy(&mux
->data
, dev_get_platdata(&pdev
->dev
),
177 ret
= i2c_mux_reg_probe_dt(mux
, pdev
);
178 if (ret
== -EPROBE_DEFER
)
182 dev_err(&pdev
->dev
, "Error parsing device tree");
187 parent
= i2c_get_adapter(mux
->data
.parent
);
189 return -EPROBE_DEFER
;
191 if (!mux
->data
.reg
) {
193 "Register not set, using platform resource\n");
194 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
195 mux
->data
.reg_size
= resource_size(res
);
196 mux
->data
.reg
= devm_ioremap_resource(&pdev
->dev
, res
);
197 if (IS_ERR(mux
->data
.reg
)) {
198 ret
= PTR_ERR(mux
->data
.reg
);
203 if (mux
->data
.reg_size
!= 4 && mux
->data
.reg_size
!= 2 &&
204 mux
->data
.reg_size
!= 1) {
205 dev_err(&pdev
->dev
, "Invalid register size\n");
210 muxc
= i2c_mux_alloc(parent
, &pdev
->dev
, mux
->data
.n_values
, 0, 0,
211 i2c_mux_reg_select
, NULL
);
218 platform_set_drvdata(pdev
, muxc
);
220 if (mux
->data
.idle_in_use
)
221 muxc
->deselect
= i2c_mux_reg_deselect
;
223 for (i
= 0; i
< mux
->data
.n_values
; i
++) {
224 nr
= mux
->data
.base_nr
? (mux
->data
.base_nr
+ i
) : 0;
225 class = mux
->data
.classes
? mux
->data
.classes
[i
] : 0;
227 ret
= i2c_mux_add_adapter(muxc
, nr
, mux
->data
.values
[i
], class);
229 goto err_del_mux_adapters
;
232 dev_dbg(&pdev
->dev
, "%d port mux on %s adapter\n",
233 mux
->data
.n_values
, muxc
->parent
->name
);
237 err_del_mux_adapters
:
238 i2c_mux_del_adapters(muxc
);
240 i2c_put_adapter(parent
);
245 static int i2c_mux_reg_remove(struct platform_device
*pdev
)
247 struct i2c_mux_core
*muxc
= platform_get_drvdata(pdev
);
249 i2c_mux_del_adapters(muxc
);
250 i2c_put_adapter(muxc
->parent
);
255 static const struct of_device_id i2c_mux_reg_of_match
[] = {
256 { .compatible
= "i2c-mux-reg", },
259 MODULE_DEVICE_TABLE(of
, i2c_mux_reg_of_match
);
261 static struct platform_driver i2c_mux_reg_driver
= {
262 .probe
= i2c_mux_reg_probe
,
263 .remove
= i2c_mux_reg_remove
,
265 .name
= "i2c-mux-reg",
266 .of_match_table
= of_match_ptr(i2c_mux_reg_of_match
),
270 module_platform_driver(i2c_mux_reg_driver
);
272 MODULE_DESCRIPTION("Register-based I2C multiplexer driver");
273 MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
274 MODULE_LICENSE("GPL");
275 MODULE_ALIAS("platform:i2c-mux-reg");