gpio-timberdale.c 6.82 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
Grant Likely's avatar
Grant Likely committed
3
 * Timberdale FPGA GPIO driver
4
 * Author: Mocean Laboratories
5 6 7 8 9 10 11
 * Copyright (c) 2009 Intel Corporation
 */

/* Supports:
 * Timberdale FPGA GPIO
 */

12
#include <linux/init.h>
13
#include <linux/gpio/driver.h>
14
#include <linux/platform_device.h>
David Miller's avatar
David Miller committed
15
#include <linux/irq.h>
16 17 18
#include <linux/io.h>
#include <linux/timb_gpio.h>
#include <linux/interrupt.h>
19
#include <linux/slab.h>
20 21 22 23 24 25 26 27 28 29 30

#define DRIVER_NAME "timb-gpio"

#define TGPIOVAL	0x00
#define TGPIODIR	0x04
#define TGPIO_IER	0x08
#define TGPIO_ISR	0x0c
#define TGPIO_IPR	0x10
#define TGPIO_ICR	0x14
#define TGPIO_FLR	0x18
#define TGPIO_LVR	0x1c
31 32
#define TGPIO_VER	0x20
#define TGPIO_BFLR	0x24
33 34 35 36 37 38

struct timbgpio {
	void __iomem		*membase;
	spinlock_t		lock; /* mutual exclusion */
	struct gpio_chip	gpio;
	int			irq_base;
39
	unsigned long		last_ier;
40 41 42 43 44
};

static int timbgpio_update_bit(struct gpio_chip *gpio, unsigned index,
	unsigned offset, bool enabled)
{
45
	struct timbgpio *tgpio = gpiochip_get_data(gpio);
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
	u32 reg;

	spin_lock(&tgpio->lock);
	reg = ioread32(tgpio->membase + offset);

	if (enabled)
		reg |= (1 << index);
	else
		reg &= ~(1 << index);

	iowrite32(reg, tgpio->membase + offset);
	spin_unlock(&tgpio->lock);

	return 0;
}

static int timbgpio_gpio_direction_input(struct gpio_chip *gpio, unsigned nr)
{
	return timbgpio_update_bit(gpio, nr, TGPIODIR, true);
}

static int timbgpio_gpio_get(struct gpio_chip *gpio, unsigned nr)
{
69
	struct timbgpio *tgpio = gpiochip_get_data(gpio);
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
	u32 value;

	value = ioread32(tgpio->membase + TGPIOVAL);
	return (value & (1 << nr)) ? 1 : 0;
}

static int timbgpio_gpio_direction_output(struct gpio_chip *gpio,
						unsigned nr, int val)
{
	return timbgpio_update_bit(gpio, nr, TGPIODIR, false);
}

static void timbgpio_gpio_set(struct gpio_chip *gpio,
				unsigned nr, int val)
{
	timbgpio_update_bit(gpio, nr, TGPIOVAL, val != 0);
}

static int timbgpio_to_irq(struct gpio_chip *gpio, unsigned offset)
{
90
	struct timbgpio *tgpio = gpiochip_get_data(gpio);
91 92 93 94 95 96 97 98 99 100

	if (tgpio->irq_base <= 0)
		return -EINVAL;

	return tgpio->irq_base + offset;
}

/*
 * GPIO IRQ
 */
101
static void timbgpio_irq_disable(struct irq_data *d)
102
{
103 104
	struct timbgpio *tgpio = irq_data_get_irq_chip_data(d);
	int offset = d->irq - tgpio->irq_base;
105
	unsigned long flags;
106

107
	spin_lock_irqsave(&tgpio->lock, flags);
108
	tgpio->last_ier &= ~(1UL << offset);
109 110
	iowrite32(tgpio->last_ier, tgpio->membase + TGPIO_IER);
	spin_unlock_irqrestore(&tgpio->lock, flags);
111 112
}

113
static void timbgpio_irq_enable(struct irq_data *d)
114
{
115 116
	struct timbgpio *tgpio = irq_data_get_irq_chip_data(d);
	int offset = d->irq - tgpio->irq_base;
117
	unsigned long flags;
118

119
	spin_lock_irqsave(&tgpio->lock, flags);
120
	tgpio->last_ier |= 1UL << offset;
121 122
	iowrite32(tgpio->last_ier, tgpio->membase + TGPIO_IER);
	spin_unlock_irqrestore(&tgpio->lock, flags);
123 124
}

