1 /*
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>
19 #include <linux/io.h>
20 #include <linux/gpio.h>
21 #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
28 #include <mach/hardware.h>
29 #include <mach/at91_pio.h>
30
31 #include "core.h"
32
33 #define MAX_GPIO_BANKS 5
34 #define MAX_NB_GPIO_PER_BANK 32
35
36 struct at91_pinctrl_mux_ops;
37
38 struct at91_gpio_chip {
39 struct gpio_chip chip;
40 struct pinctrl_gpio_range range;
41 struct at91_gpio_chip *next; /* Bank sharing same clock */
42 int pioc_hwirq; /* PIO bank interrupt identifier on AIC */
43 int pioc_virq; /* PIO bank Linux virtual interrupt */
44 int pioc_idx; /* PIO bank index */
45 void __iomem *regbase; /* PIO bank virtual address */
46 struct clk *clock; /* associated clock */
47 struct at91_pinctrl_mux_ops *ops; /* ops */
48 };
49
50 #define to_at91_gpio_chip(c) container_of(c, struct at91_gpio_chip, chip)
51
52 static struct at91_gpio_chip *gpio_chips[MAX_GPIO_BANKS];
53
54 static int gpio_banks;
55
56 #define PULL_UP (1 << 0)
57 #define MULTI_DRIVE (1 << 1)
58 #define DEGLITCH (1 << 2)
59 #define PULL_DOWN (1 << 3)
60 #define DIS_SCHMIT (1 << 4)
61 #define DRIVE_STRENGTH_SHIFT 5
62 #define DRIVE_STRENGTH_MASK 0x3
63 #define DRIVE_STRENGTH (DRIVE_STRENGTH_MASK << DRIVE_STRENGTH_SHIFT)
64 #define DEBOUNCE (1 << 16)
65 #define DEBOUNCE_VAL_SHIFT 17
66 #define DEBOUNCE_VAL (0x3fff << DEBOUNCE_VAL_SHIFT)
67
68 /**
69 * 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 */
77 #define DRIVE_STRENGTH_DEFAULT (0 << DRIVE_STRENGTH_SHIFT)
78 #define DRIVE_STRENGTH_LOW (1 << DRIVE_STRENGTH_SHIFT)
79 #define DRIVE_STRENGTH_MED (2 << DRIVE_STRENGTH_SHIFT)
80 #define DRIVE_STRENGTH_HI (3 << DRIVE_STRENGTH_SHIFT)
81
82 /**
83 * struct at91_pmx_func - describes AT91 pinmux functions
84 * @name: the name of this specific function
85 * @groups: corresponding pin groups
86 * @ngroups: the number of groups
87 */
88 struct at91_pmx_func {
89 const char *name;
90 const char **groups;
91 unsigned ngroups;
92 };
93
94 enum at91_mux {
95 AT91_MUX_GPIO = 0,
96 AT91_MUX_PERIPH_A = 1,
97 AT91_MUX_PERIPH_B = 2,
98 AT91_MUX_PERIPH_C = 3,
99 AT91_MUX_PERIPH_D = 4,
100 };
101
102 /**
103 * struct at91_pmx_pin - describes an At91 pin mux
104 * @bank: the bank of the pin
105 * @pin: the pin number in the @bank
106 * @mux: the mux mode : gpio or periph_x of the pin i.e. alternate function.
107 * @conf: the configuration of the pin: PULL_UP, MULTIDRIVE etc...
108 */
109 struct at91_pmx_pin {
110 uint32_t bank;
111 uint32_t pin;
112 enum at91_mux mux;
113 unsigned long conf;
114 };
115
116 /**
117 * struct at91_pin_group - describes an At91 pin group
118 * @name: the name of this specific pin group
119 * @pins_conf: the mux mode for each pin in this group. The size of this
120 * array is the same as pins.
121 * @pins: an array of discrete physical pins used in this group, taken
122 * from the driver-local pin enumeration space
123 * @npins: the number of pins in this group array, i.e. the number of
124 * elements in .pins so we can iterate over that array
125 */
126 struct at91_pin_group {
127 const char *name;
128 struct at91_pmx_pin *pins_conf;
129 unsigned int *pins;
130 unsigned npins;
131 };
132
133 /**
134 * struct at91_pinctrl_mux_ops - describes an AT91 mux ops group
135 * on new IP with support for periph C and D the way to mux in
136 * periph A and B has changed
137 * So provide the right call back
138 * if not present means the IP does not support it
139 * @get_periph: return the periph mode configured
140 * @mux_A_periph: mux as periph A
141 * @mux_B_periph: mux as periph B
142 * @mux_C_periph: mux as periph C
143 * @mux_D_periph: mux as periph D
144 * @get_deglitch: get deglitch status
145 * @set_deglitch: enable/disable deglitch
146 * @get_debounce: get debounce status
147 * @set_debounce: enable/disable debounce
148 * @get_pulldown: get pulldown status
149 * @set_pulldown: enable/disable pulldown
150 * @get_schmitt_trig: get schmitt trigger status
151 * @disable_schmitt_trig: disable schmitt trigger
152 * @irq_type: return irq type
153 */
154 struct at91_pinctrl_mux_ops {
155 enum at91_mux (*get_periph)(void __iomem *pio, unsigned mask);
156 void (*mux_A_periph)(void __iomem *pio, unsigned mask);
157 void (*mux_B_periph)(void __iomem *pio, unsigned mask);
158 void (*mux_C_periph)(void __iomem *pio, unsigned mask);
159 void (*mux_D_periph)(void __iomem *pio, unsigned mask);
160 bool (*get_deglitch)(void __iomem *pio, unsigned pin);
161 void (*set_deglitch)(void __iomem *pio, unsigned mask, bool is_on);
162 bool (*get_debounce)(void __iomem *pio, unsigned pin, u32 *div);
163 void (*set_debounce)(void __iomem *pio, unsigned mask, bool is_on, u32 div);
164 bool (*get_pulldown)(void __iomem *pio, unsigned pin);
165 void (*set_pulldown)(void __iomem *pio, unsigned mask, bool is_on);
166 bool (*get_schmitt_trig)(void __iomem *pio, unsigned pin);
167 void (*disable_schmitt_trig)(void __iomem *pio, unsigned mask);
168 unsigned (*get_drivestrength)(void __iomem *pio, unsigned pin);
169 void (*set_drivestrength)(void __iomem *pio, unsigned pin,
170 u32 strength);
171 /* irq */
172 int (*irq_type)(struct irq_data *d, unsigned type);
173 };
174
175 static int gpio_irq_type(struct irq_data *d, unsigned type);
176 static int alt_gpio_irq_type(struct irq_data *d, unsigned type);
177
178 struct at91_pinctrl {
179 struct device *dev;
180 struct pinctrl_dev *pctl;
181
182 int nactive_banks;
183
184 uint32_t *mux_mask;
185 int nmux;
186
187 struct at91_pmx_func *functions;
188 int nfunctions;
189
190 struct at91_pin_group *groups;
191 int ngroups;
192
193 struct at91_pinctrl_mux_ops *ops;
194 };
195
at91_pinctrl_find_group_by_name(const struct at91_pinctrl * info,const char * name)196 static const inline struct at91_pin_group *at91_pinctrl_find_group_by_name(
197 const struct at91_pinctrl *info,
198 const char *name)
199 {
200 const struct at91_pin_group *grp = NULL;
201 int i;
202
203 for (i = 0; i < info->ngroups; i++) {
204 if (strcmp(info->groups[i].name, name))
205 continue;
206
207 grp = &info->groups[i];
208 dev_dbg(info->dev, "%s: %d 0:%d\n", name, grp->npins, grp->pins[0]);
209 break;
210 }
211
212 return grp;
213 }
214
at91_get_groups_count(struct pinctrl_dev * pctldev)215 static int at91_get_groups_count(struct pinctrl_dev *pctldev)
216 {
217 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
218
219 return info->ngroups;
220 }
221
at91_get_group_name(struct pinctrl_dev * pctldev,unsigned selector)222 static const char *at91_get_group_name(struct pinctrl_dev *pctldev,
223 unsigned selector)
224 {
225 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
226
227 return info->groups[selector].name;
228 }
229
at91_get_group_pins(struct pinctrl_dev * pctldev,unsigned selector,const unsigned ** pins,unsigned * npins)230 static int at91_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
231 const unsigned **pins,
232 unsigned *npins)
233 {
234 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
235
236 if (selector >= info->ngroups)
237 return -EINVAL;
238
239 *pins = info->groups[selector].pins;
240 *npins = info->groups[selector].npins;
241
242 return 0;
243 }
244
at91_pin_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned offset)245 static void at91_pin_dbg_show(struct pinctrl_dev *pctldev, struct seq_file *s,
246 unsigned offset)
247 {
248 seq_printf(s, "%s", dev_name(pctldev->dev));
249 }
250
at91_dt_node_to_map(struct pinctrl_dev * pctldev,struct device_node * np,struct pinctrl_map ** map,unsigned * num_maps)251 static int at91_dt_node_to_map(struct pinctrl_dev *pctldev,
252 struct device_node *np,
253 struct pinctrl_map **map, unsigned *num_maps)
254 {
255 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
256 const struct at91_pin_group *grp;
257 struct pinctrl_map *new_map;
258 struct device_node *parent;
259 int map_num = 1;
260 int i;
261
262 /*
263 * first find the group of this node and check if we need to create
264 * config maps for pins
265 */
266 grp = at91_pinctrl_find_group_by_name(info, np->name);
267 if (!grp) {
268 dev_err(info->dev, "unable to find group for node %s\n",
269 np->name);
270 return -EINVAL;
271 }
272
273 map_num += grp->npins;
274 new_map = devm_kzalloc(pctldev->dev, sizeof(*new_map) * map_num, GFP_KERNEL);
275 if (!new_map)
276 return -ENOMEM;
277
278 *map = new_map;
279 *num_maps = map_num;
280
281 /* create mux map */
282 parent = of_get_parent(np);
283 if (!parent) {
284 devm_kfree(pctldev->dev, new_map);
285 return -EINVAL;
286 }
287 new_map[0].type = PIN_MAP_TYPE_MUX_GROUP;
288 new_map[0].data.mux.function = parent->name;
289 new_map[0].data.mux.group = np->name;
290 of_node_put(parent);
291
292 /* create config map */
293 new_map++;
294 for (i = 0; i < grp->npins; i++) {
295 new_map[i].type = PIN_MAP_TYPE_CONFIGS_PIN;
296 new_map[i].data.configs.group_or_pin =
297 pin_get_name(pctldev, grp->pins[i]);
298 new_map[i].data.configs.configs = &grp->pins_conf[i].conf;
299 new_map[i].data.configs.num_configs = 1;
300 }
301
302 dev_dbg(pctldev->dev, "maps: function %s group %s num %d\n",
303 (*map)->data.mux.function, (*map)->data.mux.group, map_num);
304
305 return 0;
306 }
307
at91_dt_free_map(struct pinctrl_dev * pctldev,struct pinctrl_map * map,unsigned num_maps)308 static void at91_dt_free_map(struct pinctrl_dev *pctldev,
309 struct pinctrl_map *map, unsigned num_maps)
310 {
311 }
312
313 static const struct pinctrl_ops at91_pctrl_ops = {
314 .get_groups_count = at91_get_groups_count,
315 .get_group_name = at91_get_group_name,
316 .get_group_pins = at91_get_group_pins,
317 .pin_dbg_show = at91_pin_dbg_show,
318 .dt_node_to_map = at91_dt_node_to_map,
319 .dt_free_map = at91_dt_free_map,
320 };
321
pin_to_controller(struct at91_pinctrl * info,unsigned int bank)322 static void __iomem *pin_to_controller(struct at91_pinctrl *info,
323 unsigned int bank)
324 {
325 return gpio_chips[bank]->regbase;
326 }
327
pin_to_bank(unsigned pin)328 static inline int pin_to_bank(unsigned pin)
329 {
330 return pin /= MAX_NB_GPIO_PER_BANK;
331 }
332
pin_to_mask(unsigned int pin)333 static unsigned pin_to_mask(unsigned int pin)
334 {
335 return 1 << pin;
336 }
337
two_bit_pin_value_shift_amount(unsigned int pin)338 static unsigned two_bit_pin_value_shift_amount(unsigned int pin)
339 {
340 /* return the shift value for a pin for "two bit" per pin registers,
341 * i.e. drive strength */
342 return 2*((pin >= MAX_NB_GPIO_PER_BANK/2)
343 ? pin - MAX_NB_GPIO_PER_BANK/2 : pin);
344 }
345
sama5d3_get_drive_register(unsigned int pin)346 static unsigned sama5d3_get_drive_register(unsigned int pin)
347 {
348 /* drive strength is split between two registers
349 * with two bits per pin */
350 return (pin >= MAX_NB_GPIO_PER_BANK/2)
351 ? SAMA5D3_PIO_DRIVER2 : SAMA5D3_PIO_DRIVER1;
352 }
353
at91sam9x5_get_drive_register(unsigned int pin)354 static unsigned at91sam9x5_get_drive_register(unsigned int pin)
355 {
356 /* drive strength is split between two registers
357 * with two bits per pin */
358 return (pin >= MAX_NB_GPIO_PER_BANK/2)
359 ? AT91SAM9X5_PIO_DRIVER2 : AT91SAM9X5_PIO_DRIVER1;
360 }
361
at91_mux_disable_interrupt(void __iomem * pio,unsigned mask)362 static void at91_mux_disable_interrupt(void __iomem *pio, unsigned mask)
363 {
364 writel_relaxed(mask, pio + PIO_IDR);
365 }
366
at91_mux_get_pullup(void __iomem * pio,unsigned pin)367 static unsigned at91_mux_get_pullup(void __iomem *pio, unsigned pin)
368 {
369 return !((readl_relaxed(pio + PIO_PUSR) >> pin) & 0x1);
370 }
371
at91_mux_set_pullup(void __iomem * pio,unsigned mask,bool on)372 static void at91_mux_set_pullup(void __iomem *pio, unsigned mask, bool on)
373 {
374 if (on)
375 writel_relaxed(mask, pio + PIO_PPDDR);
376
377 writel_relaxed(mask, pio + (on ? PIO_PUER : PIO_PUDR));
378 }
379
at91_mux_get_multidrive(void __iomem * pio,unsigned pin)380 static unsigned at91_mux_get_multidrive(void __iomem *pio, unsigned pin)
381 {
382 return (readl_relaxed(pio + PIO_MDSR) >> pin) & 0x1;
383 }
384
at91_mux_set_multidrive(void __iomem * pio,unsigned mask,bool on)385 static void at91_mux_set_multidrive(void __iomem *pio, unsigned mask, bool on)
386 {
387 writel_relaxed(mask, pio + (on ? PIO_MDER : PIO_MDDR));
388 }
389
at91_mux_set_A_periph(void __iomem * pio,unsigned mask)390 static void at91_mux_set_A_periph(void __iomem *pio, unsigned mask)
391 {
392 writel_relaxed(mask, pio + PIO_ASR);
393 }
394
at91_mux_set_B_periph(void __iomem * pio,unsigned mask)395 static void at91_mux_set_B_periph(void __iomem *pio, unsigned mask)
396 {
397 writel_relaxed(mask, pio + PIO_BSR);
398 }
399
at91_mux_pio3_set_A_periph(void __iomem * pio,unsigned mask)400 static void at91_mux_pio3_set_A_periph(void __iomem *pio, unsigned mask)
401 {
402
403 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask,
404 pio + PIO_ABCDSR1);
405 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
406 pio + PIO_ABCDSR2);
407 }
408
at91_mux_pio3_set_B_periph(void __iomem * pio,unsigned mask)409 static void at91_mux_pio3_set_B_periph(void __iomem *pio, unsigned mask)
410 {
411 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask,
412 pio + PIO_ABCDSR1);
413 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) & ~mask,
414 pio + PIO_ABCDSR2);
415 }
416
at91_mux_pio3_set_C_periph(void __iomem * pio,unsigned mask)417 static void at91_mux_pio3_set_C_periph(void __iomem *pio, unsigned mask)
418 {
419 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) & ~mask, pio + PIO_ABCDSR1);
420 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
421 }
422
at91_mux_pio3_set_D_periph(void __iomem * pio,unsigned mask)423 static void at91_mux_pio3_set_D_periph(void __iomem *pio, unsigned mask)
424 {
425 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR1) | mask, pio + PIO_ABCDSR1);
426 writel_relaxed(readl_relaxed(pio + PIO_ABCDSR2) | mask, pio + PIO_ABCDSR2);
427 }
428
at91_mux_pio3_get_periph(void __iomem * pio,unsigned mask)429 static enum at91_mux at91_mux_pio3_get_periph(void __iomem *pio, unsigned mask)
430 {
431 unsigned select;
432
433 if (readl_relaxed(pio + PIO_PSR) & mask)
434 return AT91_MUX_GPIO;
435
436 select = !!(readl_relaxed(pio + PIO_ABCDSR1) & mask);
437 select |= (!!(readl_relaxed(pio + PIO_ABCDSR2) & mask) << 1);
438
439 return select + 1;
440 }
441
at91_mux_get_periph(void __iomem * pio,unsigned mask)442 static enum at91_mux at91_mux_get_periph(void __iomem *pio, unsigned mask)
443 {
444 unsigned select;
445
446 if (readl_relaxed(pio + PIO_PSR) & mask)
447 return AT91_MUX_GPIO;
448
449 select = readl_relaxed(pio + PIO_ABSR) & mask;
450
451 return select + 1;
452 }
453
at91_mux_get_deglitch(void __iomem * pio,unsigned pin)454 static bool at91_mux_get_deglitch(void __iomem *pio, unsigned pin)
455 {
456 return (__raw_readl(pio + PIO_IFSR) >> pin) & 0x1;
457 }
458
at91_mux_set_deglitch(void __iomem * pio,unsigned mask,bool is_on)459 static void at91_mux_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
460 {
461 __raw_writel(mask, pio + (is_on ? PIO_IFER : PIO_IFDR));
462 }
463
at91_mux_pio3_get_deglitch(void __iomem * pio,unsigned pin)464 static bool at91_mux_pio3_get_deglitch(void __iomem *pio, unsigned pin)
465 {
466 if ((__raw_readl(pio + PIO_IFSR) >> pin) & 0x1)
467 return !((__raw_readl(pio + PIO_IFSCSR) >> pin) & 0x1);
468
469 return false;
470 }
471
at91_mux_pio3_set_deglitch(void __iomem * pio,unsigned mask,bool is_on)472 static void at91_mux_pio3_set_deglitch(void __iomem *pio, unsigned mask, bool is_on)
473 {
474 if (is_on)
475 __raw_writel(mask, pio + PIO_IFSCDR);
476 at91_mux_set_deglitch(pio, mask, is_on);
477 }
478
at91_mux_pio3_get_debounce(void __iomem * pio,unsigned pin,u32 * div)479 static bool at91_mux_pio3_get_debounce(void __iomem *pio, unsigned pin, u32 *div)
480 {
481 *div = __raw_readl(pio + PIO_SCDR);
482
483 return ((__raw_readl(pio + PIO_IFSR) >> pin) & 0x1) &&
484 ((__raw_readl(pio + PIO_IFSCSR) >> pin) & 0x1);
485 }
486
at91_mux_pio3_set_debounce(void __iomem * pio,unsigned mask,bool is_on,u32 div)487 static void at91_mux_pio3_set_debounce(void __iomem *pio, unsigned mask,
488 bool is_on, u32 div)
489 {
490 if (is_on) {
491 __raw_writel(mask, pio + PIO_IFSCER);
492 __raw_writel(div & PIO_SCDR_DIV, pio + PIO_SCDR);
493 __raw_writel(mask, pio + PIO_IFER);
494 } else
495 __raw_writel(mask, pio + PIO_IFSCDR);
496 }
497
at91_mux_pio3_get_pulldown(void __iomem * pio,unsigned pin)498 static bool at91_mux_pio3_get_pulldown(void __iomem *pio, unsigned pin)
499 {
500 return !((__raw_readl(pio + PIO_PPDSR) >> pin) & 0x1);
501 }
502
at91_mux_pio3_set_pulldown(void __iomem * pio,unsigned mask,bool is_on)503 static void at91_mux_pio3_set_pulldown(void __iomem *pio, unsigned mask, bool is_on)
504 {
505 if (is_on)
506 __raw_writel(mask, pio + PIO_PUDR);
507
508 __raw_writel(mask, pio + (is_on ? PIO_PPDER : PIO_PPDDR));
509 }
510
at91_mux_pio3_disable_schmitt_trig(void __iomem * pio,unsigned mask)511 static void at91_mux_pio3_disable_schmitt_trig(void __iomem *pio, unsigned mask)
512 {
513 __raw_writel(__raw_readl(pio + PIO_SCHMITT) | mask, pio + PIO_SCHMITT);
514 }
515
at91_mux_pio3_get_schmitt_trig(void __iomem * pio,unsigned pin)516 static bool at91_mux_pio3_get_schmitt_trig(void __iomem *pio, unsigned pin)
517 {
518 return (__raw_readl(pio + PIO_SCHMITT) >> pin) & 0x1;
519 }
520
read_drive_strength(void __iomem * reg,unsigned pin)521 static inline u32 read_drive_strength(void __iomem *reg, unsigned pin)
522 {
523 unsigned tmp = __raw_readl(reg);
524
525 tmp = tmp >> two_bit_pin_value_shift_amount(pin);
526
527 return tmp & DRIVE_STRENGTH_MASK;
528 }
529
at91_mux_sama5d3_get_drivestrength(void __iomem * pio,unsigned pin)530 static unsigned at91_mux_sama5d3_get_drivestrength(void __iomem *pio,
531 unsigned pin)
532 {
533 unsigned tmp = read_drive_strength(pio +
534 sama5d3_get_drive_register(pin), pin);
535
536 /* SAMA5 strength is 1:1 with our defines,
537 * except 0 is equivalent to low per datasheet */
538 if (!tmp)
539 tmp = DRIVE_STRENGTH_LOW;
540
541 return tmp;
542 }
543
at91_mux_sam9x5_get_drivestrength(void __iomem * pio,unsigned pin)544 static unsigned at91_mux_sam9x5_get_drivestrength(void __iomem *pio,
545 unsigned pin)
546 {
547 unsigned tmp = read_drive_strength(pio +
548 at91sam9x5_get_drive_register(pin), pin);
549
550 /* strength is inverse in SAM9x5s hardware with the pinctrl defines
551 * hardware: 0 = hi, 1 = med, 2 = low, 3 = rsvd */
552 tmp = DRIVE_STRENGTH_HI - tmp;
553
554 return tmp;
555 }
556
set_drive_strength(void __iomem * reg,unsigned pin,u32 strength)557 static void set_drive_strength(void __iomem *reg, unsigned pin, u32 strength)
558 {
559 unsigned tmp = __raw_readl(reg);
560 unsigned shift = two_bit_pin_value_shift_amount(pin);
561
562 tmp &= ~(DRIVE_STRENGTH_MASK << shift);
563 tmp |= strength << shift;
564
565 __raw_writel(tmp, reg);
566 }
567
at91_mux_sama5d3_set_drivestrength(void __iomem * pio,unsigned pin,u32 setting)568 static void at91_mux_sama5d3_set_drivestrength(void __iomem *pio, unsigned pin,
569 u32 setting)
570 {
571 /* do nothing if setting is zero */
572 if (!setting)
573 return;
574
575 /* strength is 1 to 1 with setting for SAMA5 */
576 set_drive_strength(pio + sama5d3_get_drive_register(pin), pin, setting);
577 }
578
at91_mux_sam9x5_set_drivestrength(void __iomem * pio,unsigned pin,u32 setting)579 static void at91_mux_sam9x5_set_drivestrength(void __iomem *pio, unsigned pin,
580 u32 setting)
581 {
582 /* do nothing if setting is zero */
583 if (!setting)
584 return;
585
586 /* strength is inverse on SAM9x5s with our defines
587 * 0 = hi, 1 = med, 2 = low, 3 = rsvd */
588 setting = DRIVE_STRENGTH_HI - setting;
589
590 set_drive_strength(pio + at91sam9x5_get_drive_register(pin), pin,
591 setting);
592 }
593
594 static struct at91_pinctrl_mux_ops at91rm9200_ops = {
595 .get_periph = at91_mux_get_periph,
596 .mux_A_periph = at91_mux_set_A_periph,
597 .mux_B_periph = at91_mux_set_B_periph,
598 .get_deglitch = at91_mux_get_deglitch,
599 .set_deglitch = at91_mux_set_deglitch,
600 .irq_type = gpio_irq_type,
601 };
602
603 static struct at91_pinctrl_mux_ops at91sam9x5_ops = {
604 .get_periph = at91_mux_pio3_get_periph,
605 .mux_A_periph = at91_mux_pio3_set_A_periph,
606 .mux_B_periph = at91_mux_pio3_set_B_periph,
607 .mux_C_periph = at91_mux_pio3_set_C_periph,
608 .mux_D_periph = at91_mux_pio3_set_D_periph,
609 .get_deglitch = at91_mux_pio3_get_deglitch,
610 .set_deglitch = at91_mux_pio3_set_deglitch,
611 .get_debounce = at91_mux_pio3_get_debounce,
612 .set_debounce = at91_mux_pio3_set_debounce,
613 .get_pulldown = at91_mux_pio3_get_pulldown,
614 .set_pulldown = at91_mux_pio3_set_pulldown,
615 .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
616 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
617 .get_drivestrength = at91_mux_sam9x5_get_drivestrength,
618 .set_drivestrength = at91_mux_sam9x5_set_drivestrength,
619 .irq_type = alt_gpio_irq_type,
620 };
621
622 static struct at91_pinctrl_mux_ops sama5d3_ops = {
623 .get_periph = at91_mux_pio3_get_periph,
624 .mux_A_periph = at91_mux_pio3_set_A_periph,
625 .mux_B_periph = at91_mux_pio3_set_B_periph,
626 .mux_C_periph = at91_mux_pio3_set_C_periph,
627 .mux_D_periph = at91_mux_pio3_set_D_periph,
628 .get_deglitch = at91_mux_pio3_get_deglitch,
629 .set_deglitch = at91_mux_pio3_set_deglitch,
630 .get_debounce = at91_mux_pio3_get_debounce,
631 .set_debounce = at91_mux_pio3_set_debounce,
632 .get_pulldown = at91_mux_pio3_get_pulldown,
633 .set_pulldown = at91_mux_pio3_set_pulldown,
634 .get_schmitt_trig = at91_mux_pio3_get_schmitt_trig,
635 .disable_schmitt_trig = at91_mux_pio3_disable_schmitt_trig,
636 .get_drivestrength = at91_mux_sama5d3_get_drivestrength,
637 .set_drivestrength = at91_mux_sama5d3_set_drivestrength,
638 .irq_type = alt_gpio_irq_type,
639 };
640
at91_pin_dbg(const struct device * dev,const struct at91_pmx_pin * pin)641 static void at91_pin_dbg(const struct device *dev, const struct at91_pmx_pin *pin)
642 {
643 if (pin->mux) {
644 dev_dbg(dev, "pio%c%d configured as periph%c with conf = 0x%lx\n",
645 pin->bank + 'A', pin->pin, pin->mux - 1 + 'A', pin->conf);
646 } else {
647 dev_dbg(dev, "pio%c%d configured as gpio with conf = 0x%lx\n",
648 pin->bank + 'A', pin->pin, pin->conf);
649 }
650 }
651
pin_check_config(struct at91_pinctrl * info,const char * name,int index,const struct at91_pmx_pin * pin)652 static int pin_check_config(struct at91_pinctrl *info, const char *name,
653 int index, const struct at91_pmx_pin *pin)
654 {
655 int mux;
656
657 /* check if it's a valid config */
658 if (pin->bank >= gpio_banks) {
659 dev_err(info->dev, "%s: pin conf %d bank_id %d >= nbanks %d\n",
660 name, index, pin->bank, gpio_banks);
661 return -EINVAL;
662 }
663
664 if (!gpio_chips[pin->bank]) {
665 dev_err(info->dev, "%s: pin conf %d bank_id %d not enabled\n",
666 name, index, pin->bank);
667 return -ENXIO;
668 }
669
670 if (pin->pin >= MAX_NB_GPIO_PER_BANK) {
671 dev_err(info->dev, "%s: pin conf %d pin_bank_id %d >= %d\n",
672 name, index, pin->pin, MAX_NB_GPIO_PER_BANK);
673 return -EINVAL;
674 }
675
676 if (!pin->mux)
677 return 0;
678
679 mux = pin->mux - 1;
680
681 if (mux >= info->nmux) {
682 dev_err(info->dev, "%s: pin conf %d mux_id %d >= nmux %d\n",
683 name, index, mux, info->nmux);
684 return -EINVAL;
685 }
686
687 if (!(info->mux_mask[pin->bank * info->nmux + mux] & 1 << pin->pin)) {
688 dev_err(info->dev, "%s: pin conf %d mux_id %d not supported for pio%c%d\n",
689 name, index, mux, pin->bank + 'A', pin->pin);
690 return -EINVAL;
691 }
692
693 return 0;
694 }
695
at91_mux_gpio_disable(void __iomem * pio,unsigned mask)696 static void at91_mux_gpio_disable(void __iomem *pio, unsigned mask)
697 {
698 writel_relaxed(mask, pio + PIO_PDR);
699 }
700
at91_mux_gpio_enable(void __iomem * pio,unsigned mask,bool input)701 static void at91_mux_gpio_enable(void __iomem *pio, unsigned mask, bool input)
702 {
703 writel_relaxed(mask, pio + PIO_PER);
704 writel_relaxed(mask, pio + (input ? PIO_ODR : PIO_OER));
705 }
706
at91_pmx_set(struct pinctrl_dev * pctldev,unsigned selector,unsigned group)707 static int at91_pmx_set(struct pinctrl_dev *pctldev, unsigned selector,
708 unsigned group)
709 {
710 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
711 const struct at91_pmx_pin *pins_conf = info->groups[group].pins_conf;
712 const struct at91_pmx_pin *pin;
713 uint32_t npins = info->groups[group].npins;
714 int i, ret;
715 unsigned mask;
716 void __iomem *pio;
717
718 dev_dbg(info->dev, "enable function %s group %s\n",
719 info->functions[selector].name, info->groups[group].name);
720
721 /* first check that all the pins of the group are valid with a valid
722 * parameter */
723 for (i = 0; i < npins; i++) {
724 pin = &pins_conf[i];
725 ret = pin_check_config(info, info->groups[group].name, i, pin);
726 if (ret)
727 return ret;
728 }
729
730 for (i = 0; i < npins; i++) {
731 pin = &pins_conf[i];
732 at91_pin_dbg(info->dev, pin);
733 pio = pin_to_controller(info, pin->bank);
734 mask = pin_to_mask(pin->pin);
735 at91_mux_disable_interrupt(pio, mask);
736 switch (pin->mux) {
737 case AT91_MUX_GPIO:
738 at91_mux_gpio_enable(pio, mask, 1);
739 break;
740 case AT91_MUX_PERIPH_A:
741 info->ops->mux_A_periph(pio, mask);
742 break;
743 case AT91_MUX_PERIPH_B:
744 info->ops->mux_B_periph(pio, mask);
745 break;
746 case AT91_MUX_PERIPH_C:
747 if (!info->ops->mux_C_periph)
748 return -EINVAL;
749 info->ops->mux_C_periph(pio, mask);
750 break;
751 case AT91_MUX_PERIPH_D:
752 if (!info->ops->mux_D_periph)
753 return -EINVAL;
754 info->ops->mux_D_periph(pio, mask);
755 break;
756 }
757 if (pin->mux)
758 at91_mux_gpio_disable(pio, mask);
759 }
760
761 return 0;
762 }
763
at91_pmx_get_funcs_count(struct pinctrl_dev * pctldev)764 static int at91_pmx_get_funcs_count(struct pinctrl_dev *pctldev)
765 {
766 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
767
768 return info->nfunctions;
769 }
770
at91_pmx_get_func_name(struct pinctrl_dev * pctldev,unsigned selector)771 static const char *at91_pmx_get_func_name(struct pinctrl_dev *pctldev,
772 unsigned selector)
773 {
774 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
775
776 return info->functions[selector].name;
777 }
778
at91_pmx_get_groups(struct pinctrl_dev * pctldev,unsigned selector,const char * const ** groups,unsigned * const num_groups)779 static int at91_pmx_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
780 const char * const **groups,
781 unsigned * const num_groups)
782 {
783 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
784
785 *groups = info->functions[selector].groups;
786 *num_groups = info->functions[selector].ngroups;
787
788 return 0;
789 }
790
at91_gpio_request_enable(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned offset)791 static int at91_gpio_request_enable(struct pinctrl_dev *pctldev,
792 struct pinctrl_gpio_range *range,
793 unsigned offset)
794 {
795 struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
796 struct at91_gpio_chip *at91_chip;
797 struct gpio_chip *chip;
798 unsigned mask;
799
800 if (!range) {
801 dev_err(npct->dev, "invalid range\n");
802 return -EINVAL;
803 }
804 if (!range->gc) {
805 dev_err(npct->dev, "missing GPIO chip in range\n");
806 return -EINVAL;
807 }
808 chip = range->gc;
809 at91_chip = container_of(chip, struct at91_gpio_chip, chip);
810
811 dev_dbg(npct->dev, "enable pin %u as GPIO\n", offset);
812
813 mask = 1 << (offset - chip->base);
814
815 dev_dbg(npct->dev, "enable pin %u as PIO%c%d 0x%x\n",
816 offset, 'A' + range->id, offset - chip->base, mask);
817
818 writel_relaxed(mask, at91_chip->regbase + PIO_PER);
819
820 return 0;
821 }
822
at91_gpio_disable_free(struct pinctrl_dev * pctldev,struct pinctrl_gpio_range * range,unsigned offset)823 static void at91_gpio_disable_free(struct pinctrl_dev *pctldev,
824 struct pinctrl_gpio_range *range,
825 unsigned offset)
826 {
827 struct at91_pinctrl *npct = pinctrl_dev_get_drvdata(pctldev);
828
829 dev_dbg(npct->dev, "disable pin %u as GPIO\n", offset);
830 /* Set the pin to some default state, GPIO is usually default */
831 }
832
833 static const struct pinmux_ops at91_pmx_ops = {
834 .get_functions_count = at91_pmx_get_funcs_count,
835 .get_function_name = at91_pmx_get_func_name,
836 .get_function_groups = at91_pmx_get_groups,
837 .set_mux = at91_pmx_set,
838 .gpio_request_enable = at91_gpio_request_enable,
839 .gpio_disable_free = at91_gpio_disable_free,
840 };
841
at91_pinconf_get(struct pinctrl_dev * pctldev,unsigned pin_id,unsigned long * config)842 static int at91_pinconf_get(struct pinctrl_dev *pctldev,
843 unsigned pin_id, unsigned long *config)
844 {
845 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
846 void __iomem *pio;
847 unsigned pin;
848 int div;
849
850 *config = 0;
851 dev_dbg(info->dev, "%s:%d, pin_id=%d", __func__, __LINE__, pin_id);
852 pio = pin_to_controller(info, pin_to_bank(pin_id));
853 pin = pin_id % MAX_NB_GPIO_PER_BANK;
854
855 if (at91_mux_get_multidrive(pio, pin))
856 *config |= MULTI_DRIVE;
857
858 if (at91_mux_get_pullup(pio, pin))
859 *config |= PULL_UP;
860
861 if (info->ops->get_deglitch && info->ops->get_deglitch(pio, pin))
862 *config |= DEGLITCH;
863 if (info->ops->get_debounce && info->ops->get_debounce(pio, pin, &div))
864 *config |= DEBOUNCE | (div << DEBOUNCE_VAL_SHIFT);
865 if (info->ops->get_pulldown && info->ops->get_pulldown(pio, pin))
866 *config |= PULL_DOWN;
867 if (info->ops->get_schmitt_trig && info->ops->get_schmitt_trig(pio, pin))
868 *config |= DIS_SCHMIT;
869 if (info->ops->get_drivestrength)
870 *config |= (info->ops->get_drivestrength(pio, pin)
871 << DRIVE_STRENGTH_SHIFT);
872
873 return 0;
874 }
875
at91_pinconf_set(struct pinctrl_dev * pctldev,unsigned pin_id,unsigned long * configs,unsigned num_configs)876 static int at91_pinconf_set(struct pinctrl_dev *pctldev,
877 unsigned pin_id, unsigned long *configs,
878 unsigned num_configs)
879 {
880 struct at91_pinctrl *info = pinctrl_dev_get_drvdata(pctldev);
881 unsigned mask;
882 void __iomem *pio;
883 int i;
884 unsigned long config;
885 unsigned pin;
886
887 for (i = 0; i < num_configs; i++) {
888 config = configs[i];
889
890 dev_dbg(info->dev,
891 "%s:%d, pin_id=%d, config=0x%lx",
892 __func__, __LINE__, pin_id, config);
893 pio = pin_to_controller(info, pin_to_bank(pin_id));
894 pin = pin_id % MAX_NB_GPIO_PER_BANK;
895 mask = pin_to_mask(pin);
896
897 if (config & PULL_UP && config & PULL_DOWN)
898 return -EINVAL;
899
900 at91_mux_set_pullup(pio, mask, config & PULL_UP);
901 at91_mux_set_multidrive(pio, mask, config & MULTI_DRIVE);
902 if (info->ops->set_deglitch)
903 info->ops->set_deglitch(pio, mask, config & DEGLITCH);
904 if (info->ops->set_debounce)
905 info->ops->set_debounce(pio, mask, config & DEBOUNCE,
906 (config & DEBOUNCE_VAL) >> DEBOUNCE_VAL_SHIFT);
907 if (info->ops->set_pulldown)
908 info->ops->set_pulldown(pio, mask, config & PULL_DOWN);
909 if (info->ops->disable_schmitt_trig && config & DIS_SCHMIT)
910 info->ops->disable_schmitt_trig(pio, mask);
911 if (info->ops->set_drivestrength)
912 info->ops->set_drivestrength(pio, pin,
913 (config & DRIVE_STRENGTH)
914 >> DRIVE_STRENGTH_SHIFT);
915
916 } /* for each config */
917
918 return 0;
919 }
920
921 #define DBG_SHOW_FLAG(flag) do { \
922 if (config & flag) { \
923 if (num_conf) \
924 seq_puts(s, "|"); \
925 seq_puts(s, #flag); \
926 num_conf++; \
927 } \
928 } while (0)
929
930 #define DBG_SHOW_FLAG_MASKED(mask,flag) do { \
931 if ((config & mask) == flag) { \
932 if (num_conf) \
933 seq_puts(s, "|"); \
934 seq_puts(s, #flag); \
935 num_conf++; \
936 } \
937 } while (0)
938
at91_pinconf_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned pin_id)939 static void at91_pinconf_dbg_show(struct pinctrl_dev *pctldev,
940 struct seq_file *s, unsigned pin_id)
941 {
942 unsigned long config;
943 int val, num_conf = 0;
944
945 at91_pinconf_get(pctldev, pin_id, &config);
946
947 DBG_SHOW_FLAG(MULTI_DRIVE);
948 DBG_SHOW_FLAG(PULL_UP);
949 DBG_SHOW_FLAG(PULL_DOWN);
950 DBG_SHOW_FLAG(DIS_SCHMIT);
951 DBG_SHOW_FLAG(DEGLITCH);
952 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_LOW);
953 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_MED);
954 DBG_SHOW_FLAG_MASKED(DRIVE_STRENGTH, DRIVE_STRENGTH_HI);
955 DBG_SHOW_FLAG(DEBOUNCE);
956 if (config & DEBOUNCE) {
957 val = config >> DEBOUNCE_VAL_SHIFT;
958 seq_printf(s, "(%d)", val);
959 }
960
961 return;
962 }
963
at91_pinconf_group_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned group)964 static void at91_pinconf_group_dbg_show(struct pinctrl_dev *pctldev,
965 struct seq_file *s, unsigned group)
966 {
967 }
968
969 static const struct pinconf_ops at91_pinconf_ops = {
970 .pin_config_get = at91_pinconf_get,
971 .pin_config_set = at91_pinconf_set,
972 .pin_config_dbg_show = at91_pinconf_dbg_show,
973 .pin_config_group_dbg_show = at91_pinconf_group_dbg_show,
974 };
975
976 static struct pinctrl_desc at91_pinctrl_desc = {
977 .pctlops = &at91_pctrl_ops,
978 .pmxops = &at91_pmx_ops,
979 .confops = &at91_pinconf_ops,
980 .owner = THIS_MODULE,
981 };
982
983 static const char *gpio_compat = "atmel,at91rm9200-gpio";
984
at91_pinctrl_child_count(struct at91_pinctrl * info,struct device_node * np)985 static void at91_pinctrl_child_count(struct at91_pinctrl *info,
986 struct device_node *np)
987 {
988 struct device_node *child;
989
990 for_each_child_of_node(np, child) {
991 if (of_device_is_compatible(child, gpio_compat)) {
992 if (of_device_is_available(child))
993 info->nactive_banks++;
994 } else {
995 info->nfunctions++;
996 info->ngroups += of_get_child_count(child);
997 }
998 }
999 }
1000
at91_pinctrl_mux_mask(struct at91_pinctrl * info,struct device_node * np)1001 static int at91_pinctrl_mux_mask(struct at91_pinctrl *info,
1002 struct device_node *np)
1003 {
1004 int ret = 0;
1005 int size;
1006 const __be32 *list;
1007
1008 list = of_get_property(np, "atmel,mux-mask", &size);
1009 if (!list) {
1010 dev_err(info->dev, "can not read the mux-mask of %d\n", size);
1011 return -EINVAL;
1012 }
1013
1014 size /= sizeof(*list);
1015 if (!size || size % gpio_banks) {
1016 dev_err(info->dev, "wrong mux mask array should be by %d\n", gpio_banks);
1017 return -EINVAL;
1018 }
1019 info->nmux = size / gpio_banks;
1020
1021 info->mux_mask = devm_kzalloc(info->dev, sizeof(u32) * size, GFP_KERNEL);
1022 if (!info->mux_mask) {
1023 dev_err(info->dev, "could not alloc mux_mask\n");
1024 return -ENOMEM;
1025 }
1026
1027 ret = of_property_read_u32_array(np, "atmel,mux-mask",
1028 info->mux_mask, size);
1029 if (ret)
1030 dev_err(info->dev, "can not read the mux-mask of %d\n", size);
1031 return ret;
1032 }
1033
at91_pinctrl_parse_groups(struct device_node * np,struct at91_pin_group * grp,struct at91_pinctrl * info,u32 index)1034 static int at91_pinctrl_parse_groups(struct device_node *np,
1035 struct at91_pin_group *grp,
1036 struct at91_pinctrl *info, u32 index)
1037 {
1038 struct at91_pmx_pin *pin;
1039 int size;
1040 const __be32 *list;
1041 int i, j;
1042
1043 dev_dbg(info->dev, "group(%d): %s\n", index, np->name);
1044
1045 /* Initialise group */
1046 grp->name = np->name;
1047
1048 /*
1049 * the binding format is atmel,pins = <bank pin mux CONFIG ...>,
1050 * do sanity check and calculate pins number
1051 */
1052 list = of_get_property(np, "atmel,pins", &size);
1053 /* we do not check return since it's safe node passed down */
1054 size /= sizeof(*list);
1055 if (!size || size % 4) {
1056 dev_err(info->dev, "wrong pins number or pins and configs should be by 4\n");
1057 return -EINVAL;
1058 }
1059
1060 grp->npins = size / 4;
1061 pin = grp->pins_conf = devm_kzalloc(info->dev, grp->npins * sizeof(struct at91_pmx_pin),
1062 GFP_KERNEL);
1063 grp->pins = devm_kzalloc(info->dev, grp->npins * sizeof(unsigned int),
1064 GFP_KERNEL);
1065 if (!grp->pins_conf || !grp->pins)
1066 return -ENOMEM;
1067
1068 for (i = 0, j = 0; i < size; i += 4, j++) {
1069 pin->bank = be32_to_cpu(*list++);
1070 pin->pin = be32_to_cpu(*list++);
1071 grp->pins[j] = pin->bank * MAX_NB_GPIO_PER_BANK + pin->pin;
1072 pin->mux = be32_to_cpu(*list++);
1073 pin->conf = be32_to_cpu(*list++);
1074
1075 at91_pin_dbg(info->dev, pin);
1076 pin++;
1077 }
1078
1079 return 0;
1080 }
1081
at91_pinctrl_parse_functions(struct device_node * np,struct at91_pinctrl * info,u32 index)1082 static int at91_pinctrl_parse_functions(struct device_node *np,
1083 struct at91_pinctrl *info, u32 index)
1084 {
1085 struct device_node *child;
1086 struct at91_pmx_func *func;
1087 struct at91_pin_group *grp;
1088 int ret;
1089 static u32 grp_index;
1090 u32 i = 0;
1091
1092 dev_dbg(info->dev, "parse function(%d): %s\n", index, np->name);
1093
1094 func = &info->functions[index];
1095
1096 /* Initialise function */
1097 func->name = np->name;
1098 func->ngroups = of_get_child_count(np);
1099 if (func->ngroups == 0) {
1100 dev_err(info->dev, "no groups defined\n");
1101 return -EINVAL;
1102 }
1103 func->groups = devm_kzalloc(info->dev,
1104 func->ngroups * sizeof(char *), GFP_KERNEL);
1105 if (!func->groups)
1106 return -ENOMEM;
1107
1108 for_each_child_of_node(np, child) {
1109 func->groups[i] = child->name;
1110 grp = &info->groups[grp_index++];
1111 ret = at91_pinctrl_parse_groups(child, grp, info, i++);
1112 if (ret)
1113 return ret;
1114 }
1115
1116 return 0;
1117 }
1118
1119 static struct of_device_id at91_pinctrl_of_match[] = {
1120 { .compatible = "atmel,sama5d3-pinctrl", .data = &sama5d3_ops },
1121 { .compatible = "atmel,at91sam9x5-pinctrl", .data = &at91sam9x5_ops },
1122 { .compatible = "atmel,at91rm9200-pinctrl", .data = &at91rm9200_ops },
1123 { /* sentinel */ }
1124 };
1125
at91_pinctrl_probe_dt(struct platform_device * pdev,struct at91_pinctrl * info)1126 static int at91_pinctrl_probe_dt(struct platform_device *pdev,
1127 struct at91_pinctrl *info)
1128 {
1129 int ret = 0;
1130 int i, j;
1131 uint32_t *tmp;
1132 struct device_node *np = pdev->dev.of_node;
1133 struct device_node *child;
1134
1135 if (!np)
1136 return -ENODEV;
1137
1138 info->dev = &pdev->dev;
1139 info->ops = (struct at91_pinctrl_mux_ops *)
1140 of_match_device(at91_pinctrl_of_match, &pdev->dev)->data;
1141 at91_pinctrl_child_count(info, np);
1142
1143 if (gpio_banks < 1) {
1144 dev_err(&pdev->dev, "you need to specify at least one gpio-controller\n");
1145 return -EINVAL;
1146 }
1147
1148 ret = at91_pinctrl_mux_mask(info, np);
1149 if (ret)
1150 return ret;
1151
1152 dev_dbg(&pdev->dev, "nmux = %d\n", info->nmux);
1153
1154 dev_dbg(&pdev->dev, "mux-mask\n");
1155 tmp = info->mux_mask;
1156 for (i = 0; i < gpio_banks; i++) {
1157 for (j = 0; j < info->nmux; j++, tmp++) {
1158 dev_dbg(&pdev->dev, "%d:%d\t0x%x\n", i, j, tmp[0]);
1159 }
1160 }
1161
1162 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1163 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
1164 info->functions = devm_kzalloc(&pdev->dev, info->nfunctions * sizeof(struct at91_pmx_func),
1165 GFP_KERNEL);
1166 if (!info->functions)
1167 return -ENOMEM;
1168
1169 info->groups = devm_kzalloc(&pdev->dev, info->ngroups * sizeof(struct at91_pin_group),
1170 GFP_KERNEL);
1171 if (!info->groups)
1172 return -ENOMEM;
1173
1174 dev_dbg(&pdev->dev, "nbanks = %d\n", gpio_banks);
1175 dev_dbg(&pdev->dev, "nfunctions = %d\n", info->nfunctions);
1176 dev_dbg(&pdev->dev, "ngroups = %d\n", info->ngroups);
1177
1178 i = 0;
1179
1180 for_each_child_of_node(np, child) {
1181 if (of_device_is_compatible(child, gpio_compat))
1182 continue;
1183 ret = at91_pinctrl_parse_functions(child, info, i++);
1184 if (ret) {
1185 dev_err(&pdev->dev, "failed to parse function\n");
1186 return ret;
1187 }
1188 }
1189
1190 return 0;
1191 }
1192
at91_pinctrl_probe(struct platform_device * pdev)1193 static int at91_pinctrl_probe(struct platform_device *pdev)
1194 {
1195 struct at91_pinctrl *info;
1196 struct pinctrl_pin_desc *pdesc;
1197 int ret, i, j, k, ngpio_chips_enabled = 0;
1198
1199 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
1200 if (!info)
1201 return -ENOMEM;
1202
1203 ret = at91_pinctrl_probe_dt(pdev, info);
1204 if (ret)
1205 return ret;
1206
1207 /*
1208 * We need all the GPIO drivers to probe FIRST, or we will not be able
1209 * to obtain references to the struct gpio_chip * for them, and we
1210 * need this to proceed.
1211 */
1212 for (i = 0; i < gpio_banks; i++)
1213 if (gpio_chips[i])
1214 ngpio_chips_enabled++;
1215
1216 if (ngpio_chips_enabled < info->nactive_banks) {
1217 dev_warn(&pdev->dev,
1218 "All GPIO chips are not registered yet (%d/%d)\n",
1219 ngpio_chips_enabled, info->nactive_banks);
1220 devm_kfree(&pdev->dev, info);
1221 return -EPROBE_DEFER;
1222 }
1223
1224 at91_pinctrl_desc.name = dev_name(&pdev->dev);
1225 at91_pinctrl_desc.npins = gpio_banks * MAX_NB_GPIO_PER_BANK;
1226 at91_pinctrl_desc.pins = pdesc =
1227 devm_kzalloc(&pdev->dev, sizeof(*pdesc) * at91_pinctrl_desc.npins, GFP_KERNEL);
1228
1229 if (!at91_pinctrl_desc.pins)
1230 return -ENOMEM;
1231
1232 for (i = 0, k = 0; i < gpio_banks; i++) {
1233 for (j = 0; j < MAX_NB_GPIO_PER_BANK; j++, k++) {
1234 pdesc->number = k;
1235 pdesc->name = kasprintf(GFP_KERNEL, "pio%c%d", i + 'A', j);
1236 pdesc++;
1237 }
1238 }
1239
1240 platform_set_drvdata(pdev, info);
1241 info->pctl = pinctrl_register(&at91_pinctrl_desc, &pdev->dev, info);
1242
1243 if (!info->pctl) {
1244 dev_err(&pdev->dev, "could not register AT91 pinctrl driver\n");
1245 ret = -EINVAL;
1246 goto err;
1247 }
1248
1249 /* We will handle a range of GPIO pins */
1250 for (i = 0; i < gpio_banks; i++)
1251 if (gpio_chips[i])
1252 pinctrl_add_gpio_range(info->pctl, &gpio_chips[i]->range);
1253
1254 dev_info(&pdev->dev, "initialized AT91 pinctrl driver\n");
1255
1256 return 0;
1257
1258 err:
1259 return ret;
1260 }
1261
at91_pinctrl_remove(struct platform_device * pdev)1262 static int at91_pinctrl_remove(struct platform_device *pdev)
1263 {
1264 struct at91_pinctrl *info = platform_get_drvdata(pdev);
1265
1266 pinctrl_unregister(info->pctl);
1267
1268 return 0;
1269 }
1270
at91_gpio_request(struct gpio_chip * chip,unsigned offset)1271 static int at91_gpio_request(struct gpio_chip *chip, unsigned offset)
1272 {
1273 /*
1274 * Map back to global GPIO space and request muxing, the direction
1275 * parameter does not matter for this controller.
1276 */
1277 int gpio = chip->base + offset;
1278 int bank = chip->base / chip->ngpio;
1279
1280 dev_dbg(chip->dev, "%s:%d pio%c%d(%d)\n", __func__, __LINE__,
1281 'A' + bank, offset, gpio);
1282
1283 return pinctrl_request_gpio(gpio);
1284 }
1285
at91_gpio_free(struct gpio_chip * chip,unsigned offset)1286 static void at91_gpio_free(struct gpio_chip *chip, unsigned offset)
1287 {
1288 int gpio = chip->base + offset;
1289
1290 pinctrl_free_gpio(gpio);
1291 }
1292
at91_gpio_get_direction(struct gpio_chip * chip,unsigned offset)1293 static int at91_gpio_get_direction(struct gpio_chip *chip, unsigned offset)
1294 {
1295 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
1296 void __iomem *pio = at91_gpio->regbase;
1297 unsigned mask = 1 << offset;
1298 u32 osr;
1299
1300 osr = readl_relaxed(pio + PIO_OSR);
1301 return !(osr & mask);
1302 }
1303
at91_gpio_direction_input(struct gpio_chip * chip,unsigned offset)1304 static int at91_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
1305 {
1306 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
1307 void __iomem *pio = at91_gpio->regbase;
1308 unsigned mask = 1 << offset;
1309
1310 writel_relaxed(mask, pio + PIO_ODR);
1311 return 0;
1312 }
1313
at91_gpio_get(struct gpio_chip * chip,unsigned offset)1314 static int at91_gpio_get(struct gpio_chip *chip, unsigned offset)
1315 {
1316 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
1317 void __iomem *pio = at91_gpio->regbase;
1318 unsigned mask = 1 << offset;
1319 u32 pdsr;
1320
1321 pdsr = readl_relaxed(pio + PIO_PDSR);
1322 return (pdsr & mask) != 0;
1323 }
1324
at91_gpio_set(struct gpio_chip * chip,unsigned offset,int val)1325 static void at91_gpio_set(struct gpio_chip *chip, unsigned offset,
1326 int val)
1327 {
1328 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
1329 void __iomem *pio = at91_gpio->regbase;
1330 unsigned mask = 1 << offset;
1331
1332 writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
1333 }
1334
at91_gpio_direction_output(struct gpio_chip * chip,unsigned offset,int val)1335 static int at91_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
1336 int val)
1337 {
1338 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
1339 void __iomem *pio = at91_gpio->regbase;
1340 unsigned mask = 1 << offset;
1341
1342 writel_relaxed(mask, pio + (val ? PIO_SODR : PIO_CODR));
1343 writel_relaxed(mask, pio + PIO_OER);
1344
1345 return 0;
1346 }
1347
1348 #ifdef CONFIG_DEBUG_FS
at91_gpio_dbg_show(struct seq_file * s,struct gpio_chip * chip)1349 static void at91_gpio_dbg_show(struct seq_file *s, struct gpio_chip *chip)
1350 {
1351 enum at91_mux mode;
1352 int i;
1353 struct at91_gpio_chip *at91_gpio = to_at91_gpio_chip(chip);
1354 void __iomem *pio = at91_gpio->regbase;
1355
1356 for (i = 0; i < chip->ngpio; i++) {
1357 unsigned mask = pin_to_mask(i);
1358 const char *gpio_label;
1359 u32 pdsr;
1360
1361 gpio_label = gpiochip_is_requested(chip, i);
1362 if (!gpio_label)
1363 continue;
1364 mode = at91_gpio->ops->get_periph(pio, mask);
1365 seq_printf(s, "[%s] GPIO%s%d: ",
1366 gpio_label, chip->label, i);
1367 if (mode == AT91_MUX_GPIO) {
1368 pdsr = readl_relaxed(pio + PIO_PDSR);
1369
1370 seq_printf(s, "[gpio] %s\n",
1371 pdsr & mask ?
1372 "set" : "clear");
1373 } else {
1374 seq_printf(s, "[periph %c]\n",
1375 mode + 'A' - 1);
1376 }
1377 }
1378 }
1379 #else
1380 #define at91_gpio_dbg_show NULL
1381 #endif
1382
1383 /* Several AIC controller irqs are dispatched through this GPIO handler.
1384 * To use any AT91_PIN_* as an externally triggered IRQ, first call
1385 * at91_set_gpio_input() then maybe enable its glitch filter.
1386 * Then just request_irq() with the pin ID; it works like any ARM IRQ
1387 * handler.
1388 * First implementation always triggers on rising and falling edges
1389 * whereas the newer PIO3 can be additionally configured to trigger on
1390 * level, edge with any polarity.
1391 *
1392 * Alternatively, certain pins may be used directly as IRQ0..IRQ6 after
1393 * configuring them with at91_set_a_periph() or at91_set_b_periph().
1394 * IRQ0..IRQ6 should be configurable, e.g. level vs edge triggering.
1395 */
1396
gpio_irq_mask(struct irq_data * d)1397 static void gpio_irq_mask(struct irq_data *d)
1398 {
1399 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1400 void __iomem *pio = at91_gpio->regbase;
1401 unsigned mask = 1 << d->hwirq;
1402
1403 if (pio)
1404 writel_relaxed(mask, pio + PIO_IDR);
1405 }
1406
gpio_irq_unmask(struct irq_data * d)1407 static void gpio_irq_unmask(struct irq_data *d)
1408 {
1409 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1410 void __iomem *pio = at91_gpio->regbase;
1411 unsigned mask = 1 << d->hwirq;
1412
1413 if (pio)
1414 writel_relaxed(mask, pio + PIO_IER);
1415 }
1416
gpio_irq_type(struct irq_data * d,unsigned type)1417 static int gpio_irq_type(struct irq_data *d, unsigned type)
1418 {
1419 switch (type) {
1420 case IRQ_TYPE_NONE:
1421 case IRQ_TYPE_EDGE_BOTH:
1422 return 0;
1423 default:
1424 return -EINVAL;
1425 }
1426 }
1427
1428 /* Alternate irq type for PIO3 support */
alt_gpio_irq_type(struct irq_data * d,unsigned type)1429 static int alt_gpio_irq_type(struct irq_data *d, unsigned type)
1430 {
1431 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1432 void __iomem *pio = at91_gpio->regbase;
1433 unsigned mask = 1 << d->hwirq;
1434
1435 switch (type) {
1436 case IRQ_TYPE_EDGE_RISING:
1437 __irq_set_handler_locked(d->irq, handle_simple_irq);
1438 writel_relaxed(mask, pio + PIO_ESR);
1439 writel_relaxed(mask, pio + PIO_REHLSR);
1440 break;
1441 case IRQ_TYPE_EDGE_FALLING:
1442 __irq_set_handler_locked(d->irq, handle_simple_irq);
1443 writel_relaxed(mask, pio + PIO_ESR);
1444 writel_relaxed(mask, pio + PIO_FELLSR);
1445 break;
1446 case IRQ_TYPE_LEVEL_LOW:
1447 __irq_set_handler_locked(d->irq, handle_level_irq);
1448 writel_relaxed(mask, pio + PIO_LSR);
1449 writel_relaxed(mask, pio + PIO_FELLSR);
1450 break;
1451 case IRQ_TYPE_LEVEL_HIGH:
1452 __irq_set_handler_locked(d->irq, handle_level_irq);
1453 writel_relaxed(mask, pio + PIO_LSR);
1454 writel_relaxed(mask, pio + PIO_REHLSR);
1455 break;
1456 case IRQ_TYPE_EDGE_BOTH:
1457 /*
1458 * disable additional interrupt modes:
1459 * fall back to default behavior
1460 */
1461 __irq_set_handler_locked(d->irq, handle_simple_irq);
1462 writel_relaxed(mask, pio + PIO_AIMDR);
1463 return 0;
1464 case IRQ_TYPE_NONE:
1465 default:
1466 pr_warn("AT91: No type for irq %d\n", gpio_to_irq(d->irq));
1467 return -EINVAL;
1468 }
1469
1470 /* enable additional interrupt modes */
1471 writel_relaxed(mask, pio + PIO_AIMER);
1472
1473 return 0;
1474 }
1475
gpio_irq_ack(struct irq_data * d)1476 static void gpio_irq_ack(struct irq_data *d)
1477 {
1478 /* the interrupt is already cleared before by reading ISR */
1479 }
1480
gpio_irq_startup(struct irq_data * d)1481 static unsigned int gpio_irq_startup(struct irq_data *d)
1482 {
1483 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1484 unsigned pin = d->hwirq;
1485 int ret;
1486
1487 ret = gpio_lock_as_irq(&at91_gpio->chip, pin);
1488 if (ret) {
1489 dev_err(at91_gpio->chip.dev, "unable to lock pind %lu IRQ\n",
1490 d->hwirq);
1491 return ret;
1492 }
1493 gpio_irq_unmask(d);
1494 return 0;
1495 }
1496
gpio_irq_shutdown(struct irq_data * d)1497 static void gpio_irq_shutdown(struct irq_data *d)
1498 {
1499 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1500 unsigned pin = d->hwirq;
1501
1502 gpio_irq_mask(d);
1503 gpio_unlock_as_irq(&at91_gpio->chip, pin);
1504 }
1505
1506 #ifdef CONFIG_PM
1507
1508 static u32 wakeups[MAX_GPIO_BANKS];
1509 static u32 backups[MAX_GPIO_BANKS];
1510
gpio_irq_set_wake(struct irq_data * d,unsigned state)1511 static int gpio_irq_set_wake(struct irq_data *d, unsigned state)
1512 {
1513 struct at91_gpio_chip *at91_gpio = irq_data_get_irq_chip_data(d);
1514 unsigned bank = at91_gpio->pioc_idx;
1515 unsigned mask = 1 << d->hwirq;
1516
1517 if (unlikely(bank >= MAX_GPIO_BANKS))
1518 return -EINVAL;
1519
1520 if (state)
1521 wakeups[bank] |= mask;
1522 else
1523 wakeups[bank] &= ~mask;
1524
1525 irq_set_irq_wake(at91_gpio->pioc_virq, state);
1526
1527 return 0;
1528 }
1529
at91_pinctrl_gpio_suspend(void)1530 void at91_pinctrl_gpio_suspend(void)
1531 {
1532 int i;
1533
1534 for (i = 0; i < gpio_banks; i++) {
1535 void __iomem *pio;
1536
1537 if (!gpio_chips[i])
1538 continue;
1539
1540 pio = gpio_chips[i]->regbase;
1541
1542 backups[i] = __raw_readl(pio + PIO_IMR);
1543 __raw_writel(backups[i], pio + PIO_IDR);
1544 __raw_writel(wakeups[i], pio + PIO_IER);
1545
1546 if (!wakeups[i])
1547 clk_disable_unprepare(gpio_chips[i]->clock);
1548 else
1549 printk(KERN_DEBUG "GPIO-%c may wake for %08x\n",
1550 'A'+i, wakeups[i]);
1551 }
1552 }
1553
at91_pinctrl_gpio_resume(void)1554 void at91_pinctrl_gpio_resume(void)
1555 {
1556 int i;
1557
1558 for (i = 0; i < gpio_banks; i++) {
1559 void __iomem *pio;
1560
1561 if (!gpio_chips[i])
1562 continue;
1563
1564 pio = gpio_chips[i]->regbase;
1565
1566 if (!wakeups[i])
1567 clk_prepare_enable(gpio_chips[i]->clock);
1568
1569 __raw_writel(wakeups[i], pio + PIO_IDR);
1570 __raw_writel(backups[i], pio + PIO_IER);
1571 }
1572 }
1573
1574 #else
1575 #define gpio_irq_set_wake NULL
1576 #endif /* CONFIG_PM */
1577
1578 static struct irq_chip gpio_irqchip = {
1579 .name = "GPIO",
1580 .irq_ack = gpio_irq_ack,
1581 .irq_startup = gpio_irq_startup,
1582 .irq_shutdown = gpio_irq_shutdown,
1583 .irq_disable = gpio_irq_mask,
1584 .irq_mask = gpio_irq_mask,
1585 .irq_unmask = gpio_irq_unmask,
1586 /* .irq_set_type is set dynamically */
1587 .irq_set_wake = gpio_irq_set_wake,
1588 };
1589
gpio_irq_handler(unsigned irq,struct irq_desc * desc)1590 static void gpio_irq_handler(unsigned irq, struct irq_desc *desc)
1591 {
1592 struct irq_chip *chip = irq_get_chip(irq);
1593 struct gpio_chip *gpio_chip = irq_desc_get_handler_data(desc);
1594 struct at91_gpio_chip *at91_gpio = container_of(gpio_chip,
1595 struct at91_gpio_chip, chip);
1596
1597 void __iomem *pio = at91_gpio->regbase;
1598 unsigned long isr;
1599 int n;
1600
1601 chained_irq_enter(chip, desc);
1602 for (;;) {
1603 /* Reading ISR acks pending (edge triggered) GPIO interrupts.
1604 * When there are none pending, we're finished unless we need
1605 * to process multiple banks (like ID_PIOCDE on sam9263).
1606 */
1607 isr = readl_relaxed(pio + PIO_ISR) & readl_relaxed(pio + PIO_IMR);
1608 if (!isr) {
1609 if (!at91_gpio->next)
1610 break;
1611 at91_gpio = at91_gpio->next;
1612 pio = at91_gpio->regbase;
1613 gpio_chip = &at91_gpio->chip;
1614 continue;
1615 }
1616
1617 for_each_set_bit(n, &isr, BITS_PER_LONG) {
1618 generic_handle_irq(irq_find_mapping(
1619 gpio_chip->irqdomain, n));
1620 }
1621 }
1622 chained_irq_exit(chip, desc);
1623 /* now it may re-trigger */
1624 }
1625
at91_gpio_of_irq_setup(struct platform_device * pdev,struct at91_gpio_chip * at91_gpio)1626 static int at91_gpio_of_irq_setup(struct platform_device *pdev,
1627 struct at91_gpio_chip *at91_gpio)
1628 {
1629 struct gpio_chip *gpiochip_prev = NULL;
1630 struct at91_gpio_chip *prev = NULL;
1631 struct irq_data *d = irq_get_irq_data(at91_gpio->pioc_virq);
1632 int ret, i;
1633
1634 at91_gpio->pioc_hwirq = irqd_to_hwirq(d);
1635
1636 /* Setup proper .irq_set_type function */
1637 gpio_irqchip.irq_set_type = at91_gpio->ops->irq_type;
1638
1639 /* Disable irqs of this PIO controller */
1640 writel_relaxed(~0, at91_gpio->regbase + PIO_IDR);
1641
1642 /*
1643 * Let the generic code handle this edge IRQ, the the chained
1644 * handler will perform the actual work of handling the parent
1645 * interrupt.
1646 */
1647 ret = gpiochip_irqchip_add(&at91_gpio->chip,
1648 &gpio_irqchip,
1649 0,
1650 handle_edge_irq,
1651 IRQ_TYPE_EDGE_BOTH);
1652 if (ret) {
1653 dev_err(&pdev->dev, "at91_gpio.%d: Couldn't add irqchip to gpiochip.\n",
1654 at91_gpio->pioc_idx);
1655 return ret;
1656 }
1657
1658 /* The top level handler handles one bank of GPIOs, except
1659 * on some SoC it can handle up to three...
1660 * We only set up the handler for the first of the list.
1661 */
1662 gpiochip_prev = irq_get_handler_data(at91_gpio->pioc_virq);
1663 if (!gpiochip_prev) {
1664 /* Then register the chain on the parent IRQ */
1665 gpiochip_set_chained_irqchip(&at91_gpio->chip,
1666 &gpio_irqchip,
1667 at91_gpio->pioc_virq,
1668 gpio_irq_handler);
1669 return 0;
1670 }
1671
1672 prev = container_of(gpiochip_prev, struct at91_gpio_chip, chip);
1673
1674 /* we can only have 2 banks before */
1675 for (i = 0; i < 2; i++) {
1676 if (prev->next) {
1677 prev = prev->next;
1678 } else {
1679 prev->next = at91_gpio;
1680 return 0;
1681 }
1682 }
1683
1684 return -EINVAL;
1685 }
1686
1687 /* This structure is replicated for each GPIO block allocated at probe time */
1688 static struct gpio_chip at91_gpio_template = {
1689 .request = at91_gpio_request,
1690 .free = at91_gpio_free,
1691 .get_direction = at91_gpio_get_direction,
1692 .direction_input = at91_gpio_direction_input,
1693 .get = at91_gpio_get,
1694 .direction_output = at91_gpio_direction_output,
1695 .set = at91_gpio_set,
1696 .dbg_show = at91_gpio_dbg_show,
1697 .can_sleep = false,
1698 .ngpio = MAX_NB_GPIO_PER_BANK,
1699 };
1700
1701 static struct of_device_id at91_gpio_of_match[] = {
1702 { .compatible = "atmel,at91sam9x5-gpio", .data = &at91sam9x5_ops, },
1703 { .compatible = "atmel,at91rm9200-gpio", .data = &at91rm9200_ops },
1704 { /* sentinel */ }
1705 };
1706
at91_gpio_probe(struct platform_device * pdev)1707 static int at91_gpio_probe(struct platform_device *pdev)
1708 {
1709 struct device_node *np = pdev->dev.of_node;
1710 struct resource *res;
1711 struct at91_gpio_chip *at91_chip = NULL;
1712 struct gpio_chip *chip;
1713 struct pinctrl_gpio_range *range;
1714 int ret = 0;
1715 int irq, i;
1716 int alias_idx = of_alias_get_id(np, "gpio");
1717 uint32_t ngpio;
1718 char **names;
1719
1720 BUG_ON(alias_idx >= ARRAY_SIZE(gpio_chips));
1721 if (gpio_chips[alias_idx]) {
1722 ret = -EBUSY;
1723 goto err;
1724 }
1725
1726 irq = platform_get_irq(pdev, 0);
1727 if (irq < 0) {
1728 ret = irq;
1729 goto err;
1730 }
1731
1732 at91_chip = devm_kzalloc(&pdev->dev, sizeof(*at91_chip), GFP_KERNEL);
1733 if (!at91_chip) {
1734 ret = -ENOMEM;
1735 goto err;
1736 }
1737
1738 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1739 at91_chip->regbase = devm_ioremap_resource(&pdev->dev, res);
1740 if (IS_ERR(at91_chip->regbase)) {
1741 ret = PTR_ERR(at91_chip->regbase);
1742 goto err;
1743 }
1744
1745 at91_chip->ops = (struct at91_pinctrl_mux_ops *)
1746 of_match_device(at91_gpio_of_match, &pdev->dev)->data;
1747 at91_chip->pioc_virq = irq;
1748 at91_chip->pioc_idx = alias_idx;
1749
1750 at91_chip->clock = devm_clk_get(&pdev->dev, NULL);
1751 if (IS_ERR(at91_chip->clock)) {
1752 dev_err(&pdev->dev, "failed to get clock, ignoring.\n");
1753 ret = PTR_ERR(at91_chip->clock);
1754 goto err;
1755 }
1756
1757 ret = clk_prepare(at91_chip->clock);
1758 if (ret)
1759 goto clk_prepare_err;
1760
1761 /* enable PIO controller's clock */
1762 ret = clk_enable(at91_chip->clock);
1763 if (ret) {
1764 dev_err(&pdev->dev, "failed to enable clock, ignoring.\n");
1765 goto clk_enable_err;
1766 }
1767
1768 at91_chip->chip = at91_gpio_template;
1769
1770 chip = &at91_chip->chip;
1771 chip->of_node = np;
1772 chip->label = dev_name(&pdev->dev);
1773 chip->dev = &pdev->dev;
1774 chip->owner = THIS_MODULE;
1775 chip->base = alias_idx * MAX_NB_GPIO_PER_BANK;
1776
1777 if (!of_property_read_u32(np, "#gpio-lines", &ngpio)) {
1778 if (ngpio >= MAX_NB_GPIO_PER_BANK)
1779 pr_err("at91_gpio.%d, gpio-nb >= %d failback to %d\n",
1780 alias_idx, MAX_NB_GPIO_PER_BANK, MAX_NB_GPIO_PER_BANK);
1781 else
1782 chip->ngpio = ngpio;
1783 }
1784
1785 names = devm_kzalloc(&pdev->dev, sizeof(char *) * chip->ngpio,
1786 GFP_KERNEL);
1787
1788 if (!names) {
1789 ret = -ENOMEM;
1790 goto clk_enable_err;
1791 }
1792
1793 for (i = 0; i < chip->ngpio; i++)
1794 names[i] = kasprintf(GFP_KERNEL, "pio%c%d", alias_idx + 'A', i);
1795
1796 chip->names = (const char *const *)names;
1797
1798 range = &at91_chip->range;
1799 range->name = chip->label;
1800 range->id = alias_idx;
1801 range->pin_base = range->base = range->id * MAX_NB_GPIO_PER_BANK;
1802
1803 range->npins = chip->ngpio;
1804 range->gc = chip;
1805
1806 ret = gpiochip_add(chip);
1807 if (ret)
1808 goto gpiochip_add_err;
1809
1810 gpio_chips[alias_idx] = at91_chip;
1811 gpio_banks = max(gpio_banks, alias_idx + 1);
1812
1813 ret = at91_gpio_of_irq_setup(pdev, at91_chip);
1814 if (ret)
1815 goto irq_setup_err;
1816
1817 dev_info(&pdev->dev, "at address %p\n", at91_chip->regbase);
1818
1819 return 0;
1820
1821 irq_setup_err:
1822 gpiochip_remove(chip);
1823 gpiochip_add_err:
1824 clk_disable(at91_chip->clock);
1825 clk_enable_err:
1826 clk_unprepare(at91_chip->clock);
1827 clk_prepare_err:
1828 err:
1829 dev_err(&pdev->dev, "Failure %i for GPIO %i\n", ret, alias_idx);
1830
1831 return ret;
1832 }
1833
1834 static struct platform_driver at91_gpio_driver = {
1835 .driver = {
1836 .name = "gpio-at91",
1837 .owner = THIS_MODULE,
1838 .of_match_table = at91_gpio_of_match,
1839 },
1840 .probe = at91_gpio_probe,
1841 };
1842
1843 static struct platform_driver at91_pinctrl_driver = {
1844 .driver = {
1845 .name = "pinctrl-at91",
1846 .owner = THIS_MODULE,
1847 .of_match_table = at91_pinctrl_of_match,
1848 },
1849 .probe = at91_pinctrl_probe,
1850 .remove = at91_pinctrl_remove,
1851 };
1852
at91_pinctrl_init(void)1853 static int __init at91_pinctrl_init(void)
1854 {
1855 int ret;
1856
1857 ret = platform_driver_register(&at91_gpio_driver);
1858 if (ret)
1859 return ret;
1860 return platform_driver_register(&at91_pinctrl_driver);
1861 }
1862 arch_initcall(at91_pinctrl_init);
1863
at91_pinctrl_exit(void)1864 static void __exit at91_pinctrl_exit(void)
1865 {
1866 platform_driver_unregister(&at91_pinctrl_driver);
1867 }
1868
1869 module_exit(at91_pinctrl_exit);
1870 MODULE_AUTHOR("Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>");
1871 MODULE_DESCRIPTION("Atmel AT91 pinctrl driver");
1872 MODULE_LICENSE("GPL v2");
1873