1 // SPDX-License-Identifier: GPL-2.0-only
3 * General Purpose I2C multiplexer
5 * Copyright (C) 2017 Axentia Technologies AB
7 * Author: Peter Rosin <peda@axentia.se>
10 #include <linux/i2c.h>
11 #include <linux/i2c-mux.h>
12 #include <linux/module.h>
13 #include <linux/mux/consumer.h>
14 #include <linux/of_device.h>
15 #include <linux/platform_device.h>
18 struct mux_control
*control
;
23 static int i2c_mux_select(struct i2c_mux_core
*muxc
, u32 chan
)
25 struct mux
*mux
= i2c_mux_priv(muxc
);
28 ret
= mux_control_select(mux
->control
, chan
);
29 mux
->do_not_deselect
= ret
< 0;
34 static int i2c_mux_deselect(struct i2c_mux_core
*muxc
, u32 chan
)
36 struct mux
*mux
= i2c_mux_priv(muxc
);
38 if (mux
->do_not_deselect
)
41 return mux_control_deselect(mux
->control
);
44 static struct i2c_adapter
*mux_parent_adapter(struct device
*dev
)
46 struct device_node
*np
= dev
->of_node
;
47 struct device_node
*parent_np
;
48 struct i2c_adapter
*parent
;
50 parent_np
= of_parse_phandle(np
, "i2c-parent", 0);
52 dev_err(dev
, "Cannot parse i2c-parent\n");
53 return ERR_PTR(-ENODEV
);
55 parent
= of_find_i2c_adapter_by_node(parent_np
);
56 of_node_put(parent_np
);
58 return ERR_PTR(-EPROBE_DEFER
);
63 static const struct of_device_id i2c_mux_of_match
[] = {
64 { .compatible
= "i2c-mux", },
67 MODULE_DEVICE_TABLE(of
, i2c_mux_of_match
);
69 static int i2c_mux_probe(struct platform_device
*pdev
)
71 struct device
*dev
= &pdev
->dev
;
72 struct device_node
*np
= dev
->of_node
;
73 struct device_node
*child
;
74 struct i2c_mux_core
*muxc
;
76 struct i2c_adapter
*parent
;
83 mux
= devm_kzalloc(dev
, sizeof(*mux
), GFP_KERNEL
);
87 mux
->control
= devm_mux_control_get(dev
, NULL
);
88 if (IS_ERR(mux
->control
)) {
89 if (PTR_ERR(mux
->control
) != -EPROBE_DEFER
)
90 dev_err(dev
, "failed to get control-mux\n");
91 return PTR_ERR(mux
->control
);
94 parent
= mux_parent_adapter(dev
);
96 if (PTR_ERR(parent
) != -EPROBE_DEFER
)
97 dev_err(dev
, "failed to get i2c-parent adapter\n");
98 return PTR_ERR(parent
);
101 children
= of_get_child_count(np
);
103 muxc
= i2c_mux_alloc(parent
, dev
, children
, 0, 0,
104 i2c_mux_select
, i2c_mux_deselect
);
111 platform_set_drvdata(pdev
, muxc
);
113 muxc
->mux_locked
= of_property_read_bool(np
, "mux-locked");
115 for_each_child_of_node(np
, child
) {
118 ret
= of_property_read_u32(child
, "reg", &chan
);
120 dev_err(dev
, "no reg property for node '%pOFn'\n",
125 if (chan
>= mux_control_states(mux
->control
)) {
126 dev_err(dev
, "invalid reg %u\n", chan
);
131 ret
= i2c_mux_add_adapter(muxc
, 0, chan
, 0);
136 dev_info(dev
, "%d-port mux on %s adapter\n", children
, parent
->name
);
141 i2c_mux_del_adapters(muxc
);
143 i2c_put_adapter(parent
);
148 static int i2c_mux_remove(struct platform_device
*pdev
)
150 struct i2c_mux_core
*muxc
= platform_get_drvdata(pdev
);
152 i2c_mux_del_adapters(muxc
);
153 i2c_put_adapter(muxc
->parent
);
158 static struct platform_driver i2c_mux_driver
= {
159 .probe
= i2c_mux_probe
,
160 .remove
= i2c_mux_remove
,
162 .name
= "i2c-mux-gpmux",
163 .of_match_table
= i2c_mux_of_match
,
166 module_platform_driver(i2c_mux_driver
);
168 MODULE_DESCRIPTION("General Purpose I2C multiplexer driver");
169 MODULE_AUTHOR("Peter Rosin <peda@axentia.se>");
170 MODULE_LICENSE("GPL v2");