PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / i2c / muxes / i2c-mux-pinctrl.c
blob4ff0ef3e07a6b9839c4bcb9e48618f859bc7e10d
1 /*
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
13 * more details.
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>
26 #include <linux/of.h>
28 struct i2c_mux_pinctrl {
29 struct device *dev;
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,
39 u32 chan)
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,
47 u32 chan)
49 struct i2c_mux_pinctrl *mux = data;
51 return pinctrl_select_state(mux->pinctrl, mux->state_idle);
54 #ifdef CONFIG_OF
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;
63 if (!np)
64 return 0;
66 mux->pdata = devm_kzalloc(&pdev->dev, sizeof(*mux->pdata), GFP_KERNEL);
67 if (!mux->pdata) {
68 dev_err(mux->dev,
69 "Cannot allocate i2c_mux_pinctrl_platform_data\n");
70 return -ENOMEM;
73 num_names = of_property_count_strings(np, "pinctrl-names");
74 if (num_names < 0) {
75 dev_err(mux->dev, "Cannot parse pinctrl-names: %d\n",
76 num_names);
77 return num_names;
80 mux->pdata->pinctrl_states = devm_kzalloc(&pdev->dev,
81 sizeof(*mux->pdata->pinctrl_states) * num_names,
82 GFP_KERNEL);
83 if (!mux->pdata->pinctrl_states) {
84 dev_err(mux->dev, "Cannot allocate pinctrl_states\n");
85 return -ENOMEM;
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]);
91 if (ret < 0) {
92 dev_err(mux->dev, "Cannot parse pinctrl-names: %d\n",
93 ret);
94 return ret;
96 if (!strcmp(mux->pdata->pinctrl_states[mux->pdata->bus_count],
97 "idle")) {
98 if (i != num_names - 1) {
99 dev_err(mux->dev, "idle state must be last\n");
100 return -EINVAL;
102 mux->pdata->pinctrl_state_idle = "idle";
103 } else {
104 mux->pdata->bus_count++;
108 adapter_np = of_parse_phandle(np, "i2c-parent", 0);
109 if (!adapter_np) {
110 dev_err(mux->dev, "Cannot parse i2c-parent\n");
111 return -ENODEV;
113 adapter = of_find_i2c_adapter_by_node(adapter_np);
114 if (!adapter) {
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);
121 return 0;
123 #else
124 static inline int i2c_mux_pinctrl_parse_dt(struct i2c_mux_pinctrl *mux,
125 struct platform_device *pdev)
127 return 0;
129 #endif
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);
135 int i, ret;
137 mux = devm_kzalloc(&pdev->dev, sizeof(*mux), GFP_KERNEL);
138 if (!mux) {
139 dev_err(&pdev->dev, "Cannot allocate i2c_mux_pinctrl\n");
140 ret = -ENOMEM;
141 goto err;
143 platform_set_drvdata(pdev, mux);
145 mux->dev = &pdev->dev;
147 mux->pdata = dev_get_platdata(&pdev->dev);
148 if (!mux->pdata) {
149 ret = i2c_mux_pinctrl_parse_dt(mux, pdev);
150 if (ret < 0)
151 goto err;
153 if (!mux->pdata) {
154 dev_err(&pdev->dev, "Missing platform data\n");
155 ret = -ENODEV;
156 goto err;
159 mux->states = devm_kzalloc(&pdev->dev,
160 sizeof(*mux->states) * mux->pdata->bus_count,
161 GFP_KERNEL);
162 if (!mux->states) {
163 dev_err(&pdev->dev, "Cannot allocate states\n");
164 ret = -ENOMEM;
165 goto err;
168 mux->busses = devm_kzalloc(&pdev->dev,
169 sizeof(*mux->busses) * mux->pdata->bus_count,
170 GFP_KERNEL);
171 if (!mux->busses) {
172 dev_err(&pdev->dev, "Cannot allocate busses\n");
173 ret = -ENOMEM;
174 goto err;
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);
181 goto err;
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]);
188 dev_err(&pdev->dev,
189 "Cannot look up pinctrl state %s: %d\n",
190 mux->pdata->pinctrl_states[i], ret);
191 goto err;
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);
199 dev_err(&pdev->dev,
200 "Cannot look up pinctrl state %s: %d\n",
201 mux->pdata->pinctrl_state_idle, ret);
202 goto err;
205 deselect = i2c_mux_pinctrl_deselect;
206 } else {
207 deselect = NULL;
210 mux->parent = i2c_get_adapter(mux->pdata->parent_bus_num);
211 if (!mux->parent) {
212 dev_err(&pdev->dev, "Parent adapter (%d) not found\n",
213 mux->pdata->parent_bus_num);
214 ret = -EPROBE_DEFER;
215 goto err;
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,
223 mux, bus, i, 0,
224 i2c_mux_pinctrl_select,
225 deselect);
226 if (!mux->busses[i]) {
227 ret = -ENODEV;
228 dev_err(&pdev->dev, "Failed to add adapter %d\n", i);
229 goto err_del_adapter;
233 return 0;
235 err_del_adapter:
236 for (; i > 0; i--)
237 i2c_del_mux_adapter(mux->busses[i - 1]);
238 i2c_put_adapter(mux->parent);
239 err:
240 return ret;
243 static int i2c_mux_pinctrl_remove(struct platform_device *pdev)
245 struct i2c_mux_pinctrl *mux = platform_get_drvdata(pdev);
246 int i;
248 for (i = 0; i < mux->pdata->bus_count; i++)
249 i2c_del_mux_adapter(mux->busses[i]);
251 i2c_put_adapter(mux->parent);
253 return 0;
256 #ifdef CONFIG_OF
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);
262 #endif
264 static struct platform_driver i2c_mux_pinctrl_driver = {
265 .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");