1 // SPDX-License-Identifier: GPL-2.0-only
3 * I2C multiplexer for the 2 I2C Masters of the RTL9300
4 * with up to 8 channels each, but which are not entirely
5 * independent of each other
7 #include <linux/i2c-mux.h>
8 #include <linux/module.h>
9 #include <linux/mux/consumer.h>
10 #include <linux/of_device.h>
11 #include <linux/of_address.h>
12 #include <linux/platform_device.h>
14 #include "../busses/i2c-rtl9300.h"
19 #define REG(mst, x) (mux->base + x + (mst ? mux->i2c->mst2_offset : 0))
20 #define REG_MASK(mst, clear, set, reg) \
21 writel((readl(REG((mst),(reg))) & ~(clear)) | (set), REG((mst),(reg)))
28 static struct channel channels
[NUM_MASTERS
* NUM_BUSSES
];
33 struct i2c_adapter
*parent
;
34 struct rtl9300_i2c
* i2c
;
42 int (*i2c_mux_select
)(struct i2c_mux_core
*muxc
, u32 chan
);
43 int (*i2c_mux_deselect
)(struct i2c_mux_core
*muxc
, u32 chan
);
44 void (*sda_sel
)(struct i2c_mux_core
*muxc
, int pin
);
47 static int rtl9300_i2c_mux_select(struct i2c_mux_core
*muxc
, u32 chan
)
49 struct rtl9300_mux
*mux
= i2c_mux_priv(muxc
);
52 REG_MASK(channels
[chan
].scl_num
, 0,
53 BIT(RTL9300_I2C_CTRL1_GPIO8_SCL_SEL
), RTL9300_I2C_CTRL1
);
56 REG_MASK(channels
[chan
].scl_num
, 0x7 << RTL9300_I2C_CTRL1_SDA_OUT_SEL
,
57 channels
[chan
].sda_num
<< RTL9300_I2C_CTRL1_SDA_OUT_SEL
, RTL9300_I2C_CTRL1
);
59 mux
->i2c
->sda_num
= channels
[chan
].sda_num
;
60 mux
->i2c
->scl_num
= channels
[chan
].scl_num
;
65 static int rtl9310_i2c_mux_select(struct i2c_mux_core
*muxc
, u32 chan
)
67 struct rtl9300_mux
*mux
= i2c_mux_priv(muxc
);
70 REG_MASK(0, 0, BIT(RTL9310_I2C_MST_IF_SEL_GPIO_SCL_SEL
+ channels
[chan
].scl_num
),
71 RTL9310_I2C_MST_IF_SEL
);
74 REG_MASK(channels
[chan
].scl_num
, 0xf << RTL9310_I2C_CTRL_SDA_OUT_SEL
,
75 channels
[chan
].sda_num
<< RTL9310_I2C_CTRL_SDA_OUT_SEL
, RTL9310_I2C_CTRL
);
77 mux
->i2c
->sda_num
= channels
[chan
].sda_num
;
78 mux
->i2c
->scl_num
= channels
[chan
].scl_num
;
83 static int rtl9300_i2c_mux_deselect(struct i2c_mux_core
*muxc
, u32 chan
)
88 static void rtl9300_sda_sel(struct i2c_mux_core
*muxc
, int pin
)
90 struct rtl9300_mux
*mux
= i2c_mux_priv(muxc
);
93 // Set SDA pin to I2C functionality
94 v
= readl(REG(0, RTL9300_I2C_MST_GLB_CTRL
));
96 writel(v
, REG(0, RTL9300_I2C_MST_GLB_CTRL
));
99 static void rtl9310_sda_sel(struct i2c_mux_core
*muxc
, int pin
)
101 struct rtl9300_mux
*mux
= i2c_mux_priv(muxc
);
104 // Set SDA pin to I2C functionality
105 v
= readl(REG(0, RTL9310_I2C_MST_IF_SEL
));
107 writel(v
, REG(0, RTL9310_I2C_MST_IF_SEL
));
110 static struct device_node
*mux_parent_adapter(struct device
*dev
, struct rtl9300_mux
*mux
)
112 struct device_node
*node
= dev
->of_node
;
113 struct device_node
*parent_np
;
114 struct i2c_adapter
*parent
;
116 parent_np
= of_parse_phandle(node
, "i2c-parent", 0);
118 dev_err(dev
, "Cannot parse i2c-parent\n");
119 return ERR_PTR(-ENODEV
);
121 parent
= of_find_i2c_adapter_by_node(parent_np
);
122 of_node_put(parent_np
);
124 return ERR_PTR(-EPROBE_DEFER
);
126 if (!(of_device_is_compatible(parent_np
, "realtek,rtl9300-i2c")
127 || of_device_is_compatible(parent_np
, "realtek,rtl9310-i2c"))){
128 dev_err(dev
, "I2C parent not an RTL9300 I2C controller\n");
129 return ERR_PTR(-ENODEV
);
132 mux
->parent
= parent
;
133 mux
->i2c
= (struct rtl9300_i2c
*)i2c_get_adapdata(parent
);
134 mux
->base
= mux
->i2c
->base
;
139 struct i2c_mux_data rtl9300_i2c_mux_data
= {
144 .i2c_mux_select
= rtl9300_i2c_mux_select
,
145 .i2c_mux_deselect
= rtl9300_i2c_mux_deselect
,
146 .sda_sel
= rtl9300_sda_sel
,
149 struct i2c_mux_data rtl9310_i2c_mux_data
= {
154 .i2c_mux_select
= rtl9310_i2c_mux_select
,
155 .i2c_mux_deselect
= rtl9300_i2c_mux_deselect
,
156 .sda_sel
= rtl9310_sda_sel
,
159 static const struct of_device_id rtl9300_i2c_mux_of_match
[] = {
160 { .compatible
= "realtek,i2c-mux-rtl9300", .data
= (void *) &rtl9300_i2c_mux_data
},
161 { .compatible
= "realtek,i2c-mux-rtl9310", .data
= (void *) &rtl9310_i2c_mux_data
},
165 MODULE_DEVICE_TABLE(of
, rtl9300_i2c_mux_of_match
);
167 static int rtl9300_i2c_mux_probe(struct platform_device
*pdev
)
169 struct device
*dev
= &pdev
->dev
;
170 struct device_node
*node
= dev
->of_node
;
171 struct device_node
*parent_np
;
172 struct device_node
*child
;
173 struct i2c_mux_core
*muxc
;
174 struct rtl9300_mux
*mux
;
175 struct i2c_mux_data
*mux_data
;
179 pr_info("%s probing I2C adapter\n", __func__
);
182 dev_err(dev
, "No DT found\n");
186 mux
= devm_kzalloc(dev
, sizeof(*mux
), GFP_KERNEL
);
192 mux_data
= (struct i2c_mux_data
*) device_get_match_data(dev
);
194 parent_np
= mux_parent_adapter(dev
, mux
);
195 if (IS_ERR(parent_np
))
196 return dev_err_probe(dev
, PTR_ERR(parent_np
), "i2c-parent adapter not found\n");
198 pr_info("%s base memory %08x\n", __func__
, (u32
)mux
->base
);
200 children
= of_get_child_count(node
);
202 muxc
= i2c_mux_alloc(mux
->parent
, dev
, children
, 0, 0,
203 mux_data
->i2c_mux_select
, mux_data
->i2c_mux_deselect
);
210 platform_set_drvdata(pdev
, muxc
);
212 for_each_child_of_node(node
, child
) {
216 ret
= of_property_read_u32(child
, "reg", &chan
);
218 dev_err(dev
, "no reg property for node '%pOFn'\n",
223 if (chan
>= NUM_MASTERS
* NUM_BUSSES
) {
224 dev_err(dev
, "invalid reg %u\n", chan
);
229 if (of_property_read_u32(child
, "scl-pin", &pin
)) {
230 dev_warn(dev
, "SCL pin not found in DT, using default\n");
231 pin
= mux_data
->scl0_pin
;
233 if (!(pin
== mux_data
->scl0_pin
|| pin
== mux_data
->scl1_pin
)) {
234 dev_warn(dev
, "SCL pin %d not supported\n", pin
);
238 channels
[chan
].scl_num
= pin
== mux_data
->scl0_pin
? 0 : 1;
239 pr_info("%s channel %d scl_num %d\n", __func__
, chan
, channels
[chan
].scl_num
);
241 if (of_property_read_u32(child
, "sda-pin", &pin
)) {
242 dev_warn(dev
, "SDA pin not found in DT, using default \n");
243 pin
= mux_data
->sda0_pin
;
245 channels
[chan
].sda_num
= pin
- mux_data
->sda0_pin
;
246 if (channels
[chan
].sda_num
< 0 || channels
[chan
].sda_num
>= mux_data
->sda_pins
) {
247 dev_warn(dev
, "SDA pin %d not supported\n", pin
);
250 pr_info("%s channel %d sda_num %d\n", __func__
, chan
, channels
[chan
].sda_num
);
252 mux_data
->sda_sel(muxc
, channels
[chan
].sda_num
);
254 ret
= i2c_mux_add_adapter(muxc
, 0, chan
, 0);
259 dev_info(dev
, "%d-port mux on %s adapter\n", children
, mux
->parent
->name
);
264 i2c_mux_del_adapters(muxc
);
266 i2c_put_adapter(mux
->parent
);
271 static int rtl9300_i2c_mux_remove(struct platform_device
*pdev
)
273 struct i2c_mux_core
*muxc
= platform_get_drvdata(pdev
);
275 i2c_mux_del_adapters(muxc
);
276 i2c_put_adapter(muxc
->parent
);
281 static struct platform_driver i2c_mux_driver
= {
282 .probe
= rtl9300_i2c_mux_probe
,
283 .remove
= rtl9300_i2c_mux_remove
,
285 .name
= "i2c-mux-rtl9300",
286 .of_match_table
= rtl9300_i2c_mux_of_match
,
289 module_platform_driver(i2c_mux_driver
);
291 MODULE_DESCRIPTION("RTL9300 I2C multiplexer driver");
292 MODULE_AUTHOR("Birger Koblitz");
293 MODULE_LICENSE("GPL v2");