11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29c26df9bSWilliam Breathitt Gray /*
39c26df9bSWilliam Breathitt Gray * GPIO driver for the WinSystems WS16C48
49c26df9bSWilliam Breathitt Gray * Copyright (C) 2016 William Breathitt Gray
59c26df9bSWilliam Breathitt Gray */
60988ffa0SWilliam Breathitt Gray #include <linux/bitfield.h>
70988ffa0SWilliam Breathitt Gray #include <linux/bits.h>
89c26df9bSWilliam Breathitt Gray #include <linux/device.h>
90988ffa0SWilliam Breathitt Gray #include <linux/err.h>
100988ffa0SWilliam Breathitt Gray #include <linux/gpio/regmap.h>
110988ffa0SWilliam Breathitt Gray #include <linux/irq.h>
12cc736607SWilliam Breathitt Gray #include <linux/isa.h>
139c26df9bSWilliam Breathitt Gray #include <linux/kernel.h>
149c26df9bSWilliam Breathitt Gray #include <linux/module.h>
159c26df9bSWilliam Breathitt Gray #include <linux/moduleparam.h>
169c26df9bSWilliam Breathitt Gray #include <linux/spinlock.h>
170988ffa0SWilliam Breathitt Gray #include <linux/regmap.h>
182c05a0f2SWilliam Breathitt Gray #include <linux/types.h>
199c26df9bSWilliam Breathitt Gray
20*33f83d13SWilliam Breathitt Gray #define WS16C48_EXTENT 11
21cc736607SWilliam Breathitt Gray #define MAX_NUM_WS16C48 max_num_isa_dev(WS16C48_EXTENT)
22cc736607SWilliam Breathitt Gray
23cc736607SWilliam Breathitt Gray static unsigned int base[MAX_NUM_WS16C48];
24cc736607SWilliam Breathitt Gray static unsigned int num_ws16c48;
25d759f906SDavid Howells module_param_hw_array(base, uint, ioport, &num_ws16c48, 0);
26cc736607SWilliam Breathitt Gray MODULE_PARM_DESC(base, "WinSystems WS16C48 base addresses");
27cc736607SWilliam Breathitt Gray
28cc736607SWilliam Breathitt Gray static unsigned int irq[MAX_NUM_WS16C48];
29c95671a3SWilliam Breathitt Gray static unsigned int num_irq;
30c95671a3SWilliam Breathitt Gray module_param_hw_array(irq, uint, irq, &num_irq, 0);
31cc736607SWilliam Breathitt Gray MODULE_PARM_DESC(irq, "WinSystems WS16C48 interrupt line numbers");
329c26df9bSWilliam Breathitt Gray
330988ffa0SWilliam Breathitt Gray #define WS16C48_DAT_BASE 0x0
340988ffa0SWilliam Breathitt Gray #define WS16C48_PAGE_LOCK 0x7
350988ffa0SWilliam Breathitt Gray #define WS16C48_PAGE_BASE 0x8
360988ffa0SWilliam Breathitt Gray #define WS16C48_POL WS16C48_PAGE_BASE
370988ffa0SWilliam Breathitt Gray #define WS16C48_ENAB WS16C48_PAGE_BASE
380988ffa0SWilliam Breathitt Gray #define WS16C48_INT_ID WS16C48_PAGE_BASE
390988ffa0SWilliam Breathitt Gray
400988ffa0SWilliam Breathitt Gray #define PAGE_LOCK_PAGE_FIELD GENMASK(7, 6)
410988ffa0SWilliam Breathitt Gray #define POL_PAGE u8_encode_bits(1, PAGE_LOCK_PAGE_FIELD)
420988ffa0SWilliam Breathitt Gray #define ENAB_PAGE u8_encode_bits(2, PAGE_LOCK_PAGE_FIELD)
430988ffa0SWilliam Breathitt Gray #define INT_ID_PAGE u8_encode_bits(3, PAGE_LOCK_PAGE_FIELD)
440988ffa0SWilliam Breathitt Gray
450988ffa0SWilliam Breathitt Gray static const struct regmap_range ws16c48_wr_ranges[] = {
460988ffa0SWilliam Breathitt Gray regmap_reg_range(0x0, 0x5), regmap_reg_range(0x7, 0xA),
470988ffa0SWilliam Breathitt Gray };
480988ffa0SWilliam Breathitt Gray static const struct regmap_range ws16c48_rd_ranges[] = {
490988ffa0SWilliam Breathitt Gray regmap_reg_range(0x0, 0xA),
500988ffa0SWilliam Breathitt Gray };
510988ffa0SWilliam Breathitt Gray static const struct regmap_range ws16c48_volatile_ranges[] = {
520988ffa0SWilliam Breathitt Gray regmap_reg_range(0x0, 0x6), regmap_reg_range(0x8, 0xA),
530988ffa0SWilliam Breathitt Gray };
540988ffa0SWilliam Breathitt Gray static const struct regmap_access_table ws16c48_wr_table = {
550988ffa0SWilliam Breathitt Gray .yes_ranges = ws16c48_wr_ranges,
560988ffa0SWilliam Breathitt Gray .n_yes_ranges = ARRAY_SIZE(ws16c48_wr_ranges),
570988ffa0SWilliam Breathitt Gray };
580988ffa0SWilliam Breathitt Gray static const struct regmap_access_table ws16c48_rd_table = {
590988ffa0SWilliam Breathitt Gray .yes_ranges = ws16c48_rd_ranges,
600988ffa0SWilliam Breathitt Gray .n_yes_ranges = ARRAY_SIZE(ws16c48_rd_ranges),
610988ffa0SWilliam Breathitt Gray };
620988ffa0SWilliam Breathitt Gray static const struct regmap_access_table ws16c48_volatile_table = {
630988ffa0SWilliam Breathitt Gray .yes_ranges = ws16c48_volatile_ranges,
640988ffa0SWilliam Breathitt Gray .n_yes_ranges = ARRAY_SIZE(ws16c48_volatile_ranges),
650988ffa0SWilliam Breathitt Gray };
660988ffa0SWilliam Breathitt Gray static const struct regmap_config ws16c48_regmap_config = {
670988ffa0SWilliam Breathitt Gray .reg_bits = 8,
680988ffa0SWilliam Breathitt Gray .reg_stride = 1,
690988ffa0SWilliam Breathitt Gray .val_bits = 8,
700988ffa0SWilliam Breathitt Gray .io_port = true,
710988ffa0SWilliam Breathitt Gray .wr_table = &ws16c48_wr_table,
720988ffa0SWilliam Breathitt Gray .rd_table = &ws16c48_rd_table,
730988ffa0SWilliam Breathitt Gray .volatile_table = &ws16c48_volatile_table,
740988ffa0SWilliam Breathitt Gray .cache_type = REGCACHE_FLAT,
750988ffa0SWilliam Breathitt Gray .use_raw_spinlock = true,
760988ffa0SWilliam Breathitt Gray };
770988ffa0SWilliam Breathitt Gray
780988ffa0SWilliam Breathitt Gray #define WS16C48_NGPIO_PER_REG 8
790988ffa0SWilliam Breathitt Gray #define WS16C48_REGMAP_IRQ(_id) \
800988ffa0SWilliam Breathitt Gray [_id] = { \
810988ffa0SWilliam Breathitt Gray .reg_offset = (_id) / WS16C48_NGPIO_PER_REG, \
820988ffa0SWilliam Breathitt Gray .mask = BIT((_id) % WS16C48_NGPIO_PER_REG), \
830988ffa0SWilliam Breathitt Gray .type = { \
840988ffa0SWilliam Breathitt Gray .type_reg_offset = (_id) / WS16C48_NGPIO_PER_REG, \
850988ffa0SWilliam Breathitt Gray .types_supported = IRQ_TYPE_EDGE_BOTH, \
860988ffa0SWilliam Breathitt Gray }, \
870988ffa0SWilliam Breathitt Gray }
880988ffa0SWilliam Breathitt Gray
890988ffa0SWilliam Breathitt Gray /* Only the first 24 lines (Port 0-2) support interrupts */
900988ffa0SWilliam Breathitt Gray #define WS16C48_NUM_IRQS 24
910988ffa0SWilliam Breathitt Gray static const struct regmap_irq ws16c48_regmap_irqs[WS16C48_NUM_IRQS] = {
920988ffa0SWilliam Breathitt Gray WS16C48_REGMAP_IRQ(0), WS16C48_REGMAP_IRQ(1), WS16C48_REGMAP_IRQ(2), /* 0-2 */
930988ffa0SWilliam Breathitt Gray WS16C48_REGMAP_IRQ(3), WS16C48_REGMAP_IRQ(4), WS16C48_REGMAP_IRQ(5), /* 3-5 */
940988ffa0SWilliam Breathitt Gray WS16C48_REGMAP_IRQ(6), WS16C48_REGMAP_IRQ(7), WS16C48_REGMAP_IRQ(8), /* 6-8 */
950988ffa0SWilliam Breathitt Gray WS16C48_REGMAP_IRQ(9), WS16C48_REGMAP_IRQ(10), WS16C48_REGMAP_IRQ(11), /* 9-11 */
960988ffa0SWilliam Breathitt Gray WS16C48_REGMAP_IRQ(12), WS16C48_REGMAP_IRQ(13), WS16C48_REGMAP_IRQ(14), /* 12-14 */
970988ffa0SWilliam Breathitt Gray WS16C48_REGMAP_IRQ(15), WS16C48_REGMAP_IRQ(16), WS16C48_REGMAP_IRQ(17), /* 15-17 */
980988ffa0SWilliam Breathitt Gray WS16C48_REGMAP_IRQ(18), WS16C48_REGMAP_IRQ(19), WS16C48_REGMAP_IRQ(20), /* 18-20 */
990988ffa0SWilliam Breathitt Gray WS16C48_REGMAP_IRQ(21), WS16C48_REGMAP_IRQ(22), WS16C48_REGMAP_IRQ(23), /* 21-23 */
1002c05a0f2SWilliam Breathitt Gray };
1012c05a0f2SWilliam Breathitt Gray
1022c05a0f2SWilliam Breathitt Gray /**
1039c26df9bSWilliam Breathitt Gray * struct ws16c48_gpio - GPIO device private data structure
1040988ffa0SWilliam Breathitt Gray * @map: regmap for the device
1059c26df9bSWilliam Breathitt Gray * @lock: synchronization lock to prevent I/O race conditions
1069c26df9bSWilliam Breathitt Gray * @irq_mask: I/O bits affected by interrupts
1079c26df9bSWilliam Breathitt Gray */
1089c26df9bSWilliam Breathitt Gray struct ws16c48_gpio {
1090988ffa0SWilliam Breathitt Gray struct regmap *map;
110a0a584f0SJulia Cartwright raw_spinlock_t lock;
1110988ffa0SWilliam Breathitt Gray u8 irq_mask[WS16C48_NUM_IRQS / WS16C48_NGPIO_PER_REG];
1129c26df9bSWilliam Breathitt Gray };
1139c26df9bSWilliam Breathitt Gray
ws16c48_handle_pre_irq(void * const irq_drv_data)1140988ffa0SWilliam Breathitt Gray static int ws16c48_handle_pre_irq(void *const irq_drv_data) __acquires(&ws16c48gpio->lock)
1159c26df9bSWilliam Breathitt Gray {
1160988ffa0SWilliam Breathitt Gray struct ws16c48_gpio *const ws16c48gpio = irq_drv_data;
1179c26df9bSWilliam Breathitt Gray
1180988ffa0SWilliam Breathitt Gray /* Lock to prevent Page/Lock register change while we handle IRQ */
1190988ffa0SWilliam Breathitt Gray raw_spin_lock(&ws16c48gpio->lock);
1209c26df9bSWilliam Breathitt Gray
1219c26df9bSWilliam Breathitt Gray return 0;
1229c26df9bSWilliam Breathitt Gray }
1239c26df9bSWilliam Breathitt Gray
ws16c48_handle_post_irq(void * const irq_drv_data)1240988ffa0SWilliam Breathitt Gray static int ws16c48_handle_post_irq(void *const irq_drv_data) __releases(&ws16c48gpio->lock)
1259c26df9bSWilliam Breathitt Gray {
1260988ffa0SWilliam Breathitt Gray struct ws16c48_gpio *const ws16c48gpio = irq_drv_data;
1279c26df9bSWilliam Breathitt Gray
1280988ffa0SWilliam Breathitt Gray raw_spin_unlock(&ws16c48gpio->lock);
1299c26df9bSWilliam Breathitt Gray
1309c26df9bSWilliam Breathitt Gray return 0;
1319c26df9bSWilliam Breathitt Gray }
1329c26df9bSWilliam Breathitt Gray
ws16c48_handle_mask_sync(const int index,const unsigned int mask_buf_def,const unsigned int mask_buf,void * const irq_drv_data)1330988ffa0SWilliam Breathitt Gray static int ws16c48_handle_mask_sync(const int index, const unsigned int mask_buf_def,
1340988ffa0SWilliam Breathitt Gray const unsigned int mask_buf, void *const irq_drv_data)
1359c26df9bSWilliam Breathitt Gray {
1360988ffa0SWilliam Breathitt Gray struct ws16c48_gpio *const ws16c48gpio = irq_drv_data;
1379c26df9bSWilliam Breathitt Gray unsigned long flags;
1380988ffa0SWilliam Breathitt Gray int ret = 0;
1399c26df9bSWilliam Breathitt Gray
140a0a584f0SJulia Cartwright raw_spin_lock_irqsave(&ws16c48gpio->lock, flags);
1419c26df9bSWilliam Breathitt Gray
1420988ffa0SWilliam Breathitt Gray /* exit early if no change since the last mask sync */
1430988ffa0SWilliam Breathitt Gray if (mask_buf == ws16c48gpio->irq_mask[index])
1440988ffa0SWilliam Breathitt Gray goto exit_unlock;
1450988ffa0SWilliam Breathitt Gray ws16c48gpio->irq_mask[index] = mask_buf;
1469c26df9bSWilliam Breathitt Gray
1470988ffa0SWilliam Breathitt Gray ret = regmap_write(ws16c48gpio->map, WS16C48_PAGE_LOCK, ENAB_PAGE);
1480988ffa0SWilliam Breathitt Gray if (ret)
1490988ffa0SWilliam Breathitt Gray goto exit_unlock;
1509c26df9bSWilliam Breathitt Gray
1510988ffa0SWilliam Breathitt Gray /* Update ENAB register (inverted mask) */
1520988ffa0SWilliam Breathitt Gray ret = regmap_write(ws16c48gpio->map, WS16C48_ENAB + index, ~mask_buf);
1530988ffa0SWilliam Breathitt Gray if (ret)
1540988ffa0SWilliam Breathitt Gray goto exit_unlock;
1550988ffa0SWilliam Breathitt Gray
1560988ffa0SWilliam Breathitt Gray ret = regmap_write(ws16c48gpio->map, WS16C48_PAGE_LOCK, INT_ID_PAGE);
1570988ffa0SWilliam Breathitt Gray if (ret)
1580988ffa0SWilliam Breathitt Gray goto exit_unlock;
1590988ffa0SWilliam Breathitt Gray
1600988ffa0SWilliam Breathitt Gray exit_unlock:
161a0a584f0SJulia Cartwright raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
1629c26df9bSWilliam Breathitt Gray
1630988ffa0SWilliam Breathitt Gray return ret;
1649c26df9bSWilliam Breathitt Gray }
1659c26df9bSWilliam Breathitt Gray
ws16c48_set_type_config(unsigned int ** const buf,const unsigned int type,const struct regmap_irq * const irq_data,const int idx,void * const irq_drv_data)1660988ffa0SWilliam Breathitt Gray static int ws16c48_set_type_config(unsigned int **const buf, const unsigned int type,
1670988ffa0SWilliam Breathitt Gray const struct regmap_irq *const irq_data, const int idx,
1680988ffa0SWilliam Breathitt Gray void *const irq_drv_data)
169a8ff510dSWilliam Breathitt Gray {
1700988ffa0SWilliam Breathitt Gray struct ws16c48_gpio *const ws16c48gpio = irq_drv_data;
1710988ffa0SWilliam Breathitt Gray unsigned int polarity;
1729c26df9bSWilliam Breathitt Gray unsigned long flags;
1730988ffa0SWilliam Breathitt Gray int ret;
1749c26df9bSWilliam Breathitt Gray
1750988ffa0SWilliam Breathitt Gray switch (type) {
1769c26df9bSWilliam Breathitt Gray case IRQ_TYPE_EDGE_RISING:
1770988ffa0SWilliam Breathitt Gray polarity = irq_data->mask;
1789c26df9bSWilliam Breathitt Gray break;
1799c26df9bSWilliam Breathitt Gray case IRQ_TYPE_EDGE_FALLING:
1800988ffa0SWilliam Breathitt Gray polarity = 0;
1819c26df9bSWilliam Breathitt Gray break;
1829c26df9bSWilliam Breathitt Gray default:
1839c26df9bSWilliam Breathitt Gray return -EINVAL;
1849c26df9bSWilliam Breathitt Gray }
1859c26df9bSWilliam Breathitt Gray
1860988ffa0SWilliam Breathitt Gray raw_spin_lock_irqsave(&ws16c48gpio->lock, flags);
1872c05a0f2SWilliam Breathitt Gray
1880988ffa0SWilliam Breathitt Gray ret = regmap_write(ws16c48gpio->map, WS16C48_PAGE_LOCK, POL_PAGE);
1890988ffa0SWilliam Breathitt Gray if (ret)
1900988ffa0SWilliam Breathitt Gray goto exit_unlock;
1912c05a0f2SWilliam Breathitt Gray
1922c05a0f2SWilliam Breathitt Gray /* Set interrupt polarity */
1930988ffa0SWilliam Breathitt Gray ret = regmap_update_bits(ws16c48gpio->map, WS16C48_POL + idx, irq_data->mask, polarity);
1940988ffa0SWilliam Breathitt Gray if (ret)
1950988ffa0SWilliam Breathitt Gray goto exit_unlock;
1962c05a0f2SWilliam Breathitt Gray
1970988ffa0SWilliam Breathitt Gray ret = regmap_write(ws16c48gpio->map, WS16C48_PAGE_LOCK, INT_ID_PAGE);
1980988ffa0SWilliam Breathitt Gray if (ret)
1990988ffa0SWilliam Breathitt Gray goto exit_unlock;
2009c26df9bSWilliam Breathitt Gray
2010988ffa0SWilliam Breathitt Gray exit_unlock:
202a0a584f0SJulia Cartwright raw_spin_unlock_irqrestore(&ws16c48gpio->lock, flags);
2039c26df9bSWilliam Breathitt Gray
2040988ffa0SWilliam Breathitt Gray return ret;
2059c26df9bSWilliam Breathitt Gray }
2069c26df9bSWilliam Breathitt Gray
2075238f60fSWilliam Breathitt Gray #define WS16C48_NGPIO 48
2085238f60fSWilliam Breathitt Gray static const char *ws16c48_names[WS16C48_NGPIO] = {
2095238f60fSWilliam Breathitt Gray "Port 0 Bit 0", "Port 0 Bit 1", "Port 0 Bit 2", "Port 0 Bit 3",
2105238f60fSWilliam Breathitt Gray "Port 0 Bit 4", "Port 0 Bit 5", "Port 0 Bit 6", "Port 0 Bit 7",
2115238f60fSWilliam Breathitt Gray "Port 1 Bit 0", "Port 1 Bit 1", "Port 1 Bit 2", "Port 1 Bit 3",
2125238f60fSWilliam Breathitt Gray "Port 1 Bit 4", "Port 1 Bit 5", "Port 1 Bit 6", "Port 1 Bit 7",
2135238f60fSWilliam Breathitt Gray "Port 2 Bit 0", "Port 2 Bit 1", "Port 2 Bit 2", "Port 2 Bit 3",
2145238f60fSWilliam Breathitt Gray "Port 2 Bit 4", "Port 2 Bit 5", "Port 2 Bit 6", "Port 2 Bit 7",
2155238f60fSWilliam Breathitt Gray "Port 3 Bit 0", "Port 3 Bit 1", "Port 3 Bit 2", "Port 3 Bit 3",
2165238f60fSWilliam Breathitt Gray "Port 3 Bit 4", "Port 3 Bit 5", "Port 3 Bit 6", "Port 3 Bit 7",
2175238f60fSWilliam Breathitt Gray "Port 4 Bit 0", "Port 4 Bit 1", "Port 4 Bit 2", "Port 4 Bit 3",
2185238f60fSWilliam Breathitt Gray "Port 4 Bit 4", "Port 4 Bit 5", "Port 4 Bit 6", "Port 4 Bit 7",
2195238f60fSWilliam Breathitt Gray "Port 5 Bit 0", "Port 5 Bit 1", "Port 5 Bit 2", "Port 5 Bit 3",
2205238f60fSWilliam Breathitt Gray "Port 5 Bit 4", "Port 5 Bit 5", "Port 5 Bit 6", "Port 5 Bit 7"
2215238f60fSWilliam Breathitt Gray };
2225238f60fSWilliam Breathitt Gray
ws16c48_irq_init_hw(struct regmap * const map)2230988ffa0SWilliam Breathitt Gray static int ws16c48_irq_init_hw(struct regmap *const map)
224fceb7ab3SLinus Walleij {
2250988ffa0SWilliam Breathitt Gray int err;
226fceb7ab3SLinus Walleij
2270988ffa0SWilliam Breathitt Gray err = regmap_write(map, WS16C48_PAGE_LOCK, ENAB_PAGE);
2280988ffa0SWilliam Breathitt Gray if (err)
2290988ffa0SWilliam Breathitt Gray return err;
2302c05a0f2SWilliam Breathitt Gray
2312c05a0f2SWilliam Breathitt Gray /* Disable interrupts for all lines */
2320988ffa0SWilliam Breathitt Gray err = regmap_write(map, WS16C48_ENAB + 0, 0x00);
2330988ffa0SWilliam Breathitt Gray if (err)
2340988ffa0SWilliam Breathitt Gray return err;
2350988ffa0SWilliam Breathitt Gray err = regmap_write(map, WS16C48_ENAB + 1, 0x00);
2360988ffa0SWilliam Breathitt Gray if (err)
2370988ffa0SWilliam Breathitt Gray return err;
2380988ffa0SWilliam Breathitt Gray err = regmap_write(map, WS16C48_ENAB + 2, 0x00);
2390988ffa0SWilliam Breathitt Gray if (err)
2400988ffa0SWilliam Breathitt Gray return err;
2412c05a0f2SWilliam Breathitt Gray
2420988ffa0SWilliam Breathitt Gray return regmap_write(map, WS16C48_PAGE_LOCK, INT_ID_PAGE);
243fceb7ab3SLinus Walleij }
244fceb7ab3SLinus Walleij
ws16c48_probe(struct device * dev,unsigned int id)245cc736607SWilliam Breathitt Gray static int ws16c48_probe(struct device *dev, unsigned int id)
2469c26df9bSWilliam Breathitt Gray {
2479c26df9bSWilliam Breathitt Gray struct ws16c48_gpio *ws16c48gpio;
2489c26df9bSWilliam Breathitt Gray const char *const name = dev_name(dev);
2499c26df9bSWilliam Breathitt Gray int err;
2500988ffa0SWilliam Breathitt Gray struct gpio_regmap_config gpio_config = {};
2510988ffa0SWilliam Breathitt Gray void __iomem *regs;
2520988ffa0SWilliam Breathitt Gray struct regmap_irq_chip *chip;
2530988ffa0SWilliam Breathitt Gray struct regmap_irq_chip_data *chip_data;
2549c26df9bSWilliam Breathitt Gray
2559c26df9bSWilliam Breathitt Gray ws16c48gpio = devm_kzalloc(dev, sizeof(*ws16c48gpio), GFP_KERNEL);
2569c26df9bSWilliam Breathitt Gray if (!ws16c48gpio)
2579c26df9bSWilliam Breathitt Gray return -ENOMEM;
2589c26df9bSWilliam Breathitt Gray
259cc736607SWilliam Breathitt Gray if (!devm_request_region(dev, base[id], WS16C48_EXTENT, name)) {
260148ad68bSWilliam Breathitt Gray dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
261cc736607SWilliam Breathitt Gray base[id], base[id] + WS16C48_EXTENT);
262148ad68bSWilliam Breathitt Gray return -EBUSY;
2639c26df9bSWilliam Breathitt Gray }
2649c26df9bSWilliam Breathitt Gray
2650988ffa0SWilliam Breathitt Gray regs = devm_ioport_map(dev, base[id], WS16C48_EXTENT);
2660988ffa0SWilliam Breathitt Gray if (!regs)
2675561a2b0SWilliam Breathitt Gray return -ENOMEM;
2685561a2b0SWilliam Breathitt Gray
2690988ffa0SWilliam Breathitt Gray ws16c48gpio->map = devm_regmap_init_mmio(dev, regs, &ws16c48_regmap_config);
2700988ffa0SWilliam Breathitt Gray if (IS_ERR(ws16c48gpio->map))
2710988ffa0SWilliam Breathitt Gray return dev_err_probe(dev, PTR_ERR(ws16c48gpio->map),
2720988ffa0SWilliam Breathitt Gray "Unable to initialize register map\n");
2739c26df9bSWilliam Breathitt Gray
2740988ffa0SWilliam Breathitt Gray chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
2750988ffa0SWilliam Breathitt Gray if (!chip)
2760988ffa0SWilliam Breathitt Gray return -ENOMEM;
2770988ffa0SWilliam Breathitt Gray
2780988ffa0SWilliam Breathitt Gray chip->name = name;
2790988ffa0SWilliam Breathitt Gray chip->status_base = WS16C48_INT_ID;
2800988ffa0SWilliam Breathitt Gray chip->mask_base = WS16C48_ENAB;
2810988ffa0SWilliam Breathitt Gray chip->ack_base = WS16C48_INT_ID;
2820988ffa0SWilliam Breathitt Gray chip->num_regs = 3;
2830988ffa0SWilliam Breathitt Gray chip->irqs = ws16c48_regmap_irqs;
2840988ffa0SWilliam Breathitt Gray chip->num_irqs = ARRAY_SIZE(ws16c48_regmap_irqs);
2850988ffa0SWilliam Breathitt Gray chip->handle_pre_irq = ws16c48_handle_pre_irq;
2860988ffa0SWilliam Breathitt Gray chip->handle_post_irq = ws16c48_handle_post_irq;
2870988ffa0SWilliam Breathitt Gray chip->handle_mask_sync = ws16c48_handle_mask_sync;
2880988ffa0SWilliam Breathitt Gray chip->set_type_config = ws16c48_set_type_config;
2890988ffa0SWilliam Breathitt Gray chip->irq_drv_data = ws16c48gpio;
290fceb7ab3SLinus Walleij
291a0a584f0SJulia Cartwright raw_spin_lock_init(&ws16c48gpio->lock);
2929c26df9bSWilliam Breathitt Gray
2930988ffa0SWilliam Breathitt Gray /* Initialize to prevent spurious interrupts before we're ready */
2940988ffa0SWilliam Breathitt Gray err = ws16c48_irq_init_hw(ws16c48gpio->map);
2950988ffa0SWilliam Breathitt Gray if (err)
296148ad68bSWilliam Breathitt Gray return err;
2979c26df9bSWilliam Breathitt Gray
2980988ffa0SWilliam Breathitt Gray err = devm_regmap_add_irq_chip(dev, ws16c48gpio->map, irq[id], 0, 0, chip, &chip_data);
2990988ffa0SWilliam Breathitt Gray if (err)
3000988ffa0SWilliam Breathitt Gray return dev_err_probe(dev, err, "IRQ registration failed\n");
3019c26df9bSWilliam Breathitt Gray
3020988ffa0SWilliam Breathitt Gray gpio_config.parent = dev;
3030988ffa0SWilliam Breathitt Gray gpio_config.regmap = ws16c48gpio->map;
3040988ffa0SWilliam Breathitt Gray gpio_config.ngpio = WS16C48_NGPIO;
3050988ffa0SWilliam Breathitt Gray gpio_config.names = ws16c48_names;
3060988ffa0SWilliam Breathitt Gray gpio_config.reg_dat_base = GPIO_REGMAP_ADDR(WS16C48_DAT_BASE);
3070988ffa0SWilliam Breathitt Gray gpio_config.reg_set_base = GPIO_REGMAP_ADDR(WS16C48_DAT_BASE);
3080988ffa0SWilliam Breathitt Gray /* Setting a GPIO to 0 allows it to be used as an input */
3090988ffa0SWilliam Breathitt Gray gpio_config.reg_dir_out_base = GPIO_REGMAP_ADDR(WS16C48_DAT_BASE);
3100988ffa0SWilliam Breathitt Gray gpio_config.ngpio_per_reg = WS16C48_NGPIO_PER_REG;
3110988ffa0SWilliam Breathitt Gray gpio_config.irq_domain = regmap_irq_get_domain(chip_data);
3120988ffa0SWilliam Breathitt Gray
3130988ffa0SWilliam Breathitt Gray return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(dev, &gpio_config));
3149c26df9bSWilliam Breathitt Gray }
3159c26df9bSWilliam Breathitt Gray
316cc736607SWilliam Breathitt Gray static struct isa_driver ws16c48_driver = {
317cc736607SWilliam Breathitt Gray .probe = ws16c48_probe,
3189c26df9bSWilliam Breathitt Gray .driver = {
3199c26df9bSWilliam Breathitt Gray .name = "ws16c48"
3209c26df9bSWilliam Breathitt Gray },
3219c26df9bSWilliam Breathitt Gray };
3229c26df9bSWilliam Breathitt Gray
323c95671a3SWilliam Breathitt Gray module_isa_driver_with_irq(ws16c48_driver, num_ws16c48, num_irq);
3249c26df9bSWilliam Breathitt Gray
3259c26df9bSWilliam Breathitt Gray MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
3269c26df9bSWilliam Breathitt Gray MODULE_DESCRIPTION("WinSystems WS16C48 GPIO driver");
32722aeddb5SWilliam Breathitt Gray MODULE_LICENSE("GPL v2");
328