blob: a74e6f4ffbd776f0840f365b094a5235c32dda68 [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)
769 writew(1 << offset, &regs->data_set);
770 else
771 writew(1 << offset, &regs->data_clear);
772
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
Axel Linfe4315c2013-09-24 15:47:41 +0800783 adi_gpio_set_value(chip, offset, value);
784
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800785 spin_lock_irqsave(&port->lock, flags);
786
787 writew(readw(&regs->inen) & ~(1 << offset), &regs->inen);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800788 writew(1 << offset, &regs->dir_set);
789
790 spin_unlock_irqrestore(&port->lock, flags);
791
792 return 0;
793}
794
795static int adi_gpio_get_value(struct gpio_chip *chip, unsigned offset)
796{
797 struct gpio_port *port = container_of(chip, struct gpio_port, chip);
798 struct gpio_port_t *regs = port->regs;
799 unsigned long flags;
800 int ret;
801
802 spin_lock_irqsave(&port->lock, flags);
803
804 ret = !!(readw(&regs->data) & BIT(offset));
805
806 spin_unlock_irqrestore(&port->lock, flags);
807
808 return ret;
809}
810
811static int adi_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
812{
813 struct gpio_port *port = container_of(chip, struct gpio_port, chip);
814
815 if (port->irq_base >= 0)
816 return irq_find_mapping(port->domain, offset);
817 else
818 return irq_create_mapping(port->domain, offset);
819}
820
821static int adi_pint_map_port(struct gpio_pint *pint, bool assign, u8 map,
822 struct irq_domain *domain)
823{
824 struct gpio_pint_regs *regs = pint->regs;
825 u32 map_mask;
826
827 if (pint->map_count > 1)
828 return -EINVAL;
829
830 pint->map_count++;
831
832 /* The map_mask of each gpio port is a 16-bit duplicate
833 * of the 8-bit map. It can be set to either high 16 bits or low
834 * 16 bits of the pint assignment register.
835 */
836 map_mask = (map << 8) | map;
837 if (assign) {
838 map_mask <<= PINT_HI_OFFSET;
839 writel((readl(&regs->assign) & 0xFFFF) | map_mask,
840 &regs->assign);
841 } else
842 writel((readl(&regs->assign) & 0xFFFF0000) | map_mask,
843 &regs->assign);
844
845 pint->domain[assign] = domain;
846
847 return 0;
848}
849
850static int adi_gpio_pint_probe(struct platform_device *pdev)
851{
852 struct device *dev = &pdev->dev;
853 struct resource *res;
854 struct gpio_pint *pint;
855
856 pint = devm_kzalloc(dev, sizeof(struct gpio_pint), GFP_KERNEL);
857 if (!pint) {
858 dev_err(dev, "Memory alloc failed\n");
859 return -ENOMEM;
860 }
861
862 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Axel Linc8690d62013-09-24 15:46:11 +0800863 pint->base = devm_ioremap_resource(dev, res);
864 if (IS_ERR(pint->base))
865 return PTR_ERR(pint->base);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800866
867 pint->regs = (struct gpio_pint_regs *)pint->base;
868
869 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
870 if (!res) {
871 dev_err(dev, "Invalid IRQ resource\n");
872 return -ENODEV;
873 }
874
875 spin_lock_init(&pint->lock);
876
877 pint->irq = res->start;
878 pint->pint_map_port = adi_pint_map_port;
879 platform_set_drvdata(pdev, pint);
880
881 irq_set_chained_handler(pint->irq, adi_gpio_handle_pint_irq);
882 irq_set_handler_data(pint->irq, pint);
883
884 list_add_tail(&pint->node, &adi_pint_list);
885
886 return 0;
887}
888
889static int adi_gpio_pint_remove(struct platform_device *pdev)
890{
891 struct gpio_pint *pint = platform_get_drvdata(pdev);
892
893 list_del(&pint->node);
894 irq_set_handler(pint->irq, handle_simple_irq);
895
896 return 0;
897}
898
899static int adi_gpio_irq_map(struct irq_domain *d, unsigned int irq,
900 irq_hw_number_t hwirq)
901{
902 struct gpio_port *port = d->host_data;
903
904 if (!port)
905 return -EINVAL;
906
907 irq_set_chip_data(irq, port);
908 irq_set_chip_and_handler(irq, &adi_gpio_irqchip,
909 handle_level_irq);
910
911 return 0;
912}
913
914const struct irq_domain_ops adi_gpio_irq_domain_ops = {
915 .map = adi_gpio_irq_map,
916 .xlate = irq_domain_xlate_onecell,
917};
918
919static int adi_gpio_init_int(struct gpio_port *port)
920{
921 struct device_node *node = port->dev->of_node;
922 struct gpio_pint *pint = port->pint;
923 int ret;
924
925 port->domain = irq_domain_add_linear(node, port->width,
926 &adi_gpio_irq_domain_ops, port);
927 if (!port->domain) {
928 dev_err(port->dev, "Failed to create irqdomain\n");
929 return -ENOSYS;
930 }
931
932 /* According to BF54x and BF60x HRM, pin interrupt devices are not
933 * part of the GPIO port device. in GPIO interrupt mode, the GPIO
934 * pins of multiple port devices can be routed into one pin interrupt
935 * device. The mapping can be configured by setting pint assignment
936 * register with the mapping value of different GPIO port. This is
937 * done via function pint_map_port().
938 */
939 ret = pint->pint_map_port(port->pint, port->pint_assign,
940 port->pint_map, port->domain);
941 if (ret)
942 return ret;
943
944 if (port->irq_base >= 0) {
945 ret = irq_create_strict_mappings(port->domain, port->irq_base,
946 0, port->width);
947 if (ret) {
948 dev_err(port->dev, "Couldn't associate to domain\n");
949 return ret;
950 }
951 }
952
953 return 0;
954}
955
956#define DEVNAME_SIZE 16
957
958static int adi_gpio_probe(struct platform_device *pdev)
959{
960 struct device *dev = &pdev->dev;
961 const struct adi_pinctrl_gpio_platform_data *pdata;
962 struct resource *res;
963 struct gpio_port *port;
964 char pinctrl_devname[DEVNAME_SIZE];
965 static int gpio;
966 int ret = 0, ret1;
967
968 pdata = dev->platform_data;
969 if (!pdata)
970 return -EINVAL;
971
972 port = devm_kzalloc(dev, sizeof(struct gpio_port), GFP_KERNEL);
973 if (!port) {
974 dev_err(dev, "Memory alloc failed\n");
975 return -ENOMEM;
976 }
977
978 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Axel Linc8690d62013-09-24 15:46:11 +0800979 port->base = devm_ioremap_resource(dev, res);
980 if (IS_ERR(port->base))
981 return PTR_ERR(port->base);
Sonic Zhange9a03ad2013-09-03 16:28:59 +0800982
983 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
984 if (!res)
985 port->irq_base = -1;
986 else
987 port->irq_base = res->start;
988
989 port->width = pdata->port_width;
990 port->dev = dev;
991 port->regs = (struct gpio_port_t *)port->base;
992 port->pint_assign = pdata->pint_assign;
993 port->pint_map = pdata->pint_map;
994
995 port->pint = find_gpio_pint(pdata->pint_id);
996 if (port->pint) {
997 ret = adi_gpio_init_int(port);
998 if (ret)
999 return ret;
1000 }
1001
1002 spin_lock_init(&port->lock);
1003
1004 platform_set_drvdata(pdev, port);
1005
1006 port->chip.label = "adi-gpio";
1007 port->chip.direction_input = adi_gpio_direction_input;
1008 port->chip.get = adi_gpio_get_value;
1009 port->chip.direction_output = adi_gpio_direction_output;
1010 port->chip.set = adi_gpio_set_value;
1011 port->chip.request = adi_gpio_request;
1012 port->chip.free = adi_gpio_free;
1013 port->chip.to_irq = adi_gpio_to_irq;
1014 if (pdata->port_gpio_base > 0)
1015 port->chip.base = pdata->port_gpio_base;
1016 else
1017 port->chip.base = gpio;
1018 port->chip.ngpio = port->width;
1019 gpio = port->chip.base + port->width;
1020
1021 ret = gpiochip_add(&port->chip);
1022 if (ret) {
1023 dev_err(&pdev->dev, "Fail to add GPIO chip.\n");
1024 goto out_remove_domain;
1025 }
1026
1027 /* Add gpio pin range */
1028 snprintf(pinctrl_devname, DEVNAME_SIZE, "pinctrl-adi2.%d",
1029 pdata->pinctrl_id);
1030 pinctrl_devname[DEVNAME_SIZE - 1] = 0;
1031 ret = gpiochip_add_pin_range(&port->chip, pinctrl_devname,
1032 0, pdata->port_pin_base, port->width);
1033 if (ret) {
1034 dev_err(&pdev->dev, "Fail to add pin range to %s.\n",
1035 pinctrl_devname);
1036 goto out_remove_gpiochip;
1037 }
1038
1039 list_add_tail(&port->node, &adi_gpio_port_list);
1040
1041 return 0;
1042
1043out_remove_gpiochip:
1044 ret1 = gpiochip_remove(&port->chip);
1045out_remove_domain:
1046 if (port->pint)
1047 irq_domain_remove(port->domain);
1048
1049 return ret;
1050}
1051
1052static int adi_gpio_remove(struct platform_device *pdev)
1053{
1054 struct gpio_port *port = platform_get_drvdata(pdev);
1055 int ret;
1056 u8 offset;
1057
1058 list_del(&port->node);
1059 gpiochip_remove_pin_ranges(&port->chip);
1060 ret = gpiochip_remove(&port->chip);
1061 if (port->pint) {
1062 for (offset = 0; offset < port->width; offset++)
1063 irq_dispose_mapping(irq_find_mapping(port->domain,
1064 offset));
1065 irq_domain_remove(port->domain);
1066 }
1067
1068 return ret;
1069}
1070
1071static int adi_pinctrl_probe(struct platform_device *pdev)
1072{
1073 struct adi_pinctrl *pinctrl;
1074
1075 pinctrl = devm_kzalloc(&pdev->dev, sizeof(*pinctrl), GFP_KERNEL);
1076 if (!pinctrl)
1077 return -ENOMEM;
1078
1079 pinctrl->dev = &pdev->dev;
1080
1081 adi_pinctrl_soc_init(&pinctrl->soc);
1082
1083 adi_pinmux_desc.pins = pinctrl->soc->pins;
1084 adi_pinmux_desc.npins = pinctrl->soc->npins;
1085
1086 /* Now register the pin controller and all pins it handles */
1087 pinctrl->pctl = pinctrl_register(&adi_pinmux_desc, &pdev->dev, pinctrl);
1088 if (!pinctrl->pctl) {
1089 dev_err(&pdev->dev, "could not register pinctrl ADI2 driver\n");
1090 return -EINVAL;
1091 }
1092
1093 platform_set_drvdata(pdev, pinctrl);
1094
1095 return 0;
1096}
1097
1098static int adi_pinctrl_remove(struct platform_device *pdev)
1099{
1100 struct adi_pinctrl *pinctrl = platform_get_drvdata(pdev);
1101
1102 pinctrl_unregister(pinctrl->pctl);
1103
1104 return 0;
1105}
1106
1107static struct platform_driver adi_pinctrl_driver = {
1108 .probe = adi_pinctrl_probe,
1109 .remove = adi_pinctrl_remove,
1110 .driver = {
1111 .name = DRIVER_NAME,
1112 },
1113};
1114
1115static struct platform_driver adi_gpio_pint_driver = {
1116 .probe = adi_gpio_pint_probe,
1117 .remove = adi_gpio_pint_remove,
1118 .driver = {
1119 .name = "adi-gpio-pint",
1120 },
1121};
1122
1123static struct platform_driver adi_gpio_driver = {
1124 .probe = adi_gpio_probe,
1125 .remove = adi_gpio_remove,
1126 .driver = {
1127 .name = "adi-gpio",
1128 },
1129};
1130
1131static int __init adi_pinctrl_setup(void)
1132{
1133 int ret;
1134
1135 ret = platform_driver_register(&adi_pinctrl_driver);
1136 if (ret)
1137 return ret;
1138
1139 ret = platform_driver_register(&adi_gpio_pint_driver);
1140 if (ret)
1141 goto pint_error;
1142
1143 ret = platform_driver_register(&adi_gpio_driver);
1144 if (ret)
1145 goto gpio_error;
1146
1147#ifdef CONFIG_PM
1148 register_syscore_ops(&gpio_pm_syscore_ops);
1149#endif
1150 return ret;
1151gpio_error:
1152 platform_driver_unregister(&adi_gpio_pint_driver);
1153pint_error:
1154 platform_driver_unregister(&adi_pinctrl_driver);
1155
1156 return ret;
1157}
1158arch_initcall(adi_pinctrl_setup);
1159
1160MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
1161MODULE_DESCRIPTION("ADI gpio2 pin control driver");
1162MODULE_LICENSE("GPL");