Commit | Line | Data |
---|---|---|
1802d0be | 1 | // SPDX-License-Identifier: GPL-2.0-only |
02e74fc0 WBG |
2 | /* |
3 | * GPIO driver for the ACCES PCI-IDIO-16 | |
4 | * Copyright (C) 2017 William Breathitt Gray | |
02e74fc0 | 5 | */ |
e7f758fa | 6 | #include <linux/bits.h> |
02e74fc0 | 7 | #include <linux/device.h> |
73d8f3ef WBG |
8 | #include <linux/err.h> |
9 | #include <linux/irq.h> | |
02e74fc0 WBG |
10 | #include <linux/kernel.h> |
11 | #include <linux/module.h> | |
12 | #include <linux/pci.h> | |
73d8f3ef | 13 | #include <linux/regmap.h> |
02e74fc0 WBG |
14 | #include <linux/types.h> |
15 | ||
e7f758fa | 16 | #include "gpio-idio-16.h" |
02e74fc0 | 17 | |
73d8f3ef WBG |
18 | static const struct regmap_range idio_16_wr_ranges[] = { |
19 | regmap_reg_range(0x0, 0x2), regmap_reg_range(0x3, 0x4), | |
02e74fc0 | 20 | }; |
73d8f3ef WBG |
21 | static const struct regmap_range idio_16_rd_ranges[] = { |
22 | regmap_reg_range(0x1, 0x2), regmap_reg_range(0x5, 0x6), | |
02e74fc0 | 23 | }; |
73d8f3ef WBG |
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, | |
02e74fc0 WBG |
50 | }; |
51 | ||
73d8f3ef WBG |
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 | } | |
4530a840 | 58 | |
73d8f3ef WBG |
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 | }; | |
4530a840 | 67 | |
02e74fc0 WBG |
68 | static int idio_16_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
69 | { | |
70 | struct device *const dev = &pdev->dev; | |
02e74fc0 | 71 | int err; |
deab2b05 | 72 | const size_t pci_bar_index = 2; |
02e74fc0 | 73 | const char *const name = pci_name(pdev); |
73d8f3ef WBG |
74 | struct idio_16_regmap_config config = {}; |
75 | void __iomem *regs; | |
76 | struct regmap *map; | |
02e74fc0 WBG |
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 | ||
deab2b05 | 84 | err = pcim_iomap_regions(pdev, BIT(pci_bar_index), name); |
02e74fc0 WBG |
85 | if (err) { |
86 | dev_err(dev, "Unable to map PCI I/O addresses (%d)\n", err); | |
87 | return err; | |
88 | } | |
89 | ||
73d8f3ef | 90 | regs = pcim_iomap_table(pdev)[pci_bar_index]; |
02e74fc0 | 91 | |
73d8f3ef WBG |
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"); | |
02e74fc0 | 95 | |
73d8f3ef WBG |
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; | |
02e74fc0 | 102 | |
73d8f3ef | 103 | return devm_idio_16_regmap_register(dev, &config); |
02e74fc0 WBG |
104 | } |
105 | ||
106 | static const struct pci_device_id idio_16_pci_dev_id[] = { | |
fd254a23 | 107 | { PCI_DEVICE(0x494F, 0x0DC8) }, { 0 } |
02e74fc0 WBG |
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"); | |
e7f758fa | 122 | MODULE_IMPORT_NS(GPIO_IDIO_16); |