blob: 8bdd65b924ca676f145969745ef72f5e6b90a2ff [file] [log] [blame]
Maxime Ripard0e37f882013-01-18 22:30:34 +01001/*
2 * Allwinner A1X SoCs pinctrl driver.
3 *
4 * Copyright (C) 2012 Maxime Ripard
5 *
6 * Maxime Ripard <maxime.ripard@free-electrons.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
13#include <linux/io.h>
Emilio López950707c2013-03-22 11:20:40 -030014#include <linux/clk.h>
Maxime Ripard08e9e612013-01-28 21:33:12 +010015#include <linux/gpio.h>
Maxime Ripard60242db2013-06-08 12:05:44 +020016#include <linux/irqdomain.h>
Chen-Yu Tsai905a5112014-02-11 00:22:37 +080017#include <linux/irqchip/chained_irq.h>
Maxime Ripard0e37f882013-01-18 22:30:34 +010018#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/of_address.h>
21#include <linux/of_device.h>
Maxime Ripard60242db2013-06-08 12:05:44 +020022#include <linux/of_irq.h>
Maxime Ripard0e37f882013-01-18 22:30:34 +010023#include <linux/pinctrl/consumer.h>
24#include <linux/pinctrl/machine.h>
25#include <linux/pinctrl/pinctrl.h>
26#include <linux/pinctrl/pinconf-generic.h>
27#include <linux/pinctrl/pinmux.h>
28#include <linux/platform_device.h>
29#include <linux/slab.h>
30
Maxime Ripard5f910772014-04-18 18:53:02 +020031#include "../core.h"
Maxime Ripard0e37f882013-01-18 22:30:34 +010032#include "pinctrl-sunxi.h"
Maxime Ripardeaa3d842013-01-18 22:30:35 +010033
Maxime Ripard0e37f882013-01-18 22:30:34 +010034static struct sunxi_pinctrl_group *
35sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl *pctl, const char *group)
36{
37 int i;
38
39 for (i = 0; i < pctl->ngroups; i++) {
40 struct sunxi_pinctrl_group *grp = pctl->groups + i;
41
42 if (!strcmp(grp->name, group))
43 return grp;
44 }
45
46 return NULL;
47}
48
49static struct sunxi_pinctrl_function *
50sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl *pctl,
51 const char *name)
52{
53 struct sunxi_pinctrl_function *func = pctl->functions;
54 int i;
55
56 for (i = 0; i < pctl->nfunctions; i++) {
57 if (!func[i].name)
58 break;
59
60 if (!strcmp(func[i].name, name))
61 return func + i;
62 }
63
64 return NULL;
65}
66
67static struct sunxi_desc_function *
68sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl *pctl,
69 const char *pin_name,
70 const char *func_name)
71{
72 int i;
73
74 for (i = 0; i < pctl->desc->npins; i++) {
75 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
76
77 if (!strcmp(pin->pin.name, pin_name)) {
78 struct sunxi_desc_function *func = pin->functions;
79
80 while (func->name) {
81 if (!strcmp(func->name, func_name))
82 return func;
83
84 func++;
85 }
86 }
87 }
88
89 return NULL;
90}
91
Maxime Ripard814d4f22013-06-08 12:05:43 +020092static struct sunxi_desc_function *
93sunxi_pinctrl_desc_find_function_by_pin(struct sunxi_pinctrl *pctl,
94 const u16 pin_num,
95 const char *func_name)
96{
97 int i;
98
99 for (i = 0; i < pctl->desc->npins; i++) {
100 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
101
102 if (pin->pin.number == pin_num) {
103 struct sunxi_desc_function *func = pin->functions;
104
105 while (func->name) {
106 if (!strcmp(func->name, func_name))
107 return func;
108
109 func++;
110 }
111 }
112 }
113
114 return NULL;
115}
116
Maxime Ripard0e37f882013-01-18 22:30:34 +0100117static int sunxi_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
118{
119 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
120
121 return pctl->ngroups;
122}
123
124static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev *pctldev,
125 unsigned group)
126{
127 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
128
129 return pctl->groups[group].name;
130}
131
132static int sunxi_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
133 unsigned group,
134 const unsigned **pins,
135 unsigned *num_pins)
136{
137 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
138
139 *pins = (unsigned *)&pctl->groups[group].pin;
140 *num_pins = 1;
141
142 return 0;
143}
144
145static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
146 struct device_node *node,
147 struct pinctrl_map **map,
148 unsigned *num_maps)
149{
150 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
151 unsigned long *pinconfig;
152 struct property *prop;
153 const char *function;
154 const char *group;
155 int ret, nmaps, i = 0;
156 u32 val;
157
158 *map = NULL;
159 *num_maps = 0;
160
161 ret = of_property_read_string(node, "allwinner,function", &function);
162 if (ret) {
163 dev_err(pctl->dev,
164 "missing allwinner,function property in node %s\n",
165 node->name);
166 return -EINVAL;
167 }
168
169 nmaps = of_property_count_strings(node, "allwinner,pins") * 2;
170 if (nmaps < 0) {
171 dev_err(pctl->dev,
172 "missing allwinner,pins property in node %s\n",
173 node->name);
174 return -EINVAL;
175 }
176
177 *map = kmalloc(nmaps * sizeof(struct pinctrl_map), GFP_KERNEL);
Sachin Kamat3efa9212013-07-29 13:49:32 +0530178 if (!*map)
Maxime Ripard0e37f882013-01-18 22:30:34 +0100179 return -ENOMEM;
180
181 of_property_for_each_string(node, "allwinner,pins", prop, group) {
182 struct sunxi_pinctrl_group *grp =
183 sunxi_pinctrl_find_group_by_name(pctl, group);
184 int j = 0, configlen = 0;
185
186 if (!grp) {
187 dev_err(pctl->dev, "unknown pin %s", group);
188 continue;
189 }
190
191 if (!sunxi_pinctrl_desc_find_function_by_name(pctl,
192 grp->name,
193 function)) {
194 dev_err(pctl->dev, "unsupported function %s on pin %s",
195 function, group);
196 continue;
197 }
198
199 (*map)[i].type = PIN_MAP_TYPE_MUX_GROUP;
200 (*map)[i].data.mux.group = group;
201 (*map)[i].data.mux.function = function;
202
203 i++;
204
205 (*map)[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
206 (*map)[i].data.configs.group_or_pin = group;
207
208 if (of_find_property(node, "allwinner,drive", NULL))
209 configlen++;
210 if (of_find_property(node, "allwinner,pull", NULL))
211 configlen++;
212
213 pinconfig = kzalloc(configlen * sizeof(*pinconfig), GFP_KERNEL);
214
215 if (!of_property_read_u32(node, "allwinner,drive", &val)) {
216 u16 strength = (val + 1) * 10;
217 pinconfig[j++] =
218 pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH,
219 strength);
220 }
221
222 if (!of_property_read_u32(node, "allwinner,pull", &val)) {
223 enum pin_config_param pull = PIN_CONFIG_END;
224 if (val == 1)
225 pull = PIN_CONFIG_BIAS_PULL_UP;
226 else if (val == 2)
227 pull = PIN_CONFIG_BIAS_PULL_DOWN;
228 pinconfig[j++] = pinconf_to_config_packed(pull, 0);
229 }
230
231 (*map)[i].data.configs.configs = pinconfig;
232 (*map)[i].data.configs.num_configs = configlen;
233
234 i++;
235 }
236
237 *num_maps = nmaps;
238
239 return 0;
240}
241
242static void sunxi_pctrl_dt_free_map(struct pinctrl_dev *pctldev,
243 struct pinctrl_map *map,
244 unsigned num_maps)
245{
246 int i;
247
248 for (i = 0; i < num_maps; i++) {
249 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
250 kfree(map[i].data.configs.configs);
251 }
252
253 kfree(map);
254}
255
Laurent Pinchart022ab142013-02-16 10:25:07 +0100256static const struct pinctrl_ops sunxi_pctrl_ops = {
Maxime Ripard0e37f882013-01-18 22:30:34 +0100257 .dt_node_to_map = sunxi_pctrl_dt_node_to_map,
258 .dt_free_map = sunxi_pctrl_dt_free_map,
259 .get_groups_count = sunxi_pctrl_get_groups_count,
260 .get_group_name = sunxi_pctrl_get_group_name,
261 .get_group_pins = sunxi_pctrl_get_group_pins,
262};
263
264static int sunxi_pconf_group_get(struct pinctrl_dev *pctldev,
265 unsigned group,
266 unsigned long *config)
267{
268 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
269
270 *config = pctl->groups[group].config;
271
272 return 0;
273}
274
275static int sunxi_pconf_group_set(struct pinctrl_dev *pctldev,
276 unsigned group,
Sherman Yin03b054e2013-08-27 11:32:12 -0700277 unsigned long *configs,
278 unsigned num_configs)
Maxime Ripard0e37f882013-01-18 22:30:34 +0100279{
280 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
281 struct sunxi_pinctrl_group *g = &pctl->groups[group];
Maxime Ripard1bee9632013-08-04 12:38:48 +0200282 unsigned long flags;
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800283 unsigned pin = g->pin - pctl->desc->pin_base;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100284 u32 val, mask;
285 u16 strength;
286 u8 dlevel;
Sherman Yin03b054e2013-08-27 11:32:12 -0700287 int i;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100288
Linus Walleij6ad30ce2013-08-29 09:46:30 +0200289 spin_lock_irqsave(&pctl->lock, flags);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200290
Sherman Yin03b054e2013-08-27 11:32:12 -0700291 for (i = 0; i < num_configs; i++) {
292 switch (pinconf_to_config_param(configs[i])) {
293 case PIN_CONFIG_DRIVE_STRENGTH:
294 strength = pinconf_to_config_argument(configs[i]);
Linus Walleij07b7eb92013-08-29 19:17:13 +0200295 if (strength > 40) {
296 spin_unlock_irqrestore(&pctl->lock, flags);
Sherman Yin03b054e2013-08-27 11:32:12 -0700297 return -EINVAL;
Linus Walleij07b7eb92013-08-29 19:17:13 +0200298 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700299 /*
300 * We convert from mA to what the register expects:
301 * 0: 10mA
302 * 1: 20mA
303 * 2: 30mA
304 * 3: 40mA
305 */
306 dlevel = strength / 10 - 1;
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800307 val = readl(pctl->membase + sunxi_dlevel_reg(pin));
308 mask = DLEVEL_PINS_MASK << sunxi_dlevel_offset(pin);
Sherman Yin03b054e2013-08-27 11:32:12 -0700309 writel((val & ~mask)
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800310 | dlevel << sunxi_dlevel_offset(pin),
311 pctl->membase + sunxi_dlevel_reg(pin));
Sherman Yin03b054e2013-08-27 11:32:12 -0700312 break;
313 case PIN_CONFIG_BIAS_PULL_UP:
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800314 val = readl(pctl->membase + sunxi_pull_reg(pin));
315 mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
316 writel((val & ~mask) | 1 << sunxi_pull_offset(pin),
317 pctl->membase + sunxi_pull_reg(pin));
Sherman Yin03b054e2013-08-27 11:32:12 -0700318 break;
319 case PIN_CONFIG_BIAS_PULL_DOWN:
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800320 val = readl(pctl->membase + sunxi_pull_reg(pin));
321 mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
322 writel((val & ~mask) | 2 << sunxi_pull_offset(pin),
323 pctl->membase + sunxi_pull_reg(pin));
Sherman Yin03b054e2013-08-27 11:32:12 -0700324 break;
325 default:
326 break;
327 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700328 /* cache the config value */
329 g->config = configs[i];
330 } /* for each config */
Maxime Ripard0e37f882013-01-18 22:30:34 +0100331
Linus Walleij6ad30ce2013-08-29 09:46:30 +0200332 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100333
334 return 0;
335}
336
Laurent Pinchart022ab142013-02-16 10:25:07 +0100337static const struct pinconf_ops sunxi_pconf_ops = {
Maxime Ripard0e37f882013-01-18 22:30:34 +0100338 .pin_config_group_get = sunxi_pconf_group_get,
339 .pin_config_group_set = sunxi_pconf_group_set,
340};
341
342static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
343{
344 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
345
346 return pctl->nfunctions;
347}
348
349static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev,
350 unsigned function)
351{
352 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
353
354 return pctl->functions[function].name;
355}
356
357static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev,
358 unsigned function,
359 const char * const **groups,
360 unsigned * const num_groups)
361{
362 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
363
364 *groups = pctl->functions[function].groups;
365 *num_groups = pctl->functions[function].ngroups;
366
367 return 0;
368}
369
370static void sunxi_pmx_set(struct pinctrl_dev *pctldev,
371 unsigned pin,
372 u8 config)
373{
374 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200375 unsigned long flags;
376 u32 val, mask;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100377
Maxime Ripard1bee9632013-08-04 12:38:48 +0200378 spin_lock_irqsave(&pctl->lock, flags);
379
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800380 pin -= pctl->desc->pin_base;
Maxime Ripard1bee9632013-08-04 12:38:48 +0200381 val = readl(pctl->membase + sunxi_mux_reg(pin));
382 mask = MUX_PINS_MASK << sunxi_mux_offset(pin);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100383 writel((val & ~mask) | config << sunxi_mux_offset(pin),
384 pctl->membase + sunxi_mux_reg(pin));
Maxime Ripard1bee9632013-08-04 12:38:48 +0200385
386 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100387}
388
389static int sunxi_pmx_enable(struct pinctrl_dev *pctldev,
390 unsigned function,
391 unsigned group)
392{
393 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
394 struct sunxi_pinctrl_group *g = pctl->groups + group;
395 struct sunxi_pinctrl_function *func = pctl->functions + function;
396 struct sunxi_desc_function *desc =
397 sunxi_pinctrl_desc_find_function_by_name(pctl,
398 g->name,
399 func->name);
400
401 if (!desc)
402 return -EINVAL;
403
404 sunxi_pmx_set(pctldev, g->pin, desc->muxval);
405
406 return 0;
407}
408
Maxime Ripard08e9e612013-01-28 21:33:12 +0100409static int
410sunxi_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
411 struct pinctrl_gpio_range *range,
412 unsigned offset,
413 bool input)
414{
415 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
416 struct sunxi_desc_function *desc;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100417 const char *func;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100418
419 if (input)
420 func = "gpio_in";
421 else
422 func = "gpio_out";
423
Maxime Ripard814d4f22013-06-08 12:05:43 +0200424 desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, func);
425 if (!desc)
426 return -EINVAL;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100427
428 sunxi_pmx_set(pctldev, offset, desc->muxval);
429
Maxime Ripard814d4f22013-06-08 12:05:43 +0200430 return 0;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100431}
432
Laurent Pinchart022ab142013-02-16 10:25:07 +0100433static const struct pinmux_ops sunxi_pmx_ops = {
Maxime Ripard0e37f882013-01-18 22:30:34 +0100434 .get_functions_count = sunxi_pmx_get_funcs_cnt,
435 .get_function_name = sunxi_pmx_get_func_name,
436 .get_function_groups = sunxi_pmx_get_func_groups,
437 .enable = sunxi_pmx_enable,
Maxime Ripard08e9e612013-01-28 21:33:12 +0100438 .gpio_set_direction = sunxi_pmx_gpio_set_direction,
Maxime Ripard0e37f882013-01-18 22:30:34 +0100439};
440
Maxime Ripard08e9e612013-01-28 21:33:12 +0100441static int sunxi_pinctrl_gpio_request(struct gpio_chip *chip, unsigned offset)
442{
443 return pinctrl_request_gpio(chip->base + offset);
444}
445
446static void sunxi_pinctrl_gpio_free(struct gpio_chip *chip, unsigned offset)
447{
448 pinctrl_free_gpio(chip->base + offset);
449}
450
451static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip *chip,
452 unsigned offset)
453{
454 return pinctrl_gpio_direction_input(chip->base + offset);
455}
456
457static int sunxi_pinctrl_gpio_get(struct gpio_chip *chip, unsigned offset)
458{
459 struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
460
461 u32 reg = sunxi_data_reg(offset);
462 u8 index = sunxi_data_offset(offset);
463 u32 val = (readl(pctl->membase + reg) >> index) & DATA_PINS_MASK;
464
465 return val;
466}
467
Maxime Ripard08e9e612013-01-28 21:33:12 +0100468static void sunxi_pinctrl_gpio_set(struct gpio_chip *chip,
469 unsigned offset, int value)
470{
471 struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
472 u32 reg = sunxi_data_reg(offset);
473 u8 index = sunxi_data_offset(offset);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200474 unsigned long flags;
475 u32 regval;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100476
Maxime Ripard1bee9632013-08-04 12:38:48 +0200477 spin_lock_irqsave(&pctl->lock, flags);
478
479 regval = readl(pctl->membase + reg);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100480
Maxime Riparddf7b34f2013-07-25 12:41:16 +0200481 if (value)
482 regval |= BIT(index);
483 else
484 regval &= ~(BIT(index));
485
486 writel(regval, pctl->membase + reg);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200487
488 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100489}
490
Chen-Yu Tsaifa8cf572014-01-16 14:34:23 +0800491static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
492 unsigned offset, int value)
493{
494 sunxi_pinctrl_gpio_set(chip, offset, value);
495 return pinctrl_gpio_direction_output(chip->base + offset);
496}
497
Maxime Riparda0d72092013-02-03 12:10:11 +0100498static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
499 const struct of_phandle_args *gpiospec,
500 u32 *flags)
501{
502 int pin, base;
503
504 base = PINS_PER_BANK * gpiospec->args[0];
505 pin = base + gpiospec->args[1];
506
507 if (pin > (gc->base + gc->ngpio))
508 return -EINVAL;
509
510 if (flags)
511 *flags = gpiospec->args[2];
512
513 return pin;
514}
515
Maxime Ripard60242db2013-06-08 12:05:44 +0200516static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
517{
518 struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
519 struct sunxi_desc_function *desc;
520
Axel Linc9e3b2d2013-08-30 16:31:25 +0800521 if (offset >= chip->ngpio)
Maxime Ripard60242db2013-06-08 12:05:44 +0200522 return -ENXIO;
523
524 desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, "irq");
525 if (!desc)
526 return -EINVAL;
527
Maxime Ripard60242db2013-06-08 12:05:44 +0200528 dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
529 chip->label, offset + chip->base, desc->irqnum);
530
531 return irq_find_mapping(pctl->domain, desc->irqnum);
532}
533
Hans de Goedefea6d8e2014-06-29 16:11:00 +0200534static int sunxi_pinctrl_irq_request_resources(struct irq_data *d)
535{
536 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
537 struct sunxi_desc_function *func;
538
539 func = sunxi_pinctrl_desc_find_function_by_pin(pctl,
540 pctl->irq_array[d->hwirq], "irq");
541 if (!func)
542 return -EINVAL;
543
544 /* Change muxing to INT mode */
545 sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval);
546
547 return 0;
548}
Maxime Ripard08e9e612013-01-28 21:33:12 +0100549
Maxime Ripard60242db2013-06-08 12:05:44 +0200550static int sunxi_pinctrl_irq_set_type(struct irq_data *d,
551 unsigned int type)
552{
553 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
554 u32 reg = sunxi_irq_cfg_reg(d->hwirq);
555 u8 index = sunxi_irq_cfg_offset(d->hwirq);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200556 unsigned long flags;
Maxime Ripard2aaaddf2013-08-04 12:38:47 +0200557 u32 regval;
Maxime Ripard60242db2013-06-08 12:05:44 +0200558 u8 mode;
559
560 switch (type) {
561 case IRQ_TYPE_EDGE_RISING:
562 mode = IRQ_EDGE_RISING;
563 break;
564 case IRQ_TYPE_EDGE_FALLING:
565 mode = IRQ_EDGE_FALLING;
566 break;
567 case IRQ_TYPE_EDGE_BOTH:
568 mode = IRQ_EDGE_BOTH;
569 break;
570 case IRQ_TYPE_LEVEL_HIGH:
571 mode = IRQ_LEVEL_HIGH;
572 break;
573 case IRQ_TYPE_LEVEL_LOW:
574 mode = IRQ_LEVEL_LOW;
575 break;
576 default:
577 return -EINVAL;
578 }
579
Maxime Ripard1bee9632013-08-04 12:38:48 +0200580 spin_lock_irqsave(&pctl->lock, flags);
581
Maxime Ripard2aaaddf2013-08-04 12:38:47 +0200582 regval = readl(pctl->membase + reg);
Hans de Goeded82f9402014-02-17 22:19:43 +0100583 regval &= ~(IRQ_CFG_IRQ_MASK << index);
Maxime Ripard2aaaddf2013-08-04 12:38:47 +0200584 writel(regval | (mode << index), pctl->membase + reg);
Maxime Ripard60242db2013-06-08 12:05:44 +0200585
Maxime Ripard1bee9632013-08-04 12:38:48 +0200586 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard60242db2013-06-08 12:05:44 +0200587
588 return 0;
589}
590
Maxime Ripard645ec712014-06-05 15:26:00 +0200591static void sunxi_pinctrl_irq_ack(struct irq_data *d)
Maxime Ripard60242db2013-06-08 12:05:44 +0200592{
593 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
Maxime Ripard60242db2013-06-08 12:05:44 +0200594 u32 status_reg = sunxi_irq_status_reg(d->hwirq);
595 u8 status_idx = sunxi_irq_status_offset(d->hwirq);
Maxime Ripard60242db2013-06-08 12:05:44 +0200596
597 /* Clear the IRQ */
598 writel(1 << status_idx, pctl->membase + status_reg);
599}
600
601static void sunxi_pinctrl_irq_mask(struct irq_data *d)
602{
603 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
604 u32 reg = sunxi_irq_ctrl_reg(d->hwirq);
605 u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200606 unsigned long flags;
Maxime Ripard60242db2013-06-08 12:05:44 +0200607 u32 val;
608
Maxime Ripard1bee9632013-08-04 12:38:48 +0200609 spin_lock_irqsave(&pctl->lock, flags);
610
Maxime Ripard60242db2013-06-08 12:05:44 +0200611 /* Mask the IRQ */
612 val = readl(pctl->membase + reg);
613 writel(val & ~(1 << idx), pctl->membase + reg);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200614
615 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard60242db2013-06-08 12:05:44 +0200616}
617
618static void sunxi_pinctrl_irq_unmask(struct irq_data *d)
619{
620 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
Maxime Ripard60242db2013-06-08 12:05:44 +0200621 u32 reg = sunxi_irq_ctrl_reg(d->hwirq);
622 u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200623 unsigned long flags;
Maxime Ripard60242db2013-06-08 12:05:44 +0200624 u32 val;
625
Maxime Ripard1bee9632013-08-04 12:38:48 +0200626 spin_lock_irqsave(&pctl->lock, flags);
627
Maxime Ripard60242db2013-06-08 12:05:44 +0200628 /* Unmask the IRQ */
629 val = readl(pctl->membase + reg);
630 writel(val | (1 << idx), pctl->membase + reg);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200631
632 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard60242db2013-06-08 12:05:44 +0200633}
634
635static struct irq_chip sunxi_pinctrl_irq_chip = {
Maxime Ripard645ec712014-06-05 15:26:00 +0200636 .irq_ack = sunxi_pinctrl_irq_ack,
Maxime Ripard60242db2013-06-08 12:05:44 +0200637 .irq_mask = sunxi_pinctrl_irq_mask,
Maxime Ripard60242db2013-06-08 12:05:44 +0200638 .irq_unmask = sunxi_pinctrl_irq_unmask,
Hans de Goedefea6d8e2014-06-29 16:11:00 +0200639 .irq_request_resources = sunxi_pinctrl_irq_request_resources,
Maxime Ripard60242db2013-06-08 12:05:44 +0200640 .irq_set_type = sunxi_pinctrl_irq_set_type,
Chen-Yu Tsai578c0a82014-06-29 16:10:59 +0200641 .flags = IRQCHIP_SKIP_SET_WAKE,
Maxime Ripard60242db2013-06-08 12:05:44 +0200642};
643
644static void sunxi_pinctrl_irq_handler(unsigned irq, struct irq_desc *desc)
645{
Chen-Yu Tsai905a5112014-02-11 00:22:37 +0800646 struct irq_chip *chip = irq_get_chip(irq);
Maxime Ripard60242db2013-06-08 12:05:44 +0200647 struct sunxi_pinctrl *pctl = irq_get_handler_data(irq);
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200648 unsigned long bank, reg, val;
649
650 for (bank = 0; bank < pctl->desc->irq_banks; bank++)
651 if (irq == pctl->irq[bank])
652 break;
653
654 if (bank == pctl->desc->irq_banks)
655 return;
656
657 reg = sunxi_irq_status_reg_from_bank(bank);
658 val = readl(pctl->membase + reg);
Maxime Ripard60242db2013-06-08 12:05:44 +0200659
660 /* Clear all interrupts */
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200661 writel(val, pctl->membase + reg);
Maxime Ripard60242db2013-06-08 12:05:44 +0200662
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200663 if (val) {
Maxime Ripard60242db2013-06-08 12:05:44 +0200664 int irqoffset;
665
Chen-Yu Tsai905a5112014-02-11 00:22:37 +0800666 chained_irq_enter(chip, desc);
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200667 for_each_set_bit(irqoffset, &val, IRQ_PER_BANK) {
668 int pin_irq = irq_find_mapping(pctl->domain,
669 bank * IRQ_PER_BANK + irqoffset);
Maxime Ripard60242db2013-06-08 12:05:44 +0200670 generic_handle_irq(pin_irq);
671 }
Chen-Yu Tsai905a5112014-02-11 00:22:37 +0800672 chained_irq_exit(chip, desc);
Maxime Ripard60242db2013-06-08 12:05:44 +0200673 }
674}
675
Maxime Ripard0e37f882013-01-18 22:30:34 +0100676static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
677 const char *name)
678{
679 struct sunxi_pinctrl_function *func = pctl->functions;
680
681 while (func->name) {
682 /* function already there */
683 if (strcmp(func->name, name) == 0) {
684 func->ngroups++;
685 return -EEXIST;
686 }
687 func++;
688 }
689
690 func->name = name;
691 func->ngroups = 1;
692
693 pctl->nfunctions++;
694
695 return 0;
696}
697
698static int sunxi_pinctrl_build_state(struct platform_device *pdev)
699{
700 struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
701 int i;
702
703 pctl->ngroups = pctl->desc->npins;
704
705 /* Allocate groups */
706 pctl->groups = devm_kzalloc(&pdev->dev,
707 pctl->ngroups * sizeof(*pctl->groups),
708 GFP_KERNEL);
709 if (!pctl->groups)
710 return -ENOMEM;
711
712 for (i = 0; i < pctl->desc->npins; i++) {
713 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
714 struct sunxi_pinctrl_group *group = pctl->groups + i;
715
716 group->name = pin->pin.name;
717 group->pin = pin->pin.number;
718 }
719
720 /*
721 * We suppose that we won't have any more functions than pins,
722 * we'll reallocate that later anyway
723 */
724 pctl->functions = devm_kzalloc(&pdev->dev,
725 pctl->desc->npins * sizeof(*pctl->functions),
726 GFP_KERNEL);
727 if (!pctl->functions)
728 return -ENOMEM;
729
730 /* Count functions and their associated groups */
731 for (i = 0; i < pctl->desc->npins; i++) {
732 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
733 struct sunxi_desc_function *func = pin->functions;
734
735 while (func->name) {
Chen-Yu Tsaid54e9a22014-05-26 09:47:56 +0200736 /* Create interrupt mapping while we're at it */
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200737 if (!strcmp(func->name, "irq")) {
738 int irqnum = func->irqnum + func->irqbank * IRQ_PER_BANK;
739 pctl->irq_array[irqnum] = pin->pin.number;
740 }
741
Maxime Ripard0e37f882013-01-18 22:30:34 +0100742 sunxi_pinctrl_add_function(pctl, func->name);
743 func++;
744 }
745 }
746
747 pctl->functions = krealloc(pctl->functions,
748 pctl->nfunctions * sizeof(*pctl->functions),
749 GFP_KERNEL);
750
751 for (i = 0; i < pctl->desc->npins; i++) {
752 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
753 struct sunxi_desc_function *func = pin->functions;
754
755 while (func->name) {
756 struct sunxi_pinctrl_function *func_item;
757 const char **func_grp;
758
759 func_item = sunxi_pinctrl_find_function_by_name(pctl,
760 func->name);
761 if (!func_item)
762 return -EINVAL;
763
764 if (!func_item->groups) {
765 func_item->groups =
766 devm_kzalloc(&pdev->dev,
767 func_item->ngroups * sizeof(*func_item->groups),
768 GFP_KERNEL);
769 if (!func_item->groups)
770 return -ENOMEM;
771 }
772
773 func_grp = func_item->groups;
774 while (*func_grp)
775 func_grp++;
776
777 *func_grp = pin->pin.name;
778 func++;
779 }
780 }
781
782 return 0;
783}
784
Maxime Ripard2284ba62014-04-18 20:10:41 +0200785int sunxi_pinctrl_init(struct platform_device *pdev,
786 const struct sunxi_pinctrl_desc *desc)
Maxime Ripard0e37f882013-01-18 22:30:34 +0100787{
788 struct device_node *node = pdev->dev.of_node;
Maxime Ripardba6764d2014-05-22 16:25:27 +0200789 struct pinctrl_desc *pctrl_desc;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100790 struct pinctrl_pin_desc *pins;
791 struct sunxi_pinctrl *pctl;
Maxime Ripard4409caf2014-04-26 21:59:50 +0200792 struct resource *res;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100793 int i, ret, last_pin;
Emilio López950707c2013-03-22 11:20:40 -0300794 struct clk *clk;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100795
796 pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
797 if (!pctl)
798 return -ENOMEM;
799 platform_set_drvdata(pdev, pctl);
800
Maxime Ripard1bee9632013-08-04 12:38:48 +0200801 spin_lock_init(&pctl->lock);
802
Maxime Ripard4409caf2014-04-26 21:59:50 +0200803 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
804 pctl->membase = devm_ioremap_resource(&pdev->dev, res);
805 if (IS_ERR(pctl->membase))
806 return PTR_ERR(pctl->membase);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100807
Maxime Ripardba6764d2014-05-22 16:25:27 +0200808 pctl->dev = &pdev->dev;
Maxime Ripard2284ba62014-04-18 20:10:41 +0200809 pctl->desc = desc;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100810
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200811 pctl->irq_array = devm_kcalloc(&pdev->dev,
812 IRQ_PER_BANK * pctl->desc->irq_banks,
813 sizeof(*pctl->irq_array),
814 GFP_KERNEL);
815 if (!pctl->irq_array)
816 return -ENOMEM;
817
Maxime Ripard0e37f882013-01-18 22:30:34 +0100818 ret = sunxi_pinctrl_build_state(pdev);
819 if (ret) {
820 dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
821 return ret;
822 }
823
824 pins = devm_kzalloc(&pdev->dev,
825 pctl->desc->npins * sizeof(*pins),
826 GFP_KERNEL);
827 if (!pins)
828 return -ENOMEM;
829
830 for (i = 0; i < pctl->desc->npins; i++)
831 pins[i] = pctl->desc->pins[i].pin;
832
Maxime Ripardba6764d2014-05-22 16:25:27 +0200833 pctrl_desc = devm_kzalloc(&pdev->dev,
834 sizeof(*pctrl_desc),
835 GFP_KERNEL);
836 if (!pctrl_desc)
837 return -ENOMEM;
838
839 pctrl_desc->name = dev_name(&pdev->dev);
840 pctrl_desc->owner = THIS_MODULE;
841 pctrl_desc->pins = pins;
842 pctrl_desc->npins = pctl->desc->npins;
843 pctrl_desc->confops = &sunxi_pconf_ops;
844 pctrl_desc->pctlops = &sunxi_pctrl_ops;
845 pctrl_desc->pmxops = &sunxi_pmx_ops;
846
847 pctl->pctl_dev = pinctrl_register(pctrl_desc,
Maxime Ripard0e37f882013-01-18 22:30:34 +0100848 &pdev->dev, pctl);
849 if (!pctl->pctl_dev) {
850 dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
851 return -EINVAL;
852 }
853
Maxime Ripard08e9e612013-01-28 21:33:12 +0100854 pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
855 if (!pctl->chip) {
856 ret = -ENOMEM;
857 goto pinctrl_error;
858 }
859
860 last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
Boris BREZILLONd83c82c2014-04-10 15:52:43 +0200861 pctl->chip->owner = THIS_MODULE;
862 pctl->chip->request = sunxi_pinctrl_gpio_request,
863 pctl->chip->free = sunxi_pinctrl_gpio_free,
864 pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input,
865 pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output,
866 pctl->chip->get = sunxi_pinctrl_gpio_get,
867 pctl->chip->set = sunxi_pinctrl_gpio_set,
868 pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate,
869 pctl->chip->to_irq = sunxi_pinctrl_gpio_to_irq,
870 pctl->chip->of_gpio_n_cells = 3,
871 pctl->chip->can_sleep = false,
872 pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK) -
873 pctl->desc->pin_base;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100874 pctl->chip->label = dev_name(&pdev->dev);
875 pctl->chip->dev = &pdev->dev;
Boris BREZILLONd83c82c2014-04-10 15:52:43 +0200876 pctl->chip->base = pctl->desc->pin_base;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100877
878 ret = gpiochip_add(pctl->chip);
879 if (ret)
880 goto pinctrl_error;
881
882 for (i = 0; i < pctl->desc->npins; i++) {
883 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
884
885 ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
886 pin->pin.number,
887 pin->pin.number, 1);
888 if (ret)
889 goto gpiochip_error;
890 }
891
Emilio López950707c2013-03-22 11:20:40 -0300892 clk = devm_clk_get(&pdev->dev, NULL);
Wei Yongjund72f88a2013-05-23 17:32:14 +0800893 if (IS_ERR(clk)) {
894 ret = PTR_ERR(clk);
Emilio López950707c2013-03-22 11:20:40 -0300895 goto gpiochip_error;
Wei Yongjund72f88a2013-05-23 17:32:14 +0800896 }
Emilio López950707c2013-03-22 11:20:40 -0300897
Boris BREZILLON64150932014-04-10 15:52:40 +0200898 ret = clk_prepare_enable(clk);
899 if (ret)
900 goto gpiochip_error;
Emilio López950707c2013-03-22 11:20:40 -0300901
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200902 pctl->irq = devm_kcalloc(&pdev->dev,
903 pctl->desc->irq_banks,
904 sizeof(*pctl->irq),
905 GFP_KERNEL);
Maxime Ripard60242db2013-06-08 12:05:44 +0200906 if (!pctl->irq) {
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200907 ret = -ENOMEM;
Maxime Riparddc969102014-04-26 22:28:54 +0200908 goto clk_error;
Maxime Ripard60242db2013-06-08 12:05:44 +0200909 }
910
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200911 for (i = 0; i < pctl->desc->irq_banks; i++) {
912 pctl->irq[i] = platform_get_irq(pdev, i);
913 if (pctl->irq[i] < 0) {
914 ret = pctl->irq[i];
915 goto clk_error;
916 }
917 }
918
919 pctl->domain = irq_domain_add_linear(node,
920 pctl->desc->irq_banks * IRQ_PER_BANK,
921 &irq_domain_simple_ops,
922 NULL);
Maxime Ripard60242db2013-06-08 12:05:44 +0200923 if (!pctl->domain) {
924 dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
925 ret = -ENOMEM;
Maxime Riparddc969102014-04-26 22:28:54 +0200926 goto clk_error;
Maxime Ripard60242db2013-06-08 12:05:44 +0200927 }
928
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200929 for (i = 0; i < (pctl->desc->irq_banks * IRQ_PER_BANK); i++) {
Maxime Ripard60242db2013-06-08 12:05:44 +0200930 int irqno = irq_create_mapping(pctl->domain, i);
931
932 irq_set_chip_and_handler(irqno, &sunxi_pinctrl_irq_chip,
933 handle_simple_irq);
934 irq_set_chip_data(irqno, pctl);
935 };
936
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200937 for (i = 0; i < pctl->desc->irq_banks; i++) {
938 irq_set_chained_handler(pctl->irq[i],
939 sunxi_pinctrl_irq_handler);
940 irq_set_handler_data(pctl->irq[i], pctl);
941 }
Maxime Ripard60242db2013-06-08 12:05:44 +0200942
Maxime Ripard08e9e612013-01-28 21:33:12 +0100943 dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
Maxime Ripard0e37f882013-01-18 22:30:34 +0100944
945 return 0;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100946
Boris BREZILLONe2bddc62014-04-10 15:52:41 +0200947clk_error:
948 clk_disable_unprepare(clk);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100949gpiochip_error:
Axel Lin97fc4632013-05-19 13:58:37 +0800950 if (gpiochip_remove(pctl->chip))
951 dev_err(&pdev->dev, "failed to remove gpio chip\n");
Maxime Ripard08e9e612013-01-28 21:33:12 +0100952pinctrl_error:
953 pinctrl_unregister(pctl->pctl_dev);
954 return ret;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100955}