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>
10 #include <linux/clk.h>
11 #include <linux/gpio/driver.h>
12 #include <linux/interrupt.h>
14 #include <linux/init.h>
16 #include <linux/platform_device.h>
17 #include <linux/pinctrl/pinconf.h>
18 #include <linux/pinctrl/pinconf-generic.h>
19 #include <linux/pinctrl/pinctrl.h>
20 #include <linux/pinctrl/pinmux.h>
21 #include <linux/slab.h>
24 #include "pinctrl-utils.h"
28 * In order to not introduce confusion between Atmel PIO groups and pinctrl
29 * framework groups, Atmel PIO groups will be called banks, line is kept to
30 * designed the pin id into this bank.
33 #define ATMEL_PIO_MSKR 0x0000
34 #define ATMEL_PIO_CFGR 0x0004
35 #define ATMEL_PIO_CFGR_FUNC_MASK GENMASK(2, 0)
36 #define ATMEL_PIO_DIR_MASK BIT(8)
37 #define ATMEL_PIO_PUEN_MASK BIT(9)
38 #define ATMEL_PIO_PDEN_MASK BIT(10)
39 #define ATMEL_PIO_IFEN_MASK BIT(12)
40 #define ATMEL_PIO_IFSCEN_MASK BIT(13)
41 #define ATMEL_PIO_OPD_MASK BIT(14)
42 #define ATMEL_PIO_SCHMITT_MASK BIT(15)
43 #define ATMEL_PIO_DRVSTR_MASK GENMASK(17, 16)
44 #define ATMEL_PIO_DRVSTR_OFFSET 16
45 #define ATMEL_PIO_CFGR_EVTSEL_MASK GENMASK(26, 24)
46 #define ATMEL_PIO_CFGR_EVTSEL_FALLING (0 << 24)
47 #define ATMEL_PIO_CFGR_EVTSEL_RISING (1 << 24)
48 #define ATMEL_PIO_CFGR_EVTSEL_BOTH (2 << 24)
49 #define ATMEL_PIO_CFGR_EVTSEL_LOW (3 << 24)
50 #define ATMEL_PIO_CFGR_EVTSEL_HIGH (4 << 24)
51 #define ATMEL_PIO_PDSR 0x0008
52 #define ATMEL_PIO_LOCKSR 0x000C
53 #define ATMEL_PIO_SODR 0x0010
54 #define ATMEL_PIO_CODR 0x0014
55 #define ATMEL_PIO_ODSR 0x0018
56 #define ATMEL_PIO_IER 0x0020
57 #define ATMEL_PIO_IDR 0x0024
58 #define ATMEL_PIO_IMR 0x0028
59 #define ATMEL_PIO_ISR 0x002C
60 #define ATMEL_PIO_IOFR 0x003C
62 #define ATMEL_PIO_NPINS_PER_BANK 32
63 #define ATMEL_PIO_BANK(pin_id) (pin_id / ATMEL_PIO_NPINS_PER_BANK)
64 #define ATMEL_PIO_LINE(pin_id) (pin_id % ATMEL_PIO_NPINS_PER_BANK)
65 #define ATMEL_PIO_BANK_OFFSET 0x40
67 #define ATMEL_GET_PIN_NO(pinfunc) ((pinfunc) & 0xff)
68 #define ATMEL_GET_PIN_FUNC(pinfunc) ((pinfunc >> 16) & 0xf)
69 #define ATMEL_GET_PIN_IOSET(pinfunc) ((pinfunc >> 20) & 0xf)
71 /* Custom pinconf parameters */
72 #define ATMEL_PIN_CONFIG_DRIVE_STRENGTH (PIN_CONFIG_END + 1)
75 * struct atmel_pioctrl_data - Atmel PIO controller (pinmux + gpio) data struct
76 * @nbanks: number of PIO banks
77 * @last_bank_count: number of lines in the last bank (can be less than
78 * the rest of the banks).
80 struct atmel_pioctrl_data
{
82 unsigned last_bank_count
;
100 * struct atmel_pioctrl - Atmel PIO controller (pinmux + gpio)
101 * @reg_base: base address of the controller.
102 * @clk: clock of the controller.
103 * @nbanks: number of PIO groups, it can vary depending on the SoC.
104 * @pinctrl_dev: pinctrl device registered.
105 * @groups: groups table to provide group name and pin in the group to pinctrl.
106 * @group_names: group names table to provide all the group/pin names to
108 * @pins: pins table used for both pinctrl and gpio. pin_id, bank and line
109 * fields are set at probe time. Other ones are set when parsing dt
111 * @npins: number of pins.
112 * @gpio_chip: gpio chip registered.
113 * @irq_domain: irq domain for the gpio controller.
114 * @irqs: table containing the hw irq number of the bank. The index of the
115 * table is the bank id.
116 * @pm_wakeup_sources: bitmap of wakeup sources (lines)
117 * @pm_suspend_backup: backup/restore register values on suspend/resume
118 * @dev: device entry for the Atmel PIO controller.
119 * @node: node of the Atmel PIO controller.
121 struct atmel_pioctrl
{
122 void __iomem
*reg_base
;
125 struct pinctrl_dev
*pinctrl_dev
;
126 struct atmel_group
*groups
;
127 const char * const *group_names
;
128 struct atmel_pin
**pins
;
130 struct gpio_chip
*gpio_chip
;
131 struct irq_domain
*irq_domain
;
133 unsigned *pm_wakeup_sources
;
137 u32 cfgr
[ATMEL_PIO_NPINS_PER_BANK
];
138 } *pm_suspend_backup
;
140 struct device_node
*node
;
143 static const char * const atmel_functions
[] = {
144 "GPIO", "A", "B", "C", "D", "E", "F", "G"
147 static const struct pinconf_generic_params atmel_custom_bindings
[] = {
148 {"atmel,drive-strength", ATMEL_PIN_CONFIG_DRIVE_STRENGTH
, 0},
152 static unsigned int atmel_gpio_read(struct atmel_pioctrl
*atmel_pioctrl
,
153 unsigned int bank
, unsigned int reg
)
155 return readl_relaxed(atmel_pioctrl
->reg_base
156 + ATMEL_PIO_BANK_OFFSET
* bank
+ reg
);
159 static void atmel_gpio_write(struct atmel_pioctrl
*atmel_pioctrl
,
160 unsigned int bank
, unsigned int reg
,
163 writel_relaxed(val
, atmel_pioctrl
->reg_base
164 + ATMEL_PIO_BANK_OFFSET
* bank
+ reg
);
167 static void atmel_gpio_irq_ack(struct irq_data
*d
)
170 * Nothing to do, interrupt is cleared when reading the status
175 static int atmel_gpio_irq_set_type(struct irq_data
*d
, unsigned type
)
177 struct atmel_pioctrl
*atmel_pioctrl
= irq_data_get_irq_chip_data(d
);
178 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[d
->hwirq
];
181 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_MSKR
,
183 reg
= atmel_gpio_read(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
);
184 reg
&= (~ATMEL_PIO_CFGR_EVTSEL_MASK
);
187 case IRQ_TYPE_EDGE_RISING
:
188 irq_set_handler_locked(d
, handle_edge_irq
);
189 reg
|= ATMEL_PIO_CFGR_EVTSEL_RISING
;
191 case IRQ_TYPE_EDGE_FALLING
:
192 irq_set_handler_locked(d
, handle_edge_irq
);
193 reg
|= ATMEL_PIO_CFGR_EVTSEL_FALLING
;
195 case IRQ_TYPE_EDGE_BOTH
:
196 irq_set_handler_locked(d
, handle_edge_irq
);
197 reg
|= ATMEL_PIO_CFGR_EVTSEL_BOTH
;
199 case IRQ_TYPE_LEVEL_LOW
:
200 irq_set_handler_locked(d
, handle_level_irq
);
201 reg
|= ATMEL_PIO_CFGR_EVTSEL_LOW
;
203 case IRQ_TYPE_LEVEL_HIGH
:
204 irq_set_handler_locked(d
, handle_level_irq
);
205 reg
|= ATMEL_PIO_CFGR_EVTSEL_HIGH
;
212 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
, reg
);
217 static void atmel_gpio_irq_mask(struct irq_data
*d
)
219 struct atmel_pioctrl
*atmel_pioctrl
= irq_data_get_irq_chip_data(d
);
220 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[d
->hwirq
];
222 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_IDR
,
226 static void atmel_gpio_irq_unmask(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_IER
,
235 #ifdef CONFIG_PM_SLEEP
237 static int atmel_gpio_irq_set_wake(struct irq_data
*d
, unsigned int on
)
239 struct atmel_pioctrl
*atmel_pioctrl
= irq_data_get_irq_chip_data(d
);
240 int bank
= ATMEL_PIO_BANK(d
->hwirq
);
241 int line
= ATMEL_PIO_LINE(d
->hwirq
);
243 /* The gpio controller has one interrupt line per bank. */
244 irq_set_irq_wake(atmel_pioctrl
->irqs
[bank
], on
);
247 atmel_pioctrl
->pm_wakeup_sources
[bank
] |= BIT(line
);
249 atmel_pioctrl
->pm_wakeup_sources
[bank
] &= ~(BIT(line
));
254 #define atmel_gpio_irq_set_wake NULL
255 #endif /* CONFIG_PM_SLEEP */
257 static struct irq_chip atmel_gpio_irq_chip
= {
259 .irq_ack
= atmel_gpio_irq_ack
,
260 .irq_mask
= atmel_gpio_irq_mask
,
261 .irq_unmask
= atmel_gpio_irq_unmask
,
262 .irq_set_type
= atmel_gpio_irq_set_type
,
263 .irq_set_wake
= atmel_gpio_irq_set_wake
,
266 static int atmel_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
268 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
270 return irq_find_mapping(atmel_pioctrl
->irq_domain
, offset
);
273 static void atmel_gpio_irq_handler(struct irq_desc
*desc
)
275 unsigned int irq
= irq_desc_get_irq(desc
);
276 struct atmel_pioctrl
*atmel_pioctrl
= irq_desc_get_handler_data(desc
);
277 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
281 /* Find from which bank is the irq received. */
282 for (n
= 0; n
< atmel_pioctrl
->nbanks
; n
++) {
283 if (atmel_pioctrl
->irqs
[n
] == irq
) {
290 dev_err(atmel_pioctrl
->dev
,
291 "no bank associated to irq %u\n", irq
);
295 chained_irq_enter(chip
, desc
);
298 isr
= (unsigned long)atmel_gpio_read(atmel_pioctrl
, bank
,
300 isr
&= (unsigned long)atmel_gpio_read(atmel_pioctrl
, bank
,
305 for_each_set_bit(n
, &isr
, BITS_PER_LONG
)
306 generic_handle_irq(atmel_gpio_to_irq(
307 atmel_pioctrl
->gpio_chip
,
308 bank
* ATMEL_PIO_NPINS_PER_BANK
+ n
));
311 chained_irq_exit(chip
, desc
);
314 static int atmel_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
316 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
317 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[offset
];
320 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_MSKR
,
322 reg
= atmel_gpio_read(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
);
323 reg
&= ~ATMEL_PIO_DIR_MASK
;
324 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
, reg
);
329 static int atmel_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
331 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
332 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[offset
];
335 reg
= atmel_gpio_read(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_PDSR
);
337 return !!(reg
& BIT(pin
->line
));
340 static int atmel_gpio_get_multiple(struct gpio_chip
*chip
, unsigned long *mask
,
343 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
346 bitmap_zero(bits
, atmel_pioctrl
->npins
);
348 for (bank
= 0; bank
< atmel_pioctrl
->nbanks
; bank
++) {
349 unsigned int word
= bank
;
350 unsigned int offset
= 0;
353 #if ATMEL_PIO_NPINS_PER_BANK != BITS_PER_LONG
354 word
= BIT_WORD(bank
* ATMEL_PIO_NPINS_PER_BANK
);
355 offset
= bank
* ATMEL_PIO_NPINS_PER_BANK
% BITS_PER_LONG
;
360 reg
= atmel_gpio_read(atmel_pioctrl
, bank
, ATMEL_PIO_PDSR
);
361 bits
[word
] |= mask
[word
] & (reg
<< offset
);
367 static int atmel_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
370 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
371 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[offset
];
374 atmel_gpio_write(atmel_pioctrl
, pin
->bank
,
375 value
? ATMEL_PIO_SODR
: ATMEL_PIO_CODR
,
378 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_MSKR
,
380 reg
= atmel_gpio_read(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
);
381 reg
|= ATMEL_PIO_DIR_MASK
;
382 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
, reg
);
387 static void atmel_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int val
)
389 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
390 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[offset
];
392 atmel_gpio_write(atmel_pioctrl
, pin
->bank
,
393 val
? ATMEL_PIO_SODR
: ATMEL_PIO_CODR
,
397 static void atmel_gpio_set_multiple(struct gpio_chip
*chip
, unsigned long *mask
,
400 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
403 for (bank
= 0; bank
< atmel_pioctrl
->nbanks
; bank
++) {
404 unsigned int bitmask
;
405 unsigned int word
= bank
;
408 * On a 64-bit platform, BITS_PER_LONG is 64 so it is necessary to iterate over
409 * two 32bit words to handle the whole bitmask
411 #if ATMEL_PIO_NPINS_PER_BANK != BITS_PER_LONG
412 word
= BIT_WORD(bank
* ATMEL_PIO_NPINS_PER_BANK
);
417 bitmask
= mask
[word
] & bits
[word
];
418 atmel_gpio_write(atmel_pioctrl
, bank
, ATMEL_PIO_SODR
, bitmask
);
420 bitmask
= mask
[word
] & ~bits
[word
];
421 atmel_gpio_write(atmel_pioctrl
, bank
, ATMEL_PIO_CODR
, bitmask
);
423 #if ATMEL_PIO_NPINS_PER_BANK != BITS_PER_LONG
424 mask
[word
] >>= ATMEL_PIO_NPINS_PER_BANK
;
425 bits
[word
] >>= ATMEL_PIO_NPINS_PER_BANK
;
430 static struct gpio_chip atmel_gpio_chip
= {
431 .direction_input
= atmel_gpio_direction_input
,
432 .get
= atmel_gpio_get
,
433 .get_multiple
= atmel_gpio_get_multiple
,
434 .direction_output
= atmel_gpio_direction_output
,
435 .set
= atmel_gpio_set
,
436 .set_multiple
= atmel_gpio_set_multiple
,
437 .to_irq
= atmel_gpio_to_irq
,
441 /* --- PINCTRL --- */
442 static unsigned int atmel_pin_config_read(struct pinctrl_dev
*pctldev
,
445 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
446 unsigned bank
= atmel_pioctrl
->pins
[pin_id
]->bank
;
447 unsigned line
= atmel_pioctrl
->pins
[pin_id
]->line
;
448 void __iomem
*addr
= atmel_pioctrl
->reg_base
449 + bank
* ATMEL_PIO_BANK_OFFSET
;
451 writel_relaxed(BIT(line
), addr
+ ATMEL_PIO_MSKR
);
452 /* Have to set MSKR first, to access the right pin CFGR. */
455 return readl_relaxed(addr
+ ATMEL_PIO_CFGR
);
458 static void atmel_pin_config_write(struct pinctrl_dev
*pctldev
,
459 unsigned pin_id
, u32 conf
)
461 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
462 unsigned bank
= atmel_pioctrl
->pins
[pin_id
]->bank
;
463 unsigned line
= atmel_pioctrl
->pins
[pin_id
]->line
;
464 void __iomem
*addr
= atmel_pioctrl
->reg_base
465 + bank
* ATMEL_PIO_BANK_OFFSET
;
467 writel_relaxed(BIT(line
), addr
+ ATMEL_PIO_MSKR
);
468 /* Have to set MSKR first, to access the right pin CFGR. */
470 writel_relaxed(conf
, addr
+ ATMEL_PIO_CFGR
);
473 static int atmel_pctl_get_groups_count(struct pinctrl_dev
*pctldev
)
475 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
477 return atmel_pioctrl
->npins
;
480 static const char *atmel_pctl_get_group_name(struct pinctrl_dev
*pctldev
,
483 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
485 return atmel_pioctrl
->groups
[selector
].name
;
488 static int atmel_pctl_get_group_pins(struct pinctrl_dev
*pctldev
,
489 unsigned selector
, const unsigned **pins
,
492 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
494 *pins
= (unsigned *)&atmel_pioctrl
->groups
[selector
].pin
;
500 static struct atmel_group
*
501 atmel_pctl_find_group_by_pin(struct pinctrl_dev
*pctldev
, unsigned pin
)
503 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
506 for (i
= 0; i
< atmel_pioctrl
->npins
; i
++) {
507 struct atmel_group
*grp
= atmel_pioctrl
->groups
+ i
;
516 static int atmel_pctl_xlate_pinfunc(struct pinctrl_dev
*pctldev
,
517 struct device_node
*np
,
518 u32 pinfunc
, const char **grp_name
,
519 const char **func_name
)
521 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
522 unsigned pin_id
, func_id
;
523 struct atmel_group
*grp
;
525 pin_id
= ATMEL_GET_PIN_NO(pinfunc
);
526 func_id
= ATMEL_GET_PIN_FUNC(pinfunc
);
528 if (func_id
>= ARRAY_SIZE(atmel_functions
))
531 *func_name
= atmel_functions
[func_id
];
533 grp
= atmel_pctl_find_group_by_pin(pctldev
, pin_id
);
536 *grp_name
= grp
->name
;
538 atmel_pioctrl
->pins
[pin_id
]->mux
= func_id
;
539 atmel_pioctrl
->pins
[pin_id
]->ioset
= ATMEL_GET_PIN_IOSET(pinfunc
);
540 /* Want the device name not the group one. */
541 if (np
->parent
== atmel_pioctrl
->node
)
542 atmel_pioctrl
->pins
[pin_id
]->device
= np
->name
;
544 atmel_pioctrl
->pins
[pin_id
]->device
= np
->parent
->name
;
549 static int atmel_pctl_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
550 struct device_node
*np
,
551 struct pinctrl_map
**map
,
552 unsigned *reserved_maps
,
555 unsigned num_pins
, num_configs
, reserve
;
556 unsigned long *configs
;
557 struct property
*pins
;
561 pins
= of_find_property(np
, "pinmux", NULL
);
565 ret
= pinconf_generic_parse_dt_config(np
, pctldev
, &configs
,
568 dev_err(pctldev
->dev
, "%pOF: could not parse node property\n",
573 num_pins
= pins
->length
/ sizeof(u32
);
575 dev_err(pctldev
->dev
, "no pins found in node %pOF\n", np
);
581 * Reserve maps, at least there is a mux map and an optional conf
588 ret
= pinctrl_utils_reserve_map(pctldev
, map
, reserved_maps
, num_maps
,
593 for (i
= 0; i
< num_pins
; i
++) {
594 const char *group
, *func
;
596 ret
= of_property_read_u32_index(np
, "pinmux", i
, &pinfunc
);
600 ret
= atmel_pctl_xlate_pinfunc(pctldev
, np
, pinfunc
, &group
,
605 pinctrl_utils_add_map_mux(pctldev
, map
, reserved_maps
, num_maps
,
609 ret
= pinctrl_utils_add_map_configs(pctldev
, map
,
610 reserved_maps
, num_maps
, group
,
611 configs
, num_configs
,
612 PIN_MAP_TYPE_CONFIGS_GROUP
);
623 static int atmel_pctl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
624 struct device_node
*np_config
,
625 struct pinctrl_map
**map
,
628 struct device_node
*np
;
629 unsigned reserved_maps
;
637 * If all the pins of a device have the same configuration (or no one),
638 * it is useless to add a subnode, so directly parse node referenced by
641 ret
= atmel_pctl_dt_subnode_to_map(pctldev
, np_config
, map
,
642 &reserved_maps
, num_maps
);
644 for_each_child_of_node(np_config
, np
) {
645 ret
= atmel_pctl_dt_subnode_to_map(pctldev
, np
, map
,
646 &reserved_maps
, num_maps
);
655 pinctrl_utils_free_map(pctldev
, *map
, *num_maps
);
656 dev_err(pctldev
->dev
, "can't create maps for node %pOF\n",
663 static const struct pinctrl_ops atmel_pctlops
= {
664 .get_groups_count
= atmel_pctl_get_groups_count
,
665 .get_group_name
= atmel_pctl_get_group_name
,
666 .get_group_pins
= atmel_pctl_get_group_pins
,
667 .dt_node_to_map
= atmel_pctl_dt_node_to_map
,
668 .dt_free_map
= pinctrl_utils_free_map
,
671 static int atmel_pmx_get_functions_count(struct pinctrl_dev
*pctldev
)
673 return ARRAY_SIZE(atmel_functions
);
676 static const char *atmel_pmx_get_function_name(struct pinctrl_dev
*pctldev
,
679 return atmel_functions
[selector
];
682 static int atmel_pmx_get_function_groups(struct pinctrl_dev
*pctldev
,
684 const char * const **groups
,
685 unsigned * const num_groups
)
687 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
689 *groups
= atmel_pioctrl
->group_names
;
690 *num_groups
= atmel_pioctrl
->npins
;
695 static int atmel_pmx_set_mux(struct pinctrl_dev
*pctldev
,
699 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
703 dev_dbg(pctldev
->dev
, "enable function %s group %s\n",
704 atmel_functions
[function
], atmel_pioctrl
->groups
[group
].name
);
706 pin
= atmel_pioctrl
->groups
[group
].pin
;
707 conf
= atmel_pin_config_read(pctldev
, pin
);
708 conf
&= (~ATMEL_PIO_CFGR_FUNC_MASK
);
709 conf
|= (function
& ATMEL_PIO_CFGR_FUNC_MASK
);
710 dev_dbg(pctldev
->dev
, "pin: %u, conf: 0x%08x\n", pin
, conf
);
711 atmel_pin_config_write(pctldev
, pin
, conf
);
716 static const struct pinmux_ops atmel_pmxops
= {
717 .get_functions_count
= atmel_pmx_get_functions_count
,
718 .get_function_name
= atmel_pmx_get_function_name
,
719 .get_function_groups
= atmel_pmx_get_function_groups
,
720 .set_mux
= atmel_pmx_set_mux
,
723 static int atmel_conf_pin_config_group_get(struct pinctrl_dev
*pctldev
,
725 unsigned long *config
)
727 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
728 unsigned param
= pinconf_to_config_param(*config
), arg
= 0;
729 struct atmel_group
*grp
= atmel_pioctrl
->groups
+ group
;
730 unsigned pin_id
= grp
->pin
;
733 res
= atmel_pin_config_read(pctldev
, pin_id
);
736 case PIN_CONFIG_BIAS_PULL_UP
:
737 if (!(res
& ATMEL_PIO_PUEN_MASK
))
741 case PIN_CONFIG_BIAS_PULL_DOWN
:
742 if ((res
& ATMEL_PIO_PUEN_MASK
) ||
743 (!(res
& ATMEL_PIO_PDEN_MASK
)))
747 case PIN_CONFIG_BIAS_DISABLE
:
748 if ((res
& ATMEL_PIO_PUEN_MASK
) ||
749 ((res
& ATMEL_PIO_PDEN_MASK
)))
753 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
754 if (!(res
& ATMEL_PIO_OPD_MASK
))
758 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
759 if (!(res
& ATMEL_PIO_SCHMITT_MASK
))
763 case ATMEL_PIN_CONFIG_DRIVE_STRENGTH
:
764 if (!(res
& ATMEL_PIO_DRVSTR_MASK
))
766 arg
= (res
& ATMEL_PIO_DRVSTR_MASK
) >> ATMEL_PIO_DRVSTR_OFFSET
;
772 *config
= pinconf_to_config_packed(param
, arg
);
776 static int atmel_conf_pin_config_group_set(struct pinctrl_dev
*pctldev
,
778 unsigned long *configs
,
779 unsigned num_configs
)
781 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
782 struct atmel_group
*grp
= atmel_pioctrl
->groups
+ group
;
783 unsigned bank
, pin
, pin_id
= grp
->pin
;
787 conf
= atmel_pin_config_read(pctldev
, pin_id
);
789 for (i
= 0; i
< num_configs
; i
++) {
790 unsigned param
= pinconf_to_config_param(configs
[i
]);
791 unsigned arg
= pinconf_to_config_argument(configs
[i
]);
793 dev_dbg(pctldev
->dev
, "%s: pin=%u, config=0x%lx\n",
794 __func__
, pin_id
, configs
[i
]);
797 case PIN_CONFIG_BIAS_DISABLE
:
798 conf
&= (~ATMEL_PIO_PUEN_MASK
);
799 conf
&= (~ATMEL_PIO_PDEN_MASK
);
801 case PIN_CONFIG_BIAS_PULL_UP
:
802 conf
|= ATMEL_PIO_PUEN_MASK
;
803 conf
&= (~ATMEL_PIO_PDEN_MASK
);
805 case PIN_CONFIG_BIAS_PULL_DOWN
:
806 conf
|= ATMEL_PIO_PDEN_MASK
;
807 conf
&= (~ATMEL_PIO_PUEN_MASK
);
809 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
811 conf
&= (~ATMEL_PIO_OPD_MASK
);
813 conf
|= ATMEL_PIO_OPD_MASK
;
815 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
817 conf
|= ATMEL_PIO_SCHMITT_MASK
;
819 conf
&= (~ATMEL_PIO_SCHMITT_MASK
);
821 case PIN_CONFIG_INPUT_DEBOUNCE
:
823 conf
&= (~ATMEL_PIO_IFEN_MASK
);
824 conf
&= (~ATMEL_PIO_IFSCEN_MASK
);
827 * We don't care about the debounce value for several reasons:
828 * - can't have different debounce periods inside a same group,
829 * - the register to configure this period is a secure register.
830 * The debouncing filter can filter a pulse with a duration of less
831 * than 1/2 slow clock period.
833 conf
|= ATMEL_PIO_IFEN_MASK
;
834 conf
|= ATMEL_PIO_IFSCEN_MASK
;
837 case PIN_CONFIG_OUTPUT
:
838 conf
|= ATMEL_PIO_DIR_MASK
;
839 bank
= ATMEL_PIO_BANK(pin_id
);
840 pin
= ATMEL_PIO_LINE(pin_id
);
844 writel_relaxed(mask
, atmel_pioctrl
->reg_base
+
845 bank
* ATMEL_PIO_BANK_OFFSET
+
848 writel_relaxed(mask
, atmel_pioctrl
->reg_base
+
849 bank
* ATMEL_PIO_BANK_OFFSET
+
853 case ATMEL_PIN_CONFIG_DRIVE_STRENGTH
:
855 case ATMEL_PIO_DRVSTR_LO
:
856 case ATMEL_PIO_DRVSTR_ME
:
857 case ATMEL_PIO_DRVSTR_HI
:
858 conf
&= (~ATMEL_PIO_DRVSTR_MASK
);
859 conf
|= arg
<< ATMEL_PIO_DRVSTR_OFFSET
;
862 dev_warn(pctldev
->dev
, "drive strength not updated (incorrect value)\n");
866 dev_warn(pctldev
->dev
,
867 "unsupported configuration parameter: %u\n",
873 dev_dbg(pctldev
->dev
, "%s: reg=0x%08x\n", __func__
, conf
);
874 atmel_pin_config_write(pctldev
, pin_id
, conf
);
879 static void atmel_conf_pin_config_dbg_show(struct pinctrl_dev
*pctldev
,
880 struct seq_file
*s
, unsigned pin_id
)
882 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
885 if (!atmel_pioctrl
->pins
[pin_id
]->device
)
888 if (atmel_pioctrl
->pins
[pin_id
])
889 seq_printf(s
, " (%s, ioset %u) ",
890 atmel_pioctrl
->pins
[pin_id
]->device
,
891 atmel_pioctrl
->pins
[pin_id
]->ioset
);
893 conf
= atmel_pin_config_read(pctldev
, pin_id
);
894 if (conf
& ATMEL_PIO_PUEN_MASK
)
895 seq_printf(s
, "%s ", "pull-up");
896 if (conf
& ATMEL_PIO_PDEN_MASK
)
897 seq_printf(s
, "%s ", "pull-down");
898 if (conf
& ATMEL_PIO_IFEN_MASK
)
899 seq_printf(s
, "%s ", "debounce");
900 if (conf
& ATMEL_PIO_OPD_MASK
)
901 seq_printf(s
, "%s ", "open-drain");
902 if (conf
& ATMEL_PIO_SCHMITT_MASK
)
903 seq_printf(s
, "%s ", "schmitt");
904 if (conf
& ATMEL_PIO_DRVSTR_MASK
) {
905 switch ((conf
& ATMEL_PIO_DRVSTR_MASK
) >> ATMEL_PIO_DRVSTR_OFFSET
) {
906 case ATMEL_PIO_DRVSTR_ME
:
907 seq_printf(s
, "%s ", "medium-drive");
909 case ATMEL_PIO_DRVSTR_HI
:
910 seq_printf(s
, "%s ", "high-drive");
912 /* ATMEL_PIO_DRVSTR_LO and 0 which is the default value at reset */
914 seq_printf(s
, "%s ", "low-drive");
919 static const struct pinconf_ops atmel_confops
= {
920 .pin_config_group_get
= atmel_conf_pin_config_group_get
,
921 .pin_config_group_set
= atmel_conf_pin_config_group_set
,
922 .pin_config_dbg_show
= atmel_conf_pin_config_dbg_show
,
925 static struct pinctrl_desc atmel_pinctrl_desc
= {
926 .name
= "atmel_pinctrl",
927 .confops
= &atmel_confops
,
928 .pctlops
= &atmel_pctlops
,
929 .pmxops
= &atmel_pmxops
,
932 static int __maybe_unused
atmel_pctrl_suspend(struct device
*dev
)
934 struct atmel_pioctrl
*atmel_pioctrl
= dev_get_drvdata(dev
);
938 * For each bank, save IMR to restore it later and disable all GPIO
939 * interrupts excepting the ones marked as wakeup sources.
941 for (i
= 0; i
< atmel_pioctrl
->nbanks
; i
++) {
942 atmel_pioctrl
->pm_suspend_backup
[i
].imr
=
943 atmel_gpio_read(atmel_pioctrl
, i
, ATMEL_PIO_IMR
);
944 atmel_gpio_write(atmel_pioctrl
, i
, ATMEL_PIO_IDR
,
945 ~atmel_pioctrl
->pm_wakeup_sources
[i
]);
946 atmel_pioctrl
->pm_suspend_backup
[i
].odsr
=
947 atmel_gpio_read(atmel_pioctrl
, i
, ATMEL_PIO_ODSR
);
948 for (j
= 0; j
< ATMEL_PIO_NPINS_PER_BANK
; j
++) {
949 atmel_gpio_write(atmel_pioctrl
, i
,
950 ATMEL_PIO_MSKR
, BIT(j
));
951 atmel_pioctrl
->pm_suspend_backup
[i
].cfgr
[j
] =
952 atmel_gpio_read(atmel_pioctrl
, i
,
960 static int __maybe_unused
atmel_pctrl_resume(struct device
*dev
)
962 struct atmel_pioctrl
*atmel_pioctrl
= dev_get_drvdata(dev
);
965 for (i
= 0; i
< atmel_pioctrl
->nbanks
; i
++) {
966 atmel_gpio_write(atmel_pioctrl
, i
, ATMEL_PIO_IER
,
967 atmel_pioctrl
->pm_suspend_backup
[i
].imr
);
968 atmel_gpio_write(atmel_pioctrl
, i
, ATMEL_PIO_SODR
,
969 atmel_pioctrl
->pm_suspend_backup
[i
].odsr
);
970 for (j
= 0; j
< ATMEL_PIO_NPINS_PER_BANK
; j
++) {
971 atmel_gpio_write(atmel_pioctrl
, i
,
972 ATMEL_PIO_MSKR
, BIT(j
));
973 atmel_gpio_write(atmel_pioctrl
, i
, ATMEL_PIO_CFGR
,
974 atmel_pioctrl
->pm_suspend_backup
[i
].cfgr
[j
]);
981 static const struct dev_pm_ops atmel_pctrl_pm_ops
= {
982 SET_SYSTEM_SLEEP_PM_OPS(atmel_pctrl_suspend
, atmel_pctrl_resume
)
986 * The number of banks can be different from a SoC to another one.
987 * We can have up to 16 banks.
989 static const struct atmel_pioctrl_data atmel_sama5d2_pioctrl_data
= {
991 .last_bank_count
= ATMEL_PIO_NPINS_PER_BANK
,
994 static const struct atmel_pioctrl_data microchip_sama7g5_pioctrl_data
= {
996 .last_bank_count
= 8, /* sama7g5 has only PE0 to PE7 */
999 static const struct of_device_id atmel_pctrl_of_match
[] = {
1001 .compatible
= "atmel,sama5d2-pinctrl",
1002 .data
= &atmel_sama5d2_pioctrl_data
,
1004 .compatible
= "microchip,sama7g5-pinctrl",
1005 .data
= µchip_sama7g5_pioctrl_data
,
1011 static int atmel_pinctrl_probe(struct platform_device
*pdev
)
1013 struct device
*dev
= &pdev
->dev
;
1014 struct pinctrl_pin_desc
*pin_desc
;
1015 const char **group_names
;
1016 const struct of_device_id
*match
;
1018 struct resource
*res
;
1019 struct atmel_pioctrl
*atmel_pioctrl
;
1020 const struct atmel_pioctrl_data
*atmel_pioctrl_data
;
1022 atmel_pioctrl
= devm_kzalloc(dev
, sizeof(*atmel_pioctrl
), GFP_KERNEL
);
1025 atmel_pioctrl
->dev
= dev
;
1026 atmel_pioctrl
->node
= dev
->of_node
;
1027 platform_set_drvdata(pdev
, atmel_pioctrl
);
1029 match
= of_match_node(atmel_pctrl_of_match
, dev
->of_node
);
1031 dev_err(dev
, "unknown compatible string\n");
1034 atmel_pioctrl_data
= match
->data
;
1035 atmel_pioctrl
->nbanks
= atmel_pioctrl_data
->nbanks
;
1036 atmel_pioctrl
->npins
= atmel_pioctrl
->nbanks
* ATMEL_PIO_NPINS_PER_BANK
;
1037 /* if last bank has limited number of pins, adjust accordingly */
1038 if (atmel_pioctrl_data
->last_bank_count
!= ATMEL_PIO_NPINS_PER_BANK
) {
1039 atmel_pioctrl
->npins
-= ATMEL_PIO_NPINS_PER_BANK
;
1040 atmel_pioctrl
->npins
+= atmel_pioctrl_data
->last_bank_count
;
1043 atmel_pioctrl
->reg_base
= devm_platform_ioremap_resource(pdev
, 0);
1044 if (IS_ERR(atmel_pioctrl
->reg_base
))
1045 return PTR_ERR(atmel_pioctrl
->reg_base
);
1047 atmel_pioctrl
->clk
= devm_clk_get(dev
, NULL
);
1048 if (IS_ERR(atmel_pioctrl
->clk
)) {
1049 dev_err(dev
, "failed to get clock\n");
1050 return PTR_ERR(atmel_pioctrl
->clk
);
1053 atmel_pioctrl
->pins
= devm_kcalloc(dev
,
1054 atmel_pioctrl
->npins
,
1055 sizeof(*atmel_pioctrl
->pins
),
1057 if (!atmel_pioctrl
->pins
)
1060 pin_desc
= devm_kcalloc(dev
, atmel_pioctrl
->npins
, sizeof(*pin_desc
),
1064 atmel_pinctrl_desc
.pins
= pin_desc
;
1065 atmel_pinctrl_desc
.npins
= atmel_pioctrl
->npins
;
1066 atmel_pinctrl_desc
.num_custom_params
= ARRAY_SIZE(atmel_custom_bindings
);
1067 atmel_pinctrl_desc
.custom_params
= atmel_custom_bindings
;
1069 /* One pin is one group since a pin can achieve all functions. */
1070 group_names
= devm_kcalloc(dev
,
1071 atmel_pioctrl
->npins
, sizeof(*group_names
),
1075 atmel_pioctrl
->group_names
= group_names
;
1077 atmel_pioctrl
->groups
= devm_kcalloc(&pdev
->dev
,
1078 atmel_pioctrl
->npins
, sizeof(*atmel_pioctrl
->groups
),
1080 if (!atmel_pioctrl
->groups
)
1082 for (i
= 0 ; i
< atmel_pioctrl
->npins
; i
++) {
1083 struct atmel_group
*group
= atmel_pioctrl
->groups
+ i
;
1084 unsigned bank
= ATMEL_PIO_BANK(i
);
1085 unsigned line
= ATMEL_PIO_LINE(i
);
1087 atmel_pioctrl
->pins
[i
] = devm_kzalloc(dev
,
1088 sizeof(**atmel_pioctrl
->pins
), GFP_KERNEL
);
1089 if (!atmel_pioctrl
->pins
[i
])
1092 atmel_pioctrl
->pins
[i
]->pin_id
= i
;
1093 atmel_pioctrl
->pins
[i
]->bank
= bank
;
1094 atmel_pioctrl
->pins
[i
]->line
= line
;
1096 pin_desc
[i
].number
= i
;
1097 /* Pin naming convention: P(bank_name)(bank_pin_number). */
1098 pin_desc
[i
].name
= kasprintf(GFP_KERNEL
, "P%c%d",
1101 group
->name
= group_names
[i
] = pin_desc
[i
].name
;
1102 group
->pin
= pin_desc
[i
].number
;
1104 dev_dbg(dev
, "pin_id=%u, bank=%u, line=%u", i
, bank
, line
);
1107 atmel_pioctrl
->gpio_chip
= &atmel_gpio_chip
;
1108 atmel_pioctrl
->gpio_chip
->of_node
= dev
->of_node
;
1109 atmel_pioctrl
->gpio_chip
->ngpio
= atmel_pioctrl
->npins
;
1110 atmel_pioctrl
->gpio_chip
->label
= dev_name(dev
);
1111 atmel_pioctrl
->gpio_chip
->parent
= dev
;
1112 atmel_pioctrl
->gpio_chip
->names
= atmel_pioctrl
->group_names
;
1114 atmel_pioctrl
->pm_wakeup_sources
= devm_kcalloc(dev
,
1115 atmel_pioctrl
->nbanks
,
1116 sizeof(*atmel_pioctrl
->pm_wakeup_sources
),
1118 if (!atmel_pioctrl
->pm_wakeup_sources
)
1121 atmel_pioctrl
->pm_suspend_backup
= devm_kcalloc(dev
,
1122 atmel_pioctrl
->nbanks
,
1123 sizeof(*atmel_pioctrl
->pm_suspend_backup
),
1125 if (!atmel_pioctrl
->pm_suspend_backup
)
1128 atmel_pioctrl
->irqs
= devm_kcalloc(dev
,
1129 atmel_pioctrl
->nbanks
,
1130 sizeof(*atmel_pioctrl
->irqs
),
1132 if (!atmel_pioctrl
->irqs
)
1135 /* There is one controller but each bank has its own irq line. */
1136 for (i
= 0; i
< atmel_pioctrl
->nbanks
; i
++) {
1137 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, i
);
1139 dev_err(dev
, "missing irq resource for group %c\n",
1143 atmel_pioctrl
->irqs
[i
] = res
->start
;
1144 irq_set_chained_handler_and_data(res
->start
,
1145 atmel_gpio_irq_handler
, atmel_pioctrl
);
1146 dev_dbg(dev
, "bank %i: irq=%pr\n", i
, res
);
1149 atmel_pioctrl
->irq_domain
= irq_domain_add_linear(dev
->of_node
,
1150 atmel_pioctrl
->gpio_chip
->ngpio
,
1151 &irq_domain_simple_ops
, NULL
);
1152 if (!atmel_pioctrl
->irq_domain
) {
1153 dev_err(dev
, "can't add the irq domain\n");
1156 atmel_pioctrl
->irq_domain
->name
= "atmel gpio";
1158 for (i
= 0; i
< atmel_pioctrl
->npins
; i
++) {
1159 int irq
= irq_create_mapping(atmel_pioctrl
->irq_domain
, i
);
1161 irq_set_chip_and_handler(irq
, &atmel_gpio_irq_chip
,
1163 irq_set_chip_data(irq
, atmel_pioctrl
);
1165 "atmel gpio irq domain: hwirq: %d, linux irq: %d\n",
1169 ret
= clk_prepare_enable(atmel_pioctrl
->clk
);
1171 dev_err(dev
, "failed to prepare and enable clock\n");
1172 goto clk_prepare_enable_error
;
1175 atmel_pioctrl
->pinctrl_dev
= devm_pinctrl_register(&pdev
->dev
,
1176 &atmel_pinctrl_desc
,
1178 if (IS_ERR(atmel_pioctrl
->pinctrl_dev
)) {
1179 ret
= PTR_ERR(atmel_pioctrl
->pinctrl_dev
);
1180 dev_err(dev
, "pinctrl registration failed\n");
1184 ret
= gpiochip_add_data(atmel_pioctrl
->gpio_chip
, atmel_pioctrl
);
1186 dev_err(dev
, "failed to add gpiochip\n");
1190 ret
= gpiochip_add_pin_range(atmel_pioctrl
->gpio_chip
, dev_name(dev
),
1191 0, 0, atmel_pioctrl
->gpio_chip
->ngpio
);
1193 dev_err(dev
, "failed to add gpio pin range\n");
1194 goto gpiochip_add_pin_range_error
;
1197 dev_info(&pdev
->dev
, "atmel pinctrl initialized\n");
1201 gpiochip_add_pin_range_error
:
1202 gpiochip_remove(atmel_pioctrl
->gpio_chip
);
1205 clk_disable_unprepare(atmel_pioctrl
->clk
);
1207 clk_prepare_enable_error
:
1208 irq_domain_remove(atmel_pioctrl
->irq_domain
);
1213 static struct platform_driver atmel_pinctrl_driver
= {
1215 .name
= "pinctrl-at91-pio4",
1216 .of_match_table
= atmel_pctrl_of_match
,
1217 .pm
= &atmel_pctrl_pm_ops
,
1218 .suppress_bind_attrs
= true,
1220 .probe
= atmel_pinctrl_probe
,
1222 builtin_platform_driver(atmel_pinctrl_driver
);