1 // SPDX-License-Identifier: GPL-2.0
3 * TQ-Systems TQMx86 PLD GPIO driver
5 * Based on vendor driver by:
6 * Vadim V.Vlasov <vvlasov@dev.rtsoft.ru>
9 #include <linux/bitmap.h>
10 #include <linux/bitops.h>
11 #include <linux/errno.h>
12 #include <linux/gpio/driver.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_runtime.h>
19 #include <linux/seq_file.h>
20 #include <linux/slab.h>
22 #define TQMX86_NGPIO 8
23 #define TQMX86_NGPO 4 /* 0-3 - output */
24 #define TQMX86_NGPI 4 /* 4-7 - input */
25 #define TQMX86_DIR_INPUT_MASK 0xf0 /* 0-3 - output, 4-7 - input */
27 #define TQMX86_GPIODD 0 /* GPIO Data Direction Register */
28 #define TQMX86_GPIOD 1 /* GPIO Data Register */
29 #define TQMX86_GPIIC 3 /* GPI Interrupt Configuration Register */
30 #define TQMX86_GPIIS 4 /* GPI Interrupt Status Register */
32 #define TQMX86_GPII_NONE 0
33 #define TQMX86_GPII_FALLING BIT(0)
34 #define TQMX86_GPII_RISING BIT(1)
35 /* Stored in irq_type as a trigger type, but not actually valid as a register
36 * value, so the name doesn't use "GPII"
38 #define TQMX86_INT_BOTH (BIT(0) | BIT(1))
39 #define TQMX86_GPII_MASK (BIT(0) | BIT(1))
40 #define TQMX86_GPII_BITS 2
41 /* Stored in irq_type with GPII bits */
42 #define TQMX86_INT_UNMASKED BIT(2)
44 struct tqmx86_gpio_data
{
45 struct gpio_chip chip
;
46 void __iomem
*io_base
;
48 /* Lock must be held for accessing output and irq_type fields */
49 raw_spinlock_t spinlock
;
50 DECLARE_BITMAP(output
, TQMX86_NGPIO
);
51 u8 irq_type
[TQMX86_NGPI
];
54 static u8
tqmx86_gpio_read(struct tqmx86_gpio_data
*gd
, unsigned int reg
)
56 return ioread8(gd
->io_base
+ reg
);
59 static void tqmx86_gpio_write(struct tqmx86_gpio_data
*gd
, u8 val
,
62 iowrite8(val
, gd
->io_base
+ reg
);
65 static int tqmx86_gpio_get(struct gpio_chip
*chip
, unsigned int offset
)
67 struct tqmx86_gpio_data
*gpio
= gpiochip_get_data(chip
);
69 return !!(tqmx86_gpio_read(gpio
, TQMX86_GPIOD
) & BIT(offset
));
72 static void tqmx86_gpio_set(struct gpio_chip
*chip
, unsigned int offset
,
75 struct tqmx86_gpio_data
*gpio
= gpiochip_get_data(chip
);
78 raw_spin_lock_irqsave(&gpio
->spinlock
, flags
);
79 __assign_bit(offset
, gpio
->output
, value
);
80 tqmx86_gpio_write(gpio
, bitmap_get_value8(gpio
->output
, 0), TQMX86_GPIOD
);
81 raw_spin_unlock_irqrestore(&gpio
->spinlock
, flags
);
84 static int tqmx86_gpio_direction_input(struct gpio_chip
*chip
,
87 /* Direction cannot be changed. Validate is an input. */
88 if (BIT(offset
) & TQMX86_DIR_INPUT_MASK
)
94 static int tqmx86_gpio_direction_output(struct gpio_chip
*chip
,
98 /* Direction cannot be changed, validate is an output */
99 if (BIT(offset
) & TQMX86_DIR_INPUT_MASK
)
102 tqmx86_gpio_set(chip
, offset
, value
);
106 static int tqmx86_gpio_get_direction(struct gpio_chip
*chip
,
109 if (TQMX86_DIR_INPUT_MASK
& BIT(offset
))
110 return GPIO_LINE_DIRECTION_IN
;
112 return GPIO_LINE_DIRECTION_OUT
;
115 static void tqmx86_gpio_irq_config(struct tqmx86_gpio_data
*gpio
, int offset
)
116 __must_hold(&gpio
->spinlock
)
118 u8 type
= TQMX86_GPII_NONE
, gpiic
;
120 if (gpio
->irq_type
[offset
] & TQMX86_INT_UNMASKED
) {
121 type
= gpio
->irq_type
[offset
] & TQMX86_GPII_MASK
;
123 if (type
== TQMX86_INT_BOTH
)
124 type
= tqmx86_gpio_get(&gpio
->chip
, offset
+ TQMX86_NGPO
)
125 ? TQMX86_GPII_FALLING
126 : TQMX86_GPII_RISING
;
129 gpiic
= tqmx86_gpio_read(gpio
, TQMX86_GPIIC
);
130 gpiic
&= ~(TQMX86_GPII_MASK
<< (offset
* TQMX86_GPII_BITS
));
131 gpiic
|= type
<< (offset
* TQMX86_GPII_BITS
);
132 tqmx86_gpio_write(gpio
, gpiic
, TQMX86_GPIIC
);
135 static void tqmx86_gpio_irq_mask(struct irq_data
*data
)
137 unsigned int offset
= (data
->hwirq
- TQMX86_NGPO
);
138 struct tqmx86_gpio_data
*gpio
= gpiochip_get_data(
139 irq_data_get_irq_chip_data(data
));
142 raw_spin_lock_irqsave(&gpio
->spinlock
, flags
);
143 gpio
->irq_type
[offset
] &= ~TQMX86_INT_UNMASKED
;
144 tqmx86_gpio_irq_config(gpio
, offset
);
145 raw_spin_unlock_irqrestore(&gpio
->spinlock
, flags
);
147 gpiochip_disable_irq(&gpio
->chip
, irqd_to_hwirq(data
));
150 static void tqmx86_gpio_irq_unmask(struct irq_data
*data
)
152 unsigned int offset
= (data
->hwirq
- TQMX86_NGPO
);
153 struct tqmx86_gpio_data
*gpio
= gpiochip_get_data(
154 irq_data_get_irq_chip_data(data
));
157 gpiochip_enable_irq(&gpio
->chip
, irqd_to_hwirq(data
));
159 raw_spin_lock_irqsave(&gpio
->spinlock
, flags
);
160 gpio
->irq_type
[offset
] |= TQMX86_INT_UNMASKED
;
161 tqmx86_gpio_irq_config(gpio
, offset
);
162 raw_spin_unlock_irqrestore(&gpio
->spinlock
, flags
);
165 static int tqmx86_gpio_irq_set_type(struct irq_data
*data
, unsigned int type
)
167 struct tqmx86_gpio_data
*gpio
= gpiochip_get_data(
168 irq_data_get_irq_chip_data(data
));
169 unsigned int offset
= (data
->hwirq
- TQMX86_NGPO
);
170 unsigned int edge_type
= type
& IRQF_TRIGGER_MASK
;
175 case IRQ_TYPE_EDGE_RISING
:
176 new_type
= TQMX86_GPII_RISING
;
178 case IRQ_TYPE_EDGE_FALLING
:
179 new_type
= TQMX86_GPII_FALLING
;
181 case IRQ_TYPE_EDGE_BOTH
:
182 new_type
= TQMX86_INT_BOTH
;
185 return -EINVAL
; /* not supported */
188 raw_spin_lock_irqsave(&gpio
->spinlock
, flags
);
189 gpio
->irq_type
[offset
] &= ~TQMX86_GPII_MASK
;
190 gpio
->irq_type
[offset
] |= new_type
;
191 tqmx86_gpio_irq_config(gpio
, offset
);
192 raw_spin_unlock_irqrestore(&gpio
->spinlock
, flags
);
197 static void tqmx86_gpio_irq_handler(struct irq_desc
*desc
)
199 struct gpio_chip
*chip
= irq_desc_get_handler_data(desc
);
200 struct tqmx86_gpio_data
*gpio
= gpiochip_get_data(chip
);
201 struct irq_chip
*irq_chip
= irq_desc_get_chip(desc
);
202 unsigned long irq_bits
, flags
;
206 chained_irq_enter(irq_chip
, desc
);
208 irq_status
= tqmx86_gpio_read(gpio
, TQMX86_GPIIS
);
209 tqmx86_gpio_write(gpio
, irq_status
, TQMX86_GPIIS
);
211 irq_bits
= irq_status
;
213 raw_spin_lock_irqsave(&gpio
->spinlock
, flags
);
214 for_each_set_bit(i
, &irq_bits
, TQMX86_NGPI
) {
216 * Edge-both triggers are implemented by flipping the edge
217 * trigger after each interrupt, as the controller only supports
218 * either rising or falling edge triggers, but not both.
220 * Internally, the TQMx86 GPIO controller has separate status
221 * registers for rising and falling edge interrupts. GPIIC
222 * configures which bits from which register are visible in the
223 * interrupt status register GPIIS and defines what triggers the
224 * parent IRQ line. Writing to GPIIS always clears both rising
225 * and falling interrupt flags internally, regardless of the
226 * currently configured trigger.
228 * In consequence, we can cleanly implement the edge-both
229 * trigger in software by first clearing the interrupt and then
230 * setting the new trigger based on the current GPIO input in
231 * tqmx86_gpio_irq_config() - even if an edge arrives between
232 * reading the input and setting the trigger, we will have a new
235 if ((gpio
->irq_type
[i
] & TQMX86_GPII_MASK
) == TQMX86_INT_BOTH
)
236 tqmx86_gpio_irq_config(gpio
, i
);
238 raw_spin_unlock_irqrestore(&gpio
->spinlock
, flags
);
240 for_each_set_bit(i
, &irq_bits
, TQMX86_NGPI
)
241 generic_handle_domain_irq(gpio
->chip
.irq
.domain
,
244 chained_irq_exit(irq_chip
, desc
);
247 /* Minimal runtime PM is needed by the IRQ subsystem */
248 static int __maybe_unused
tqmx86_gpio_runtime_suspend(struct device
*dev
)
253 static int __maybe_unused
tqmx86_gpio_runtime_resume(struct device
*dev
)
258 static const struct dev_pm_ops tqmx86_gpio_dev_pm_ops
= {
259 SET_RUNTIME_PM_OPS(tqmx86_gpio_runtime_suspend
,
260 tqmx86_gpio_runtime_resume
, NULL
)
263 static void tqmx86_init_irq_valid_mask(struct gpio_chip
*chip
,
264 unsigned long *valid_mask
,
267 /* Only GPIOs 4-7 are valid for interrupts. Clear the others */
268 clear_bit(0, valid_mask
);
269 clear_bit(1, valid_mask
);
270 clear_bit(2, valid_mask
);
271 clear_bit(3, valid_mask
);
274 static void tqmx86_gpio_irq_print_chip(struct irq_data
*d
, struct seq_file
*p
)
276 struct gpio_chip
*gc
= irq_data_get_irq_chip_data(d
);
278 seq_puts(p
, gc
->label
);
281 static const struct irq_chip tqmx86_gpio_irq_chip
= {
282 .irq_mask
= tqmx86_gpio_irq_mask
,
283 .irq_unmask
= tqmx86_gpio_irq_unmask
,
284 .irq_set_type
= tqmx86_gpio_irq_set_type
,
285 .irq_print_chip
= tqmx86_gpio_irq_print_chip
,
286 .flags
= IRQCHIP_IMMUTABLE
,
287 GPIOCHIP_IRQ_RESOURCE_HELPERS
,
290 static int tqmx86_gpio_probe(struct platform_device
*pdev
)
292 struct device
*dev
= &pdev
->dev
;
293 struct tqmx86_gpio_data
*gpio
;
294 struct gpio_chip
*chip
;
295 struct gpio_irq_chip
*girq
;
296 void __iomem
*io_base
;
297 struct resource
*res
;
300 irq
= platform_get_irq_optional(pdev
, 0);
301 if (irq
< 0 && irq
!= -ENXIO
)
304 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
306 dev_err(&pdev
->dev
, "Cannot get I/O\n");
310 io_base
= devm_ioport_map(&pdev
->dev
, res
->start
, resource_size(res
));
314 gpio
= devm_kzalloc(dev
, sizeof(*gpio
), GFP_KERNEL
);
318 raw_spin_lock_init(&gpio
->spinlock
);
319 gpio
->io_base
= io_base
;
321 tqmx86_gpio_write(gpio
, (u8
)~TQMX86_DIR_INPUT_MASK
, TQMX86_GPIODD
);
324 * Reading the previous output state is not possible with TQMx86 hardware.
325 * Initialize all outputs to 0 to have a defined state that matches the
328 tqmx86_gpio_write(gpio
, 0, TQMX86_GPIOD
);
331 chip
->label
= "gpio-tqmx86";
332 chip
->owner
= THIS_MODULE
;
333 chip
->can_sleep
= false;
335 chip
->direction_input
= tqmx86_gpio_direction_input
;
336 chip
->direction_output
= tqmx86_gpio_direction_output
;
337 chip
->get_direction
= tqmx86_gpio_get_direction
;
338 chip
->get
= tqmx86_gpio_get
;
339 chip
->set
= tqmx86_gpio_set
;
340 chip
->ngpio
= TQMX86_NGPIO
;
341 chip
->parent
= pdev
->dev
.parent
;
343 pm_runtime_enable(&pdev
->dev
);
348 /* Mask all interrupts */
349 tqmx86_gpio_write(gpio
, 0, TQMX86_GPIIC
);
351 /* Clear all pending interrupts */
352 irq_status
= tqmx86_gpio_read(gpio
, TQMX86_GPIIS
);
353 tqmx86_gpio_write(gpio
, irq_status
, TQMX86_GPIIS
);
356 gpio_irq_chip_set_chip(girq
, &tqmx86_gpio_irq_chip
);
357 girq
->parent_handler
= tqmx86_gpio_irq_handler
;
358 girq
->num_parents
= 1;
359 girq
->parents
= devm_kcalloc(&pdev
->dev
, 1,
360 sizeof(*girq
->parents
),
362 if (!girq
->parents
) {
366 girq
->parents
[0] = irq
;
367 girq
->default_type
= IRQ_TYPE_NONE
;
368 girq
->handler
= handle_simple_irq
;
369 girq
->init_valid_mask
= tqmx86_init_irq_valid_mask
;
371 irq_domain_set_pm_device(girq
->domain
, dev
);
374 ret
= devm_gpiochip_add_data(dev
, chip
, gpio
);
376 dev_err(dev
, "Could not register GPIO chip\n");
380 dev_info(dev
, "GPIO functionality initialized with %d pins\n",
386 pm_runtime_disable(&pdev
->dev
);
391 static struct platform_driver tqmx86_gpio_driver
= {
393 .name
= "tqmx86-gpio",
394 .pm
= &tqmx86_gpio_dev_pm_ops
,
396 .probe
= tqmx86_gpio_probe
,
399 module_platform_driver(tqmx86_gpio_driver
);
401 MODULE_DESCRIPTION("TQMx86 PLD GPIO Driver");
402 MODULE_AUTHOR("Andrew Lunn <andrew@lunn.ch>");
403 MODULE_LICENSE("GPL");
404 MODULE_ALIAS("platform:tqmx86-gpio");