1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * GPIO driver for the ACCES PCI-IDIO-16
4 * Copyright (C) 2017 William Breathitt Gray
5 */
6 #include <linux/bits.h>
7 #include <linux/device.h>
8 #include <linux/err.h>
9 #include <linux/irq.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/regmap.h>
14 #include <linux/types.h>
15
16 #include "gpio-idio-16.h"
17
18 static const struct regmap_range idio_16_wr_ranges[] = {
19 regmap_reg_range(0x0, 0x2), regmap_reg_range(0x3, 0x4),
20 };
21 static const struct regmap_range idio_16_rd_ranges[] = {
22 regmap_reg_range(0x1, 0x2), regmap_reg_range(0x5, 0x6),
23 };
24 static const struct regmap_range idio_16_precious_ranges[] = {
25 regmap_reg_range(0x2, 0x2),
26 };
27 static const struct regmap_access_table idio_16_wr_table = {
28 .yes_ranges = idio_16_wr_ranges,
29 .n_yes_ranges = ARRAY_SIZE(idio_16_wr_ranges),
30 };
31 static const struct regmap_access_table idio_16_rd_table = {
32 .yes_ranges = idio_16_rd_ranges,
33 .n_yes_ranges = ARRAY_SIZE(idio_16_rd_ranges),
34 };
35 static const struct regmap_access_table idio_16_precious_table = {
36 .yes_ranges = idio_16_precious_ranges,
37 .n_yes_ranges = ARRAY_SIZE(idio_16_precious_ranges),
38 };
39 static const struct regmap_config idio_16_regmap_config = {
40 .reg_bits = 8,
41 .reg_stride = 1,
42 .val_bits = 8,
43 .io_port = true,
44 .wr_table = &idio_16_wr_table,
45 .rd_table = &idio_16_rd_table,
46 .volatile_table = &idio_16_rd_table,
47 .precious_table = &idio_16_precious_table,
48 .cache_type = REGCACHE_FLAT,
49 .use_raw_spinlock = true,
50 };
51
52 /* Only input lines (GPIO 16-31) support interrupts */
53 #define IDIO_16_REGMAP_IRQ(_id) \
54 [16 + _id] = { \
55 .mask = BIT(2), \
56 .type = { .types_supported = IRQ_TYPE_EDGE_BOTH }, \
57 }
58
59 static const struct regmap_irq idio_16_regmap_irqs[] = {
60 IDIO_16_REGMAP_IRQ(0), IDIO_16_REGMAP_IRQ(1), IDIO_16_REGMAP_IRQ(2), /* 0-2 */
61 IDIO_16_REGMAP_IRQ(3), IDIO_16_REGMAP_IRQ(4), IDIO_16_REGMAP_IRQ(5), /* 3-5 */
62 IDIO_16_REGMAP_IRQ(6), IDIO_16_REGMAP_IRQ(7), IDIO_16_REGMAP_IRQ(8), /* 6-8 */
63 IDIO_16_REGMAP_IRQ(9), IDIO_16_REGMAP_IRQ(10), IDIO_16_REGMAP_IRQ(11), /* 9-11 */
64 IDIO_16_REGMAP_IRQ(12), IDIO_16_REGMAP_IRQ(13), IDIO_16_REGMAP_IRQ(14), /* 12-14 */
65 IDIO_16_REGMAP_IRQ(15), /* 15 */
66 };
67
idio_16_probe(struct pci_dev * pdev,const struct pci_device_id * id)68 static int idio_16_probe(struct pci_dev *pdev, const struct pci_device_id *id)
69 {
70 struct device *const dev = &pdev->dev;
71 int err;
72 const size_t pci_bar_index = 2;
73 const char *const name = pci_name(pdev);
74 struct idio_16_regmap_config config = {};
75 void __iomem *regs;
76 struct regmap *map;
77
78 err = pcim_enable_device(pdev);
79 if (err) {
80 dev_err(dev, "Failed to enable PCI device (%d)\n", err);
81 return err;
82 }
83
84 err = pcim_iomap_regions(pdev, BIT(pci_bar_index), name);
85 if (err) {
86 dev_err(dev, "Unable to map PCI I/O addresses (%d)\n", err);
87 return err;
88 }
89
90 regs = pcim_iomap_table(pdev)[pci_bar_index];
91
92 map = devm_regmap_init_mmio(dev, regs, &idio_16_regmap_config);
93 if (IS_ERR(map))
94 return dev_err_probe(dev, PTR_ERR(map), "Unable to initialize register map\n");
95
96 config.parent = dev;
97 config.map = map;
98 config.regmap_irqs = idio_16_regmap_irqs;
99 config.num_regmap_irqs = ARRAY_SIZE(idio_16_regmap_irqs);
100 config.irq = pdev->irq;
101 config.filters = true;
102
103 return devm_idio_16_regmap_register(dev, &config);
104 }
105
106 static const struct pci_device_id idio_16_pci_dev_id[] = {
107 { PCI_DEVICE(0x494F, 0x0DC8) }, { 0 }
108 };
109 MODULE_DEVICE_TABLE(pci, idio_16_pci_dev_id);
110
111 static struct pci_driver idio_16_driver = {
112 .name = "pci-idio-16",
113 .id_table = idio_16_pci_dev_id,
114 .probe = idio_16_probe
115 };
116
117 module_pci_driver(idio_16_driver);
118
119 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
120 MODULE_DESCRIPTION("ACCES PCI-IDIO-16 GPIO driver");
121 MODULE_LICENSE("GPL v2");
122 MODULE_IMPORT_NS(GPIO_IDIO_16);
123