blob: 131adf7953cd84e3b5e82ee394ad6d3f5bf29ca9 [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>
Linus Walleij88057d62015-12-08 22:40:43 +010015#include <linux/gpio/driver.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>
Paul Gortmakerbcc76192016-02-29 15:48:37 -050018#include <linux/export.h>
Maxime Ripard0e37f882013-01-18 22:30:34 +010019#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 Ripard42676fa2016-10-11 17:46:00 +020031#include <dt-bindings/pinctrl/sun4i-a10.h>
32
Maxime Ripard5f910772014-04-18 18:53:02 +020033#include "../core.h"
Maxime Ripard0e37f882013-01-18 22:30:34 +010034#include "pinctrl-sunxi.h"
Maxime Ripardeaa3d842013-01-18 22:30:35 +010035
Hans de Goedef4c51c12014-06-29 16:11:01 +020036static struct irq_chip sunxi_pinctrl_edge_irq_chip;
37static struct irq_chip sunxi_pinctrl_level_irq_chip;
38
Maxime Ripard0e37f882013-01-18 22:30:34 +010039static struct sunxi_pinctrl_group *
40sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl *pctl, const char *group)
41{
42 int i;
43
44 for (i = 0; i < pctl->ngroups; i++) {
45 struct sunxi_pinctrl_group *grp = pctl->groups + i;
46
47 if (!strcmp(grp->name, group))
48 return grp;
49 }
50
51 return NULL;
52}
53
54static struct sunxi_pinctrl_function *
55sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl *pctl,
56 const char *name)
57{
58 struct sunxi_pinctrl_function *func = pctl->functions;
59 int i;
60
61 for (i = 0; i < pctl->nfunctions; i++) {
62 if (!func[i].name)
63 break;
64
65 if (!strcmp(func[i].name, name))
66 return func + i;
67 }
68
69 return NULL;
70}
71
72static struct sunxi_desc_function *
73sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl *pctl,
74 const char *pin_name,
75 const char *func_name)
76{
77 int i;
78
79 for (i = 0; i < pctl->desc->npins; i++) {
80 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
81
82 if (!strcmp(pin->pin.name, pin_name)) {
83 struct sunxi_desc_function *func = pin->functions;
84
85 while (func->name) {
hao_zhang32e21f02018-01-09 13:59:02 +080086 if (!strcmp(func->name, func_name) &&
87 (!func->variant ||
88 func->variant & pctl->variant))
Maxime Ripard0e37f882013-01-18 22:30:34 +010089 return func;
90
91 func++;
92 }
93 }
94 }
95
96 return NULL;
97}
98
Maxime Ripard814d4f22013-06-08 12:05:43 +020099static struct sunxi_desc_function *
100sunxi_pinctrl_desc_find_function_by_pin(struct sunxi_pinctrl *pctl,
101 const u16 pin_num,
102 const char *func_name)
103{
104 int i;
105
106 for (i = 0; i < pctl->desc->npins; i++) {
107 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
108
109 if (pin->pin.number == pin_num) {
110 struct sunxi_desc_function *func = pin->functions;
111
112 while (func->name) {
113 if (!strcmp(func->name, func_name))
114 return func;
115
116 func++;
117 }
118 }
119 }
120
121 return NULL;
122}
123
Maxime Ripard0e37f882013-01-18 22:30:34 +0100124static int sunxi_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
125{
126 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
127
128 return pctl->ngroups;
129}
130
131static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev *pctldev,
132 unsigned group)
133{
134 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
135
136 return pctl->groups[group].name;
137}
138
139static int sunxi_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
140 unsigned group,
141 const unsigned **pins,
142 unsigned *num_pins)
143{
144 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
145
146 *pins = (unsigned *)&pctl->groups[group].pin;
147 *num_pins = 1;
148
149 return 0;
150}
151
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200152static bool sunxi_pctrl_has_bias_prop(struct device_node *node)
153{
Maxime Ripardcefbf1a2016-10-20 15:49:03 +0200154 return of_find_property(node, "bias-pull-up", NULL) ||
155 of_find_property(node, "bias-pull-down", NULL) ||
156 of_find_property(node, "bias-disable", NULL) ||
157 of_find_property(node, "allwinner,pull", NULL);
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200158}
159
160static bool sunxi_pctrl_has_drive_prop(struct device_node *node)
161{
Maxime Ripardcefbf1a2016-10-20 15:49:03 +0200162 return of_find_property(node, "drive-strength", NULL) ||
163 of_find_property(node, "allwinner,drive", NULL);
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200164}
165
166static int sunxi_pctrl_parse_bias_prop(struct device_node *node)
167{
168 u32 val;
169
Maxime Ripardcefbf1a2016-10-20 15:49:03 +0200170 /* Try the new style binding */
171 if (of_find_property(node, "bias-pull-up", NULL))
172 return PIN_CONFIG_BIAS_PULL_UP;
173
174 if (of_find_property(node, "bias-pull-down", NULL))
175 return PIN_CONFIG_BIAS_PULL_DOWN;
176
177 if (of_find_property(node, "bias-disable", NULL))
178 return PIN_CONFIG_BIAS_DISABLE;
179
180 /* And fall back to the old binding */
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200181 if (of_property_read_u32(node, "allwinner,pull", &val))
182 return -EINVAL;
183
184 switch (val) {
Maxime Ripard07fe64b2016-10-11 17:46:01 +0200185 case SUN4I_PINCTRL_NO_PULL:
186 return PIN_CONFIG_BIAS_DISABLE;
Maxime Ripard42676fa2016-10-11 17:46:00 +0200187 case SUN4I_PINCTRL_PULL_UP:
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200188 return PIN_CONFIG_BIAS_PULL_UP;
Maxime Ripard42676fa2016-10-11 17:46:00 +0200189 case SUN4I_PINCTRL_PULL_DOWN:
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200190 return PIN_CONFIG_BIAS_PULL_DOWN;
191 }
192
193 return -EINVAL;
194}
195
196static int sunxi_pctrl_parse_drive_prop(struct device_node *node)
197{
198 u32 val;
199
Maxime Ripardcefbf1a2016-10-20 15:49:03 +0200200 /* Try the new style binding */
201 if (!of_property_read_u32(node, "drive-strength", &val)) {
202 /* We can't go below 10mA ... */
203 if (val < 10)
204 return -EINVAL;
205
206 /* ... and only up to 40 mA ... */
207 if (val > 40)
208 val = 40;
209
210 /* by steps of 10 mA */
211 return rounddown(val, 10);
212 }
213
214 /* And then fall back to the old binding */
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200215 if (of_property_read_u32(node, "allwinner,drive", &val))
216 return -EINVAL;
217
218 return (val + 1) * 10;
219}
220
221static const char *sunxi_pctrl_parse_function_prop(struct device_node *node)
222{
223 const char *function;
224 int ret;
225
Maxime Ripardcefbf1a2016-10-20 15:49:03 +0200226 /* Try the generic binding */
227 ret = of_property_read_string(node, "function", &function);
228 if (!ret)
229 return function;
230
231 /* And fall back to our legacy one */
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200232 ret = of_property_read_string(node, "allwinner,function", &function);
233 if (!ret)
234 return function;
235
236 return NULL;
237}
238
239static const char *sunxi_pctrl_find_pins_prop(struct device_node *node,
240 int *npins)
241{
242 int count;
243
Maxime Ripardcefbf1a2016-10-20 15:49:03 +0200244 /* Try the generic binding */
245 count = of_property_count_strings(node, "pins");
246 if (count > 0) {
247 *npins = count;
248 return "pins";
249 }
250
251 /* And fall back to our legacy one */
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200252 count = of_property_count_strings(node, "allwinner,pins");
253 if (count > 0) {
254 *npins = count;
255 return "allwinner,pins";
256 }
257
258 return NULL;
259}
260
261static unsigned long *sunxi_pctrl_build_pin_config(struct device_node *node,
262 unsigned int *len)
263{
264 unsigned long *pinconfig;
265 unsigned int configlen = 0, idx = 0;
Maxime Riparde11dee22016-10-20 15:49:02 +0200266 int ret;
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200267
268 if (sunxi_pctrl_has_drive_prop(node))
269 configlen++;
270 if (sunxi_pctrl_has_bias_prop(node))
271 configlen++;
272
Maxime Riparde11dee22016-10-20 15:49:02 +0200273 /*
274 * If we don't have any configuration, bail out
275 */
276 if (!configlen)
277 return NULL;
278
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200279 pinconfig = kzalloc(configlen * sizeof(*pinconfig), GFP_KERNEL);
280 if (!pinconfig)
Maxime Riparde11dee22016-10-20 15:49:02 +0200281 return ERR_PTR(-ENOMEM);
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200282
283 if (sunxi_pctrl_has_drive_prop(node)) {
284 int drive = sunxi_pctrl_parse_drive_prop(node);
Maxime Riparde11dee22016-10-20 15:49:02 +0200285 if (drive < 0) {
286 ret = drive;
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200287 goto err_free;
Maxime Riparde11dee22016-10-20 15:49:02 +0200288 }
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200289
290 pinconfig[idx++] = pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH,
291 drive);
292 }
293
294 if (sunxi_pctrl_has_bias_prop(node)) {
295 int pull = sunxi_pctrl_parse_bias_prop(node);
Chen-Yu Tsai223dba02016-11-11 17:50:34 +0800296 int arg = 0;
Maxime Riparde11dee22016-10-20 15:49:02 +0200297 if (pull < 0) {
298 ret = pull;
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200299 goto err_free;
Maxime Riparde11dee22016-10-20 15:49:02 +0200300 }
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200301
Chen-Yu Tsai223dba02016-11-11 17:50:34 +0800302 if (pull != PIN_CONFIG_BIAS_DISABLE)
303 arg = 1; /* hardware uses weak pull resistors */
304
305 pinconfig[idx++] = pinconf_to_config_packed(pull, arg);
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200306 }
307
308
309 *len = configlen;
310 return pinconfig;
311
312err_free:
313 kfree(pinconfig);
Maxime Riparde11dee22016-10-20 15:49:02 +0200314 return ERR_PTR(ret);
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200315}
316
Maxime Ripard0e37f882013-01-18 22:30:34 +0100317static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
318 struct device_node *node,
319 struct pinctrl_map **map,
320 unsigned *num_maps)
321{
322 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
323 unsigned long *pinconfig;
324 struct property *prop;
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200325 const char *function, *pin_prop;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100326 const char *group;
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200327 int ret, npins, nmaps, configlen = 0, i = 0;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100328
329 *map = NULL;
330 *num_maps = 0;
331
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200332 function = sunxi_pctrl_parse_function_prop(node);
333 if (!function) {
334 dev_err(pctl->dev, "missing function property in node %s\n",
Maxime Ripard0e37f882013-01-18 22:30:34 +0100335 node->name);
336 return -EINVAL;
337 }
338
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200339 pin_prop = sunxi_pctrl_find_pins_prop(node, &npins);
340 if (!pin_prop) {
341 dev_err(pctl->dev, "missing pins property in node %s\n",
Maxime Ripard0e37f882013-01-18 22:30:34 +0100342 node->name);
343 return -EINVAL;
344 }
345
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200346 /*
347 * We have two maps for each pin: one for the function, one
Maxime Riparde11dee22016-10-20 15:49:02 +0200348 * for the configuration (bias, strength, etc).
349 *
350 * We might be slightly overshooting, since we might not have
351 * any configuration.
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200352 */
353 nmaps = npins * 2;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100354 *map = kmalloc(nmaps * sizeof(struct pinctrl_map), GFP_KERNEL);
Sachin Kamat3efa9212013-07-29 13:49:32 +0530355 if (!*map)
Maxime Ripard0e37f882013-01-18 22:30:34 +0100356 return -ENOMEM;
357
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200358 pinconfig = sunxi_pctrl_build_pin_config(node, &configlen);
Maxime Riparde11dee22016-10-20 15:49:02 +0200359 if (IS_ERR(pinconfig)) {
360 ret = PTR_ERR(pinconfig);
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200361 goto err_free_map;
362 }
363
364 of_property_for_each_string(node, pin_prop, prop, group) {
Maxime Ripard0e37f882013-01-18 22:30:34 +0100365 struct sunxi_pinctrl_group *grp =
366 sunxi_pinctrl_find_group_by_name(pctl, group);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100367
368 if (!grp) {
369 dev_err(pctl->dev, "unknown pin %s", group);
370 continue;
371 }
372
373 if (!sunxi_pinctrl_desc_find_function_by_name(pctl,
374 grp->name,
375 function)) {
376 dev_err(pctl->dev, "unsupported function %s on pin %s",
377 function, group);
378 continue;
379 }
380
381 (*map)[i].type = PIN_MAP_TYPE_MUX_GROUP;
382 (*map)[i].data.mux.group = group;
383 (*map)[i].data.mux.function = function;
384
385 i++;
386
Maxime Riparde11dee22016-10-20 15:49:02 +0200387 if (pinconfig) {
388 (*map)[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
389 (*map)[i].data.configs.group_or_pin = group;
390 (*map)[i].data.configs.configs = pinconfig;
391 (*map)[i].data.configs.num_configs = configlen;
392 i++;
393 }
Maxime Ripard0e37f882013-01-18 22:30:34 +0100394 }
395
Maxime Riparde11dee22016-10-20 15:49:02 +0200396 *num_maps = i;
397
398 /*
399 * We know have the number of maps we need, we can resize our
400 * map array
401 */
402 *map = krealloc(*map, i * sizeof(struct pinctrl_map), GFP_KERNEL);
Dan Carpenterb3cde192016-11-18 14:35:57 +0300403 if (!*map)
Maxime Riparde11dee22016-10-20 15:49:02 +0200404 return -ENOMEM;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100405
406 return 0;
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200407
408err_free_map:
Dan Carpenterb3cde192016-11-18 14:35:57 +0300409 kfree(*map);
410 *map = NULL;
Maxime Ripardf233dbc2016-10-11 17:45:59 +0200411 return ret;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100412}
413
414static void sunxi_pctrl_dt_free_map(struct pinctrl_dev *pctldev,
415 struct pinctrl_map *map,
416 unsigned num_maps)
417{
Chen-Yu Tsai88f01a12016-11-11 10:35:10 +0800418 int i;
419
420 /* pin config is never in the first map */
421 for (i = 1; i < num_maps; i++) {
422 if (map[i].type != PIN_MAP_TYPE_CONFIGS_GROUP)
423 continue;
424
425 /*
426 * All the maps share the same pin config,
427 * free only the first one we find.
428 */
429 kfree(map[i].data.configs.configs);
430 break;
431 }
432
Maxime Ripard0e37f882013-01-18 22:30:34 +0100433 kfree(map);
434}
435
Laurent Pinchart022ab142013-02-16 10:25:07 +0100436static const struct pinctrl_ops sunxi_pctrl_ops = {
Maxime Ripard0e37f882013-01-18 22:30:34 +0100437 .dt_node_to_map = sunxi_pctrl_dt_node_to_map,
438 .dt_free_map = sunxi_pctrl_dt_free_map,
439 .get_groups_count = sunxi_pctrl_get_groups_count,
440 .get_group_name = sunxi_pctrl_get_group_name,
441 .get_group_pins = sunxi_pctrl_get_group_pins,
442};
443
Chen-Yu Tsaic5fda172016-11-11 17:50:35 +0800444static int sunxi_pconf_reg(unsigned pin, enum pin_config_param param,
445 u32 *offset, u32 *shift, u32 *mask)
446{
447 switch (param) {
448 case PIN_CONFIG_DRIVE_STRENGTH:
449 *offset = sunxi_dlevel_reg(pin);
450 *shift = sunxi_dlevel_offset(pin);
451 *mask = DLEVEL_PINS_MASK;
452 break;
453
454 case PIN_CONFIG_BIAS_PULL_UP:
455 case PIN_CONFIG_BIAS_PULL_DOWN:
456 case PIN_CONFIG_BIAS_DISABLE:
457 *offset = sunxi_pull_reg(pin);
458 *shift = sunxi_pull_offset(pin);
459 *mask = PULL_PINS_MASK;
460 break;
461
462 default:
463 return -ENOTSUPP;
464 }
465
466 return 0;
467}
468
469static int sunxi_pconf_get(struct pinctrl_dev *pctldev, unsigned pin,
470 unsigned long *config)
471{
472 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
473 enum pin_config_param param = pinconf_to_config_param(*config);
474 u32 offset, shift, mask, val;
475 u16 arg;
476 int ret;
477
478 pin -= pctl->desc->pin_base;
479
480 ret = sunxi_pconf_reg(pin, param, &offset, &shift, &mask);
481 if (ret < 0)
482 return ret;
483
484 val = (readl(pctl->membase + offset) >> shift) & mask;
485
486 switch (pinconf_to_config_param(*config)) {
487 case PIN_CONFIG_DRIVE_STRENGTH:
488 arg = (val + 1) * 10;
489 break;
490
491 case PIN_CONFIG_BIAS_PULL_UP:
492 if (val != SUN4I_PINCTRL_PULL_UP)
493 return -EINVAL;
494 arg = 1; /* hardware is weak pull-up */
495 break;
496
497 case PIN_CONFIG_BIAS_PULL_DOWN:
498 if (val != SUN4I_PINCTRL_PULL_DOWN)
499 return -EINVAL;
500 arg = 1; /* hardware is weak pull-down */
501 break;
502
503 case PIN_CONFIG_BIAS_DISABLE:
504 if (val != SUN4I_PINCTRL_NO_PULL)
505 return -EINVAL;
506 arg = 0;
507 break;
508
509 default:
510 /* sunxi_pconf_reg should catch anything unsupported */
511 WARN_ON(1);
512 return -ENOTSUPP;
513 }
514
515 *config = pinconf_to_config_packed(param, arg);
516
517 return 0;
518}
519
Maxime Ripard0e37f882013-01-18 22:30:34 +0100520static int sunxi_pconf_group_get(struct pinctrl_dev *pctldev,
521 unsigned group,
522 unsigned long *config)
523{
524 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
Chen-Yu Tsaic5fda172016-11-11 17:50:35 +0800525 struct sunxi_pinctrl_group *g = &pctl->groups[group];
Maxime Ripard0e37f882013-01-18 22:30:34 +0100526
Chen-Yu Tsaic5fda172016-11-11 17:50:35 +0800527 /* We only support 1 pin per group. Chain it to the pin callback */
528 return sunxi_pconf_get(pctldev, g->pin, config);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100529}
530
531static int sunxi_pconf_group_set(struct pinctrl_dev *pctldev,
532 unsigned group,
Sherman Yin03b054e2013-08-27 11:32:12 -0700533 unsigned long *configs,
534 unsigned num_configs)
Maxime Ripard0e37f882013-01-18 22:30:34 +0100535{
536 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
537 struct sunxi_pinctrl_group *g = &pctl->groups[group];
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800538 unsigned pin = g->pin - pctl->desc->pin_base;
Sherman Yin03b054e2013-08-27 11:32:12 -0700539 int i;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100540
Sherman Yin03b054e2013-08-27 11:32:12 -0700541 for (i = 0; i < num_configs; i++) {
Chen-Yu Tsai51814822016-11-11 17:50:36 +0800542 enum pin_config_param param;
543 unsigned long flags;
544 u32 offset, shift, mask, reg;
Mika Westerberg58957d22017-01-23 15:34:32 +0300545 u32 arg, val;
Chen-Yu Tsai51814822016-11-11 17:50:36 +0800546 int ret;
547
548 param = pinconf_to_config_param(configs[i]);
549 arg = pinconf_to_config_argument(configs[i]);
550
551 ret = sunxi_pconf_reg(pin, param, &offset, &shift, &mask);
552 if (ret < 0)
553 return ret;
554
555 switch (param) {
Sherman Yin03b054e2013-08-27 11:32:12 -0700556 case PIN_CONFIG_DRIVE_STRENGTH:
Chen-Yu Tsai51814822016-11-11 17:50:36 +0800557 if (arg < 10 || arg > 40)
Sherman Yin03b054e2013-08-27 11:32:12 -0700558 return -EINVAL;
559 /*
560 * We convert from mA to what the register expects:
561 * 0: 10mA
562 * 1: 20mA
563 * 2: 30mA
564 * 3: 40mA
565 */
Chen-Yu Tsai51814822016-11-11 17:50:36 +0800566 val = arg / 10 - 1;
Sherman Yin03b054e2013-08-27 11:32:12 -0700567 break;
Maxime Ripard07fe64b2016-10-11 17:46:01 +0200568 case PIN_CONFIG_BIAS_DISABLE:
Priit Laesac059e22017-08-27 15:55:23 +0300569 val = 0;
570 break;
Sherman Yin03b054e2013-08-27 11:32:12 -0700571 case PIN_CONFIG_BIAS_PULL_UP:
Chen-Yu Tsai51814822016-11-11 17:50:36 +0800572 if (arg == 0)
573 return -EINVAL;
574 val = 1;
Sherman Yin03b054e2013-08-27 11:32:12 -0700575 break;
576 case PIN_CONFIG_BIAS_PULL_DOWN:
Chen-Yu Tsai51814822016-11-11 17:50:36 +0800577 if (arg == 0)
578 return -EINVAL;
579 val = 2;
Sherman Yin03b054e2013-08-27 11:32:12 -0700580 break;
581 default:
Chen-Yu Tsai51814822016-11-11 17:50:36 +0800582 /* sunxi_pconf_reg should catch anything unsupported */
583 WARN_ON(1);
584 return -ENOTSUPP;
Sherman Yin03b054e2013-08-27 11:32:12 -0700585 }
Maxime Ripard0e37f882013-01-18 22:30:34 +0100586
Julia Cartwrightf658ed32017-03-09 10:22:06 -0600587 raw_spin_lock_irqsave(&pctl->lock, flags);
Chen-Yu Tsai51814822016-11-11 17:50:36 +0800588 reg = readl(pctl->membase + offset);
589 reg &= ~(mask << shift);
590 writel(reg | val << shift, pctl->membase + offset);
Julia Cartwrightf658ed32017-03-09 10:22:06 -0600591 raw_spin_unlock_irqrestore(&pctl->lock, flags);
Chen-Yu Tsai51814822016-11-11 17:50:36 +0800592 } /* for each config */
Maxime Ripard0e37f882013-01-18 22:30:34 +0100593
594 return 0;
595}
596
Laurent Pinchart022ab142013-02-16 10:25:07 +0100597static const struct pinconf_ops sunxi_pconf_ops = {
Chen-Yu Tsaic5fda172016-11-11 17:50:35 +0800598 .is_generic = true,
599 .pin_config_get = sunxi_pconf_get,
Maxime Ripard0e37f882013-01-18 22:30:34 +0100600 .pin_config_group_get = sunxi_pconf_group_get,
601 .pin_config_group_set = sunxi_pconf_group_set,
602};
603
604static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
605{
606 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
607
608 return pctl->nfunctions;
609}
610
611static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev,
612 unsigned function)
613{
614 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
615
616 return pctl->functions[function].name;
617}
618
619static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev,
620 unsigned function,
621 const char * const **groups,
622 unsigned * const num_groups)
623{
624 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
625
626 *groups = pctl->functions[function].groups;
627 *num_groups = pctl->functions[function].ngroups;
628
629 return 0;
630}
631
632static void sunxi_pmx_set(struct pinctrl_dev *pctldev,
633 unsigned pin,
634 u8 config)
635{
636 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200637 unsigned long flags;
638 u32 val, mask;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100639
Julia Cartwrightf658ed32017-03-09 10:22:06 -0600640 raw_spin_lock_irqsave(&pctl->lock, flags);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200641
Chen-Yu Tsaib4575c62014-05-22 23:20:55 +0800642 pin -= pctl->desc->pin_base;
Maxime Ripard1bee9632013-08-04 12:38:48 +0200643 val = readl(pctl->membase + sunxi_mux_reg(pin));
644 mask = MUX_PINS_MASK << sunxi_mux_offset(pin);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100645 writel((val & ~mask) | config << sunxi_mux_offset(pin),
646 pctl->membase + sunxi_mux_reg(pin));
Maxime Ripard1bee9632013-08-04 12:38:48 +0200647
Julia Cartwrightf658ed32017-03-09 10:22:06 -0600648 raw_spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100649}
650
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200651static int sunxi_pmx_set_mux(struct pinctrl_dev *pctldev,
652 unsigned function,
653 unsigned group)
Maxime Ripard0e37f882013-01-18 22:30:34 +0100654{
655 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
656 struct sunxi_pinctrl_group *g = pctl->groups + group;
657 struct sunxi_pinctrl_function *func = pctl->functions + function;
658 struct sunxi_desc_function *desc =
659 sunxi_pinctrl_desc_find_function_by_name(pctl,
660 g->name,
661 func->name);
662
663 if (!desc)
664 return -EINVAL;
665
666 sunxi_pmx_set(pctldev, g->pin, desc->muxval);
667
668 return 0;
669}
670
Maxime Ripard08e9e612013-01-28 21:33:12 +0100671static int
672sunxi_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
673 struct pinctrl_gpio_range *range,
674 unsigned offset,
675 bool input)
676{
677 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
678 struct sunxi_desc_function *desc;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100679 const char *func;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100680
681 if (input)
682 func = "gpio_in";
683 else
684 func = "gpio_out";
685
Maxime Ripard814d4f22013-06-08 12:05:43 +0200686 desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, func);
687 if (!desc)
688 return -EINVAL;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100689
690 sunxi_pmx_set(pctldev, offset, desc->muxval);
691
Maxime Ripard814d4f22013-06-08 12:05:43 +0200692 return 0;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100693}
694
Laurent Pinchart022ab142013-02-16 10:25:07 +0100695static const struct pinmux_ops sunxi_pmx_ops = {
Maxime Ripard0e37f882013-01-18 22:30:34 +0100696 .get_functions_count = sunxi_pmx_get_funcs_cnt,
697 .get_function_name = sunxi_pmx_get_func_name,
698 .get_function_groups = sunxi_pmx_get_func_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200699 .set_mux = sunxi_pmx_set_mux,
Maxime Ripard08e9e612013-01-28 21:33:12 +0100700 .gpio_set_direction = sunxi_pmx_gpio_set_direction,
Maxime Ripard13960072017-10-09 22:53:39 +0200701 .strict = true,
Maxime Ripard0e37f882013-01-18 22:30:34 +0100702};
703
Maxime Ripard08e9e612013-01-28 21:33:12 +0100704static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip *chip,
705 unsigned offset)
706{
707 return pinctrl_gpio_direction_input(chip->base + offset);
708}
709
710static int sunxi_pinctrl_gpio_get(struct gpio_chip *chip, unsigned offset)
711{
Linus Walleij88057d62015-12-08 22:40:43 +0100712 struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100713 u32 reg = sunxi_data_reg(offset);
714 u8 index = sunxi_data_offset(offset);
Linus Walleij6cee3822016-02-11 20:16:45 +0100715 bool set_mux = pctl->desc->irq_read_needs_mux &&
716 gpiochip_line_is_irq(chip, offset);
Krzysztof Adamskibe2d1072016-02-09 15:58:49 +0100717 u32 pin = offset + chip->base;
Hans de Goedeef6d24c2015-03-08 22:13:57 +0100718 u32 val;
719
720 if (set_mux)
Krzysztof Adamskibe2d1072016-02-09 15:58:49 +0100721 sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_INPUT);
Hans de Goedeef6d24c2015-03-08 22:13:57 +0100722
723 val = (readl(pctl->membase + reg) >> index) & DATA_PINS_MASK;
724
725 if (set_mux)
Krzysztof Adamskibe2d1072016-02-09 15:58:49 +0100726 sunxi_pmx_set(pctl->pctl_dev, pin, SUN4I_FUNC_IRQ);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100727
Linus Walleij39e24ac2015-12-21 16:40:27 +0100728 return !!val;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100729}
730
Maxime Ripard08e9e612013-01-28 21:33:12 +0100731static void sunxi_pinctrl_gpio_set(struct gpio_chip *chip,
732 unsigned offset, int value)
733{
Linus Walleij88057d62015-12-08 22:40:43 +0100734 struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100735 u32 reg = sunxi_data_reg(offset);
736 u8 index = sunxi_data_offset(offset);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200737 unsigned long flags;
738 u32 regval;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100739
Julia Cartwrightf658ed32017-03-09 10:22:06 -0600740 raw_spin_lock_irqsave(&pctl->lock, flags);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200741
742 regval = readl(pctl->membase + reg);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100743
Maxime Riparddf7b34f2013-07-25 12:41:16 +0200744 if (value)
745 regval |= BIT(index);
746 else
747 regval &= ~(BIT(index));
748
749 writel(regval, pctl->membase + reg);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200750
Julia Cartwrightf658ed32017-03-09 10:22:06 -0600751 raw_spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100752}
753
Chen-Yu Tsaifa8cf572014-01-16 14:34:23 +0800754static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
755 unsigned offset, int value)
756{
757 sunxi_pinctrl_gpio_set(chip, offset, value);
758 return pinctrl_gpio_direction_output(chip->base + offset);
759}
760
Maxime Riparda0d72092013-02-03 12:10:11 +0100761static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
762 const struct of_phandle_args *gpiospec,
763 u32 *flags)
764{
765 int pin, base;
766
767 base = PINS_PER_BANK * gpiospec->args[0];
768 pin = base + gpiospec->args[1];
769
Chen-Yu Tsai343f1322014-07-15 01:24:37 +0800770 if (pin > gc->ngpio)
Maxime Riparda0d72092013-02-03 12:10:11 +0100771 return -EINVAL;
772
773 if (flags)
774 *flags = gpiospec->args[2];
775
776 return pin;
777}
778
Maxime Ripard60242db2013-06-08 12:05:44 +0200779static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
780{
Linus Walleij88057d62015-12-08 22:40:43 +0100781 struct sunxi_pinctrl *pctl = gpiochip_get_data(chip);
Maxime Ripard60242db2013-06-08 12:05:44 +0200782 struct sunxi_desc_function *desc;
Chen-Yu Tsai343f1322014-07-15 01:24:37 +0800783 unsigned pinnum = pctl->desc->pin_base + offset;
Chen-Yu Tsai0d3bafa2014-07-01 00:04:59 +0800784 unsigned irqnum;
Maxime Ripard60242db2013-06-08 12:05:44 +0200785
Axel Linc9e3b2d2013-08-30 16:31:25 +0800786 if (offset >= chip->ngpio)
Maxime Ripard60242db2013-06-08 12:05:44 +0200787 return -ENXIO;
788
Chen-Yu Tsai343f1322014-07-15 01:24:37 +0800789 desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pinnum, "irq");
Maxime Ripard60242db2013-06-08 12:05:44 +0200790 if (!desc)
791 return -EINVAL;
792
Chen-Yu Tsai0d3bafa2014-07-01 00:04:59 +0800793 irqnum = desc->irqbank * IRQ_PER_BANK + desc->irqnum;
Maxime Ripard60242db2013-06-08 12:05:44 +0200794
Linus Walleij58383c782015-11-04 09:56:26 +0100795 dev_dbg(chip->parent, "%s: request IRQ for GPIO %d, return %d\n",
Chen-Yu Tsai0d3bafa2014-07-01 00:04:59 +0800796 chip->label, offset + chip->base, irqnum);
797
798 return irq_find_mapping(pctl->domain, irqnum);
Maxime Ripard60242db2013-06-08 12:05:44 +0200799}
800
Hans de Goedefea6d8e2014-06-29 16:11:00 +0200801static int sunxi_pinctrl_irq_request_resources(struct irq_data *d)
Maxime Ripard60242db2013-06-08 12:05:44 +0200802{
803 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
Hans de Goedefea6d8e2014-06-29 16:11:00 +0200804 struct sunxi_desc_function *func;
Chen-Yu Tsaif83549d2014-07-15 01:24:36 +0800805 int ret;
Hans de Goedefea6d8e2014-06-29 16:11:00 +0200806
807 func = sunxi_pinctrl_desc_find_function_by_pin(pctl,
808 pctl->irq_array[d->hwirq], "irq");
809 if (!func)
810 return -EINVAL;
811
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900812 ret = gpiochip_lock_as_irq(pctl->chip,
Chen-Yu Tsai343f1322014-07-15 01:24:37 +0800813 pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
Chen-Yu Tsaif83549d2014-07-15 01:24:36 +0800814 if (ret) {
815 dev_err(pctl->dev, "unable to lock HW IRQ %lu for IRQ\n",
816 irqd_to_hwirq(d));
817 return ret;
818 }
819
Hans de Goedefea6d8e2014-06-29 16:11:00 +0200820 /* Change muxing to INT mode */
821 sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval);
822
823 return 0;
824}
Maxime Ripard08e9e612013-01-28 21:33:12 +0100825
Chen-Yu Tsaif83549d2014-07-15 01:24:36 +0800826static void sunxi_pinctrl_irq_release_resources(struct irq_data *d)
827{
828 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
829
Alexandre Courbote3a2e872014-10-23 17:27:07 +0900830 gpiochip_unlock_as_irq(pctl->chip,
831 pctl->irq_array[d->hwirq] - pctl->desc->pin_base);
Chen-Yu Tsaif83549d2014-07-15 01:24:36 +0800832}
833
Hans de Goedef4c51c12014-06-29 16:11:01 +0200834static int sunxi_pinctrl_irq_set_type(struct irq_data *d, unsigned int type)
Maxime Ripard60242db2013-06-08 12:05:44 +0200835{
836 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
Hans de Goede5e7515b2016-03-12 19:44:57 +0100837 u32 reg = sunxi_irq_cfg_reg(d->hwirq, pctl->desc->irq_bank_base);
Maxime Ripard60242db2013-06-08 12:05:44 +0200838 u8 index = sunxi_irq_cfg_offset(d->hwirq);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200839 unsigned long flags;
Maxime Ripard2aaaddf2013-08-04 12:38:47 +0200840 u32 regval;
Maxime Ripard60242db2013-06-08 12:05:44 +0200841 u8 mode;
842
843 switch (type) {
844 case IRQ_TYPE_EDGE_RISING:
845 mode = IRQ_EDGE_RISING;
846 break;
847 case IRQ_TYPE_EDGE_FALLING:
848 mode = IRQ_EDGE_FALLING;
849 break;
850 case IRQ_TYPE_EDGE_BOTH:
851 mode = IRQ_EDGE_BOTH;
852 break;
853 case IRQ_TYPE_LEVEL_HIGH:
854 mode = IRQ_LEVEL_HIGH;
855 break;
856 case IRQ_TYPE_LEVEL_LOW:
857 mode = IRQ_LEVEL_LOW;
858 break;
859 default:
860 return -EINVAL;
861 }
862
Julia Cartwrightf658ed32017-03-09 10:22:06 -0600863 raw_spin_lock_irqsave(&pctl->lock, flags);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200864
Maxime Riparda0d6de92015-07-20 14:41:11 +0200865 if (type & IRQ_TYPE_LEVEL_MASK)
Thomas Gleixnerb9a5ec332015-09-16 12:32:40 +0200866 irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_level_irq_chip,
867 handle_fasteoi_irq, NULL);
Maxime Riparda0d6de92015-07-20 14:41:11 +0200868 else
Thomas Gleixnerb9a5ec332015-09-16 12:32:40 +0200869 irq_set_chip_handler_name_locked(d, &sunxi_pinctrl_edge_irq_chip,
870 handle_edge_irq, NULL);
Maxime Riparda0d6de92015-07-20 14:41:11 +0200871
Maxime Ripard2aaaddf2013-08-04 12:38:47 +0200872 regval = readl(pctl->membase + reg);
Hans de Goeded82f9402014-02-17 22:19:43 +0100873 regval &= ~(IRQ_CFG_IRQ_MASK << index);
Maxime Ripard2aaaddf2013-08-04 12:38:47 +0200874 writel(regval | (mode << index), pctl->membase + reg);
Maxime Ripard60242db2013-06-08 12:05:44 +0200875
Julia Cartwrightf658ed32017-03-09 10:22:06 -0600876 raw_spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard60242db2013-06-08 12:05:44 +0200877
878 return 0;
879}
880
Maxime Ripard645ec712014-06-05 15:26:00 +0200881static void sunxi_pinctrl_irq_ack(struct irq_data *d)
Maxime Ripard60242db2013-06-08 12:05:44 +0200882{
883 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
Hans de Goede5e7515b2016-03-12 19:44:57 +0100884 u32 status_reg = sunxi_irq_status_reg(d->hwirq,
885 pctl->desc->irq_bank_base);
Maxime Ripard60242db2013-06-08 12:05:44 +0200886 u8 status_idx = sunxi_irq_status_offset(d->hwirq);
Maxime Ripard60242db2013-06-08 12:05:44 +0200887
888 /* Clear the IRQ */
889 writel(1 << status_idx, pctl->membase + status_reg);
890}
891
892static void sunxi_pinctrl_irq_mask(struct irq_data *d)
893{
894 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
Hans de Goede5e7515b2016-03-12 19:44:57 +0100895 u32 reg = sunxi_irq_ctrl_reg(d->hwirq, pctl->desc->irq_bank_base);
Maxime Ripard60242db2013-06-08 12:05:44 +0200896 u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200897 unsigned long flags;
Maxime Ripard60242db2013-06-08 12:05:44 +0200898 u32 val;
899
Julia Cartwrightf658ed32017-03-09 10:22:06 -0600900 raw_spin_lock_irqsave(&pctl->lock, flags);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200901
Maxime Ripard60242db2013-06-08 12:05:44 +0200902 /* Mask the IRQ */
903 val = readl(pctl->membase + reg);
904 writel(val & ~(1 << idx), pctl->membase + reg);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200905
Julia Cartwrightf658ed32017-03-09 10:22:06 -0600906 raw_spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard60242db2013-06-08 12:05:44 +0200907}
908
909static void sunxi_pinctrl_irq_unmask(struct irq_data *d)
910{
911 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
Hans de Goede5e7515b2016-03-12 19:44:57 +0100912 u32 reg = sunxi_irq_ctrl_reg(d->hwirq, pctl->desc->irq_bank_base);
Maxime Ripard60242db2013-06-08 12:05:44 +0200913 u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200914 unsigned long flags;
Maxime Ripard60242db2013-06-08 12:05:44 +0200915 u32 val;
916
Julia Cartwrightf658ed32017-03-09 10:22:06 -0600917 raw_spin_lock_irqsave(&pctl->lock, flags);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200918
Maxime Ripard60242db2013-06-08 12:05:44 +0200919 /* Unmask the IRQ */
920 val = readl(pctl->membase + reg);
921 writel(val | (1 << idx), pctl->membase + reg);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200922
Julia Cartwrightf658ed32017-03-09 10:22:06 -0600923 raw_spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard60242db2013-06-08 12:05:44 +0200924}
925
Hans de Goeded61e23e2014-06-29 16:11:02 +0200926static void sunxi_pinctrl_irq_ack_unmask(struct irq_data *d)
927{
928 sunxi_pinctrl_irq_ack(d);
929 sunxi_pinctrl_irq_unmask(d);
930}
931
Hans de Goedef4c51c12014-06-29 16:11:01 +0200932static struct irq_chip sunxi_pinctrl_edge_irq_chip = {
Maxime Ripardfb5b7782015-07-20 14:41:12 +0200933 .name = "sunxi_pio_edge",
Maxime Ripard645ec712014-06-05 15:26:00 +0200934 .irq_ack = sunxi_pinctrl_irq_ack,
Maxime Ripard60242db2013-06-08 12:05:44 +0200935 .irq_mask = sunxi_pinctrl_irq_mask,
Maxime Ripard60242db2013-06-08 12:05:44 +0200936 .irq_unmask = sunxi_pinctrl_irq_unmask,
Hans de Goedefea6d8e2014-06-29 16:11:00 +0200937 .irq_request_resources = sunxi_pinctrl_irq_request_resources,
Chen-Yu Tsaif83549d2014-07-15 01:24:36 +0800938 .irq_release_resources = sunxi_pinctrl_irq_release_resources,
Maxime Ripard60242db2013-06-08 12:05:44 +0200939 .irq_set_type = sunxi_pinctrl_irq_set_type,
Chen-Yu Tsai578c0a82014-06-29 16:10:59 +0200940 .flags = IRQCHIP_SKIP_SET_WAKE,
Maxime Ripard60242db2013-06-08 12:05:44 +0200941};
942
Hans de Goedef4c51c12014-06-29 16:11:01 +0200943static struct irq_chip sunxi_pinctrl_level_irq_chip = {
Maxime Ripardfb5b7782015-07-20 14:41:12 +0200944 .name = "sunxi_pio_level",
Hans de Goedef4c51c12014-06-29 16:11:01 +0200945 .irq_eoi = sunxi_pinctrl_irq_ack,
946 .irq_mask = sunxi_pinctrl_irq_mask,
947 .irq_unmask = sunxi_pinctrl_irq_unmask,
Hans de Goeded61e23e2014-06-29 16:11:02 +0200948 /* Define irq_enable / disable to avoid spurious irqs for drivers
949 * using these to suppress irqs while they clear the irq source */
950 .irq_enable = sunxi_pinctrl_irq_ack_unmask,
951 .irq_disable = sunxi_pinctrl_irq_mask,
Hans de Goedef4c51c12014-06-29 16:11:01 +0200952 .irq_request_resources = sunxi_pinctrl_irq_request_resources,
Chen-Yu Tsaif83549d2014-07-15 01:24:36 +0800953 .irq_release_resources = sunxi_pinctrl_irq_release_resources,
Hans de Goedef4c51c12014-06-29 16:11:01 +0200954 .irq_set_type = sunxi_pinctrl_irq_set_type,
955 .flags = IRQCHIP_SKIP_SET_WAKE | IRQCHIP_EOI_THREADED |
956 IRQCHIP_EOI_IF_HANDLED,
Maxime Ripard60242db2013-06-08 12:05:44 +0200957};
958
Maxime Ripardd8323c62015-07-27 14:41:57 +0200959static int sunxi_pinctrl_irq_of_xlate(struct irq_domain *d,
960 struct device_node *node,
961 const u32 *intspec,
962 unsigned int intsize,
963 unsigned long *out_hwirq,
964 unsigned int *out_type)
965{
Hans de Goede82979922015-10-16 09:46:11 +0200966 struct sunxi_pinctrl *pctl = d->host_data;
Maxime Ripardd8323c62015-07-27 14:41:57 +0200967 struct sunxi_desc_function *desc;
968 int pin, base;
969
970 if (intsize < 3)
971 return -EINVAL;
972
973 base = PINS_PER_BANK * intspec[0];
Hans de Goede82979922015-10-16 09:46:11 +0200974 pin = pctl->desc->pin_base + base + intspec[1];
Maxime Ripardd8323c62015-07-27 14:41:57 +0200975
Hans de Goede82979922015-10-16 09:46:11 +0200976 desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, pin, "irq");
Maxime Ripardd8323c62015-07-27 14:41:57 +0200977 if (!desc)
978 return -EINVAL;
979
980 *out_hwirq = desc->irqbank * PINS_PER_BANK + desc->irqnum;
981 *out_type = intspec[2];
982
983 return 0;
984}
985
Tobias Klauser2421dfd2017-06-02 13:29:58 +0200986static const struct irq_domain_ops sunxi_pinctrl_irq_domain_ops = {
Maxime Ripardd8323c62015-07-27 14:41:57 +0200987 .xlate = sunxi_pinctrl_irq_of_xlate,
988};
989
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +0200990static void sunxi_pinctrl_irq_handler(struct irq_desc *desc)
Maxime Ripard60242db2013-06-08 12:05:44 +0200991{
Thomas Gleixnereeef97b2015-07-13 01:55:27 +0200992 unsigned int irq = irq_desc_get_irq(desc);
Jiang Liu5663bb22015-06-04 12:13:16 +0800993 struct irq_chip *chip = irq_desc_get_chip(desc);
994 struct sunxi_pinctrl *pctl = irq_desc_get_handler_data(desc);
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200995 unsigned long bank, reg, val;
Maxime Ripard60242db2013-06-08 12:05:44 +0200996
Maxime Ripardaebdc8a2014-06-05 15:26:04 +0200997 for (bank = 0; bank < pctl->desc->irq_banks; bank++)
998 if (irq == pctl->irq[bank])
999 break;
Maxime Ripard60242db2013-06-08 12:05:44 +02001000
Maxime Ripardaebdc8a2014-06-05 15:26:04 +02001001 if (bank == pctl->desc->irq_banks)
1002 return;
1003
Hans de Goede5e7515b2016-03-12 19:44:57 +01001004 reg = sunxi_irq_status_reg_from_bank(bank, pctl->desc->irq_bank_base);
Maxime Ripardaebdc8a2014-06-05 15:26:04 +02001005 val = readl(pctl->membase + reg);
Maxime Ripard60242db2013-06-08 12:05:44 +02001006
Maxime Ripardaebdc8a2014-06-05 15:26:04 +02001007 if (val) {
Maxime Ripard60242db2013-06-08 12:05:44 +02001008 int irqoffset;
1009
Chen-Yu Tsai905a5112014-02-11 00:22:37 +08001010 chained_irq_enter(chip, desc);
Maxime Ripardaebdc8a2014-06-05 15:26:04 +02001011 for_each_set_bit(irqoffset, &val, IRQ_PER_BANK) {
1012 int pin_irq = irq_find_mapping(pctl->domain,
1013 bank * IRQ_PER_BANK + irqoffset);
Maxime Ripard60242db2013-06-08 12:05:44 +02001014 generic_handle_irq(pin_irq);
1015 }
Chen-Yu Tsai905a5112014-02-11 00:22:37 +08001016 chained_irq_exit(chip, desc);
Maxime Ripard60242db2013-06-08 12:05:44 +02001017 }
1018}
1019
Maxime Ripard0e37f882013-01-18 22:30:34 +01001020static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
1021 const char *name)
1022{
1023 struct sunxi_pinctrl_function *func = pctl->functions;
1024
1025 while (func->name) {
1026 /* function already there */
1027 if (strcmp(func->name, name) == 0) {
1028 func->ngroups++;
1029 return -EEXIST;
1030 }
1031 func++;
1032 }
1033
1034 func->name = name;
1035 func->ngroups = 1;
1036
1037 pctl->nfunctions++;
1038
1039 return 0;
1040}
1041
1042static int sunxi_pinctrl_build_state(struct platform_device *pdev)
1043{
1044 struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
1045 int i;
1046
Maxime Ripard578db852017-01-08 22:31:15 +01001047 /*
1048 * Allocate groups
1049 *
1050 * We assume that the number of groups is the number of pins
1051 * given in the data array.
Maxime Ripard0e37f882013-01-18 22:30:34 +01001052
Maxime Ripard578db852017-01-08 22:31:15 +01001053 * This will not always be true, since some pins might not be
1054 * available in the current variant, but fortunately for us,
1055 * this means that the number of pins is the maximum group
1056 * number we will ever see.
1057 */
Maxime Ripard0e37f882013-01-18 22:30:34 +01001058 pctl->groups = devm_kzalloc(&pdev->dev,
Maxime Ripard578db852017-01-08 22:31:15 +01001059 pctl->desc->npins * sizeof(*pctl->groups),
Maxime Ripard0e37f882013-01-18 22:30:34 +01001060 GFP_KERNEL);
1061 if (!pctl->groups)
1062 return -ENOMEM;
1063
1064 for (i = 0; i < pctl->desc->npins; i++) {
1065 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
Maxime Ripard578db852017-01-08 22:31:15 +01001066 struct sunxi_pinctrl_group *group = pctl->groups + pctl->ngroups;
1067
1068 if (pin->variant && !(pctl->variant & pin->variant))
1069 continue;
Maxime Ripard0e37f882013-01-18 22:30:34 +01001070
1071 group->name = pin->pin.name;
1072 group->pin = pin->pin.number;
Maxime Ripard578db852017-01-08 22:31:15 +01001073
1074 /* And now we count the actual number of pins / groups */
1075 pctl->ngroups++;
Maxime Ripard0e37f882013-01-18 22:30:34 +01001076 }
1077
1078 /*
1079 * We suppose that we won't have any more functions than pins,
1080 * we'll reallocate that later anyway
1081 */
1082 pctl->functions = devm_kzalloc(&pdev->dev,
Maxime Ripard578db852017-01-08 22:31:15 +01001083 pctl->ngroups * sizeof(*pctl->functions),
1084 GFP_KERNEL);
Maxime Ripard0e37f882013-01-18 22:30:34 +01001085 if (!pctl->functions)
1086 return -ENOMEM;
1087
1088 /* Count functions and their associated groups */
1089 for (i = 0; i < pctl->desc->npins; i++) {
1090 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
Maxime Ripard578db852017-01-08 22:31:15 +01001091 struct sunxi_desc_function *func;
Maxime Ripard0e37f882013-01-18 22:30:34 +01001092
Maxime Ripard578db852017-01-08 22:31:15 +01001093 if (pin->variant && !(pctl->variant & pin->variant))
1094 continue;
1095
1096 for (func = pin->functions; func->name; func++) {
1097 if (func->variant && !(pctl->variant & func->variant))
1098 continue;
1099
Chen-Yu Tsaid54e9a22014-05-26 09:47:56 +02001100 /* Create interrupt mapping while we're at it */
Maxime Ripardaebdc8a2014-06-05 15:26:04 +02001101 if (!strcmp(func->name, "irq")) {
1102 int irqnum = func->irqnum + func->irqbank * IRQ_PER_BANK;
1103 pctl->irq_array[irqnum] = pin->pin.number;
1104 }
1105
Maxime Ripard0e37f882013-01-18 22:30:34 +01001106 sunxi_pinctrl_add_function(pctl, func->name);
Maxime Ripard0e37f882013-01-18 22:30:34 +01001107 }
1108 }
1109
Maxime Ripard578db852017-01-08 22:31:15 +01001110 /* And now allocated and fill the array for real */
Maxime Ripard0e37f882013-01-18 22:30:34 +01001111 pctl->functions = krealloc(pctl->functions,
Maxime Ripard578db852017-01-08 22:31:15 +01001112 pctl->nfunctions * sizeof(*pctl->functions),
1113 GFP_KERNEL);
1114 if (!pctl->functions) {
1115 kfree(pctl->functions);
1116 return -ENOMEM;
1117 }
Maxime Ripard0e37f882013-01-18 22:30:34 +01001118
1119 for (i = 0; i < pctl->desc->npins; i++) {
1120 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
Maxime Ripard578db852017-01-08 22:31:15 +01001121 struct sunxi_desc_function *func;
Maxime Ripard0e37f882013-01-18 22:30:34 +01001122
Maxime Ripard578db852017-01-08 22:31:15 +01001123 if (pin->variant && !(pctl->variant & pin->variant))
1124 continue;
1125
1126 for (func = pin->functions; func->name; func++) {
Maxime Ripard0e37f882013-01-18 22:30:34 +01001127 struct sunxi_pinctrl_function *func_item;
1128 const char **func_grp;
1129
Maxime Ripard578db852017-01-08 22:31:15 +01001130 if (func->variant && !(pctl->variant & func->variant))
1131 continue;
1132
Maxime Ripard0e37f882013-01-18 22:30:34 +01001133 func_item = sunxi_pinctrl_find_function_by_name(pctl,
1134 func->name);
1135 if (!func_item)
1136 return -EINVAL;
1137
1138 if (!func_item->groups) {
1139 func_item->groups =
1140 devm_kzalloc(&pdev->dev,
1141 func_item->ngroups * sizeof(*func_item->groups),
1142 GFP_KERNEL);
1143 if (!func_item->groups)
1144 return -ENOMEM;
1145 }
1146
1147 func_grp = func_item->groups;
1148 while (*func_grp)
1149 func_grp++;
1150
1151 *func_grp = pin->pin.name;
Maxime Ripard0e37f882013-01-18 22:30:34 +01001152 }
1153 }
1154
1155 return 0;
1156}
1157
Maxime Ripard7c926492016-11-14 21:53:03 +01001158static int sunxi_pinctrl_get_debounce_div(struct clk *clk, int freq, int *diff)
1159{
1160 unsigned long clock = clk_get_rate(clk);
Arnd Bergmannd8a22212016-11-16 15:18:18 +01001161 unsigned int best_diff, best_div;
Maxime Ripard7c926492016-11-14 21:53:03 +01001162 int i;
1163
Arnd Bergmannd8a22212016-11-16 15:18:18 +01001164 best_diff = abs(freq - clock);
1165 best_div = 0;
1166
1167 for (i = 1; i < 8; i++) {
Maxime Ripard7c926492016-11-14 21:53:03 +01001168 int cur_diff = abs(freq - (clock >> i));
1169
1170 if (cur_diff < best_diff) {
1171 best_diff = cur_diff;
1172 best_div = i;
1173 }
1174 }
1175
1176 *diff = best_diff;
1177 return best_div;
1178}
1179
1180static int sunxi_pinctrl_setup_debounce(struct sunxi_pinctrl *pctl,
1181 struct device_node *node)
1182{
1183 unsigned int hosc_diff, losc_diff;
1184 unsigned int hosc_div, losc_div;
1185 struct clk *hosc, *losc;
1186 u8 div, src;
1187 int i, ret;
1188
1189 /* Deal with old DTs that didn't have the oscillators */
1190 if (of_count_phandle_with_args(node, "clocks", "#clock-cells") != 3)
1191 return 0;
1192
1193 /* If we don't have any setup, bail out */
1194 if (!of_find_property(node, "input-debounce", NULL))
1195 return 0;
1196
1197 losc = devm_clk_get(pctl->dev, "losc");
1198 if (IS_ERR(losc))
1199 return PTR_ERR(losc);
1200
1201 hosc = devm_clk_get(pctl->dev, "hosc");
1202 if (IS_ERR(hosc))
1203 return PTR_ERR(hosc);
1204
1205 for (i = 0; i < pctl->desc->irq_banks; i++) {
1206 unsigned long debounce_freq;
1207 u32 debounce;
1208
1209 ret = of_property_read_u32_index(node, "input-debounce",
1210 i, &debounce);
1211 if (ret)
1212 return ret;
1213
1214 if (!debounce)
1215 continue;
1216
1217 debounce_freq = DIV_ROUND_CLOSEST(USEC_PER_SEC, debounce);
1218 losc_div = sunxi_pinctrl_get_debounce_div(losc,
1219 debounce_freq,
1220 &losc_diff);
1221
1222 hosc_div = sunxi_pinctrl_get_debounce_div(hosc,
1223 debounce_freq,
1224 &hosc_diff);
1225
1226 if (hosc_diff < losc_diff) {
1227 div = hosc_div;
1228 src = 1;
1229 } else {
1230 div = losc_div;
1231 src = 0;
1232 }
1233
1234 writel(src | div << 4,
1235 pctl->membase +
1236 sunxi_irq_debounce_reg_from_bank(i,
1237 pctl->desc->irq_bank_base));
1238 }
1239
1240 return 0;
1241}
1242
Maxime Ripard578db852017-01-08 22:31:15 +01001243int sunxi_pinctrl_init_with_variant(struct platform_device *pdev,
1244 const struct sunxi_pinctrl_desc *desc,
1245 unsigned long variant)
Maxime Ripard0e37f882013-01-18 22:30:34 +01001246{
1247 struct device_node *node = pdev->dev.of_node;
Maxime Ripardba6764d2014-05-22 16:25:27 +02001248 struct pinctrl_desc *pctrl_desc;
Maxime Ripard0e37f882013-01-18 22:30:34 +01001249 struct pinctrl_pin_desc *pins;
1250 struct sunxi_pinctrl *pctl;
Maxime Ripardaae842a2017-10-09 22:53:37 +02001251 struct pinmux_ops *pmxops;
Maxime Ripard4409caf2014-04-26 21:59:50 +02001252 struct resource *res;
Maxime Ripard578db852017-01-08 22:31:15 +01001253 int i, ret, last_pin, pin_idx;
Emilio López950707c2013-03-22 11:20:40 -03001254 struct clk *clk;
Maxime Ripard0e37f882013-01-18 22:30:34 +01001255
1256 pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
1257 if (!pctl)
1258 return -ENOMEM;
1259 platform_set_drvdata(pdev, pctl);
1260
Julia Cartwrightf658ed32017-03-09 10:22:06 -06001261 raw_spin_lock_init(&pctl->lock);
Maxime Ripard1bee9632013-08-04 12:38:48 +02001262
Maxime Ripard4409caf2014-04-26 21:59:50 +02001263 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1264 pctl->membase = devm_ioremap_resource(&pdev->dev, res);
1265 if (IS_ERR(pctl->membase))
1266 return PTR_ERR(pctl->membase);
Maxime Ripard0e37f882013-01-18 22:30:34 +01001267
Maxime Ripardba6764d2014-05-22 16:25:27 +02001268 pctl->dev = &pdev->dev;
Maxime Ripard2284ba62014-04-18 20:10:41 +02001269 pctl->desc = desc;
Maxime Ripard578db852017-01-08 22:31:15 +01001270 pctl->variant = variant;
Maxime Ripard0e37f882013-01-18 22:30:34 +01001271
Maxime Ripardaebdc8a2014-06-05 15:26:04 +02001272 pctl->irq_array = devm_kcalloc(&pdev->dev,
1273 IRQ_PER_BANK * pctl->desc->irq_banks,
1274 sizeof(*pctl->irq_array),
1275 GFP_KERNEL);
1276 if (!pctl->irq_array)
1277 return -ENOMEM;
1278
Maxime Ripard0e37f882013-01-18 22:30:34 +01001279 ret = sunxi_pinctrl_build_state(pdev);
1280 if (ret) {
1281 dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
1282 return ret;
1283 }
1284
1285 pins = devm_kzalloc(&pdev->dev,
1286 pctl->desc->npins * sizeof(*pins),
1287 GFP_KERNEL);
1288 if (!pins)
1289 return -ENOMEM;
1290
Maxime Ripard578db852017-01-08 22:31:15 +01001291 for (i = 0, pin_idx = 0; i < pctl->desc->npins; i++) {
1292 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1293
1294 if (pin->variant && !(pctl->variant & pin->variant))
1295 continue;
1296
1297 pins[pin_idx++] = pin->pin;
1298 }
Maxime Ripard0e37f882013-01-18 22:30:34 +01001299
Maxime Ripardba6764d2014-05-22 16:25:27 +02001300 pctrl_desc = devm_kzalloc(&pdev->dev,
1301 sizeof(*pctrl_desc),
1302 GFP_KERNEL);
1303 if (!pctrl_desc)
1304 return -ENOMEM;
1305
1306 pctrl_desc->name = dev_name(&pdev->dev);
1307 pctrl_desc->owner = THIS_MODULE;
1308 pctrl_desc->pins = pins;
Maxime Ripard578db852017-01-08 22:31:15 +01001309 pctrl_desc->npins = pctl->ngroups;
Maxime Ripardba6764d2014-05-22 16:25:27 +02001310 pctrl_desc->confops = &sunxi_pconf_ops;
1311 pctrl_desc->pctlops = &sunxi_pctrl_ops;
Maxime Ripardaae842a2017-10-09 22:53:37 +02001312
1313 pmxops = devm_kmemdup(&pdev->dev, &sunxi_pmx_ops, sizeof(sunxi_pmx_ops),
1314 GFP_KERNEL);
1315 if (!pmxops)
1316 return -ENOMEM;
1317
1318 if (desc->disable_strict_mode)
1319 pmxops->strict = false;
1320
1321 pctrl_desc->pmxops = pmxops;
Maxime Ripardba6764d2014-05-22 16:25:27 +02001322
Laxman Dewangan45078ea2016-02-24 14:44:07 +05301323 pctl->pctl_dev = devm_pinctrl_register(&pdev->dev, pctrl_desc, pctl);
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001324 if (IS_ERR(pctl->pctl_dev)) {
Maxime Ripard0e37f882013-01-18 22:30:34 +01001325 dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001326 return PTR_ERR(pctl->pctl_dev);
Maxime Ripard0e37f882013-01-18 22:30:34 +01001327 }
1328
Maxime Ripard08e9e612013-01-28 21:33:12 +01001329 pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
Laxman Dewangan45078ea2016-02-24 14:44:07 +05301330 if (!pctl->chip)
1331 return -ENOMEM;
Maxime Ripard08e9e612013-01-28 21:33:12 +01001332
1333 last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
Boris BREZILLONd83c82c2014-04-10 15:52:43 +02001334 pctl->chip->owner = THIS_MODULE;
Jonas Gorski98c85d52015-10-11 17:34:19 +02001335 pctl->chip->request = gpiochip_generic_request,
1336 pctl->chip->free = gpiochip_generic_free,
Boris BREZILLONd83c82c2014-04-10 15:52:43 +02001337 pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input,
1338 pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output,
1339 pctl->chip->get = sunxi_pinctrl_gpio_get,
1340 pctl->chip->set = sunxi_pinctrl_gpio_set,
1341 pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate,
1342 pctl->chip->to_irq = sunxi_pinctrl_gpio_to_irq,
1343 pctl->chip->of_gpio_n_cells = 3,
1344 pctl->chip->can_sleep = false,
1345 pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK) -
1346 pctl->desc->pin_base;
Maxime Ripard08e9e612013-01-28 21:33:12 +01001347 pctl->chip->label = dev_name(&pdev->dev);
Linus Walleij58383c782015-11-04 09:56:26 +01001348 pctl->chip->parent = &pdev->dev;
Boris BREZILLONd83c82c2014-04-10 15:52:43 +02001349 pctl->chip->base = pctl->desc->pin_base;
Maxime Ripard08e9e612013-01-28 21:33:12 +01001350
Linus Walleij88057d62015-12-08 22:40:43 +01001351 ret = gpiochip_add_data(pctl->chip, pctl);
Maxime Ripard08e9e612013-01-28 21:33:12 +01001352 if (ret)
Laxman Dewangan45078ea2016-02-24 14:44:07 +05301353 return ret;
Maxime Ripard08e9e612013-01-28 21:33:12 +01001354
1355 for (i = 0; i < pctl->desc->npins; i++) {
1356 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
1357
1358 ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
Chen-Yu Tsai343f1322014-07-15 01:24:37 +08001359 pin->pin.number - pctl->desc->pin_base,
Maxime Ripard08e9e612013-01-28 21:33:12 +01001360 pin->pin.number, 1);
1361 if (ret)
1362 goto gpiochip_error;
1363 }
1364
Emilio López950707c2013-03-22 11:20:40 -03001365 clk = devm_clk_get(&pdev->dev, NULL);
Wei Yongjund72f88a2013-05-23 17:32:14 +08001366 if (IS_ERR(clk)) {
1367 ret = PTR_ERR(clk);
Emilio López950707c2013-03-22 11:20:40 -03001368 goto gpiochip_error;
Wei Yongjund72f88a2013-05-23 17:32:14 +08001369 }
Emilio López950707c2013-03-22 11:20:40 -03001370
Boris BREZILLON64150932014-04-10 15:52:40 +02001371 ret = clk_prepare_enable(clk);
1372 if (ret)
1373 goto gpiochip_error;
Emilio López950707c2013-03-22 11:20:40 -03001374
Maxime Ripardaebdc8a2014-06-05 15:26:04 +02001375 pctl->irq = devm_kcalloc(&pdev->dev,
1376 pctl->desc->irq_banks,
1377 sizeof(*pctl->irq),
1378 GFP_KERNEL);
Maxime Ripard60242db2013-06-08 12:05:44 +02001379 if (!pctl->irq) {
Maxime Ripardaebdc8a2014-06-05 15:26:04 +02001380 ret = -ENOMEM;
Maxime Riparddc969102014-04-26 22:28:54 +02001381 goto clk_error;
Maxime Ripard60242db2013-06-08 12:05:44 +02001382 }
1383
Maxime Ripardaebdc8a2014-06-05 15:26:04 +02001384 for (i = 0; i < pctl->desc->irq_banks; i++) {
1385 pctl->irq[i] = platform_get_irq(pdev, i);
1386 if (pctl->irq[i] < 0) {
1387 ret = pctl->irq[i];
1388 goto clk_error;
1389 }
1390 }
1391
1392 pctl->domain = irq_domain_add_linear(node,
1393 pctl->desc->irq_banks * IRQ_PER_BANK,
Maxime Ripardd8323c62015-07-27 14:41:57 +02001394 &sunxi_pinctrl_irq_domain_ops,
1395 pctl);
Maxime Ripard60242db2013-06-08 12:05:44 +02001396 if (!pctl->domain) {
1397 dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
1398 ret = -ENOMEM;
Maxime Riparddc969102014-04-26 22:28:54 +02001399 goto clk_error;
Maxime Ripard60242db2013-06-08 12:05:44 +02001400 }
1401
Maxime Ripardaebdc8a2014-06-05 15:26:04 +02001402 for (i = 0; i < (pctl->desc->irq_banks * IRQ_PER_BANK); i++) {
Maxime Ripard60242db2013-06-08 12:05:44 +02001403 int irqno = irq_create_mapping(pctl->domain, i);
1404
Hans de Goedef4c51c12014-06-29 16:11:01 +02001405 irq_set_chip_and_handler(irqno, &sunxi_pinctrl_edge_irq_chip,
1406 handle_edge_irq);
Maxime Ripard60242db2013-06-08 12:05:44 +02001407 irq_set_chip_data(irqno, pctl);
Javier Martinez Canillas5c99c0f2015-09-16 10:28:29 +02001408 }
Maxime Ripard60242db2013-06-08 12:05:44 +02001409
Maxime Ripardaebdc8a2014-06-05 15:26:04 +02001410 for (i = 0; i < pctl->desc->irq_banks; i++) {
Hans de Goedef4c51c12014-06-29 16:11:01 +02001411 /* Mask and clear all IRQs before registering a handler */
Hans de Goede5e7515b2016-03-12 19:44:57 +01001412 writel(0, pctl->membase + sunxi_irq_ctrl_reg_from_bank(i,
1413 pctl->desc->irq_bank_base));
Hans de Goedef4c51c12014-06-29 16:11:01 +02001414 writel(0xffffffff,
Hans de Goede5e7515b2016-03-12 19:44:57 +01001415 pctl->membase + sunxi_irq_status_reg_from_bank(i,
1416 pctl->desc->irq_bank_base));
Hans de Goedef4c51c12014-06-29 16:11:01 +02001417
Thomas Gleixneref80e872015-06-21 20:16:18 +02001418 irq_set_chained_handler_and_data(pctl->irq[i],
1419 sunxi_pinctrl_irq_handler,
1420 pctl);
Maxime Ripardaebdc8a2014-06-05 15:26:04 +02001421 }
Maxime Ripard60242db2013-06-08 12:05:44 +02001422
Maxime Ripard7c926492016-11-14 21:53:03 +01001423 sunxi_pinctrl_setup_debounce(pctl, node);
1424
Maxime Ripard08e9e612013-01-28 21:33:12 +01001425 dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
Maxime Ripard0e37f882013-01-18 22:30:34 +01001426
1427 return 0;
Maxime Ripard08e9e612013-01-28 21:33:12 +01001428
Boris BREZILLONe2bddc62014-04-10 15:52:41 +02001429clk_error:
1430 clk_disable_unprepare(clk);
Maxime Ripard08e9e612013-01-28 21:33:12 +01001431gpiochip_error:
abdoulaye bertheb4e7c552014-07-12 22:30:13 +02001432 gpiochip_remove(pctl->chip);
Maxime Ripard08e9e612013-01-28 21:33:12 +01001433 return ret;
Maxime Ripard0e37f882013-01-18 22:30:34 +01001434}