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/init.h>
22 #include <linux/module.h>
23 #include <linux/of_i2c.h>
24 #include <linux/pinctrl/consumer.h>
25 #include <linux/i2c-mux-pinctrl.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
29 struct i2c_mux_pinctrl {
31 struct i2c_mux_pinctrl_platform_data *pdata;
32 struct pinctrl *pinctrl;
33 struct pinctrl_state **states;
34 struct pinctrl_state *state_idle;
35 struct i2c_adapter *parent;
36 struct i2c_adapter **busses;
39 static int i2c_mux_pinctrl_select(struct i2c_adapter *adap, void *data,
42 struct i2c_mux_pinctrl *mux = data;
44 return pinctrl_select_state(mux->pinctrl, mux->states[chan]);
47 static int i2c_mux_pinctrl_deselect(struct i2c_adapter *adap, void *data,
50 struct i2c_mux_pinctrl *mux = data;
52 return pinctrl_select_state(mux->pinctrl, mux->state_idle);
56 static int i2c_mux_pinctrl_parse_dt(struct i2c_mux_pinctrl *mux,
57 struct platform_device *pdev)
59 struct device_node *np = pdev->dev.of_node;
60 int num_names, i, ret;
61 struct device_node *adapter_np;
62 struct i2c_adapter *adapter;
67 mux->pdata = devm_kzalloc(&pdev->dev, sizeof(*mux->pdata), GFP_KERNEL);
70 "Cannot allocate i2c_mux_pinctrl_platform_data\n");
74 num_names = of_property_count_strings(np, "pinctrl-names");
76 dev_err(mux->dev, "Cannot parse pinctrl-names: %d\n",
81 mux->pdata->pinctrl_states = devm_kzalloc(&pdev->dev,
82 sizeof(*mux->pdata->pinctrl_states) * num_names,
84 if (!mux->pdata->pinctrl_states) {
85 dev_err(mux->dev, "Cannot allocate pinctrl_states\n");
89 for (i = 0; i < num_names; i++) {
90 ret = of_property_read_string_index(np, "pinctrl-names", i,
91 &mux->pdata->pinctrl_states[mux->pdata->bus_count]);
93 dev_err(mux->dev, "Cannot parse pinctrl-names: %d\n",
97 if (!strcmp(mux->pdata->pinctrl_states[mux->pdata->bus_count],
99 if (i != num_names - 1) {
100 dev_err(mux->dev, "idle state must be last\n");
103 mux->pdata->pinctrl_state_idle = "idle";
105 mux->pdata->bus_count++;
109 adapter_np = of_parse_phandle(np, "i2c-parent", 0);
111 dev_err(mux->dev, "Cannot parse i2c-parent\n");
114 adapter = of_find_i2c_adapter_by_node(adapter_np);
116 dev_err(mux->dev, "Cannot find parent bus\n");
119 mux->pdata->parent_bus_num = i2c_adapter_id(adapter);
120 put_device(&adapter->dev);
125 static inline int i2c_mux_pinctrl_parse_dt(struct i2c_mux_pinctrl *mux,
126 struct platform_device *pdev)
132 static int i2c_mux_pinctrl_probe(struct platform_device *pdev)
134 struct i2c_mux_pinctrl *mux;
135 int (*deselect)(struct i2c_adapter *, void *, u32);
138 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
140 dev_err(&pdev->dev, "Cannot allocate i2c_mux_pinctrl\n");
144 platform_set_drvdata(pdev, mux);
146 mux->dev = &pdev->dev;
148 mux->pdata = pdev->dev.platform_data;
150 ret = i2c_mux_pinctrl_parse_dt(mux, pdev);
155 dev_err(&pdev->dev, "Missing platform data\n");
160 mux->states = devm_kzalloc(&pdev->dev,
161 sizeof(*mux->states) * mux->pdata->bus_count,
164 dev_err(&pdev->dev, "Cannot allocate states\n");
169 mux->busses = devm_kzalloc(&pdev->dev,
170 sizeof(mux->busses) * mux->pdata->bus_count,
173 dev_err(&pdev->dev, "Cannot allocate busses\n");
178 mux->pinctrl = devm_pinctrl_get(&pdev->dev);
179 if (IS_ERR(mux->pinctrl)) {
180 ret = PTR_ERR(mux->pinctrl);
181 dev_err(&pdev->dev, "Cannot get pinctrl: %d\n", ret);
184 for (i = 0; i < mux->pdata->bus_count; i++) {
185 mux->states[i] = pinctrl_lookup_state(mux->pinctrl,
186 mux->pdata->pinctrl_states[i]);
187 if (IS_ERR(mux->states[i])) {
188 ret = PTR_ERR(mux->states[i]);
190 "Cannot look up pinctrl state %s: %d\n",
191 mux->pdata->pinctrl_states[i], ret);
195 if (mux->pdata->pinctrl_state_idle) {
196 mux->state_idle = pinctrl_lookup_state(mux->pinctrl,
197 mux->pdata->pinctrl_state_idle);
198 if (IS_ERR(mux->state_idle)) {
199 ret = PTR_ERR(mux->state_idle);
201 "Cannot look up pinctrl state %s: %d\n",
202 mux->pdata->pinctrl_state_idle, ret);
206 deselect = i2c_mux_pinctrl_deselect;
211 mux->parent = i2c_get_adapter(mux->pdata->parent_bus_num);
213 dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
214 mux->pdata->parent_bus_num);
219 for (i = 0; i < mux->pdata->bus_count; i++) {
220 u32 bus = mux->pdata->base_bus_num ?
221 (mux->pdata->base_bus_num + i) : 0;
223 mux->busses[i] = i2c_add_mux_adapter(mux->parent, &pdev->dev,
225 i2c_mux_pinctrl_select,
227 if (!mux->busses[i]) {
229 dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
230 goto err_del_adapter;
238 i2c_del_mux_adapter(mux->busses[i - 1]);
239 i2c_put_adapter(mux->parent);
244 static int i2c_mux_pinctrl_remove(struct platform_device *pdev)
246 struct i2c_mux_pinctrl *mux = platform_get_drvdata(pdev);
249 for (i = 0; i < mux->pdata->bus_count; i++)
250 i2c_del_mux_adapter(mux->busses[i]);
252 i2c_put_adapter(mux->parent);
258 static const struct of_device_id i2c_mux_pinctrl_of_match[] = {
259 { .compatible = "i2c-mux-pinctrl", },
262 MODULE_DEVICE_TABLE(of, i2c_mux_pinctrl_of_match);
265 static struct platform_driver i2c_mux_pinctrl_driver = {
267 .name = "i2c-mux-pinctrl",
268 .owner = THIS_MODULE,
269 .of_match_table = of_match_ptr(i2c_mux_pinctrl_of_match),
271 .probe = i2c_mux_pinctrl_probe,
272 .remove = i2c_mux_pinctrl_remove,
274 module_platform_driver(i2c_mux_pinctrl_driver);
276 MODULE_DESCRIPTION("pinctrl-based I2C multiplexer driver");
277 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
278 MODULE_LICENSE("GPL v2");
279 MODULE_ALIAS("platform:i2c-mux-pinctrl");