blob: cb7c432769a5ece3775c31c70d3f13a387313d7b [file] [log] [blame]
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001/*
2 * at91 pinctrl driver based on at91 pinmux core
3 *
4 * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
5 *
6 * Under GPLv2 only
7 */
8
9#include <linux/clk.h>
10#include <linux/err.h>
11#include <linux/init.h>
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080012#include <linux/of.h>
13#include <linux/of_device.h>
14#include <linux/of_address.h>
15#include <linux/of_irq.h>
16#include <linux/slab.h>
17#include <linux/interrupt.h>
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080018#include <linux/io.h>
Linus Walleij1c5fb662018-09-13 13:58:21 +020019#include <linux/gpio/driver.h>
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080020#include <linux/pinctrl/machine.h>
21#include <linux/pinctrl/pinconf.h>
22#include <linux/pinctrl/pinctrl.h>
23#include <linux/pinctrl/pinmux.h>
24/* Since we request GPIOs from ourself */
25#include <linux/pinctrl/consumer.h>
26
Alexandre Bellonic654b6b2014-10-17 11:49:40 +020027#include "pinctrl-at91.h"
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080028#include "core.h"
29
Linus Walleij94daf852013-11-05 10:30:14 +010030#define MAX_GPIO_BANKS 5
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080031#define MAX_NB_GPIO_PER_BANK 32
32
33struct at91_pinctrl_mux_ops;
34
35struct at91_gpio_chip {
36 struct gpio_chip chip;
37 struct pinctrl_gpio_range range;
38 struct at91_gpio_chip *next; /* Bank sharing same clock */
39 int pioc_hwirq; /* PIO bank interrupt identifier on AIC */
40 int pioc_virq; /* PIO bank Linux virtual interrupt */
41 int pioc_idx; /* PIO bank index */
42 void __iomem *regbase; /* PIO bank virtual address */
43 struct clk *clock; /* associated clock */
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080044 struct at91_pinctrl_mux_ops *ops; /* ops */
45};
46
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080047static struct at91_gpio_chip *gpio_chips[MAX_GPIO_BANKS];
48
49static int gpio_banks;
50
Jean-Christophe PLAGNIOL-VILLARD525fae22012-10-23 18:28:00 +020051#define PULL_UP (1 << 0)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080052#define MULTI_DRIVE (1 << 1)
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +080053#define DEGLITCH (1 << 2)
54#define PULL_DOWN (1 << 3)
55#define DIS_SCHMIT (1 << 4)
Marek Roszko4334ac22014-08-23 23:12:04 -040056#define DRIVE_STRENGTH_SHIFT 5
57#define DRIVE_STRENGTH_MASK 0x3
58#define DRIVE_STRENGTH (DRIVE_STRENGTH_MASK << DRIVE_STRENGTH_SHIFT)
Boris BREZILLON96bb12d2016-10-28 15:54:10 +080059#define OUTPUT (1 << 7)
60#define OUTPUT_VAL_SHIFT 8
61#define OUTPUT_VAL (0x1 << OUTPUT_VAL_SHIFT)
Claudiu Beznea64e21ad2019-02-07 09:25:05 +000062#define SLEWRATE_SHIFT 9
63#define SLEWRATE_MASK 0x1
64#define SLEWRATE (SLEWRATE_MASK << SLEWRATE_SHIFT)
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +080065#define DEBOUNCE (1 << 16)
66#define DEBOUNCE_VAL_SHIFT 17
67#define DEBOUNCE_VAL (0x3fff << DEBOUNCE_VAL_SHIFT)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080068
69/**
Marek Roszko4334ac22014-08-23 23:12:04 -040070 * These defines will translated the dt binding settings to our internal
71 * settings. They are not necessarily the same value as the register setting.
72 * The actual drive strength current of low, medium and high must be looked up
73 * from the corresponding device datasheet. This value is different for pins
74 * that are even in the same banks. It is also dependent on VCC.
75 * DRIVE_STRENGTH_DEFAULT is just a placeholder to avoid changing the drive
76 * strength when there is no dt config for it.
77 */
Claudiu Bezneab67328e2019-02-07 09:24:46 +000078enum drive_strength_bit {
79 DRIVE_STRENGTH_BIT_DEF,
80 DRIVE_STRENGTH_BIT_LOW,
81 DRIVE_STRENGTH_BIT_MED,
82 DRIVE_STRENGTH_BIT_HI,
83};
84
85#define DRIVE_STRENGTH_BIT_MSK(name) (DRIVE_STRENGTH_BIT_##name << \
86 DRIVE_STRENGTH_SHIFT)
Marek Roszko4334ac22014-08-23 23:12:04 -040087
Claudiu Beznea64e21ad2019-02-07 09:25:05 +000088enum slewrate_bit {
89 SLEWRATE_BIT_DIS,
90 SLEWRATE_BIT_ENA,
91};
92
93#define SLEWRATE_BIT_MSK(name) (SLEWRATE_BIT_##name << SLEWRATE_SHIFT)
94
Marek Roszko4334ac22014-08-23 23:12:04 -040095/**
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080096 * struct at91_pmx_func - describes AT91 pinmux functions
97 * @name: the name of this specific function
98 * @groups: corresponding pin groups
99 * @ngroups: the number of groups
100 */
101struct at91_pmx_func {
102 const char *name;
103 const char **groups;
104 unsigned ngroups;
105};
106
107enum at91_mux {
108 AT91_MUX_GPIO = 0,
109 AT91_MUX_PERIPH_A = 1,
110 AT91_MUX_PERIPH_B = 2,
111 AT91_MUX_PERIPH_C = 3,
112 AT91_MUX_PERIPH_D = 4,
113};
114
115/**
116 * struct at91_pmx_pin - describes an At91 pin mux
117 * @bank: the bank of the pin
118 * @pin: the pin number in the @bank
119 * @mux: the mux mode : gpio or periph_x of the pin i.e. alternate function.
120 * @conf: the configuration of the pin: PULL_UP, MULTIDRIVE etc...
121 */
122struct at91_pmx_pin {
123 uint32_t bank;
124 uint32_t pin;
125 enum at91_mux mux;
126 unsigned long conf;
127};
128
129/**
130 * struct at91_pin_group - describes an At91 pin group
131 * @name: the name of this specific pin group
132 * @pins_conf: the mux mode for each pin in this group. The size of this
133 * array is the same as pins.
134 * @pins: an array of discrete physical pins used in this group, taken
135 * from the driver-local pin enumeration space
136 * @npins: the number of pins in this group array, i.e. the number of
137 * elements in .pins so we can iterate over that array
138 */
139struct at91_pin_group {
140 const char *name;
141 struct at91_pmx_pin *pins_conf;
142 unsigned int *pins;
143 unsigned npins;
144};
145
146/**
Alexandre Bellonic2eb9e72013-12-07 14:08:52 +0100147 * struct at91_pinctrl_mux_ops - describes an AT91 mux ops group
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800148 * on new IP with support for periph C and D the way to mux in
149 * periph A and B has changed
150 * So provide the right call back
151 * if not present means the IP does not support it
152 * @get_periph: return the periph mode configured
153 * @mux_A_periph: mux as periph A
154 * @mux_B_periph: mux as periph B
155 * @mux_C_periph: mux as periph C
156 * @mux_D_periph: mux as periph D
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800157 * @get_deglitch: get deglitch status
158 * @set_deglitch: enable/disable deglitch
159 * @get_debounce: get debounce status
160 * @set_debounce: enable/disable debounce
161 * @get_pulldown: get pulldown status
162 * @set_pulldown: enable/disable pulldown
163 * @get_schmitt_trig: get schmitt trigger status
164 * @disable_schmitt_trig: disable schmitt trigger
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800165 * @irq_type: return irq type
166 */
167struct at91_pinctrl_mux_ops {
168 enum at91_mux (*get_periph)(void __iomem *pio, unsigned mask);
169 void (*mux_A_periph)(void __iomem *pio, unsigned mask);
170 void (*mux_B_periph)(void __iomem *pio, unsigned mask);
171 void (*mux_C_periph)(void __iomem *pio, unsigned mask);
172 void (*mux_D_periph)(void __iomem *pio, unsigned mask);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800173 bool (*get_deglitch)(void __iomem *pio, unsigned pin);
Boris BREZILLON77966ad2013-09-13 09:45:33 +0200174 void (*set_deglitch)(void __iomem *pio, unsigned mask, bool is_on);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800175 bool (*get_debounce)(void __iomem *pio, unsigned pin, u32 *div);
Boris BREZILLON77966ad2013-09-13 09:45:33 +0200176 void (*set_debounce)(void __iomem *pio, unsigned mask, bool is_on, u32 div);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800177 bool (*get_pulldown)(void __iomem *pio, unsigned pin);
Boris BREZILLON77966ad2013-09-13 09:45:33 +0200178 void (*set_pulldown)(void __iomem *pio, unsigned mask, bool is_on);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800179 bool (*get_schmitt_trig)(void __iomem *pio, unsigned pin);
180 void (*disable_schmitt_trig)(void __iomem *pio, unsigned mask);
Marek Roszko4334ac22014-08-23 23:12:04 -0400181 unsigned (*get_drivestrength)(void __iomem *pio, unsigned pin);
182 void (*set_drivestrength)(void __iomem *pio, unsigned pin,
183 u32 strength);
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000184 unsigned (*get_slewrate)(void __iomem *pio, unsigned pin);
185 void (*set_slewrate)(void __iomem *pio, unsigned pin, u32 slewrate);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800186 /* irq */
187 int (*irq_type)(struct irq_data *d, unsigned type);
188};
189
190static int gpio_irq_type(struct irq_data *d, unsigned type);
191static int alt_gpio_irq_type(struct irq_data *d, unsigned type);
192
193struct at91_pinctrl {
194 struct device *dev;
195 struct pinctrl_dev *pctl;
196
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +0100197 int nactive_banks;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800198
199 uint32_t *mux_mask;
200 int nmux;
201
202 struct at91_pmx_func *functions;
203 int nfunctions;
204
205 struct at91_pin_group *groups;
206 int ngroups;
207
208 struct at91_pinctrl_mux_ops *ops;
209};
210
Arnd Bergmann56411f32016-06-13 17:18:34 +0200211static inline const struct at91_pin_group *at91_pinctrl_find_group_by_name(
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800212 const struct at91_pinctrl *info,
213 const char *name)
214{
215 const struct at91_pin_group *grp = NULL;
216 int i;
217
218 for (i = 0; i < info->ngroups; i++) {
219 if (strcmp(info->groups[i].name, name))
220 continue;
221
222 grp = &info->groups[i];
223 dev_dbg(info->dev, "%s: %d 0:%d\n", name, grp->npins, grp->pins[0]);
224 break;
225 }
226
227 return grp;
228}
229
230static int at91_get_groups_count(struct pinctrl_dev *pctldev)
231{
232 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
233
234 return info->ngroups;
235}
236
237static const char *at91_get_group_name(struct pinctrl_dev *pctldev,
238 unsigned selector)
239{
240 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
241
242 return info->groups[selector].name;
243}
244
245static int at91_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
246 const unsigned **pins,
247 unsigned *npins)
248{
249 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
250
251 if (selector >= info->ngroups)
252 return -EINVAL;
253
254 *pins = info->groups[selector].pins;
255 *npins = info->groups[selector].npins;
256
257 return 0;
258}
259
260static void at91_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
261 unsigned offset)
262{
263 seq_printf(s, "%s", dev_name(pctldev->dev));
264}
265
266static int at91_dt_node_to_map(struct pinctrl_dev *pctldev,
267 struct device_node *np,
268 struct pinctrl_map **map, unsigned *num_maps)
269{
270 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
271 const struct at91_pin_group *grp;
272 struct pinctrl_map *new_map;
273 struct device_node *parent;
274 int map_num = 1;
275 int i;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800276
277 /*
Alexandre Belloni61e310a2013-10-16 16:12:33 +0200278 * first find the group of this node and check if we need to create
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800279 * config maps for pins
280 */
281 grp = at91_pinctrl_find_group_by_name(info, np->name);
282 if (!grp) {
Rob Herring94f4e542018-08-27 20:52:41 -0500283 dev_err(info->dev, "unable to find group for node %pOFn\n",
284 np);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800285 return -EINVAL;
286 }
287
288 map_num += grp->npins;
Kees Cooka86854d2018-06-12 14:07:58 -0700289 new_map = devm_kcalloc(pctldev->dev, map_num, sizeof(*new_map),
290 GFP_KERNEL);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800291 if (!new_map)
292 return -ENOMEM;
293
294 *map = new_map;
295 *num_maps = map_num;
296
297 /* create mux map */
298 parent = of_get_parent(np);
299 if (!parent) {
Julia Lawallc62b2b32012-12-12 15:22:44 +0100300 devm_kfree(pctldev->dev, new_map);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800301 return -EINVAL;
302 }
303 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
304 new_map[0].data.mux.function = parent->name;
305 new_map[0].data.mux.group = np->name;
306 of_node_put(parent);
307
308 /* create config map */
309 new_map++;
310 for (i = 0; i < grp->npins; i++) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800311 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
312 new_map[i].data.configs.group_or_pin =
313 pin_get_name(pctldev, grp->pins[i]);
314 new_map[i].data.configs.configs = &grp->pins_conf[i].conf;
315 new_map[i].data.configs.num_configs = 1;
316 }
317
318 dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
319 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
320
321 return 0;
322}
323
324static void at91_dt_free_map(struct pinctrl_dev *pctldev,
325 struct pinctrl_map *map, unsigned num_maps)
326{
327}
328
Laurent Pinchart022ab142013-02-16 10:25:07 +0100329static const struct pinctrl_ops at91_pctrl_ops = {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800330 .get_groups_count = at91_get_groups_count,
331 .get_group_name = at91_get_group_name,
332 .get_group_pins = at91_get_group_pins,
333 .pin_dbg_show = at91_pin_dbg_show,
334 .dt_node_to_map = at91_dt_node_to_map,
335 .dt_free_map = at91_dt_free_map,
336};
337
Sachin Kamat3c936002013-03-15 10:07:03 +0530338static void __iomem *pin_to_controller(struct at91_pinctrl *info,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800339 unsigned int bank)
340{
David Dueck1ab36382015-07-28 09:48:16 +0200341 if (!gpio_chips[bank])
342 return NULL;
343
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800344 return gpio_chips[bank]->regbase;
345}
346
347static inline int pin_to_bank(unsigned pin)
348{
349 return pin /= MAX_NB_GPIO_PER_BANK;
350}
351
352static unsigned pin_to_mask(unsigned int pin)
353{
354 return 1 << pin;
355}
356
Marek Roszko4334ac22014-08-23 23:12:04 -0400357static unsigned two_bit_pin_value_shift_amount(unsigned int pin)
358{
359 /* return the shift value for a pin for "two bit" per pin registers,
360 * i.e. drive strength */
361 return 2*((pin >= MAX_NB_GPIO_PER_BANK/2)
362 ? pin - MAX_NB_GPIO_PER_BANK/2 : pin);
363}
364
365static unsigned sama5d3_get_drive_register(unsigned int pin)
366{
367 /* drive strength is split between two registers
368 * with two bits per pin */
369 return (pin >= MAX_NB_GPIO_PER_BANK/2)
370 ? SAMA5D3_PIO_DRIVER2 : SAMA5D3_PIO_DRIVER1;
371}
372
373static unsigned at91sam9x5_get_drive_register(unsigned int pin)
374{
375 /* drive strength is split between two registers
376 * with two bits per pin */
377 return (pin >= MAX_NB_GPIO_PER_BANK/2)
378 ? AT91SAM9X5_PIO_DRIVER2 : AT91SAM9X5_PIO_DRIVER1;
379}
380
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800381static void at91_mux_disable_interrupt(void __iomem *pio, unsigned mask)
382{
383 writel_relaxed(mask, pio + PIO_IDR);
384}
385
386static unsigned at91_mux_get_pullup(void __iomem *pio, unsigned pin)
387{
Boris BREZILLON05d35342013-08-27 15:19:21 +0200388 return !((readl_relaxed(pio + PIO_PUSR) >> pin) & 0x1);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800389}
390
391static void at91_mux_set_pullup(void __iomem *pio, unsigned mask, bool on)
392{
Wenyou Yang3d784272014-09-11 16:40:15 +0200393 if (on)
394 writel_relaxed(mask, pio + PIO_PPDDR);
395
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800396 writel_relaxed(mask, pio + (on ? PIO_PUER : PIO_PUDR));
397}
398
Boris BREZILLON96bb12d2016-10-28 15:54:10 +0800399static bool at91_mux_get_output(void __iomem *pio, unsigned int pin, bool *val)
400{
401 *val = (readl_relaxed(pio + PIO_ODSR) >> pin) & 0x1;
402 return (readl_relaxed(pio + PIO_OSR) >> pin) & 0x1;
403}
404
405static void at91_mux_set_output(void __iomem *pio, unsigned int mask,
406 bool is_on, bool val)
407{
408 writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
409 writel_relaxed(mask, pio + (is_on ? PIO_OER : PIO_ODR));
410}
411
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800412static unsigned at91_mux_get_multidrive(void __iomem *pio, unsigned pin)
413{
414 return (readl_relaxed(pio + PIO_MDSR) >> pin) & 0x1;
415}
416
417static void at91_mux_set_multidrive(void __iomem *pio, unsigned mask, bool on)
418{
419 writel_relaxed(mask, pio + (on ? PIO_MDER : PIO_MDDR));
420}
421
422static void at91_mux_set_A_periph(void __iomem *pio, unsigned mask)
423{
424 writel_relaxed(mask, pio + PIO_ASR);
425}
426
427static void at91_mux_set_B_periph(void __iomem *pio, unsigned mask)
428{
429 writel_relaxed(mask, pio + PIO_BSR);
430}
431
432static void at91_mux_pio3_set_A_periph(void __iomem *pio, unsigned mask)
433{
434
435 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask,
436 pio + PIO_ABCDSR1);
437 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
438 pio + PIO_ABCDSR2);
439}
440
441static void at91_mux_pio3_set_B_periph(void __iomem *pio, unsigned mask)
442{
443 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask,
444 pio + PIO_ABCDSR1);
445 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
446 pio + PIO_ABCDSR2);
447}
448
449static void at91_mux_pio3_set_C_periph(void __iomem *pio, unsigned mask)
450{
451 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask, pio + PIO_ABCDSR1);
452 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
453}
454
455static void at91_mux_pio3_set_D_periph(void __iomem *pio, unsigned mask)
456{
457 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask, pio + PIO_ABCDSR1);
458 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
459}
460
461static enum at91_mux at91_mux_pio3_get_periph(void __iomem *pio, unsigned mask)
462{
463 unsigned select;
464
465 if (readl_relaxed(pio + PIO_PSR) & mask)
466 return AT91_MUX_GPIO;
467
468 select = !!(readl_relaxed(pio + PIO_ABCDSR1) & mask);
469 select |= (!!(readl_relaxed(pio + PIO_ABCDSR2) & mask) << 1);
470
471 return select + 1;
472}
473
474static enum at91_mux at91_mux_get_periph(void __iomem *pio, unsigned mask)
475{
476 unsigned select;
477
478 if (readl_relaxed(pio + PIO_PSR) & mask)
479 return AT91_MUX_GPIO;
480
481 select = readl_relaxed(pio + PIO_ABSR) & mask;
482
483 return select + 1;
484}
485
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800486static bool at91_mux_get_deglitch(void __iomem *pio, unsigned pin)
487{
Ben Dooksd480239b2015-03-26 12:18:49 +0000488 return (readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1;
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800489}
490
491static void at91_mux_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
492{
Ben Dooksd480239b2015-03-26 12:18:49 +0000493 writel_relaxed(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800494}
495
Boris BREZILLONc8dba022013-09-13 09:47:22 +0200496static bool at91_mux_pio3_get_deglitch(void __iomem *pio, unsigned pin)
497{
Ben Dooksd480239b2015-03-26 12:18:49 +0000498 if ((readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1)
499 return !((readl_relaxed(pio + PIO_IFSCSR) >> pin) & 0x1);
Boris BREZILLONc8dba022013-09-13 09:47:22 +0200500
501 return false;
502}
503
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800504static void at91_mux_pio3_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
505{
506 if (is_on)
Ben Dooksd480239b2015-03-26 12:18:49 +0000507 writel_relaxed(mask, pio + PIO_IFSCDR);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800508 at91_mux_set_deglitch(pio, mask, is_on);
509}
510
511static bool at91_mux_pio3_get_debounce(void __iomem *pio, unsigned pin, u32 *div)
512{
Ben Dooksd480239b2015-03-26 12:18:49 +0000513 *div = readl_relaxed(pio + PIO_SCDR);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800514
Ben Dooksd480239b2015-03-26 12:18:49 +0000515 return ((readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1) &&
516 ((readl_relaxed(pio + PIO_IFSCSR) >> pin) & 0x1);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800517}
518
519static void at91_mux_pio3_set_debounce(void __iomem *pio, unsigned mask,
520 bool is_on, u32 div)
521{
522 if (is_on) {
Ben Dooksd480239b2015-03-26 12:18:49 +0000523 writel_relaxed(mask, pio + PIO_IFSCER);
524 writel_relaxed(div & PIO_SCDR_DIV, pio + PIO_SCDR);
525 writel_relaxed(mask, pio + PIO_IFER);
Boris BREZILLONc8dba022013-09-13 09:47:22 +0200526 } else
Ben Dooksd480239b2015-03-26 12:18:49 +0000527 writel_relaxed(mask, pio + PIO_IFSCDR);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800528}
529
530static bool at91_mux_pio3_get_pulldown(void __iomem *pio, unsigned pin)
531{
Ben Dooksd480239b2015-03-26 12:18:49 +0000532 return !((readl_relaxed(pio + PIO_PPDSR) >> pin) & 0x1);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800533}
534
535static void at91_mux_pio3_set_pulldown(void __iomem *pio, unsigned mask, bool is_on)
536{
Wenyou Yang3d784272014-09-11 16:40:15 +0200537 if (is_on)
Ben Dooksd480239b2015-03-26 12:18:49 +0000538 writel_relaxed(mask, pio + PIO_PUDR);
Wenyou Yang3d784272014-09-11 16:40:15 +0200539
Ben Dooksd480239b2015-03-26 12:18:49 +0000540 writel_relaxed(mask, pio + (is_on ? PIO_PPDER : PIO_PPDDR));
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800541}
542
543static void at91_mux_pio3_disable_schmitt_trig(void __iomem *pio, unsigned mask)
544{
Ben Dooksd480239b2015-03-26 12:18:49 +0000545 writel_relaxed(readl_relaxed(pio + PIO_SCHMITT) | mask, pio + PIO_SCHMITT);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800546}
547
548static bool at91_mux_pio3_get_schmitt_trig(void __iomem *pio, unsigned pin)
549{
Ben Dooksd480239b2015-03-26 12:18:49 +0000550 return (readl_relaxed(pio + PIO_SCHMITT) >> pin) & 0x1;
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800551}
552
Marek Roszko4334ac22014-08-23 23:12:04 -0400553static inline u32 read_drive_strength(void __iomem *reg, unsigned pin)
554{
Ben Dooksd480239b2015-03-26 12:18:49 +0000555 unsigned tmp = readl_relaxed(reg);
Marek Roszko4334ac22014-08-23 23:12:04 -0400556
557 tmp = tmp >> two_bit_pin_value_shift_amount(pin);
558
559 return tmp & DRIVE_STRENGTH_MASK;
560}
561
562static unsigned at91_mux_sama5d3_get_drivestrength(void __iomem *pio,
563 unsigned pin)
564{
565 unsigned tmp = read_drive_strength(pio +
566 sama5d3_get_drive_register(pin), pin);
567
568 /* SAMA5 strength is 1:1 with our defines,
569 * except 0 is equivalent to low per datasheet */
570 if (!tmp)
Claudiu Bezneab67328e2019-02-07 09:24:46 +0000571 tmp = DRIVE_STRENGTH_BIT_MSK(LOW);
Marek Roszko4334ac22014-08-23 23:12:04 -0400572
573 return tmp;
574}
575
576static unsigned at91_mux_sam9x5_get_drivestrength(void __iomem *pio,
577 unsigned pin)
578{
579 unsigned tmp = read_drive_strength(pio +
580 at91sam9x5_get_drive_register(pin), pin);
581
582 /* strength is inverse in SAM9x5s hardware with the pinctrl defines
583 * hardware: 0 = hi, 1 = med, 2 = low, 3 = rsvd */
Claudiu Bezneab67328e2019-02-07 09:24:46 +0000584 tmp = DRIVE_STRENGTH_BIT_MSK(HI) - tmp;
Marek Roszko4334ac22014-08-23 23:12:04 -0400585
586 return tmp;
587}
588
Claudiu Beznea42ef7552019-02-07 09:24:49 +0000589static unsigned at91_mux_sam9x60_get_drivestrength(void __iomem *pio,
590 unsigned pin)
591{
592 unsigned tmp = readl_relaxed(pio + SAM9X60_PIO_DRIVER1);
593
594 if (tmp & BIT(pin))
595 return DRIVE_STRENGTH_BIT_HI;
596
597 return DRIVE_STRENGTH_BIT_LOW;
598}
599
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000600static unsigned at91_mux_sam9x60_get_slewrate(void __iomem *pio, unsigned pin)
601{
602 unsigned tmp = readl_relaxed(pio + SAM9X60_PIO_SLEWR);
603
604 if ((tmp & BIT(pin)))
605 return SLEWRATE_BIT_ENA;
606
607 return SLEWRATE_BIT_DIS;
608}
609
Marek Roszko4334ac22014-08-23 23:12:04 -0400610static void set_drive_strength(void __iomem *reg, unsigned pin, u32 strength)
611{
Ben Dooksd480239b2015-03-26 12:18:49 +0000612 unsigned tmp = readl_relaxed(reg);
Marek Roszko4334ac22014-08-23 23:12:04 -0400613 unsigned shift = two_bit_pin_value_shift_amount(pin);
614
615 tmp &= ~(DRIVE_STRENGTH_MASK << shift);
616 tmp |= strength << shift;
617
Ben Dooksd480239b2015-03-26 12:18:49 +0000618 writel_relaxed(tmp, reg);
Marek Roszko4334ac22014-08-23 23:12:04 -0400619}
620
621static void at91_mux_sama5d3_set_drivestrength(void __iomem *pio, unsigned pin,
622 u32 setting)
623{
624 /* do nothing if setting is zero */
625 if (!setting)
626 return;
627
628 /* strength is 1 to 1 with setting for SAMA5 */
629 set_drive_strength(pio + sama5d3_get_drive_register(pin), pin, setting);
630}
631
632static void at91_mux_sam9x5_set_drivestrength(void __iomem *pio, unsigned pin,
633 u32 setting)
634{
635 /* do nothing if setting is zero */
636 if (!setting)
637 return;
638
639 /* strength is inverse on SAM9x5s with our defines
640 * 0 = hi, 1 = med, 2 = low, 3 = rsvd */
Claudiu Bezneab67328e2019-02-07 09:24:46 +0000641 setting = DRIVE_STRENGTH_BIT_MSK(HI) - setting;
Marek Roszko4334ac22014-08-23 23:12:04 -0400642
643 set_drive_strength(pio + at91sam9x5_get_drive_register(pin), pin,
644 setting);
645}
646
Claudiu Beznea42ef7552019-02-07 09:24:49 +0000647static void at91_mux_sam9x60_set_drivestrength(void __iomem *pio, unsigned pin,
648 u32 setting)
649{
650 unsigned int tmp;
651
652 if (setting <= DRIVE_STRENGTH_BIT_DEF ||
653 setting == DRIVE_STRENGTH_BIT_MED ||
654 setting > DRIVE_STRENGTH_BIT_HI)
655 return;
656
657 tmp = readl_relaxed(pio + SAM9X60_PIO_DRIVER1);
658
659 /* Strength is 0: low, 1: hi */
660 if (setting == DRIVE_STRENGTH_BIT_LOW)
661 tmp &= ~BIT(pin);
662 else
663 tmp |= BIT(pin);
664
665 writel_relaxed(tmp, pio + SAM9X60_PIO_DRIVER1);
666}
667
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000668static void at91_mux_sam9x60_set_slewrate(void __iomem *pio, unsigned pin,
669 u32 setting)
670{
671 unsigned int tmp;
672
673 if (setting < SLEWRATE_BIT_DIS || setting > SLEWRATE_BIT_ENA)
674 return;
675
676 tmp = readl_relaxed(pio + SAM9X60_PIO_SLEWR);
677
678 if (setting == SLEWRATE_BIT_DIS)
679 tmp &= ~BIT(pin);
680 else
681 tmp |= BIT(pin);
682
683 writel_relaxed(tmp, pio + SAM9X60_PIO_SLEWR);
684}
685
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800686static struct at91_pinctrl_mux_ops at91rm9200_ops = {
687 .get_periph = at91_mux_get_periph,
688 .mux_A_periph = at91_mux_set_A_periph,
689 .mux_B_periph = at91_mux_set_B_periph,
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800690 .get_deglitch = at91_mux_get_deglitch,
691 .set_deglitch = at91_mux_set_deglitch,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800692 .irq_type = gpio_irq_type,
693};
694
695static struct at91_pinctrl_mux_ops at91sam9x5_ops = {
696 .get_periph = at91_mux_pio3_get_periph,
697 .mux_A_periph = at91_mux_pio3_set_A_periph,
698 .mux_B_periph = at91_mux_pio3_set_B_periph,
699 .mux_C_periph = at91_mux_pio3_set_C_periph,
700 .mux_D_periph = at91_mux_pio3_set_D_periph,
Boris BREZILLONc8dba022013-09-13 09:47:22 +0200701 .get_deglitch = at91_mux_pio3_get_deglitch,
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800702 .set_deglitch = at91_mux_pio3_set_deglitch,
703 .get_debounce = at91_mux_pio3_get_debounce,
704 .set_debounce = at91_mux_pio3_set_debounce,
705 .get_pulldown = at91_mux_pio3_get_pulldown,
706 .set_pulldown = at91_mux_pio3_set_pulldown,
707 .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
708 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
Marek Roszko4334ac22014-08-23 23:12:04 -0400709 .get_drivestrength = at91_mux_sam9x5_get_drivestrength,
710 .set_drivestrength = at91_mux_sam9x5_set_drivestrength,
711 .irq_type = alt_gpio_irq_type,
712};
713
Claudiu Beznea42ef7552019-02-07 09:24:49 +0000714static const struct at91_pinctrl_mux_ops sam9x60_ops = {
715 .get_periph = at91_mux_pio3_get_periph,
716 .mux_A_periph = at91_mux_pio3_set_A_periph,
717 .mux_B_periph = at91_mux_pio3_set_B_periph,
718 .mux_C_periph = at91_mux_pio3_set_C_periph,
719 .mux_D_periph = at91_mux_pio3_set_D_periph,
720 .get_deglitch = at91_mux_pio3_get_deglitch,
721 .set_deglitch = at91_mux_pio3_set_deglitch,
722 .get_debounce = at91_mux_pio3_get_debounce,
723 .set_debounce = at91_mux_pio3_set_debounce,
724 .get_pulldown = at91_mux_pio3_get_pulldown,
725 .set_pulldown = at91_mux_pio3_set_pulldown,
726 .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
727 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
728 .get_drivestrength = at91_mux_sam9x60_get_drivestrength,
729 .set_drivestrength = at91_mux_sam9x60_set_drivestrength,
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000730 .get_slewrate = at91_mux_sam9x60_get_slewrate,
731 .set_slewrate = at91_mux_sam9x60_set_slewrate,
Claudiu Beznea42ef7552019-02-07 09:24:49 +0000732 .irq_type = alt_gpio_irq_type,
733
734};
735
Marek Roszko4334ac22014-08-23 23:12:04 -0400736static struct at91_pinctrl_mux_ops sama5d3_ops = {
737 .get_periph = at91_mux_pio3_get_periph,
738 .mux_A_periph = at91_mux_pio3_set_A_periph,
739 .mux_B_periph = at91_mux_pio3_set_B_periph,
740 .mux_C_periph = at91_mux_pio3_set_C_periph,
741 .mux_D_periph = at91_mux_pio3_set_D_periph,
742 .get_deglitch = at91_mux_pio3_get_deglitch,
743 .set_deglitch = at91_mux_pio3_set_deglitch,
744 .get_debounce = at91_mux_pio3_get_debounce,
745 .set_debounce = at91_mux_pio3_set_debounce,
746 .get_pulldown = at91_mux_pio3_get_pulldown,
747 .set_pulldown = at91_mux_pio3_set_pulldown,
748 .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
749 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
750 .get_drivestrength = at91_mux_sama5d3_get_drivestrength,
751 .set_drivestrength = at91_mux_sama5d3_set_drivestrength,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800752 .irq_type = alt_gpio_irq_type,
753};
754
755static void at91_pin_dbg(const struct device *dev, const struct at91_pmx_pin *pin)
756{
757 if (pin->mux) {
Hans Wennborg4b6fe452014-08-05 21:43:16 -0700758 dev_dbg(dev, "pio%c%d configured as periph%c with conf = 0x%lx\n",
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800759 pin->bank + 'A', pin->pin, pin->mux - 1 + 'A', pin->conf);
760 } else {
Hans Wennborg4b6fe452014-08-05 21:43:16 -0700761 dev_dbg(dev, "pio%c%d configured as gpio with conf = 0x%lx\n",
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800762 pin->bank + 'A', pin->pin, pin->conf);
763 }
764}
765
Sachin Kamat3c936002013-03-15 10:07:03 +0530766static int pin_check_config(struct at91_pinctrl *info, const char *name,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800767 int index, const struct at91_pmx_pin *pin)
768{
769 int mux;
770
771 /* check if it's a valid config */
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +0100772 if (pin->bank >= gpio_banks) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800773 dev_err(info->dev, "%s: pin conf %d bank_id %d >= nbanks %d\n",
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +0100774 name, index, pin->bank, gpio_banks);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800775 return -EINVAL;
776 }
777
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +0100778 if (!gpio_chips[pin->bank]) {
779 dev_err(info->dev, "%s: pin conf %d bank_id %d not enabled\n",
780 name, index, pin->bank);
781 return -ENXIO;
782 }
783
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800784 if (pin->pin >= MAX_NB_GPIO_PER_BANK) {
785 dev_err(info->dev, "%s: pin conf %d pin_bank_id %d >= %d\n",
786 name, index, pin->pin, MAX_NB_GPIO_PER_BANK);
787 return -EINVAL;
788 }
789
790 if (!pin->mux)
791 return 0;
792
793 mux = pin->mux - 1;
794
795 if (mux >= info->nmux) {
796 dev_err(info->dev, "%s: pin conf %d mux_id %d >= nmux %d\n",
797 name, index, mux, info->nmux);
798 return -EINVAL;
799 }
800
801 if (!(info->mux_mask[pin->bank * info->nmux + mux] & 1 << pin->pin)) {
802 dev_err(info->dev, "%s: pin conf %d mux_id %d not supported for pio%c%d\n",
803 name, index, mux, pin->bank + 'A', pin->pin);
804 return -EINVAL;
805 }
806
807 return 0;
808}
809
810static void at91_mux_gpio_disable(void __iomem *pio, unsigned mask)
811{
812 writel_relaxed(mask, pio + PIO_PDR);
813}
814
815static void at91_mux_gpio_enable(void __iomem *pio, unsigned mask, bool input)
816{
817 writel_relaxed(mask, pio + PIO_PER);
818 writel_relaxed(mask, pio + (input ? PIO_ODR : PIO_OER));
819}
820
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200821static int at91_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
822 unsigned group)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800823{
824 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
825 const struct at91_pmx_pin *pins_conf = info->groups[group].pins_conf;
826 const struct at91_pmx_pin *pin;
827 uint32_t npins = info->groups[group].npins;
828 int i, ret;
829 unsigned mask;
830 void __iomem *pio;
831
832 dev_dbg(info->dev, "enable function %s group %s\n",
833 info->functions[selector].name, info->groups[group].name);
834
835 /* first check that all the pins of the group are valid with a valid
Alexandre Belloni61e310a2013-10-16 16:12:33 +0200836 * parameter */
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800837 for (i = 0; i < npins; i++) {
838 pin = &pins_conf[i];
839 ret = pin_check_config(info, info->groups[group].name, i, pin);
840 if (ret)
841 return ret;
842 }
843
844 for (i = 0; i < npins; i++) {
845 pin = &pins_conf[i];
846 at91_pin_dbg(info->dev, pin);
847 pio = pin_to_controller(info, pin->bank);
David Dueck1ab36382015-07-28 09:48:16 +0200848
849 if (!pio)
850 continue;
851
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800852 mask = pin_to_mask(pin->pin);
853 at91_mux_disable_interrupt(pio, mask);
Sachin Kamat3c936002013-03-15 10:07:03 +0530854 switch (pin->mux) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800855 case AT91_MUX_GPIO:
856 at91_mux_gpio_enable(pio, mask, 1);
857 break;
858 case AT91_MUX_PERIPH_A:
859 info->ops->mux_A_periph(pio, mask);
860 break;
861 case AT91_MUX_PERIPH_B:
862 info->ops->mux_B_periph(pio, mask);
863 break;
864 case AT91_MUX_PERIPH_C:
865 if (!info->ops->mux_C_periph)
866 return -EINVAL;
867 info->ops->mux_C_periph(pio, mask);
868 break;
869 case AT91_MUX_PERIPH_D:
870 if (!info->ops->mux_D_periph)
871 return -EINVAL;
872 info->ops->mux_D_periph(pio, mask);
873 break;
874 }
875 if (pin->mux)
876 at91_mux_gpio_disable(pio, mask);
877 }
878
879 return 0;
880}
881
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800882static int at91_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
883{
884 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
885
886 return info->nfunctions;
887}
888
889static const char *at91_pmx_get_func_name(struct pinctrl_dev *pctldev,
890 unsigned selector)
891{
892 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
893
894 return info->functions[selector].name;
895}
896
897static int at91_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
898 const char * const **groups,
899 unsigned * const num_groups)
900{
901 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
902
903 *groups = info->functions[selector].groups;
904 *num_groups = info->functions[selector].ngroups;
905
906 return 0;
907}
908
Axel Linf6f94f62012-11-05 21:23:50 +0800909static int at91_gpio_request_enable(struct pinctrl_dev *pctldev,
910 struct pinctrl_gpio_range *range,
911 unsigned offset)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800912{
913 struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
914 struct at91_gpio_chip *at91_chip;
915 struct gpio_chip *chip;
916 unsigned mask;
917
918 if (!range) {
919 dev_err(npct->dev, "invalid range\n");
920 return -EINVAL;
921 }
922 if (!range->gc) {
923 dev_err(npct->dev, "missing GPIO chip in range\n");
924 return -EINVAL;
925 }
926 chip = range->gc;
Linus Walleij370ea612015-12-08 09:27:45 +0100927 at91_chip = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800928
929 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
930
931 mask = 1 << (offset - chip->base);
932
933 dev_dbg(npct->dev, "enable pin %u as PIO%c%d 0x%x\n",
934 offset, 'A' + range->id, offset - chip->base, mask);
935
936 writel_relaxed(mask, at91_chip->regbase + PIO_PER);
937
938 return 0;
939}
940
Axel Linf6f94f62012-11-05 21:23:50 +0800941static void at91_gpio_disable_free(struct pinctrl_dev *pctldev,
942 struct pinctrl_gpio_range *range,
943 unsigned offset)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800944{
945 struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
946
947 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
948 /* Set the pin to some default state, GPIO is usually default */
949}
950
Laurent Pinchart022ab142013-02-16 10:25:07 +0100951static const struct pinmux_ops at91_pmx_ops = {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800952 .get_functions_count = at91_pmx_get_funcs_count,
953 .get_function_name = at91_pmx_get_func_name,
954 .get_function_groups = at91_pmx_get_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200955 .set_mux = at91_pmx_set,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800956 .gpio_request_enable = at91_gpio_request_enable,
957 .gpio_disable_free = at91_gpio_disable_free,
958};
959
960static int at91_pinconf_get(struct pinctrl_dev *pctldev,
961 unsigned pin_id, unsigned long *config)
962{
963 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
964 void __iomem *pio;
965 unsigned pin;
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800966 int div;
Boris BREZILLON96bb12d2016-10-28 15:54:10 +0800967 bool out;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800968
Alexandre Belloni1292e692013-12-07 14:08:53 +0100969 *config = 0;
970 dev_dbg(info->dev, "%s:%d, pin_id=%d", __func__, __LINE__, pin_id);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800971 pio = pin_to_controller(info, pin_to_bank(pin_id));
David Dueck1ab36382015-07-28 09:48:16 +0200972
973 if (!pio)
974 return -EINVAL;
975
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800976 pin = pin_id % MAX_NB_GPIO_PER_BANK;
977
978 if (at91_mux_get_multidrive(pio, pin))
979 *config |= MULTI_DRIVE;
980
981 if (at91_mux_get_pullup(pio, pin))
982 *config |= PULL_UP;
983
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800984 if (info->ops->get_deglitch && info->ops->get_deglitch(pio, pin))
985 *config |= DEGLITCH;
986 if (info->ops->get_debounce && info->ops->get_debounce(pio, pin, &div))
987 *config |= DEBOUNCE | (div << DEBOUNCE_VAL_SHIFT);
988 if (info->ops->get_pulldown && info->ops->get_pulldown(pio, pin))
989 *config |= PULL_DOWN;
990 if (info->ops->get_schmitt_trig && info->ops->get_schmitt_trig(pio, pin))
991 *config |= DIS_SCHMIT;
Marek Roszko4334ac22014-08-23 23:12:04 -0400992 if (info->ops->get_drivestrength)
993 *config |= (info->ops->get_drivestrength(pio, pin)
994 << DRIVE_STRENGTH_SHIFT);
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000995 if (info->ops->get_slewrate)
996 *config |= (info->ops->get_slewrate(pio, pin) << SLEWRATE_SHIFT);
Boris BREZILLON96bb12d2016-10-28 15:54:10 +0800997 if (at91_mux_get_output(pio, pin, &out))
998 *config |= OUTPUT | (out << OUTPUT_VAL_SHIFT);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800999
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001000 return 0;
1001}
1002
1003static int at91_pinconf_set(struct pinctrl_dev *pctldev,
Sherman Yin03b054e2013-08-27 11:32:12 -07001004 unsigned pin_id, unsigned long *configs,
1005 unsigned num_configs)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001006{
1007 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1008 unsigned mask;
1009 void __iomem *pio;
Sherman Yin03b054e2013-08-27 11:32:12 -07001010 int i;
1011 unsigned long config;
Marek Roszko4334ac22014-08-23 23:12:04 -04001012 unsigned pin;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001013
Sherman Yin03b054e2013-08-27 11:32:12 -07001014 for (i = 0; i < num_configs; i++) {
1015 config = configs[i];
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001016
Sherman Yin03b054e2013-08-27 11:32:12 -07001017 dev_dbg(info->dev,
1018 "%s:%d, pin_id=%d, config=0x%lx",
1019 __func__, __LINE__, pin_id, config);
1020 pio = pin_to_controller(info, pin_to_bank(pin_id));
David Dueck1ab36382015-07-28 09:48:16 +02001021
1022 if (!pio)
1023 return -EINVAL;
1024
Marek Roszko4334ac22014-08-23 23:12:04 -04001025 pin = pin_id % MAX_NB_GPIO_PER_BANK;
1026 mask = pin_to_mask(pin);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +08001027
Sherman Yin03b054e2013-08-27 11:32:12 -07001028 if (config & PULL_UP && config & PULL_DOWN)
1029 return -EINVAL;
1030
Boris BREZILLON96bb12d2016-10-28 15:54:10 +08001031 at91_mux_set_output(pio, mask, config & OUTPUT,
1032 (config & OUTPUT_VAL) >> OUTPUT_VAL_SHIFT);
Sherman Yin03b054e2013-08-27 11:32:12 -07001033 at91_mux_set_pullup(pio, mask, config & PULL_UP);
1034 at91_mux_set_multidrive(pio, mask, config & MULTI_DRIVE);
1035 if (info->ops->set_deglitch)
1036 info->ops->set_deglitch(pio, mask, config & DEGLITCH);
1037 if (info->ops->set_debounce)
1038 info->ops->set_debounce(pio, mask, config & DEBOUNCE,
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +08001039 (config & DEBOUNCE_VAL) >> DEBOUNCE_VAL_SHIFT);
Sherman Yin03b054e2013-08-27 11:32:12 -07001040 if (info->ops->set_pulldown)
1041 info->ops->set_pulldown(pio, mask, config & PULL_DOWN);
1042 if (info->ops->disable_schmitt_trig && config & DIS_SCHMIT)
1043 info->ops->disable_schmitt_trig(pio, mask);
Marek Roszko4334ac22014-08-23 23:12:04 -04001044 if (info->ops->set_drivestrength)
1045 info->ops->set_drivestrength(pio, pin,
1046 (config & DRIVE_STRENGTH)
1047 >> DRIVE_STRENGTH_SHIFT);
Claudiu Beznea64e21ad2019-02-07 09:25:05 +00001048 if (info->ops->set_slewrate)
1049 info->ops->set_slewrate(pio, pin,
1050 (config & SLEWRATE) >> SLEWRATE_SHIFT);
Sherman Yin03b054e2013-08-27 11:32:12 -07001051
1052 } /* for each config */
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +08001053
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001054 return 0;
1055}
1056
Alexandre Belloni4d9b8a82013-12-07 14:08:54 +01001057#define DBG_SHOW_FLAG(flag) do { \
1058 if (config & flag) { \
1059 if (num_conf) \
1060 seq_puts(s, "|"); \
1061 seq_puts(s, #flag); \
1062 num_conf++; \
1063 } \
1064} while (0)
1065
Claudiu Bezneab67328e2019-02-07 09:24:46 +00001066#define DBG_SHOW_FLAG_MASKED(mask, flag, name) do { \
Marek Roszko4334ac22014-08-23 23:12:04 -04001067 if ((config & mask) == flag) { \
1068 if (num_conf) \
1069 seq_puts(s, "|"); \
Claudiu Bezneab67328e2019-02-07 09:24:46 +00001070 seq_puts(s, #name); \
Marek Roszko4334ac22014-08-23 23:12:04 -04001071 num_conf++; \
1072 } \
1073} while (0)
1074
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001075static void at91_pinconf_dbg_show(struct pinctrl_dev *pctldev,
1076 struct seq_file *s, unsigned pin_id)
1077{
Alexandre Belloni4d9b8a82013-12-07 14:08:54 +01001078 unsigned long config;
Rickard Strandqvist445d2022014-06-26 15:41:31 +02001079 int val, num_conf = 0;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001080
Rickard Strandqvist445d2022014-06-26 15:41:31 +02001081 at91_pinconf_get(pctldev, pin_id, &config);
Alexandre Belloni4d9b8a82013-12-07 14:08:54 +01001082
1083 DBG_SHOW_FLAG(MULTI_DRIVE);
1084 DBG_SHOW_FLAG(PULL_UP);
1085 DBG_SHOW_FLAG(PULL_DOWN);
1086 DBG_SHOW_FLAG(DIS_SCHMIT);
1087 DBG_SHOW_FLAG(DEGLITCH);
Claudiu Bezneab67328e2019-02-07 09:24:46 +00001088 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_BIT_MSK(LOW),
1089 DRIVE_STRENGTH_LOW);
1090 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_BIT_MSK(MED),
1091 DRIVE_STRENGTH_MED);
1092 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_BIT_MSK(HI),
1093 DRIVE_STRENGTH_HI);
Claudiu Beznea64e21ad2019-02-07 09:25:05 +00001094 DBG_SHOW_FLAG(SLEWRATE);
Alexandre Belloni4d9b8a82013-12-07 14:08:54 +01001095 DBG_SHOW_FLAG(DEBOUNCE);
1096 if (config & DEBOUNCE) {
1097 val = config >> DEBOUNCE_VAL_SHIFT;
1098 seq_printf(s, "(%d)", val);
1099 }
1100
1101 return;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001102}
1103
1104static void at91_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
1105 struct seq_file *s, unsigned group)
1106{
1107}
1108
Laurent Pinchart022ab142013-02-16 10:25:07 +01001109static const struct pinconf_ops at91_pinconf_ops = {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001110 .pin_config_get = at91_pinconf_get,
1111 .pin_config_set = at91_pinconf_set,
1112 .pin_config_dbg_show = at91_pinconf_dbg_show,
1113 .pin_config_group_dbg_show = at91_pinconf_group_dbg_show,
1114};
1115
1116static struct pinctrl_desc at91_pinctrl_desc = {
1117 .pctlops = &at91_pctrl_ops,
1118 .pmxops = &at91_pmx_ops,
1119 .confops = &at91_pinconf_ops,
1120 .owner = THIS_MODULE,
1121};
1122
1123static const char *gpio_compat = "atmel,at91rm9200-gpio";
1124
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001125static void at91_pinctrl_child_count(struct at91_pinctrl *info,
1126 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001127{
1128 struct device_node *child;
1129
1130 for_each_child_of_node(np, child) {
1131 if (of_device_is_compatible(child, gpio_compat)) {
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001132 if (of_device_is_available(child))
1133 info->nactive_banks++;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001134 } else {
1135 info->nfunctions++;
1136 info->ngroups += of_get_child_count(child);
1137 }
1138 }
1139}
1140
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001141static int at91_pinctrl_mux_mask(struct at91_pinctrl *info,
1142 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001143{
1144 int ret = 0;
1145 int size;
Sachin Kamat1164d732013-03-15 10:07:02 +05301146 const __be32 *list;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001147
1148 list = of_get_property(np, "atmel,mux-mask", &size);
1149 if (!list) {
1150 dev_err(info->dev, "can not read the mux-mask of %d\n", size);
1151 return -EINVAL;
1152 }
1153
1154 size /= sizeof(*list);
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001155 if (!size || size % gpio_banks) {
1156 dev_err(info->dev, "wrong mux mask array should be by %d\n", gpio_banks);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001157 return -EINVAL;
1158 }
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001159 info->nmux = size / gpio_banks;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001160
Kees Cooka86854d2018-06-12 14:07:58 -07001161 info->mux_mask = devm_kcalloc(info->dev, size, sizeof(u32),
1162 GFP_KERNEL);
Markus Elfring3da941b2017-12-23 20:44:27 +01001163 if (!info->mux_mask)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001164 return -ENOMEM;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001165
1166 ret = of_property_read_u32_array(np, "atmel,mux-mask",
1167 info->mux_mask, size);
1168 if (ret)
1169 dev_err(info->dev, "can not read the mux-mask of %d\n", size);
1170 return ret;
1171}
1172
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001173static int at91_pinctrl_parse_groups(struct device_node *np,
1174 struct at91_pin_group *grp,
1175 struct at91_pinctrl *info, u32 index)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001176{
1177 struct at91_pmx_pin *pin;
1178 int size;
Sachin Kamat1164d732013-03-15 10:07:02 +05301179 const __be32 *list;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001180 int i, j;
1181
Rob Herring94f4e542018-08-27 20:52:41 -05001182 dev_dbg(info->dev, "group(%d): %pOFn\n", index, np);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001183
1184 /* Initialise group */
1185 grp->name = np->name;
1186
1187 /*
1188 * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
1189 * do sanity check and calculate pins number
1190 */
1191 list = of_get_property(np, "atmel,pins", &size);
1192 /* we do not check return since it's safe node passed down */
1193 size /= sizeof(*list);
1194 if (!size || size % 4) {
1195 dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
1196 return -EINVAL;
1197 }
1198
1199 grp->npins = size / 4;
Kees Cooka86854d2018-06-12 14:07:58 -07001200 pin = grp->pins_conf = devm_kcalloc(info->dev,
1201 grp->npins,
1202 sizeof(struct at91_pmx_pin),
1203 GFP_KERNEL);
1204 grp->pins = devm_kcalloc(info->dev, grp->npins, sizeof(unsigned int),
1205 GFP_KERNEL);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001206 if (!grp->pins_conf || !grp->pins)
1207 return -ENOMEM;
1208
1209 for (i = 0, j = 0; i < size; i += 4, j++) {
1210 pin->bank = be32_to_cpu(*list++);
1211 pin->pin = be32_to_cpu(*list++);
1212 grp->pins[j] = pin->bank * MAX_NB_GPIO_PER_BANK + pin->pin;
1213 pin->mux = be32_to_cpu(*list++);
1214 pin->conf = be32_to_cpu(*list++);
1215
1216 at91_pin_dbg(info->dev, pin);
1217 pin++;
1218 }
1219
1220 return 0;
1221}
1222
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001223static int at91_pinctrl_parse_functions(struct device_node *np,
1224 struct at91_pinctrl *info, u32 index)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001225{
1226 struct device_node *child;
1227 struct at91_pmx_func *func;
1228 struct at91_pin_group *grp;
1229 int ret;
1230 static u32 grp_index;
1231 u32 i = 0;
1232
Rob Herring94f4e542018-08-27 20:52:41 -05001233 dev_dbg(info->dev, "parse function(%d): %pOFn\n", index, np);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001234
1235 func = &info->functions[index];
1236
1237 /* Initialise function */
1238 func->name = np->name;
1239 func->ngroups = of_get_child_count(np);
Rickard Strandqvistca7162a2014-06-26 13:26:45 +02001240 if (func->ngroups == 0) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001241 dev_err(info->dev, "no groups defined\n");
1242 return -EINVAL;
1243 }
Kees Cooka86854d2018-06-12 14:07:58 -07001244 func->groups = devm_kcalloc(info->dev,
1245 func->ngroups, sizeof(char *), GFP_KERNEL);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001246 if (!func->groups)
1247 return -ENOMEM;
1248
1249 for_each_child_of_node(np, child) {
1250 func->groups[i] = child->name;
1251 grp = &info->groups[grp_index++];
1252 ret = at91_pinctrl_parse_groups(child, grp, info, i++);
Julia Lawalld94b9862015-10-24 16:42:35 +02001253 if (ret) {
1254 of_node_put(child);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001255 return ret;
Julia Lawalld94b9862015-10-24 16:42:35 +02001256 }
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001257 }
1258
1259 return 0;
1260}
1261
Fabian Frederickbaa9946e2015-03-16 20:59:09 +01001262static const struct of_device_id at91_pinctrl_of_match[] = {
Marek Roszko4334ac22014-08-23 23:12:04 -04001263 { .compatible = "atmel,sama5d3-pinctrl", .data = &sama5d3_ops },
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001264 { .compatible = "atmel,at91sam9x5-pinctrl", .data = &at91sam9x5_ops },
1265 { .compatible = "atmel,at91rm9200-pinctrl", .data = &at91rm9200_ops },
Claudiu Bezneaa2fcb1c2019-02-07 09:24:53 +00001266 { .compatible = "microchip,sam9x60-pinctrl", .data = &sam9x60_ops },
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001267 { /* sentinel */ }
1268};
1269
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001270static int at91_pinctrl_probe_dt(struct platform_device *pdev,
1271 struct at91_pinctrl *info)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001272{
1273 int ret = 0;
1274 int i, j;
1275 uint32_t *tmp;
1276 struct device_node *np = pdev->dev.of_node;
1277 struct device_node *child;
1278
1279 if (!np)
1280 return -ENODEV;
1281
1282 info->dev = &pdev->dev;
Sachin Kamat3c936002013-03-15 10:07:03 +05301283 info->ops = (struct at91_pinctrl_mux_ops *)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001284 of_match_device(at91_pinctrl_of_match, &pdev->dev)->data;
1285 at91_pinctrl_child_count(info, np);
1286
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001287 if (gpio_banks < 1) {
Alexandre Belloni61e310a2013-10-16 16:12:33 +02001288 dev_err(&pdev->dev, "you need to specify at least one gpio-controller\n");
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001289 return -EINVAL;
1290 }
1291
1292 ret = at91_pinctrl_mux_mask(info, np);
1293 if (ret)
1294 return ret;
1295
1296 dev_dbg(&pdev->dev, "nmux = %d\n", info->nmux);
1297
1298 dev_dbg(&pdev->dev, "mux-mask\n");
1299 tmp = info->mux_mask;
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001300 for (i = 0; i < gpio_banks; i++) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001301 for (j = 0; j < info->nmux; j++, tmp++) {
1302 dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
1303 }
1304 }
1305
1306 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1307 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
Kees Cooka86854d2018-06-12 14:07:58 -07001308 info->functions = devm_kcalloc(&pdev->dev,
1309 info->nfunctions,
1310 sizeof(struct at91_pmx_func),
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001311 GFP_KERNEL);
1312 if (!info->functions)
1313 return -ENOMEM;
1314
Kees Cooka86854d2018-06-12 14:07:58 -07001315 info->groups = devm_kcalloc(&pdev->dev,
1316 info->ngroups,
1317 sizeof(struct at91_pin_group),
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001318 GFP_KERNEL);
1319 if (!info->groups)
1320 return -ENOMEM;
1321
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001322 dev_dbg(&pdev->dev, "nbanks = %d\n", gpio_banks);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001323 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1324 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
1325
1326 i = 0;
1327
1328 for_each_child_of_node(np, child) {
1329 if (of_device_is_compatible(child, gpio_compat))
1330 continue;
1331 ret = at91_pinctrl_parse_functions(child, info, i++);
1332 if (ret) {
1333 dev_err(&pdev->dev, "failed to parse function\n");
Julia Lawalld94b9862015-10-24 16:42:35 +02001334 of_node_put(child);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001335 return ret;
1336 }
1337 }
1338
1339 return 0;
1340}
1341
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001342static int at91_pinctrl_probe(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001343{
1344 struct at91_pinctrl *info;
1345 struct pinctrl_pin_desc *pdesc;
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001346 int ret, i, j, k, ngpio_chips_enabled = 0;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001347
1348 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1349 if (!info)
1350 return -ENOMEM;
1351
1352 ret = at91_pinctrl_probe_dt(pdev, info);
1353 if (ret)
1354 return ret;
1355
1356 /*
1357 * We need all the GPIO drivers to probe FIRST, or we will not be able
1358 * to obtain references to the struct gpio_chip * for them, and we
1359 * need this to proceed.
1360 */
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001361 for (i = 0; i < gpio_banks; i++)
1362 if (gpio_chips[i])
1363 ngpio_chips_enabled++;
1364
1365 if (ngpio_chips_enabled < info->nactive_banks) {
1366 dev_warn(&pdev->dev,
1367 "All GPIO chips are not registered yet (%d/%d)\n",
1368 ngpio_chips_enabled, info->nactive_banks);
1369 devm_kfree(&pdev->dev, info);
1370 return -EPROBE_DEFER;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001371 }
1372
1373 at91_pinctrl_desc.name = dev_name(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001374 at91_pinctrl_desc.npins = gpio_banks * MAX_NB_GPIO_PER_BANK;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001375 at91_pinctrl_desc.pins = pdesc =
Kees Cooka86854d2018-06-12 14:07:58 -07001376 devm_kcalloc(&pdev->dev,
1377 at91_pinctrl_desc.npins, sizeof(*pdesc),
1378 GFP_KERNEL);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001379
1380 if (!at91_pinctrl_desc.pins)
1381 return -ENOMEM;
1382
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001383 for (i = 0, k = 0; i < gpio_banks; i++) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001384 for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) {
1385 pdesc->number = k;
1386 pdesc->name = kasprintf(GFP_KERNEL, "pio%c%d", i + 'A', j);
1387 pdesc++;
1388 }
1389 }
1390
1391 platform_set_drvdata(pdev, info);
Laxman Dewangan5c674252016-02-28 14:32:19 +05301392 info->pctl = devm_pinctrl_register(&pdev->dev, &at91_pinctrl_desc,
1393 info);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001394
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001395 if (IS_ERR(info->pctl)) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001396 dev_err(&pdev->dev, "could not register AT91 pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001397 return PTR_ERR(info->pctl);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001398 }
1399
1400 /* We will handle a range of GPIO pins */
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001401 for (i = 0; i < gpio_banks; i++)
1402 if (gpio_chips[i])
1403 pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001404
1405 dev_info(&pdev->dev, "initialized AT91 pinctrl driver\n");
1406
1407 return 0;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001408}
1409
Richard Genoud8af584b2014-02-17 17:57:26 +01001410static int at91_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
1411{
Linus Walleij370ea612015-12-08 09:27:45 +01001412 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Richard Genoud8af584b2014-02-17 17:57:26 +01001413 void __iomem *pio = at91_gpio->regbase;
1414 unsigned mask = 1 << offset;
1415 u32 osr;
1416
1417 osr = readl_relaxed(pio + PIO_OSR);
1418 return !(osr & mask);
1419}
1420
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001421static int at91_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
1422{
Linus Walleij370ea612015-12-08 09:27:45 +01001423 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001424 void __iomem *pio = at91_gpio->regbase;
1425 unsigned mask = 1 << offset;
1426
1427 writel_relaxed(mask, pio + PIO_ODR);
1428 return 0;
1429}
1430
1431static int at91_gpio_get(struct gpio_chip *chip, unsigned offset)
1432{
Linus Walleij370ea612015-12-08 09:27:45 +01001433 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001434 void __iomem *pio = at91_gpio->regbase;
1435 unsigned mask = 1 << offset;
1436 u32 pdsr;
1437
1438 pdsr = readl_relaxed(pio + PIO_PDSR);
1439 return (pdsr & mask) != 0;
1440}
1441
1442static void at91_gpio_set(struct gpio_chip *chip, unsigned offset,
1443 int val)
1444{
Linus Walleij370ea612015-12-08 09:27:45 +01001445 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001446 void __iomem *pio = at91_gpio->regbase;
1447 unsigned mask = 1 << offset;
1448
1449 writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
1450}
1451
Alexander Stein1893b2c2015-04-02 11:55:49 +02001452static void at91_gpio_set_multiple(struct gpio_chip *chip,
1453 unsigned long *mask, unsigned long *bits)
1454{
Linus Walleij370ea612015-12-08 09:27:45 +01001455 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Alexander Stein1893b2c2015-04-02 11:55:49 +02001456 void __iomem *pio = at91_gpio->regbase;
1457
1458#define BITS_MASK(bits) (((bits) == 32) ? ~0U : (BIT(bits) - 1))
1459 /* Mask additionally to ngpio as not all GPIO controllers have 32 pins */
1460 uint32_t set_mask = (*mask & *bits) & BITS_MASK(chip->ngpio);
1461 uint32_t clear_mask = (*mask & ~(*bits)) & BITS_MASK(chip->ngpio);
1462
1463 writel_relaxed(set_mask, pio + PIO_SODR);
1464 writel_relaxed(clear_mask, pio + PIO_CODR);
1465}
1466
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001467static int at91_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
1468 int val)
1469{
Linus Walleij370ea612015-12-08 09:27:45 +01001470 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001471 void __iomem *pio = at91_gpio->regbase;
1472 unsigned mask = 1 << offset;
1473
1474 writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
1475 writel_relaxed(mask, pio + PIO_OER);
1476
1477 return 0;
1478}
1479
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001480#ifdef CONFIG_DEBUG_FS
1481static void at91_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1482{
1483 enum at91_mux mode;
1484 int i;
Linus Walleij370ea612015-12-08 09:27:45 +01001485 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001486 void __iomem *pio = at91_gpio->regbase;
1487
1488 for (i = 0; i < chip->ngpio; i++) {
Alexander Stein47f22712014-04-14 20:53:08 +02001489 unsigned mask = pin_to_mask(i);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001490 const char *gpio_label;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001491
1492 gpio_label = gpiochip_is_requested(chip, i);
1493 if (!gpio_label)
1494 continue;
1495 mode = at91_gpio->ops->get_periph(pio, mask);
1496 seq_printf(s, "[%s] GPIO%s%d: ",
1497 gpio_label, chip->label, i);
1498 if (mode == AT91_MUX_GPIO) {
Matthieu Crapet853b6bf2014-11-18 15:43:45 +01001499 seq_printf(s, "[gpio] ");
1500 seq_printf(s, "%s ",
1501 readl_relaxed(pio + PIO_OSR) & mask ?
1502 "output" : "input");
1503 seq_printf(s, "%s\n",
1504 readl_relaxed(pio + PIO_PDSR) & mask ?
1505 "set" : "clear");
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001506 } else {
1507 seq_printf(s, "[periph %c]\n",
1508 mode + 'A' - 1);
1509 }
1510 }
1511}
1512#else
1513#define at91_gpio_dbg_show NULL
1514#endif
1515
1516/* Several AIC controller irqs are dispatched through this GPIO handler.
1517 * To use any AT91_PIN_* as an externally triggered IRQ, first call
1518 * at91_set_gpio_input() then maybe enable its glitch filter.
1519 * Then just request_irq() with the pin ID; it works like any ARM IRQ
1520 * handler.
1521 * First implementation always triggers on rising and falling edges
1522 * whereas the newer PIO3 can be additionally configured to trigger on
1523 * level, edge with any polarity.
1524 *
1525 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
1526 * configuring them with at91_set_a_periph() or at91_set_b_periph().
1527 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
1528 */
1529
1530static void gpio_irq_mask(struct irq_data *d)
1531{
1532 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1533 void __iomem *pio = at91_gpio->regbase;
1534 unsigned mask = 1 << d->hwirq;
1535
1536 if (pio)
1537 writel_relaxed(mask, pio + PIO_IDR);
1538}
1539
1540static void gpio_irq_unmask(struct irq_data *d)
1541{
1542 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1543 void __iomem *pio = at91_gpio->regbase;
1544 unsigned mask = 1 << d->hwirq;
1545
1546 if (pio)
1547 writel_relaxed(mask, pio + PIO_IER);
1548}
1549
1550static int gpio_irq_type(struct irq_data *d, unsigned type)
1551{
1552 switch (type) {
1553 case IRQ_TYPE_NONE:
1554 case IRQ_TYPE_EDGE_BOTH:
1555 return 0;
1556 default:
1557 return -EINVAL;
1558 }
1559}
1560
1561/* Alternate irq type for PIO3 support */
1562static int alt_gpio_irq_type(struct irq_data *d, unsigned type)
1563{
1564 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1565 void __iomem *pio = at91_gpio->regbase;
1566 unsigned mask = 1 << d->hwirq;
1567
1568 switch (type) {
1569 case IRQ_TYPE_EDGE_RISING:
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001570 irq_set_handler_locked(d, handle_simple_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001571 writel_relaxed(mask, pio + PIO_ESR);
1572 writel_relaxed(mask, pio + PIO_REHLSR);
1573 break;
1574 case IRQ_TYPE_EDGE_FALLING:
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001575 irq_set_handler_locked(d, handle_simple_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001576 writel_relaxed(mask, pio + PIO_ESR);
1577 writel_relaxed(mask, pio + PIO_FELLSR);
1578 break;
1579 case IRQ_TYPE_LEVEL_LOW:
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001580 irq_set_handler_locked(d, handle_level_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001581 writel_relaxed(mask, pio + PIO_LSR);
1582 writel_relaxed(mask, pio + PIO_FELLSR);
1583 break;
1584 case IRQ_TYPE_LEVEL_HIGH:
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001585 irq_set_handler_locked(d, handle_level_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001586 writel_relaxed(mask, pio + PIO_LSR);
1587 writel_relaxed(mask, pio + PIO_REHLSR);
1588 break;
1589 case IRQ_TYPE_EDGE_BOTH:
1590 /*
1591 * disable additional interrupt modes:
1592 * fall back to default behavior
1593 */
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001594 irq_set_handler_locked(d, handle_simple_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001595 writel_relaxed(mask, pio + PIO_AIMDR);
1596 return 0;
1597 case IRQ_TYPE_NONE:
1598 default:
Linus Walleij1c5fb662018-09-13 13:58:21 +02001599 pr_warn("AT91: No type for GPIO irq offset %d\n", d->irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001600 return -EINVAL;
1601 }
1602
1603 /* enable additional interrupt modes */
1604 writel_relaxed(mask, pio + PIO_AIMER);
1605
1606 return 0;
1607}
1608
Alexander Stein80cc3732014-04-15 22:09:41 +02001609static void gpio_irq_ack(struct irq_data *d)
1610{
1611 /* the interrupt is already cleared before by reading ISR */
1612}
1613
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001614#ifdef CONFIG_PM
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001615
1616static u32 wakeups[MAX_GPIO_BANKS];
1617static u32 backups[MAX_GPIO_BANKS];
1618
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001619static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
1620{
1621 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1622 unsigned bank = at91_gpio->pioc_idx;
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001623 unsigned mask = 1 << d->hwirq;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001624
1625 if (unlikely(bank >= MAX_GPIO_BANKS))
1626 return -EINVAL;
1627
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001628 if (state)
1629 wakeups[bank] |= mask;
1630 else
1631 wakeups[bank] &= ~mask;
1632
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001633 irq_set_irq_wake(at91_gpio->pioc_virq, state);
1634
1635 return 0;
1636}
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001637
1638void at91_pinctrl_gpio_suspend(void)
1639{
1640 int i;
1641
1642 for (i = 0; i < gpio_banks; i++) {
1643 void __iomem *pio;
1644
1645 if (!gpio_chips[i])
1646 continue;
1647
1648 pio = gpio_chips[i]->regbase;
1649
Ben Dooksd480239b2015-03-26 12:18:49 +00001650 backups[i] = readl_relaxed(pio + PIO_IMR);
1651 writel_relaxed(backups[i], pio + PIO_IDR);
1652 writel_relaxed(wakeups[i], pio + PIO_IER);
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001653
Boris BREZILLON795f9952013-12-15 19:30:51 +01001654 if (!wakeups[i])
1655 clk_disable_unprepare(gpio_chips[i]->clock);
1656 else
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001657 printk(KERN_DEBUG "GPIO-%c may wake for %08x\n",
1658 'A'+i, wakeups[i]);
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001659 }
1660}
1661
1662void at91_pinctrl_gpio_resume(void)
1663{
1664 int i;
1665
1666 for (i = 0; i < gpio_banks; i++) {
1667 void __iomem *pio;
1668
1669 if (!gpio_chips[i])
1670 continue;
1671
1672 pio = gpio_chips[i]->regbase;
1673
Boris BREZILLON37ef1d92013-12-15 19:30:52 +01001674 if (!wakeups[i])
1675 clk_prepare_enable(gpio_chips[i]->clock);
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001676
Ben Dooksd480239b2015-03-26 12:18:49 +00001677 writel_relaxed(wakeups[i], pio + PIO_IDR);
1678 writel_relaxed(backups[i], pio + PIO_IER);
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001679 }
1680}
1681
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001682#else
1683#define gpio_irq_set_wake NULL
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001684#endif /* CONFIG_PM */
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001685
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +02001686static void gpio_irq_handler(struct irq_desc *desc)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001687{
Jiang Liu5663bb22015-06-04 12:13:16 +08001688 struct irq_chip *chip = irq_desc_get_chip(desc);
Alexander Stein80cc3732014-04-15 22:09:41 +02001689 struct gpio_chip *gpio_chip = irq_desc_get_handler_data(desc);
Linus Walleij370ea612015-12-08 09:27:45 +01001690 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(gpio_chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001691 void __iomem *pio = at91_gpio->regbase;
1692 unsigned long isr;
1693 int n;
1694
1695 chained_irq_enter(chip, desc);
1696 for (;;) {
1697 /* Reading ISR acks pending (edge triggered) GPIO interrupts.
Alexandre Bellonic2eb9e72013-12-07 14:08:52 +01001698 * When there are none pending, we're finished unless we need
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001699 * to process multiple banks (like ID_PIOCDE on sam9263).
1700 */
1701 isr = readl_relaxed(pio + PIO_ISR) & readl_relaxed(pio + PIO_IMR);
1702 if (!isr) {
1703 if (!at91_gpio->next)
1704 break;
1705 at91_gpio = at91_gpio->next;
1706 pio = at91_gpio->regbase;
Alexander Steincccb0c32014-04-24 19:55:39 +02001707 gpio_chip = &at91_gpio->chip;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001708 continue;
1709 }
1710
Wei Yongjun05daa162012-10-26 22:50:54 +08001711 for_each_set_bit(n, &isr, BITS_PER_LONG) {
Alexander Stein80cc3732014-04-15 22:09:41 +02001712 generic_handle_irq(irq_find_mapping(
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001713 gpio_chip->irq.domain, n));
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001714 }
1715 }
1716 chained_irq_exit(chip, desc);
1717 /* now it may re-trigger */
1718}
1719
Pramod Gurav834e1672014-09-09 15:50:37 +05301720static int at91_gpio_of_irq_setup(struct platform_device *pdev,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001721 struct at91_gpio_chip *at91_gpio)
1722{
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001723 struct gpio_chip *gpiochip_prev = NULL;
Alexander Steincccb0c32014-04-24 19:55:39 +02001724 struct at91_gpio_chip *prev = NULL;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001725 struct irq_data *d = irq_get_irq_data(at91_gpio->pioc_virq);
Ludovic Desroches0c3dfa12018-09-13 14:42:13 +02001726 struct irq_chip *gpio_irqchip;
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001727 int ret, i;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001728
Ludovic Desroches0c3dfa12018-09-13 14:42:13 +02001729 gpio_irqchip = devm_kzalloc(&pdev->dev, sizeof(*gpio_irqchip), GFP_KERNEL);
1730 if (!gpio_irqchip)
1731 return -ENOMEM;
1732
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001733 at91_gpio->pioc_hwirq = irqd_to_hwirq(d);
1734
Ludovic Desroches0c3dfa12018-09-13 14:42:13 +02001735 gpio_irqchip->name = "GPIO";
1736 gpio_irqchip->irq_ack = gpio_irq_ack;
1737 gpio_irqchip->irq_disable = gpio_irq_mask;
1738 gpio_irqchip->irq_mask = gpio_irq_mask;
1739 gpio_irqchip->irq_unmask = gpio_irq_unmask;
1740 gpio_irqchip->irq_set_wake = gpio_irq_set_wake,
1741 gpio_irqchip->irq_set_type = at91_gpio->ops->irq_type;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001742
1743 /* Disable irqs of this PIO controller */
1744 writel_relaxed(~0, at91_gpio->regbase + PIO_IDR);
1745
Alexander Stein80cc3732014-04-15 22:09:41 +02001746 /*
1747 * Let the generic code handle this edge IRQ, the the chained
1748 * handler will perform the actual work of handling the parent
1749 * interrupt.
1750 */
1751 ret = gpiochip_irqchip_add(&at91_gpio->chip,
Ludovic Desroches0c3dfa12018-09-13 14:42:13 +02001752 gpio_irqchip,
Alexander Stein80cc3732014-04-15 22:09:41 +02001753 0,
1754 handle_edge_irq,
Marc Zyngier58033482016-09-06 14:58:15 +01001755 IRQ_TYPE_NONE);
Pramod Gurav834e1672014-09-09 15:50:37 +05301756 if (ret) {
1757 dev_err(&pdev->dev, "at91_gpio.%d: Couldn't add irqchip to gpiochip.\n",
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001758 at91_gpio->pioc_idx);
Pramod Gurav834e1672014-09-09 15:50:37 +05301759 return ret;
1760 }
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001761
Alexander Steincccb0c32014-04-24 19:55:39 +02001762 /* The top level handler handles one bank of GPIOs, except
1763 * on some SoC it can handle up to three...
1764 * We only set up the handler for the first of the list.
1765 */
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001766 gpiochip_prev = irq_get_handler_data(at91_gpio->pioc_virq);
1767 if (!gpiochip_prev) {
1768 /* Then register the chain on the parent IRQ */
1769 gpiochip_set_chained_irqchip(&at91_gpio->chip,
Ludovic Desroches0c3dfa12018-09-13 14:42:13 +02001770 gpio_irqchip,
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001771 at91_gpio->pioc_virq,
1772 gpio_irq_handler);
Alexander Steincccb0c32014-04-24 19:55:39 +02001773 return 0;
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001774 }
Alexander Steincccb0c32014-04-24 19:55:39 +02001775
Linus Walleij370ea612015-12-08 09:27:45 +01001776 prev = gpiochip_get_data(gpiochip_prev);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001777
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001778 /* we can only have 2 banks before */
1779 for (i = 0; i < 2; i++) {
1780 if (prev->next) {
1781 prev = prev->next;
1782 } else {
1783 prev->next = at91_gpio;
1784 return 0;
1785 }
1786 }
1787
1788 return -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001789}
1790
1791/* This structure is replicated for each GPIO block allocated at probe time */
Alexander Stein234b6512016-04-29 14:50:02 +02001792static const struct gpio_chip at91_gpio_template = {
Jonas Gorski98c85d52015-10-11 17:34:19 +02001793 .request = gpiochip_generic_request,
1794 .free = gpiochip_generic_free,
Richard Genoud8af584b2014-02-17 17:57:26 +01001795 .get_direction = at91_gpio_get_direction,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001796 .direction_input = at91_gpio_direction_input,
1797 .get = at91_gpio_get,
1798 .direction_output = at91_gpio_direction_output,
1799 .set = at91_gpio_set,
Alexander Stein1893b2c2015-04-02 11:55:49 +02001800 .set_multiple = at91_gpio_set_multiple,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001801 .dbg_show = at91_gpio_dbg_show,
Linus Walleij9fb1f392013-12-04 14:42:46 +01001802 .can_sleep = false,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001803 .ngpio = MAX_NB_GPIO_PER_BANK,
1804};
1805
Fabian Frederickbaa9946e2015-03-16 20:59:09 +01001806static const struct of_device_id at91_gpio_of_match[] = {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001807 { .compatible = "atmel,at91sam9x5-gpio", .data = &at91sam9x5_ops, },
1808 { .compatible = "atmel,at91rm9200-gpio", .data = &at91rm9200_ops },
Claudiu Bezneaa2fcb1c2019-02-07 09:24:53 +00001809 { .compatible = "microchip,sam9x60-gpio", .data = &sam9x60_ops },
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001810 { /* sentinel */ }
1811};
1812
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001813static int at91_gpio_probe(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001814{
1815 struct device_node *np = pdev->dev.of_node;
1816 struct resource *res;
1817 struct at91_gpio_chip *at91_chip = NULL;
1818 struct gpio_chip *chip;
1819 struct pinctrl_gpio_range *range;
1820 int ret = 0;
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001821 int irq, i;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001822 int alias_idx = of_alias_get_id(np, "gpio");
1823 uint32_t ngpio;
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001824 char **names;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001825
1826 BUG_ON(alias_idx >= ARRAY_SIZE(gpio_chips));
1827 if (gpio_chips[alias_idx]) {
1828 ret = -EBUSY;
1829 goto err;
1830 }
1831
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001832 irq = platform_get_irq(pdev, 0);
1833 if (irq < 0) {
1834 ret = irq;
1835 goto err;
1836 }
1837
1838 at91_chip = devm_kzalloc(&pdev->dev, sizeof(*at91_chip), GFP_KERNEL);
1839 if (!at91_chip) {
1840 ret = -ENOMEM;
1841 goto err;
1842 }
1843
Wolfram Sangf50b9e12013-05-10 10:17:03 +02001844 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding9e0c1fb2013-01-21 11:09:14 +01001845 at91_chip->regbase = devm_ioremap_resource(&pdev->dev, res);
1846 if (IS_ERR(at91_chip->regbase)) {
1847 ret = PTR_ERR(at91_chip->regbase);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001848 goto err;
1849 }
1850
Sachin Kamat3c936002013-03-15 10:07:03 +05301851 at91_chip->ops = (struct at91_pinctrl_mux_ops *)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001852 of_match_device(at91_gpio_of_match, &pdev->dev)->data;
1853 at91_chip->pioc_virq = irq;
1854 at91_chip->pioc_idx = alias_idx;
1855
Pramod Gurav02b837f2014-08-31 16:51:52 +05301856 at91_chip->clock = devm_clk_get(&pdev->dev, NULL);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001857 if (IS_ERR(at91_chip->clock)) {
1858 dev_err(&pdev->dev, "failed to get clock, ignoring.\n");
Pramod Gurav70e41972014-09-09 15:50:36 +05301859 ret = PTR_ERR(at91_chip->clock);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001860 goto err;
1861 }
1862
Alexander Stein7d3a3fe2016-04-29 14:50:03 +02001863 ret = clk_prepare_enable(at91_chip->clock);
Pramod Gurav70e41972014-09-09 15:50:36 +05301864 if (ret) {
Alexander Stein7d3a3fe2016-04-29 14:50:03 +02001865 dev_err(&pdev->dev, "failed to prepare and enable clock, ignoring.\n");
Pramod Gurav70e41972014-09-09 15:50:36 +05301866 goto clk_enable_err;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001867 }
1868
1869 at91_chip->chip = at91_gpio_template;
1870
1871 chip = &at91_chip->chip;
1872 chip->of_node = np;
1873 chip->label = dev_name(&pdev->dev);
Linus Walleij58383c782015-11-04 09:56:26 +01001874 chip->parent = &pdev->dev;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001875 chip->owner = THIS_MODULE;
1876 chip->base = alias_idx * MAX_NB_GPIO_PER_BANK;
1877
1878 if (!of_property_read_u32(np, "#gpio-lines", &ngpio)) {
1879 if (ngpio >= MAX_NB_GPIO_PER_BANK)
1880 pr_err("at91_gpio.%d, gpio-nb >= %d failback to %d\n",
1881 alias_idx, MAX_NB_GPIO_PER_BANK, MAX_NB_GPIO_PER_BANK);
1882 else
1883 chip->ngpio = ngpio;
1884 }
1885
Kees Cooka86854d2018-06-12 14:07:58 -07001886 names = devm_kcalloc(&pdev->dev, chip->ngpio, sizeof(char *),
Sachin Kamat3c936002013-03-15 10:07:03 +05301887 GFP_KERNEL);
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001888
1889 if (!names) {
1890 ret = -ENOMEM;
Pramod Gurav70e41972014-09-09 15:50:36 +05301891 goto clk_enable_err;
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001892 }
1893
1894 for (i = 0; i < chip->ngpio; i++)
1895 names[i] = kasprintf(GFP_KERNEL, "pio%c%d", alias_idx + 'A', i);
1896
Sachin Kamat3c936002013-03-15 10:07:03 +05301897 chip->names = (const char *const *)names;
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001898
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001899 range = &at91_chip->range;
1900 range->name = chip->label;
1901 range->id = alias_idx;
1902 range->pin_base = range->base = range->id * MAX_NB_GPIO_PER_BANK;
1903
1904 range->npins = chip->ngpio;
1905 range->gc = chip;
1906
Linus Walleij370ea612015-12-08 09:27:45 +01001907 ret = gpiochip_add_data(chip, at91_chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001908 if (ret)
Pramod Gurav70e41972014-09-09 15:50:36 +05301909 goto gpiochip_add_err;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001910
1911 gpio_chips[alias_idx] = at91_chip;
1912 gpio_banks = max(gpio_banks, alias_idx + 1);
1913
Pramod Gurav834e1672014-09-09 15:50:37 +05301914 ret = at91_gpio_of_irq_setup(pdev, at91_chip);
1915 if (ret)
1916 goto irq_setup_err;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001917
1918 dev_info(&pdev->dev, "at address %p\n", at91_chip->regbase);
1919
1920 return 0;
1921
Pramod Gurav834e1672014-09-09 15:50:37 +05301922irq_setup_err:
1923 gpiochip_remove(chip);
Pramod Gurav70e41972014-09-09 15:50:36 +05301924gpiochip_add_err:
Pramod Gurav70e41972014-09-09 15:50:36 +05301925clk_enable_err:
Alexander Stein7d3a3fe2016-04-29 14:50:03 +02001926 clk_disable_unprepare(at91_chip->clock);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001927err:
1928 dev_err(&pdev->dev, "Failure %i for GPIO %i\n", ret, alias_idx);
1929
1930 return ret;
1931}
1932
1933static struct platform_driver at91_gpio_driver = {
1934 .driver = {
1935 .name = "gpio-at91",
Sachin Kamat606fca92013-09-28 17:38:48 +05301936 .of_match_table = at91_gpio_of_match,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001937 },
1938 .probe = at91_gpio_probe,
1939};
1940
1941static struct platform_driver at91_pinctrl_driver = {
1942 .driver = {
1943 .name = "pinctrl-at91",
Sachin Kamat606fca92013-09-28 17:38:48 +05301944 .of_match_table = at91_pinctrl_of_match,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001945 },
1946 .probe = at91_pinctrl_probe,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001947};
1948
Thierry Redingbab7f5a2015-12-02 17:31:55 +01001949static struct platform_driver * const drivers[] = {
1950 &at91_gpio_driver,
1951 &at91_pinctrl_driver,
1952};
1953
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001954static int __init at91_pinctrl_init(void)
1955{
Thierry Redingbab7f5a2015-12-02 17:31:55 +01001956 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001957}
1958arch_initcall(at91_pinctrl_init);