1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * 74xx MMIO GPIO driver
5 * Copyright (C) 2014 Alexander Shiyan <shc_work@mail.ru>
8 #include <linux/bits.h>
10 #include <linux/gpio/driver.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/property.h>
16 #define MMIO_74XX_DIR_IN BIT(8)
17 #define MMIO_74XX_DIR_OUT BIT(9)
18 #define MMIO_74XX_BIT_CNT(x) ((x) & GENMASK(7, 0))
20 struct mmio_74xx_gpio_priv
{
25 static const struct of_device_id mmio_74xx_gpio_ids
[] = {
27 .compatible
= "ti,741g125",
28 .data
= (const void *)(MMIO_74XX_DIR_IN
| 1),
31 .compatible
= "ti,742g125",
32 .data
= (const void *)(MMIO_74XX_DIR_IN
| 2),
35 .compatible
= "ti,74125",
36 .data
= (const void *)(MMIO_74XX_DIR_IN
| 4),
39 .compatible
= "ti,74365",
40 .data
= (const void *)(MMIO_74XX_DIR_IN
| 6),
43 .compatible
= "ti,74244",
44 .data
= (const void *)(MMIO_74XX_DIR_IN
| 8),
47 .compatible
= "ti,741624",
48 .data
= (const void *)(MMIO_74XX_DIR_IN
| 16),
51 .compatible
= "ti,741g74",
52 .data
= (const void *)(MMIO_74XX_DIR_OUT
| 1),
55 .compatible
= "ti,7474",
56 .data
= (const void *)(MMIO_74XX_DIR_OUT
| 2),
59 .compatible
= "ti,74175",
60 .data
= (const void *)(MMIO_74XX_DIR_OUT
| 4),
63 .compatible
= "ti,74174",
64 .data
= (const void *)(MMIO_74XX_DIR_OUT
| 6),
67 .compatible
= "ti,74273",
68 .data
= (const void *)(MMIO_74XX_DIR_OUT
| 8),
71 .compatible
= "ti,7416374",
72 .data
= (const void *)(MMIO_74XX_DIR_OUT
| 16),
76 MODULE_DEVICE_TABLE(of
, mmio_74xx_gpio_ids
);
78 static int mmio_74xx_get_direction(struct gpio_chip
*gc
, unsigned offset
)
80 struct mmio_74xx_gpio_priv
*priv
= gpiochip_get_data(gc
);
82 if (priv
->flags
& MMIO_74XX_DIR_OUT
)
83 return GPIO_LINE_DIRECTION_OUT
;
85 return GPIO_LINE_DIRECTION_IN
;
88 static int mmio_74xx_dir_in(struct gpio_chip
*gc
, unsigned int gpio
)
90 struct mmio_74xx_gpio_priv
*priv
= gpiochip_get_data(gc
);
92 if (priv
->flags
& MMIO_74XX_DIR_IN
)
98 static int mmio_74xx_dir_out(struct gpio_chip
*gc
, unsigned int gpio
, int val
)
100 struct mmio_74xx_gpio_priv
*priv
= gpiochip_get_data(gc
);
102 if (priv
->flags
& MMIO_74XX_DIR_OUT
) {
103 gc
->set(gc
, gpio
, val
);
110 static int mmio_74xx_gpio_probe(struct platform_device
*pdev
)
112 struct mmio_74xx_gpio_priv
*priv
;
116 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
120 priv
->flags
= (uintptr_t)device_get_match_data(&pdev
->dev
);
122 dat
= devm_platform_ioremap_resource(pdev
, 0);
126 err
= bgpio_init(&priv
->gc
, &pdev
->dev
,
127 DIV_ROUND_UP(MMIO_74XX_BIT_CNT(priv
->flags
), 8),
128 dat
, NULL
, NULL
, NULL
, NULL
, 0);
132 priv
->gc
.direction_input
= mmio_74xx_dir_in
;
133 priv
->gc
.direction_output
= mmio_74xx_dir_out
;
134 priv
->gc
.get_direction
= mmio_74xx_get_direction
;
135 priv
->gc
.ngpio
= MMIO_74XX_BIT_CNT(priv
->flags
);
136 priv
->gc
.owner
= THIS_MODULE
;
138 return devm_gpiochip_add_data(&pdev
->dev
, &priv
->gc
, priv
);
141 static struct platform_driver mmio_74xx_gpio_driver
= {
143 .name
= "74xx-mmio-gpio",
144 .of_match_table
= mmio_74xx_gpio_ids
,
146 .probe
= mmio_74xx_gpio_probe
,
148 module_platform_driver(mmio_74xx_gpio_driver
);
150 MODULE_LICENSE("GPL");
151 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
152 MODULE_DESCRIPTION("74xx MMIO GPIO driver");