1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for the Atmel PIO4 controller
5 * Copyright (C) 2015 Atmel,
6 * 2015 Ludovic Desroches <ludovic.desroches@atmel.com>
9 #include <dt-bindings/pinctrl/at91.h>
11 #include <linux/clk.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
17 #include <linux/platform_device.h>
18 #include <linux/seq_file.h>
19 #include <linux/slab.h>
21 #include <linux/pinctrl/pinconf-generic.h>
22 #include <linux/pinctrl/pinconf.h>
23 #include <linux/pinctrl/pinctrl.h>
24 #include <linux/pinctrl/pinmux.h>
28 #include "pinctrl-utils.h"
32 * In order to not introduce confusion between Atmel PIO groups and pinctrl
33 * framework groups, Atmel PIO groups will be called banks, line is kept to
34 * designed the pin id into this bank.
37 #define ATMEL_PIO_MSKR 0x0000
38 #define ATMEL_PIO_CFGR 0x0004
39 #define ATMEL_PIO_CFGR_FUNC_MASK GENMASK(2, 0)
40 #define ATMEL_PIO_DIR_MASK BIT(8)
41 #define ATMEL_PIO_PUEN_MASK BIT(9)
42 #define ATMEL_PIO_PDEN_MASK BIT(10)
43 #define ATMEL_PIO_SR_MASK BIT(11)
44 #define ATMEL_PIO_IFEN_MASK BIT(12)
45 #define ATMEL_PIO_IFSCEN_MASK BIT(13)
46 #define ATMEL_PIO_OPD_MASK BIT(14)
47 #define ATMEL_PIO_SCHMITT_MASK BIT(15)
48 #define ATMEL_PIO_DRVSTR_MASK GENMASK(17, 16)
49 #define ATMEL_PIO_DRVSTR_OFFSET 16
50 #define ATMEL_PIO_CFGR_EVTSEL_MASK GENMASK(26, 24)
51 #define ATMEL_PIO_CFGR_EVTSEL_FALLING (0 << 24)
52 #define ATMEL_PIO_CFGR_EVTSEL_RISING (1 << 24)
53 #define ATMEL_PIO_CFGR_EVTSEL_BOTH (2 << 24)
54 #define ATMEL_PIO_CFGR_EVTSEL_LOW (3 << 24)
55 #define ATMEL_PIO_CFGR_EVTSEL_HIGH (4 << 24)
56 #define ATMEL_PIO_PDSR 0x0008
57 #define ATMEL_PIO_LOCKSR 0x000C
58 #define ATMEL_PIO_SODR 0x0010
59 #define ATMEL_PIO_CODR 0x0014
60 #define ATMEL_PIO_ODSR 0x0018
61 #define ATMEL_PIO_IER 0x0020
62 #define ATMEL_PIO_IDR 0x0024
63 #define ATMEL_PIO_IMR 0x0028
64 #define ATMEL_PIO_ISR 0x002C
65 #define ATMEL_PIO_IOFR 0x003C
67 #define ATMEL_PIO_NPINS_PER_BANK 32
68 #define ATMEL_PIO_BANK(pin_id) (pin_id / ATMEL_PIO_NPINS_PER_BANK)
69 #define ATMEL_PIO_LINE(pin_id) (pin_id % ATMEL_PIO_NPINS_PER_BANK)
70 #define ATMEL_PIO_BANK_OFFSET 0x40
72 #define ATMEL_GET_PIN_NO(pinfunc) ((pinfunc) & 0xff)
73 #define ATMEL_GET_PIN_FUNC(pinfunc) ((pinfunc >> 16) & 0xf)
74 #define ATMEL_GET_PIN_IOSET(pinfunc) ((pinfunc >> 20) & 0xf)
76 /* Custom pinconf parameters */
77 #define ATMEL_PIN_CONFIG_DRIVE_STRENGTH (PIN_CONFIG_END + 1)
80 * struct atmel_pioctrl_data - Atmel PIO controller (pinmux + gpio) data struct
81 * @nbanks: number of PIO banks
82 * @last_bank_count: number of lines in the last bank (can be less than
83 * the rest of the banks).
84 * @slew_rate_support: slew rate support
86 struct atmel_pioctrl_data
{
88 unsigned int last_bank_count
;
89 unsigned int slew_rate_support
;
107 * struct atmel_pioctrl - Atmel PIO controller (pinmux + gpio)
108 * @reg_base: base address of the controller.
109 * @clk: clock of the controller.
110 * @nbanks: number of PIO groups, it can vary depending on the SoC.
111 * @pinctrl_dev: pinctrl device registered.
112 * @groups: groups table to provide group name and pin in the group to pinctrl.
113 * @group_names: group names table to provide all the group/pin names to
115 * @pins: pins table used for both pinctrl and gpio. pin_id, bank and line
116 * fields are set at probe time. Other ones are set when parsing dt
118 * @npins: number of pins.
119 * @gpio_chip: gpio chip registered.
120 * @irq_domain: irq domain for the gpio controller.
121 * @irqs: table containing the hw irq number of the bank. The index of the
122 * table is the bank id.
123 * @pm_wakeup_sources: bitmap of wakeup sources (lines)
124 * @pm_suspend_backup: backup/restore register values on suspend/resume
125 * @dev: device entry for the Atmel PIO controller.
126 * @node: node of the Atmel PIO controller.
127 * @slew_rate_support: slew rate support
129 struct atmel_pioctrl
{
130 void __iomem
*reg_base
;
133 struct pinctrl_dev
*pinctrl_dev
;
134 struct atmel_group
*groups
;
135 const char * const *group_names
;
136 struct atmel_pin
**pins
;
138 struct gpio_chip
*gpio_chip
;
139 struct irq_domain
*irq_domain
;
141 unsigned int *pm_wakeup_sources
;
145 u32 cfgr
[ATMEL_PIO_NPINS_PER_BANK
];
146 } *pm_suspend_backup
;
148 struct device_node
*node
;
149 unsigned int slew_rate_support
;
152 static const char * const atmel_functions
[] = {
153 "GPIO", "A", "B", "C", "D", "E", "F", "G"
156 static const struct pinconf_generic_params atmel_custom_bindings
[] = {
157 {"atmel,drive-strength", ATMEL_PIN_CONFIG_DRIVE_STRENGTH
, 0},
161 static unsigned int atmel_gpio_read(struct atmel_pioctrl
*atmel_pioctrl
,
162 unsigned int bank
, unsigned int reg
)
164 return readl_relaxed(atmel_pioctrl
->reg_base
165 + ATMEL_PIO_BANK_OFFSET
* bank
+ reg
);
168 static void atmel_gpio_write(struct atmel_pioctrl
*atmel_pioctrl
,
169 unsigned int bank
, unsigned int reg
,
172 writel_relaxed(val
, atmel_pioctrl
->reg_base
173 + ATMEL_PIO_BANK_OFFSET
* bank
+ reg
);
176 static void atmel_gpio_irq_ack(struct irq_data
*d
)
179 * Nothing to do, interrupt is cleared when reading the status
184 static int atmel_gpio_irq_set_type(struct irq_data
*d
, unsigned int type
)
186 struct atmel_pioctrl
*atmel_pioctrl
= irq_data_get_irq_chip_data(d
);
187 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[d
->hwirq
];
190 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_MSKR
,
192 reg
= atmel_gpio_read(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
);
193 reg
&= (~ATMEL_PIO_CFGR_EVTSEL_MASK
);
196 case IRQ_TYPE_EDGE_RISING
:
197 irq_set_handler_locked(d
, handle_edge_irq
);
198 reg
|= ATMEL_PIO_CFGR_EVTSEL_RISING
;
200 case IRQ_TYPE_EDGE_FALLING
:
201 irq_set_handler_locked(d
, handle_edge_irq
);
202 reg
|= ATMEL_PIO_CFGR_EVTSEL_FALLING
;
204 case IRQ_TYPE_EDGE_BOTH
:
205 irq_set_handler_locked(d
, handle_edge_irq
);
206 reg
|= ATMEL_PIO_CFGR_EVTSEL_BOTH
;
208 case IRQ_TYPE_LEVEL_LOW
:
209 irq_set_handler_locked(d
, handle_level_irq
);
210 reg
|= ATMEL_PIO_CFGR_EVTSEL_LOW
;
212 case IRQ_TYPE_LEVEL_HIGH
:
213 irq_set_handler_locked(d
, handle_level_irq
);
214 reg
|= ATMEL_PIO_CFGR_EVTSEL_HIGH
;
221 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
, reg
);
226 static void atmel_gpio_irq_mask(struct irq_data
*d
)
228 struct atmel_pioctrl
*atmel_pioctrl
= irq_data_get_irq_chip_data(d
);
229 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[d
->hwirq
];
231 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_IDR
,
235 static void atmel_gpio_irq_unmask(struct irq_data
*d
)
237 struct atmel_pioctrl
*atmel_pioctrl
= irq_data_get_irq_chip_data(d
);
238 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[d
->hwirq
];
240 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_IER
,
244 static int atmel_gpio_irq_set_wake(struct irq_data
*d
, unsigned int on
)
246 struct atmel_pioctrl
*atmel_pioctrl
= irq_data_get_irq_chip_data(d
);
247 int bank
= ATMEL_PIO_BANK(d
->hwirq
);
248 int line
= ATMEL_PIO_LINE(d
->hwirq
);
250 /* The gpio controller has one interrupt line per bank. */
251 irq_set_irq_wake(atmel_pioctrl
->irqs
[bank
], on
);
254 atmel_pioctrl
->pm_wakeup_sources
[bank
] |= BIT(line
);
256 atmel_pioctrl
->pm_wakeup_sources
[bank
] &= ~(BIT(line
));
261 static struct irq_chip atmel_gpio_irq_chip
= {
263 .irq_ack
= atmel_gpio_irq_ack
,
264 .irq_mask
= atmel_gpio_irq_mask
,
265 .irq_unmask
= atmel_gpio_irq_unmask
,
266 .irq_set_type
= atmel_gpio_irq_set_type
,
267 .irq_set_wake
= pm_sleep_ptr(atmel_gpio_irq_set_wake
),
270 static int atmel_gpio_to_irq(struct gpio_chip
*chip
, unsigned int offset
)
272 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
274 return irq_find_mapping(atmel_pioctrl
->irq_domain
, offset
);
277 static void atmel_gpio_irq_handler(struct irq_desc
*desc
)
279 unsigned int irq
= irq_desc_get_irq(desc
);
280 struct atmel_pioctrl
*atmel_pioctrl
= irq_desc_get_handler_data(desc
);
281 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
285 /* Find from which bank is the irq received. */
286 for (n
= 0; n
< atmel_pioctrl
->nbanks
; n
++) {
287 if (atmel_pioctrl
->irqs
[n
] == irq
) {
294 dev_err(atmel_pioctrl
->dev
,
295 "no bank associated to irq %u\n", irq
);
299 chained_irq_enter(chip
, desc
);
302 isr
= (unsigned long)atmel_gpio_read(atmel_pioctrl
, bank
,
304 isr
&= (unsigned long)atmel_gpio_read(atmel_pioctrl
, bank
,
309 for_each_set_bit(n
, &isr
, BITS_PER_LONG
)
310 generic_handle_irq(atmel_gpio_to_irq(
311 atmel_pioctrl
->gpio_chip
,
312 bank
* ATMEL_PIO_NPINS_PER_BANK
+ n
));
315 chained_irq_exit(chip
, desc
);
318 static int atmel_gpio_direction_input(struct gpio_chip
*chip
,
321 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
322 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[offset
];
325 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_MSKR
,
327 reg
= atmel_gpio_read(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
);
328 reg
&= ~ATMEL_PIO_DIR_MASK
;
329 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
, reg
);
334 static int atmel_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
336 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
337 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[offset
];
340 reg
= atmel_gpio_read(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_PDSR
);
342 return !!(reg
& BIT(pin
->line
));
345 static int atmel_gpio_get_multiple(struct gpio_chip
*chip
, unsigned long *mask
,
348 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
351 bitmap_zero(bits
, atmel_pioctrl
->npins
);
353 for (bank
= 0; bank
< atmel_pioctrl
->nbanks
; bank
++) {
354 unsigned int word
= bank
;
355 unsigned int offset
= 0;
358 #if ATMEL_PIO_NPINS_PER_BANK != BITS_PER_LONG
359 word
= BIT_WORD(bank
* ATMEL_PIO_NPINS_PER_BANK
);
360 offset
= bank
* ATMEL_PIO_NPINS_PER_BANK
% BITS_PER_LONG
;
365 reg
= atmel_gpio_read(atmel_pioctrl
, bank
, ATMEL_PIO_PDSR
);
366 bits
[word
] |= mask
[word
] & (reg
<< offset
);
372 static int atmel_gpio_direction_output(struct gpio_chip
*chip
,
376 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
377 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[offset
];
380 atmel_gpio_write(atmel_pioctrl
, pin
->bank
,
381 value
? ATMEL_PIO_SODR
: ATMEL_PIO_CODR
,
384 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_MSKR
,
386 reg
= atmel_gpio_read(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
);
387 reg
|= ATMEL_PIO_DIR_MASK
;
388 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
, reg
);
393 static void atmel_gpio_set(struct gpio_chip
*chip
, unsigned int offset
, int val
)
395 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
396 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[offset
];
398 atmel_gpio_write(atmel_pioctrl
, pin
->bank
,
399 val
? ATMEL_PIO_SODR
: ATMEL_PIO_CODR
,
403 static void atmel_gpio_set_multiple(struct gpio_chip
*chip
, unsigned long *mask
,
406 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
409 for (bank
= 0; bank
< atmel_pioctrl
->nbanks
; bank
++) {
410 unsigned int bitmask
;
411 unsigned int word
= bank
;
414 * On a 64-bit platform, BITS_PER_LONG is 64 so it is necessary to iterate over
415 * two 32bit words to handle the whole bitmask
417 #if ATMEL_PIO_NPINS_PER_BANK != BITS_PER_LONG
418 word
= BIT_WORD(bank
* ATMEL_PIO_NPINS_PER_BANK
);
423 bitmask
= mask
[word
] & bits
[word
];
424 atmel_gpio_write(atmel_pioctrl
, bank
, ATMEL_PIO_SODR
, bitmask
);
426 bitmask
= mask
[word
] & ~bits
[word
];
427 atmel_gpio_write(atmel_pioctrl
, bank
, ATMEL_PIO_CODR
, bitmask
);
429 #if ATMEL_PIO_NPINS_PER_BANK != BITS_PER_LONG
430 mask
[word
] >>= ATMEL_PIO_NPINS_PER_BANK
;
431 bits
[word
] >>= ATMEL_PIO_NPINS_PER_BANK
;
436 static struct gpio_chip atmel_gpio_chip
= {
437 .direction_input
= atmel_gpio_direction_input
,
438 .get
= atmel_gpio_get
,
439 .get_multiple
= atmel_gpio_get_multiple
,
440 .direction_output
= atmel_gpio_direction_output
,
441 .set
= atmel_gpio_set
,
442 .set_multiple
= atmel_gpio_set_multiple
,
443 .to_irq
= atmel_gpio_to_irq
,
447 /* --- PINCTRL --- */
448 static unsigned int atmel_pin_config_read(struct pinctrl_dev
*pctldev
,
451 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
452 unsigned int bank
= atmel_pioctrl
->pins
[pin_id
]->bank
;
453 unsigned int line
= atmel_pioctrl
->pins
[pin_id
]->line
;
454 void __iomem
*addr
= atmel_pioctrl
->reg_base
455 + bank
* ATMEL_PIO_BANK_OFFSET
;
457 writel_relaxed(BIT(line
), addr
+ ATMEL_PIO_MSKR
);
458 /* Have to set MSKR first, to access the right pin CFGR. */
461 return readl_relaxed(addr
+ ATMEL_PIO_CFGR
);
464 static void atmel_pin_config_write(struct pinctrl_dev
*pctldev
,
465 unsigned int pin_id
, u32 conf
)
467 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
468 unsigned int bank
= atmel_pioctrl
->pins
[pin_id
]->bank
;
469 unsigned int line
= atmel_pioctrl
->pins
[pin_id
]->line
;
470 void __iomem
*addr
= atmel_pioctrl
->reg_base
471 + bank
* ATMEL_PIO_BANK_OFFSET
;
473 writel_relaxed(BIT(line
), addr
+ ATMEL_PIO_MSKR
);
474 /* Have to set MSKR first, to access the right pin CFGR. */
476 writel_relaxed(conf
, addr
+ ATMEL_PIO_CFGR
);
479 static int atmel_pctl_get_groups_count(struct pinctrl_dev
*pctldev
)
481 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
483 return atmel_pioctrl
->npins
;
486 static const char *atmel_pctl_get_group_name(struct pinctrl_dev
*pctldev
,
487 unsigned int selector
)
489 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
491 return atmel_pioctrl
->groups
[selector
].name
;
494 static int atmel_pctl_get_group_pins(struct pinctrl_dev
*pctldev
,
495 unsigned int selector
,
496 const unsigned int **pins
,
497 unsigned int *num_pins
)
499 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
501 *pins
= (unsigned int *)&atmel_pioctrl
->groups
[selector
].pin
;
507 static struct atmel_group
*
508 atmel_pctl_find_group_by_pin(struct pinctrl_dev
*pctldev
, unsigned int pin
)
510 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
513 for (i
= 0; i
< atmel_pioctrl
->npins
; i
++) {
514 struct atmel_group
*grp
= atmel_pioctrl
->groups
+ i
;
523 static int atmel_pctl_xlate_pinfunc(struct pinctrl_dev
*pctldev
,
524 struct device_node
*np
,
525 u32 pinfunc
, const char **grp_name
,
526 const char **func_name
)
528 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
529 unsigned int pin_id
, func_id
;
530 struct atmel_group
*grp
;
532 pin_id
= ATMEL_GET_PIN_NO(pinfunc
);
533 func_id
= ATMEL_GET_PIN_FUNC(pinfunc
);
535 if (func_id
>= ARRAY_SIZE(atmel_functions
))
538 *func_name
= atmel_functions
[func_id
];
540 grp
= atmel_pctl_find_group_by_pin(pctldev
, pin_id
);
543 *grp_name
= grp
->name
;
545 atmel_pioctrl
->pins
[pin_id
]->mux
= func_id
;
546 atmel_pioctrl
->pins
[pin_id
]->ioset
= ATMEL_GET_PIN_IOSET(pinfunc
);
547 /* Want the device name not the group one. */
548 if (np
->parent
== atmel_pioctrl
->node
)
549 atmel_pioctrl
->pins
[pin_id
]->device
= np
->name
;
551 atmel_pioctrl
->pins
[pin_id
]->device
= np
->parent
->name
;
556 static int atmel_pctl_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
557 struct device_node
*np
,
558 struct pinctrl_map
**map
,
559 unsigned int *reserved_maps
,
560 unsigned int *num_maps
)
562 unsigned int num_pins
, num_configs
, reserve
;
563 unsigned long *configs
;
564 struct property
*pins
;
568 pins
= of_find_property(np
, "pinmux", NULL
);
572 ret
= pinconf_generic_parse_dt_config(np
, pctldev
, &configs
,
575 dev_err(pctldev
->dev
, "%pOF: could not parse node property\n",
580 num_pins
= pins
->length
/ sizeof(u32
);
582 dev_err(pctldev
->dev
, "no pins found in node %pOF\n", np
);
588 * Reserve maps, at least there is a mux map and an optional conf
595 ret
= pinctrl_utils_reserve_map(pctldev
, map
, reserved_maps
, num_maps
,
600 for (i
= 0; i
< num_pins
; i
++) {
601 const char *group
, *func
;
603 ret
= of_property_read_u32_index(np
, "pinmux", i
, &pinfunc
);
607 ret
= atmel_pctl_xlate_pinfunc(pctldev
, np
, pinfunc
, &group
,
612 pinctrl_utils_add_map_mux(pctldev
, map
, reserved_maps
, num_maps
,
616 ret
= pinctrl_utils_add_map_configs(pctldev
, map
,
617 reserved_maps
, num_maps
, group
,
618 configs
, num_configs
,
619 PIN_MAP_TYPE_CONFIGS_GROUP
);
630 static int atmel_pctl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
631 struct device_node
*np_config
,
632 struct pinctrl_map
**map
,
633 unsigned int *num_maps
)
635 unsigned int reserved_maps
;
643 * If all the pins of a device have the same configuration (or no one),
644 * it is useless to add a subnode, so directly parse node referenced by
647 ret
= atmel_pctl_dt_subnode_to_map(pctldev
, np_config
, map
,
648 &reserved_maps
, num_maps
);
650 for_each_child_of_node_scoped(np_config
, np
) {
651 ret
= atmel_pctl_dt_subnode_to_map(pctldev
, np
, map
,
652 &reserved_maps
, num_maps
);
659 pinctrl_utils_free_map(pctldev
, *map
, *num_maps
);
660 dev_err(pctldev
->dev
, "can't create maps for node %pOF\n",
667 static const struct pinctrl_ops atmel_pctlops
= {
668 .get_groups_count
= atmel_pctl_get_groups_count
,
669 .get_group_name
= atmel_pctl_get_group_name
,
670 .get_group_pins
= atmel_pctl_get_group_pins
,
671 .dt_node_to_map
= atmel_pctl_dt_node_to_map
,
672 .dt_free_map
= pinctrl_utils_free_map
,
675 static int atmel_pmx_get_functions_count(struct pinctrl_dev
*pctldev
)
677 return ARRAY_SIZE(atmel_functions
);
680 static const char *atmel_pmx_get_function_name(struct pinctrl_dev
*pctldev
,
681 unsigned int selector
)
683 return atmel_functions
[selector
];
686 static int atmel_pmx_get_function_groups(struct pinctrl_dev
*pctldev
,
687 unsigned int selector
,
688 const char * const **groups
,
689 unsigned * const num_groups
)
691 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
693 *groups
= atmel_pioctrl
->group_names
;
694 *num_groups
= atmel_pioctrl
->npins
;
699 static int atmel_pmx_set_mux(struct pinctrl_dev
*pctldev
,
700 unsigned int function
,
703 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
707 dev_dbg(pctldev
->dev
, "enable function %s group %s\n",
708 atmel_functions
[function
], atmel_pioctrl
->groups
[group
].name
);
710 pin
= atmel_pioctrl
->groups
[group
].pin
;
711 conf
= atmel_pin_config_read(pctldev
, pin
);
712 conf
&= (~ATMEL_PIO_CFGR_FUNC_MASK
);
713 conf
|= (function
& ATMEL_PIO_CFGR_FUNC_MASK
);
714 dev_dbg(pctldev
->dev
, "pin: %u, conf: 0x%08x\n", pin
, conf
);
715 atmel_pin_config_write(pctldev
, pin
, conf
);
720 static const struct pinmux_ops atmel_pmxops
= {
721 .get_functions_count
= atmel_pmx_get_functions_count
,
722 .get_function_name
= atmel_pmx_get_function_name
,
723 .get_function_groups
= atmel_pmx_get_function_groups
,
724 .set_mux
= atmel_pmx_set_mux
,
727 static int atmel_conf_pin_config_group_get(struct pinctrl_dev
*pctldev
,
729 unsigned long *config
)
731 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
732 unsigned int param
= pinconf_to_config_param(*config
), arg
= 0;
733 struct atmel_group
*grp
= atmel_pioctrl
->groups
+ group
;
734 unsigned int pin_id
= grp
->pin
;
737 res
= atmel_pin_config_read(pctldev
, pin_id
);
740 case PIN_CONFIG_BIAS_PULL_UP
:
741 if (!(res
& ATMEL_PIO_PUEN_MASK
))
745 case PIN_CONFIG_BIAS_PULL_DOWN
:
746 if ((res
& ATMEL_PIO_PUEN_MASK
) ||
747 (!(res
& ATMEL_PIO_PDEN_MASK
)))
751 case PIN_CONFIG_BIAS_DISABLE
:
752 if ((res
& ATMEL_PIO_PUEN_MASK
) ||
753 ((res
& ATMEL_PIO_PDEN_MASK
)))
757 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
758 if (!(res
& ATMEL_PIO_OPD_MASK
))
762 case PIN_CONFIG_DRIVE_PUSH_PULL
:
763 if (res
& ATMEL_PIO_OPD_MASK
)
767 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
768 if (!(res
& ATMEL_PIO_SCHMITT_MASK
))
772 case PIN_CONFIG_SLEW_RATE
:
773 if (!atmel_pioctrl
->slew_rate_support
)
775 if (!(res
& ATMEL_PIO_SR_MASK
))
779 case ATMEL_PIN_CONFIG_DRIVE_STRENGTH
:
780 if (!(res
& ATMEL_PIO_DRVSTR_MASK
))
782 arg
= (res
& ATMEL_PIO_DRVSTR_MASK
) >> ATMEL_PIO_DRVSTR_OFFSET
;
784 case PIN_CONFIG_PERSIST_STATE
:
790 *config
= pinconf_to_config_packed(param
, arg
);
794 static int atmel_conf_pin_config_group_set(struct pinctrl_dev
*pctldev
,
796 unsigned long *configs
,
797 unsigned int num_configs
)
799 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
800 struct atmel_group
*grp
= atmel_pioctrl
->groups
+ group
;
801 unsigned int bank
, pin
, pin_id
= grp
->pin
;
805 conf
= atmel_pin_config_read(pctldev
, pin_id
);
807 /* Keep slew rate enabled by default. */
808 if (atmel_pioctrl
->slew_rate_support
)
809 conf
|= ATMEL_PIO_SR_MASK
;
811 for (i
= 0; i
< num_configs
; i
++) {
812 unsigned int param
= pinconf_to_config_param(configs
[i
]);
813 unsigned int arg
= pinconf_to_config_argument(configs
[i
]);
815 dev_dbg(pctldev
->dev
, "%s: pin=%u, config=0x%lx\n",
816 __func__
, pin_id
, configs
[i
]);
819 case PIN_CONFIG_BIAS_DISABLE
:
820 conf
&= (~ATMEL_PIO_PUEN_MASK
);
821 conf
&= (~ATMEL_PIO_PDEN_MASK
);
823 case PIN_CONFIG_BIAS_PULL_UP
:
824 conf
|= ATMEL_PIO_PUEN_MASK
;
825 conf
&= (~ATMEL_PIO_PDEN_MASK
);
827 case PIN_CONFIG_BIAS_PULL_DOWN
:
828 conf
|= ATMEL_PIO_PDEN_MASK
;
829 conf
&= (~ATMEL_PIO_PUEN_MASK
);
831 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
832 conf
|= ATMEL_PIO_OPD_MASK
;
834 case PIN_CONFIG_DRIVE_PUSH_PULL
:
835 conf
&= ~ATMEL_PIO_OPD_MASK
;
837 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
839 conf
|= ATMEL_PIO_SCHMITT_MASK
;
841 conf
&= (~ATMEL_PIO_SCHMITT_MASK
);
843 case PIN_CONFIG_INPUT_DEBOUNCE
:
845 conf
&= (~ATMEL_PIO_IFEN_MASK
);
846 conf
&= (~ATMEL_PIO_IFSCEN_MASK
);
849 * We don't care about the debounce value for several reasons:
850 * - can't have different debounce periods inside a same group,
851 * - the register to configure this period is a secure register.
852 * The debouncing filter can filter a pulse with a duration of less
853 * than 1/2 slow clock period.
855 conf
|= ATMEL_PIO_IFEN_MASK
;
856 conf
|= ATMEL_PIO_IFSCEN_MASK
;
859 case PIN_CONFIG_OUTPUT
:
860 conf
|= ATMEL_PIO_DIR_MASK
;
861 bank
= ATMEL_PIO_BANK(pin_id
);
862 pin
= ATMEL_PIO_LINE(pin_id
);
866 writel_relaxed(mask
, atmel_pioctrl
->reg_base
+
867 bank
* ATMEL_PIO_BANK_OFFSET
+
870 writel_relaxed(mask
, atmel_pioctrl
->reg_base
+
871 bank
* ATMEL_PIO_BANK_OFFSET
+
875 case PIN_CONFIG_SLEW_RATE
:
876 if (!atmel_pioctrl
->slew_rate_support
)
878 /* And remove it if explicitly requested. */
880 conf
&= ~ATMEL_PIO_SR_MASK
;
882 case ATMEL_PIN_CONFIG_DRIVE_STRENGTH
:
884 case ATMEL_PIO_DRVSTR_LO
:
885 case ATMEL_PIO_DRVSTR_ME
:
886 case ATMEL_PIO_DRVSTR_HI
:
887 conf
&= (~ATMEL_PIO_DRVSTR_MASK
);
888 conf
|= arg
<< ATMEL_PIO_DRVSTR_OFFSET
;
891 dev_warn(pctldev
->dev
, "drive strength not updated (incorrect value)\n");
894 case PIN_CONFIG_PERSIST_STATE
:
897 dev_warn(pctldev
->dev
,
898 "unsupported configuration parameter: %u\n",
904 dev_dbg(pctldev
->dev
, "%s: reg=0x%08x\n", __func__
, conf
);
905 atmel_pin_config_write(pctldev
, pin_id
, conf
);
910 static int atmel_conf_pin_config_set(struct pinctrl_dev
*pctldev
,
912 unsigned long *configs
,
913 unsigned num_configs
)
915 struct atmel_group
*grp
= atmel_pctl_find_group_by_pin(pctldev
, pin
);
917 return atmel_conf_pin_config_group_set(pctldev
, grp
->pin
, configs
, num_configs
);
920 static int atmel_conf_pin_config_get(struct pinctrl_dev
*pctldev
,
922 unsigned long *configs
)
924 struct atmel_group
*grp
= atmel_pctl_find_group_by_pin(pctldev
, pin
);
926 return atmel_conf_pin_config_group_get(pctldev
, grp
->pin
, configs
);
929 static void atmel_conf_pin_config_dbg_show(struct pinctrl_dev
*pctldev
,
933 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
936 if (!atmel_pioctrl
->pins
[pin_id
]->device
)
939 seq_printf(s
, " (%s, ioset %u) ",
940 atmel_pioctrl
->pins
[pin_id
]->device
,
941 atmel_pioctrl
->pins
[pin_id
]->ioset
);
943 conf
= atmel_pin_config_read(pctldev
, pin_id
);
944 if (conf
& ATMEL_PIO_PUEN_MASK
)
945 seq_printf(s
, "%s ", "pull-up");
946 if (conf
& ATMEL_PIO_PDEN_MASK
)
947 seq_printf(s
, "%s ", "pull-down");
948 if (conf
& ATMEL_PIO_IFEN_MASK
)
949 seq_printf(s
, "%s ", "debounce");
950 if (conf
& ATMEL_PIO_OPD_MASK
)
951 seq_printf(s
, "%s ", "open-drain");
953 seq_printf(s
, "%s ", "push-pull");
954 if (conf
& ATMEL_PIO_SCHMITT_MASK
)
955 seq_printf(s
, "%s ", "schmitt");
956 if (atmel_pioctrl
->slew_rate_support
&& (conf
& ATMEL_PIO_SR_MASK
))
957 seq_printf(s
, "%s ", "slew-rate");
958 if (conf
& ATMEL_PIO_DRVSTR_MASK
) {
959 switch ((conf
& ATMEL_PIO_DRVSTR_MASK
) >> ATMEL_PIO_DRVSTR_OFFSET
) {
960 case ATMEL_PIO_DRVSTR_ME
:
961 seq_printf(s
, "%s ", "medium-drive");
963 case ATMEL_PIO_DRVSTR_HI
:
964 seq_printf(s
, "%s ", "high-drive");
966 /* ATMEL_PIO_DRVSTR_LO and 0 which is the default value at reset */
968 seq_printf(s
, "%s ", "low-drive");
973 static const struct pinconf_ops atmel_confops
= {
974 .pin_config_group_get
= atmel_conf_pin_config_group_get
,
975 .pin_config_group_set
= atmel_conf_pin_config_group_set
,
976 .pin_config_dbg_show
= atmel_conf_pin_config_dbg_show
,
977 .pin_config_set
= atmel_conf_pin_config_set
,
978 .pin_config_get
= atmel_conf_pin_config_get
,
981 static struct pinctrl_desc atmel_pinctrl_desc
= {
982 .name
= "atmel_pinctrl",
983 .confops
= &atmel_confops
,
984 .pctlops
= &atmel_pctlops
,
985 .pmxops
= &atmel_pmxops
,
988 static int __maybe_unused
atmel_pctrl_suspend(struct device
*dev
)
990 struct atmel_pioctrl
*atmel_pioctrl
= dev_get_drvdata(dev
);
994 * For each bank, save IMR to restore it later and disable all GPIO
995 * interrupts excepting the ones marked as wakeup sources.
997 for (i
= 0; i
< atmel_pioctrl
->nbanks
; i
++) {
998 atmel_pioctrl
->pm_suspend_backup
[i
].imr
=
999 atmel_gpio_read(atmel_pioctrl
, i
, ATMEL_PIO_IMR
);
1000 atmel_gpio_write(atmel_pioctrl
, i
, ATMEL_PIO_IDR
,
1001 ~atmel_pioctrl
->pm_wakeup_sources
[i
]);
1002 atmel_pioctrl
->pm_suspend_backup
[i
].odsr
=
1003 atmel_gpio_read(atmel_pioctrl
, i
, ATMEL_PIO_ODSR
);
1004 for (j
= 0; j
< ATMEL_PIO_NPINS_PER_BANK
; j
++) {
1005 atmel_gpio_write(atmel_pioctrl
, i
,
1006 ATMEL_PIO_MSKR
, BIT(j
));
1007 atmel_pioctrl
->pm_suspend_backup
[i
].cfgr
[j
] =
1008 atmel_gpio_read(atmel_pioctrl
, i
,
1016 static int __maybe_unused
atmel_pctrl_resume(struct device
*dev
)
1018 struct atmel_pioctrl
*atmel_pioctrl
= dev_get_drvdata(dev
);
1021 for (i
= 0; i
< atmel_pioctrl
->nbanks
; i
++) {
1022 atmel_gpio_write(atmel_pioctrl
, i
, ATMEL_PIO_IER
,
1023 atmel_pioctrl
->pm_suspend_backup
[i
].imr
);
1024 atmel_gpio_write(atmel_pioctrl
, i
, ATMEL_PIO_SODR
,
1025 atmel_pioctrl
->pm_suspend_backup
[i
].odsr
);
1026 for (j
= 0; j
< ATMEL_PIO_NPINS_PER_BANK
; j
++) {
1027 atmel_gpio_write(atmel_pioctrl
, i
,
1028 ATMEL_PIO_MSKR
, BIT(j
));
1029 atmel_gpio_write(atmel_pioctrl
, i
, ATMEL_PIO_CFGR
,
1030 atmel_pioctrl
->pm_suspend_backup
[i
].cfgr
[j
]);
1037 static const struct dev_pm_ops atmel_pctrl_pm_ops
= {
1038 SET_SYSTEM_SLEEP_PM_OPS(atmel_pctrl_suspend
, atmel_pctrl_resume
)
1042 * The number of banks can be different from a SoC to another one.
1043 * We can have up to 16 banks.
1045 static const struct atmel_pioctrl_data atmel_sama5d2_pioctrl_data
= {
1047 .last_bank_count
= ATMEL_PIO_NPINS_PER_BANK
,
1050 static const struct atmel_pioctrl_data microchip_sama7g5_pioctrl_data
= {
1052 .last_bank_count
= 8, /* sama7g5 has only PE0 to PE7 */
1053 .slew_rate_support
= 1,
1056 static const struct of_device_id atmel_pctrl_of_match
[] = {
1058 .compatible
= "atmel,sama5d2-pinctrl",
1059 .data
= &atmel_sama5d2_pioctrl_data
,
1061 .compatible
= "microchip,sama7g5-pinctrl",
1062 .data
= µchip_sama7g5_pioctrl_data
,
1069 * This lock class allows to tell lockdep that parent IRQ and children IRQ do
1070 * not share the same class so it does not raise false positive
1072 static struct lock_class_key atmel_lock_key
;
1073 static struct lock_class_key atmel_request_key
;
1075 static int atmel_pinctrl_probe(struct platform_device
*pdev
)
1077 struct device
*dev
= &pdev
->dev
;
1078 struct pinctrl_pin_desc
*pin_desc
;
1079 const char **group_names
;
1081 struct atmel_pioctrl
*atmel_pioctrl
;
1082 const struct atmel_pioctrl_data
*atmel_pioctrl_data
;
1084 atmel_pioctrl
= devm_kzalloc(dev
, sizeof(*atmel_pioctrl
), GFP_KERNEL
);
1087 atmel_pioctrl
->dev
= dev
;
1088 atmel_pioctrl
->node
= dev
->of_node
;
1089 platform_set_drvdata(pdev
, atmel_pioctrl
);
1091 atmel_pioctrl_data
= device_get_match_data(dev
);
1092 if (!atmel_pioctrl_data
)
1093 return dev_err_probe(dev
, -ENODEV
, "Invalid device data\n");
1095 atmel_pioctrl
->nbanks
= atmel_pioctrl_data
->nbanks
;
1096 atmel_pioctrl
->npins
= atmel_pioctrl
->nbanks
* ATMEL_PIO_NPINS_PER_BANK
;
1097 /* if last bank has limited number of pins, adjust accordingly */
1098 if (atmel_pioctrl_data
->last_bank_count
!= ATMEL_PIO_NPINS_PER_BANK
) {
1099 atmel_pioctrl
->npins
-= ATMEL_PIO_NPINS_PER_BANK
;
1100 atmel_pioctrl
->npins
+= atmel_pioctrl_data
->last_bank_count
;
1102 atmel_pioctrl
->slew_rate_support
= atmel_pioctrl_data
->slew_rate_support
;
1104 atmel_pioctrl
->reg_base
= devm_platform_ioremap_resource(pdev
, 0);
1105 if (IS_ERR(atmel_pioctrl
->reg_base
))
1106 return PTR_ERR(atmel_pioctrl
->reg_base
);
1108 atmel_pioctrl
->clk
= devm_clk_get_enabled(dev
, NULL
);
1109 if (IS_ERR(atmel_pioctrl
->clk
))
1110 return dev_err_probe(dev
, PTR_ERR(atmel_pioctrl
->clk
), "failed to get clock\n");
1112 atmel_pioctrl
->pins
= devm_kcalloc(dev
,
1113 atmel_pioctrl
->npins
,
1114 sizeof(*atmel_pioctrl
->pins
),
1116 if (!atmel_pioctrl
->pins
)
1119 pin_desc
= devm_kcalloc(dev
, atmel_pioctrl
->npins
, sizeof(*pin_desc
),
1123 atmel_pinctrl_desc
.pins
= pin_desc
;
1124 atmel_pinctrl_desc
.npins
= atmel_pioctrl
->npins
;
1125 atmel_pinctrl_desc
.num_custom_params
= ARRAY_SIZE(atmel_custom_bindings
);
1126 atmel_pinctrl_desc
.custom_params
= atmel_custom_bindings
;
1128 /* One pin is one group since a pin can achieve all functions. */
1129 group_names
= devm_kcalloc(dev
,
1130 atmel_pioctrl
->npins
, sizeof(*group_names
),
1134 atmel_pioctrl
->group_names
= group_names
;
1136 atmel_pioctrl
->groups
= devm_kcalloc(&pdev
->dev
,
1137 atmel_pioctrl
->npins
, sizeof(*atmel_pioctrl
->groups
),
1139 if (!atmel_pioctrl
->groups
)
1141 for (i
= 0 ; i
< atmel_pioctrl
->npins
; i
++) {
1142 struct atmel_group
*group
= atmel_pioctrl
->groups
+ i
;
1143 unsigned int bank
= ATMEL_PIO_BANK(i
);
1144 unsigned int line
= ATMEL_PIO_LINE(i
);
1146 atmel_pioctrl
->pins
[i
] = devm_kzalloc(dev
,
1147 sizeof(**atmel_pioctrl
->pins
), GFP_KERNEL
);
1148 if (!atmel_pioctrl
->pins
[i
])
1151 atmel_pioctrl
->pins
[i
]->pin_id
= i
;
1152 atmel_pioctrl
->pins
[i
]->bank
= bank
;
1153 atmel_pioctrl
->pins
[i
]->line
= line
;
1155 pin_desc
[i
].number
= i
;
1156 /* Pin naming convention: P(bank_name)(bank_pin_number). */
1157 pin_desc
[i
].name
= devm_kasprintf(&pdev
->dev
, GFP_KERNEL
, "P%c%u",
1159 if (!pin_desc
[i
].name
)
1162 group
->name
= group_names
[i
] = pin_desc
[i
].name
;
1163 group
->pin
= pin_desc
[i
].number
;
1165 dev_dbg(dev
, "pin_id=%u, bank=%u, line=%u", i
, bank
, line
);
1168 atmel_pioctrl
->gpio_chip
= &atmel_gpio_chip
;
1169 atmel_pioctrl
->gpio_chip
->ngpio
= atmel_pioctrl
->npins
;
1170 atmel_pioctrl
->gpio_chip
->label
= dev_name(dev
);
1171 atmel_pioctrl
->gpio_chip
->parent
= dev
;
1172 atmel_pioctrl
->gpio_chip
->names
= atmel_pioctrl
->group_names
;
1173 atmel_pioctrl
->gpio_chip
->set_config
= gpiochip_generic_config
;
1175 atmel_pioctrl
->pm_wakeup_sources
= devm_kcalloc(dev
,
1176 atmel_pioctrl
->nbanks
,
1177 sizeof(*atmel_pioctrl
->pm_wakeup_sources
),
1179 if (!atmel_pioctrl
->pm_wakeup_sources
)
1182 atmel_pioctrl
->pm_suspend_backup
= devm_kcalloc(dev
,
1183 atmel_pioctrl
->nbanks
,
1184 sizeof(*atmel_pioctrl
->pm_suspend_backup
),
1186 if (!atmel_pioctrl
->pm_suspend_backup
)
1189 atmel_pioctrl
->irqs
= devm_kcalloc(dev
,
1190 atmel_pioctrl
->nbanks
,
1191 sizeof(*atmel_pioctrl
->irqs
),
1193 if (!atmel_pioctrl
->irqs
)
1196 /* There is one controller but each bank has its own irq line. */
1197 for (i
= 0; i
< atmel_pioctrl
->nbanks
; i
++) {
1198 ret
= platform_get_irq(pdev
, i
);
1200 dev_dbg(dev
, "missing irq resource for group %c\n",
1204 atmel_pioctrl
->irqs
[i
] = ret
;
1205 irq_set_chained_handler_and_data(ret
, atmel_gpio_irq_handler
, atmel_pioctrl
);
1206 dev_dbg(dev
, "bank %i: irq=%d\n", i
, ret
);
1209 atmel_pioctrl
->irq_domain
= irq_domain_add_linear(dev
->of_node
,
1210 atmel_pioctrl
->gpio_chip
->ngpio
,
1211 &irq_domain_simple_ops
, NULL
);
1212 if (!atmel_pioctrl
->irq_domain
)
1213 return dev_err_probe(dev
, -ENODEV
, "can't add the irq domain\n");
1215 for (i
= 0; i
< atmel_pioctrl
->npins
; i
++) {
1216 int irq
= irq_create_mapping(atmel_pioctrl
->irq_domain
, i
);
1218 irq_set_chip_and_handler(irq
, &atmel_gpio_irq_chip
,
1220 irq_set_chip_data(irq
, atmel_pioctrl
);
1221 irq_set_lockdep_class(irq
, &atmel_lock_key
, &atmel_request_key
);
1223 "atmel gpio irq domain: hwirq: %d, linux irq: %d\n",
1227 atmel_pioctrl
->pinctrl_dev
= devm_pinctrl_register(&pdev
->dev
,
1228 &atmel_pinctrl_desc
,
1230 if (IS_ERR(atmel_pioctrl
->pinctrl_dev
)) {
1231 ret
= PTR_ERR(atmel_pioctrl
->pinctrl_dev
);
1232 dev_err(dev
, "pinctrl registration failed\n");
1233 goto irq_domain_remove_error
;
1236 ret
= gpiochip_add_data(atmel_pioctrl
->gpio_chip
, atmel_pioctrl
);
1238 dev_err(dev
, "failed to add gpiochip\n");
1239 goto irq_domain_remove_error
;
1242 ret
= gpiochip_add_pin_range(atmel_pioctrl
->gpio_chip
, dev_name(dev
),
1243 0, 0, atmel_pioctrl
->gpio_chip
->ngpio
);
1245 dev_err(dev
, "failed to add gpio pin range\n");
1246 goto gpiochip_add_pin_range_error
;
1249 dev_info(&pdev
->dev
, "atmel pinctrl initialized\n");
1253 gpiochip_add_pin_range_error
:
1254 gpiochip_remove(atmel_pioctrl
->gpio_chip
);
1256 irq_domain_remove_error
:
1257 irq_domain_remove(atmel_pioctrl
->irq_domain
);
1262 static struct platform_driver atmel_pinctrl_driver
= {
1264 .name
= "pinctrl-at91-pio4",
1265 .of_match_table
= atmel_pctrl_of_match
,
1266 .pm
= &atmel_pctrl_pm_ops
,
1267 .suppress_bind_attrs
= true,
1269 .probe
= atmel_pinctrl_probe
,
1271 builtin_platform_driver(atmel_pinctrl_driver
);