2 * Pinctrl based I2C DeMultiplexer
4 * Copyright (C) 2015-16 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
5 * Copyright (C) 2015-16 by Renesas Electronics Corporation
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; version 2 of the License.
11 * See the bindings doc for DTS setup and the sysfs doc for usage information.
12 * (look for filenames containing 'i2c-demux-pinctrl' in Documentation/)
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/platform_device.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/slab.h>
23 #include <linux/sysfs.h>
25 struct i2c_demux_pinctrl_chan {
26 struct device_node *parent_np;
27 struct i2c_adapter *parent_adap;
28 struct of_changeset chgset;
31 struct i2c_demux_pinctrl_priv {
36 struct i2c_adapter cur_adap;
37 struct i2c_algorithm algo;
38 struct i2c_demux_pinctrl_chan chan[];
41 static int i2c_demux_master_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
43 struct i2c_demux_pinctrl_priv *priv = adap->algo_data;
44 struct i2c_adapter *parent = priv->chan[priv->cur_chan].parent_adap;
46 return __i2c_transfer(parent, msgs, num);
49 static u32 i2c_demux_functionality(struct i2c_adapter *adap)
51 struct i2c_demux_pinctrl_priv *priv = adap->algo_data;
52 struct i2c_adapter *parent = priv->chan[priv->cur_chan].parent_adap;
54 return parent->algo->functionality(parent);
57 static int i2c_demux_activate_master(struct i2c_demux_pinctrl_priv *priv, u32 new_chan)
59 struct i2c_adapter *adap;
63 ret = of_changeset_apply(&priv->chan[new_chan].chgset);
67 adap = of_find_i2c_adapter_by_node(priv->chan[new_chan].parent_np);
74 * Check if there are pinctrl states at all. Note: we cant' use
75 * devm_pinctrl_get_select() because we need to distinguish between
76 * the -ENODEV from devm_pinctrl_get() and pinctrl_lookup_state().
78 p = devm_pinctrl_get(adap->dev.parent);
81 /* continue if just no pinctrl states (e.g. i2c-gpio), otherwise exit */
85 /* there are states. check and use them */
86 struct pinctrl_state *s = pinctrl_lookup_state(p, priv->bus_name);
92 ret = pinctrl_select_state(p, s);
97 priv->chan[new_chan].parent_adap = adap;
98 priv->cur_chan = new_chan;
100 /* Now fill out current adapter structure. cur_chan must be up to date */
101 priv->algo.master_xfer = i2c_demux_master_xfer;
102 priv->algo.functionality = i2c_demux_functionality;
104 snprintf(priv->cur_adap.name, sizeof(priv->cur_adap.name),
105 "i2c-demux (master i2c-%d)", i2c_adapter_id(adap));
106 priv->cur_adap.owner = THIS_MODULE;
107 priv->cur_adap.algo = &priv->algo;
108 priv->cur_adap.algo_data = priv;
109 priv->cur_adap.dev.parent = &adap->dev;
110 priv->cur_adap.class = adap->class;
111 priv->cur_adap.retries = adap->retries;
112 priv->cur_adap.timeout = adap->timeout;
113 priv->cur_adap.quirks = adap->quirks;
114 priv->cur_adap.dev.of_node = priv->dev->of_node;
115 ret = i2c_add_adapter(&priv->cur_adap);
122 i2c_put_adapter(adap);
124 of_changeset_revert(&priv->chan[new_chan].chgset);
126 dev_err(priv->dev, "failed to setup demux-adapter %d (%d)\n", new_chan, ret);
127 priv->cur_chan = -EINVAL;
131 static int i2c_demux_deactivate_master(struct i2c_demux_pinctrl_priv *priv)
133 int ret, cur = priv->cur_chan;
138 i2c_del_adapter(&priv->cur_adap);
139 i2c_put_adapter(priv->chan[cur].parent_adap);
141 ret = of_changeset_revert(&priv->chan[cur].chgset);
143 priv->chan[cur].parent_adap = NULL;
144 priv->cur_chan = -EINVAL;
149 static int i2c_demux_change_master(struct i2c_demux_pinctrl_priv *priv, u32 new_chan)
153 if (new_chan == priv->cur_chan)
156 ret = i2c_demux_deactivate_master(priv);
160 return i2c_demux_activate_master(priv, new_chan);
163 static ssize_t available_masters_show(struct device *dev,
164 struct device_attribute *attr,
167 struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
170 for (i = 0; i < priv->num_chan && count < PAGE_SIZE; i++)
171 count += scnprintf(buf + count, PAGE_SIZE - count, "%d:%pOF%c",
172 i, priv->chan[i].parent_np,
173 i == priv->num_chan - 1 ? '\n' : ' ');
177 static DEVICE_ATTR_RO(available_masters);
179 static ssize_t current_master_show(struct device *dev,
180 struct device_attribute *attr,
183 struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
185 return sprintf(buf, "%d\n", priv->cur_chan);
188 static ssize_t current_master_store(struct device *dev,
189 struct device_attribute *attr,
190 const char *buf, size_t count)
192 struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
196 ret = kstrtouint(buf, 0, &val);
200 if (val >= priv->num_chan)
203 ret = i2c_demux_change_master(priv, val);
205 return ret < 0 ? ret : count;
207 static DEVICE_ATTR_RW(current_master);
209 static int i2c_demux_pinctrl_probe(struct platform_device *pdev)
211 struct device_node *np = pdev->dev.of_node;
212 struct i2c_demux_pinctrl_priv *priv;
213 struct property *props;
214 int num_chan, i, j, err;
216 num_chan = of_count_phandle_with_args(np, "i2c-parent", NULL);
218 dev_err(&pdev->dev, "Need at least two I2C masters to switch\n");
222 priv = devm_kzalloc(&pdev->dev, sizeof(*priv)
223 + num_chan * sizeof(struct i2c_demux_pinctrl_chan), GFP_KERNEL);
225 props = devm_kcalloc(&pdev->dev, num_chan, sizeof(*props), GFP_KERNEL);
230 err = of_property_read_string(np, "i2c-bus-name", &priv->bus_name);
234 for (i = 0; i < num_chan; i++) {
235 struct device_node *adap_np;
237 adap_np = of_parse_phandle(np, "i2c-parent", i);
239 dev_err(&pdev->dev, "can't get phandle for parent %d\n", i);
243 priv->chan[i].parent_np = adap_np;
245 props[i].name = devm_kstrdup(&pdev->dev, "status", GFP_KERNEL);
246 props[i].value = devm_kstrdup(&pdev->dev, "ok", GFP_KERNEL);
249 of_changeset_init(&priv->chan[i].chgset);
250 of_changeset_update_property(&priv->chan[i].chgset, adap_np, &props[i]);
253 priv->num_chan = num_chan;
254 priv->dev = &pdev->dev;
256 platform_set_drvdata(pdev, priv);
258 pm_runtime_no_callbacks(&pdev->dev);
260 /* switch to first parent as active master */
261 i2c_demux_activate_master(priv, 0);
263 err = device_create_file(&pdev->dev, &dev_attr_available_masters);
267 err = device_create_file(&pdev->dev, &dev_attr_current_master);
269 goto err_rollback_available;
273 err_rollback_available:
274 device_remove_file(&pdev->dev, &dev_attr_available_masters);
276 for (j = 0; j < i; j++) {
277 of_node_put(priv->chan[j].parent_np);
278 of_changeset_destroy(&priv->chan[j].chgset);
284 static int i2c_demux_pinctrl_remove(struct platform_device *pdev)
286 struct i2c_demux_pinctrl_priv *priv = platform_get_drvdata(pdev);
289 device_remove_file(&pdev->dev, &dev_attr_current_master);
290 device_remove_file(&pdev->dev, &dev_attr_available_masters);
292 i2c_demux_deactivate_master(priv);
294 for (i = 0; i < priv->num_chan; i++) {
295 of_node_put(priv->chan[i].parent_np);
296 of_changeset_destroy(&priv->chan[i].chgset);
302 static const struct of_device_id i2c_demux_pinctrl_of_match[] = {
303 { .compatible = "i2c-demux-pinctrl", },
306 MODULE_DEVICE_TABLE(of, i2c_demux_pinctrl_of_match);
308 static struct platform_driver i2c_demux_pinctrl_driver = {
310 .name = "i2c-demux-pinctrl",
311 .of_match_table = i2c_demux_pinctrl_of_match,
313 .probe = i2c_demux_pinctrl_probe,
314 .remove = i2c_demux_pinctrl_remove,
316 module_platform_driver(i2c_demux_pinctrl_driver);
318 MODULE_DESCRIPTION("pinctrl-based I2C demux driver");
319 MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
320 MODULE_LICENSE("GPL v2");
321 MODULE_ALIAS("platform:i2c-demux-pinctrl");