xref: /linux/drivers/gpio/gpio-idio-16.c (revision 09b1704f5b02c18dd02b21343530463fcfc92c54)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * GPIO library for the ACCES IDIO-16 family
4  * Copyright (C) 2022 William Breathitt Gray
5  */
6 
7 #define DEFAULT_SYMBOL_NAMESPACE "GPIO_IDIO_16"
8 
9 #include <linux/bitmap.h>
10 #include <linux/bits.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/export.h>
14 #include <linux/gpio/regmap.h>
15 #include <linux/module.h>
16 #include <linux/regmap.h>
17 #include <linux/types.h>
18 
19 #include "gpio-idio-16.h"
20 
21 #define IDIO_16_DAT_BASE 0x0
22 #define IDIO_16_OUT_BASE IDIO_16_DAT_BASE
23 #define IDIO_16_IN_BASE (IDIO_16_DAT_BASE + 1)
24 #define IDIO_16_CLEAR_INTERRUPT 0x1
25 #define IDIO_16_ENABLE_IRQ 0x2
26 #define IDIO_16_DEACTIVATE_INPUT_FILTERS 0x3
27 #define IDIO_16_DISABLE_IRQ IDIO_16_ENABLE_IRQ
28 #define IDIO_16_INTERRUPT_STATUS 0x6
29 
30 #define IDIO_16_NGPIO 32
31 #define IDIO_16_NGPIO_PER_REG 8
32 #define IDIO_16_REG_STRIDE 4
33 
34 struct idio_16_data {
35 	struct regmap *map;
36 	unsigned int irq_mask;
37 };
38 
39 static int idio_16_handle_mask_sync(const int index, const unsigned int mask_buf_def,
40 				    const unsigned int mask_buf, void *const irq_drv_data)
41 {
42 	struct idio_16_data *const data = irq_drv_data;
43 	const unsigned int prev_mask = data->irq_mask;
44 	int err;
45 	unsigned int val;
46 
47 	/* exit early if no change since the previous mask */
48 	if (mask_buf == prev_mask)
49 		return 0;
50 
51 	/* remember the current mask for the next mask sync */
52 	data->irq_mask = mask_buf;
53 
54 	/* if all previously masked, enable interrupts when unmasking */
55 	if (prev_mask == mask_buf_def) {
56 		err = regmap_write(data->map, IDIO_16_CLEAR_INTERRUPT, 0x00);
57 		if (err)
58 			return err;
59 		return regmap_read(data->map, IDIO_16_ENABLE_IRQ, &val);
60 	}
61 
62 	/* if all are currently masked, disable interrupts */
63 	if (mask_buf == mask_buf_def)
64 		return regmap_write(data->map, IDIO_16_DISABLE_IRQ, 0x00);
65 
66 	return 0;
67 }
68 
69 static int idio_16_reg_mask_xlate(struct gpio_regmap *const gpio, const unsigned int base,
70 				  const unsigned int offset, unsigned int *const reg,
71 				  unsigned int *const mask)
72 {
73 	unsigned int stride;
74 
75 	/* Input lines start at GPIO 16 */
76 	if (offset < 16) {
77 		stride = offset / IDIO_16_NGPIO_PER_REG;
78 		*reg = IDIO_16_OUT_BASE + stride * IDIO_16_REG_STRIDE;
79 	} else {
80 		stride = (offset - 16) / IDIO_16_NGPIO_PER_REG;
81 		*reg = IDIO_16_IN_BASE + stride * IDIO_16_REG_STRIDE;
82 	}
83 
84 	*mask = BIT(offset % IDIO_16_NGPIO_PER_REG);
85 
86 	return 0;
87 }
88 
89 static const char *idio_16_names[IDIO_16_NGPIO] = {
90 	"OUT0", "OUT1", "OUT2", "OUT3", "OUT4", "OUT5", "OUT6", "OUT7",
91 	"OUT8", "OUT9", "OUT10", "OUT11", "OUT12", "OUT13", "OUT14", "OUT15",
92 	"IIN0", "IIN1", "IIN2", "IIN3", "IIN4", "IIN5", "IIN6", "IIN7",
93 	"IIN8", "IIN9", "IIN10", "IIN11", "IIN12", "IIN13", "IIN14", "IIN15",
94 };
95 
96 /**
97  * devm_idio_16_regmap_register - Register an IDIO-16 GPIO device
98  * @dev:	device that is registering this IDIO-16 GPIO device
99  * @config:	configuration for idio_16_regmap_config
100  *
101  * Registers an IDIO-16 GPIO device. Returns 0 on success and negative error number on failure.
102  */
103 int devm_idio_16_regmap_register(struct device *const dev,
104 				 const struct idio_16_regmap_config *const config)
105 {
106 	struct gpio_regmap_config gpio_config = {};
107 	int err;
108 	struct idio_16_data *data;
109 	struct regmap_irq_chip *chip;
110 	struct regmap_irq_chip_data *chip_data;
111 	DECLARE_BITMAP(fixed_direction_output, IDIO_16_NGPIO);
112 
113 	if (!config->parent)
114 		return -EINVAL;
115 
116 	if (!config->map)
117 		return -EINVAL;
118 
119 	if (!config->regmap_irqs)
120 		return -EINVAL;
121 
122 	data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
123 	if (!data)
124 		return -ENOMEM;
125 	data->map = config->map;
126 
127 	chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
128 	if (!chip)
129 		return -ENOMEM;
130 
131 	chip->name = dev_name(dev);
132 	chip->status_base = IDIO_16_INTERRUPT_STATUS;
133 	chip->mask_base = IDIO_16_ENABLE_IRQ;
134 	chip->ack_base = IDIO_16_CLEAR_INTERRUPT;
135 	chip->no_status = config->no_status;
136 	chip->num_regs = 1;
137 	chip->irqs = config->regmap_irqs;
138 	chip->num_irqs = config->num_regmap_irqs;
139 	chip->handle_mask_sync = idio_16_handle_mask_sync;
140 	chip->irq_drv_data = data;
141 
142 	/* Disable IRQ to prevent spurious interrupts before we're ready */
143 	err = regmap_write(data->map, IDIO_16_DISABLE_IRQ, 0x00);
144 	if (err)
145 		return err;
146 
147 	err = devm_regmap_add_irq_chip(dev, data->map, config->irq, 0, 0, chip, &chip_data);
148 	if (err)
149 		return dev_err_probe(dev, err, "IRQ registration failed\n");
150 
151 	if (config->filters) {
152 		/* Deactivate input filters */
153 		err = regmap_write(data->map, IDIO_16_DEACTIVATE_INPUT_FILTERS, 0x00);
154 		if (err)
155 			return err;
156 	}
157 
158 	gpio_config.parent = config->parent;
159 	gpio_config.regmap = data->map;
160 	gpio_config.ngpio = IDIO_16_NGPIO;
161 	gpio_config.names = idio_16_names;
162 	gpio_config.reg_dat_base = GPIO_REGMAP_ADDR(IDIO_16_DAT_BASE);
163 	gpio_config.reg_set_base = GPIO_REGMAP_ADDR(IDIO_16_DAT_BASE);
164 	gpio_config.ngpio_per_reg = IDIO_16_NGPIO_PER_REG;
165 	gpio_config.reg_stride = IDIO_16_REG_STRIDE;
166 	gpio_config.irq_domain = regmap_irq_get_domain(chip_data);
167 	gpio_config.reg_mask_xlate = idio_16_reg_mask_xlate;
168 
169 	bitmap_from_u64(fixed_direction_output, GENMASK_U64(15, 0));
170 	gpio_config.fixed_direction_output = fixed_direction_output;
171 
172 	return PTR_ERR_OR_ZERO(devm_gpio_regmap_register(dev, &gpio_config));
173 }
174 EXPORT_SYMBOL_GPL(devm_idio_16_regmap_register);
175 
176 MODULE_AUTHOR("William Breathitt Gray");
177 MODULE_DESCRIPTION("ACCES IDIO-16 GPIO Library");
178 MODULE_LICENSE("GPL");
179