2 * I2C multiplexer using pinctrl API
4 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/i2c.h>
20 #include <linux/i2c-mux.h>
21 #include <linux/module.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/i2c-mux-pinctrl.h>
24 #include <linux/platform_device.h>
25 #include <linux/slab.h>
27 #include "../../pinctrl/core.h"
29 struct i2c_mux_pinctrl {
30 struct i2c_mux_pinctrl_platform_data *pdata;
31 struct pinctrl *pinctrl;
32 struct pinctrl_state **states;
33 struct pinctrl_state *state_idle;
36 static int i2c_mux_pinctrl_select(struct i2c_mux_core *muxc, u32 chan)
38 struct i2c_mux_pinctrl *mux = i2c_mux_priv(muxc);
40 return pinctrl_select_state(mux->pinctrl, mux->states[chan]);
43 static int i2c_mux_pinctrl_deselect(struct i2c_mux_core *muxc, u32 chan)
45 struct i2c_mux_pinctrl *mux = i2c_mux_priv(muxc);
47 return pinctrl_select_state(mux->pinctrl, mux->state_idle);
51 static int i2c_mux_pinctrl_parse_dt(struct i2c_mux_pinctrl *mux,
52 struct platform_device *pdev)
54 struct device_node *np = pdev->dev.of_node;
55 int num_names, i, ret;
56 struct device_node *adapter_np;
57 struct i2c_adapter *adapter;
62 mux->pdata = devm_kzalloc(&pdev->dev, sizeof(*mux->pdata), GFP_KERNEL);
66 num_names = of_property_count_strings(np, "pinctrl-names");
68 dev_err(&pdev->dev, "Cannot parse pinctrl-names: %d\n",
73 mux->pdata->pinctrl_states = devm_kzalloc(&pdev->dev,
74 sizeof(*mux->pdata->pinctrl_states) * num_names,
76 if (!mux->pdata->pinctrl_states)
79 for (i = 0; i < num_names; i++) {
80 ret = of_property_read_string_index(np, "pinctrl-names", i,
81 &mux->pdata->pinctrl_states[mux->pdata->bus_count]);
83 dev_err(&pdev->dev, "Cannot parse pinctrl-names: %d\n",
87 if (!strcmp(mux->pdata->pinctrl_states[mux->pdata->bus_count],
89 if (i != num_names - 1) {
91 "idle state must be last\n");
94 mux->pdata->pinctrl_state_idle = "idle";
96 mux->pdata->bus_count++;
100 adapter_np = of_parse_phandle(np, "i2c-parent", 0);
102 dev_err(&pdev->dev, "Cannot parse i2c-parent\n");
105 adapter = of_find_i2c_adapter_by_node(adapter_np);
106 of_node_put(adapter_np);
108 dev_err(&pdev->dev, "Cannot find parent bus\n");
109 return -EPROBE_DEFER;
111 mux->pdata->parent_bus_num = i2c_adapter_id(adapter);
112 put_device(&adapter->dev);
117 static inline int i2c_mux_pinctrl_parse_dt(struct i2c_mux_pinctrl *mux,
118 struct platform_device *pdev)
124 static struct i2c_adapter *i2c_mux_pinctrl_root_adapter(
125 struct pinctrl_state *state)
127 struct i2c_adapter *root = NULL;
128 struct pinctrl_setting *setting;
129 struct i2c_adapter *pin_root;
131 list_for_each_entry(setting, &state->settings, node) {
132 pin_root = i2c_root_adapter(setting->pctldev->dev);
137 else if (root != pin_root)
144 static int i2c_mux_pinctrl_probe(struct platform_device *pdev)
146 struct i2c_mux_core *muxc;
147 struct i2c_mux_pinctrl *mux;
148 struct i2c_adapter *root;
151 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
157 mux->pdata = dev_get_platdata(&pdev->dev);
159 ret = i2c_mux_pinctrl_parse_dt(mux, pdev);
164 dev_err(&pdev->dev, "Missing platform data\n");
169 mux->states = devm_kzalloc(&pdev->dev,
170 sizeof(*mux->states) * mux->pdata->bus_count,
173 dev_err(&pdev->dev, "Cannot allocate states\n");
178 muxc = i2c_mux_alloc(NULL, &pdev->dev, mux->pdata->bus_count, 0, 0,
179 i2c_mux_pinctrl_select, NULL);
186 platform_set_drvdata(pdev, muxc);
188 mux->pinctrl = devm_pinctrl_get(&pdev->dev);
189 if (IS_ERR(mux->pinctrl)) {
190 ret = PTR_ERR(mux->pinctrl);
191 dev_err(&pdev->dev, "Cannot get pinctrl: %d\n", ret);
194 for (i = 0; i < mux->pdata->bus_count; i++) {
195 mux->states[i] = pinctrl_lookup_state(mux->pinctrl,
196 mux->pdata->pinctrl_states[i]);
197 if (IS_ERR(mux->states[i])) {
198 ret = PTR_ERR(mux->states[i]);
200 "Cannot look up pinctrl state %s: %d\n",
201 mux->pdata->pinctrl_states[i], ret);
205 if (mux->pdata->pinctrl_state_idle) {
206 mux->state_idle = pinctrl_lookup_state(mux->pinctrl,
207 mux->pdata->pinctrl_state_idle);
208 if (IS_ERR(mux->state_idle)) {
209 ret = PTR_ERR(mux->state_idle);
211 "Cannot look up pinctrl state %s: %d\n",
212 mux->pdata->pinctrl_state_idle, ret);
216 muxc->deselect = i2c_mux_pinctrl_deselect;
219 muxc->parent = i2c_get_adapter(mux->pdata->parent_bus_num);
221 dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
222 mux->pdata->parent_bus_num);
227 root = i2c_root_adapter(&muxc->parent->dev);
229 muxc->mux_locked = true;
230 for (i = 0; i < mux->pdata->bus_count; i++) {
231 if (root != i2c_mux_pinctrl_root_adapter(mux->states[i])) {
232 muxc->mux_locked = false;
236 if (muxc->mux_locked && mux->pdata->pinctrl_state_idle &&
237 root != i2c_mux_pinctrl_root_adapter(mux->state_idle))
238 muxc->mux_locked = false;
240 if (muxc->mux_locked)
241 dev_info(&pdev->dev, "mux-locked i2c mux\n");
243 for (i = 0; i < mux->pdata->bus_count; i++) {
244 u32 bus = mux->pdata->base_bus_num ?
245 (mux->pdata->base_bus_num + i) : 0;
247 ret = i2c_mux_add_adapter(muxc, bus, i, 0);
249 goto err_del_adapter;
255 i2c_mux_del_adapters(muxc);
256 i2c_put_adapter(muxc->parent);
261 static int i2c_mux_pinctrl_remove(struct platform_device *pdev)
263 struct i2c_mux_core *muxc = platform_get_drvdata(pdev);
265 i2c_mux_del_adapters(muxc);
266 i2c_put_adapter(muxc->parent);
271 static const struct of_device_id i2c_mux_pinctrl_of_match[] = {
272 { .compatible = "i2c-mux-pinctrl", },
275 MODULE_DEVICE_TABLE(of, i2c_mux_pinctrl_of_match);
278 static struct platform_driver i2c_mux_pinctrl_driver = {
280 .name = "i2c-mux-pinctrl",
281 .of_match_table = of_match_ptr(i2c_mux_pinctrl_of_match),
283 .probe = i2c_mux_pinctrl_probe,
284 .remove = i2c_mux_pinctrl_remove,
286 module_platform_driver(i2c_mux_pinctrl_driver);
288 MODULE_DESCRIPTION("pinctrl-based I2C multiplexer driver");
289 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
290 MODULE_LICENSE("GPL v2");
291 MODULE_ALIAS("platform:i2c-mux-pinctrl");