1 // SPDX-License-Identifier: GPL-2.0-only
3 * Pinctrl based I2C DeMultiplexer
5 * Copyright (C) 2015-16 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
6 * Copyright (C) 2015-16 by Renesas Electronics Corporation
8 * See the bindings doc for DTS setup and the sysfs doc for usage information.
9 * (look for filenames containing 'i2c-demux-pinctrl' in Documentation/)
12 #include <linux/i2c.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
16 #include <linux/pinctrl/consumer.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/slab.h>
20 #include <linux/sysfs.h>
22 struct i2c_demux_pinctrl_chan {
23 struct device_node *parent_np;
24 struct i2c_adapter *parent_adap;
25 struct of_changeset chgset;
28 struct i2c_demux_pinctrl_priv {
33 struct i2c_adapter cur_adap;
34 struct i2c_algorithm algo;
35 struct i2c_demux_pinctrl_chan chan[];
38 static int i2c_demux_master_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
40 struct i2c_demux_pinctrl_priv *priv = adap->algo_data;
41 struct i2c_adapter *parent = priv->chan[priv->cur_chan].parent_adap;
43 return __i2c_transfer(parent, msgs, num);
46 static u32 i2c_demux_functionality(struct i2c_adapter *adap)
48 struct i2c_demux_pinctrl_priv *priv = adap->algo_data;
49 struct i2c_adapter *parent = priv->chan[priv->cur_chan].parent_adap;
51 return parent->algo->functionality(parent);
54 static int i2c_demux_activate_master(struct i2c_demux_pinctrl_priv *priv, u32 new_chan)
56 struct i2c_adapter *adap;
60 ret = of_changeset_apply(&priv->chan[new_chan].chgset);
64 adap = of_find_i2c_adapter_by_node(priv->chan[new_chan].parent_np);
71 * Check if there are pinctrl states at all. Note: we cant' use
72 * devm_pinctrl_get_select() because we need to distinguish between
73 * the -ENODEV from devm_pinctrl_get() and pinctrl_lookup_state().
75 p = devm_pinctrl_get(adap->dev.parent);
78 /* continue if just no pinctrl states (e.g. i2c-gpio), otherwise exit */
82 /* there are states. check and use them */
83 struct pinctrl_state *s = pinctrl_lookup_state(p, priv->bus_name);
89 ret = pinctrl_select_state(p, s);
94 priv->chan[new_chan].parent_adap = adap;
95 priv->cur_chan = new_chan;
97 /* Now fill out current adapter structure. cur_chan must be up to date */
98 priv->algo.master_xfer = i2c_demux_master_xfer;
99 if (adap->algo->master_xfer_atomic)
100 priv->algo.master_xfer_atomic = i2c_demux_master_xfer;
101 priv->algo.functionality = i2c_demux_functionality;
103 snprintf(priv->cur_adap.name, sizeof(priv->cur_adap.name),
104 "i2c-demux (master i2c-%d)", i2c_adapter_id(adap));
105 priv->cur_adap.owner = THIS_MODULE;
106 priv->cur_adap.algo = &priv->algo;
107 priv->cur_adap.algo_data = priv;
108 priv->cur_adap.dev.parent = &adap->dev;
109 priv->cur_adap.class = adap->class;
110 priv->cur_adap.retries = adap->retries;
111 priv->cur_adap.timeout = adap->timeout;
112 priv->cur_adap.quirks = adap->quirks;
113 priv->cur_adap.dev.of_node = priv->dev->of_node;
114 ret = i2c_add_adapter(&priv->cur_adap);
121 i2c_put_adapter(adap);
123 of_changeset_revert(&priv->chan[new_chan].chgset);
125 dev_err(priv->dev, "failed to setup demux-adapter %d (%d)\n", new_chan, ret);
126 priv->cur_chan = -EINVAL;
130 static int i2c_demux_deactivate_master(struct i2c_demux_pinctrl_priv *priv)
132 int ret, cur = priv->cur_chan;
137 i2c_del_adapter(&priv->cur_adap);
138 i2c_put_adapter(priv->chan[cur].parent_adap);
140 ret = of_changeset_revert(&priv->chan[cur].chgset);
142 priv->chan[cur].parent_adap = NULL;
143 priv->cur_chan = -EINVAL;
148 static int i2c_demux_change_master(struct i2c_demux_pinctrl_priv *priv, u32 new_chan)
152 if (new_chan == priv->cur_chan)
155 ret = i2c_demux_deactivate_master(priv);
159 return i2c_demux_activate_master(priv, new_chan);
162 static ssize_t available_masters_show(struct device *dev,
163 struct device_attribute *attr,
166 struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
169 for (i = 0; i < priv->num_chan && count < PAGE_SIZE; i++)
170 count += scnprintf(buf + count, PAGE_SIZE - count, "%d:%pOF%c",
171 i, priv->chan[i].parent_np,
172 i == priv->num_chan - 1 ? '\n' : ' ');
176 static DEVICE_ATTR_RO(available_masters);
178 static ssize_t current_master_show(struct device *dev,
179 struct device_attribute *attr,
182 struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
184 return sprintf(buf, "%d\n", priv->cur_chan);
187 static ssize_t current_master_store(struct device *dev,
188 struct device_attribute *attr,
189 const char *buf, size_t count)
191 struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
195 ret = kstrtouint(buf, 0, &val);
199 if (val >= priv->num_chan)
202 ret = i2c_demux_change_master(priv, val);
204 return ret < 0 ? ret : count;
206 static DEVICE_ATTR_RW(current_master);
208 static int i2c_demux_pinctrl_probe(struct platform_device *pdev)
210 struct device_node *np = pdev->dev.of_node;
211 struct i2c_demux_pinctrl_priv *priv;
212 struct property *props;
213 int num_chan, i, j, err;
215 num_chan = of_count_phandle_with_args(np, "i2c-parent", NULL);
217 dev_err(&pdev->dev, "Need at least two I2C masters to switch\n");
221 priv = devm_kzalloc(&pdev->dev, struct_size(priv, chan, num_chan),
224 props = devm_kcalloc(&pdev->dev, num_chan, sizeof(*props), GFP_KERNEL);
229 err = of_property_read_string(np, "i2c-bus-name", &priv->bus_name);
233 for (i = 0; i < num_chan; i++) {
234 struct device_node *adap_np;
236 adap_np = of_parse_phandle(np, "i2c-parent", i);
238 dev_err(&pdev->dev, "can't get phandle for parent %d\n", i);
242 priv->chan[i].parent_np = adap_np;
244 props[i].name = devm_kstrdup(&pdev->dev, "status", GFP_KERNEL);
245 props[i].value = devm_kstrdup(&pdev->dev, "ok", GFP_KERNEL);
248 of_changeset_init(&priv->chan[i].chgset);
249 of_changeset_update_property(&priv->chan[i].chgset, adap_np, &props[i]);
252 priv->num_chan = num_chan;
253 priv->dev = &pdev->dev;
255 platform_set_drvdata(pdev, priv);
257 pm_runtime_no_callbacks(&pdev->dev);
259 /* switch to first parent as active master */
260 i2c_demux_activate_master(priv, 0);
262 err = device_create_file(&pdev->dev, &dev_attr_available_masters);
264 goto err_rollback_activation;
266 err = device_create_file(&pdev->dev, &dev_attr_current_master);
268 goto err_rollback_available;
272 err_rollback_available:
273 device_remove_file(&pdev->dev, &dev_attr_available_masters);
274 err_rollback_activation:
275 i2c_demux_deactivate_master(priv);
277 for (j = 0; j < i; j++) {
278 of_node_put(priv->chan[j].parent_np);
279 of_changeset_destroy(&priv->chan[j].chgset);
285 static void i2c_demux_pinctrl_remove(struct platform_device *pdev)
287 struct i2c_demux_pinctrl_priv *priv = platform_get_drvdata(pdev);
290 device_remove_file(&pdev->dev, &dev_attr_current_master);
291 device_remove_file(&pdev->dev, &dev_attr_available_masters);
293 i2c_demux_deactivate_master(priv);
295 for (i = 0; i < priv->num_chan; i++) {
296 of_node_put(priv->chan[i].parent_np);
297 of_changeset_destroy(&priv->chan[i].chgset);
301 static const struct of_device_id i2c_demux_pinctrl_of_match[] = {
302 { .compatible = "i2c-demux-pinctrl", },
305 MODULE_DEVICE_TABLE(of, i2c_demux_pinctrl_of_match);
307 static struct platform_driver i2c_demux_pinctrl_driver = {
309 .name = "i2c-demux-pinctrl",
310 .of_match_table = i2c_demux_pinctrl_of_match,
312 .probe = i2c_demux_pinctrl_probe,
313 .remove_new = i2c_demux_pinctrl_remove,
315 module_platform_driver(i2c_demux_pinctrl_driver);
317 MODULE_DESCRIPTION("pinctrl-based I2C demux driver");
318 MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
319 MODULE_LICENSE("GPL v2");
320 MODULE_ALIAS("platform:i2c-demux-pinctrl");