1 /*
2  * Pinctrl based I2C DeMultiplexer
3  *
4  * Copyright (C) 2015-16 by Wolfram Sang, Sang Engineering <wsa@sang-engineering.com>
5  * Copyright (C) 2015-16 by Renesas Electronics Corporation
6  *
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.
10  *
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/)
13  */
14 
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/of.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/sysfs.h>
23 
24 struct i2c_demux_pinctrl_chan {
25 	struct device_node *parent_np;
26 	struct i2c_adapter *parent_adap;
27 	struct of_changeset chgset;
28 };
29 
30 struct i2c_demux_pinctrl_priv {
31 	int cur_chan;
32 	int num_chan;
33 	struct device *dev;
34 	const char *bus_name;
35 	struct i2c_adapter cur_adap;
36 	struct i2c_algorithm algo;
37 	struct i2c_demux_pinctrl_chan chan[];
38 };
39 
40 static int i2c_demux_master_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
41 {
42 	struct i2c_demux_pinctrl_priv *priv = adap->algo_data;
43 	struct i2c_adapter *parent = priv->chan[priv->cur_chan].parent_adap;
44 
45 	return __i2c_transfer(parent, msgs, num);
46 }
47 
48 static u32 i2c_demux_functionality(struct i2c_adapter *adap)
49 {
50 	struct i2c_demux_pinctrl_priv *priv = adap->algo_data;
51 	struct i2c_adapter *parent = priv->chan[priv->cur_chan].parent_adap;
52 
53 	return parent->algo->functionality(parent);
54 }
55 
56 static int i2c_demux_activate_master(struct i2c_demux_pinctrl_priv *priv, u32 new_chan)
57 {
58 	struct i2c_adapter *adap;
59 	struct pinctrl *p;
60 	int ret;
61 
62 	ret = of_changeset_apply(&priv->chan[new_chan].chgset);
63 	if (ret)
64 		goto err;
65 
66 	adap = of_find_i2c_adapter_by_node(priv->chan[new_chan].parent_np);
67 	if (!adap) {
68 		ret = -ENODEV;
69 		goto err_with_revert;
70 	}
71 
72 	/*
73 	 * Check if there are pinctrl states at all. Note: we cant' use
74 	 * devm_pinctrl_get_select() because we need to distinguish between
75 	 * the -ENODEV from devm_pinctrl_get() and pinctrl_lookup_state().
76 	 */
77 	p = devm_pinctrl_get(adap->dev.parent);
78 	if (IS_ERR(p)) {
79 		ret = PTR_ERR(p);
80 		/* continue if just no pinctrl states (e.g. i2c-gpio), otherwise exit */
81 		if (ret != -ENODEV)
82 			goto err_with_put;
83 	} else {
84 		/* there are states. check and use them */
85 		struct pinctrl_state *s = pinctrl_lookup_state(p, priv->bus_name);
86 
87 		if (IS_ERR(s)) {
88 			ret = PTR_ERR(s);
89 			goto err_with_put;
90 		}
91 		ret = pinctrl_select_state(p, s);
92 		if (ret < 0)
93 			goto err_with_put;
94 	}
95 
96 	priv->chan[new_chan].parent_adap = adap;
97 	priv->cur_chan = new_chan;
98 
99 	/* Now fill out current adapter structure. cur_chan must be up to date */
100 	priv->algo.master_xfer = i2c_demux_master_xfer;
101 	priv->algo.functionality = i2c_demux_functionality;
102 
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 = priv->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);
115 	if (ret < 0)
116 		goto err_with_put;
117 
118 	return 0;
119 
120  err_with_put:
121 	i2c_put_adapter(adap);
122  err_with_revert:
123 	of_changeset_revert(&priv->chan[new_chan].chgset);
124  err:
125 	dev_err(priv->dev, "failed to setup demux-adapter %d (%d)\n", new_chan, ret);
126 	priv->cur_chan = -EINVAL;
127 	return ret;
128 }
129 
130 static int i2c_demux_deactivate_master(struct i2c_demux_pinctrl_priv *priv)
131 {
132 	int ret, cur = priv->cur_chan;
133 
134 	if (cur < 0)
135 		return 0;
136 
137 	i2c_del_adapter(&priv->cur_adap);
138 	i2c_put_adapter(priv->chan[cur].parent_adap);
139 
140 	ret = of_changeset_revert(&priv->chan[cur].chgset);
141 
142 	priv->chan[cur].parent_adap = NULL;
143 	priv->cur_chan = -EINVAL;
144 
145 	return ret;
146 }
147 
148 static int i2c_demux_change_master(struct i2c_demux_pinctrl_priv *priv, u32 new_chan)
149 {
150 	int ret;
151 
152 	if (new_chan == priv->cur_chan)
153 		return 0;
154 
155 	ret = i2c_demux_deactivate_master(priv);
156 	if (ret)
157 		return ret;
158 
159 	return i2c_demux_activate_master(priv, new_chan);
160 }
161 
162 static ssize_t available_masters_show(struct device *dev,
163 				      struct device_attribute *attr,
164 				      char *buf)
165 {
166 	struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
167 	int count = 0, i;
168 
169 	for (i = 0; i < priv->num_chan && count < PAGE_SIZE; i++)
170 		count += scnprintf(buf + count, PAGE_SIZE - count, "%d:%s%c",
171 				   i, priv->chan[i].parent_np->full_name,
172 				   i == priv->num_chan - 1 ? '\n' : ' ');
173 
174 	return count;
175 }
176 static DEVICE_ATTR_RO(available_masters);
177 
178 static ssize_t current_master_show(struct device *dev,
179 				   struct device_attribute *attr,
180 				   char *buf)
181 {
182 	struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
183 
184 	return sprintf(buf, "%d\n", priv->cur_chan);
185 }
186 
187 static ssize_t current_master_store(struct device *dev,
188 				    struct device_attribute *attr,
189 				    const char *buf, size_t count)
190 {
191 	struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
192 	unsigned int val;
193 	int ret;
194 
195 	ret = kstrtouint(buf, 0, &val);
196 	if (ret < 0)
197 		return ret;
198 
199 	if (val >= priv->num_chan)
200 		return -EINVAL;
201 
202 	ret = i2c_demux_change_master(priv, val);
203 
204 	return ret < 0 ? ret : count;
205 }
206 static DEVICE_ATTR_RW(current_master);
207 
208 static int i2c_demux_pinctrl_probe(struct platform_device *pdev)
209 {
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;
214 
215 	num_chan = of_count_phandle_with_args(np, "i2c-parent", NULL);
216 	if (num_chan < 2) {
217 		dev_err(&pdev->dev, "Need at least two I2C masters to switch\n");
218 		return -EINVAL;
219 	}
220 
221 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv)
222 			   + num_chan * sizeof(struct i2c_demux_pinctrl_chan), GFP_KERNEL);
223 
224 	props = devm_kcalloc(&pdev->dev, num_chan, sizeof(*props), GFP_KERNEL);
225 
226 	if (!priv || !props)
227 		return -ENOMEM;
228 
229 	err = of_property_read_string(np, "i2c-bus-name", &priv->bus_name);
230 	if (err)
231 		return err;
232 
233 	for (i = 0; i < num_chan; i++) {
234 		struct device_node *adap_np;
235 
236 		adap_np = of_parse_phandle(np, "i2c-parent", i);
237 		if (!adap_np) {
238 			dev_err(&pdev->dev, "can't get phandle for parent %d\n", i);
239 			err = -ENOENT;
240 			goto err_rollback;
241 		}
242 		priv->chan[i].parent_np = adap_np;
243 
244 		props[i].name = devm_kstrdup(&pdev->dev, "status", GFP_KERNEL);
245 		props[i].value = devm_kstrdup(&pdev->dev, "ok", GFP_KERNEL);
246 		props[i].length = 3;
247 
248 		of_changeset_init(&priv->chan[i].chgset);
249 		of_changeset_update_property(&priv->chan[i].chgset, adap_np, &props[i]);
250 	}
251 
252 	priv->num_chan = num_chan;
253 	priv->dev = &pdev->dev;
254 
255 	platform_set_drvdata(pdev, priv);
256 
257 	/* switch to first parent as active master */
258 	i2c_demux_activate_master(priv, 0);
259 
260 	err = device_create_file(&pdev->dev, &dev_attr_available_masters);
261 	if (err)
262 		goto err_rollback;
263 
264 	err = device_create_file(&pdev->dev, &dev_attr_current_master);
265 	if (err)
266 		goto err_rollback_available;
267 
268 	return 0;
269 
270 err_rollback_available:
271 	device_remove_file(&pdev->dev, &dev_attr_available_masters);
272 err_rollback:
273 	for (j = 0; j < i; j++) {
274 		of_node_put(priv->chan[j].parent_np);
275 		of_changeset_destroy(&priv->chan[j].chgset);
276 	}
277 
278 	return err;
279 }
280 
281 static int i2c_demux_pinctrl_remove(struct platform_device *pdev)
282 {
283 	struct i2c_demux_pinctrl_priv *priv = platform_get_drvdata(pdev);
284 	int i;
285 
286 	device_remove_file(&pdev->dev, &dev_attr_current_master);
287 	device_remove_file(&pdev->dev, &dev_attr_available_masters);
288 
289 	i2c_demux_deactivate_master(priv);
290 
291 	for (i = 0; i < priv->num_chan; i++) {
292 		of_node_put(priv->chan[i].parent_np);
293 		of_changeset_destroy(&priv->chan[i].chgset);
294 	}
295 
296 	return 0;
297 }
298 
299 static const struct of_device_id i2c_demux_pinctrl_of_match[] = {
300 	{ .compatible = "i2c-demux-pinctrl", },
301 	{},
302 };
303 MODULE_DEVICE_TABLE(of, i2c_demux_pinctrl_of_match);
304 
305 static struct platform_driver i2c_demux_pinctrl_driver = {
306 	.driver	= {
307 		.name = "i2c-demux-pinctrl",
308 		.of_match_table = i2c_demux_pinctrl_of_match,
309 	},
310 	.probe	= i2c_demux_pinctrl_probe,
311 	.remove	= i2c_demux_pinctrl_remove,
312 };
313 module_platform_driver(i2c_demux_pinctrl_driver);
314 
315 MODULE_DESCRIPTION("pinctrl-based I2C demux driver");
316 MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
317 MODULE_LICENSE("GPL v2");
318 MODULE_ALIAS("platform:i2c-demux-pinctrl");
319