blob: f9c7a05541d814f7fc12ae4c09e9583fee81828d [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"
Hans de Goedeef6d24c2015-03-08 22:13:57 +010032#include "../../gpio/gpiolib.h"
Maxime Ripard0e37f882013-01-18 22:30:34 +010033#include "pinctrl-sunxi.h"
Maxime Ripardeaa3d842013-01-18 22:30:35 +010034
Hans de Goedef4c51c12014-06-29 16:11:01 +020035static struct irq_chip sunxi_pinctrl_edge_irq_chip;
36static struct irq_chip sunxi_pinctrl_level_irq_chip;
37
Maxime Ripard0e37f882013-01-18 22:30:34 +010038static struct sunxi_pinctrl_group *
39sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl *pctl, const char *group)
40{
41 int i;
42
43 for (i = 0; i < pctl->ngroups; i++) {
44 struct sunxi_pinctrl_group *grp = pctl->groups + i;
45
46 if (!strcmp(grp->name, group))
47 return grp;
48 }
49
50 return NULL;
51}
52
53static struct sunxi_pinctrl_function *
54sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl *pctl,
55 const char *name)
56{
57 struct sunxi_pinctrl_function *func = pctl->functions;
58 int i;
59
60 for (i = 0; i < pctl->nfunctions; i++) {
61 if (!func[i].name)
62 break;
63
64 if (!strcmp(func[i].name, name))
65 return func + i;
66 }
67
68 return NULL;
69}
70
71static struct sunxi_desc_function *
72sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl *pctl,
73 const char *pin_name,
74 const char *func_name)
75{
76 int i;
77
78 for (i = 0; i < pctl->desc->npins; i++) {
79 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
80
81 if (!strcmp(pin->pin.name, pin_name)) {
82 struct sunxi_desc_function *func = pin->functions;
83
84 while (func->name) {
85 if (!strcmp(func->name, func_name))
86 return func;
87
88 func++;
89 }
90 }
91 }
92
93 return NULL;
94}
95
Maxime Ripard814d4f22013-06-08 12:05:43 +020096static struct sunxi_desc_function *
97sunxi_pinctrl_desc_find_function_by_pin(struct sunxi_pinctrl *pctl,
98 const u16 pin_num,
99 const char *func_name)
100{
101 int i;
102
103 for (i = 0; i < pctl->desc->npins; i++) {
104 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
105
106 if (pin->pin.number == pin_num) {
107 struct sunxi_desc_function *func = pin->functions;
108
109 while (func->name) {
110 if (!strcmp(func->name, func_name))
111 return func;
112
113 func++;
114 }
115 }
116 }
117
118 return NULL;
119}
120
Maxime Ripard0e37f882013-01-18 22:30:34 +0100121static int sunxi_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
122{
123 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
124
125 return pctl->ngroups;
126}
127
128static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev *pctldev,
129 unsigned group)
130{
131 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
132
133 return pctl->groups[group].name;
134}
135
136static int sunxi_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
137 unsigned group,
138 const unsigned **pins,
139 unsigned *num_pins)
140{
141 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
142
143 *pins = (unsigned *)&pctl->groups[group].pin;
144 *num_pins = 1;
145
146 return 0;
147}
148
149static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
150 struct device_node *node,
151 struct pinctrl_map **map,
152 unsigned *num_maps)
153{
154 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
155 unsigned long *pinconfig;
156 struct property *prop;
157 const char *function;
158 const char *group;
159 int ret, nmaps, i = 0;
160 u32 val;
161
162 *map = NULL;
163 *num_maps = 0;
164
165 ret = of_property_read_string(node, "allwinner,function", &function);
166 if (ret) {
167 dev_err(pctl->dev,
168 "missing allwinner,function property in node %s\n",
169 node->name);
170 return -EINVAL;
171 }
172
173 nmaps = of_property_count_strings(node, "allwinner,pins") * 2;
174 if (nmaps < 0) {
175 dev_err(pctl->dev,
176 "missing allwinner,pins property in node %s\n",
177 node->name);
178 return -EINVAL;
179 }
180
181 *map = kmalloc(nmaps * sizeof(struct pinctrl_map), GFP_KERNEL);
Sachin Kamat3efa9212013-07-29 13:49:32 +0530182 if (!*map)
Maxime Ripard0e37f882013-01-18 22:30:34 +0100183 return -ENOMEM;
184
185 of_property_for_each_string(node, "allwinner,pins", prop, group) {
186 struct sunxi_pinctrl_group *grp =
187 sunxi_pinctrl_find_group_by_name(pctl, group);
188 int j = 0, configlen = 0;
189
190 if (!grp) {
191 dev_err(pctl->dev, "unknown pin %s", group);
192 continue;
193 }
194
195 if (!sunxi_pinctrl_desc_find_function_by_name(pctl,
196 grp->name,
197 function)) {
198 dev_err(pctl->dev, "unsupported function %s on pin %s",
199 function, group);
200 continue;
201 }
202
203 (*map)[i].type = PIN_MAP_TYPE_MUX_GROUP;
204 (*map)[i].data.mux.group = group;
205 (*map)[i].data.mux.function = function;
206
207 i++;
208
209 (*map)[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
210 (*map)[i].data.configs.group_or_pin = group;
211
212 if (of_find_property(node, "allwinner,drive", NULL))
213 configlen++;
214 if (of_find_property(node, "allwinner,pull", NULL))
215 configlen++;
216
217 pinconfig = kzalloc(configlen * sizeof(*pinconfig), GFP_KERNEL);
Sachin Kamatbd078942014-05-30 15:50:52 +0530218 if (!pinconfig) {
219 kfree(*map);
220 return -ENOMEM;
221 }
Maxime Ripard0e37f882013-01-18 22:30:34 +0100222
223 if (!of_property_read_u32(node, "allwinner,drive", &val)) {
224 u16 strength = (val + 1) * 10;
225 pinconfig[j++] =
226 pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH,
227 strength);
228 }
229
230 if (!of_property_read_u32(node, "allwinner,pull", &val)) {
231 enum pin_config_param pull = PIN_CONFIG_END;
232 if (val == 1)
233 pull = PIN_CONFIG_BIAS_PULL_UP;
234 else if (val == 2)
235 pull = PIN_CONFIG_BIAS_PULL_DOWN;
236 pinconfig[j++] = pinconf_to_config_packed(pull, 0);
237 }
238
239 (*map)[i].data.configs.configs = pinconfig;
240 (*map)[i].data.configs.num_configs = configlen;
241
242 i++;
243 }
244
245 *num_maps = nmaps;
246
247 return 0;
248}
249
250static void sunxi_pctrl_dt_free_map(struct pinctrl_dev *pctldev,
251 struct pinctrl_map *map,
252 unsigned num_maps)
253{
254 int i;
255
256 for (i = 0; i < num_maps; i++) {
257 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
258 kfree(map[i].data.configs.configs);
259 }
260
261 kfree(map);
262}
263
Laurent Pinchart022ab142013-02-16 10:25:07 +0100264static const struct pinctrl_ops sunxi_pctrl_ops = {
Maxime Ripard0e37f882013-01-18 22:30:34 +0100265 .dt_node_to_map = sunxi_pctrl_dt_node_to_map,
266 .dt_free_map = sunxi_pctrl_dt_free_map,
267 .get_groups_count = sunxi_pctrl_get_groups_count,
268 .get_group_name = sunxi_pctrl_get_group_name,
269 .get_group_pins = sunxi_pctrl_get_group_pins,
270};
271
272static int sunxi_pconf_group_get(struct pinctrl_dev *pctldev,
273 unsigned group,
274 unsigned long *config)
275{
276 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
277
278 *config = pctl->groups[group].config;
279
280 return 0;
281}
282
283static int sunxi_pconf_group_set(struct pinctrl_dev *pctldev,
284 unsigned group,
Sherman Yin03b054e2013-08-27 11:32:12 -0700285 unsigned long *configs,
286 unsigned num_configs)
Maxime Ripard0e37f882013-01-18 22:30:34 +0100287{
288 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
289 struct sunxi_pinctrl_group *g = &pctl->groups[group];
Maxime Ripard1bee9632013-08-04 12:38:48 +0200290 unsigned long flags;
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800291 unsigned pin = g->pin - pctl->desc->pin_base;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100292 u32 val, mask;
293 u16 strength;
294 u8 dlevel;
Sherman Yin03b054e2013-08-27 11:32:12 -0700295 int i;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100296
Linus Walleij6ad30ce2013-08-29 09:46:30 +0200297 spin_lock_irqsave(&pctl->lock, flags);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200298
Sherman Yin03b054e2013-08-27 11:32:12 -0700299 for (i = 0; i < num_configs; i++) {
300 switch (pinconf_to_config_param(configs[i])) {
301 case PIN_CONFIG_DRIVE_STRENGTH:
302 strength = pinconf_to_config_argument(configs[i]);
Linus Walleij07b7eb92013-08-29 19:17:13 +0200303 if (strength > 40) {
304 spin_unlock_irqrestore(&pctl->lock, flags);
Sherman Yin03b054e2013-08-27 11:32:12 -0700305 return -EINVAL;
Linus Walleij07b7eb92013-08-29 19:17:13 +0200306 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700307 /*
308 * We convert from mA to what the register expects:
309 * 0: 10mA
310 * 1: 20mA
311 * 2: 30mA
312 * 3: 40mA
313 */
314 dlevel = strength / 10 - 1;
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800315 val = readl(pctl->membase + sunxi_dlevel_reg(pin));
316 mask = DLEVEL_PINS_MASK << sunxi_dlevel_offset(pin);
Sherman Yin03b054e2013-08-27 11:32:12 -0700317 writel((val & ~mask)
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800318 | dlevel << sunxi_dlevel_offset(pin),
319 pctl->membase + sunxi_dlevel_reg(pin));
Sherman Yin03b054e2013-08-27 11:32:12 -0700320 break;
321 case PIN_CONFIG_BIAS_PULL_UP:
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800322 val = readl(pctl->membase + sunxi_pull_reg(pin));
323 mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
324 writel((val & ~mask) | 1 << sunxi_pull_offset(pin),
325 pctl->membase + sunxi_pull_reg(pin));
Sherman Yin03b054e2013-08-27 11:32:12 -0700326 break;
327 case PIN_CONFIG_BIAS_PULL_DOWN:
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800328 val = readl(pctl->membase + sunxi_pull_reg(pin));
329 mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
330 writel((val & ~mask) | 2 << sunxi_pull_offset(pin),
331 pctl->membase + sunxi_pull_reg(pin));
Sherman Yin03b054e2013-08-27 11:32:12 -0700332 break;
333 default:
334 break;
335 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700336 /* cache the config value */
337 g->config = configs[i];
338 } /* for each config */
Maxime Ripard0e37f882013-01-18 22:30:34 +0100339
Linus Walleij6ad30ce2013-08-29 09:46:30 +0200340 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100341
342 return 0;
343}
344
Laurent Pinchart022ab142013-02-16 10:25:07 +0100345static const struct pinconf_ops sunxi_pconf_ops = {
Maxime Ripard0e37f882013-01-18 22:30:34 +0100346 .pin_config_group_get = sunxi_pconf_group_get,
347 .pin_config_group_set = sunxi_pconf_group_set,
348};
349
350static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
351{
352 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
353
354 return pctl->nfunctions;
355}
356
357static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev,
358 unsigned function)
359{
360 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
361
362 return pctl->functions[function].name;
363}
364
365static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev,
366 unsigned function,
367 const char * const **groups,
368 unsigned * const num_groups)
369{
370 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
371
372 *groups = pctl->functions[function].groups;
373 *num_groups = pctl->functions[function].ngroups;
374
375 return 0;
376}
377
378static void sunxi_pmx_set(struct pinctrl_dev *pctldev,
379 unsigned pin,
380 u8 config)
381{
382 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200383 unsigned long flags;
384 u32 val, mask;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100385
Maxime Ripard1bee9632013-08-04 12:38:48 +0200386 spin_lock_irqsave(&pctl->lock, flags);
387
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800388 pin -= pctl->desc->pin_base;
Maxime Ripard1bee9632013-08-04 12:38:48 +0200389 val = readl(pctl->membase + sunxi_mux_reg(pin));
390 mask = MUX_PINS_MASK << sunxi_mux_offset(pin);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100391 writel((val & ~mask) | config << sunxi_mux_offset(pin),
392 pctl->membase + sunxi_mux_reg(pin));
Maxime Ripard1bee9632013-08-04 12:38:48 +0200393
394 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100395}
396
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200397static int sunxi_pmx_set_mux(struct pinctrl_dev *pctldev,
398 unsigned function,
399 unsigned group)
Maxime Ripard0e37f882013-01-18 22:30:34 +0100400{
401 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
402 struct sunxi_pinctrl_group *g = pctl->groups + group;
403 struct sunxi_pinctrl_function *func = pctl->functions + function;
404 struct sunxi_desc_function *desc =
405 sunxi_pinctrl_desc_find_function_by_name(pctl,
406 g->name,
407 func->name);
408
409 if (!desc)
410 return -EINVAL;
411
412 sunxi_pmx_set(pctldev, g->pin, desc->muxval);
413
414 return 0;
415}
416
Maxime Ripard08e9e612013-01-28 21:33:12 +0100417static int
418sunxi_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
419 struct pinctrl_gpio_range *range,
420 unsigned offset,
421 bool input)
422{
423 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
424 struct sunxi_desc_function *desc;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100425 const char *func;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100426
427 if (input)
428 func = "gpio_in";
429 else
430 func = "gpio_out";
431
Maxime Ripard814d4f22013-06-08 12:05:43 +0200432 desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, func);
433 if (!desc)
434 return -EINVAL;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100435
436 sunxi_pmx_set(pctldev, offset, desc->muxval);
437
Maxime Ripard814d4f22013-06-08 12:05:43 +0200438 return 0;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100439}
440
Laurent Pinchart022ab142013-02-16 10:25:07 +0100441static const struct pinmux_ops sunxi_pmx_ops = {
Maxime Ripard0e37f882013-01-18 22:30:34 +0100442 .get_functions_count = sunxi_pmx_get_funcs_cnt,
443 .get_function_name = sunxi_pmx_get_func_name,
444 .get_function_groups = sunxi_pmx_get_func_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200445 .set_mux = sunxi_pmx_set_mux,
Maxime Ripard08e9e612013-01-28 21:33:12 +0100446 .gpio_set_direction = sunxi_pmx_gpio_set_direction,
Maxime Ripard0e37f882013-01-18 22:30:34 +0100447};
448
Maxime Ripard08e9e612013-01-28 21:33:12 +0100449static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip *chip,
450 unsigned offset)
451{
452 return pinctrl_gpio_direction_input(chip->base + offset);
453}
454
455static int sunxi_pinctrl_gpio_get(struct gpio_chip *chip, unsigned offset)
456{
457 struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100458 u32 reg = sunxi_data_reg(offset);
459 u8 index = sunxi_data_offset(offset);
Hans de Goedeef6d24c2015-03-08 22:13:57 +0100460 u32 set_mux = pctl->desc->irq_read_needs_mux &&
461 test_bit(FLAG_USED_AS_IRQ, &chip->desc[offset].flags);
462 u32 val;
463
464 if (set_mux)
465 sunxi_pmx_set(pctl->pctl_dev, offset, SUN4I_FUNC_INPUT);
466
467 val = (readl(pctl->membase + reg) >> index) & DATA_PINS_MASK;
468
469 if (set_mux)
470 sunxi_pmx_set(pctl->pctl_dev, offset, SUN4I_FUNC_IRQ);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100471
472 return val;
473}
474
Maxime Ripard08e9e612013-01-28 21:33:12 +0100475static void sunxi_pinctrl_gpio_set(struct gpio_chip *chip,
476 unsigned offset, int value)
477{
478 struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
479 u32 reg = sunxi_data_reg(offset);
480 u8 index = sunxi_data_offset(offset);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200481 unsigned long flags;
482 u32 regval;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100483
Maxime Ripard1bee9632013-08-04 12:38:48 +0200484 spin_lock_irqsave(&pctl->lock, flags);
485
486 regval = readl(pctl->membase + reg);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100487
Maxime Riparddf7b34f2013-07-25 12:41:16 +0200488 if (value)
489 regval |= BIT(index);
490 else
491 regval &= ~(BIT(index));
492
493 writel(regval, pctl->membase + reg);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200494
495 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100496}
497
Chen-Yu Tsaifa8cf572014-01-16 14:34:23 +0800498static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
499 unsigned offset, int value)
500{
501 sunxi_pinctrl_gpio_set(chip, offset, value);
502 return pinctrl_gpio_direction_output(chip->base + offset);
503}
504
Maxime Riparda0d72092013-02-03 12:10:11 +0100505static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
506 const struct of_phandle_args *gpiospec,
507 u32 *flags)
508{
509 int pin, base;
510
511 base = PINS_PER_BANK * gpiospec->args[0];
512 pin = base + gpiospec->args[1];
513
Chen-Yu Tsai343f1322014-07-15 01:24:37 +0800514 if (pin > gc->ngpio)
Maxime Riparda0d72092013-02-03 12:10:11 +0100515 return -EINVAL;
516
517 if (flags)
518 *flags = gpiospec->args[2];
519
520 return pin;
521}
522
Maxime Ripard60242db2013-06-08 12:05:44 +0200523static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
524{
525 struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
526 struct sunxi_desc_function *desc;
Chen-Yu Tsai343f1322014-07-15 01:24:37 +0800527 unsigned pinnum = pctl->desc->pin_base + offset;
Chen-Yu Tsai0d3bafa2014-07-01 00:04:59 +0800528 unsigned irqnum;
Maxime Ripard60242db2013-06-08 12:05:44 +0200529
Axel Linc9e3b2d2013-08-30 16:31:25 +0800530 if (offset >= chip->ngpio)
Maxime Ripard60242db2013-06-08 12:05:44 +0200531 return -ENXIO;
532
Chen-Yu Tsai343f1322014-07-15 01:24:37 +0800533 desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pinnum, "irq");
Maxime Ripard60242db2013-06-08 12:05:44 +0200534 if (!desc)
535 return -EINVAL;
536
Chen-Yu Tsai0d3bafa2014-07-01 00:04:59 +0800537 irqnum = desc->irqbank * IRQ_PER_BANK + desc->irqnum;
Maxime Ripard60242db2013-06-08 12:05:44 +0200538
Chen-Yu Tsai0d3bafa2014-07-01 00:04:59 +0800539 dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
540 chip->label, offset + chip->base, irqnum);
541
542 return irq_find_mapping(pctl->domain, irqnum);
Maxime Ripard60242db2013-06-08 12:05:44 +0200543}
544
Hans de Goedefea6d8e2014-06-29 16:11:00 +0200545static int sunxi_pinctrl_irq_request_resources(struct irq_data *d)
Maxime Ripard60242db2013-06-08 12:05:44 +0200546{
547 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
Hans de Goedefea6d8e2014-06-29 16:11:00 +0200548 struct sunxi_desc_function *func;
Chen-Yu Tsaif83549d2014-07-15 01:24:36 +0800549 int ret;
Hans de Goedefea6d8e2014-06-29 16:11:00 +0200550
551 func = sunxi_pinctrl_desc_find_function_by_pin(pctl,
552 pctl->irq_array[d->hwirq], "irq");
553 if (!func)
554 return -EINVAL;
555
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900556 ret = gpiochip_lock_as_irq(pctl->chip,
Chen-Yu Tsai343f1322014-07-15 01:24:37 +0800557 pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
Chen-Yu Tsaif83549d2014-07-15 01:24:36 +0800558 if (ret) {
559 dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
560 irqd_to_hwirq(d));
561 return ret;
562 }
563
Hans de Goedefea6d8e2014-06-29 16:11:00 +0200564 /* Change muxing to INT mode */
565 sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval);
566
567 return 0;
568}
Maxime Ripard08e9e612013-01-28 21:33:12 +0100569
Chen-Yu Tsaif83549d2014-07-15 01:24:36 +0800570static void sunxi_pinctrl_irq_release_resources(struct irq_data *d)
571{
572 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
573
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900574 gpiochip_unlock_as_irq(pctl->chip,
575 pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
Chen-Yu Tsaif83549d2014-07-15 01:24:36 +0800576}
577
Hans de Goedef4c51c12014-06-29 16:11:01 +0200578static int sunxi_pinctrl_irq_set_type(struct irq_data *d, unsigned int type)
Maxime Ripard60242db2013-06-08 12:05:44 +0200579{
580 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
581 u32 reg = sunxi_irq_cfg_reg(d->hwirq);
582 u8 index = sunxi_irq_cfg_offset(d->hwirq);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200583 unsigned long flags;
Maxime Ripard2aaaddf2013-08-04 12:38:47 +0200584 u32 regval;
Maxime Ripard60242db2013-06-08 12:05:44 +0200585 u8 mode;
586
587 switch (type) {
588 case IRQ_TYPE_EDGE_RISING:
589 mode = IRQ_EDGE_RISING;
590 break;
591 case IRQ_TYPE_EDGE_FALLING:
592 mode = IRQ_EDGE_FALLING;
593 break;
594 case IRQ_TYPE_EDGE_BOTH:
595 mode = IRQ_EDGE_BOTH;
596 break;
597 case IRQ_TYPE_LEVEL_HIGH:
598 mode = IRQ_LEVEL_HIGH;
599 break;
600 case IRQ_TYPE_LEVEL_LOW:
601 mode = IRQ_LEVEL_LOW;
602 break;
603 default:
604 return -EINVAL;
605 }
606
Maxime Ripard1bee9632013-08-04 12:38:48 +0200607 spin_lock_irqsave(&pctl->lock, flags);
608
Maxime Riparda0d6de92015-07-20 14:41:11 +0200609 if (type & IRQ_TYPE_LEVEL_MASK)
Thomas Gleixnerb9a5ec332015-09-16 12:32:40 +0200610 irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_level_irq_chip,
611 handle_fasteoi_irq, NULL);
Maxime Riparda0d6de92015-07-20 14:41:11 +0200612 else
Thomas Gleixnerb9a5ec332015-09-16 12:32:40 +0200613 irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_edge_irq_chip,
614 handle_edge_irq, NULL);
Maxime Riparda0d6de92015-07-20 14:41:11 +0200615
Maxime Ripard2aaaddf2013-08-04 12:38:47 +0200616 regval = readl(pctl->membase + reg);
Hans de Goeded82f9402014-02-17 22:19:43 +0100617 regval &= ~(IRQ_CFG_IRQ_MASK << index);
Maxime Ripard2aaaddf2013-08-04 12:38:47 +0200618 writel(regval | (mode << index), pctl->membase + reg);
Maxime Ripard60242db2013-06-08 12:05:44 +0200619
Maxime Ripard1bee9632013-08-04 12:38:48 +0200620 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard60242db2013-06-08 12:05:44 +0200621
622 return 0;
623}
624
Maxime Ripard645ec712014-06-05 15:26:00 +0200625static void sunxi_pinctrl_irq_ack(struct irq_data *d)
Maxime Ripard60242db2013-06-08 12:05:44 +0200626{
627 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
Maxime Ripard60242db2013-06-08 12:05:44 +0200628 u32 status_reg = sunxi_irq_status_reg(d->hwirq);
629 u8 status_idx = sunxi_irq_status_offset(d->hwirq);
Maxime Ripard60242db2013-06-08 12:05:44 +0200630
631 /* Clear the IRQ */
632 writel(1 << status_idx, pctl->membase + status_reg);
633}
634
635static void sunxi_pinctrl_irq_mask(struct irq_data *d)
636{
637 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
638 u32 reg = sunxi_irq_ctrl_reg(d->hwirq);
639 u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200640 unsigned long flags;
Maxime Ripard60242db2013-06-08 12:05:44 +0200641 u32 val;
642
Maxime Ripard1bee9632013-08-04 12:38:48 +0200643 spin_lock_irqsave(&pctl->lock, flags);
644
Maxime Ripard60242db2013-06-08 12:05:44 +0200645 /* Mask the IRQ */
646 val = readl(pctl->membase + reg);
647 writel(val & ~(1 << idx), pctl->membase + reg);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200648
649 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard60242db2013-06-08 12:05:44 +0200650}
651
652static void sunxi_pinctrl_irq_unmask(struct irq_data *d)
653{
654 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
Maxime Ripard60242db2013-06-08 12:05:44 +0200655 u32 reg = sunxi_irq_ctrl_reg(d->hwirq);
656 u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200657 unsigned long flags;
Maxime Ripard60242db2013-06-08 12:05:44 +0200658 u32 val;
659
Maxime Ripard1bee9632013-08-04 12:38:48 +0200660 spin_lock_irqsave(&pctl->lock, flags);
661
Maxime Ripard60242db2013-06-08 12:05:44 +0200662 /* Unmask the IRQ */
663 val = readl(pctl->membase + reg);
664 writel(val | (1 << idx), pctl->membase + reg);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200665
666 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard60242db2013-06-08 12:05:44 +0200667}
668
Hans de Goeded61e23e2014-06-29 16:11:02 +0200669static void sunxi_pinctrl_irq_ack_unmask(struct irq_data *d)
670{
671 sunxi_pinctrl_irq_ack(d);
672 sunxi_pinctrl_irq_unmask(d);
673}
674
Hans de Goedef4c51c12014-06-29 16:11:01 +0200675static struct irq_chip sunxi_pinctrl_edge_irq_chip = {
Maxime Ripardfb5b7782015-07-20 14:41:12 +0200676 .name = "sunxi_pio_edge",
Maxime Ripard645ec712014-06-05 15:26:00 +0200677 .irq_ack = sunxi_pinctrl_irq_ack,
Maxime Ripard60242db2013-06-08 12:05:44 +0200678 .irq_mask = sunxi_pinctrl_irq_mask,
Maxime Ripard60242db2013-06-08 12:05:44 +0200679 .irq_unmask = sunxi_pinctrl_irq_unmask,
Hans de Goedefea6d8e2014-06-29 16:11:00 +0200680 .irq_request_resources = sunxi_pinctrl_irq_request_resources,
Chen-Yu Tsaif83549d2014-07-15 01:24:36 +0800681 .irq_release_resources = sunxi_pinctrl_irq_release_resources,
Maxime Ripard60242db2013-06-08 12:05:44 +0200682 .irq_set_type = sunxi_pinctrl_irq_set_type,
Chen-Yu Tsai578c0a82014-06-29 16:10:59 +0200683 .flags = IRQCHIP_SKIP_SET_WAKE,
Maxime Ripard60242db2013-06-08 12:05:44 +0200684};
685
Hans de Goedef4c51c12014-06-29 16:11:01 +0200686static struct irq_chip sunxi_pinctrl_level_irq_chip = {
Maxime Ripardfb5b7782015-07-20 14:41:12 +0200687 .name = "sunxi_pio_level",
Hans de Goedef4c51c12014-06-29 16:11:01 +0200688 .irq_eoi = sunxi_pinctrl_irq_ack,
689 .irq_mask = sunxi_pinctrl_irq_mask,
690 .irq_unmask = sunxi_pinctrl_irq_unmask,
Hans de Goeded61e23e2014-06-29 16:11:02 +0200691 /* Define irq_enable / disable to avoid spurious irqs for drivers
692 * using these to suppress irqs while they clear the irq source */
693 .irq_enable = sunxi_pinctrl_irq_ack_unmask,
694 .irq_disable = sunxi_pinctrl_irq_mask,
Hans de Goedef4c51c12014-06-29 16:11:01 +0200695 .irq_request_resources = sunxi_pinctrl_irq_request_resources,
Chen-Yu Tsaif83549d2014-07-15 01:24:36 +0800696 .irq_release_resources = sunxi_pinctrl_irq_release_resources,
Hans de Goedef4c51c12014-06-29 16:11:01 +0200697 .irq_set_type = sunxi_pinctrl_irq_set_type,
698 .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_EOI_THREADED |
699 IRQCHIP_EOI_IF_HANDLED,
Maxime Ripard60242db2013-06-08 12:05:44 +0200700};
701
Maxime Ripardd8323c62015-07-27 14:41:57 +0200702static int sunxi_pinctrl_irq_of_xlate(struct irq_domain *d,
703 struct device_node *node,
704 const u32 *intspec,
705 unsigned int intsize,
706 unsigned long *out_hwirq,
707 unsigned int *out_type)
708{
709 struct sunxi_desc_function *desc;
710 int pin, base;
711
712 if (intsize < 3)
713 return -EINVAL;
714
715 base = PINS_PER_BANK * intspec[0];
716 pin = base + intspec[1];
717
718 desc = sunxi_pinctrl_desc_find_function_by_pin(d->host_data,
719 pin, "irq");
720 if (!desc)
721 return -EINVAL;
722
723 *out_hwirq = desc->irqbank * PINS_PER_BANK + desc->irqnum;
724 *out_type = intspec[2];
725
726 return 0;
727}
728
729static struct irq_domain_ops sunxi_pinctrl_irq_domain_ops = {
730 .xlate = sunxi_pinctrl_irq_of_xlate,
731};
732
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200733static void sunxi_pinctrl_irq_handler(struct irq_desc *desc)
Maxime Ripard60242db2013-06-08 12:05:44 +0200734{
Thomas Gleixnereeef97b2015-07-13 01:55:27 +0200735 unsigned int irq = irq_desc_get_irq(desc);
Jiang Liu5663bb22015-06-04 12:13:16 +0800736 struct irq_chip *chip = irq_desc_get_chip(desc);
737 struct sunxi_pinctrl *pctl = irq_desc_get_handler_data(desc);
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200738 unsigned long bank, reg, val;
Maxime Ripard60242db2013-06-08 12:05:44 +0200739
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200740 for (bank = 0; bank < pctl->desc->irq_banks; bank++)
741 if (irq == pctl->irq[bank])
742 break;
Maxime Ripard60242db2013-06-08 12:05:44 +0200743
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200744 if (bank == pctl->desc->irq_banks)
745 return;
746
747 reg = sunxi_irq_status_reg_from_bank(bank);
748 val = readl(pctl->membase + reg);
Maxime Ripard60242db2013-06-08 12:05:44 +0200749
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200750 if (val) {
Maxime Ripard60242db2013-06-08 12:05:44 +0200751 int irqoffset;
752
Chen-Yu Tsai905a5112014-02-11 00:22:37 +0800753 chained_irq_enter(chip, desc);
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200754 for_each_set_bit(irqoffset, &val, IRQ_PER_BANK) {
755 int pin_irq = irq_find_mapping(pctl->domain,
756 bank * IRQ_PER_BANK + irqoffset);
Maxime Ripard60242db2013-06-08 12:05:44 +0200757 generic_handle_irq(pin_irq);
758 }
Chen-Yu Tsai905a5112014-02-11 00:22:37 +0800759 chained_irq_exit(chip, desc);
Maxime Ripard60242db2013-06-08 12:05:44 +0200760 }
761}
762
Maxime Ripard0e37f882013-01-18 22:30:34 +0100763static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
764 const char *name)
765{
766 struct sunxi_pinctrl_function *func = pctl->functions;
767
768 while (func->name) {
769 /* function already there */
770 if (strcmp(func->name, name) == 0) {
771 func->ngroups++;
772 return -EEXIST;
773 }
774 func++;
775 }
776
777 func->name = name;
778 func->ngroups = 1;
779
780 pctl->nfunctions++;
781
782 return 0;
783}
784
785static int sunxi_pinctrl_build_state(struct platform_device *pdev)
786{
787 struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
788 int i;
789
790 pctl->ngroups = pctl->desc->npins;
791
792 /* Allocate groups */
793 pctl->groups = devm_kzalloc(&pdev->dev,
794 pctl->ngroups * sizeof(*pctl->groups),
795 GFP_KERNEL);
796 if (!pctl->groups)
797 return -ENOMEM;
798
799 for (i = 0; i < pctl->desc->npins; i++) {
800 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
801 struct sunxi_pinctrl_group *group = pctl->groups + i;
802
803 group->name = pin->pin.name;
804 group->pin = pin->pin.number;
805 }
806
807 /*
808 * We suppose that we won't have any more functions than pins,
809 * we'll reallocate that later anyway
810 */
811 pctl->functions = devm_kzalloc(&pdev->dev,
812 pctl->desc->npins * sizeof(*pctl->functions),
813 GFP_KERNEL);
814 if (!pctl->functions)
815 return -ENOMEM;
816
817 /* Count functions and their associated groups */
818 for (i = 0; i < pctl->desc->npins; i++) {
819 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
820 struct sunxi_desc_function *func = pin->functions;
821
822 while (func->name) {
Chen-Yu Tsaid54e9a22014-05-26 09:47:56 +0200823 /* Create interrupt mapping while we're at it */
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200824 if (!strcmp(func->name, "irq")) {
825 int irqnum = func->irqnum + func->irqbank * IRQ_PER_BANK;
826 pctl->irq_array[irqnum] = pin->pin.number;
827 }
828
Maxime Ripard0e37f882013-01-18 22:30:34 +0100829 sunxi_pinctrl_add_function(pctl, func->name);
830 func++;
831 }
832 }
833
834 pctl->functions = krealloc(pctl->functions,
835 pctl->nfunctions * sizeof(*pctl->functions),
836 GFP_KERNEL);
837
838 for (i = 0; i < pctl->desc->npins; i++) {
839 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
840 struct sunxi_desc_function *func = pin->functions;
841
842 while (func->name) {
843 struct sunxi_pinctrl_function *func_item;
844 const char **func_grp;
845
846 func_item = sunxi_pinctrl_find_function_by_name(pctl,
847 func->name);
848 if (!func_item)
849 return -EINVAL;
850
851 if (!func_item->groups) {
852 func_item->groups =
853 devm_kzalloc(&pdev->dev,
854 func_item->ngroups * sizeof(*func_item->groups),
855 GFP_KERNEL);
856 if (!func_item->groups)
857 return -ENOMEM;
858 }
859
860 func_grp = func_item->groups;
861 while (*func_grp)
862 func_grp++;
863
864 *func_grp = pin->pin.name;
865 func++;
866 }
867 }
868
869 return 0;
870}
871
Maxime Ripard2284ba62014-04-18 20:10:41 +0200872int sunxi_pinctrl_init(struct platform_device *pdev,
873 const struct sunxi_pinctrl_desc *desc)
Maxime Ripard0e37f882013-01-18 22:30:34 +0100874{
875 struct device_node *node = pdev->dev.of_node;
Maxime Ripardba6764d2014-05-22 16:25:27 +0200876 struct pinctrl_desc *pctrl_desc;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100877 struct pinctrl_pin_desc *pins;
878 struct sunxi_pinctrl *pctl;
Maxime Ripard4409caf2014-04-26 21:59:50 +0200879 struct resource *res;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100880 int i, ret, last_pin;
Emilio López950707c2013-03-22 11:20:40 -0300881 struct clk *clk;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100882
883 pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
884 if (!pctl)
885 return -ENOMEM;
886 platform_set_drvdata(pdev, pctl);
887
Maxime Ripard1bee9632013-08-04 12:38:48 +0200888 spin_lock_init(&pctl->lock);
889
Maxime Ripard4409caf2014-04-26 21:59:50 +0200890 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
891 pctl->membase = devm_ioremap_resource(&pdev->dev, res);
892 if (IS_ERR(pctl->membase))
893 return PTR_ERR(pctl->membase);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100894
Maxime Ripardba6764d2014-05-22 16:25:27 +0200895 pctl->dev = &pdev->dev;
Maxime Ripard2284ba62014-04-18 20:10:41 +0200896 pctl->desc = desc;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100897
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200898 pctl->irq_array = devm_kcalloc(&pdev->dev,
899 IRQ_PER_BANK * pctl->desc->irq_banks,
900 sizeof(*pctl->irq_array),
901 GFP_KERNEL);
902 if (!pctl->irq_array)
903 return -ENOMEM;
904
Maxime Ripard0e37f882013-01-18 22:30:34 +0100905 ret = sunxi_pinctrl_build_state(pdev);
906 if (ret) {
907 dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
908 return ret;
909 }
910
911 pins = devm_kzalloc(&pdev->dev,
912 pctl->desc->npins * sizeof(*pins),
913 GFP_KERNEL);
914 if (!pins)
915 return -ENOMEM;
916
917 for (i = 0; i < pctl->desc->npins; i++)
918 pins[i] = pctl->desc->pins[i].pin;
919
Maxime Ripardba6764d2014-05-22 16:25:27 +0200920 pctrl_desc = devm_kzalloc(&pdev->dev,
921 sizeof(*pctrl_desc),
922 GFP_KERNEL);
923 if (!pctrl_desc)
924 return -ENOMEM;
925
926 pctrl_desc->name = dev_name(&pdev->dev);
927 pctrl_desc->owner = THIS_MODULE;
928 pctrl_desc->pins = pins;
929 pctrl_desc->npins = pctl->desc->npins;
930 pctrl_desc->confops = &sunxi_pconf_ops;
931 pctrl_desc->pctlops = &sunxi_pctrl_ops;
932 pctrl_desc->pmxops = &sunxi_pmx_ops;
933
934 pctl->pctl_dev = pinctrl_register(pctrl_desc,
Maxime Ripard0e37f882013-01-18 22:30:34 +0100935 &pdev->dev, pctl);
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900936 if (IS_ERR(pctl->pctl_dev)) {
Maxime Ripard0e37f882013-01-18 22:30:34 +0100937 dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +0900938 return PTR_ERR(pctl->pctl_dev);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100939 }
940
Maxime Ripard08e9e612013-01-28 21:33:12 +0100941 pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
942 if (!pctl->chip) {
943 ret = -ENOMEM;
944 goto pinctrl_error;
945 }
946
947 last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
Boris BREZILLONd83c82c2014-04-10 15:52:43 +0200948 pctl->chip->owner = THIS_MODULE;
Jonas Gorski98c85d52015-10-11 17:34:19 +0200949 pctl->chip->request = gpiochip_generic_request,
950 pctl->chip->free = gpiochip_generic_free,
Boris BREZILLONd83c82c2014-04-10 15:52:43 +0200951 pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input,
952 pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output,
953 pctl->chip->get = sunxi_pinctrl_gpio_get,
954 pctl->chip->set = sunxi_pinctrl_gpio_set,
955 pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate,
956 pctl->chip->to_irq = sunxi_pinctrl_gpio_to_irq,
957 pctl->chip->of_gpio_n_cells = 3,
958 pctl->chip->can_sleep = false,
959 pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK) -
960 pctl->desc->pin_base;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100961 pctl->chip->label = dev_name(&pdev->dev);
962 pctl->chip->dev = &pdev->dev;
Boris BREZILLONd83c82c2014-04-10 15:52:43 +0200963 pctl->chip->base = pctl->desc->pin_base;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100964
965 ret = gpiochip_add(pctl->chip);
966 if (ret)
967 goto pinctrl_error;
968
969 for (i = 0; i < pctl->desc->npins; i++) {
970 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
971
972 ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
Chen-Yu Tsai343f1322014-07-15 01:24:37 +0800973 pin->pin.number - pctl->desc->pin_base,
Maxime Ripard08e9e612013-01-28 21:33:12 +0100974 pin->pin.number, 1);
975 if (ret)
976 goto gpiochip_error;
977 }
978
Emilio López950707c2013-03-22 11:20:40 -0300979 clk = devm_clk_get(&pdev->dev, NULL);
Wei Yongjund72f88a2013-05-23 17:32:14 +0800980 if (IS_ERR(clk)) {
981 ret = PTR_ERR(clk);
Emilio López950707c2013-03-22 11:20:40 -0300982 goto gpiochip_error;
Wei Yongjund72f88a2013-05-23 17:32:14 +0800983 }
Emilio López950707c2013-03-22 11:20:40 -0300984
Boris BREZILLON64150932014-04-10 15:52:40 +0200985 ret = clk_prepare_enable(clk);
986 if (ret)
987 goto gpiochip_error;
Emilio López950707c2013-03-22 11:20:40 -0300988
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200989 pctl->irq = devm_kcalloc(&pdev->dev,
990 pctl->desc->irq_banks,
991 sizeof(*pctl->irq),
992 GFP_KERNEL);
Maxime Ripard60242db2013-06-08 12:05:44 +0200993 if (!pctl->irq) {
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200994 ret = -ENOMEM;
Maxime Riparddc969102014-04-26 22:28:54 +0200995 goto clk_error;
Maxime Ripard60242db2013-06-08 12:05:44 +0200996 }
997
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200998 for (i = 0; i < pctl->desc->irq_banks; i++) {
999 pctl->irq[i] = platform_get_irq(pdev, i);
1000 if (pctl->irq[i] < 0) {
1001 ret = pctl->irq[i];
1002 goto clk_error;
1003 }
1004 }
1005
1006 pctl->domain = irq_domain_add_linear(node,
1007 pctl->desc->irq_banks * IRQ_PER_BANK,
Maxime Ripardd8323c62015-07-27 14:41:57 +02001008 &sunxi_pinctrl_irq_domain_ops,
1009 pctl);
Maxime Ripard60242db2013-06-08 12:05:44 +02001010 if (!pctl->domain) {
1011 dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
1012 ret = -ENOMEM;
Maxime Riparddc969102014-04-26 22:28:54 +02001013 goto clk_error;
Maxime Ripard60242db2013-06-08 12:05:44 +02001014 }
1015
Maxime Ripardaebdc8a2014-06-05 15:26:04 +02001016 for (i = 0; i < (pctl->desc->irq_banks * IRQ_PER_BANK); i++) {
Maxime Ripard60242db2013-06-08 12:05:44 +02001017 int irqno = irq_create_mapping(pctl->domain, i);
1018
Hans de Goedef4c51c12014-06-29 16:11:01 +02001019 irq_set_chip_and_handler(irqno, &sunxi_pinctrl_edge_irq_chip,
1020 handle_edge_irq);
Maxime Ripard60242db2013-06-08 12:05:44 +02001021 irq_set_chip_data(irqno, pctl);
1022 };
1023
Maxime Ripardaebdc8a2014-06-05 15:26:04 +02001024 for (i = 0; i < pctl->desc->irq_banks; i++) {
Hans de Goedef4c51c12014-06-29 16:11:01 +02001025 /* Mask and clear all IRQs before registering a handler */
1026 writel(0, pctl->membase + sunxi_irq_ctrl_reg_from_bank(i));
1027 writel(0xffffffff,
1028 pctl->membase + sunxi_irq_status_reg_from_bank(i));
1029
Thomas Gleixneref80e872015-06-21 20:16:18 +02001030 irq_set_chained_handler_and_data(pctl->irq[i],
1031 sunxi_pinctrl_irq_handler,
1032 pctl);
Maxime Ripardaebdc8a2014-06-05 15:26:04 +02001033 }
Maxime Ripard60242db2013-06-08 12:05:44 +02001034
Maxime Ripard08e9e612013-01-28 21:33:12 +01001035 dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
Maxime Ripard0e37f882013-01-18 22:30:34 +01001036
1037 return 0;
Maxime Ripard08e9e612013-01-28 21:33:12 +01001038
Boris BREZILLONe2bddc62014-04-10 15:52:41 +02001039clk_error:
1040 clk_disable_unprepare(clk);
Maxime Ripard08e9e612013-01-28 21:33:12 +01001041gpiochip_error:
abdoulaye bertheb4e7c552014-07-12 22:30:13 +02001042 gpiochip_remove(pctl->chip);
Maxime Ripard08e9e612013-01-28 21:33:12 +01001043pinctrl_error:
1044 pinctrl_unregister(pctl->pctl_dev);
1045 return ret;
Maxime Ripard0e37f882013-01-18 22:30:34 +01001046}