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>
20 #define MAX_NR_SGPIO 80
22 #define ASPEED_SGPIO_CTRL 0x54
24 #define ASPEED_SGPIO_PINS_MASK GENMASK(9, 6)
25 #define ASPEED_SGPIO_CLK_DIV_MASK GENMASK(31, 16)
26 #define ASPEED_SGPIO_ENABLE BIT(0)
29 struct gpio_chip chip
;
37 struct aspeed_sgpio_bank
{
41 const char names
[4][3];
45 * Note: The "value" register returns the input value when the GPIO is
46 * configured as an input.
48 * The "rdata" register returns the output value when the GPIO is
49 * configured as an output.
51 static const struct aspeed_sgpio_bank aspeed_sgpio_banks
[] = {
56 .names
= { "A", "B", "C", "D" },
62 .names
= { "E", "F", "G", "H" },
68 .names
= { "I", "J" },
72 enum aspeed_sgpio_reg
{
82 #define GPIO_VAL_VALUE 0x00
83 #define GPIO_IRQ_ENABLE 0x00
84 #define GPIO_IRQ_TYPE0 0x04
85 #define GPIO_IRQ_TYPE1 0x08
86 #define GPIO_IRQ_TYPE2 0x0C
87 #define GPIO_IRQ_STATUS 0x10
89 static void __iomem
*bank_reg(struct aspeed_sgpio
*gpio
,
90 const struct aspeed_sgpio_bank
*bank
,
91 const enum aspeed_sgpio_reg reg
)
95 return gpio
->base
+ bank
->val_regs
+ GPIO_VAL_VALUE
;
97 return gpio
->base
+ bank
->rdata_reg
;
99 return gpio
->base
+ bank
->irq_regs
+ GPIO_IRQ_ENABLE
;
101 return gpio
->base
+ bank
->irq_regs
+ GPIO_IRQ_TYPE0
;
103 return gpio
->base
+ bank
->irq_regs
+ GPIO_IRQ_TYPE1
;
105 return gpio
->base
+ bank
->irq_regs
+ GPIO_IRQ_TYPE2
;
107 return gpio
->base
+ bank
->irq_regs
+ GPIO_IRQ_STATUS
;
109 /* acturally if code runs to here, it's an error case */
114 #define GPIO_BANK(x) ((x) >> 5)
115 #define GPIO_OFFSET(x) ((x) & 0x1f)
116 #define GPIO_BIT(x) BIT(GPIO_OFFSET(x))
118 static const struct aspeed_sgpio_bank
*to_bank(unsigned int offset
)
120 unsigned int bank
= GPIO_BANK(offset
);
122 WARN_ON(bank
>= ARRAY_SIZE(aspeed_sgpio_banks
));
123 return &aspeed_sgpio_banks
[bank
];
126 static int aspeed_sgpio_get(struct gpio_chip
*gc
, unsigned int offset
)
128 struct aspeed_sgpio
*gpio
= gpiochip_get_data(gc
);
129 const struct aspeed_sgpio_bank
*bank
= to_bank(offset
);
131 enum aspeed_sgpio_reg reg
;
135 spin_lock_irqsave(&gpio
->lock
, flags
);
137 is_input
= gpio
->dir_in
[GPIO_BANK(offset
)] & GPIO_BIT(offset
);
138 reg
= is_input
? reg_val
: reg_rdata
;
139 rc
= !!(ioread32(bank_reg(gpio
, bank
, reg
)) & GPIO_BIT(offset
));
141 spin_unlock_irqrestore(&gpio
->lock
, flags
);
146 static void sgpio_set_value(struct gpio_chip
*gc
, unsigned int offset
, int val
)
148 struct aspeed_sgpio
*gpio
= gpiochip_get_data(gc
);
149 const struct aspeed_sgpio_bank
*bank
= to_bank(offset
);
153 addr
= bank_reg(gpio
, bank
, reg_val
);
154 reg
= ioread32(addr
);
157 reg
|= GPIO_BIT(offset
);
159 reg
&= ~GPIO_BIT(offset
);
161 iowrite32(reg
, addr
);
164 static void aspeed_sgpio_set(struct gpio_chip
*gc
, unsigned int offset
, int val
)
166 struct aspeed_sgpio
*gpio
= gpiochip_get_data(gc
);
169 spin_lock_irqsave(&gpio
->lock
, flags
);
171 sgpio_set_value(gc
, offset
, val
);
173 spin_unlock_irqrestore(&gpio
->lock
, flags
);
176 static int aspeed_sgpio_dir_in(struct gpio_chip
*gc
, unsigned int offset
)
178 struct aspeed_sgpio
*gpio
= gpiochip_get_data(gc
);
181 spin_lock_irqsave(&gpio
->lock
, flags
);
182 gpio
->dir_in
[GPIO_BANK(offset
)] |= GPIO_BIT(offset
);
183 spin_unlock_irqrestore(&gpio
->lock
, flags
);
188 static int aspeed_sgpio_dir_out(struct gpio_chip
*gc
, unsigned int offset
, int val
)
190 struct aspeed_sgpio
*gpio
= gpiochip_get_data(gc
);
193 spin_lock_irqsave(&gpio
->lock
, flags
);
195 gpio
->dir_in
[GPIO_BANK(offset
)] &= ~GPIO_BIT(offset
);
196 sgpio_set_value(gc
, offset
, val
);
198 spin_unlock_irqrestore(&gpio
->lock
, flags
);
203 static int aspeed_sgpio_get_direction(struct gpio_chip
*gc
, unsigned int offset
)
206 struct aspeed_sgpio
*gpio
= gpiochip_get_data(gc
);
209 spin_lock_irqsave(&gpio
->lock
, flags
);
210 dir_status
= gpio
->dir_in
[GPIO_BANK(offset
)] & GPIO_BIT(offset
);
211 spin_unlock_irqrestore(&gpio
->lock
, flags
);
217 static void irqd_to_aspeed_sgpio_data(struct irq_data
*d
,
218 struct aspeed_sgpio
**gpio
,
219 const struct aspeed_sgpio_bank
**bank
,
220 u32
*bit
, int *offset
)
222 struct aspeed_sgpio
*internal
;
224 *offset
= irqd_to_hwirq(d
);
225 internal
= irq_data_get_irq_chip_data(d
);
229 *bank
= to_bank(*offset
);
230 *bit
= GPIO_BIT(*offset
);
233 static void aspeed_sgpio_irq_ack(struct irq_data
*d
)
235 const struct aspeed_sgpio_bank
*bank
;
236 struct aspeed_sgpio
*gpio
;
238 void __iomem
*status_addr
;
242 irqd_to_aspeed_sgpio_data(d
, &gpio
, &bank
, &bit
, &offset
);
244 status_addr
= bank_reg(gpio
, bank
, reg_irq_status
);
246 spin_lock_irqsave(&gpio
->lock
, flags
);
248 iowrite32(bit
, status_addr
);
250 spin_unlock_irqrestore(&gpio
->lock
, flags
);
253 static void aspeed_sgpio_irq_set_mask(struct irq_data
*d
, bool set
)
255 const struct aspeed_sgpio_bank
*bank
;
256 struct aspeed_sgpio
*gpio
;
262 irqd_to_aspeed_sgpio_data(d
, &gpio
, &bank
, &bit
, &offset
);
263 addr
= bank_reg(gpio
, bank
, reg_irq_enable
);
265 spin_lock_irqsave(&gpio
->lock
, flags
);
267 reg
= ioread32(addr
);
273 iowrite32(reg
, addr
);
275 spin_unlock_irqrestore(&gpio
->lock
, flags
);
278 static void aspeed_sgpio_irq_mask(struct irq_data
*d
)
280 aspeed_sgpio_irq_set_mask(d
, false);
283 static void aspeed_sgpio_irq_unmask(struct irq_data
*d
)
285 aspeed_sgpio_irq_set_mask(d
, true);
288 static int aspeed_sgpio_set_type(struct irq_data
*d
, unsigned int type
)
294 const struct aspeed_sgpio_bank
*bank
;
295 irq_flow_handler_t handler
;
296 struct aspeed_sgpio
*gpio
;
301 irqd_to_aspeed_sgpio_data(d
, &gpio
, &bank
, &bit
, &offset
);
303 switch (type
& IRQ_TYPE_SENSE_MASK
) {
304 case IRQ_TYPE_EDGE_BOTH
:
307 case IRQ_TYPE_EDGE_RISING
:
310 case IRQ_TYPE_EDGE_FALLING
:
311 handler
= handle_edge_irq
;
313 case IRQ_TYPE_LEVEL_HIGH
:
316 case IRQ_TYPE_LEVEL_LOW
:
318 handler
= handle_level_irq
;
324 spin_lock_irqsave(&gpio
->lock
, flags
);
326 addr
= bank_reg(gpio
, bank
, reg_irq_type0
);
327 reg
= ioread32(addr
);
328 reg
= (reg
& ~bit
) | type0
;
329 iowrite32(reg
, addr
);
331 addr
= bank_reg(gpio
, bank
, reg_irq_type1
);
332 reg
= ioread32(addr
);
333 reg
= (reg
& ~bit
) | type1
;
334 iowrite32(reg
, addr
);
336 addr
= bank_reg(gpio
, bank
, reg_irq_type2
);
337 reg
= ioread32(addr
);
338 reg
= (reg
& ~bit
) | type2
;
339 iowrite32(reg
, addr
);
341 spin_unlock_irqrestore(&gpio
->lock
, flags
);
343 irq_set_handler_locked(d
, handler
);
348 static void aspeed_sgpio_irq_handler(struct irq_desc
*desc
)
350 struct gpio_chip
*gc
= irq_desc_get_handler_data(desc
);
351 struct irq_chip
*ic
= irq_desc_get_chip(desc
);
352 struct aspeed_sgpio
*data
= gpiochip_get_data(gc
);
353 unsigned int i
, p
, girq
;
356 chained_irq_enter(ic
, desc
);
358 for (i
= 0; i
< ARRAY_SIZE(aspeed_sgpio_banks
); i
++) {
359 const struct aspeed_sgpio_bank
*bank
= &aspeed_sgpio_banks
[i
];
361 reg
= ioread32(bank_reg(data
, bank
, reg_irq_status
));
363 for_each_set_bit(p
, ®
, 32) {
364 girq
= irq_find_mapping(gc
->irq
.domain
, i
* 32 + p
);
365 generic_handle_irq(girq
);
370 chained_irq_exit(ic
, desc
);
373 static struct irq_chip aspeed_sgpio_irqchip
= {
374 .name
= "aspeed-sgpio",
375 .irq_ack
= aspeed_sgpio_irq_ack
,
376 .irq_mask
= aspeed_sgpio_irq_mask
,
377 .irq_unmask
= aspeed_sgpio_irq_unmask
,
378 .irq_set_type
= aspeed_sgpio_set_type
,
381 static int aspeed_sgpio_setup_irqs(struct aspeed_sgpio
*gpio
,
382 struct platform_device
*pdev
)
385 const struct aspeed_sgpio_bank
*bank
;
386 struct gpio_irq_chip
*irq
;
388 rc
= platform_get_irq(pdev
, 0);
394 /* Disable IRQ and clear Interrupt status registers for all SGPIO Pins. */
395 for (i
= 0; i
< ARRAY_SIZE(aspeed_sgpio_banks
); i
++) {
396 bank
= &aspeed_sgpio_banks
[i
];
397 /* disable irq enable bits */
398 iowrite32(0x00000000, bank_reg(gpio
, bank
, reg_irq_enable
));
399 /* clear status bits */
400 iowrite32(0xffffffff, bank_reg(gpio
, bank
, reg_irq_status
));
403 irq
= &gpio
->chip
.irq
;
404 irq
->chip
= &aspeed_sgpio_irqchip
;
405 irq
->handler
= handle_bad_irq
;
406 irq
->default_type
= IRQ_TYPE_NONE
;
407 irq
->parent_handler
= aspeed_sgpio_irq_handler
;
408 irq
->parent_handler_data
= gpio
;
409 irq
->parents
= &gpio
->irq
;
410 irq
->num_parents
= 1;
412 /* set IRQ settings and Enable Interrupt */
413 for (i
= 0; i
< ARRAY_SIZE(aspeed_sgpio_banks
); i
++) {
414 bank
= &aspeed_sgpio_banks
[i
];
415 /* set falling or level-low irq */
416 iowrite32(0x00000000, bank_reg(gpio
, bank
, reg_irq_type0
));
417 /* trigger type is edge */
418 iowrite32(0x00000000, bank_reg(gpio
, bank
, reg_irq_type1
));
419 /* dual edge trigger mode. */
420 iowrite32(0xffffffff, bank_reg(gpio
, bank
, reg_irq_type2
));
422 iowrite32(0xffffffff, bank_reg(gpio
, bank
, reg_irq_enable
));
428 static const struct of_device_id aspeed_sgpio_of_table
[] = {
429 { .compatible
= "aspeed,ast2400-sgpio" },
430 { .compatible
= "aspeed,ast2500-sgpio" },
434 MODULE_DEVICE_TABLE(of
, aspeed_sgpio_of_table
);
436 static int __init
aspeed_sgpio_probe(struct platform_device
*pdev
)
438 struct aspeed_sgpio
*gpio
;
439 u32 nr_gpios
, sgpio_freq
, sgpio_clk_div
;
441 unsigned long apb_freq
;
443 gpio
= devm_kzalloc(&pdev
->dev
, sizeof(*gpio
), GFP_KERNEL
);
447 gpio
->base
= devm_platform_ioremap_resource(pdev
, 0);
448 if (IS_ERR(gpio
->base
))
449 return PTR_ERR(gpio
->base
);
451 rc
= of_property_read_u32(pdev
->dev
.of_node
, "ngpios", &nr_gpios
);
453 dev_err(&pdev
->dev
, "Could not read ngpios property\n");
455 } else if (nr_gpios
> MAX_NR_SGPIO
) {
456 dev_err(&pdev
->dev
, "Number of GPIOs exceeds the maximum of %d: %d\n",
457 MAX_NR_SGPIO
, nr_gpios
);
461 rc
= of_property_read_u32(pdev
->dev
.of_node
, "bus-frequency", &sgpio_freq
);
463 dev_err(&pdev
->dev
, "Could not read bus-frequency property\n");
467 gpio
->pclk
= devm_clk_get(&pdev
->dev
, NULL
);
468 if (IS_ERR(gpio
->pclk
)) {
469 dev_err(&pdev
->dev
, "devm_clk_get failed\n");
470 return PTR_ERR(gpio
->pclk
);
473 apb_freq
= clk_get_rate(gpio
->pclk
);
476 * From the datasheet,
477 * SGPIO period = 1/PCLK * 2 * (GPIO254[31:16] + 1)
478 * period = 2 * (GPIO254[31:16] + 1) / PCLK
479 * frequency = 1 / (2 * (GPIO254[31:16] + 1) / PCLK)
480 * frequency = PCLK / (2 * (GPIO254[31:16] + 1))
481 * frequency * 2 * (GPIO254[31:16] + 1) = PCLK
482 * GPIO254[31:16] = PCLK / (frequency * 2) - 1
487 sgpio_clk_div
= (apb_freq
/ (sgpio_freq
* 2)) - 1;
489 if (sgpio_clk_div
> (1 << 16) - 1)
492 iowrite32(FIELD_PREP(ASPEED_SGPIO_CLK_DIV_MASK
, sgpio_clk_div
) |
493 FIELD_PREP(ASPEED_SGPIO_PINS_MASK
, (nr_gpios
/ 8)) |
495 gpio
->base
+ ASPEED_SGPIO_CTRL
);
497 spin_lock_init(&gpio
->lock
);
499 gpio
->chip
.parent
= &pdev
->dev
;
500 gpio
->chip
.ngpio
= nr_gpios
;
501 gpio
->chip
.direction_input
= aspeed_sgpio_dir_in
;
502 gpio
->chip
.direction_output
= aspeed_sgpio_dir_out
;
503 gpio
->chip
.get_direction
= aspeed_sgpio_get_direction
;
504 gpio
->chip
.request
= NULL
;
505 gpio
->chip
.free
= NULL
;
506 gpio
->chip
.get
= aspeed_sgpio_get
;
507 gpio
->chip
.set
= aspeed_sgpio_set
;
508 gpio
->chip
.set_config
= NULL
;
509 gpio
->chip
.label
= dev_name(&pdev
->dev
);
510 gpio
->chip
.base
= -1;
512 /* set all SGPIO pins as input (1). */
513 memset(gpio
->dir_in
, 0xff, sizeof(gpio
->dir_in
));
515 aspeed_sgpio_setup_irqs(gpio
, pdev
);
517 rc
= devm_gpiochip_add_data(&pdev
->dev
, &gpio
->chip
, gpio
);
524 static struct platform_driver aspeed_sgpio_driver
= {
526 .name
= KBUILD_MODNAME
,
527 .of_match_table
= aspeed_sgpio_of_table
,
531 module_platform_driver_probe(aspeed_sgpio_driver
, aspeed_sgpio_probe
);
532 MODULE_DESCRIPTION("Aspeed Serial GPIO Driver");
533 MODULE_LICENSE("GPL");