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 	bool has_config;
481 	u32 pinfunc;
482 	int ret, i;
483 
484 	pins = of_find_property(np, "pinmux", NULL);
485 	if (!pins)
486 		return -EINVAL;
487 
488 	ret = pinconf_generic_parse_dt_config(np, pctldev, &configs,
489 					      &num_configs);
490 	if (ret < 0) {
491 		dev_err(pctldev->dev, "%s: could not parse node property\n",
492 			of_node_full_name(np));
493 		return ret;
494 	}
495 
496 	if (num_configs)
497 		has_config = true;
498 
499 	num_pins = pins->length / sizeof(u32);
500 	if (!num_pins) {
501 		dev_err(pctldev->dev, "no pins found in node %s\n",
502 			of_node_full_name(np));
503 		return -EINVAL;
504 	}
505 
506 	/*
507 	 * Reserve maps, at least there is a mux map and an optional conf
508 	 * map for each pin.
509 	 */
510 	reserve = 1;
511 	if (has_config && num_pins >= 1)
512 		reserve++;
513 	reserve *= num_pins;
514 	ret = pinctrl_utils_reserve_map(pctldev, map, reserved_maps, num_maps,
515 					reserve);
516 	if (ret < 0)
517 		return ret;
518 
519 	for (i = 0; i < num_pins; i++) {
520 		const char *group, *func;
521 
522 		ret = of_property_read_u32_index(np, "pinmux", i, &pinfunc);
523 		if (ret)
524 			return ret;
525 
526 		ret = atmel_pctl_xlate_pinfunc(pctldev, np, pinfunc, &group,
527 					       &func);
528 		if (ret)
529 			return ret;
530 
531 		pinctrl_utils_add_map_mux(pctldev, map, reserved_maps, num_maps,
532 					  group, func);
533 
534 		if (has_config) {
535 			ret = pinctrl_utils_add_map_configs(pctldev, map,
536 					reserved_maps, num_maps, group,
537 					configs, num_configs,
538 					PIN_MAP_TYPE_CONFIGS_GROUP);
539 			if (ret < 0)
540 				return ret;
541 		}
542 	}
543 
544 	return 0;
545 }
546 
atmel_pctl_dt_node_to_map(struct pinctrl_dev * pctldev,struct device_node * np_config,struct pinctrl_map ** map,unsigned * num_maps)547 static int atmel_pctl_dt_node_to_map(struct pinctrl_dev *pctldev,
548 				     struct device_node *np_config,
549 				     struct pinctrl_map **map,
550 				     unsigned *num_maps)
551 {
552 	struct device_node *np;
553 	unsigned reserved_maps;
554 	int ret;
555 
556 	*map = NULL;
557 	*num_maps = 0;
558 	reserved_maps = 0;
559 
560 	/*
561 	 * If all the pins of a device have the same configuration (or no one),
562 	 * it is useless to add a subnode, so directly parse node referenced by
563 	 * phandle.
564 	 */
565 	ret = atmel_pctl_dt_subnode_to_map(pctldev, np_config, map,
566 					   &reserved_maps, num_maps);
567 	if (ret) {
568 		for_each_child_of_node(np_config, np) {
569 			ret = atmel_pctl_dt_subnode_to_map(pctldev, np, map,
570 						    &reserved_maps, num_maps);
571 			if (ret < 0)
572 				break;
573 		}
574 	}
575 
576 	if (ret < 0) {
577 		pinctrl_utils_dt_free_map(pctldev, *map, *num_maps);
578 		dev_err(pctldev->dev, "can't create maps for node %s\n",
579 			np_config->full_name);
580 	}
581 
582 	return ret;
583 }
584 
585 static const struct pinctrl_ops atmel_pctlops = {
586 	.get_groups_count	= atmel_pctl_get_groups_count,
587 	.get_group_name		= atmel_pctl_get_group_name,
588 	.get_group_pins		= atmel_pctl_get_group_pins,
589 	.dt_node_to_map		= atmel_pctl_dt_node_to_map,
590 	.dt_free_map		= pinctrl_utils_dt_free_map,
591 };
592 
atmel_pmx_get_functions_count(struct pinctrl_dev * pctldev)593 static int atmel_pmx_get_functions_count(struct pinctrl_dev *pctldev)
594 {
595 	return ARRAY_SIZE(atmel_functions);
596 }
597 
atmel_pmx_get_function_name(struct pinctrl_dev * pctldev,unsigned selector)598 static const char *atmel_pmx_get_function_name(struct pinctrl_dev *pctldev,
599 					       unsigned selector)
600 {
601 	return atmel_functions[selector];
602 }
603 
atmel_pmx_get_function_groups(struct pinctrl_dev * pctldev,unsigned selector,const char * const ** groups,unsigned * const num_groups)604 static int atmel_pmx_get_function_groups(struct pinctrl_dev *pctldev,
605 					 unsigned selector,
606 					 const char * const **groups,
607 					 unsigned * const num_groups)
608 {
609 	struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
610 
611 	*groups = atmel_pioctrl->group_names;
612 	*num_groups = atmel_pioctrl->npins;
613 
614 	return 0;
615 }
616 
atmel_pmx_set_mux(struct pinctrl_dev * pctldev,unsigned function,unsigned group)617 static int atmel_pmx_set_mux(struct pinctrl_dev *pctldev,
618 			     unsigned function,
619 			     unsigned group)
620 {
621 	struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
622 	unsigned pin;
623 	u32 conf;
624 
625 	dev_dbg(pctldev->dev, "enable function %s group %s\n",
626 		atmel_functions[function], atmel_pioctrl->groups[group].name);
627 
628 	pin = atmel_pioctrl->groups[group].pin;
629 	conf = atmel_pin_config_read(pctldev, pin);
630 	conf &= (~ATMEL_PIO_CFGR_FUNC_MASK);
631 	conf |= (function & ATMEL_PIO_CFGR_FUNC_MASK);
632 	dev_dbg(pctldev->dev, "pin: %u, conf: 0x%08x\n", pin, conf);
633 	atmel_pin_config_write(pctldev, pin, conf);
634 
635 	return 0;
636 }
637 
638 static const struct pinmux_ops atmel_pmxops = {
639 	.get_functions_count	= atmel_pmx_get_functions_count,
640 	.get_function_name	= atmel_pmx_get_function_name,
641 	.get_function_groups	= atmel_pmx_get_function_groups,
642 	.set_mux		= atmel_pmx_set_mux,
643 };
644 
atmel_conf_pin_config_group_get(struct pinctrl_dev * pctldev,unsigned group,unsigned long * config)645 static int atmel_conf_pin_config_group_get(struct pinctrl_dev *pctldev,
646 					   unsigned group,
647 					   unsigned long *config)
648 {
649 	struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
650 	unsigned param = pinconf_to_config_param(*config), arg = 0;
651 	struct atmel_group *grp = atmel_pioctrl->groups + group;
652 	unsigned pin_id = grp->pin;
653 	u32 res;
654 
655 	res = atmel_pin_config_read(pctldev, pin_id);
656 
657 	switch (param) {
658 	case PIN_CONFIG_BIAS_PULL_UP:
659 		if (!(res & ATMEL_PIO_PUEN_MASK))
660 			return -EINVAL;
661 		arg = 1;
662 		break;
663 	case PIN_CONFIG_BIAS_PULL_DOWN:
664 		if ((res & ATMEL_PIO_PUEN_MASK) ||
665 		    (!(res & ATMEL_PIO_PDEN_MASK)))
666 			return -EINVAL;
667 		arg = 1;
668 		break;
669 	case PIN_CONFIG_BIAS_DISABLE:
670 		if ((res & ATMEL_PIO_PUEN_MASK) ||
671 		    ((res & ATMEL_PIO_PDEN_MASK)))
672 			return -EINVAL;
673 		arg = 1;
674 		break;
675 	case PIN_CONFIG_DRIVE_OPEN_DRAIN:
676 		if (!(res & ATMEL_PIO_OPD_MASK))
677 			return -EINVAL;
678 		arg = 1;
679 		break;
680 	case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
681 		if (!(res & ATMEL_PIO_SCHMITT_MASK))
682 			return -EINVAL;
683 		arg = 1;
684 		break;
685 	default:
686 		return -ENOTSUPP;
687 	}
688 
689 	*config = pinconf_to_config_packed(param, arg);
690 	return 0;
691 }
692 
atmel_conf_pin_config_group_set(struct pinctrl_dev * pctldev,unsigned group,unsigned long * configs,unsigned num_configs)693 static int atmel_conf_pin_config_group_set(struct pinctrl_dev *pctldev,
694 					   unsigned group,
695 					   unsigned long *configs,
696 					   unsigned num_configs)
697 {
698 	struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
699 	struct atmel_group *grp = atmel_pioctrl->groups + group;
700 	unsigned bank, pin, pin_id = grp->pin;
701 	u32 mask, conf = 0;
702 	int i;
703 
704 	conf = atmel_pin_config_read(pctldev, pin_id);
705 
706 	for (i = 0; i < num_configs; i++) {
707 		unsigned param = pinconf_to_config_param(configs[i]);
708 		unsigned arg = pinconf_to_config_argument(configs[i]);
709 
710 		dev_dbg(pctldev->dev, "%s: pin=%u, config=0x%lx\n",
711 			__func__, pin_id, configs[i]);
712 
713 		switch (param) {
714 		case PIN_CONFIG_BIAS_DISABLE:
715 			conf &= (~ATMEL_PIO_PUEN_MASK);
716 			conf &= (~ATMEL_PIO_PDEN_MASK);
717 			break;
718 		case PIN_CONFIG_BIAS_PULL_UP:
719 			conf |= ATMEL_PIO_PUEN_MASK;
720 			conf &= (~ATMEL_PIO_PDEN_MASK);
721 			break;
722 		case PIN_CONFIG_BIAS_PULL_DOWN:
723 			conf |= ATMEL_PIO_PDEN_MASK;
724 			conf &= (~ATMEL_PIO_PUEN_MASK);
725 			break;
726 		case PIN_CONFIG_DRIVE_OPEN_DRAIN:
727 			if (arg == 0)
728 				conf &= (~ATMEL_PIO_OPD_MASK);
729 			else
730 				conf |= ATMEL_PIO_OPD_MASK;
731 			break;
732 		case PIN_CONFIG_INPUT_SCHMITT_ENABLE:
733 			if (arg == 0)
734 				conf |= ATMEL_PIO_SCHMITT_MASK;
735 			else
736 				conf &= (~ATMEL_PIO_SCHMITT_MASK);
737 			break;
738 		case PIN_CONFIG_INPUT_DEBOUNCE:
739 			if (arg == 0) {
740 				conf &= (~ATMEL_PIO_IFEN_MASK);
741 				conf &= (~ATMEL_PIO_IFSCEN_MASK);
742 			} else {
743 				/*
744 				 * We don't care about the debounce value for several reasons:
745 				 * - can't have different debounce periods inside a same group,
746 				 * - the register to configure this period is a secure register.
747 				 * The debouncing filter can filter a pulse with a duration of less
748 				 * than 1/2 slow clock period.
749 				 */
750 				conf |= ATMEL_PIO_IFEN_MASK;
751 				conf |= ATMEL_PIO_IFSCEN_MASK;
752 			}
753 			break;
754 		case PIN_CONFIG_OUTPUT:
755 			conf |= ATMEL_PIO_DIR_MASK;
756 			bank = ATMEL_PIO_BANK(pin_id);
757 			pin = ATMEL_PIO_LINE(pin_id);
758 			mask = 1 << pin;
759 
760 			if (arg == 0) {
761 				writel_relaxed(mask, atmel_pioctrl->reg_base +
762 					bank * ATMEL_PIO_BANK_OFFSET +
763 					ATMEL_PIO_CODR);
764 			} else {
765 				writel_relaxed(mask, atmel_pioctrl->reg_base +
766 					bank * ATMEL_PIO_BANK_OFFSET +
767 					ATMEL_PIO_SODR);
768 			}
769 			break;
770 		default:
771 			dev_warn(pctldev->dev,
772 				 "unsupported configuration parameter: %u\n",
773 				 param);
774 			continue;
775 		}
776 	}
777 
778 	dev_dbg(pctldev->dev, "%s: reg=0x%08x\n", __func__, conf);
779 	atmel_pin_config_write(pctldev, pin_id, conf);
780 
781 	return 0;
782 }
783 
atmel_conf_pin_config_dbg_show(struct pinctrl_dev * pctldev,struct seq_file * s,unsigned pin_id)784 static void atmel_conf_pin_config_dbg_show(struct pinctrl_dev *pctldev,
785 					   struct seq_file *s, unsigned pin_id)
786 {
787 	struct atmel_pioctrl *atmel_pioctrl = pinctrl_dev_get_drvdata(pctldev);
788 	u32 conf;
789 
790 	if (!atmel_pioctrl->pins[pin_id]->device)
791 		return;
792 
793 	if (atmel_pioctrl->pins[pin_id])
794 		seq_printf(s, " (%s, ioset %u) ",
795 			   atmel_pioctrl->pins[pin_id]->device,
796 			   atmel_pioctrl->pins[pin_id]->ioset);
797 
798 	conf = atmel_pin_config_read(pctldev, pin_id);
799 	if (conf & ATMEL_PIO_PUEN_MASK)
800 		seq_printf(s, "%s ", "pull-up");
801 	if (conf & ATMEL_PIO_PDEN_MASK)
802 		seq_printf(s, "%s ", "pull-down");
803 	if (conf & ATMEL_PIO_IFEN_MASK)
804 		seq_printf(s, "%s ", "debounce");
805 	if (conf & ATMEL_PIO_OPD_MASK)
806 		seq_printf(s, "%s ", "open-drain");
807 	if (conf & ATMEL_PIO_SCHMITT_MASK)
808 		seq_printf(s, "%s ", "schmitt");
809 }
810 
811 static const struct pinconf_ops atmel_confops = {
812 	.pin_config_group_get	= atmel_conf_pin_config_group_get,
813 	.pin_config_group_set	= atmel_conf_pin_config_group_set,
814 	.pin_config_dbg_show	= atmel_conf_pin_config_dbg_show,
815 };
816 
817 static struct pinctrl_desc atmel_pinctrl_desc = {
818 	.name		= "atmel_pinctrl",
819 	.confops	= &atmel_confops,
820 	.pctlops	= &atmel_pctlops,
821 	.pmxops		= &atmel_pmxops,
822 };
823 
atmel_pctrl_suspend(struct device * dev)824 static int atmel_pctrl_suspend(struct device *dev)
825 {
826 	struct platform_device *pdev = to_platform_device(dev);
827 	struct atmel_pioctrl *atmel_pioctrl = platform_get_drvdata(pdev);
828 	int i;
829 
830 	/*
831 	 * For each bank, save IMR to restore it later and disable all GPIO
832 	 * interrupts excepting the ones marked as wakeup sources.
833 	 */
834 	for (i = 0; i < atmel_pioctrl->nbanks; i++) {
835 		atmel_pioctrl->pm_suspend_backup[i] =
836 			atmel_gpio_read(atmel_pioctrl, i, ATMEL_PIO_IMR);
837 		atmel_gpio_write(atmel_pioctrl, i, ATMEL_PIO_IDR,
838 				 ~atmel_pioctrl->pm_wakeup_sources[i]);
839 	}
840 
841 	return 0;
842 }
843 
atmel_pctrl_resume(struct device * dev)844 static int atmel_pctrl_resume(struct device *dev)
845 {
846 	struct platform_device *pdev = to_platform_device(dev);
847 	struct atmel_pioctrl *atmel_pioctrl = platform_get_drvdata(pdev);
848 	int i;
849 
850 	for (i = 0; i < atmel_pioctrl->nbanks; i++)
851 		atmel_gpio_write(atmel_pioctrl, i, ATMEL_PIO_IER,
852 				 atmel_pioctrl->pm_suspend_backup[i]);
853 
854 	return 0;
855 }
856 
857 static const struct dev_pm_ops atmel_pctrl_pm_ops = {
858 	SET_SYSTEM_SLEEP_PM_OPS(atmel_pctrl_suspend, atmel_pctrl_resume)
859 };
860 
861 /*
862  * The number of banks can be different from a SoC to another one.
863  * We can have up to 16 banks.
864  */
865 static const struct atmel_pioctrl_data atmel_sama5d2_pioctrl_data = {
866 	.nbanks		= 4,
867 };
868 
869 static const struct of_device_id atmel_pctrl_of_match[] = {
870 	{
871 		.compatible = "atmel,sama5d2-pinctrl",
872 		.data = &atmel_sama5d2_pioctrl_data,
873 	}, {
874 		/* sentinel */
875 	}
876 };
877 MODULE_DEVICE_TABLE(of, atmel_pctrl_of_match);
878 
atmel_pinctrl_probe(struct platform_device * pdev)879 static int atmel_pinctrl_probe(struct platform_device *pdev)
880 {
881 	struct device *dev = &pdev->dev;
882 	struct pinctrl_pin_desc	*pin_desc;
883 	const char **group_names;
884 	const struct of_device_id *match;
885 	int i, ret;
886 	struct resource	*res;
887 	struct atmel_pioctrl *atmel_pioctrl;
888 	struct atmel_pioctrl_data *atmel_pioctrl_data;
889 
890 	atmel_pioctrl = devm_kzalloc(dev, sizeof(*atmel_pioctrl), GFP_KERNEL);
891 	if (!atmel_pioctrl)
892 		return -ENOMEM;
893 	atmel_pioctrl->dev = dev;
894 	atmel_pioctrl->node = dev->of_node;
895 	platform_set_drvdata(pdev, atmel_pioctrl);
896 
897 	match = of_match_node(atmel_pctrl_of_match, dev->of_node);
898 	if (!match) {
899 		dev_err(dev, "unknown compatible string\n");
900 		return -ENODEV;
901 	}
902 	atmel_pioctrl_data = (struct atmel_pioctrl_data *)match->data;
903 	atmel_pioctrl->nbanks = atmel_pioctrl_data->nbanks;
904 	atmel_pioctrl->npins = atmel_pioctrl->nbanks * ATMEL_PIO_NPINS_PER_BANK;
905 
906 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
907 	if (!res) {
908 		dev_err(dev, "unable to get atmel pinctrl resource\n");
909 		return -EINVAL;
910 	}
911 	atmel_pioctrl->reg_base = devm_ioremap_resource(dev, res);
912 	if (IS_ERR(atmel_pioctrl->reg_base))
913 		return -EINVAL;
914 
915 	atmel_pioctrl->clk = devm_clk_get(dev, NULL);
916 	if (IS_ERR(atmel_pioctrl->clk)) {
917 		dev_err(dev, "failed to get clock\n");
918 		return PTR_ERR(atmel_pioctrl->clk);
919 	}
920 
921 	atmel_pioctrl->pins = devm_kzalloc(dev, sizeof(*atmel_pioctrl->pins)
922 			* atmel_pioctrl->npins, GFP_KERNEL);
923 	if (!atmel_pioctrl->pins)
924 		return -ENOMEM;
925 
926 	pin_desc = devm_kzalloc(dev, sizeof(*pin_desc)
927 			* atmel_pioctrl->npins, GFP_KERNEL);
928 	if (!pin_desc)
929 		return -ENOMEM;
930 	atmel_pinctrl_desc.pins = pin_desc;
931 	atmel_pinctrl_desc.npins = atmel_pioctrl->npins;
932 
933 	/* One pin is one group since a pin can achieve all functions. */
934 	group_names = devm_kzalloc(dev, sizeof(*group_names)
935 			* atmel_pioctrl->npins, GFP_KERNEL);
936 	if (!group_names)
937 		return -ENOMEM;
938 	atmel_pioctrl->group_names = group_names;
939 
940 	atmel_pioctrl->groups = devm_kzalloc(&pdev->dev,
941 			sizeof(*atmel_pioctrl->groups) * atmel_pioctrl->npins,
942 			GFP_KERNEL);
943 	if (!atmel_pioctrl->groups)
944 		return -ENOMEM;
945 	for (i = 0 ; i < atmel_pioctrl->npins; i++) {
946 		struct atmel_group *group = atmel_pioctrl->groups + i;
947 		unsigned bank = ATMEL_PIO_BANK(i);
948 		unsigned line = ATMEL_PIO_LINE(i);
949 
950 		atmel_pioctrl->pins[i] = devm_kzalloc(dev,
951 				sizeof(**atmel_pioctrl->pins), GFP_KERNEL);
952 		if (!atmel_pioctrl->pins[i])
953 			return -ENOMEM;
954 
955 		atmel_pioctrl->pins[i]->pin_id = i;
956 		atmel_pioctrl->pins[i]->bank = bank;
957 		atmel_pioctrl->pins[i]->line = line;
958 
959 		pin_desc[i].number = i;
960 		/* Pin naming convention: P(bank_name)(bank_pin_number). */
961 		pin_desc[i].name = kasprintf(GFP_KERNEL, "P%c%d",
962 					     bank + 'A', line);
963 
964 		group->name = group_names[i] = pin_desc[i].name;
965 		group->pin = pin_desc[i].number;
966 
967 		dev_dbg(dev, "pin_id=%u, bank=%u, line=%u", i, bank, line);
968 	}
969 
970 	atmel_pioctrl->gpio_chip = &atmel_gpio_chip;
971 	atmel_pioctrl->gpio_chip->of_node = dev->of_node;
972 	atmel_pioctrl->gpio_chip->ngpio = atmel_pioctrl->npins;
973 	atmel_pioctrl->gpio_chip->label = dev_name(dev);
974 	atmel_pioctrl->gpio_chip->dev = dev;
975 	atmel_pioctrl->gpio_chip->names = atmel_pioctrl->group_names;
976 
977 	atmel_pioctrl->pm_wakeup_sources = devm_kzalloc(dev,
978 			sizeof(*atmel_pioctrl->pm_wakeup_sources)
979 			* atmel_pioctrl->nbanks, GFP_KERNEL);
980 	if (!atmel_pioctrl->pm_wakeup_sources)
981 		return -ENOMEM;
982 
983 	atmel_pioctrl->pm_suspend_backup = devm_kzalloc(dev,
984 			sizeof(*atmel_pioctrl->pm_suspend_backup)
985 			* atmel_pioctrl->nbanks, GFP_KERNEL);
986 	if (!atmel_pioctrl->pm_suspend_backup)
987 		return -ENOMEM;
988 
989 	atmel_pioctrl->irqs = devm_kzalloc(dev, sizeof(*atmel_pioctrl->irqs)
990 			* atmel_pioctrl->nbanks, GFP_KERNEL);
991 	if (!atmel_pioctrl->irqs)
992 		return -ENOMEM;
993 
994 	/* There is one controller but each bank has its own irq line. */
995 	for (i = 0; i < atmel_pioctrl->nbanks; i++) {
996 		res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
997 		if (!res) {
998 			dev_err(dev, "missing irq resource for group %c\n",
999 				'A' + i);
1000 			return -EINVAL;
1001 		}
1002 		atmel_pioctrl->irqs[i] = res->start;
1003 		irq_set_chained_handler(res->start, atmel_gpio_irq_handler);
1004 		irq_set_handler_data(res->start, atmel_pioctrl);
1005 		dev_dbg(dev, "bank %i: hwirq=%u\n", i, res->start);
1006 	}
1007 
1008 	atmel_pioctrl->irq_domain = irq_domain_add_linear(dev->of_node,
1009 			atmel_pioctrl->gpio_chip->ngpio,
1010 			&irq_domain_simple_ops, NULL);
1011 	if (!atmel_pioctrl->irq_domain) {
1012 		dev_err(dev, "can't add the irq domain\n");
1013 		return -ENODEV;
1014 	}
1015 	atmel_pioctrl->irq_domain->name = "atmel gpio";
1016 
1017 	for (i = 0; i < atmel_pioctrl->npins; i++) {
1018 		int irq = irq_create_mapping(atmel_pioctrl->irq_domain, i);
1019 
1020 		irq_set_chip_and_handler(irq, &atmel_gpio_irq_chip,
1021 					 handle_simple_irq);
1022 		irq_set_chip_data(irq, atmel_pioctrl);
1023 		dev_dbg(dev,
1024 			"atmel gpio irq domain: hwirq: %d, linux irq: %d\n",
1025 			i, irq);
1026 	}
1027 
1028 	ret = clk_prepare_enable(atmel_pioctrl->clk);
1029 	if (ret) {
1030 		dev_err(dev, "failed to prepare and enable clock\n");
1031 		goto clk_prepare_enable_error;
1032 	}
1033 
1034 	atmel_pioctrl->pinctrl_dev = pinctrl_register(&atmel_pinctrl_desc,
1035 						      &pdev->dev,
1036 						      atmel_pioctrl);
1037 	if (!atmel_pioctrl->pinctrl_dev) {
1038 		dev_err(dev, "pinctrl registration failed\n");
1039 		goto pinctrl_register_error;
1040 	}
1041 
1042 	ret = gpiochip_add(atmel_pioctrl->gpio_chip);
1043 	if (ret) {
1044 		dev_err(dev, "failed to add gpiochip\n");
1045 		goto gpiochip_add_error;
1046 	}
1047 
1048 	ret = gpiochip_add_pin_range(atmel_pioctrl->gpio_chip, dev_name(dev),
1049 				     0, 0, atmel_pioctrl->gpio_chip->ngpio);
1050 	if (ret) {
1051 		dev_err(dev, "failed to add gpio pin range\n");
1052 		goto gpiochip_add_pin_range_error;
1053 	}
1054 
1055 	dev_info(&pdev->dev, "atmel pinctrl initialized\n");
1056 
1057 	return 0;
1058 
1059 clk_prepare_enable_error:
1060 	irq_domain_remove(atmel_pioctrl->irq_domain);
1061 pinctrl_register_error:
1062 	clk_disable_unprepare(atmel_pioctrl->clk);
1063 gpiochip_add_error:
1064 	pinctrl_unregister(atmel_pioctrl->pinctrl_dev);
1065 gpiochip_add_pin_range_error:
1066 	gpiochip_remove(atmel_pioctrl->gpio_chip);
1067 
1068 	return ret;
1069 }
1070 
atmel_pinctrl_remove(struct platform_device * pdev)1071 int atmel_pinctrl_remove(struct platform_device *pdev)
1072 {
1073 	struct atmel_pioctrl *atmel_pioctrl = platform_get_drvdata(pdev);
1074 
1075 	irq_domain_remove(atmel_pioctrl->irq_domain);
1076 	clk_disable_unprepare(atmel_pioctrl->clk);
1077 	pinctrl_unregister(atmel_pioctrl->pinctrl_dev);
1078 	gpiochip_remove(atmel_pioctrl->gpio_chip);
1079 
1080 	return 0;
1081 }
1082 
1083 static struct platform_driver atmel_pinctrl_driver = {
1084 	.driver = {
1085 		.name = "pinctrl-at91-pio4",
1086 		.of_match_table = atmel_pctrl_of_match,
1087 		.pm = &atmel_pctrl_pm_ops,
1088 	},
1089 	.probe = atmel_pinctrl_probe,
1090 	.remove = atmel_pinctrl_remove,
1091 };
1092 module_platform_driver(atmel_pinctrl_driver);
1093 
1094 MODULE_AUTHOR(Ludovic Desroches <ludovic.desroches@atmel.com>);
1095 MODULE_DESCRIPTION("Atmel PIO4 pinctrl driver");
1096 MODULE_LICENSE("GPL v2");
1097