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>
28 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
;
34 struct i2c_adapter
*parent
;
35 struct i2c_adapter
**busses
;
38 static int i2c_mux_pinctrl_select(struct i2c_adapter
*adap
, void *data
,
41 struct i2c_mux_pinctrl
*mux
= data
;
43 return pinctrl_select_state(mux
->pinctrl
, mux
->states
[chan
]);
46 static int i2c_mux_pinctrl_deselect(struct i2c_adapter
*adap
, void *data
,
49 struct i2c_mux_pinctrl
*mux
= data
;
51 return pinctrl_select_state(mux
->pinctrl
, mux
->state_idle
);
55 static int i2c_mux_pinctrl_parse_dt(struct i2c_mux_pinctrl
*mux
,
56 struct platform_device
*pdev
)
58 struct device_node
*np
= pdev
->dev
.of_node
;
59 int num_names
, i
, ret
;
60 struct device_node
*adapter_np
;
61 struct i2c_adapter
*adapter
;
66 mux
->pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*mux
->pdata
), GFP_KERNEL
);
69 "Cannot allocate i2c_mux_pinctrl_platform_data\n");
73 num_names
= of_property_count_strings(np
, "pinctrl-names");
75 dev_err(mux
->dev
, "Cannot parse pinctrl-names: %d\n",
80 mux
->pdata
->pinctrl_states
= devm_kzalloc(&pdev
->dev
,
81 sizeof(*mux
->pdata
->pinctrl_states
) * num_names
,
83 if (!mux
->pdata
->pinctrl_states
) {
84 dev_err(mux
->dev
, "Cannot allocate pinctrl_states\n");
88 for (i
= 0; i
< num_names
; i
++) {
89 ret
= of_property_read_string_index(np
, "pinctrl-names", i
,
90 &mux
->pdata
->pinctrl_states
[mux
->pdata
->bus_count
]);
92 dev_err(mux
->dev
, "Cannot parse pinctrl-names: %d\n",
96 if (!strcmp(mux
->pdata
->pinctrl_states
[mux
->pdata
->bus_count
],
98 if (i
!= num_names
- 1) {
99 dev_err(mux
->dev
, "idle state must be last\n");
102 mux
->pdata
->pinctrl_state_idle
= "idle";
104 mux
->pdata
->bus_count
++;
108 adapter_np
= of_parse_phandle(np
, "i2c-parent", 0);
110 dev_err(mux
->dev
, "Cannot parse i2c-parent\n");
113 adapter
= of_find_i2c_adapter_by_node(adapter_np
);
115 dev_err(mux
->dev
, "Cannot find parent bus\n");
116 return -EPROBE_DEFER
;
118 mux
->pdata
->parent_bus_num
= i2c_adapter_id(adapter
);
119 put_device(&adapter
->dev
);
124 static inline int i2c_mux_pinctrl_parse_dt(struct i2c_mux_pinctrl
*mux
,
125 struct platform_device
*pdev
)
131 static int i2c_mux_pinctrl_probe(struct platform_device
*pdev
)
133 struct i2c_mux_pinctrl
*mux
;
134 int (*deselect
)(struct i2c_adapter
*, void *, u32
);
137 mux
= devm_kzalloc(&pdev
->dev
, sizeof(*mux
), GFP_KERNEL
);
139 dev_err(&pdev
->dev
, "Cannot allocate i2c_mux_pinctrl\n");
143 platform_set_drvdata(pdev
, mux
);
145 mux
->dev
= &pdev
->dev
;
147 mux
->pdata
= dev_get_platdata(&pdev
->dev
);
149 ret
= i2c_mux_pinctrl_parse_dt(mux
, pdev
);
154 dev_err(&pdev
->dev
, "Missing platform data\n");
159 mux
->states
= devm_kzalloc(&pdev
->dev
,
160 sizeof(*mux
->states
) * mux
->pdata
->bus_count
,
163 dev_err(&pdev
->dev
, "Cannot allocate states\n");
168 mux
->busses
= devm_kzalloc(&pdev
->dev
,
169 sizeof(*mux
->busses
) * mux
->pdata
->bus_count
,
172 dev_err(&pdev
->dev
, "Cannot allocate busses\n");
177 mux
->pinctrl
= devm_pinctrl_get(&pdev
->dev
);
178 if (IS_ERR(mux
->pinctrl
)) {
179 ret
= PTR_ERR(mux
->pinctrl
);
180 dev_err(&pdev
->dev
, "Cannot get pinctrl: %d\n", ret
);
183 for (i
= 0; i
< mux
->pdata
->bus_count
; i
++) {
184 mux
->states
[i
] = pinctrl_lookup_state(mux
->pinctrl
,
185 mux
->pdata
->pinctrl_states
[i
]);
186 if (IS_ERR(mux
->states
[i
])) {
187 ret
= PTR_ERR(mux
->states
[i
]);
189 "Cannot look up pinctrl state %s: %d\n",
190 mux
->pdata
->pinctrl_states
[i
], ret
);
194 if (mux
->pdata
->pinctrl_state_idle
) {
195 mux
->state_idle
= pinctrl_lookup_state(mux
->pinctrl
,
196 mux
->pdata
->pinctrl_state_idle
);
197 if (IS_ERR(mux
->state_idle
)) {
198 ret
= PTR_ERR(mux
->state_idle
);
200 "Cannot look up pinctrl state %s: %d\n",
201 mux
->pdata
->pinctrl_state_idle
, ret
);
205 deselect
= i2c_mux_pinctrl_deselect
;
210 mux
->parent
= i2c_get_adapter(mux
->pdata
->parent_bus_num
);
212 dev_err(&pdev
->dev
, "Parent adapter (%d) not found\n",
213 mux
->pdata
->parent_bus_num
);
218 for (i
= 0; i
< mux
->pdata
->bus_count
; i
++) {
219 u32 bus
= mux
->pdata
->base_bus_num
?
220 (mux
->pdata
->base_bus_num
+ i
) : 0;
222 mux
->busses
[i
] = i2c_add_mux_adapter(mux
->parent
, &pdev
->dev
,
224 i2c_mux_pinctrl_select
,
226 if (!mux
->busses
[i
]) {
228 dev_err(&pdev
->dev
, "Failed to add adapter %d\n", i
);
229 goto err_del_adapter
;
237 i2c_del_mux_adapter(mux
->busses
[i
- 1]);
238 i2c_put_adapter(mux
->parent
);
243 static int i2c_mux_pinctrl_remove(struct platform_device
*pdev
)
245 struct i2c_mux_pinctrl
*mux
= platform_get_drvdata(pdev
);
248 for (i
= 0; i
< mux
->pdata
->bus_count
; i
++)
249 i2c_del_mux_adapter(mux
->busses
[i
]);
251 i2c_put_adapter(mux
->parent
);
257 static const struct of_device_id i2c_mux_pinctrl_of_match
[] = {
258 { .compatible
= "i2c-mux-pinctrl", },
261 MODULE_DEVICE_TABLE(of
, i2c_mux_pinctrl_of_match
);
264 static struct platform_driver i2c_mux_pinctrl_driver
= {
266 .name
= "i2c-mux-pinctrl",
267 .owner
= THIS_MODULE
,
268 .of_match_table
= of_match_ptr(i2c_mux_pinctrl_of_match
),
270 .probe
= i2c_mux_pinctrl_probe
,
271 .remove
= i2c_mux_pinctrl_remove
,
273 module_platform_driver(i2c_mux_pinctrl_driver
);
275 MODULE_DESCRIPTION("pinctrl-based I2C multiplexer driver");
276 MODULE_AUTHOR("Stephen Warren <swarren@nvidia.com>");
277 MODULE_LICENSE("GPL v2");
278 MODULE_ALIAS("platform:i2c-mux-pinctrl");