1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * GPIO driver for the ACCES 104-IDIO-16 family
4  * Copyright (C) 2015 William Breathitt Gray
5  *
6  * This driver supports the following ACCES devices: 104-IDIO-16,
7  * 104-IDIO-16E, 104-IDO-16, 104-IDIO-8, 104-IDIO-8E, and 104-IDO-8.
8  */
9 #include <linux/bits.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/ioport.h>
13 #include <linux/irq.h>
14 #include <linux/isa.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/regmap.h>
19 #include <linux/types.h>
20 
21 #include "gpio-idio-16.h"
22 
23 #define IDIO_16_EXTENT 8
24 #define MAX_NUM_IDIO_16 max_num_isa_dev(IDIO_16_EXTENT)
25 
26 static unsigned int base[MAX_NUM_IDIO_16];
27 static unsigned int num_idio_16;
28 module_param_hw_array(base, uint, ioport, &num_idio_16, 0);
29 MODULE_PARM_DESC(base, "ACCES 104-IDIO-16 base addresses");
30 
31 static unsigned int irq[MAX_NUM_IDIO_16];
32 static unsigned int num_irq;
33 module_param_hw_array(irq, uint, irq, &num_irq, 0);
34 MODULE_PARM_DESC(irq, "ACCES 104-IDIO-16 interrupt line numbers");
35 
36 static const struct regmap_range idio_16_wr_ranges[] = {
37 	regmap_reg_range(0x0, 0x2), regmap_reg_range(0x4, 0x4),
38 };
39 static const struct regmap_range idio_16_rd_ranges[] = {
40 	regmap_reg_range(0x1, 0x2), regmap_reg_range(0x5, 0x5),
41 };
42 static const struct regmap_range idio_16_precious_ranges[] = {
43 	regmap_reg_range(0x2, 0x2),
44 };
45 static const struct regmap_access_table idio_16_wr_table = {
46 	.yes_ranges = idio_16_wr_ranges,
47 	.n_yes_ranges = ARRAY_SIZE(idio_16_wr_ranges),
48 };
49 static const struct regmap_access_table idio_16_rd_table = {
50 	.yes_ranges = idio_16_rd_ranges,
51 	.n_yes_ranges = ARRAY_SIZE(idio_16_rd_ranges),
52 };
53 static const struct regmap_access_table idio_16_precious_table = {
54 	.yes_ranges = idio_16_precious_ranges,
55 	.n_yes_ranges = ARRAY_SIZE(idio_16_precious_ranges),
56 };
57 static const struct regmap_config idio_16_regmap_config = {
58 	.reg_bits = 8,
59 	.reg_stride = 1,
60 	.val_bits = 8,
61 	.io_port = true,
62 	.wr_table = &idio_16_wr_table,
63 	.rd_table = &idio_16_rd_table,
64 	.volatile_table = &idio_16_rd_table,
65 	.precious_table = &idio_16_precious_table,
66 	.cache_type = REGCACHE_FLAT,
67 	.use_raw_spinlock = true,
68 };
69 
70 /* Only input lines (GPIO 16-31) support interrupts */
71 #define IDIO_16_REGMAP_IRQ(_id)						\
72 	[16 + _id] = {							\
73 		.mask = BIT(_id),					\
74 		.type = { .types_supported = IRQ_TYPE_EDGE_BOTH },	\
75 	}
76 
77 static const struct regmap_irq idio_16_regmap_irqs[] = {
78 	IDIO_16_REGMAP_IRQ(0), IDIO_16_REGMAP_IRQ(1), IDIO_16_REGMAP_IRQ(2), /* 0-2 */
79 	IDIO_16_REGMAP_IRQ(3), IDIO_16_REGMAP_IRQ(4), IDIO_16_REGMAP_IRQ(5), /* 3-5 */
80 	IDIO_16_REGMAP_IRQ(6), IDIO_16_REGMAP_IRQ(7), IDIO_16_REGMAP_IRQ(8), /* 6-8 */
81 	IDIO_16_REGMAP_IRQ(9), IDIO_16_REGMAP_IRQ(10), IDIO_16_REGMAP_IRQ(11), /* 9-11 */
82 	IDIO_16_REGMAP_IRQ(12), IDIO_16_REGMAP_IRQ(13), IDIO_16_REGMAP_IRQ(14), /* 12-14 */
83 	IDIO_16_REGMAP_IRQ(15), /* 15 */
84 };
85 
idio_16_probe(struct device * dev,unsigned int id)86 static int idio_16_probe(struct device *dev, unsigned int id)
87 {
88 	const char *const name = dev_name(dev);
89 	struct idio_16_regmap_config config = {};
90 	void __iomem *regs;
91 	struct regmap *map;
92 
93 	if (!devm_request_region(dev, base[id], IDIO_16_EXTENT, name)) {
94 		dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
95 			base[id], base[id] + IDIO_16_EXTENT);
96 		return -EBUSY;
97 	}
98 
99 	regs = devm_ioport_map(dev, base[id], IDIO_16_EXTENT);
100 	if (!regs)
101 		return -ENOMEM;
102 
103 	map = devm_regmap_init_mmio(dev, regs, &idio_16_regmap_config);
104 	if (IS_ERR(map))
105 		return dev_err_probe(dev, PTR_ERR(map), "Unable to initialize register map\n");
106 
107 	config.parent = dev;
108 	config.map = map;
109 	config.regmap_irqs = idio_16_regmap_irqs;
110 	config.num_regmap_irqs = ARRAY_SIZE(idio_16_regmap_irqs);
111 	config.irq = irq[id];
112 	config.no_status = true;
113 
114 	return devm_idio_16_regmap_register(dev, &config);
115 }
116 
117 static struct isa_driver idio_16_driver = {
118 	.probe = idio_16_probe,
119 	.driver = {
120 		.name = "104-idio-16"
121 	},
122 };
123 
124 module_isa_driver_with_irq(idio_16_driver, num_idio_16, num_irq);
125 
126 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
127 MODULE_DESCRIPTION("ACCES 104-IDIO-16 GPIO driver");
128 MODULE_LICENSE("GPL v2");
129 MODULE_IMPORT_NS(GPIO_IDIO_16);
130