11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21ceacea2SWilliam Breathitt Gray /*
31ceacea2SWilliam Breathitt Gray * GPIO driver for the ACCES 104-IDIO-16 family
41ceacea2SWilliam Breathitt Gray * Copyright (C) 2015 William Breathitt Gray
51ceacea2SWilliam Breathitt Gray *
686ea8a95SWilliam Breathitt Gray * This driver supports the following ACCES devices: 104-IDIO-16,
786ea8a95SWilliam Breathitt Gray * 104-IDIO-16E, 104-IDO-16, 104-IDIO-8, 104-IDIO-8E, and 104-IDO-8.
81ceacea2SWilliam Breathitt Gray */
9*2c210c9aSWilliam Breathitt Gray #include <linux/bits.h>
101ceacea2SWilliam Breathitt Gray #include <linux/device.h>
11*2c210c9aSWilliam Breathitt Gray #include <linux/err.h>
121ceacea2SWilliam Breathitt Gray #include <linux/ioport.h>
13*2c210c9aSWilliam Breathitt Gray #include <linux/irq.h>
1486ea8a95SWilliam Breathitt Gray #include <linux/isa.h>
151ceacea2SWilliam Breathitt Gray #include <linux/kernel.h>
161ceacea2SWilliam Breathitt Gray #include <linux/module.h>
171ceacea2SWilliam Breathitt Gray #include <linux/moduleparam.h>
18*2c210c9aSWilliam Breathitt Gray #include <linux/regmap.h>
19cc442e4dSWilliam Breathitt Gray #include <linux/types.h>
201ceacea2SWilliam Breathitt Gray
21c4ec384cSWilliam Breathitt Gray #include "gpio-idio-16.h"
22c4ec384cSWilliam Breathitt Gray
2386ea8a95SWilliam Breathitt Gray #define IDIO_16_EXTENT 8
2486ea8a95SWilliam Breathitt Gray #define MAX_NUM_IDIO_16 max_num_isa_dev(IDIO_16_EXTENT)
2586ea8a95SWilliam Breathitt Gray
2686ea8a95SWilliam Breathitt Gray static unsigned int base[MAX_NUM_IDIO_16];
2786ea8a95SWilliam Breathitt Gray static unsigned int num_idio_16;
28d759f906SDavid Howells module_param_hw_array(base, uint, ioport, &num_idio_16, 0);
2986ea8a95SWilliam Breathitt Gray MODULE_PARM_DESC(base, "ACCES 104-IDIO-16 base addresses");
3086ea8a95SWilliam Breathitt Gray
3186ea8a95SWilliam Breathitt Gray static unsigned int irq[MAX_NUM_IDIO_16];
32c6074f3fSWilliam Breathitt Gray static unsigned int num_irq;
33c6074f3fSWilliam Breathitt Gray module_param_hw_array(irq, uint, irq, &num_irq, 0);
3486ea8a95SWilliam Breathitt Gray MODULE_PARM_DESC(irq, "ACCES 104-IDIO-16 interrupt line numbers");
351ceacea2SWilliam Breathitt Gray
36*2c210c9aSWilliam Breathitt Gray static const struct regmap_range idio_16_wr_ranges[] = {
37*2c210c9aSWilliam Breathitt Gray regmap_reg_range(0x0, 0x2), regmap_reg_range(0x4, 0x4),
38*2c210c9aSWilliam Breathitt Gray };
39*2c210c9aSWilliam Breathitt Gray static const struct regmap_range idio_16_rd_ranges[] = {
40*2c210c9aSWilliam Breathitt Gray regmap_reg_range(0x1, 0x2), regmap_reg_range(0x5, 0x5),
41*2c210c9aSWilliam Breathitt Gray };
42*2c210c9aSWilliam Breathitt Gray static const struct regmap_range idio_16_precious_ranges[] = {
43*2c210c9aSWilliam Breathitt Gray regmap_reg_range(0x2, 0x2),
44*2c210c9aSWilliam Breathitt Gray };
45*2c210c9aSWilliam Breathitt Gray static const struct regmap_access_table idio_16_wr_table = {
46*2c210c9aSWilliam Breathitt Gray .yes_ranges = idio_16_wr_ranges,
47*2c210c9aSWilliam Breathitt Gray .n_yes_ranges = ARRAY_SIZE(idio_16_wr_ranges),
48*2c210c9aSWilliam Breathitt Gray };
49*2c210c9aSWilliam Breathitt Gray static const struct regmap_access_table idio_16_rd_table = {
50*2c210c9aSWilliam Breathitt Gray .yes_ranges = idio_16_rd_ranges,
51*2c210c9aSWilliam Breathitt Gray .n_yes_ranges = ARRAY_SIZE(idio_16_rd_ranges),
52*2c210c9aSWilliam Breathitt Gray };
53*2c210c9aSWilliam Breathitt Gray static const struct regmap_access_table idio_16_precious_table = {
54*2c210c9aSWilliam Breathitt Gray .yes_ranges = idio_16_precious_ranges,
55*2c210c9aSWilliam Breathitt Gray .n_yes_ranges = ARRAY_SIZE(idio_16_precious_ranges),
56*2c210c9aSWilliam Breathitt Gray };
57*2c210c9aSWilliam Breathitt Gray static const struct regmap_config idio_16_regmap_config = {
58*2c210c9aSWilliam Breathitt Gray .reg_bits = 8,
59*2c210c9aSWilliam Breathitt Gray .reg_stride = 1,
60*2c210c9aSWilliam Breathitt Gray .val_bits = 8,
61*2c210c9aSWilliam Breathitt Gray .io_port = true,
62*2c210c9aSWilliam Breathitt Gray .wr_table = &idio_16_wr_table,
63*2c210c9aSWilliam Breathitt Gray .rd_table = &idio_16_rd_table,
64*2c210c9aSWilliam Breathitt Gray .volatile_table = &idio_16_rd_table,
65*2c210c9aSWilliam Breathitt Gray .precious_table = &idio_16_precious_table,
66*2c210c9aSWilliam Breathitt Gray .cache_type = REGCACHE_FLAT,
67*2c210c9aSWilliam Breathitt Gray .use_raw_spinlock = true,
681ceacea2SWilliam Breathitt Gray };
691ceacea2SWilliam Breathitt Gray
70*2c210c9aSWilliam Breathitt Gray /* Only input lines (GPIO 16-31) support interrupts */
71*2c210c9aSWilliam Breathitt Gray #define IDIO_16_REGMAP_IRQ(_id) \
72*2c210c9aSWilliam Breathitt Gray [16 + _id] = { \
73*2c210c9aSWilliam Breathitt Gray .mask = BIT(_id), \
74*2c210c9aSWilliam Breathitt Gray .type = { .types_supported = IRQ_TYPE_EDGE_BOTH }, \
751ceacea2SWilliam Breathitt Gray }
761ceacea2SWilliam Breathitt Gray
77*2c210c9aSWilliam Breathitt Gray static const struct regmap_irq idio_16_regmap_irqs[] = {
78*2c210c9aSWilliam Breathitt Gray IDIO_16_REGMAP_IRQ(0), IDIO_16_REGMAP_IRQ(1), IDIO_16_REGMAP_IRQ(2), /* 0-2 */
79*2c210c9aSWilliam Breathitt Gray IDIO_16_REGMAP_IRQ(3), IDIO_16_REGMAP_IRQ(4), IDIO_16_REGMAP_IRQ(5), /* 3-5 */
80*2c210c9aSWilliam Breathitt Gray IDIO_16_REGMAP_IRQ(6), IDIO_16_REGMAP_IRQ(7), IDIO_16_REGMAP_IRQ(8), /* 6-8 */
81*2c210c9aSWilliam Breathitt Gray IDIO_16_REGMAP_IRQ(9), IDIO_16_REGMAP_IRQ(10), IDIO_16_REGMAP_IRQ(11), /* 9-11 */
82*2c210c9aSWilliam Breathitt Gray IDIO_16_REGMAP_IRQ(12), IDIO_16_REGMAP_IRQ(13), IDIO_16_REGMAP_IRQ(14), /* 12-14 */
83*2c210c9aSWilliam Breathitt Gray IDIO_16_REGMAP_IRQ(15), /* 15 */
84a1184147SWilliam Breathitt Gray };
85a1184147SWilliam Breathitt Gray
idio_16_probe(struct device * dev,unsigned int id)8686ea8a95SWilliam Breathitt Gray static int idio_16_probe(struct device *dev, unsigned int id)
871ceacea2SWilliam Breathitt Gray {
886e0171b4SWilliam Breathitt Gray const char *const name = dev_name(dev);
89*2c210c9aSWilliam Breathitt Gray struct idio_16_regmap_config config = {};
90*2c210c9aSWilliam Breathitt Gray void __iomem *regs;
91*2c210c9aSWilliam Breathitt Gray struct regmap *map;
921ceacea2SWilliam Breathitt Gray
9386ea8a95SWilliam Breathitt Gray if (!devm_request_region(dev, base[id], IDIO_16_EXTENT, name)) {
94cb32389cSWilliam Breathitt Gray dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
9586ea8a95SWilliam Breathitt Gray base[id], base[id] + IDIO_16_EXTENT);
96cb32389cSWilliam Breathitt Gray return -EBUSY;
971ceacea2SWilliam Breathitt Gray }
981ceacea2SWilliam Breathitt Gray
99*2c210c9aSWilliam Breathitt Gray regs = devm_ioport_map(dev, base[id], IDIO_16_EXTENT);
100*2c210c9aSWilliam Breathitt Gray if (!regs)
101e0a574efSWilliam Breathitt Gray return -ENOMEM;
102e0a574efSWilliam Breathitt Gray
103*2c210c9aSWilliam Breathitt Gray map = devm_regmap_init_mmio(dev, regs, &idio_16_regmap_config);
104*2c210c9aSWilliam Breathitt Gray if (IS_ERR(map))
105*2c210c9aSWilliam Breathitt Gray return dev_err_probe(dev, PTR_ERR(map), "Unable to initialize register map\n");
106c4ec384cSWilliam Breathitt Gray
107*2c210c9aSWilliam Breathitt Gray config.parent = dev;
108*2c210c9aSWilliam Breathitt Gray config.map = map;
109*2c210c9aSWilliam Breathitt Gray config.regmap_irqs = idio_16_regmap_irqs;
110*2c210c9aSWilliam Breathitt Gray config.num_regmap_irqs = ARRAY_SIZE(idio_16_regmap_irqs);
111*2c210c9aSWilliam Breathitt Gray config.irq = irq[id];
112*2c210c9aSWilliam Breathitt Gray config.no_status = true;
1131ceacea2SWilliam Breathitt Gray
114*2c210c9aSWilliam Breathitt Gray return devm_idio_16_regmap_register(dev, &config);
1151ceacea2SWilliam Breathitt Gray }
1161ceacea2SWilliam Breathitt Gray
11786ea8a95SWilliam Breathitt Gray static struct isa_driver idio_16_driver = {
11886ea8a95SWilliam Breathitt Gray .probe = idio_16_probe,
1191ceacea2SWilliam Breathitt Gray .driver = {
1201ceacea2SWilliam Breathitt Gray .name = "104-idio-16"
1211ceacea2SWilliam Breathitt Gray },
1221ceacea2SWilliam Breathitt Gray };
1231ceacea2SWilliam Breathitt Gray
124c6074f3fSWilliam Breathitt Gray module_isa_driver_with_irq(idio_16_driver, num_idio_16, num_irq);
1251ceacea2SWilliam Breathitt Gray
1261ceacea2SWilliam Breathitt Gray MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
1271ceacea2SWilliam Breathitt Gray MODULE_DESCRIPTION("ACCES 104-IDIO-16 GPIO driver");
12822aeddb5SWilliam Breathitt Gray MODULE_LICENSE("GPL v2");
129c4ec384cSWilliam Breathitt Gray MODULE_IMPORT_NS(GPIO_IDIO_16);
130