]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/pinctrl/sh-pfc/pinctrl.c
sh-pfc: Drop the sh_pfc_pinctrl spinlock
[mirror_ubuntu-artful-kernel.git] / drivers / pinctrl / sh-pfc / pinctrl.c
1 /*
2 * SuperH Pin Function Controller pinmux support.
3 *
4 * Copyright (C) 2012 Paul Mundt
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
8 * for more details.
9 */
10
11 #define DRV_NAME "sh-pfc"
12 #define pr_fmt(fmt) KBUILD_MODNAME " pinctrl: " fmt
13
14 #include <linux/device.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/pinctrl/consumer.h>
19 #include <linux/pinctrl/pinconf.h>
20 #include <linux/pinctrl/pinconf-generic.h>
21 #include <linux/pinctrl/pinctrl.h>
22 #include <linux/pinctrl/pinmux.h>
23 #include <linux/slab.h>
24 #include <linux/spinlock.h>
25
26 #include "core.h"
27
28 struct sh_pfc_pinctrl {
29 struct pinctrl_dev *pctl;
30 struct pinctrl_desc pctl_desc;
31 struct pinctrl_gpio_range range;
32
33 struct sh_pfc *pfc;
34
35 struct pinmux_gpio **functions;
36 unsigned int nr_functions;
37
38 struct pinctrl_pin_desc *pads;
39 unsigned int nr_pads;
40 };
41
42 static int sh_pfc_get_groups_count(struct pinctrl_dev *pctldev)
43 {
44 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
45
46 return pmx->nr_pads;
47 }
48
49 static const char *sh_pfc_get_group_name(struct pinctrl_dev *pctldev,
50 unsigned selector)
51 {
52 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
53
54 return pmx->pads[selector].name;
55 }
56
57 static int sh_pfc_get_group_pins(struct pinctrl_dev *pctldev, unsigned group,
58 const unsigned **pins, unsigned *num_pins)
59 {
60 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
61
62 *pins = &pmx->pads[group].number;
63 *num_pins = 1;
64
65 return 0;
66 }
67
68 static void sh_pfc_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
69 unsigned offset)
70 {
71 seq_printf(s, "%s", DRV_NAME);
72 }
73
74 static const struct pinctrl_ops sh_pfc_pinctrl_ops = {
75 .get_groups_count = sh_pfc_get_groups_count,
76 .get_group_name = sh_pfc_get_group_name,
77 .get_group_pins = sh_pfc_get_group_pins,
78 .pin_dbg_show = sh_pfc_pin_dbg_show,
79 };
80
81 static int sh_pfc_get_functions_count(struct pinctrl_dev *pctldev)
82 {
83 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
84
85 return pmx->nr_functions;
86 }
87
88 static const char *sh_pfc_get_function_name(struct pinctrl_dev *pctldev,
89 unsigned selector)
90 {
91 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
92
93 return pmx->functions[selector]->name;
94 }
95
96 static int sh_pfc_get_function_groups(struct pinctrl_dev *pctldev, unsigned func,
97 const char * const **groups,
98 unsigned * const num_groups)
99 {
100 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
101
102 *groups = &pmx->functions[func]->name;
103 *num_groups = 1;
104
105 return 0;
106 }
107
108 static int sh_pfc_noop_enable(struct pinctrl_dev *pctldev, unsigned func,
109 unsigned group)
110 {
111 return 0;
112 }
113
114 static void sh_pfc_noop_disable(struct pinctrl_dev *pctldev, unsigned func,
115 unsigned group)
116 {
117 }
118
119 static int sh_pfc_config_function(struct sh_pfc *pfc, unsigned offset)
120 {
121 if (sh_pfc_config_gpio(pfc, offset,
122 PINMUX_TYPE_FUNCTION,
123 GPIO_CFG_DRYRUN) != 0)
124 return -EINVAL;
125
126 if (sh_pfc_config_gpio(pfc, offset,
127 PINMUX_TYPE_FUNCTION,
128 GPIO_CFG_REQ) != 0)
129 return -EINVAL;
130
131 return 0;
132 }
133
134 static int sh_pfc_reconfig_pin(struct sh_pfc *pfc, unsigned offset,
135 int new_type)
136 {
137 unsigned long flags;
138 int pinmux_type;
139 int ret = -EINVAL;
140
141 spin_lock_irqsave(&pfc->lock, flags);
142
143 pinmux_type = pfc->info->gpios[offset].flags & PINMUX_FLAG_TYPE;
144
145 /*
146 * See if the present config needs to first be de-configured.
147 */
148 switch (pinmux_type) {
149 case PINMUX_TYPE_GPIO:
150 break;
151 case PINMUX_TYPE_OUTPUT:
152 case PINMUX_TYPE_INPUT:
153 case PINMUX_TYPE_INPUT_PULLUP:
154 case PINMUX_TYPE_INPUT_PULLDOWN:
155 sh_pfc_config_gpio(pfc, offset, pinmux_type, GPIO_CFG_FREE);
156 break;
157 default:
158 goto err;
159 }
160
161 /*
162 * Dry run
163 */
164 if (sh_pfc_config_gpio(pfc, offset, new_type,
165 GPIO_CFG_DRYRUN) != 0)
166 goto err;
167
168 /*
169 * Request
170 */
171 if (sh_pfc_config_gpio(pfc, offset, new_type,
172 GPIO_CFG_REQ) != 0)
173 goto err;
174
175 pfc->info->gpios[offset].flags &= ~PINMUX_FLAG_TYPE;
176 pfc->info->gpios[offset].flags |= new_type;
177
178 ret = 0;
179
180 err:
181 spin_unlock_irqrestore(&pfc->lock, flags);
182
183 return ret;
184 }
185
186
187 static int sh_pfc_gpio_request_enable(struct pinctrl_dev *pctldev,
188 struct pinctrl_gpio_range *range,
189 unsigned offset)
190 {
191 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
192 struct sh_pfc *pfc = pmx->pfc;
193 unsigned long flags;
194 int ret, pinmux_type;
195
196 spin_lock_irqsave(&pfc->lock, flags);
197
198 pinmux_type = pfc->info->gpios[offset].flags & PINMUX_FLAG_TYPE;
199
200 switch (pinmux_type) {
201 case PINMUX_TYPE_FUNCTION:
202 pr_notice_once("Use of GPIO API for function requests is "
203 "deprecated, convert to pinctrl\n");
204 /* handle for now */
205 ret = sh_pfc_config_function(pfc, offset);
206 if (unlikely(ret < 0))
207 goto err;
208
209 break;
210 case PINMUX_TYPE_GPIO:
211 case PINMUX_TYPE_INPUT:
212 case PINMUX_TYPE_OUTPUT:
213 break;
214 default:
215 pr_err("Unsupported mux type (%d), bailing...\n", pinmux_type);
216 ret = -ENOTSUPP;
217 goto err;
218 }
219
220 ret = 0;
221
222 err:
223 spin_unlock_irqrestore(&pfc->lock, flags);
224
225 return ret;
226 }
227
228 static void sh_pfc_gpio_disable_free(struct pinctrl_dev *pctldev,
229 struct pinctrl_gpio_range *range,
230 unsigned offset)
231 {
232 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
233 struct sh_pfc *pfc = pmx->pfc;
234 unsigned long flags;
235 int pinmux_type;
236
237 spin_lock_irqsave(&pfc->lock, flags);
238
239 pinmux_type = pfc->info->gpios[offset].flags & PINMUX_FLAG_TYPE;
240
241 sh_pfc_config_gpio(pfc, offset, pinmux_type, GPIO_CFG_FREE);
242
243 spin_unlock_irqrestore(&pfc->lock, flags);
244 }
245
246 static int sh_pfc_gpio_set_direction(struct pinctrl_dev *pctldev,
247 struct pinctrl_gpio_range *range,
248 unsigned offset, bool input)
249 {
250 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
251 int type = input ? PINMUX_TYPE_INPUT : PINMUX_TYPE_OUTPUT;
252
253 return sh_pfc_reconfig_pin(pmx->pfc, offset, type);
254 }
255
256 static const struct pinmux_ops sh_pfc_pinmux_ops = {
257 .get_functions_count = sh_pfc_get_functions_count,
258 .get_function_name = sh_pfc_get_function_name,
259 .get_function_groups = sh_pfc_get_function_groups,
260 .enable = sh_pfc_noop_enable,
261 .disable = sh_pfc_noop_disable,
262 .gpio_request_enable = sh_pfc_gpio_request_enable,
263 .gpio_disable_free = sh_pfc_gpio_disable_free,
264 .gpio_set_direction = sh_pfc_gpio_set_direction,
265 };
266
267 static int sh_pfc_pinconf_get(struct pinctrl_dev *pctldev, unsigned pin,
268 unsigned long *config)
269 {
270 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
271 struct sh_pfc *pfc = pmx->pfc;
272
273 *config = pfc->info->gpios[pin].flags & PINMUX_FLAG_TYPE;
274
275 return 0;
276 }
277
278 static int sh_pfc_pinconf_set(struct pinctrl_dev *pctldev, unsigned pin,
279 unsigned long config)
280 {
281 struct sh_pfc_pinctrl *pmx = pinctrl_dev_get_drvdata(pctldev);
282
283 /* Validate the new type */
284 if (config >= PINMUX_FLAG_TYPE)
285 return -EINVAL;
286
287 return sh_pfc_reconfig_pin(pmx->pfc, pin, config);
288 }
289
290 static void sh_pfc_pinconf_dbg_show(struct pinctrl_dev *pctldev,
291 struct seq_file *s, unsigned pin)
292 {
293 const char *pinmux_type_str[] = {
294 [PINMUX_TYPE_NONE] = "none",
295 [PINMUX_TYPE_FUNCTION] = "function",
296 [PINMUX_TYPE_GPIO] = "gpio",
297 [PINMUX_TYPE_OUTPUT] = "output",
298 [PINMUX_TYPE_INPUT] = "input",
299 [PINMUX_TYPE_INPUT_PULLUP] = "input bias pull up",
300 [PINMUX_TYPE_INPUT_PULLDOWN] = "input bias pull down",
301 };
302 unsigned long config;
303 int rc;
304
305 rc = sh_pfc_pinconf_get(pctldev, pin, &config);
306 if (unlikely(rc != 0))
307 return;
308
309 seq_printf(s, " %s", pinmux_type_str[config]);
310 }
311
312 static const struct pinconf_ops sh_pfc_pinconf_ops = {
313 .pin_config_get = sh_pfc_pinconf_get,
314 .pin_config_set = sh_pfc_pinconf_set,
315 .pin_config_dbg_show = sh_pfc_pinconf_dbg_show,
316 };
317
318 static void sh_pfc_map_one_gpio(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx,
319 struct pinmux_gpio *gpio, unsigned offset)
320 {
321 struct pinmux_data_reg *dummy;
322 int bit;
323
324 gpio->flags &= ~PINMUX_FLAG_TYPE;
325
326 if (sh_pfc_get_data_reg(pfc, offset, &dummy, &bit) == 0)
327 gpio->flags |= PINMUX_TYPE_GPIO;
328 else {
329 gpio->flags |= PINMUX_TYPE_FUNCTION;
330 pmx->nr_functions++;
331 }
332 }
333
334 /* pinmux ranges -> pinctrl pin descs */
335 static int sh_pfc_map_gpios(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
336 {
337 unsigned long flags;
338 int i;
339
340 pmx->nr_pads = pfc->info->last_gpio - pfc->info->first_gpio + 1;
341
342 pmx->pads = devm_kzalloc(pfc->dev, sizeof(*pmx->pads) * pmx->nr_pads,
343 GFP_KERNEL);
344 if (unlikely(!pmx->pads)) {
345 pmx->nr_pads = 0;
346 return -ENOMEM;
347 }
348
349 spin_lock_irqsave(&pfc->lock, flags);
350
351 /*
352 * We don't necessarily have a 1:1 mapping between pin and linux
353 * GPIO number, as the latter maps to the associated enum_id.
354 * Care needs to be taken to translate back to pin space when
355 * dealing with any pin configurations.
356 */
357 for (i = 0; i < pmx->nr_pads; i++) {
358 struct pinctrl_pin_desc *pin = pmx->pads + i;
359 struct pinmux_gpio *gpio = pfc->info->gpios + i;
360
361 pin->number = pfc->info->first_gpio + i;
362 pin->name = gpio->name;
363
364 /* XXX */
365 if (unlikely(!gpio->enum_id))
366 continue;
367
368 sh_pfc_map_one_gpio(pfc, pmx, gpio, i);
369 }
370
371 spin_unlock_irqrestore(&pfc->lock, flags);
372
373 return 0;
374 }
375
376 static int sh_pfc_map_functions(struct sh_pfc *pfc, struct sh_pfc_pinctrl *pmx)
377 {
378 int i, fn;
379
380 pmx->functions = devm_kzalloc(pfc->dev, pmx->nr_functions *
381 sizeof(*pmx->functions), GFP_KERNEL);
382 if (unlikely(!pmx->functions))
383 return -ENOMEM;
384
385 for (i = fn = 0; i < pmx->nr_pads; i++) {
386 struct pinmux_gpio *gpio = pfc->info->gpios + i;
387
388 if ((gpio->flags & PINMUX_FLAG_TYPE) == PINMUX_TYPE_FUNCTION)
389 pmx->functions[fn++] = gpio;
390 }
391
392 return 0;
393 }
394
395 int sh_pfc_register_pinctrl(struct sh_pfc *pfc)
396 {
397 struct sh_pfc_pinctrl *pmx;
398 int ret;
399
400 pmx = devm_kzalloc(pfc->dev, sizeof(*pmx), GFP_KERNEL);
401 if (unlikely(!pmx))
402 return -ENOMEM;
403
404 pmx->pfc = pfc;
405 pfc->pinctrl = pmx;
406
407 ret = sh_pfc_map_gpios(pfc, pmx);
408 if (unlikely(ret != 0))
409 return ret;
410
411 ret = sh_pfc_map_functions(pfc, pmx);
412 if (unlikely(ret != 0))
413 return ret;
414
415 pmx->pctl_desc.name = DRV_NAME;
416 pmx->pctl_desc.owner = THIS_MODULE;
417 pmx->pctl_desc.pctlops = &sh_pfc_pinctrl_ops;
418 pmx->pctl_desc.pmxops = &sh_pfc_pinmux_ops;
419 pmx->pctl_desc.confops = &sh_pfc_pinconf_ops;
420 pmx->pctl_desc.pins = pmx->pads;
421 pmx->pctl_desc.npins = pmx->nr_pads;
422
423 pmx->pctl = pinctrl_register(&pmx->pctl_desc, pfc->dev, pmx);
424 if (IS_ERR(pmx->pctl))
425 return PTR_ERR(pmx->pctl);
426
427 pmx->range.name = DRV_NAME,
428 pmx->range.id = 0;
429 pmx->range.npins = pfc->info->last_gpio - pfc->info->first_gpio + 1;
430 pmx->range.base = pfc->info->first_gpio;
431 pmx->range.pin_base = pfc->info->first_gpio;
432
433 pinctrl_add_gpio_range(pmx->pctl, &pmx->range);
434
435 return 0;
436 }
437
438 int sh_pfc_unregister_pinctrl(struct sh_pfc *pfc)
439 {
440 struct sh_pfc_pinctrl *pmx = pfc->pinctrl;
441
442 pinctrl_unregister(pmx->pctl);
443
444 pfc->pinctrl = NULL;
445 return 0;
446 }