125
static int timbgpio_irq_type(struct irq_data *d, unsigned trigger)
126
{
127 128
	struct timbgpio *tgpio = irq_data_get_irq_chip_data(d);
	int offset = d->irq - tgpio->irq_base;
129
	unsigned long flags;
130 131
	u32 lvr, flr, bflr = 0;
	u32 ver;
132
	int ret = 0;
133 134 135 136

	if (offset < 0 || offset > tgpio->gpio.ngpio)
		return -EINVAL;

137 138
	ver = ioread32(tgpio->membase + TGPIO_VER);

139 140 141 142
	spin_lock_irqsave(&tgpio->lock, flags);

	lvr = ioread32(tgpio->membase + TGPIO_LVR);
	flr = ioread32(tgpio->membase + TGPIO_FLR);
143 144
	if (ver > 2)
		bflr = ioread32(tgpio->membase + TGPIO_BFLR);
145 146

	if (trigger & (IRQ_TYPE_LEVEL_HIGH | IRQ_TYPE_LEVEL_LOW)) {
147
		bflr &= ~(1 << offset);
148 149 150 151 152 153 154
		flr &= ~(1 << offset);
		if (trigger & IRQ_TYPE_LEVEL_HIGH)
			lvr |= 1 << offset;
		else
			lvr &= ~(1 << offset);
	}

155
	if ((trigger & IRQ_TYPE_EDGE_BOTH) == IRQ_TYPE_EDGE_BOTH) {
156 157 158
		if (ver < 3) {
			ret = -EINVAL;
			goto out;
159
		} else {
160 161 162 163 164
			flr |= 1 << offset;
			bflr |= 1 << offset;
		}
	} else {
		bflr &= ~(1 << offset);
165 166 167
		flr |= 1 << offset;
		if (trigger & IRQ_TYPE_EDGE_FALLING)
			lvr &= ~(1 << offset);
168 169
		else
			lvr |= 1 << offset;
170 171 172 173
	}

	iowrite32(lvr, tgpio->membase + TGPIO_LVR);
	iowrite32(flr, tgpio->membase + TGPIO_FLR);
174 175 176
	if (ver > 2)
		iowrite32(bflr, tgpio->membase + TGPIO_BFLR);

177 178
	iowrite32(1 << offset, tgpio->membase + TGPIO_ICR);

179 180 181
out:
	spin_unlock_irqrestore(&tgpio->lock, flags);
	return ret;
182 183
}

184
static void timbgpio_irq(struct irq_desc *desc)
185
{
186 187
	struct timbgpio *tgpio = irq_desc_get_handler_data(desc);
	struct irq_data *data = irq_desc_get_irq_data(desc);
188 189 190
	unsigned long ipr;
	int offset;

191
	data->chip->irq_ack(data);
192 193 194
	ipr = ioread32(tgpio->membase + TGPIO_IPR);
	iowrite32(ipr, tgpio->membase + TGPIO_ICR);

195 196 197 198 199 200
	/*
	 * Some versions of the hardware trash the IER register if more than
	 * one interrupt is received simultaneously.
	 */
	iowrite32(0, tgpio->membase + TGPIO_IER);

201
	for_each_set_bit(offset, &ipr, tgpio->gpio.ngpio)
202
		generic_handle_irq(timbgpio_to_irq(&tgpio->gpio, offset));
203 204

	iowrite32(tgpio->last_ier, tgpio->membase + TGPIO_IER);
205 206 207 208
}

static struct irq_chip timbgpio_irqchip = {
	.name		= "GPIO",
209 210 211
	.irq_enable	= timbgpio_irq_enable,
	.irq_disable	= timbgpio_irq_disable,
	.irq_set_type	= timbgpio_irq_type,
212 213
};

214
static int timbgpio_probe(struct platform_device *pdev)
215 216
{
	int err, i;
217
	struct device *dev = &pdev->dev;
218 219
	struct gpio_chip *gc;
	struct timbgpio *tgpio;
220
	struct timbgpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
221 222 223
	int irq = platform_get_irq(pdev, 0);

	if (!pdata || pdata->nr_pins > 32) {
224 225
		dev_err(dev, "Invalid platform data\n");
		return -EINVAL;
226 227
	}

228
	tgpio = devm_kzalloc(dev, sizeof(*tgpio), GFP_KERNEL);
229
	if (!tgpio)
230
		return -EINVAL;
231

232 233 234 235
	tgpio->irq_base = pdata->irq_base;

	spin_lock_init(&tgpio->lock);

236
	tgpio->membase = devm_platform_ioremap_resource(pdev, 0);
237 238
	if (IS_ERR(tgpio->membase))
		return PTR_ERR(tgpio->membase);
239 240 241 242 243

	gc = &tgpio->gpio;

	gc->label = dev_name(&pdev->dev);
	gc->owner = THIS_MODULE;
244
	gc->parent = &pdev->dev;
245 246 247 248 249 250 251 252
	gc->direction_input = timbgpio_gpio_direction_input;
	gc->get = timbgpio_gpio_get;
	gc->direction_output = timbgpio_gpio_direction_output;
	gc->set = timbgpio_gpio_set;
	gc->to_irq = (irq >= 0 && tgpio->irq_base > 0) ? timbgpio_to_irq : NULL;
	gc->dbg_show = NULL;
	gc->base = pdata->gpio_base;
	gc->ngpio = pdata->nr_pins;
253
	gc->can_sleep = false;
254

255
	err = devm_gpiochip_add_data(&pdev->dev, gc, tgpio);
256
	if (err)
257
		return err;
258 259 260 261 262 263 264 265 266 267

	platform_set_drvdata(pdev, tgpio);

	/* make sure to disable interrupts */
	iowrite32(0x0, tgpio->membase + TGPIO_IER);

	if (irq < 0 || tgpio->irq_base <= 0)
		return 0;

	for (i = 0; i < pdata->nr_pins; i++) {
268 269
		irq_set_chip_and_handler(tgpio->irq_base + i,
			&timbgpio_irqchip, handle_simple_irq);
270
		irq_set_chip_data(tgpio->irq_base + i, tgpio);
271
		irq_clear_status_flags(tgpio->irq_base + i, IRQ_NOREQUEST | IRQ_NOPROBE);
272 273
	}

274
	irq_set_chained_handler_and_data(irq, timbgpio_irq, tgpio);
275 276 277 278 279 280

	return 0;
}

static struct platform_driver timbgpio_platform_driver = {
	.driver = {
281 282
		.name			= DRIVER_NAME,
		.suppress_bind_attrs	= true,
283 284 285 286 287 288
	},
	.probe		= timbgpio_probe,
};

/*--------------------------------------------------------------------------*/

289
builtin_platform_driver(timbgpio_platform_driver);