1 // SPDX-License-Identifier: GPL-2.0-only
3 * GPIO driver for the ACCES PCI-IDIO-16
4 * Copyright (C) 2017 William Breathitt Gray
6 #include <linux/bits.h>
7 #include <linux/device.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>
16 #include "gpio-idio-16.h"
18 static const struct regmap_range idio_16_wr_ranges
[] = {
19 regmap_reg_range(0x0, 0x2), regmap_reg_range(0x3, 0x4),
21 static const struct regmap_range idio_16_rd_ranges
[] = {
22 regmap_reg_range(0x1, 0x2), regmap_reg_range(0x5, 0x6),
24 static const struct regmap_range idio_16_precious_ranges
[] = {
25 regmap_reg_range(0x2, 0x2),
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
),
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
),
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
),
39 static const struct regmap_config idio_16_regmap_config
= {
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,
52 /* Only input lines (GPIO 16-31) support interrupts */
53 #define IDIO_16_REGMAP_IRQ(_id) \
56 .type = { .types_supported = IRQ_TYPE_EDGE_BOTH }, \
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 */
68 static int idio_16_probe(struct pci_dev
*pdev
, const struct pci_device_id
*id
)
70 struct device
*const dev
= &pdev
->dev
;
72 const size_t pci_bar_index
= 2;
73 const char *const name
= pci_name(pdev
);
74 struct idio_16_regmap_config config
= {};
78 err
= pcim_enable_device(pdev
);
80 dev_err(dev
, "Failed to enable PCI device (%d)\n", err
);
84 err
= pcim_iomap_regions(pdev
, BIT(pci_bar_index
), name
);
86 dev_err(dev
, "Unable to map PCI I/O addresses (%d)\n", err
);
90 regs
= pcim_iomap_table(pdev
)[pci_bar_index
];
92 map
= devm_regmap_init_mmio(dev
, regs
, &idio_16_regmap_config
);
94 return dev_err_probe(dev
, PTR_ERR(map
), "Unable to initialize register map\n");
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;
103 return devm_idio_16_regmap_register(dev
, &config
);
106 static const struct pci_device_id idio_16_pci_dev_id
[] = {
107 { PCI_DEVICE(0x494F, 0x0DC8) }, { 0 }
109 MODULE_DEVICE_TABLE(pci
, idio_16_pci_dev_id
);
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
117 module_pci_driver(idio_16_driver
);
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
);