1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2019 American Megatrends International LLC.
5 * Author: Karthikeyan Mani <karthikeyanm@amiindia.co.in>
8 #include <linux/bitfield.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/hashtable.h>
12 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/spinlock.h>
18 #include <linux/string.h>
21 * MAX_NR_HW_GPIO represents the number of actual hardware-supported GPIOs (ie,
22 * slots within the clocked serial GPIO data). Since each HW GPIO is both an
23 * input and an output, we provide MAX_NR_HW_GPIO * 2 lines on our gpiochip
26 * We use SGPIO_OUTPUT_OFFSET to define the split between the inputs and
27 * outputs; the inputs start at line 0, the outputs start at OUTPUT_OFFSET.
29 #define MAX_NR_HW_SGPIO 80
30 #define SGPIO_OUTPUT_OFFSET MAX_NR_HW_SGPIO
32 #define ASPEED_SGPIO_CTRL 0x54
34 #define ASPEED_SGPIO_PINS_MASK GENMASK(9, 6)
35 #define ASPEED_SGPIO_CLK_DIV_MASK GENMASK(31, 16)
36 #define ASPEED_SGPIO_ENABLE BIT(0)
39 struct gpio_chip chip
;
47 struct aspeed_sgpio_bank
{
51 const char names
[4][3];
55 * Note: The "value" register returns the input value when the GPIO is
56 * configured as an input.
58 * The "rdata" register returns the output value when the GPIO is
59 * configured as an output.
61 static const struct aspeed_sgpio_bank aspeed_sgpio_banks
[] = {
66 .names
= { "A", "B", "C", "D" },
72 .names
= { "E", "F", "G", "H" },
78 .names
= { "I", "J" },
82 enum aspeed_sgpio_reg
{
92 #define GPIO_VAL_VALUE 0x00
93 #define GPIO_IRQ_ENABLE 0x00
94 #define GPIO_IRQ_TYPE0 0x04
95 #define GPIO_IRQ_TYPE1 0x08
96 #define GPIO_IRQ_TYPE2 0x0C
97 #define GPIO_IRQ_STATUS 0x10
99 static void __iomem
*bank_reg(struct aspeed_sgpio
*gpio
,
100 const struct aspeed_sgpio_bank
*bank
,
101 const enum aspeed_sgpio_reg reg
)
105 return gpio
->base
+ bank
->val_regs
+ GPIO_VAL_VALUE
;
107 return gpio
->base
+ bank
->rdata_reg
;
109 return gpio
->base
+ bank
->irq_regs
+ GPIO_IRQ_ENABLE
;
111 return gpio
->base
+ bank
->irq_regs
+ GPIO_IRQ_TYPE0
;
113 return gpio
->base
+ bank
->irq_regs
+ GPIO_IRQ_TYPE1
;
115 return gpio
->base
+ bank
->irq_regs
+ GPIO_IRQ_TYPE2
;
117 return gpio
->base
+ bank
->irq_regs
+ GPIO_IRQ_STATUS
;
119 /* acturally if code runs to here, it's an error case */
124 #define GPIO_BANK(x) ((x % SGPIO_OUTPUT_OFFSET) >> 5)
125 #define GPIO_OFFSET(x) ((x % SGPIO_OUTPUT_OFFSET) & 0x1f)
126 #define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
128 static const struct aspeed_sgpio_bank
*to_bank(unsigned int offset
)
132 bank
= GPIO_BANK(offset
);
134 WARN_ON(bank
>= ARRAY_SIZE(aspeed_sgpio_banks
));
135 return &aspeed_sgpio_banks
[bank
];
138 static int aspeed_sgpio_init_valid_mask(struct gpio_chip
*gc
,
139 unsigned long *valid_mask
, unsigned int ngpios
)
141 struct aspeed_sgpio
*sgpio
= gpiochip_get_data(gc
);
142 int n
= sgpio
->n_sgpio
;
143 int c
= SGPIO_OUTPUT_OFFSET
- n
;
145 WARN_ON(ngpios
< MAX_NR_HW_SGPIO
* 2);
147 /* input GPIOs in the lower range */
148 bitmap_set(valid_mask
, 0, n
);
149 bitmap_clear(valid_mask
, n
, c
);
151 /* output GPIOS above SGPIO_OUTPUT_OFFSET */
152 bitmap_set(valid_mask
, SGPIO_OUTPUT_OFFSET
, n
);
153 bitmap_clear(valid_mask
, SGPIO_OUTPUT_OFFSET
+ n
, c
);
158 static void aspeed_sgpio_irq_init_valid_mask(struct gpio_chip
*gc
,
159 unsigned long *valid_mask
, unsigned int ngpios
)
161 struct aspeed_sgpio
*sgpio
= gpiochip_get_data(gc
);
162 int n
= sgpio
->n_sgpio
;
164 WARN_ON(ngpios
< MAX_NR_HW_SGPIO
* 2);
166 /* input GPIOs in the lower range */
167 bitmap_set(valid_mask
, 0, n
);
168 bitmap_clear(valid_mask
, n
, ngpios
- n
);
171 static bool aspeed_sgpio_is_input(unsigned int offset
)
173 return offset
< SGPIO_OUTPUT_OFFSET
;
176 static int aspeed_sgpio_get(struct gpio_chip
*gc
, unsigned int offset
)
178 struct aspeed_sgpio
*gpio
= gpiochip_get_data(gc
);
179 const struct aspeed_sgpio_bank
*bank
= to_bank(offset
);
181 enum aspeed_sgpio_reg reg
;
184 spin_lock_irqsave(&gpio
->lock
, flags
);
186 reg
= aspeed_sgpio_is_input(offset
) ? reg_val
: reg_rdata
;
187 rc
= !!(ioread32(bank_reg(gpio
, bank
, reg
)) & GPIO_BIT(offset
));
189 spin_unlock_irqrestore(&gpio
->lock
, flags
);
194 static int sgpio_set_value(struct gpio_chip
*gc
, unsigned int offset
, int val
)
196 struct aspeed_sgpio
*gpio
= gpiochip_get_data(gc
);
197 const struct aspeed_sgpio_bank
*bank
= to_bank(offset
);
198 void __iomem
*addr_r
, *addr_w
;
201 if (aspeed_sgpio_is_input(offset
))
204 /* Since this is an output, read the cached value from rdata, then
206 addr_r
= bank_reg(gpio
, bank
, reg_rdata
);
207 addr_w
= bank_reg(gpio
, bank
, reg_val
);
209 reg
= ioread32(addr_r
);
212 reg
|= GPIO_BIT(offset
);
214 reg
&= ~GPIO_BIT(offset
);
216 iowrite32(reg
, addr_w
);
221 static void aspeed_sgpio_set(struct gpio_chip
*gc
, unsigned int offset
, int val
)
223 struct aspeed_sgpio
*gpio
= gpiochip_get_data(gc
);
226 spin_lock_irqsave(&gpio
->lock
, flags
);
228 sgpio_set_value(gc
, offset
, val
);
230 spin_unlock_irqrestore(&gpio
->lock
, flags
);
233 static int aspeed_sgpio_dir_in(struct gpio_chip
*gc
, unsigned int offset
)
235 return aspeed_sgpio_is_input(offset
) ? 0 : -EINVAL
;
238 static int aspeed_sgpio_dir_out(struct gpio_chip
*gc
, unsigned int offset
, int val
)
240 struct aspeed_sgpio
*gpio
= gpiochip_get_data(gc
);
244 /* No special action is required for setting the direction; we'll
245 * error-out in sgpio_set_value if this isn't an output GPIO */
247 spin_lock_irqsave(&gpio
->lock
, flags
);
248 rc
= sgpio_set_value(gc
, offset
, val
);
249 spin_unlock_irqrestore(&gpio
->lock
, flags
);
254 static int aspeed_sgpio_get_direction(struct gpio_chip
*gc
, unsigned int offset
)
256 return !!aspeed_sgpio_is_input(offset
);
259 static void irqd_to_aspeed_sgpio_data(struct irq_data
*d
,
260 struct aspeed_sgpio
**gpio
,
261 const struct aspeed_sgpio_bank
**bank
,
262 u32
*bit
, int *offset
)
264 struct aspeed_sgpio
*internal
;
266 *offset
= irqd_to_hwirq(d
);
267 internal
= irq_data_get_irq_chip_data(d
);
271 *bank
= to_bank(*offset
);
272 *bit
= GPIO_BIT(*offset
);
275 static void aspeed_sgpio_irq_ack(struct irq_data
*d
)
277 const struct aspeed_sgpio_bank
*bank
;
278 struct aspeed_sgpio
*gpio
;
280 void __iomem
*status_addr
;
284 irqd_to_aspeed_sgpio_data(d
, &gpio
, &bank
, &bit
, &offset
);
286 status_addr
= bank_reg(gpio
, bank
, reg_irq_status
);
288 spin_lock_irqsave(&gpio
->lock
, flags
);
290 iowrite32(bit
, status_addr
);
292 spin_unlock_irqrestore(&gpio
->lock
, flags
);
295 static void aspeed_sgpio_irq_set_mask(struct irq_data
*d
, bool set
)
297 const struct aspeed_sgpio_bank
*bank
;
298 struct aspeed_sgpio
*gpio
;
304 irqd_to_aspeed_sgpio_data(d
, &gpio
, &bank
, &bit
, &offset
);
305 addr
= bank_reg(gpio
, bank
, reg_irq_enable
);
307 spin_lock_irqsave(&gpio
->lock
, flags
);
309 reg
= ioread32(addr
);
315 iowrite32(reg
, addr
);
317 spin_unlock_irqrestore(&gpio
->lock
, flags
);
320 static void aspeed_sgpio_irq_mask(struct irq_data
*d
)
322 aspeed_sgpio_irq_set_mask(d
, false);
325 static void aspeed_sgpio_irq_unmask(struct irq_data
*d
)
327 aspeed_sgpio_irq_set_mask(d
, true);
330 static int aspeed_sgpio_set_type(struct irq_data
*d
, unsigned int type
)
336 const struct aspeed_sgpio_bank
*bank
;
337 irq_flow_handler_t handler
;
338 struct aspeed_sgpio
*gpio
;
343 irqd_to_aspeed_sgpio_data(d
, &gpio
, &bank
, &bit
, &offset
);
345 switch (type
& IRQ_TYPE_SENSE_MASK
) {
346 case IRQ_TYPE_EDGE_BOTH
:
349 case IRQ_TYPE_EDGE_RISING
:
352 case IRQ_TYPE_EDGE_FALLING
:
353 handler
= handle_edge_irq
;
355 case IRQ_TYPE_LEVEL_HIGH
:
358 case IRQ_TYPE_LEVEL_LOW
:
360 handler
= handle_level_irq
;
366 spin_lock_irqsave(&gpio
->lock
, flags
);
368 addr
= bank_reg(gpio
, bank
, reg_irq_type0
);
369 reg
= ioread32(addr
);
370 reg
= (reg
& ~bit
) | type0
;
371 iowrite32(reg
, addr
);
373 addr
= bank_reg(gpio
, bank
, reg_irq_type1
);
374 reg
= ioread32(addr
);
375 reg
= (reg
& ~bit
) | type1
;
376 iowrite32(reg
, addr
);
378 addr
= bank_reg(gpio
, bank
, reg_irq_type2
);
379 reg
= ioread32(addr
);
380 reg
= (reg
& ~bit
) | type2
;
381 iowrite32(reg
, addr
);
383 spin_unlock_irqrestore(&gpio
->lock
, flags
);
385 irq_set_handler_locked(d
, handler
);
390 static void aspeed_sgpio_irq_handler(struct irq_desc
*desc
)
392 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
393 struct irq_chip
*ic
= irq_desc_get_chip(desc
);
394 struct aspeed_sgpio
*data
= gpiochip_get_data(gc
);
395 unsigned int i
, p
, girq
;
398 chained_irq_enter(ic
, desc
);
400 for (i
= 0; i
< ARRAY_SIZE(aspeed_sgpio_banks
); i
++) {
401 const struct aspeed_sgpio_bank
*bank
= &aspeed_sgpio_banks
[i
];
403 reg
= ioread32(bank_reg(data
, bank
, reg_irq_status
));
405 for_each_set_bit(p
, ®
, 32) {
406 girq
= irq_find_mapping(gc
->irq
.domain
, i
* 32 + p
);
407 generic_handle_irq(girq
);
412 chained_irq_exit(ic
, desc
);
415 static struct irq_chip aspeed_sgpio_irqchip
= {
416 .name
= "aspeed-sgpio",
417 .irq_ack
= aspeed_sgpio_irq_ack
,
418 .irq_mask
= aspeed_sgpio_irq_mask
,
419 .irq_unmask
= aspeed_sgpio_irq_unmask
,
420 .irq_set_type
= aspeed_sgpio_set_type
,
423 static int aspeed_sgpio_setup_irqs(struct aspeed_sgpio
*gpio
,
424 struct platform_device
*pdev
)
427 const struct aspeed_sgpio_bank
*bank
;
428 struct gpio_irq_chip
*irq
;
430 rc
= platform_get_irq(pdev
, 0);
436 /* Disable IRQ and clear Interrupt status registers for all SGPIO Pins. */
437 for (i
= 0; i
< ARRAY_SIZE(aspeed_sgpio_banks
); i
++) {
438 bank
= &aspeed_sgpio_banks
[i
];
439 /* disable irq enable bits */
440 iowrite32(0x00000000, bank_reg(gpio
, bank
, reg_irq_enable
));
441 /* clear status bits */
442 iowrite32(0xffffffff, bank_reg(gpio
, bank
, reg_irq_status
));
445 irq
= &gpio
->chip
.irq
;
446 irq
->chip
= &aspeed_sgpio_irqchip
;
447 irq
->init_valid_mask
= aspeed_sgpio_irq_init_valid_mask
;
448 irq
->handler
= handle_bad_irq
;
449 irq
->default_type
= IRQ_TYPE_NONE
;
450 irq
->parent_handler
= aspeed_sgpio_irq_handler
;
451 irq
->parent_handler_data
= gpio
;
452 irq
->parents
= &gpio
->irq
;
453 irq
->num_parents
= 1;
455 /* Apply default IRQ settings */
456 for (i
= 0; i
< ARRAY_SIZE(aspeed_sgpio_banks
); i
++) {
457 bank
= &aspeed_sgpio_banks
[i
];
458 /* set falling or level-low irq */
459 iowrite32(0x00000000, bank_reg(gpio
, bank
, reg_irq_type0
));
460 /* trigger type is edge */
461 iowrite32(0x00000000, bank_reg(gpio
, bank
, reg_irq_type1
));
462 /* single edge trigger */
463 iowrite32(0x00000000, bank_reg(gpio
, bank
, reg_irq_type2
));
469 static const struct of_device_id aspeed_sgpio_of_table
[] = {
470 { .compatible
= "aspeed,ast2400-sgpio" },
471 { .compatible
= "aspeed,ast2500-sgpio" },
475 MODULE_DEVICE_TABLE(of
, aspeed_sgpio_of_table
);
477 static int __init
aspeed_sgpio_probe(struct platform_device
*pdev
)
479 struct aspeed_sgpio
*gpio
;
480 u32 nr_gpios
, sgpio_freq
, sgpio_clk_div
;
482 unsigned long apb_freq
;
484 gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*gpio
), GFP_KERNEL
);
488 gpio
->base
= devm_platform_ioremap_resource(pdev
, 0);
489 if (IS_ERR(gpio
->base
))
490 return PTR_ERR(gpio
->base
);
492 rc
= of_property_read_u32(pdev
->dev
.of_node
, "ngpios", &nr_gpios
);
494 dev_err(&pdev
->dev
, "Could not read ngpios property\n");
496 } else if (nr_gpios
> MAX_NR_HW_SGPIO
) {
497 dev_err(&pdev
->dev
, "Number of GPIOs exceeds the maximum of %d: %d\n",
498 MAX_NR_HW_SGPIO
, nr_gpios
);
501 gpio
->n_sgpio
= nr_gpios
;
503 rc
= of_property_read_u32(pdev
->dev
.of_node
, "bus-frequency", &sgpio_freq
);
505 dev_err(&pdev
->dev
, "Could not read bus-frequency property\n");
509 gpio
->pclk
= devm_clk_get(&pdev
->dev
, NULL
);
510 if (IS_ERR(gpio
->pclk
)) {
511 dev_err(&pdev
->dev
, "devm_clk_get failed\n");
512 return PTR_ERR(gpio
->pclk
);
515 apb_freq
= clk_get_rate(gpio
->pclk
);
518 * From the datasheet,
519 * SGPIO period = 1/PCLK * 2 * (GPIO254[31:16] + 1)
520 * period = 2 * (GPIO254[31:16] + 1) / PCLK
521 * frequency = 1 / (2 * (GPIO254[31:16] + 1) / PCLK)
522 * frequency = PCLK / (2 * (GPIO254[31:16] + 1))
523 * frequency * 2 * (GPIO254[31:16] + 1) = PCLK
524 * GPIO254[31:16] = PCLK / (frequency * 2) - 1
529 sgpio_clk_div
= (apb_freq
/ (sgpio_freq
* 2)) - 1;
531 if (sgpio_clk_div
> (1 << 16) - 1)
534 iowrite32(FIELD_PREP(ASPEED_SGPIO_CLK_DIV_MASK
, sgpio_clk_div
) |
535 FIELD_PREP(ASPEED_SGPIO_PINS_MASK
, (nr_gpios
/ 8)) |
537 gpio
->base
+ ASPEED_SGPIO_CTRL
);
539 spin_lock_init(&gpio
->lock
);
541 gpio
->chip
.parent
= &pdev
->dev
;
542 gpio
->chip
.ngpio
= MAX_NR_HW_SGPIO
* 2;
543 gpio
->chip
.init_valid_mask
= aspeed_sgpio_init_valid_mask
;
544 gpio
->chip
.direction_input
= aspeed_sgpio_dir_in
;
545 gpio
->chip
.direction_output
= aspeed_sgpio_dir_out
;
546 gpio
->chip
.get_direction
= aspeed_sgpio_get_direction
;
547 gpio
->chip
.request
= NULL
;
548 gpio
->chip
.free
= NULL
;
549 gpio
->chip
.get
= aspeed_sgpio_get
;
550 gpio
->chip
.set
= aspeed_sgpio_set
;
551 gpio
->chip
.set_config
= NULL
;
552 gpio
->chip
.label
= dev_name(&pdev
->dev
);
553 gpio
->chip
.base
= -1;
555 aspeed_sgpio_setup_irqs(gpio
, pdev
);
557 rc
= devm_gpiochip_add_data(&pdev
->dev
, &gpio
->chip
, gpio
);
564 static struct platform_driver aspeed_sgpio_driver
= {
566 .name
= KBUILD_MODNAME
,
567 .of_match_table
= aspeed_sgpio_of_table
,
571 module_platform_driver_probe(aspeed_sgpio_driver
, aspeed_sgpio_probe
);
572 MODULE_DESCRIPTION("Aspeed Serial GPIO Driver");
573 MODULE_LICENSE("GPL");