blob: c135149e84e9954a74946276d484664954631d39 [file] [log] [blame]
Thomas Gleixner28b665f2019-06-01 10:08:48 +02001// SPDX-License-Identifier: GPL-2.0-only
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08002/*
3 * at91 pinctrl driver based on at91 pinmux core
4 *
5 * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08006 */
7
8#include <linux/clk.h>
9#include <linux/err.h>
10#include <linux/init.h>
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080011#include <linux/of.h>
12#include <linux/of_device.h>
13#include <linux/of_address.h>
14#include <linux/of_irq.h>
15#include <linux/slab.h>
16#include <linux/interrupt.h>
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080017#include <linux/io.h>
Linus Walleij1c5fb662018-09-13 13:58:21 +020018#include <linux/gpio/driver.h>
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080019#include <linux/pinctrl/machine.h>
20#include <linux/pinctrl/pinconf.h>
21#include <linux/pinctrl/pinctrl.h>
22#include <linux/pinctrl/pinmux.h>
23/* Since we request GPIOs from ourself */
24#include <linux/pinctrl/consumer.h>
25
Alexandre Bellonic654b6b2014-10-17 11:49:40 +020026#include "pinctrl-at91.h"
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080027#include "core.h"
28
Linus Walleij94daf852013-11-05 10:30:14 +010029#define MAX_GPIO_BANKS 5
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080030#define MAX_NB_GPIO_PER_BANK 32
31
32struct at91_pinctrl_mux_ops;
33
34struct at91_gpio_chip {
35 struct gpio_chip chip;
36 struct pinctrl_gpio_range range;
37 struct at91_gpio_chip *next; /* Bank sharing same clock */
38 int pioc_hwirq; /* PIO bank interrupt identifier on AIC */
39 int pioc_virq; /* PIO bank Linux virtual interrupt */
40 int pioc_idx; /* PIO bank index */
41 void __iomem *regbase; /* PIO bank virtual address */
42 struct clk *clock; /* associated clock */
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080043 struct at91_pinctrl_mux_ops *ops; /* ops */
44};
45
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080046static struct at91_gpio_chip *gpio_chips[MAX_GPIO_BANKS];
47
48static int gpio_banks;
49
Jean-Christophe PLAGNIOL-VILLARD525fae22012-10-23 18:28:00 +020050#define PULL_UP (1 << 0)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080051#define MULTI_DRIVE (1 << 1)
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +080052#define DEGLITCH (1 << 2)
53#define PULL_DOWN (1 << 3)
54#define DIS_SCHMIT (1 << 4)
Marek Roszko4334ac22014-08-23 23:12:04 -040055#define DRIVE_STRENGTH_SHIFT 5
56#define DRIVE_STRENGTH_MASK 0x3
57#define DRIVE_STRENGTH (DRIVE_STRENGTH_MASK << DRIVE_STRENGTH_SHIFT)
Boris BREZILLON96bb12d2016-10-28 15:54:10 +080058#define OUTPUT (1 << 7)
59#define OUTPUT_VAL_SHIFT 8
60#define OUTPUT_VAL (0x1 << OUTPUT_VAL_SHIFT)
Claudiu Beznea64e21ad2019-02-07 09:25:05 +000061#define SLEWRATE_SHIFT 9
62#define SLEWRATE_MASK 0x1
63#define SLEWRATE (SLEWRATE_MASK << SLEWRATE_SHIFT)
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +080064#define DEBOUNCE (1 << 16)
65#define DEBOUNCE_VAL_SHIFT 17
66#define DEBOUNCE_VAL (0x3fff << DEBOUNCE_VAL_SHIFT)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080067
68/**
Marek Roszko4334ac22014-08-23 23:12:04 -040069 * These defines will translated the dt binding settings to our internal
70 * settings. They are not necessarily the same value as the register setting.
71 * The actual drive strength current of low, medium and high must be looked up
72 * from the corresponding device datasheet. This value is different for pins
73 * that are even in the same banks. It is also dependent on VCC.
74 * DRIVE_STRENGTH_DEFAULT is just a placeholder to avoid changing the drive
75 * strength when there is no dt config for it.
76 */
Claudiu Bezneab67328e2019-02-07 09:24:46 +000077enum drive_strength_bit {
78 DRIVE_STRENGTH_BIT_DEF,
79 DRIVE_STRENGTH_BIT_LOW,
80 DRIVE_STRENGTH_BIT_MED,
81 DRIVE_STRENGTH_BIT_HI,
82};
83
84#define DRIVE_STRENGTH_BIT_MSK(name) (DRIVE_STRENGTH_BIT_##name << \
85 DRIVE_STRENGTH_SHIFT)
Marek Roszko4334ac22014-08-23 23:12:04 -040086
Claudiu Beznea64e21ad2019-02-07 09:25:05 +000087enum slewrate_bit {
Claudiu Beznea64e21ad2019-02-07 09:25:05 +000088 SLEWRATE_BIT_ENA,
Codrin Ciubotariu0b329282019-11-01 11:20:31 +020089 SLEWRATE_BIT_DIS,
Claudiu Beznea64e21ad2019-02-07 09:25:05 +000090};
91
92#define SLEWRATE_BIT_MSK(name) (SLEWRATE_BIT_##name << SLEWRATE_SHIFT)
93
Marek Roszko4334ac22014-08-23 23:12:04 -040094/**
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080095 * struct at91_pmx_func - describes AT91 pinmux functions
96 * @name: the name of this specific function
97 * @groups: corresponding pin groups
98 * @ngroups: the number of groups
99 */
100struct at91_pmx_func {
101 const char *name;
102 const char **groups;
103 unsigned ngroups;
104};
105
106enum at91_mux {
107 AT91_MUX_GPIO = 0,
108 AT91_MUX_PERIPH_A = 1,
109 AT91_MUX_PERIPH_B = 2,
110 AT91_MUX_PERIPH_C = 3,
111 AT91_MUX_PERIPH_D = 4,
112};
113
114/**
115 * struct at91_pmx_pin - describes an At91 pin mux
116 * @bank: the bank of the pin
117 * @pin: the pin number in the @bank
118 * @mux: the mux mode : gpio or periph_x of the pin i.e. alternate function.
119 * @conf: the configuration of the pin: PULL_UP, MULTIDRIVE etc...
120 */
121struct at91_pmx_pin {
122 uint32_t bank;
123 uint32_t pin;
124 enum at91_mux mux;
125 unsigned long conf;
126};
127
128/**
129 * struct at91_pin_group - describes an At91 pin group
130 * @name: the name of this specific pin group
131 * @pins_conf: the mux mode for each pin in this group. The size of this
132 * array is the same as pins.
133 * @pins: an array of discrete physical pins used in this group, taken
134 * from the driver-local pin enumeration space
135 * @npins: the number of pins in this group array, i.e. the number of
136 * elements in .pins so we can iterate over that array
137 */
138struct at91_pin_group {
139 const char *name;
140 struct at91_pmx_pin *pins_conf;
141 unsigned int *pins;
142 unsigned npins;
143};
144
145/**
Alexandre Bellonic2eb9e72013-12-07 14:08:52 +0100146 * struct at91_pinctrl_mux_ops - describes an AT91 mux ops group
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800147 * on new IP with support for periph C and D the way to mux in
148 * periph A and B has changed
149 * So provide the right call back
150 * if not present means the IP does not support it
151 * @get_periph: return the periph mode configured
152 * @mux_A_periph: mux as periph A
153 * @mux_B_periph: mux as periph B
154 * @mux_C_periph: mux as periph C
155 * @mux_D_periph: mux as periph D
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800156 * @get_deglitch: get deglitch status
157 * @set_deglitch: enable/disable deglitch
158 * @get_debounce: get debounce status
159 * @set_debounce: enable/disable debounce
160 * @get_pulldown: get pulldown status
161 * @set_pulldown: enable/disable pulldown
162 * @get_schmitt_trig: get schmitt trigger status
163 * @disable_schmitt_trig: disable schmitt trigger
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800164 * @irq_type: return irq type
165 */
166struct at91_pinctrl_mux_ops {
167 enum at91_mux (*get_periph)(void __iomem *pio, unsigned mask);
168 void (*mux_A_periph)(void __iomem *pio, unsigned mask);
169 void (*mux_B_periph)(void __iomem *pio, unsigned mask);
170 void (*mux_C_periph)(void __iomem *pio, unsigned mask);
171 void (*mux_D_periph)(void __iomem *pio, unsigned mask);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800172 bool (*get_deglitch)(void __iomem *pio, unsigned pin);
Boris BREZILLON77966ad2013-09-13 09:45:33 +0200173 void (*set_deglitch)(void __iomem *pio, unsigned mask, bool is_on);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800174 bool (*get_debounce)(void __iomem *pio, unsigned pin, u32 *div);
Boris BREZILLON77966ad2013-09-13 09:45:33 +0200175 void (*set_debounce)(void __iomem *pio, unsigned mask, bool is_on, u32 div);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800176 bool (*get_pulldown)(void __iomem *pio, unsigned pin);
Boris BREZILLON77966ad2013-09-13 09:45:33 +0200177 void (*set_pulldown)(void __iomem *pio, unsigned mask, bool is_on);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800178 bool (*get_schmitt_trig)(void __iomem *pio, unsigned pin);
179 void (*disable_schmitt_trig)(void __iomem *pio, unsigned mask);
Marek Roszko4334ac22014-08-23 23:12:04 -0400180 unsigned (*get_drivestrength)(void __iomem *pio, unsigned pin);
181 void (*set_drivestrength)(void __iomem *pio, unsigned pin,
182 u32 strength);
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000183 unsigned (*get_slewrate)(void __iomem *pio, unsigned pin);
184 void (*set_slewrate)(void __iomem *pio, unsigned pin, u32 slewrate);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800185 /* irq */
186 int (*irq_type)(struct irq_data *d, unsigned type);
187};
188
189static int gpio_irq_type(struct irq_data *d, unsigned type);
190static int alt_gpio_irq_type(struct irq_data *d, unsigned type);
191
192struct at91_pinctrl {
193 struct device *dev;
194 struct pinctrl_dev *pctl;
195
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +0100196 int nactive_banks;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800197
198 uint32_t *mux_mask;
199 int nmux;
200
201 struct at91_pmx_func *functions;
202 int nfunctions;
203
204 struct at91_pin_group *groups;
205 int ngroups;
206
207 struct at91_pinctrl_mux_ops *ops;
208};
209
Arnd Bergmann56411f32016-06-13 17:18:34 +0200210static inline const struct at91_pin_group *at91_pinctrl_find_group_by_name(
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800211 const struct at91_pinctrl *info,
212 const char *name)
213{
214 const struct at91_pin_group *grp = NULL;
215 int i;
216
217 for (i = 0; i < info->ngroups; i++) {
218 if (strcmp(info->groups[i].name, name))
219 continue;
220
221 grp = &info->groups[i];
222 dev_dbg(info->dev, "%s: %d 0:%d\n", name, grp->npins, grp->pins[0]);
223 break;
224 }
225
226 return grp;
227}
228
229static int at91_get_groups_count(struct pinctrl_dev *pctldev)
230{
231 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
232
233 return info->ngroups;
234}
235
236static const char *at91_get_group_name(struct pinctrl_dev *pctldev,
237 unsigned selector)
238{
239 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
240
241 return info->groups[selector].name;
242}
243
244static int at91_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
245 const unsigned **pins,
246 unsigned *npins)
247{
248 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
249
250 if (selector >= info->ngroups)
251 return -EINVAL;
252
253 *pins = info->groups[selector].pins;
254 *npins = info->groups[selector].npins;
255
256 return 0;
257}
258
259static void at91_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
260 unsigned offset)
261{
262 seq_printf(s, "%s", dev_name(pctldev->dev));
263}
264
265static int at91_dt_node_to_map(struct pinctrl_dev *pctldev,
266 struct device_node *np,
267 struct pinctrl_map **map, unsigned *num_maps)
268{
269 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
270 const struct at91_pin_group *grp;
271 struct pinctrl_map *new_map;
272 struct device_node *parent;
273 int map_num = 1;
274 int i;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800275
276 /*
Alexandre Belloni61e310a2013-10-16 16:12:33 +0200277 * first find the group of this node and check if we need to create
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800278 * config maps for pins
279 */
280 grp = at91_pinctrl_find_group_by_name(info, np->name);
281 if (!grp) {
Rob Herring94f4e542018-08-27 20:52:41 -0500282 dev_err(info->dev, "unable to find group for node %pOFn\n",
283 np);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800284 return -EINVAL;
285 }
286
287 map_num += grp->npins;
Kees Cooka86854d2018-06-12 14:07:58 -0700288 new_map = devm_kcalloc(pctldev->dev, map_num, sizeof(*new_map),
289 GFP_KERNEL);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800290 if (!new_map)
291 return -ENOMEM;
292
293 *map = new_map;
294 *num_maps = map_num;
295
296 /* create mux map */
297 parent = of_get_parent(np);
298 if (!parent) {
Julia Lawallc62b2b32012-12-12 15:22:44 +0100299 devm_kfree(pctldev->dev, new_map);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800300 return -EINVAL;
301 }
302 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
303 new_map[0].data.mux.function = parent->name;
304 new_map[0].data.mux.group = np->name;
305 of_node_put(parent);
306
307 /* create config map */
308 new_map++;
309 for (i = 0; i < grp->npins; i++) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800310 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
311 new_map[i].data.configs.group_or_pin =
312 pin_get_name(pctldev, grp->pins[i]);
313 new_map[i].data.configs.configs = &grp->pins_conf[i].conf;
314 new_map[i].data.configs.num_configs = 1;
315 }
316
317 dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
318 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
319
320 return 0;
321}
322
323static void at91_dt_free_map(struct pinctrl_dev *pctldev,
324 struct pinctrl_map *map, unsigned num_maps)
325{
326}
327
Laurent Pinchart022ab142013-02-16 10:25:07 +0100328static const struct pinctrl_ops at91_pctrl_ops = {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800329 .get_groups_count = at91_get_groups_count,
330 .get_group_name = at91_get_group_name,
331 .get_group_pins = at91_get_group_pins,
332 .pin_dbg_show = at91_pin_dbg_show,
333 .dt_node_to_map = at91_dt_node_to_map,
334 .dt_free_map = at91_dt_free_map,
335};
336
Sachin Kamat3c936002013-03-15 10:07:03 +0530337static void __iomem *pin_to_controller(struct at91_pinctrl *info,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800338 unsigned int bank)
339{
David Dueck1ab36382015-07-28 09:48:16 +0200340 if (!gpio_chips[bank])
341 return NULL;
342
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800343 return gpio_chips[bank]->regbase;
344}
345
346static inline int pin_to_bank(unsigned pin)
347{
348 return pin /= MAX_NB_GPIO_PER_BANK;
349}
350
351static unsigned pin_to_mask(unsigned int pin)
352{
353 return 1 << pin;
354}
355
Marek Roszko4334ac22014-08-23 23:12:04 -0400356static unsigned two_bit_pin_value_shift_amount(unsigned int pin)
357{
358 /* return the shift value for a pin for "two bit" per pin registers,
359 * i.e. drive strength */
360 return 2*((pin >= MAX_NB_GPIO_PER_BANK/2)
361 ? pin - MAX_NB_GPIO_PER_BANK/2 : pin);
362}
363
364static unsigned sama5d3_get_drive_register(unsigned int pin)
365{
366 /* drive strength is split between two registers
367 * with two bits per pin */
368 return (pin >= MAX_NB_GPIO_PER_BANK/2)
369 ? SAMA5D3_PIO_DRIVER2 : SAMA5D3_PIO_DRIVER1;
370}
371
372static unsigned at91sam9x5_get_drive_register(unsigned int pin)
373{
374 /* drive strength is split between two registers
375 * with two bits per pin */
376 return (pin >= MAX_NB_GPIO_PER_BANK/2)
377 ? AT91SAM9X5_PIO_DRIVER2 : AT91SAM9X5_PIO_DRIVER1;
378}
379
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800380static void at91_mux_disable_interrupt(void __iomem *pio, unsigned mask)
381{
382 writel_relaxed(mask, pio + PIO_IDR);
383}
384
385static unsigned at91_mux_get_pullup(void __iomem *pio, unsigned pin)
386{
Boris BREZILLON05d35342013-08-27 15:19:21 +0200387 return !((readl_relaxed(pio + PIO_PUSR) >> pin) & 0x1);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800388}
389
390static void at91_mux_set_pullup(void __iomem *pio, unsigned mask, bool on)
391{
Wenyou Yang3d784272014-09-11 16:40:15 +0200392 if (on)
393 writel_relaxed(mask, pio + PIO_PPDDR);
394
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800395 writel_relaxed(mask, pio + (on ? PIO_PUER : PIO_PUDR));
396}
397
Boris BREZILLON96bb12d2016-10-28 15:54:10 +0800398static bool at91_mux_get_output(void __iomem *pio, unsigned int pin, bool *val)
399{
400 *val = (readl_relaxed(pio + PIO_ODSR) >> pin) & 0x1;
401 return (readl_relaxed(pio + PIO_OSR) >> pin) & 0x1;
402}
403
404static void at91_mux_set_output(void __iomem *pio, unsigned int mask,
405 bool is_on, bool val)
406{
407 writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
408 writel_relaxed(mask, pio + (is_on ? PIO_OER : PIO_ODR));
409}
410
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800411static unsigned at91_mux_get_multidrive(void __iomem *pio, unsigned pin)
412{
413 return (readl_relaxed(pio + PIO_MDSR) >> pin) & 0x1;
414}
415
416static void at91_mux_set_multidrive(void __iomem *pio, unsigned mask, bool on)
417{
418 writel_relaxed(mask, pio + (on ? PIO_MDER : PIO_MDDR));
419}
420
421static void at91_mux_set_A_periph(void __iomem *pio, unsigned mask)
422{
423 writel_relaxed(mask, pio + PIO_ASR);
424}
425
426static void at91_mux_set_B_periph(void __iomem *pio, unsigned mask)
427{
428 writel_relaxed(mask, pio + PIO_BSR);
429}
430
431static void at91_mux_pio3_set_A_periph(void __iomem *pio, unsigned mask)
432{
433
434 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask,
435 pio + PIO_ABCDSR1);
436 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
437 pio + PIO_ABCDSR2);
438}
439
440static void at91_mux_pio3_set_B_periph(void __iomem *pio, unsigned mask)
441{
442 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask,
443 pio + PIO_ABCDSR1);
444 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
445 pio + PIO_ABCDSR2);
446}
447
448static void at91_mux_pio3_set_C_periph(void __iomem *pio, unsigned mask)
449{
450 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask, pio + PIO_ABCDSR1);
451 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
452}
453
454static void at91_mux_pio3_set_D_periph(void __iomem *pio, unsigned mask)
455{
456 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask, pio + PIO_ABCDSR1);
457 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
458}
459
460static enum at91_mux at91_mux_pio3_get_periph(void __iomem *pio, unsigned mask)
461{
462 unsigned select;
463
464 if (readl_relaxed(pio + PIO_PSR) & mask)
465 return AT91_MUX_GPIO;
466
467 select = !!(readl_relaxed(pio + PIO_ABCDSR1) & mask);
468 select |= (!!(readl_relaxed(pio + PIO_ABCDSR2) & mask) << 1);
469
470 return select + 1;
471}
472
473static enum at91_mux at91_mux_get_periph(void __iomem *pio, unsigned mask)
474{
475 unsigned select;
476
477 if (readl_relaxed(pio + PIO_PSR) & mask)
478 return AT91_MUX_GPIO;
479
480 select = readl_relaxed(pio + PIO_ABSR) & mask;
481
482 return select + 1;
483}
484
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800485static bool at91_mux_get_deglitch(void __iomem *pio, unsigned pin)
486{
Ben Dooksd480239b2015-03-26 12:18:49 +0000487 return (readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1;
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800488}
489
490static void at91_mux_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
491{
Ben Dooksd480239b2015-03-26 12:18:49 +0000492 writel_relaxed(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800493}
494
Boris BREZILLONc8dba022013-09-13 09:47:22 +0200495static bool at91_mux_pio3_get_deglitch(void __iomem *pio, unsigned pin)
496{
Ben Dooksd480239b2015-03-26 12:18:49 +0000497 if ((readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1)
498 return !((readl_relaxed(pio + PIO_IFSCSR) >> pin) & 0x1);
Boris BREZILLONc8dba022013-09-13 09:47:22 +0200499
500 return false;
501}
502
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800503static void at91_mux_pio3_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
504{
505 if (is_on)
Ben Dooksd480239b2015-03-26 12:18:49 +0000506 writel_relaxed(mask, pio + PIO_IFSCDR);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800507 at91_mux_set_deglitch(pio, mask, is_on);
508}
509
510static bool at91_mux_pio3_get_debounce(void __iomem *pio, unsigned pin, u32 *div)
511{
Ben Dooksd480239b2015-03-26 12:18:49 +0000512 *div = readl_relaxed(pio + PIO_SCDR);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800513
Ben Dooksd480239b2015-03-26 12:18:49 +0000514 return ((readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1) &&
515 ((readl_relaxed(pio + PIO_IFSCSR) >> pin) & 0x1);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800516}
517
518static void at91_mux_pio3_set_debounce(void __iomem *pio, unsigned mask,
519 bool is_on, u32 div)
520{
521 if (is_on) {
Ben Dooksd480239b2015-03-26 12:18:49 +0000522 writel_relaxed(mask, pio + PIO_IFSCER);
523 writel_relaxed(div & PIO_SCDR_DIV, pio + PIO_SCDR);
524 writel_relaxed(mask, pio + PIO_IFER);
Boris BREZILLONc8dba022013-09-13 09:47:22 +0200525 } else
Ben Dooksd480239b2015-03-26 12:18:49 +0000526 writel_relaxed(mask, pio + PIO_IFSCDR);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800527}
528
529static bool at91_mux_pio3_get_pulldown(void __iomem *pio, unsigned pin)
530{
Ben Dooksd480239b2015-03-26 12:18:49 +0000531 return !((readl_relaxed(pio + PIO_PPDSR) >> pin) & 0x1);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800532}
533
534static void at91_mux_pio3_set_pulldown(void __iomem *pio, unsigned mask, bool is_on)
535{
Wenyou Yang3d784272014-09-11 16:40:15 +0200536 if (is_on)
Ben Dooksd480239b2015-03-26 12:18:49 +0000537 writel_relaxed(mask, pio + PIO_PUDR);
Wenyou Yang3d784272014-09-11 16:40:15 +0200538
Ben Dooksd480239b2015-03-26 12:18:49 +0000539 writel_relaxed(mask, pio + (is_on ? PIO_PPDER : PIO_PPDDR));
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800540}
541
542static void at91_mux_pio3_disable_schmitt_trig(void __iomem *pio, unsigned mask)
543{
Ben Dooksd480239b2015-03-26 12:18:49 +0000544 writel_relaxed(readl_relaxed(pio + PIO_SCHMITT) | mask, pio + PIO_SCHMITT);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800545}
546
547static bool at91_mux_pio3_get_schmitt_trig(void __iomem *pio, unsigned pin)
548{
Ben Dooksd480239b2015-03-26 12:18:49 +0000549 return (readl_relaxed(pio + PIO_SCHMITT) >> pin) & 0x1;
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800550}
551
Marek Roszko4334ac22014-08-23 23:12:04 -0400552static inline u32 read_drive_strength(void __iomem *reg, unsigned pin)
553{
Ben Dooksd480239b2015-03-26 12:18:49 +0000554 unsigned tmp = readl_relaxed(reg);
Marek Roszko4334ac22014-08-23 23:12:04 -0400555
556 tmp = tmp >> two_bit_pin_value_shift_amount(pin);
557
558 return tmp & DRIVE_STRENGTH_MASK;
559}
560
561static unsigned at91_mux_sama5d3_get_drivestrength(void __iomem *pio,
562 unsigned pin)
563{
564 unsigned tmp = read_drive_strength(pio +
565 sama5d3_get_drive_register(pin), pin);
566
567 /* SAMA5 strength is 1:1 with our defines,
568 * except 0 is equivalent to low per datasheet */
569 if (!tmp)
Claudiu Bezneab67328e2019-02-07 09:24:46 +0000570 tmp = DRIVE_STRENGTH_BIT_MSK(LOW);
Marek Roszko4334ac22014-08-23 23:12:04 -0400571
572 return tmp;
573}
574
575static unsigned at91_mux_sam9x5_get_drivestrength(void __iomem *pio,
576 unsigned pin)
577{
578 unsigned tmp = read_drive_strength(pio +
579 at91sam9x5_get_drive_register(pin), pin);
580
581 /* strength is inverse in SAM9x5s hardware with the pinctrl defines
582 * hardware: 0 = hi, 1 = med, 2 = low, 3 = rsvd */
Claudiu Bezneab67328e2019-02-07 09:24:46 +0000583 tmp = DRIVE_STRENGTH_BIT_MSK(HI) - tmp;
Marek Roszko4334ac22014-08-23 23:12:04 -0400584
585 return tmp;
586}
587
Claudiu Beznea42ef7552019-02-07 09:24:49 +0000588static unsigned at91_mux_sam9x60_get_drivestrength(void __iomem *pio,
589 unsigned pin)
590{
591 unsigned tmp = readl_relaxed(pio + SAM9X60_PIO_DRIVER1);
592
593 if (tmp & BIT(pin))
594 return DRIVE_STRENGTH_BIT_HI;
595
596 return DRIVE_STRENGTH_BIT_LOW;
597}
598
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000599static unsigned at91_mux_sam9x60_get_slewrate(void __iomem *pio, unsigned pin)
600{
601 unsigned tmp = readl_relaxed(pio + SAM9X60_PIO_SLEWR);
602
603 if ((tmp & BIT(pin)))
604 return SLEWRATE_BIT_ENA;
605
606 return SLEWRATE_BIT_DIS;
607}
608
Marek Roszko4334ac22014-08-23 23:12:04 -0400609static void set_drive_strength(void __iomem *reg, unsigned pin, u32 strength)
610{
Ben Dooksd480239b2015-03-26 12:18:49 +0000611 unsigned tmp = readl_relaxed(reg);
Marek Roszko4334ac22014-08-23 23:12:04 -0400612 unsigned shift = two_bit_pin_value_shift_amount(pin);
613
614 tmp &= ~(DRIVE_STRENGTH_MASK << shift);
615 tmp |= strength << shift;
616
Ben Dooksd480239b2015-03-26 12:18:49 +0000617 writel_relaxed(tmp, reg);
Marek Roszko4334ac22014-08-23 23:12:04 -0400618}
619
620static void at91_mux_sama5d3_set_drivestrength(void __iomem *pio, unsigned pin,
621 u32 setting)
622{
623 /* do nothing if setting is zero */
624 if (!setting)
625 return;
626
627 /* strength is 1 to 1 with setting for SAMA5 */
628 set_drive_strength(pio + sama5d3_get_drive_register(pin), pin, setting);
629}
630
631static void at91_mux_sam9x5_set_drivestrength(void __iomem *pio, unsigned pin,
632 u32 setting)
633{
634 /* do nothing if setting is zero */
635 if (!setting)
636 return;
637
638 /* strength is inverse on SAM9x5s with our defines
639 * 0 = hi, 1 = med, 2 = low, 3 = rsvd */
Claudiu Bezneab67328e2019-02-07 09:24:46 +0000640 setting = DRIVE_STRENGTH_BIT_MSK(HI) - setting;
Marek Roszko4334ac22014-08-23 23:12:04 -0400641
642 set_drive_strength(pio + at91sam9x5_get_drive_register(pin), pin,
643 setting);
644}
645
Claudiu Beznea42ef7552019-02-07 09:24:49 +0000646static void at91_mux_sam9x60_set_drivestrength(void __iomem *pio, unsigned pin,
647 u32 setting)
648{
649 unsigned int tmp;
650
651 if (setting <= DRIVE_STRENGTH_BIT_DEF ||
652 setting == DRIVE_STRENGTH_BIT_MED ||
653 setting > DRIVE_STRENGTH_BIT_HI)
654 return;
655
656 tmp = readl_relaxed(pio + SAM9X60_PIO_DRIVER1);
657
658 /* Strength is 0: low, 1: hi */
659 if (setting == DRIVE_STRENGTH_BIT_LOW)
660 tmp &= ~BIT(pin);
661 else
662 tmp |= BIT(pin);
663
664 writel_relaxed(tmp, pio + SAM9X60_PIO_DRIVER1);
665}
666
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000667static void at91_mux_sam9x60_set_slewrate(void __iomem *pio, unsigned pin,
668 u32 setting)
669{
670 unsigned int tmp;
671
Codrin Ciubotariu0b329282019-11-01 11:20:31 +0200672 if (setting < SLEWRATE_BIT_ENA || setting > SLEWRATE_BIT_DIS)
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000673 return;
674
675 tmp = readl_relaxed(pio + SAM9X60_PIO_SLEWR);
676
677 if (setting == SLEWRATE_BIT_DIS)
678 tmp &= ~BIT(pin);
679 else
680 tmp |= BIT(pin);
681
682 writel_relaxed(tmp, pio + SAM9X60_PIO_SLEWR);
683}
684
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800685static struct at91_pinctrl_mux_ops at91rm9200_ops = {
686 .get_periph = at91_mux_get_periph,
687 .mux_A_periph = at91_mux_set_A_periph,
688 .mux_B_periph = at91_mux_set_B_periph,
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800689 .get_deglitch = at91_mux_get_deglitch,
690 .set_deglitch = at91_mux_set_deglitch,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800691 .irq_type = gpio_irq_type,
692};
693
694static struct at91_pinctrl_mux_ops at91sam9x5_ops = {
695 .get_periph = at91_mux_pio3_get_periph,
696 .mux_A_periph = at91_mux_pio3_set_A_periph,
697 .mux_B_periph = at91_mux_pio3_set_B_periph,
698 .mux_C_periph = at91_mux_pio3_set_C_periph,
699 .mux_D_periph = at91_mux_pio3_set_D_periph,
Boris BREZILLONc8dba022013-09-13 09:47:22 +0200700 .get_deglitch = at91_mux_pio3_get_deglitch,
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800701 .set_deglitch = at91_mux_pio3_set_deglitch,
702 .get_debounce = at91_mux_pio3_get_debounce,
703 .set_debounce = at91_mux_pio3_set_debounce,
704 .get_pulldown = at91_mux_pio3_get_pulldown,
705 .set_pulldown = at91_mux_pio3_set_pulldown,
706 .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
707 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
Marek Roszko4334ac22014-08-23 23:12:04 -0400708 .get_drivestrength = at91_mux_sam9x5_get_drivestrength,
709 .set_drivestrength = at91_mux_sam9x5_set_drivestrength,
710 .irq_type = alt_gpio_irq_type,
711};
712
Claudiu Beznea42ef7552019-02-07 09:24:49 +0000713static const struct at91_pinctrl_mux_ops sam9x60_ops = {
714 .get_periph = at91_mux_pio3_get_periph,
715 .mux_A_periph = at91_mux_pio3_set_A_periph,
716 .mux_B_periph = at91_mux_pio3_set_B_periph,
717 .mux_C_periph = at91_mux_pio3_set_C_periph,
718 .mux_D_periph = at91_mux_pio3_set_D_periph,
719 .get_deglitch = at91_mux_pio3_get_deglitch,
720 .set_deglitch = at91_mux_pio3_set_deglitch,
721 .get_debounce = at91_mux_pio3_get_debounce,
722 .set_debounce = at91_mux_pio3_set_debounce,
723 .get_pulldown = at91_mux_pio3_get_pulldown,
724 .set_pulldown = at91_mux_pio3_set_pulldown,
725 .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
726 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
727 .get_drivestrength = at91_mux_sam9x60_get_drivestrength,
728 .set_drivestrength = at91_mux_sam9x60_set_drivestrength,
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000729 .get_slewrate = at91_mux_sam9x60_get_slewrate,
730 .set_slewrate = at91_mux_sam9x60_set_slewrate,
Claudiu Beznea42ef7552019-02-07 09:24:49 +0000731 .irq_type = alt_gpio_irq_type,
732
733};
734
Marek Roszko4334ac22014-08-23 23:12:04 -0400735static struct at91_pinctrl_mux_ops sama5d3_ops = {
736 .get_periph = at91_mux_pio3_get_periph,
737 .mux_A_periph = at91_mux_pio3_set_A_periph,
738 .mux_B_periph = at91_mux_pio3_set_B_periph,
739 .mux_C_periph = at91_mux_pio3_set_C_periph,
740 .mux_D_periph = at91_mux_pio3_set_D_periph,
741 .get_deglitch = at91_mux_pio3_get_deglitch,
742 .set_deglitch = at91_mux_pio3_set_deglitch,
743 .get_debounce = at91_mux_pio3_get_debounce,
744 .set_debounce = at91_mux_pio3_set_debounce,
745 .get_pulldown = at91_mux_pio3_get_pulldown,
746 .set_pulldown = at91_mux_pio3_set_pulldown,
747 .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
748 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
749 .get_drivestrength = at91_mux_sama5d3_get_drivestrength,
750 .set_drivestrength = at91_mux_sama5d3_set_drivestrength,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800751 .irq_type = alt_gpio_irq_type,
752};
753
754static void at91_pin_dbg(const struct device *dev, const struct at91_pmx_pin *pin)
755{
756 if (pin->mux) {
Hans Wennborg4b6fe452014-08-05 21:43:16 -0700757 dev_dbg(dev, "pio%c%d configured as periph%c with conf = 0x%lx\n",
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800758 pin->bank + 'A', pin->pin, pin->mux - 1 + 'A', pin->conf);
759 } else {
Hans Wennborg4b6fe452014-08-05 21:43:16 -0700760 dev_dbg(dev, "pio%c%d configured as gpio with conf = 0x%lx\n",
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800761 pin->bank + 'A', pin->pin, pin->conf);
762 }
763}
764
Sachin Kamat3c936002013-03-15 10:07:03 +0530765static int pin_check_config(struct at91_pinctrl *info, const char *name,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800766 int index, const struct at91_pmx_pin *pin)
767{
768 int mux;
769
770 /* check if it's a valid config */
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +0100771 if (pin->bank >= gpio_banks) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800772 dev_err(info->dev, "%s: pin conf %d bank_id %d >= nbanks %d\n",
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +0100773 name, index, pin->bank, gpio_banks);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800774 return -EINVAL;
775 }
776
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +0100777 if (!gpio_chips[pin->bank]) {
778 dev_err(info->dev, "%s: pin conf %d bank_id %d not enabled\n",
779 name, index, pin->bank);
780 return -ENXIO;
781 }
782
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800783 if (pin->pin >= MAX_NB_GPIO_PER_BANK) {
784 dev_err(info->dev, "%s: pin conf %d pin_bank_id %d >= %d\n",
785 name, index, pin->pin, MAX_NB_GPIO_PER_BANK);
786 return -EINVAL;
787 }
788
789 if (!pin->mux)
790 return 0;
791
792 mux = pin->mux - 1;
793
794 if (mux >= info->nmux) {
795 dev_err(info->dev, "%s: pin conf %d mux_id %d >= nmux %d\n",
796 name, index, mux, info->nmux);
797 return -EINVAL;
798 }
799
800 if (!(info->mux_mask[pin->bank * info->nmux + mux] & 1 << pin->pin)) {
801 dev_err(info->dev, "%s: pin conf %d mux_id %d not supported for pio%c%d\n",
802 name, index, mux, pin->bank + 'A', pin->pin);
803 return -EINVAL;
804 }
805
806 return 0;
807}
808
809static void at91_mux_gpio_disable(void __iomem *pio, unsigned mask)
810{
811 writel_relaxed(mask, pio + PIO_PDR);
812}
813
814static void at91_mux_gpio_enable(void __iomem *pio, unsigned mask, bool input)
815{
816 writel_relaxed(mask, pio + PIO_PER);
817 writel_relaxed(mask, pio + (input ? PIO_ODR : PIO_OER));
818}
819
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200820static int at91_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
821 unsigned group)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800822{
823 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
824 const struct at91_pmx_pin *pins_conf = info->groups[group].pins_conf;
825 const struct at91_pmx_pin *pin;
826 uint32_t npins = info->groups[group].npins;
827 int i, ret;
828 unsigned mask;
829 void __iomem *pio;
830
831 dev_dbg(info->dev, "enable function %s group %s\n",
832 info->functions[selector].name, info->groups[group].name);
833
834 /* first check that all the pins of the group are valid with a valid
Alexandre Belloni61e310a2013-10-16 16:12:33 +0200835 * parameter */
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800836 for (i = 0; i < npins; i++) {
837 pin = &pins_conf[i];
838 ret = pin_check_config(info, info->groups[group].name, i, pin);
839 if (ret)
840 return ret;
841 }
842
843 for (i = 0; i < npins; i++) {
844 pin = &pins_conf[i];
845 at91_pin_dbg(info->dev, pin);
846 pio = pin_to_controller(info, pin->bank);
David Dueck1ab36382015-07-28 09:48:16 +0200847
848 if (!pio)
849 continue;
850
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800851 mask = pin_to_mask(pin->pin);
852 at91_mux_disable_interrupt(pio, mask);
Sachin Kamat3c936002013-03-15 10:07:03 +0530853 switch (pin->mux) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800854 case AT91_MUX_GPIO:
855 at91_mux_gpio_enable(pio, mask, 1);
856 break;
857 case AT91_MUX_PERIPH_A:
858 info->ops->mux_A_periph(pio, mask);
859 break;
860 case AT91_MUX_PERIPH_B:
861 info->ops->mux_B_periph(pio, mask);
862 break;
863 case AT91_MUX_PERIPH_C:
864 if (!info->ops->mux_C_periph)
865 return -EINVAL;
866 info->ops->mux_C_periph(pio, mask);
867 break;
868 case AT91_MUX_PERIPH_D:
869 if (!info->ops->mux_D_periph)
870 return -EINVAL;
871 info->ops->mux_D_periph(pio, mask);
872 break;
873 }
874 if (pin->mux)
875 at91_mux_gpio_disable(pio, mask);
876 }
877
878 return 0;
879}
880
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800881static int at91_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
882{
883 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
884
885 return info->nfunctions;
886}
887
888static const char *at91_pmx_get_func_name(struct pinctrl_dev *pctldev,
889 unsigned selector)
890{
891 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
892
893 return info->functions[selector].name;
894}
895
896static int at91_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
897 const char * const **groups,
898 unsigned * const num_groups)
899{
900 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
901
902 *groups = info->functions[selector].groups;
903 *num_groups = info->functions[selector].ngroups;
904
905 return 0;
906}
907
Axel Linf6f94f62012-11-05 21:23:50 +0800908static int at91_gpio_request_enable(struct pinctrl_dev *pctldev,
909 struct pinctrl_gpio_range *range,
910 unsigned offset)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800911{
912 struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
913 struct at91_gpio_chip *at91_chip;
914 struct gpio_chip *chip;
915 unsigned mask;
916
917 if (!range) {
918 dev_err(npct->dev, "invalid range\n");
919 return -EINVAL;
920 }
921 if (!range->gc) {
922 dev_err(npct->dev, "missing GPIO chip in range\n");
923 return -EINVAL;
924 }
925 chip = range->gc;
Linus Walleij370ea612015-12-08 09:27:45 +0100926 at91_chip = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800927
928 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
929
930 mask = 1 << (offset - chip->base);
931
932 dev_dbg(npct->dev, "enable pin %u as PIO%c%d 0x%x\n",
933 offset, 'A' + range->id, offset - chip->base, mask);
934
935 writel_relaxed(mask, at91_chip->regbase + PIO_PER);
936
937 return 0;
938}
939
Axel Linf6f94f62012-11-05 21:23:50 +0800940static void at91_gpio_disable_free(struct pinctrl_dev *pctldev,
941 struct pinctrl_gpio_range *range,
942 unsigned offset)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800943{
944 struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
945
946 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
947 /* Set the pin to some default state, GPIO is usually default */
948}
949
Laurent Pinchart022ab142013-02-16 10:25:07 +0100950static const struct pinmux_ops at91_pmx_ops = {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800951 .get_functions_count = at91_pmx_get_funcs_count,
952 .get_function_name = at91_pmx_get_func_name,
953 .get_function_groups = at91_pmx_get_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200954 .set_mux = at91_pmx_set,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800955 .gpio_request_enable = at91_gpio_request_enable,
956 .gpio_disable_free = at91_gpio_disable_free,
957};
958
959static int at91_pinconf_get(struct pinctrl_dev *pctldev,
960 unsigned pin_id, unsigned long *config)
961{
962 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
963 void __iomem *pio;
964 unsigned pin;
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800965 int div;
Boris BREZILLON96bb12d2016-10-28 15:54:10 +0800966 bool out;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800967
Alexandre Belloni1292e692013-12-07 14:08:53 +0100968 *config = 0;
969 dev_dbg(info->dev, "%s:%d, pin_id=%d", __func__, __LINE__, pin_id);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800970 pio = pin_to_controller(info, pin_to_bank(pin_id));
David Dueck1ab36382015-07-28 09:48:16 +0200971
972 if (!pio)
973 return -EINVAL;
974
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800975 pin = pin_id % MAX_NB_GPIO_PER_BANK;
976
977 if (at91_mux_get_multidrive(pio, pin))
978 *config |= MULTI_DRIVE;
979
980 if (at91_mux_get_pullup(pio, pin))
981 *config |= PULL_UP;
982
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800983 if (info->ops->get_deglitch && info->ops->get_deglitch(pio, pin))
984 *config |= DEGLITCH;
985 if (info->ops->get_debounce && info->ops->get_debounce(pio, pin, &div))
986 *config |= DEBOUNCE | (div << DEBOUNCE_VAL_SHIFT);
987 if (info->ops->get_pulldown && info->ops->get_pulldown(pio, pin))
988 *config |= PULL_DOWN;
989 if (info->ops->get_schmitt_trig && info->ops->get_schmitt_trig(pio, pin))
990 *config |= DIS_SCHMIT;
Marek Roszko4334ac22014-08-23 23:12:04 -0400991 if (info->ops->get_drivestrength)
992 *config |= (info->ops->get_drivestrength(pio, pin)
993 << DRIVE_STRENGTH_SHIFT);
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000994 if (info->ops->get_slewrate)
995 *config |= (info->ops->get_slewrate(pio, pin) << SLEWRATE_SHIFT);
Boris BREZILLON96bb12d2016-10-28 15:54:10 +0800996 if (at91_mux_get_output(pio, pin, &out))
997 *config |= OUTPUT | (out << OUTPUT_VAL_SHIFT);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800998
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800999 return 0;
1000}
1001
1002static int at91_pinconf_set(struct pinctrl_dev *pctldev,
Sherman Yin03b054e2013-08-27 11:32:12 -07001003 unsigned pin_id, unsigned long *configs,
1004 unsigned num_configs)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001005{
1006 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1007 unsigned mask;
1008 void __iomem *pio;
Sherman Yin03b054e2013-08-27 11:32:12 -07001009 int i;
1010 unsigned long config;
Marek Roszko4334ac22014-08-23 23:12:04 -04001011 unsigned pin;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001012
Sherman Yin03b054e2013-08-27 11:32:12 -07001013 for (i = 0; i < num_configs; i++) {
1014 config = configs[i];
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001015
Sherman Yin03b054e2013-08-27 11:32:12 -07001016 dev_dbg(info->dev,
1017 "%s:%d, pin_id=%d, config=0x%lx",
1018 __func__, __LINE__, pin_id, config);
1019 pio = pin_to_controller(info, pin_to_bank(pin_id));
David Dueck1ab36382015-07-28 09:48:16 +02001020
1021 if (!pio)
1022 return -EINVAL;
1023
Marek Roszko4334ac22014-08-23 23:12:04 -04001024 pin = pin_id % MAX_NB_GPIO_PER_BANK;
1025 mask = pin_to_mask(pin);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +08001026
Sherman Yin03b054e2013-08-27 11:32:12 -07001027 if (config & PULL_UP && config & PULL_DOWN)
1028 return -EINVAL;
1029
Boris BREZILLON96bb12d2016-10-28 15:54:10 +08001030 at91_mux_set_output(pio, mask, config & OUTPUT,
1031 (config & OUTPUT_VAL) >> OUTPUT_VAL_SHIFT);
Sherman Yin03b054e2013-08-27 11:32:12 -07001032 at91_mux_set_pullup(pio, mask, config & PULL_UP);
1033 at91_mux_set_multidrive(pio, mask, config & MULTI_DRIVE);
1034 if (info->ops->set_deglitch)
1035 info->ops->set_deglitch(pio, mask, config & DEGLITCH);
1036 if (info->ops->set_debounce)
1037 info->ops->set_debounce(pio, mask, config & DEBOUNCE,
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +08001038 (config & DEBOUNCE_VAL) >> DEBOUNCE_VAL_SHIFT);
Sherman Yin03b054e2013-08-27 11:32:12 -07001039 if (info->ops->set_pulldown)
1040 info->ops->set_pulldown(pio, mask, config & PULL_DOWN);
1041 if (info->ops->disable_schmitt_trig && config & DIS_SCHMIT)
1042 info->ops->disable_schmitt_trig(pio, mask);
Marek Roszko4334ac22014-08-23 23:12:04 -04001043 if (info->ops->set_drivestrength)
1044 info->ops->set_drivestrength(pio, pin,
1045 (config & DRIVE_STRENGTH)
1046 >> DRIVE_STRENGTH_SHIFT);
Claudiu Beznea64e21ad2019-02-07 09:25:05 +00001047 if (info->ops->set_slewrate)
1048 info->ops->set_slewrate(pio, pin,
1049 (config & SLEWRATE) >> SLEWRATE_SHIFT);
Sherman Yin03b054e2013-08-27 11:32:12 -07001050
1051 } /* for each config */
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +08001052
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001053 return 0;
1054}
1055
Alexandre Belloni4d9b8a82013-12-07 14:08:54 +01001056#define DBG_SHOW_FLAG(flag) do { \
1057 if (config & flag) { \
1058 if (num_conf) \
1059 seq_puts(s, "|"); \
1060 seq_puts(s, #flag); \
1061 num_conf++; \
1062 } \
1063} while (0)
1064
Claudiu Bezneab67328e2019-02-07 09:24:46 +00001065#define DBG_SHOW_FLAG_MASKED(mask, flag, name) do { \
Marek Roszko4334ac22014-08-23 23:12:04 -04001066 if ((config & mask) == flag) { \
1067 if (num_conf) \
1068 seq_puts(s, "|"); \
Claudiu Bezneab67328e2019-02-07 09:24:46 +00001069 seq_puts(s, #name); \
Marek Roszko4334ac22014-08-23 23:12:04 -04001070 num_conf++; \
1071 } \
1072} while (0)
1073
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001074static void at91_pinconf_dbg_show(struct pinctrl_dev *pctldev,
1075 struct seq_file *s, unsigned pin_id)
1076{
Alexandre Belloni4d9b8a82013-12-07 14:08:54 +01001077 unsigned long config;
Rickard Strandqvist445d2022014-06-26 15:41:31 +02001078 int val, num_conf = 0;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001079
Rickard Strandqvist445d2022014-06-26 15:41:31 +02001080 at91_pinconf_get(pctldev, pin_id, &config);
Alexandre Belloni4d9b8a82013-12-07 14:08:54 +01001081
1082 DBG_SHOW_FLAG(MULTI_DRIVE);
1083 DBG_SHOW_FLAG(PULL_UP);
1084 DBG_SHOW_FLAG(PULL_DOWN);
1085 DBG_SHOW_FLAG(DIS_SCHMIT);
1086 DBG_SHOW_FLAG(DEGLITCH);
Claudiu Bezneab67328e2019-02-07 09:24:46 +00001087 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_BIT_MSK(LOW),
1088 DRIVE_STRENGTH_LOW);
1089 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_BIT_MSK(MED),
1090 DRIVE_STRENGTH_MED);
1091 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_BIT_MSK(HI),
1092 DRIVE_STRENGTH_HI);
Claudiu Beznea64e21ad2019-02-07 09:25:05 +00001093 DBG_SHOW_FLAG(SLEWRATE);
Alexandre Belloni4d9b8a82013-12-07 14:08:54 +01001094 DBG_SHOW_FLAG(DEBOUNCE);
1095 if (config & DEBOUNCE) {
1096 val = config >> DEBOUNCE_VAL_SHIFT;
1097 seq_printf(s, "(%d)", val);
1098 }
1099
1100 return;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001101}
1102
1103static void at91_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
1104 struct seq_file *s, unsigned group)
1105{
1106}
1107
Laurent Pinchart022ab142013-02-16 10:25:07 +01001108static const struct pinconf_ops at91_pinconf_ops = {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001109 .pin_config_get = at91_pinconf_get,
1110 .pin_config_set = at91_pinconf_set,
1111 .pin_config_dbg_show = at91_pinconf_dbg_show,
1112 .pin_config_group_dbg_show = at91_pinconf_group_dbg_show,
1113};
1114
1115static struct pinctrl_desc at91_pinctrl_desc = {
1116 .pctlops = &at91_pctrl_ops,
1117 .pmxops = &at91_pmx_ops,
1118 .confops = &at91_pinconf_ops,
1119 .owner = THIS_MODULE,
1120};
1121
1122static const char *gpio_compat = "atmel,at91rm9200-gpio";
1123
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001124static void at91_pinctrl_child_count(struct at91_pinctrl *info,
1125 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001126{
1127 struct device_node *child;
1128
1129 for_each_child_of_node(np, child) {
1130 if (of_device_is_compatible(child, gpio_compat)) {
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001131 if (of_device_is_available(child))
1132 info->nactive_banks++;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001133 } else {
1134 info->nfunctions++;
1135 info->ngroups += of_get_child_count(child);
1136 }
1137 }
1138}
1139
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001140static int at91_pinctrl_mux_mask(struct at91_pinctrl *info,
1141 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001142{
1143 int ret = 0;
1144 int size;
Sachin Kamat1164d732013-03-15 10:07:02 +05301145 const __be32 *list;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001146
1147 list = of_get_property(np, "atmel,mux-mask", &size);
1148 if (!list) {
1149 dev_err(info->dev, "can not read the mux-mask of %d\n", size);
1150 return -EINVAL;
1151 }
1152
1153 size /= sizeof(*list);
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001154 if (!size || size % gpio_banks) {
1155 dev_err(info->dev, "wrong mux mask array should be by %d\n", gpio_banks);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001156 return -EINVAL;
1157 }
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001158 info->nmux = size / gpio_banks;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001159
Kees Cooka86854d2018-06-12 14:07:58 -07001160 info->mux_mask = devm_kcalloc(info->dev, size, sizeof(u32),
1161 GFP_KERNEL);
Markus Elfring3da941b2017-12-23 20:44:27 +01001162 if (!info->mux_mask)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001163 return -ENOMEM;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001164
1165 ret = of_property_read_u32_array(np, "atmel,mux-mask",
1166 info->mux_mask, size);
1167 if (ret)
1168 dev_err(info->dev, "can not read the mux-mask of %d\n", size);
1169 return ret;
1170}
1171
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001172static int at91_pinctrl_parse_groups(struct device_node *np,
1173 struct at91_pin_group *grp,
1174 struct at91_pinctrl *info, u32 index)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001175{
1176 struct at91_pmx_pin *pin;
1177 int size;
Sachin Kamat1164d732013-03-15 10:07:02 +05301178 const __be32 *list;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001179 int i, j;
1180
Rob Herring94f4e542018-08-27 20:52:41 -05001181 dev_dbg(info->dev, "group(%d): %pOFn\n", index, np);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001182
1183 /* Initialise group */
1184 grp->name = np->name;
1185
1186 /*
1187 * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
1188 * do sanity check and calculate pins number
1189 */
1190 list = of_get_property(np, "atmel,pins", &size);
1191 /* we do not check return since it's safe node passed down */
1192 size /= sizeof(*list);
1193 if (!size || size % 4) {
1194 dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
1195 return -EINVAL;
1196 }
1197
1198 grp->npins = size / 4;
Kees Cooka86854d2018-06-12 14:07:58 -07001199 pin = grp->pins_conf = devm_kcalloc(info->dev,
1200 grp->npins,
1201 sizeof(struct at91_pmx_pin),
1202 GFP_KERNEL);
1203 grp->pins = devm_kcalloc(info->dev, grp->npins, sizeof(unsigned int),
1204 GFP_KERNEL);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001205 if (!grp->pins_conf || !grp->pins)
1206 return -ENOMEM;
1207
1208 for (i = 0, j = 0; i < size; i += 4, j++) {
1209 pin->bank = be32_to_cpu(*list++);
1210 pin->pin = be32_to_cpu(*list++);
1211 grp->pins[j] = pin->bank * MAX_NB_GPIO_PER_BANK + pin->pin;
1212 pin->mux = be32_to_cpu(*list++);
1213 pin->conf = be32_to_cpu(*list++);
1214
1215 at91_pin_dbg(info->dev, pin);
1216 pin++;
1217 }
1218
1219 return 0;
1220}
1221
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001222static int at91_pinctrl_parse_functions(struct device_node *np,
1223 struct at91_pinctrl *info, u32 index)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001224{
1225 struct device_node *child;
1226 struct at91_pmx_func *func;
1227 struct at91_pin_group *grp;
1228 int ret;
1229 static u32 grp_index;
1230 u32 i = 0;
1231
Rob Herring94f4e542018-08-27 20:52:41 -05001232 dev_dbg(info->dev, "parse function(%d): %pOFn\n", index, np);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001233
1234 func = &info->functions[index];
1235
1236 /* Initialise function */
1237 func->name = np->name;
1238 func->ngroups = of_get_child_count(np);
Rickard Strandqvistca7162a2014-06-26 13:26:45 +02001239 if (func->ngroups == 0) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001240 dev_err(info->dev, "no groups defined\n");
1241 return -EINVAL;
1242 }
Kees Cooka86854d2018-06-12 14:07:58 -07001243 func->groups = devm_kcalloc(info->dev,
1244 func->ngroups, sizeof(char *), GFP_KERNEL);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001245 if (!func->groups)
1246 return -ENOMEM;
1247
1248 for_each_child_of_node(np, child) {
1249 func->groups[i] = child->name;
1250 grp = &info->groups[grp_index++];
1251 ret = at91_pinctrl_parse_groups(child, grp, info, i++);
Julia Lawalld94b9862015-10-24 16:42:35 +02001252 if (ret) {
1253 of_node_put(child);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001254 return ret;
Julia Lawalld94b9862015-10-24 16:42:35 +02001255 }
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001256 }
1257
1258 return 0;
1259}
1260
Fabian Frederickbaa9946e2015-03-16 20:59:09 +01001261static const struct of_device_id at91_pinctrl_of_match[] = {
Marek Roszko4334ac22014-08-23 23:12:04 -04001262 { .compatible = "atmel,sama5d3-pinctrl", .data = &sama5d3_ops },
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001263 { .compatible = "atmel,at91sam9x5-pinctrl", .data = &at91sam9x5_ops },
1264 { .compatible = "atmel,at91rm9200-pinctrl", .data = &at91rm9200_ops },
Claudiu Bezneaa2fcb1c2019-02-07 09:24:53 +00001265 { .compatible = "microchip,sam9x60-pinctrl", .data = &sam9x60_ops },
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001266 { /* sentinel */ }
1267};
1268
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001269static int at91_pinctrl_probe_dt(struct platform_device *pdev,
1270 struct at91_pinctrl *info)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001271{
1272 int ret = 0;
1273 int i, j;
1274 uint32_t *tmp;
1275 struct device_node *np = pdev->dev.of_node;
1276 struct device_node *child;
1277
1278 if (!np)
1279 return -ENODEV;
1280
1281 info->dev = &pdev->dev;
Sachin Kamat3c936002013-03-15 10:07:03 +05301282 info->ops = (struct at91_pinctrl_mux_ops *)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001283 of_match_device(at91_pinctrl_of_match, &pdev->dev)->data;
1284 at91_pinctrl_child_count(info, np);
1285
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001286 if (gpio_banks < 1) {
Alexandre Belloni61e310a2013-10-16 16:12:33 +02001287 dev_err(&pdev->dev, "you need to specify at least one gpio-controller\n");
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001288 return -EINVAL;
1289 }
1290
1291 ret = at91_pinctrl_mux_mask(info, np);
1292 if (ret)
1293 return ret;
1294
1295 dev_dbg(&pdev->dev, "nmux = %d\n", info->nmux);
1296
1297 dev_dbg(&pdev->dev, "mux-mask\n");
1298 tmp = info->mux_mask;
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001299 for (i = 0; i < gpio_banks; i++) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001300 for (j = 0; j < info->nmux; j++, tmp++) {
1301 dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
1302 }
1303 }
1304
1305 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1306 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
Kees Cooka86854d2018-06-12 14:07:58 -07001307 info->functions = devm_kcalloc(&pdev->dev,
1308 info->nfunctions,
1309 sizeof(struct at91_pmx_func),
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001310 GFP_KERNEL);
1311 if (!info->functions)
1312 return -ENOMEM;
1313
Kees Cooka86854d2018-06-12 14:07:58 -07001314 info->groups = devm_kcalloc(&pdev->dev,
1315 info->ngroups,
1316 sizeof(struct at91_pin_group),
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001317 GFP_KERNEL);
1318 if (!info->groups)
1319 return -ENOMEM;
1320
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001321 dev_dbg(&pdev->dev, "nbanks = %d\n", gpio_banks);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001322 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1323 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
1324
1325 i = 0;
1326
1327 for_each_child_of_node(np, child) {
1328 if (of_device_is_compatible(child, gpio_compat))
1329 continue;
1330 ret = at91_pinctrl_parse_functions(child, info, i++);
1331 if (ret) {
1332 dev_err(&pdev->dev, "failed to parse function\n");
Julia Lawalld94b9862015-10-24 16:42:35 +02001333 of_node_put(child);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001334 return ret;
1335 }
1336 }
1337
1338 return 0;
1339}
1340
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001341static int at91_pinctrl_probe(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001342{
1343 struct at91_pinctrl *info;
1344 struct pinctrl_pin_desc *pdesc;
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001345 int ret, i, j, k, ngpio_chips_enabled = 0;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001346
1347 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1348 if (!info)
1349 return -ENOMEM;
1350
1351 ret = at91_pinctrl_probe_dt(pdev, info);
1352 if (ret)
1353 return ret;
1354
1355 /*
1356 * We need all the GPIO drivers to probe FIRST, or we will not be able
1357 * to obtain references to the struct gpio_chip * for them, and we
1358 * need this to proceed.
1359 */
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001360 for (i = 0; i < gpio_banks; i++)
1361 if (gpio_chips[i])
1362 ngpio_chips_enabled++;
1363
1364 if (ngpio_chips_enabled < info->nactive_banks) {
1365 dev_warn(&pdev->dev,
1366 "All GPIO chips are not registered yet (%d/%d)\n",
1367 ngpio_chips_enabled, info->nactive_banks);
1368 devm_kfree(&pdev->dev, info);
1369 return -EPROBE_DEFER;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001370 }
1371
1372 at91_pinctrl_desc.name = dev_name(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001373 at91_pinctrl_desc.npins = gpio_banks * MAX_NB_GPIO_PER_BANK;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001374 at91_pinctrl_desc.pins = pdesc =
Kees Cooka86854d2018-06-12 14:07:58 -07001375 devm_kcalloc(&pdev->dev,
1376 at91_pinctrl_desc.npins, sizeof(*pdesc),
1377 GFP_KERNEL);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001378
1379 if (!at91_pinctrl_desc.pins)
1380 return -ENOMEM;
1381
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001382 for (i = 0, k = 0; i < gpio_banks; i++) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001383 for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) {
1384 pdesc->number = k;
1385 pdesc->name = kasprintf(GFP_KERNEL, "pio%c%d", i + 'A', j);
1386 pdesc++;
1387 }
1388 }
1389
1390 platform_set_drvdata(pdev, info);
Laxman Dewangan5c674252016-02-28 14:32:19 +05301391 info->pctl = devm_pinctrl_register(&pdev->dev, &at91_pinctrl_desc,
1392 info);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001393
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001394 if (IS_ERR(info->pctl)) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001395 dev_err(&pdev->dev, "could not register AT91 pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001396 return PTR_ERR(info->pctl);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001397 }
1398
1399 /* We will handle a range of GPIO pins */
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001400 for (i = 0; i < gpio_banks; i++)
1401 if (gpio_chips[i])
1402 pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001403
1404 dev_info(&pdev->dev, "initialized AT91 pinctrl driver\n");
1405
1406 return 0;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001407}
1408
Richard Genoud8af584b2014-02-17 17:57:26 +01001409static int at91_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
1410{
Linus Walleij370ea612015-12-08 09:27:45 +01001411 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Richard Genoud8af584b2014-02-17 17:57:26 +01001412 void __iomem *pio = at91_gpio->regbase;
1413 unsigned mask = 1 << offset;
1414 u32 osr;
1415
1416 osr = readl_relaxed(pio + PIO_OSR);
1417 return !(osr & mask);
1418}
1419
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001420static int at91_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
1421{
Linus Walleij370ea612015-12-08 09:27:45 +01001422 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001423 void __iomem *pio = at91_gpio->regbase;
1424 unsigned mask = 1 << offset;
1425
1426 writel_relaxed(mask, pio + PIO_ODR);
1427 return 0;
1428}
1429
1430static int at91_gpio_get(struct gpio_chip *chip, unsigned offset)
1431{
Linus Walleij370ea612015-12-08 09:27:45 +01001432 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001433 void __iomem *pio = at91_gpio->regbase;
1434 unsigned mask = 1 << offset;
1435 u32 pdsr;
1436
1437 pdsr = readl_relaxed(pio + PIO_PDSR);
1438 return (pdsr & mask) != 0;
1439}
1440
1441static void at91_gpio_set(struct gpio_chip *chip, unsigned offset,
1442 int val)
1443{
Linus Walleij370ea612015-12-08 09:27:45 +01001444 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001445 void __iomem *pio = at91_gpio->regbase;
1446 unsigned mask = 1 << offset;
1447
1448 writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
1449}
1450
Alexander Stein1893b2c2015-04-02 11:55:49 +02001451static void at91_gpio_set_multiple(struct gpio_chip *chip,
1452 unsigned long *mask, unsigned long *bits)
1453{
Linus Walleij370ea612015-12-08 09:27:45 +01001454 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Alexander Stein1893b2c2015-04-02 11:55:49 +02001455 void __iomem *pio = at91_gpio->regbase;
1456
1457#define BITS_MASK(bits) (((bits) == 32) ? ~0U : (BIT(bits) - 1))
1458 /* Mask additionally to ngpio as not all GPIO controllers have 32 pins */
1459 uint32_t set_mask = (*mask & *bits) & BITS_MASK(chip->ngpio);
1460 uint32_t clear_mask = (*mask & ~(*bits)) & BITS_MASK(chip->ngpio);
1461
1462 writel_relaxed(set_mask, pio + PIO_SODR);
1463 writel_relaxed(clear_mask, pio + PIO_CODR);
1464}
1465
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001466static int at91_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
1467 int val)
1468{
Linus Walleij370ea612015-12-08 09:27:45 +01001469 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001470 void __iomem *pio = at91_gpio->regbase;
1471 unsigned mask = 1 << offset;
1472
1473 writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
1474 writel_relaxed(mask, pio + PIO_OER);
1475
1476 return 0;
1477}
1478
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001479#ifdef CONFIG_DEBUG_FS
1480static void at91_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1481{
1482 enum at91_mux mode;
1483 int i;
Linus Walleij370ea612015-12-08 09:27:45 +01001484 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001485 void __iomem *pio = at91_gpio->regbase;
1486
1487 for (i = 0; i < chip->ngpio; i++) {
Alexander Stein47f22712014-04-14 20:53:08 +02001488 unsigned mask = pin_to_mask(i);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001489 const char *gpio_label;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001490
1491 gpio_label = gpiochip_is_requested(chip, i);
1492 if (!gpio_label)
1493 continue;
1494 mode = at91_gpio->ops->get_periph(pio, mask);
1495 seq_printf(s, "[%s] GPIO%s%d: ",
1496 gpio_label, chip->label, i);
1497 if (mode == AT91_MUX_GPIO) {
Matthieu Crapet853b6bf2014-11-18 15:43:45 +01001498 seq_printf(s, "[gpio] ");
1499 seq_printf(s, "%s ",
1500 readl_relaxed(pio + PIO_OSR) & mask ?
1501 "output" : "input");
1502 seq_printf(s, "%s\n",
1503 readl_relaxed(pio + PIO_PDSR) & mask ?
1504 "set" : "clear");
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001505 } else {
1506 seq_printf(s, "[periph %c]\n",
1507 mode + 'A' - 1);
1508 }
1509 }
1510}
1511#else
1512#define at91_gpio_dbg_show NULL
1513#endif
1514
1515/* Several AIC controller irqs are dispatched through this GPIO handler.
1516 * To use any AT91_PIN_* as an externally triggered IRQ, first call
1517 * at91_set_gpio_input() then maybe enable its glitch filter.
1518 * Then just request_irq() with the pin ID; it works like any ARM IRQ
1519 * handler.
1520 * First implementation always triggers on rising and falling edges
1521 * whereas the newer PIO3 can be additionally configured to trigger on
1522 * level, edge with any polarity.
1523 *
1524 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
1525 * configuring them with at91_set_a_periph() or at91_set_b_periph().
1526 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
1527 */
1528
1529static void gpio_irq_mask(struct irq_data *d)
1530{
1531 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1532 void __iomem *pio = at91_gpio->regbase;
1533 unsigned mask = 1 << d->hwirq;
1534
1535 if (pio)
1536 writel_relaxed(mask, pio + PIO_IDR);
1537}
1538
1539static void gpio_irq_unmask(struct irq_data *d)
1540{
1541 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1542 void __iomem *pio = at91_gpio->regbase;
1543 unsigned mask = 1 << d->hwirq;
1544
1545 if (pio)
1546 writel_relaxed(mask, pio + PIO_IER);
1547}
1548
1549static int gpio_irq_type(struct irq_data *d, unsigned type)
1550{
1551 switch (type) {
1552 case IRQ_TYPE_NONE:
1553 case IRQ_TYPE_EDGE_BOTH:
1554 return 0;
1555 default:
1556 return -EINVAL;
1557 }
1558}
1559
1560/* Alternate irq type for PIO3 support */
1561static int alt_gpio_irq_type(struct irq_data *d, unsigned type)
1562{
1563 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1564 void __iomem *pio = at91_gpio->regbase;
1565 unsigned mask = 1 << d->hwirq;
1566
1567 switch (type) {
1568 case IRQ_TYPE_EDGE_RISING:
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001569 irq_set_handler_locked(d, handle_simple_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001570 writel_relaxed(mask, pio + PIO_ESR);
1571 writel_relaxed(mask, pio + PIO_REHLSR);
1572 break;
1573 case IRQ_TYPE_EDGE_FALLING:
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001574 irq_set_handler_locked(d, handle_simple_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001575 writel_relaxed(mask, pio + PIO_ESR);
1576 writel_relaxed(mask, pio + PIO_FELLSR);
1577 break;
1578 case IRQ_TYPE_LEVEL_LOW:
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001579 irq_set_handler_locked(d, handle_level_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001580 writel_relaxed(mask, pio + PIO_LSR);
1581 writel_relaxed(mask, pio + PIO_FELLSR);
1582 break;
1583 case IRQ_TYPE_LEVEL_HIGH:
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001584 irq_set_handler_locked(d, handle_level_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001585 writel_relaxed(mask, pio + PIO_LSR);
1586 writel_relaxed(mask, pio + PIO_REHLSR);
1587 break;
1588 case IRQ_TYPE_EDGE_BOTH:
1589 /*
1590 * disable additional interrupt modes:
1591 * fall back to default behavior
1592 */
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001593 irq_set_handler_locked(d, handle_simple_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001594 writel_relaxed(mask, pio + PIO_AIMDR);
1595 return 0;
1596 case IRQ_TYPE_NONE:
1597 default:
Linus Walleij1c5fb662018-09-13 13:58:21 +02001598 pr_warn("AT91: No type for GPIO irq offset %d\n", d->irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001599 return -EINVAL;
1600 }
1601
1602 /* enable additional interrupt modes */
1603 writel_relaxed(mask, pio + PIO_AIMER);
1604
1605 return 0;
1606}
1607
Alexander Stein80cc3732014-04-15 22:09:41 +02001608static void gpio_irq_ack(struct irq_data *d)
1609{
1610 /* the interrupt is already cleared before by reading ISR */
1611}
1612
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001613#ifdef CONFIG_PM
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001614
1615static u32 wakeups[MAX_GPIO_BANKS];
1616static u32 backups[MAX_GPIO_BANKS];
1617
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001618static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
1619{
1620 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1621 unsigned bank = at91_gpio->pioc_idx;
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001622 unsigned mask = 1 << d->hwirq;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001623
1624 if (unlikely(bank >= MAX_GPIO_BANKS))
1625 return -EINVAL;
1626
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001627 if (state)
1628 wakeups[bank] |= mask;
1629 else
1630 wakeups[bank] &= ~mask;
1631
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001632 irq_set_irq_wake(at91_gpio->pioc_virq, state);
1633
1634 return 0;
1635}
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001636
1637void at91_pinctrl_gpio_suspend(void)
1638{
1639 int i;
1640
1641 for (i = 0; i < gpio_banks; i++) {
1642 void __iomem *pio;
1643
1644 if (!gpio_chips[i])
1645 continue;
1646
1647 pio = gpio_chips[i]->regbase;
1648
Ben Dooksd480239b2015-03-26 12:18:49 +00001649 backups[i] = readl_relaxed(pio + PIO_IMR);
1650 writel_relaxed(backups[i], pio + PIO_IDR);
1651 writel_relaxed(wakeups[i], pio + PIO_IER);
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001652
Boris BREZILLON795f9952013-12-15 19:30:51 +01001653 if (!wakeups[i])
1654 clk_disable_unprepare(gpio_chips[i]->clock);
1655 else
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001656 printk(KERN_DEBUG "GPIO-%c may wake for %08x\n",
1657 'A'+i, wakeups[i]);
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001658 }
1659}
1660
1661void at91_pinctrl_gpio_resume(void)
1662{
1663 int i;
1664
1665 for (i = 0; i < gpio_banks; i++) {
1666 void __iomem *pio;
1667
1668 if (!gpio_chips[i])
1669 continue;
1670
1671 pio = gpio_chips[i]->regbase;
1672
Boris BREZILLON37ef1d92013-12-15 19:30:52 +01001673 if (!wakeups[i])
1674 clk_prepare_enable(gpio_chips[i]->clock);
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001675
Ben Dooksd480239b2015-03-26 12:18:49 +00001676 writel_relaxed(wakeups[i], pio + PIO_IDR);
1677 writel_relaxed(backups[i], pio + PIO_IER);
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001678 }
1679}
1680
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001681#else
1682#define gpio_irq_set_wake NULL
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001683#endif /* CONFIG_PM */
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001684
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +02001685static void gpio_irq_handler(struct irq_desc *desc)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001686{
Jiang Liu5663bb22015-06-04 12:13:16 +08001687 struct irq_chip *chip = irq_desc_get_chip(desc);
Alexander Stein80cc3732014-04-15 22:09:41 +02001688 struct gpio_chip *gpio_chip = irq_desc_get_handler_data(desc);
Linus Walleij370ea612015-12-08 09:27:45 +01001689 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(gpio_chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001690 void __iomem *pio = at91_gpio->regbase;
1691 unsigned long isr;
1692 int n;
1693
1694 chained_irq_enter(chip, desc);
1695 for (;;) {
1696 /* Reading ISR acks pending (edge triggered) GPIO interrupts.
Alexandre Bellonic2eb9e72013-12-07 14:08:52 +01001697 * When there are none pending, we're finished unless we need
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001698 * to process multiple banks (like ID_PIOCDE on sam9263).
1699 */
1700 isr = readl_relaxed(pio + PIO_ISR) & readl_relaxed(pio + PIO_IMR);
1701 if (!isr) {
1702 if (!at91_gpio->next)
1703 break;
1704 at91_gpio = at91_gpio->next;
1705 pio = at91_gpio->regbase;
Alexander Steincccb0c32014-04-24 19:55:39 +02001706 gpio_chip = &at91_gpio->chip;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001707 continue;
1708 }
1709
Wei Yongjun05daa162012-10-26 22:50:54 +08001710 for_each_set_bit(n, &isr, BITS_PER_LONG) {
Alexander Stein80cc3732014-04-15 22:09:41 +02001711 generic_handle_irq(irq_find_mapping(
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001712 gpio_chip->irq.domain, n));
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001713 }
1714 }
1715 chained_irq_exit(chip, desc);
1716 /* now it may re-trigger */
1717}
1718
Pramod Gurav834e1672014-09-09 15:50:37 +05301719static int at91_gpio_of_irq_setup(struct platform_device *pdev,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001720 struct at91_gpio_chip *at91_gpio)
1721{
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001722 struct gpio_chip *gpiochip_prev = NULL;
Alexander Steincccb0c32014-04-24 19:55:39 +02001723 struct at91_gpio_chip *prev = NULL;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001724 struct irq_data *d = irq_get_irq_data(at91_gpio->pioc_virq);
Ludovic Desroches0c3dfa12018-09-13 14:42:13 +02001725 struct irq_chip *gpio_irqchip;
Linus Walleij35dea5d2019-10-01 15:06:45 +02001726 struct gpio_irq_chip *girq;
1727 int i;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001728
Linus Walleij35dea5d2019-10-01 15:06:45 +02001729 gpio_irqchip = devm_kzalloc(&pdev->dev, sizeof(*gpio_irqchip),
1730 GFP_KERNEL);
Ludovic Desroches0c3dfa12018-09-13 14:42:13 +02001731 if (!gpio_irqchip)
1732 return -ENOMEM;
1733
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001734 at91_gpio->pioc_hwirq = irqd_to_hwirq(d);
1735
Ludovic Desroches0c3dfa12018-09-13 14:42:13 +02001736 gpio_irqchip->name = "GPIO";
1737 gpio_irqchip->irq_ack = gpio_irq_ack;
1738 gpio_irqchip->irq_disable = gpio_irq_mask;
1739 gpio_irqchip->irq_mask = gpio_irq_mask;
1740 gpio_irqchip->irq_unmask = gpio_irq_unmask;
1741 gpio_irqchip->irq_set_wake = gpio_irq_set_wake,
1742 gpio_irqchip->irq_set_type = at91_gpio->ops->irq_type;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001743
1744 /* Disable irqs of this PIO controller */
1745 writel_relaxed(~0, at91_gpio->regbase + PIO_IDR);
1746
Alexander Stein80cc3732014-04-15 22:09:41 +02001747 /*
1748 * Let the generic code handle this edge IRQ, the the chained
1749 * handler will perform the actual work of handling the parent
1750 * interrupt.
1751 */
Linus Walleij35dea5d2019-10-01 15:06:45 +02001752 girq = &at91_gpio->chip.irq;
1753 girq->chip = gpio_irqchip;
1754 girq->default_type = IRQ_TYPE_NONE;
1755 girq->handler = handle_edge_irq;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001756
Linus Walleij35dea5d2019-10-01 15:06:45 +02001757 /*
1758 * The top level handler handles one bank of GPIOs, except
Alexander Steincccb0c32014-04-24 19:55:39 +02001759 * on some SoC it can handle up to three...
1760 * We only set up the handler for the first of the list.
1761 */
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001762 gpiochip_prev = irq_get_handler_data(at91_gpio->pioc_virq);
1763 if (!gpiochip_prev) {
Linus Walleij35dea5d2019-10-01 15:06:45 +02001764 girq->parent_handler = gpio_irq_handler;
1765 girq->num_parents = 1;
1766 girq->parents = devm_kcalloc(&pdev->dev, 1,
1767 sizeof(*girq->parents),
1768 GFP_KERNEL);
1769 if (!girq->parents)
1770 return -ENOMEM;
1771 girq->parents[0] = at91_gpio->pioc_virq;
Alexander Steincccb0c32014-04-24 19:55:39 +02001772 return 0;
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001773 }
Alexander Steincccb0c32014-04-24 19:55:39 +02001774
Linus Walleij370ea612015-12-08 09:27:45 +01001775 prev = gpiochip_get_data(gpiochip_prev);
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001776 /* we can only have 2 banks before */
1777 for (i = 0; i < 2; i++) {
1778 if (prev->next) {
1779 prev = prev->next;
1780 } else {
1781 prev->next = at91_gpio;
1782 return 0;
1783 }
1784 }
1785
1786 return -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001787}
1788
1789/* This structure is replicated for each GPIO block allocated at probe time */
Alexander Stein234b6512016-04-29 14:50:02 +02001790static const struct gpio_chip at91_gpio_template = {
Jonas Gorski98c85d52015-10-11 17:34:19 +02001791 .request = gpiochip_generic_request,
1792 .free = gpiochip_generic_free,
Richard Genoud8af584b2014-02-17 17:57:26 +01001793 .get_direction = at91_gpio_get_direction,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001794 .direction_input = at91_gpio_direction_input,
1795 .get = at91_gpio_get,
1796 .direction_output = at91_gpio_direction_output,
1797 .set = at91_gpio_set,
Alexander Stein1893b2c2015-04-02 11:55:49 +02001798 .set_multiple = at91_gpio_set_multiple,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001799 .dbg_show = at91_gpio_dbg_show,
Linus Walleij9fb1f392013-12-04 14:42:46 +01001800 .can_sleep = false,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001801 .ngpio = MAX_NB_GPIO_PER_BANK,
1802};
1803
Fabian Frederickbaa9946e2015-03-16 20:59:09 +01001804static const struct of_device_id at91_gpio_of_match[] = {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001805 { .compatible = "atmel,at91sam9x5-gpio", .data = &at91sam9x5_ops, },
1806 { .compatible = "atmel,at91rm9200-gpio", .data = &at91rm9200_ops },
Claudiu Bezneaa2fcb1c2019-02-07 09:24:53 +00001807 { .compatible = "microchip,sam9x60-gpio", .data = &sam9x60_ops },
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001808 { /* sentinel */ }
1809};
1810
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001811static int at91_gpio_probe(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001812{
1813 struct device_node *np = pdev->dev.of_node;
1814 struct resource *res;
1815 struct at91_gpio_chip *at91_chip = NULL;
1816 struct gpio_chip *chip;
1817 struct pinctrl_gpio_range *range;
1818 int ret = 0;
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001819 int irq, i;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001820 int alias_idx = of_alias_get_id(np, "gpio");
1821 uint32_t ngpio;
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001822 char **names;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001823
1824 BUG_ON(alias_idx >= ARRAY_SIZE(gpio_chips));
1825 if (gpio_chips[alias_idx]) {
1826 ret = -EBUSY;
1827 goto err;
1828 }
1829
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001830 irq = platform_get_irq(pdev, 0);
1831 if (irq < 0) {
1832 ret = irq;
1833 goto err;
1834 }
1835
1836 at91_chip = devm_kzalloc(&pdev->dev, sizeof(*at91_chip), GFP_KERNEL);
1837 if (!at91_chip) {
1838 ret = -ENOMEM;
1839 goto err;
1840 }
1841
Wolfram Sangf50b9e12013-05-10 10:17:03 +02001842 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding9e0c1fb2013-01-21 11:09:14 +01001843 at91_chip->regbase = devm_ioremap_resource(&pdev->dev, res);
1844 if (IS_ERR(at91_chip->regbase)) {
1845 ret = PTR_ERR(at91_chip->regbase);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001846 goto err;
1847 }
1848
Sachin Kamat3c936002013-03-15 10:07:03 +05301849 at91_chip->ops = (struct at91_pinctrl_mux_ops *)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001850 of_match_device(at91_gpio_of_match, &pdev->dev)->data;
1851 at91_chip->pioc_virq = irq;
1852 at91_chip->pioc_idx = alias_idx;
1853
Pramod Gurav02b837f2014-08-31 16:51:52 +05301854 at91_chip->clock = devm_clk_get(&pdev->dev, NULL);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001855 if (IS_ERR(at91_chip->clock)) {
1856 dev_err(&pdev->dev, "failed to get clock, ignoring.\n");
Pramod Gurav70e41972014-09-09 15:50:36 +05301857 ret = PTR_ERR(at91_chip->clock);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001858 goto err;
1859 }
1860
Alexander Stein7d3a3fe2016-04-29 14:50:03 +02001861 ret = clk_prepare_enable(at91_chip->clock);
Pramod Gurav70e41972014-09-09 15:50:36 +05301862 if (ret) {
Alexander Stein7d3a3fe2016-04-29 14:50:03 +02001863 dev_err(&pdev->dev, "failed to prepare and enable clock, ignoring.\n");
Pramod Gurav70e41972014-09-09 15:50:36 +05301864 goto clk_enable_err;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001865 }
1866
1867 at91_chip->chip = at91_gpio_template;
1868
1869 chip = &at91_chip->chip;
1870 chip->of_node = np;
1871 chip->label = dev_name(&pdev->dev);
Linus Walleij58383c782015-11-04 09:56:26 +01001872 chip->parent = &pdev->dev;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001873 chip->owner = THIS_MODULE;
1874 chip->base = alias_idx * MAX_NB_GPIO_PER_BANK;
1875
1876 if (!of_property_read_u32(np, "#gpio-lines", &ngpio)) {
1877 if (ngpio >= MAX_NB_GPIO_PER_BANK)
1878 pr_err("at91_gpio.%d, gpio-nb >= %d failback to %d\n",
1879 alias_idx, MAX_NB_GPIO_PER_BANK, MAX_NB_GPIO_PER_BANK);
1880 else
1881 chip->ngpio = ngpio;
1882 }
1883
Kees Cooka86854d2018-06-12 14:07:58 -07001884 names = devm_kcalloc(&pdev->dev, chip->ngpio, sizeof(char *),
Sachin Kamat3c936002013-03-15 10:07:03 +05301885 GFP_KERNEL);
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001886
1887 if (!names) {
1888 ret = -ENOMEM;
Pramod Gurav70e41972014-09-09 15:50:36 +05301889 goto clk_enable_err;
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001890 }
1891
1892 for (i = 0; i < chip->ngpio; i++)
1893 names[i] = kasprintf(GFP_KERNEL, "pio%c%d", alias_idx + 'A', i);
1894
Sachin Kamat3c936002013-03-15 10:07:03 +05301895 chip->names = (const char *const *)names;
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001896
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001897 range = &at91_chip->range;
1898 range->name = chip->label;
1899 range->id = alias_idx;
1900 range->pin_base = range->base = range->id * MAX_NB_GPIO_PER_BANK;
1901
1902 range->npins = chip->ngpio;
1903 range->gc = chip;
1904
Linus Walleij35dea5d2019-10-01 15:06:45 +02001905 ret = at91_gpio_of_irq_setup(pdev, at91_chip);
1906 if (ret)
1907 goto gpiochip_add_err;
1908
Linus Walleij370ea612015-12-08 09:27:45 +01001909 ret = gpiochip_add_data(chip, at91_chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001910 if (ret)
Pramod Gurav70e41972014-09-09 15:50:36 +05301911 goto gpiochip_add_err;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001912
1913 gpio_chips[alias_idx] = at91_chip;
1914 gpio_banks = max(gpio_banks, alias_idx + 1);
1915
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001916 dev_info(&pdev->dev, "at address %p\n", at91_chip->regbase);
1917
1918 return 0;
1919
Pramod Gurav70e41972014-09-09 15:50:36 +05301920gpiochip_add_err:
Pramod Gurav70e41972014-09-09 15:50:36 +05301921clk_enable_err:
Alexander Stein7d3a3fe2016-04-29 14:50:03 +02001922 clk_disable_unprepare(at91_chip->clock);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001923err:
1924 dev_err(&pdev->dev, "Failure %i for GPIO %i\n", ret, alias_idx);
1925
1926 return ret;
1927}
1928
1929static struct platform_driver at91_gpio_driver = {
1930 .driver = {
1931 .name = "gpio-at91",
Sachin Kamat606fca92013-09-28 17:38:48 +05301932 .of_match_table = at91_gpio_of_match,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001933 },
1934 .probe = at91_gpio_probe,
1935};
1936
1937static struct platform_driver at91_pinctrl_driver = {
1938 .driver = {
1939 .name = "pinctrl-at91",
Sachin Kamat606fca92013-09-28 17:38:48 +05301940 .of_match_table = at91_pinctrl_of_match,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001941 },
1942 .probe = at91_pinctrl_probe,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001943};
1944
Thierry Redingbab7f5a2015-12-02 17:31:55 +01001945static struct platform_driver * const drivers[] = {
1946 &at91_gpio_driver,
1947 &at91_pinctrl_driver,
1948};
1949
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001950static int __init at91_pinctrl_init(void)
1951{
Thierry Redingbab7f5a2015-12-02 17:31:55 +01001952 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001953}
1954arch_initcall(at91_pinctrl_init);