summaryrefslogtreecommitdiff
path: root/drivers/gpio/gpio-blzp1600.c
blob: 055cb296ae54758cdc206d36aa1df7b8377d32ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (C) 2019 VeriSilicon Limited.
 * Copyright (C) 2025 Blaize, Inc.
 */

#include <linux/errno.h>
#include <linux/gpio/driver.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/property.h>
#include <linux/slab.h>
#include <linux/spinlock.h>

#define GPIO_DIR_REG	0x00
#define GPIO_CTRL_REG	0x04
#define GPIO_SET_REG	0x08
#define GPIO_CLR_REG	0x0C
#define GPIO_ODATA_REG	0x10
#define GPIO_IDATA_REG	0x14
#define GPIO_IEN_REG	0x18
#define GPIO_IS_REG	0x1C
#define GPIO_IBE_REG	0x20
#define GPIO_IEV_REG	0x24
#define GPIO_RIS_REG	0x28
#define GPIO_IM_REG	0x2C
#define GPIO_MIS_REG	0x30
#define GPIO_IC_REG	0x34
#define GPIO_DB_REG	0x38
#define GPIO_DFG_REG	0x3C

#define DRIVER_NAME "blzp1600-gpio"

struct blzp1600_gpio {
	void __iomem *base;
	struct gpio_chip gc;
	int irq;
};

static inline struct blzp1600_gpio *get_blzp1600_gpio_from_irq_data(struct irq_data *d)
{
	return gpiochip_get_data(irq_data_get_irq_chip_data(d));
}

static inline struct blzp1600_gpio *get_blzp1600_gpio_from_irq_desc(struct irq_desc *d)
{
	return gpiochip_get_data(irq_desc_get_handler_data(d));
}

static inline u32 blzp1600_gpio_read(struct blzp1600_gpio *chip, unsigned int offset)
{
	return readl_relaxed(chip->base + offset);
}

static inline void blzp1600_gpio_write(struct blzp1600_gpio *chip, unsigned int offset, u32 val)
{
	writel_relaxed(val, chip->base + offset);
}

static inline void blzp1600_gpio_rmw(void __iomem *reg, u32 mask, bool set)
{
	u32 val = readl_relaxed(reg);

	if (set)
		val |= mask;
	else
		val &= ~mask;

	writel_relaxed(val, reg);
}

static void blzp1600_gpio_irq_mask(struct irq_data *d)
{
	struct blzp1600_gpio *chip = get_blzp1600_gpio_from_irq_data(d);

	guard(raw_spinlock_irqsave)(&chip->gc.bgpio_lock);
	blzp1600_gpio_rmw(chip->base + GPIO_IM_REG, BIT(d->hwirq), 1);
}

static void blzp1600_gpio_irq_unmask(struct irq_data *d)
{
	struct blzp1600_gpio *chip = get_blzp1600_gpio_from_irq_data(d);

	guard(raw_spinlock_irqsave)(&chip->gc.bgpio_lock);
	blzp1600_gpio_rmw(chip->base + GPIO_IM_REG, BIT(d->hwirq), 0);
}

static void blzp1600_gpio_irq_ack(struct irq_data *d)
{
	struct blzp1600_gpio *chip = get_blzp1600_gpio_from_irq_data(d);

	blzp1600_gpio_write(chip, GPIO_IC_REG, BIT(d->hwirq));
}

static void blzp1600_gpio_irq_enable(struct irq_data *d)
{
	struct blzp1600_gpio *chip = get_blzp1600_gpio_from_irq_data(d);

	gpiochip_enable_irq(&chip->gc, irqd_to_hwirq(d));

	guard(raw_spinlock_irqsave)(&chip->gc.bgpio_lock);
	blzp1600_gpio_rmw(chip->base + GPIO_DIR_REG, BIT(d->hwirq), 0);
	blzp1600_gpio_rmw(chip->base + GPIO_IEN_REG, BIT(d->hwirq), 1);
}

static void blzp1600_gpio_irq_disable(struct irq_data *d)
{
	struct blzp1600_gpio *chip = get_blzp1600_gpio_from_irq_data(d);

	guard(raw_spinlock_irqsave)(&chip->gc.bgpio_lock);
	blzp1600_gpio_rmw(chip->base + GPIO_IEN_REG, BIT(d->hwirq), 0);
	gpiochip_disable_irq(&chip->gc, irqd_to_hwirq(d));
}

static int blzp1600_gpio_irq_set_type(struct irq_data *d, u32 type)
{
	struct blzp1600_gpio *chip = get_blzp1600_gpio_from_irq_data(d);
	u32 edge_level, single_both, fall_rise;
	int mask = BIT(d->hwirq);

	guard(raw_spinlock_irqsave)(&chip->gc.bgpio_lock);
	edge_level = blzp1600_gpio_read(chip, GPIO_IS_REG);
	single_both = blzp1600_gpio_read(chip, GPIO_IBE_REG);
	fall_rise = blzp1600_gpio_read(chip, GPIO_IEV_REG);

	switch (type) {
	case IRQ_TYPE_EDGE_BOTH:
		edge_level &= ~mask;
		single_both |= mask;
		break;
	case IRQ_TYPE_EDGE_RISING:
		edge_level &= ~mask;
		single_both &= ~mask;
		fall_rise |= mask;
		break;
	case IRQ_TYPE_EDGE_FALLING:
		edge_level &= ~mask;
		single_both &= ~mask;
		fall_rise &= ~mask;
		break;
	case IRQ_TYPE_LEVEL_HIGH:
		edge_level |= mask;
		fall_rise |= mask;
		break;
	case IRQ_TYPE_LEVEL_LOW:
		edge_level |= mask;
		fall_rise &= ~mask;
		break;
	default:
		return -EINVAL;
	}

	blzp1600_gpio_write(chip, GPIO_IS_REG, edge_level);
	blzp1600_gpio_write(chip, GPIO_IBE_REG, single_both);
	blzp1600_gpio_write(chip, GPIO_IEV_REG, fall_rise);

	if (type & IRQ_TYPE_LEVEL_MASK)
		irq_set_handler_locked(d, handle_level_irq);
	else
		irq_set_handler_locked(d, handle_edge_irq);

	return 0;
}

