1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * GPIO driver for the Diamond Systems GPIO-MM
4  * Copyright (C) 2016 William Breathitt Gray
5  *
6  * This driver supports the following Diamond Systems devices: GPIO-MM and
7  * GPIO-MM-12.
8  */
9 #include <linux/device.h>
10 #include <linux/errno.h>
11 #include <linux/ioport.h>
12 #include <linux/isa.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/regmap.h>
17 #include <linux/types.h>
18 
19 #include "gpio-i8255.h"
20 
21 MODULE_IMPORT_NS(I8255);
22 
23 #define GPIOMM_EXTENT 8
24 #define MAX_NUM_GPIOMM max_num_isa_dev(GPIOMM_EXTENT)
25 
26 static unsigned int base[MAX_NUM_GPIOMM];
27 static unsigned int num_gpiomm;
28 module_param_hw_array(base, uint, ioport, &num_gpiomm, 0);
29 MODULE_PARM_DESC(base, "Diamond Systems GPIO-MM base addresses");
30 
31 #define GPIOMM_NUM_PPI 2
32 
33 static const struct regmap_range gpiomm_volatile_ranges[] = {
34 	i8255_volatile_regmap_range(0x0), i8255_volatile_regmap_range(0x4),
35 };
36 static const struct regmap_access_table gpiomm_volatile_table = {
37 	.yes_ranges = gpiomm_volatile_ranges,
38 	.n_yes_ranges = ARRAY_SIZE(gpiomm_volatile_ranges),
39 };
40 static const struct regmap_config gpiomm_regmap_config = {
41 	.reg_bits = 8,
42 	.reg_stride = 1,
43 	.val_bits = 8,
44 	.io_port = true,
45 	.max_register = 0x7,
46 	.volatile_table = &gpiomm_volatile_table,
47 	.cache_type = REGCACHE_FLAT,
48 };
49 
50 #define GPIOMM_NGPIO 48
51 static const char *gpiomm_names[GPIOMM_NGPIO] = {
52 	"Port 1A0", "Port 1A1", "Port 1A2", "Port 1A3", "Port 1A4", "Port 1A5",
53 	"Port 1A6", "Port 1A7", "Port 1B0", "Port 1B1", "Port 1B2", "Port 1B3",
54 	"Port 1B4", "Port 1B5", "Port 1B6", "Port 1B7", "Port 1C0", "Port 1C1",
55 	"Port 1C2", "Port 1C3", "Port 1C4", "Port 1C5", "Port 1C6", "Port 1C7",
56 	"Port 2A0", "Port 2A1", "Port 2A2", "Port 2A3", "Port 2A4", "Port 2A5",
57 	"Port 2A6", "Port 2A7", "Port 2B0", "Port 2B1", "Port 2B2", "Port 2B3",
58 	"Port 2B4", "Port 2B5", "Port 2B6", "Port 2B7", "Port 2C0", "Port 2C1",
59 	"Port 2C2", "Port 2C3", "Port 2C4", "Port 2C5", "Port 2C6", "Port 2C7",
60 };
61 
gpiomm_probe(struct device * dev,unsigned int id)62 static int gpiomm_probe(struct device *dev, unsigned int id)
63 {
64 	const char *const name = dev_name(dev);
65 	struct i8255_regmap_config config = {};
66 	void __iomem *regs;
67 
68 	if (!devm_request_region(dev, base[id], GPIOMM_EXTENT, name)) {
69 		dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
70 			base[id], base[id] + GPIOMM_EXTENT);
71 		return -EBUSY;
72 	}
73 
74 	regs = devm_ioport_map(dev, base[id], GPIOMM_EXTENT);
75 	if (!regs)
76 		return -ENOMEM;
77 
78 	config.map = devm_regmap_init_mmio(dev, regs, &gpiomm_regmap_config);
79 	if (IS_ERR(config.map))
80 		return dev_err_probe(dev, PTR_ERR(config.map),
81 				     "Unable to initialize register map\n");
82 
83 	config.parent = dev;
84 	config.num_ppi = GPIOMM_NUM_PPI;
85 	config.names = gpiomm_names;
86 
87 	return devm_i8255_regmap_register(dev, &config);
88 }
89 
90 static struct isa_driver gpiomm_driver = {
91 	.probe = gpiomm_probe,
92 	.driver = {
93 		.name = "gpio-mm"
94 	},
95 };
96 
97 module_isa_driver(gpiomm_driver, num_gpiomm);
98 
99 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
100 MODULE_DESCRIPTION("Diamond Systems GPIO-MM GPIO driver");
101 MODULE_LICENSE("GPL v2");
102