blob: 6dfc172af9d317ab8f9902e35599fc083dd01603 [file] [log] [blame]
Maxime Ripard0e37f882013-01-18 22:30:34 +01001/*
2 * Allwinner A1X SoCs pinctrl driver.
3 *
4 * Copyright (C) 2012 Maxime Ripard
5 *
6 * Maxime Ripard <maxime.ripard@free-electrons.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2. This program is licensed "as is" without any
10 * warranty of any kind, whether express or implied.
11 */
12
13#include <linux/io.h>
Emilio López950707c2013-03-22 11:20:40 -030014#include <linux/clk.h>
Maxime Ripard08e9e612013-01-28 21:33:12 +010015#include <linux/gpio.h>
Maxime Ripard60242db2013-06-08 12:05:44 +020016#include <linux/irqdomain.h>
Chen-Yu Tsai905a5112014-02-11 00:22:37 +080017#include <linux/irqchip/chained_irq.h>
Maxime Ripard0e37f882013-01-18 22:30:34 +010018#include <linux/module.h>
19#include <linux/of.h>
20#include <linux/of_address.h>
21#include <linux/of_device.h>
Maxime Ripard60242db2013-06-08 12:05:44 +020022#include <linux/of_irq.h>
Maxime Ripard0e37f882013-01-18 22:30:34 +010023#include <linux/pinctrl/consumer.h>
24#include <linux/pinctrl/machine.h>
25#include <linux/pinctrl/pinctrl.h>
26#include <linux/pinctrl/pinconf-generic.h>
27#include <linux/pinctrl/pinmux.h>
28#include <linux/platform_device.h>
29#include <linux/slab.h>
30
Maxime Ripard5f910772014-04-18 18:53:02 +020031#include "../core.h"
Maxime Ripard0e37f882013-01-18 22:30:34 +010032#include "pinctrl-sunxi.h"
Maxime Ripardeaa3d842013-01-18 22:30:35 +010033
Maxime Ripard0e37f882013-01-18 22:30:34 +010034static struct sunxi_pinctrl_group *
35sunxi_pinctrl_find_group_by_name(struct sunxi_pinctrl *pctl, const char *group)
36{
37 int i;
38
39 for (i = 0; i < pctl->ngroups; i++) {
40 struct sunxi_pinctrl_group *grp = pctl->groups + i;
41
42 if (!strcmp(grp->name, group))
43 return grp;
44 }
45
46 return NULL;
47}
48
49static struct sunxi_pinctrl_function *
50sunxi_pinctrl_find_function_by_name(struct sunxi_pinctrl *pctl,
51 const char *name)
52{
53 struct sunxi_pinctrl_function *func = pctl->functions;
54 int i;
55
56 for (i = 0; i < pctl->nfunctions; i++) {
57 if (!func[i].name)
58 break;
59
60 if (!strcmp(func[i].name, name))
61 return func + i;
62 }
63
64 return NULL;
65}
66
67static struct sunxi_desc_function *
68sunxi_pinctrl_desc_find_function_by_name(struct sunxi_pinctrl *pctl,
69 const char *pin_name,
70 const char *func_name)
71{
72 int i;
73
74 for (i = 0; i < pctl->desc->npins; i++) {
75 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
76
77 if (!strcmp(pin->pin.name, pin_name)) {
78 struct sunxi_desc_function *func = pin->functions;
79
80 while (func->name) {
81 if (!strcmp(func->name, func_name))
82 return func;
83
84 func++;
85 }
86 }
87 }
88
89 return NULL;
90}
91
Maxime Ripard814d4f22013-06-08 12:05:43 +020092static struct sunxi_desc_function *
93sunxi_pinctrl_desc_find_function_by_pin(struct sunxi_pinctrl *pctl,
94 const u16 pin_num,
95 const char *func_name)
96{
97 int i;
98
99 for (i = 0; i < pctl->desc->npins; i++) {
100 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
101
102 if (pin->pin.number == pin_num) {
103 struct sunxi_desc_function *func = pin->functions;
104
105 while (func->name) {
106 if (!strcmp(func->name, func_name))
107 return func;
108
109 func++;
110 }
111 }
112 }
113
114 return NULL;
115}
116
Maxime Ripard0e37f882013-01-18 22:30:34 +0100117static int sunxi_pctrl_get_groups_count(struct pinctrl_dev *pctldev)
118{
119 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
120
121 return pctl->ngroups;
122}
123
124static const char *sunxi_pctrl_get_group_name(struct pinctrl_dev *pctldev,
125 unsigned group)
126{
127 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
128
129 return pctl->groups[group].name;
130}
131
132static int sunxi_pctrl_get_group_pins(struct pinctrl_dev *pctldev,
133 unsigned group,
134 const unsigned **pins,
135 unsigned *num_pins)
136{
137 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
138
139 *pins = (unsigned *)&pctl->groups[group].pin;
140 *num_pins = 1;
141
142 return 0;
143}
144
145static int sunxi_pctrl_dt_node_to_map(struct pinctrl_dev *pctldev,
146 struct device_node *node,
147 struct pinctrl_map **map,
148 unsigned *num_maps)
149{
150 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
151 unsigned long *pinconfig;
152 struct property *prop;
153 const char *function;
154 const char *group;
155 int ret, nmaps, i = 0;
156 u32 val;
157
158 *map = NULL;
159 *num_maps = 0;
160
161 ret = of_property_read_string(node, "allwinner,function", &function);
162 if (ret) {
163 dev_err(pctl->dev,
164 "missing allwinner,function property in node %s\n",
165 node->name);
166 return -EINVAL;
167 }
168
169 nmaps = of_property_count_strings(node, "allwinner,pins") * 2;
170 if (nmaps < 0) {
171 dev_err(pctl->dev,
172 "missing allwinner,pins property in node %s\n",
173 node->name);
174 return -EINVAL;
175 }
176
177 *map = kmalloc(nmaps * sizeof(struct pinctrl_map), GFP_KERNEL);
Sachin Kamat3efa9212013-07-29 13:49:32 +0530178 if (!*map)
Maxime Ripard0e37f882013-01-18 22:30:34 +0100179 return -ENOMEM;
180
181 of_property_for_each_string(node, "allwinner,pins", prop, group) {
182 struct sunxi_pinctrl_group *grp =
183 sunxi_pinctrl_find_group_by_name(pctl, group);
184 int j = 0, configlen = 0;
185
186 if (!grp) {
187 dev_err(pctl->dev, "unknown pin %s", group);
188 continue;
189 }
190
191 if (!sunxi_pinctrl_desc_find_function_by_name(pctl,
192 grp->name,
193 function)) {
194 dev_err(pctl->dev, "unsupported function %s on pin %s",
195 function, group);
196 continue;
197 }
198
199 (*map)[i].type = PIN_MAP_TYPE_MUX_GROUP;
200 (*map)[i].data.mux.group = group;
201 (*map)[i].data.mux.function = function;
202
203 i++;
204
205 (*map)[i].type = PIN_MAP_TYPE_CONFIGS_GROUP;
206 (*map)[i].data.configs.group_or_pin = group;
207
208 if (of_find_property(node, "allwinner,drive", NULL))
209 configlen++;
210 if (of_find_property(node, "allwinner,pull", NULL))
211 configlen++;
212
213 pinconfig = kzalloc(configlen * sizeof(*pinconfig), GFP_KERNEL);
214
215 if (!of_property_read_u32(node, "allwinner,drive", &val)) {
216 u16 strength = (val + 1) * 10;
217 pinconfig[j++] =
218 pinconf_to_config_packed(PIN_CONFIG_DRIVE_STRENGTH,
219 strength);
220 }
221
222 if (!of_property_read_u32(node, "allwinner,pull", &val)) {
223 enum pin_config_param pull = PIN_CONFIG_END;
224 if (val == 1)
225 pull = PIN_CONFIG_BIAS_PULL_UP;
226 else if (val == 2)
227 pull = PIN_CONFIG_BIAS_PULL_DOWN;
228 pinconfig[j++] = pinconf_to_config_packed(pull, 0);
229 }
230
231 (*map)[i].data.configs.configs = pinconfig;
232 (*map)[i].data.configs.num_configs = configlen;
233
234 i++;
235 }
236
237 *num_maps = nmaps;
238
239 return 0;
240}
241
242static void sunxi_pctrl_dt_free_map(struct pinctrl_dev *pctldev,
243 struct pinctrl_map *map,
244 unsigned num_maps)
245{
246 int i;
247
248 for (i = 0; i < num_maps; i++) {
249 if (map[i].type == PIN_MAP_TYPE_CONFIGS_GROUP)
250 kfree(map[i].data.configs.configs);
251 }
252
253 kfree(map);
254}
255
Laurent Pinchart022ab142013-02-16 10:25:07 +0100256static const struct pinctrl_ops sunxi_pctrl_ops = {
Maxime Ripard0e37f882013-01-18 22:30:34 +0100257 .dt_node_to_map = sunxi_pctrl_dt_node_to_map,
258 .dt_free_map = sunxi_pctrl_dt_free_map,
259 .get_groups_count = sunxi_pctrl_get_groups_count,
260 .get_group_name = sunxi_pctrl_get_group_name,
261 .get_group_pins = sunxi_pctrl_get_group_pins,
262};
263
264static int sunxi_pconf_group_get(struct pinctrl_dev *pctldev,
265 unsigned group,
266 unsigned long *config)
267{
268 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
269
270 *config = pctl->groups[group].config;
271
272 return 0;
273}
274
275static int sunxi_pconf_group_set(struct pinctrl_dev *pctldev,
276 unsigned group,
Sherman Yin03b054e2013-08-27 11:32:12 -0700277 unsigned long *configs,
278 unsigned num_configs)
Maxime Ripard0e37f882013-01-18 22:30:34 +0100279{
280 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
281 struct sunxi_pinctrl_group *g = &pctl->groups[group];
Maxime Ripard1bee9632013-08-04 12:38:48 +0200282 unsigned long flags;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100283 u32 val, mask;
284 u16 strength;
285 u8 dlevel;
Sherman Yin03b054e2013-08-27 11:32:12 -0700286 int i;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100287
Linus Walleij6ad30ce2013-08-29 09:46:30 +0200288 spin_lock_irqsave(&pctl->lock, flags);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200289
Sherman Yin03b054e2013-08-27 11:32:12 -0700290 for (i = 0; i < num_configs; i++) {
291 switch (pinconf_to_config_param(configs[i])) {
292 case PIN_CONFIG_DRIVE_STRENGTH:
293 strength = pinconf_to_config_argument(configs[i]);
Linus Walleij07b7eb92013-08-29 19:17:13 +0200294 if (strength > 40) {
295 spin_unlock_irqrestore(&pctl->lock, flags);
Sherman Yin03b054e2013-08-27 11:32:12 -0700296 return -EINVAL;
Linus Walleij07b7eb92013-08-29 19:17:13 +0200297 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700298 /*
299 * We convert from mA to what the register expects:
300 * 0: 10mA
301 * 1: 20mA
302 * 2: 30mA
303 * 3: 40mA
304 */
305 dlevel = strength / 10 - 1;
306 val = readl(pctl->membase + sunxi_dlevel_reg(g->pin));
307 mask = DLEVEL_PINS_MASK << sunxi_dlevel_offset(g->pin);
308 writel((val & ~mask)
309 | dlevel << sunxi_dlevel_offset(g->pin),
310 pctl->membase + sunxi_dlevel_reg(g->pin));
311 break;
312 case PIN_CONFIG_BIAS_PULL_UP:
313 val = readl(pctl->membase + sunxi_pull_reg(g->pin));
314 mask = PULL_PINS_MASK << sunxi_pull_offset(g->pin);
315 writel((val & ~mask) | 1 << sunxi_pull_offset(g->pin),
316 pctl->membase + sunxi_pull_reg(g->pin));
317 break;
318 case PIN_CONFIG_BIAS_PULL_DOWN:
319 val = readl(pctl->membase + sunxi_pull_reg(g->pin));
320 mask = PULL_PINS_MASK << sunxi_pull_offset(g->pin);
321 writel((val & ~mask) | 2 << sunxi_pull_offset(g->pin),
322 pctl->membase + sunxi_pull_reg(g->pin));
323 break;
324 default:
325 break;
326 }
Sherman Yin03b054e2013-08-27 11:32:12 -0700327 /* cache the config value */
328 g->config = configs[i];
329 } /* for each config */
Maxime Ripard0e37f882013-01-18 22:30:34 +0100330
Linus Walleij6ad30ce2013-08-29 09:46:30 +0200331 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100332
333 return 0;
334}
335
Laurent Pinchart022ab142013-02-16 10:25:07 +0100336static const struct pinconf_ops sunxi_pconf_ops = {
Maxime Ripard0e37f882013-01-18 22:30:34 +0100337 .pin_config_group_get = sunxi_pconf_group_get,
338 .pin_config_group_set = sunxi_pconf_group_set,
339};
340
341static int sunxi_pmx_get_funcs_cnt(struct pinctrl_dev *pctldev)
342{
343 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
344
345 return pctl->nfunctions;
346}
347
348static const char *sunxi_pmx_get_func_name(struct pinctrl_dev *pctldev,
349 unsigned function)
350{
351 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
352
353 return pctl->functions[function].name;
354}
355
356static int sunxi_pmx_get_func_groups(struct pinctrl_dev *pctldev,
357 unsigned function,
358 const char * const **groups,
359 unsigned * const num_groups)
360{
361 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
362
363 *groups = pctl->functions[function].groups;
364 *num_groups = pctl->functions[function].ngroups;
365
366 return 0;
367}
368
369static void sunxi_pmx_set(struct pinctrl_dev *pctldev,
370 unsigned pin,
371 u8 config)
372{
373 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200374 unsigned long flags;
375 u32 val, mask;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100376
Maxime Ripard1bee9632013-08-04 12:38:48 +0200377 spin_lock_irqsave(&pctl->lock, flags);
378
379 val = readl(pctl->membase + sunxi_mux_reg(pin));
380 mask = MUX_PINS_MASK << sunxi_mux_offset(pin);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100381 writel((val & ~mask) | config << sunxi_mux_offset(pin),
382 pctl->membase + sunxi_mux_reg(pin));
Maxime Ripard1bee9632013-08-04 12:38:48 +0200383
384 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100385}
386
387static int sunxi_pmx_enable(struct pinctrl_dev *pctldev,
388 unsigned function,
389 unsigned group)
390{
391 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
392 struct sunxi_pinctrl_group *g = pctl->groups + group;
393 struct sunxi_pinctrl_function *func = pctl->functions + function;
394 struct sunxi_desc_function *desc =
395 sunxi_pinctrl_desc_find_function_by_name(pctl,
396 g->name,
397 func->name);
398
399 if (!desc)
400 return -EINVAL;
401
402 sunxi_pmx_set(pctldev, g->pin, desc->muxval);
403
404 return 0;
405}
406
Maxime Ripard08e9e612013-01-28 21:33:12 +0100407static int
408sunxi_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
409 struct pinctrl_gpio_range *range,
410 unsigned offset,
411 bool input)
412{
413 struct sunxi_pinctrl *pctl = pinctrl_dev_get_drvdata(pctldev);
414 struct sunxi_desc_function *desc;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100415 const char *func;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100416
417 if (input)
418 func = "gpio_in";
419 else
420 func = "gpio_out";
421
Maxime Ripard814d4f22013-06-08 12:05:43 +0200422 desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, func);
423 if (!desc)
424 return -EINVAL;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100425
426 sunxi_pmx_set(pctldev, offset, desc->muxval);
427
Maxime Ripard814d4f22013-06-08 12:05:43 +0200428 return 0;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100429}
430
Laurent Pinchart022ab142013-02-16 10:25:07 +0100431static const struct pinmux_ops sunxi_pmx_ops = {
Maxime Ripard0e37f882013-01-18 22:30:34 +0100432 .get_functions_count = sunxi_pmx_get_funcs_cnt,
433 .get_function_name = sunxi_pmx_get_func_name,
434 .get_function_groups = sunxi_pmx_get_func_groups,
435 .enable = sunxi_pmx_enable,
Maxime Ripard08e9e612013-01-28 21:33:12 +0100436 .gpio_set_direction = sunxi_pmx_gpio_set_direction,
Maxime Ripard0e37f882013-01-18 22:30:34 +0100437};
438
Maxime Ripard08e9e612013-01-28 21:33:12 +0100439static int sunxi_pinctrl_gpio_request(struct gpio_chip *chip, unsigned offset)
440{
441 return pinctrl_request_gpio(chip->base + offset);
442}
443
444static void sunxi_pinctrl_gpio_free(struct gpio_chip *chip, unsigned offset)
445{
446 pinctrl_free_gpio(chip->base + offset);
447}
448
449static int sunxi_pinctrl_gpio_direction_input(struct gpio_chip *chip,
450 unsigned offset)
451{
452 return pinctrl_gpio_direction_input(chip->base + offset);
453}
454
455static int sunxi_pinctrl_gpio_get(struct gpio_chip *chip, unsigned offset)
456{
457 struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
458
459 u32 reg = sunxi_data_reg(offset);
460 u8 index = sunxi_data_offset(offset);
461 u32 val = (readl(pctl->membase + reg) >> index) & DATA_PINS_MASK;
462
463 return val;
464}
465
Maxime Ripard08e9e612013-01-28 21:33:12 +0100466static void sunxi_pinctrl_gpio_set(struct gpio_chip *chip,
467 unsigned offset, int value)
468{
469 struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
470 u32 reg = sunxi_data_reg(offset);
471 u8 index = sunxi_data_offset(offset);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200472 unsigned long flags;
473 u32 regval;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100474
Maxime Ripard1bee9632013-08-04 12:38:48 +0200475 spin_lock_irqsave(&pctl->lock, flags);
476
477 regval = readl(pctl->membase + reg);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100478
Maxime Riparddf7b34f2013-07-25 12:41:16 +0200479 if (value)
480 regval |= BIT(index);
481 else
482 regval &= ~(BIT(index));
483
484 writel(regval, pctl->membase + reg);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200485
486 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100487}
488
Chen-Yu Tsaifa8cf572014-01-16 14:34:23 +0800489static int sunxi_pinctrl_gpio_direction_output(struct gpio_chip *chip,
490 unsigned offset, int value)
491{
492 sunxi_pinctrl_gpio_set(chip, offset, value);
493 return pinctrl_gpio_direction_output(chip->base + offset);
494}
495
Maxime Riparda0d72092013-02-03 12:10:11 +0100496static int sunxi_pinctrl_gpio_of_xlate(struct gpio_chip *gc,
497 const struct of_phandle_args *gpiospec,
498 u32 *flags)
499{
500 int pin, base;
501
502 base = PINS_PER_BANK * gpiospec->args[0];
503 pin = base + gpiospec->args[1];
504
505 if (pin > (gc->base + gc->ngpio))
506 return -EINVAL;
507
508 if (flags)
509 *flags = gpiospec->args[2];
510
511 return pin;
512}
513
Maxime Ripard60242db2013-06-08 12:05:44 +0200514static int sunxi_pinctrl_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
515{
516 struct sunxi_pinctrl *pctl = dev_get_drvdata(chip->dev);
517 struct sunxi_desc_function *desc;
518
Axel Linc9e3b2d2013-08-30 16:31:25 +0800519 if (offset >= chip->ngpio)
Maxime Ripard60242db2013-06-08 12:05:44 +0200520 return -ENXIO;
521
522 desc = sunxi_pinctrl_desc_find_function_by_pin(pctl, offset, "irq");
523 if (!desc)
524 return -EINVAL;
525
526 pctl->irq_array[desc->irqnum] = offset;
527
528 dev_dbg(chip->dev, "%s: request IRQ for GPIO %d, return %d\n",
529 chip->label, offset + chip->base, desc->irqnum);
530
531 return irq_find_mapping(pctl->domain, desc->irqnum);
532}
533
Maxime Ripard08e9e612013-01-28 21:33:12 +0100534
Maxime Ripard60242db2013-06-08 12:05:44 +0200535static int sunxi_pinctrl_irq_set_type(struct irq_data *d,
536 unsigned int type)
537{
538 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
539 u32 reg = sunxi_irq_cfg_reg(d->hwirq);
540 u8 index = sunxi_irq_cfg_offset(d->hwirq);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200541 unsigned long flags;
Maxime Ripard2aaaddf2013-08-04 12:38:47 +0200542 u32 regval;
Maxime Ripard60242db2013-06-08 12:05:44 +0200543 u8 mode;
544
545 switch (type) {
546 case IRQ_TYPE_EDGE_RISING:
547 mode = IRQ_EDGE_RISING;
548 break;
549 case IRQ_TYPE_EDGE_FALLING:
550 mode = IRQ_EDGE_FALLING;
551 break;
552 case IRQ_TYPE_EDGE_BOTH:
553 mode = IRQ_EDGE_BOTH;
554 break;
555 case IRQ_TYPE_LEVEL_HIGH:
556 mode = IRQ_LEVEL_HIGH;
557 break;
558 case IRQ_TYPE_LEVEL_LOW:
559 mode = IRQ_LEVEL_LOW;
560 break;
561 default:
562 return -EINVAL;
563 }
564
Maxime Ripard1bee9632013-08-04 12:38:48 +0200565 spin_lock_irqsave(&pctl->lock, flags);
566
Maxime Ripard2aaaddf2013-08-04 12:38:47 +0200567 regval = readl(pctl->membase + reg);
Hans de Goeded82f9402014-02-17 22:19:43 +0100568 regval &= ~(IRQ_CFG_IRQ_MASK << index);
Maxime Ripard2aaaddf2013-08-04 12:38:47 +0200569 writel(regval | (mode << index), pctl->membase + reg);
Maxime Ripard60242db2013-06-08 12:05:44 +0200570
Maxime Ripard1bee9632013-08-04 12:38:48 +0200571 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard60242db2013-06-08 12:05:44 +0200572
573 return 0;
574}
575
576static void sunxi_pinctrl_irq_mask_ack(struct irq_data *d)
577{
578 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
579 u32 ctrl_reg = sunxi_irq_ctrl_reg(d->hwirq);
580 u8 ctrl_idx = sunxi_irq_ctrl_offset(d->hwirq);
581 u32 status_reg = sunxi_irq_status_reg(d->hwirq);
582 u8 status_idx = sunxi_irq_status_offset(d->hwirq);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200583 unsigned long flags;
Maxime Ripard60242db2013-06-08 12:05:44 +0200584 u32 val;
585
Maxime Ripard1bee9632013-08-04 12:38:48 +0200586 spin_lock_irqsave(&pctl->lock, flags);
587
Maxime Ripard60242db2013-06-08 12:05:44 +0200588 /* Mask the IRQ */
589 val = readl(pctl->membase + ctrl_reg);
590 writel(val & ~(1 << ctrl_idx), pctl->membase + ctrl_reg);
591
592 /* Clear the IRQ */
593 writel(1 << status_idx, pctl->membase + status_reg);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200594
595 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard60242db2013-06-08 12:05:44 +0200596}
597
598static void sunxi_pinctrl_irq_mask(struct irq_data *d)
599{
600 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
601 u32 reg = sunxi_irq_ctrl_reg(d->hwirq);
602 u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200603 unsigned long flags;
Maxime Ripard60242db2013-06-08 12:05:44 +0200604 u32 val;
605
Maxime Ripard1bee9632013-08-04 12:38:48 +0200606 spin_lock_irqsave(&pctl->lock, flags);
607
Maxime Ripard60242db2013-06-08 12:05:44 +0200608 /* Mask the IRQ */
609 val = readl(pctl->membase + reg);
610 writel(val & ~(1 << idx), pctl->membase + reg);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200611
612 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard60242db2013-06-08 12:05:44 +0200613}
614
615static void sunxi_pinctrl_irq_unmask(struct irq_data *d)
616{
617 struct sunxi_pinctrl *pctl = irq_data_get_irq_chip_data(d);
618 struct sunxi_desc_function *func;
619 u32 reg = sunxi_irq_ctrl_reg(d->hwirq);
620 u8 idx = sunxi_irq_ctrl_offset(d->hwirq);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200621 unsigned long flags;
Maxime Ripard60242db2013-06-08 12:05:44 +0200622 u32 val;
623
624 func = sunxi_pinctrl_desc_find_function_by_pin(pctl,
625 pctl->irq_array[d->hwirq],
626 "irq");
627
628 /* Change muxing to INT mode */
629 sunxi_pmx_set(pctl->pctl_dev, pctl->irq_array[d->hwirq], func->muxval);
630
Maxime Ripard1bee9632013-08-04 12:38:48 +0200631 spin_lock_irqsave(&pctl->lock, flags);
632
Maxime Ripard60242db2013-06-08 12:05:44 +0200633 /* Unmask the IRQ */
634 val = readl(pctl->membase + reg);
635 writel(val | (1 << idx), pctl->membase + reg);
Maxime Ripard1bee9632013-08-04 12:38:48 +0200636
637 spin_unlock_irqrestore(&pctl->lock, flags);
Maxime Ripard60242db2013-06-08 12:05:44 +0200638}
639
640static struct irq_chip sunxi_pinctrl_irq_chip = {
641 .irq_mask = sunxi_pinctrl_irq_mask,
642 .irq_mask_ack = sunxi_pinctrl_irq_mask_ack,
643 .irq_unmask = sunxi_pinctrl_irq_unmask,
644 .irq_set_type = sunxi_pinctrl_irq_set_type,
645};
646
647static void sunxi_pinctrl_irq_handler(unsigned irq, struct irq_desc *desc)
648{
Chen-Yu Tsai905a5112014-02-11 00:22:37 +0800649 struct irq_chip *chip = irq_get_chip(irq);
Maxime Ripard60242db2013-06-08 12:05:44 +0200650 struct sunxi_pinctrl *pctl = irq_get_handler_data(irq);
651 const unsigned long reg = readl(pctl->membase + IRQ_STATUS_REG);
652
653 /* Clear all interrupts */
654 writel(reg, pctl->membase + IRQ_STATUS_REG);
655
656 if (reg) {
657 int irqoffset;
658
Chen-Yu Tsai905a5112014-02-11 00:22:37 +0800659 chained_irq_enter(chip, desc);
Maxime Ripard60242db2013-06-08 12:05:44 +0200660 for_each_set_bit(irqoffset, &reg, SUNXI_IRQ_NUMBER) {
661 int pin_irq = irq_find_mapping(pctl->domain, irqoffset);
662 generic_handle_irq(pin_irq);
663 }
Chen-Yu Tsai905a5112014-02-11 00:22:37 +0800664 chained_irq_exit(chip, desc);
Maxime Ripard60242db2013-06-08 12:05:44 +0200665 }
666}
667
Maxime Ripard0e37f882013-01-18 22:30:34 +0100668static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl,
669 const char *name)
670{
671 struct sunxi_pinctrl_function *func = pctl->functions;
672
673 while (func->name) {
674 /* function already there */
675 if (strcmp(func->name, name) == 0) {
676 func->ngroups++;
677 return -EEXIST;
678 }
679 func++;
680 }
681
682 func->name = name;
683 func->ngroups = 1;
684
685 pctl->nfunctions++;
686
687 return 0;
688}
689
690static int sunxi_pinctrl_build_state(struct platform_device *pdev)
691{
692 struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev);
693 int i;
694
695 pctl->ngroups = pctl->desc->npins;
696
697 /* Allocate groups */
698 pctl->groups = devm_kzalloc(&pdev->dev,
699 pctl->ngroups * sizeof(*pctl->groups),
700 GFP_KERNEL);
701 if (!pctl->groups)
702 return -ENOMEM;
703
704 for (i = 0; i < pctl->desc->npins; i++) {
705 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
706 struct sunxi_pinctrl_group *group = pctl->groups + i;
707
708 group->name = pin->pin.name;
709 group->pin = pin->pin.number;
710 }
711
712 /*
713 * We suppose that we won't have any more functions than pins,
714 * we'll reallocate that later anyway
715 */
716 pctl->functions = devm_kzalloc(&pdev->dev,
717 pctl->desc->npins * sizeof(*pctl->functions),
718 GFP_KERNEL);
719 if (!pctl->functions)
720 return -ENOMEM;
721
722 /* Count functions and their associated groups */
723 for (i = 0; i < pctl->desc->npins; i++) {
724 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
725 struct sunxi_desc_function *func = pin->functions;
726
727 while (func->name) {
728 sunxi_pinctrl_add_function(pctl, func->name);
729 func++;
730 }
731 }
732
733 pctl->functions = krealloc(pctl->functions,
734 pctl->nfunctions * sizeof(*pctl->functions),
735 GFP_KERNEL);
736
737 for (i = 0; i < pctl->desc->npins; i++) {
738 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
739 struct sunxi_desc_function *func = pin->functions;
740
741 while (func->name) {
742 struct sunxi_pinctrl_function *func_item;
743 const char **func_grp;
744
745 func_item = sunxi_pinctrl_find_function_by_name(pctl,
746 func->name);
747 if (!func_item)
748 return -EINVAL;
749
750 if (!func_item->groups) {
751 func_item->groups =
752 devm_kzalloc(&pdev->dev,
753 func_item->ngroups * sizeof(*func_item->groups),
754 GFP_KERNEL);
755 if (!func_item->groups)
756 return -ENOMEM;
757 }
758
759 func_grp = func_item->groups;
760 while (*func_grp)
761 func_grp++;
762
763 *func_grp = pin->pin.name;
764 func++;
765 }
766 }
767
768 return 0;
769}
770
Maxime Ripard2284ba62014-04-18 20:10:41 +0200771int sunxi_pinctrl_init(struct platform_device *pdev,
772 const struct sunxi_pinctrl_desc *desc)
Maxime Ripard0e37f882013-01-18 22:30:34 +0100773{
774 struct device_node *node = pdev->dev.of_node;
Maxime Ripardba6764d2014-05-22 16:25:27 +0200775 struct pinctrl_desc *pctrl_desc;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100776 struct pinctrl_pin_desc *pins;
777 struct sunxi_pinctrl *pctl;
Maxime Ripard4409caf2014-04-26 21:59:50 +0200778 struct resource *res;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100779 int i, ret, last_pin;
Emilio López950707c2013-03-22 11:20:40 -0300780 struct clk *clk;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100781
782 pctl = devm_kzalloc(&pdev->dev, sizeof(*pctl), GFP_KERNEL);
783 if (!pctl)
784 return -ENOMEM;
785 platform_set_drvdata(pdev, pctl);
786
Maxime Ripard1bee9632013-08-04 12:38:48 +0200787 spin_lock_init(&pctl->lock);
788
Maxime Ripard4409caf2014-04-26 21:59:50 +0200789 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
790 pctl->membase = devm_ioremap_resource(&pdev->dev, res);
791 if (IS_ERR(pctl->membase))
792 return PTR_ERR(pctl->membase);
Maxime Ripard0e37f882013-01-18 22:30:34 +0100793
Maxime Ripardba6764d2014-05-22 16:25:27 +0200794 pctl->dev = &pdev->dev;
Maxime Ripard2284ba62014-04-18 20:10:41 +0200795 pctl->desc = desc;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100796
797 ret = sunxi_pinctrl_build_state(pdev);
798 if (ret) {
799 dev_err(&pdev->dev, "dt probe failed: %d\n", ret);
800 return ret;
801 }
802
803 pins = devm_kzalloc(&pdev->dev,
804 pctl->desc->npins * sizeof(*pins),
805 GFP_KERNEL);
806 if (!pins)
807 return -ENOMEM;
808
809 for (i = 0; i < pctl->desc->npins; i++)
810 pins[i] = pctl->desc->pins[i].pin;
811
Maxime Ripardba6764d2014-05-22 16:25:27 +0200812 pctrl_desc = devm_kzalloc(&pdev->dev,
813 sizeof(*pctrl_desc),
814 GFP_KERNEL);
815 if (!pctrl_desc)
816 return -ENOMEM;
817
818 pctrl_desc->name = dev_name(&pdev->dev);
819 pctrl_desc->owner = THIS_MODULE;
820 pctrl_desc->pins = pins;
821 pctrl_desc->npins = pctl->desc->npins;
822 pctrl_desc->confops = &sunxi_pconf_ops;
823 pctrl_desc->pctlops = &sunxi_pctrl_ops;
824 pctrl_desc->pmxops = &sunxi_pmx_ops;
825
826 pctl->pctl_dev = pinctrl_register(pctrl_desc,
Maxime Ripard0e37f882013-01-18 22:30:34 +0100827 &pdev->dev, pctl);
828 if (!pctl->pctl_dev) {
829 dev_err(&pdev->dev, "couldn't register pinctrl driver\n");
830 return -EINVAL;
831 }
832
Maxime Ripard08e9e612013-01-28 21:33:12 +0100833 pctl->chip = devm_kzalloc(&pdev->dev, sizeof(*pctl->chip), GFP_KERNEL);
834 if (!pctl->chip) {
835 ret = -ENOMEM;
836 goto pinctrl_error;
837 }
838
839 last_pin = pctl->desc->pins[pctl->desc->npins - 1].pin.number;
Boris BREZILLONd83c82c2014-04-10 15:52:43 +0200840 pctl->chip->owner = THIS_MODULE;
841 pctl->chip->request = sunxi_pinctrl_gpio_request,
842 pctl->chip->free = sunxi_pinctrl_gpio_free,
843 pctl->chip->direction_input = sunxi_pinctrl_gpio_direction_input,
844 pctl->chip->direction_output = sunxi_pinctrl_gpio_direction_output,
845 pctl->chip->get = sunxi_pinctrl_gpio_get,
846 pctl->chip->set = sunxi_pinctrl_gpio_set,
847 pctl->chip->of_xlate = sunxi_pinctrl_gpio_of_xlate,
848 pctl->chip->to_irq = sunxi_pinctrl_gpio_to_irq,
849 pctl->chip->of_gpio_n_cells = 3,
850 pctl->chip->can_sleep = false,
851 pctl->chip->ngpio = round_up(last_pin, PINS_PER_BANK) -
852 pctl->desc->pin_base;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100853 pctl->chip->label = dev_name(&pdev->dev);
854 pctl->chip->dev = &pdev->dev;
Boris BREZILLONd83c82c2014-04-10 15:52:43 +0200855 pctl->chip->base = pctl->desc->pin_base;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100856
857 ret = gpiochip_add(pctl->chip);
858 if (ret)
859 goto pinctrl_error;
860
861 for (i = 0; i < pctl->desc->npins; i++) {
862 const struct sunxi_desc_pin *pin = pctl->desc->pins + i;
863
864 ret = gpiochip_add_pin_range(pctl->chip, dev_name(&pdev->dev),
865 pin->pin.number,
866 pin->pin.number, 1);
867 if (ret)
868 goto gpiochip_error;
869 }
870
Emilio López950707c2013-03-22 11:20:40 -0300871 clk = devm_clk_get(&pdev->dev, NULL);
Wei Yongjund72f88a2013-05-23 17:32:14 +0800872 if (IS_ERR(clk)) {
873 ret = PTR_ERR(clk);
Emilio López950707c2013-03-22 11:20:40 -0300874 goto gpiochip_error;
Wei Yongjund72f88a2013-05-23 17:32:14 +0800875 }
Emilio López950707c2013-03-22 11:20:40 -0300876
Boris BREZILLON64150932014-04-10 15:52:40 +0200877 ret = clk_prepare_enable(clk);
878 if (ret)
879 goto gpiochip_error;
Emilio López950707c2013-03-22 11:20:40 -0300880
Maxime Ripard60242db2013-06-08 12:05:44 +0200881 pctl->irq = irq_of_parse_and_map(node, 0);
882 if (!pctl->irq) {
883 ret = -EINVAL;
Maxime Riparddc969102014-04-26 22:28:54 +0200884 goto clk_error;
Maxime Ripard60242db2013-06-08 12:05:44 +0200885 }
886
887 pctl->domain = irq_domain_add_linear(node, SUNXI_IRQ_NUMBER,
888 &irq_domain_simple_ops, NULL);
889 if (!pctl->domain) {
890 dev_err(&pdev->dev, "Couldn't register IRQ domain\n");
891 ret = -ENOMEM;
Maxime Riparddc969102014-04-26 22:28:54 +0200892 goto clk_error;
Maxime Ripard60242db2013-06-08 12:05:44 +0200893 }
894
895 for (i = 0; i < SUNXI_IRQ_NUMBER; i++) {
896 int irqno = irq_create_mapping(pctl->domain, i);
897
898 irq_set_chip_and_handler(irqno, &sunxi_pinctrl_irq_chip,
899 handle_simple_irq);
900 irq_set_chip_data(irqno, pctl);
901 };
902
903 irq_set_chained_handler(pctl->irq, sunxi_pinctrl_irq_handler);
904 irq_set_handler_data(pctl->irq, pctl);
905
Maxime Ripard08e9e612013-01-28 21:33:12 +0100906 dev_info(&pdev->dev, "initialized sunXi PIO driver\n");
Maxime Ripard0e37f882013-01-18 22:30:34 +0100907
908 return 0;
Maxime Ripard08e9e612013-01-28 21:33:12 +0100909
Boris BREZILLONe2bddc62014-04-10 15:52:41 +0200910clk_error:
911 clk_disable_unprepare(clk);
Maxime Ripard08e9e612013-01-28 21:33:12 +0100912gpiochip_error:
Axel Lin97fc4632013-05-19 13:58:37 +0800913 if (gpiochip_remove(pctl->chip))
914 dev_err(&pdev->dev, "failed to remove gpio chip\n");
Maxime Ripard08e9e612013-01-28 21:33:12 +0100915pinctrl_error:
916 pinctrl_unregister(pctl->pctl_dev);
917 return ret;
Maxime Ripard0e37f882013-01-18 22:30:34 +0100918}