static const struct irq_chip blzp1600_gpio_irqchip = {
	.name = DRIVER_NAME,
	.irq_ack = blzp1600_gpio_irq_ack,
	.irq_mask = blzp1600_gpio_irq_mask,
	.irq_unmask = blzp1600_gpio_irq_unmask,
	.irq_set_type = blzp1600_gpio_irq_set_type,
	.irq_enable = blzp1600_gpio_irq_enable,
	.irq_disable = blzp1600_gpio_irq_disable,
	.flags = IRQCHIP_IMMUTABLE | IRQCHIP_MASK_ON_SUSPEND,
	GPIOCHIP_IRQ_RESOURCE_HELPERS,
};

static void blzp1600_gpio_irqhandler(struct irq_desc *desc)
{
	struct blzp1600_gpio *gpio = get_blzp1600_gpio_from_irq_desc(desc);
	struct irq_chip *irqchip = irq_desc_get_chip(desc);
	unsigned long irq_status;
	int hwirq = 0;

	chained_irq_enter(irqchip, desc);
	irq_status = blzp1600_gpio_read(gpio, GPIO_RIS_REG);
	for_each_set_bit(hwirq, &irq_status, gpio->gc.ngpio)
		generic_handle_domain_irq(gpio->gc.irq.domain, hwirq);

	chained_irq_exit(irqchip, desc);
}

static int blzp1600_gpio_set_debounce(struct gpio_chip *gc, unsigned int offset,
				      unsigned int debounce)
{
	struct blzp1600_gpio *chip = gpiochip_get_data(gc);

	guard(raw_spinlock_irqsave)(&chip->gc.bgpio_lock);
	blzp1600_gpio_rmw(chip->base + GPIO_DB_REG, BIT(offset), debounce);

	return 0;
}

static int blzp1600_gpio_set_config(struct gpio_chip *gc, unsigned int offset, unsigned long config)
{
	u32 debounce;

	if (pinconf_to_config_param(config) != PIN_CONFIG_INPUT_DEBOUNCE)
		return -ENOTSUPP;

	debounce = pinconf_to_config_argument(config);
	return blzp1600_gpio_set_debounce(gc, offset, debounce);
}

static int blzp1600_gpio_probe(struct platform_device *pdev)
{
	struct blzp1600_gpio *chip;
	struct gpio_chip *gc;
	int ret;

	chip = devm_kzalloc(&pdev->dev, sizeof(*chip), GFP_KERNEL);
	if (!chip)
		return -ENOMEM;

	chip->base = devm_platform_ioremap_resource(pdev, 0);
	if (IS_ERR(chip->base))
		return PTR_ERR(chip->base);

	ret = bgpio_init(&chip->gc, &pdev->dev, 4, chip->base + GPIO_IDATA_REG,
			 chip->base + GPIO_SET_REG, chip->base + GPIO_CLR_REG,
			 chip->base + GPIO_DIR_REG, NULL, 0);
	if (ret)
		return dev_err_probe(&pdev->dev, ret, "Failed to register generic gpio\n");

	/* configure the gpio chip */
	gc = &chip->gc;
	gc->set_config = blzp1600_gpio_set_config;

	if (device_property_present(&pdev->dev, "interrupt-controller")) {
		struct gpio_irq_chip *girq;

		chip->irq = platform_get_irq(pdev, 0);
		if (chip->irq < 0)
			return chip->irq;

		girq = &gc->irq;
		gpio_irq_chip_set_chip(girq, &blzp1600_gpio_irqchip);
		girq->parent_handler = blzp1600_gpio_irqhandler;
		girq->num_parents = 1;
		girq->parents = devm_kcalloc(&pdev->dev, 1, sizeof(*girq->parents), GFP_KERNEL);
		if (!girq->parents)
			return -ENOMEM;

		girq->parents[0] = chip->irq;
		girq->default_type = IRQ_TYPE_NONE;
	}

	return devm_gpiochip_add_data(&pdev->dev, gc, chip);
}

static const struct of_device_id blzp1600_gpio_of_match[] = {
	{ .compatible = "blaize,blzp1600-gpio", },
	{ /* Sentinel */ },
};
MODULE_DEVICE_TABLE(of, blzp1600_gpio_of_match);

static struct platform_driver blzp1600_gpio_driver = {
	.driver		= {
		.name	= DRIVER_NAME,
		.of_match_table = blzp1600_gpio_of_match,
	},
	.probe		= blzp1600_gpio_probe,
};

module_platform_driver(blzp1600_gpio_driver);

MODULE_AUTHOR("Nikolaos Pasaloukos <nikolaos.pasaloukos@blaize.com>");
MODULE_DESCRIPTION("Blaize BLZP1600 GPIO driver");
MODULE_LICENSE("GPL");