]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/pinctrl/pinctrl-at91.c
Merge tag 'gcc-plugins-v5.2-rc1' of ssh://gitolite.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-jammy-kernel.git] / drivers / pinctrl / pinctrl-at91.c
CommitLineData
6732ae5c
JCPV
1/*
2 * at91 pinctrl driver based on at91 pinmux core
3 *
4 * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
5 *
6 * Under GPLv2 only
7 */
8
9#include <linux/clk.h>
10#include <linux/err.h>
11#include <linux/init.h>
6732ae5c
JCPV
12#include <linux/of.h>
13#include <linux/of_device.h>
14#include <linux/of_address.h>
15#include <linux/of_irq.h>
16#include <linux/slab.h>
17#include <linux/interrupt.h>
6732ae5c 18#include <linux/io.h>
1c5fb66a 19#include <linux/gpio/driver.h>
6732ae5c
JCPV
20#include <linux/pinctrl/machine.h>
21#include <linux/pinctrl/pinconf.h>
22#include <linux/pinctrl/pinctrl.h>
23#include <linux/pinctrl/pinmux.h>
24/* Since we request GPIOs from ourself */
25#include <linux/pinctrl/consumer.h>
26
c654b6bf 27#include "pinctrl-at91.h"
6732ae5c
JCPV
28#include "core.h"
29
94daf85e 30#define MAX_GPIO_BANKS 5
6732ae5c
JCPV
31#define MAX_NB_GPIO_PER_BANK 32
32
33struct at91_pinctrl_mux_ops;
34
35struct at91_gpio_chip {
36 struct gpio_chip chip;
37 struct pinctrl_gpio_range range;
38 struct at91_gpio_chip *next; /* Bank sharing same clock */
39 int pioc_hwirq; /* PIO bank interrupt identifier on AIC */
40 int pioc_virq; /* PIO bank Linux virtual interrupt */
41 int pioc_idx; /* PIO bank index */
42 void __iomem *regbase; /* PIO bank virtual address */
43 struct clk *clock; /* associated clock */
6732ae5c
JCPV
44 struct at91_pinctrl_mux_ops *ops; /* ops */
45};
46
6732ae5c
JCPV
47static struct at91_gpio_chip *gpio_chips[MAX_GPIO_BANKS];
48
49static int gpio_banks;
50
525fae21 51#define PULL_UP (1 << 0)
6732ae5c 52#define MULTI_DRIVE (1 << 1)
7ebd7a3a
JCPV
53#define DEGLITCH (1 << 2)
54#define PULL_DOWN (1 << 3)
55#define DIS_SCHMIT (1 << 4)
4334ac2d
MR
56#define DRIVE_STRENGTH_SHIFT 5
57#define DRIVE_STRENGTH_MASK 0x3
58#define DRIVE_STRENGTH (DRIVE_STRENGTH_MASK << DRIVE_STRENGTH_SHIFT)
96bb12de
BB
59#define OUTPUT (1 << 7)
60#define OUTPUT_VAL_SHIFT 8
61#define OUTPUT_VAL (0x1 << OUTPUT_VAL_SHIFT)
64e21add
CB
62#define SLEWRATE_SHIFT 9
63#define SLEWRATE_MASK 0x1
64#define SLEWRATE (SLEWRATE_MASK << SLEWRATE_SHIFT)
7ebd7a3a
JCPV
65#define DEBOUNCE (1 << 16)
66#define DEBOUNCE_VAL_SHIFT 17
67#define DEBOUNCE_VAL (0x3fff << DEBOUNCE_VAL_SHIFT)
6732ae5c 68
4334ac2d
MR
69/**
70 * These defines will translated the dt binding settings to our internal
71 * settings. They are not necessarily the same value as the register setting.
72 * The actual drive strength current of low, medium and high must be looked up
73 * from the corresponding device datasheet. This value is different for pins
74 * that are even in the same banks. It is also dependent on VCC.
75 * DRIVE_STRENGTH_DEFAULT is just a placeholder to avoid changing the drive
76 * strength when there is no dt config for it.
77 */
b67328e1
CB
78enum drive_strength_bit {
79 DRIVE_STRENGTH_BIT_DEF,
80 DRIVE_STRENGTH_BIT_LOW,
81 DRIVE_STRENGTH_BIT_MED,
82 DRIVE_STRENGTH_BIT_HI,
83};
84
85#define DRIVE_STRENGTH_BIT_MSK(name) (DRIVE_STRENGTH_BIT_##name << \
86 DRIVE_STRENGTH_SHIFT)
4334ac2d 87
64e21add
CB
88enum slewrate_bit {
89 SLEWRATE_BIT_DIS,
90 SLEWRATE_BIT_ENA,
91};
92
93#define SLEWRATE_BIT_MSK(name) (SLEWRATE_BIT_##name << SLEWRATE_SHIFT)
94
6732ae5c
JCPV
95/**
96 * struct at91_pmx_func - describes AT91 pinmux functions
97 * @name: the name of this specific function
98 * @groups: corresponding pin groups
99 * @ngroups: the number of groups
100 */
101struct at91_pmx_func {
102 const char *name;
103 const char **groups;
104 unsigned ngroups;
105};
106
107enum at91_mux {
108 AT91_MUX_GPIO = 0,
109 AT91_MUX_PERIPH_A = 1,
110 AT91_MUX_PERIPH_B = 2,
111 AT91_MUX_PERIPH_C = 3,
112 AT91_MUX_PERIPH_D = 4,
113};
114
115/**
116 * struct at91_pmx_pin - describes an At91 pin mux
117 * @bank: the bank of the pin
118 * @pin: the pin number in the @bank
119 * @mux: the mux mode : gpio or periph_x of the pin i.e. alternate function.
120 * @conf: the configuration of the pin: PULL_UP, MULTIDRIVE etc...
121 */
122struct at91_pmx_pin {
123 uint32_t bank;
124 uint32_t pin;
125 enum at91_mux mux;
126 unsigned long conf;
127};
128
129/**
130 * struct at91_pin_group - describes an At91 pin group
131 * @name: the name of this specific pin group
132 * @pins_conf: the mux mode for each pin in this group. The size of this
133 * array is the same as pins.
134 * @pins: an array of discrete physical pins used in this group, taken
135 * from the driver-local pin enumeration space
136 * @npins: the number of pins in this group array, i.e. the number of
137 * elements in .pins so we can iterate over that array
138 */
139struct at91_pin_group {
140 const char *name;
141 struct at91_pmx_pin *pins_conf;
142 unsigned int *pins;
143 unsigned npins;
144};
145
146/**
c2eb9e7f 147 * struct at91_pinctrl_mux_ops - describes an AT91 mux ops group
6732ae5c
JCPV
148 * on new IP with support for periph C and D the way to mux in
149 * periph A and B has changed
150 * So provide the right call back
151 * if not present means the IP does not support it
152 * @get_periph: return the periph mode configured
153 * @mux_A_periph: mux as periph A
154 * @mux_B_periph: mux as periph B
155 * @mux_C_periph: mux as periph C
156 * @mux_D_periph: mux as periph D
7ebd7a3a
JCPV
157 * @get_deglitch: get deglitch status
158 * @set_deglitch: enable/disable deglitch
159 * @get_debounce: get debounce status
160 * @set_debounce: enable/disable debounce
161 * @get_pulldown: get pulldown status
162 * @set_pulldown: enable/disable pulldown
163 * @get_schmitt_trig: get schmitt trigger status
164 * @disable_schmitt_trig: disable schmitt trigger
6732ae5c
JCPV
165 * @irq_type: return irq type
166 */
167struct at91_pinctrl_mux_ops {
168 enum at91_mux (*get_periph)(void __iomem *pio, unsigned mask);
169 void (*mux_A_periph)(void __iomem *pio, unsigned mask);
170 void (*mux_B_periph)(void __iomem *pio, unsigned mask);
171 void (*mux_C_periph)(void __iomem *pio, unsigned mask);
172 void (*mux_D_periph)(void __iomem *pio, unsigned mask);
7ebd7a3a 173 bool (*get_deglitch)(void __iomem *pio, unsigned pin);
77966ad7 174 void (*set_deglitch)(void __iomem *pio, unsigned mask, bool is_on);
7ebd7a3a 175 bool (*get_debounce)(void __iomem *pio, unsigned pin, u32 *div);
77966ad7 176 void (*set_debounce)(void __iomem *pio, unsigned mask, bool is_on, u32 div);
7ebd7a3a 177 bool (*get_pulldown)(void __iomem *pio, unsigned pin);
77966ad7 178 void (*set_pulldown)(void __iomem *pio, unsigned mask, bool is_on);
7ebd7a3a
JCPV
179 bool (*get_schmitt_trig)(void __iomem *pio, unsigned pin);
180 void (*disable_schmitt_trig)(void __iomem *pio, unsigned mask);
4334ac2d
MR
181 unsigned (*get_drivestrength)(void __iomem *pio, unsigned pin);
182 void (*set_drivestrength)(void __iomem *pio, unsigned pin,
183 u32 strength);
64e21add
CB
184 unsigned (*get_slewrate)(void __iomem *pio, unsigned pin);
185 void (*set_slewrate)(void __iomem *pio, unsigned pin, u32 slewrate);
6732ae5c
JCPV
186 /* irq */
187 int (*irq_type)(struct irq_data *d, unsigned type);
188};
189
190static int gpio_irq_type(struct irq_data *d, unsigned type);
191static int alt_gpio_irq_type(struct irq_data *d, unsigned type);
192
193struct at91_pinctrl {
194 struct device *dev;
195 struct pinctrl_dev *pctl;
196
a0b957f3 197 int nactive_banks;
6732ae5c
JCPV
198
199 uint32_t *mux_mask;
200 int nmux;
201
202 struct at91_pmx_func *functions;
203 int nfunctions;
204
205 struct at91_pin_group *groups;
206 int ngroups;
207
208 struct at91_pinctrl_mux_ops *ops;
209};
210
56411f3c 211static inline const struct at91_pin_group *at91_pinctrl_find_group_by_name(
6732ae5c
JCPV
212 const struct at91_pinctrl *info,
213 const char *name)
214{
215 const struct at91_pin_group *grp = NULL;
216 int i;
217
218 for (i = 0; i < info->ngroups; i++) {
219 if (strcmp(info->groups[i].name, name))
220 continue;
221
222 grp = &info->groups[i];
223 dev_dbg(info->dev, "%s: %d 0:%d\n", name, grp->npins, grp->pins[0]);
224 break;
225 }
226
227 return grp;
228}
229
230static int at91_get_groups_count(struct pinctrl_dev *pctldev)
231{
232 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
233
234 return info->ngroups;
235}
236
237static const char *at91_get_group_name(struct pinctrl_dev *pctldev,
238 unsigned selector)
239{
240 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
241
242 return info->groups[selector].name;
243}
244
245static int at91_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
246 const unsigned **pins,
247 unsigned *npins)
248{
249 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
250
251 if (selector >= info->ngroups)
252 return -EINVAL;
253
254 *pins = info->groups[selector].pins;
255 *npins = info->groups[selector].npins;
256
257 return 0;
258}
259
260static void at91_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
261 unsigned offset)
262{
263 seq_printf(s, "%s", dev_name(pctldev->dev));
264}
265
266static int at91_dt_node_to_map(struct pinctrl_dev *pctldev,
267 struct device_node *np,
268 struct pinctrl_map **map, unsigned *num_maps)
269{
270 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
271 const struct at91_pin_group *grp;
272 struct pinctrl_map *new_map;
273 struct device_node *parent;
274 int map_num = 1;
275 int i;
6732ae5c
JCPV
276
277 /*
61e310a1 278 * first find the group of this node and check if we need to create
6732ae5c
JCPV
279 * config maps for pins
280 */
281 grp = at91_pinctrl_find_group_by_name(info, np->name);
282 if (!grp) {
94f4e54c
RH
283 dev_err(info->dev, "unable to find group for node %pOFn\n",
284 np);
6732ae5c
JCPV
285 return -EINVAL;
286 }
287
288 map_num += grp->npins;
a86854d0
KC
289 new_map = devm_kcalloc(pctldev->dev, map_num, sizeof(*new_map),
290 GFP_KERNEL);
6732ae5c
JCPV
291 if (!new_map)
292 return -ENOMEM;
293
294 *map = new_map;
295 *num_maps = map_num;
296
297 /* create mux map */
298 parent = of_get_parent(np);
299 if (!parent) {
c62b2b34 300 devm_kfree(pctldev->dev, new_map);
6732ae5c
JCPV
301 return -EINVAL;
302 }
303 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
304 new_map[0].data.mux.function = parent->name;
305 new_map[0].data.mux.group = np->name;
306 of_node_put(parent);
307
308 /* create config map */
309 new_map++;
310 for (i = 0; i < grp->npins; i++) {
6732ae5c
JCPV
311 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
312 new_map[i].data.configs.group_or_pin =
313 pin_get_name(pctldev, grp->pins[i]);
314 new_map[i].data.configs.configs = &grp->pins_conf[i].conf;
315 new_map[i].data.configs.num_configs = 1;
316 }
317
318 dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
319 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
320
321 return 0;
322}
323
324static void at91_dt_free_map(struct pinctrl_dev *pctldev,
325 struct pinctrl_map *map, unsigned num_maps)
326{
327}
328
022ab148 329static const struct pinctrl_ops at91_pctrl_ops = {
6732ae5c
JCPV
330 .get_groups_count = at91_get_groups_count,
331 .get_group_name = at91_get_group_name,
332 .get_group_pins = at91_get_group_pins,
333 .pin_dbg_show = at91_pin_dbg_show,
334 .dt_node_to_map = at91_dt_node_to_map,
335 .dt_free_map = at91_dt_free_map,
336};
337
3c93600d 338static void __iomem *pin_to_controller(struct at91_pinctrl *info,
6732ae5c
JCPV
339 unsigned int bank)
340{
1ab36387
DD
341 if (!gpio_chips[bank])
342 return NULL;
343
6732ae5c
JCPV
344 return gpio_chips[bank]->regbase;
345}
346
347static inline int pin_to_bank(unsigned pin)
348{
349 return pin /= MAX_NB_GPIO_PER_BANK;
350}
351
352static unsigned pin_to_mask(unsigned int pin)
353{
354 return 1 << pin;
355}
356
4334ac2d
MR
357static unsigned two_bit_pin_value_shift_amount(unsigned int pin)
358{
359 /* return the shift value for a pin for "two bit" per pin registers,
360 * i.e. drive strength */
361 return 2*((pin >= MAX_NB_GPIO_PER_BANK/2)
362 ? pin - MAX_NB_GPIO_PER_BANK/2 : pin);
363}
364
365static unsigned sama5d3_get_drive_register(unsigned int pin)
366{
367 /* drive strength is split between two registers
368 * with two bits per pin */
369 return (pin >= MAX_NB_GPIO_PER_BANK/2)
370 ? SAMA5D3_PIO_DRIVER2 : SAMA5D3_PIO_DRIVER1;
371}
372
373static unsigned at91sam9x5_get_drive_register(unsigned int pin)
374{
375 /* drive strength is split between two registers
376 * with two bits per pin */
377 return (pin >= MAX_NB_GPIO_PER_BANK/2)
378 ? AT91SAM9X5_PIO_DRIVER2 : AT91SAM9X5_PIO_DRIVER1;
379}
380
6732ae5c
JCPV
381static void at91_mux_disable_interrupt(void __iomem *pio, unsigned mask)
382{
383 writel_relaxed(mask, pio + PIO_IDR);
384}
385
386static unsigned at91_mux_get_pullup(void __iomem *pio, unsigned pin)
387{
05d3534a 388 return !((readl_relaxed(pio + PIO_PUSR) >> pin) & 0x1);
6732ae5c
JCPV
389}
390
391static void at91_mux_set_pullup(void __iomem *pio, unsigned mask, bool on)
392{
3d784273
WY
393 if (on)
394 writel_relaxed(mask, pio + PIO_PPDDR);
395
6732ae5c
JCPV
396 writel_relaxed(mask, pio + (on ? PIO_PUER : PIO_PUDR));
397}
398
96bb12de
BB
399static bool at91_mux_get_output(void __iomem *pio, unsigned int pin, bool *val)
400{
401 *val = (readl_relaxed(pio + PIO_ODSR) >> pin) & 0x1;
402 return (readl_relaxed(pio + PIO_OSR) >> pin) & 0x1;
403}
404
405static void at91_mux_set_output(void __iomem *pio, unsigned int mask,
406 bool is_on, bool val)
407{
408 writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
409 writel_relaxed(mask, pio + (is_on ? PIO_OER : PIO_ODR));
410}
411
6732ae5c
JCPV
412static unsigned at91_mux_get_multidrive(void __iomem *pio, unsigned pin)
413{
414 return (readl_relaxed(pio + PIO_MDSR) >> pin) & 0x1;
415}
416
417static void at91_mux_set_multidrive(void __iomem *pio, unsigned mask, bool on)
418{
419 writel_relaxed(mask, pio + (on ? PIO_MDER : PIO_MDDR));
420}
421
422static void at91_mux_set_A_periph(void __iomem *pio, unsigned mask)
423{
424 writel_relaxed(mask, pio + PIO_ASR);
425}
426
427static void at91_mux_set_B_periph(void __iomem *pio, unsigned mask)
428{
429 writel_relaxed(mask, pio + PIO_BSR);
430}
431
432static void at91_mux_pio3_set_A_periph(void __iomem *pio, unsigned mask)
433{
434
435 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask,
436 pio + PIO_ABCDSR1);
437 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
438 pio + PIO_ABCDSR2);
439}
440
441static void at91_mux_pio3_set_B_periph(void __iomem *pio, unsigned mask)
442{
443 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask,
444 pio + PIO_ABCDSR1);
445 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
446 pio + PIO_ABCDSR2);
447}
448
449static void at91_mux_pio3_set_C_periph(void __iomem *pio, unsigned mask)
450{
451 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask, pio + PIO_ABCDSR1);
452 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
453}
454
455static void at91_mux_pio3_set_D_periph(void __iomem *pio, unsigned mask)
456{
457 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask, pio + PIO_ABCDSR1);
458 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
459}
460
461static enum at91_mux at91_mux_pio3_get_periph(void __iomem *pio, unsigned mask)
462{
463 unsigned select;
464
465 if (readl_relaxed(pio + PIO_PSR) & mask)
466 return AT91_MUX_GPIO;
467
468 select = !!(readl_relaxed(pio + PIO_ABCDSR1) & mask);
469 select |= (!!(readl_relaxed(pio + PIO_ABCDSR2) & mask) << 1);
470
471 return select + 1;
472}
473
474static enum at91_mux at91_mux_get_periph(void __iomem *pio, unsigned mask)
475{
476 unsigned select;
477
478 if (readl_relaxed(pio + PIO_PSR) & mask)
479 return AT91_MUX_GPIO;
480
481 select = readl_relaxed(pio + PIO_ABSR) & mask;
482
483 return select + 1;
484}
485
7ebd7a3a
JCPV
486static bool at91_mux_get_deglitch(void __iomem *pio, unsigned pin)
487{
d480239b 488 return (readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1;
7ebd7a3a
JCPV
489}
490
491static void at91_mux_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
492{
d480239b 493 writel_relaxed(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
7ebd7a3a
JCPV
494}
495
c8dba02e
BB
496static bool at91_mux_pio3_get_deglitch(void __iomem *pio, unsigned pin)
497{
d480239b
BD
498 if ((readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1)
499 return !((readl_relaxed(pio + PIO_IFSCSR) >> pin) & 0x1);
c8dba02e
BB
500
501 return false;
502}
503
7ebd7a3a
JCPV
504static void at91_mux_pio3_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
505{
506 if (is_on)
d480239b 507 writel_relaxed(mask, pio + PIO_IFSCDR);
7ebd7a3a
JCPV
508 at91_mux_set_deglitch(pio, mask, is_on);
509}
510
511static bool at91_mux_pio3_get_debounce(void __iomem *pio, unsigned pin, u32 *div)
512{
d480239b 513 *div = readl_relaxed(pio + PIO_SCDR);
7ebd7a3a 514
d480239b
BD
515 return ((readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1) &&
516 ((readl_relaxed(pio + PIO_IFSCSR) >> pin) & 0x1);
7ebd7a3a
JCPV
517}
518
519static void at91_mux_pio3_set_debounce(void __iomem *pio, unsigned mask,
520 bool is_on, u32 div)
521{
522 if (is_on) {
d480239b
BD
523 writel_relaxed(mask, pio + PIO_IFSCER);
524 writel_relaxed(div & PIO_SCDR_DIV, pio + PIO_SCDR);
525 writel_relaxed(mask, pio + PIO_IFER);
c8dba02e 526 } else
d480239b 527 writel_relaxed(mask, pio + PIO_IFSCDR);
7ebd7a3a
JCPV
528}
529
530static bool at91_mux_pio3_get_pulldown(void __iomem *pio, unsigned pin)
531{
d480239b 532 return !((readl_relaxed(pio + PIO_PPDSR) >> pin) & 0x1);
7ebd7a3a
JCPV
533}
534
535static void at91_mux_pio3_set_pulldown(void __iomem *pio, unsigned mask, bool is_on)
536{
3d784273 537 if (is_on)
d480239b 538 writel_relaxed(mask, pio + PIO_PUDR);
3d784273 539
d480239b 540 writel_relaxed(mask, pio + (is_on ? PIO_PPDER : PIO_PPDDR));
7ebd7a3a
JCPV
541}
542
543static void at91_mux_pio3_disable_schmitt_trig(void __iomem *pio, unsigned mask)
544{
d480239b 545 writel_relaxed(readl_relaxed(pio + PIO_SCHMITT) | mask, pio + PIO_SCHMITT);
7ebd7a3a
JCPV
546}
547
548static bool at91_mux_pio3_get_schmitt_trig(void __iomem *pio, unsigned pin)
549{
d480239b 550 return (readl_relaxed(pio + PIO_SCHMITT) >> pin) & 0x1;
7ebd7a3a
JCPV
551}
552
4334ac2d
MR
553static inline u32 read_drive_strength(void __iomem *reg, unsigned pin)
554{
d480239b 555 unsigned tmp = readl_relaxed(reg);
4334ac2d
MR
556
557 tmp = tmp >> two_bit_pin_value_shift_amount(pin);
558
559 return tmp & DRIVE_STRENGTH_MASK;
560}
561
562static unsigned at91_mux_sama5d3_get_drivestrength(void __iomem *pio,
563 unsigned pin)
564{
565 unsigned tmp = read_drive_strength(pio +
566 sama5d3_get_drive_register(pin), pin);
567
568 /* SAMA5 strength is 1:1 with our defines,
569 * except 0 is equivalent to low per datasheet */
570 if (!tmp)
b67328e1 571 tmp = DRIVE_STRENGTH_BIT_MSK(LOW);
4334ac2d
MR
572
573 return tmp;
574}
575
576static unsigned at91_mux_sam9x5_get_drivestrength(void __iomem *pio,
577 unsigned pin)
578{
579 unsigned tmp = read_drive_strength(pio +
580 at91sam9x5_get_drive_register(pin), pin);
581
582 /* strength is inverse in SAM9x5s hardware with the pinctrl defines
583 * hardware: 0 = hi, 1 = med, 2 = low, 3 = rsvd */
b67328e1 584 tmp = DRIVE_STRENGTH_BIT_MSK(HI) - tmp;
4334ac2d
MR
585
586 return tmp;
587}
588
42ef7557
CB
589static unsigned at91_mux_sam9x60_get_drivestrength(void __iomem *pio,
590 unsigned pin)
591{
592 unsigned tmp = readl_relaxed(pio + SAM9X60_PIO_DRIVER1);
593
594 if (tmp & BIT(pin))
595 return DRIVE_STRENGTH_BIT_HI;
596
597 return DRIVE_STRENGTH_BIT_LOW;
598}
599
64e21add
CB
600static unsigned at91_mux_sam9x60_get_slewrate(void __iomem *pio, unsigned pin)
601{
602 unsigned tmp = readl_relaxed(pio + SAM9X60_PIO_SLEWR);
603
604 if ((tmp & BIT(pin)))
605 return SLEWRATE_BIT_ENA;
606
607 return SLEWRATE_BIT_DIS;
608}
609
4334ac2d
MR
610static void set_drive_strength(void __iomem *reg, unsigned pin, u32 strength)
611{
d480239b 612 unsigned tmp = readl_relaxed(reg);
4334ac2d
MR
613 unsigned shift = two_bit_pin_value_shift_amount(pin);
614
615 tmp &= ~(DRIVE_STRENGTH_MASK << shift);
616 tmp |= strength << shift;
617
d480239b 618 writel_relaxed(tmp, reg);
4334ac2d
MR
619}
620
621static void at91_mux_sama5d3_set_drivestrength(void __iomem *pio, unsigned pin,
622 u32 setting)
623{
624 /* do nothing if setting is zero */
625 if (!setting)
626 return;
627
628 /* strength is 1 to 1 with setting for SAMA5 */
629 set_drive_strength(pio + sama5d3_get_drive_register(pin), pin, setting);
630}
631
632static void at91_mux_sam9x5_set_drivestrength(void __iomem *pio, unsigned pin,
633 u32 setting)
634{
635 /* do nothing if setting is zero */
636 if (!setting)
637 return;
638
639 /* strength is inverse on SAM9x5s with our defines
640 * 0 = hi, 1 = med, 2 = low, 3 = rsvd */
b67328e1 641 setting = DRIVE_STRENGTH_BIT_MSK(HI) - setting;
4334ac2d
MR
642
643 set_drive_strength(pio + at91sam9x5_get_drive_register(pin), pin,
644 setting);
645}
646
42ef7557
CB
647static void at91_mux_sam9x60_set_drivestrength(void __iomem *pio, unsigned pin,
648 u32 setting)
649{
650 unsigned int tmp;
651
652 if (setting <= DRIVE_STRENGTH_BIT_DEF ||
653 setting == DRIVE_STRENGTH_BIT_MED ||
654 setting > DRIVE_STRENGTH_BIT_HI)
655 return;
656
657 tmp = readl_relaxed(pio + SAM9X60_PIO_DRIVER1);
658
659 /* Strength is 0: low, 1: hi */
660 if (setting == DRIVE_STRENGTH_BIT_LOW)
661 tmp &= ~BIT(pin);
662 else
663 tmp |= BIT(pin);
664
665 writel_relaxed(tmp, pio + SAM9X60_PIO_DRIVER1);
666}
667
64e21add
CB
668static void at91_mux_sam9x60_set_slewrate(void __iomem *pio, unsigned pin,
669 u32 setting)
670{
671 unsigned int tmp;
672
673 if (setting < SLEWRATE_BIT_DIS || setting > SLEWRATE_BIT_ENA)
674 return;
675
676 tmp = readl_relaxed(pio + SAM9X60_PIO_SLEWR);
677
678 if (setting == SLEWRATE_BIT_DIS)
679 tmp &= ~BIT(pin);
680 else
681 tmp |= BIT(pin);
682
683 writel_relaxed(tmp, pio + SAM9X60_PIO_SLEWR);
684}
685
6732ae5c
JCPV
686static struct at91_pinctrl_mux_ops at91rm9200_ops = {
687 .get_periph = at91_mux_get_periph,
688 .mux_A_periph = at91_mux_set_A_periph,
689 .mux_B_periph = at91_mux_set_B_periph,
7ebd7a3a
JCPV
690 .get_deglitch = at91_mux_get_deglitch,
691 .set_deglitch = at91_mux_set_deglitch,
6732ae5c
JCPV
692 .irq_type = gpio_irq_type,
693};
694
695static struct at91_pinctrl_mux_ops at91sam9x5_ops = {
696 .get_periph = at91_mux_pio3_get_periph,
697 .mux_A_periph = at91_mux_pio3_set_A_periph,
698 .mux_B_periph = at91_mux_pio3_set_B_periph,
699 .mux_C_periph = at91_mux_pio3_set_C_periph,
700 .mux_D_periph = at91_mux_pio3_set_D_periph,
c8dba02e 701 .get_deglitch = at91_mux_pio3_get_deglitch,
7ebd7a3a
JCPV
702 .set_deglitch = at91_mux_pio3_set_deglitch,
703 .get_debounce = at91_mux_pio3_get_debounce,
704 .set_debounce = at91_mux_pio3_set_debounce,
705 .get_pulldown = at91_mux_pio3_get_pulldown,
706 .set_pulldown = at91_mux_pio3_set_pulldown,
707 .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
708 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
4334ac2d
MR
709 .get_drivestrength = at91_mux_sam9x5_get_drivestrength,
710 .set_drivestrength = at91_mux_sam9x5_set_drivestrength,
711 .irq_type = alt_gpio_irq_type,
712};
713
42ef7557
CB
714static const struct at91_pinctrl_mux_ops sam9x60_ops = {
715 .get_periph = at91_mux_pio3_get_periph,
716 .mux_A_periph = at91_mux_pio3_set_A_periph,
717 .mux_B_periph = at91_mux_pio3_set_B_periph,
718 .mux_C_periph = at91_mux_pio3_set_C_periph,
719 .mux_D_periph = at91_mux_pio3_set_D_periph,
720 .get_deglitch = at91_mux_pio3_get_deglitch,
721 .set_deglitch = at91_mux_pio3_set_deglitch,
722 .get_debounce = at91_mux_pio3_get_debounce,
723 .set_debounce = at91_mux_pio3_set_debounce,
724 .get_pulldown = at91_mux_pio3_get_pulldown,
725 .set_pulldown = at91_mux_pio3_set_pulldown,
726 .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
727 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
728 .get_drivestrength = at91_mux_sam9x60_get_drivestrength,
729 .set_drivestrength = at91_mux_sam9x60_set_drivestrength,
64e21add
CB
730 .get_slewrate = at91_mux_sam9x60_get_slewrate,
731 .set_slewrate = at91_mux_sam9x60_set_slewrate,
42ef7557
CB
732 .irq_type = alt_gpio_irq_type,
733
734};
735
4334ac2d
MR
736static struct at91_pinctrl_mux_ops sama5d3_ops = {
737 .get_periph = at91_mux_pio3_get_periph,
738 .mux_A_periph = at91_mux_pio3_set_A_periph,
739 .mux_B_periph = at91_mux_pio3_set_B_periph,
740 .mux_C_periph = at91_mux_pio3_set_C_periph,
741 .mux_D_periph = at91_mux_pio3_set_D_periph,
742 .get_deglitch = at91_mux_pio3_get_deglitch,
743 .set_deglitch = at91_mux_pio3_set_deglitch,
744 .get_debounce = at91_mux_pio3_get_debounce,
745 .set_debounce = at91_mux_pio3_set_debounce,
746 .get_pulldown = at91_mux_pio3_get_pulldown,
747 .set_pulldown = at91_mux_pio3_set_pulldown,
748 .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
749 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
750 .get_drivestrength = at91_mux_sama5d3_get_drivestrength,
751 .set_drivestrength = at91_mux_sama5d3_set_drivestrength,
6732ae5c
JCPV
752 .irq_type = alt_gpio_irq_type,
753};
754
755static void at91_pin_dbg(const struct device *dev, const struct at91_pmx_pin *pin)
756{
757 if (pin->mux) {
4b6fe45a 758 dev_dbg(dev, "pio%c%d configured as periph%c with conf = 0x%lx\n",
6732ae5c
JCPV
759 pin->bank + 'A', pin->pin, pin->mux - 1 + 'A', pin->conf);
760 } else {
4b6fe45a 761 dev_dbg(dev, "pio%c%d configured as gpio with conf = 0x%lx\n",
6732ae5c
JCPV
762 pin->bank + 'A', pin->pin, pin->conf);
763 }
764}
765
3c93600d 766static int pin_check_config(struct at91_pinctrl *info, const char *name,
6732ae5c
JCPV
767 int index, const struct at91_pmx_pin *pin)
768{
769 int mux;
770
771 /* check if it's a valid config */
a0b957f3 772 if (pin->bank >= gpio_banks) {
6732ae5c 773 dev_err(info->dev, "%s: pin conf %d bank_id %d >= nbanks %d\n",
a0b957f3 774 name, index, pin->bank, gpio_banks);
6732ae5c
JCPV
775 return -EINVAL;
776 }
777
a0b957f3
JCPV
778 if (!gpio_chips[pin->bank]) {
779 dev_err(info->dev, "%s: pin conf %d bank_id %d not enabled\n",
780 name, index, pin->bank);
781 return -ENXIO;
782 }
783
6732ae5c
JCPV
784 if (pin->pin >= MAX_NB_GPIO_PER_BANK) {
785 dev_err(info->dev, "%s: pin conf %d pin_bank_id %d >= %d\n",
786 name, index, pin->pin, MAX_NB_GPIO_PER_BANK);
787 return -EINVAL;
788 }
789
790 if (!pin->mux)
791 return 0;
792
793 mux = pin->mux - 1;
794
795 if (mux >= info->nmux) {
796 dev_err(info->dev, "%s: pin conf %d mux_id %d >= nmux %d\n",
797 name, index, mux, info->nmux);
798 return -EINVAL;
799 }
800
801 if (!(info->mux_mask[pin->bank * info->nmux + mux] & 1 << pin->pin)) {
802 dev_err(info->dev, "%s: pin conf %d mux_id %d not supported for pio%c%d\n",
803 name, index, mux, pin->bank + 'A', pin->pin);
804 return -EINVAL;
805 }
806
807 return 0;
808}
809
810static void at91_mux_gpio_disable(void __iomem *pio, unsigned mask)
811{
812 writel_relaxed(mask, pio + PIO_PDR);
813}
814
815static void at91_mux_gpio_enable(void __iomem *pio, unsigned mask, bool input)
816{
817 writel_relaxed(mask, pio + PIO_PER);
818 writel_relaxed(mask, pio + (input ? PIO_ODR : PIO_OER));
819}
820
03e9f0ca
LW
821static int at91_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
822 unsigned group)
6732ae5c
JCPV
823{
824 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
825 const struct at91_pmx_pin *pins_conf = info->groups[group].pins_conf;
826 const struct at91_pmx_pin *pin;
827 uint32_t npins = info->groups[group].npins;
828 int i, ret;
829 unsigned mask;
830 void __iomem *pio;
831
832 dev_dbg(info->dev, "enable function %s group %s\n",
833 info->functions[selector].name, info->groups[group].name);
834
835 /* first check that all the pins of the group are valid with a valid
61e310a1 836 * parameter */
6732ae5c
JCPV
837 for (i = 0; i < npins; i++) {
838 pin = &pins_conf[i];
839 ret = pin_check_config(info, info->groups[group].name, i, pin);
840 if (ret)
841 return ret;
842 }
843
844 for (i = 0; i < npins; i++) {
845 pin = &pins_conf[i];
846 at91_pin_dbg(info->dev, pin);
847 pio = pin_to_controller(info, pin->bank);
1ab36387
DD
848
849 if (!pio)
850 continue;
851
6732ae5c
JCPV
852 mask = pin_to_mask(pin->pin);
853 at91_mux_disable_interrupt(pio, mask);
3c93600d 854 switch (pin->mux) {
6732ae5c
JCPV
855 case AT91_MUX_GPIO:
856 at91_mux_gpio_enable(pio, mask, 1);
857 break;
858 case AT91_MUX_PERIPH_A:
859 info->ops->mux_A_periph(pio, mask);
860 break;
861 case AT91_MUX_PERIPH_B:
862 info->ops->mux_B_periph(pio, mask);
863 break;
864 case AT91_MUX_PERIPH_C:
865 if (!info->ops->mux_C_periph)
866 return -EINVAL;
867 info->ops->mux_C_periph(pio, mask);
868 break;
869 case AT91_MUX_PERIPH_D:
870 if (!info->ops->mux_D_periph)
871 return -EINVAL;
872 info->ops->mux_D_periph(pio, mask);
873 break;
874 }
875 if (pin->mux)
876 at91_mux_gpio_disable(pio, mask);
877 }
878
879 return 0;
880}
881
6732ae5c
JCPV
882static int at91_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
883{
884 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
885
886 return info->nfunctions;
887}
888
889static const char *at91_pmx_get_func_name(struct pinctrl_dev *pctldev,
890 unsigned selector)
891{
892 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
893
894 return info->functions[selector].name;
895}
896
897static int at91_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
898 const char * const **groups,
899 unsigned * const num_groups)
900{
901 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
902
903 *groups = info->functions[selector].groups;
904 *num_groups = info->functions[selector].ngroups;
905
906 return 0;
907}
908
f6f94f66
AL
909static int at91_gpio_request_enable(struct pinctrl_dev *pctldev,
910 struct pinctrl_gpio_range *range,
911 unsigned offset)
6732ae5c
JCPV
912{
913 struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
914 struct at91_gpio_chip *at91_chip;
915 struct gpio_chip *chip;
916 unsigned mask;
917
918 if (!range) {
919 dev_err(npct->dev, "invalid range\n");
920 return -EINVAL;
921 }
922 if (!range->gc) {
923 dev_err(npct->dev, "missing GPIO chip in range\n");
924 return -EINVAL;
925 }
926 chip = range->gc;
370ea611 927 at91_chip = gpiochip_get_data(chip);
6732ae5c
JCPV
928
929 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
930
931 mask = 1 << (offset - chip->base);
932
933 dev_dbg(npct->dev, "enable pin %u as PIO%c%d 0x%x\n",
934 offset, 'A' + range->id, offset - chip->base, mask);
935
936 writel_relaxed(mask, at91_chip->regbase + PIO_PER);
937
938 return 0;
939}
940
f6f94f66
AL
941static void at91_gpio_disable_free(struct pinctrl_dev *pctldev,
942 struct pinctrl_gpio_range *range,
943 unsigned offset)
6732ae5c
JCPV
944{
945 struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
946
947 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
948 /* Set the pin to some default state, GPIO is usually default */
949}
950
022ab148 951static const struct pinmux_ops at91_pmx_ops = {
6732ae5c
JCPV
952 .get_functions_count = at91_pmx_get_funcs_count,
953 .get_function_name = at91_pmx_get_func_name,
954 .get_function_groups = at91_pmx_get_groups,
03e9f0ca 955 .set_mux = at91_pmx_set,
6732ae5c
JCPV
956 .gpio_request_enable = at91_gpio_request_enable,
957 .gpio_disable_free = at91_gpio_disable_free,
958};
959
960static int at91_pinconf_get(struct pinctrl_dev *pctldev,
961 unsigned pin_id, unsigned long *config)
962{
963 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
964 void __iomem *pio;
965 unsigned pin;
7ebd7a3a 966 int div;
96bb12de 967 bool out;
6732ae5c 968
1292e693
AB
969 *config = 0;
970 dev_dbg(info->dev, "%s:%d, pin_id=%d", __func__, __LINE__, pin_id);
6732ae5c 971 pio = pin_to_controller(info, pin_to_bank(pin_id));
1ab36387
DD
972
973 if (!pio)
974 return -EINVAL;
975
6732ae5c
JCPV
976 pin = pin_id % MAX_NB_GPIO_PER_BANK;
977
978 if (at91_mux_get_multidrive(pio, pin))
979 *config |= MULTI_DRIVE;
980
981 if (at91_mux_get_pullup(pio, pin))
982 *config |= PULL_UP;
983
7ebd7a3a
JCPV
984 if (info->ops->get_deglitch && info->ops->get_deglitch(pio, pin))
985 *config |= DEGLITCH;
986 if (info->ops->get_debounce && info->ops->get_debounce(pio, pin, &div))
987 *config |= DEBOUNCE | (div << DEBOUNCE_VAL_SHIFT);
988 if (info->ops->get_pulldown && info->ops->get_pulldown(pio, pin))
989 *config |= PULL_DOWN;
990 if (info->ops->get_schmitt_trig && info->ops->get_schmitt_trig(pio, pin))
991 *config |= DIS_SCHMIT;
4334ac2d
MR
992 if (info->ops->get_drivestrength)
993 *config |= (info->ops->get_drivestrength(pio, pin)
994 << DRIVE_STRENGTH_SHIFT);
64e21add
CB
995 if (info->ops->get_slewrate)
996 *config |= (info->ops->get_slewrate(pio, pin) << SLEWRATE_SHIFT);
96bb12de
BB
997 if (at91_mux_get_output(pio, pin, &out))
998 *config |= OUTPUT | (out << OUTPUT_VAL_SHIFT);
7ebd7a3a 999
6732ae5c
JCPV
1000 return 0;
1001}
1002
1003static int at91_pinconf_set(struct pinctrl_dev *pctldev,
03b054e9
SY
1004 unsigned pin_id, unsigned long *configs,
1005 unsigned num_configs)
6732ae5c
JCPV
1006{
1007 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1008 unsigned mask;
1009 void __iomem *pio;
03b054e9
SY
1010 int i;
1011 unsigned long config;
4334ac2d 1012 unsigned pin;
03b054e9
SY
1013
1014 for (i = 0; i < num_configs; i++) {
1015 config = configs[i];
1016
1017 dev_dbg(info->dev,
1018 "%s:%d, pin_id=%d, config=0x%lx",
1019 __func__, __LINE__, pin_id, config);
1020 pio = pin_to_controller(info, pin_to_bank(pin_id));
1ab36387
DD
1021
1022 if (!pio)
1023 return -EINVAL;
1024
4334ac2d
MR
1025 pin = pin_id % MAX_NB_GPIO_PER_BANK;
1026 mask = pin_to_mask(pin);
03b054e9
SY
1027
1028 if (config & PULL_UP && config & PULL_DOWN)
1029 return -EINVAL;
1030
96bb12de
BB
1031 at91_mux_set_output(pio, mask, config & OUTPUT,
1032 (config & OUTPUT_VAL) >> OUTPUT_VAL_SHIFT);
03b054e9
SY
1033 at91_mux_set_pullup(pio, mask, config & PULL_UP);
1034 at91_mux_set_multidrive(pio, mask, config & MULTI_DRIVE);
1035 if (info->ops->set_deglitch)
1036 info->ops->set_deglitch(pio, mask, config & DEGLITCH);
1037 if (info->ops->set_debounce)
1038 info->ops->set_debounce(pio, mask, config & DEBOUNCE,
7ebd7a3a 1039 (config & DEBOUNCE_VAL) >> DEBOUNCE_VAL_SHIFT);
03b054e9
SY
1040 if (info->ops->set_pulldown)
1041 info->ops->set_pulldown(pio, mask, config & PULL_DOWN);
1042 if (info->ops->disable_schmitt_trig && config & DIS_SCHMIT)
1043 info->ops->disable_schmitt_trig(pio, mask);
4334ac2d
MR
1044 if (info->ops->set_drivestrength)
1045 info->ops->set_drivestrength(pio, pin,
1046 (config & DRIVE_STRENGTH)
1047 >> DRIVE_STRENGTH_SHIFT);
64e21add
CB
1048 if (info->ops->set_slewrate)
1049 info->ops->set_slewrate(pio, pin,
1050 (config & SLEWRATE) >> SLEWRATE_SHIFT);
03b054e9
SY
1051
1052 } /* for each config */
7ebd7a3a 1053
6732ae5c
JCPV
1054 return 0;
1055}
1056
4d9b8a8e
AB
1057#define DBG_SHOW_FLAG(flag) do { \
1058 if (config & flag) { \
1059 if (num_conf) \
1060 seq_puts(s, "|"); \
1061 seq_puts(s, #flag); \
1062 num_conf++; \
1063 } \
1064} while (0)
1065
b67328e1 1066#define DBG_SHOW_FLAG_MASKED(mask, flag, name) do { \
4334ac2d
MR
1067 if ((config & mask) == flag) { \
1068 if (num_conf) \
1069 seq_puts(s, "|"); \
b67328e1 1070 seq_puts(s, #name); \
4334ac2d
MR
1071 num_conf++; \
1072 } \
1073} while (0)
1074
6732ae5c
JCPV
1075static void at91_pinconf_dbg_show(struct pinctrl_dev *pctldev,
1076 struct seq_file *s, unsigned pin_id)
1077{
4d9b8a8e 1078 unsigned long config;
445d2026 1079 int val, num_conf = 0;
4d9b8a8e 1080
445d2026 1081 at91_pinconf_get(pctldev, pin_id, &config);
4d9b8a8e
AB
1082
1083 DBG_SHOW_FLAG(MULTI_DRIVE);
1084 DBG_SHOW_FLAG(PULL_UP);
1085 DBG_SHOW_FLAG(PULL_DOWN);
1086 DBG_SHOW_FLAG(DIS_SCHMIT);
1087 DBG_SHOW_FLAG(DEGLITCH);
b67328e1
CB
1088 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_BIT_MSK(LOW),
1089 DRIVE_STRENGTH_LOW);
1090 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_BIT_MSK(MED),
1091 DRIVE_STRENGTH_MED);
1092 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_BIT_MSK(HI),
1093 DRIVE_STRENGTH_HI);
64e21add 1094 DBG_SHOW_FLAG(SLEWRATE);
4d9b8a8e
AB
1095 DBG_SHOW_FLAG(DEBOUNCE);
1096 if (config & DEBOUNCE) {
1097 val = config >> DEBOUNCE_VAL_SHIFT;
1098 seq_printf(s, "(%d)", val);
1099 }
6732ae5c 1100
4d9b8a8e 1101 return;
6732ae5c
JCPV
1102}
1103
1104static void at91_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
1105 struct seq_file *s, unsigned group)
1106{
1107}
1108
022ab148 1109static const struct pinconf_ops at91_pinconf_ops = {
6732ae5c
JCPV
1110 .pin_config_get = at91_pinconf_get,
1111 .pin_config_set = at91_pinconf_set,
1112 .pin_config_dbg_show = at91_pinconf_dbg_show,
1113 .pin_config_group_dbg_show = at91_pinconf_group_dbg_show,
1114};
1115
1116static struct pinctrl_desc at91_pinctrl_desc = {
1117 .pctlops = &at91_pctrl_ops,
1118 .pmxops = &at91_pmx_ops,
1119 .confops = &at91_pinconf_ops,
1120 .owner = THIS_MODULE,
1121};
1122
1123static const char *gpio_compat = "atmel,at91rm9200-gpio";
1124
150632b0
GKH
1125static void at91_pinctrl_child_count(struct at91_pinctrl *info,
1126 struct device_node *np)
6732ae5c
JCPV
1127{
1128 struct device_node *child;
1129
1130 for_each_child_of_node(np, child) {
1131 if (of_device_is_compatible(child, gpio_compat)) {
a0b957f3
JCPV
1132 if (of_device_is_available(child))
1133 info->nactive_banks++;
6732ae5c
JCPV
1134 } else {
1135 info->nfunctions++;
1136 info->ngroups += of_get_child_count(child);
1137 }
1138 }
1139}
1140
150632b0
GKH
1141static int at91_pinctrl_mux_mask(struct at91_pinctrl *info,
1142 struct device_node *np)
6732ae5c
JCPV
1143{
1144 int ret = 0;
1145 int size;
1164d73a 1146 const __be32 *list;
6732ae5c
JCPV
1147
1148 list = of_get_property(np, "atmel,mux-mask", &size);
1149 if (!list) {
1150 dev_err(info->dev, "can not read the mux-mask of %d\n", size);
1151 return -EINVAL;
1152 }
1153
1154 size /= sizeof(*list);
a0b957f3
JCPV
1155 if (!size || size % gpio_banks) {
1156 dev_err(info->dev, "wrong mux mask array should be by %d\n", gpio_banks);
6732ae5c
JCPV
1157 return -EINVAL;
1158 }
a0b957f3 1159 info->nmux = size / gpio_banks;
6732ae5c 1160
a86854d0
KC
1161 info->mux_mask = devm_kcalloc(info->dev, size, sizeof(u32),
1162 GFP_KERNEL);
3da941b0 1163 if (!info->mux_mask)
6732ae5c 1164 return -ENOMEM;
6732ae5c
JCPV
1165
1166 ret = of_property_read_u32_array(np, "atmel,mux-mask",
1167 info->mux_mask, size);
1168 if (ret)
1169 dev_err(info->dev, "can not read the mux-mask of %d\n", size);
1170 return ret;
1171}
1172
150632b0
GKH
1173static int at91_pinctrl_parse_groups(struct device_node *np,
1174 struct at91_pin_group *grp,
1175 struct at91_pinctrl *info, u32 index)
6732ae5c
JCPV
1176{
1177 struct at91_pmx_pin *pin;
1178 int size;
1164d73a 1179 const __be32 *list;
6732ae5c
JCPV
1180 int i, j;
1181
94f4e54c 1182 dev_dbg(info->dev, "group(%d): %pOFn\n", index, np);
6732ae5c
JCPV
1183
1184 /* Initialise group */
1185 grp->name = np->name;
1186
1187 /*
1188 * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
1189 * do sanity check and calculate pins number
1190 */
1191 list = of_get_property(np, "atmel,pins", &size);
1192 /* we do not check return since it's safe node passed down */
1193 size /= sizeof(*list);
1194 if (!size || size % 4) {
1195 dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
1196 return -EINVAL;
1197 }
1198
1199 grp->npins = size / 4;
a86854d0
KC
1200 pin = grp->pins_conf = devm_kcalloc(info->dev,
1201 grp->npins,
1202 sizeof(struct at91_pmx_pin),
1203 GFP_KERNEL);
1204 grp->pins = devm_kcalloc(info->dev, grp->npins, sizeof(unsigned int),
1205 GFP_KERNEL);
6732ae5c
JCPV
1206 if (!grp->pins_conf || !grp->pins)
1207 return -ENOMEM;
1208
1209 for (i = 0, j = 0; i < size; i += 4, j++) {
1210 pin->bank = be32_to_cpu(*list++);
1211 pin->pin = be32_to_cpu(*list++);
1212 grp->pins[j] = pin->bank * MAX_NB_GPIO_PER_BANK + pin->pin;
1213 pin->mux = be32_to_cpu(*list++);
1214 pin->conf = be32_to_cpu(*list++);
1215
1216 at91_pin_dbg(info->dev, pin);
1217 pin++;
1218 }
1219
1220 return 0;
1221}
1222
150632b0
GKH
1223static int at91_pinctrl_parse_functions(struct device_node *np,
1224 struct at91_pinctrl *info, u32 index)
6732ae5c
JCPV
1225{
1226 struct device_node *child;
1227 struct at91_pmx_func *func;
1228 struct at91_pin_group *grp;
1229 int ret;
1230 static u32 grp_index;
1231 u32 i = 0;
1232
94f4e54c 1233 dev_dbg(info->dev, "parse function(%d): %pOFn\n", index, np);
6732ae5c
JCPV
1234
1235 func = &info->functions[index];
1236
1237 /* Initialise function */
1238 func->name = np->name;
1239 func->ngroups = of_get_child_count(np);
ca7162ad 1240 if (func->ngroups == 0) {
6732ae5c
JCPV
1241 dev_err(info->dev, "no groups defined\n");
1242 return -EINVAL;
1243 }
a86854d0
KC
1244 func->groups = devm_kcalloc(info->dev,
1245 func->ngroups, sizeof(char *), GFP_KERNEL);
6732ae5c
JCPV
1246 if (!func->groups)
1247 return -ENOMEM;
1248
1249 for_each_child_of_node(np, child) {
1250 func->groups[i] = child->name;
1251 grp = &info->groups[grp_index++];
1252 ret = at91_pinctrl_parse_groups(child, grp, info, i++);
d94b986a
JL
1253 if (ret) {
1254 of_node_put(child);
6732ae5c 1255 return ret;
d94b986a 1256 }
6732ae5c
JCPV
1257 }
1258
1259 return 0;
1260}
1261
baa9946e 1262static const struct of_device_id at91_pinctrl_of_match[] = {
4334ac2d 1263 { .compatible = "atmel,sama5d3-pinctrl", .data = &sama5d3_ops },
6732ae5c
JCPV
1264 { .compatible = "atmel,at91sam9x5-pinctrl", .data = &at91sam9x5_ops },
1265 { .compatible = "atmel,at91rm9200-pinctrl", .data = &at91rm9200_ops },
a2fcb1ce 1266 { .compatible = "microchip,sam9x60-pinctrl", .data = &sam9x60_ops },
6732ae5c
JCPV
1267 { /* sentinel */ }
1268};
1269
150632b0
GKH
1270static int at91_pinctrl_probe_dt(struct platform_device *pdev,
1271 struct at91_pinctrl *info)
6732ae5c
JCPV
1272{
1273 int ret = 0;
1274 int i, j;
1275 uint32_t *tmp;
1276 struct device_node *np = pdev->dev.of_node;
1277 struct device_node *child;
1278
1279 if (!np)
1280 return -ENODEV;
1281
1282 info->dev = &pdev->dev;
3c93600d 1283 info->ops = (struct at91_pinctrl_mux_ops *)
6732ae5c
JCPV
1284 of_match_device(at91_pinctrl_of_match, &pdev->dev)->data;
1285 at91_pinctrl_child_count(info, np);
1286
a0b957f3 1287 if (gpio_banks < 1) {
61e310a1 1288 dev_err(&pdev->dev, "you need to specify at least one gpio-controller\n");
6732ae5c
JCPV
1289 return -EINVAL;
1290 }
1291
1292 ret = at91_pinctrl_mux_mask(info, np);
1293 if (ret)
1294 return ret;
1295
1296 dev_dbg(&pdev->dev, "nmux = %d\n", info->nmux);
1297
1298 dev_dbg(&pdev->dev, "mux-mask\n");
1299 tmp = info->mux_mask;
a0b957f3 1300 for (i = 0; i < gpio_banks; i++) {
6732ae5c
JCPV
1301 for (j = 0; j < info->nmux; j++, tmp++) {
1302 dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
1303 }
1304 }
1305
1306 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1307 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
a86854d0
KC
1308 info->functions = devm_kcalloc(&pdev->dev,
1309 info->nfunctions,
1310 sizeof(struct at91_pmx_func),
6732ae5c
JCPV
1311 GFP_KERNEL);
1312 if (!info->functions)
1313 return -ENOMEM;
1314
a86854d0
KC
1315 info->groups = devm_kcalloc(&pdev->dev,
1316 info->ngroups,
1317 sizeof(struct at91_pin_group),
6732ae5c
JCPV
1318 GFP_KERNEL);
1319 if (!info->groups)
1320 return -ENOMEM;
1321
a0b957f3 1322 dev_dbg(&pdev->dev, "nbanks = %d\n", gpio_banks);
6732ae5c
JCPV
1323 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1324 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
1325
1326 i = 0;
1327
1328 for_each_child_of_node(np, child) {
1329 if (of_device_is_compatible(child, gpio_compat))
1330 continue;
1331 ret = at91_pinctrl_parse_functions(child, info, i++);
1332 if (ret) {
1333 dev_err(&pdev->dev, "failed to parse function\n");
d94b986a 1334 of_node_put(child);
6732ae5c
JCPV
1335 return ret;
1336 }
1337 }
1338
1339 return 0;
1340}
1341
150632b0 1342static int at91_pinctrl_probe(struct platform_device *pdev)
6732ae5c
JCPV
1343{
1344 struct at91_pinctrl *info;
1345 struct pinctrl_pin_desc *pdesc;
a0b957f3 1346 int ret, i, j, k, ngpio_chips_enabled = 0;
6732ae5c
JCPV
1347
1348 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1349 if (!info)
1350 return -ENOMEM;
1351
1352 ret = at91_pinctrl_probe_dt(pdev, info);
1353 if (ret)
1354 return ret;
1355
1356 /*
1357 * We need all the GPIO drivers to probe FIRST, or we will not be able
1358 * to obtain references to the struct gpio_chip * for them, and we
1359 * need this to proceed.
1360 */
a0b957f3
JCPV
1361 for (i = 0; i < gpio_banks; i++)
1362 if (gpio_chips[i])
1363 ngpio_chips_enabled++;
1364
1365 if (ngpio_chips_enabled < info->nactive_banks) {
1366 dev_warn(&pdev->dev,
1367 "All GPIO chips are not registered yet (%d/%d)\n",
1368 ngpio_chips_enabled, info->nactive_banks);
1369 devm_kfree(&pdev->dev, info);
1370 return -EPROBE_DEFER;
6732ae5c
JCPV
1371 }
1372
1373 at91_pinctrl_desc.name = dev_name(&pdev->dev);
a0b957f3 1374 at91_pinctrl_desc.npins = gpio_banks * MAX_NB_GPIO_PER_BANK;
6732ae5c 1375 at91_pinctrl_desc.pins = pdesc =
a86854d0
KC
1376 devm_kcalloc(&pdev->dev,
1377 at91_pinctrl_desc.npins, sizeof(*pdesc),
1378 GFP_KERNEL);
6732ae5c
JCPV
1379
1380 if (!at91_pinctrl_desc.pins)
1381 return -ENOMEM;
1382
a0b957f3 1383 for (i = 0, k = 0; i < gpio_banks; i++) {
6732ae5c
JCPV
1384 for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) {
1385 pdesc->number = k;
1386 pdesc->name = kasprintf(GFP_KERNEL, "pio%c%d", i + 'A', j);
1387 pdesc++;
1388 }
1389 }
1390
1391 platform_set_drvdata(pdev, info);
5c67425a
LD
1392 info->pctl = devm_pinctrl_register(&pdev->dev, &at91_pinctrl_desc,
1393 info);
6732ae5c 1394
323de9ef 1395 if (IS_ERR(info->pctl)) {
6732ae5c 1396 dev_err(&pdev->dev, "could not register AT91 pinctrl driver\n");
323de9ef 1397 return PTR_ERR(info->pctl);
6732ae5c
JCPV
1398 }
1399
1400 /* We will handle a range of GPIO pins */
a0b957f3
JCPV
1401 for (i = 0; i < gpio_banks; i++)
1402 if (gpio_chips[i])
1403 pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
6732ae5c
JCPV
1404
1405 dev_info(&pdev->dev, "initialized AT91 pinctrl driver\n");
1406
1407 return 0;
6732ae5c
JCPV
1408}
1409
8af584b8
RG
1410static int at91_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
1411{
370ea611 1412 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
8af584b8
RG
1413 void __iomem *pio = at91_gpio->regbase;
1414 unsigned mask = 1 << offset;
1415 u32 osr;
1416
1417 osr = readl_relaxed(pio + PIO_OSR);
1418 return !(osr & mask);
1419}
1420
6732ae5c
JCPV
1421static int at91_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
1422{
370ea611 1423 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
6732ae5c
JCPV
1424 void __iomem *pio = at91_gpio->regbase;
1425 unsigned mask = 1 << offset;
1426
1427 writel_relaxed(mask, pio + PIO_ODR);
1428 return 0;
1429}
1430
1431static int at91_gpio_get(struct gpio_chip *chip, unsigned offset)
1432{
370ea611 1433 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
6732ae5c
JCPV
1434 void __iomem *pio = at91_gpio->regbase;
1435 unsigned mask = 1 << offset;
1436 u32 pdsr;
1437
1438 pdsr = readl_relaxed(pio + PIO_PDSR);
1439 return (pdsr & mask) != 0;
1440}
1441
1442static void at91_gpio_set(struct gpio_chip *chip, unsigned offset,
1443 int val)
1444{
370ea611 1445 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
6732ae5c
JCPV
1446 void __iomem *pio = at91_gpio->regbase;
1447 unsigned mask = 1 << offset;
1448
1449 writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
1450}
1451
1893b2cf
AS
1452static void at91_gpio_set_multiple(struct gpio_chip *chip,
1453 unsigned long *mask, unsigned long *bits)
1454{
370ea611 1455 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
1893b2cf
AS
1456 void __iomem *pio = at91_gpio->regbase;
1457
1458#define BITS_MASK(bits) (((bits) == 32) ? ~0U : (BIT(bits) - 1))
1459 /* Mask additionally to ngpio as not all GPIO controllers have 32 pins */
1460 uint32_t set_mask = (*mask & *bits) & BITS_MASK(chip->ngpio);
1461 uint32_t clear_mask = (*mask & ~(*bits)) & BITS_MASK(chip->ngpio);
1462
1463 writel_relaxed(set_mask, pio + PIO_SODR);
1464 writel_relaxed(clear_mask, pio + PIO_CODR);
1465}
1466
6732ae5c
JCPV
1467static int at91_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
1468 int val)
1469{
370ea611 1470 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
6732ae5c
JCPV
1471 void __iomem *pio = at91_gpio->regbase;
1472 unsigned mask = 1 << offset;
1473
1474 writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
1475 writel_relaxed(mask, pio + PIO_OER);
1476
1477 return 0;
1478}
1479
6732ae5c
JCPV
1480#ifdef CONFIG_DEBUG_FS
1481static void at91_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1482{
1483 enum at91_mux mode;
1484 int i;
370ea611 1485 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
6732ae5c
JCPV
1486 void __iomem *pio = at91_gpio->regbase;
1487
1488 for (i = 0; i < chip->ngpio; i++) {
47f22716 1489 unsigned mask = pin_to_mask(i);
6732ae5c 1490 const char *gpio_label;
6732ae5c
JCPV
1491
1492 gpio_label = gpiochip_is_requested(chip, i);
1493 if (!gpio_label)
1494 continue;
1495 mode = at91_gpio->ops->get_periph(pio, mask);
1496 seq_printf(s, "[%s] GPIO%s%d: ",
1497 gpio_label, chip->label, i);
1498 if (mode == AT91_MUX_GPIO) {
853b6bf0
MC
1499 seq_printf(s, "[gpio] ");
1500 seq_printf(s, "%s ",
1501 readl_relaxed(pio + PIO_OSR) & mask ?
1502 "output" : "input");
1503 seq_printf(s, "%s\n",
1504 readl_relaxed(pio + PIO_PDSR) & mask ?
1505 "set" : "clear");
6732ae5c
JCPV
1506 } else {
1507 seq_printf(s, "[periph %c]\n",
1508 mode + 'A' - 1);
1509 }
1510 }
1511}
1512#else
1513#define at91_gpio_dbg_show NULL
1514#endif
1515
1516/* Several AIC controller irqs are dispatched through this GPIO handler.
1517 * To use any AT91_PIN_* as an externally triggered IRQ, first call
1518 * at91_set_gpio_input() then maybe enable its glitch filter.
1519 * Then just request_irq() with the pin ID; it works like any ARM IRQ
1520 * handler.
1521 * First implementation always triggers on rising and falling edges
1522 * whereas the newer PIO3 can be additionally configured to trigger on
1523 * level, edge with any polarity.
1524 *
1525 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
1526 * configuring them with at91_set_a_periph() or at91_set_b_periph().
1527 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
1528 */
1529
1530static void gpio_irq_mask(struct irq_data *d)
1531{
1532 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1533 void __iomem *pio = at91_gpio->regbase;
1534 unsigned mask = 1 << d->hwirq;
1535
1536 if (pio)
1537 writel_relaxed(mask, pio + PIO_IDR);
1538}
1539
1540static void gpio_irq_unmask(struct irq_data *d)
1541{
1542 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1543 void __iomem *pio = at91_gpio->regbase;
1544 unsigned mask = 1 << d->hwirq;
1545
1546 if (pio)
1547 writel_relaxed(mask, pio + PIO_IER);
1548}
1549
1550static int gpio_irq_type(struct irq_data *d, unsigned type)
1551{
1552 switch (type) {
1553 case IRQ_TYPE_NONE:
1554 case IRQ_TYPE_EDGE_BOTH:
1555 return 0;
1556 default:
1557 return -EINVAL;
1558 }
1559}
1560
1561/* Alternate irq type for PIO3 support */
1562static int alt_gpio_irq_type(struct irq_data *d, unsigned type)
1563{
1564 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1565 void __iomem *pio = at91_gpio->regbase;
1566 unsigned mask = 1 << d->hwirq;
1567
1568 switch (type) {
1569 case IRQ_TYPE_EDGE_RISING:
c639845b 1570 irq_set_handler_locked(d, handle_simple_irq);
6732ae5c
JCPV
1571 writel_relaxed(mask, pio + PIO_ESR);
1572 writel_relaxed(mask, pio + PIO_REHLSR);
1573 break;
1574 case IRQ_TYPE_EDGE_FALLING:
c639845b 1575 irq_set_handler_locked(d, handle_simple_irq);
6732ae5c
JCPV
1576 writel_relaxed(mask, pio + PIO_ESR);
1577 writel_relaxed(mask, pio + PIO_FELLSR);
1578 break;
1579 case IRQ_TYPE_LEVEL_LOW:
c639845b 1580 irq_set_handler_locked(d, handle_level_irq);
6732ae5c
JCPV
1581 writel_relaxed(mask, pio + PIO_LSR);
1582 writel_relaxed(mask, pio + PIO_FELLSR);
1583 break;
1584 case IRQ_TYPE_LEVEL_HIGH:
c639845b 1585 irq_set_handler_locked(d, handle_level_irq);
6732ae5c
JCPV
1586 writel_relaxed(mask, pio + PIO_LSR);
1587 writel_relaxed(mask, pio + PIO_REHLSR);
1588 break;
1589 case IRQ_TYPE_EDGE_BOTH:
1590 /*
1591 * disable additional interrupt modes:
1592 * fall back to default behavior
1593 */
c639845b 1594 irq_set_handler_locked(d, handle_simple_irq);
6732ae5c
JCPV
1595 writel_relaxed(mask, pio + PIO_AIMDR);
1596 return 0;
1597 case IRQ_TYPE_NONE:
1598 default:
1c5fb66a 1599 pr_warn("AT91: No type for GPIO irq offset %d\n", d->irq);
6732ae5c
JCPV
1600 return -EINVAL;
1601 }
1602
1603 /* enable additional interrupt modes */
1604 writel_relaxed(mask, pio + PIO_AIMER);
1605
1606 return 0;
1607}
1608
80cc3732
AS
1609static void gpio_irq_ack(struct irq_data *d)
1610{
1611 /* the interrupt is already cleared before by reading ISR */
1612}
1613
6732ae5c 1614#ifdef CONFIG_PM
647f8d94
LD
1615
1616static u32 wakeups[MAX_GPIO_BANKS];
1617static u32 backups[MAX_GPIO_BANKS];
1618
6732ae5c
JCPV
1619static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
1620{
1621 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1622 unsigned bank = at91_gpio->pioc_idx;
647f8d94 1623 unsigned mask = 1 << d->hwirq;
6732ae5c
JCPV
1624
1625 if (unlikely(bank >= MAX_GPIO_BANKS))
1626 return -EINVAL;
1627
647f8d94
LD
1628 if (state)
1629 wakeups[bank] |= mask;
1630 else
1631 wakeups[bank] &= ~mask;
1632
6732ae5c
JCPV
1633 irq_set_irq_wake(at91_gpio->pioc_virq, state);
1634
1635 return 0;
1636}
647f8d94
LD
1637
1638void at91_pinctrl_gpio_suspend(void)
1639{
1640 int i;
1641
1642 for (i = 0; i < gpio_banks; i++) {
1643 void __iomem *pio;
1644
1645 if (!gpio_chips[i])
1646 continue;
1647
1648 pio = gpio_chips[i]->regbase;
1649
d480239b
BD
1650 backups[i] = readl_relaxed(pio + PIO_IMR);
1651 writel_relaxed(backups[i], pio + PIO_IDR);
1652 writel_relaxed(wakeups[i], pio + PIO_IER);
647f8d94 1653
795f9953
BB
1654 if (!wakeups[i])
1655 clk_disable_unprepare(gpio_chips[i]->clock);
1656 else
647f8d94
LD
1657 printk(KERN_DEBUG "GPIO-%c may wake for %08x\n",
1658 'A'+i, wakeups[i]);
647f8d94
LD
1659 }
1660}
1661
1662void at91_pinctrl_gpio_resume(void)
1663{
1664 int i;
1665
1666 for (i = 0; i < gpio_banks; i++) {
1667 void __iomem *pio;
1668
1669 if (!gpio_chips[i])
1670 continue;
1671
1672 pio = gpio_chips[i]->regbase;
1673
37ef1d92
BB
1674 if (!wakeups[i])
1675 clk_prepare_enable(gpio_chips[i]->clock);
647f8d94 1676
d480239b
BD
1677 writel_relaxed(wakeups[i], pio + PIO_IDR);
1678 writel_relaxed(backups[i], pio + PIO_IER);
647f8d94
LD
1679 }
1680}
1681
6732ae5c
JCPV
1682#else
1683#define gpio_irq_set_wake NULL
647f8d94 1684#endif /* CONFIG_PM */
6732ae5c 1685
bd0b9ac4 1686static void gpio_irq_handler(struct irq_desc *desc)
6732ae5c 1687{
5663bb27 1688 struct irq_chip *chip = irq_desc_get_chip(desc);
80cc3732 1689 struct gpio_chip *gpio_chip = irq_desc_get_handler_data(desc);
370ea611 1690 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(gpio_chip);
6732ae5c
JCPV
1691 void __iomem *pio = at91_gpio->regbase;
1692 unsigned long isr;
1693 int n;
1694
1695 chained_irq_enter(chip, desc);
1696 for (;;) {
1697 /* Reading ISR acks pending (edge triggered) GPIO interrupts.
c2eb9e7f 1698 * When there are none pending, we're finished unless we need
6732ae5c
JCPV
1699 * to process multiple banks (like ID_PIOCDE on sam9263).
1700 */
1701 isr = readl_relaxed(pio + PIO_ISR) & readl_relaxed(pio + PIO_IMR);
1702 if (!isr) {
1703 if (!at91_gpio->next)
1704 break;
1705 at91_gpio = at91_gpio->next;
1706 pio = at91_gpio->regbase;
cccb0c3e 1707 gpio_chip = &at91_gpio->chip;
6732ae5c
JCPV
1708 continue;
1709 }
1710
05daa16a 1711 for_each_set_bit(n, &isr, BITS_PER_LONG) {
80cc3732 1712 generic_handle_irq(irq_find_mapping(
f0fbe7bc 1713 gpio_chip->irq.domain, n));
6732ae5c
JCPV
1714 }
1715 }
1716 chained_irq_exit(chip, desc);
1717 /* now it may re-trigger */
1718}
1719
834e1678 1720static int at91_gpio_of_irq_setup(struct platform_device *pdev,
6732ae5c
JCPV
1721 struct at91_gpio_chip *at91_gpio)
1722{
a0b957f3 1723 struct gpio_chip *gpiochip_prev = NULL;
cccb0c3e 1724 struct at91_gpio_chip *prev = NULL;
6732ae5c 1725 struct irq_data *d = irq_get_irq_data(at91_gpio->pioc_virq);
0c3dfa17 1726 struct irq_chip *gpio_irqchip;
a0b957f3 1727 int ret, i;
6732ae5c 1728
0c3dfa17
LD
1729 gpio_irqchip = devm_kzalloc(&pdev->dev, sizeof(*gpio_irqchip), GFP_KERNEL);
1730 if (!gpio_irqchip)
1731 return -ENOMEM;
1732
6732ae5c
JCPV
1733 at91_gpio->pioc_hwirq = irqd_to_hwirq(d);
1734
0c3dfa17
LD
1735 gpio_irqchip->name = "GPIO";
1736 gpio_irqchip->irq_ack = gpio_irq_ack;
1737 gpio_irqchip->irq_disable = gpio_irq_mask;
1738 gpio_irqchip->irq_mask = gpio_irq_mask;
1739 gpio_irqchip->irq_unmask = gpio_irq_unmask;
1740 gpio_irqchip->irq_set_wake = gpio_irq_set_wake,
1741 gpio_irqchip->irq_set_type = at91_gpio->ops->irq_type;
6732ae5c
JCPV
1742
1743 /* Disable irqs of this PIO controller */
1744 writel_relaxed(~0, at91_gpio->regbase + PIO_IDR);
1745
80cc3732
AS
1746 /*
1747 * Let the generic code handle this edge IRQ, the the chained
1748 * handler will perform the actual work of handling the parent
1749 * interrupt.
1750 */
1751 ret = gpiochip_irqchip_add(&at91_gpio->chip,
0c3dfa17 1752 gpio_irqchip,
80cc3732
AS
1753 0,
1754 handle_edge_irq,
5803348c 1755 IRQ_TYPE_NONE);
834e1678
PG
1756 if (ret) {
1757 dev_err(&pdev->dev, "at91_gpio.%d: Couldn't add irqchip to gpiochip.\n",
6732ae5c 1758 at91_gpio->pioc_idx);
834e1678
PG
1759 return ret;
1760 }
6732ae5c 1761
cccb0c3e
AS
1762 /* The top level handler handles one bank of GPIOs, except
1763 * on some SoC it can handle up to three...
1764 * We only set up the handler for the first of the list.
1765 */
a0b957f3
JCPV
1766 gpiochip_prev = irq_get_handler_data(at91_gpio->pioc_virq);
1767 if (!gpiochip_prev) {
1768 /* Then register the chain on the parent IRQ */
1769 gpiochip_set_chained_irqchip(&at91_gpio->chip,
0c3dfa17 1770 gpio_irqchip,
a0b957f3
JCPV
1771 at91_gpio->pioc_virq,
1772 gpio_irq_handler);
cccb0c3e 1773 return 0;
a0b957f3 1774 }
cccb0c3e 1775
370ea611 1776 prev = gpiochip_get_data(gpiochip_prev);
6732ae5c 1777
a0b957f3
JCPV
1778 /* we can only have 2 banks before */
1779 for (i = 0; i < 2; i++) {
1780 if (prev->next) {
1781 prev = prev->next;
1782 } else {
1783 prev->next = at91_gpio;
1784 return 0;
1785 }
1786 }
1787
1788 return -EINVAL;
6732ae5c
JCPV
1789}
1790
1791/* This structure is replicated for each GPIO block allocated at probe time */
234b6513 1792static const struct gpio_chip at91_gpio_template = {
98c85d58
JG
1793 .request = gpiochip_generic_request,
1794 .free = gpiochip_generic_free,
8af584b8 1795 .get_direction = at91_gpio_get_direction,
6732ae5c
JCPV
1796 .direction_input = at91_gpio_direction_input,
1797 .get = at91_gpio_get,
1798 .direction_output = at91_gpio_direction_output,
1799 .set = at91_gpio_set,
1893b2cf 1800 .set_multiple = at91_gpio_set_multiple,
6732ae5c 1801 .dbg_show = at91_gpio_dbg_show,
9fb1f39e 1802 .can_sleep = false,
6732ae5c
JCPV
1803 .ngpio = MAX_NB_GPIO_PER_BANK,
1804};
1805
baa9946e 1806static const struct of_device_id at91_gpio_of_match[] = {
6732ae5c
JCPV
1807 { .compatible = "atmel,at91sam9x5-gpio", .data = &at91sam9x5_ops, },
1808 { .compatible = "atmel,at91rm9200-gpio", .data = &at91rm9200_ops },
a2fcb1ce 1809 { .compatible = "microchip,sam9x60-gpio", .data = &sam9x60_ops },
6732ae5c
JCPV
1810 { /* sentinel */ }
1811};
1812
150632b0 1813static int at91_gpio_probe(struct platform_device *pdev)
6732ae5c
JCPV
1814{
1815 struct device_node *np = pdev->dev.of_node;
1816 struct resource *res;
1817 struct at91_gpio_chip *at91_chip = NULL;
1818 struct gpio_chip *chip;
1819 struct pinctrl_gpio_range *range;
1820 int ret = 0;
32b01a36 1821 int irq, i;
6732ae5c
JCPV
1822 int alias_idx = of_alias_get_id(np, "gpio");
1823 uint32_t ngpio;
32b01a36 1824 char **names;
6732ae5c
JCPV
1825
1826 BUG_ON(alias_idx >= ARRAY_SIZE(gpio_chips));
1827 if (gpio_chips[alias_idx]) {
1828 ret = -EBUSY;
1829 goto err;
1830 }
1831
6732ae5c
JCPV
1832 irq = platform_get_irq(pdev, 0);
1833 if (irq < 0) {
1834 ret = irq;
1835 goto err;
1836 }
1837
1838 at91_chip = devm_kzalloc(&pdev->dev, sizeof(*at91_chip), GFP_KERNEL);
1839 if (!at91_chip) {
1840 ret = -ENOMEM;
1841 goto err;
1842 }
1843
f50b9e12 1844 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
9e0c1fb2
TR
1845 at91_chip->regbase = devm_ioremap_resource(&pdev->dev, res);
1846 if (IS_ERR(at91_chip->regbase)) {
1847 ret = PTR_ERR(at91_chip->regbase);
6732ae5c
JCPV
1848 goto err;
1849 }
1850
3c93600d 1851 at91_chip->ops = (struct at91_pinctrl_mux_ops *)
6732ae5c
JCPV
1852 of_match_device(at91_gpio_of_match, &pdev->dev)->data;
1853 at91_chip->pioc_virq = irq;
1854 at91_chip->pioc_idx = alias_idx;
1855
02b837ff 1856 at91_chip->clock = devm_clk_get(&pdev->dev, NULL);
6732ae5c
JCPV
1857 if (IS_ERR(at91_chip->clock)) {
1858 dev_err(&pdev->dev, "failed to get clock, ignoring.\n");
70e41974 1859 ret = PTR_ERR(at91_chip->clock);
6732ae5c
JCPV
1860 goto err;
1861 }
1862
7d3a3fe6 1863 ret = clk_prepare_enable(at91_chip->clock);
70e41974 1864 if (ret) {
7d3a3fe6 1865 dev_err(&pdev->dev, "failed to prepare and enable clock, ignoring.\n");
70e41974 1866 goto clk_enable_err;
6732ae5c
JCPV
1867 }
1868
1869 at91_chip->chip = at91_gpio_template;
1870
1871 chip = &at91_chip->chip;
1872 chip->of_node = np;
1873 chip->label = dev_name(&pdev->dev);
58383c78 1874 chip->parent = &pdev->dev;
6732ae5c
JCPV
1875 chip->owner = THIS_MODULE;
1876 chip->base = alias_idx * MAX_NB_GPIO_PER_BANK;
1877
1878 if (!of_property_read_u32(np, "#gpio-lines", &ngpio)) {
1879 if (ngpio >= MAX_NB_GPIO_PER_BANK)
1880 pr_err("at91_gpio.%d, gpio-nb >= %d failback to %d\n",
1881 alias_idx, MAX_NB_GPIO_PER_BANK, MAX_NB_GPIO_PER_BANK);
1882 else
1883 chip->ngpio = ngpio;
1884 }
1885
a86854d0 1886 names = devm_kcalloc(&pdev->dev, chip->ngpio, sizeof(char *),
3c93600d 1887 GFP_KERNEL);
32b01a36
JCPV
1888
1889 if (!names) {
1890 ret = -ENOMEM;
70e41974 1891 goto clk_enable_err;
32b01a36
JCPV
1892 }
1893
1894 for (i = 0; i < chip->ngpio; i++)
1895 names[i] = kasprintf(GFP_KERNEL, "pio%c%d", alias_idx + 'A', i);
1896
3c93600d 1897 chip->names = (const char *const *)names;
32b01a36 1898
6732ae5c
JCPV
1899 range = &at91_chip->range;
1900 range->name = chip->label;
1901 range->id = alias_idx;
1902 range->pin_base = range->base = range->id * MAX_NB_GPIO_PER_BANK;
1903
1904 range->npins = chip->ngpio;
1905 range->gc = chip;
1906
370ea611 1907 ret = gpiochip_add_data(chip, at91_chip);
6732ae5c 1908 if (ret)
70e41974 1909 goto gpiochip_add_err;
6732ae5c
JCPV
1910
1911 gpio_chips[alias_idx] = at91_chip;
1912 gpio_banks = max(gpio_banks, alias_idx + 1);
1913
834e1678
PG
1914 ret = at91_gpio_of_irq_setup(pdev, at91_chip);
1915 if (ret)
1916 goto irq_setup_err;
6732ae5c
JCPV
1917
1918 dev_info(&pdev->dev, "at address %p\n", at91_chip->regbase);
1919
1920 return 0;
1921
834e1678
PG
1922irq_setup_err:
1923 gpiochip_remove(chip);
70e41974 1924gpiochip_add_err:
70e41974 1925clk_enable_err:
7d3a3fe6 1926 clk_disable_unprepare(at91_chip->clock);
6732ae5c
JCPV
1927err:
1928 dev_err(&pdev->dev, "Failure %i for GPIO %i\n", ret, alias_idx);
1929
1930 return ret;
1931}
1932
1933static struct platform_driver at91_gpio_driver = {
1934 .driver = {
1935 .name = "gpio-at91",
606fca94 1936 .of_match_table = at91_gpio_of_match,
6732ae5c
JCPV
1937 },
1938 .probe = at91_gpio_probe,
1939};
1940
1941static struct platform_driver at91_pinctrl_driver = {
1942 .driver = {
1943 .name = "pinctrl-at91",
606fca94 1944 .of_match_table = at91_pinctrl_of_match,
6732ae5c
JCPV
1945 },
1946 .probe = at91_pinctrl_probe,
6732ae5c
JCPV
1947};
1948
bab7f5a4
TR
1949static struct platform_driver * const drivers[] = {
1950 &at91_gpio_driver,
1951 &at91_pinctrl_driver,
1952};
1953
6732ae5c
JCPV
1954static int __init at91_pinctrl_init(void)
1955{
bab7f5a4 1956 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
6732ae5c
JCPV
1957}
1958arch_initcall(at91_pinctrl_init);