2 * S3C24XX specific support for Samsung pinctrl/gpiolib driver.
4 * Copyright (c) 2013 Heiko Stuebner <heiko@sntech.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This file contains the SamsungS3C24XX specific information required by the
12 * Samsung pinctrl/gpiolib driver. It also includes the implementation of
13 * external gpio and wakeup interrupt support.
16 #include <linux/init.h>
17 #include <linux/device.h>
18 #include <linux/interrupt.h>
19 #include <linux/irqdomain.h>
20 #include <linux/irq.h>
21 #include <linux/of_irq.h>
22 #include <linux/irqchip/chained_irq.h>
24 #include <linux/slab.h>
25 #include <linux/err.h>
27 #include "pinctrl-samsung.h"
30 #define NUM_EINT_IRQ 6
31 #define EINT_MAX_PER_GROUP 8
33 #define EINTPEND_REG 0xa8
34 #define EINTMASK_REG 0xa4
36 #define EINT_GROUP(i) ((int)((i) / EINT_MAX_PER_GROUP))
37 #define EINT_REG(i) ((EINT_GROUP(i) * 4) + 0x88)
38 #define EINT_OFFS(i) ((i) % EINT_MAX_PER_GROUP * 4)
40 #define EINT_LEVEL_LOW 0
41 #define EINT_LEVEL_HIGH 1
42 #define EINT_EDGE_FALLING 2
43 #define EINT_EDGE_RISING 4
44 #define EINT_EDGE_BOTH 6
47 static const struct samsung_pin_bank_type bank_type_1bit
= {
48 .fld_width
= { 1, 1, },
49 .reg_offset
= { 0x00, 0x04, },
52 static const struct samsung_pin_bank_type bank_type_2bit
= {
53 .fld_width
= { 2, 1, 2, },
54 .reg_offset
= { 0x00, 0x04, 0x08, },
57 #define PIN_BANK_A(pins, reg, id) \
59 .type = &bank_type_1bit, \
62 .eint_type = EINT_TYPE_NONE, \
66 #define PIN_BANK_2BIT(pins, reg, id) \
68 .type = &bank_type_2bit, \
71 .eint_type = EINT_TYPE_NONE, \
75 #define PIN_BANK_2BIT_EINTW(pins, reg, id, eoffs, emask)\
77 .type = &bank_type_2bit, \
80 .eint_type = EINT_TYPE_WKUP, \
83 .eint_offset = eoffs, \
88 * struct s3c24xx_eint_data: EINT common data
89 * @drvdata: pin controller driver data
90 * @domains: IRQ domains of particular EINT interrupts
91 * @parents: mapped parent irqs in the main interrupt controller
93 struct s3c24xx_eint_data
{
94 struct samsung_pinctrl_drv_data
*drvdata
;
95 struct irq_domain
*domains
[NUM_EINT
];
96 int parents
[NUM_EINT_IRQ
];
100 * struct s3c24xx_eint_domain_data: per irq-domain data
101 * @bank: pin bank related to the domain
102 * @eint_data: common data
103 * eint0_3_parent_only: live eints 0-3 only in the main intc
105 struct s3c24xx_eint_domain_data
{
106 struct samsung_pin_bank
*bank
;
107 struct s3c24xx_eint_data
*eint_data
;
108 bool eint0_3_parent_only
;
111 static int s3c24xx_eint_get_trigger(unsigned int type
)
114 case IRQ_TYPE_EDGE_RISING
:
115 return EINT_EDGE_RISING
;
117 case IRQ_TYPE_EDGE_FALLING
:
118 return EINT_EDGE_FALLING
;
120 case IRQ_TYPE_EDGE_BOTH
:
121 return EINT_EDGE_BOTH
;
123 case IRQ_TYPE_LEVEL_HIGH
:
124 return EINT_LEVEL_HIGH
;
126 case IRQ_TYPE_LEVEL_LOW
:
127 return EINT_LEVEL_LOW
;
134 static void s3c24xx_eint_set_handler(struct irq_data
*d
, unsigned int type
)
136 /* Edge- and level-triggered interrupts need different handlers */
137 if (type
& IRQ_TYPE_EDGE_BOTH
)
138 irq_set_handler_locked(d
, handle_edge_irq
);
140 irq_set_handler_locked(d
, handle_level_irq
);
143 static void s3c24xx_eint_set_function(struct samsung_pinctrl_drv_data
*d
,
144 struct samsung_pin_bank
*bank
, int pin
)
146 const struct samsung_pin_bank_type
*bank_type
= bank
->type
;
153 /* Make sure that pin is configured as interrupt */
154 reg
= bank
->pctl_base
+ bank
->pctl_offset
;
155 shift
= pin
* bank_type
->fld_width
[PINCFG_TYPE_FUNC
];
156 mask
= (1 << bank_type
->fld_width
[PINCFG_TYPE_FUNC
]) - 1;
158 spin_lock_irqsave(&bank
->slock
, flags
);
161 val
&= ~(mask
<< shift
);
162 val
|= bank
->eint_func
<< shift
;
165 spin_unlock_irqrestore(&bank
->slock
, flags
);
168 static int s3c24xx_eint_type(struct irq_data
*data
, unsigned int type
)
170 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(data
);
171 struct samsung_pinctrl_drv_data
*d
= bank
->drvdata
;
172 int index
= bank
->eint_offset
+ data
->hwirq
;
178 trigger
= s3c24xx_eint_get_trigger(type
);
180 dev_err(d
->dev
, "unsupported external interrupt type\n");
184 s3c24xx_eint_set_handler(data
, type
);
186 /* Set up interrupt trigger */
187 reg
= bank
->eint_base
+ EINT_REG(index
);
188 shift
= EINT_OFFS(index
);
191 val
&= ~(EINT_MASK
<< shift
);
192 val
|= trigger
<< shift
;
195 s3c24xx_eint_set_function(d
, bank
, data
->hwirq
);
200 /* Handling of EINTs 0-3 on all except S3C2412 and S3C2413 */
202 static void s3c2410_eint0_3_ack(struct irq_data
*data
)
204 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(data
);
205 struct s3c24xx_eint_domain_data
*ddata
= bank
->irq_domain
->host_data
;
206 struct s3c24xx_eint_data
*eint_data
= ddata
->eint_data
;
207 int parent_irq
= eint_data
->parents
[data
->hwirq
];
208 struct irq_chip
*parent_chip
= irq_get_chip(parent_irq
);
210 parent_chip
->irq_ack(irq_get_irq_data(parent_irq
));
213 static void s3c2410_eint0_3_mask(struct irq_data
*data
)
215 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(data
);
216 struct s3c24xx_eint_domain_data
*ddata
= bank
->irq_domain
->host_data
;
217 struct s3c24xx_eint_data
*eint_data
= ddata
->eint_data
;
218 int parent_irq
= eint_data
->parents
[data
->hwirq
];
219 struct irq_chip
*parent_chip
= irq_get_chip(parent_irq
);
221 parent_chip
->irq_mask(irq_get_irq_data(parent_irq
));
224 static void s3c2410_eint0_3_unmask(struct irq_data
*data
)
226 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(data
);
227 struct s3c24xx_eint_domain_data
*ddata
= bank
->irq_domain
->host_data
;
228 struct s3c24xx_eint_data
*eint_data
= ddata
->eint_data
;
229 int parent_irq
= eint_data
->parents
[data
->hwirq
];
230 struct irq_chip
*parent_chip
= irq_get_chip(parent_irq
);
232 parent_chip
->irq_unmask(irq_get_irq_data(parent_irq
));
235 static struct irq_chip s3c2410_eint0_3_chip
= {
236 .name
= "s3c2410-eint0_3",
237 .irq_ack
= s3c2410_eint0_3_ack
,
238 .irq_mask
= s3c2410_eint0_3_mask
,
239 .irq_unmask
= s3c2410_eint0_3_unmask
,
240 .irq_set_type
= s3c24xx_eint_type
,
243 static void s3c2410_demux_eint0_3(struct irq_desc
*desc
)
245 struct irq_data
*data
= irq_desc_get_irq_data(desc
);
246 struct s3c24xx_eint_data
*eint_data
= irq_desc_get_handler_data(desc
);
249 /* the first 4 eints have a simple 1 to 1 mapping */
250 virq
= irq_linear_revmap(eint_data
->domains
[data
->hwirq
], data
->hwirq
);
251 /* Something must be really wrong if an unmapped EINT is unmasked */
254 generic_handle_irq(virq
);
257 /* Handling of EINTs 0-3 on S3C2412 and S3C2413 */
259 static void s3c2412_eint0_3_ack(struct irq_data
*data
)
261 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(data
);
263 unsigned long bitval
= 1UL << data
->hwirq
;
264 writel(bitval
, bank
->eint_base
+ EINTPEND_REG
);
267 static void s3c2412_eint0_3_mask(struct irq_data
*data
)
269 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(data
);
272 mask
= readl(bank
->eint_base
+ EINTMASK_REG
);
273 mask
|= (1UL << data
->hwirq
);
274 writel(mask
, bank
->eint_base
+ EINTMASK_REG
);
277 static void s3c2412_eint0_3_unmask(struct irq_data
*data
)
279 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(data
);
282 mask
= readl(bank
->eint_base
+ EINTMASK_REG
);
283 mask
&= ~(1UL << data
->hwirq
);
284 writel(mask
, bank
->eint_base
+ EINTMASK_REG
);
287 static struct irq_chip s3c2412_eint0_3_chip
= {
288 .name
= "s3c2412-eint0_3",
289 .irq_ack
= s3c2412_eint0_3_ack
,
290 .irq_mask
= s3c2412_eint0_3_mask
,
291 .irq_unmask
= s3c2412_eint0_3_unmask
,
292 .irq_set_type
= s3c24xx_eint_type
,
295 static void s3c2412_demux_eint0_3(struct irq_desc
*desc
)
297 struct s3c24xx_eint_data
*eint_data
= irq_desc_get_handler_data(desc
);
298 struct irq_data
*data
= irq_desc_get_irq_data(desc
);
299 struct irq_chip
*chip
= irq_data_get_irq_chip(data
);
302 chained_irq_enter(chip
, desc
);
304 /* the first 4 eints have a simple 1 to 1 mapping */
305 virq
= irq_linear_revmap(eint_data
->domains
[data
->hwirq
], data
->hwirq
);
306 /* Something must be really wrong if an unmapped EINT is unmasked */
309 generic_handle_irq(virq
);
311 chained_irq_exit(chip
, desc
);
314 /* Handling of all other eints */
316 static void s3c24xx_eint_ack(struct irq_data
*data
)
318 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(data
);
319 unsigned char index
= bank
->eint_offset
+ data
->hwirq
;
321 writel(1UL << index
, bank
->eint_base
+ EINTPEND_REG
);
324 static void s3c24xx_eint_mask(struct irq_data
*data
)
326 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(data
);
327 unsigned char index
= bank
->eint_offset
+ data
->hwirq
;
330 mask
= readl(bank
->eint_base
+ EINTMASK_REG
);
331 mask
|= (1UL << index
);
332 writel(mask
, bank
->eint_base
+ EINTMASK_REG
);
335 static void s3c24xx_eint_unmask(struct irq_data
*data
)
337 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(data
);
338 unsigned char index
= bank
->eint_offset
+ data
->hwirq
;
341 mask
= readl(bank
->eint_base
+ EINTMASK_REG
);
342 mask
&= ~(1UL << index
);
343 writel(mask
, bank
->eint_base
+ EINTMASK_REG
);
346 static struct irq_chip s3c24xx_eint_chip
= {
348 .irq_ack
= s3c24xx_eint_ack
,
349 .irq_mask
= s3c24xx_eint_mask
,
350 .irq_unmask
= s3c24xx_eint_unmask
,
351 .irq_set_type
= s3c24xx_eint_type
,
354 static inline void s3c24xx_demux_eint(struct irq_desc
*desc
,
355 u32 offset
, u32 range
)
357 struct s3c24xx_eint_data
*data
= irq_desc_get_handler_data(desc
);
358 struct irq_chip
*chip
= irq_desc_get_chip(desc
);
359 struct irq_data
*irqd
= irq_desc_get_irq_data(desc
);
360 struct samsung_pin_bank
*bank
= irq_data_get_irq_chip_data(irqd
);
361 unsigned int pend
, mask
;
363 chained_irq_enter(chip
, desc
);
365 pend
= readl(bank
->eint_base
+ EINTPEND_REG
);
366 mask
= readl(bank
->eint_base
+ EINTMASK_REG
);
372 unsigned int virq
, irq
;
376 virq
= irq_linear_revmap(data
->domains
[irq
], irq
- offset
);
377 /* Something is really wrong if an unmapped EINT is unmasked */
380 generic_handle_irq(virq
);
383 chained_irq_exit(chip
, desc
);
386 static void s3c24xx_demux_eint4_7(struct irq_desc
*desc
)
388 s3c24xx_demux_eint(desc
, 0, 0xf0);
391 static void s3c24xx_demux_eint8_23(struct irq_desc
*desc
)
393 s3c24xx_demux_eint(desc
, 8, 0xffff00);
396 static irq_flow_handler_t s3c2410_eint_handlers
[NUM_EINT_IRQ
] = {
397 s3c2410_demux_eint0_3
,
398 s3c2410_demux_eint0_3
,
399 s3c2410_demux_eint0_3
,
400 s3c2410_demux_eint0_3
,
401 s3c24xx_demux_eint4_7
,
402 s3c24xx_demux_eint8_23
,
405 static irq_flow_handler_t s3c2412_eint_handlers
[NUM_EINT_IRQ
] = {
406 s3c2412_demux_eint0_3
,
407 s3c2412_demux_eint0_3
,
408 s3c2412_demux_eint0_3
,
409 s3c2412_demux_eint0_3
,
410 s3c24xx_demux_eint4_7
,
411 s3c24xx_demux_eint8_23
,
414 static int s3c24xx_gpf_irq_map(struct irq_domain
*h
, unsigned int virq
,
417 struct s3c24xx_eint_domain_data
*ddata
= h
->host_data
;
418 struct samsung_pin_bank
*bank
= ddata
->bank
;
420 if (!(bank
->eint_mask
& (1 << (bank
->eint_offset
+ hw
))))
424 if (ddata
->eint0_3_parent_only
)
425 irq_set_chip_and_handler(virq
, &s3c2410_eint0_3_chip
,
428 irq_set_chip_and_handler(virq
, &s3c2412_eint0_3_chip
,
431 irq_set_chip_and_handler(virq
, &s3c24xx_eint_chip
,
434 irq_set_chip_data(virq
, bank
);
438 static const struct irq_domain_ops s3c24xx_gpf_irq_ops
= {
439 .map
= s3c24xx_gpf_irq_map
,
440 .xlate
= irq_domain_xlate_twocell
,
443 static int s3c24xx_gpg_irq_map(struct irq_domain
*h
, unsigned int virq
,
446 struct s3c24xx_eint_domain_data
*ddata
= h
->host_data
;
447 struct samsung_pin_bank
*bank
= ddata
->bank
;
449 if (!(bank
->eint_mask
& (1 << (bank
->eint_offset
+ hw
))))
452 irq_set_chip_and_handler(virq
, &s3c24xx_eint_chip
, handle_edge_irq
);
453 irq_set_chip_data(virq
, bank
);
457 static const struct irq_domain_ops s3c24xx_gpg_irq_ops
= {
458 .map
= s3c24xx_gpg_irq_map
,
459 .xlate
= irq_domain_xlate_twocell
,
462 static const struct of_device_id s3c24xx_eint_irq_ids
[] = {
463 { .compatible
= "samsung,s3c2410-wakeup-eint", .data
= (void *)1 },
464 { .compatible
= "samsung,s3c2412-wakeup-eint", .data
= (void *)0 },
468 static int s3c24xx_eint_init(struct samsung_pinctrl_drv_data
*d
)
470 struct device
*dev
= d
->dev
;
471 const struct of_device_id
*match
;
472 struct device_node
*eint_np
= NULL
;
473 struct device_node
*np
;
474 struct samsung_pin_bank
*bank
;
475 struct s3c24xx_eint_data
*eint_data
;
476 const struct irq_domain_ops
*ops
;
478 bool eint0_3_parent_only
;
479 irq_flow_handler_t
*handlers
;
481 for_each_child_of_node(dev
->of_node
, np
) {
482 match
= of_match_node(s3c24xx_eint_irq_ids
, np
);
485 eint0_3_parent_only
= (bool)match
->data
;
492 eint_data
= devm_kzalloc(dev
, sizeof(*eint_data
), GFP_KERNEL
);
496 eint_data
->drvdata
= d
;
498 handlers
= eint0_3_parent_only
? s3c2410_eint_handlers
499 : s3c2412_eint_handlers
;
500 for (i
= 0; i
< NUM_EINT_IRQ
; ++i
) {
503 irq
= irq_of_parse_and_map(eint_np
, i
);
505 dev_err(dev
, "failed to get wakeup EINT IRQ %d\n", i
);
509 eint_data
->parents
[i
] = irq
;
510 irq_set_chained_handler_and_data(irq
, handlers
[i
], eint_data
);
514 for (i
= 0; i
< d
->nr_banks
; ++i
, ++bank
) {
515 struct s3c24xx_eint_domain_data
*ddata
;
520 if (bank
->eint_type
!= EINT_TYPE_WKUP
)
523 ddata
= devm_kzalloc(dev
, sizeof(*ddata
), GFP_KERNEL
);
528 ddata
->eint_data
= eint_data
;
529 ddata
->eint0_3_parent_only
= eint0_3_parent_only
;
531 ops
= (bank
->eint_offset
== 0) ? &s3c24xx_gpf_irq_ops
532 : &s3c24xx_gpg_irq_ops
;
534 bank
->irq_domain
= irq_domain_add_linear(bank
->of_node
,
535 bank
->nr_pins
, ops
, ddata
);
536 if (!bank
->irq_domain
) {
537 dev_err(dev
, "wkup irq domain add failed\n");
541 irq
= bank
->eint_offset
;
542 mask
= bank
->eint_mask
;
543 for (pin
= 0; mask
; ++pin
, mask
>>= 1) {
548 eint_data
->domains
[irq
] = bank
->irq_domain
;
556 static const struct samsung_pin_bank_data s3c2412_pin_banks
[] __initconst
= {
557 PIN_BANK_A(23, 0x000, "gpa"),
558 PIN_BANK_2BIT(11, 0x010, "gpb"),
559 PIN_BANK_2BIT(16, 0x020, "gpc"),
560 PIN_BANK_2BIT(16, 0x030, "gpd"),
561 PIN_BANK_2BIT(16, 0x040, "gpe"),
562 PIN_BANK_2BIT_EINTW(8, 0x050, "gpf", 0, 0xff),
563 PIN_BANK_2BIT_EINTW(16, 0x060, "gpg", 8, 0xffff00),
564 PIN_BANK_2BIT(11, 0x070, "gph"),
565 PIN_BANK_2BIT(13, 0x080, "gpj"),
568 const struct samsung_pin_ctrl s3c2412_pin_ctrl
[] __initconst
= {
570 .pin_banks
= s3c2412_pin_banks
,
571 .nr_banks
= ARRAY_SIZE(s3c2412_pin_banks
),
572 .eint_wkup_init
= s3c24xx_eint_init
,
576 static const struct samsung_pin_bank_data s3c2416_pin_banks
[] __initconst
= {
577 PIN_BANK_A(27, 0x000, "gpa"),
578 PIN_BANK_2BIT(11, 0x010, "gpb"),
579 PIN_BANK_2BIT(16, 0x020, "gpc"),
580 PIN_BANK_2BIT(16, 0x030, "gpd"),
581 PIN_BANK_2BIT(16, 0x040, "gpe"),
582 PIN_BANK_2BIT_EINTW(8, 0x050, "gpf", 0, 0xff),
583 PIN_BANK_2BIT_EINTW(8, 0x060, "gpg", 8, 0xff00),
584 PIN_BANK_2BIT(15, 0x070, "gph"),
585 PIN_BANK_2BIT(16, 0x0e0, "gpk"),
586 PIN_BANK_2BIT(14, 0x0f0, "gpl"),
587 PIN_BANK_2BIT(2, 0x100, "gpm"),
590 const struct samsung_pin_ctrl s3c2416_pin_ctrl
[] __initconst
= {
592 .pin_banks
= s3c2416_pin_banks
,
593 .nr_banks
= ARRAY_SIZE(s3c2416_pin_banks
),
594 .eint_wkup_init
= s3c24xx_eint_init
,
598 static const struct samsung_pin_bank_data s3c2440_pin_banks
[] __initconst
= {
599 PIN_BANK_A(25, 0x000, "gpa"),
600 PIN_BANK_2BIT(11, 0x010, "gpb"),
601 PIN_BANK_2BIT(16, 0x020, "gpc"),
602 PIN_BANK_2BIT(16, 0x030, "gpd"),
603 PIN_BANK_2BIT(16, 0x040, "gpe"),
604 PIN_BANK_2BIT_EINTW(8, 0x050, "gpf", 0, 0xff),
605 PIN_BANK_2BIT_EINTW(16, 0x060, "gpg", 8, 0xffff00),
606 PIN_BANK_2BIT(11, 0x070, "gph"),
607 PIN_BANK_2BIT(13, 0x0d0, "gpj"),
610 const struct samsung_pin_ctrl s3c2440_pin_ctrl
[] __initconst
= {
612 .pin_banks
= s3c2440_pin_banks
,
613 .nr_banks
= ARRAY_SIZE(s3c2440_pin_banks
),
614 .eint_wkup_init
= s3c24xx_eint_init
,
618 static const struct samsung_pin_bank_data s3c2450_pin_banks
[] __initconst
= {
619 PIN_BANK_A(28, 0x000, "gpa"),
620 PIN_BANK_2BIT(11, 0x010, "gpb"),
621 PIN_BANK_2BIT(16, 0x020, "gpc"),
622 PIN_BANK_2BIT(16, 0x030, "gpd"),
623 PIN_BANK_2BIT(16, 0x040, "gpe"),
624 PIN_BANK_2BIT_EINTW(8, 0x050, "gpf", 0, 0xff),
625 PIN_BANK_2BIT_EINTW(16, 0x060, "gpg", 8, 0xffff00),
626 PIN_BANK_2BIT(15, 0x070, "gph"),
627 PIN_BANK_2BIT(16, 0x0d0, "gpj"),
628 PIN_BANK_2BIT(16, 0x0e0, "gpk"),
629 PIN_BANK_2BIT(15, 0x0f0, "gpl"),
630 PIN_BANK_2BIT(2, 0x100, "gpm"),
633 const struct samsung_pin_ctrl s3c2450_pin_ctrl
[] __initconst
= {
635 .pin_banks
= s3c2450_pin_banks
,
636 .nr_banks
= ARRAY_SIZE(s3c2450_pin_banks
),
637 .eint_wkup_init
= s3c24xx_eint_init
,