blob: 96ee6bb0fad581b35d28e7e97f806b00346f5482 [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
Hans de Goedef4c51c12014-06-29 16:11:01 +020034static struct irq_chip sunxi_pinctrl_edge_irq_chip;
35static struct irq_chip sunxi_pinctrl_level_irq_chip;
36
Maxime Ripard0e37f882013-01-18 22:30:34 +010037static struct sunxi_pinctrl_group *
38sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl *pctl, const char *group)
39{
40 int i;
41
42 for (i = 0; i < pctl->ngroups; i++) {
43 struct sunxi_pinctrl_group *grp = pctl->groups + i;
44
45 if (!strcmp(grp->name, group))
46 return grp;
47 }
48
49 return NULL;
50}
51
52static struct sunxi_pinctrl_function *
53sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl *pctl,
54 const char *name)
55{
56 struct sunxi_pinctrl_function *func = pctl->functions;
57 int i;
58
59 for (i = 0; i < pctl->nfunctions; i++) {
60 if (!func[i].name)
61 break;
62
63 if (!strcmp(func[i].name, name))
64 return func + i;
65 }
66
67 return NULL;
68}
69
70static struct sunxi_desc_function *
71sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl *pctl,
72 const char *pin_name,
73 const char *func_name)
74{
75 int i;
76
77 for (i = 0; i < pctl->desc->npins; i++) {
78 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
79
80 if (!strcmp(pin->pin.name, pin_name)) {
81 struct sunxi_desc_function *func = pin->functions;
82
83 while (func->name) {
84 if (!strcmp(func->name, func_name))
85 return func;
86
87 func++;
88 }
89 }
90 }
91
92 return NULL;
93}
94
Maxime Ripard814d4f22013-06-08 12:05:43 +020095static struct sunxi_desc_function *
96sunxi_pinctrl_desc_find_function_by_pin(struct sunxi_pinctrl *pctl,
97 const u16 pin_num,
98 const char *func_name)
99{
100 int i;
101
102 for (i = 0; i < pctl->desc->npins; i++) {
103 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
104
105 if (pin->pin.number == pin_num) {
106 struct sunxi_desc_function *func = pin->functions;
107
108 while (func->name) {
109 if (!strcmp(func->name, func_name))
110 return func;
111
112 func++;
113 }
114 }
115 }
116
117 return NULL;
118}
119
Maxime Ripard0e37f882013-01-18 22:30:34 +0100120static int sunxi_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
121{
122 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
123
124 return pctl->ngroups;
125}
126
127static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev *pctldev,
128 unsigned group)
129{
130 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
131
132 return pctl->groups[group].name;
133}
134
135static int sunxi_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
136 unsigned group,
137 const unsigned **pins,
138 unsigned *num_pins)
139{
140 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
141
142 *pins = (unsigned *)&pctl->groups[group].pin;
143 *num_pins = 1;
144
145 return 0;
146}
147
148static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
149 struct device_node *node,
150 struct pinctrl_map **map,
151 unsigned *num_maps)
152{
153 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
154 unsigned long *pinconfig;
155 struct property *prop;
156 const char *function;
157 const char *group;
158 int ret, nmaps, i = 0;
159 u32 val;
160
161 *map = NULL;
162 *num_maps = 0;
163
164 ret = of_property_read_string(node, "allwinner,function", &function);
165 if (ret) {
166 dev_err(pctl->dev,
167 "missing allwinner,function property in node %s\n",
168 node->name);
169 return -EINVAL;
170 }
171
172 nmaps = of_property_count_strings(node, "allwinner,pins") * 2;
173 if (nmaps < 0) {
174 dev_err(pctl->dev,
175 "missing allwinner,pins property in node %s\n",
176 node->name);
177 return -EINVAL;
178 }
179
180 *map = kmalloc(nmaps * sizeof(struct pinctrl_map), GFP_KERNEL);
Sachin Kamat3efa9212013-07-29 13:49:32 +0530181 if (!*map)
Maxime Ripard0e37f882013-01-18 22:30:34 +0100182 return -ENOMEM;
183
184 of_property_for_each_string(node, "allwinner,pins", prop, group) {
185 struct sunxi_pinctrl_group *grp =
186 sunxi_pinctrl_find_group_by_name(pctl, group);
187 int j = 0, configlen = 0;
188
189 if (!grp) {
190 dev_err(pctl->dev, "unknown pin %s", group);
191 continue;
192 }
193
194 if (!sunxi_pinctrl_desc_find_function_by_name(pctl,
195 grp->name,
196 function)) {
197 dev_err(pctl->dev, "unsupported function %s on pin %s",
198 function, group);
199 continue;
200 }
201
202 (*map)[i].type = PIN_MAP_TYPE_MUX_GROUP;
203 (*map)[i].data.mux.group = group;
204 (*map)[i].data.mux.function = function;
205
206 i++;
207
208 (*map)[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
209 (*map)[i].data.configs.group_or_pin = group;
210
211 if (of_find_property(node, "allwinner,drive", NULL))
212 configlen++;
213 if (of_find_property(node, "allwinner,pull", NULL))
214 configlen++;
215
216 pinconfig = kzalloc(configlen * sizeof(*pinconfig), GFP_KERNEL);
217
218 if (!of_property_read_u32(node, "allwinner,drive", &val)) {
219 u16 strength = (val + 1) * 10;
220 pinconfig[j++] =
221 pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH,
222 strength);
223 }
224
225 if (!of_property_read_u32(node, "allwinner,pull", &val)) {
226 enum pin_config_param pull = PIN_CONFIG_END;
227 if (val == 1)
228 pull = PIN_CONFIG_BIAS_PULL_UP;
229 else if (val == 2)
230 pull = PIN_CONFIG_BIAS_PULL_DOWN;
231 pinconfig[j++] = pinconf_to_config_packed(pull, 0);
232 }
233
234 (*map)[i].data.configs.configs = pinconfig;
235 (*map)[i].data.configs.num_configs = configlen;
236
237 i++;
238 }
239
240 *num_maps = nmaps;
241
242 return 0;
243}
244
245static void sunxi_pctrl_dt_free_map(struct pinctrl_dev *pctldev,
246 struct pinctrl_map *map,
247 unsigned num_maps)
248{
249 int i;
250
251 for (i = 0; i < num_maps; i++) {
252 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
253 kfree(map[i].data.configs.configs);
254 }
255
256 kfree(map);
257}
258
Laurent Pinchart022ab142013-02-16 10:25:07 +0100259static const struct pinctrl_ops sunxi_pctrl_ops = {
Maxime Ripard0e37f882013-01-18 22:30:34 +0100260 .dt_node_to_map = sunxi_pctrl_dt_node_to_map,
261 .dt_free_map = sunxi_pctrl_dt_free_map,
262 .get_groups_count = sunxi_pctrl_get_groups_count,
263 .get_group_name = sunxi_pctrl_get_group_name,
264 .get_group_pins = sunxi_pctrl_get_group_pins,
265};
266
267static int sunxi_pconf_group_get(struct pinctrl_dev *pctldev,
268 unsigned group,
269 unsigned long *config)
270{
271 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
272
273 *config = pctl->groups[group].config;
274
275 return 0;
276}
277
278static int sunxi_pconf_group_set(struct pinctrl_dev *pctldev,
279 unsigned group,
Sherman Yin03b054e2013-08-27 11:32:12 -0700280 unsigned long *configs,
281 unsigned num_configs)
Maxime Ripard0e37f882013-01-18 22:30:34 +0100282{
283 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
284 struct sunxi_pinctrl_group *g = &pctl->groups[group];
Maxime Ripard1bee9632013-08-04 12:38:48 +0200285 unsigned long flags;
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800286 unsigned pin = g->pin - pctl->desc->pin_base;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100287 u32 val, mask;
288 u16 strength;
289 u8 dlevel;
Sherman Yin03b054e2013-08-27 11:32:12 -0700290 int i;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100291
Linus Walleij6ad30ce2013-08-29 09:46:30 +0200292 spin_lock_irqsave(&pctl->lock, flags);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200293
Sherman Yin03b054e2013-08-27 11:32:12 -0700294 for (i = 0; i < num_configs; i++) {
295 switch (pinconf_to_config_param(configs[i])) {
296 case PIN_CONFIG_DRIVE_STRENGTH:
297 strength = pinconf_to_config_argument(configs[i]);
Linus Walleij07b7eb92013-08-29 19:17:13 +0200298 if (strength > 40) {
299 spin_unlock_irqrestore(&pctl->lock, flags);
Sherman Yin03b054e2013-08-27 11:32:12 -0700300 return -EINVAL;
Linus Walleij07b7eb92013-08-29 19:17:13 +0200301 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700302 /*
303 * We convert from mA to what the register expects:
304 * 0: 10mA
305 * 1: 20mA
306 * 2: 30mA
307 * 3: 40mA
308 */
309 dlevel = strength / 10 - 1;
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800310 val = readl(pctl->membase + sunxi_dlevel_reg(pin));
311 mask = DLEVEL_PINS_MASK << sunxi_dlevel_offset(pin);
Sherman Yin03b054e2013-08-27 11:32:12 -0700312 writel((val & ~mask)
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800313 | dlevel << sunxi_dlevel_offset(pin),
314 pctl->membase + sunxi_dlevel_reg(pin));
Sherman Yin03b054e2013-08-27 11:32:12 -0700315 break;
316 case PIN_CONFIG_BIAS_PULL_UP:
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800317 val = readl(pctl->membase + sunxi_pull_reg(pin));
318 mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
319 writel((val & ~mask) | 1 << sunxi_pull_offset(pin),
320 pctl->membase + sunxi_pull_reg(pin));
Sherman Yin03b054e2013-08-27 11:32:12 -0700321 break;
322 case PIN_CONFIG_BIAS_PULL_DOWN:
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800323 val = readl(pctl->membase + sunxi_pull_reg(pin));
324 mask = PULL_PINS_MASK << sunxi_pull_offset(pin);
325 writel((val & ~mask) | 2 << sunxi_pull_offset(pin),
326 pctl->membase + sunxi_pull_reg(pin));
Sherman Yin03b054e2013-08-27 11:32:12 -0700327 break;
328 default:
329 break;
330 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700331 /* cache the config value */
332 g->config = configs[i];
333 } /* for each config */
Maxime Ripard0e37f882013-01-18 22:30:34 +0100334
Linus Walleij6ad30ce2013-08-29 09:46:30 +0200335 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100336
337 return 0;
338}
339
Laurent Pinchart022ab142013-02-16 10:25:07 +0100340static const struct pinconf_ops sunxi_pconf_ops = {
Maxime Ripard0e37f882013-01-18 22:30:34 +0100341 .pin_config_group_get = sunxi_pconf_group_get,
342 .pin_config_group_set = sunxi_pconf_group_set,
343};
344
345static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
346{
347 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
348
349 return pctl->nfunctions;
350}
351
352static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev,
353 unsigned function)
354{
355 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
356
357 return pctl->functions[function].name;
358}
359
360static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev,
361 unsigned function,
362 const char * const **groups,
363 unsigned * const num_groups)
364{
365 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
366
367 *groups = pctl->functions[function].groups;
368 *num_groups = pctl->functions[function].ngroups;
369
370 return 0;
371}
372
373static void sunxi_pmx_set(struct pinctrl_dev *pctldev,
374 unsigned pin,
375 u8 config)
376{
377 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200378 unsigned long flags;
379 u32 val, mask;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100380
Maxime Ripard1bee9632013-08-04 12:38:48 +0200381 spin_lock_irqsave(&pctl->lock, flags);
382
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800383 pin -= pctl->desc->pin_base;
Maxime Ripard1bee9632013-08-04 12:38:48 +0200384 val = readl(pctl->membase + sunxi_mux_reg(pin));
385 mask = MUX_PINS_MASK << sunxi_mux_offset(pin);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100386 writel((val & ~mask) | config << sunxi_mux_offset(pin),
387 pctl->membase + sunxi_mux_reg(pin));
Maxime Ripard1bee9632013-08-04 12:38:48 +0200388
389 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100390}
391
392static int sunxi_pmx_enable(struct pinctrl_dev *pctldev,
393 unsigned function,
394 unsigned group)
395{
396 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
397 struct sunxi_pinctrl_group *g = pctl->groups + group;
398 struct sunxi_pinctrl_function *func = pctl->functions + function;
399 struct sunxi_desc_function *desc =
400 sunxi_pinctrl_desc_find_function_by_name(pctl,
401 g->name,
402 func->name);
403
404 if (!desc)
405 return -EINVAL;
406
407 sunxi_pmx_set(pctldev, g->pin, desc->muxval);
408
409 return 0;
410}
411
Maxime Ripard08e9e612013-01-28 21:33:12 +0100412static int
413sunxi_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
414 struct pinctrl_gpio_range *range,
415 unsigned offset,
416 bool input)
417{
418 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
419 struct sunxi_desc_function *desc;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100420 const char *func;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100421
422 if (input)
423 func = "gpio_in";
424 else
425 func = "gpio_out";
426
Maxime Ripard814d4f22013-06-08 12:05:43 +0200427 desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, func);
428 if (!desc)
429 return -EINVAL;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100430
431 sunxi_pmx_set(pctldev, offset, desc->muxval);
432
Maxime Ripard814d4f22013-06-08 12:05:43 +0200433 return 0;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100434}
435
Laurent Pinchart022ab142013-02-16 10:25:07 +0100436static const struct pinmux_ops sunxi_pmx_ops = {
Maxime Ripard0e37f882013-01-18 22:30:34 +0100437 .get_functions_count = sunxi_pmx_get_funcs_cnt,
438 .get_function_name = sunxi_pmx_get_func_name,
439 .get_function_groups = sunxi_pmx_get_func_groups,
440 .enable = sunxi_pmx_enable,
Maxime Ripard08e9e612013-01-28 21:33:12 +0100441 .gpio_set_direction = sunxi_pmx_gpio_set_direction,
Maxime Ripard0e37f882013-01-18 22:30:34 +0100442};
443
Maxime Ripard08e9e612013-01-28 21:33:12 +0100444static int sunxi_pinctrl_gpio_request(struct gpio_chip *chip, unsigned offset)
445{
446 return pinctrl_request_gpio(chip->base + offset);
447}
448
449static void sunxi_pinctrl_gpio_free(struct gpio_chip *chip, unsigned offset)
450{
451 pinctrl_free_gpio(chip->base + offset);
452}
453
454static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip *chip,
455 unsigned offset)
456{
457 return pinctrl_gpio_direction_input(chip->base + offset);
458}
459
460static int sunxi_pinctrl_gpio_get(struct gpio_chip *chip, unsigned offset)
461{
462 struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
463
464 u32 reg = sunxi_data_reg(offset);
465 u8 index = sunxi_data_offset(offset);
466 u32 val = (readl(pctl->membase + reg) >> index) & DATA_PINS_MASK;
467
468 return val;
469}
470
Maxime Ripard08e9e612013-01-28 21:33:12 +0100471static void sunxi_pinctrl_gpio_set(struct gpio_chip *chip,
472 unsigned offset, int value)
473{
474 struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
475 u32 reg = sunxi_data_reg(offset);
476 u8 index = sunxi_data_offset(offset);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200477 unsigned long flags;
478 u32 regval;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100479
Maxime Ripard1bee9632013-08-04 12:38:48 +0200480 spin_lock_irqsave(&pctl->lock, flags);
481
482 regval = readl(pctl->membase + reg);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100483
Maxime Riparddf7b34f2013-07-25 12:41:16 +0200484 if (value)
485 regval |= BIT(index);
486 else
487 regval &= ~(BIT(index));
488
489 writel(regval, pctl->membase + reg);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200490
491 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100492}
493
Chen-Yu Tsaifa8cf572014-01-16 14:34:23 +0800494static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
495 unsigned offset, int value)
496{
497 sunxi_pinctrl_gpio_set(chip, offset, value);
498 return pinctrl_gpio_direction_output(chip->base + offset);
499}
500
Maxime Riparda0d72092013-02-03 12:10:11 +0100501static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
502 const struct of_phandle_args *gpiospec,
503 u32 *flags)
504{
505 int pin, base;
506
507 base = PINS_PER_BANK * gpiospec->args[0];
508 pin = base + gpiospec->args[1];
509
510 if (pin > (gc->base + gc->ngpio))
511 return -EINVAL;
512
513 if (flags)
514 *flags = gpiospec->args[2];
515
516 return pin;
517}
518
Maxime Ripard60242db2013-06-08 12:05:44 +0200519static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
520{
521 struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
522 struct sunxi_desc_function *desc;
Chen-Yu Tsai0d3bafa2014-07-01 00:04:59 +0800523 unsigned irqnum;
Maxime Ripard60242db2013-06-08 12:05:44 +0200524
Axel Linc9e3b2d2013-08-30 16:31:25 +0800525 if (offset >= chip->ngpio)
Maxime Ripard60242db2013-06-08 12:05:44 +0200526 return -ENXIO;
527
528 desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, "irq");
529 if (!desc)
530 return -EINVAL;
531
Chen-Yu Tsai0d3bafa2014-07-01 00:04:59 +0800532 irqnum = desc->irqbank * IRQ_PER_BANK + desc->irqnum;
Maxime Ripard60242db2013-06-08 12:05:44 +0200533
Chen-Yu Tsai0d3bafa2014-07-01 00:04:59 +0800534 dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
535 chip->label, offset + chip->base, irqnum);
536
537 return irq_find_mapping(pctl->domain, irqnum);
Maxime Ripard60242db2013-06-08 12:05:44 +0200538}
539
Hans de Goedefea6d8e2014-06-29 16:11:00 +0200540static int sunxi_pinctrl_irq_request_resources(struct irq_data *d)
541{
542 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
543 struct sunxi_desc_function *func;
544
545 func = sunxi_pinctrl_desc_find_function_by_pin(pctl,
546 pctl->irq_array[d->hwirq], "irq");
547 if (!func)
548 return -EINVAL;
549
550 /* Change muxing to INT mode */
551 sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval);
552
553 return 0;
554}
Maxime Ripard08e9e612013-01-28 21:33:12 +0100555
Hans de Goedef4c51c12014-06-29 16:11:01 +0200556static int sunxi_pinctrl_irq_set_type(struct irq_data *d, unsigned int type)
Maxime Ripard60242db2013-06-08 12:05:44 +0200557{
558 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
Hans de Goedef4c51c12014-06-29 16:11:01 +0200559 struct irq_desc *desc = container_of(d, struct irq_desc, irq_data);
Maxime Ripard60242db2013-06-08 12:05:44 +0200560 u32 reg = sunxi_irq_cfg_reg(d->hwirq);
561 u8 index = sunxi_irq_cfg_offset(d->hwirq);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200562 unsigned long flags;
Maxime Ripard2aaaddf2013-08-04 12:38:47 +0200563 u32 regval;
Maxime Ripard60242db2013-06-08 12:05:44 +0200564 u8 mode;
565
566 switch (type) {
567 case IRQ_TYPE_EDGE_RISING:
568 mode = IRQ_EDGE_RISING;
569 break;
570 case IRQ_TYPE_EDGE_FALLING:
571 mode = IRQ_EDGE_FALLING;
572 break;
573 case IRQ_TYPE_EDGE_BOTH:
574 mode = IRQ_EDGE_BOTH;
575 break;
576 case IRQ_TYPE_LEVEL_HIGH:
577 mode = IRQ_LEVEL_HIGH;
578 break;
579 case IRQ_TYPE_LEVEL_LOW:
580 mode = IRQ_LEVEL_LOW;
581 break;
582 default:
583 return -EINVAL;
584 }
585
Hans de Goedef4c51c12014-06-29 16:11:01 +0200586 if (type & IRQ_TYPE_LEVEL_MASK) {
587 d->chip = &sunxi_pinctrl_level_irq_chip;
588 desc->handle_irq = handle_fasteoi_irq;
589 } else {
590 d->chip = &sunxi_pinctrl_edge_irq_chip;
591 desc->handle_irq = handle_edge_irq;
592 }
593
Maxime Ripard1bee9632013-08-04 12:38:48 +0200594 spin_lock_irqsave(&pctl->lock, flags);
595
Maxime Ripard2aaaddf2013-08-04 12:38:47 +0200596 regval = readl(pctl->membase + reg);
Hans de Goeded82f9402014-02-17 22:19:43 +0100597 regval &= ~(IRQ_CFG_IRQ_MASK << index);
Maxime Ripard2aaaddf2013-08-04 12:38:47 +0200598 writel(regval | (mode << index), pctl->membase + reg);
Maxime Ripard60242db2013-06-08 12:05:44 +0200599
Maxime Ripard1bee9632013-08-04 12:38:48 +0200600 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard60242db2013-06-08 12:05:44 +0200601
602 return 0;
603}
604
Maxime Ripard645ec712014-06-05 15:26:00 +0200605static void sunxi_pinctrl_irq_ack(struct irq_data *d)
Maxime Ripard60242db2013-06-08 12:05:44 +0200606{
607 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
Maxime Ripard60242db2013-06-08 12:05:44 +0200608 u32 status_reg = sunxi_irq_status_reg(d->hwirq);
609 u8 status_idx = sunxi_irq_status_offset(d->hwirq);
Maxime Ripard60242db2013-06-08 12:05:44 +0200610
611 /* Clear the IRQ */
612 writel(1 << status_idx, pctl->membase + status_reg);
613}
614
615static void sunxi_pinctrl_irq_mask(struct irq_data *d)
616{
617 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
618 u32 reg = sunxi_irq_ctrl_reg(d->hwirq);
619 u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200620 unsigned long flags;
Maxime Ripard60242db2013-06-08 12:05:44 +0200621 u32 val;
622
Maxime Ripard1bee9632013-08-04 12:38:48 +0200623 spin_lock_irqsave(&pctl->lock, flags);
624
Maxime Ripard60242db2013-06-08 12:05:44 +0200625 /* Mask the IRQ */
626 val = readl(pctl->membase + reg);
627 writel(val & ~(1 << idx), pctl->membase + reg);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200628
629 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard60242db2013-06-08 12:05:44 +0200630}
631
632static void sunxi_pinctrl_irq_unmask(struct irq_data *d)
633{
634 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
Maxime Ripard60242db2013-06-08 12:05:44 +0200635 u32 reg = sunxi_irq_ctrl_reg(d->hwirq);
636 u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200637 unsigned long flags;
Maxime Ripard60242db2013-06-08 12:05:44 +0200638 u32 val;
639
Maxime Ripard1bee9632013-08-04 12:38:48 +0200640 spin_lock_irqsave(&pctl->lock, flags);
641
Maxime Ripard60242db2013-06-08 12:05:44 +0200642 /* Unmask the IRQ */
643 val = readl(pctl->membase + reg);
644 writel(val | (1 << idx), pctl->membase + reg);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200645
646 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard60242db2013-06-08 12:05:44 +0200647}
648
Hans de Goeded61e23e2014-06-29 16:11:02 +0200649static void sunxi_pinctrl_irq_ack_unmask(struct irq_data *d)
650{
651 sunxi_pinctrl_irq_ack(d);
652 sunxi_pinctrl_irq_unmask(d);
653}
654
Hans de Goedef4c51c12014-06-29 16:11:01 +0200655static struct irq_chip sunxi_pinctrl_edge_irq_chip = {
Maxime Ripard645ec712014-06-05 15:26:00 +0200656 .irq_ack = sunxi_pinctrl_irq_ack,
Maxime Ripard60242db2013-06-08 12:05:44 +0200657 .irq_mask = sunxi_pinctrl_irq_mask,
Maxime Ripard60242db2013-06-08 12:05:44 +0200658 .irq_unmask = sunxi_pinctrl_irq_unmask,
Hans de Goedefea6d8e2014-06-29 16:11:00 +0200659 .irq_request_resources = sunxi_pinctrl_irq_request_resources,
Maxime Ripard60242db2013-06-08 12:05:44 +0200660 .irq_set_type = sunxi_pinctrl_irq_set_type,
Chen-Yu Tsai578c0a82014-06-29 16:10:59 +0200661 .flags = IRQCHIP_SKIP_SET_WAKE,
Maxime Ripard60242db2013-06-08 12:05:44 +0200662};
663
Hans de Goedef4c51c12014-06-29 16:11:01 +0200664static struct irq_chip sunxi_pinctrl_level_irq_chip = {
665 .irq_eoi = sunxi_pinctrl_irq_ack,
666 .irq_mask = sunxi_pinctrl_irq_mask,
667 .irq_unmask = sunxi_pinctrl_irq_unmask,
Hans de Goeded61e23e2014-06-29 16:11:02 +0200668 /* Define irq_enable / disable to avoid spurious irqs for drivers
669 * using these to suppress irqs while they clear the irq source */
670 .irq_enable = sunxi_pinctrl_irq_ack_unmask,
671 .irq_disable = sunxi_pinctrl_irq_mask,
Hans de Goedef4c51c12014-06-29 16:11:01 +0200672 .irq_request_resources = sunxi_pinctrl_irq_request_resources,
673 .irq_set_type = sunxi_pinctrl_irq_set_type,
674 .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_EOI_THREADED |
675 IRQCHIP_EOI_IF_HANDLED,
676};
677
Maxime Ripard60242db2013-06-08 12:05:44 +0200678static void sunxi_pinctrl_irq_handler(unsigned irq, struct irq_desc *desc)
679{
Chen-Yu Tsai905a5112014-02-11 00:22:37 +0800680 struct irq_chip *chip = irq_get_chip(irq);
Maxime Ripard60242db2013-06-08 12:05:44 +0200681 struct sunxi_pinctrl *pctl = irq_get_handler_data(irq);
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200682 unsigned long bank, reg, val;
683
684 for (bank = 0; bank < pctl->desc->irq_banks; bank++)
685 if (irq == pctl->irq[bank])
686 break;
687
688 if (bank == pctl->desc->irq_banks)
689 return;
690
691 reg = sunxi_irq_status_reg_from_bank(bank);
692 val = readl(pctl->membase + reg);
Maxime Ripard60242db2013-06-08 12:05:44 +0200693
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200694 if (val) {
Maxime Ripard60242db2013-06-08 12:05:44 +0200695 int irqoffset;
696
Chen-Yu Tsai905a5112014-02-11 00:22:37 +0800697 chained_irq_enter(chip, desc);
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200698 for_each_set_bit(irqoffset, &val, IRQ_PER_BANK) {
699 int pin_irq = irq_find_mapping(pctl->domain,
700 bank * IRQ_PER_BANK + irqoffset);
Maxime Ripard60242db2013-06-08 12:05:44 +0200701 generic_handle_irq(pin_irq);
702 }
Chen-Yu Tsai905a5112014-02-11 00:22:37 +0800703 chained_irq_exit(chip, desc);
Maxime Ripard60242db2013-06-08 12:05:44 +0200704 }
705}
706
Maxime Ripard0e37f882013-01-18 22:30:34 +0100707static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
708 const char *name)
709{
710 struct sunxi_pinctrl_function *func = pctl->functions;
711
712 while (func->name) {
713 /* function already there */
714 if (strcmp(func->name, name) == 0) {
715 func->ngroups++;
716 return -EEXIST;
717 }
718 func++;
719 }
720
721 func->name = name;
722 func->ngroups = 1;
723
724 pctl->nfunctions++;
725
726 return 0;
727}
728
729static int sunxi_pinctrl_build_state(struct platform_device *pdev)
730{
731 struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
732 int i;
733
734 pctl->ngroups = pctl->desc->npins;
735
736 /* Allocate groups */
737 pctl->groups = devm_kzalloc(&pdev->dev,
738 pctl->ngroups * sizeof(*pctl->groups),
739 GFP_KERNEL);
740 if (!pctl->groups)
741 return -ENOMEM;
742
743 for (i = 0; i < pctl->desc->npins; i++) {
744 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
745 struct sunxi_pinctrl_group *group = pctl->groups + i;
746
747 group->name = pin->pin.name;
748 group->pin = pin->pin.number;
749 }
750
751 /*
752 * We suppose that we won't have any more functions than pins,
753 * we'll reallocate that later anyway
754 */
755 pctl->functions = devm_kzalloc(&pdev->dev,
756 pctl->desc->npins * sizeof(*pctl->functions),
757 GFP_KERNEL);
758 if (!pctl->functions)
759 return -ENOMEM;
760
761 /* Count functions and their associated groups */
762 for (i = 0; i < pctl->desc->npins; i++) {
763 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
764 struct sunxi_desc_function *func = pin->functions;
765
766 while (func->name) {
Chen-Yu Tsaid54e9a22014-05-26 09:47:56 +0200767 /* Create interrupt mapping while we're at it */
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200768 if (!strcmp(func->name, "irq")) {
769 int irqnum = func->irqnum + func->irqbank * IRQ_PER_BANK;
770 pctl->irq_array[irqnum] = pin->pin.number;
771 }
772
Maxime Ripard0e37f882013-01-18 22:30:34 +0100773 sunxi_pinctrl_add_function(pctl, func->name);
774 func++;
775 }
776 }
777
778 pctl->functions = krealloc(pctl->functions,
779 pctl->nfunctions * sizeof(*pctl->functions),
780 GFP_KERNEL);
781
782 for (i = 0; i < pctl->desc->npins; i++) {
783 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
784 struct sunxi_desc_function *func = pin->functions;
785
786 while (func->name) {
787 struct sunxi_pinctrl_function *func_item;
788 const char **func_grp;
789
790 func_item = sunxi_pinctrl_find_function_by_name(pctl,
791 func->name);
792 if (!func_item)
793 return -EINVAL;
794
795 if (!func_item->groups) {
796 func_item->groups =
797 devm_kzalloc(&pdev->dev,
798 func_item->ngroups * sizeof(*func_item->groups),
799 GFP_KERNEL);
800 if (!func_item->groups)
801 return -ENOMEM;
802 }
803
804 func_grp = func_item->groups;
805 while (*func_grp)
806 func_grp++;
807
808 *func_grp = pin->pin.name;
809 func++;
810 }
811 }
812
813 return 0;
814}
815
Maxime Ripard2284ba62014-04-18 20:10:41 +0200816int sunxi_pinctrl_init(struct platform_device *pdev,
817 const struct sunxi_pinctrl_desc *desc)
Maxime Ripard0e37f882013-01-18 22:30:34 +0100818{
819 struct device_node *node = pdev->dev.of_node;
Maxime Ripardba6764d2014-05-22 16:25:27 +0200820 struct pinctrl_desc *pctrl_desc;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100821 struct pinctrl_pin_desc *pins;
822 struct sunxi_pinctrl *pctl;
Maxime Ripard4409caf2014-04-26 21:59:50 +0200823 struct resource *res;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100824 int i, ret, last_pin;
Emilio López950707c2013-03-22 11:20:40 -0300825 struct clk *clk;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100826
827 pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
828 if (!pctl)
829 return -ENOMEM;
830 platform_set_drvdata(pdev, pctl);
831
Maxime Ripard1bee9632013-08-04 12:38:48 +0200832 spin_lock_init(&pctl->lock);
833
Maxime Ripard4409caf2014-04-26 21:59:50 +0200834 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
835 pctl->membase = devm_ioremap_resource(&pdev->dev, res);
836 if (IS_ERR(pctl->membase))
837 return PTR_ERR(pctl->membase);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100838
Maxime Ripardba6764d2014-05-22 16:25:27 +0200839 pctl->dev = &pdev->dev;
Maxime Ripard2284ba62014-04-18 20:10:41 +0200840 pctl->desc = desc;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100841
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200842 pctl->irq_array = devm_kcalloc(&pdev->dev,
843 IRQ_PER_BANK * pctl->desc->irq_banks,
844 sizeof(*pctl->irq_array),
845 GFP_KERNEL);
846 if (!pctl->irq_array)
847 return -ENOMEM;
848
Maxime Ripard0e37f882013-01-18 22:30:34 +0100849 ret = sunxi_pinctrl_build_state(pdev);
850 if (ret) {
851 dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
852 return ret;
853 }
854
855 pins = devm_kzalloc(&pdev->dev,
856 pctl->desc->npins * sizeof(*pins),
857 GFP_KERNEL);
858 if (!pins)
859 return -ENOMEM;
860
861 for (i = 0; i < pctl->desc->npins; i++)
862 pins[i] = pctl->desc->pins[i].pin;
863
Maxime Ripardba6764d2014-05-22 16:25:27 +0200864 pctrl_desc = devm_kzalloc(&pdev->dev,
865 sizeof(*pctrl_desc),
866 GFP_KERNEL);
867 if (!pctrl_desc)
868 return -ENOMEM;
869
870 pctrl_desc->name = dev_name(&pdev->dev);
871 pctrl_desc->owner = THIS_MODULE;
872 pctrl_desc->pins = pins;
873 pctrl_desc->npins = pctl->desc->npins;
874 pctrl_desc->confops = &sunxi_pconf_ops;
875 pctrl_desc->pctlops = &sunxi_pctrl_ops;
876 pctrl_desc->pmxops = &sunxi_pmx_ops;
877
878 pctl->pctl_dev = pinctrl_register(pctrl_desc,
Maxime Ripard0e37f882013-01-18 22:30:34 +0100879 &pdev->dev, pctl);
880 if (!pctl->pctl_dev) {
881 dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
882 return -EINVAL;
883 }
884
Maxime Ripard08e9e612013-01-28 21:33:12 +0100885 pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
886 if (!pctl->chip) {
887 ret = -ENOMEM;
888 goto pinctrl_error;
889 }
890
891 last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
Boris BREZILLONd83c82c2014-04-10 15:52:43 +0200892 pctl->chip->owner = THIS_MODULE;
893 pctl->chip->request = sunxi_pinctrl_gpio_request,
894 pctl->chip->free = sunxi_pinctrl_gpio_free,
895 pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input,
896 pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output,
897 pctl->chip->get = sunxi_pinctrl_gpio_get,
898 pctl->chip->set = sunxi_pinctrl_gpio_set,
899 pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate,
900 pctl->chip->to_irq = sunxi_pinctrl_gpio_to_irq,
901 pctl->chip->of_gpio_n_cells = 3,
902 pctl->chip->can_sleep = false,
903 pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK) -
904 pctl->desc->pin_base;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100905 pctl->chip->label = dev_name(&pdev->dev);
906 pctl->chip->dev = &pdev->dev;
Boris BREZILLONd83c82c2014-04-10 15:52:43 +0200907 pctl->chip->base = pctl->desc->pin_base;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100908
909 ret = gpiochip_add(pctl->chip);
910 if (ret)
911 goto pinctrl_error;
912
913 for (i = 0; i < pctl->desc->npins; i++) {
914 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
915
916 ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
917 pin->pin.number,
918 pin->pin.number, 1);
919 if (ret)
920 goto gpiochip_error;
921 }
922
Emilio López950707c2013-03-22 11:20:40 -0300923 clk = devm_clk_get(&pdev->dev, NULL);
Wei Yongjund72f88a2013-05-23 17:32:14 +0800924 if (IS_ERR(clk)) {
925 ret = PTR_ERR(clk);
Emilio López950707c2013-03-22 11:20:40 -0300926 goto gpiochip_error;
Wei Yongjund72f88a2013-05-23 17:32:14 +0800927 }
Emilio López950707c2013-03-22 11:20:40 -0300928
Boris BREZILLON64150932014-04-10 15:52:40 +0200929 ret = clk_prepare_enable(clk);
930 if (ret)
931 goto gpiochip_error;
Emilio López950707c2013-03-22 11:20:40 -0300932
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200933 pctl->irq = devm_kcalloc(&pdev->dev,
934 pctl->desc->irq_banks,
935 sizeof(*pctl->irq),
936 GFP_KERNEL);
Maxime Ripard60242db2013-06-08 12:05:44 +0200937 if (!pctl->irq) {
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200938 ret = -ENOMEM;
Maxime Riparddc969102014-04-26 22:28:54 +0200939 goto clk_error;
Maxime Ripard60242db2013-06-08 12:05:44 +0200940 }
941
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200942 for (i = 0; i < pctl->desc->irq_banks; i++) {
943 pctl->irq[i] = platform_get_irq(pdev, i);
944 if (pctl->irq[i] < 0) {
945 ret = pctl->irq[i];
946 goto clk_error;
947 }
948 }
949
950 pctl->domain = irq_domain_add_linear(node,
951 pctl->desc->irq_banks * IRQ_PER_BANK,
952 &irq_domain_simple_ops,
953 NULL);
Maxime Ripard60242db2013-06-08 12:05:44 +0200954 if (!pctl->domain) {
955 dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
956 ret = -ENOMEM;
Maxime Riparddc969102014-04-26 22:28:54 +0200957 goto clk_error;
Maxime Ripard60242db2013-06-08 12:05:44 +0200958 }
959
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200960 for (i = 0; i < (pctl->desc->irq_banks * IRQ_PER_BANK); i++) {
Maxime Ripard60242db2013-06-08 12:05:44 +0200961 int irqno = irq_create_mapping(pctl->domain, i);
962
Hans de Goedef4c51c12014-06-29 16:11:01 +0200963 irq_set_chip_and_handler(irqno, &sunxi_pinctrl_edge_irq_chip,
964 handle_edge_irq);
Maxime Ripard60242db2013-06-08 12:05:44 +0200965 irq_set_chip_data(irqno, pctl);
966 };
967
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200968 for (i = 0; i < pctl->desc->irq_banks; i++) {
Hans de Goedef4c51c12014-06-29 16:11:01 +0200969 /* Mask and clear all IRQs before registering a handler */
970 writel(0, pctl->membase + sunxi_irq_ctrl_reg_from_bank(i));
971 writel(0xffffffff,
972 pctl->membase + sunxi_irq_status_reg_from_bank(i));
973
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200974 irq_set_chained_handler(pctl->irq[i],
975 sunxi_pinctrl_irq_handler);
976 irq_set_handler_data(pctl->irq[i], pctl);
977 }
Maxime Ripard60242db2013-06-08 12:05:44 +0200978
Maxime Ripard08e9e612013-01-28 21:33:12 +0100979 dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
Maxime Ripard0e37f882013-01-18 22:30:34 +0100980
981 return 0;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100982
Boris BREZILLONe2bddc62014-04-10 15:52:41 +0200983clk_error:
984 clk_disable_unprepare(clk);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100985gpiochip_error:
abdoulaye bertheb4e7c552014-07-12 22:30:13 +0200986 gpiochip_remove(pctl->chip);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100987pinctrl_error:
988 pinctrl_unregister(pctl->pctl_dev);
989 return ret;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100990}