Commit | Line | Data |
---|---|---|
50a5ba87 WS |
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 struct property status_okay = { .name = "status", .length = 3, .value = "ok" }; | |
41 | ||
42 | static int i2c_demux_master_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num) | |
43 | { | |
44 | struct i2c_demux_pinctrl_priv *priv = adap->algo_data; | |
45 | struct i2c_adapter *parent = priv->chan[priv->cur_chan].parent_adap; | |
46 | ||
47 | return __i2c_transfer(parent, msgs, num); | |
48 | } | |
49 | ||
50 | static u32 i2c_demux_functionality(struct i2c_adapter *adap) | |
51 | { | |
52 | struct i2c_demux_pinctrl_priv *priv = adap->algo_data; | |
53 | struct i2c_adapter *parent = priv->chan[priv->cur_chan].parent_adap; | |
54 | ||
55 | return parent->algo->functionality(parent); | |
56 | } | |
57 | ||
58 | static int i2c_demux_activate_master(struct i2c_demux_pinctrl_priv *priv, u32 new_chan) | |
59 | { | |
60 | struct i2c_adapter *adap; | |
61 | struct pinctrl *p; | |
62 | int ret; | |
63 | ||
64 | ret = of_changeset_apply(&priv->chan[new_chan].chgset); | |
65 | if (ret) | |
66 | goto err; | |
67 | ||
68 | adap = of_find_i2c_adapter_by_node(priv->chan[new_chan].parent_np); | |
69 | if (!adap) { | |
70 | ret = -ENODEV; | |
ce8cb803 | 71 | goto err_with_revert; |
50a5ba87 WS |
72 | } |
73 | ||
74 | p = devm_pinctrl_get_select(adap->dev.parent, priv->bus_name); | |
75 | if (IS_ERR(p)) { | |
76 | ret = PTR_ERR(p); | |
77 | goto err_with_put; | |
78 | } | |
79 | ||
80 | priv->chan[new_chan].parent_adap = adap; | |
81 | priv->cur_chan = new_chan; | |
82 | ||
83 | /* Now fill out current adapter structure. cur_chan must be up to date */ | |
84 | priv->algo.master_xfer = i2c_demux_master_xfer; | |
85 | priv->algo.functionality = i2c_demux_functionality; | |
86 | ||
87 | snprintf(priv->cur_adap.name, sizeof(priv->cur_adap.name), | |
88 | "i2c-demux (master i2c-%d)", i2c_adapter_id(adap)); | |
89 | priv->cur_adap.owner = THIS_MODULE; | |
90 | priv->cur_adap.algo = &priv->algo; | |
91 | priv->cur_adap.algo_data = priv; | |
92 | priv->cur_adap.dev.parent = priv->dev; | |
93 | priv->cur_adap.class = adap->class; | |
94 | priv->cur_adap.retries = adap->retries; | |
95 | priv->cur_adap.timeout = adap->timeout; | |
96 | priv->cur_adap.quirks = adap->quirks; | |
97 | priv->cur_adap.dev.of_node = priv->dev->of_node; | |
98 | ret = i2c_add_adapter(&priv->cur_adap); | |
99 | if (ret < 0) | |
100 | goto err_with_put; | |
101 | ||
102 | return 0; | |
103 | ||
104 | err_with_put: | |
105 | i2c_put_adapter(adap); | |
ce8cb803 WS |
106 | err_with_revert: |
107 | of_changeset_revert(&priv->chan[new_chan].chgset); | |
50a5ba87 WS |
108 | err: |
109 | dev_err(priv->dev, "failed to setup demux-adapter %d (%d)\n", new_chan, ret); | |
110 | return ret; | |
111 | } | |
112 | ||
113 | static int i2c_demux_deactivate_master(struct i2c_demux_pinctrl_priv *priv) | |
114 | { | |
115 | int ret, cur = priv->cur_chan; | |
116 | ||
117 | if (cur < 0) | |
118 | return 0; | |
119 | ||
120 | i2c_del_adapter(&priv->cur_adap); | |
121 | i2c_put_adapter(priv->chan[cur].parent_adap); | |
122 | ||
123 | ret = of_changeset_revert(&priv->chan[cur].chgset); | |
124 | ||
125 | priv->chan[cur].parent_adap = NULL; | |
126 | priv->cur_chan = -EINVAL; | |
127 | ||
128 | return ret; | |
129 | } | |
130 | ||
131 | static int i2c_demux_change_master(struct i2c_demux_pinctrl_priv *priv, u32 new_chan) | |
132 | { | |
133 | int ret; | |
134 | ||
135 | if (new_chan == priv->cur_chan) | |
136 | return 0; | |
137 | ||
138 | ret = i2c_demux_deactivate_master(priv); | |
139 | if (ret) | |
140 | return ret; | |
141 | ||
142 | return i2c_demux_activate_master(priv, new_chan); | |
143 | } | |
144 | ||
c0c508a4 BH |
145 | static ssize_t available_masters_show(struct device *dev, |
146 | struct device_attribute *attr, | |
147 | char *buf) | |
50a5ba87 WS |
148 | { |
149 | struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev); | |
150 | int count = 0, i; | |
151 | ||
152 | for (i = 0; i < priv->num_chan && count < PAGE_SIZE; i++) | |
c0c508a4 BH |
153 | count += scnprintf(buf + count, PAGE_SIZE - count, "%d:%s%c", |
154 | i, priv->chan[i].parent_np->full_name, | |
155 | i == priv->num_chan - 1 ? '\n' : ' '); | |
50a5ba87 WS |
156 | |
157 | return count; | |
158 | } | |
c0c508a4 | 159 | static DEVICE_ATTR_RO(available_masters); |
50a5ba87 | 160 | |
c0c508a4 BH |
161 | static ssize_t current_master_show(struct device *dev, |
162 | struct device_attribute *attr, | |
163 | char *buf) | |
164 | { | |
165 | struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev); | |
166 | ||
167 | return sprintf(buf, "%d\n", priv->cur_chan); | |
168 | } | |
169 | ||
170 | static ssize_t current_master_store(struct device *dev, | |
171 | struct device_attribute *attr, | |
172 | const char *buf, size_t count) | |
50a5ba87 WS |
173 | { |
174 | struct i2c_demux_pinctrl_priv *priv = dev_get_drvdata(dev); | |
175 | unsigned int val; | |
176 | int ret; | |
177 | ||
178 | ret = kstrtouint(buf, 0, &val); | |
179 | if (ret < 0) | |
180 | return ret; | |
181 | ||
182 | if (val >= priv->num_chan) | |
183 | return -EINVAL; | |
184 | ||
185 | ret = i2c_demux_change_master(priv, val); | |
186 | ||
187 | return ret < 0 ? ret : count; | |
188 | } | |
c0c508a4 | 189 | static DEVICE_ATTR_RW(current_master); |
50a5ba87 WS |
190 | |
191 | static int i2c_demux_pinctrl_probe(struct platform_device *pdev) | |
192 | { | |
193 | struct device_node *np = pdev->dev.of_node; | |
194 | struct i2c_demux_pinctrl_priv *priv; | |
195 | int num_chan, i, j, err; | |
196 | ||
197 | num_chan = of_count_phandle_with_args(np, "i2c-parent", NULL); | |
198 | if (num_chan < 2) { | |
199 | dev_err(&pdev->dev, "Need at least two I2C masters to switch\n"); | |
200 | return -EINVAL; | |
201 | } | |
202 | ||
203 | priv = devm_kzalloc(&pdev->dev, sizeof(*priv) | |
204 | + num_chan * sizeof(struct i2c_demux_pinctrl_chan), GFP_KERNEL); | |
205 | if (!priv) | |
206 | return -ENOMEM; | |
207 | ||
208 | err = of_property_read_string(np, "i2c-bus-name", &priv->bus_name); | |
209 | if (err) | |
210 | return err; | |
211 | ||
212 | for (i = 0; i < num_chan; i++) { | |
213 | struct device_node *adap_np; | |
214 | ||
215 | adap_np = of_parse_phandle(np, "i2c-parent", i); | |
216 | if (!adap_np) { | |
217 | dev_err(&pdev->dev, "can't get phandle for parent %d\n", i); | |
218 | err = -ENOENT; | |
219 | goto err_rollback; | |
220 | } | |
221 | priv->chan[i].parent_np = adap_np; | |
222 | ||
223 | of_changeset_init(&priv->chan[i].chgset); | |
224 | of_changeset_update_property(&priv->chan[i].chgset, adap_np, &status_okay); | |
225 | } | |
226 | ||
227 | priv->num_chan = num_chan; | |
228 | priv->dev = &pdev->dev; | |
229 | ||
230 | platform_set_drvdata(pdev, priv); | |
231 | ||
232 | /* switch to first parent as active master */ | |
233 | i2c_demux_activate_master(priv, 0); | |
234 | ||
c0c508a4 | 235 | err = device_create_file(&pdev->dev, &dev_attr_available_masters); |
50a5ba87 WS |
236 | if (err) |
237 | goto err_rollback; | |
238 | ||
c0c508a4 BH |
239 | err = device_create_file(&pdev->dev, &dev_attr_current_master); |
240 | if (err) | |
241 | goto err_rollback_available; | |
242 | ||
50a5ba87 WS |
243 | return 0; |
244 | ||
c0c508a4 BH |
245 | err_rollback_available: |
246 | device_remove_file(&pdev->dev, &dev_attr_available_masters); | |
50a5ba87 WS |
247 | err_rollback: |
248 | for (j = 0; j < i; j++) { | |
249 | of_node_put(priv->chan[j].parent_np); | |
250 | of_changeset_destroy(&priv->chan[j].chgset); | |
251 | } | |
252 | ||
253 | return err; | |
254 | } | |
255 | ||
256 | static int i2c_demux_pinctrl_remove(struct platform_device *pdev) | |
257 | { | |
258 | struct i2c_demux_pinctrl_priv *priv = platform_get_drvdata(pdev); | |
259 | int i; | |
260 | ||
c0c508a4 BH |
261 | device_remove_file(&pdev->dev, &dev_attr_current_master); |
262 | device_remove_file(&pdev->dev, &dev_attr_available_masters); | |
50a5ba87 WS |
263 | |
264 | i2c_demux_deactivate_master(priv); | |
265 | ||
266 | for (i = 0; i < priv->num_chan; i++) { | |
267 | of_node_put(priv->chan[i].parent_np); | |
268 | of_changeset_destroy(&priv->chan[i].chgset); | |
269 | } | |
270 | ||
271 | return 0; | |
272 | } | |
273 | ||
274 | static const struct of_device_id i2c_demux_pinctrl_of_match[] = { | |
275 | { .compatible = "i2c-demux-pinctrl", }, | |
276 | {}, | |
277 | }; | |
278 | MODULE_DEVICE_TABLE(of, i2c_demux_pinctrl_of_match); | |
279 | ||
280 | static struct platform_driver i2c_demux_pinctrl_driver = { | |
281 | .driver = { | |
282 | .name = "i2c-demux-pinctrl", | |
283 | .of_match_table = i2c_demux_pinctrl_of_match, | |
284 | }, | |
285 | .probe = i2c_demux_pinctrl_probe, | |
286 | .remove = i2c_demux_pinctrl_remove, | |
287 | }; | |
288 | module_platform_driver(i2c_demux_pinctrl_driver); | |
289 | ||
290 | MODULE_DESCRIPTION("pinctrl-based I2C demux driver"); | |
291 | MODULE_AUTHOR("Wolfram Sang <wsa@sang-engineering.com>"); | |
292 | MODULE_LICENSE("GPL v2"); | |
293 | MODULE_ALIAS("platform:i2c-demux-pinctrl"); |