blob: 72edc675431ce824d637ede01a3c912f7864f250 [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
Lee Jonesaa786552020-07-13 15:49:22 +010068/*
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
Lee Jonesaa786552020-07-13 15:49:22 +0100164 * @get_drivestrength: get driver strength
165 * @set_drivestrength: set driver strength
166 * @get_slewrate: get slew rate
167 * @set_slewrate: set slew rate
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800168 * @irq_type: return irq type
169 */
170struct at91_pinctrl_mux_ops {
171 enum at91_mux (*get_periph)(void __iomem *pio, unsigned mask);
172 void (*mux_A_periph)(void __iomem *pio, unsigned mask);
173 void (*mux_B_periph)(void __iomem *pio, unsigned mask);
174 void (*mux_C_periph)(void __iomem *pio, unsigned mask);
175 void (*mux_D_periph)(void __iomem *pio, unsigned mask);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800176 bool (*get_deglitch)(void __iomem *pio, unsigned pin);
Boris BREZILLON77966ad2013-09-13 09:45:33 +0200177 void (*set_deglitch)(void __iomem *pio, unsigned mask, bool is_on);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800178 bool (*get_debounce)(void __iomem *pio, unsigned pin, u32 *div);
Boris BREZILLON77966ad2013-09-13 09:45:33 +0200179 void (*set_debounce)(void __iomem *pio, unsigned mask, bool is_on, u32 div);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800180 bool (*get_pulldown)(void __iomem *pio, unsigned pin);
Boris BREZILLON77966ad2013-09-13 09:45:33 +0200181 void (*set_pulldown)(void __iomem *pio, unsigned mask, bool is_on);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800182 bool (*get_schmitt_trig)(void __iomem *pio, unsigned pin);
183 void (*disable_schmitt_trig)(void __iomem *pio, unsigned mask);
Marek Roszko4334ac22014-08-23 23:12:04 -0400184 unsigned (*get_drivestrength)(void __iomem *pio, unsigned pin);
185 void (*set_drivestrength)(void __iomem *pio, unsigned pin,
186 u32 strength);
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000187 unsigned (*get_slewrate)(void __iomem *pio, unsigned pin);
188 void (*set_slewrate)(void __iomem *pio, unsigned pin, u32 slewrate);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800189 /* irq */
190 int (*irq_type)(struct irq_data *d, unsigned type);
191};
192
193static int gpio_irq_type(struct irq_data *d, unsigned type);
194static int alt_gpio_irq_type(struct irq_data *d, unsigned type);
195
196struct at91_pinctrl {
197 struct device *dev;
198 struct pinctrl_dev *pctl;
199
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +0100200 int nactive_banks;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800201
202 uint32_t *mux_mask;
203 int nmux;
204
205 struct at91_pmx_func *functions;
206 int nfunctions;
207
208 struct at91_pin_group *groups;
209 int ngroups;
210
211 struct at91_pinctrl_mux_ops *ops;
212};
213
Arnd Bergmann56411f32016-06-13 17:18:34 +0200214static inline const struct at91_pin_group *at91_pinctrl_find_group_by_name(
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800215 const struct at91_pinctrl *info,
216 const char *name)
217{
218 const struct at91_pin_group *grp = NULL;
219 int i;
220
221 for (i = 0; i < info->ngroups; i++) {
222 if (strcmp(info->groups[i].name, name))
223 continue;
224
225 grp = &info->groups[i];
226 dev_dbg(info->dev, "%s: %d 0:%d\n", name, grp->npins, grp->pins[0]);
227 break;
228 }
229
230 return grp;
231}
232
233static int at91_get_groups_count(struct pinctrl_dev *pctldev)
234{
235 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
236
237 return info->ngroups;
238}
239
240static const char *at91_get_group_name(struct pinctrl_dev *pctldev,
241 unsigned selector)
242{
243 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
244
245 return info->groups[selector].name;
246}
247
248static int at91_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
249 const unsigned **pins,
250 unsigned *npins)
251{
252 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
253
254 if (selector >= info->ngroups)
255 return -EINVAL;
256
257 *pins = info->groups[selector].pins;
258 *npins = info->groups[selector].npins;
259
260 return 0;
261}
262
263static void at91_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
264 unsigned offset)
265{
266 seq_printf(s, "%s", dev_name(pctldev->dev));
267}
268
269static int at91_dt_node_to_map(struct pinctrl_dev *pctldev,
270 struct device_node *np,
271 struct pinctrl_map **map, unsigned *num_maps)
272{
273 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
274 const struct at91_pin_group *grp;
275 struct pinctrl_map *new_map;
276 struct device_node *parent;
277 int map_num = 1;
278 int i;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800279
280 /*
Alexandre Belloni61e310a2013-10-16 16:12:33 +0200281 * first find the group of this node and check if we need to create
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800282 * config maps for pins
283 */
284 grp = at91_pinctrl_find_group_by_name(info, np->name);
285 if (!grp) {
Rob Herring94f4e542018-08-27 20:52:41 -0500286 dev_err(info->dev, "unable to find group for node %pOFn\n",
287 np);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800288 return -EINVAL;
289 }
290
291 map_num += grp->npins;
Kees Cooka86854d2018-06-12 14:07:58 -0700292 new_map = devm_kcalloc(pctldev->dev, map_num, sizeof(*new_map),
293 GFP_KERNEL);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800294 if (!new_map)
295 return -ENOMEM;
296
297 *map = new_map;
298 *num_maps = map_num;
299
300 /* create mux map */
301 parent = of_get_parent(np);
302 if (!parent) {
Julia Lawallc62b2b32012-12-12 15:22:44 +0100303 devm_kfree(pctldev->dev, new_map);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800304 return -EINVAL;
305 }
306 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
307 new_map[0].data.mux.function = parent->name;
308 new_map[0].data.mux.group = np->name;
309 of_node_put(parent);
310
311 /* create config map */
312 new_map++;
313 for (i = 0; i < grp->npins; i++) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800314 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
315 new_map[i].data.configs.group_or_pin =
316 pin_get_name(pctldev, grp->pins[i]);
317 new_map[i].data.configs.configs = &grp->pins_conf[i].conf;
318 new_map[i].data.configs.num_configs = 1;
319 }
320
321 dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
322 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
323
324 return 0;
325}
326
327static void at91_dt_free_map(struct pinctrl_dev *pctldev,
328 struct pinctrl_map *map, unsigned num_maps)
329{
330}
331
Laurent Pinchart022ab142013-02-16 10:25:07 +0100332static const struct pinctrl_ops at91_pctrl_ops = {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800333 .get_groups_count = at91_get_groups_count,
334 .get_group_name = at91_get_group_name,
335 .get_group_pins = at91_get_group_pins,
336 .pin_dbg_show = at91_pin_dbg_show,
337 .dt_node_to_map = at91_dt_node_to_map,
338 .dt_free_map = at91_dt_free_map,
339};
340
Sachin Kamat3c936002013-03-15 10:07:03 +0530341static void __iomem *pin_to_controller(struct at91_pinctrl *info,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800342 unsigned int bank)
343{
David Dueck1ab36382015-07-28 09:48:16 +0200344 if (!gpio_chips[bank])
345 return NULL;
346
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800347 return gpio_chips[bank]->regbase;
348}
349
350static inline int pin_to_bank(unsigned pin)
351{
352 return pin /= MAX_NB_GPIO_PER_BANK;
353}
354
355static unsigned pin_to_mask(unsigned int pin)
356{
357 return 1 << pin;
358}
359
Marek Roszko4334ac22014-08-23 23:12:04 -0400360static unsigned two_bit_pin_value_shift_amount(unsigned int pin)
361{
362 /* return the shift value for a pin for "two bit" per pin registers,
363 * i.e. drive strength */
364 return 2*((pin >= MAX_NB_GPIO_PER_BANK/2)
365 ? pin - MAX_NB_GPIO_PER_BANK/2 : pin);
366}
367
368static unsigned sama5d3_get_drive_register(unsigned int pin)
369{
370 /* drive strength is split between two registers
371 * with two bits per pin */
372 return (pin >= MAX_NB_GPIO_PER_BANK/2)
373 ? SAMA5D3_PIO_DRIVER2 : SAMA5D3_PIO_DRIVER1;
374}
375
376static unsigned at91sam9x5_get_drive_register(unsigned int pin)
377{
378 /* drive strength is split between two registers
379 * with two bits per pin */
380 return (pin >= MAX_NB_GPIO_PER_BANK/2)
381 ? AT91SAM9X5_PIO_DRIVER2 : AT91SAM9X5_PIO_DRIVER1;
382}
383
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800384static void at91_mux_disable_interrupt(void __iomem *pio, unsigned mask)
385{
386 writel_relaxed(mask, pio + PIO_IDR);
387}
388
389static unsigned at91_mux_get_pullup(void __iomem *pio, unsigned pin)
390{
Boris BREZILLON05d35342013-08-27 15:19:21 +0200391 return !((readl_relaxed(pio + PIO_PUSR) >> pin) & 0x1);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800392}
393
394static void at91_mux_set_pullup(void __iomem *pio, unsigned mask, bool on)
395{
Wenyou Yang3d784272014-09-11 16:40:15 +0200396 if (on)
397 writel_relaxed(mask, pio + PIO_PPDDR);
398
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800399 writel_relaxed(mask, pio + (on ? PIO_PUER : PIO_PUDR));
400}
401
Boris BREZILLON96bb12d2016-10-28 15:54:10 +0800402static bool at91_mux_get_output(void __iomem *pio, unsigned int pin, bool *val)
403{
404 *val = (readl_relaxed(pio + PIO_ODSR) >> pin) & 0x1;
405 return (readl_relaxed(pio + PIO_OSR) >> pin) & 0x1;
406}
407
408static void at91_mux_set_output(void __iomem *pio, unsigned int mask,
409 bool is_on, bool val)
410{
411 writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
412 writel_relaxed(mask, pio + (is_on ? PIO_OER : PIO_ODR));
413}
414
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800415static unsigned at91_mux_get_multidrive(void __iomem *pio, unsigned pin)
416{
417 return (readl_relaxed(pio + PIO_MDSR) >> pin) & 0x1;
418}
419
420static void at91_mux_set_multidrive(void __iomem *pio, unsigned mask, bool on)
421{
422 writel_relaxed(mask, pio + (on ? PIO_MDER : PIO_MDDR));
423}
424
425static void at91_mux_set_A_periph(void __iomem *pio, unsigned mask)
426{
427 writel_relaxed(mask, pio + PIO_ASR);
428}
429
430static void at91_mux_set_B_periph(void __iomem *pio, unsigned mask)
431{
432 writel_relaxed(mask, pio + PIO_BSR);
433}
434
435static void at91_mux_pio3_set_A_periph(void __iomem *pio, unsigned mask)
436{
437
438 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask,
439 pio + PIO_ABCDSR1);
440 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
441 pio + PIO_ABCDSR2);
442}
443
444static void at91_mux_pio3_set_B_periph(void __iomem *pio, unsigned mask)
445{
446 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask,
447 pio + PIO_ABCDSR1);
448 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
449 pio + PIO_ABCDSR2);
450}
451
452static void at91_mux_pio3_set_C_periph(void __iomem *pio, unsigned mask)
453{
454 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask, pio + PIO_ABCDSR1);
455 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
456}
457
458static void at91_mux_pio3_set_D_periph(void __iomem *pio, unsigned mask)
459{
460 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask, pio + PIO_ABCDSR1);
461 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
462}
463
464static enum at91_mux at91_mux_pio3_get_periph(void __iomem *pio, unsigned mask)
465{
466 unsigned select;
467
468 if (readl_relaxed(pio + PIO_PSR) & mask)
469 return AT91_MUX_GPIO;
470
471 select = !!(readl_relaxed(pio + PIO_ABCDSR1) & mask);
472 select |= (!!(readl_relaxed(pio + PIO_ABCDSR2) & mask) << 1);
473
474 return select + 1;
475}
476
477static enum at91_mux at91_mux_get_periph(void __iomem *pio, unsigned mask)
478{
479 unsigned select;
480
481 if (readl_relaxed(pio + PIO_PSR) & mask)
482 return AT91_MUX_GPIO;
483
484 select = readl_relaxed(pio + PIO_ABSR) & mask;
485
486 return select + 1;
487}
488
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800489static bool at91_mux_get_deglitch(void __iomem *pio, unsigned pin)
490{
Ben Dooksd480239b2015-03-26 12:18:49 +0000491 return (readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1;
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800492}
493
494static void at91_mux_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
495{
Ben Dooksd480239b2015-03-26 12:18:49 +0000496 writel_relaxed(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800497}
498
Boris BREZILLONc8dba022013-09-13 09:47:22 +0200499static bool at91_mux_pio3_get_deglitch(void __iomem *pio, unsigned pin)
500{
Ben Dooksd480239b2015-03-26 12:18:49 +0000501 if ((readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1)
502 return !((readl_relaxed(pio + PIO_IFSCSR) >> pin) & 0x1);
Boris BREZILLONc8dba022013-09-13 09:47:22 +0200503
504 return false;
505}
506
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800507static void at91_mux_pio3_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
508{
509 if (is_on)
Ben Dooksd480239b2015-03-26 12:18:49 +0000510 writel_relaxed(mask, pio + PIO_IFSCDR);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800511 at91_mux_set_deglitch(pio, mask, is_on);
512}
513
514static bool at91_mux_pio3_get_debounce(void __iomem *pio, unsigned pin, u32 *div)
515{
Ben Dooksd480239b2015-03-26 12:18:49 +0000516 *div = readl_relaxed(pio + PIO_SCDR);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800517
Ben Dooksd480239b2015-03-26 12:18:49 +0000518 return ((readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1) &&
519 ((readl_relaxed(pio + PIO_IFSCSR) >> pin) & 0x1);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800520}
521
522static void at91_mux_pio3_set_debounce(void __iomem *pio, unsigned mask,
523 bool is_on, u32 div)
524{
525 if (is_on) {
Ben Dooksd480239b2015-03-26 12:18:49 +0000526 writel_relaxed(mask, pio + PIO_IFSCER);
527 writel_relaxed(div & PIO_SCDR_DIV, pio + PIO_SCDR);
528 writel_relaxed(mask, pio + PIO_IFER);
Boris BREZILLONc8dba022013-09-13 09:47:22 +0200529 } else
Ben Dooksd480239b2015-03-26 12:18:49 +0000530 writel_relaxed(mask, pio + PIO_IFSCDR);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800531}
532
533static bool at91_mux_pio3_get_pulldown(void __iomem *pio, unsigned pin)
534{
Ben Dooksd480239b2015-03-26 12:18:49 +0000535 return !((readl_relaxed(pio + PIO_PPDSR) >> pin) & 0x1);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800536}
537
538static void at91_mux_pio3_set_pulldown(void __iomem *pio, unsigned mask, bool is_on)
539{
Wenyou Yang3d784272014-09-11 16:40:15 +0200540 if (is_on)
Ben Dooksd480239b2015-03-26 12:18:49 +0000541 writel_relaxed(mask, pio + PIO_PUDR);
Wenyou Yang3d784272014-09-11 16:40:15 +0200542
Ben Dooksd480239b2015-03-26 12:18:49 +0000543 writel_relaxed(mask, pio + (is_on ? PIO_PPDER : PIO_PPDDR));
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800544}
545
546static void at91_mux_pio3_disable_schmitt_trig(void __iomem *pio, unsigned mask)
547{
Ben Dooksd480239b2015-03-26 12:18:49 +0000548 writel_relaxed(readl_relaxed(pio + PIO_SCHMITT) | mask, pio + PIO_SCHMITT);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800549}
550
551static bool at91_mux_pio3_get_schmitt_trig(void __iomem *pio, unsigned pin)
552{
Ben Dooksd480239b2015-03-26 12:18:49 +0000553 return (readl_relaxed(pio + PIO_SCHMITT) >> pin) & 0x1;
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800554}
555
Marek Roszko4334ac22014-08-23 23:12:04 -0400556static inline u32 read_drive_strength(void __iomem *reg, unsigned pin)
557{
Ben Dooksd480239b2015-03-26 12:18:49 +0000558 unsigned tmp = readl_relaxed(reg);
Marek Roszko4334ac22014-08-23 23:12:04 -0400559
560 tmp = tmp >> two_bit_pin_value_shift_amount(pin);
561
562 return tmp & DRIVE_STRENGTH_MASK;
563}
564
565static unsigned at91_mux_sama5d3_get_drivestrength(void __iomem *pio,
566 unsigned pin)
567{
568 unsigned tmp = read_drive_strength(pio +
569 sama5d3_get_drive_register(pin), pin);
570
571 /* SAMA5 strength is 1:1 with our defines,
572 * except 0 is equivalent to low per datasheet */
573 if (!tmp)
Claudiu Bezneab67328e2019-02-07 09:24:46 +0000574 tmp = DRIVE_STRENGTH_BIT_MSK(LOW);
Marek Roszko4334ac22014-08-23 23:12:04 -0400575
576 return tmp;
577}
578
579static unsigned at91_mux_sam9x5_get_drivestrength(void __iomem *pio,
580 unsigned pin)
581{
582 unsigned tmp = read_drive_strength(pio +
583 at91sam9x5_get_drive_register(pin), pin);
584
585 /* strength is inverse in SAM9x5s hardware with the pinctrl defines
586 * hardware: 0 = hi, 1 = med, 2 = low, 3 = rsvd */
Claudiu Bezneab67328e2019-02-07 09:24:46 +0000587 tmp = DRIVE_STRENGTH_BIT_MSK(HI) - tmp;
Marek Roszko4334ac22014-08-23 23:12:04 -0400588
589 return tmp;
590}
591
Claudiu Beznea42ef7552019-02-07 09:24:49 +0000592static unsigned at91_mux_sam9x60_get_drivestrength(void __iomem *pio,
593 unsigned pin)
594{
595 unsigned tmp = readl_relaxed(pio + SAM9X60_PIO_DRIVER1);
596
597 if (tmp & BIT(pin))
598 return DRIVE_STRENGTH_BIT_HI;
599
600 return DRIVE_STRENGTH_BIT_LOW;
601}
602
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000603static unsigned at91_mux_sam9x60_get_slewrate(void __iomem *pio, unsigned pin)
604{
605 unsigned tmp = readl_relaxed(pio + SAM9X60_PIO_SLEWR);
606
607 if ((tmp & BIT(pin)))
608 return SLEWRATE_BIT_ENA;
609
610 return SLEWRATE_BIT_DIS;
611}
612
Marek Roszko4334ac22014-08-23 23:12:04 -0400613static void set_drive_strength(void __iomem *reg, unsigned pin, u32 strength)
614{
Ben Dooksd480239b2015-03-26 12:18:49 +0000615 unsigned tmp = readl_relaxed(reg);
Marek Roszko4334ac22014-08-23 23:12:04 -0400616 unsigned shift = two_bit_pin_value_shift_amount(pin);
617
618 tmp &= ~(DRIVE_STRENGTH_MASK << shift);
619 tmp |= strength << shift;
620
Ben Dooksd480239b2015-03-26 12:18:49 +0000621 writel_relaxed(tmp, reg);
Marek Roszko4334ac22014-08-23 23:12:04 -0400622}
623
624static void at91_mux_sama5d3_set_drivestrength(void __iomem *pio, unsigned pin,
625 u32 setting)
626{
627 /* do nothing if setting is zero */
628 if (!setting)
629 return;
630
631 /* strength is 1 to 1 with setting for SAMA5 */
632 set_drive_strength(pio + sama5d3_get_drive_register(pin), pin, setting);
633}
634
635static void at91_mux_sam9x5_set_drivestrength(void __iomem *pio, unsigned pin,
636 u32 setting)
637{
638 /* do nothing if setting is zero */
639 if (!setting)
640 return;
641
642 /* strength is inverse on SAM9x5s with our defines
643 * 0 = hi, 1 = med, 2 = low, 3 = rsvd */
Claudiu Bezneab67328e2019-02-07 09:24:46 +0000644 setting = DRIVE_STRENGTH_BIT_MSK(HI) - setting;
Marek Roszko4334ac22014-08-23 23:12:04 -0400645
646 set_drive_strength(pio + at91sam9x5_get_drive_register(pin), pin,
647 setting);
648}
649
Claudiu Beznea42ef7552019-02-07 09:24:49 +0000650static void at91_mux_sam9x60_set_drivestrength(void __iomem *pio, unsigned pin,
651 u32 setting)
652{
653 unsigned int tmp;
654
655 if (setting <= DRIVE_STRENGTH_BIT_DEF ||
656 setting == DRIVE_STRENGTH_BIT_MED ||
657 setting > DRIVE_STRENGTH_BIT_HI)
658 return;
659
660 tmp = readl_relaxed(pio + SAM9X60_PIO_DRIVER1);
661
662 /* Strength is 0: low, 1: hi */
663 if (setting == DRIVE_STRENGTH_BIT_LOW)
664 tmp &= ~BIT(pin);
665 else
666 tmp |= BIT(pin);
667
668 writel_relaxed(tmp, pio + SAM9X60_PIO_DRIVER1);
669}
670
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000671static void at91_mux_sam9x60_set_slewrate(void __iomem *pio, unsigned pin,
672 u32 setting)
673{
674 unsigned int tmp;
675
Codrin Ciubotariu0b329282019-11-01 11:20:31 +0200676 if (setting < SLEWRATE_BIT_ENA || setting > SLEWRATE_BIT_DIS)
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000677 return;
678
679 tmp = readl_relaxed(pio + SAM9X60_PIO_SLEWR);
680
681 if (setting == SLEWRATE_BIT_DIS)
682 tmp &= ~BIT(pin);
683 else
684 tmp |= BIT(pin);
685
686 writel_relaxed(tmp, pio + SAM9X60_PIO_SLEWR);
687}
688
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800689static struct at91_pinctrl_mux_ops at91rm9200_ops = {
690 .get_periph = at91_mux_get_periph,
691 .mux_A_periph = at91_mux_set_A_periph,
692 .mux_B_periph = at91_mux_set_B_periph,
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800693 .get_deglitch = at91_mux_get_deglitch,
694 .set_deglitch = at91_mux_set_deglitch,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800695 .irq_type = gpio_irq_type,
696};
697
698static struct at91_pinctrl_mux_ops at91sam9x5_ops = {
699 .get_periph = at91_mux_pio3_get_periph,
700 .mux_A_periph = at91_mux_pio3_set_A_periph,
701 .mux_B_periph = at91_mux_pio3_set_B_periph,
702 .mux_C_periph = at91_mux_pio3_set_C_periph,
703 .mux_D_periph = at91_mux_pio3_set_D_periph,
Boris BREZILLONc8dba022013-09-13 09:47:22 +0200704 .get_deglitch = at91_mux_pio3_get_deglitch,
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800705 .set_deglitch = at91_mux_pio3_set_deglitch,
706 .get_debounce = at91_mux_pio3_get_debounce,
707 .set_debounce = at91_mux_pio3_set_debounce,
708 .get_pulldown = at91_mux_pio3_get_pulldown,
709 .set_pulldown = at91_mux_pio3_set_pulldown,
710 .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
711 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
Marek Roszko4334ac22014-08-23 23:12:04 -0400712 .get_drivestrength = at91_mux_sam9x5_get_drivestrength,
713 .set_drivestrength = at91_mux_sam9x5_set_drivestrength,
714 .irq_type = alt_gpio_irq_type,
715};
716
Claudiu Beznea42ef7552019-02-07 09:24:49 +0000717static const struct at91_pinctrl_mux_ops sam9x60_ops = {
718 .get_periph = at91_mux_pio3_get_periph,
719 .mux_A_periph = at91_mux_pio3_set_A_periph,
720 .mux_B_periph = at91_mux_pio3_set_B_periph,
721 .mux_C_periph = at91_mux_pio3_set_C_periph,
722 .mux_D_periph = at91_mux_pio3_set_D_periph,
723 .get_deglitch = at91_mux_pio3_get_deglitch,
724 .set_deglitch = at91_mux_pio3_set_deglitch,
725 .get_debounce = at91_mux_pio3_get_debounce,
726 .set_debounce = at91_mux_pio3_set_debounce,
727 .get_pulldown = at91_mux_pio3_get_pulldown,
728 .set_pulldown = at91_mux_pio3_set_pulldown,
729 .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
730 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
731 .get_drivestrength = at91_mux_sam9x60_get_drivestrength,
732 .set_drivestrength = at91_mux_sam9x60_set_drivestrength,
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000733 .get_slewrate = at91_mux_sam9x60_get_slewrate,
734 .set_slewrate = at91_mux_sam9x60_set_slewrate,
Claudiu Beznea42ef7552019-02-07 09:24:49 +0000735 .irq_type = alt_gpio_irq_type,
736
737};
738
Marek Roszko4334ac22014-08-23 23:12:04 -0400739static struct at91_pinctrl_mux_ops sama5d3_ops = {
740 .get_periph = at91_mux_pio3_get_periph,
741 .mux_A_periph = at91_mux_pio3_set_A_periph,
742 .mux_B_periph = at91_mux_pio3_set_B_periph,
743 .mux_C_periph = at91_mux_pio3_set_C_periph,
744 .mux_D_periph = at91_mux_pio3_set_D_periph,
745 .get_deglitch = at91_mux_pio3_get_deglitch,
746 .set_deglitch = at91_mux_pio3_set_deglitch,
747 .get_debounce = at91_mux_pio3_get_debounce,
748 .set_debounce = at91_mux_pio3_set_debounce,
749 .get_pulldown = at91_mux_pio3_get_pulldown,
750 .set_pulldown = at91_mux_pio3_set_pulldown,
751 .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
752 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
753 .get_drivestrength = at91_mux_sama5d3_get_drivestrength,
754 .set_drivestrength = at91_mux_sama5d3_set_drivestrength,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800755 .irq_type = alt_gpio_irq_type,
756};
757
758static void at91_pin_dbg(const struct device *dev, const struct at91_pmx_pin *pin)
759{
760 if (pin->mux) {
Hans Wennborg4b6fe452014-08-05 21:43:16 -0700761 dev_dbg(dev, "pio%c%d configured as periph%c with conf = 0x%lx\n",
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800762 pin->bank + 'A', pin->pin, pin->mux - 1 + 'A', pin->conf);
763 } else {
Hans Wennborg4b6fe452014-08-05 21:43:16 -0700764 dev_dbg(dev, "pio%c%d configured as gpio with conf = 0x%lx\n",
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800765 pin->bank + 'A', pin->pin, pin->conf);
766 }
767}
768
Sachin Kamat3c936002013-03-15 10:07:03 +0530769static int pin_check_config(struct at91_pinctrl *info, const char *name,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800770 int index, const struct at91_pmx_pin *pin)
771{
772 int mux;
773
774 /* check if it's a valid config */
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +0100775 if (pin->bank >= gpio_banks) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800776 dev_err(info->dev, "%s: pin conf %d bank_id %d >= nbanks %d\n",
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +0100777 name, index, pin->bank, gpio_banks);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800778 return -EINVAL;
779 }
780
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +0100781 if (!gpio_chips[pin->bank]) {
782 dev_err(info->dev, "%s: pin conf %d bank_id %d not enabled\n",
783 name, index, pin->bank);
784 return -ENXIO;
785 }
786
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800787 if (pin->pin >= MAX_NB_GPIO_PER_BANK) {
788 dev_err(info->dev, "%s: pin conf %d pin_bank_id %d >= %d\n",
789 name, index, pin->pin, MAX_NB_GPIO_PER_BANK);
790 return -EINVAL;
791 }
792
793 if (!pin->mux)
794 return 0;
795
796 mux = pin->mux - 1;
797
798 if (mux >= info->nmux) {
799 dev_err(info->dev, "%s: pin conf %d mux_id %d >= nmux %d\n",
800 name, index, mux, info->nmux);
801 return -EINVAL;
802 }
803
804 if (!(info->mux_mask[pin->bank * info->nmux + mux] & 1 << pin->pin)) {
805 dev_err(info->dev, "%s: pin conf %d mux_id %d not supported for pio%c%d\n",
806 name, index, mux, pin->bank + 'A', pin->pin);
807 return -EINVAL;
808 }
809
810 return 0;
811}
812
813static void at91_mux_gpio_disable(void __iomem *pio, unsigned mask)
814{
815 writel_relaxed(mask, pio + PIO_PDR);
816}
817
818static void at91_mux_gpio_enable(void __iomem *pio, unsigned mask, bool input)
819{
820 writel_relaxed(mask, pio + PIO_PER);
821 writel_relaxed(mask, pio + (input ? PIO_ODR : PIO_OER));
822}
823
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200824static int at91_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
825 unsigned group)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800826{
827 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
828 const struct at91_pmx_pin *pins_conf = info->groups[group].pins_conf;
829 const struct at91_pmx_pin *pin;
830 uint32_t npins = info->groups[group].npins;
831 int i, ret;
832 unsigned mask;
833 void __iomem *pio;
834
835 dev_dbg(info->dev, "enable function %s group %s\n",
836 info->functions[selector].name, info->groups[group].name);
837
838 /* first check that all the pins of the group are valid with a valid
Alexandre Belloni61e310a2013-10-16 16:12:33 +0200839 * parameter */
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800840 for (i = 0; i < npins; i++) {
841 pin = &pins_conf[i];
842 ret = pin_check_config(info, info->groups[group].name, i, pin);
843 if (ret)
844 return ret;
845 }
846
847 for (i = 0; i < npins; i++) {
848 pin = &pins_conf[i];
849 at91_pin_dbg(info->dev, pin);
850 pio = pin_to_controller(info, pin->bank);
David Dueck1ab36382015-07-28 09:48:16 +0200851
852 if (!pio)
853 continue;
854
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800855 mask = pin_to_mask(pin->pin);
856 at91_mux_disable_interrupt(pio, mask);
Sachin Kamat3c936002013-03-15 10:07:03 +0530857 switch (pin->mux) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800858 case AT91_MUX_GPIO:
859 at91_mux_gpio_enable(pio, mask, 1);
860 break;
861 case AT91_MUX_PERIPH_A:
862 info->ops->mux_A_periph(pio, mask);
863 break;
864 case AT91_MUX_PERIPH_B:
865 info->ops->mux_B_periph(pio, mask);
866 break;
867 case AT91_MUX_PERIPH_C:
868 if (!info->ops->mux_C_periph)
869 return -EINVAL;
870 info->ops->mux_C_periph(pio, mask);
871 break;
872 case AT91_MUX_PERIPH_D:
873 if (!info->ops->mux_D_periph)
874 return -EINVAL;
875 info->ops->mux_D_periph(pio, mask);
876 break;
877 }
878 if (pin->mux)
879 at91_mux_gpio_disable(pio, mask);
880 }
881
882 return 0;
883}
884
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800885static int at91_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
886{
887 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
888
889 return info->nfunctions;
890}
891
892static const char *at91_pmx_get_func_name(struct pinctrl_dev *pctldev,
893 unsigned selector)
894{
895 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
896
897 return info->functions[selector].name;
898}
899
900static int at91_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
901 const char * const **groups,
902 unsigned * const num_groups)
903{
904 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
905
906 *groups = info->functions[selector].groups;
907 *num_groups = info->functions[selector].ngroups;
908
909 return 0;
910}
911
Axel Linf6f94f62012-11-05 21:23:50 +0800912static int at91_gpio_request_enable(struct pinctrl_dev *pctldev,
913 struct pinctrl_gpio_range *range,
914 unsigned offset)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800915{
916 struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
917 struct at91_gpio_chip *at91_chip;
918 struct gpio_chip *chip;
919 unsigned mask;
920
921 if (!range) {
922 dev_err(npct->dev, "invalid range\n");
923 return -EINVAL;
924 }
925 if (!range->gc) {
926 dev_err(npct->dev, "missing GPIO chip in range\n");
927 return -EINVAL;
928 }
929 chip = range->gc;
Linus Walleij370ea612015-12-08 09:27:45 +0100930 at91_chip = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800931
932 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
933
934 mask = 1 << (offset - chip->base);
935
936 dev_dbg(npct->dev, "enable pin %u as PIO%c%d 0x%x\n",
937 offset, 'A' + range->id, offset - chip->base, mask);
938
939 writel_relaxed(mask, at91_chip->regbase + PIO_PER);
940
941 return 0;
942}
943
Axel Linf6f94f62012-11-05 21:23:50 +0800944static void at91_gpio_disable_free(struct pinctrl_dev *pctldev,
945 struct pinctrl_gpio_range *range,
946 unsigned offset)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800947{
948 struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
949
950 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
951 /* Set the pin to some default state, GPIO is usually default */
952}
953
Laurent Pinchart022ab142013-02-16 10:25:07 +0100954static const struct pinmux_ops at91_pmx_ops = {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800955 .get_functions_count = at91_pmx_get_funcs_count,
956 .get_function_name = at91_pmx_get_func_name,
957 .get_function_groups = at91_pmx_get_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200958 .set_mux = at91_pmx_set,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800959 .gpio_request_enable = at91_gpio_request_enable,
960 .gpio_disable_free = at91_gpio_disable_free,
961};
962
963static int at91_pinconf_get(struct pinctrl_dev *pctldev,
964 unsigned pin_id, unsigned long *config)
965{
966 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
967 void __iomem *pio;
968 unsigned pin;
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800969 int div;
Boris BREZILLON96bb12d2016-10-28 15:54:10 +0800970 bool out;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800971
Alexandre Belloni1292e692013-12-07 14:08:53 +0100972 *config = 0;
973 dev_dbg(info->dev, "%s:%d, pin_id=%d", __func__, __LINE__, pin_id);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800974 pio = pin_to_controller(info, pin_to_bank(pin_id));
David Dueck1ab36382015-07-28 09:48:16 +0200975
976 if (!pio)
977 return -EINVAL;
978
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800979 pin = pin_id % MAX_NB_GPIO_PER_BANK;
980
981 if (at91_mux_get_multidrive(pio, pin))
982 *config |= MULTI_DRIVE;
983
984 if (at91_mux_get_pullup(pio, pin))
985 *config |= PULL_UP;
986
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800987 if (info->ops->get_deglitch && info->ops->get_deglitch(pio, pin))
988 *config |= DEGLITCH;
989 if (info->ops->get_debounce && info->ops->get_debounce(pio, pin, &div))
990 *config |= DEBOUNCE | (div << DEBOUNCE_VAL_SHIFT);
991 if (info->ops->get_pulldown && info->ops->get_pulldown(pio, pin))
992 *config |= PULL_DOWN;
993 if (info->ops->get_schmitt_trig && info->ops->get_schmitt_trig(pio, pin))
994 *config |= DIS_SCHMIT;
Marek Roszko4334ac22014-08-23 23:12:04 -0400995 if (info->ops->get_drivestrength)
996 *config |= (info->ops->get_drivestrength(pio, pin)
997 << DRIVE_STRENGTH_SHIFT);
Claudiu Beznea64e21ad2019-02-07 09:25:05 +0000998 if (info->ops->get_slewrate)
999 *config |= (info->ops->get_slewrate(pio, pin) << SLEWRATE_SHIFT);
Boris BREZILLON96bb12d2016-10-28 15:54:10 +08001000 if (at91_mux_get_output(pio, pin, &out))
1001 *config |= OUTPUT | (out << OUTPUT_VAL_SHIFT);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +08001002
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001003 return 0;
1004}
1005
1006static int at91_pinconf_set(struct pinctrl_dev *pctldev,
Sherman Yin03b054e2013-08-27 11:32:12 -07001007 unsigned pin_id, unsigned long *configs,
1008 unsigned num_configs)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001009{
1010 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
1011 unsigned mask;
1012 void __iomem *pio;
Sherman Yin03b054e2013-08-27 11:32:12 -07001013 int i;
1014 unsigned long config;
Marek Roszko4334ac22014-08-23 23:12:04 -04001015 unsigned pin;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001016
Sherman Yin03b054e2013-08-27 11:32:12 -07001017 for (i = 0; i < num_configs; i++) {
1018 config = configs[i];
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001019
Sherman Yin03b054e2013-08-27 11:32:12 -07001020 dev_dbg(info->dev,
1021 "%s:%d, pin_id=%d, config=0x%lx",
1022 __func__, __LINE__, pin_id, config);
1023 pio = pin_to_controller(info, pin_to_bank(pin_id));
David Dueck1ab36382015-07-28 09:48:16 +02001024
1025 if (!pio)
1026 return -EINVAL;
1027
Marek Roszko4334ac22014-08-23 23:12:04 -04001028 pin = pin_id % MAX_NB_GPIO_PER_BANK;
1029 mask = pin_to_mask(pin);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +08001030
Sherman Yin03b054e2013-08-27 11:32:12 -07001031 if (config & PULL_UP && config & PULL_DOWN)
1032 return -EINVAL;
1033
Boris BREZILLON96bb12d2016-10-28 15:54:10 +08001034 at91_mux_set_output(pio, mask, config & OUTPUT,
1035 (config & OUTPUT_VAL) >> OUTPUT_VAL_SHIFT);
Sherman Yin03b054e2013-08-27 11:32:12 -07001036 at91_mux_set_pullup(pio, mask, config & PULL_UP);
1037 at91_mux_set_multidrive(pio, mask, config & MULTI_DRIVE);
1038 if (info->ops->set_deglitch)
1039 info->ops->set_deglitch(pio, mask, config & DEGLITCH);
1040 if (info->ops->set_debounce)
1041 info->ops->set_debounce(pio, mask, config & DEBOUNCE,
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +08001042 (config & DEBOUNCE_VAL) >> DEBOUNCE_VAL_SHIFT);
Sherman Yin03b054e2013-08-27 11:32:12 -07001043 if (info->ops->set_pulldown)
1044 info->ops->set_pulldown(pio, mask, config & PULL_DOWN);
1045 if (info->ops->disable_schmitt_trig && config & DIS_SCHMIT)
1046 info->ops->disable_schmitt_trig(pio, mask);
Marek Roszko4334ac22014-08-23 23:12:04 -04001047 if (info->ops->set_drivestrength)
1048 info->ops->set_drivestrength(pio, pin,
1049 (config & DRIVE_STRENGTH)
1050 >> DRIVE_STRENGTH_SHIFT);
Claudiu Beznea64e21ad2019-02-07 09:25:05 +00001051 if (info->ops->set_slewrate)
1052 info->ops->set_slewrate(pio, pin,
1053 (config & SLEWRATE) >> SLEWRATE_SHIFT);
Sherman Yin03b054e2013-08-27 11:32:12 -07001054
1055 } /* for each config */
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +08001056
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001057 return 0;
1058}
1059
Alexandre Belloni4d9b8a82013-12-07 14:08:54 +01001060#define DBG_SHOW_FLAG(flag) do { \
1061 if (config & flag) { \
1062 if (num_conf) \
1063 seq_puts(s, "|"); \
1064 seq_puts(s, #flag); \
1065 num_conf++; \
1066 } \
1067} while (0)
1068
Claudiu Bezneab67328e2019-02-07 09:24:46 +00001069#define DBG_SHOW_FLAG_MASKED(mask, flag, name) do { \
Marek Roszko4334ac22014-08-23 23:12:04 -04001070 if ((config & mask) == flag) { \
1071 if (num_conf) \
1072 seq_puts(s, "|"); \
Claudiu Bezneab67328e2019-02-07 09:24:46 +00001073 seq_puts(s, #name); \
Marek Roszko4334ac22014-08-23 23:12:04 -04001074 num_conf++; \
1075 } \
1076} while (0)
1077
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001078static void at91_pinconf_dbg_show(struct pinctrl_dev *pctldev,
1079 struct seq_file *s, unsigned pin_id)
1080{
Alexandre Belloni4d9b8a82013-12-07 14:08:54 +01001081 unsigned long config;
Rickard Strandqvist445d2022014-06-26 15:41:31 +02001082 int val, num_conf = 0;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001083
Rickard Strandqvist445d2022014-06-26 15:41:31 +02001084 at91_pinconf_get(pctldev, pin_id, &config);
Alexandre Belloni4d9b8a82013-12-07 14:08:54 +01001085
1086 DBG_SHOW_FLAG(MULTI_DRIVE);
1087 DBG_SHOW_FLAG(PULL_UP);
1088 DBG_SHOW_FLAG(PULL_DOWN);
1089 DBG_SHOW_FLAG(DIS_SCHMIT);
1090 DBG_SHOW_FLAG(DEGLITCH);
Claudiu Bezneab67328e2019-02-07 09:24:46 +00001091 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_BIT_MSK(LOW),
1092 DRIVE_STRENGTH_LOW);
1093 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_BIT_MSK(MED),
1094 DRIVE_STRENGTH_MED);
1095 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_BIT_MSK(HI),
1096 DRIVE_STRENGTH_HI);
Claudiu Beznea64e21ad2019-02-07 09:25:05 +00001097 DBG_SHOW_FLAG(SLEWRATE);
Alexandre Belloni4d9b8a82013-12-07 14:08:54 +01001098 DBG_SHOW_FLAG(DEBOUNCE);
1099 if (config & DEBOUNCE) {
1100 val = config >> DEBOUNCE_VAL_SHIFT;
1101 seq_printf(s, "(%d)", val);
1102 }
1103
1104 return;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001105}
1106
1107static void at91_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
1108 struct seq_file *s, unsigned group)
1109{
1110}
1111
Laurent Pinchart022ab142013-02-16 10:25:07 +01001112static const struct pinconf_ops at91_pinconf_ops = {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001113 .pin_config_get = at91_pinconf_get,
1114 .pin_config_set = at91_pinconf_set,
1115 .pin_config_dbg_show = at91_pinconf_dbg_show,
1116 .pin_config_group_dbg_show = at91_pinconf_group_dbg_show,
1117};
1118
1119static struct pinctrl_desc at91_pinctrl_desc = {
1120 .pctlops = &at91_pctrl_ops,
1121 .pmxops = &at91_pmx_ops,
1122 .confops = &at91_pinconf_ops,
1123 .owner = THIS_MODULE,
1124};
1125
1126static const char *gpio_compat = "atmel,at91rm9200-gpio";
1127
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001128static void at91_pinctrl_child_count(struct at91_pinctrl *info,
1129 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001130{
1131 struct device_node *child;
1132
1133 for_each_child_of_node(np, child) {
1134 if (of_device_is_compatible(child, gpio_compat)) {
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001135 if (of_device_is_available(child))
1136 info->nactive_banks++;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001137 } else {
1138 info->nfunctions++;
1139 info->ngroups += of_get_child_count(child);
1140 }
1141 }
1142}
1143
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001144static int at91_pinctrl_mux_mask(struct at91_pinctrl *info,
1145 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001146{
1147 int ret = 0;
1148 int size;
Sachin Kamat1164d732013-03-15 10:07:02 +05301149 const __be32 *list;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001150
1151 list = of_get_property(np, "atmel,mux-mask", &size);
1152 if (!list) {
1153 dev_err(info->dev, "can not read the mux-mask of %d\n", size);
1154 return -EINVAL;
1155 }
1156
1157 size /= sizeof(*list);
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001158 if (!size || size % gpio_banks) {
1159 dev_err(info->dev, "wrong mux mask array should be by %d\n", gpio_banks);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001160 return -EINVAL;
1161 }
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001162 info->nmux = size / gpio_banks;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001163
Kees Cooka86854d2018-06-12 14:07:58 -07001164 info->mux_mask = devm_kcalloc(info->dev, size, sizeof(u32),
1165 GFP_KERNEL);
Markus Elfring3da941b2017-12-23 20:44:27 +01001166 if (!info->mux_mask)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001167 return -ENOMEM;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001168
1169 ret = of_property_read_u32_array(np, "atmel,mux-mask",
1170 info->mux_mask, size);
1171 if (ret)
1172 dev_err(info->dev, "can not read the mux-mask of %d\n", size);
1173 return ret;
1174}
1175
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001176static int at91_pinctrl_parse_groups(struct device_node *np,
1177 struct at91_pin_group *grp,
1178 struct at91_pinctrl *info, u32 index)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001179{
1180 struct at91_pmx_pin *pin;
1181 int size;
Sachin Kamat1164d732013-03-15 10:07:02 +05301182 const __be32 *list;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001183 int i, j;
1184
Rob Herring94f4e542018-08-27 20:52:41 -05001185 dev_dbg(info->dev, "group(%d): %pOFn\n", index, np);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001186
1187 /* Initialise group */
1188 grp->name = np->name;
1189
1190 /*
1191 * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
1192 * do sanity check and calculate pins number
1193 */
1194 list = of_get_property(np, "atmel,pins", &size);
1195 /* we do not check return since it's safe node passed down */
1196 size /= sizeof(*list);
1197 if (!size || size % 4) {
1198 dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
1199 return -EINVAL;
1200 }
1201
1202 grp->npins = size / 4;
Kees Cooka86854d2018-06-12 14:07:58 -07001203 pin = grp->pins_conf = devm_kcalloc(info->dev,
1204 grp->npins,
1205 sizeof(struct at91_pmx_pin),
1206 GFP_KERNEL);
1207 grp->pins = devm_kcalloc(info->dev, grp->npins, sizeof(unsigned int),
1208 GFP_KERNEL);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001209 if (!grp->pins_conf || !grp->pins)
1210 return -ENOMEM;
1211
1212 for (i = 0, j = 0; i < size; i += 4, j++) {
1213 pin->bank = be32_to_cpu(*list++);
1214 pin->pin = be32_to_cpu(*list++);
1215 grp->pins[j] = pin->bank * MAX_NB_GPIO_PER_BANK + pin->pin;
1216 pin->mux = be32_to_cpu(*list++);
1217 pin->conf = be32_to_cpu(*list++);
1218
1219 at91_pin_dbg(info->dev, pin);
1220 pin++;
1221 }
1222
1223 return 0;
1224}
1225
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001226static int at91_pinctrl_parse_functions(struct device_node *np,
1227 struct at91_pinctrl *info, u32 index)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001228{
1229 struct device_node *child;
1230 struct at91_pmx_func *func;
1231 struct at91_pin_group *grp;
1232 int ret;
1233 static u32 grp_index;
1234 u32 i = 0;
1235
Rob Herring94f4e542018-08-27 20:52:41 -05001236 dev_dbg(info->dev, "parse function(%d): %pOFn\n", index, np);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001237
1238 func = &info->functions[index];
1239
1240 /* Initialise function */
1241 func->name = np->name;
1242 func->ngroups = of_get_child_count(np);
Rickard Strandqvistca7162a2014-06-26 13:26:45 +02001243 if (func->ngroups == 0) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001244 dev_err(info->dev, "no groups defined\n");
1245 return -EINVAL;
1246 }
Kees Cooka86854d2018-06-12 14:07:58 -07001247 func->groups = devm_kcalloc(info->dev,
1248 func->ngroups, sizeof(char *), GFP_KERNEL);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001249 if (!func->groups)
1250 return -ENOMEM;
1251
1252 for_each_child_of_node(np, child) {
1253 func->groups[i] = child->name;
1254 grp = &info->groups[grp_index++];
1255 ret = at91_pinctrl_parse_groups(child, grp, info, i++);
Julia Lawalld94b9862015-10-24 16:42:35 +02001256 if (ret) {
1257 of_node_put(child);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001258 return ret;
Julia Lawalld94b9862015-10-24 16:42:35 +02001259 }
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001260 }
1261
1262 return 0;
1263}
1264
Fabian Frederickbaa9946e2015-03-16 20:59:09 +01001265static const struct of_device_id at91_pinctrl_of_match[] = {
Marek Roszko4334ac22014-08-23 23:12:04 -04001266 { .compatible = "atmel,sama5d3-pinctrl", .data = &sama5d3_ops },
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001267 { .compatible = "atmel,at91sam9x5-pinctrl", .data = &at91sam9x5_ops },
1268 { .compatible = "atmel,at91rm9200-pinctrl", .data = &at91rm9200_ops },
Claudiu Bezneaa2fcb1c2019-02-07 09:24:53 +00001269 { .compatible = "microchip,sam9x60-pinctrl", .data = &sam9x60_ops },
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001270 { /* sentinel */ }
1271};
1272
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001273static int at91_pinctrl_probe_dt(struct platform_device *pdev,
1274 struct at91_pinctrl *info)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001275{
1276 int ret = 0;
1277 int i, j;
1278 uint32_t *tmp;
1279 struct device_node *np = pdev->dev.of_node;
1280 struct device_node *child;
1281
1282 if (!np)
1283 return -ENODEV;
1284
1285 info->dev = &pdev->dev;
Sachin Kamat3c936002013-03-15 10:07:03 +05301286 info->ops = (struct at91_pinctrl_mux_ops *)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001287 of_match_device(at91_pinctrl_of_match, &pdev->dev)->data;
1288 at91_pinctrl_child_count(info, np);
1289
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001290 if (gpio_banks < 1) {
Alexandre Belloni61e310a2013-10-16 16:12:33 +02001291 dev_err(&pdev->dev, "you need to specify at least one gpio-controller\n");
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001292 return -EINVAL;
1293 }
1294
1295 ret = at91_pinctrl_mux_mask(info, np);
1296 if (ret)
1297 return ret;
1298
1299 dev_dbg(&pdev->dev, "nmux = %d\n", info->nmux);
1300
1301 dev_dbg(&pdev->dev, "mux-mask\n");
1302 tmp = info->mux_mask;
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001303 for (i = 0; i < gpio_banks; i++) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001304 for (j = 0; j < info->nmux; j++, tmp++) {
1305 dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
1306 }
1307 }
1308
1309 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1310 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
Kees Cooka86854d2018-06-12 14:07:58 -07001311 info->functions = devm_kcalloc(&pdev->dev,
1312 info->nfunctions,
1313 sizeof(struct at91_pmx_func),
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001314 GFP_KERNEL);
1315 if (!info->functions)
1316 return -ENOMEM;
1317
Kees Cooka86854d2018-06-12 14:07:58 -07001318 info->groups = devm_kcalloc(&pdev->dev,
1319 info->ngroups,
1320 sizeof(struct at91_pin_group),
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001321 GFP_KERNEL);
1322 if (!info->groups)
1323 return -ENOMEM;
1324
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001325 dev_dbg(&pdev->dev, "nbanks = %d\n", gpio_banks);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001326 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1327 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
1328
1329 i = 0;
1330
1331 for_each_child_of_node(np, child) {
1332 if (of_device_is_compatible(child, gpio_compat))
1333 continue;
1334 ret = at91_pinctrl_parse_functions(child, info, i++);
1335 if (ret) {
1336 dev_err(&pdev->dev, "failed to parse function\n");
Julia Lawalld94b9862015-10-24 16:42:35 +02001337 of_node_put(child);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001338 return ret;
1339 }
1340 }
1341
1342 return 0;
1343}
1344
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001345static int at91_pinctrl_probe(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001346{
1347 struct at91_pinctrl *info;
1348 struct pinctrl_pin_desc *pdesc;
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001349 int ret, i, j, k, ngpio_chips_enabled = 0;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001350
1351 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1352 if (!info)
1353 return -ENOMEM;
1354
1355 ret = at91_pinctrl_probe_dt(pdev, info);
1356 if (ret)
1357 return ret;
1358
1359 /*
1360 * We need all the GPIO drivers to probe FIRST, or we will not be able
1361 * to obtain references to the struct gpio_chip * for them, and we
1362 * need this to proceed.
1363 */
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001364 for (i = 0; i < gpio_banks; i++)
1365 if (gpio_chips[i])
1366 ngpio_chips_enabled++;
1367
1368 if (ngpio_chips_enabled < info->nactive_banks) {
1369 dev_warn(&pdev->dev,
1370 "All GPIO chips are not registered yet (%d/%d)\n",
1371 ngpio_chips_enabled, info->nactive_banks);
1372 devm_kfree(&pdev->dev, info);
1373 return -EPROBE_DEFER;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001374 }
1375
1376 at91_pinctrl_desc.name = dev_name(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001377 at91_pinctrl_desc.npins = gpio_banks * MAX_NB_GPIO_PER_BANK;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001378 at91_pinctrl_desc.pins = pdesc =
Kees Cooka86854d2018-06-12 14:07:58 -07001379 devm_kcalloc(&pdev->dev,
1380 at91_pinctrl_desc.npins, sizeof(*pdesc),
1381 GFP_KERNEL);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001382
1383 if (!at91_pinctrl_desc.pins)
1384 return -ENOMEM;
1385
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001386 for (i = 0, k = 0; i < gpio_banks; i++) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001387 for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) {
1388 pdesc->number = k;
1389 pdesc->name = kasprintf(GFP_KERNEL, "pio%c%d", i + 'A', j);
1390 pdesc++;
1391 }
1392 }
1393
1394 platform_set_drvdata(pdev, info);
Laxman Dewangan5c674252016-02-28 14:32:19 +05301395 info->pctl = devm_pinctrl_register(&pdev->dev, &at91_pinctrl_desc,
1396 info);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001397
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001398 if (IS_ERR(info->pctl)) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001399 dev_err(&pdev->dev, "could not register AT91 pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001400 return PTR_ERR(info->pctl);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001401 }
1402
1403 /* We will handle a range of GPIO pins */
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001404 for (i = 0; i < gpio_banks; i++)
1405 if (gpio_chips[i])
1406 pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001407
1408 dev_info(&pdev->dev, "initialized AT91 pinctrl driver\n");
1409
1410 return 0;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001411}
1412
Richard Genoud8af584b2014-02-17 17:57:26 +01001413static int at91_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
1414{
Linus Walleij370ea612015-12-08 09:27:45 +01001415 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Richard Genoud8af584b2014-02-17 17:57:26 +01001416 void __iomem *pio = at91_gpio->regbase;
1417 unsigned mask = 1 << offset;
1418 u32 osr;
1419
1420 osr = readl_relaxed(pio + PIO_OSR);
Matti Vaittinen3c827872020-02-14 15:57:12 +02001421 if (osr & mask)
1422 return GPIO_LINE_DIRECTION_OUT;
1423
1424 return GPIO_LINE_DIRECTION_IN;
Richard Genoud8af584b2014-02-17 17:57:26 +01001425}
1426
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001427static int at91_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
1428{
Linus Walleij370ea612015-12-08 09:27:45 +01001429 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001430 void __iomem *pio = at91_gpio->regbase;
1431 unsigned mask = 1 << offset;
1432
1433 writel_relaxed(mask, pio + PIO_ODR);
1434 return 0;
1435}
1436
1437static int at91_gpio_get(struct gpio_chip *chip, unsigned offset)
1438{
Linus Walleij370ea612015-12-08 09:27:45 +01001439 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001440 void __iomem *pio = at91_gpio->regbase;
1441 unsigned mask = 1 << offset;
1442 u32 pdsr;
1443
1444 pdsr = readl_relaxed(pio + PIO_PDSR);
1445 return (pdsr & mask) != 0;
1446}
1447
1448static void at91_gpio_set(struct gpio_chip *chip, unsigned offset,
1449 int val)
1450{
Linus Walleij370ea612015-12-08 09:27:45 +01001451 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001452 void __iomem *pio = at91_gpio->regbase;
1453 unsigned mask = 1 << offset;
1454
1455 writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
1456}
1457
Alexander Stein1893b2c2015-04-02 11:55:49 +02001458static void at91_gpio_set_multiple(struct gpio_chip *chip,
1459 unsigned long *mask, unsigned long *bits)
1460{
Linus Walleij370ea612015-12-08 09:27:45 +01001461 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Alexander Stein1893b2c2015-04-02 11:55:49 +02001462 void __iomem *pio = at91_gpio->regbase;
1463
1464#define BITS_MASK(bits) (((bits) == 32) ? ~0U : (BIT(bits) - 1))
1465 /* Mask additionally to ngpio as not all GPIO controllers have 32 pins */
1466 uint32_t set_mask = (*mask & *bits) & BITS_MASK(chip->ngpio);
1467 uint32_t clear_mask = (*mask & ~(*bits)) & BITS_MASK(chip->ngpio);
1468
1469 writel_relaxed(set_mask, pio + PIO_SODR);
1470 writel_relaxed(clear_mask, pio + PIO_CODR);
1471}
1472
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001473static int at91_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
1474 int val)
1475{
Linus Walleij370ea612015-12-08 09:27:45 +01001476 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001477 void __iomem *pio = at91_gpio->regbase;
1478 unsigned mask = 1 << offset;
1479
1480 writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
1481 writel_relaxed(mask, pio + PIO_OER);
1482
1483 return 0;
1484}
1485
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001486#ifdef CONFIG_DEBUG_FS
1487static void at91_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1488{
1489 enum at91_mux mode;
1490 int i;
Linus Walleij370ea612015-12-08 09:27:45 +01001491 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001492 void __iomem *pio = at91_gpio->regbase;
Andy Shevchenko5bae1f02020-06-15 18:05:45 +03001493 const char *gpio_label;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001494
Andy Shevchenko5bae1f02020-06-15 18:05:45 +03001495 for_each_requested_gpio(chip, i, gpio_label) {
Alexander Stein47f22712014-04-14 20:53:08 +02001496 unsigned mask = pin_to_mask(i);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001497
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001498 mode = at91_gpio->ops->get_periph(pio, mask);
1499 seq_printf(s, "[%s] GPIO%s%d: ",
1500 gpio_label, chip->label, i);
1501 if (mode == AT91_MUX_GPIO) {
Matthieu Crapet853b6bf2014-11-18 15:43:45 +01001502 seq_printf(s, "[gpio] ");
1503 seq_printf(s, "%s ",
1504 readl_relaxed(pio + PIO_OSR) & mask ?
1505 "output" : "input");
1506 seq_printf(s, "%s\n",
1507 readl_relaxed(pio + PIO_PDSR) & mask ?
1508 "set" : "clear");
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001509 } else {
1510 seq_printf(s, "[periph %c]\n",
1511 mode + 'A' - 1);
1512 }
1513 }
1514}
1515#else
1516#define at91_gpio_dbg_show NULL
1517#endif
1518
1519/* Several AIC controller irqs are dispatched through this GPIO handler.
1520 * To use any AT91_PIN_* as an externally triggered IRQ, first call
1521 * at91_set_gpio_input() then maybe enable its glitch filter.
1522 * Then just request_irq() with the pin ID; it works like any ARM IRQ
1523 * handler.
1524 * First implementation always triggers on rising and falling edges
1525 * whereas the newer PIO3 can be additionally configured to trigger on
1526 * level, edge with any polarity.
1527 *
1528 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
1529 * configuring them with at91_set_a_periph() or at91_set_b_periph().
1530 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
1531 */
1532
1533static void gpio_irq_mask(struct irq_data *d)
1534{
1535 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1536 void __iomem *pio = at91_gpio->regbase;
1537 unsigned mask = 1 << d->hwirq;
1538
1539 if (pio)
1540 writel_relaxed(mask, pio + PIO_IDR);
1541}
1542
1543static void gpio_irq_unmask(struct irq_data *d)
1544{
1545 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1546 void __iomem *pio = at91_gpio->regbase;
1547 unsigned mask = 1 << d->hwirq;
1548
1549 if (pio)
1550 writel_relaxed(mask, pio + PIO_IER);
1551}
1552
1553static int gpio_irq_type(struct irq_data *d, unsigned type)
1554{
1555 switch (type) {
1556 case IRQ_TYPE_NONE:
1557 case IRQ_TYPE_EDGE_BOTH:
1558 return 0;
1559 default:
1560 return -EINVAL;
1561 }
1562}
1563
1564/* Alternate irq type for PIO3 support */
1565static int alt_gpio_irq_type(struct irq_data *d, unsigned type)
1566{
1567 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1568 void __iomem *pio = at91_gpio->regbase;
1569 unsigned mask = 1 << d->hwirq;
1570
1571 switch (type) {
1572 case IRQ_TYPE_EDGE_RISING:
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001573 irq_set_handler_locked(d, handle_simple_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001574 writel_relaxed(mask, pio + PIO_ESR);
1575 writel_relaxed(mask, pio + PIO_REHLSR);
1576 break;
1577 case IRQ_TYPE_EDGE_FALLING:
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001578 irq_set_handler_locked(d, handle_simple_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001579 writel_relaxed(mask, pio + PIO_ESR);
1580 writel_relaxed(mask, pio + PIO_FELLSR);
1581 break;
1582 case IRQ_TYPE_LEVEL_LOW:
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001583 irq_set_handler_locked(d, handle_level_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001584 writel_relaxed(mask, pio + PIO_LSR);
1585 writel_relaxed(mask, pio + PIO_FELLSR);
1586 break;
1587 case IRQ_TYPE_LEVEL_HIGH:
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001588 irq_set_handler_locked(d, handle_level_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001589 writel_relaxed(mask, pio + PIO_LSR);
1590 writel_relaxed(mask, pio + PIO_REHLSR);
1591 break;
1592 case IRQ_TYPE_EDGE_BOTH:
1593 /*
1594 * disable additional interrupt modes:
1595 * fall back to default behavior
1596 */
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001597 irq_set_handler_locked(d, handle_simple_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001598 writel_relaxed(mask, pio + PIO_AIMDR);
1599 return 0;
1600 case IRQ_TYPE_NONE:
1601 default:
Linus Walleij1c5fb662018-09-13 13:58:21 +02001602 pr_warn("AT91: No type for GPIO irq offset %d\n", d->irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001603 return -EINVAL;
1604 }
1605
1606 /* enable additional interrupt modes */
1607 writel_relaxed(mask, pio + PIO_AIMER);
1608
1609 return 0;
1610}
1611
Alexander Stein80cc3732014-04-15 22:09:41 +02001612static void gpio_irq_ack(struct irq_data *d)
1613{
1614 /* the interrupt is already cleared before by reading ISR */
1615}
1616
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001617#ifdef CONFIG_PM
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001618
1619static u32 wakeups[MAX_GPIO_BANKS];
1620static u32 backups[MAX_GPIO_BANKS];
1621
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001622static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
1623{
1624 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1625 unsigned bank = at91_gpio->pioc_idx;
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001626 unsigned mask = 1 << d->hwirq;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001627
1628 if (unlikely(bank >= MAX_GPIO_BANKS))
1629 return -EINVAL;
1630
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001631 if (state)
1632 wakeups[bank] |= mask;
1633 else
1634 wakeups[bank] &= ~mask;
1635
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001636 irq_set_irq_wake(at91_gpio->pioc_virq, state);
1637
1638 return 0;
1639}
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001640
1641void at91_pinctrl_gpio_suspend(void)
1642{
1643 int i;
1644
1645 for (i = 0; i < gpio_banks; i++) {
1646 void __iomem *pio;
1647
1648 if (!gpio_chips[i])
1649 continue;
1650
1651 pio = gpio_chips[i]->regbase;
1652
Ben Dooksd480239b2015-03-26 12:18:49 +00001653 backups[i] = readl_relaxed(pio + PIO_IMR);
1654 writel_relaxed(backups[i], pio + PIO_IDR);
1655 writel_relaxed(wakeups[i], pio + PIO_IER);
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001656
Boris BREZILLON795f9952013-12-15 19:30:51 +01001657 if (!wakeups[i])
1658 clk_disable_unprepare(gpio_chips[i]->clock);
1659 else
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001660 printk(KERN_DEBUG "GPIO-%c may wake for %08x\n",
1661 'A'+i, wakeups[i]);
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001662 }
1663}
1664
1665void at91_pinctrl_gpio_resume(void)
1666{
1667 int i;
1668
1669 for (i = 0; i < gpio_banks; i++) {
1670 void __iomem *pio;
1671
1672 if (!gpio_chips[i])
1673 continue;
1674
1675 pio = gpio_chips[i]->regbase;
1676
Boris BREZILLON37ef1d92013-12-15 19:30:52 +01001677 if (!wakeups[i])
1678 clk_prepare_enable(gpio_chips[i]->clock);
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001679
Ben Dooksd480239b2015-03-26 12:18:49 +00001680 writel_relaxed(wakeups[i], pio + PIO_IDR);
1681 writel_relaxed(backups[i], pio + PIO_IER);
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001682 }
1683}
1684
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001685#else
1686#define gpio_irq_set_wake NULL
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001687#endif /* CONFIG_PM */
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001688
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +02001689static void gpio_irq_handler(struct irq_desc *desc)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001690{
Jiang Liu5663bb22015-06-04 12:13:16 +08001691 struct irq_chip *chip = irq_desc_get_chip(desc);
Alexander Stein80cc3732014-04-15 22:09:41 +02001692 struct gpio_chip *gpio_chip = irq_desc_get_handler_data(desc);
Linus Walleij370ea612015-12-08 09:27:45 +01001693 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(gpio_chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001694 void __iomem *pio = at91_gpio->regbase;
1695 unsigned long isr;
1696 int n;
1697
1698 chained_irq_enter(chip, desc);
1699 for (;;) {
1700 /* Reading ISR acks pending (edge triggered) GPIO interrupts.
Alexandre Bellonic2eb9e72013-12-07 14:08:52 +01001701 * When there are none pending, we're finished unless we need
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001702 * to process multiple banks (like ID_PIOCDE on sam9263).
1703 */
1704 isr = readl_relaxed(pio + PIO_ISR) & readl_relaxed(pio + PIO_IMR);
1705 if (!isr) {
1706 if (!at91_gpio->next)
1707 break;
1708 at91_gpio = at91_gpio->next;
1709 pio = at91_gpio->regbase;
Alexander Steincccb0c32014-04-24 19:55:39 +02001710 gpio_chip = &at91_gpio->chip;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001711 continue;
1712 }
1713
Wei Yongjun05daa162012-10-26 22:50:54 +08001714 for_each_set_bit(n, &isr, BITS_PER_LONG) {
Alexander Stein80cc3732014-04-15 22:09:41 +02001715 generic_handle_irq(irq_find_mapping(
Thierry Redingf0fbe7b2017-11-07 19:15:47 +01001716 gpio_chip->irq.domain, n));
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001717 }
1718 }
1719 chained_irq_exit(chip, desc);
1720 /* now it may re-trigger */
1721}
1722
Pramod Gurav834e1672014-09-09 15:50:37 +05301723static int at91_gpio_of_irq_setup(struct platform_device *pdev,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001724 struct at91_gpio_chip *at91_gpio)
1725{
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001726 struct gpio_chip *gpiochip_prev = NULL;
Alexander Steincccb0c32014-04-24 19:55:39 +02001727 struct at91_gpio_chip *prev = NULL;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001728 struct irq_data *d = irq_get_irq_data(at91_gpio->pioc_virq);
Ludovic Desroches0c3dfa12018-09-13 14:42:13 +02001729 struct irq_chip *gpio_irqchip;
Linus Walleij35dea5d2019-10-01 15:06:45 +02001730 struct gpio_irq_chip *girq;
1731 int i;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001732
Linus Walleij35dea5d2019-10-01 15:06:45 +02001733 gpio_irqchip = devm_kzalloc(&pdev->dev, sizeof(*gpio_irqchip),
1734 GFP_KERNEL);
Ludovic Desroches0c3dfa12018-09-13 14:42:13 +02001735 if (!gpio_irqchip)
1736 return -ENOMEM;
1737
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001738 at91_gpio->pioc_hwirq = irqd_to_hwirq(d);
1739
Ludovic Desroches0c3dfa12018-09-13 14:42:13 +02001740 gpio_irqchip->name = "GPIO";
1741 gpio_irqchip->irq_ack = gpio_irq_ack;
1742 gpio_irqchip->irq_disable = gpio_irq_mask;
1743 gpio_irqchip->irq_mask = gpio_irq_mask;
1744 gpio_irqchip->irq_unmask = gpio_irq_unmask;
1745 gpio_irqchip->irq_set_wake = gpio_irq_set_wake,
1746 gpio_irqchip->irq_set_type = at91_gpio->ops->irq_type;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001747
1748 /* Disable irqs of this PIO controller */
1749 writel_relaxed(~0, at91_gpio->regbase + PIO_IDR);
1750
Alexander Stein80cc3732014-04-15 22:09:41 +02001751 /*
1752 * Let the generic code handle this edge IRQ, the the chained
1753 * handler will perform the actual work of handling the parent
1754 * interrupt.
1755 */
Linus Walleij35dea5d2019-10-01 15:06:45 +02001756 girq = &at91_gpio->chip.irq;
1757 girq->chip = gpio_irqchip;
1758 girq->default_type = IRQ_TYPE_NONE;
1759 girq->handler = handle_edge_irq;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001760
Linus Walleij35dea5d2019-10-01 15:06:45 +02001761 /*
1762 * The top level handler handles one bank of GPIOs, except
Alexander Steincccb0c32014-04-24 19:55:39 +02001763 * 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) {
Linus Walleij35dea5d2019-10-01 15:06:45 +02001768 girq->parent_handler = gpio_irq_handler;
1769 girq->num_parents = 1;
1770 girq->parents = devm_kcalloc(&pdev->dev, 1,
1771 sizeof(*girq->parents),
1772 GFP_KERNEL);
1773 if (!girq->parents)
1774 return -ENOMEM;
1775 girq->parents[0] = at91_gpio->pioc_virq;
Alexander Steincccb0c32014-04-24 19:55:39 +02001776 return 0;
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001777 }
Alexander Steincccb0c32014-04-24 19:55:39 +02001778
Linus Walleij370ea612015-12-08 09:27:45 +01001779 prev = gpiochip_get_data(gpiochip_prev);
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001780 /* we can only have 2 banks before */
1781 for (i = 0; i < 2; i++) {
1782 if (prev->next) {
1783 prev = prev->next;
1784 } else {
1785 prev->next = at91_gpio;
1786 return 0;
1787 }
1788 }
1789
1790 return -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001791}
1792
1793/* This structure is replicated for each GPIO block allocated at probe time */
Alexander Stein234b6512016-04-29 14:50:02 +02001794static const struct gpio_chip at91_gpio_template = {
Jonas Gorski98c85d52015-10-11 17:34:19 +02001795 .request = gpiochip_generic_request,
1796 .free = gpiochip_generic_free,
Richard Genoud8af584b2014-02-17 17:57:26 +01001797 .get_direction = at91_gpio_get_direction,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001798 .direction_input = at91_gpio_direction_input,
1799 .get = at91_gpio_get,
1800 .direction_output = at91_gpio_direction_output,
1801 .set = at91_gpio_set,
Alexander Stein1893b2c2015-04-02 11:55:49 +02001802 .set_multiple = at91_gpio_set_multiple,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001803 .dbg_show = at91_gpio_dbg_show,
Linus Walleij9fb1f392013-12-04 14:42:46 +01001804 .can_sleep = false,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001805 .ngpio = MAX_NB_GPIO_PER_BANK,
1806};
1807
Fabian Frederickbaa9946e2015-03-16 20:59:09 +01001808static const struct of_device_id at91_gpio_of_match[] = {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001809 { .compatible = "atmel,at91sam9x5-gpio", .data = &at91sam9x5_ops, },
1810 { .compatible = "atmel,at91rm9200-gpio", .data = &at91rm9200_ops },
Claudiu Bezneaa2fcb1c2019-02-07 09:24:53 +00001811 { .compatible = "microchip,sam9x60-gpio", .data = &sam9x60_ops },
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001812 { /* sentinel */ }
1813};
1814
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001815static int at91_gpio_probe(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001816{
1817 struct device_node *np = pdev->dev.of_node;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001818 struct at91_gpio_chip *at91_chip = NULL;
1819 struct gpio_chip *chip;
1820 struct pinctrl_gpio_range *range;
1821 int ret = 0;
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001822 int irq, i;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001823 int alias_idx = of_alias_get_id(np, "gpio");
1824 uint32_t ngpio;
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001825 char **names;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001826
1827 BUG_ON(alias_idx >= ARRAY_SIZE(gpio_chips));
1828 if (gpio_chips[alias_idx]) {
1829 ret = -EBUSY;
1830 goto err;
1831 }
1832
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001833 irq = platform_get_irq(pdev, 0);
1834 if (irq < 0) {
1835 ret = irq;
1836 goto err;
1837 }
1838
1839 at91_chip = devm_kzalloc(&pdev->dev, sizeof(*at91_chip), GFP_KERNEL);
1840 if (!at91_chip) {
1841 ret = -ENOMEM;
1842 goto err;
1843 }
1844
YueHaibing4b024222019-11-04 22:26:54 +08001845 at91_chip->regbase = devm_platform_ioremap_resource(pdev, 0);
Thierry Reding9e0c1fb2013-01-21 11:09:14 +01001846 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 Walleij35dea5d2019-10-01 15:06:45 +02001907 ret = at91_gpio_of_irq_setup(pdev, at91_chip);
1908 if (ret)
1909 goto gpiochip_add_err;
1910
Linus Walleij370ea612015-12-08 09:27:45 +01001911 ret = gpiochip_add_data(chip, at91_chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001912 if (ret)
Pramod Gurav70e41972014-09-09 15:50:36 +05301913 goto gpiochip_add_err;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001914
1915 gpio_chips[alias_idx] = at91_chip;
1916 gpio_banks = max(gpio_banks, alias_idx + 1);
1917
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001918 dev_info(&pdev->dev, "at address %p\n", at91_chip->regbase);
1919
1920 return 0;
1921
Pramod Gurav70e41972014-09-09 15:50:36 +05301922gpiochip_add_err:
Pramod Gurav70e41972014-09-09 15:50:36 +05301923clk_enable_err:
Alexander Stein7d3a3fe2016-04-29 14:50:03 +02001924 clk_disable_unprepare(at91_chip->clock);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001925err:
1926 dev_err(&pdev->dev, "Failure %i for GPIO %i\n", ret, alias_idx);
1927
1928 return ret;
1929}
1930
1931static struct platform_driver at91_gpio_driver = {
1932 .driver = {
1933 .name = "gpio-at91",
Sachin Kamat606fca92013-09-28 17:38:48 +05301934 .of_match_table = at91_gpio_of_match,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001935 },
1936 .probe = at91_gpio_probe,
1937};
1938
1939static struct platform_driver at91_pinctrl_driver = {
1940 .driver = {
1941 .name = "pinctrl-at91",
Sachin Kamat606fca92013-09-28 17:38:48 +05301942 .of_match_table = at91_pinctrl_of_match,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001943 },
1944 .probe = at91_pinctrl_probe,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001945};
1946
Thierry Redingbab7f5a2015-12-02 17:31:55 +01001947static struct platform_driver * const drivers[] = {
1948 &at91_gpio_driver,
1949 &at91_pinctrl_driver,
1950};
1951
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001952static int __init at91_pinctrl_init(void)
1953{
Thierry Redingbab7f5a2015-12-02 17:31:55 +01001954 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001955}
1956arch_initcall(at91_pinctrl_init);