blob: 994292ff1b07e88e099164c515951c1acf4a67d1 [file] [log] [blame]
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001/*
2 * at91 pinctrl driver based on at91 pinmux core
3 *
4 * Copyright (C) 2011-2012 Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
5 *
6 * Under GPLv2 only
7 */
8
9#include <linux/clk.h>
10#include <linux/err.h>
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/of.h>
14#include <linux/of_device.h>
15#include <linux/of_address.h>
16#include <linux/of_irq.h>
17#include <linux/slab.h>
18#include <linux/interrupt.h>
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080019#include <linux/io.h>
20#include <linux/gpio.h>
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080021#include <linux/pinctrl/machine.h>
22#include <linux/pinctrl/pinconf.h>
23#include <linux/pinctrl/pinctrl.h>
24#include <linux/pinctrl/pinmux.h>
25/* Since we request GPIOs from ourself */
26#include <linux/pinctrl/consumer.h>
27
Alexandre Bellonic654b6b2014-10-17 11:49:40 +020028#include "pinctrl-at91.h"
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080029#include "core.h"
30
Linus Walleij94daf852013-11-05 10:30:14 +010031#define MAX_GPIO_BANKS 5
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080032#define MAX_NB_GPIO_PER_BANK 32
33
34struct at91_pinctrl_mux_ops;
35
36struct at91_gpio_chip {
37 struct gpio_chip chip;
38 struct pinctrl_gpio_range range;
39 struct at91_gpio_chip *next; /* Bank sharing same clock */
40 int pioc_hwirq; /* PIO bank interrupt identifier on AIC */
41 int pioc_virq; /* PIO bank Linux virtual interrupt */
42 int pioc_idx; /* PIO bank index */
43 void __iomem *regbase; /* PIO bank virtual address */
44 struct clk *clock; /* associated clock */
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080045 struct at91_pinctrl_mux_ops *ops; /* ops */
46};
47
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080048static struct at91_gpio_chip *gpio_chips[MAX_GPIO_BANKS];
49
50static int gpio_banks;
51
Jean-Christophe PLAGNIOL-VILLARD525fae22012-10-23 18:28:00 +020052#define PULL_UP (1 << 0)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080053#define MULTI_DRIVE (1 << 1)
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +080054#define DEGLITCH (1 << 2)
55#define PULL_DOWN (1 << 3)
56#define DIS_SCHMIT (1 << 4)
Marek Roszko4334ac22014-08-23 23:12:04 -040057#define DRIVE_STRENGTH_SHIFT 5
58#define DRIVE_STRENGTH_MASK 0x3
59#define DRIVE_STRENGTH (DRIVE_STRENGTH_MASK << DRIVE_STRENGTH_SHIFT)
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +080060#define DEBOUNCE (1 << 16)
61#define DEBOUNCE_VAL_SHIFT 17
62#define DEBOUNCE_VAL (0x3fff << DEBOUNCE_VAL_SHIFT)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080063
64/**
Marek Roszko4334ac22014-08-23 23:12:04 -040065 * These defines will translated the dt binding settings to our internal
66 * settings. They are not necessarily the same value as the register setting.
67 * The actual drive strength current of low, medium and high must be looked up
68 * from the corresponding device datasheet. This value is different for pins
69 * that are even in the same banks. It is also dependent on VCC.
70 * DRIVE_STRENGTH_DEFAULT is just a placeholder to avoid changing the drive
71 * strength when there is no dt config for it.
72 */
73#define DRIVE_STRENGTH_DEFAULT (0 << DRIVE_STRENGTH_SHIFT)
74#define DRIVE_STRENGTH_LOW (1 << DRIVE_STRENGTH_SHIFT)
75#define DRIVE_STRENGTH_MED (2 << DRIVE_STRENGTH_SHIFT)
76#define DRIVE_STRENGTH_HI (3 << DRIVE_STRENGTH_SHIFT)
77
78/**
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +080079 * struct at91_pmx_func - describes AT91 pinmux functions
80 * @name: the name of this specific function
81 * @groups: corresponding pin groups
82 * @ngroups: the number of groups
83 */
84struct at91_pmx_func {
85 const char *name;
86 const char **groups;
87 unsigned ngroups;
88};
89
90enum at91_mux {
91 AT91_MUX_GPIO = 0,
92 AT91_MUX_PERIPH_A = 1,
93 AT91_MUX_PERIPH_B = 2,
94 AT91_MUX_PERIPH_C = 3,
95 AT91_MUX_PERIPH_D = 4,
96};
97
98/**
99 * struct at91_pmx_pin - describes an At91 pin mux
100 * @bank: the bank of the pin
101 * @pin: the pin number in the @bank
102 * @mux: the mux mode : gpio or periph_x of the pin i.e. alternate function.
103 * @conf: the configuration of the pin: PULL_UP, MULTIDRIVE etc...
104 */
105struct at91_pmx_pin {
106 uint32_t bank;
107 uint32_t pin;
108 enum at91_mux mux;
109 unsigned long conf;
110};
111
112/**
113 * struct at91_pin_group - describes an At91 pin group
114 * @name: the name of this specific pin group
115 * @pins_conf: the mux mode for each pin in this group. The size of this
116 * array is the same as pins.
117 * @pins: an array of discrete physical pins used in this group, taken
118 * from the driver-local pin enumeration space
119 * @npins: the number of pins in this group array, i.e. the number of
120 * elements in .pins so we can iterate over that array
121 */
122struct at91_pin_group {
123 const char *name;
124 struct at91_pmx_pin *pins_conf;
125 unsigned int *pins;
126 unsigned npins;
127};
128
129/**
Alexandre Bellonic2eb9e72013-12-07 14:08:52 +0100130 * struct at91_pinctrl_mux_ops - describes an AT91 mux ops group
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800131 * on new IP with support for periph C and D the way to mux in
132 * periph A and B has changed
133 * So provide the right call back
134 * if not present means the IP does not support it
135 * @get_periph: return the periph mode configured
136 * @mux_A_periph: mux as periph A
137 * @mux_B_periph: mux as periph B
138 * @mux_C_periph: mux as periph C
139 * @mux_D_periph: mux as periph D
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800140 * @get_deglitch: get deglitch status
141 * @set_deglitch: enable/disable deglitch
142 * @get_debounce: get debounce status
143 * @set_debounce: enable/disable debounce
144 * @get_pulldown: get pulldown status
145 * @set_pulldown: enable/disable pulldown
146 * @get_schmitt_trig: get schmitt trigger status
147 * @disable_schmitt_trig: disable schmitt trigger
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800148 * @irq_type: return irq type
149 */
150struct at91_pinctrl_mux_ops {
151 enum at91_mux (*get_periph)(void __iomem *pio, unsigned mask);
152 void (*mux_A_periph)(void __iomem *pio, unsigned mask);
153 void (*mux_B_periph)(void __iomem *pio, unsigned mask);
154 void (*mux_C_periph)(void __iomem *pio, unsigned mask);
155 void (*mux_D_periph)(void __iomem *pio, unsigned mask);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800156 bool (*get_deglitch)(void __iomem *pio, unsigned pin);
Boris BREZILLON77966ad2013-09-13 09:45:33 +0200157 void (*set_deglitch)(void __iomem *pio, unsigned mask, bool is_on);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800158 bool (*get_debounce)(void __iomem *pio, unsigned pin, u32 *div);
Boris BREZILLON77966ad2013-09-13 09:45:33 +0200159 void (*set_debounce)(void __iomem *pio, unsigned mask, bool is_on, u32 div);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800160 bool (*get_pulldown)(void __iomem *pio, unsigned pin);
Boris BREZILLON77966ad2013-09-13 09:45:33 +0200161 void (*set_pulldown)(void __iomem *pio, unsigned mask, bool is_on);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800162 bool (*get_schmitt_trig)(void __iomem *pio, unsigned pin);
163 void (*disable_schmitt_trig)(void __iomem *pio, unsigned mask);
Marek Roszko4334ac22014-08-23 23:12:04 -0400164 unsigned (*get_drivestrength)(void __iomem *pio, unsigned pin);
165 void (*set_drivestrength)(void __iomem *pio, unsigned pin,
166 u32 strength);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800167 /* irq */
168 int (*irq_type)(struct irq_data *d, unsigned type);
169};
170
171static int gpio_irq_type(struct irq_data *d, unsigned type);
172static int alt_gpio_irq_type(struct irq_data *d, unsigned type);
173
174struct at91_pinctrl {
175 struct device *dev;
176 struct pinctrl_dev *pctl;
177
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +0100178 int nactive_banks;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800179
180 uint32_t *mux_mask;
181 int nmux;
182
183 struct at91_pmx_func *functions;
184 int nfunctions;
185
186 struct at91_pin_group *groups;
187 int ngroups;
188
189 struct at91_pinctrl_mux_ops *ops;
190};
191
192static const inline struct at91_pin_group *at91_pinctrl_find_group_by_name(
193 const struct at91_pinctrl *info,
194 const char *name)
195{
196 const struct at91_pin_group *grp = NULL;
197 int i;
198
199 for (i = 0; i < info->ngroups; i++) {
200 if (strcmp(info->groups[i].name, name))
201 continue;
202
203 grp = &info->groups[i];
204 dev_dbg(info->dev, "%s: %d 0:%d\n", name, grp->npins, grp->pins[0]);
205 break;
206 }
207
208 return grp;
209}
210
211static int at91_get_groups_count(struct pinctrl_dev *pctldev)
212{
213 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
214
215 return info->ngroups;
216}
217
218static const char *at91_get_group_name(struct pinctrl_dev *pctldev,
219 unsigned selector)
220{
221 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
222
223 return info->groups[selector].name;
224}
225
226static int at91_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
227 const unsigned **pins,
228 unsigned *npins)
229{
230 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
231
232 if (selector >= info->ngroups)
233 return -EINVAL;
234
235 *pins = info->groups[selector].pins;
236 *npins = info->groups[selector].npins;
237
238 return 0;
239}
240
241static void at91_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
242 unsigned offset)
243{
244 seq_printf(s, "%s", dev_name(pctldev->dev));
245}
246
247static int at91_dt_node_to_map(struct pinctrl_dev *pctldev,
248 struct device_node *np,
249 struct pinctrl_map **map, unsigned *num_maps)
250{
251 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
252 const struct at91_pin_group *grp;
253 struct pinctrl_map *new_map;
254 struct device_node *parent;
255 int map_num = 1;
256 int i;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800257
258 /*
Alexandre Belloni61e310a2013-10-16 16:12:33 +0200259 * first find the group of this node and check if we need to create
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800260 * config maps for pins
261 */
262 grp = at91_pinctrl_find_group_by_name(info, np->name);
263 if (!grp) {
264 dev_err(info->dev, "unable to find group for node %s\n",
265 np->name);
266 return -EINVAL;
267 }
268
269 map_num += grp->npins;
270 new_map = devm_kzalloc(pctldev->dev, sizeof(*new_map) * map_num, GFP_KERNEL);
271 if (!new_map)
272 return -ENOMEM;
273
274 *map = new_map;
275 *num_maps = map_num;
276
277 /* create mux map */
278 parent = of_get_parent(np);
279 if (!parent) {
Julia Lawallc62b2b32012-12-12 15:22:44 +0100280 devm_kfree(pctldev->dev, new_map);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800281 return -EINVAL;
282 }
283 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
284 new_map[0].data.mux.function = parent->name;
285 new_map[0].data.mux.group = np->name;
286 of_node_put(parent);
287
288 /* create config map */
289 new_map++;
290 for (i = 0; i < grp->npins; i++) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800291 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
292 new_map[i].data.configs.group_or_pin =
293 pin_get_name(pctldev, grp->pins[i]);
294 new_map[i].data.configs.configs = &grp->pins_conf[i].conf;
295 new_map[i].data.configs.num_configs = 1;
296 }
297
298 dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
299 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
300
301 return 0;
302}
303
304static void at91_dt_free_map(struct pinctrl_dev *pctldev,
305 struct pinctrl_map *map, unsigned num_maps)
306{
307}
308
Laurent Pinchart022ab142013-02-16 10:25:07 +0100309static const struct pinctrl_ops at91_pctrl_ops = {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800310 .get_groups_count = at91_get_groups_count,
311 .get_group_name = at91_get_group_name,
312 .get_group_pins = at91_get_group_pins,
313 .pin_dbg_show = at91_pin_dbg_show,
314 .dt_node_to_map = at91_dt_node_to_map,
315 .dt_free_map = at91_dt_free_map,
316};
317
Sachin Kamat3c936002013-03-15 10:07:03 +0530318static void __iomem *pin_to_controller(struct at91_pinctrl *info,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800319 unsigned int bank)
320{
David Dueck1ab36382015-07-28 09:48:16 +0200321 if (!gpio_chips[bank])
322 return NULL;
323
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800324 return gpio_chips[bank]->regbase;
325}
326
327static inline int pin_to_bank(unsigned pin)
328{
329 return pin /= MAX_NB_GPIO_PER_BANK;
330}
331
332static unsigned pin_to_mask(unsigned int pin)
333{
334 return 1 << pin;
335}
336
Marek Roszko4334ac22014-08-23 23:12:04 -0400337static unsigned two_bit_pin_value_shift_amount(unsigned int pin)
338{
339 /* return the shift value for a pin for "two bit" per pin registers,
340 * i.e. drive strength */
341 return 2*((pin >= MAX_NB_GPIO_PER_BANK/2)
342 ? pin - MAX_NB_GPIO_PER_BANK/2 : pin);
343}
344
345static unsigned sama5d3_get_drive_register(unsigned int pin)
346{
347 /* drive strength is split between two registers
348 * with two bits per pin */
349 return (pin >= MAX_NB_GPIO_PER_BANK/2)
350 ? SAMA5D3_PIO_DRIVER2 : SAMA5D3_PIO_DRIVER1;
351}
352
353static unsigned at91sam9x5_get_drive_register(unsigned int pin)
354{
355 /* drive strength is split between two registers
356 * with two bits per pin */
357 return (pin >= MAX_NB_GPIO_PER_BANK/2)
358 ? AT91SAM9X5_PIO_DRIVER2 : AT91SAM9X5_PIO_DRIVER1;
359}
360
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800361static void at91_mux_disable_interrupt(void __iomem *pio, unsigned mask)
362{
363 writel_relaxed(mask, pio + PIO_IDR);
364}
365
366static unsigned at91_mux_get_pullup(void __iomem *pio, unsigned pin)
367{
Boris BREZILLON05d35342013-08-27 15:19:21 +0200368 return !((readl_relaxed(pio + PIO_PUSR) >> pin) & 0x1);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800369}
370
371static void at91_mux_set_pullup(void __iomem *pio, unsigned mask, bool on)
372{
Wenyou Yang3d784272014-09-11 16:40:15 +0200373 if (on)
374 writel_relaxed(mask, pio + PIO_PPDDR);
375
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800376 writel_relaxed(mask, pio + (on ? PIO_PUER : PIO_PUDR));
377}
378
379static unsigned at91_mux_get_multidrive(void __iomem *pio, unsigned pin)
380{
381 return (readl_relaxed(pio + PIO_MDSR) >> pin) & 0x1;
382}
383
384static void at91_mux_set_multidrive(void __iomem *pio, unsigned mask, bool on)
385{
386 writel_relaxed(mask, pio + (on ? PIO_MDER : PIO_MDDR));
387}
388
389static void at91_mux_set_A_periph(void __iomem *pio, unsigned mask)
390{
391 writel_relaxed(mask, pio + PIO_ASR);
392}
393
394static void at91_mux_set_B_periph(void __iomem *pio, unsigned mask)
395{
396 writel_relaxed(mask, pio + PIO_BSR);
397}
398
399static void at91_mux_pio3_set_A_periph(void __iomem *pio, unsigned mask)
400{
401
402 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask,
403 pio + PIO_ABCDSR1);
404 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
405 pio + PIO_ABCDSR2);
406}
407
408static void at91_mux_pio3_set_B_periph(void __iomem *pio, unsigned mask)
409{
410 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask,
411 pio + PIO_ABCDSR1);
412 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
413 pio + PIO_ABCDSR2);
414}
415
416static void at91_mux_pio3_set_C_periph(void __iomem *pio, unsigned mask)
417{
418 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask, pio + PIO_ABCDSR1);
419 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
420}
421
422static void at91_mux_pio3_set_D_periph(void __iomem *pio, unsigned mask)
423{
424 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask, pio + PIO_ABCDSR1);
425 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
426}
427
428static enum at91_mux at91_mux_pio3_get_periph(void __iomem *pio, unsigned mask)
429{
430 unsigned select;
431
432 if (readl_relaxed(pio + PIO_PSR) & mask)
433 return AT91_MUX_GPIO;
434
435 select = !!(readl_relaxed(pio + PIO_ABCDSR1) & mask);
436 select |= (!!(readl_relaxed(pio + PIO_ABCDSR2) & mask) << 1);
437
438 return select + 1;
439}
440
441static enum at91_mux at91_mux_get_periph(void __iomem *pio, unsigned mask)
442{
443 unsigned select;
444
445 if (readl_relaxed(pio + PIO_PSR) & mask)
446 return AT91_MUX_GPIO;
447
448 select = readl_relaxed(pio + PIO_ABSR) & mask;
449
450 return select + 1;
451}
452
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800453static bool at91_mux_get_deglitch(void __iomem *pio, unsigned pin)
454{
Ben Dooksd480239b2015-03-26 12:18:49 +0000455 return (readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1;
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800456}
457
458static void at91_mux_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
459{
Ben Dooksd480239b2015-03-26 12:18:49 +0000460 writel_relaxed(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800461}
462
Boris BREZILLONc8dba022013-09-13 09:47:22 +0200463static bool at91_mux_pio3_get_deglitch(void __iomem *pio, unsigned pin)
464{
Ben Dooksd480239b2015-03-26 12:18:49 +0000465 if ((readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1)
466 return !((readl_relaxed(pio + PIO_IFSCSR) >> pin) & 0x1);
Boris BREZILLONc8dba022013-09-13 09:47:22 +0200467
468 return false;
469}
470
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800471static void at91_mux_pio3_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
472{
473 if (is_on)
Ben Dooksd480239b2015-03-26 12:18:49 +0000474 writel_relaxed(mask, pio + PIO_IFSCDR);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800475 at91_mux_set_deglitch(pio, mask, is_on);
476}
477
478static bool at91_mux_pio3_get_debounce(void __iomem *pio, unsigned pin, u32 *div)
479{
Ben Dooksd480239b2015-03-26 12:18:49 +0000480 *div = readl_relaxed(pio + PIO_SCDR);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800481
Ben Dooksd480239b2015-03-26 12:18:49 +0000482 return ((readl_relaxed(pio + PIO_IFSR) >> pin) & 0x1) &&
483 ((readl_relaxed(pio + PIO_IFSCSR) >> pin) & 0x1);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800484}
485
486static void at91_mux_pio3_set_debounce(void __iomem *pio, unsigned mask,
487 bool is_on, u32 div)
488{
489 if (is_on) {
Ben Dooksd480239b2015-03-26 12:18:49 +0000490 writel_relaxed(mask, pio + PIO_IFSCER);
491 writel_relaxed(div & PIO_SCDR_DIV, pio + PIO_SCDR);
492 writel_relaxed(mask, pio + PIO_IFER);
Boris BREZILLONc8dba022013-09-13 09:47:22 +0200493 } else
Ben Dooksd480239b2015-03-26 12:18:49 +0000494 writel_relaxed(mask, pio + PIO_IFSCDR);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800495}
496
497static bool at91_mux_pio3_get_pulldown(void __iomem *pio, unsigned pin)
498{
Ben Dooksd480239b2015-03-26 12:18:49 +0000499 return !((readl_relaxed(pio + PIO_PPDSR) >> pin) & 0x1);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800500}
501
502static void at91_mux_pio3_set_pulldown(void __iomem *pio, unsigned mask, bool is_on)
503{
Wenyou Yang3d784272014-09-11 16:40:15 +0200504 if (is_on)
Ben Dooksd480239b2015-03-26 12:18:49 +0000505 writel_relaxed(mask, pio + PIO_PUDR);
Wenyou Yang3d784272014-09-11 16:40:15 +0200506
Ben Dooksd480239b2015-03-26 12:18:49 +0000507 writel_relaxed(mask, pio + (is_on ? PIO_PPDER : PIO_PPDDR));
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800508}
509
510static void at91_mux_pio3_disable_schmitt_trig(void __iomem *pio, unsigned mask)
511{
Ben Dooksd480239b2015-03-26 12:18:49 +0000512 writel_relaxed(readl_relaxed(pio + PIO_SCHMITT) | mask, pio + PIO_SCHMITT);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800513}
514
515static bool at91_mux_pio3_get_schmitt_trig(void __iomem *pio, unsigned pin)
516{
Ben Dooksd480239b2015-03-26 12:18:49 +0000517 return (readl_relaxed(pio + PIO_SCHMITT) >> pin) & 0x1;
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800518}
519
Marek Roszko4334ac22014-08-23 23:12:04 -0400520static inline u32 read_drive_strength(void __iomem *reg, unsigned pin)
521{
Ben Dooksd480239b2015-03-26 12:18:49 +0000522 unsigned tmp = readl_relaxed(reg);
Marek Roszko4334ac22014-08-23 23:12:04 -0400523
524 tmp = tmp >> two_bit_pin_value_shift_amount(pin);
525
526 return tmp & DRIVE_STRENGTH_MASK;
527}
528
529static unsigned at91_mux_sama5d3_get_drivestrength(void __iomem *pio,
530 unsigned pin)
531{
532 unsigned tmp = read_drive_strength(pio +
533 sama5d3_get_drive_register(pin), pin);
534
535 /* SAMA5 strength is 1:1 with our defines,
536 * except 0 is equivalent to low per datasheet */
537 if (!tmp)
538 tmp = DRIVE_STRENGTH_LOW;
539
540 return tmp;
541}
542
543static unsigned at91_mux_sam9x5_get_drivestrength(void __iomem *pio,
544 unsigned pin)
545{
546 unsigned tmp = read_drive_strength(pio +
547 at91sam9x5_get_drive_register(pin), pin);
548
549 /* strength is inverse in SAM9x5s hardware with the pinctrl defines
550 * hardware: 0 = hi, 1 = med, 2 = low, 3 = rsvd */
551 tmp = DRIVE_STRENGTH_HI - tmp;
552
553 return tmp;
554}
555
556static void set_drive_strength(void __iomem *reg, unsigned pin, u32 strength)
557{
Ben Dooksd480239b2015-03-26 12:18:49 +0000558 unsigned tmp = readl_relaxed(reg);
Marek Roszko4334ac22014-08-23 23:12:04 -0400559 unsigned shift = two_bit_pin_value_shift_amount(pin);
560
561 tmp &= ~(DRIVE_STRENGTH_MASK << shift);
562 tmp |= strength << shift;
563
Ben Dooksd480239b2015-03-26 12:18:49 +0000564 writel_relaxed(tmp, reg);
Marek Roszko4334ac22014-08-23 23:12:04 -0400565}
566
567static void at91_mux_sama5d3_set_drivestrength(void __iomem *pio, unsigned pin,
568 u32 setting)
569{
570 /* do nothing if setting is zero */
571 if (!setting)
572 return;
573
574 /* strength is 1 to 1 with setting for SAMA5 */
575 set_drive_strength(pio + sama5d3_get_drive_register(pin), pin, setting);
576}
577
578static void at91_mux_sam9x5_set_drivestrength(void __iomem *pio, unsigned pin,
579 u32 setting)
580{
581 /* do nothing if setting is zero */
582 if (!setting)
583 return;
584
585 /* strength is inverse on SAM9x5s with our defines
586 * 0 = hi, 1 = med, 2 = low, 3 = rsvd */
587 setting = DRIVE_STRENGTH_HI - setting;
588
589 set_drive_strength(pio + at91sam9x5_get_drive_register(pin), pin,
590 setting);
591}
592
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800593static struct at91_pinctrl_mux_ops at91rm9200_ops = {
594 .get_periph = at91_mux_get_periph,
595 .mux_A_periph = at91_mux_set_A_periph,
596 .mux_B_periph = at91_mux_set_B_periph,
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800597 .get_deglitch = at91_mux_get_deglitch,
598 .set_deglitch = at91_mux_set_deglitch,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800599 .irq_type = gpio_irq_type,
600};
601
602static struct at91_pinctrl_mux_ops at91sam9x5_ops = {
603 .get_periph = at91_mux_pio3_get_periph,
604 .mux_A_periph = at91_mux_pio3_set_A_periph,
605 .mux_B_periph = at91_mux_pio3_set_B_periph,
606 .mux_C_periph = at91_mux_pio3_set_C_periph,
607 .mux_D_periph = at91_mux_pio3_set_D_periph,
Boris BREZILLONc8dba022013-09-13 09:47:22 +0200608 .get_deglitch = at91_mux_pio3_get_deglitch,
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800609 .set_deglitch = at91_mux_pio3_set_deglitch,
610 .get_debounce = at91_mux_pio3_get_debounce,
611 .set_debounce = at91_mux_pio3_set_debounce,
612 .get_pulldown = at91_mux_pio3_get_pulldown,
613 .set_pulldown = at91_mux_pio3_set_pulldown,
614 .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
615 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
Marek Roszko4334ac22014-08-23 23:12:04 -0400616 .get_drivestrength = at91_mux_sam9x5_get_drivestrength,
617 .set_drivestrength = at91_mux_sam9x5_set_drivestrength,
618 .irq_type = alt_gpio_irq_type,
619};
620
621static struct at91_pinctrl_mux_ops sama5d3_ops = {
622 .get_periph = at91_mux_pio3_get_periph,
623 .mux_A_periph = at91_mux_pio3_set_A_periph,
624 .mux_B_periph = at91_mux_pio3_set_B_periph,
625 .mux_C_periph = at91_mux_pio3_set_C_periph,
626 .mux_D_periph = at91_mux_pio3_set_D_periph,
627 .get_deglitch = at91_mux_pio3_get_deglitch,
628 .set_deglitch = at91_mux_pio3_set_deglitch,
629 .get_debounce = at91_mux_pio3_get_debounce,
630 .set_debounce = at91_mux_pio3_set_debounce,
631 .get_pulldown = at91_mux_pio3_get_pulldown,
632 .set_pulldown = at91_mux_pio3_set_pulldown,
633 .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
634 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
635 .get_drivestrength = at91_mux_sama5d3_get_drivestrength,
636 .set_drivestrength = at91_mux_sama5d3_set_drivestrength,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800637 .irq_type = alt_gpio_irq_type,
638};
639
640static void at91_pin_dbg(const struct device *dev, const struct at91_pmx_pin *pin)
641{
642 if (pin->mux) {
Hans Wennborg4b6fe452014-08-05 21:43:16 -0700643 dev_dbg(dev, "pio%c%d configured as periph%c with conf = 0x%lx\n",
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800644 pin->bank + 'A', pin->pin, pin->mux - 1 + 'A', pin->conf);
645 } else {
Hans Wennborg4b6fe452014-08-05 21:43:16 -0700646 dev_dbg(dev, "pio%c%d configured as gpio with conf = 0x%lx\n",
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800647 pin->bank + 'A', pin->pin, pin->conf);
648 }
649}
650
Sachin Kamat3c936002013-03-15 10:07:03 +0530651static int pin_check_config(struct at91_pinctrl *info, const char *name,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800652 int index, const struct at91_pmx_pin *pin)
653{
654 int mux;
655
656 /* check if it's a valid config */
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +0100657 if (pin->bank >= gpio_banks) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800658 dev_err(info->dev, "%s: pin conf %d bank_id %d >= nbanks %d\n",
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +0100659 name, index, pin->bank, gpio_banks);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800660 return -EINVAL;
661 }
662
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +0100663 if (!gpio_chips[pin->bank]) {
664 dev_err(info->dev, "%s: pin conf %d bank_id %d not enabled\n",
665 name, index, pin->bank);
666 return -ENXIO;
667 }
668
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800669 if (pin->pin >= MAX_NB_GPIO_PER_BANK) {
670 dev_err(info->dev, "%s: pin conf %d pin_bank_id %d >= %d\n",
671 name, index, pin->pin, MAX_NB_GPIO_PER_BANK);
672 return -EINVAL;
673 }
674
675 if (!pin->mux)
676 return 0;
677
678 mux = pin->mux - 1;
679
680 if (mux >= info->nmux) {
681 dev_err(info->dev, "%s: pin conf %d mux_id %d >= nmux %d\n",
682 name, index, mux, info->nmux);
683 return -EINVAL;
684 }
685
686 if (!(info->mux_mask[pin->bank * info->nmux + mux] & 1 << pin->pin)) {
687 dev_err(info->dev, "%s: pin conf %d mux_id %d not supported for pio%c%d\n",
688 name, index, mux, pin->bank + 'A', pin->pin);
689 return -EINVAL;
690 }
691
692 return 0;
693}
694
695static void at91_mux_gpio_disable(void __iomem *pio, unsigned mask)
696{
697 writel_relaxed(mask, pio + PIO_PDR);
698}
699
700static void at91_mux_gpio_enable(void __iomem *pio, unsigned mask, bool input)
701{
702 writel_relaxed(mask, pio + PIO_PER);
703 writel_relaxed(mask, pio + (input ? PIO_ODR : PIO_OER));
704}
705
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200706static int at91_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
707 unsigned group)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800708{
709 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
710 const struct at91_pmx_pin *pins_conf = info->groups[group].pins_conf;
711 const struct at91_pmx_pin *pin;
712 uint32_t npins = info->groups[group].npins;
713 int i, ret;
714 unsigned mask;
715 void __iomem *pio;
716
717 dev_dbg(info->dev, "enable function %s group %s\n",
718 info->functions[selector].name, info->groups[group].name);
719
720 /* first check that all the pins of the group are valid with a valid
Alexandre Belloni61e310a2013-10-16 16:12:33 +0200721 * parameter */
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800722 for (i = 0; i < npins; i++) {
723 pin = &pins_conf[i];
724 ret = pin_check_config(info, info->groups[group].name, i, pin);
725 if (ret)
726 return ret;
727 }
728
729 for (i = 0; i < npins; i++) {
730 pin = &pins_conf[i];
731 at91_pin_dbg(info->dev, pin);
732 pio = pin_to_controller(info, pin->bank);
David Dueck1ab36382015-07-28 09:48:16 +0200733
734 if (!pio)
735 continue;
736
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800737 mask = pin_to_mask(pin->pin);
738 at91_mux_disable_interrupt(pio, mask);
Sachin Kamat3c936002013-03-15 10:07:03 +0530739 switch (pin->mux) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800740 case AT91_MUX_GPIO:
741 at91_mux_gpio_enable(pio, mask, 1);
742 break;
743 case AT91_MUX_PERIPH_A:
744 info->ops->mux_A_periph(pio, mask);
745 break;
746 case AT91_MUX_PERIPH_B:
747 info->ops->mux_B_periph(pio, mask);
748 break;
749 case AT91_MUX_PERIPH_C:
750 if (!info->ops->mux_C_periph)
751 return -EINVAL;
752 info->ops->mux_C_periph(pio, mask);
753 break;
754 case AT91_MUX_PERIPH_D:
755 if (!info->ops->mux_D_periph)
756 return -EINVAL;
757 info->ops->mux_D_periph(pio, mask);
758 break;
759 }
760 if (pin->mux)
761 at91_mux_gpio_disable(pio, mask);
762 }
763
764 return 0;
765}
766
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800767static int at91_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
768{
769 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
770
771 return info->nfunctions;
772}
773
774static const char *at91_pmx_get_func_name(struct pinctrl_dev *pctldev,
775 unsigned selector)
776{
777 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
778
779 return info->functions[selector].name;
780}
781
782static int at91_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
783 const char * const **groups,
784 unsigned * const num_groups)
785{
786 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
787
788 *groups = info->functions[selector].groups;
789 *num_groups = info->functions[selector].ngroups;
790
791 return 0;
792}
793
Axel Linf6f94f62012-11-05 21:23:50 +0800794static int at91_gpio_request_enable(struct pinctrl_dev *pctldev,
795 struct pinctrl_gpio_range *range,
796 unsigned offset)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800797{
798 struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
799 struct at91_gpio_chip *at91_chip;
800 struct gpio_chip *chip;
801 unsigned mask;
802
803 if (!range) {
804 dev_err(npct->dev, "invalid range\n");
805 return -EINVAL;
806 }
807 if (!range->gc) {
808 dev_err(npct->dev, "missing GPIO chip in range\n");
809 return -EINVAL;
810 }
811 chip = range->gc;
Linus Walleij370ea612015-12-08 09:27:45 +0100812 at91_chip = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800813
814 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
815
816 mask = 1 << (offset - chip->base);
817
818 dev_dbg(npct->dev, "enable pin %u as PIO%c%d 0x%x\n",
819 offset, 'A' + range->id, offset - chip->base, mask);
820
821 writel_relaxed(mask, at91_chip->regbase + PIO_PER);
822
823 return 0;
824}
825
Axel Linf6f94f62012-11-05 21:23:50 +0800826static void at91_gpio_disable_free(struct pinctrl_dev *pctldev,
827 struct pinctrl_gpio_range *range,
828 unsigned offset)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800829{
830 struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
831
832 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
833 /* Set the pin to some default state, GPIO is usually default */
834}
835
Laurent Pinchart022ab142013-02-16 10:25:07 +0100836static const struct pinmux_ops at91_pmx_ops = {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800837 .get_functions_count = at91_pmx_get_funcs_count,
838 .get_function_name = at91_pmx_get_func_name,
839 .get_function_groups = at91_pmx_get_groups,
Linus Walleij03e9f0c2014-09-03 13:02:56 +0200840 .set_mux = at91_pmx_set,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800841 .gpio_request_enable = at91_gpio_request_enable,
842 .gpio_disable_free = at91_gpio_disable_free,
843};
844
845static int at91_pinconf_get(struct pinctrl_dev *pctldev,
846 unsigned pin_id, unsigned long *config)
847{
848 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
849 void __iomem *pio;
850 unsigned pin;
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800851 int div;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800852
Alexandre Belloni1292e692013-12-07 14:08:53 +0100853 *config = 0;
854 dev_dbg(info->dev, "%s:%d, pin_id=%d", __func__, __LINE__, pin_id);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800855 pio = pin_to_controller(info, pin_to_bank(pin_id));
David Dueck1ab36382015-07-28 09:48:16 +0200856
857 if (!pio)
858 return -EINVAL;
859
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800860 pin = pin_id % MAX_NB_GPIO_PER_BANK;
861
862 if (at91_mux_get_multidrive(pio, pin))
863 *config |= MULTI_DRIVE;
864
865 if (at91_mux_get_pullup(pio, pin))
866 *config |= PULL_UP;
867
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800868 if (info->ops->get_deglitch && info->ops->get_deglitch(pio, pin))
869 *config |= DEGLITCH;
870 if (info->ops->get_debounce && info->ops->get_debounce(pio, pin, &div))
871 *config |= DEBOUNCE | (div << DEBOUNCE_VAL_SHIFT);
872 if (info->ops->get_pulldown && info->ops->get_pulldown(pio, pin))
873 *config |= PULL_DOWN;
874 if (info->ops->get_schmitt_trig && info->ops->get_schmitt_trig(pio, pin))
875 *config |= DIS_SCHMIT;
Marek Roszko4334ac22014-08-23 23:12:04 -0400876 if (info->ops->get_drivestrength)
877 *config |= (info->ops->get_drivestrength(pio, pin)
878 << DRIVE_STRENGTH_SHIFT);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800879
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800880 return 0;
881}
882
883static int at91_pinconf_set(struct pinctrl_dev *pctldev,
Sherman Yin03b054e2013-08-27 11:32:12 -0700884 unsigned pin_id, unsigned long *configs,
885 unsigned num_configs)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800886{
887 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
888 unsigned mask;
889 void __iomem *pio;
Sherman Yin03b054e2013-08-27 11:32:12 -0700890 int i;
891 unsigned long config;
Marek Roszko4334ac22014-08-23 23:12:04 -0400892 unsigned pin;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800893
Sherman Yin03b054e2013-08-27 11:32:12 -0700894 for (i = 0; i < num_configs; i++) {
895 config = configs[i];
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800896
Sherman Yin03b054e2013-08-27 11:32:12 -0700897 dev_dbg(info->dev,
898 "%s:%d, pin_id=%d, config=0x%lx",
899 __func__, __LINE__, pin_id, config);
900 pio = pin_to_controller(info, pin_to_bank(pin_id));
David Dueck1ab36382015-07-28 09:48:16 +0200901
902 if (!pio)
903 return -EINVAL;
904
Marek Roszko4334ac22014-08-23 23:12:04 -0400905 pin = pin_id % MAX_NB_GPIO_PER_BANK;
906 mask = pin_to_mask(pin);
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800907
Sherman Yin03b054e2013-08-27 11:32:12 -0700908 if (config & PULL_UP && config & PULL_DOWN)
909 return -EINVAL;
910
911 at91_mux_set_pullup(pio, mask, config & PULL_UP);
912 at91_mux_set_multidrive(pio, mask, config & MULTI_DRIVE);
913 if (info->ops->set_deglitch)
914 info->ops->set_deglitch(pio, mask, config & DEGLITCH);
915 if (info->ops->set_debounce)
916 info->ops->set_debounce(pio, mask, config & DEBOUNCE,
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800917 (config & DEBOUNCE_VAL) >> DEBOUNCE_VAL_SHIFT);
Sherman Yin03b054e2013-08-27 11:32:12 -0700918 if (info->ops->set_pulldown)
919 info->ops->set_pulldown(pio, mask, config & PULL_DOWN);
920 if (info->ops->disable_schmitt_trig && config & DIS_SCHMIT)
921 info->ops->disable_schmitt_trig(pio, mask);
Marek Roszko4334ac22014-08-23 23:12:04 -0400922 if (info->ops->set_drivestrength)
923 info->ops->set_drivestrength(pio, pin,
924 (config & DRIVE_STRENGTH)
925 >> DRIVE_STRENGTH_SHIFT);
Sherman Yin03b054e2013-08-27 11:32:12 -0700926
927 } /* for each config */
Jean-Christophe PLAGNIOL-VILLARD7ebd7a32012-09-26 14:57:45 +0800928
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800929 return 0;
930}
931
Alexandre Belloni4d9b8a82013-12-07 14:08:54 +0100932#define DBG_SHOW_FLAG(flag) do { \
933 if (config & flag) { \
934 if (num_conf) \
935 seq_puts(s, "|"); \
936 seq_puts(s, #flag); \
937 num_conf++; \
938 } \
939} while (0)
940
Marek Roszko4334ac22014-08-23 23:12:04 -0400941#define DBG_SHOW_FLAG_MASKED(mask,flag) do { \
942 if ((config & mask) == flag) { \
943 if (num_conf) \
944 seq_puts(s, "|"); \
945 seq_puts(s, #flag); \
946 num_conf++; \
947 } \
948} while (0)
949
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800950static void at91_pinconf_dbg_show(struct pinctrl_dev *pctldev,
951 struct seq_file *s, unsigned pin_id)
952{
Alexandre Belloni4d9b8a82013-12-07 14:08:54 +0100953 unsigned long config;
Rickard Strandqvist445d2022014-06-26 15:41:31 +0200954 int val, num_conf = 0;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800955
Rickard Strandqvist445d2022014-06-26 15:41:31 +0200956 at91_pinconf_get(pctldev, pin_id, &config);
Alexandre Belloni4d9b8a82013-12-07 14:08:54 +0100957
958 DBG_SHOW_FLAG(MULTI_DRIVE);
959 DBG_SHOW_FLAG(PULL_UP);
960 DBG_SHOW_FLAG(PULL_DOWN);
961 DBG_SHOW_FLAG(DIS_SCHMIT);
962 DBG_SHOW_FLAG(DEGLITCH);
Marek Roszko4334ac22014-08-23 23:12:04 -0400963 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_LOW);
964 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_MED);
965 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_HI);
Alexandre Belloni4d9b8a82013-12-07 14:08:54 +0100966 DBG_SHOW_FLAG(DEBOUNCE);
967 if (config & DEBOUNCE) {
968 val = config >> DEBOUNCE_VAL_SHIFT;
969 seq_printf(s, "(%d)", val);
970 }
971
972 return;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800973}
974
975static void at91_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
976 struct seq_file *s, unsigned group)
977{
978}
979
Laurent Pinchart022ab142013-02-16 10:25:07 +0100980static const struct pinconf_ops at91_pinconf_ops = {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800981 .pin_config_get = at91_pinconf_get,
982 .pin_config_set = at91_pinconf_set,
983 .pin_config_dbg_show = at91_pinconf_dbg_show,
984 .pin_config_group_dbg_show = at91_pinconf_group_dbg_show,
985};
986
987static struct pinctrl_desc at91_pinctrl_desc = {
988 .pctlops = &at91_pctrl_ops,
989 .pmxops = &at91_pmx_ops,
990 .confops = &at91_pinconf_ops,
991 .owner = THIS_MODULE,
992};
993
994static const char *gpio_compat = "atmel,at91rm9200-gpio";
995
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -0800996static void at91_pinctrl_child_count(struct at91_pinctrl *info,
997 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +0800998{
999 struct device_node *child;
1000
1001 for_each_child_of_node(np, child) {
1002 if (of_device_is_compatible(child, gpio_compat)) {
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001003 if (of_device_is_available(child))
1004 info->nactive_banks++;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001005 } else {
1006 info->nfunctions++;
1007 info->ngroups += of_get_child_count(child);
1008 }
1009 }
1010}
1011
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001012static int at91_pinctrl_mux_mask(struct at91_pinctrl *info,
1013 struct device_node *np)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001014{
1015 int ret = 0;
1016 int size;
Sachin Kamat1164d732013-03-15 10:07:02 +05301017 const __be32 *list;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001018
1019 list = of_get_property(np, "atmel,mux-mask", &size);
1020 if (!list) {
1021 dev_err(info->dev, "can not read the mux-mask of %d\n", size);
1022 return -EINVAL;
1023 }
1024
1025 size /= sizeof(*list);
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001026 if (!size || size % gpio_banks) {
1027 dev_err(info->dev, "wrong mux mask array should be by %d\n", gpio_banks);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001028 return -EINVAL;
1029 }
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001030 info->nmux = size / gpio_banks;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001031
1032 info->mux_mask = devm_kzalloc(info->dev, sizeof(u32) * size, GFP_KERNEL);
1033 if (!info->mux_mask) {
1034 dev_err(info->dev, "could not alloc mux_mask\n");
1035 return -ENOMEM;
1036 }
1037
1038 ret = of_property_read_u32_array(np, "atmel,mux-mask",
1039 info->mux_mask, size);
1040 if (ret)
1041 dev_err(info->dev, "can not read the mux-mask of %d\n", size);
1042 return ret;
1043}
1044
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001045static int at91_pinctrl_parse_groups(struct device_node *np,
1046 struct at91_pin_group *grp,
1047 struct at91_pinctrl *info, u32 index)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001048{
1049 struct at91_pmx_pin *pin;
1050 int size;
Sachin Kamat1164d732013-03-15 10:07:02 +05301051 const __be32 *list;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001052 int i, j;
1053
1054 dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
1055
1056 /* Initialise group */
1057 grp->name = np->name;
1058
1059 /*
1060 * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
1061 * do sanity check and calculate pins number
1062 */
1063 list = of_get_property(np, "atmel,pins", &size);
1064 /* we do not check return since it's safe node passed down */
1065 size /= sizeof(*list);
1066 if (!size || size % 4) {
1067 dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
1068 return -EINVAL;
1069 }
1070
1071 grp->npins = size / 4;
1072 pin = grp->pins_conf = devm_kzalloc(info->dev, grp->npins * sizeof(struct at91_pmx_pin),
1073 GFP_KERNEL);
1074 grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int),
1075 GFP_KERNEL);
1076 if (!grp->pins_conf || !grp->pins)
1077 return -ENOMEM;
1078
1079 for (i = 0, j = 0; i < size; i += 4, j++) {
1080 pin->bank = be32_to_cpu(*list++);
1081 pin->pin = be32_to_cpu(*list++);
1082 grp->pins[j] = pin->bank * MAX_NB_GPIO_PER_BANK + pin->pin;
1083 pin->mux = be32_to_cpu(*list++);
1084 pin->conf = be32_to_cpu(*list++);
1085
1086 at91_pin_dbg(info->dev, pin);
1087 pin++;
1088 }
1089
1090 return 0;
1091}
1092
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001093static int at91_pinctrl_parse_functions(struct device_node *np,
1094 struct at91_pinctrl *info, u32 index)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001095{
1096 struct device_node *child;
1097 struct at91_pmx_func *func;
1098 struct at91_pin_group *grp;
1099 int ret;
1100 static u32 grp_index;
1101 u32 i = 0;
1102
1103 dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
1104
1105 func = &info->functions[index];
1106
1107 /* Initialise function */
1108 func->name = np->name;
1109 func->ngroups = of_get_child_count(np);
Rickard Strandqvistca7162a2014-06-26 13:26:45 +02001110 if (func->ngroups == 0) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001111 dev_err(info->dev, "no groups defined\n");
1112 return -EINVAL;
1113 }
1114 func->groups = devm_kzalloc(info->dev,
1115 func->ngroups * sizeof(char *), GFP_KERNEL);
1116 if (!func->groups)
1117 return -ENOMEM;
1118
1119 for_each_child_of_node(np, child) {
1120 func->groups[i] = child->name;
1121 grp = &info->groups[grp_index++];
1122 ret = at91_pinctrl_parse_groups(child, grp, info, i++);
Julia Lawalld94b9862015-10-24 16:42:35 +02001123 if (ret) {
1124 of_node_put(child);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001125 return ret;
Julia Lawalld94b9862015-10-24 16:42:35 +02001126 }
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001127 }
1128
1129 return 0;
1130}
1131
Fabian Frederickbaa9946e2015-03-16 20:59:09 +01001132static const struct of_device_id at91_pinctrl_of_match[] = {
Marek Roszko4334ac22014-08-23 23:12:04 -04001133 { .compatible = "atmel,sama5d3-pinctrl", .data = &sama5d3_ops },
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001134 { .compatible = "atmel,at91sam9x5-pinctrl", .data = &at91sam9x5_ops },
1135 { .compatible = "atmel,at91rm9200-pinctrl", .data = &at91rm9200_ops },
1136 { /* sentinel */ }
1137};
1138
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001139static int at91_pinctrl_probe_dt(struct platform_device *pdev,
1140 struct at91_pinctrl *info)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001141{
1142 int ret = 0;
1143 int i, j;
1144 uint32_t *tmp;
1145 struct device_node *np = pdev->dev.of_node;
1146 struct device_node *child;
1147
1148 if (!np)
1149 return -ENODEV;
1150
1151 info->dev = &pdev->dev;
Sachin Kamat3c936002013-03-15 10:07:03 +05301152 info->ops = (struct at91_pinctrl_mux_ops *)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001153 of_match_device(at91_pinctrl_of_match, &pdev->dev)->data;
1154 at91_pinctrl_child_count(info, np);
1155
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001156 if (gpio_banks < 1) {
Alexandre Belloni61e310a2013-10-16 16:12:33 +02001157 dev_err(&pdev->dev, "you need to specify at least one gpio-controller\n");
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001158 return -EINVAL;
1159 }
1160
1161 ret = at91_pinctrl_mux_mask(info, np);
1162 if (ret)
1163 return ret;
1164
1165 dev_dbg(&pdev->dev, "nmux = %d\n", info->nmux);
1166
1167 dev_dbg(&pdev->dev, "mux-mask\n");
1168 tmp = info->mux_mask;
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001169 for (i = 0; i < gpio_banks; i++) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001170 for (j = 0; j < info->nmux; j++, tmp++) {
1171 dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
1172 }
1173 }
1174
1175 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1176 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
1177 info->functions = devm_kzalloc(&pdev->dev, info->nfunctions * sizeof(struct at91_pmx_func),
1178 GFP_KERNEL);
1179 if (!info->functions)
1180 return -ENOMEM;
1181
1182 info->groups = devm_kzalloc(&pdev->dev, info->ngroups * sizeof(struct at91_pin_group),
1183 GFP_KERNEL);
1184 if (!info->groups)
1185 return -ENOMEM;
1186
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001187 dev_dbg(&pdev->dev, "nbanks = %d\n", gpio_banks);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001188 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1189 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
1190
1191 i = 0;
1192
1193 for_each_child_of_node(np, child) {
1194 if (of_device_is_compatible(child, gpio_compat))
1195 continue;
1196 ret = at91_pinctrl_parse_functions(child, info, i++);
1197 if (ret) {
1198 dev_err(&pdev->dev, "failed to parse function\n");
Julia Lawalld94b9862015-10-24 16:42:35 +02001199 of_node_put(child);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001200 return ret;
1201 }
1202 }
1203
1204 return 0;
1205}
1206
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001207static int at91_pinctrl_probe(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001208{
1209 struct at91_pinctrl *info;
1210 struct pinctrl_pin_desc *pdesc;
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001211 int ret, i, j, k, ngpio_chips_enabled = 0;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001212
1213 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1214 if (!info)
1215 return -ENOMEM;
1216
1217 ret = at91_pinctrl_probe_dt(pdev, info);
1218 if (ret)
1219 return ret;
1220
1221 /*
1222 * We need all the GPIO drivers to probe FIRST, or we will not be able
1223 * to obtain references to the struct gpio_chip * for them, and we
1224 * need this to proceed.
1225 */
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001226 for (i = 0; i < gpio_banks; i++)
1227 if (gpio_chips[i])
1228 ngpio_chips_enabled++;
1229
1230 if (ngpio_chips_enabled < info->nactive_banks) {
1231 dev_warn(&pdev->dev,
1232 "All GPIO chips are not registered yet (%d/%d)\n",
1233 ngpio_chips_enabled, info->nactive_banks);
1234 devm_kfree(&pdev->dev, info);
1235 return -EPROBE_DEFER;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001236 }
1237
1238 at91_pinctrl_desc.name = dev_name(&pdev->dev);
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001239 at91_pinctrl_desc.npins = gpio_banks * MAX_NB_GPIO_PER_BANK;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001240 at91_pinctrl_desc.pins = pdesc =
1241 devm_kzalloc(&pdev->dev, sizeof(*pdesc) * at91_pinctrl_desc.npins, GFP_KERNEL);
1242
1243 if (!at91_pinctrl_desc.pins)
1244 return -ENOMEM;
1245
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001246 for (i = 0, k = 0; i < gpio_banks; i++) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001247 for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) {
1248 pdesc->number = k;
1249 pdesc->name = kasprintf(GFP_KERNEL, "pio%c%d", i + 'A', j);
1250 pdesc++;
1251 }
1252 }
1253
1254 platform_set_drvdata(pdev, info);
Laxman Dewangan5c674252016-02-28 14:32:19 +05301255 info->pctl = devm_pinctrl_register(&pdev->dev, &at91_pinctrl_desc,
1256 info);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001257
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001258 if (IS_ERR(info->pctl)) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001259 dev_err(&pdev->dev, "could not register AT91 pinctrl driver\n");
Masahiro Yamada323de9e2015-06-09 13:01:16 +09001260 return PTR_ERR(info->pctl);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001261 }
1262
1263 /* We will handle a range of GPIO pins */
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001264 for (i = 0; i < gpio_banks; i++)
1265 if (gpio_chips[i])
1266 pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001267
1268 dev_info(&pdev->dev, "initialized AT91 pinctrl driver\n");
1269
1270 return 0;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001271}
1272
Richard Genoud8af584b2014-02-17 17:57:26 +01001273static int at91_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
1274{
Linus Walleij370ea612015-12-08 09:27:45 +01001275 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Richard Genoud8af584b2014-02-17 17:57:26 +01001276 void __iomem *pio = at91_gpio->regbase;
1277 unsigned mask = 1 << offset;
1278 u32 osr;
1279
1280 osr = readl_relaxed(pio + PIO_OSR);
1281 return !(osr & mask);
1282}
1283
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001284static int at91_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
1285{
Linus Walleij370ea612015-12-08 09:27:45 +01001286 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001287 void __iomem *pio = at91_gpio->regbase;
1288 unsigned mask = 1 << offset;
1289
1290 writel_relaxed(mask, pio + PIO_ODR);
1291 return 0;
1292}
1293
1294static int at91_gpio_get(struct gpio_chip *chip, unsigned offset)
1295{
Linus Walleij370ea612015-12-08 09:27:45 +01001296 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001297 void __iomem *pio = at91_gpio->regbase;
1298 unsigned mask = 1 << offset;
1299 u32 pdsr;
1300
1301 pdsr = readl_relaxed(pio + PIO_PDSR);
1302 return (pdsr & mask) != 0;
1303}
1304
1305static void at91_gpio_set(struct gpio_chip *chip, unsigned offset,
1306 int val)
1307{
Linus Walleij370ea612015-12-08 09:27:45 +01001308 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001309 void __iomem *pio = at91_gpio->regbase;
1310 unsigned mask = 1 << offset;
1311
1312 writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
1313}
1314
Alexander Stein1893b2c2015-04-02 11:55:49 +02001315static void at91_gpio_set_multiple(struct gpio_chip *chip,
1316 unsigned long *mask, unsigned long *bits)
1317{
Linus Walleij370ea612015-12-08 09:27:45 +01001318 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Alexander Stein1893b2c2015-04-02 11:55:49 +02001319 void __iomem *pio = at91_gpio->regbase;
1320
1321#define BITS_MASK(bits) (((bits) == 32) ? ~0U : (BIT(bits) - 1))
1322 /* Mask additionally to ngpio as not all GPIO controllers have 32 pins */
1323 uint32_t set_mask = (*mask & *bits) & BITS_MASK(chip->ngpio);
1324 uint32_t clear_mask = (*mask & ~(*bits)) & BITS_MASK(chip->ngpio);
1325
1326 writel_relaxed(set_mask, pio + PIO_SODR);
1327 writel_relaxed(clear_mask, pio + PIO_CODR);
1328}
1329
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001330static int at91_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
1331 int val)
1332{
Linus Walleij370ea612015-12-08 09:27:45 +01001333 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001334 void __iomem *pio = at91_gpio->regbase;
1335 unsigned mask = 1 << offset;
1336
1337 writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
1338 writel_relaxed(mask, pio + PIO_OER);
1339
1340 return 0;
1341}
1342
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001343#ifdef CONFIG_DEBUG_FS
1344static void at91_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1345{
1346 enum at91_mux mode;
1347 int i;
Linus Walleij370ea612015-12-08 09:27:45 +01001348 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001349 void __iomem *pio = at91_gpio->regbase;
1350
1351 for (i = 0; i < chip->ngpio; i++) {
Alexander Stein47f22712014-04-14 20:53:08 +02001352 unsigned mask = pin_to_mask(i);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001353 const char *gpio_label;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001354
1355 gpio_label = gpiochip_is_requested(chip, i);
1356 if (!gpio_label)
1357 continue;
1358 mode = at91_gpio->ops->get_periph(pio, mask);
1359 seq_printf(s, "[%s] GPIO%s%d: ",
1360 gpio_label, chip->label, i);
1361 if (mode == AT91_MUX_GPIO) {
Matthieu Crapet853b6bf2014-11-18 15:43:45 +01001362 seq_printf(s, "[gpio] ");
1363 seq_printf(s, "%s ",
1364 readl_relaxed(pio + PIO_OSR) & mask ?
1365 "output" : "input");
1366 seq_printf(s, "%s\n",
1367 readl_relaxed(pio + PIO_PDSR) & mask ?
1368 "set" : "clear");
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001369 } else {
1370 seq_printf(s, "[periph %c]\n",
1371 mode + 'A' - 1);
1372 }
1373 }
1374}
1375#else
1376#define at91_gpio_dbg_show NULL
1377#endif
1378
1379/* Several AIC controller irqs are dispatched through this GPIO handler.
1380 * To use any AT91_PIN_* as an externally triggered IRQ, first call
1381 * at91_set_gpio_input() then maybe enable its glitch filter.
1382 * Then just request_irq() with the pin ID; it works like any ARM IRQ
1383 * handler.
1384 * First implementation always triggers on rising and falling edges
1385 * whereas the newer PIO3 can be additionally configured to trigger on
1386 * level, edge with any polarity.
1387 *
1388 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
1389 * configuring them with at91_set_a_periph() or at91_set_b_periph().
1390 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
1391 */
1392
1393static void gpio_irq_mask(struct irq_data *d)
1394{
1395 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1396 void __iomem *pio = at91_gpio->regbase;
1397 unsigned mask = 1 << d->hwirq;
1398
1399 if (pio)
1400 writel_relaxed(mask, pio + PIO_IDR);
1401}
1402
1403static void gpio_irq_unmask(struct irq_data *d)
1404{
1405 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1406 void __iomem *pio = at91_gpio->regbase;
1407 unsigned mask = 1 << d->hwirq;
1408
1409 if (pio)
1410 writel_relaxed(mask, pio + PIO_IER);
1411}
1412
1413static int gpio_irq_type(struct irq_data *d, unsigned type)
1414{
1415 switch (type) {
1416 case IRQ_TYPE_NONE:
1417 case IRQ_TYPE_EDGE_BOTH:
1418 return 0;
1419 default:
1420 return -EINVAL;
1421 }
1422}
1423
1424/* Alternate irq type for PIO3 support */
1425static int alt_gpio_irq_type(struct irq_data *d, unsigned type)
1426{
1427 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1428 void __iomem *pio = at91_gpio->regbase;
1429 unsigned mask = 1 << d->hwirq;
1430
1431 switch (type) {
1432 case IRQ_TYPE_EDGE_RISING:
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001433 irq_set_handler_locked(d, handle_simple_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001434 writel_relaxed(mask, pio + PIO_ESR);
1435 writel_relaxed(mask, pio + PIO_REHLSR);
1436 break;
1437 case IRQ_TYPE_EDGE_FALLING:
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001438 irq_set_handler_locked(d, handle_simple_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001439 writel_relaxed(mask, pio + PIO_ESR);
1440 writel_relaxed(mask, pio + PIO_FELLSR);
1441 break;
1442 case IRQ_TYPE_LEVEL_LOW:
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001443 irq_set_handler_locked(d, handle_level_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001444 writel_relaxed(mask, pio + PIO_LSR);
1445 writel_relaxed(mask, pio + PIO_FELLSR);
1446 break;
1447 case IRQ_TYPE_LEVEL_HIGH:
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001448 irq_set_handler_locked(d, handle_level_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001449 writel_relaxed(mask, pio + PIO_LSR);
1450 writel_relaxed(mask, pio + PIO_REHLSR);
1451 break;
1452 case IRQ_TYPE_EDGE_BOTH:
1453 /*
1454 * disable additional interrupt modes:
1455 * fall back to default behavior
1456 */
Thomas Gleixnerc639845b2015-06-23 15:52:49 +02001457 irq_set_handler_locked(d, handle_simple_irq);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001458 writel_relaxed(mask, pio + PIO_AIMDR);
1459 return 0;
1460 case IRQ_TYPE_NONE:
1461 default:
1462 pr_warn("AT91: No type for irq %d\n", gpio_to_irq(d->irq));
1463 return -EINVAL;
1464 }
1465
1466 /* enable additional interrupt modes */
1467 writel_relaxed(mask, pio + PIO_AIMER);
1468
1469 return 0;
1470}
1471
Alexander Stein80cc3732014-04-15 22:09:41 +02001472static void gpio_irq_ack(struct irq_data *d)
1473{
1474 /* the interrupt is already cleared before by reading ISR */
1475}
1476
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001477#ifdef CONFIG_PM
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001478
1479static u32 wakeups[MAX_GPIO_BANKS];
1480static u32 backups[MAX_GPIO_BANKS];
1481
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001482static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
1483{
1484 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1485 unsigned bank = at91_gpio->pioc_idx;
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001486 unsigned mask = 1 << d->hwirq;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001487
1488 if (unlikely(bank >= MAX_GPIO_BANKS))
1489 return -EINVAL;
1490
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001491 if (state)
1492 wakeups[bank] |= mask;
1493 else
1494 wakeups[bank] &= ~mask;
1495
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001496 irq_set_irq_wake(at91_gpio->pioc_virq, state);
1497
1498 return 0;
1499}
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001500
1501void at91_pinctrl_gpio_suspend(void)
1502{
1503 int i;
1504
1505 for (i = 0; i < gpio_banks; i++) {
1506 void __iomem *pio;
1507
1508 if (!gpio_chips[i])
1509 continue;
1510
1511 pio = gpio_chips[i]->regbase;
1512
Ben Dooksd480239b2015-03-26 12:18:49 +00001513 backups[i] = readl_relaxed(pio + PIO_IMR);
1514 writel_relaxed(backups[i], pio + PIO_IDR);
1515 writel_relaxed(wakeups[i], pio + PIO_IER);
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001516
Boris BREZILLON795f9952013-12-15 19:30:51 +01001517 if (!wakeups[i])
1518 clk_disable_unprepare(gpio_chips[i]->clock);
1519 else
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001520 printk(KERN_DEBUG "GPIO-%c may wake for %08x\n",
1521 'A'+i, wakeups[i]);
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001522 }
1523}
1524
1525void at91_pinctrl_gpio_resume(void)
1526{
1527 int i;
1528
1529 for (i = 0; i < gpio_banks; i++) {
1530 void __iomem *pio;
1531
1532 if (!gpio_chips[i])
1533 continue;
1534
1535 pio = gpio_chips[i]->regbase;
1536
Boris BREZILLON37ef1d92013-12-15 19:30:52 +01001537 if (!wakeups[i])
1538 clk_prepare_enable(gpio_chips[i]->clock);
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001539
Ben Dooksd480239b2015-03-26 12:18:49 +00001540 writel_relaxed(wakeups[i], pio + PIO_IDR);
1541 writel_relaxed(backups[i], pio + PIO_IER);
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001542 }
1543}
1544
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001545#else
1546#define gpio_irq_set_wake NULL
Ludovic Desroches647f8d92013-03-08 16:18:21 +01001547#endif /* CONFIG_PM */
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001548
1549static struct irq_chip gpio_irqchip = {
1550 .name = "GPIO",
Alexander Stein80cc3732014-04-15 22:09:41 +02001551 .irq_ack = gpio_irq_ack,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001552 .irq_disable = gpio_irq_mask,
1553 .irq_mask = gpio_irq_mask,
1554 .irq_unmask = gpio_irq_unmask,
1555 /* .irq_set_type is set dynamically */
1556 .irq_set_wake = gpio_irq_set_wake,
1557};
1558
Thomas Gleixnerbd0b9ac2015-09-14 10:42:37 +02001559static void gpio_irq_handler(struct irq_desc *desc)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001560{
Jiang Liu5663bb22015-06-04 12:13:16 +08001561 struct irq_chip *chip = irq_desc_get_chip(desc);
Alexander Stein80cc3732014-04-15 22:09:41 +02001562 struct gpio_chip *gpio_chip = irq_desc_get_handler_data(desc);
Linus Walleij370ea612015-12-08 09:27:45 +01001563 struct at91_gpio_chip *at91_gpio = gpiochip_get_data(gpio_chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001564 void __iomem *pio = at91_gpio->regbase;
1565 unsigned long isr;
1566 int n;
1567
1568 chained_irq_enter(chip, desc);
1569 for (;;) {
1570 /* Reading ISR acks pending (edge triggered) GPIO interrupts.
Alexandre Bellonic2eb9e72013-12-07 14:08:52 +01001571 * When there are none pending, we're finished unless we need
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001572 * to process multiple banks (like ID_PIOCDE on sam9263).
1573 */
1574 isr = readl_relaxed(pio + PIO_ISR) & readl_relaxed(pio + PIO_IMR);
1575 if (!isr) {
1576 if (!at91_gpio->next)
1577 break;
1578 at91_gpio = at91_gpio->next;
1579 pio = at91_gpio->regbase;
Alexander Steincccb0c32014-04-24 19:55:39 +02001580 gpio_chip = &at91_gpio->chip;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001581 continue;
1582 }
1583
Wei Yongjun05daa162012-10-26 22:50:54 +08001584 for_each_set_bit(n, &isr, BITS_PER_LONG) {
Alexander Stein80cc3732014-04-15 22:09:41 +02001585 generic_handle_irq(irq_find_mapping(
1586 gpio_chip->irqdomain, n));
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001587 }
1588 }
1589 chained_irq_exit(chip, desc);
1590 /* now it may re-trigger */
1591}
1592
Pramod Gurav834e1672014-09-09 15:50:37 +05301593static int at91_gpio_of_irq_setup(struct platform_device *pdev,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001594 struct at91_gpio_chip *at91_gpio)
1595{
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001596 struct gpio_chip *gpiochip_prev = NULL;
Alexander Steincccb0c32014-04-24 19:55:39 +02001597 struct at91_gpio_chip *prev = NULL;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001598 struct irq_data *d = irq_get_irq_data(at91_gpio->pioc_virq);
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001599 int ret, i;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001600
1601 at91_gpio->pioc_hwirq = irqd_to_hwirq(d);
1602
1603 /* Setup proper .irq_set_type function */
1604 gpio_irqchip.irq_set_type = at91_gpio->ops->irq_type;
1605
1606 /* Disable irqs of this PIO controller */
1607 writel_relaxed(~0, at91_gpio->regbase + PIO_IDR);
1608
Alexander Stein80cc3732014-04-15 22:09:41 +02001609 /*
1610 * Let the generic code handle this edge IRQ, the the chained
1611 * handler will perform the actual work of handling the parent
1612 * interrupt.
1613 */
1614 ret = gpiochip_irqchip_add(&at91_gpio->chip,
1615 &gpio_irqchip,
1616 0,
1617 handle_edge_irq,
1618 IRQ_TYPE_EDGE_BOTH);
Pramod Gurav834e1672014-09-09 15:50:37 +05301619 if (ret) {
1620 dev_err(&pdev->dev, "at91_gpio.%d: Couldn't add irqchip to gpiochip.\n",
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001621 at91_gpio->pioc_idx);
Pramod Gurav834e1672014-09-09 15:50:37 +05301622 return ret;
1623 }
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001624
Alexander Steincccb0c32014-04-24 19:55:39 +02001625 /* The top level handler handles one bank of GPIOs, except
1626 * on some SoC it can handle up to three...
1627 * We only set up the handler for the first of the list.
1628 */
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001629 gpiochip_prev = irq_get_handler_data(at91_gpio->pioc_virq);
1630 if (!gpiochip_prev) {
1631 /* Then register the chain on the parent IRQ */
1632 gpiochip_set_chained_irqchip(&at91_gpio->chip,
1633 &gpio_irqchip,
1634 at91_gpio->pioc_virq,
1635 gpio_irq_handler);
Alexander Steincccb0c32014-04-24 19:55:39 +02001636 return 0;
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001637 }
Alexander Steincccb0c32014-04-24 19:55:39 +02001638
Linus Walleij370ea612015-12-08 09:27:45 +01001639 prev = gpiochip_get_data(gpiochip_prev);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001640
Jean-Christophe PLAGNIOL-VILLARDa0b957f2015-01-16 16:31:05 +01001641 /* we can only have 2 banks before */
1642 for (i = 0; i < 2; i++) {
1643 if (prev->next) {
1644 prev = prev->next;
1645 } else {
1646 prev->next = at91_gpio;
1647 return 0;
1648 }
1649 }
1650
1651 return -EINVAL;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001652}
1653
1654/* This structure is replicated for each GPIO block allocated at probe time */
Alexander Stein234b6512016-04-29 14:50:02 +02001655static const struct gpio_chip at91_gpio_template = {
Jonas Gorski98c85d52015-10-11 17:34:19 +02001656 .request = gpiochip_generic_request,
1657 .free = gpiochip_generic_free,
Richard Genoud8af584b2014-02-17 17:57:26 +01001658 .get_direction = at91_gpio_get_direction,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001659 .direction_input = at91_gpio_direction_input,
1660 .get = at91_gpio_get,
1661 .direction_output = at91_gpio_direction_output,
1662 .set = at91_gpio_set,
Alexander Stein1893b2c2015-04-02 11:55:49 +02001663 .set_multiple = at91_gpio_set_multiple,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001664 .dbg_show = at91_gpio_dbg_show,
Linus Walleij9fb1f392013-12-04 14:42:46 +01001665 .can_sleep = false,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001666 .ngpio = MAX_NB_GPIO_PER_BANK,
1667};
1668
Fabian Frederickbaa9946e2015-03-16 20:59:09 +01001669static const struct of_device_id at91_gpio_of_match[] = {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001670 { .compatible = "atmel,at91sam9x5-gpio", .data = &at91sam9x5_ops, },
1671 { .compatible = "atmel,at91rm9200-gpio", .data = &at91rm9200_ops },
1672 { /* sentinel */ }
1673};
1674
Greg Kroah-Hartman150632b2012-12-21 13:10:23 -08001675static int at91_gpio_probe(struct platform_device *pdev)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001676{
1677 struct device_node *np = pdev->dev.of_node;
1678 struct resource *res;
1679 struct at91_gpio_chip *at91_chip = NULL;
1680 struct gpio_chip *chip;
1681 struct pinctrl_gpio_range *range;
1682 int ret = 0;
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001683 int irq, i;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001684 int alias_idx = of_alias_get_id(np, "gpio");
1685 uint32_t ngpio;
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001686 char **names;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001687
1688 BUG_ON(alias_idx >= ARRAY_SIZE(gpio_chips));
1689 if (gpio_chips[alias_idx]) {
1690 ret = -EBUSY;
1691 goto err;
1692 }
1693
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001694 irq = platform_get_irq(pdev, 0);
1695 if (irq < 0) {
1696 ret = irq;
1697 goto err;
1698 }
1699
1700 at91_chip = devm_kzalloc(&pdev->dev, sizeof(*at91_chip), GFP_KERNEL);
1701 if (!at91_chip) {
1702 ret = -ENOMEM;
1703 goto err;
1704 }
1705
Wolfram Sangf50b9e12013-05-10 10:17:03 +02001706 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Thierry Reding9e0c1fb2013-01-21 11:09:14 +01001707 at91_chip->regbase = devm_ioremap_resource(&pdev->dev, res);
1708 if (IS_ERR(at91_chip->regbase)) {
1709 ret = PTR_ERR(at91_chip->regbase);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001710 goto err;
1711 }
1712
Sachin Kamat3c936002013-03-15 10:07:03 +05301713 at91_chip->ops = (struct at91_pinctrl_mux_ops *)
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001714 of_match_device(at91_gpio_of_match, &pdev->dev)->data;
1715 at91_chip->pioc_virq = irq;
1716 at91_chip->pioc_idx = alias_idx;
1717
Pramod Gurav02b837f2014-08-31 16:51:52 +05301718 at91_chip->clock = devm_clk_get(&pdev->dev, NULL);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001719 if (IS_ERR(at91_chip->clock)) {
1720 dev_err(&pdev->dev, "failed to get clock, ignoring.\n");
Pramod Gurav70e41972014-09-09 15:50:36 +05301721 ret = PTR_ERR(at91_chip->clock);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001722 goto err;
1723 }
1724
Pramod Gurav70e41972014-09-09 15:50:36 +05301725 ret = clk_prepare(at91_chip->clock);
1726 if (ret)
1727 goto clk_prepare_err;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001728
1729 /* enable PIO controller's clock */
Pramod Gurav70e41972014-09-09 15:50:36 +05301730 ret = clk_enable(at91_chip->clock);
1731 if (ret) {
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001732 dev_err(&pdev->dev, "failed to enable clock, ignoring.\n");
Pramod Gurav70e41972014-09-09 15:50:36 +05301733 goto clk_enable_err;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001734 }
1735
1736 at91_chip->chip = at91_gpio_template;
1737
1738 chip = &at91_chip->chip;
1739 chip->of_node = np;
1740 chip->label = dev_name(&pdev->dev);
Linus Walleij58383c782015-11-04 09:56:26 +01001741 chip->parent = &pdev->dev;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001742 chip->owner = THIS_MODULE;
1743 chip->base = alias_idx * MAX_NB_GPIO_PER_BANK;
1744
1745 if (!of_property_read_u32(np, "#gpio-lines", &ngpio)) {
1746 if (ngpio >= MAX_NB_GPIO_PER_BANK)
1747 pr_err("at91_gpio.%d, gpio-nb >= %d failback to %d\n",
1748 alias_idx, MAX_NB_GPIO_PER_BANK, MAX_NB_GPIO_PER_BANK);
1749 else
1750 chip->ngpio = ngpio;
1751 }
1752
Sachin Kamat3c936002013-03-15 10:07:03 +05301753 names = devm_kzalloc(&pdev->dev, sizeof(char *) * chip->ngpio,
1754 GFP_KERNEL);
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001755
1756 if (!names) {
1757 ret = -ENOMEM;
Pramod Gurav70e41972014-09-09 15:50:36 +05301758 goto clk_enable_err;
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001759 }
1760
1761 for (i = 0; i < chip->ngpio; i++)
1762 names[i] = kasprintf(GFP_KERNEL, "pio%c%d", alias_idx + 'A', i);
1763
Sachin Kamat3c936002013-03-15 10:07:03 +05301764 chip->names = (const char *const *)names;
Jean-Christophe PLAGNIOL-VILLARD32b01a32012-11-07 00:33:34 +08001765
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001766 range = &at91_chip->range;
1767 range->name = chip->label;
1768 range->id = alias_idx;
1769 range->pin_base = range->base = range->id * MAX_NB_GPIO_PER_BANK;
1770
1771 range->npins = chip->ngpio;
1772 range->gc = chip;
1773
Linus Walleij370ea612015-12-08 09:27:45 +01001774 ret = gpiochip_add_data(chip, at91_chip);
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001775 if (ret)
Pramod Gurav70e41972014-09-09 15:50:36 +05301776 goto gpiochip_add_err;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001777
1778 gpio_chips[alias_idx] = at91_chip;
1779 gpio_banks = max(gpio_banks, alias_idx + 1);
1780
Pramod Gurav834e1672014-09-09 15:50:37 +05301781 ret = at91_gpio_of_irq_setup(pdev, at91_chip);
1782 if (ret)
1783 goto irq_setup_err;
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001784
1785 dev_info(&pdev->dev, "at address %p\n", at91_chip->regbase);
1786
1787 return 0;
1788
Pramod Gurav834e1672014-09-09 15:50:37 +05301789irq_setup_err:
1790 gpiochip_remove(chip);
Pramod Gurav70e41972014-09-09 15:50:36 +05301791gpiochip_add_err:
1792 clk_disable(at91_chip->clock);
1793clk_enable_err:
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001794 clk_unprepare(at91_chip->clock);
Pramod Gurav70e41972014-09-09 15:50:36 +05301795clk_prepare_err:
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001796err:
1797 dev_err(&pdev->dev, "Failure %i for GPIO %i\n", ret, alias_idx);
1798
1799 return ret;
1800}
1801
1802static struct platform_driver at91_gpio_driver = {
1803 .driver = {
1804 .name = "gpio-at91",
Sachin Kamat606fca92013-09-28 17:38:48 +05301805 .of_match_table = at91_gpio_of_match,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001806 },
1807 .probe = at91_gpio_probe,
1808};
1809
1810static struct platform_driver at91_pinctrl_driver = {
1811 .driver = {
1812 .name = "pinctrl-at91",
Sachin Kamat606fca92013-09-28 17:38:48 +05301813 .of_match_table = at91_pinctrl_of_match,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001814 },
1815 .probe = at91_pinctrl_probe,
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001816};
1817
Thierry Redingbab7f5a2015-12-02 17:31:55 +01001818static struct platform_driver * const drivers[] = {
1819 &at91_gpio_driver,
1820 &at91_pinctrl_driver,
1821};
1822
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001823static int __init at91_pinctrl_init(void)
1824{
Thierry Redingbab7f5a2015-12-02 17:31:55 +01001825 return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001826}
1827arch_initcall(at91_pinctrl_init);
1828
1829static void __exit at91_pinctrl_exit(void)
1830{
Thierry Redingbab7f5a2015-12-02 17:31:55 +01001831 platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
Jean-Christophe PLAGNIOL-VILLARD6732ae52012-07-12 23:35:02 +08001832}
1833
1834module_exit(at91_pinctrl_exit);
1835MODULE_AUTHOR("Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>");
1836MODULE_DESCRIPTION("Atmel AT91 pinctrl driver");
1837MODULE_LICENSE("GPL v2");