2 * Driver for the Atmel PIO4 controller
4 * Copyright (C) 2015 Atmel,
5 * 2015 Ludovic Desroches <ludovic.desroches@atmel.com>
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <dt-bindings/pinctrl/at91.h>
18 #include <linux/clk.h>
19 #include <linux/gpio/driver.h>
20 /* FIXME: needed for gpio_to_irq(), get rid of this */
21 #include <linux/gpio.h>
22 #include <linux/interrupt.h>
24 #include <linux/init.h>
26 #include <linux/platform_device.h>
27 #include <linux/pinctrl/pinconf.h>
28 #include <linux/pinctrl/pinconf-generic.h>
29 #include <linux/pinctrl/pinctrl.h>
30 #include <linux/pinctrl/pinmux.h>
31 #include <linux/slab.h>
34 #include "pinctrl-utils.h"
38 * In order to not introduce confusion between Atmel PIO groups and pinctrl
39 * framework groups, Atmel PIO groups will be called banks, line is kept to
40 * designed the pin id into this bank.
43 #define ATMEL_PIO_MSKR 0x0000
44 #define ATMEL_PIO_CFGR 0x0004
45 #define ATMEL_PIO_CFGR_FUNC_MASK GENMASK(2, 0)
46 #define ATMEL_PIO_DIR_MASK BIT(8)
47 #define ATMEL_PIO_PUEN_MASK BIT(9)
48 #define ATMEL_PIO_PDEN_MASK BIT(10)
49 #define ATMEL_PIO_IFEN_MASK BIT(12)
50 #define ATMEL_PIO_IFSCEN_MASK BIT(13)
51 #define ATMEL_PIO_OPD_MASK BIT(14)
52 #define ATMEL_PIO_SCHMITT_MASK BIT(15)
53 #define ATMEL_PIO_DRVSTR_MASK GENMASK(17, 16)
54 #define ATMEL_PIO_DRVSTR_OFFSET 16
55 #define ATMEL_PIO_CFGR_EVTSEL_MASK GENMASK(26, 24)
56 #define ATMEL_PIO_CFGR_EVTSEL_FALLING (0 << 24)
57 #define ATMEL_PIO_CFGR_EVTSEL_RISING (1 << 24)
58 #define ATMEL_PIO_CFGR_EVTSEL_BOTH (2 << 24)
59 #define ATMEL_PIO_CFGR_EVTSEL_LOW (3 << 24)
60 #define ATMEL_PIO_CFGR_EVTSEL_HIGH (4 << 24)
61 #define ATMEL_PIO_PDSR 0x0008
62 #define ATMEL_PIO_LOCKSR 0x000C
63 #define ATMEL_PIO_SODR 0x0010
64 #define ATMEL_PIO_CODR 0x0014
65 #define ATMEL_PIO_ODSR 0x0018
66 #define ATMEL_PIO_IER 0x0020
67 #define ATMEL_PIO_IDR 0x0024
68 #define ATMEL_PIO_IMR 0x0028
69 #define ATMEL_PIO_ISR 0x002C
70 #define ATMEL_PIO_IOFR 0x003C
72 #define ATMEL_PIO_NPINS_PER_BANK 32
73 #define ATMEL_PIO_BANK(pin_id) (pin_id / ATMEL_PIO_NPINS_PER_BANK)
74 #define ATMEL_PIO_LINE(pin_id) (pin_id % ATMEL_PIO_NPINS_PER_BANK)
75 #define ATMEL_PIO_BANK_OFFSET 0x40
77 #define ATMEL_GET_PIN_NO(pinfunc) ((pinfunc) & 0xff)
78 #define ATMEL_GET_PIN_FUNC(pinfunc) ((pinfunc >> 16) & 0xf)
79 #define ATMEL_GET_PIN_IOSET(pinfunc) ((pinfunc >> 20) & 0xf)
81 /* Custom pinconf parameters */
82 #define ATMEL_PIN_CONFIG_DRIVE_STRENGTH (PIN_CONFIG_END + 1)
84 struct atmel_pioctrl_data
{
103 * struct atmel_pioctrl - Atmel PIO controller (pinmux + gpio)
104 * @reg_base: base address of the controller.
105 * @clk: clock of the controller.
106 * @nbanks: number of PIO groups, it can vary depending on the SoC.
107 * @pinctrl_dev: pinctrl device registered.
108 * @groups: groups table to provide group name and pin in the group to pinctrl.
109 * @group_names: group names table to provide all the group/pin names to
111 * @pins: pins table used for both pinctrl and gpio. pin_id, bank and line
112 * fields are set at probe time. Other ones are set when parsing dt
114 * @npins: number of pins.
115 * @gpio_chip: gpio chip registered.
116 * @irq_domain: irq domain for the gpio controller.
117 * @irqs: table containing the hw irq number of the bank. The index of the
118 * table is the bank id.
119 * @dev: device entry for the Atmel PIO controller.
120 * @node: node of the Atmel PIO controller.
122 struct atmel_pioctrl
{
123 void __iomem
*reg_base
;
126 struct pinctrl_dev
*pinctrl_dev
;
127 struct atmel_group
*groups
;
128 const char * const *group_names
;
129 struct atmel_pin
**pins
;
131 struct gpio_chip
*gpio_chip
;
132 struct irq_domain
*irq_domain
;
134 unsigned *pm_wakeup_sources
;
138 u32 cfgr
[ATMEL_PIO_NPINS_PER_BANK
];
139 } *pm_suspend_backup
;
141 struct device_node
*node
;
144 static const char * const atmel_functions
[] = {
145 "GPIO", "A", "B", "C", "D", "E", "F", "G"
148 static const struct pinconf_generic_params atmel_custom_bindings
[] = {
149 {"atmel,drive-strength", ATMEL_PIN_CONFIG_DRIVE_STRENGTH
, 0},
153 static unsigned int atmel_gpio_read(struct atmel_pioctrl
*atmel_pioctrl
,
154 unsigned int bank
, unsigned int reg
)
156 return readl_relaxed(atmel_pioctrl
->reg_base
157 + ATMEL_PIO_BANK_OFFSET
* bank
+ reg
);
160 static void atmel_gpio_write(struct atmel_pioctrl
*atmel_pioctrl
,
161 unsigned int bank
, unsigned int reg
,
164 writel_relaxed(val
, atmel_pioctrl
->reg_base
165 + ATMEL_PIO_BANK_OFFSET
* bank
+ reg
);
168 static void atmel_gpio_irq_ack(struct irq_data
*d
)
171 * Nothing to do, interrupt is cleared when reading the status
176 static int atmel_gpio_irq_set_type(struct irq_data
*d
, unsigned type
)
178 struct atmel_pioctrl
*atmel_pioctrl
= irq_data_get_irq_chip_data(d
);
179 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[d
->hwirq
];
182 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_MSKR
,
184 reg
= atmel_gpio_read(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
);
185 reg
&= (~ATMEL_PIO_CFGR_EVTSEL_MASK
);
188 case IRQ_TYPE_EDGE_RISING
:
189 irq_set_handler_locked(d
, handle_edge_irq
);
190 reg
|= ATMEL_PIO_CFGR_EVTSEL_RISING
;
192 case IRQ_TYPE_EDGE_FALLING
:
193 irq_set_handler_locked(d
, handle_edge_irq
);
194 reg
|= ATMEL_PIO_CFGR_EVTSEL_FALLING
;
196 case IRQ_TYPE_EDGE_BOTH
:
197 irq_set_handler_locked(d
, handle_edge_irq
);
198 reg
|= ATMEL_PIO_CFGR_EVTSEL_BOTH
;
200 case IRQ_TYPE_LEVEL_LOW
:
201 irq_set_handler_locked(d
, handle_level_irq
);
202 reg
|= ATMEL_PIO_CFGR_EVTSEL_LOW
;
204 case IRQ_TYPE_LEVEL_HIGH
:
205 irq_set_handler_locked(d
, handle_level_irq
);
206 reg
|= ATMEL_PIO_CFGR_EVTSEL_HIGH
;
213 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
, reg
);
218 static void atmel_gpio_irq_mask(struct irq_data
*d
)
220 struct atmel_pioctrl
*atmel_pioctrl
= irq_data_get_irq_chip_data(d
);
221 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[d
->hwirq
];
223 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_IDR
,
227 static void atmel_gpio_irq_unmask(struct irq_data
*d
)
229 struct atmel_pioctrl
*atmel_pioctrl
= irq_data_get_irq_chip_data(d
);
230 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[d
->hwirq
];
232 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_IER
,
236 #ifdef CONFIG_PM_SLEEP
238 static int atmel_gpio_irq_set_wake(struct irq_data
*d
, unsigned int on
)
240 struct atmel_pioctrl
*atmel_pioctrl
= irq_data_get_irq_chip_data(d
);
241 int bank
= ATMEL_PIO_BANK(d
->hwirq
);
242 int line
= ATMEL_PIO_LINE(d
->hwirq
);
244 /* The gpio controller has one interrupt line per bank. */
245 irq_set_irq_wake(atmel_pioctrl
->irqs
[bank
], on
);
248 atmel_pioctrl
->pm_wakeup_sources
[bank
] |= BIT(line
);
250 atmel_pioctrl
->pm_wakeup_sources
[bank
] &= ~(BIT(line
));
255 #define atmel_gpio_irq_set_wake NULL
256 #endif /* CONFIG_PM_SLEEP */
258 static struct irq_chip atmel_gpio_irq_chip
= {
260 .irq_ack
= atmel_gpio_irq_ack
,
261 .irq_mask
= atmel_gpio_irq_mask
,
262 .irq_unmask
= atmel_gpio_irq_unmask
,
263 .irq_set_type
= atmel_gpio_irq_set_type
,
264 .irq_set_wake
= atmel_gpio_irq_set_wake
,
267 static void atmel_gpio_irq_handler(struct irq_desc
*desc
)
269 unsigned int irq
= irq_desc_get_irq(desc
);
270 struct atmel_pioctrl
*atmel_pioctrl
= irq_desc_get_handler_data(desc
);
271 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
275 /* Find from which bank is the irq received. */
276 for (n
= 0; n
< atmel_pioctrl
->nbanks
; n
++) {
277 if (atmel_pioctrl
->irqs
[n
] == irq
) {
284 dev_err(atmel_pioctrl
->dev
,
285 "no bank associated to irq %u\n", irq
);
289 chained_irq_enter(chip
, desc
);
292 isr
= (unsigned long)atmel_gpio_read(atmel_pioctrl
, bank
,
294 isr
&= (unsigned long)atmel_gpio_read(atmel_pioctrl
, bank
,
299 for_each_set_bit(n
, &isr
, BITS_PER_LONG
)
300 generic_handle_irq(gpio_to_irq(bank
*
301 ATMEL_PIO_NPINS_PER_BANK
+ n
));
304 chained_irq_exit(chip
, desc
);
307 static int atmel_gpio_direction_input(struct gpio_chip
*chip
, unsigned offset
)
309 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
310 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[offset
];
313 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_MSKR
,
315 reg
= atmel_gpio_read(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
);
316 reg
&= ~ATMEL_PIO_DIR_MASK
;
317 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
, reg
);
322 static int atmel_gpio_get(struct gpio_chip
*chip
, unsigned offset
)
324 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
325 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[offset
];
328 reg
= atmel_gpio_read(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_PDSR
);
330 return !!(reg
& BIT(pin
->line
));
333 static int atmel_gpio_direction_output(struct gpio_chip
*chip
, unsigned offset
,
336 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
337 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[offset
];
340 atmel_gpio_write(atmel_pioctrl
, pin
->bank
,
341 value
? ATMEL_PIO_SODR
: ATMEL_PIO_CODR
,
344 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_MSKR
,
346 reg
= atmel_gpio_read(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
);
347 reg
|= ATMEL_PIO_DIR_MASK
;
348 atmel_gpio_write(atmel_pioctrl
, pin
->bank
, ATMEL_PIO_CFGR
, reg
);
353 static void atmel_gpio_set(struct gpio_chip
*chip
, unsigned offset
, int val
)
355 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
356 struct atmel_pin
*pin
= atmel_pioctrl
->pins
[offset
];
358 atmel_gpio_write(atmel_pioctrl
, pin
->bank
,
359 val
? ATMEL_PIO_SODR
: ATMEL_PIO_CODR
,
363 static int atmel_gpio_to_irq(struct gpio_chip
*chip
, unsigned offset
)
365 struct atmel_pioctrl
*atmel_pioctrl
= gpiochip_get_data(chip
);
367 return irq_find_mapping(atmel_pioctrl
->irq_domain
, offset
);
370 static struct gpio_chip atmel_gpio_chip
= {
371 .direction_input
= atmel_gpio_direction_input
,
372 .get
= atmel_gpio_get
,
373 .direction_output
= atmel_gpio_direction_output
,
374 .set
= atmel_gpio_set
,
375 .to_irq
= atmel_gpio_to_irq
,
379 /* --- PINCTRL --- */
380 static unsigned int atmel_pin_config_read(struct pinctrl_dev
*pctldev
,
383 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
384 unsigned bank
= atmel_pioctrl
->pins
[pin_id
]->bank
;
385 unsigned line
= atmel_pioctrl
->pins
[pin_id
]->line
;
386 void __iomem
*addr
= atmel_pioctrl
->reg_base
387 + bank
* ATMEL_PIO_BANK_OFFSET
;
389 writel_relaxed(BIT(line
), addr
+ ATMEL_PIO_MSKR
);
390 /* Have to set MSKR first, to access the right pin CFGR. */
393 return readl_relaxed(addr
+ ATMEL_PIO_CFGR
);
396 static void atmel_pin_config_write(struct pinctrl_dev
*pctldev
,
397 unsigned pin_id
, u32 conf
)
399 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
400 unsigned bank
= atmel_pioctrl
->pins
[pin_id
]->bank
;
401 unsigned line
= atmel_pioctrl
->pins
[pin_id
]->line
;
402 void __iomem
*addr
= atmel_pioctrl
->reg_base
403 + bank
* ATMEL_PIO_BANK_OFFSET
;
405 writel_relaxed(BIT(line
), addr
+ ATMEL_PIO_MSKR
);
406 /* Have to set MSKR first, to access the right pin CFGR. */
408 writel_relaxed(conf
, addr
+ ATMEL_PIO_CFGR
);
411 static int atmel_pctl_get_groups_count(struct pinctrl_dev
*pctldev
)
413 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
415 return atmel_pioctrl
->npins
;
418 static const char *atmel_pctl_get_group_name(struct pinctrl_dev
*pctldev
,
421 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
423 return atmel_pioctrl
->groups
[selector
].name
;
426 static int atmel_pctl_get_group_pins(struct pinctrl_dev
*pctldev
,
427 unsigned selector
, const unsigned **pins
,
430 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
432 *pins
= (unsigned *)&atmel_pioctrl
->groups
[selector
].pin
;
438 static struct atmel_group
*
439 atmel_pctl_find_group_by_pin(struct pinctrl_dev
*pctldev
, unsigned pin
)
441 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
444 for (i
= 0; i
< atmel_pioctrl
->npins
; i
++) {
445 struct atmel_group
*grp
= atmel_pioctrl
->groups
+ i
;
454 static int atmel_pctl_xlate_pinfunc(struct pinctrl_dev
*pctldev
,
455 struct device_node
*np
,
456 u32 pinfunc
, const char **grp_name
,
457 const char **func_name
)
459 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
460 unsigned pin_id
, func_id
;
461 struct atmel_group
*grp
;
463 pin_id
= ATMEL_GET_PIN_NO(pinfunc
);
464 func_id
= ATMEL_GET_PIN_FUNC(pinfunc
);
466 if (func_id
>= ARRAY_SIZE(atmel_functions
))
469 *func_name
= atmel_functions
[func_id
];
471 grp
= atmel_pctl_find_group_by_pin(pctldev
, pin_id
);
474 *grp_name
= grp
->name
;
476 atmel_pioctrl
->pins
[pin_id
]->mux
= func_id
;
477 atmel_pioctrl
->pins
[pin_id
]->ioset
= ATMEL_GET_PIN_IOSET(pinfunc
);
478 /* Want the device name not the group one. */
479 if (np
->parent
== atmel_pioctrl
->node
)
480 atmel_pioctrl
->pins
[pin_id
]->device
= np
->name
;
482 atmel_pioctrl
->pins
[pin_id
]->device
= np
->parent
->name
;
487 static int atmel_pctl_dt_subnode_to_map(struct pinctrl_dev
*pctldev
,
488 struct device_node
*np
,
489 struct pinctrl_map
**map
,
490 unsigned *reserved_maps
,
493 unsigned num_pins
, num_configs
, reserve
;
494 unsigned long *configs
;
495 struct property
*pins
;
499 pins
= of_find_property(np
, "pinmux", NULL
);
503 ret
= pinconf_generic_parse_dt_config(np
, pctldev
, &configs
,
506 dev_err(pctldev
->dev
, "%pOF: could not parse node property\n",
511 num_pins
= pins
->length
/ sizeof(u32
);
513 dev_err(pctldev
->dev
, "no pins found in node %pOF\n", np
);
519 * Reserve maps, at least there is a mux map and an optional conf
526 ret
= pinctrl_utils_reserve_map(pctldev
, map
, reserved_maps
, num_maps
,
531 for (i
= 0; i
< num_pins
; i
++) {
532 const char *group
, *func
;
534 ret
= of_property_read_u32_index(np
, "pinmux", i
, &pinfunc
);
538 ret
= atmel_pctl_xlate_pinfunc(pctldev
, np
, pinfunc
, &group
,
543 pinctrl_utils_add_map_mux(pctldev
, map
, reserved_maps
, num_maps
,
547 ret
= pinctrl_utils_add_map_configs(pctldev
, map
,
548 reserved_maps
, num_maps
, group
,
549 configs
, num_configs
,
550 PIN_MAP_TYPE_CONFIGS_GROUP
);
561 static int atmel_pctl_dt_node_to_map(struct pinctrl_dev
*pctldev
,
562 struct device_node
*np_config
,
563 struct pinctrl_map
**map
,
566 struct device_node
*np
;
567 unsigned reserved_maps
;
575 * If all the pins of a device have the same configuration (or no one),
576 * it is useless to add a subnode, so directly parse node referenced by
579 ret
= atmel_pctl_dt_subnode_to_map(pctldev
, np_config
, map
,
580 &reserved_maps
, num_maps
);
582 for_each_child_of_node(np_config
, np
) {
583 ret
= atmel_pctl_dt_subnode_to_map(pctldev
, np
, map
,
584 &reserved_maps
, num_maps
);
593 pinctrl_utils_free_map(pctldev
, *map
, *num_maps
);
594 dev_err(pctldev
->dev
, "can't create maps for node %pOF\n",
601 static const struct pinctrl_ops atmel_pctlops
= {
602 .get_groups_count
= atmel_pctl_get_groups_count
,
603 .get_group_name
= atmel_pctl_get_group_name
,
604 .get_group_pins
= atmel_pctl_get_group_pins
,
605 .dt_node_to_map
= atmel_pctl_dt_node_to_map
,
606 .dt_free_map
= pinctrl_utils_free_map
,
609 static int atmel_pmx_get_functions_count(struct pinctrl_dev
*pctldev
)
611 return ARRAY_SIZE(atmel_functions
);
614 static const char *atmel_pmx_get_function_name(struct pinctrl_dev
*pctldev
,
617 return atmel_functions
[selector
];
620 static int atmel_pmx_get_function_groups(struct pinctrl_dev
*pctldev
,
622 const char * const **groups
,
623 unsigned * const num_groups
)
625 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
627 *groups
= atmel_pioctrl
->group_names
;
628 *num_groups
= atmel_pioctrl
->npins
;
633 static int atmel_pmx_set_mux(struct pinctrl_dev
*pctldev
,
637 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
641 dev_dbg(pctldev
->dev
, "enable function %s group %s\n",
642 atmel_functions
[function
], atmel_pioctrl
->groups
[group
].name
);
644 pin
= atmel_pioctrl
->groups
[group
].pin
;
645 conf
= atmel_pin_config_read(pctldev
, pin
);
646 conf
&= (~ATMEL_PIO_CFGR_FUNC_MASK
);
647 conf
|= (function
& ATMEL_PIO_CFGR_FUNC_MASK
);
648 dev_dbg(pctldev
->dev
, "pin: %u, conf: 0x%08x\n", pin
, conf
);
649 atmel_pin_config_write(pctldev
, pin
, conf
);
654 static const struct pinmux_ops atmel_pmxops
= {
655 .get_functions_count
= atmel_pmx_get_functions_count
,
656 .get_function_name
= atmel_pmx_get_function_name
,
657 .get_function_groups
= atmel_pmx_get_function_groups
,
658 .set_mux
= atmel_pmx_set_mux
,
661 static int atmel_conf_pin_config_group_get(struct pinctrl_dev
*pctldev
,
663 unsigned long *config
)
665 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
666 unsigned param
= pinconf_to_config_param(*config
), arg
= 0;
667 struct atmel_group
*grp
= atmel_pioctrl
->groups
+ group
;
668 unsigned pin_id
= grp
->pin
;
671 res
= atmel_pin_config_read(pctldev
, pin_id
);
674 case PIN_CONFIG_BIAS_PULL_UP
:
675 if (!(res
& ATMEL_PIO_PUEN_MASK
))
679 case PIN_CONFIG_BIAS_PULL_DOWN
:
680 if ((res
& ATMEL_PIO_PUEN_MASK
) ||
681 (!(res
& ATMEL_PIO_PDEN_MASK
)))
685 case PIN_CONFIG_BIAS_DISABLE
:
686 if ((res
& ATMEL_PIO_PUEN_MASK
) ||
687 ((res
& ATMEL_PIO_PDEN_MASK
)))
691 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
692 if (!(res
& ATMEL_PIO_OPD_MASK
))
696 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
697 if (!(res
& ATMEL_PIO_SCHMITT_MASK
))
701 case ATMEL_PIN_CONFIG_DRIVE_STRENGTH
:
702 if (!(res
& ATMEL_PIO_DRVSTR_MASK
))
704 arg
= (res
& ATMEL_PIO_DRVSTR_MASK
) >> ATMEL_PIO_DRVSTR_OFFSET
;
710 *config
= pinconf_to_config_packed(param
, arg
);
714 static int atmel_conf_pin_config_group_set(struct pinctrl_dev
*pctldev
,
716 unsigned long *configs
,
717 unsigned num_configs
)
719 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
720 struct atmel_group
*grp
= atmel_pioctrl
->groups
+ group
;
721 unsigned bank
, pin
, pin_id
= grp
->pin
;
725 conf
= atmel_pin_config_read(pctldev
, pin_id
);
727 for (i
= 0; i
< num_configs
; i
++) {
728 unsigned param
= pinconf_to_config_param(configs
[i
]);
729 unsigned arg
= pinconf_to_config_argument(configs
[i
]);
731 dev_dbg(pctldev
->dev
, "%s: pin=%u, config=0x%lx\n",
732 __func__
, pin_id
, configs
[i
]);
735 case PIN_CONFIG_BIAS_DISABLE
:
736 conf
&= (~ATMEL_PIO_PUEN_MASK
);
737 conf
&= (~ATMEL_PIO_PDEN_MASK
);
739 case PIN_CONFIG_BIAS_PULL_UP
:
740 conf
|= ATMEL_PIO_PUEN_MASK
;
741 conf
&= (~ATMEL_PIO_PDEN_MASK
);
743 case PIN_CONFIG_BIAS_PULL_DOWN
:
744 conf
|= ATMEL_PIO_PDEN_MASK
;
745 conf
&= (~ATMEL_PIO_PUEN_MASK
);
747 case PIN_CONFIG_DRIVE_OPEN_DRAIN
:
749 conf
&= (~ATMEL_PIO_OPD_MASK
);
751 conf
|= ATMEL_PIO_OPD_MASK
;
753 case PIN_CONFIG_INPUT_SCHMITT_ENABLE
:
755 conf
|= ATMEL_PIO_SCHMITT_MASK
;
757 conf
&= (~ATMEL_PIO_SCHMITT_MASK
);
759 case PIN_CONFIG_INPUT_DEBOUNCE
:
761 conf
&= (~ATMEL_PIO_IFEN_MASK
);
762 conf
&= (~ATMEL_PIO_IFSCEN_MASK
);
765 * We don't care about the debounce value for several reasons:
766 * - can't have different debounce periods inside a same group,
767 * - the register to configure this period is a secure register.
768 * The debouncing filter can filter a pulse with a duration of less
769 * than 1/2 slow clock period.
771 conf
|= ATMEL_PIO_IFEN_MASK
;
772 conf
|= ATMEL_PIO_IFSCEN_MASK
;
775 case PIN_CONFIG_OUTPUT
:
776 conf
|= ATMEL_PIO_DIR_MASK
;
777 bank
= ATMEL_PIO_BANK(pin_id
);
778 pin
= ATMEL_PIO_LINE(pin_id
);
782 writel_relaxed(mask
, atmel_pioctrl
->reg_base
+
783 bank
* ATMEL_PIO_BANK_OFFSET
+
786 writel_relaxed(mask
, atmel_pioctrl
->reg_base
+
787 bank
* ATMEL_PIO_BANK_OFFSET
+
791 case ATMEL_PIN_CONFIG_DRIVE_STRENGTH
:
793 case ATMEL_PIO_DRVSTR_LO
:
794 case ATMEL_PIO_DRVSTR_ME
:
795 case ATMEL_PIO_DRVSTR_HI
:
796 conf
&= (~ATMEL_PIO_DRVSTR_MASK
);
797 conf
|= arg
<< ATMEL_PIO_DRVSTR_OFFSET
;
800 dev_warn(pctldev
->dev
, "drive strength not updated (incorrect value)\n");
804 dev_warn(pctldev
->dev
,
805 "unsupported configuration parameter: %u\n",
811 dev_dbg(pctldev
->dev
, "%s: reg=0x%08x\n", __func__
, conf
);
812 atmel_pin_config_write(pctldev
, pin_id
, conf
);
817 static void atmel_conf_pin_config_dbg_show(struct pinctrl_dev
*pctldev
,
818 struct seq_file
*s
, unsigned pin_id
)
820 struct atmel_pioctrl
*atmel_pioctrl
= pinctrl_dev_get_drvdata(pctldev
);
823 if (!atmel_pioctrl
->pins
[pin_id
]->device
)
826 if (atmel_pioctrl
->pins
[pin_id
])
827 seq_printf(s
, " (%s, ioset %u) ",
828 atmel_pioctrl
->pins
[pin_id
]->device
,
829 atmel_pioctrl
->pins
[pin_id
]->ioset
);
831 conf
= atmel_pin_config_read(pctldev
, pin_id
);
832 if (conf
& ATMEL_PIO_PUEN_MASK
)
833 seq_printf(s
, "%s ", "pull-up");
834 if (conf
& ATMEL_PIO_PDEN_MASK
)
835 seq_printf(s
, "%s ", "pull-down");
836 if (conf
& ATMEL_PIO_IFEN_MASK
)
837 seq_printf(s
, "%s ", "debounce");
838 if (conf
& ATMEL_PIO_OPD_MASK
)
839 seq_printf(s
, "%s ", "open-drain");
840 if (conf
& ATMEL_PIO_SCHMITT_MASK
)
841 seq_printf(s
, "%s ", "schmitt");
842 if (conf
& ATMEL_PIO_DRVSTR_MASK
) {
843 switch ((conf
& ATMEL_PIO_DRVSTR_MASK
) >> ATMEL_PIO_DRVSTR_OFFSET
) {
844 case ATMEL_PIO_DRVSTR_ME
:
845 seq_printf(s
, "%s ", "medium-drive");
847 case ATMEL_PIO_DRVSTR_HI
:
848 seq_printf(s
, "%s ", "high-drive");
850 /* ATMEL_PIO_DRVSTR_LO and 0 which is the default value at reset */
852 seq_printf(s
, "%s ", "low-drive");
857 static const struct pinconf_ops atmel_confops
= {
858 .pin_config_group_get
= atmel_conf_pin_config_group_get
,
859 .pin_config_group_set
= atmel_conf_pin_config_group_set
,
860 .pin_config_dbg_show
= atmel_conf_pin_config_dbg_show
,
863 static struct pinctrl_desc atmel_pinctrl_desc
= {
864 .name
= "atmel_pinctrl",
865 .confops
= &atmel_confops
,
866 .pctlops
= &atmel_pctlops
,
867 .pmxops
= &atmel_pmxops
,
870 static int __maybe_unused
atmel_pctrl_suspend(struct device
*dev
)
872 struct platform_device
*pdev
= to_platform_device(dev
);
873 struct atmel_pioctrl
*atmel_pioctrl
= platform_get_drvdata(pdev
);
877 * For each bank, save IMR to restore it later and disable all GPIO
878 * interrupts excepting the ones marked as wakeup sources.
880 for (i
= 0; i
< atmel_pioctrl
->nbanks
; i
++) {
881 atmel_pioctrl
->pm_suspend_backup
[i
].imr
=
882 atmel_gpio_read(atmel_pioctrl
, i
, ATMEL_PIO_IMR
);
883 atmel_gpio_write(atmel_pioctrl
, i
, ATMEL_PIO_IDR
,
884 ~atmel_pioctrl
->pm_wakeup_sources
[i
]);
885 atmel_pioctrl
->pm_suspend_backup
[i
].odsr
=
886 atmel_gpio_read(atmel_pioctrl
, i
, ATMEL_PIO_ODSR
);
887 for (j
= 0; j
< ATMEL_PIO_NPINS_PER_BANK
; j
++) {
888 atmel_gpio_write(atmel_pioctrl
, i
,
889 ATMEL_PIO_MSKR
, BIT(j
));
890 atmel_pioctrl
->pm_suspend_backup
[i
].cfgr
[j
] =
891 atmel_gpio_read(atmel_pioctrl
, i
,
899 static int __maybe_unused
atmel_pctrl_resume(struct device
*dev
)
901 struct platform_device
*pdev
= to_platform_device(dev
);
902 struct atmel_pioctrl
*atmel_pioctrl
= platform_get_drvdata(pdev
);
905 for (i
= 0; i
< atmel_pioctrl
->nbanks
; i
++) {
906 atmel_gpio_write(atmel_pioctrl
, i
, ATMEL_PIO_IER
,
907 atmel_pioctrl
->pm_suspend_backup
[i
].imr
);
908 atmel_gpio_write(atmel_pioctrl
, i
, ATMEL_PIO_SODR
,
909 atmel_pioctrl
->pm_suspend_backup
[i
].odsr
);
910 for (j
= 0; j
< ATMEL_PIO_NPINS_PER_BANK
; j
++) {
911 atmel_gpio_write(atmel_pioctrl
, i
,
912 ATMEL_PIO_MSKR
, BIT(j
));
913 atmel_gpio_write(atmel_pioctrl
, i
, ATMEL_PIO_CFGR
,
914 atmel_pioctrl
->pm_suspend_backup
[i
].cfgr
[j
]);
921 static const struct dev_pm_ops atmel_pctrl_pm_ops
= {
922 SET_SYSTEM_SLEEP_PM_OPS(atmel_pctrl_suspend
, atmel_pctrl_resume
)
926 * The number of banks can be different from a SoC to another one.
927 * We can have up to 16 banks.
929 static const struct atmel_pioctrl_data atmel_sama5d2_pioctrl_data
= {
933 static const struct of_device_id atmel_pctrl_of_match
[] = {
935 .compatible
= "atmel,sama5d2-pinctrl",
936 .data
= &atmel_sama5d2_pioctrl_data
,
942 static int atmel_pinctrl_probe(struct platform_device
*pdev
)
944 struct device
*dev
= &pdev
->dev
;
945 struct pinctrl_pin_desc
*pin_desc
;
946 const char **group_names
;
947 const struct of_device_id
*match
;
949 struct resource
*res
;
950 struct atmel_pioctrl
*atmel_pioctrl
;
951 const struct atmel_pioctrl_data
*atmel_pioctrl_data
;
953 atmel_pioctrl
= devm_kzalloc(dev
, sizeof(*atmel_pioctrl
), GFP_KERNEL
);
956 atmel_pioctrl
->dev
= dev
;
957 atmel_pioctrl
->node
= dev
->of_node
;
958 platform_set_drvdata(pdev
, atmel_pioctrl
);
960 match
= of_match_node(atmel_pctrl_of_match
, dev
->of_node
);
962 dev_err(dev
, "unknown compatible string\n");
965 atmel_pioctrl_data
= match
->data
;
966 atmel_pioctrl
->nbanks
= atmel_pioctrl_data
->nbanks
;
967 atmel_pioctrl
->npins
= atmel_pioctrl
->nbanks
* ATMEL_PIO_NPINS_PER_BANK
;
969 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
970 atmel_pioctrl
->reg_base
= devm_ioremap_resource(dev
, res
);
971 if (IS_ERR(atmel_pioctrl
->reg_base
))
974 atmel_pioctrl
->clk
= devm_clk_get(dev
, NULL
);
975 if (IS_ERR(atmel_pioctrl
->clk
)) {
976 dev_err(dev
, "failed to get clock\n");
977 return PTR_ERR(atmel_pioctrl
->clk
);
980 atmel_pioctrl
->pins
= devm_kcalloc(dev
,
981 atmel_pioctrl
->npins
,
982 sizeof(*atmel_pioctrl
->pins
),
984 if (!atmel_pioctrl
->pins
)
987 pin_desc
= devm_kcalloc(dev
, atmel_pioctrl
->npins
, sizeof(*pin_desc
),
991 atmel_pinctrl_desc
.pins
= pin_desc
;
992 atmel_pinctrl_desc
.npins
= atmel_pioctrl
->npins
;
993 atmel_pinctrl_desc
.num_custom_params
= ARRAY_SIZE(atmel_custom_bindings
);
994 atmel_pinctrl_desc
.custom_params
= atmel_custom_bindings
;
996 /* One pin is one group since a pin can achieve all functions. */
997 group_names
= devm_kcalloc(dev
,
998 atmel_pioctrl
->npins
, sizeof(*group_names
),
1002 atmel_pioctrl
->group_names
= group_names
;
1004 atmel_pioctrl
->groups
= devm_kcalloc(&pdev
->dev
,
1005 atmel_pioctrl
->npins
, sizeof(*atmel_pioctrl
->groups
),
1007 if (!atmel_pioctrl
->groups
)
1009 for (i
= 0 ; i
< atmel_pioctrl
->npins
; i
++) {
1010 struct atmel_group
*group
= atmel_pioctrl
->groups
+ i
;
1011 unsigned bank
= ATMEL_PIO_BANK(i
);
1012 unsigned line
= ATMEL_PIO_LINE(i
);
1014 atmel_pioctrl
->pins
[i
] = devm_kzalloc(dev
,
1015 sizeof(**atmel_pioctrl
->pins
), GFP_KERNEL
);
1016 if (!atmel_pioctrl
->pins
[i
])
1019 atmel_pioctrl
->pins
[i
]->pin_id
= i
;
1020 atmel_pioctrl
->pins
[i
]->bank
= bank
;
1021 atmel_pioctrl
->pins
[i
]->line
= line
;
1023 pin_desc
[i
].number
= i
;
1024 /* Pin naming convention: P(bank_name)(bank_pin_number). */
1025 pin_desc
[i
].name
= kasprintf(GFP_KERNEL
, "P%c%d",
1028 group
->name
= group_names
[i
] = pin_desc
[i
].name
;
1029 group
->pin
= pin_desc
[i
].number
;
1031 dev_dbg(dev
, "pin_id=%u, bank=%u, line=%u", i
, bank
, line
);
1034 atmel_pioctrl
->gpio_chip
= &atmel_gpio_chip
;
1035 atmel_pioctrl
->gpio_chip
->of_node
= dev
->of_node
;
1036 atmel_pioctrl
->gpio_chip
->ngpio
= atmel_pioctrl
->npins
;
1037 atmel_pioctrl
->gpio_chip
->label
= dev_name(dev
);
1038 atmel_pioctrl
->gpio_chip
->parent
= dev
;
1039 atmel_pioctrl
->gpio_chip
->names
= atmel_pioctrl
->group_names
;
1041 atmel_pioctrl
->pm_wakeup_sources
= devm_kcalloc(dev
,
1042 atmel_pioctrl
->nbanks
,
1043 sizeof(*atmel_pioctrl
->pm_wakeup_sources
),
1045 if (!atmel_pioctrl
->pm_wakeup_sources
)
1048 atmel_pioctrl
->pm_suspend_backup
= devm_kcalloc(dev
,
1049 atmel_pioctrl
->nbanks
,
1050 sizeof(*atmel_pioctrl
->pm_suspend_backup
),
1052 if (!atmel_pioctrl
->pm_suspend_backup
)
1055 atmel_pioctrl
->irqs
= devm_kcalloc(dev
,
1056 atmel_pioctrl
->nbanks
,
1057 sizeof(*atmel_pioctrl
->irqs
),
1059 if (!atmel_pioctrl
->irqs
)
1062 /* There is one controller but each bank has its own irq line. */
1063 for (i
= 0; i
< atmel_pioctrl
->nbanks
; i
++) {
1064 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, i
);
1066 dev_err(dev
, "missing irq resource for group %c\n",
1070 atmel_pioctrl
->irqs
[i
] = res
->start
;
1071 irq_set_chained_handler(res
->start
, atmel_gpio_irq_handler
);
1072 irq_set_handler_data(res
->start
, atmel_pioctrl
);
1073 dev_dbg(dev
, "bank %i: irq=%pr\n", i
, res
);
1076 atmel_pioctrl
->irq_domain
= irq_domain_add_linear(dev
->of_node
,
1077 atmel_pioctrl
->gpio_chip
->ngpio
,
1078 &irq_domain_simple_ops
, NULL
);
1079 if (!atmel_pioctrl
->irq_domain
) {
1080 dev_err(dev
, "can't add the irq domain\n");
1083 atmel_pioctrl
->irq_domain
->name
= "atmel gpio";
1085 for (i
= 0; i
< atmel_pioctrl
->npins
; i
++) {
1086 int irq
= irq_create_mapping(atmel_pioctrl
->irq_domain
, i
);
1088 irq_set_chip_and_handler(irq
, &atmel_gpio_irq_chip
,
1090 irq_set_chip_data(irq
, atmel_pioctrl
);
1092 "atmel gpio irq domain: hwirq: %d, linux irq: %d\n",
1096 ret
= clk_prepare_enable(atmel_pioctrl
->clk
);
1098 dev_err(dev
, "failed to prepare and enable clock\n");
1099 goto clk_prepare_enable_error
;
1102 atmel_pioctrl
->pinctrl_dev
= devm_pinctrl_register(&pdev
->dev
,
1103 &atmel_pinctrl_desc
,
1105 if (IS_ERR(atmel_pioctrl
->pinctrl_dev
)) {
1106 ret
= PTR_ERR(atmel_pioctrl
->pinctrl_dev
);
1107 dev_err(dev
, "pinctrl registration failed\n");
1111 ret
= gpiochip_add_data(atmel_pioctrl
->gpio_chip
, atmel_pioctrl
);
1113 dev_err(dev
, "failed to add gpiochip\n");
1117 ret
= gpiochip_add_pin_range(atmel_pioctrl
->gpio_chip
, dev_name(dev
),
1118 0, 0, atmel_pioctrl
->gpio_chip
->ngpio
);
1120 dev_err(dev
, "failed to add gpio pin range\n");
1121 goto gpiochip_add_pin_range_error
;
1124 dev_info(&pdev
->dev
, "atmel pinctrl initialized\n");
1128 gpiochip_add_pin_range_error
:
1129 gpiochip_remove(atmel_pioctrl
->gpio_chip
);
1132 clk_disable_unprepare(atmel_pioctrl
->clk
);
1134 clk_prepare_enable_error
:
1135 irq_domain_remove(atmel_pioctrl
->irq_domain
);
1140 static struct platform_driver atmel_pinctrl_driver
= {
1142 .name
= "pinctrl-at91-pio4",
1143 .of_match_table
= atmel_pctrl_of_match
,
1144 .pm
= &atmel_pctrl_pm_ops
,
1145 .suppress_bind_attrs
= true,
1147 .probe
= atmel_pinctrl_probe
,
1149 builtin_platform_driver(atmel_pinctrl_driver
);