2 * at91 pinctrl driver based on at91 pinmux core
4 * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
10 #include <linux/err.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/of_address.h>
16 #include <linux/of_irq.h>
17 #include <linux/slab.h>
18 #include <linux/interrupt.h>
20 #include <linux/gpio.h>
21 #include <linux/pinctrl/machine.h>
22 #include <linux/pinctrl/pinconf.h>
23 #include <linux/pinctrl/pinctrl.h>
24 #include <linux/pinctrl/pinmux.h>
25 /* Since we request GPIOs from ourself */
26 #include <linux/pinctrl/consumer.h>
28 #include "pinctrl-at91.h"
31 #define MAX_GPIO_BANKS 5
32 #define MAX_NB_GPIO_PER_BANK 32
34 struct at91_pinctrl_mux_ops
;
36 struct at91_gpio_chip
{
37 struct gpio_chip chip
;
38 struct pinctrl_gpio_range range
;
39 struct at91_gpio_chip
*next
; /* Bank sharing same clock */
40 int pioc_hwirq
; /* PIO bank interrupt identifier on AIC */
41 int pioc_virq
; /* PIO bank Linux virtual interrupt */
42 int pioc_idx
; /* PIO bank index */
43 void __iomem
*regbase
; /* PIO bank virtual address */
44 struct clk
*clock
; /* associated clock */
45 struct at91_pinctrl_mux_ops
*ops
; /* ops */
48 #define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip)
50 static struct at91_gpio_chip
*gpio_chips
[MAX_GPIO_BANKS
];
52 static int gpio_banks
;
54 #define PULL_UP (1 << 0)
55 #define MULTI_DRIVE (1 << 1)
56 #define DEGLITCH (1 << 2)
57 #define PULL_DOWN (1 << 3)
58 #define DIS_SCHMIT (1 << 4)
59 #define DRIVE_STRENGTH_SHIFT 5
60 #define DRIVE_STRENGTH_MASK 0x3
61 #define DRIVE_STRENGTH (DRIVE_STRENGTH_MASK << DRIVE_STRENGTH_SHIFT)
62 #define DEBOUNCE (1 << 16)
63 #define DEBOUNCE_VAL_SHIFT 17
64 #define DEBOUNCE_VAL (0x3fff << DEBOUNCE_VAL_SHIFT)
67 * These defines will translated the dt binding settings to our internal
68 * settings. They are not necessarily the same value as the register setting.
69 * The actual drive strength current of low, medium and high must be looked up
70 * from the corresponding device datasheet. This value is different for pins
71 * that are even in the same banks. It is also dependent on VCC.
72 * DRIVE_STRENGTH_DEFAULT is just a placeholder to avoid changing the drive
73 * strength when there is no dt config for it.
75 #define DRIVE_STRENGTH_DEFAULT (0 << DRIVE_STRENGTH_SHIFT)
76 #define DRIVE_STRENGTH_LOW (1 << DRIVE_STRENGTH_SHIFT)
77 #define DRIVE_STRENGTH_MED (2 << DRIVE_STRENGTH_SHIFT)
78 #define DRIVE_STRENGTH_HI (3 << DRIVE_STRENGTH_SHIFT)
81 * struct at91_pmx_func - describes AT91 pinmux functions
82 * @name: the name of this specific function
83 * @groups: corresponding pin groups
84 * @ngroups: the number of groups
86 struct at91_pmx_func
{
94 AT91_MUX_PERIPH_A
= 1,
95 AT91_MUX_PERIPH_B
= 2,
96 AT91_MUX_PERIPH_C
= 3,
97 AT91_MUX_PERIPH_D
= 4,
101 * struct at91_pmx_pin - describes an At91 pin mux
102 * @bank: the bank of the pin
103 * @pin: the pin number in the @bank
104 * @mux: the mux mode : gpio or periph_x of the pin i.e. alternate function.
105 * @conf: the configuration of the pin: PULL_UP, MULTIDRIVE etc...
107 struct at91_pmx_pin
{
115 * struct at91_pin_group - describes an At91 pin group
116 * @name: the name of this specific pin group
117 * @pins_conf: the mux mode for each pin in this group. The size of this
118 * array is the same as pins.
119 * @pins: an array of discrete physical pins used in this group, taken
120 * from the driver-local pin enumeration space
121 * @npins: the number of pins in this group array, i.e. the number of
122 * elements in .pins so we can iterate over that array
124 struct at91_pin_group
{
126 struct at91_pmx_pin
*pins_conf
;
132 * struct at91_pinctrl_mux_ops - describes an AT91 mux ops group
133 * on new IP with support for periph C and D the way to mux in
134 * periph A and B has changed
135 * So provide the right call back
136 * if not present means the IP does not support it
137 * @get_periph: return the periph mode configured
138 * @mux_A_periph: mux as periph A
139 * @mux_B_periph: mux as periph B
140 * @mux_C_periph: mux as periph C
141 * @mux_D_periph: mux as periph D
142 * @get_deglitch: get deglitch status
143 * @set_deglitch: enable/disable deglitch
144 * @get_debounce: get debounce status
145 * @set_debounce: enable/disable debounce
146 * @get_pulldown: get pulldown status
147 * @set_pulldown: enable/disable pulldown
148 * @get_schmitt_trig: get schmitt trigger status
149 * @disable_schmitt_trig: disable schmitt trigger
150 * @irq_type: return irq type
152 struct at91_pinctrl_mux_ops
{
153 enum at91_mux (*get_periph
)(void __iomem
*pio
, unsigned mask
);
154 void (*mux_A_periph
)(void __iomem
*pio
, unsigned mask
);
155 void (*mux_B_periph
)(void __iomem
*pio
, unsigned mask
);
156 void (*mux_C_periph
)(void __iomem
*pio
, unsigned mask
);
157 void (*mux_D_periph
)(void __iomem
*pio
, unsigned mask
);
158 bool (*get_deglitch
)(void __iomem
*pio
, unsigned pin
);
159 void (*set_deglitch
)(void __iomem
*pio
, unsigned mask
, bool is_on
);
160 bool (*get_debounce
)(void __iomem
*pio
, unsigned pin
, u32
*div
);
161 void (*set_debounce
)(void __iomem
*pio
, unsigned mask
, bool is_on
, u32 div
);
162 bool (*get_pulldown
)(void __iomem
*pio
, unsigned pin
);
163 void (*set_pulldown
)(void __iomem
*pio
, unsigned mask
, bool is_on
);
164 bool (*get_schmitt_trig
)(void __iomem
*pio
, unsigned pin
);
165 void (*disable_schmitt_trig
)(void __iomem
*pio
, unsigned mask
);
166 unsigned (*get_drivestrength
)(void __iomem
*pio
, unsigned pin
);
167 void (*set_drivestrength
)(void __iomem
*pio
, unsigned pin
,
170 int (*irq_type
)(struct irq_data
*d
, unsigned type
);
173 static int gpio_irq_type(struct irq_data
*d
, unsigned type
);
174 static int alt_gpio_irq_type(struct irq_data
*d
, unsigned type
);
176 struct at91_pinctrl
{
178 struct pinctrl_dev
*pctl
;
185 struct at91_pmx_func
*functions
;
188 struct at91_pin_group
*groups
;
191 struct at91_pinctrl_mux_ops
*ops
;
194 static const inline struct at91_pin_group
*at91_pinctrl_find_group_by_name(
195 const struct at91_pinctrl
*info
,
198 const struct at91_pin_group
*grp
= NULL
;
201 for (i
= 0; i
< info
->ngroups
; i
++) {
202 if (strcmp(info
->groups
[i
].name
, name
))
205 grp
= &info
->groups
[i
];
206 dev_dbg(info
->dev
, "%s: %d 0:%d\n", name
, grp
->npins
, grp
->pins
[0]);
213 static int at91_get_groups_count(struct pinctrl_dev
*pctldev
)
215 struct at91_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
217 return info
->ngroups
;
220 static const char *at91_get_group_name(struct pinctrl_dev
*pctldev
,
223 struct at91_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
225 return info
->groups
[selector
].name
;
228 static int at91_get_group_pins(struct pinctrl_dev
*pctldev
, unsigned selector
,
229 const unsigned **pins
,
232 struct at91_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
234 if (selector
>= info
->ngroups
)
237 *pins
= info
->groups
[selector
].pins
;
238 *npins
= info
->groups
[selector
].npins
;
243 static void at91_pin_dbg_show(struct pinctrl_dev
*pctldev
, struct seq_file
*s
,
246 seq_printf(s
, "%s", dev_name(pctldev
->dev
));
249 static int at91_dt_node_to_map(struct pinctrl_dev
*pctldev
,
250 struct device_node
*np
,
251 struct pinctrl_map
**map
, unsigned *num_maps
)
253 struct at91_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
254 const struct at91_pin_group
*grp
;
255 struct pinctrl_map
*new_map
;
256 struct device_node
*parent
;
261 * first find the group of this node and check if we need to create
262 * config maps for pins
264 grp
= at91_pinctrl_find_group_by_name(info
, np
->name
);
266 dev_err(info
->dev
, "unable to find group for node %s\n",
271 map_num
+= grp
->npins
;
272 new_map
= devm_kzalloc(pctldev
->dev
, sizeof(*new_map
) * map_num
, GFP_KERNEL
);
280 parent
= of_get_parent(np
);
282 devm_kfree(pctldev
->dev
, new_map
);
285 new_map
[0].type
= PIN_MAP_TYPE_MUX_GROUP
;
286 new_map
[0].data
.mux
.function
= parent
->name
;
287 new_map
[0].data
.mux
.group
= np
->name
;
290 /* create config map */
292 for (i
= 0; i
< grp
->npins
; i
++) {
293 new_map
[i
].type
= PIN_MAP_TYPE_CONFIGS_PIN
;
294 new_map
[i
].data
.configs
.group_or_pin
=
295 pin_get_name(pctldev
, grp
->pins
[i
]);
296 new_map
[i
].data
.configs
.configs
= &grp
->pins_conf
[i
].conf
;
297 new_map
[i
].data
.configs
.num_configs
= 1;
300 dev_dbg(pctldev
->dev
, "maps: function %s group %s num %d\n",
301 (*map
)->data
.mux
.function
, (*map
)->data
.mux
.group
, map_num
);
306 static void at91_dt_free_map(struct pinctrl_dev
*pctldev
,
307 struct pinctrl_map
*map
, unsigned num_maps
)
311 static const struct pinctrl_ops at91_pctrl_ops
= {
312 .get_groups_count
= at91_get_groups_count
,
313 .get_group_name
= at91_get_group_name
,
314 .get_group_pins
= at91_get_group_pins
,
315 .pin_dbg_show
= at91_pin_dbg_show
,
316 .dt_node_to_map
= at91_dt_node_to_map
,
317 .dt_free_map
= at91_dt_free_map
,
320 static void __iomem
*pin_to_controller(struct at91_pinctrl
*info
,
323 return gpio_chips
[bank
]->regbase
;
326 static inline int pin_to_bank(unsigned pin
)
328 return pin
/= MAX_NB_GPIO_PER_BANK
;
331 static unsigned pin_to_mask(unsigned int pin
)
336 static unsigned two_bit_pin_value_shift_amount(unsigned int pin
)
338 /* return the shift value for a pin for "two bit" per pin registers,
339 * i.e. drive strength */
340 return 2*((pin
>= MAX_NB_GPIO_PER_BANK
/2)
341 ? pin
- MAX_NB_GPIO_PER_BANK
/2 : pin
);
344 static unsigned sama5d3_get_drive_register(unsigned int pin
)
346 /* drive strength is split between two registers
347 * with two bits per pin */
348 return (pin
>= MAX_NB_GPIO_PER_BANK
/2)
349 ? SAMA5D3_PIO_DRIVER2
: SAMA5D3_PIO_DRIVER1
;
352 static unsigned at91sam9x5_get_drive_register(unsigned int pin
)
354 /* drive strength is split between two registers
355 * with two bits per pin */
356 return (pin
>= MAX_NB_GPIO_PER_BANK
/2)
357 ? AT91SAM9X5_PIO_DRIVER2
: AT91SAM9X5_PIO_DRIVER1
;
360 static void at91_mux_disable_interrupt(void __iomem
*pio
, unsigned mask
)
362 writel_relaxed(mask
, pio
+ PIO_IDR
);
365 static unsigned at91_mux_get_pullup(void __iomem
*pio
, unsigned pin
)
367 return !((readl_relaxed(pio
+ PIO_PUSR
) >> pin
) & 0x1);
370 static void at91_mux_set_pullup(void __iomem
*pio
, unsigned mask
, bool on
)
373 writel_relaxed(mask
, pio
+ PIO_PPDDR
);
375 writel_relaxed(mask
, pio
+ (on
? PIO_PUER
: PIO_PUDR
));
378 static unsigned at91_mux_get_multidrive(void __iomem
*pio
, unsigned pin
)
380 return (readl_relaxed(pio
+ PIO_MDSR
) >> pin
) & 0x1;
383 static void at91_mux_set_multidrive(void __iomem
*pio
, unsigned mask
, bool on
)
385 writel_relaxed(mask
, pio
+ (on
? PIO_MDER
: PIO_MDDR
));
388 static void at91_mux_set_A_periph(void __iomem
*pio
, unsigned mask
)
390 writel_relaxed(mask
, pio
+ PIO_ASR
);
393 static void at91_mux_set_B_periph(void __iomem
*pio
, unsigned mask
)
395 writel_relaxed(mask
, pio
+ PIO_BSR
);
398 static void at91_mux_pio3_set_A_periph(void __iomem
*pio
, unsigned mask
)
401 writel_relaxed(readl_relaxed(pio
+ PIO_ABCDSR1
) & ~mask
,
403 writel_relaxed(readl_relaxed(pio
+ PIO_ABCDSR2
) & ~mask
,
407 static void at91_mux_pio3_set_B_periph(void __iomem
*pio
, unsigned mask
)
409 writel_relaxed(readl_relaxed(pio
+ PIO_ABCDSR1
) | mask
,
411 writel_relaxed(readl_relaxed(pio
+ PIO_ABCDSR2
) & ~mask
,
415 static void at91_mux_pio3_set_C_periph(void __iomem
*pio
, unsigned mask
)
417 writel_relaxed(readl_relaxed(pio
+ PIO_ABCDSR1
) & ~mask
, pio
+ PIO_ABCDSR1
);
418 writel_relaxed(readl_relaxed(pio
+ PIO_ABCDSR2
) | mask
, pio
+ PIO_ABCDSR2
);
421 static void at91_mux_pio3_set_D_periph(void __iomem
*pio
, unsigned mask
)
423 writel_relaxed(readl_relaxed(pio
+ PIO_ABCDSR1
) | mask
, pio
+ PIO_ABCDSR1
);
424 writel_relaxed(readl_relaxed(pio
+ PIO_ABCDSR2
) | mask
, pio
+ PIO_ABCDSR2
);
427 static enum at91_mux
at91_mux_pio3_get_periph(void __iomem
*pio
, unsigned mask
)
431 if (readl_relaxed(pio
+ PIO_PSR
) & mask
)
432 return AT91_MUX_GPIO
;
434 select
= !!(readl_relaxed(pio
+ PIO_ABCDSR1
) & mask
);
435 select
|= (!!(readl_relaxed(pio
+ PIO_ABCDSR2
) & mask
) << 1);
440 static enum at91_mux
at91_mux_get_periph(void __iomem
*pio
, unsigned mask
)
444 if (readl_relaxed(pio
+ PIO_PSR
) & mask
)
445 return AT91_MUX_GPIO
;
447 select
= readl_relaxed(pio
+ PIO_ABSR
) & mask
;
452 static bool at91_mux_get_deglitch(void __iomem
*pio
, unsigned pin
)
454 return (__raw_readl(pio
+ PIO_IFSR
) >> pin
) & 0x1;
457 static void at91_mux_set_deglitch(void __iomem
*pio
, unsigned mask
, bool is_on
)
459 __raw_writel(mask
, pio
+ (is_on
? PIO_IFER
: PIO_IFDR
));
462 static bool at91_mux_pio3_get_deglitch(void __iomem
*pio
, unsigned pin
)
464 if ((__raw_readl(pio
+ PIO_IFSR
) >> pin
) & 0x1)
465 return !((__raw_readl(pio
+ PIO_IFSCSR
) >> pin
) & 0x1);
470 static void at91_mux_pio3_set_deglitch(void __iomem
*pio
, unsigned mask
, bool is_on
)
473 __raw_writel(mask
, pio
+ PIO_IFSCDR
);
474 at91_mux_set_deglitch(pio
, mask
, is_on
);
477 static bool at91_mux_pio3_get_debounce(void __iomem
*pio
, unsigned pin
, u32
*div
)
479 *div
= __raw_readl(pio
+ PIO_SCDR
);
481 return ((__raw_readl(pio
+ PIO_IFSR
) >> pin
) & 0x1) &&
482 ((__raw_readl(pio
+ PIO_IFSCSR
) >> pin
) & 0x1);
485 static void at91_mux_pio3_set_debounce(void __iomem
*pio
, unsigned mask
,
489 __raw_writel(mask
, pio
+ PIO_IFSCER
);
490 __raw_writel(div
& PIO_SCDR_DIV
, pio
+ PIO_SCDR
);
491 __raw_writel(mask
, pio
+ PIO_IFER
);
493 __raw_writel(mask
, pio
+ PIO_IFSCDR
);
496 static bool at91_mux_pio3_get_pulldown(void __iomem
*pio
, unsigned pin
)
498 return !((__raw_readl(pio
+ PIO_PPDSR
) >> pin
) & 0x1);
501 static void at91_mux_pio3_set_pulldown(void __iomem
*pio
, unsigned mask
, bool is_on
)
504 __raw_writel(mask
, pio
+ PIO_PUDR
);
506 __raw_writel(mask
, pio
+ (is_on
? PIO_PPDER
: PIO_PPDDR
));
509 static void at91_mux_pio3_disable_schmitt_trig(void __iomem
*pio
, unsigned mask
)
511 __raw_writel(__raw_readl(pio
+ PIO_SCHMITT
) | mask
, pio
+ PIO_SCHMITT
);
514 static bool at91_mux_pio3_get_schmitt_trig(void __iomem
*pio
, unsigned pin
)
516 return (__raw_readl(pio
+ PIO_SCHMITT
) >> pin
) & 0x1;
519 static inline u32
read_drive_strength(void __iomem
*reg
, unsigned pin
)
521 unsigned tmp
= __raw_readl(reg
);
523 tmp
= tmp
>> two_bit_pin_value_shift_amount(pin
);
525 return tmp
& DRIVE_STRENGTH_MASK
;
528 static unsigned at91_mux_sama5d3_get_drivestrength(void __iomem
*pio
,
531 unsigned tmp
= read_drive_strength(pio
+
532 sama5d3_get_drive_register(pin
), pin
);
534 /* SAMA5 strength is 1:1 with our defines,
535 * except 0 is equivalent to low per datasheet */
537 tmp
= DRIVE_STRENGTH_LOW
;
542 static unsigned at91_mux_sam9x5_get_drivestrength(void __iomem
*pio
,
545 unsigned tmp
= read_drive_strength(pio
+
546 at91sam9x5_get_drive_register(pin
), pin
);
548 /* strength is inverse in SAM9x5s hardware with the pinctrl defines
549 * hardware: 0 = hi, 1 = med, 2 = low, 3 = rsvd */
550 tmp
= DRIVE_STRENGTH_HI
- tmp
;
555 static void set_drive_strength(void __iomem
*reg
, unsigned pin
, u32 strength
)
557 unsigned tmp
= __raw_readl(reg
);
558 unsigned shift
= two_bit_pin_value_shift_amount(pin
);
560 tmp
&= ~(DRIVE_STRENGTH_MASK
<< shift
);
561 tmp
|= strength
<< shift
;
563 __raw_writel(tmp
, reg
);
566 static void at91_mux_sama5d3_set_drivestrength(void __iomem
*pio
, unsigned pin
,
569 /* do nothing if setting is zero */
573 /* strength is 1 to 1 with setting for SAMA5 */
574 set_drive_strength(pio
+ sama5d3_get_drive_register(pin
), pin
, setting
);
577 static void at91_mux_sam9x5_set_drivestrength(void __iomem
*pio
, unsigned pin
,
580 /* do nothing if setting is zero */
584 /* strength is inverse on SAM9x5s with our defines
585 * 0 = hi, 1 = med, 2 = low, 3 = rsvd */
586 setting
= DRIVE_STRENGTH_HI
- setting
;
588 set_drive_strength(pio
+ at91sam9x5_get_drive_register(pin
), pin
,
592 static struct at91_pinctrl_mux_ops at91rm9200_ops
= {
593 .get_periph
= at91_mux_get_periph
,
594 .mux_A_periph
= at91_mux_set_A_periph
,
595 .mux_B_periph
= at91_mux_set_B_periph
,
596 .get_deglitch
= at91_mux_get_deglitch
,
597 .set_deglitch
= at91_mux_set_deglitch
,
598 .irq_type
= gpio_irq_type
,
601 static struct at91_pinctrl_mux_ops at91sam9x5_ops
= {
602 .get_periph
= at91_mux_pio3_get_periph
,
603 .mux_A_periph
= at91_mux_pio3_set_A_periph
,
604 .mux_B_periph
= at91_mux_pio3_set_B_periph
,
605 .mux_C_periph
= at91_mux_pio3_set_C_periph
,
606 .mux_D_periph
= at91_mux_pio3_set_D_periph
,
607 .get_deglitch
= at91_mux_pio3_get_deglitch
,
608 .set_deglitch
= at91_mux_pio3_set_deglitch
,
609 .get_debounce
= at91_mux_pio3_get_debounce
,
610 .set_debounce
= at91_mux_pio3_set_debounce
,
611 .get_pulldown
= at91_mux_pio3_get_pulldown
,
612 .set_pulldown
= at91_mux_pio3_set_pulldown
,
613 .get_schmitt_trig
= at91_mux_pio3_get_schmitt_trig
,
614 .disable_schmitt_trig
= at91_mux_pio3_disable_schmitt_trig
,
615 .get_drivestrength
= at91_mux_sam9x5_get_drivestrength
,
616 .set_drivestrength
= at91_mux_sam9x5_set_drivestrength
,
617 .irq_type
= alt_gpio_irq_type
,
620 static struct at91_pinctrl_mux_ops sama5d3_ops
= {
621 .get_periph
= at91_mux_pio3_get_periph
,
622 .mux_A_periph
= at91_mux_pio3_set_A_periph
,
623 .mux_B_periph
= at91_mux_pio3_set_B_periph
,
624 .mux_C_periph
= at91_mux_pio3_set_C_periph
,
625 .mux_D_periph
= at91_mux_pio3_set_D_periph
,
626 .get_deglitch
= at91_mux_pio3_get_deglitch
,
627 .set_deglitch
= at91_mux_pio3_set_deglitch
,
628 .get_debounce
= at91_mux_pio3_get_debounce
,
629 .set_debounce
= at91_mux_pio3_set_debounce
,
630 .get_pulldown
= at91_mux_pio3_get_pulldown
,
631 .set_pulldown
= at91_mux_pio3_set_pulldown
,
632 .get_schmitt_trig
= at91_mux_pio3_get_schmitt_trig
,
633 .disable_schmitt_trig
= at91_mux_pio3_disable_schmitt_trig
,
634 .get_drivestrength
= at91_mux_sama5d3_get_drivestrength
,
635 .set_drivestrength
= at91_mux_sama5d3_set_drivestrength
,
636 .irq_type
= alt_gpio_irq_type
,
639 static void at91_pin_dbg(const struct device
*dev
, const struct at91_pmx_pin
*pin
)
642 dev_dbg(dev
, "pio%c%d configured as periph%c with conf = 0x%lx\n",
643 pin
->bank
+ 'A', pin
->pin
, pin
->mux
- 1 + 'A', pin
->conf
);
645 dev_dbg(dev
, "pio%c%d configured as gpio with conf = 0x%lx\n",
646 pin
->bank
+ 'A', pin
->pin
, pin
->conf
);
650 static int pin_check_config(struct at91_pinctrl
*info
, const char *name
,
651 int index
, const struct at91_pmx_pin
*pin
)
655 /* check if it's a valid config */
656 if (pin
->bank
>= info
->nbanks
) {
657 dev_err(info
->dev
, "%s: pin conf %d bank_id %d >= nbanks %d\n",
658 name
, index
, pin
->bank
, info
->nbanks
);
662 if (pin
->pin
>= MAX_NB_GPIO_PER_BANK
) {
663 dev_err(info
->dev
, "%s: pin conf %d pin_bank_id %d >= %d\n",
664 name
, index
, pin
->pin
, MAX_NB_GPIO_PER_BANK
);
673 if (mux
>= info
->nmux
) {
674 dev_err(info
->dev
, "%s: pin conf %d mux_id %d >= nmux %d\n",
675 name
, index
, mux
, info
->nmux
);
679 if (!(info
->mux_mask
[pin
->bank
* info
->nmux
+ mux
] & 1 << pin
->pin
)) {
680 dev_err(info
->dev
, "%s: pin conf %d mux_id %d not supported for pio%c%d\n",
681 name
, index
, mux
, pin
->bank
+ 'A', pin
->pin
);
688 static void at91_mux_gpio_disable(void __iomem
*pio
, unsigned mask
)
690 writel_relaxed(mask
, pio
+ PIO_PDR
);
693 static void at91_mux_gpio_enable(void __iomem
*pio
, unsigned mask
, bool input
)
695 writel_relaxed(mask
, pio
+ PIO_PER
);
696 writel_relaxed(mask
, pio
+ (input
? PIO_ODR
: PIO_OER
));
699 static int at91_pmx_set(struct pinctrl_dev
*pctldev
, unsigned selector
,
702 struct at91_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
703 const struct at91_pmx_pin
*pins_conf
= info
->groups
[group
].pins_conf
;
704 const struct at91_pmx_pin
*pin
;
705 uint32_t npins
= info
->groups
[group
].npins
;
710 dev_dbg(info
->dev
, "enable function %s group %s\n",
711 info
->functions
[selector
].name
, info
->groups
[group
].name
);
713 /* first check that all the pins of the group are valid with a valid
715 for (i
= 0; i
< npins
; i
++) {
717 ret
= pin_check_config(info
, info
->groups
[group
].name
, i
, pin
);
722 for (i
= 0; i
< npins
; i
++) {
724 at91_pin_dbg(info
->dev
, pin
);
725 pio
= pin_to_controller(info
, pin
->bank
);
726 mask
= pin_to_mask(pin
->pin
);
727 at91_mux_disable_interrupt(pio
, mask
);
730 at91_mux_gpio_enable(pio
, mask
, 1);
732 case AT91_MUX_PERIPH_A
:
733 info
->ops
->mux_A_periph(pio
, mask
);
735 case AT91_MUX_PERIPH_B
:
736 info
->ops
->mux_B_periph(pio
, mask
);
738 case AT91_MUX_PERIPH_C
:
739 if (!info
->ops
->mux_C_periph
)
741 info
->ops
->mux_C_periph(pio
, mask
);
743 case AT91_MUX_PERIPH_D
:
744 if (!info
->ops
->mux_D_periph
)
746 info
->ops
->mux_D_periph(pio
, mask
);
750 at91_mux_gpio_disable(pio
, mask
);
756 static int at91_pmx_get_funcs_count(struct pinctrl_dev
*pctldev
)
758 struct at91_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
760 return info
->nfunctions
;
763 static const char *at91_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
766 struct at91_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
768 return info
->functions
[selector
].name
;
771 static int at91_pmx_get_groups(struct pinctrl_dev
*pctldev
, unsigned selector
,
772 const char * const **groups
,
773 unsigned * const num_groups
)
775 struct at91_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
777 *groups
= info
->functions
[selector
].groups
;
778 *num_groups
= info
->functions
[selector
].ngroups
;
783 static int at91_gpio_request_enable(struct pinctrl_dev
*pctldev
,
784 struct pinctrl_gpio_range
*range
,
787 struct at91_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
788 struct at91_gpio_chip
*at91_chip
;
789 struct gpio_chip
*chip
;
793 dev_err(npct
->dev
, "invalid range\n");
797 dev_err(npct
->dev
, "missing GPIO chip in range\n");
801 at91_chip
= container_of(chip
, struct at91_gpio_chip
, chip
);
803 dev_dbg(npct
->dev
, "enable pin %u as GPIO\n", offset
);
805 mask
= 1 << (offset
- chip
->base
);
807 dev_dbg(npct
->dev
, "enable pin %u as PIO%c%d 0x%x\n",
808 offset
, 'A' + range
->id
, offset
- chip
->base
, mask
);
810 writel_relaxed(mask
, at91_chip
->regbase
+ PIO_PER
);
815 static void at91_gpio_disable_free(struct pinctrl_dev
*pctldev
,
816 struct pinctrl_gpio_range
*range
,
819 struct at91_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
821 dev_dbg(npct
->dev
, "disable pin %u as GPIO\n", offset
);
822 /* Set the pin to some default state, GPIO is usually default */
825 static const struct pinmux_ops at91_pmx_ops
= {
826 .get_functions_count
= at91_pmx_get_funcs_count
,
827 .get_function_name
= at91_pmx_get_func_name
,
828 .get_function_groups
= at91_pmx_get_groups
,
829 .set_mux
= at91_pmx_set
,
830 .gpio_request_enable
= at91_gpio_request_enable
,
831 .gpio_disable_free
= at91_gpio_disable_free
,
834 static int at91_pinconf_get(struct pinctrl_dev
*pctldev
,
835 unsigned pin_id
, unsigned long *config
)
837 struct at91_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
843 dev_dbg(info
->dev
, "%s:%d, pin_id=%d", __func__
, __LINE__
, pin_id
);
844 pio
= pin_to_controller(info
, pin_to_bank(pin_id
));
845 pin
= pin_id
% MAX_NB_GPIO_PER_BANK
;
847 if (at91_mux_get_multidrive(pio
, pin
))
848 *config
|= MULTI_DRIVE
;
850 if (at91_mux_get_pullup(pio
, pin
))
853 if (info
->ops
->get_deglitch
&& info
->ops
->get_deglitch(pio
, pin
))
855 if (info
->ops
->get_debounce
&& info
->ops
->get_debounce(pio
, pin
, &div
))
856 *config
|= DEBOUNCE
| (div
<< DEBOUNCE_VAL_SHIFT
);
857 if (info
->ops
->get_pulldown
&& info
->ops
->get_pulldown(pio
, pin
))
858 *config
|= PULL_DOWN
;
859 if (info
->ops
->get_schmitt_trig
&& info
->ops
->get_schmitt_trig(pio
, pin
))
860 *config
|= DIS_SCHMIT
;
861 if (info
->ops
->get_drivestrength
)
862 *config
|= (info
->ops
->get_drivestrength(pio
, pin
)
863 << DRIVE_STRENGTH_SHIFT
);
868 static int at91_pinconf_set(struct pinctrl_dev
*pctldev
,
869 unsigned pin_id
, unsigned long *configs
,
870 unsigned num_configs
)
872 struct at91_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
876 unsigned long config
;
879 for (i
= 0; i
< num_configs
; i
++) {
883 "%s:%d, pin_id=%d, config=0x%lx",
884 __func__
, __LINE__
, pin_id
, config
);
885 pio
= pin_to_controller(info
, pin_to_bank(pin_id
));
886 pin
= pin_id
% MAX_NB_GPIO_PER_BANK
;
887 mask
= pin_to_mask(pin
);
889 if (config
& PULL_UP
&& config
& PULL_DOWN
)
892 at91_mux_set_pullup(pio
, mask
, config
& PULL_UP
);
893 at91_mux_set_multidrive(pio
, mask
, config
& MULTI_DRIVE
);
894 if (info
->ops
->set_deglitch
)
895 info
->ops
->set_deglitch(pio
, mask
, config
& DEGLITCH
);
896 if (info
->ops
->set_debounce
)
897 info
->ops
->set_debounce(pio
, mask
, config
& DEBOUNCE
,
898 (config
& DEBOUNCE_VAL
) >> DEBOUNCE_VAL_SHIFT
);
899 if (info
->ops
->set_pulldown
)
900 info
->ops
->set_pulldown(pio
, mask
, config
& PULL_DOWN
);
901 if (info
->ops
->disable_schmitt_trig
&& config
& DIS_SCHMIT
)
902 info
->ops
->disable_schmitt_trig(pio
, mask
);
903 if (info
->ops
->set_drivestrength
)
904 info
->ops
->set_drivestrength(pio
, pin
,
905 (config
& DRIVE_STRENGTH
)
906 >> DRIVE_STRENGTH_SHIFT
);
908 } /* for each config */
913 #define DBG_SHOW_FLAG(flag) do { \
914 if (config & flag) { \
917 seq_puts(s, #flag); \
922 #define DBG_SHOW_FLAG_MASKED(mask,flag) do { \
923 if ((config & mask) == flag) { \
926 seq_puts(s, #flag); \
931 static void at91_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
932 struct seq_file
*s
, unsigned pin_id
)
934 unsigned long config
;
935 int val
, num_conf
= 0;
937 at91_pinconf_get(pctldev
, pin_id
, &config
);
939 DBG_SHOW_FLAG(MULTI_DRIVE
);
940 DBG_SHOW_FLAG(PULL_UP
);
941 DBG_SHOW_FLAG(PULL_DOWN
);
942 DBG_SHOW_FLAG(DIS_SCHMIT
);
943 DBG_SHOW_FLAG(DEGLITCH
);
944 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH
, DRIVE_STRENGTH_LOW
);
945 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH
, DRIVE_STRENGTH_MED
);
946 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH
, DRIVE_STRENGTH_HI
);
947 DBG_SHOW_FLAG(DEBOUNCE
);
948 if (config
& DEBOUNCE
) {
949 val
= config
>> DEBOUNCE_VAL_SHIFT
;
950 seq_printf(s
, "(%d)", val
);
956 static void at91_pinconf_group_dbg_show(struct pinctrl_dev
*pctldev
,
957 struct seq_file
*s
, unsigned group
)
961 static const struct pinconf_ops at91_pinconf_ops
= {
962 .pin_config_get
= at91_pinconf_get
,
963 .pin_config_set
= at91_pinconf_set
,
964 .pin_config_dbg_show
= at91_pinconf_dbg_show
,
965 .pin_config_group_dbg_show
= at91_pinconf_group_dbg_show
,
968 static struct pinctrl_desc at91_pinctrl_desc
= {
969 .pctlops
= &at91_pctrl_ops
,
970 .pmxops
= &at91_pmx_ops
,
971 .confops
= &at91_pinconf_ops
,
972 .owner
= THIS_MODULE
,
975 static const char *gpio_compat
= "atmel,at91rm9200-gpio";
977 static void at91_pinctrl_child_count(struct at91_pinctrl
*info
,
978 struct device_node
*np
)
980 struct device_node
*child
;
982 for_each_child_of_node(np
, child
) {
983 if (of_device_is_compatible(child
, gpio_compat
)) {
987 info
->ngroups
+= of_get_child_count(child
);
992 static int at91_pinctrl_mux_mask(struct at91_pinctrl
*info
,
993 struct device_node
*np
)
999 list
= of_get_property(np
, "atmel,mux-mask", &size
);
1001 dev_err(info
->dev
, "can not read the mux-mask of %d\n", size
);
1005 size
/= sizeof(*list
);
1006 if (!size
|| size
% info
->nbanks
) {
1007 dev_err(info
->dev
, "wrong mux mask array should be by %d\n", info
->nbanks
);
1010 info
->nmux
= size
/ info
->nbanks
;
1012 info
->mux_mask
= devm_kzalloc(info
->dev
, sizeof(u32
) * size
, GFP_KERNEL
);
1013 if (!info
->mux_mask
) {
1014 dev_err(info
->dev
, "could not alloc mux_mask\n");
1018 ret
= of_property_read_u32_array(np
, "atmel,mux-mask",
1019 info
->mux_mask
, size
);
1021 dev_err(info
->dev
, "can not read the mux-mask of %d\n", size
);
1025 static int at91_pinctrl_parse_groups(struct device_node
*np
,
1026 struct at91_pin_group
*grp
,
1027 struct at91_pinctrl
*info
, u32 index
)
1029 struct at91_pmx_pin
*pin
;
1034 dev_dbg(info
->dev
, "group(%d): %s\n", index
, np
->name
);
1036 /* Initialise group */
1037 grp
->name
= np
->name
;
1040 * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
1041 * do sanity check and calculate pins number
1043 list
= of_get_property(np
, "atmel,pins", &size
);
1044 /* we do not check return since it's safe node passed down */
1045 size
/= sizeof(*list
);
1046 if (!size
|| size
% 4) {
1047 dev_err(info
->dev
, "wrong pins number or pins and configs should be by 4\n");
1051 grp
->npins
= size
/ 4;
1052 pin
= grp
->pins_conf
= devm_kzalloc(info
->dev
, grp
->npins
* sizeof(struct at91_pmx_pin
),
1054 grp
->pins
= devm_kzalloc(info
->dev
, grp
->npins
* sizeof(unsigned int),
1056 if (!grp
->pins_conf
|| !grp
->pins
)
1059 for (i
= 0, j
= 0; i
< size
; i
+= 4, j
++) {
1060 pin
->bank
= be32_to_cpu(*list
++);
1061 pin
->pin
= be32_to_cpu(*list
++);
1062 grp
->pins
[j
] = pin
->bank
* MAX_NB_GPIO_PER_BANK
+ pin
->pin
;
1063 pin
->mux
= be32_to_cpu(*list
++);
1064 pin
->conf
= be32_to_cpu(*list
++);
1066 at91_pin_dbg(info
->dev
, pin
);
1073 static int at91_pinctrl_parse_functions(struct device_node
*np
,
1074 struct at91_pinctrl
*info
, u32 index
)
1076 struct device_node
*child
;
1077 struct at91_pmx_func
*func
;
1078 struct at91_pin_group
*grp
;
1080 static u32 grp_index
;
1083 dev_dbg(info
->dev
, "parse function(%d): %s\n", index
, np
->name
);
1085 func
= &info
->functions
[index
];
1087 /* Initialise function */
1088 func
->name
= np
->name
;
1089 func
->ngroups
= of_get_child_count(np
);
1090 if (func
->ngroups
== 0) {
1091 dev_err(info
->dev
, "no groups defined\n");
1094 func
->groups
= devm_kzalloc(info
->dev
,
1095 func
->ngroups
* sizeof(char *), GFP_KERNEL
);
1099 for_each_child_of_node(np
, child
) {
1100 func
->groups
[i
] = child
->name
;
1101 grp
= &info
->groups
[grp_index
++];
1102 ret
= at91_pinctrl_parse_groups(child
, grp
, info
, i
++);
1110 static struct of_device_id at91_pinctrl_of_match
[] = {
1111 { .compatible
= "atmel,sama5d3-pinctrl", .data
= &sama5d3_ops
},
1112 { .compatible
= "atmel,at91sam9x5-pinctrl", .data
= &at91sam9x5_ops
},
1113 { .compatible
= "atmel,at91rm9200-pinctrl", .data
= &at91rm9200_ops
},
1117 static int at91_pinctrl_probe_dt(struct platform_device
*pdev
,
1118 struct at91_pinctrl
*info
)
1123 struct device_node
*np
= pdev
->dev
.of_node
;
1124 struct device_node
*child
;
1129 info
->dev
= &pdev
->dev
;
1130 info
->ops
= (struct at91_pinctrl_mux_ops
*)
1131 of_match_device(at91_pinctrl_of_match
, &pdev
->dev
)->data
;
1132 at91_pinctrl_child_count(info
, np
);
1134 if (info
->nbanks
< 1) {
1135 dev_err(&pdev
->dev
, "you need to specify at least one gpio-controller\n");
1139 ret
= at91_pinctrl_mux_mask(info
, np
);
1143 dev_dbg(&pdev
->dev
, "nmux = %d\n", info
->nmux
);
1145 dev_dbg(&pdev
->dev
, "mux-mask\n");
1146 tmp
= info
->mux_mask
;
1147 for (i
= 0; i
< info
->nbanks
; i
++) {
1148 for (j
= 0; j
< info
->nmux
; j
++, tmp
++) {
1149 dev_dbg(&pdev
->dev
, "%d:%d\t0x%x\n", i
, j
, tmp
[0]);
1153 dev_dbg(&pdev
->dev
, "nfunctions = %d\n", info
->nfunctions
);
1154 dev_dbg(&pdev
->dev
, "ngroups = %d\n", info
->ngroups
);
1155 info
->functions
= devm_kzalloc(&pdev
->dev
, info
->nfunctions
* sizeof(struct at91_pmx_func
),
1157 if (!info
->functions
)
1160 info
->groups
= devm_kzalloc(&pdev
->dev
, info
->ngroups
* sizeof(struct at91_pin_group
),
1165 dev_dbg(&pdev
->dev
, "nbanks = %d\n", info
->nbanks
);
1166 dev_dbg(&pdev
->dev
, "nfunctions = %d\n", info
->nfunctions
);
1167 dev_dbg(&pdev
->dev
, "ngroups = %d\n", info
->ngroups
);
1171 for_each_child_of_node(np
, child
) {
1172 if (of_device_is_compatible(child
, gpio_compat
))
1174 ret
= at91_pinctrl_parse_functions(child
, info
, i
++);
1176 dev_err(&pdev
->dev
, "failed to parse function\n");
1184 static int at91_pinctrl_probe(struct platform_device
*pdev
)
1186 struct at91_pinctrl
*info
;
1187 struct pinctrl_pin_desc
*pdesc
;
1190 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
1194 ret
= at91_pinctrl_probe_dt(pdev
, info
);
1199 * We need all the GPIO drivers to probe FIRST, or we will not be able
1200 * to obtain references to the struct gpio_chip * for them, and we
1201 * need this to proceed.
1203 for (i
= 0; i
< info
->nbanks
; i
++) {
1204 if (!gpio_chips
[i
]) {
1205 dev_warn(&pdev
->dev
, "GPIO chip %d not registered yet\n", i
);
1206 devm_kfree(&pdev
->dev
, info
);
1207 return -EPROBE_DEFER
;
1211 at91_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
1212 at91_pinctrl_desc
.npins
= info
->nbanks
* MAX_NB_GPIO_PER_BANK
;
1213 at91_pinctrl_desc
.pins
= pdesc
=
1214 devm_kzalloc(&pdev
->dev
, sizeof(*pdesc
) * at91_pinctrl_desc
.npins
, GFP_KERNEL
);
1216 if (!at91_pinctrl_desc
.pins
)
1219 for (i
= 0 , k
= 0; i
< info
->nbanks
; i
++) {
1220 for (j
= 0; j
< MAX_NB_GPIO_PER_BANK
; j
++, k
++) {
1222 pdesc
->name
= kasprintf(GFP_KERNEL
, "pio%c%d", i
+ 'A', j
);
1227 platform_set_drvdata(pdev
, info
);
1228 info
->pctl
= pinctrl_register(&at91_pinctrl_desc
, &pdev
->dev
, info
);
1231 dev_err(&pdev
->dev
, "could not register AT91 pinctrl driver\n");
1236 /* We will handle a range of GPIO pins */
1237 for (i
= 0; i
< info
->nbanks
; i
++)
1238 pinctrl_add_gpio_range(info
->pctl
, &gpio_chips
[i
]->range
);
1240 dev_info(&pdev
->dev
, "initialized AT91 pinctrl driver\n");
1248 static int at91_pinctrl_remove(struct platform_device
*pdev
)
1250 struct at91_pinctrl
*info
= platform_get_drvdata(pdev
);
1252 pinctrl_unregister(info
->pctl
);
1257 static int at91_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
1260 * Map back to global GPIO space and request muxing, the direction
1261 * parameter does not matter for this controller.
1263 int gpio
= chip
->base
+ offset
;
1264 int bank
= chip
->base
/ chip
->ngpio
;
1266 dev_dbg(chip
->dev
, "%s:%d pio%c%d(%d)\n", __func__
, __LINE__
,
1267 'A' + bank
, offset
, gpio
);
1269 return pinctrl_request_gpio(gpio
);
1272 static void at91_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
1274 int gpio
= chip
->base
+ offset
;
1276 pinctrl_free_gpio(gpio
);
1279 static int at91_gpio_get_direction(struct gpio_chip
*chip
, unsigned offset
)
1281 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
1282 void __iomem
*pio
= at91_gpio
->regbase
;
1283 unsigned mask
= 1 << offset
;
1286 osr
= readl_relaxed(pio
+ PIO_OSR
);
1287 return !(osr
& mask
);
1290 static int at91_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
1292 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
1293 void __iomem
*pio
= at91_gpio
->regbase
;
1294 unsigned mask
= 1 << offset
;
1296 writel_relaxed(mask
, pio
+ PIO_ODR
);
1300 static int at91_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
1302 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
1303 void __iomem
*pio
= at91_gpio
->regbase
;
1304 unsigned mask
= 1 << offset
;
1307 pdsr
= readl_relaxed(pio
+ PIO_PDSR
);
1308 return (pdsr
& mask
) != 0;
1311 static void at91_gpio_set(struct gpio_chip
*chip
, unsigned offset
,
1314 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
1315 void __iomem
*pio
= at91_gpio
->regbase
;
1316 unsigned mask
= 1 << offset
;
1318 writel_relaxed(mask
, pio
+ (val
? PIO_SODR
: PIO_CODR
));
1321 static int at91_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
1324 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
1325 void __iomem
*pio
= at91_gpio
->regbase
;
1326 unsigned mask
= 1 << offset
;
1328 writel_relaxed(mask
, pio
+ (val
? PIO_SODR
: PIO_CODR
));
1329 writel_relaxed(mask
, pio
+ PIO_OER
);
1334 #ifdef CONFIG_DEBUG_FS
1335 static void at91_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
1339 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
1340 void __iomem
*pio
= at91_gpio
->regbase
;
1342 for (i
= 0; i
< chip
->ngpio
; i
++) {
1343 unsigned mask
= pin_to_mask(i
);
1344 const char *gpio_label
;
1346 gpio_label
= gpiochip_is_requested(chip
, i
);
1349 mode
= at91_gpio
->ops
->get_periph(pio
, mask
);
1350 seq_printf(s
, "[%s] GPIO%s%d: ",
1351 gpio_label
, chip
->label
, i
);
1352 if (mode
== AT91_MUX_GPIO
) {
1353 seq_printf(s
, "[gpio] ");
1354 seq_printf(s
, "%s ",
1355 readl_relaxed(pio
+ PIO_OSR
) & mask
?
1356 "output" : "input");
1357 seq_printf(s
, "%s\n",
1358 readl_relaxed(pio
+ PIO_PDSR
) & mask
?
1361 seq_printf(s
, "[periph %c]\n",
1367 #define at91_gpio_dbg_show NULL
1370 /* Several AIC controller irqs are dispatched through this GPIO handler.
1371 * To use any AT91_PIN_* as an externally triggered IRQ, first call
1372 * at91_set_gpio_input() then maybe enable its glitch filter.
1373 * Then just request_irq() with the pin ID; it works like any ARM IRQ
1375 * First implementation always triggers on rising and falling edges
1376 * whereas the newer PIO3 can be additionally configured to trigger on
1377 * level, edge with any polarity.
1379 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
1380 * configuring them with at91_set_a_periph() or at91_set_b_periph().
1381 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
1384 static void gpio_irq_mask(struct irq_data
*d
)
1386 struct at91_gpio_chip
*at91_gpio
= irq_data_get_irq_chip_data(d
);
1387 void __iomem
*pio
= at91_gpio
->regbase
;
1388 unsigned mask
= 1 << d
->hwirq
;
1391 writel_relaxed(mask
, pio
+ PIO_IDR
);
1394 static void gpio_irq_unmask(struct irq_data
*d
)
1396 struct at91_gpio_chip
*at91_gpio
= irq_data_get_irq_chip_data(d
);
1397 void __iomem
*pio
= at91_gpio
->regbase
;
1398 unsigned mask
= 1 << d
->hwirq
;
1401 writel_relaxed(mask
, pio
+ PIO_IER
);
1404 static int gpio_irq_type(struct irq_data
*d
, unsigned type
)
1408 case IRQ_TYPE_EDGE_BOTH
:
1415 /* Alternate irq type for PIO3 support */
1416 static int alt_gpio_irq_type(struct irq_data
*d
, unsigned type
)
1418 struct at91_gpio_chip
*at91_gpio
= irq_data_get_irq_chip_data(d
);
1419 void __iomem
*pio
= at91_gpio
->regbase
;
1420 unsigned mask
= 1 << d
->hwirq
;
1423 case IRQ_TYPE_EDGE_RISING
:
1424 __irq_set_handler_locked(d
->irq
, handle_simple_irq
);
1425 writel_relaxed(mask
, pio
+ PIO_ESR
);
1426 writel_relaxed(mask
, pio
+ PIO_REHLSR
);
1428 case IRQ_TYPE_EDGE_FALLING
:
1429 __irq_set_handler_locked(d
->irq
, handle_simple_irq
);
1430 writel_relaxed(mask
, pio
+ PIO_ESR
);
1431 writel_relaxed(mask
, pio
+ PIO_FELLSR
);
1433 case IRQ_TYPE_LEVEL_LOW
:
1434 __irq_set_handler_locked(d
->irq
, handle_level_irq
);
1435 writel_relaxed(mask
, pio
+ PIO_LSR
);
1436 writel_relaxed(mask
, pio
+ PIO_FELLSR
);
1438 case IRQ_TYPE_LEVEL_HIGH
:
1439 __irq_set_handler_locked(d
->irq
, handle_level_irq
);
1440 writel_relaxed(mask
, pio
+ PIO_LSR
);
1441 writel_relaxed(mask
, pio
+ PIO_REHLSR
);
1443 case IRQ_TYPE_EDGE_BOTH
:
1445 * disable additional interrupt modes:
1446 * fall back to default behavior
1448 __irq_set_handler_locked(d
->irq
, handle_simple_irq
);
1449 writel_relaxed(mask
, pio
+ PIO_AIMDR
);
1453 pr_warn("AT91: No type for irq %d\n", gpio_to_irq(d
->irq
));
1457 /* enable additional interrupt modes */
1458 writel_relaxed(mask
, pio
+ PIO_AIMER
);
1463 static void gpio_irq_ack(struct irq_data
*d
)
1465 /* the interrupt is already cleared before by reading ISR */
1468 static unsigned int gpio_irq_startup(struct irq_data
*d
)
1470 struct at91_gpio_chip
*at91_gpio
= irq_data_get_irq_chip_data(d
);
1471 unsigned pin
= d
->hwirq
;
1474 ret
= gpiochip_lock_as_irq(&at91_gpio
->chip
, pin
);
1476 dev_err(at91_gpio
->chip
.dev
, "unable to lock pind %lu IRQ\n",
1484 static void gpio_irq_shutdown(struct irq_data
*d
)
1486 struct at91_gpio_chip
*at91_gpio
= irq_data_get_irq_chip_data(d
);
1487 unsigned pin
= d
->hwirq
;
1490 gpiochip_unlock_as_irq(&at91_gpio
->chip
, pin
);
1495 static u32 wakeups
[MAX_GPIO_BANKS
];
1496 static u32 backups
[MAX_GPIO_BANKS
];
1498 static int gpio_irq_set_wake(struct irq_data
*d
, unsigned state
)
1500 struct at91_gpio_chip
*at91_gpio
= irq_data_get_irq_chip_data(d
);
1501 unsigned bank
= at91_gpio
->pioc_idx
;
1502 unsigned mask
= 1 << d
->hwirq
;
1504 if (unlikely(bank
>= MAX_GPIO_BANKS
))
1508 wakeups
[bank
] |= mask
;
1510 wakeups
[bank
] &= ~mask
;
1512 irq_set_irq_wake(at91_gpio
->pioc_virq
, state
);
1517 void at91_pinctrl_gpio_suspend(void)
1521 for (i
= 0; i
< gpio_banks
; i
++) {
1527 pio
= gpio_chips
[i
]->regbase
;
1529 backups
[i
] = __raw_readl(pio
+ PIO_IMR
);
1530 __raw_writel(backups
[i
], pio
+ PIO_IDR
);
1531 __raw_writel(wakeups
[i
], pio
+ PIO_IER
);
1534 clk_disable_unprepare(gpio_chips
[i
]->clock
);
1536 printk(KERN_DEBUG
"GPIO-%c may wake for %08x\n",
1541 void at91_pinctrl_gpio_resume(void)
1545 for (i
= 0; i
< gpio_banks
; i
++) {
1551 pio
= gpio_chips
[i
]->regbase
;
1554 clk_prepare_enable(gpio_chips
[i
]->clock
);
1556 __raw_writel(wakeups
[i
], pio
+ PIO_IDR
);
1557 __raw_writel(backups
[i
], pio
+ PIO_IER
);
1562 #define gpio_irq_set_wake NULL
1563 #endif /* CONFIG_PM */
1565 static struct irq_chip gpio_irqchip
= {
1567 .irq_ack
= gpio_irq_ack
,
1568 .irq_startup
= gpio_irq_startup
,
1569 .irq_shutdown
= gpio_irq_shutdown
,
1570 .irq_disable
= gpio_irq_mask
,
1571 .irq_mask
= gpio_irq_mask
,
1572 .irq_unmask
= gpio_irq_unmask
,
1573 /* .irq_set_type is set dynamically */
1574 .irq_set_wake
= gpio_irq_set_wake
,
1577 static void gpio_irq_handler(unsigned irq
, struct irq_desc
*desc
)
1579 struct irq_chip
*chip
= irq_get_chip(irq
);
1580 struct gpio_chip
*gpio_chip
= irq_desc_get_handler_data(desc
);
1581 struct at91_gpio_chip
*at91_gpio
= container_of(gpio_chip
,
1582 struct at91_gpio_chip
, chip
);
1584 void __iomem
*pio
= at91_gpio
->regbase
;
1588 chained_irq_enter(chip
, desc
);
1590 /* Reading ISR acks pending (edge triggered) GPIO interrupts.
1591 * When there are none pending, we're finished unless we need
1592 * to process multiple banks (like ID_PIOCDE on sam9263).
1594 isr
= readl_relaxed(pio
+ PIO_ISR
) & readl_relaxed(pio
+ PIO_IMR
);
1596 if (!at91_gpio
->next
)
1598 at91_gpio
= at91_gpio
->next
;
1599 pio
= at91_gpio
->regbase
;
1600 gpio_chip
= &at91_gpio
->chip
;
1604 for_each_set_bit(n
, &isr
, BITS_PER_LONG
) {
1605 generic_handle_irq(irq_find_mapping(
1606 gpio_chip
->irqdomain
, n
));
1609 chained_irq_exit(chip
, desc
);
1610 /* now it may re-trigger */
1613 static int at91_gpio_of_irq_setup(struct platform_device
*pdev
,
1614 struct at91_gpio_chip
*at91_gpio
)
1616 struct at91_gpio_chip
*prev
= NULL
;
1617 struct irq_data
*d
= irq_get_irq_data(at91_gpio
->pioc_virq
);
1620 at91_gpio
->pioc_hwirq
= irqd_to_hwirq(d
);
1622 /* Setup proper .irq_set_type function */
1623 gpio_irqchip
.irq_set_type
= at91_gpio
->ops
->irq_type
;
1625 /* Disable irqs of this PIO controller */
1626 writel_relaxed(~0, at91_gpio
->regbase
+ PIO_IDR
);
1629 * Let the generic code handle this edge IRQ, the the chained
1630 * handler will perform the actual work of handling the parent
1633 ret
= gpiochip_irqchip_add(&at91_gpio
->chip
,
1637 IRQ_TYPE_EDGE_BOTH
);
1639 dev_err(&pdev
->dev
, "at91_gpio.%d: Couldn't add irqchip to gpiochip.\n",
1640 at91_gpio
->pioc_idx
);
1644 /* Setup chained handler */
1645 if (at91_gpio
->pioc_idx
)
1646 prev
= gpio_chips
[at91_gpio
->pioc_idx
- 1];
1648 /* The top level handler handles one bank of GPIOs, except
1649 * on some SoC it can handle up to three...
1650 * We only set up the handler for the first of the list.
1652 if (prev
&& prev
->next
== at91_gpio
)
1655 /* Then register the chain on the parent IRQ */
1656 gpiochip_set_chained_irqchip(&at91_gpio
->chip
,
1658 at91_gpio
->pioc_virq
,
1664 /* This structure is replicated for each GPIO block allocated at probe time */
1665 static struct gpio_chip at91_gpio_template
= {
1666 .request
= at91_gpio_request
,
1667 .free
= at91_gpio_free
,
1668 .get_direction
= at91_gpio_get_direction
,
1669 .direction_input
= at91_gpio_direction_input
,
1670 .get
= at91_gpio_get
,
1671 .direction_output
= at91_gpio_direction_output
,
1672 .set
= at91_gpio_set
,
1673 .dbg_show
= at91_gpio_dbg_show
,
1675 .ngpio
= MAX_NB_GPIO_PER_BANK
,
1678 static void at91_gpio_probe_fixup(void)
1681 struct at91_gpio_chip
*at91_gpio
, *last
= NULL
;
1683 for (i
= 0; i
< gpio_banks
; i
++) {
1684 at91_gpio
= gpio_chips
[i
];
1687 * GPIO controller are grouped on some SoC:
1688 * PIOC, PIOD and PIOE can share the same IRQ line
1690 if (last
&& last
->pioc_virq
== at91_gpio
->pioc_virq
)
1691 last
->next
= at91_gpio
;
1696 static struct of_device_id at91_gpio_of_match
[] = {
1697 { .compatible
= "atmel,at91sam9x5-gpio", .data
= &at91sam9x5_ops
, },
1698 { .compatible
= "atmel,at91rm9200-gpio", .data
= &at91rm9200_ops
},
1702 static int at91_gpio_probe(struct platform_device
*pdev
)
1704 struct device_node
*np
= pdev
->dev
.of_node
;
1705 struct resource
*res
;
1706 struct at91_gpio_chip
*at91_chip
= NULL
;
1707 struct gpio_chip
*chip
;
1708 struct pinctrl_gpio_range
*range
;
1711 int alias_idx
= of_alias_get_id(np
, "gpio");
1715 BUG_ON(alias_idx
>= ARRAY_SIZE(gpio_chips
));
1716 if (gpio_chips
[alias_idx
]) {
1721 irq
= platform_get_irq(pdev
, 0);
1727 at91_chip
= devm_kzalloc(&pdev
->dev
, sizeof(*at91_chip
), GFP_KERNEL
);
1733 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1734 at91_chip
->regbase
= devm_ioremap_resource(&pdev
->dev
, res
);
1735 if (IS_ERR(at91_chip
->regbase
)) {
1736 ret
= PTR_ERR(at91_chip
->regbase
);
1740 at91_chip
->ops
= (struct at91_pinctrl_mux_ops
*)
1741 of_match_device(at91_gpio_of_match
, &pdev
->dev
)->data
;
1742 at91_chip
->pioc_virq
= irq
;
1743 at91_chip
->pioc_idx
= alias_idx
;
1745 at91_chip
->clock
= devm_clk_get(&pdev
->dev
, NULL
);
1746 if (IS_ERR(at91_chip
->clock
)) {
1747 dev_err(&pdev
->dev
, "failed to get clock, ignoring.\n");
1748 ret
= PTR_ERR(at91_chip
->clock
);
1752 ret
= clk_prepare(at91_chip
->clock
);
1754 goto clk_prepare_err
;
1756 /* enable PIO controller's clock */
1757 ret
= clk_enable(at91_chip
->clock
);
1759 dev_err(&pdev
->dev
, "failed to enable clock, ignoring.\n");
1760 goto clk_enable_err
;
1763 at91_chip
->chip
= at91_gpio_template
;
1765 chip
= &at91_chip
->chip
;
1767 chip
->label
= dev_name(&pdev
->dev
);
1768 chip
->dev
= &pdev
->dev
;
1769 chip
->owner
= THIS_MODULE
;
1770 chip
->base
= alias_idx
* MAX_NB_GPIO_PER_BANK
;
1772 if (!of_property_read_u32(np
, "#gpio-lines", &ngpio
)) {
1773 if (ngpio
>= MAX_NB_GPIO_PER_BANK
)
1774 pr_err("at91_gpio.%d, gpio-nb >= %d failback to %d\n",
1775 alias_idx
, MAX_NB_GPIO_PER_BANK
, MAX_NB_GPIO_PER_BANK
);
1777 chip
->ngpio
= ngpio
;
1780 names
= devm_kzalloc(&pdev
->dev
, sizeof(char *) * chip
->ngpio
,
1785 goto clk_enable_err
;
1788 for (i
= 0; i
< chip
->ngpio
; i
++)
1789 names
[i
] = kasprintf(GFP_KERNEL
, "pio%c%d", alias_idx
+ 'A', i
);
1791 chip
->names
= (const char *const *)names
;
1793 range
= &at91_chip
->range
;
1794 range
->name
= chip
->label
;
1795 range
->id
= alias_idx
;
1796 range
->pin_base
= range
->base
= range
->id
* MAX_NB_GPIO_PER_BANK
;
1798 range
->npins
= chip
->ngpio
;
1801 ret
= gpiochip_add(chip
);
1803 goto gpiochip_add_err
;
1805 gpio_chips
[alias_idx
] = at91_chip
;
1806 gpio_banks
= max(gpio_banks
, alias_idx
+ 1);
1808 at91_gpio_probe_fixup();
1810 ret
= at91_gpio_of_irq_setup(pdev
, at91_chip
);
1814 dev_info(&pdev
->dev
, "at address %p\n", at91_chip
->regbase
);
1819 gpiochip_remove(chip
);
1821 clk_disable(at91_chip
->clock
);
1823 clk_unprepare(at91_chip
->clock
);
1826 dev_err(&pdev
->dev
, "Failure %i for GPIO %i\n", ret
, alias_idx
);
1831 static struct platform_driver at91_gpio_driver
= {
1833 .name
= "gpio-at91",
1834 .of_match_table
= at91_gpio_of_match
,
1836 .probe
= at91_gpio_probe
,
1839 static struct platform_driver at91_pinctrl_driver
= {
1841 .name
= "pinctrl-at91",
1842 .of_match_table
= at91_pinctrl_of_match
,
1844 .probe
= at91_pinctrl_probe
,
1845 .remove
= at91_pinctrl_remove
,
1848 static int __init
at91_pinctrl_init(void)
1852 ret
= platform_driver_register(&at91_gpio_driver
);
1855 return platform_driver_register(&at91_pinctrl_driver
);
1857 arch_initcall(at91_pinctrl_init
);
1859 static void __exit
at91_pinctrl_exit(void)
1861 platform_driver_unregister(&at91_pinctrl_driver
);
1864 module_exit(at91_pinctrl_exit
);
1865 MODULE_AUTHOR("Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>");
1866 MODULE_DESCRIPTION("Atmel AT91 pinctrl driver");
1867 MODULE_LICENSE("GPL v2");