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