1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * I2C multiplexer using a single register
5 * Copyright 2015 Freescale Semiconductor
6 * York Sun <yorksun@freescale.com>
10 #include <linux/i2c-mux.h>
11 #include <linux/init.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>
20 struct i2c_mux_reg_platform_data data
;
23 static int i2c_mux_reg_set(const struct regmux
*mux
, unsigned int chan_id
)
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
34 switch (mux
->data
.reg_size
) {
36 if (mux
->data
.little_endian
)
37 iowrite32(chan_id
, mux
->data
.reg
);
39 iowrite32be(chan_id
, mux
->data
.reg
);
40 if (!mux
->data
.write_only
)
41 ioread32(mux
->data
.reg
);
44 if (mux
->data
.little_endian
)
45 iowrite16(chan_id
, mux
->data
.reg
);
47 iowrite16be(chan_id
, mux
->data
.reg
);
48 if (!mux
->data
.write_only
)
49 ioread16(mux
->data
.reg
);
52 iowrite8(chan_id
, mux
->data
.reg
);
53 if (!mux
->data
.write_only
)
54 ioread8(mux
->data
.reg
);
61 static int i2c_mux_reg_select(struct i2c_mux_core
*muxc
, u32 chan
)
63 struct regmux
*mux
= i2c_mux_priv(muxc
);
65 return i2c_mux_reg_set(mux
, chan
);
68 static int i2c_mux_reg_deselect(struct i2c_mux_core
*muxc
, u32 chan
)
70 struct regmux
*mux
= i2c_mux_priv(muxc
);
72 if (mux
->data
.idle_in_use
)
73 return i2c_mux_reg_set(mux
, mux
->data
.idle
);
79 static int i2c_mux_reg_probe_dt(struct regmux
*mux
,
80 struct platform_device
*pdev
)
82 struct device_node
*np
= pdev
->dev
.of_node
;
83 struct device_node
*adapter_np
, *child
;
84 struct i2c_adapter
*adapter
;
92 adapter_np
= of_parse_phandle(np
, "i2c-parent", 0);
94 dev_err(&pdev
->dev
, "Cannot parse i2c-parent\n");
97 adapter
= of_find_i2c_adapter_by_node(adapter_np
);
98 of_node_put(adapter_np
);
100 return -EPROBE_DEFER
;
102 mux
->data
.parent
= i2c_adapter_id(adapter
);
103 put_device(&adapter
->dev
);
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;
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;
118 #error Endianness not defined?
121 mux
->data
.write_only
= of_property_read_bool(np
, "write-only");
123 values
= devm_kcalloc(&pdev
->dev
,
124 mux
->data
.n_values
, sizeof(*mux
->data
.values
),
129 for_each_child_of_node(np
, child
) {
130 of_property_read_u32(child
, "reg", values
+ i
);
133 mux
->data
.values
= values
;
135 if (!of_property_read_u32(np
, "idle-state", &mux
->data
.idle
))
136 mux
->data
.idle_in_use
= true;
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
);
149 static int i2c_mux_reg_probe_dt(struct regmux
*mux
,
150 struct platform_device
*pdev
)
156 static int i2c_mux_reg_probe(struct platform_device
*pdev
)
158 struct i2c_mux_core
*muxc
;
160 struct i2c_adapter
*parent
;
161 struct resource
*res
;
165 mux
= devm_kzalloc(&pdev
->dev
, sizeof(*mux
), GFP_KERNEL
);
169 if (dev_get_platdata(&pdev
->dev
)) {
170 memcpy(&mux
->data
, dev_get_platdata(&pdev
->dev
),
173 ret
= i2c_mux_reg_probe_dt(mux
, pdev
);
174 if (ret
== -EPROBE_DEFER
)
178 dev_err(&pdev
->dev
, "Error parsing device tree");
183 parent
= i2c_get_adapter(mux
->data
.parent
);
185 return -EPROBE_DEFER
;
187 if (!mux
->data
.reg
) {
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
);
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");
206 muxc
= i2c_mux_alloc(parent
, &pdev
->dev
, mux
->data
.n_values
, 0, 0,
207 i2c_mux_reg_select
, NULL
);
214 platform_set_drvdata(pdev
, muxc
);
216 if (mux
->data
.idle_in_use
)
217 muxc
->deselect
= i2c_mux_reg_deselect
;
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;
223 ret
= i2c_mux_add_adapter(muxc
, nr
, mux
->data
.values
[i
], class);
225 goto err_del_mux_adapters
;
228 dev_dbg(&pdev
->dev
, "%d port mux on %s adapter\n",
229 mux
->data
.n_values
, muxc
->parent
->name
);
233 err_del_mux_adapters
:
234 i2c_mux_del_adapters(muxc
);
236 i2c_put_adapter(parent
);
241 static int i2c_mux_reg_remove(struct platform_device
*pdev
)
243 struct i2c_mux_core
*muxc
= platform_get_drvdata(pdev
);
245 i2c_mux_del_adapters(muxc
);
246 i2c_put_adapter(muxc
->parent
);
251 static const struct of_device_id i2c_mux_reg_of_match
[] = {
252 { .compatible
= "i2c-mux-reg", },
255 MODULE_DEVICE_TABLE(of
, i2c_mux_reg_of_match
);
257 static struct platform_driver i2c_mux_reg_driver
= {
258 .probe
= i2c_mux_reg_probe
,
259 .remove
= i2c_mux_reg_remove
,
261 .name
= "i2c-mux-reg",
262 .of_match_table
= of_match_ptr(i2c_mux_reg_of_match
),
266 module_platform_driver(i2c_mux_reg_driver
);
268 MODULE_DESCRIPTION("Register-based I2C multiplexer driver");
269 MODULE_AUTHOR("York Sun <yorksun@freescale.com>");
270 MODULE_LICENSE("GPL");
271 MODULE_ALIAS("platform:i2c-mux-reg");