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 
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 
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 
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 	if (adap->algo->master_xfer_atomic)
103 		priv->algo.master_xfer_atomic = i2c_demux_master_xfer;
104 	priv->algo.functionality = i2c_demux_functionality;
105 
106 	snprintf(priv->cur_adap.name, sizeof(priv->cur_adap.name),
107 		 "i2c-demux (master i2c-%d)", i2c_adapter_id(adap));
108 	priv->cur_adap.owner = THIS_MODULE;
109 	priv->cur_adap.algo = &priv->algo;
110 	priv->cur_adap.algo_data = priv;
111 	priv->cur_adap.dev.parent = &adap->dev;
112 	priv->cur_adap.class = adap->class;
113 	priv->cur_adap.retries = adap->retries;
114 	priv->cur_adap.timeout = adap->timeout;
115 	priv->cur_adap.quirks = adap->quirks;
116 	priv->cur_adap.dev.of_node = priv->dev->of_node;
117 	ret = i2c_add_adapter(&priv->cur_adap);
118 	if (ret < 0)
119 		goto err_with_put;
120 
121 	return 0;
122 
123  err_with_put:
124 	i2c_put_adapter(adap);
125  err_with_revert:
126 	of_changeset_revert(&priv->chan[new_chan].chgset);
127  err:
128 	dev_err(priv->dev, "failed to setup demux-adapter %d (%d)\n", new_chan, ret);
129 	priv->cur_chan = -EINVAL;
130 	return ret;
131 }
132 
133 static int i2c_demux_deactivate_master(struct i2c_demux_pinctrl_priv *priv)
134 {
135 	int ret, cur = priv->cur_chan;
136 
137 	if (cur < 0)
138 		return 0;
139 
140 	i2c_del_adapter(&priv->cur_adap);
141 	i2c_put_adapter(priv->chan[cur].parent_adap);
142 
143 	ret = of_changeset_revert(&priv->chan[cur].chgset);
144 
145 	priv->chan[cur].parent_adap = NULL;
146 	priv->cur_chan = -EINVAL;
147 
148 	return ret;
149 }
150 
151 static int i2c_demux_change_master(struct i2c_demux_pinctrl_priv *priv, u32 new_chan)
152 {
153 	int ret;
154 
155 	if (new_chan == priv->cur_chan)
156 		return 0;
157 
158 	ret = i2c_demux_deactivate_master(priv);
159 	if (ret)
160 		return ret;
161 
162 	return i2c_demux_activate_master(priv, new_chan);
163 }
164 
165 static ssize_t available_masters_show(struct device *dev,
166 				      struct device_attribute *attr,
167 				      char *buf)
168 {
169 	struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
170 	int count = 0, i;
171 
172 	for (i = 0; i < priv->num_chan && count < PAGE_SIZE; i++)
173 		count += scnprintf(buf + count, PAGE_SIZE - count, "%d:%pOF%c",
174 				   i, priv->chan[i].parent_np,
175 				   i == priv->num_chan - 1 ? '\n' : ' ');
176 
177 	return count;
178 }
179 static DEVICE_ATTR_RO(available_masters);
180 
181 static ssize_t current_master_show(struct device *dev,
182 				   struct device_attribute *attr,
183 				   char *buf)
184 {
185 	struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
186 
187 	return sprintf(buf, "%d\n", priv->cur_chan);
188 }
189 
190 static ssize_t current_master_store(struct device *dev,
191 				    struct device_attribute *attr,
192 				    const char *buf, size_t count)
193 {
194 	struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev);
195 	unsigned int val;
196 	int ret;
197 
198 	ret = kstrtouint(buf, 0, &val);
199 	if (ret < 0)
200 		return ret;
201 
202 	if (val >= priv->num_chan)
203 		return -EINVAL;
204 
205 	ret = i2c_demux_change_master(priv, val);
206 
207 	return ret < 0 ? ret : count;
208 }
209 static DEVICE_ATTR_RW(current_master);
210 
211 static int i2c_demux_pinctrl_probe(struct platform_device *pdev)
212 {
213 	struct device_node *np = pdev->dev.of_node;
214 	struct i2c_demux_pinctrl_priv *priv;
215 	struct property *props;
216 	int num_chan, i, j, err;
217 
218 	num_chan = of_count_phandle_with_args(np, "i2c-parent", NULL);
219 	if (num_chan < 2) {
220 		dev_err(&pdev->dev, "Need at least two I2C masters to switch\n");
221 		return -EINVAL;
222 	}
223 
224 	priv = devm_kzalloc(&pdev->dev, struct_size(priv, chan, num_chan),
225 			    GFP_KERNEL);
226 
227 	props = devm_kcalloc(&pdev->dev, num_chan, sizeof(*props), GFP_KERNEL);
228 
229 	if (!priv || !props)
230 		return -ENOMEM;
231 
232 	err = of_property_read_string(np, "i2c-bus-name", &priv->bus_name);
233 	if (err)
234 		return err;
235 
236 	for (i = 0; i < num_chan; i++) {
237 		struct device_node *adap_np;
238 
239 		adap_np = of_parse_phandle(np, "i2c-parent", i);
240 		if (!adap_np) {
241 			dev_err(&pdev->dev, "can't get phandle for parent %d\n", i);
242 			err = -ENOENT;
243 			goto err_rollback;
244 		}
245 		priv->chan[i].parent_np = adap_np;
246 
247 		props[i].name = devm_kstrdup(&pdev->dev, "status", GFP_KERNEL);
248 		props[i].value = devm_kstrdup(&pdev->dev, "ok", GFP_KERNEL);
249 		props[i].length = 3;
250 
251 		of_changeset_init(&priv->chan[i].chgset);
252 		of_changeset_update_property(&priv->chan[i].chgset, adap_np, &props[i]);
253 	}
254 
255 	priv->num_chan = num_chan;
256 	priv->dev = &pdev->dev;
257 
258 	platform_set_drvdata(pdev, priv);
259 
260 	pm_runtime_no_callbacks(&pdev->dev);
261 
262 	/* switch to first parent as active master */
263 	i2c_demux_activate_master(priv, 0);
264 
265 	err = device_create_file(&pdev->dev, &dev_attr_available_masters);
266 	if (err)
267 		goto err_rollback;
268 
269 	err = device_create_file(&pdev->dev, &dev_attr_current_master);
270 	if (err)
271 		goto err_rollback_available;
272 
273 	return 0;
274 
275 err_rollback_available:
276 	device_remove_file(&pdev->dev, &dev_attr_available_masters);
277 err_rollback:
278 	for (j = 0; j < i; j++) {
279 		of_node_put(priv->chan[j].parent_np);
280 		of_changeset_destroy(&priv->chan[j].chgset);
281 	}
282 
283 	return err;
284 }
285 
286 static int i2c_demux_pinctrl_remove(struct platform_device *pdev)
287 {
288 	struct i2c_demux_pinctrl_priv *priv = platform_get_drvdata(pdev);
289 	int i;
290 
291 	device_remove_file(&pdev->dev, &dev_attr_current_master);
292 	device_remove_file(&pdev->dev, &dev_attr_available_masters);
293 
294 	i2c_demux_deactivate_master(priv);
295 
296 	for (i = 0; i < priv->num_chan; i++) {
297 		of_node_put(priv->chan[i].parent_np);
298 		of_changeset_destroy(&priv->chan[i].chgset);
299 	}
300 
301 	return 0;
302 }
303 
304 static const struct of_device_id i2c_demux_pinctrl_of_match[] = {
305 	{ .compatible = "i2c-demux-pinctrl", },
306 	{},
307 };
308 MODULE_DEVICE_TABLE(of, i2c_demux_pinctrl_of_match);
309 
310 static struct platform_driver i2c_demux_pinctrl_driver = {
311 	.driver	= {
312 		.name = "i2c-demux-pinctrl",
313 		.of_match_table = i2c_demux_pinctrl_of_match,
314 	},
315 	.probe	= i2c_demux_pinctrl_probe,
316 	.remove	= i2c_demux_pinctrl_remove,
317 };
318 module_platform_driver(i2c_demux_pinctrl_driver);
319 
320 MODULE_DESCRIPTION("pinctrl-based I2C demux driver");
321 MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>");
322 MODULE_LICENSE("GPL v2");
323 MODULE_ALIAS("platform:i2c-demux-pinctrl");
324