aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/pinctrl/pinctrl-adi2.c
diff options
context:
space:
mode:
authorSonic Zhang <sonic.zhang@analog.com>2013-09-03 04:28:59 -0400
committerLinus Walleij <linus.walleij@linaro.org>2013-09-19 08:37:27 -0400
commite9a03add0c6ed5341fc59ff9c76843c2888a33fa (patch)
tree3530e2d42d4bcd01c13cc3024cad2b4d8624ce6a /drivers/pinctrl/pinctrl-adi2.c
parent272b98c6455f00884f0350f775c5342358ebb73f (diff)
pinctrl: ADI PIN control driver for the GPIO controller on bf54x and bf60x.
The new ADI GPIO2 controller was introduced since the BF548 and BF60x processors. It differs a lot from the old one on BF5xx processors. So, create a pinctrl driver under the pinctrl framework. - Define gpio ports and pin interrupt controllers as individual platform devices. - Register a pinctrl driver for the whole GPIO ports and pin interrupt devices. - Probe pint devices before port devices. Put device instances into the global gpio and pint lists. - Define peripheral, irq and gpio reservation bit masks for each gpio port as runtime resources. - Save and restore gpio port and pint status MMRs in syscore PM functions. - Create the plug-in subdrivers to hold the pinctrl soc data for bf54x and bf60x. Add soc data into struct adi_pinctrl. Initialize the soc data in pin controller probe function. Get the pin groups and functions via the soc data reference. - Call gpiochip_add_pin_range() in gpio device probe function to register range cross reference between gpio device and pin control device. - Get range by pinctrl_find_gpio_range_from_pin(), find gpio_port object by container_of() and find adi_pinctrl by pin control device name. - Handle peripheral and gpio requests in pinctrl operation functions. - Demux gpio IRQs via the irq_domain created by each GPIO port. v2-changes: - Remove unlinke() directive. v3-changes: - Rename struct adi_pmx to adi_pinctrl. - Fix the comments of struct gpio_pint. - Remove unused pin_base in struct gpio_port. - Change pint_assign into bool type. - Add comments about the relationship between pint device and port device to the driver header. - Use BIT macro to shift bit. - Remove all bitmap reservation help functions. Inline reservation functions into the actual code. - Remove gpio and offset mutual reference help functions. - Remove all help functions to find gpio_port and adi_pinctrl structs. Get range by pinctrl_find_gpio_range_from_pin(), find gpio_port object by container_of() and find adi_pinctrl by pin control device name. - Pass bool type usage variable to port_setup help function. - Separate long bit operations into several lines and add comments. - Use debugfs to output all GPIO request information. - Avoid to set drvdata to NULL - Add explanation to function adi_gpio_init_int() - Call gpiochip_add_pin_range() in gpio device probe function to register range cross reference between gpio device and pin control device. - Remove the reference to pin control device from the gpio_port struct. Remove the reference list to gpio device from the adi_pinctrl struct. Replace the global adi_pinctrl list with adi_gpio_port_list. Walk through the gpio list to do power suspend and resume operations. - Remove the global GPIO base from struct adi_pinctrl, define pin base in the platform data for each GPIO port device. - Initialize adi_pinctrl_setup in arch_initcall(). - print the status of triggers, whether it is in GPIO mode, if it is flagged to be used as IRQ, etc in adi_pin_dbg_show(). - Create the plug-in subdrivers to hold the pinctrl soc data for bf54x and bf60x. Add soc data into struct adi_pinctrl. Initialize the soc data in pin controller probe function. Get the pin groups and functions via the soc data reference. v4-changes: - remove useless system_state checking. - replace dev_err with dev_warn in both irq and gpio pin cases. - comment on relationship between irq type and invert operation. - It is not necessary to check the reservation mode of the requested pin in IRQ chip operation. Remove the reservation map. - Use existing gpio/pinctrl subsystem debugfs files. Remove pinctrl-adi2 driver specific debugfs output. - Add linkport group and function information for bf60x. - Separate uart and ctsrts pins into 2 groups. - Separate APAPI and alternative ATAPI pins into 2 groups. Signed-off-by: Sonic Zhang <sonic.zhang@analog.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
Diffstat (limited to 'drivers/pinctrl/pinctrl-adi2.c')
-rw-r--r--drivers/pinctrl/pinctrl-adi2.c1183
1 files changed, 1183 insertions, 0 deletions
diff --git a/drivers/pinctrl/pinctrl-adi2.c b/drivers/pinctrl/pinctrl-adi2.c
new file mode 100644
index 000000000000..7a24e59b4138
--- /dev/null
+++ b/drivers/pinctrl/pinctrl-adi2.c
@@ -0,0 +1,1183 @@
1/*
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);
250 spin_lock_irqsave(&port->pint->lock, flags);
251
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
261 spin_unlock_irqrestore(&port->pint->lock, flags);
262 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);
273 spin_lock_irqsave(&port->pint->lock, flags);
274
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
285 spin_unlock_irqrestore(&port->pint->lock, flags);
286 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);
296 spin_lock_irqsave(&port->pint->lock, flags);
297
298 writel(hwirq_to_pintbit(port, d->hwirq), &regs->mask_clear);
299
300 spin_unlock_irqrestore(&port->pint->lock, flags);
301 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);
311 spin_lock_irqsave(&port->pint->lock, flags);
312
313 writel(hwirq_to_pintbit(port, d->hwirq), &regs->mask_set);
314
315 spin_unlock_irqrestore(&port->pint->lock, flags);
316 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);
323 struct gpio_pint_regs *regs = port->pint->regs;
324
325 if (!port) {
326 dev_err(port->dev, "GPIO IRQ %d :Not exist\n", d->irq);
327 return -ENODEV;
328 }
329
330 spin_lock_irqsave(&port->lock, flags);
331 spin_lock_irqsave(&port->pint->lock, flags);
332
333 port_setup(port, d->hwirq, true);
334 writew(BIT(d->hwirq), &port->regs->dir_clear);
335 writew(readw(&port->regs->inen) | BIT(d->hwirq), &port->regs->inen);
336
337 writel(hwirq_to_pintbit(port, d->hwirq), &regs->mask_set);
338
339 spin_unlock_irqrestore(&port->pint->lock, flags);
340 spin_unlock_irqrestore(&port->lock, flags);
341
342 return 0;
343}
344
345static void adi_gpio_irq_shutdown(struct irq_data *d)
346{
347 unsigned long flags;
348 struct gpio_port *port = irq_data_get_irq_chip_data(d);
349 struct gpio_pint_regs *regs = port->pint->regs;
350
351 spin_lock_irqsave(&port->lock, flags);
352 spin_lock_irqsave(&port->pint->lock, flags);
353
354 writel(hwirq_to_pintbit(port, d->hwirq), &regs->mask_clear);
355
356 spin_unlock_irqrestore(&port->pint->lock, flags);
357 spin_unlock_irqrestore(&port->lock, flags);
358}
359
360static int adi_gpio_irq_type(struct irq_data *d, unsigned int type)
361{
362 unsigned long flags;
363 struct gpio_port *port = irq_data_get_irq_chip_data(d);
364 struct gpio_pint_regs *pint_regs = port->pint->regs;
365 unsigned pintmask;
366 unsigned int irq = d->irq;
367 int ret = 0;
368 char buf[16];
369
370 if (!port) {
371 dev_err(port->dev, "GPIO IRQ %d :Not exist\n", irq);
372 return -ENODEV;
373 }
374
375 pintmask = hwirq_to_pintbit(port, d->hwirq);
376
377 spin_lock_irqsave(&port->lock, flags);
378 spin_lock_irqsave(&port->pint->lock, flags);
379
380 /* In case of interrupt autodetect, set irq type to edge sensitive. */
381 if (type == IRQ_TYPE_PROBE)
382 type = IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING;
383
384 if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING |
385 IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
386 snprintf(buf, 16, "gpio-irq%d", irq);
387 port_setup(port, d->hwirq, true);
388 } else
389 goto out;
390
391 /* The GPIO interrupt is triggered only when its input value
392 * transfer from 0 to 1. So, invert the input value if the
393 * irq type is low or falling
394 */
395 if ((type & (IRQ_TYPE_EDGE_FALLING | IRQ_TYPE_LEVEL_LOW)))
396 writel(pintmask, &pint_regs->invert_set);
397 else
398 writel(pintmask, &pint_regs->invert_clear);
399
400 /* In edge sensitive case, if the input value of the requested irq
401 * is already 1, invert it.
402 */
403 if ((type & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
404 if (gpio_get_value(port->chip.base + d->hwirq))
405 writel(pintmask, &pint_regs->invert_set);
406 else
407 writel(pintmask, &pint_regs->invert_clear);
408 }
409
410 if (type & (IRQ_TYPE_EDGE_RISING | IRQ_TYPE_EDGE_FALLING)) {
411 writel(pintmask, &pint_regs->edge_set);
412 __irq_set_handler_locked(irq, handle_edge_irq);
413 } else {
414 writel(pintmask, &pint_regs->edge_clear);
415 __irq_set_handler_locked(irq, handle_level_irq);
416 }
417
418out:
419 spin_unlock_irqrestore(&port->pint->lock, flags);
420 spin_unlock_irqrestore(&port->lock, flags);
421
422 return ret;
423}
424
425#ifdef CONFIG_PM
426static int adi_gpio_set_wake(struct irq_data *d, unsigned int state)
427{
428 struct gpio_port *port = irq_data_get_irq_chip_data(d);
429
430 if (!port || !port->pint || port->pint->irq != d->irq)
431 return -EINVAL;
432
433#ifndef SEC_GCTL
434 adi_internal_set_wake(port->pint->irq, state);
435#endif
436
437 return 0;
438}
439
440static int adi_pint_suspend(void)
441{
442 struct gpio_pint *pint;
443
444 list_for_each_entry(pint, &adi_pint_list, node) {
445 writel(0xffffffff, &pint->regs->mask_clear);
446 pint->saved_data.assign = readl(&pint->regs->assign);
447 pint->saved_data.edge_set = readl(&pint->regs->edge_set);
448 pint->saved_data.invert_set = readl(&pint->regs->invert_set);
449 }
450
451 return 0;
452}
453
454static void adi_pint_resume(void)
455{
456 struct gpio_pint *pint;
457
458 list_for_each_entry(pint, &adi_pint_list, node) {
459 writel(pint->saved_data.assign, &pint->regs->assign);
460 writel(pint->saved_data.edge_set, &pint->regs->edge_set);
461 writel(pint->saved_data.invert_set, &pint->regs->invert_set);
462 }
463}
464
465static int adi_gpio_suspend(void)
466{
467 struct gpio_port *port;
468
469 list_for_each_entry(port, &adi_gpio_port_list, node) {
470 port->saved_data.fer = readw(&port->regs->port_fer);
471 port->saved_data.mux = readl(&port->regs->port_mux);
472 port->saved_data.data = readw(&port->regs->data);
473 port->saved_data.inen = readw(&port->regs->inen);
474 port->saved_data.dir = readw(&port->regs->dir_set);
475 }
476
477 return adi_pint_suspend();
478}
479
480static void adi_gpio_resume(void)
481{
482 struct gpio_port *port;
483
484 adi_pint_resume();
485
486 list_for_each_entry(port, &adi_gpio_port_list, node) {
487 writel(port->saved_data.mux, &port->regs->port_mux);
488 writew(port->saved_data.fer, &port->regs->port_fer);
489 writew(port->saved_data.inen, &port->regs->inen);
490 writew(port->saved_data.data & port->saved_data.dir,
491 &port->regs->data_set);
492 writew(port->saved_data.dir, &port->regs->dir_set);
493 }
494
495}
496
497static struct syscore_ops gpio_pm_syscore_ops = {
498 .suspend = adi_gpio_suspend,
499 .resume = adi_gpio_resume,
500};
501#else /* CONFIG_PM */
502#define adi_gpio_set_wake NULL
503#endif /* CONFIG_PM */
504
505#ifdef CONFIG_IRQ_PREFLOW_FASTEOI
506static inline void preflow_handler(struct irq_desc *desc)
507{
508 if (desc->preflow_handler)
509 desc->preflow_handler(&desc->irq_data);
510}
511#else
512static inline void preflow_handler(struct irq_desc *desc) { }
513#endif
514
515static void adi_gpio_handle_pint_irq(unsigned int inta_irq,
516 struct irq_desc *desc)
517{
518 u32 request;
519 u32 level_mask, hwirq;
520 bool umask = false;
521 struct gpio_pint *pint = irq_desc_get_handler_data(desc);
522 struct irq_chip *chip = irq_desc_get_chip(desc);
523 struct gpio_pint_regs *regs = pint->regs;
524 struct irq_domain *domain;
525
526 preflow_handler(desc);
527 chained_irq_enter(chip, desc);
528
529 request = readl(&regs->request);
530 level_mask = readl(&regs->edge_set) & request;
531
532 hwirq = 0;
533 domain = pint->domain[0];
534 while (request) {
535 /* domain pointer need to be changed only once at IRQ 16 when
536 * we go through IRQ requests from bit 0 to bit 31.
537 */
538 if (hwirq == PINT_HI_OFFSET)
539 domain = pint->domain[1];
540
541 if (request & 1) {
542 if (level_mask & BIT(hwirq)) {
543 umask = true;
544 chained_irq_exit(chip, desc);
545 }
546 generic_handle_irq(irq_find_mapping(domain,
547 hwirq % PINT_HI_OFFSET));
548 }
549
550 hwirq++;
551 request >>= 1;
552 }
553
554 if (!umask)
555 chained_irq_exit(chip, desc);
556}
557
558static struct irq_chip adi_gpio_irqchip = {
559 .name = "GPIO",
560 .irq_ack = adi_gpio_ack_irq,
561 .irq_mask = adi_gpio_mask_irq,
562 .irq_mask_ack = adi_gpio_mask_ack_irq,
563 .irq_unmask = adi_gpio_unmask_irq,
564 .irq_disable = adi_gpio_mask_irq,
565 .irq_enable = adi_gpio_unmask_irq,
566 .irq_set_type = adi_gpio_irq_type,
567 .irq_startup = adi_gpio_irq_startup,
568 .irq_shutdown = adi_gpio_irq_shutdown,
569 .irq_set_wake = adi_gpio_set_wake,
570};
571
572static int adi_get_groups_count(struct pinctrl_dev *pctldev)
573{
574 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
575
576 return pinctrl->soc->ngroups;
577}
578
579static const char *adi_get_group_name(struct pinctrl_dev *pctldev,
580 unsigned selector)
581{
582 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
583
584 return pinctrl->soc->groups[selector].name;
585}
586
587static int adi_get_group_pins(struct pinctrl_dev *pctldev, unsigned selector,
588 const unsigned **pins,
589 unsigned *num_pins)
590{
591 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
592
593 *pins = pinctrl->soc->groups[selector].pins;
594 *num_pins = pinctrl->soc->groups[selector].num;
595 return 0;
596}
597
598static struct pinctrl_ops adi_pctrl_ops = {
599 .get_groups_count = adi_get_groups_count,
600 .get_group_name = adi_get_group_name,
601 .get_group_pins = adi_get_group_pins,
602};
603
604static int adi_pinmux_enable(struct pinctrl_dev *pctldev, unsigned selector,
605 unsigned group)
606{
607 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
608 struct gpio_port *port;
609 struct pinctrl_gpio_range *range;
610 unsigned long flags;
611 unsigned short *mux, pin;
612
613 mux = (unsigned short *)pinctrl->soc->functions[selector].mux;
614
615 while (*mux) {
616 pin = P_IDENT(*mux);
617
618 range = pinctrl_find_gpio_range_from_pin(pctldev, pin);
619 if (range == NULL) /* should not happen */
620 return -ENODEV;
621
622 port = container_of(range->gc, struct gpio_port, chip);
623
624 spin_lock_irqsave(&port->lock, flags);
625
626 portmux_setup(port, pin_to_offset(range, pin),
627 P_FUNCT2MUX(*mux));
628 port_setup(port, pin_to_offset(range, pin), false);
629 mux++;
630
631 spin_unlock_irqrestore(&port->lock, flags);
632 }
633
634 return 0;
635}
636
637static void adi_pinmux_disable(struct pinctrl_dev *pctldev, unsigned selector,
638 unsigned group)
639{
640 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
641 struct gpio_port *port;
642 struct pinctrl_gpio_range *range;
643 unsigned long flags;
644 unsigned short *mux, pin;
645
646 mux = (unsigned short *)pinctrl->soc->functions[selector].mux;
647
648 while (*mux) {
649 pin = P_IDENT(*mux);
650
651 range = pinctrl_find_gpio_range_from_pin(pctldev, pin);
652 if (range == NULL) /* should not happen */
653 return;
654
655 port = container_of(range->gc, struct gpio_port, chip);
656
657 spin_lock_irqsave(&port->lock, flags);
658
659 port_setup(port, pin_to_offset(range, pin), true);
660 mux++;
661
662 spin_unlock_irqrestore(&port->lock, flags);
663 }
664}
665
666static int adi_pinmux_get_funcs_count(struct pinctrl_dev *pctldev)
667{
668 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
669
670 return pinctrl->soc->nfunctions;
671}
672
673static const char *adi_pinmux_get_func_name(struct pinctrl_dev *pctldev,
674 unsigned selector)
675{
676 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
677
678 return pinctrl->soc->functions[selector].name;
679}
680
681static int adi_pinmux_get_groups(struct pinctrl_dev *pctldev, unsigned selector,
682 const char * const **groups,
683 unsigned * const num_groups)
684{
685 struct adi_pinctrl *pinctrl = pinctrl_dev_get_drvdata(pctldev);
686
687 *groups = pinctrl->soc->functions[selector].groups;
688 *num_groups = pinctrl->soc->functions[selector].num_groups;
689 return 0;
690}
691
692static int adi_pinmux_request_gpio(struct pinctrl_dev *pctldev,
693 struct pinctrl_gpio_range *range, unsigned pin)
694{
695 struct gpio_port *port;
696 unsigned long flags;
697 u8 offset;
698
699 port = container_of(range->gc, struct gpio_port, chip);
700 offset = pin_to_offset(range, pin);
701
702 spin_lock_irqsave(&port->lock, flags);
703
704 port_setup(port, offset, true);
705
706 spin_unlock_irqrestore(&port->lock, flags);
707
708 return 0;
709}
710
711static struct pinmux_ops adi_pinmux_ops = {
712 .enable = adi_pinmux_enable,
713 .disable = adi_pinmux_disable,
714 .get_functions_count = adi_pinmux_get_funcs_count,
715 .get_function_name = adi_pinmux_get_func_name,
716 .get_function_groups = adi_pinmux_get_groups,
717 .gpio_request_enable = adi_pinmux_request_gpio,
718};
719
720
721static struct pinctrl_desc adi_pinmux_desc = {
722 .name = DRIVER_NAME,
723 .pctlops = &adi_pctrl_ops,
724 .pmxops = &adi_pinmux_ops,
725 .owner = THIS_MODULE,
726};
727
728static int adi_gpio_request(struct gpio_chip *chip, unsigned offset)
729{
730 return pinctrl_request_gpio(chip->base + offset);
731}
732
733static void adi_gpio_free(struct gpio_chip *chip, unsigned offset)
734{
735 pinctrl_free_gpio(chip->base + offset);
736}
737
738static int adi_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
739{
740 struct gpio_port *port;
741 unsigned long flags;
742
743 port = container_of(chip, struct gpio_port, chip);
744
745 spin_lock_irqsave(&port->lock, flags);
746
747 writew(BIT(offset), &port->regs->dir_clear);
748 writew(readw(&port->regs->inen) | BIT(offset), &port->regs->inen);
749
750 spin_unlock_irqrestore(&port->lock, flags);
751
752 return 0;
753}
754
755static void adi_gpio_set_value(struct gpio_chip *chip, unsigned offset,
756 int value)
757{
758 struct gpio_port *port = container_of(chip, struct gpio_port, chip);
759 struct gpio_port_t *regs = port->regs;
760 unsigned long flags;
761
762 spin_lock_irqsave(&port->lock, flags);
763
764 if (value)
765 writew(1 << offset, &regs->data_set);
766 else
767 writew(1 << offset, &regs->data_clear);
768
769 spin_unlock_irqrestore(&port->lock, flags);
770}
771
772static int adi_gpio_direction_output(struct gpio_chip *chip, unsigned offset,
773 int value)
774{
775 struct gpio_port *port = container_of(chip, struct gpio_port, chip);
776 struct gpio_port_t *regs = port->regs;
777 unsigned long flags;
778
779 spin_lock_irqsave(&port->lock, flags);
780
781 writew(readw(&regs->inen) & ~(1 << offset), &regs->inen);
782 adi_gpio_set_value(chip, offset, value);
783 writew(1 << offset, &regs->dir_set);
784
785 spin_unlock_irqrestore(&port->lock, flags);
786
787 return 0;
788}
789
790static int adi_gpio_get_value(struct gpio_chip *chip, unsigned offset)
791{
792 struct gpio_port *port = container_of(chip, struct gpio_port, chip);
793 struct gpio_port_t *regs = port->regs;
794 unsigned long flags;
795 int ret;
796
797 spin_lock_irqsave(&port->lock, flags);
798
799 ret = !!(readw(&regs->data) & BIT(offset));
800
801 spin_unlock_irqrestore(&port->lock, flags);
802
803 return ret;
804}
805
806static int adi_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
807{
808 struct gpio_port *port = container_of(chip, struct gpio_port, chip);
809
810 if (port->irq_base >= 0)
811 return irq_find_mapping(port->domain, offset);
812 else
813 return irq_create_mapping(port->domain, offset);
814}
815
816static int adi_pint_map_port(struct gpio_pint *pint, bool assign, u8 map,
817 struct irq_domain *domain)
818{
819 struct gpio_pint_regs *regs = pint->regs;
820 u32 map_mask;
821
822 if (pint->map_count > 1)
823 return -EINVAL;
824
825 pint->map_count++;
826
827 /* The map_mask of each gpio port is a 16-bit duplicate
828 * of the 8-bit map. It can be set to either high 16 bits or low
829 * 16 bits of the pint assignment register.
830 */
831 map_mask = (map << 8) | map;
832 if (assign) {
833 map_mask <<= PINT_HI_OFFSET;
834 writel((readl(&regs->assign) & 0xFFFF) | map_mask,
835 &regs->assign);
836 } else
837 writel((readl(&regs->assign) & 0xFFFF0000) | map_mask,
838 &regs->assign);
839
840 pint->domain[assign] = domain;
841
842 return 0;
843}
844
845static int adi_gpio_pint_probe(struct platform_device *pdev)
846{
847 struct device *dev = &pdev->dev;
848 struct resource *res;
849 struct gpio_pint *pint;
850
851 pint = devm_kzalloc(dev, sizeof(struct gpio_pint), GFP_KERNEL);
852 if (!pint) {
853 dev_err(dev, "Memory alloc failed\n");
854 return -ENOMEM;
855 }
856
857 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
858 if (!res) {
859 dev_err(dev, "Invalid mem resource\n");
860 return -ENODEV;
861 }
862
863 if (!devm_request_mem_region(dev, res->start, resource_size(res),
864 pdev->name)) {
865 dev_err(dev, "Region already claimed\n");
866 return -EBUSY;
867 }
868
869 pint->base = devm_ioremap(dev, res->start, resource_size(res));
870 if (!pint->base) {
871 dev_err(dev, "Could not ioremap\n");
872 return -ENOMEM;
873 }
874
875 pint->regs = (struct gpio_pint_regs *)pint->base;
876
877 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
878 if (!res) {
879 dev_err(dev, "Invalid IRQ resource\n");
880 return -ENODEV;
881 }
882
883 spin_lock_init(&pint->lock);
884
885 pint->irq = res->start;
886 pint->pint_map_port = adi_pint_map_port;
887 platform_set_drvdata(pdev, pint);
888
889 irq_set_chained_handler(pint->irq, adi_gpio_handle_pint_irq);
890 irq_set_handler_data(pint->irq, pint);
891
892 list_add_tail(&pint->node, &adi_pint_list);
893
894 return 0;
895}
896
897static int adi_gpio_pint_remove(struct platform_device *pdev)
898{
899 struct gpio_pint *pint = platform_get_drvdata(pdev);
900
901 list_del(&pint->node);
902 irq_set_handler(pint->irq, handle_simple_irq);
903
904 return 0;
905}
906
907static int adi_gpio_irq_map(struct irq_domain *d, unsigned int irq,
908 irq_hw_number_t hwirq)
909{
910 struct gpio_port *port = d->host_data;
911
912 if (!port)
913 return -EINVAL;
914
915 irq_set_chip_data(irq, port);
916 irq_set_chip_and_handler(irq, &adi_gpio_irqchip,
917 handle_level_irq);
918
919 return 0;
920}
921
922const struct irq_domain_ops adi_gpio_irq_domain_ops = {
923 .map = adi_gpio_irq_map,
924 .xlate = irq_domain_xlate_onecell,
925};
926
927static int adi_gpio_init_int(struct gpio_port *port)
928{
929 struct device_node *node = port->dev->of_node;
930 struct gpio_pint *pint = port->pint;
931 int ret;
932
933 port->domain = irq_domain_add_linear(node, port->width,
934 &adi_gpio_irq_domain_ops, port);
935 if (!port->domain) {
936 dev_err(port->dev, "Failed to create irqdomain\n");
937 return -ENOSYS;
938 }
939
940 /* According to BF54x and BF60x HRM, pin interrupt devices are not
941 * part of the GPIO port device. in GPIO interrupt mode, the GPIO
942 * pins of multiple port devices can be routed into one pin interrupt
943 * device. The mapping can be configured by setting pint assignment
944 * register with the mapping value of different GPIO port. This is
945 * done via function pint_map_port().
946 */
947 ret = pint->pint_map_port(port->pint, port->pint_assign,
948 port->pint_map, port->domain);
949 if (ret)
950 return ret;
951
952 if (port->irq_base >= 0) {
953 ret = irq_create_strict_mappings(port->domain, port->irq_base,
954 0, port->width);
955 if (ret) {
956 dev_err(port->dev, "Couldn't associate to domain\n");
957 return ret;
958 }
959 }
960
961 return 0;
962}
963
964#define DEVNAME_SIZE 16
965
966static int adi_gpio_probe(struct platform_device *pdev)
967{
968 struct device *dev = &pdev->dev;
969 const struct adi_pinctrl_gpio_platform_data *pdata;
970 struct resource *res;
971 struct gpio_port *port;
972 char pinctrl_devname[DEVNAME_SIZE];
973 static int gpio;
974 int ret = 0, ret1;
975
976 pdata = dev->platform_data;
977 if (!pdata)
978 return -EINVAL;
979
980 port = devm_kzalloc(dev, sizeof(struct gpio_port), GFP_KERNEL);
981 if (!port) {
982 dev_err(dev, "Memory alloc failed\n");
983 return -ENOMEM;
984 }
985
986 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
987 if (!res) {
988 dev_err(dev, "Invalid mem resource\n");
989 return -ENODEV;
990 }
991
992 if (!devm_request_mem_region(dev, res->start, resource_size(res),
993 pdev->name)) {
994 dev_err(dev, "Region already claimed\n");
995 return -EBUSY;
996 }
997
998 port->base = devm_ioremap(dev, res->start, resource_size(res));
999 if (!port->base) {
1000 dev_err(dev, "Could not ioremap\n");
1001 return -ENOMEM;
1002 }
1003
1004 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1005 if (!res)
1006 port->irq_base = -1;
1007 else
1008 port->irq_base = res->start;
1009
1010 port->width = pdata->port_width;
1011 port->dev = dev;
1012 port->regs = (struct gpio_port_t *)port->base;
1013 port->pint_assign = pdata->pint_assign;
1014 port->pint_map = pdata->pint_map;
1015
1016 port->pint = find_gpio_pint(pdata->pint_id);
1017 if (port->pint) {
1018 ret = adi_gpio_init_int(port);
1019 if (ret)
1020 return ret;
1021 }
1022
1023 spin_lock_init(&port->lock);
1024
1025 platform_set_drvdata(pdev, port);
1026
1027 port->chip.label = "adi-gpio";
1028 port->chip.direction_input = adi_gpio_direction_input;
1029 port->chip.get = adi_gpio_get_value;
1030 port->chip.direction_output = adi_gpio_direction_output;
1031 port->chip.set = adi_gpio_set_value;
1032 port->chip.request = adi_gpio_request;
1033 port->chip.free = adi_gpio_free;
1034 port->chip.to_irq = adi_gpio_to_irq;
1035 if (pdata->port_gpio_base > 0)
1036 port->chip.base = pdata->port_gpio_base;
1037 else
1038 port->chip.base = gpio;
1039 port->chip.ngpio = port->width;
1040 gpio = port->chip.base + port->width;
1041
1042 ret = gpiochip_add(&port->chip);
1043 if (ret) {
1044 dev_err(&pdev->dev, "Fail to add GPIO chip.\n");
1045 goto out_remove_domain;
1046 }
1047
1048 /* Add gpio pin range */
1049 snprintf(pinctrl_devname, DEVNAME_SIZE, "pinctrl-adi2.%d",
1050 pdata->pinctrl_id);
1051 pinctrl_devname[DEVNAME_SIZE - 1] = 0;
1052 ret = gpiochip_add_pin_range(&port->chip, pinctrl_devname,
1053 0, pdata->port_pin_base, port->width);
1054 if (ret) {
1055 dev_err(&pdev->dev, "Fail to add pin range to %s.\n",
1056 pinctrl_devname);
1057 goto out_remove_gpiochip;
1058 }
1059
1060 list_add_tail(&port->node, &adi_gpio_port_list);
1061
1062 return 0;
1063
1064out_remove_gpiochip:
1065 ret1 = gpiochip_remove(&port->chip);
1066out_remove_domain:
1067 if (port->pint)
1068 irq_domain_remove(port->domain);
1069
1070 return ret;
1071}
1072
1073static int adi_gpio_remove(struct platform_device *pdev)
1074{
1075 struct gpio_port *port = platform_get_drvdata(pdev);
1076 int ret;
1077 u8 offset;
1078
1079 list_del(&port->node);
1080 gpiochip_remove_pin_ranges(&port->chip);
1081 ret = gpiochip_remove(&port->chip);
1082 if (port->pint) {
1083 for (offset = 0; offset < port->width; offset++)
1084 irq_dispose_mapping(irq_find_mapping(port->domain,
1085 offset));
1086 irq_domain_remove(port->domain);
1087 }
1088
1089 return ret;
1090}
1091
1092static int adi_pinctrl_probe(struct platform_device *pdev)
1093{
1094 struct adi_pinctrl *pinctrl;
1095
1096 pinctrl = devm_kzalloc(&pdev->dev, sizeof(*pinctrl), GFP_KERNEL);
1097 if (!pinctrl)
1098 return -ENOMEM;
1099
1100 pinctrl->dev = &pdev->dev;
1101
1102 adi_pinctrl_soc_init(&pinctrl->soc);
1103
1104 adi_pinmux_desc.pins = pinctrl->soc->pins;
1105 adi_pinmux_desc.npins = pinctrl->soc->npins;
1106
1107 /* Now register the pin controller and all pins it handles */
1108 pinctrl->pctl = pinctrl_register(&adi_pinmux_desc, &pdev->dev, pinctrl);
1109 if (!pinctrl->pctl) {
1110 dev_err(&pdev->dev, "could not register pinctrl ADI2 driver\n");
1111 return -EINVAL;
1112 }
1113
1114 platform_set_drvdata(pdev, pinctrl);
1115
1116 return 0;
1117}
1118
1119static int adi_pinctrl_remove(struct platform_device *pdev)
1120{
1121 struct adi_pinctrl *pinctrl = platform_get_drvdata(pdev);
1122
1123 pinctrl_unregister(pinctrl->pctl);
1124
1125 return 0;
1126}
1127
1128static struct platform_driver adi_pinctrl_driver = {
1129 .probe = adi_pinctrl_probe,
1130 .remove = adi_pinctrl_remove,
1131 .driver = {
1132 .name = DRIVER_NAME,
1133 },
1134};
1135
1136static struct platform_driver adi_gpio_pint_driver = {
1137 .probe = adi_gpio_pint_probe,
1138 .remove = adi_gpio_pint_remove,
1139 .driver = {
1140 .name = "adi-gpio-pint",
1141 },
1142};
1143
1144static struct platform_driver adi_gpio_driver = {
1145 .probe = adi_gpio_probe,
1146 .remove = adi_gpio_remove,
1147 .driver = {
1148 .name = "adi-gpio",
1149 },
1150};
1151
1152static int __init adi_pinctrl_setup(void)
1153{
1154 int ret;
1155
1156 ret = platform_driver_register(&adi_pinctrl_driver);
1157 if (ret)
1158 return ret;
1159
1160 ret = platform_driver_register(&adi_gpio_pint_driver);
1161 if (ret)
1162 goto pint_error;
1163
1164 ret = platform_driver_register(&adi_gpio_driver);
1165 if (ret)
1166 goto gpio_error;
1167
1168#ifdef CONFIG_PM
1169 register_syscore_ops(&gpio_pm_syscore_ops);
1170#endif
1171 return ret;
1172gpio_error:
1173 platform_driver_unregister(&adi_gpio_pint_driver);
1174pint_error:
1175 platform_driver_unregister(&adi_pinctrl_driver);
1176
1177 return ret;
1178}
1179arch_initcall(adi_pinctrl_setup);
1180
1181MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
1182MODULE_DESCRIPTION("ADI gpio2 pin control driver");
1183MODULE_LICENSE("GPL");