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 if (!gpio_chips
[bank
])
326 return gpio_chips
[bank
]->regbase
;
329 static inline int pin_to_bank(unsigned pin
)
331 return pin
/= MAX_NB_GPIO_PER_BANK
;
334 static unsigned pin_to_mask(unsigned int pin
)
339 static unsigned two_bit_pin_value_shift_amount(unsigned int pin
)
341 /* return the shift value for a pin for "two bit" per pin registers,
342 * i.e. drive strength */
343 return 2*((pin
>= MAX_NB_GPIO_PER_BANK
/2)
344 ? pin
- MAX_NB_GPIO_PER_BANK
/2 : pin
);
347 static unsigned sama5d3_get_drive_register(unsigned int pin
)
349 /* drive strength is split between two registers
350 * with two bits per pin */
351 return (pin
>= MAX_NB_GPIO_PER_BANK
/2)
352 ? SAMA5D3_PIO_DRIVER2
: SAMA5D3_PIO_DRIVER1
;
355 static unsigned at91sam9x5_get_drive_register(unsigned int pin
)
357 /* drive strength is split between two registers
358 * with two bits per pin */
359 return (pin
>= MAX_NB_GPIO_PER_BANK
/2)
360 ? AT91SAM9X5_PIO_DRIVER2
: AT91SAM9X5_PIO_DRIVER1
;
363 static void at91_mux_disable_interrupt(void __iomem
*pio
, unsigned mask
)
365 writel_relaxed(mask
, pio
+ PIO_IDR
);
368 static unsigned at91_mux_get_pullup(void __iomem
*pio
, unsigned pin
)
370 return !((readl_relaxed(pio
+ PIO_PUSR
) >> pin
) & 0x1);
373 static void at91_mux_set_pullup(void __iomem
*pio
, unsigned mask
, bool on
)
376 writel_relaxed(mask
, pio
+ PIO_PPDDR
);
378 writel_relaxed(mask
, pio
+ (on
? PIO_PUER
: PIO_PUDR
));
381 static unsigned at91_mux_get_multidrive(void __iomem
*pio
, unsigned pin
)
383 return (readl_relaxed(pio
+ PIO_MDSR
) >> pin
) & 0x1;
386 static void at91_mux_set_multidrive(void __iomem
*pio
, unsigned mask
, bool on
)
388 writel_relaxed(mask
, pio
+ (on
? PIO_MDER
: PIO_MDDR
));
391 static void at91_mux_set_A_periph(void __iomem
*pio
, unsigned mask
)
393 writel_relaxed(mask
, pio
+ PIO_ASR
);
396 static void at91_mux_set_B_periph(void __iomem
*pio
, unsigned mask
)
398 writel_relaxed(mask
, pio
+ PIO_BSR
);
401 static void at91_mux_pio3_set_A_periph(void __iomem
*pio
, unsigned mask
)
404 writel_relaxed(readl_relaxed(pio
+ PIO_ABCDSR1
) & ~mask
,
406 writel_relaxed(readl_relaxed(pio
+ PIO_ABCDSR2
) & ~mask
,
410 static void at91_mux_pio3_set_B_periph(void __iomem
*pio
, unsigned mask
)
412 writel_relaxed(readl_relaxed(pio
+ PIO_ABCDSR1
) | mask
,
414 writel_relaxed(readl_relaxed(pio
+ PIO_ABCDSR2
) & ~mask
,
418 static void at91_mux_pio3_set_C_periph(void __iomem
*pio
, unsigned mask
)
420 writel_relaxed(readl_relaxed(pio
+ PIO_ABCDSR1
) & ~mask
, pio
+ PIO_ABCDSR1
);
421 writel_relaxed(readl_relaxed(pio
+ PIO_ABCDSR2
) | mask
, pio
+ PIO_ABCDSR2
);
424 static void at91_mux_pio3_set_D_periph(void __iomem
*pio
, unsigned mask
)
426 writel_relaxed(readl_relaxed(pio
+ PIO_ABCDSR1
) | mask
, pio
+ PIO_ABCDSR1
);
427 writel_relaxed(readl_relaxed(pio
+ PIO_ABCDSR2
) | mask
, pio
+ PIO_ABCDSR2
);
430 static enum at91_mux
at91_mux_pio3_get_periph(void __iomem
*pio
, unsigned mask
)
434 if (readl_relaxed(pio
+ PIO_PSR
) & mask
)
435 return AT91_MUX_GPIO
;
437 select
= !!(readl_relaxed(pio
+ PIO_ABCDSR1
) & mask
);
438 select
|= (!!(readl_relaxed(pio
+ PIO_ABCDSR2
) & mask
) << 1);
443 static enum at91_mux
at91_mux_get_periph(void __iomem
*pio
, unsigned mask
)
447 if (readl_relaxed(pio
+ PIO_PSR
) & mask
)
448 return AT91_MUX_GPIO
;
450 select
= readl_relaxed(pio
+ PIO_ABSR
) & mask
;
455 static bool at91_mux_get_deglitch(void __iomem
*pio
, unsigned pin
)
457 return (readl_relaxed(pio
+ PIO_IFSR
) >> pin
) & 0x1;
460 static void at91_mux_set_deglitch(void __iomem
*pio
, unsigned mask
, bool is_on
)
462 writel_relaxed(mask
, pio
+ (is_on
? PIO_IFER
: PIO_IFDR
));
465 static bool at91_mux_pio3_get_deglitch(void __iomem
*pio
, unsigned pin
)
467 if ((readl_relaxed(pio
+ PIO_IFSR
) >> pin
) & 0x1)
468 return !((readl_relaxed(pio
+ PIO_IFSCSR
) >> pin
) & 0x1);
473 static void at91_mux_pio3_set_deglitch(void __iomem
*pio
, unsigned mask
, bool is_on
)
476 writel_relaxed(mask
, pio
+ PIO_IFSCDR
);
477 at91_mux_set_deglitch(pio
, mask
, is_on
);
480 static bool at91_mux_pio3_get_debounce(void __iomem
*pio
, unsigned pin
, u32
*div
)
482 *div
= readl_relaxed(pio
+ PIO_SCDR
);
484 return ((readl_relaxed(pio
+ PIO_IFSR
) >> pin
) & 0x1) &&
485 ((readl_relaxed(pio
+ PIO_IFSCSR
) >> pin
) & 0x1);
488 static void at91_mux_pio3_set_debounce(void __iomem
*pio
, unsigned mask
,
492 writel_relaxed(mask
, pio
+ PIO_IFSCER
);
493 writel_relaxed(div
& PIO_SCDR_DIV
, pio
+ PIO_SCDR
);
494 writel_relaxed(mask
, pio
+ PIO_IFER
);
496 writel_relaxed(mask
, pio
+ PIO_IFSCDR
);
499 static bool at91_mux_pio3_get_pulldown(void __iomem
*pio
, unsigned pin
)
501 return !((readl_relaxed(pio
+ PIO_PPDSR
) >> pin
) & 0x1);
504 static void at91_mux_pio3_set_pulldown(void __iomem
*pio
, unsigned mask
, bool is_on
)
507 writel_relaxed(mask
, pio
+ PIO_PUDR
);
509 writel_relaxed(mask
, pio
+ (is_on
? PIO_PPDER
: PIO_PPDDR
));
512 static void at91_mux_pio3_disable_schmitt_trig(void __iomem
*pio
, unsigned mask
)
514 writel_relaxed(readl_relaxed(pio
+ PIO_SCHMITT
) | mask
, pio
+ PIO_SCHMITT
);
517 static bool at91_mux_pio3_get_schmitt_trig(void __iomem
*pio
, unsigned pin
)
519 return (readl_relaxed(pio
+ PIO_SCHMITT
) >> pin
) & 0x1;
522 static inline u32
read_drive_strength(void __iomem
*reg
, unsigned pin
)
524 unsigned tmp
= readl_relaxed(reg
);
526 tmp
= tmp
>> two_bit_pin_value_shift_amount(pin
);
528 return tmp
& DRIVE_STRENGTH_MASK
;
531 static unsigned at91_mux_sama5d3_get_drivestrength(void __iomem
*pio
,
534 unsigned tmp
= read_drive_strength(pio
+
535 sama5d3_get_drive_register(pin
), pin
);
537 /* SAMA5 strength is 1:1 with our defines,
538 * except 0 is equivalent to low per datasheet */
540 tmp
= DRIVE_STRENGTH_LOW
;
545 static unsigned at91_mux_sam9x5_get_drivestrength(void __iomem
*pio
,
548 unsigned tmp
= read_drive_strength(pio
+
549 at91sam9x5_get_drive_register(pin
), pin
);
551 /* strength is inverse in SAM9x5s hardware with the pinctrl defines
552 * hardware: 0 = hi, 1 = med, 2 = low, 3 = rsvd */
553 tmp
= DRIVE_STRENGTH_HI
- tmp
;
558 static void set_drive_strength(void __iomem
*reg
, unsigned pin
, u32 strength
)
560 unsigned tmp
= readl_relaxed(reg
);
561 unsigned shift
= two_bit_pin_value_shift_amount(pin
);
563 tmp
&= ~(DRIVE_STRENGTH_MASK
<< shift
);
564 tmp
|= strength
<< shift
;
566 writel_relaxed(tmp
, reg
);
569 static void at91_mux_sama5d3_set_drivestrength(void __iomem
*pio
, unsigned pin
,
572 /* do nothing if setting is zero */
576 /* strength is 1 to 1 with setting for SAMA5 */
577 set_drive_strength(pio
+ sama5d3_get_drive_register(pin
), pin
, setting
);
580 static void at91_mux_sam9x5_set_drivestrength(void __iomem
*pio
, unsigned pin
,
583 /* do nothing if setting is zero */
587 /* strength is inverse on SAM9x5s with our defines
588 * 0 = hi, 1 = med, 2 = low, 3 = rsvd */
589 setting
= DRIVE_STRENGTH_HI
- setting
;
591 set_drive_strength(pio
+ at91sam9x5_get_drive_register(pin
), pin
,
595 static struct at91_pinctrl_mux_ops at91rm9200_ops
= {
596 .get_periph
= at91_mux_get_periph
,
597 .mux_A_periph
= at91_mux_set_A_periph
,
598 .mux_B_periph
= at91_mux_set_B_periph
,
599 .get_deglitch
= at91_mux_get_deglitch
,
600 .set_deglitch
= at91_mux_set_deglitch
,
601 .irq_type
= gpio_irq_type
,
604 static struct at91_pinctrl_mux_ops at91sam9x5_ops
= {
605 .get_periph
= at91_mux_pio3_get_periph
,
606 .mux_A_periph
= at91_mux_pio3_set_A_periph
,
607 .mux_B_periph
= at91_mux_pio3_set_B_periph
,
608 .mux_C_periph
= at91_mux_pio3_set_C_periph
,
609 .mux_D_periph
= at91_mux_pio3_set_D_periph
,
610 .get_deglitch
= at91_mux_pio3_get_deglitch
,
611 .set_deglitch
= at91_mux_pio3_set_deglitch
,
612 .get_debounce
= at91_mux_pio3_get_debounce
,
613 .set_debounce
= at91_mux_pio3_set_debounce
,
614 .get_pulldown
= at91_mux_pio3_get_pulldown
,
615 .set_pulldown
= at91_mux_pio3_set_pulldown
,
616 .get_schmitt_trig
= at91_mux_pio3_get_schmitt_trig
,
617 .disable_schmitt_trig
= at91_mux_pio3_disable_schmitt_trig
,
618 .get_drivestrength
= at91_mux_sam9x5_get_drivestrength
,
619 .set_drivestrength
= at91_mux_sam9x5_set_drivestrength
,
620 .irq_type
= alt_gpio_irq_type
,
623 static struct at91_pinctrl_mux_ops sama5d3_ops
= {
624 .get_periph
= at91_mux_pio3_get_periph
,
625 .mux_A_periph
= at91_mux_pio3_set_A_periph
,
626 .mux_B_periph
= at91_mux_pio3_set_B_periph
,
627 .mux_C_periph
= at91_mux_pio3_set_C_periph
,
628 .mux_D_periph
= at91_mux_pio3_set_D_periph
,
629 .get_deglitch
= at91_mux_pio3_get_deglitch
,
630 .set_deglitch
= at91_mux_pio3_set_deglitch
,
631 .get_debounce
= at91_mux_pio3_get_debounce
,
632 .set_debounce
= at91_mux_pio3_set_debounce
,
633 .get_pulldown
= at91_mux_pio3_get_pulldown
,
634 .set_pulldown
= at91_mux_pio3_set_pulldown
,
635 .get_schmitt_trig
= at91_mux_pio3_get_schmitt_trig
,
636 .disable_schmitt_trig
= at91_mux_pio3_disable_schmitt_trig
,
637 .get_drivestrength
= at91_mux_sama5d3_get_drivestrength
,
638 .set_drivestrength
= at91_mux_sama5d3_set_drivestrength
,
639 .irq_type
= alt_gpio_irq_type
,
642 static void at91_pin_dbg(const struct device
*dev
, const struct at91_pmx_pin
*pin
)
645 dev_dbg(dev
, "pio%c%d configured as periph%c with conf = 0x%lx\n",
646 pin
->bank
+ 'A', pin
->pin
, pin
->mux
- 1 + 'A', pin
->conf
);
648 dev_dbg(dev
, "pio%c%d configured as gpio with conf = 0x%lx\n",
649 pin
->bank
+ 'A', pin
->pin
, pin
->conf
);
653 static int pin_check_config(struct at91_pinctrl
*info
, const char *name
,
654 int index
, const struct at91_pmx_pin
*pin
)
658 /* check if it's a valid config */
659 if (pin
->bank
>= gpio_banks
) {
660 dev_err(info
->dev
, "%s: pin conf %d bank_id %d >= nbanks %d\n",
661 name
, index
, pin
->bank
, gpio_banks
);
665 if (!gpio_chips
[pin
->bank
]) {
666 dev_err(info
->dev
, "%s: pin conf %d bank_id %d not enabled\n",
667 name
, index
, pin
->bank
);
671 if (pin
->pin
>= MAX_NB_GPIO_PER_BANK
) {
672 dev_err(info
->dev
, "%s: pin conf %d pin_bank_id %d >= %d\n",
673 name
, index
, pin
->pin
, MAX_NB_GPIO_PER_BANK
);
682 if (mux
>= info
->nmux
) {
683 dev_err(info
->dev
, "%s: pin conf %d mux_id %d >= nmux %d\n",
684 name
, index
, mux
, info
->nmux
);
688 if (!(info
->mux_mask
[pin
->bank
* info
->nmux
+ mux
] & 1 << pin
->pin
)) {
689 dev_err(info
->dev
, "%s: pin conf %d mux_id %d not supported for pio%c%d\n",
690 name
, index
, mux
, pin
->bank
+ 'A', pin
->pin
);
697 static void at91_mux_gpio_disable(void __iomem
*pio
, unsigned mask
)
699 writel_relaxed(mask
, pio
+ PIO_PDR
);
702 static void at91_mux_gpio_enable(void __iomem
*pio
, unsigned mask
, bool input
)
704 writel_relaxed(mask
, pio
+ PIO_PER
);
705 writel_relaxed(mask
, pio
+ (input
? PIO_ODR
: PIO_OER
));
708 static int at91_pmx_set(struct pinctrl_dev
*pctldev
, unsigned selector
,
711 struct at91_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
712 const struct at91_pmx_pin
*pins_conf
= info
->groups
[group
].pins_conf
;
713 const struct at91_pmx_pin
*pin
;
714 uint32_t npins
= info
->groups
[group
].npins
;
719 dev_dbg(info
->dev
, "enable function %s group %s\n",
720 info
->functions
[selector
].name
, info
->groups
[group
].name
);
722 /* first check that all the pins of the group are valid with a valid
724 for (i
= 0; i
< npins
; i
++) {
726 ret
= pin_check_config(info
, info
->groups
[group
].name
, i
, pin
);
731 for (i
= 0; i
< npins
; i
++) {
733 at91_pin_dbg(info
->dev
, pin
);
734 pio
= pin_to_controller(info
, pin
->bank
);
739 mask
= pin_to_mask(pin
->pin
);
740 at91_mux_disable_interrupt(pio
, mask
);
743 at91_mux_gpio_enable(pio
, mask
, 1);
745 case AT91_MUX_PERIPH_A
:
746 info
->ops
->mux_A_periph(pio
, mask
);
748 case AT91_MUX_PERIPH_B
:
749 info
->ops
->mux_B_periph(pio
, mask
);
751 case AT91_MUX_PERIPH_C
:
752 if (!info
->ops
->mux_C_periph
)
754 info
->ops
->mux_C_periph(pio
, mask
);
756 case AT91_MUX_PERIPH_D
:
757 if (!info
->ops
->mux_D_periph
)
759 info
->ops
->mux_D_periph(pio
, mask
);
763 at91_mux_gpio_disable(pio
, mask
);
769 static int at91_pmx_get_funcs_count(struct pinctrl_dev
*pctldev
)
771 struct at91_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
773 return info
->nfunctions
;
776 static const char *at91_pmx_get_func_name(struct pinctrl_dev
*pctldev
,
779 struct at91_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
781 return info
->functions
[selector
].name
;
784 static int at91_pmx_get_groups(struct pinctrl_dev
*pctldev
, unsigned selector
,
785 const char * const **groups
,
786 unsigned * const num_groups
)
788 struct at91_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
790 *groups
= info
->functions
[selector
].groups
;
791 *num_groups
= info
->functions
[selector
].ngroups
;
796 static int at91_gpio_request_enable(struct pinctrl_dev
*pctldev
,
797 struct pinctrl_gpio_range
*range
,
800 struct at91_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
801 struct at91_gpio_chip
*at91_chip
;
802 struct gpio_chip
*chip
;
806 dev_err(npct
->dev
, "invalid range\n");
810 dev_err(npct
->dev
, "missing GPIO chip in range\n");
814 at91_chip
= container_of(chip
, struct at91_gpio_chip
, chip
);
816 dev_dbg(npct
->dev
, "enable pin %u as GPIO\n", offset
);
818 mask
= 1 << (offset
- chip
->base
);
820 dev_dbg(npct
->dev
, "enable pin %u as PIO%c%d 0x%x\n",
821 offset
, 'A' + range
->id
, offset
- chip
->base
, mask
);
823 writel_relaxed(mask
, at91_chip
->regbase
+ PIO_PER
);
828 static void at91_gpio_disable_free(struct pinctrl_dev
*pctldev
,
829 struct pinctrl_gpio_range
*range
,
832 struct at91_pinctrl
*npct
= pinctrl_dev_get_drvdata(pctldev
);
834 dev_dbg(npct
->dev
, "disable pin %u as GPIO\n", offset
);
835 /* Set the pin to some default state, GPIO is usually default */
838 static const struct pinmux_ops at91_pmx_ops
= {
839 .get_functions_count
= at91_pmx_get_funcs_count
,
840 .get_function_name
= at91_pmx_get_func_name
,
841 .get_function_groups
= at91_pmx_get_groups
,
842 .set_mux
= at91_pmx_set
,
843 .gpio_request_enable
= at91_gpio_request_enable
,
844 .gpio_disable_free
= at91_gpio_disable_free
,
847 static int at91_pinconf_get(struct pinctrl_dev
*pctldev
,
848 unsigned pin_id
, unsigned long *config
)
850 struct at91_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
856 dev_dbg(info
->dev
, "%s:%d, pin_id=%d", __func__
, __LINE__
, pin_id
);
857 pio
= pin_to_controller(info
, pin_to_bank(pin_id
));
862 pin
= pin_id
% MAX_NB_GPIO_PER_BANK
;
864 if (at91_mux_get_multidrive(pio
, pin
))
865 *config
|= MULTI_DRIVE
;
867 if (at91_mux_get_pullup(pio
, pin
))
870 if (info
->ops
->get_deglitch
&& info
->ops
->get_deglitch(pio
, pin
))
872 if (info
->ops
->get_debounce
&& info
->ops
->get_debounce(pio
, pin
, &div
))
873 *config
|= DEBOUNCE
| (div
<< DEBOUNCE_VAL_SHIFT
);
874 if (info
->ops
->get_pulldown
&& info
->ops
->get_pulldown(pio
, pin
))
875 *config
|= PULL_DOWN
;
876 if (info
->ops
->get_schmitt_trig
&& info
->ops
->get_schmitt_trig(pio
, pin
))
877 *config
|= DIS_SCHMIT
;
878 if (info
->ops
->get_drivestrength
)
879 *config
|= (info
->ops
->get_drivestrength(pio
, pin
)
880 << DRIVE_STRENGTH_SHIFT
);
885 static int at91_pinconf_set(struct pinctrl_dev
*pctldev
,
886 unsigned pin_id
, unsigned long *configs
,
887 unsigned num_configs
)
889 struct at91_pinctrl
*info
= pinctrl_dev_get_drvdata(pctldev
);
893 unsigned long config
;
896 for (i
= 0; i
< num_configs
; i
++) {
900 "%s:%d, pin_id=%d, config=0x%lx",
901 __func__
, __LINE__
, pin_id
, config
);
902 pio
= pin_to_controller(info
, pin_to_bank(pin_id
));
907 pin
= pin_id
% MAX_NB_GPIO_PER_BANK
;
908 mask
= pin_to_mask(pin
);
910 if (config
& PULL_UP
&& config
& PULL_DOWN
)
913 at91_mux_set_pullup(pio
, mask
, config
& PULL_UP
);
914 at91_mux_set_multidrive(pio
, mask
, config
& MULTI_DRIVE
);
915 if (info
->ops
->set_deglitch
)
916 info
->ops
->set_deglitch(pio
, mask
, config
& DEGLITCH
);
917 if (info
->ops
->set_debounce
)
918 info
->ops
->set_debounce(pio
, mask
, config
& DEBOUNCE
,
919 (config
& DEBOUNCE_VAL
) >> DEBOUNCE_VAL_SHIFT
);
920 if (info
->ops
->set_pulldown
)
921 info
->ops
->set_pulldown(pio
, mask
, config
& PULL_DOWN
);
922 if (info
->ops
->disable_schmitt_trig
&& config
& DIS_SCHMIT
)
923 info
->ops
->disable_schmitt_trig(pio
, mask
);
924 if (info
->ops
->set_drivestrength
)
925 info
->ops
->set_drivestrength(pio
, pin
,
926 (config
& DRIVE_STRENGTH
)
927 >> DRIVE_STRENGTH_SHIFT
);
929 } /* for each config */
934 #define DBG_SHOW_FLAG(flag) do { \
935 if (config & flag) { \
938 seq_puts(s, #flag); \
943 #define DBG_SHOW_FLAG_MASKED(mask,flag) do { \
944 if ((config & mask) == flag) { \
947 seq_puts(s, #flag); \
952 static void at91_pinconf_dbg_show(struct pinctrl_dev
*pctldev
,
953 struct seq_file
*s
, unsigned pin_id
)
955 unsigned long config
;
956 int val
, num_conf
= 0;
958 at91_pinconf_get(pctldev
, pin_id
, &config
);
960 DBG_SHOW_FLAG(MULTI_DRIVE
);
961 DBG_SHOW_FLAG(PULL_UP
);
962 DBG_SHOW_FLAG(PULL_DOWN
);
963 DBG_SHOW_FLAG(DIS_SCHMIT
);
964 DBG_SHOW_FLAG(DEGLITCH
);
965 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH
, DRIVE_STRENGTH_LOW
);
966 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH
, DRIVE_STRENGTH_MED
);
967 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH
, DRIVE_STRENGTH_HI
);
968 DBG_SHOW_FLAG(DEBOUNCE
);
969 if (config
& DEBOUNCE
) {
970 val
= config
>> DEBOUNCE_VAL_SHIFT
;
971 seq_printf(s
, "(%d)", val
);
977 static void at91_pinconf_group_dbg_show(struct pinctrl_dev
*pctldev
,
978 struct seq_file
*s
, unsigned group
)
982 static const struct pinconf_ops at91_pinconf_ops
= {
983 .pin_config_get
= at91_pinconf_get
,
984 .pin_config_set
= at91_pinconf_set
,
985 .pin_config_dbg_show
= at91_pinconf_dbg_show
,
986 .pin_config_group_dbg_show
= at91_pinconf_group_dbg_show
,
989 static struct pinctrl_desc at91_pinctrl_desc
= {
990 .pctlops
= &at91_pctrl_ops
,
991 .pmxops
= &at91_pmx_ops
,
992 .confops
= &at91_pinconf_ops
,
993 .owner
= THIS_MODULE
,
996 static const char *gpio_compat
= "atmel,at91rm9200-gpio";
998 static void at91_pinctrl_child_count(struct at91_pinctrl
*info
,
999 struct device_node
*np
)
1001 struct device_node
*child
;
1003 for_each_child_of_node(np
, child
) {
1004 if (of_device_is_compatible(child
, gpio_compat
)) {
1005 if (of_device_is_available(child
))
1006 info
->nactive_banks
++;
1009 info
->ngroups
+= of_get_child_count(child
);
1014 static int at91_pinctrl_mux_mask(struct at91_pinctrl
*info
,
1015 struct device_node
*np
)
1021 list
= of_get_property(np
, "atmel,mux-mask", &size
);
1023 dev_err(info
->dev
, "can not read the mux-mask of %d\n", size
);
1027 size
/= sizeof(*list
);
1028 if (!size
|| size
% gpio_banks
) {
1029 dev_err(info
->dev
, "wrong mux mask array should be by %d\n", gpio_banks
);
1032 info
->nmux
= size
/ gpio_banks
;
1034 info
->mux_mask
= devm_kzalloc(info
->dev
, sizeof(u32
) * size
, GFP_KERNEL
);
1035 if (!info
->mux_mask
) {
1036 dev_err(info
->dev
, "could not alloc mux_mask\n");
1040 ret
= of_property_read_u32_array(np
, "atmel,mux-mask",
1041 info
->mux_mask
, size
);
1043 dev_err(info
->dev
, "can not read the mux-mask of %d\n", size
);
1047 static int at91_pinctrl_parse_groups(struct device_node
*np
,
1048 struct at91_pin_group
*grp
,
1049 struct at91_pinctrl
*info
, u32 index
)
1051 struct at91_pmx_pin
*pin
;
1056 dev_dbg(info
->dev
, "group(%d): %s\n", index
, np
->name
);
1058 /* Initialise group */
1059 grp
->name
= np
->name
;
1062 * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
1063 * do sanity check and calculate pins number
1065 list
= of_get_property(np
, "atmel,pins", &size
);
1066 /* we do not check return since it's safe node passed down */
1067 size
/= sizeof(*list
);
1068 if (!size
|| size
% 4) {
1069 dev_err(info
->dev
, "wrong pins number or pins and configs should be by 4\n");
1073 grp
->npins
= size
/ 4;
1074 pin
= grp
->pins_conf
= devm_kzalloc(info
->dev
, grp
->npins
* sizeof(struct at91_pmx_pin
),
1076 grp
->pins
= devm_kzalloc(info
->dev
, grp
->npins
* sizeof(unsigned int),
1078 if (!grp
->pins_conf
|| !grp
->pins
)
1081 for (i
= 0, j
= 0; i
< size
; i
+= 4, j
++) {
1082 pin
->bank
= be32_to_cpu(*list
++);
1083 pin
->pin
= be32_to_cpu(*list
++);
1084 grp
->pins
[j
] = pin
->bank
* MAX_NB_GPIO_PER_BANK
+ pin
->pin
;
1085 pin
->mux
= be32_to_cpu(*list
++);
1086 pin
->conf
= be32_to_cpu(*list
++);
1088 at91_pin_dbg(info
->dev
, pin
);
1095 static int at91_pinctrl_parse_functions(struct device_node
*np
,
1096 struct at91_pinctrl
*info
, u32 index
)
1098 struct device_node
*child
;
1099 struct at91_pmx_func
*func
;
1100 struct at91_pin_group
*grp
;
1102 static u32 grp_index
;
1105 dev_dbg(info
->dev
, "parse function(%d): %s\n", index
, np
->name
);
1107 func
= &info
->functions
[index
];
1109 /* Initialise function */
1110 func
->name
= np
->name
;
1111 func
->ngroups
= of_get_child_count(np
);
1112 if (func
->ngroups
== 0) {
1113 dev_err(info
->dev
, "no groups defined\n");
1116 func
->groups
= devm_kzalloc(info
->dev
,
1117 func
->ngroups
* sizeof(char *), GFP_KERNEL
);
1121 for_each_child_of_node(np
, child
) {
1122 func
->groups
[i
] = child
->name
;
1123 grp
= &info
->groups
[grp_index
++];
1124 ret
= at91_pinctrl_parse_groups(child
, grp
, info
, i
++);
1132 static const struct of_device_id at91_pinctrl_of_match
[] = {
1133 { .compatible
= "atmel,sama5d3-pinctrl", .data
= &sama5d3_ops
},
1134 { .compatible
= "atmel,at91sam9x5-pinctrl", .data
= &at91sam9x5_ops
},
1135 { .compatible
= "atmel,at91rm9200-pinctrl", .data
= &at91rm9200_ops
},
1139 static int at91_pinctrl_probe_dt(struct platform_device
*pdev
,
1140 struct at91_pinctrl
*info
)
1145 struct device_node
*np
= pdev
->dev
.of_node
;
1146 struct device_node
*child
;
1151 info
->dev
= &pdev
->dev
;
1152 info
->ops
= (struct at91_pinctrl_mux_ops
*)
1153 of_match_device(at91_pinctrl_of_match
, &pdev
->dev
)->data
;
1154 at91_pinctrl_child_count(info
, np
);
1156 if (gpio_banks
< 1) {
1157 dev_err(&pdev
->dev
, "you need to specify at least one gpio-controller\n");
1161 ret
= at91_pinctrl_mux_mask(info
, np
);
1165 dev_dbg(&pdev
->dev
, "nmux = %d\n", info
->nmux
);
1167 dev_dbg(&pdev
->dev
, "mux-mask\n");
1168 tmp
= info
->mux_mask
;
1169 for (i
= 0; i
< gpio_banks
; i
++) {
1170 for (j
= 0; j
< info
->nmux
; j
++, tmp
++) {
1171 dev_dbg(&pdev
->dev
, "%d:%d\t0x%x\n", i
, j
, tmp
[0]);
1175 dev_dbg(&pdev
->dev
, "nfunctions = %d\n", info
->nfunctions
);
1176 dev_dbg(&pdev
->dev
, "ngroups = %d\n", info
->ngroups
);
1177 info
->functions
= devm_kzalloc(&pdev
->dev
, info
->nfunctions
* sizeof(struct at91_pmx_func
),
1179 if (!info
->functions
)
1182 info
->groups
= devm_kzalloc(&pdev
->dev
, info
->ngroups
* sizeof(struct at91_pin_group
),
1187 dev_dbg(&pdev
->dev
, "nbanks = %d\n", gpio_banks
);
1188 dev_dbg(&pdev
->dev
, "nfunctions = %d\n", info
->nfunctions
);
1189 dev_dbg(&pdev
->dev
, "ngroups = %d\n", info
->ngroups
);
1193 for_each_child_of_node(np
, child
) {
1194 if (of_device_is_compatible(child
, gpio_compat
))
1196 ret
= at91_pinctrl_parse_functions(child
, info
, i
++);
1198 dev_err(&pdev
->dev
, "failed to parse function\n");
1206 static int at91_pinctrl_probe(struct platform_device
*pdev
)
1208 struct at91_pinctrl
*info
;
1209 struct pinctrl_pin_desc
*pdesc
;
1210 int ret
, i
, j
, k
, ngpio_chips_enabled
= 0;
1212 info
= devm_kzalloc(&pdev
->dev
, sizeof(*info
), GFP_KERNEL
);
1216 ret
= at91_pinctrl_probe_dt(pdev
, info
);
1221 * We need all the GPIO drivers to probe FIRST, or we will not be able
1222 * to obtain references to the struct gpio_chip * for them, and we
1223 * need this to proceed.
1225 for (i
= 0; i
< gpio_banks
; i
++)
1227 ngpio_chips_enabled
++;
1229 if (ngpio_chips_enabled
< info
->nactive_banks
) {
1230 dev_warn(&pdev
->dev
,
1231 "All GPIO chips are not registered yet (%d/%d)\n",
1232 ngpio_chips_enabled
, info
->nactive_banks
);
1233 devm_kfree(&pdev
->dev
, info
);
1234 return -EPROBE_DEFER
;
1237 at91_pinctrl_desc
.name
= dev_name(&pdev
->dev
);
1238 at91_pinctrl_desc
.npins
= gpio_banks
* MAX_NB_GPIO_PER_BANK
;
1239 at91_pinctrl_desc
.pins
= pdesc
=
1240 devm_kzalloc(&pdev
->dev
, sizeof(*pdesc
) * at91_pinctrl_desc
.npins
, GFP_KERNEL
);
1242 if (!at91_pinctrl_desc
.pins
)
1245 for (i
= 0, k
= 0; i
< gpio_banks
; i
++) {
1246 for (j
= 0; j
< MAX_NB_GPIO_PER_BANK
; j
++, k
++) {
1248 pdesc
->name
= kasprintf(GFP_KERNEL
, "pio%c%d", i
+ 'A', j
);
1253 platform_set_drvdata(pdev
, info
);
1254 info
->pctl
= pinctrl_register(&at91_pinctrl_desc
, &pdev
->dev
, info
);
1256 if (IS_ERR(info
->pctl
)) {
1257 dev_err(&pdev
->dev
, "could not register AT91 pinctrl driver\n");
1258 return PTR_ERR(info
->pctl
);
1261 /* We will handle a range of GPIO pins */
1262 for (i
= 0; i
< gpio_banks
; i
++)
1264 pinctrl_add_gpio_range(info
->pctl
, &gpio_chips
[i
]->range
);
1266 dev_info(&pdev
->dev
, "initialized AT91 pinctrl driver\n");
1271 static int at91_pinctrl_remove(struct platform_device
*pdev
)
1273 struct at91_pinctrl
*info
= platform_get_drvdata(pdev
);
1275 pinctrl_unregister(info
->pctl
);
1280 static int at91_gpio_request(struct gpio_chip
*chip
, unsigned offset
)
1283 * Map back to global GPIO space and request muxing, the direction
1284 * parameter does not matter for this controller.
1286 int gpio
= chip
->base
+ offset
;
1287 int bank
= chip
->base
/ chip
->ngpio
;
1289 dev_dbg(chip
->dev
, "%s:%d pio%c%d(%d)\n", __func__
, __LINE__
,
1290 'A' + bank
, offset
, gpio
);
1292 return pinctrl_request_gpio(gpio
);
1295 static void at91_gpio_free(struct gpio_chip
*chip
, unsigned offset
)
1297 int gpio
= chip
->base
+ offset
;
1299 pinctrl_free_gpio(gpio
);
1302 static int at91_gpio_get_direction(struct gpio_chip
*chip
, unsigned offset
)
1304 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
1305 void __iomem
*pio
= at91_gpio
->regbase
;
1306 unsigned mask
= 1 << offset
;
1309 osr
= readl_relaxed(pio
+ PIO_OSR
);
1310 return !(osr
& mask
);
1313 static int at91_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
1315 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
1316 void __iomem
*pio
= at91_gpio
->regbase
;
1317 unsigned mask
= 1 << offset
;
1319 writel_relaxed(mask
, pio
+ PIO_ODR
);
1323 static int at91_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
1325 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
1326 void __iomem
*pio
= at91_gpio
->regbase
;
1327 unsigned mask
= 1 << offset
;
1330 pdsr
= readl_relaxed(pio
+ PIO_PDSR
);
1331 return (pdsr
& mask
) != 0;
1334 static void at91_gpio_set(struct gpio_chip
*chip
, unsigned offset
,
1337 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
1338 void __iomem
*pio
= at91_gpio
->regbase
;
1339 unsigned mask
= 1 << offset
;
1341 writel_relaxed(mask
, pio
+ (val
? PIO_SODR
: PIO_CODR
));
1344 static void at91_gpio_set_multiple(struct gpio_chip
*chip
,
1345 unsigned long *mask
, unsigned long *bits
)
1347 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
1348 void __iomem
*pio
= at91_gpio
->regbase
;
1350 #define BITS_MASK(bits) (((bits) == 32) ? ~0U : (BIT(bits) - 1))
1351 /* Mask additionally to ngpio as not all GPIO controllers have 32 pins */
1352 uint32_t set_mask
= (*mask
& *bits
) & BITS_MASK(chip
->ngpio
);
1353 uint32_t clear_mask
= (*mask
& ~(*bits
)) & BITS_MASK(chip
->ngpio
);
1355 writel_relaxed(set_mask
, pio
+ PIO_SODR
);
1356 writel_relaxed(clear_mask
, pio
+ PIO_CODR
);
1359 static int at91_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
1362 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
1363 void __iomem
*pio
= at91_gpio
->regbase
;
1364 unsigned mask
= 1 << offset
;
1366 writel_relaxed(mask
, pio
+ (val
? PIO_SODR
: PIO_CODR
));
1367 writel_relaxed(mask
, pio
+ PIO_OER
);
1372 #ifdef CONFIG_DEBUG_FS
1373 static void at91_gpio_dbg_show(struct seq_file
*s
, struct gpio_chip
*chip
)
1377 struct at91_gpio_chip
*at91_gpio
= to_at91_gpio_chip(chip
);
1378 void __iomem
*pio
= at91_gpio
->regbase
;
1380 for (i
= 0; i
< chip
->ngpio
; i
++) {
1381 unsigned mask
= pin_to_mask(i
);
1382 const char *gpio_label
;
1384 gpio_label
= gpiochip_is_requested(chip
, i
);
1387 mode
= at91_gpio
->ops
->get_periph(pio
, mask
);
1388 seq_printf(s
, "[%s] GPIO%s%d: ",
1389 gpio_label
, chip
->label
, i
);
1390 if (mode
== AT91_MUX_GPIO
) {
1391 seq_printf(s
, "[gpio] ");
1392 seq_printf(s
, "%s ",
1393 readl_relaxed(pio
+ PIO_OSR
) & mask
?
1394 "output" : "input");
1395 seq_printf(s
, "%s\n",
1396 readl_relaxed(pio
+ PIO_PDSR
) & mask
?
1399 seq_printf(s
, "[periph %c]\n",
1405 #define at91_gpio_dbg_show NULL
1408 /* Several AIC controller irqs are dispatched through this GPIO handler.
1409 * To use any AT91_PIN_* as an externally triggered IRQ, first call
1410 * at91_set_gpio_input() then maybe enable its glitch filter.
1411 * Then just request_irq() with the pin ID; it works like any ARM IRQ
1413 * First implementation always triggers on rising and falling edges
1414 * whereas the newer PIO3 can be additionally configured to trigger on
1415 * level, edge with any polarity.
1417 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
1418 * configuring them with at91_set_a_periph() or at91_set_b_periph().
1419 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
1422 static void gpio_irq_mask(struct irq_data
*d
)
1424 struct at91_gpio_chip
*at91_gpio
= irq_data_get_irq_chip_data(d
);
1425 void __iomem
*pio
= at91_gpio
->regbase
;
1426 unsigned mask
= 1 << d
->hwirq
;
1429 writel_relaxed(mask
, pio
+ PIO_IDR
);
1432 static void gpio_irq_unmask(struct irq_data
*d
)
1434 struct at91_gpio_chip
*at91_gpio
= irq_data_get_irq_chip_data(d
);
1435 void __iomem
*pio
= at91_gpio
->regbase
;
1436 unsigned mask
= 1 << d
->hwirq
;
1439 writel_relaxed(mask
, pio
+ PIO_IER
);
1442 static int gpio_irq_type(struct irq_data
*d
, unsigned type
)
1446 case IRQ_TYPE_EDGE_BOTH
:
1453 /* Alternate irq type for PIO3 support */
1454 static int alt_gpio_irq_type(struct irq_data
*d
, unsigned type
)
1456 struct at91_gpio_chip
*at91_gpio
= irq_data_get_irq_chip_data(d
);
1457 void __iomem
*pio
= at91_gpio
->regbase
;
1458 unsigned mask
= 1 << d
->hwirq
;
1461 case IRQ_TYPE_EDGE_RISING
:
1462 irq_set_handler_locked(d
, handle_simple_irq
);
1463 writel_relaxed(mask
, pio
+ PIO_ESR
);
1464 writel_relaxed(mask
, pio
+ PIO_REHLSR
);
1466 case IRQ_TYPE_EDGE_FALLING
:
1467 irq_set_handler_locked(d
, handle_simple_irq
);
1468 writel_relaxed(mask
, pio
+ PIO_ESR
);
1469 writel_relaxed(mask
, pio
+ PIO_FELLSR
);
1471 case IRQ_TYPE_LEVEL_LOW
:
1472 irq_set_handler_locked(d
, handle_level_irq
);
1473 writel_relaxed(mask
, pio
+ PIO_LSR
);
1474 writel_relaxed(mask
, pio
+ PIO_FELLSR
);
1476 case IRQ_TYPE_LEVEL_HIGH
:
1477 irq_set_handler_locked(d
, handle_level_irq
);
1478 writel_relaxed(mask
, pio
+ PIO_LSR
);
1479 writel_relaxed(mask
, pio
+ PIO_REHLSR
);
1481 case IRQ_TYPE_EDGE_BOTH
:
1483 * disable additional interrupt modes:
1484 * fall back to default behavior
1486 irq_set_handler_locked(d
, handle_simple_irq
);
1487 writel_relaxed(mask
, pio
+ PIO_AIMDR
);
1491 pr_warn("AT91: No type for irq %d\n", gpio_to_irq(d
->irq
));
1495 /* enable additional interrupt modes */
1496 writel_relaxed(mask
, pio
+ PIO_AIMER
);
1501 static void gpio_irq_ack(struct irq_data
*d
)
1503 /* the interrupt is already cleared before by reading ISR */
1508 static u32 wakeups
[MAX_GPIO_BANKS
];
1509 static u32 backups
[MAX_GPIO_BANKS
];
1511 static int gpio_irq_set_wake(struct irq_data
*d
, unsigned state
)
1513 struct at91_gpio_chip
*at91_gpio
= irq_data_get_irq_chip_data(d
);
1514 unsigned bank
= at91_gpio
->pioc_idx
;
1515 unsigned mask
= 1 << d
->hwirq
;
1517 if (unlikely(bank
>= MAX_GPIO_BANKS
))
1521 wakeups
[bank
] |= mask
;
1523 wakeups
[bank
] &= ~mask
;
1525 irq_set_irq_wake(at91_gpio
->pioc_virq
, state
);
1530 void at91_pinctrl_gpio_suspend(void)
1534 for (i
= 0; i
< gpio_banks
; i
++) {
1540 pio
= gpio_chips
[i
]->regbase
;
1542 backups
[i
] = readl_relaxed(pio
+ PIO_IMR
);
1543 writel_relaxed(backups
[i
], pio
+ PIO_IDR
);
1544 writel_relaxed(wakeups
[i
], pio
+ PIO_IER
);
1547 clk_disable_unprepare(gpio_chips
[i
]->clock
);
1549 printk(KERN_DEBUG
"GPIO-%c may wake for %08x\n",
1554 void at91_pinctrl_gpio_resume(void)
1558 for (i
= 0; i
< gpio_banks
; i
++) {
1564 pio
= gpio_chips
[i
]->regbase
;
1567 clk_prepare_enable(gpio_chips
[i
]->clock
);
1569 writel_relaxed(wakeups
[i
], pio
+ PIO_IDR
);
1570 writel_relaxed(backups
[i
], pio
+ PIO_IER
);
1575 #define gpio_irq_set_wake NULL
1576 #endif /* CONFIG_PM */
1578 static struct irq_chip gpio_irqchip
= {
1580 .irq_ack
= gpio_irq_ack
,
1581 .irq_disable
= gpio_irq_mask
,
1582 .irq_mask
= gpio_irq_mask
,
1583 .irq_unmask
= gpio_irq_unmask
,
1584 /* .irq_set_type is set dynamically */
1585 .irq_set_wake
= gpio_irq_set_wake
,
1588 static void gpio_irq_handler(struct irq_desc
*desc
)
1590 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
1591 struct gpio_chip
*gpio_chip
= irq_desc_get_handler_data(desc
);
1592 struct at91_gpio_chip
*at91_gpio
= container_of(gpio_chip
,
1593 struct at91_gpio_chip
, chip
);
1595 void __iomem
*pio
= at91_gpio
->regbase
;
1599 chained_irq_enter(chip
, desc
);
1601 /* Reading ISR acks pending (edge triggered) GPIO interrupts.
1602 * When there are none pending, we're finished unless we need
1603 * to process multiple banks (like ID_PIOCDE on sam9263).
1605 isr
= readl_relaxed(pio
+ PIO_ISR
) & readl_relaxed(pio
+ PIO_IMR
);
1607 if (!at91_gpio
->next
)
1609 at91_gpio
= at91_gpio
->next
;
1610 pio
= at91_gpio
->regbase
;
1611 gpio_chip
= &at91_gpio
->chip
;
1615 for_each_set_bit(n
, &isr
, BITS_PER_LONG
) {
1616 generic_handle_irq(irq_find_mapping(
1617 gpio_chip
->irqdomain
, n
));
1620 chained_irq_exit(chip
, desc
);
1621 /* now it may re-trigger */
1624 static int at91_gpio_of_irq_setup(struct platform_device
*pdev
,
1625 struct at91_gpio_chip
*at91_gpio
)
1627 struct gpio_chip
*gpiochip_prev
= NULL
;
1628 struct at91_gpio_chip
*prev
= NULL
;
1629 struct irq_data
*d
= irq_get_irq_data(at91_gpio
->pioc_virq
);
1632 at91_gpio
->pioc_hwirq
= irqd_to_hwirq(d
);
1634 /* Setup proper .irq_set_type function */
1635 gpio_irqchip
.irq_set_type
= at91_gpio
->ops
->irq_type
;
1637 /* Disable irqs of this PIO controller */
1638 writel_relaxed(~0, at91_gpio
->regbase
+ PIO_IDR
);
1641 * Let the generic code handle this edge IRQ, the the chained
1642 * handler will perform the actual work of handling the parent
1645 ret
= gpiochip_irqchip_add(&at91_gpio
->chip
,
1649 IRQ_TYPE_EDGE_BOTH
);
1651 dev_err(&pdev
->dev
, "at91_gpio.%d: Couldn't add irqchip to gpiochip.\n",
1652 at91_gpio
->pioc_idx
);
1656 /* The top level handler handles one bank of GPIOs, except
1657 * on some SoC it can handle up to three...
1658 * We only set up the handler for the first of the list.
1660 gpiochip_prev
= irq_get_handler_data(at91_gpio
->pioc_virq
);
1661 if (!gpiochip_prev
) {
1662 /* Then register the chain on the parent IRQ */
1663 gpiochip_set_chained_irqchip(&at91_gpio
->chip
,
1665 at91_gpio
->pioc_virq
,
1670 prev
= container_of(gpiochip_prev
, struct at91_gpio_chip
, chip
);
1672 /* we can only have 2 banks before */
1673 for (i
= 0; i
< 2; i
++) {
1677 prev
->next
= at91_gpio
;
1685 /* This structure is replicated for each GPIO block allocated at probe time */
1686 static struct gpio_chip at91_gpio_template
= {
1687 .request
= at91_gpio_request
,
1688 .free
= at91_gpio_free
,
1689 .get_direction
= at91_gpio_get_direction
,
1690 .direction_input
= at91_gpio_direction_input
,
1691 .get
= at91_gpio_get
,
1692 .direction_output
= at91_gpio_direction_output
,
1693 .set
= at91_gpio_set
,
1694 .set_multiple
= at91_gpio_set_multiple
,
1695 .dbg_show
= at91_gpio_dbg_show
,
1697 .ngpio
= MAX_NB_GPIO_PER_BANK
,
1700 static const struct of_device_id at91_gpio_of_match
[] = {
1701 { .compatible
= "atmel,at91sam9x5-gpio", .data
= &at91sam9x5_ops
, },
1702 { .compatible
= "atmel,at91rm9200-gpio", .data
= &at91rm9200_ops
},
1706 static int at91_gpio_probe(struct platform_device
*pdev
)
1708 struct device_node
*np
= pdev
->dev
.of_node
;
1709 struct resource
*res
;
1710 struct at91_gpio_chip
*at91_chip
= NULL
;
1711 struct gpio_chip
*chip
;
1712 struct pinctrl_gpio_range
*range
;
1715 int alias_idx
= of_alias_get_id(np
, "gpio");
1719 BUG_ON(alias_idx
>= ARRAY_SIZE(gpio_chips
));
1720 if (gpio_chips
[alias_idx
]) {
1725 irq
= platform_get_irq(pdev
, 0);
1731 at91_chip
= devm_kzalloc(&pdev
->dev
, sizeof(*at91_chip
), GFP_KERNEL
);
1737 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1738 at91_chip
->regbase
= devm_ioremap_resource(&pdev
->dev
, res
);
1739 if (IS_ERR(at91_chip
->regbase
)) {
1740 ret
= PTR_ERR(at91_chip
->regbase
);
1744 at91_chip
->ops
= (struct at91_pinctrl_mux_ops
*)
1745 of_match_device(at91_gpio_of_match
, &pdev
->dev
)->data
;
1746 at91_chip
->pioc_virq
= irq
;
1747 at91_chip
->pioc_idx
= alias_idx
;
1749 at91_chip
->clock
= devm_clk_get(&pdev
->dev
, NULL
);
1750 if (IS_ERR(at91_chip
->clock
)) {
1751 dev_err(&pdev
->dev
, "failed to get clock, ignoring.\n");
1752 ret
= PTR_ERR(at91_chip
->clock
);
1756 ret
= clk_prepare(at91_chip
->clock
);
1758 goto clk_prepare_err
;
1760 /* enable PIO controller's clock */
1761 ret
= clk_enable(at91_chip
->clock
);
1763 dev_err(&pdev
->dev
, "failed to enable clock, ignoring.\n");
1764 goto clk_enable_err
;
1767 at91_chip
->chip
= at91_gpio_template
;
1769 chip
= &at91_chip
->chip
;
1771 chip
->label
= dev_name(&pdev
->dev
);
1772 chip
->dev
= &pdev
->dev
;
1773 chip
->owner
= THIS_MODULE
;
1774 chip
->base
= alias_idx
* MAX_NB_GPIO_PER_BANK
;
1776 if (!of_property_read_u32(np
, "#gpio-lines", &ngpio
)) {
1777 if (ngpio
>= MAX_NB_GPIO_PER_BANK
)
1778 pr_err("at91_gpio.%d, gpio-nb >= %d failback to %d\n",
1779 alias_idx
, MAX_NB_GPIO_PER_BANK
, MAX_NB_GPIO_PER_BANK
);
1781 chip
->ngpio
= ngpio
;
1784 names
= devm_kzalloc(&pdev
->dev
, sizeof(char *) * chip
->ngpio
,
1789 goto clk_enable_err
;
1792 for (i
= 0; i
< chip
->ngpio
; i
++)
1793 names
[i
] = kasprintf(GFP_KERNEL
, "pio%c%d", alias_idx
+ 'A', i
);
1795 chip
->names
= (const char *const *)names
;
1797 range
= &at91_chip
->range
;
1798 range
->name
= chip
->label
;
1799 range
->id
= alias_idx
;
1800 range
->pin_base
= range
->base
= range
->id
* MAX_NB_GPIO_PER_BANK
;
1802 range
->npins
= chip
->ngpio
;
1805 ret
= gpiochip_add(chip
);
1807 goto gpiochip_add_err
;
1809 gpio_chips
[alias_idx
] = at91_chip
;
1810 gpio_banks
= max(gpio_banks
, alias_idx
+ 1);
1812 ret
= at91_gpio_of_irq_setup(pdev
, at91_chip
);
1816 dev_info(&pdev
->dev
, "at address %p\n", at91_chip
->regbase
);
1821 gpiochip_remove(chip
);
1823 clk_disable(at91_chip
->clock
);
1825 clk_unprepare(at91_chip
->clock
);
1828 dev_err(&pdev
->dev
, "Failure %i for GPIO %i\n", ret
, alias_idx
);
1833 static struct platform_driver at91_gpio_driver
= {
1835 .name
= "gpio-at91",
1836 .of_match_table
= at91_gpio_of_match
,
1838 .probe
= at91_gpio_probe
,
1841 static struct platform_driver at91_pinctrl_driver
= {
1843 .name
= "pinctrl-at91",
1844 .of_match_table
= at91_pinctrl_of_match
,
1846 .probe
= at91_pinctrl_probe
,
1847 .remove
= at91_pinctrl_remove
,
1850 static int __init
at91_pinctrl_init(void)
1854 ret
= platform_driver_register(&at91_gpio_driver
);
1857 return platform_driver_register(&at91_pinctrl_driver
);
1859 arch_initcall(at91_pinctrl_init
);
1861 static void __exit
at91_pinctrl_exit(void)
1863 platform_driver_unregister(&at91_pinctrl_driver
);
1866 module_exit(at91_pinctrl_exit
);
1867 MODULE_AUTHOR("Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>");
1868 MODULE_DESCRIPTION("Atmel AT91 pinctrl driver");
1869 MODULE_LICENSE("GPL v2");