blob: 7a39562c3e42f4f70fff0ffe3924edd6ba4ac609 [file] [log] [blame]
Sonic Zhange9a03ad2013-09-03 16:28:59 +08001/*
2 * Pinctrl Driver for ADI GPIO2 controller
3 *
4 * Copyright 2007-2013 Analog Devices Inc.
5 *
6 * Licensed under the GPLv2 or later
7 */
8
9#include <linux/bitops.h>
10#include <linux/delay.h>
11#include <linux/module.h>
12#include <linux/err.h>
13#include <linux/debugfs.h>
14#include <linux/seq_file.h>
15#include <linux/irq.h>
16#include <linux/platform_data/pinctrl-adi2.h>
17#include <linux/irqdomain.h>
18#include <linux/irqchip/chained_irq.h>
19#include <linux/pinctrl/pinctrl.h>
20#include <linux/pinctrl/pinmux.h>
21#include <linux/pinctrl/consumer.h>
22#include <linux/pinctrl/machine.h>
23#include <linux/syscore_ops.h>
24#include <linux/gpio.h>
25#include <asm/portmux.h>
26#include "pinctrl-adi2.h"
27#include "core.h"
28
29/*
30According to the BF54x HRM, pint means "pin interrupt".
31http://www.analog.com/static/imported-files/processor_manuals/ADSP-BF54x_hwr_rev1.2.pdf
32
33ADSP-BF54x processor Blackfin processors have four SIC interrupt chan-
34nels dedicated to pin interrupt purposes. These channels are managed by
35four hardware blocks, called PINT0, PINT1, PINT2, and PINT3. Every PINTx
36block can sense to up to 32 pins. While PINT0 and PINT1 can sense the
37pins of port A and port B, PINT2 and PINT3 manage all the pins from port
38C to port J as shown in Figure 9-2.
39
40n BF54x HRM:
41The ten GPIO ports are subdivided into 8-bit half ports, resulting in lower and
42upper half 8-bit units. The PINTx_ASSIGN registers control the 8-bit multi-
43plexers shown in Figure 9-3. Lower half units of eight pins can be
44forwarded to either byte 0 or byte 2 of either associated PINTx block.
45Upper half units can be forwarded to either byte 1 or byte 3 of the pin
46interrupt blocks, without further restrictions.
47
48All MMR registers in the pin interrupt module are 32 bits wide. To simply the
49mapping logic, this driver only maps a 16-bit gpio port to the upper or lower
5016 bits of a PINTx block. You can find the Figure 9-3 on page 583.
51
52Each IRQ domain is binding to a GPIO bank device. 2 GPIO bank devices can map
53to one PINT device. Two in "struct gpio_pint" are used to ease the PINT
54interrupt handler.
55
56The GPIO bank mapping to the lower 16 bits of the PINT device set its IRQ
57domain pointer in domain[0]. The IRQ domain pointer of the other bank is set
58to domain[1]. PINT interrupt handler adi_gpio_handle_pint_irq() finds out
59the current domain pointer according to whether the interrupt request mask
60is in lower 16 bits (domain[0]) or upper 16bits (domain[1]).
61
62A PINT device is not part of a GPIO port device in Blackfin. Multiple GPIO
63port devices can be mapped to the same PINT device.
64
65*/
66
67static LIST_HEAD(adi_pint_list);
68static LIST_HEAD(adi_gpio_port_list);
69
70#define DRIVER_NAME "pinctrl-adi2"
71
72#define PINT_HI_OFFSET 16
73
74/**
75 * struct gpio_port_saved - GPIO port registers that should be saved between
76 * power suspend and resume operations.
77 *
78 * @fer: PORTx_FER register
79 * @data: PORTx_DATA register
80 * @dir: PORTx_DIR register
81 * @inen: PORTx_INEN register
82 * @mux: PORTx_MUX register
83 */
84struct gpio_port_saved {
85 u16 fer;
86 u16 data;
87 u16 dir;
88 u16 inen;
89 u32 mux;
90};
91
92/**
93 * struct gpio_pint - Pin interrupt controller device. Multiple ADI GPIO
94 * banks can be mapped into one Pin interrupt controller.
95 *
96 * @node: All gpio_pint instances are added to a global list.
97 * @base: PINT device register base address
98 * @irq: IRQ of the PINT device, it is the parent IRQ of all
99 * GPIO IRQs mapping to this device.
100 * @domain: [0] irq domain of the gpio port, whose hardware interrupts are
101 * mapping to the low 16-bit of the pint registers.
102 * [1] irq domain of the gpio port, whose hardware interrupts are
103 * mapping to the high 16-bit of the pint registers.
104 * @regs: address pointer to the PINT device
105 * @map_count: No more than 2 GPIO banks can be mapped to this PINT device.
106 * @lock: This lock make sure the irq_chip operations to one PINT device
107 * for different GPIO interrrupts are atomic.
108 * @pint_map_port: Set up the mapping between one PINT device and
109 * multiple GPIO banks.
110 */
111struct gpio_pint {
112 struct list_head node;
113 void __iomem *base;
114 int irq;
115 struct irq_domain *domain[2];
116 struct gpio_pint_regs *regs;
117 struct adi_pm_pint_save saved_data;
118 int map_count;
119 spinlock_t lock;
120
121 int (*pint_map_port)(struct gpio_pint *pint, bool assign,
122 u8 map, struct irq_domain *domain);
123};
124
125/**
126 * ADI pin controller
127 *
128 * @dev: a pointer back to containing device
129 * @pctl: the pinctrl device
130 * @soc: SoC data for this specific chip
131 */
132struct adi_pinctrl {
133 struct device *dev;
134 struct pinctrl_dev *pctl;
135 const struct adi_pinctrl_soc_data *soc;
136};
137
138/**
139 * struct gpio_port - GPIO bank device. Multiple ADI GPIO banks can be mapped
140 * into one pin interrupt controller.
141 *
142 * @node: All gpio_port instances are added to a list.
143 * @base: GPIO bank device register base address
144 * @irq_base: base IRQ of the GPIO bank device
145 * @width: PIN number of the GPIO bank device
146 * @regs: address pointer to the GPIO bank device
147 * @saved_data: registers that should be saved between PM operations.
148 * @dev: device structure of this GPIO bank
149 * @pint: GPIO PINT device that this GPIO bank mapped to
150 * @pint_map: GIOP bank mapping code in PINT device
151 * @pint_assign: The 32-bit PINT registers can be divided into 2 parts. A
152 * GPIO bank can be mapped into either low 16 bits[0] or high 16
153 * bits[1] of each PINT register.
154 * @lock: This lock make sure the irq_chip operations to one PINT device
155 * for different GPIO interrrupts are atomic.
156 * @chip: abstract a GPIO controller
157 * @domain: The irq domain owned by the GPIO port.
158 * @rsvmap: Reservation map array for each pin in the GPIO bank
159 */
160struct gpio_port {
161 struct list_head node;
162 void __iomem *base;
163 unsigned int irq_base;
164 unsigned int width;
165 struct gpio_port_t *regs;
166 struct gpio_port_saved saved_data;
167 struct device *dev;
168
169 struct gpio_pint *pint;
170 u8 pint_map;
171 bool pint_assign;
172
173 spinlock_t lock;
174 struct gpio_chip chip;
175 struct irq_domain *domain;
176};
177
178static inline u8 pin_to_offset(struct pinctrl_gpio_range *range, unsigned pin)
179{
180 return pin - range->pin_base;
181}
182
183static inline u32 hwirq_to_pintbit(struct gpio_port *port, int hwirq)
184{
185 return port->pint_assign ? BIT(hwirq) << PINT_HI_OFFSET : BIT(hwirq);
186}
187
188static struct gpio_pint *find_gpio_pint(unsigned id)
189{
190 struct gpio_pint *pint;
191 int i = 0;
192
193 list_for_each_entry(pint, &adi_pint_list, node) {
194 if (id == i)
195 return pint;
196 i++;
197 }
198
199 return NULL;
200}
201
202static inline void port_setup(struct gpio_port *port, unsigned offset,
203 bool use_for_gpio)
204{
205 struct gpio_port_t *regs = port->regs;
206
207 if (use_for_gpio)
208 writew(readw(&regs->port_fer) & ~BIT(offset),
209 &regs->port_fer);
210 else
211 writew(readw(&regs->port_fer) | BIT(offset), &regs->port_fer);
212}
213
214static inline void portmux_setup(struct gpio_port *port, unsigned offset,
215 unsigned short function)
216{
217 struct gpio_port_t *regs = port->regs;
218 u32 pmux;
219
220 pmux = readl(&regs->port_mux);
221
222 /* The function field of each pin has 2 consecutive bits in
223 * the mux register.
224 */
225 pmux &= ~(0x3 << (2 * offset));
226 pmux |= (function & 0x3) << (2 * offset);
227
228 writel(pmux, &regs->port_mux);
229}
230
231static inline u16 get_portmux(struct gpio_port *port, unsigned offset)
232{
233 struct gpio_port_t *regs = port->regs;
234 u32 pmux = readl(&regs->port_mux);
235
236 /* The function field of each pin has 2 consecutive bits in
237 * the mux register.
238 */
239 return pmux >> (2 * offset) & 0x3;
240}
241
242static void adi_gpio_ack_irq(struct irq_data *d)
243{
244 unsigned long flags;
245 struct gpio_port *port = irq_data_get_irq_chip_data(d);
246 struct gpio_pint_regs *regs = port->pint->regs;
247 unsigned pintbit = hwirq_to_pintbit(port, d->hwirq);
248
249 spin_lock_irqsave(&port->lock, flags);
Sonic Zhang010c51e2013-10-08 15:30:21 +0800250 spin_lock(&port->pint->lock);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800251
252 if (irqd_get_trigger_type(d) == IRQ_TYPE_EDGE_BOTH) {
253 if (readl(&regs->invert_set) & pintbit)
254 writel(pintbit, &regs->invert_clear);
255 else
256 writel(pintbit, &regs->invert_set);
257 }
258
259 writel(pintbit, &regs->request);
260
Sonic Zhang010c51e2013-10-08 15:30:21 +0800261 spin_unlock(&port->pint->lock);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800262 spin_unlock_irqrestore(&port->lock, flags);
263}
264
265static void adi_gpio_mask_ack_irq(struct irq_data *d)
266{
267 unsigned long flags;
268 struct gpio_port *port = irq_data_get_irq_chip_data(d);
269 struct gpio_pint_regs *regs = port->pint->regs;
270 unsigned pintbit = hwirq_to_pintbit(port, d->hwirq);
271
272 spin_lock_irqsave(&port->lock, flags);
Sonic Zhang010c51e2013-10-08 15:30:21 +0800273 spin_lock(&port->pint->lock);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800274
275 if (irqd_get_trigger_type(d) == IRQ_TYPE_EDGE_BOTH) {
276 if (readl(&regs->invert_set) & pintbit)
277 writel(pintbit, &regs->invert_clear);
278 else
279 writel(pintbit, &regs->invert_set);
280 }
281
282 writel(pintbit, &regs->request);
283 writel(pintbit, &regs->mask_clear);
284
Sonic Zhang010c51e2013-10-08 15:30:21 +0800285 spin_unlock(&port->pint->lock);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800286 spin_unlock_irqrestore(&port->lock, flags);
287}
288
289static void adi_gpio_mask_irq(struct irq_data *d)
290{
291 unsigned long flags;
292 struct gpio_port *port = irq_data_get_irq_chip_data(d);
293 struct gpio_pint_regs *regs = port->pint->regs;
294
295 spin_lock_irqsave(&port->lock, flags);
Sonic Zhang010c51e2013-10-08 15:30:21 +0800296 spin_lock(&port->pint->lock);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800297
298 writel(hwirq_to_pintbit(port, d->hwirq), &regs->mask_clear);
299
Sonic Zhang010c51e2013-10-08 15:30:21 +0800300 spin_unlock(&port->pint->lock);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800301 spin_unlock_irqrestore(&port->lock, flags);
302}
303
304static void adi_gpio_unmask_irq(struct irq_data *d)
305{
306 unsigned long flags;
307 struct gpio_port *port = irq_data_get_irq_chip_data(d);
308 struct gpio_pint_regs *regs = port->pint->regs;
309
310 spin_lock_irqsave(&port->lock, flags);
Sonic Zhang010c51e2013-10-08 15:30:21 +0800311 spin_lock(&port->pint->lock);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800312
313 writel(hwirq_to_pintbit(port, d->hwirq), &regs->mask_set);
314
Sonic Zhang010c51e2013-10-08 15:30:21 +0800315 spin_unlock(&port->pint->lock);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800316 spin_unlock_irqrestore(&port->lock, flags);
317}
318
319static unsigned int adi_gpio_irq_startup(struct irq_data *d)
320{
321 unsigned long flags;
322 struct gpio_port *port = irq_data_get_irq_chip_data(d);
Sonic Zhang010c51e2013-10-08 15:30:21 +0800323 struct gpio_pint_regs *regs;
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800324
325 if (!port) {
Sonic Zhang010c51e2013-10-08 15:30:21 +0800326 pr_err("GPIO IRQ %d :Not exist\n", d->irq);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800327 return -ENODEV;
328 }
329
Sonic Zhang010c51e2013-10-08 15:30:21 +0800330 regs = port->pint->regs;
331
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800332 spin_lock_irqsave(&port->lock, flags);
Sonic Zhang010c51e2013-10-08 15:30:21 +0800333 spin_lock(&port->pint->lock);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800334
335 port_setup(port, d->hwirq, true);
336 writew(BIT(d->hwirq), &port->regs->dir_clear);
337 writew(readw(&port->regs->inen) | BIT(d->hwirq), &port->regs->inen);
338
339 writel(hwirq_to_pintbit(port, d->hwirq), &regs->mask_set);
340
Sonic Zhang010c51e2013-10-08 15:30:21 +0800341 spin_unlock(&port->pint->lock);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800342 spin_unlock_irqrestore(&port->lock, flags);
343
344 return 0;
345}
346
347static void adi_gpio_irq_shutdown(struct irq_data *d)
348{
349 unsigned long flags;
350 struct gpio_port *port = irq_data_get_irq_chip_data(d);
351 struct gpio_pint_regs *regs = port->pint->regs;
352
353 spin_lock_irqsave(&port->lock, flags);
Sonic Zhang010c51e2013-10-08 15:30:21 +0800354 spin_lock(&port->pint->lock);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800355
356 writel(hwirq_to_pintbit(port, d->hwirq), &regs->mask_clear);
357
Sonic Zhang010c51e2013-10-08 15:30:21 +0800358 spin_unlock(&port->pint->lock);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800359 spin_unlock_irqrestore(&port->lock, flags);
360}
361
362static int adi_gpio_irq_type(struct irq_data *d, unsigned int type)
363{
364 unsigned long flags;
365 struct gpio_port *port = irq_data_get_irq_chip_data(d);
Sonic Zhang010c51e2013-10-08 15:30:21 +0800366 struct gpio_pint_regs *pint_regs;
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800367 unsigned pintmask;
368 unsigned int irq = d->irq;
369 int ret = 0;
370 char buf[16];
371
372 if (!port) {
Sonic Zhang010c51e2013-10-08 15:30:21 +0800373 pr_err("GPIO IRQ %d :Not exist\n", d->irq);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800374 return -ENODEV;
375 }
376
Sonic Zhang010c51e2013-10-08 15:30:21 +0800377 pint_regs = port->pint->regs;
378
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800379 pintmask = hwirq_to_pintbit(port, d->hwirq);
380
381 spin_lock_irqsave(&port->lock, flags);
Sonic Zhang010c51e2013-10-08 15:30:21 +0800382 spin_lock(&port->pint->lock);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800383
384 /* In case of interrupt autodetect, set irq type to edge sensitive. */
385 if (type == IRQ_TYPE_PROBE)
386 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
387
388 if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING |
389 IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
390 snprintf(buf, 16, "gpio-irq%d", irq);
391 port_setup(port, d->hwirq, true);
392 } else
393 goto out;
394
395 /* The GPIO interrupt is triggered only when its input value
396 * transfer from 0 to 1. So, invert the input value if the
397 * irq type is low or falling
398 */
399 if ((type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_LEVEL_LOW)))
400 writel(pintmask, &pint_regs->invert_set);
401 else
402 writel(pintmask, &pint_regs->invert_clear);
403
404 /* In edge sensitive case, if the input value of the requested irq
405 * is already 1, invert it.
406 */
407 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
408 if (gpio_get_value(port->chip.base + d->hwirq))
409 writel(pintmask, &pint_regs->invert_set);
410 else
411 writel(pintmask, &pint_regs->invert_clear);
412 }
413
414 if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
415 writel(pintmask, &pint_regs->edge_set);
416 __irq_set_handler_locked(irq, handle_edge_irq);
417 } else {
418 writel(pintmask, &pint_regs->edge_clear);
419 __irq_set_handler_locked(irq, handle_level_irq);
420 }
421
422out:
Sonic Zhang010c51e2013-10-08 15:30:21 +0800423 spin_unlock(&port->pint->lock);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800424 spin_unlock_irqrestore(&port->lock, flags);
425
426 return ret;
427}
428
429#ifdef CONFIG_PM
430static int adi_gpio_set_wake(struct irq_data *d, unsigned int state)
431{
432 struct gpio_port *port = irq_data_get_irq_chip_data(d);
433
434 if (!port || !port->pint || port->pint->irq != d->irq)
435 return -EINVAL;
436
437#ifndef SEC_GCTL
438 adi_internal_set_wake(port->pint->irq, state);
439#endif
440
441 return 0;
442}
443
444static int adi_pint_suspend(void)
445{
446 struct gpio_pint *pint;
447
448 list_for_each_entry(pint, &adi_pint_list, node) {
449 writel(0xffffffff, &pint->regs->mask_clear);
450 pint->saved_data.assign = readl(&pint->regs->assign);
451 pint->saved_data.edge_set = readl(&pint->regs->edge_set);
452 pint->saved_data.invert_set = readl(&pint->regs->invert_set);
453 }
454
455 return 0;
456}
457
458static void adi_pint_resume(void)
459{
460 struct gpio_pint *pint;
461
462 list_for_each_entry(pint, &adi_pint_list, node) {
463 writel(pint->saved_data.assign, &pint->regs->assign);
464 writel(pint->saved_data.edge_set, &pint->regs->edge_set);
465 writel(pint->saved_data.invert_set, &pint->regs->invert_set);
466 }
467}
468
469static int adi_gpio_suspend(void)
470{
471 struct gpio_port *port;
472
473 list_for_each_entry(port, &adi_gpio_port_list, node) {
474 port->saved_data.fer = readw(&port->regs->port_fer);
475 port->saved_data.mux = readl(&port->regs->port_mux);
476 port->saved_data.data = readw(&port->regs->data);
477 port->saved_data.inen = readw(&port->regs->inen);
478 port->saved_data.dir = readw(&port->regs->dir_set);
479 }
480
481 return adi_pint_suspend();
482}
483
484static void adi_gpio_resume(void)
485{
486 struct gpio_port *port;
487
488 adi_pint_resume();
489
490 list_for_each_entry(port, &adi_gpio_port_list, node) {
491 writel(port->saved_data.mux, &port->regs->port_mux);
492 writew(port->saved_data.fer, &port->regs->port_fer);
493 writew(port->saved_data.inen, &port->regs->inen);
494 writew(port->saved_data.data & port->saved_data.dir,
495 &port->regs->data_set);
496 writew(port->saved_data.dir, &port->regs->dir_set);
497 }
498
499}
500
501static struct syscore_ops gpio_pm_syscore_ops = {
502 .suspend = adi_gpio_suspend,
503 .resume = adi_gpio_resume,
504};
505#else /* CONFIG_PM */
506#define adi_gpio_set_wake NULL
507#endif /* CONFIG_PM */
508
509#ifdef CONFIG_IRQ_PREFLOW_FASTEOI
510static inline void preflow_handler(struct irq_desc *desc)
511{
512 if (desc->preflow_handler)
513 desc->preflow_handler(&desc->irq_data);
514}
515#else
516static inline void preflow_handler(struct irq_desc *desc) { }
517#endif
518
519static void adi_gpio_handle_pint_irq(unsigned int inta_irq,
520 struct irq_desc *desc)
521{
522 u32 request;
523 u32 level_mask, hwirq;
524 bool umask = false;
525 struct gpio_pint *pint = irq_desc_get_handler_data(desc);
526 struct irq_chip *chip = irq_desc_get_chip(desc);
527 struct gpio_pint_regs *regs = pint->regs;
528 struct irq_domain *domain;
529
530 preflow_handler(desc);
531 chained_irq_enter(chip, desc);
532
533 request = readl(&regs->request);
534 level_mask = readl(&regs->edge_set) & request;
535
536 hwirq = 0;
537 domain = pint->domain[0];
538 while (request) {
539 /* domain pointer need to be changed only once at IRQ 16 when
540 * we go through IRQ requests from bit 0 to bit 31.
541 */
542 if (hwirq == PINT_HI_OFFSET)
543 domain = pint->domain[1];
544
545 if (request & 1) {
546 if (level_mask & BIT(hwirq)) {
547 umask = true;
548 chained_irq_exit(chip, desc);
549 }
550 generic_handle_irq(irq_find_mapping(domain,
551 hwirq % PINT_HI_OFFSET));
552 }
553
554 hwirq++;
555 request >>= 1;
556 }
557
558 if (!umask)
559 chained_irq_exit(chip, desc);
560}
561
562static struct irq_chip adi_gpio_irqchip = {
563 .name = "GPIO",
564 .irq_ack = adi_gpio_ack_irq,
565 .irq_mask = adi_gpio_mask_irq,
566 .irq_mask_ack = adi_gpio_mask_ack_irq,
567 .irq_unmask = adi_gpio_unmask_irq,
568 .irq_disable = adi_gpio_mask_irq,
569 .irq_enable = adi_gpio_unmask_irq,
570 .irq_set_type = adi_gpio_irq_type,
571 .irq_startup = adi_gpio_irq_startup,
572 .irq_shutdown = adi_gpio_irq_shutdown,
573 .irq_set_wake = adi_gpio_set_wake,
574};
575
576static int adi_get_groups_count(struct pinctrl_dev *pctldev)
577{
578 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
579
580 return pinctrl->soc->ngroups;
581}
582
583static const char *adi_get_group_name(struct pinctrl_dev *pctldev,
584 unsigned selector)
585{
586 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
587
588 return pinctrl->soc->groups[selector].name;
589}
590
591static int adi_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
592 const unsigned **pins,
593 unsigned *num_pins)
594{
595 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
596
597 *pins = pinctrl->soc->groups[selector].pins;
598 *num_pins = pinctrl->soc->groups[selector].num;
599 return 0;
600}
601
602static struct pinctrl_ops adi_pctrl_ops = {
603 .get_groups_count = adi_get_groups_count,
604 .get_group_name = adi_get_group_name,
605 .get_group_pins = adi_get_group_pins,
606};
607
608static int adi_pinmux_enable(struct pinctrl_dev *pctldev, unsigned selector,
609 unsigned group)
610{
611 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
612 struct gpio_port *port;
613 struct pinctrl_gpio_range *range;
614 unsigned long flags;
615 unsigned short *mux, pin;
616
617 mux = (unsigned short *)pinctrl->soc->functions[selector].mux;
618
619 while (*mux) {
620 pin = P_IDENT(*mux);
621
622 range = pinctrl_find_gpio_range_from_pin(pctldev, pin);
623 if (range == NULL) /* should not happen */
624 return -ENODEV;
625
626 port = container_of(range->gc, struct gpio_port, chip);
627
628 spin_lock_irqsave(&port->lock, flags);
629
630 portmux_setup(port, pin_to_offset(range, pin),
631 P_FUNCT2MUX(*mux));
632 port_setup(port, pin_to_offset(range, pin), false);
633 mux++;
634
635 spin_unlock_irqrestore(&port->lock, flags);
636 }
637
638 return 0;
639}
640
641static void adi_pinmux_disable(struct pinctrl_dev *pctldev, unsigned selector,
642 unsigned group)
643{
644 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
645 struct gpio_port *port;
646 struct pinctrl_gpio_range *range;
647 unsigned long flags;
648 unsigned short *mux, pin;
649
650 mux = (unsigned short *)pinctrl->soc->functions[selector].mux;
651
652 while (*mux) {
653 pin = P_IDENT(*mux);
654
655 range = pinctrl_find_gpio_range_from_pin(pctldev, pin);
656 if (range == NULL) /* should not happen */
657 return;
658
659 port = container_of(range->gc, struct gpio_port, chip);
660
661 spin_lock_irqsave(&port->lock, flags);
662
663 port_setup(port, pin_to_offset(range, pin), true);
664 mux++;
665
666 spin_unlock_irqrestore(&port->lock, flags);
667 }
668}
669
670static int adi_pinmux_get_funcs_count(struct pinctrl_dev *pctldev)
671{
672 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
673
674 return pinctrl->soc->nfunctions;
675}
676
677static const char *adi_pinmux_get_func_name(struct pinctrl_dev *pctldev,
678 unsigned selector)
679{
680 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
681
682 return pinctrl->soc->functions[selector].name;
683}
684
685static int adi_pinmux_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
686 const char * const **groups,
687 unsigned * const num_groups)
688{
689 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
690
691 *groups = pinctrl->soc->functions[selector].groups;
692 *num_groups = pinctrl->soc->functions[selector].num_groups;
693 return 0;
694}
695
696static int adi_pinmux_request_gpio(struct pinctrl_dev *pctldev,
697 struct pinctrl_gpio_range *range, unsigned pin)
698{
699 struct gpio_port *port;
700 unsigned long flags;
701 u8 offset;
702
703 port = container_of(range->gc, struct gpio_port, chip);
704 offset = pin_to_offset(range, pin);
705
706 spin_lock_irqsave(&port->lock, flags);
707
708 port_setup(port, offset, true);
709
710 spin_unlock_irqrestore(&port->lock, flags);
711
712 return 0;
713}
714
715static struct pinmux_ops adi_pinmux_ops = {
716 .enable = adi_pinmux_enable,
717 .disable = adi_pinmux_disable,
718 .get_functions_count = adi_pinmux_get_funcs_count,
719 .get_function_name = adi_pinmux_get_func_name,
720 .get_function_groups = adi_pinmux_get_groups,
721 .gpio_request_enable = adi_pinmux_request_gpio,
722};
723
724
725static struct pinctrl_desc adi_pinmux_desc = {
726 .name = DRIVER_NAME,
727 .pctlops = &adi_pctrl_ops,
728 .pmxops = &adi_pinmux_ops,
729 .owner = THIS_MODULE,
730};
731
732static int adi_gpio_request(struct gpio_chip *chip, unsigned offset)
733{
734 return pinctrl_request_gpio(chip->base + offset);
735}
736
737static void adi_gpio_free(struct gpio_chip *chip, unsigned offset)
738{
739 pinctrl_free_gpio(chip->base + offset);
740}
741
742static int adi_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
743{
744 struct gpio_port *port;
745 unsigned long flags;
746
747 port = container_of(chip, struct gpio_port, chip);
748
749 spin_lock_irqsave(&port->lock, flags);
750
751 writew(BIT(offset), &port->regs->dir_clear);
752 writew(readw(&port->regs->inen) | BIT(offset), &port->regs->inen);
753
754 spin_unlock_irqrestore(&port->lock, flags);
755
756 return 0;
757}
758
759static void adi_gpio_set_value(struct gpio_chip *chip, unsigned offset,
760 int value)
761{
762 struct gpio_port *port = container_of(chip, struct gpio_port, chip);
763 struct gpio_port_t *regs = port->regs;
764 unsigned long flags;
765
766 spin_lock_irqsave(&port->lock, flags);
767
768 if (value)
Sonic Zhangd3224ed2013-10-08 15:31:21 +0800769 writew(BIT(offset), &regs->data_set);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800770 else
Sonic Zhangd3224ed2013-10-08 15:31:21 +0800771 writew(BIT(offset), &regs->data_clear);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800772
773 spin_unlock_irqrestore(&port->lock, flags);
774}
775
776static int adi_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
777 int value)
778{
779 struct gpio_port *port = container_of(chip, struct gpio_port, chip);
780 struct gpio_port_t *regs = port->regs;
781 unsigned long flags;
782
783 spin_lock_irqsave(&port->lock, flags);
784
Sonic Zhangd3224ed2013-10-08 15:31:21 +0800785 writew(readw(&regs->inen) & ~BIT(offset), &regs->inen);
786 if (value)
787 writew(BIT(offset), &regs->data_set);
788 else
789 writew(BIT(offset), &regs->data_clear);
790 writew(BIT(offset), &regs->dir_set);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800791
792 spin_unlock_irqrestore(&port->lock, flags);
793
794 return 0;
795}
796
797static int adi_gpio_get_value(struct gpio_chip *chip, unsigned offset)
798{
799 struct gpio_port *port = container_of(chip, struct gpio_port, chip);
800 struct gpio_port_t *regs = port->regs;
801 unsigned long flags;
802 int ret;
803
804 spin_lock_irqsave(&port->lock, flags);
805
806 ret = !!(readw(&regs->data) & BIT(offset));
807
808 spin_unlock_irqrestore(&port->lock, flags);
809
810 return ret;
811}
812
813static int adi_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
814{
815 struct gpio_port *port = container_of(chip, struct gpio_port, chip);
816
817 if (port->irq_base >= 0)
818 return irq_find_mapping(port->domain, offset);
819 else
820 return irq_create_mapping(port->domain, offset);
821}
822
823static int adi_pint_map_port(struct gpio_pint *pint, bool assign, u8 map,
824 struct irq_domain *domain)
825{
826 struct gpio_pint_regs *regs = pint->regs;
827 u32 map_mask;
828
829 if (pint->map_count > 1)
830 return -EINVAL;
831
832 pint->map_count++;
833
834 /* The map_mask of each gpio port is a 16-bit duplicate
835 * of the 8-bit map. It can be set to either high 16 bits or low
836 * 16 bits of the pint assignment register.
837 */
838 map_mask = (map << 8) | map;
839 if (assign) {
840 map_mask <<= PINT_HI_OFFSET;
841 writel((readl(&regs->assign) & 0xFFFF) | map_mask,
842 &regs->assign);
843 } else
844 writel((readl(&regs->assign) & 0xFFFF0000) | map_mask,
845 &regs->assign);
846
847 pint->domain[assign] = domain;
848
849 return 0;
850}
851
852static int adi_gpio_pint_probe(struct platform_device *pdev)
853{
854 struct device *dev = &pdev->dev;
855 struct resource *res;
856 struct gpio_pint *pint;
857
858 pint = devm_kzalloc(dev, sizeof(struct gpio_pint), GFP_KERNEL);
859 if (!pint) {
860 dev_err(dev, "Memory alloc failed\n");
861 return -ENOMEM;
862 }
863
864 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Axel Linc8690d62013-09-24 15:46:11 +0800865 pint->base = devm_ioremap_resource(dev, res);
866 if (IS_ERR(pint->base))
867 return PTR_ERR(pint->base);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800868
869 pint->regs = (struct gpio_pint_regs *)pint->base;
870
871 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
872 if (!res) {
873 dev_err(dev, "Invalid IRQ resource\n");
874 return -ENODEV;
875 }
876
877 spin_lock_init(&pint->lock);
878
879 pint->irq = res->start;
880 pint->pint_map_port = adi_pint_map_port;
881 platform_set_drvdata(pdev, pint);
882
883 irq_set_chained_handler(pint->irq, adi_gpio_handle_pint_irq);
884 irq_set_handler_data(pint->irq, pint);
885
886 list_add_tail(&pint->node, &adi_pint_list);
887
888 return 0;
889}
890
891static int adi_gpio_pint_remove(struct platform_device *pdev)
892{
893 struct gpio_pint *pint = platform_get_drvdata(pdev);
894
895 list_del(&pint->node);
896 irq_set_handler(pint->irq, handle_simple_irq);
897
898 return 0;
899}
900
901static int adi_gpio_irq_map(struct irq_domain *d, unsigned int irq,
902 irq_hw_number_t hwirq)
903{
904 struct gpio_port *port = d->host_data;
905
906 if (!port)
907 return -EINVAL;
908
909 irq_set_chip_data(irq, port);
910 irq_set_chip_and_handler(irq, &adi_gpio_irqchip,
911 handle_level_irq);
912
913 return 0;
914}
915
916const struct irq_domain_ops adi_gpio_irq_domain_ops = {
917 .map = adi_gpio_irq_map,
918 .xlate = irq_domain_xlate_onecell,
919};
920
921static int adi_gpio_init_int(struct gpio_port *port)
922{
923 struct device_node *node = port->dev->of_node;
924 struct gpio_pint *pint = port->pint;
925 int ret;
926
927 port->domain = irq_domain_add_linear(node, port->width,
928 &adi_gpio_irq_domain_ops, port);
929 if (!port->domain) {
930 dev_err(port->dev, "Failed to create irqdomain\n");
931 return -ENOSYS;
932 }
933
934 /* According to BF54x and BF60x HRM, pin interrupt devices are not
935 * part of the GPIO port device. in GPIO interrupt mode, the GPIO
936 * pins of multiple port devices can be routed into one pin interrupt
937 * device. The mapping can be configured by setting pint assignment
938 * register with the mapping value of different GPIO port. This is
939 * done via function pint_map_port().
940 */
941 ret = pint->pint_map_port(port->pint, port->pint_assign,
942 port->pint_map, port->domain);
943 if (ret)
944 return ret;
945
946 if (port->irq_base >= 0) {
947 ret = irq_create_strict_mappings(port->domain, port->irq_base,
948 0, port->width);
949 if (ret) {
950 dev_err(port->dev, "Couldn't associate to domain\n");
951 return ret;
952 }
953 }
954
955 return 0;
956}
957
958#define DEVNAME_SIZE 16
959
960static int adi_gpio_probe(struct platform_device *pdev)
961{
962 struct device *dev = &pdev->dev;
963 const struct adi_pinctrl_gpio_platform_data *pdata;
964 struct resource *res;
965 struct gpio_port *port;
966 char pinctrl_devname[DEVNAME_SIZE];
967 static int gpio;
968 int ret = 0, ret1;
969
970 pdata = dev->platform_data;
971 if (!pdata)
972 return -EINVAL;
973
974 port = devm_kzalloc(dev, sizeof(struct gpio_port), GFP_KERNEL);
975 if (!port) {
976 dev_err(dev, "Memory alloc failed\n");
977 return -ENOMEM;
978 }
979
980 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Axel Linc8690d62013-09-24 15:46:11 +0800981 port->base = devm_ioremap_resource(dev, res);
982 if (IS_ERR(port->base))
983 return PTR_ERR(port->base);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800984
985 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
986 if (!res)
987 port->irq_base = -1;
988 else
989 port->irq_base = res->start;
990
991 port->width = pdata->port_width;
992 port->dev = dev;
993 port->regs = (struct gpio_port_t *)port->base;
994 port->pint_assign = pdata->pint_assign;
995 port->pint_map = pdata->pint_map;
996
997 port->pint = find_gpio_pint(pdata->pint_id);
998 if (port->pint) {
999 ret = adi_gpio_init_int(port);
1000 if (ret)
1001 return ret;
1002 }
1003
1004 spin_lock_init(&port->lock);
1005
1006 platform_set_drvdata(pdev, port);
1007
1008 port->chip.label = "adi-gpio";
1009 port->chip.direction_input = adi_gpio_direction_input;
1010 port->chip.get = adi_gpio_get_value;
1011 port->chip.direction_output = adi_gpio_direction_output;
1012 port->chip.set = adi_gpio_set_value;
1013 port->chip.request = adi_gpio_request;
1014 port->chip.free = adi_gpio_free;
1015 port->chip.to_irq = adi_gpio_to_irq;
1016 if (pdata->port_gpio_base > 0)
1017 port->chip.base = pdata->port_gpio_base;
1018 else
1019 port->chip.base = gpio;
1020 port->chip.ngpio = port->width;
1021 gpio = port->chip.base + port->width;
1022
1023 ret = gpiochip_add(&port->chip);
1024 if (ret) {
1025 dev_err(&pdev->dev, "Fail to add GPIO chip.\n");
1026 goto out_remove_domain;
1027 }
1028
1029 /* Add gpio pin range */
1030 snprintf(pinctrl_devname, DEVNAME_SIZE, "pinctrl-adi2.%d",
1031 pdata->pinctrl_id);
1032 pinctrl_devname[DEVNAME_SIZE - 1] = 0;
1033 ret = gpiochip_add_pin_range(&port->chip, pinctrl_devname,
1034 0, pdata->port_pin_base, port->width);
1035 if (ret) {
1036 dev_err(&pdev->dev, "Fail to add pin range to %s.\n",
1037 pinctrl_devname);
1038 goto out_remove_gpiochip;
1039 }
1040
1041 list_add_tail(&port->node, &adi_gpio_port_list);
1042
1043 return 0;
1044
1045out_remove_gpiochip:
1046 ret1 = gpiochip_remove(&port->chip);
1047out_remove_domain:
1048 if (port->pint)
1049 irq_domain_remove(port->domain);
1050
1051 return ret;
1052}
1053
1054static int adi_gpio_remove(struct platform_device *pdev)
1055{
1056 struct gpio_port *port = platform_get_drvdata(pdev);
1057 int ret;
1058 u8 offset;
1059
1060 list_del(&port->node);
1061 gpiochip_remove_pin_ranges(&port->chip);
1062 ret = gpiochip_remove(&port->chip);
1063 if (port->pint) {
1064 for (offset = 0; offset < port->width; offset++)
1065 irq_dispose_mapping(irq_find_mapping(port->domain,
1066 offset));
1067 irq_domain_remove(port->domain);
1068 }
1069
1070 return ret;
1071}
1072
1073static int adi_pinctrl_probe(struct platform_device *pdev)
1074{
1075 struct adi_pinctrl *pinctrl;
1076
1077 pinctrl = devm_kzalloc(&pdev->dev, sizeof(*pinctrl), GFP_KERNEL);
1078 if (!pinctrl)
1079 return -ENOMEM;
1080
1081 pinctrl->dev = &pdev->dev;
1082
1083 adi_pinctrl_soc_init(&pinctrl->soc);
1084
1085 adi_pinmux_desc.pins = pinctrl->soc->pins;
1086 adi_pinmux_desc.npins = pinctrl->soc->npins;
1087
1088 /* Now register the pin controller and all pins it handles */
1089 pinctrl->pctl = pinctrl_register(&adi_pinmux_desc, &pdev->dev, pinctrl);
1090 if (!pinctrl->pctl) {
1091 dev_err(&pdev->dev, "could not register pinctrl ADI2 driver\n");
1092 return -EINVAL;
1093 }
1094
1095 platform_set_drvdata(pdev, pinctrl);
1096
1097 return 0;
1098}
1099
1100static int adi_pinctrl_remove(struct platform_device *pdev)
1101{
1102 struct adi_pinctrl *pinctrl = platform_get_drvdata(pdev);
1103
1104 pinctrl_unregister(pinctrl->pctl);
1105
1106 return 0;
1107}
1108
1109static struct platform_driver adi_pinctrl_driver = {
1110 .probe = adi_pinctrl_probe,
1111 .remove = adi_pinctrl_remove,
1112 .driver = {
1113 .name = DRIVER_NAME,
1114 },
1115};
1116
1117static struct platform_driver adi_gpio_pint_driver = {
1118 .probe = adi_gpio_pint_probe,
1119 .remove = adi_gpio_pint_remove,
1120 .driver = {
1121 .name = "adi-gpio-pint",
1122 },
1123};
1124
1125static struct platform_driver adi_gpio_driver = {
1126 .probe = adi_gpio_probe,
1127 .remove = adi_gpio_remove,
1128 .driver = {
1129 .name = "adi-gpio",
1130 },
1131};
1132
1133static int __init adi_pinctrl_setup(void)
1134{
1135 int ret;
1136
1137 ret = platform_driver_register(&adi_pinctrl_driver);
1138 if (ret)
1139 return ret;
1140
1141 ret = platform_driver_register(&adi_gpio_pint_driver);
1142 if (ret)
1143 goto pint_error;
1144
1145 ret = platform_driver_register(&adi_gpio_driver);
1146 if (ret)
1147 goto gpio_error;
1148
1149#ifdef CONFIG_PM
1150 register_syscore_ops(&gpio_pm_syscore_ops);
1151#endif
1152 return ret;
1153gpio_error:
1154 platform_driver_unregister(&adi_gpio_pint_driver);
1155pint_error:
1156 platform_driver_unregister(&adi_pinctrl_driver);
1157
1158 return ret;
1159}
1160arch_initcall(adi_pinctrl_setup);
1161
1162MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
1163MODULE_DESCRIPTION("ADI gpio2 pin control driver");
1164MODULE_LICENSE("GPL");