1 /*
2 * Driver for the Atmel PIO4 controller
3 *
4 * Copyright (C) 2015 Atmel,
5 * 2015 Ludovic Desroches <ludovic.desroches@atmel.com>
6 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17 #include <linux/clk.h>
18 #include <linux/gpio.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/platform_device.h>
24 #include <linux/pinctrl/pinconf.h>
25 #include <linux/pinctrl/pinconf-generic.h>
26 #include <linux/pinctrl/pinctrl.h>
27 #include <linux/pinctrl/pinmux.h>
28 #include <linux/slab.h>
29 #include "core.h"
30 #include "pinconf.h"
31 #include "pinctrl-utils.h"
32
33 /*
34 * Warning:
35 * In order to not introduce confusion between Atmel PIO groups and pinctrl
36 * framework groups, Atmel PIO groups will be called banks, line is kept to
37 * designed the pin id into this bank.
38 */
39
40 #define ATMEL_PIO_MSKR 0x0000
41 #define ATMEL_PIO_CFGR 0x0004
42 #define ATMEL_PIO_CFGR_FUNC_MASK GENMASK(2, 0)
43 #define ATMEL_PIO_DIR_MASK BIT(8)
44 #define ATMEL_PIO_PUEN_MASK BIT(9)
45 #define ATMEL_PIO_PDEN_MASK BIT(10)
46 #define ATMEL_PIO_IFEN_MASK BIT(12)
47 #define ATMEL_PIO_IFSCEN_MASK BIT(13)
48 #define ATMEL_PIO_OPD_MASK BIT(14)
49 #define ATMEL_PIO_SCHMITT_MASK BIT(15)
50 #define ATMEL_PIO_CFGR_EVTSEL_MASK GENMASK(26, 24)
51 #define ATMEL_PIO_CFGR_EVTSEL_FALLING (0 << 24)
52 #define ATMEL_PIO_CFGR_EVTSEL_RISING (1 << 24)
53 #define ATMEL_PIO_CFGR_EVTSEL_BOTH (2 << 24)
54 #define ATMEL_PIO_CFGR_EVTSEL_LOW (3 << 24)
55 #define ATMEL_PIO_CFGR_EVTSEL_HIGH (4 << 24)
56 #define ATMEL_PIO_PDSR 0x0008
57 #define ATMEL_PIO_LOCKSR 0x000C
58 #define ATMEL_PIO_SODR 0x0010
59 #define ATMEL_PIO_CODR 0x0014
60 #define ATMEL_PIO_ODSR 0x0018
61 #define ATMEL_PIO_IER 0x0020
62 #define ATMEL_PIO_IDR 0x0024
63 #define ATMEL_PIO_IMR 0x0028
64 #define ATMEL_PIO_ISR 0x002C
65 #define ATMEL_PIO_IOFR 0x003C
66
67 #define ATMEL_PIO_NPINS_PER_BANK 32
68 #define ATMEL_PIO_BANK(pin_id) (pin_id / ATMEL_PIO_NPINS_PER_BANK)
69 #define ATMEL_PIO_LINE(pin_id) (pin_id % ATMEL_PIO_NPINS_PER_BANK)
70 #define ATMEL_PIO_BANK_OFFSET 0x40
71
72 #define ATMEL_GET_PIN_NO(pinfunc) ((pinfunc) & 0xff)
73 #define ATMEL_GET_PIN_FUNC(pinfunc) ((pinfunc >> 16) & 0xf)
74 #define ATMEL_GET_PIN_IOSET(pinfunc) ((pinfunc >> 20) & 0xf)
75
76 struct atmel_pioctrl_data {
77 unsigned nbanks;
78 };
79
80 struct atmel_group {
81 const char *name;
82 u32 pin;
83 };
84
85 struct atmel_pin {
86 unsigned pin_id;
87 unsigned mux;
88 unsigned ioset;
89 unsigned bank;
90 unsigned line;
91 const char *device;
92 };
93
94 /**
95 * struct atmel_pioctrl - Atmel PIO controller (pinmux + gpio)
96 * @reg_base: base address of the controller.
97 * @clk: clock of the controller.
98 * @nbanks: number of PIO groups, it can vary depending on the SoC.
99 * @pinctrl_dev: pinctrl device registered.
100 * @groups: groups table to provide group name and pin in the group to pinctrl.
101 * @group_names: group names table to provide all the group/pin names to
102 * pinctrl or gpio.
103 * @pins: pins table used for both pinctrl and gpio. pin_id, bank and line
104 * fields are set at probe time. Other ones are set when parsing dt
105 * pinctrl.
106 * @npins: number of pins.
107 * @gpio_chip: gpio chip registered.
108 * @irq_domain: irq domain for the gpio controller.
109 * @irqs: table containing the hw irq number of the bank. The index of the
110 * table is the bank id.
111 * @dev: device entry for the Atmel PIO controller.
112 * @node: node of the Atmel PIO controller.
113 */
114 struct atmel_pioctrl {
115 void __iomem *reg_base;
116 struct clk *clk;
117 unsigned nbanks;
118 struct pinctrl_dev *pinctrl_dev;
119 struct atmel_group *groups;
120 const char * const *group_names;
121 struct atmel_pin **pins;
122 unsigned npins;
123 struct gpio_chip *gpio_chip;
124 struct irq_domain *irq_domain;
125 int *irqs;
126 unsigned *pm_wakeup_sources;
127 unsigned *pm_suspend_backup;
128 struct device *dev;
129 struct device_node *node;
130 };
131
132 static const char * const atmel_functions[] = {
133 "GPIO", "A", "B", "C", "D", "E", "F", "G"
134 };
135
136 /* --- GPIO --- */
atmel_gpio_read(struct atmel_pioctrl * atmel_pioctrl,unsigned int bank,unsigned int reg)137 static unsigned int atmel_gpio_read(struct atmel_pioctrl *atmel_pioctrl,
138 unsigned int bank, unsigned int reg)
139 {
140 return readl_relaxed(atmel_pioctrl->reg_base
141 + ATMEL_PIO_BANK_OFFSET * bank + reg);
142 }
143
atmel_gpio_write(struct atmel_pioctrl * atmel_pioctrl,unsigned int bank,unsigned int reg,unsigned int val)144 static void atmel_gpio_write(struct atmel_pioctrl *atmel_pioctrl,
145 unsigned int bank, unsigned int reg,
146 unsigned int val)
147 {
148 writel_relaxed(val, atmel_pioctrl->reg_base
149 + ATMEL_PIO_BANK_OFFSET * bank + reg);
150 }
151
atmel_gpio_irq_ack(struct irq_data * d)152 static void atmel_gpio_irq_ack(struct irq_data *d)
153 {
154 /*
155 * Nothing to do, interrupt is cleared when reading the status
156 * register.
157 */
158 }
159
atmel_gpio_irq_set_type(struct irq_data * d,unsigned type)160 static int atmel_gpio_irq_set_type(struct irq_data *d, unsigned type)
161 {
162 struct atmel_pioctrl *atmel_pioctrl = irq_data_get_irq_chip_data(d);
163 struct atmel_pin *pin = atmel_pioctrl->pins[d->hwirq];
164 unsigned reg;
165
166 atmel_gpio_write(atmel_pioctrl, pin->bank, ATMEL_PIO_MSKR,
167 BIT(pin->line));
168 reg = atmel_gpio_read(atmel_pioctrl, pin->bank, ATMEL_PIO_CFGR);
169 reg &= (~ATMEL_PIO_CFGR_EVTSEL_MASK);
170
171 switch (type) {
172 case IRQ_TYPE_EDGE_RISING:
173 irq_set_handler_locked(d, handle_edge_irq);
174 reg |= ATMEL_PIO_CFGR_EVTSEL_RISING;
175 break;
176 case IRQ_TYPE_EDGE_FALLING:
177 irq_set_handler_locked(d, handle_edge_irq);
178 reg |= ATMEL_PIO_CFGR_EVTSEL_FALLING;
179 break;
180 case IRQ_TYPE_EDGE_BOTH:
181 irq_set_handler_locked(d, handle_edge_irq);
182 reg |= ATMEL_PIO_CFGR_EVTSEL_BOTH;
183 break;
184 case IRQ_TYPE_LEVEL_LOW:
185 irq_set_handler_locked(d, handle_level_irq);
186 reg |= ATMEL_PIO_CFGR_EVTSEL_LOW;
187 break;
188 case IRQ_TYPE_LEVEL_HIGH:
189 irq_set_handler_locked(d, handle_level_irq);
190 reg |= ATMEL_PIO_CFGR_EVTSEL_HIGH;
191 break;
192 case IRQ_TYPE_NONE:
193 default:
194 return -EINVAL;
195 }
196
197 atmel_gpio_write(atmel_pioctrl, pin->bank, ATMEL_PIO_CFGR, reg);
198
199 return 0;
200 }
201
atmel_gpio_irq_mask(struct irq_data * d)202 static void atmel_gpio_irq_mask(struct irq_data *d)
203 {
204 struct atmel_pioctrl *atmel_pioctrl = irq_data_get_irq_chip_data(d);
205 struct atmel_pin *pin = atmel_pioctrl->pins[d->hwirq];
206
207 atmel_gpio_write(atmel_pioctrl, pin->bank, ATMEL_PIO_IDR,
208 BIT(pin->line));
209 }
210
atmel_gpio_irq_unmask(struct irq_data * d)211 static void atmel_gpio_irq_unmask(struct irq_data *d)
212 {
213 struct atmel_pioctrl *atmel_pioctrl = irq_data_get_irq_chip_data(d);
214 struct atmel_pin *pin = atmel_pioctrl->pins[d->hwirq];
215
216 atmel_gpio_write(atmel_pioctrl, pin->bank, ATMEL_PIO_IER,
217 BIT(pin->line));
218 }
219
220 #ifdef CONFIG_PM_SLEEP
221
atmel_gpio_irq_set_wake(struct irq_data * d,unsigned int on)222 static int atmel_gpio_irq_set_wake(struct irq_data *d, unsigned int on)
223 {
224 struct atmel_pioctrl *atmel_pioctrl = irq_data_get_irq_chip_data(d);
225 int bank = ATMEL_PIO_BANK(d->hwirq);
226 int line = ATMEL_PIO_LINE(d->hwirq);
227
228 /* The gpio controller has one interrupt line per bank. */
229 irq_set_irq_wake(atmel_pioctrl->irqs[bank], on);
230
231 if (on)
232 atmel_pioctrl->pm_wakeup_sources[bank] |= BIT(line);
233 else
234 atmel_pioctrl->pm_wakeup_sources[bank] &= ~(BIT(line));
235
236 return 0;
237 }
238 #else
239 #define atmel_gpio_irq_set_wake NULL
240 #endif /* CONFIG_PM_SLEEP */
241
242 static struct irq_chip atmel_gpio_irq_chip = {
243 .name = "GPIO",
244 .irq_ack = atmel_gpio_irq_ack,
245 .irq_mask = atmel_gpio_irq_mask,
246 .irq_unmask = atmel_gpio_irq_unmask,
247 .irq_set_type = atmel_gpio_irq_set_type,
248 .irq_set_wake = atmel_gpio_irq_set_wake,
249 };
250
atmel_gpio_irq_handler(struct irq_desc * desc)251 static void atmel_gpio_irq_handler(struct irq_desc *desc)
252 {
253 unsigned int irq = irq_desc_get_irq(desc);
254 struct atmel_pioctrl *atmel_pioctrl = irq_desc_get_handler_data(desc);
255 struct irq_chip *chip = irq_desc_get_chip(desc);
256 unsigned long isr;
257 int n, bank = -1;
258
259 /* Find from which bank is the irq received. */
260 for (n = 0; n < atmel_pioctrl->nbanks; n++) {
261 if (atmel_pioctrl->irqs[n] == irq) {
262 bank = n;
263 break;
264 }
265 }
266
267 if (bank < 0) {
268 dev_err(atmel_pioctrl->dev,
269 "no bank associated to irq %u\n", irq);
270 return;
271 }
272
273 chained_irq_enter(chip, desc);
274
275 for (;;) {
276 isr = (unsigned long)atmel_gpio_read(atmel_pioctrl, bank,
277 ATMEL_PIO_ISR);
278 isr &= (unsigned long)atmel_gpio_read(atmel_pioctrl, bank,
279 ATMEL_PIO_IMR);
280 if (!isr)
281 break;
282
283 for_each_set_bit(n, &isr, BITS_PER_LONG)
284 generic_handle_irq(gpio_to_irq(bank *
285 ATMEL_PIO_NPINS_PER_BANK + n));
286 }
287
288 chained_irq_exit(chip, desc);
289 }
290
atmel_gpio_direction_input(struct gpio_chip * chip,unsigned offset)291 static int atmel_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
292 {
293 struct atmel_pioctrl *atmel_pioctrl = dev_get_drvdata(chip->dev);
294 struct atmel_pin *pin = atmel_pioctrl->pins[offset];
295 unsigned reg;
296
297 atmel_gpio_write(atmel_pioctrl, pin->bank, ATMEL_PIO_MSKR,
298 BIT(pin->line));
299 reg = atmel_gpio_read(atmel_pioctrl, pin->bank, ATMEL_PIO_CFGR);
300 reg &= ~ATMEL_PIO_DIR_MASK;
301 atmel_gpio_write(atmel_pioctrl, pin->bank, ATMEL_PIO_CFGR, reg);
302
303 return 0;
304 }
305
atmel_gpio_get(struct gpio_chip * chip,unsigned offset)306 static int atmel_gpio_get(struct gpio_chip *chip, unsigned offset)
307 {
308 struct atmel_pioctrl *atmel_pioctrl = dev_get_drvdata(chip->dev);
309 struct atmel_pin *pin = atmel_pioctrl->pins[offset];
310 unsigned reg;
311
312 reg = atmel_gpio_read(atmel_pioctrl, pin->bank, ATMEL_PIO_PDSR);
313
314 return !!(reg & BIT(pin->line));
315 }
316
atmel_gpio_direction_output(struct gpio_chip * chip,unsigned offset,int value)317 static int atmel_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
318 int value)
319 {
320 struct atmel_pioctrl *atmel_pioctrl = dev_get_drvdata(chip->dev);
321 struct atmel_pin *pin = atmel_pioctrl->pins[offset];
322 unsigned reg;
323
324 atmel_gpio_write(atmel_pioctrl, pin->bank,
325 value ? ATMEL_PIO_SODR : ATMEL_PIO_CODR,
326 BIT(pin->line));
327
328 atmel_gpio_write(atmel_pioctrl, pin->bank, ATMEL_PIO_MSKR,
329 BIT(pin->line));
330 reg = atmel_gpio_read(atmel_pioctrl, pin->bank, ATMEL_PIO_CFGR);
331 reg |= ATMEL_PIO_DIR_MASK;
332 atmel_gpio_write(atmel_pioctrl, pin->bank, ATMEL_PIO_CFGR, reg);
333
334 return 0;
335 }
336
atmel_gpio_set(struct gpio_chip * chip,unsigned offset,int val)337 static void atmel_gpio_set(struct gpio_chip *chip, unsigned offset, int val)
338 {
339 struct atmel_pioctrl *atmel_pioctrl = dev_get_drvdata(chip->dev);
340 struct atmel_pin *pin = atmel_pioctrl->pins[offset];
341
342 atmel_gpio_write(atmel_pioctrl, pin->bank,
343 val ? ATMEL_PIO_SODR : ATMEL_PIO_CODR,
344 BIT(pin->line));
345 }
346
atmel_gpio_to_irq(struct gpio_chip * chip,unsigned offset)347 static int atmel_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
348 {
349 struct atmel_pioctrl *atmel_pioctrl = dev_get_drvdata(chip->dev);
350
351 return irq_find_mapping(atmel_pioctrl->irq_domain, offset);
352 }
353
354 static struct gpio_chip atmel_gpio_chip = {
355 .direction_input = atmel_gpio_direction_input,
356 .get = atmel_gpio_get,
357 .direction_output = atmel_gpio_direction_output,
358 .set = atmel_gpio_set,
359 .to_irq = atmel_gpio_to_irq,
360 .base = 0,
361 };
362
363 /* --- PINCTRL --- */
atmel_pin_config_read(struct pinctrl_dev * pctldev,unsigned pin_id)364 static unsigned int atmel_pin_config_read(struct pinctrl_dev *pctldev,
365 unsigned pin_id)
366 {
367 struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
368 unsigned bank = atmel_pioctrl->pins[pin_id]->bank;
369 unsigned line = atmel_pioctrl->pins[pin_id]->line;
370 void __iomem *addr = atmel_pioctrl->reg_base
371 + bank * ATMEL_PIO_BANK_OFFSET;
372
373 writel_relaxed(BIT(line), addr + ATMEL_PIO_MSKR);
374 /* Have to set MSKR first, to access the right pin CFGR. */
375 wmb();
376
377 return readl_relaxed(addr + ATMEL_PIO_CFGR);
378 }
379
atmel_pin_config_write(struct pinctrl_dev * pctldev,unsigned pin_id,u32 conf)380 static void atmel_pin_config_write(struct pinctrl_dev *pctldev,
381 unsigned pin_id, u32 conf)
382 {
383 struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
384 unsigned bank = atmel_pioctrl->pins[pin_id]->bank;
385 unsigned line = atmel_pioctrl->pins[pin_id]->line;
386 void __iomem *addr = atmel_pioctrl->reg_base
387 + bank * ATMEL_PIO_BANK_OFFSET;
388
389 writel_relaxed(BIT(line), addr + ATMEL_PIO_MSKR);
390 /* Have to set MSKR first, to access the right pin CFGR. */
391 wmb();
392 writel_relaxed(conf, addr + ATMEL_PIO_CFGR);
393 }
394
atmel_pctl_get_groups_count(struct pinctrl_dev * pctldev)395 static int atmel_pctl_get_groups_count(struct pinctrl_dev *pctldev)
396 {
397 struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
398
399 return atmel_pioctrl->npins;
400 }
401
atmel_pctl_get_group_name(struct pinctrl_dev * pctldev,unsigned selector)402 static const char *atmel_pctl_get_group_name(struct pinctrl_dev *pctldev,
403 unsigned selector)
404 {
405 struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
406
407 return atmel_pioctrl->groups[selector].name;
408 }
409
atmel_pctl_get_group_pins(struct pinctrl_dev * pctldev,unsigned selector,const unsigned ** pins,unsigned * num_pins)410 static int atmel_pctl_get_group_pins(struct pinctrl_dev *pctldev,
411 unsigned selector, const unsigned **pins,
412 unsigned *num_pins)
413 {
414 struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
415
416 *pins = (unsigned *)&atmel_pioctrl->groups[selector].pin;
417 *num_pins = 1;
418
419 return 0;
420 }
421
atmel_pctl_find_group_by_pin(struct pinctrl_dev * pctldev,unsigned pin)422 struct atmel_group *atmel_pctl_find_group_by_pin(struct pinctrl_dev *pctldev,
423 unsigned pin)
424 {
425 struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
426 int i;
427
428 for (i = 0; i < atmel_pioctrl->npins; i++) {
429 struct atmel_group *grp = atmel_pioctrl->groups + i;
430
431 if (grp->pin == pin)
432 return grp;
433 }
434
435 return NULL;
436 }
437
atmel_pctl_xlate_pinfunc(struct pinctrl_dev * pctldev,struct device_node * np,u32 pinfunc,const char ** grp_name,const char ** func_name)438 static int atmel_pctl_xlate_pinfunc(struct pinctrl_dev *pctldev,
439 struct device_node *np,
440 u32 pinfunc, const char **grp_name,
441 const char **func_name)
442 {
443 struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
444 unsigned pin_id, func_id;
445 struct atmel_group *grp;
446
447 pin_id = ATMEL_GET_PIN_NO(pinfunc);
448 func_id = ATMEL_GET_PIN_FUNC(pinfunc);
449
450 if (func_id >= ARRAY_SIZE(atmel_functions))
451 return -EINVAL;
452
453 *func_name = atmel_functions[func_id];
454
455 grp = atmel_pctl_find_group_by_pin(pctldev, pin_id);
456 if (!grp)
457 return -EINVAL;
458 *grp_name = grp->name;
459
460 atmel_pioctrl->pins[pin_id]->mux = func_id;
461 atmel_pioctrl->pins[pin_id]->ioset = ATMEL_GET_PIN_IOSET(pinfunc);
462 /* Want the device name not the group one. */
463 if (np->parent == atmel_pioctrl->node)
464 atmel_pioctrl->pins[pin_id]->device = np->name;
465 else
466 atmel_pioctrl->pins[pin_id]->device = np->parent->name;
467
468 return 0;
469 }
470
atmel_pctl_dt_subnode_to_map(struct pinctrl_dev * pctldev,struct device_node * np,struct pinctrl_map ** map,unsigned * reserved_maps,unsigned * num_maps)471 static int atmel_pctl_dt_subnode_to_map(struct pinctrl_dev *pctldev,
472 struct device_node *np,
473 struct pinctrl_map **map,
474 unsigned *reserved_maps,
475 unsigned *num_maps)
476 {
477 unsigned num_pins, num_configs, reserve;
478 unsigned long *configs;
479 struct property *pins;
480 u32 pinfunc;
481 int ret, i;
482
483 pins = of_find_property(np, "pinmux", NULL);
484 if (!pins)
485 return -EINVAL;
486
487 ret = pinconf_generic_parse_dt_config(np, pctldev, &configs,
488 &num_configs);
489 if (ret < 0) {
490 dev_err(pctldev->dev, "%s: could not parse node property\n",
491 of_node_full_name(np));
492 return ret;
493 }
494
495 num_pins = pins->length / sizeof(u32);
496 if (!num_pins) {
497 dev_err(pctldev->dev, "no pins found in node %s\n",
498 of_node_full_name(np));
499 return -EINVAL;
500 }
501
502 /*
503 * Reserve maps, at least there is a mux map and an optional conf
504 * map for each pin.
505 */
506 reserve = 1;
507 if (num_configs)
508 reserve++;
509 reserve *= num_pins;
510 ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps, num_maps,
511 reserve);
512 if (ret < 0)
513 return ret;
514
515 for (i = 0; i < num_pins; i++) {
516 const char *group, *func;
517
518 ret = of_property_read_u32_index(np, "pinmux", i, &pinfunc);
519 if (ret)
520 return ret;
521
522 ret = atmel_pctl_xlate_pinfunc(pctldev, np, pinfunc, &group,
523 &func);
524 if (ret)
525 return ret;
526
527 pinctrl_utils_add_map_mux(pctldev, map, reserved_maps, num_maps,
528 group, func);
529
530 if (num_configs) {
531 ret = pinctrl_utils_add_map_configs(pctldev, map,
532 reserved_maps, num_maps, group,
533 configs, num_configs,
534 PIN_MAP_TYPE_CONFIGS_GROUP);
535 if (ret < 0)
536 return ret;
537 }
538 }
539
540 return 0;
541 }
542
atmel_pctl_dt_node_to_map(struct pinctrl_dev * pctldev,struct device_node * np_config,struct pinctrl_map ** map,unsigned * num_maps)543 static int atmel_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
544 struct device_node *np_config,
545 struct pinctrl_map **map,
546 unsigned *num_maps)
547 {
548 struct device_node *np;
549 unsigned reserved_maps;
550 int ret;
551
552 *map = NULL;
553 *num_maps = 0;
554 reserved_maps = 0;
555
556 /*
557 * If all the pins of a device have the same configuration (or no one),
558 * it is useless to add a subnode, so directly parse node referenced by
559 * phandle.
560 */
561 ret = atmel_pctl_dt_subnode_to_map(pctldev, np_config, map,
562 &reserved_maps, num_maps);
563 if (ret) {
564 for_each_child_of_node(np_config, np) {
565 ret = atmel_pctl_dt_subnode_to_map(pctldev, np, map,
566 &reserved_maps, num_maps);
567 if (ret < 0) {
568 of_node_put(np);
569 break;
570 }
571 }
572 }
573
574 if (ret < 0) {
575 pinctrl_utils_dt_free_map(pctldev, *map, *num_maps);
576 dev_err(pctldev->dev, "can't create maps for node %s\n",
577 np_config->full_name);
578 }
579
580 return ret;
581 }
582
583 static const struct pinctrl_ops atmel_pctlops = {
584 .get_groups_count = atmel_pctl_get_groups_count,
585 .get_group_name = atmel_pctl_get_group_name,
586 .get_group_pins = atmel_pctl_get_group_pins,
587 .dt_node_to_map = atmel_pctl_dt_node_to_map,
588 .dt_free_map = pinctrl_utils_dt_free_map,
589 };
590
atmel_pmx_get_functions_count(struct pinctrl_dev * pctldev)591 static int atmel_pmx_get_functions_count(struct pinctrl_dev *pctldev)
592 {
593 return ARRAY_SIZE(atmel_functions);
594 }
595
atmel_pmx_get_function_name(struct pinctrl_dev * pctldev,unsigned selector)596 static const char *atmel_pmx_get_function_name(struct pinctrl_dev *pctldev,
597 unsigned selector)
598 {
599 return atmel_functions[selector];
600 }
601
atmel_pmx_get_function_groups(struct pinctrl_dev * pctldev,unsigned selector,const char * const ** groups,unsigned * const num_groups)602 static int atmel_pmx_get_function_groups(struct pinctrl_dev *pctldev,
603 unsigned selector,
604 const char * const **groups,
605 unsigned * const num_groups)
606 {
607 struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
608
609 *groups = atmel_pioctrl->group_names;
610 *num_groups = atmel_pioctrl->npins;
611
612 return 0;
613 }
614
atmel_pmx_set_mux(struct pinctrl_dev * pctldev,unsigned function,unsigned group)615 static int atmel_pmx_set_mux(struct pinctrl_dev *pctldev,
616 unsigned function,
617 unsigned group)
618 {
619 struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
620 unsigned pin;
621 u32 conf;
622
623 dev_dbg(pctldev->dev, "enable function %s group %s\n",
624 atmel_functions[function], atmel_pioctrl->groups[group].name);
625
626 pin = atmel_pioctrl->groups[group].pin;
627 conf = atmel_pin_config_read(pctldev, pin);
628 conf &= (~ATMEL_PIO_CFGR_FUNC_MASK);
629 conf |= (function & ATMEL_PIO_CFGR_FUNC_MASK);
630 dev_dbg(pctldev->dev, "pin: %u, conf: 0x%08x\n", pin, conf);
631 atmel_pin_config_write(pctldev, pin, conf);
632
633 return 0;
634 }
635
636 static const struct pinmux_ops atmel_pmxops = {
637 .get_functions_count = atmel_pmx_get_functions_count,
638 .get_function_name = atmel_pmx_get_function_name,
639 .get_function_groups = atmel_pmx_get_function_groups,
640 .set_mux = atmel_pmx_set_mux,
641 };
642
atmel_conf_pin_config_group_get(struct pinctrl_dev * pctldev,unsigned group,unsigned long * config)643 static int atmel_conf_pin_config_group_get(struct pinctrl_dev *pctldev,
644 unsigned group,
645 unsigned long *config)
646 {
647 struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
648 unsigned param = pinconf_to_config_param(*config), arg = 0;
649 struct atmel_group *grp = atmel_pioctrl->groups + group;
650 unsigned pin_id = grp->pin;
651 u32 res;
652
653 res = atmel_pin_config_read(pctldev, pin_id);
654
655 switch (param) {
656 case PIN_CONFIG_BIAS_PULL_UP:
657 if (!(res & ATMEL_PIO_PUEN_MASK))
658 return -EINVAL;
659 arg = 1;
660 break;
661 case PIN_CONFIG_BIAS_PULL_DOWN:
662 if ((res & ATMEL_PIO_PUEN_MASK) ||
663 (!(res & ATMEL_PIO_PDEN_MASK)))
664 return -EINVAL;
665 arg = 1;
666 break;
667 case PIN_CONFIG_BIAS_DISABLE:
668 if ((res & ATMEL_PIO_PUEN_MASK) ||
669 ((res & ATMEL_PIO_PDEN_MASK)))
670 return -EINVAL;
671 arg = 1;
672 break;
673 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
674 if (!(res & ATMEL_PIO_OPD_MASK))
675 return -EINVAL;
676 arg = 1;
677 break;
678 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
679 if (!(res & ATMEL_PIO_SCHMITT_MASK))
680 return -EINVAL;
681 arg = 1;
682 break;
683 default:
684 return -ENOTSUPP;
685 }
686
687 *config = pinconf_to_config_packed(param, arg);
688 return 0;
689 }
690
atmel_conf_pin_config_group_set(struct pinctrl_dev * pctldev,unsigned group,unsigned long * configs,unsigned num_configs)691 static int atmel_conf_pin_config_group_set(struct pinctrl_dev *pctldev,
692 unsigned group,
693 unsigned long *configs,
694 unsigned num_configs)
695 {
696 struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
697 struct atmel_group *grp = atmel_pioctrl->groups + group;
698 unsigned bank, pin, pin_id = grp->pin;
699 u32 mask, conf = 0;
700 int i;
701
702 conf = atmel_pin_config_read(pctldev, pin_id);
703
704 for (i = 0; i < num_configs; i++) {
705 unsigned param = pinconf_to_config_param(configs[i]);
706 unsigned arg = pinconf_to_config_argument(configs[i]);
707
708 dev_dbg(pctldev->dev, "%s: pin=%u, config=0x%lx\n",
709 __func__, pin_id, configs[i]);
710
711 switch (param) {
712 case PIN_CONFIG_BIAS_DISABLE:
713 conf &= (~ATMEL_PIO_PUEN_MASK);
714 conf &= (~ATMEL_PIO_PDEN_MASK);
715 break;
716 case PIN_CONFIG_BIAS_PULL_UP:
717 conf |= ATMEL_PIO_PUEN_MASK;
718 conf &= (~ATMEL_PIO_PDEN_MASK);
719 break;
720 case PIN_CONFIG_BIAS_PULL_DOWN:
721 conf |= ATMEL_PIO_PDEN_MASK;
722 conf &= (~ATMEL_PIO_PUEN_MASK);
723 break;
724 case PIN_CONFIG_DRIVE_OPEN_DRAIN:
725 if (arg == 0)
726 conf &= (~ATMEL_PIO_OPD_MASK);
727 else
728 conf |= ATMEL_PIO_OPD_MASK;
729 break;
730 case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
731 if (arg == 0)
732 conf |= ATMEL_PIO_SCHMITT_MASK;
733 else
734 conf &= (~ATMEL_PIO_SCHMITT_MASK);
735 break;
736 case PIN_CONFIG_INPUT_DEBOUNCE:
737 if (arg == 0) {
738 conf &= (~ATMEL_PIO_IFEN_MASK);
739 conf &= (~ATMEL_PIO_IFSCEN_MASK);
740 } else {
741 /*
742 * We don't care about the debounce value for several reasons:
743 * - can't have different debounce periods inside a same group,
744 * - the register to configure this period is a secure register.
745 * The debouncing filter can filter a pulse with a duration of less
746 * than 1/2 slow clock period.
747 */
748 conf |= ATMEL_PIO_IFEN_MASK;
749 conf |= ATMEL_PIO_IFSCEN_MASK;
750 }
751 break;
752 case PIN_CONFIG_OUTPUT:
753 conf |= ATMEL_PIO_DIR_MASK;
754 bank = ATMEL_PIO_BANK(pin_id);
755 pin = ATMEL_PIO_LINE(pin_id);
756 mask = 1 << pin;
757
758 if (arg == 0) {
759 writel_relaxed(mask, atmel_pioctrl->reg_base +
760 bank * ATMEL_PIO_BANK_OFFSET +
761 ATMEL_PIO_CODR);
762 } else {
763 writel_relaxed(mask, atmel_pioctrl->reg_base +
764 bank * ATMEL_PIO_BANK_OFFSET +
765 ATMEL_PIO_SODR);
766 }
767 break;
768 default:
769 dev_warn(pctldev->dev,
770 "unsupported configuration parameter: %u\n",
771 param);
772 continue;
773 }
774 }
775
776 dev_dbg(pctldev->dev, "%s: reg=0x%08x\n", __func__, conf);
777 atmel_pin_config_write(pctldev, pin_id, conf);
778
779 return 0;
780 }
781
atmel_conf_pin_config_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned pin_id)782 static void atmel_conf_pin_config_dbg_show(struct pinctrl_dev *pctldev,
783 struct seq_file *s, unsigned pin_id)
784 {
785 struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
786 u32 conf;
787
788 if (!atmel_pioctrl->pins[pin_id]->device)
789 return;
790
791 if (atmel_pioctrl->pins[pin_id])
792 seq_printf(s, " (%s, ioset %u) ",
793 atmel_pioctrl->pins[pin_id]->device,
794 atmel_pioctrl->pins[pin_id]->ioset);
795
796 conf = atmel_pin_config_read(pctldev, pin_id);
797 if (conf & ATMEL_PIO_PUEN_MASK)
798 seq_printf(s, "%s ", "pull-up");
799 if (conf & ATMEL_PIO_PDEN_MASK)
800 seq_printf(s, "%s ", "pull-down");
801 if (conf & ATMEL_PIO_IFEN_MASK)
802 seq_printf(s, "%s ", "debounce");
803 if (conf & ATMEL_PIO_OPD_MASK)
804 seq_printf(s, "%s ", "open-drain");
805 if (conf & ATMEL_PIO_SCHMITT_MASK)
806 seq_printf(s, "%s ", "schmitt");
807 }
808
809 static const struct pinconf_ops atmel_confops = {
810 .pin_config_group_get = atmel_conf_pin_config_group_get,
811 .pin_config_group_set = atmel_conf_pin_config_group_set,
812 .pin_config_dbg_show = atmel_conf_pin_config_dbg_show,
813 };
814
815 static struct pinctrl_desc atmel_pinctrl_desc = {
816 .name = "atmel_pinctrl",
817 .confops = &atmel_confops,
818 .pctlops = &atmel_pctlops,
819 .pmxops = &atmel_pmxops,
820 };
821
atmel_pctrl_suspend(struct device * dev)822 static int atmel_pctrl_suspend(struct device *dev)
823 {
824 struct platform_device *pdev = to_platform_device(dev);
825 struct atmel_pioctrl *atmel_pioctrl = platform_get_drvdata(pdev);
826 int i;
827
828 /*
829 * For each bank, save IMR to restore it later and disable all GPIO
830 * interrupts excepting the ones marked as wakeup sources.
831 */
832 for (i = 0; i < atmel_pioctrl->nbanks; i++) {
833 atmel_pioctrl->pm_suspend_backup[i] =
834 atmel_gpio_read(atmel_pioctrl, i, ATMEL_PIO_IMR);
835 atmel_gpio_write(atmel_pioctrl, i, ATMEL_PIO_IDR,
836 ~atmel_pioctrl->pm_wakeup_sources[i]);
837 }
838
839 return 0;
840 }
841
atmel_pctrl_resume(struct device * dev)842 static int atmel_pctrl_resume(struct device *dev)
843 {
844 struct platform_device *pdev = to_platform_device(dev);
845 struct atmel_pioctrl *atmel_pioctrl = platform_get_drvdata(pdev);
846 int i;
847
848 for (i = 0; i < atmel_pioctrl->nbanks; i++)
849 atmel_gpio_write(atmel_pioctrl, i, ATMEL_PIO_IER,
850 atmel_pioctrl->pm_suspend_backup[i]);
851
852 return 0;
853 }
854
855 static const struct dev_pm_ops atmel_pctrl_pm_ops = {
856 SET_SYSTEM_SLEEP_PM_OPS(atmel_pctrl_suspend, atmel_pctrl_resume)
857 };
858
859 /*
860 * The number of banks can be different from a SoC to another one.
861 * We can have up to 16 banks.
862 */
863 static const struct atmel_pioctrl_data atmel_sama5d2_pioctrl_data = {
864 .nbanks = 4,
865 };
866
867 static const struct of_device_id atmel_pctrl_of_match[] = {
868 {
869 .compatible = "atmel,sama5d2-pinctrl",
870 .data = &atmel_sama5d2_pioctrl_data,
871 }, {
872 /* sentinel */
873 }
874 };
875 MODULE_DEVICE_TABLE(of, atmel_pctrl_of_match);
876
atmel_pinctrl_probe(struct platform_device * pdev)877 static int atmel_pinctrl_probe(struct platform_device *pdev)
878 {
879 struct device *dev = &pdev->dev;
880 struct pinctrl_pin_desc *pin_desc;
881 const char **group_names;
882 const struct of_device_id *match;
883 int i, ret;
884 struct resource *res;
885 struct atmel_pioctrl *atmel_pioctrl;
886 struct atmel_pioctrl_data *atmel_pioctrl_data;
887
888 atmel_pioctrl = devm_kzalloc(dev, sizeof(*atmel_pioctrl), GFP_KERNEL);
889 if (!atmel_pioctrl)
890 return -ENOMEM;
891 atmel_pioctrl->dev = dev;
892 atmel_pioctrl->node = dev->of_node;
893 platform_set_drvdata(pdev, atmel_pioctrl);
894
895 match = of_match_node(atmel_pctrl_of_match, dev->of_node);
896 if (!match) {
897 dev_err(dev, "unknown compatible string\n");
898 return -ENODEV;
899 }
900 atmel_pioctrl_data = (struct atmel_pioctrl_data *)match->data;
901 atmel_pioctrl->nbanks = atmel_pioctrl_data->nbanks;
902 atmel_pioctrl->npins = atmel_pioctrl->nbanks * ATMEL_PIO_NPINS_PER_BANK;
903
904 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
905 if (!res) {
906 dev_err(dev, "unable to get atmel pinctrl resource\n");
907 return -EINVAL;
908 }
909 atmel_pioctrl->reg_base = devm_ioremap_resource(dev, res);
910 if (IS_ERR(atmel_pioctrl->reg_base))
911 return -EINVAL;
912
913 atmel_pioctrl->clk = devm_clk_get(dev, NULL);
914 if (IS_ERR(atmel_pioctrl->clk)) {
915 dev_err(dev, "failed to get clock\n");
916 return PTR_ERR(atmel_pioctrl->clk);
917 }
918
919 atmel_pioctrl->pins = devm_kzalloc(dev, sizeof(*atmel_pioctrl->pins)
920 * atmel_pioctrl->npins, GFP_KERNEL);
921 if (!atmel_pioctrl->pins)
922 return -ENOMEM;
923
924 pin_desc = devm_kzalloc(dev, sizeof(*pin_desc)
925 * atmel_pioctrl->npins, GFP_KERNEL);
926 if (!pin_desc)
927 return -ENOMEM;
928 atmel_pinctrl_desc.pins = pin_desc;
929 atmel_pinctrl_desc.npins = atmel_pioctrl->npins;
930
931 /* One pin is one group since a pin can achieve all functions. */
932 group_names = devm_kzalloc(dev, sizeof(*group_names)
933 * atmel_pioctrl->npins, GFP_KERNEL);
934 if (!group_names)
935 return -ENOMEM;
936 atmel_pioctrl->group_names = group_names;
937
938 atmel_pioctrl->groups = devm_kzalloc(&pdev->dev,
939 sizeof(*atmel_pioctrl->groups) * atmel_pioctrl->npins,
940 GFP_KERNEL);
941 if (!atmel_pioctrl->groups)
942 return -ENOMEM;
943 for (i = 0 ; i < atmel_pioctrl->npins; i++) {
944 struct atmel_group *group = atmel_pioctrl->groups + i;
945 unsigned bank = ATMEL_PIO_BANK(i);
946 unsigned line = ATMEL_PIO_LINE(i);
947
948 atmel_pioctrl->pins[i] = devm_kzalloc(dev,
949 sizeof(**atmel_pioctrl->pins), GFP_KERNEL);
950 if (!atmel_pioctrl->pins[i])
951 return -ENOMEM;
952
953 atmel_pioctrl->pins[i]->pin_id = i;
954 atmel_pioctrl->pins[i]->bank = bank;
955 atmel_pioctrl->pins[i]->line = line;
956
957 pin_desc[i].number = i;
958 /* Pin naming convention: P(bank_name)(bank_pin_number). */
959 pin_desc[i].name = kasprintf(GFP_KERNEL, "P%c%d",
960 bank + 'A', line);
961
962 group->name = group_names[i] = pin_desc[i].name;
963 group->pin = pin_desc[i].number;
964
965 dev_dbg(dev, "pin_id=%u, bank=%u, line=%u", i, bank, line);
966 }
967
968 atmel_pioctrl->gpio_chip = &atmel_gpio_chip;
969 atmel_pioctrl->gpio_chip->of_node = dev->of_node;
970 atmel_pioctrl->gpio_chip->ngpio = atmel_pioctrl->npins;
971 atmel_pioctrl->gpio_chip->label = dev_name(dev);
972 atmel_pioctrl->gpio_chip->dev = dev;
973 atmel_pioctrl->gpio_chip->names = atmel_pioctrl->group_names;
974
975 atmel_pioctrl->pm_wakeup_sources = devm_kzalloc(dev,
976 sizeof(*atmel_pioctrl->pm_wakeup_sources)
977 * atmel_pioctrl->nbanks, GFP_KERNEL);
978 if (!atmel_pioctrl->pm_wakeup_sources)
979 return -ENOMEM;
980
981 atmel_pioctrl->pm_suspend_backup = devm_kzalloc(dev,
982 sizeof(*atmel_pioctrl->pm_suspend_backup)
983 * atmel_pioctrl->nbanks, GFP_KERNEL);
984 if (!atmel_pioctrl->pm_suspend_backup)
985 return -ENOMEM;
986
987 atmel_pioctrl->irqs = devm_kzalloc(dev, sizeof(*atmel_pioctrl->irqs)
988 * atmel_pioctrl->nbanks, GFP_KERNEL);
989 if (!atmel_pioctrl->irqs)
990 return -ENOMEM;
991
992 /* There is one controller but each bank has its own irq line. */
993 for (i = 0; i < atmel_pioctrl->nbanks; i++) {
994 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
995 if (!res) {
996 dev_err(dev, "missing irq resource for group %c\n",
997 'A' + i);
998 return -EINVAL;
999 }
1000 atmel_pioctrl->irqs[i] = res->start;
1001 irq_set_chained_handler(res->start, atmel_gpio_irq_handler);
1002 irq_set_handler_data(res->start, atmel_pioctrl);
1003 dev_dbg(dev, "bank %i: irq=%pr\n", i, res);
1004 }
1005
1006 atmel_pioctrl->irq_domain = irq_domain_add_linear(dev->of_node,
1007 atmel_pioctrl->gpio_chip->ngpio,
1008 &irq_domain_simple_ops, NULL);
1009 if (!atmel_pioctrl->irq_domain) {
1010 dev_err(dev, "can't add the irq domain\n");
1011 return -ENODEV;
1012 }
1013 atmel_pioctrl->irq_domain->name = "atmel gpio";
1014
1015 for (i = 0; i < atmel_pioctrl->npins; i++) {
1016 int irq = irq_create_mapping(atmel_pioctrl->irq_domain, i);
1017
1018 irq_set_chip_and_handler(irq, &atmel_gpio_irq_chip,
1019 handle_simple_irq);
1020 irq_set_chip_data(irq, atmel_pioctrl);
1021 dev_dbg(dev,
1022 "atmel gpio irq domain: hwirq: %d, linux irq: %d\n",
1023 i, irq);
1024 }
1025
1026 ret = clk_prepare_enable(atmel_pioctrl->clk);
1027 if (ret) {
1028 dev_err(dev, "failed to prepare and enable clock\n");
1029 goto clk_prepare_enable_error;
1030 }
1031
1032 atmel_pioctrl->pinctrl_dev = pinctrl_register(&atmel_pinctrl_desc,
1033 &pdev->dev,
1034 atmel_pioctrl);
1035 if (!atmel_pioctrl->pinctrl_dev) {
1036 dev_err(dev, "pinctrl registration failed\n");
1037 goto pinctrl_register_error;
1038 }
1039
1040 ret = gpiochip_add(atmel_pioctrl->gpio_chip);
1041 if (ret) {
1042 dev_err(dev, "failed to add gpiochip\n");
1043 goto gpiochip_add_error;
1044 }
1045
1046 ret = gpiochip_add_pin_range(atmel_pioctrl->gpio_chip, dev_name(dev),
1047 0, 0, atmel_pioctrl->gpio_chip->ngpio);
1048 if (ret) {
1049 dev_err(dev, "failed to add gpio pin range\n");
1050 goto gpiochip_add_pin_range_error;
1051 }
1052
1053 dev_info(&pdev->dev, "atmel pinctrl initialized\n");
1054
1055 return 0;
1056
1057 clk_prepare_enable_error:
1058 irq_domain_remove(atmel_pioctrl->irq_domain);
1059 pinctrl_register_error:
1060 clk_disable_unprepare(atmel_pioctrl->clk);
1061 gpiochip_add_error:
1062 pinctrl_unregister(atmel_pioctrl->pinctrl_dev);
1063 gpiochip_add_pin_range_error:
1064 gpiochip_remove(atmel_pioctrl->gpio_chip);
1065
1066 return ret;
1067 }
1068
atmel_pinctrl_remove(struct platform_device * pdev)1069 int atmel_pinctrl_remove(struct platform_device *pdev)
1070 {
1071 struct atmel_pioctrl *atmel_pioctrl = platform_get_drvdata(pdev);
1072
1073 irq_domain_remove(atmel_pioctrl->irq_domain);
1074 clk_disable_unprepare(atmel_pioctrl->clk);
1075 pinctrl_unregister(atmel_pioctrl->pinctrl_dev);
1076 gpiochip_remove(atmel_pioctrl->gpio_chip);
1077
1078 return 0;
1079 }
1080
1081 static struct platform_driver atmel_pinctrl_driver = {
1082 .driver = {
1083 .name = "pinctrl-at91-pio4",
1084 .of_match_table = atmel_pctrl_of_match,
1085 .pm = &atmel_pctrl_pm_ops,
1086 },
1087 .probe = atmel_pinctrl_probe,
1088 .remove = atmel_pinctrl_remove,
1089 };
1090 module_platform_driver(atmel_pinctrl_driver);
1091
1092 MODULE_AUTHOR(Ludovic Desroches <ludovic.desroches@atmel.com>);
1093 MODULE_DESCRIPTION("Atmel PIO4 pinctrl driver");
1094 MODULE_LICENSE("GPL v2");
1095