blob: 001542f68627ccc623e31e5eff21d84e510f2093 [file] [log] [blame]
Gregory CLEMENT87466cc2017-04-05 17:18:04 +02001/*
2 * Marvell 37xx SoC pinctrl driver
3 *
4 * Copyright (C) 2017 Marvell
5 *
6 * Gregory CLEMENT <gregory.clement@free-electrons.com>
7 *
8 * This file is licensed under the terms of the GNU General Public
9 * License version 2 or later. This program is licensed "as is"
10 * without any warranty of any kind, whether express or implied.
11 */
12
Gregory CLEMENT57150922017-04-05 17:18:05 +020013#include <linux/gpio/driver.h>
Gregory CLEMENT87466cc2017-04-05 17:18:04 +020014#include <linux/mfd/syscon.h>
15#include <linux/of.h>
Gregory CLEMENT2f227602017-04-28 16:01:33 +020016#include <linux/of_address.h>
Gregory CLEMENT87466cc2017-04-05 17:18:04 +020017#include <linux/of_device.h>
Gregory CLEMENT2f227602017-04-28 16:01:33 +020018#include <linux/of_irq.h>
Gregory CLEMENT87466cc2017-04-05 17:18:04 +020019#include <linux/pinctrl/pinconf-generic.h>
20#include <linux/pinctrl/pinconf.h>
21#include <linux/pinctrl/pinctrl.h>
22#include <linux/pinctrl/pinmux.h>
23#include <linux/platform_device.h>
24#include <linux/regmap.h>
25#include <linux/slab.h>
26
27#include "../pinctrl-utils.h"
28
29#define OUTPUT_EN 0x0
Gregory CLEMENT57150922017-04-05 17:18:05 +020030#define INPUT_VAL 0x10
31#define OUTPUT_VAL 0x18
Gregory CLEMENT87466cc2017-04-05 17:18:04 +020032#define OUTPUT_CTL 0x20
33#define SELECTION 0x30
34
Gregory CLEMENT2f227602017-04-28 16:01:33 +020035#define IRQ_EN 0x0
36#define IRQ_POL 0x08
37#define IRQ_STATUS 0x10
38#define IRQ_WKUP 0x18
39
Gregory CLEMENT87466cc2017-04-05 17:18:04 +020040#define NB_FUNCS 2
41#define GPIO_PER_REG 32
42
43/**
44 * struct armada_37xx_pin_group: represents group of pins of a pinmux function.
45 * The pins of a pinmux groups are composed of one or two groups of contiguous
46 * pins.
47 * @name: Name of the pin group, used to lookup the group.
48 * @start_pins: Index of the first pin of the main range of pins belonging to
49 * the group
50 * @npins: Number of pins included in the first range
51 * @reg_mask: Bit mask matching the group in the selection register
52 * @extra_pins: Index of the first pin of the optional second range of pins
53 * belonging to the group
54 * @npins: Number of pins included in the second optional range
55 * @funcs: A list of pinmux functions that can be selected for this group.
56 * @pins: List of the pins included in the group
57 */
58struct armada_37xx_pin_group {
59 const char *name;
60 unsigned int start_pin;
61 unsigned int npins;
62 u32 reg_mask;
63 u32 val[NB_FUNCS];
64 unsigned int extra_pin;
65 unsigned int extra_npins;
66 const char *funcs[NB_FUNCS];
67 unsigned int *pins;
68};
69
70struct armada_37xx_pin_data {
71 u8 nr_pins;
72 char *name;
73 struct armada_37xx_pin_group *groups;
74 int ngroups;
75};
76
77struct armada_37xx_pmx_func {
78 const char *name;
79 const char **groups;
80 unsigned int ngroups;
81};
82
83struct armada_37xx_pinctrl {
84 struct regmap *regmap;
Gregory CLEMENT2f227602017-04-28 16:01:33 +020085 void __iomem *base;
Gregory CLEMENT87466cc2017-04-05 17:18:04 +020086 const struct armada_37xx_pin_data *data;
87 struct device *dev;
Gregory CLEMENT57150922017-04-05 17:18:05 +020088 struct gpio_chip gpio_chip;
Gregory CLEMENT2f227602017-04-28 16:01:33 +020089 struct irq_chip irq_chip;
90 spinlock_t irq_lock;
Gregory CLEMENT87466cc2017-04-05 17:18:04 +020091 struct pinctrl_desc pctl;
92 struct pinctrl_dev *pctl_dev;
93 struct armada_37xx_pin_group *groups;
94 unsigned int ngroups;
95 struct armada_37xx_pmx_func *funcs;
96 unsigned int nfuncs;
97};
98
99#define PIN_GRP(_name, _start, _nr, _mask, _func1, _func2) \
100 { \
101 .name = _name, \
102 .start_pin = _start, \
103 .npins = _nr, \
104 .reg_mask = _mask, \
105 .val = {0, _mask}, \
106 .funcs = {_func1, _func2} \
107 }
108
109#define PIN_GRP_GPIO(_name, _start, _nr, _mask, _func1) \
110 { \
111 .name = _name, \
112 .start_pin = _start, \
113 .npins = _nr, \
114 .reg_mask = _mask, \
115 .val = {0, _mask}, \
116 .funcs = {_func1, "gpio"} \
117 }
118
119#define PIN_GRP_GPIO_2(_name, _start, _nr, _mask, _val1, _val2, _func1) \
120 { \
121 .name = _name, \
122 .start_pin = _start, \
123 .npins = _nr, \
124 .reg_mask = _mask, \
125 .val = {_val1, _val2}, \
126 .funcs = {_func1, "gpio"} \
127 }
128
129#define PIN_GRP_EXTRA(_name, _start, _nr, _mask, _v1, _v2, _start2, _nr2, \
130 _f1, _f2) \
131 { \
132 .name = _name, \
133 .start_pin = _start, \
134 .npins = _nr, \
135 .reg_mask = _mask, \
136 .val = {_v1, _v2}, \
137 .extra_pin = _start2, \
138 .extra_npins = _nr2, \
139 .funcs = {_f1, _f2} \
140 }
141
142static struct armada_37xx_pin_group armada_37xx_nb_groups[] = {
143 PIN_GRP_GPIO("jtag", 20, 5, BIT(0), "jtag"),
144 PIN_GRP_GPIO("sdio0", 8, 3, BIT(1), "sdio"),
145 PIN_GRP_GPIO("emmc_nb", 27, 9, BIT(2), "emmc"),
146 PIN_GRP_GPIO("pwm0", 11, 1, BIT(3), "pwm"),
147 PIN_GRP_GPIO("pwm1", 12, 1, BIT(4), "pwm"),
148 PIN_GRP_GPIO("pwm2", 13, 1, BIT(5), "pwm"),
149 PIN_GRP_GPIO("pwm3", 14, 1, BIT(6), "pwm"),
150 PIN_GRP_GPIO("pmic1", 17, 1, BIT(7), "pmic"),
151 PIN_GRP_GPIO("pmic0", 16, 1, BIT(8), "pmic"),
152 PIN_GRP_GPIO("i2c2", 2, 2, BIT(9), "i2c"),
153 PIN_GRP_GPIO("i2c1", 0, 2, BIT(10), "i2c"),
154 PIN_GRP_GPIO("spi_cs1", 17, 1, BIT(12), "spi"),
155 PIN_GRP_GPIO_2("spi_cs2", 18, 1, BIT(13) | BIT(19), 0, BIT(13), "spi"),
156 PIN_GRP_GPIO_2("spi_cs3", 19, 1, BIT(14) | BIT(19), 0, BIT(14), "spi"),
157 PIN_GRP_GPIO("onewire", 4, 1, BIT(16), "onewire"),
158 PIN_GRP_GPIO("uart1", 25, 2, BIT(17), "uart"),
159 PIN_GRP_GPIO("spi_quad", 15, 2, BIT(18), "spi"),
160 PIN_GRP_EXTRA("uart2", 9, 2, BIT(13) | BIT(14) | BIT(19),
161 BIT(13) | BIT(14), BIT(19), 18, 2, "gpio", "uart"),
162 PIN_GRP_GPIO("led0_od", 11, 1, BIT(20), "led"),
163 PIN_GRP_GPIO("led1_od", 12, 1, BIT(21), "led"),
164 PIN_GRP_GPIO("led2_od", 13, 1, BIT(22), "led"),
165 PIN_GRP_GPIO("led3_od", 14, 1, BIT(23), "led"),
166
167};
168
169static struct armada_37xx_pin_group armada_37xx_sb_groups[] = {
170 PIN_GRP_GPIO("usb32_drvvbus0", 0, 1, BIT(0), "drvbus"),
171 PIN_GRP_GPIO("usb2_drvvbus1", 1, 1, BIT(1), "drvbus"),
172 PIN_GRP_GPIO("sdio_sb", 24, 5, BIT(2), "sdio"),
173 PIN_GRP_EXTRA("rgmii", 6, 14, BIT(3), 0, BIT(3), 23, 1, "mii", "gpio"),
174 PIN_GRP_GPIO("pcie1", 3, 2, BIT(4), "pcie"),
175 PIN_GRP_GPIO("ptp", 20, 3, BIT(5), "ptp"),
176 PIN_GRP("ptp_clk", 21, 1, BIT(6), "ptp", "mii"),
177 PIN_GRP("ptp_trig", 22, 1, BIT(7), "ptp", "mii"),
178 PIN_GRP("mii_col", 23, 1, BIT(8), "mii", "mii_err"),
179};
180
181const struct armada_37xx_pin_data armada_37xx_pin_nb = {
182 .nr_pins = 36,
183 .name = "GPIO1",
184 .groups = armada_37xx_nb_groups,
185 .ngroups = ARRAY_SIZE(armada_37xx_nb_groups),
186};
187
188const struct armada_37xx_pin_data armada_37xx_pin_sb = {
189 .nr_pins = 29,
190 .name = "GPIO2",
191 .groups = armada_37xx_sb_groups,
192 .ngroups = ARRAY_SIZE(armada_37xx_sb_groups),
193};
194
Gregory CLEMENT57150922017-04-05 17:18:05 +0200195static inline void armada_37xx_update_reg(unsigned int *reg,
196 unsigned int offset)
197{
198 /* We never have more than 2 registers */
199 if (offset >= GPIO_PER_REG) {
200 offset -= GPIO_PER_REG;
201 *reg += sizeof(u32);
202 }
203}
204
Gregory CLEMENT87466cc2017-04-05 17:18:04 +0200205static int armada_37xx_get_func_reg(struct armada_37xx_pin_group *grp,
206 const char *func)
207{
208 int f;
209
210 for (f = 0; f < NB_FUNCS; f++)
211 if (!strcmp(grp->funcs[f], func))
212 return f;
213
214 return -ENOTSUPP;
215}
216
217static struct armada_37xx_pin_group *armada_37xx_find_next_grp_by_pin(
218 struct armada_37xx_pinctrl *info, int pin, int *grp)
219{
220 while (*grp < info->ngroups) {
221 struct armada_37xx_pin_group *group = &info->groups[*grp];
222 int j;
223
224 *grp = *grp + 1;
225 for (j = 0; j < (group->npins + group->extra_npins); j++)
226 if (group->pins[j] == pin)
227 return group;
228 }
229 return NULL;
230}
231
232static int armada_37xx_pin_config_group_get(struct pinctrl_dev *pctldev,
233 unsigned int selector, unsigned long *config)
234{
235 return -ENOTSUPP;
236}
237
238static int armada_37xx_pin_config_group_set(struct pinctrl_dev *pctldev,
239 unsigned int selector, unsigned long *configs,
240 unsigned int num_configs)
241{
242 return -ENOTSUPP;
243}
244
245static struct pinconf_ops armada_37xx_pinconf_ops = {
246 .is_generic = true,
247 .pin_config_group_get = armada_37xx_pin_config_group_get,
248 .pin_config_group_set = armada_37xx_pin_config_group_set,
249};
250
251static int armada_37xx_get_groups_count(struct pinctrl_dev *pctldev)
252{
253 struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
254
255 return info->ngroups;
256}
257
258static const char *armada_37xx_get_group_name(struct pinctrl_dev *pctldev,
259 unsigned int group)
260{
261 struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
262
263 return info->groups[group].name;
264}
265
266static int armada_37xx_get_group_pins(struct pinctrl_dev *pctldev,
267 unsigned int selector,
268 const unsigned int **pins,
269 unsigned int *npins)
270{
271 struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
272
273 if (selector >= info->ngroups)
274 return -EINVAL;
275
276 *pins = info->groups[selector].pins;
277 *npins = info->groups[selector].npins +
278 info->groups[selector].extra_npins;
279
280 return 0;
281}
282
283static const struct pinctrl_ops armada_37xx_pctrl_ops = {
284 .get_groups_count = armada_37xx_get_groups_count,
285 .get_group_name = armada_37xx_get_group_name,
286 .get_group_pins = armada_37xx_get_group_pins,
287 .dt_node_to_map = pinconf_generic_dt_node_to_map_group,
288 .dt_free_map = pinctrl_utils_free_map,
289};
290
291/*
292 * Pinmux_ops handling
293 */
294
295static int armada_37xx_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
296{
297 struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
298
299 return info->nfuncs;
300}
301
302static const char *armada_37xx_pmx_get_func_name(struct pinctrl_dev *pctldev,
303 unsigned int selector)
304{
305 struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
306
307 return info->funcs[selector].name;
308}
309
310static int armada_37xx_pmx_get_groups(struct pinctrl_dev *pctldev,
311 unsigned int selector,
312 const char * const **groups,
313 unsigned int * const num_groups)
314{
315 struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
316
317 *groups = info->funcs[selector].groups;
318 *num_groups = info->funcs[selector].ngroups;
319
320 return 0;
321}
322
323static int armada_37xx_pmx_set_by_name(struct pinctrl_dev *pctldev,
324 const char *name,
325 struct armada_37xx_pin_group *grp)
326{
327 struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
328 unsigned int reg = SELECTION;
329 unsigned int mask = grp->reg_mask;
330 int func, val;
331
332 dev_dbg(info->dev, "enable function %s group %s\n",
333 name, grp->name);
334
335 func = armada_37xx_get_func_reg(grp, name);
336
337 if (func < 0)
338 return func;
339
340 val = grp->val[func];
341
342 regmap_update_bits(info->regmap, reg, mask, val);
343
344 return 0;
345}
346
347static int armada_37xx_pmx_set(struct pinctrl_dev *pctldev,
348 unsigned int selector,
349 unsigned int group)
350{
351
352 struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
353 struct armada_37xx_pin_group *grp = &info->groups[group];
354 const char *name = info->funcs[selector].name;
355
356 return armada_37xx_pmx_set_by_name(pctldev, name, grp);
357}
358
Gregory CLEMENT2f227602017-04-28 16:01:33 +0200359static inline void armada_37xx_irq_update_reg(unsigned int *reg,
360 struct irq_data *d)
361{
362 int offset = irqd_to_hwirq(d);
363
364 armada_37xx_update_reg(reg, offset);
365}
366
Gregory CLEMENT57150922017-04-05 17:18:05 +0200367static int armada_37xx_gpio_direction_input(struct gpio_chip *chip,
368 unsigned int offset)
Gregory CLEMENT87466cc2017-04-05 17:18:04 +0200369{
Gregory CLEMENT57150922017-04-05 17:18:05 +0200370 struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
Gregory CLEMENT87466cc2017-04-05 17:18:04 +0200371 unsigned int reg = OUTPUT_EN;
372 unsigned int mask;
373
Gregory CLEMENT57150922017-04-05 17:18:05 +0200374 armada_37xx_update_reg(&reg, offset);
Gregory CLEMENT87466cc2017-04-05 17:18:04 +0200375 mask = BIT(offset);
376
377 return regmap_update_bits(info->regmap, reg, mask, 0);
378}
379
Gregory CLEMENT57150922017-04-05 17:18:05 +0200380static int armada_37xx_gpio_get_direction(struct gpio_chip *chip,
381 unsigned int offset)
Gregory CLEMENT87466cc2017-04-05 17:18:04 +0200382{
Gregory CLEMENT57150922017-04-05 17:18:05 +0200383 struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
384 unsigned int reg = OUTPUT_EN;
385 unsigned int val, mask;
386
387 armada_37xx_update_reg(&reg, offset);
388 mask = BIT(offset);
389 regmap_read(info->regmap, reg, &val);
390
391 return !(val & mask);
392}
393
394static int armada_37xx_gpio_direction_output(struct gpio_chip *chip,
395 unsigned int offset, int value)
396{
397 struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
Gregory CLEMENT87466cc2017-04-05 17:18:04 +0200398 unsigned int reg = OUTPUT_EN;
399 unsigned int mask;
400
Gregory CLEMENT57150922017-04-05 17:18:05 +0200401 armada_37xx_update_reg(&reg, offset);
Gregory CLEMENT87466cc2017-04-05 17:18:04 +0200402 mask = BIT(offset);
403
404 return regmap_update_bits(info->regmap, reg, mask, mask);
405}
406
Gregory CLEMENT57150922017-04-05 17:18:05 +0200407static int armada_37xx_gpio_get(struct gpio_chip *chip, unsigned int offset)
408{
409 struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
410 unsigned int reg = INPUT_VAL;
411 unsigned int val, mask;
412
413 armada_37xx_update_reg(&reg, offset);
414 mask = BIT(offset);
415
416 regmap_read(info->regmap, reg, &val);
417
418 return (val & mask) != 0;
419}
420
421static void armada_37xx_gpio_set(struct gpio_chip *chip, unsigned int offset,
422 int value)
423{
424 struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
425 unsigned int reg = OUTPUT_VAL;
426 unsigned int mask, val;
427
428 armada_37xx_update_reg(&reg, offset);
429 mask = BIT(offset);
430 val = value ? mask : 0;
431
432 regmap_update_bits(info->regmap, reg, mask, val);
433}
434
Gregory CLEMENT87466cc2017-04-05 17:18:04 +0200435static int armada_37xx_pmx_gpio_set_direction(struct pinctrl_dev *pctldev,
436 struct pinctrl_gpio_range *range,
437 unsigned int offset, bool input)
438{
439 struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
Gregory CLEMENT57150922017-04-05 17:18:05 +0200440 struct gpio_chip *chip = range->gc;
Gregory CLEMENT87466cc2017-04-05 17:18:04 +0200441
442 dev_dbg(info->dev, "gpio_direction for pin %u as %s-%d to %s\n",
443 offset, range->name, offset, input ? "input" : "output");
444
445 if (input)
Gregory CLEMENT57150922017-04-05 17:18:05 +0200446 armada_37xx_gpio_direction_input(chip, offset);
Gregory CLEMENT87466cc2017-04-05 17:18:04 +0200447 else
Gregory CLEMENT57150922017-04-05 17:18:05 +0200448 armada_37xx_gpio_direction_output(chip, offset, 0);
Gregory CLEMENT87466cc2017-04-05 17:18:04 +0200449
450 return 0;
451}
452
453static int armada_37xx_gpio_request_enable(struct pinctrl_dev *pctldev,
454 struct pinctrl_gpio_range *range,
455 unsigned int offset)
456{
457 struct armada_37xx_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
458 struct armada_37xx_pin_group *group;
459 int grp = 0;
460
461 dev_dbg(info->dev, "requesting gpio %d\n", offset);
462
463 while ((group = armada_37xx_find_next_grp_by_pin(info, offset, &grp)))
464 armada_37xx_pmx_set_by_name(pctldev, "gpio", group);
465
466 return 0;
467}
468
469static const struct pinmux_ops armada_37xx_pmx_ops = {
470 .get_functions_count = armada_37xx_pmx_get_funcs_count,
471 .get_function_name = armada_37xx_pmx_get_func_name,
472 .get_function_groups = armada_37xx_pmx_get_groups,
473 .set_mux = armada_37xx_pmx_set,
474 .gpio_request_enable = armada_37xx_gpio_request_enable,
475 .gpio_set_direction = armada_37xx_pmx_gpio_set_direction,
476};
477
Gregory CLEMENT57150922017-04-05 17:18:05 +0200478static const struct gpio_chip armada_37xx_gpiolib_chip = {
479 .request = gpiochip_generic_request,
480 .free = gpiochip_generic_free,
481 .set = armada_37xx_gpio_set,
482 .get = armada_37xx_gpio_get,
483 .get_direction = armada_37xx_gpio_get_direction,
484 .direction_input = armada_37xx_gpio_direction_input,
485 .direction_output = armada_37xx_gpio_direction_output,
486 .owner = THIS_MODULE,
487};
488
Gregory CLEMENT2f227602017-04-28 16:01:33 +0200489static void armada_37xx_irq_ack(struct irq_data *d)
490{
491 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
492 struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
493 u32 reg = IRQ_STATUS;
494 unsigned long flags;
495
496 armada_37xx_irq_update_reg(&reg, d);
497 spin_lock_irqsave(&info->irq_lock, flags);
498 writel(d->mask, info->base + reg);
499 spin_unlock_irqrestore(&info->irq_lock, flags);
500}
501
502static void armada_37xx_irq_mask(struct irq_data *d)
503{
504 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
505 struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
506 u32 val, reg = IRQ_EN;
507 unsigned long flags;
508
509 armada_37xx_irq_update_reg(&reg, d);
510 spin_lock_irqsave(&info->irq_lock, flags);
511 val = readl(info->base + reg);
512 writel(val & ~d->mask, info->base + reg);
513 spin_unlock_irqrestore(&info->irq_lock, flags);
514}
515
516static void armada_37xx_irq_unmask(struct irq_data *d)
517{
518 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
519 struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
520 u32 val, reg = IRQ_EN;
521 unsigned long flags;
522
523 armada_37xx_irq_update_reg(&reg, d);
524 spin_lock_irqsave(&info->irq_lock, flags);
525 val = readl(info->base + reg);
526 writel(val | d->mask, info->base + reg);
527 spin_unlock_irqrestore(&info->irq_lock, flags);
528}
529
530static int armada_37xx_irq_set_wake(struct irq_data *d, unsigned int on)
531{
532 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
533 struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
534 u32 val, reg = IRQ_WKUP;
535 unsigned long flags;
536
537 armada_37xx_irq_update_reg(&reg, d);
538 spin_lock_irqsave(&info->irq_lock, flags);
539 val = readl(info->base + reg);
540 if (on)
541 val |= d->mask;
542 else
543 val &= ~d->mask;
544 writel(val, info->base + reg);
545 spin_unlock_irqrestore(&info->irq_lock, flags);
546
547 return 0;
548}
549
550static int armada_37xx_irq_set_type(struct irq_data *d, unsigned int type)
551{
552 struct gpio_chip *chip = irq_data_get_irq_chip_data(d);
553 struct armada_37xx_pinctrl *info = gpiochip_get_data(chip);
554 u32 val, reg = IRQ_POL;
555 unsigned long flags;
556
557 spin_lock_irqsave(&info->irq_lock, flags);
558 armada_37xx_irq_update_reg(&reg, d);
559 val = readl(info->base + reg);
560 switch (type) {
561 case IRQ_TYPE_EDGE_RISING:
562 val &= ~d->mask;
563 break;
564 case IRQ_TYPE_EDGE_FALLING:
565 val |= d->mask;
566 break;
567 default:
568 spin_unlock_irqrestore(&info->irq_lock, flags);
569 return -EINVAL;
570 }
571 writel(val, info->base + reg);
572 spin_unlock_irqrestore(&info->irq_lock, flags);
573
574 return 0;
575}
576
577
578static void armada_37xx_irq_handler(struct irq_desc *desc)
579{
580 struct gpio_chip *gc = irq_desc_get_handler_data(desc);
581 struct irq_chip *chip = irq_desc_get_chip(desc);
582 struct armada_37xx_pinctrl *info = gpiochip_get_data(gc);
583 struct irq_domain *d = gc->irqdomain;
584 int i;
585
586 chained_irq_enter(chip, desc);
587 for (i = 0; i <= d->revmap_size / GPIO_PER_REG; i++) {
588 u32 status;
589 unsigned long flags;
590
591 spin_lock_irqsave(&info->irq_lock, flags);
592 status = readl_relaxed(info->base + IRQ_STATUS + 4 * i);
593 /* Manage only the interrupt that was enabled */
594 status &= readl_relaxed(info->base + IRQ_EN + 4 * i);
595 spin_unlock_irqrestore(&info->irq_lock, flags);
596 while (status) {
597 u32 hwirq = ffs(status) - 1;
598 u32 virq = irq_find_mapping(d, hwirq +
599 i * GPIO_PER_REG);
600
601 generic_handle_irq(virq);
602
603 /* Update status in case a new IRQ appears */
604 spin_lock_irqsave(&info->irq_lock, flags);
605 status = readl_relaxed(info->base +
606 IRQ_STATUS + 4 * i);
607 /* Manage only the interrupt that was enabled */
608 status &= readl_relaxed(info->base + IRQ_EN + 4 * i);
609 spin_unlock_irqrestore(&info->irq_lock, flags);
610 }
611 }
612 chained_irq_exit(chip, desc);
613}
614
615static int armada_37xx_irqchip_register(struct platform_device *pdev,
616 struct armada_37xx_pinctrl *info)
617{
618 struct device_node *np = info->dev->of_node;
619 int nrirqs = info->data->nr_pins;
620 struct gpio_chip *gc = &info->gpio_chip;
621 struct irq_chip *irqchip = &info->irq_chip;
622 struct resource res;
623 int ret = -ENODEV, i, nr_irq_parent;
624
625 /* Check if we have at least one gpio-controller child node */
626 for_each_child_of_node(info->dev->of_node, np) {
627 if (of_property_read_bool(np, "gpio-controller")) {
628 ret = 0;
629 break;
630 }
631 };
632 if (ret)
633 return ret;
634
635 nr_irq_parent = of_irq_count(np);
636 spin_lock_init(&info->irq_lock);
637
638 if (!nr_irq_parent) {
639 dev_err(&pdev->dev, "Invalid or no IRQ\n");
640 return 0;
641 }
642
643 if (of_address_to_resource(info->dev->of_node, 1, &res)) {
644 dev_err(info->dev, "cannot find IO resource\n");
645 return -ENOENT;
646 }
647
648 info->base = devm_ioremap_resource(info->dev, &res);
649 if (IS_ERR(info->base))
650 return PTR_ERR(info->base);
651
652 irqchip->irq_ack = armada_37xx_irq_ack;
653 irqchip->irq_mask = armada_37xx_irq_mask;
654 irqchip->irq_unmask = armada_37xx_irq_unmask;
655 irqchip->irq_set_wake = armada_37xx_irq_set_wake;
656 irqchip->irq_set_type = armada_37xx_irq_set_type;
657 irqchip->name = info->data->name;
658
659 ret = gpiochip_irqchip_add(gc, irqchip, 0,
660 handle_edge_irq, IRQ_TYPE_NONE);
661 if (ret) {
662 dev_info(&pdev->dev, "could not add irqchip\n");
663 return ret;
664 }
665
666 /*
667 * Many interrupts are connected to the parent interrupt
668 * controller. But we do not take advantage of this and use
669 * the chained irq with all of them.
670 */
671 for (i = 0; i < nrirqs; i++) {
672 struct irq_data *d = irq_get_irq_data(gc->irq_base + i);
673
674 /*
675 * The mask field is a "precomputed bitmask for
676 * accessing the chip registers" which was introduced
677 * for the generic irqchip framework. As we don't use
678 * this framework, we can reuse this field for our own
679 * usage.
680 */
681 d->mask = BIT(i % GPIO_PER_REG);
682 }
683
684 for (i = 0; i < nr_irq_parent; i++) {
685 int irq = irq_of_parse_and_map(np, i);
686
687 if (irq < 0)
688 continue;
689
690 gpiochip_set_chained_irqchip(gc, irqchip, irq,
691 armada_37xx_irq_handler);
692 }
693
694 return 0;
695}
696
Gregory CLEMENT57150922017-04-05 17:18:05 +0200697static int armada_37xx_gpiochip_register(struct platform_device *pdev,
698 struct armada_37xx_pinctrl *info)
699{
700 struct device_node *np;
701 struct gpio_chip *gc;
702 int ret = -ENODEV;
703
704 for_each_child_of_node(info->dev->of_node, np) {
705 if (of_find_property(np, "gpio-controller", NULL)) {
706 ret = 0;
707 break;
708 }
709 };
710 if (ret)
711 return ret;
712
713 info->gpio_chip = armada_37xx_gpiolib_chip;
714
715 gc = &info->gpio_chip;
716 gc->ngpio = info->data->nr_pins;
717 gc->parent = &pdev->dev;
718 gc->base = -1;
719 gc->of_node = np;
720 gc->label = info->data->name;
721
722 ret = devm_gpiochip_add_data(&pdev->dev, gc, info);
723 if (ret)
724 return ret;
Gregory CLEMENT2f227602017-04-28 16:01:33 +0200725 ret = armada_37xx_irqchip_register(pdev, info);
726 if (ret)
727 return ret;
Gregory CLEMENT57150922017-04-05 17:18:05 +0200728
729 return 0;
730}
731
Gregory CLEMENT87466cc2017-04-05 17:18:04 +0200732/**
733 * armada_37xx_add_function() - Add a new function to the list
734 * @funcs: array of function to add the new one
735 * @funcsize: size of the remaining space for the function
736 * @name: name of the function to add
737 *
738 * If it is a new function then create it by adding its name else
739 * increment the number of group associated to this function.
740 */
741static int armada_37xx_add_function(struct armada_37xx_pmx_func *funcs,
742 int *funcsize, const char *name)
743{
744 int i = 0;
745
746 if (*funcsize <= 0)
747 return -EOVERFLOW;
748
749 while (funcs->ngroups) {
750 /* function already there */
751 if (strcmp(funcs->name, name) == 0) {
752 funcs->ngroups++;
753
754 return -EEXIST;
755 }
756 funcs++;
757 i++;
758 }
759
760 /* append new unique function */
761 funcs->name = name;
762 funcs->ngroups = 1;
763 (*funcsize)--;
764
765 return 0;
766}
767
768/**
769 * armada_37xx_fill_group() - complete the group array
770 * @info: info driver instance
771 *
772 * Based on the data available from the armada_37xx_pin_group array
773 * completes the last member of the struct for each function: the list
774 * of the groups associated to this function.
775 *
776 */
777static int armada_37xx_fill_group(struct armada_37xx_pinctrl *info)
778{
779 int n, num = 0, funcsize = info->data->nr_pins;
780
781 for (n = 0; n < info->ngroups; n++) {
782 struct armada_37xx_pin_group *grp = &info->groups[n];
783 int i, j, f;
784
785 grp->pins = devm_kzalloc(info->dev,
786 (grp->npins + grp->extra_npins) *
787 sizeof(*grp->pins), GFP_KERNEL);
788 if (!grp->pins)
789 return -ENOMEM;
790
791 for (i = 0; i < grp->npins; i++)
792 grp->pins[i] = grp->start_pin + i;
793
794 for (j = 0; j < grp->extra_npins; j++)
795 grp->pins[i+j] = grp->extra_pin + j;
796
797 for (f = 0; f < NB_FUNCS; f++) {
798 int ret;
799 /* check for unique functions and count groups */
800 ret = armada_37xx_add_function(info->funcs, &funcsize,
801 grp->funcs[f]);
802 if (ret == -EOVERFLOW)
803 dev_err(info->dev,
804 "More functions than pins(%d)\n",
805 info->data->nr_pins);
806 if (ret < 0)
807 continue;
808 num++;
809 }
810 }
811
812 info->nfuncs = num;
813
814 return 0;
815}
816
817/**
818 * armada_37xx_fill_funcs() - complete the funcs array
819 * @info: info driver instance
820 *
821 * Based on the data available from the armada_37xx_pin_group array
822 * completes the last two member of the struct for each group:
823 * - the list of the pins included in the group
824 * - the list of pinmux functions that can be selected for this group
825 *
826 */
827static int armada_37xx_fill_func(struct armada_37xx_pinctrl *info)
828{
829 struct armada_37xx_pmx_func *funcs = info->funcs;
830 int n;
831
832 for (n = 0; n < info->nfuncs; n++) {
833 const char *name = funcs[n].name;
834 const char **groups;
835 int g;
836
837 funcs[n].groups = devm_kzalloc(info->dev, funcs[n].ngroups *
838 sizeof(*(funcs[n].groups)),
839 GFP_KERNEL);
840 if (!funcs[n].groups)
841 return -ENOMEM;
842
843 groups = funcs[n].groups;
844
845 for (g = 0; g < info->ngroups; g++) {
846 struct armada_37xx_pin_group *gp = &info->groups[g];
847 int f;
848
849 for (f = 0; f < NB_FUNCS; f++) {
850 if (strcmp(gp->funcs[f], name) == 0) {
851 *groups = gp->name;
852 groups++;
853 }
854 }
855 }
856 }
857 return 0;
858}
859
860static int armada_37xx_pinctrl_register(struct platform_device *pdev,
861 struct armada_37xx_pinctrl *info)
862{
863 const struct armada_37xx_pin_data *pin_data = info->data;
864 struct pinctrl_desc *ctrldesc = &info->pctl;
865 struct pinctrl_pin_desc *pindesc, *pdesc;
866 int pin, ret;
867
868 info->groups = pin_data->groups;
869 info->ngroups = pin_data->ngroups;
870
871 ctrldesc->name = "armada_37xx-pinctrl";
872 ctrldesc->owner = THIS_MODULE;
873 ctrldesc->pctlops = &armada_37xx_pctrl_ops;
874 ctrldesc->pmxops = &armada_37xx_pmx_ops;
875 ctrldesc->confops = &armada_37xx_pinconf_ops;
876
877 pindesc = devm_kzalloc(&pdev->dev, sizeof(*pindesc) *
878 pin_data->nr_pins, GFP_KERNEL);
879 if (!pindesc)
880 return -ENOMEM;
881
882 ctrldesc->pins = pindesc;
883 ctrldesc->npins = pin_data->nr_pins;
884
885 pdesc = pindesc;
886 for (pin = 0; pin < pin_data->nr_pins; pin++) {
887 pdesc->number = pin;
888 pdesc->name = kasprintf(GFP_KERNEL, "%s-%d",
889 pin_data->name, pin);
890 pdesc++;
891 }
892
893 /*
894 * we allocate functions for number of pins and hope there are
895 * fewer unique functions than pins available
896 */
897 info->funcs = devm_kzalloc(&pdev->dev, pin_data->nr_pins *
898 sizeof(struct armada_37xx_pmx_func), GFP_KERNEL);
899 if (!info->funcs)
900 return -ENOMEM;
901
902
903 ret = armada_37xx_fill_group(info);
904 if (ret)
905 return ret;
906
907 ret = armada_37xx_fill_func(info);
908 if (ret)
909 return ret;
910
911 info->pctl_dev = devm_pinctrl_register(&pdev->dev, ctrldesc, info);
912 if (IS_ERR(info->pctl_dev)) {
913 dev_err(&pdev->dev, "could not register pinctrl driver\n");
914 return PTR_ERR(info->pctl_dev);
915 }
916
917 return 0;
918}
919
920static const struct of_device_id armada_37xx_pinctrl_of_match[] = {
921 {
922 .compatible = "marvell,armada3710-sb-pinctrl",
923 .data = (void *)&armada_37xx_pin_sb,
924 },
925 {
926 .compatible = "marvell,armada3710-nb-pinctrl",
927 .data = (void *)&armada_37xx_pin_nb,
928 },
929 { },
930};
931
932static int __init armada_37xx_pinctrl_probe(struct platform_device *pdev)
933{
934 struct armada_37xx_pinctrl *info;
935 struct device *dev = &pdev->dev;
936 struct device_node *np = dev->of_node;
937 struct regmap *regmap;
938 int ret;
939
940 info = devm_kzalloc(dev, sizeof(struct armada_37xx_pinctrl),
941 GFP_KERNEL);
942 if (!info)
943 return -ENOMEM;
944
945 info->dev = dev;
946
947 regmap = syscon_node_to_regmap(np);
948 if (IS_ERR(regmap)) {
949 dev_err(&pdev->dev, "cannot get regmap\n");
950 return PTR_ERR(regmap);
951 }
952 info->regmap = regmap;
953
954 info->data = of_device_get_match_data(dev);
955
956 ret = armada_37xx_pinctrl_register(pdev, info);
957 if (ret)
958 return ret;
959
Gregory CLEMENT57150922017-04-05 17:18:05 +0200960 ret = armada_37xx_gpiochip_register(pdev, info);
961 if (ret)
962 return ret;
963
Gregory CLEMENT87466cc2017-04-05 17:18:04 +0200964 platform_set_drvdata(pdev, info);
965
966 return 0;
967}
968
969static struct platform_driver armada_37xx_pinctrl_driver = {
970 .driver = {
971 .name = "armada-37xx-pinctrl",
972 .of_match_table = armada_37xx_pinctrl_of_match,
973 },
974};
975
976builtin_platform_driver_probe(armada_37xx_pinctrl_driver,
977 armada_37xx_pinctrl_probe);