summaryrefslogtreecommitdiff
path: root/drivers/gpio/gpio-vf610.c
blob: bd6b30d181f0a31a0bb3de152492f3d416ebb06f (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
 * vf610 GPIO support through PORT and GPIO module
 *
 * Copyright (c) 2014 Toradex AG.
 *
 * Author: Stefan Agner <stefan@agner.ch>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/bitops.h>
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/ioport.h>
#include <linux/irq.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_irq.h>

#define VF610_GPIO_PER_PORT		32

struct vf610_gpio_port {
	struct gpio_chip gc;
	void __iomem *base;
	void __iomem *gpio_base;
	u8 irqc[VF610_GPIO_PER_PORT];
	struct platform_device *pdev_wkpu;
	s8 fsl_wakeup[VF610_GPIO_PER_PORT];
	int irq;
	u32 state;
};

#define GPIO_PDOR		0x00
#define GPIO_PSOR		0x04
#define GPIO_PCOR		0x08
#define GPIO_PTOR		0x0c
#define GPIO_PDIR		0x10

#define PORT_PCR(n)		((n) * 0x4)
#define PORT_PCR_IRQC_OFFSET	16

#define PORT_ISFR		0xa0
#define PORT_DFER		0xc0
#define PORT_DFCR		0xc4
#define PORT_DFWR		0xc8

#define PORT_INT_OFF		0x0
#define PORT_INT_LOGIC_ZERO	0x8
#define PORT_INT_RISING_EDGE	0x9
#define PORT_INT_FALLING_EDGE	0xa
#define PORT_INT_EITHER_EDGE	0xb
#define PORT_INT_LOGIC_ONE	0xc

#define WKPU_WISR		0x14
#define WKPU_IRER		0x18
#define WKPU_WRER		0x1c
#define WKPU_WIREER		0x28
#define WKPU_WIFEER		0x2c
#define WKPU_WIFER		0x30
#define WKPU_WIPUER		0x34

static struct irq_chip vf610_gpio_irq_chip;

static const struct of_device_id vf610_gpio_dt_ids[] = {
	{ .compatible = "fsl,vf610-gpio" },
	{ /* sentinel */ }
};

static const struct of_device_id vf610_wkpu_dt_ids[] = {
	{ .compatible = "fsl,vf610-wkpu" },
	{ /* sentinel */ }
};

static inline void vf610_gpio_writel(u32 val, void __iomem *reg)
{
	writel_relaxed(val, reg);
}

static inline u32 vf610_gpio_readl(void __iomem *reg)
{
	return readl_relaxed(reg);
}

static int vf610_gpio_request(struct gpio_chip *chip, unsigned offset)
{
	return pinctrl_request_gpio(chip->base + offset);
}

static void vf610_gpio_free(struct gpio_chip *chip, unsigned offset)
{
	pinctrl_free_gpio(chip->base + offset);
}

static int vf610_gpio_get(struct gpio_chip *gc, unsigned int gpio)
{
	struct vf610_gpio_port *port =
		container_of(gc, struct vf610_gpio_port, gc);

	return !!(vf610_gpio_readl(port->gpio_base + GPIO_PDIR) & BIT(gpio));
}

static void vf610_gpio_set(struct gpio_chip *gc, unsigned int gpio, int val)
{
	struct vf610_gpio_port *port =
		container_of(gc, struct vf610_gpio_port, gc);
	unsigned long mask = BIT(gpio);

	if (val)
		vf610_gpio_writel(mask, port->gpio_base + GPIO_PSOR);
	else
		vf610_gpio_writel(mask, port->gpio_base + GPIO_PCOR);
}

static int vf610_gpio_direction_input(struct gpio_chip *chip, unsigned gpio)
{
	return pinctrl_gpio_direction_input(chip->base + gpio);
}

static int vf610_gpio_direction_output(struct gpio_chip *chip, unsigned gpio,
				       int value)
{
	vf610_gpio_set(chip, gpio, value);

	return pinctrl_gpio_direction_output(chip->base + gpio);
}

static void vf610_gpio_irq_handler(u32 irq, struct irq_desc *desc)
{
	struct vf610_gpio_port *port = irq_get_handler_data(irq);
	struct irq_chip *chip = irq_desc_get_chip(desc);
	int pin;
	unsigned long irq_isfr;

	chained_irq_enter(chip, desc);

	irq_isfr = vf610_gpio_readl(port->base + PORT_ISFR);

	for_each_set_bit(pin, &irq_isfr, VF610_GPIO_PER_PORT) {
		vf610_gpio_writel(BIT(pin), port->base + PORT_ISFR);

		generic_handle_irq(irq_find_mapping(port->gc.irqdomain, pin));
	}

	chained_irq_exit(chip, desc);
}

static void vf610_gpio_irq_ack(struct irq_data *d)
{
	struct vf610_gpio_port *port = irq_data_get_irq_chip_data(d);
	int gpio = d->hwirq;

	vf610_gpio_writel(BIT(gpio), port->base + PORT_ISFR);
}

static int vf610_gpio_irq_set_type(struct irq_data *d, u32 type)
{
	struct vf610_gpio_port *port = irq_data_get_irq_chip_data(d);
	s8 wkpu_gpio = port->fsl_wakeup[d->hwirq];
	u8 irqc;

	if (wkpu_gpio >= 0) {
		void __iomem *base = platform_get_drvdata(port->pdev_wkpu);
		u32 wireer, wifeer;
		u32 mask = 1 << wkpu_gpio;

		wireer = vf610_gpio_readl(base + WKPU_WIREER) & ~mask;
		wifeer = vf610_gpio_readl(base + WKPU_WIFEER) & ~mask;

		if (type & IRQ_TYPE_EDGE_RISING)
			wireer |= mask;
		if (type & IRQ_TYPE_EDGE_FALLING)
			wifeer |= mask;

		vf610_gpio_writel(wireer, base + WKPU_WIREER);
		vf610_gpio_writel(wifeer, base + WKPU_WIFEER);
	}

	switch (type) {
	case IRQ_TYPE_EDGE_RISING:
		irqc = PORT_INT_RISING_EDGE;
		break;
	case IRQ_TYPE_EDGE_FALLING:
		irqc = PORT_INT_FALLING_EDGE;
		break;
	case IRQ_TYPE_EDGE_BOTH:
		irqc = PORT_INT_EITHER_EDGE;
		break;
	case IRQ_TYPE_LEVEL_LOW:
		irqc = PORT_INT_LOGIC_ZERO;
		break;
	case IRQ_TYPE_LEVEL_HIGH:
		irqc = PORT_INT_LOGIC_ONE;
		break;
	default:
		return -EINVAL;
	}

	port->irqc[d->hwirq] = irqc;

	if (type & IRQ_TYPE_LEVEL_MASK)
		__irq_set_handler_locked(d->irq, handle_level_irq);
	else
		__irq_set_handler_locked(d->irq, handle_edge_irq);

	return 0;
}

static void vf610_gpio_irq_mask(struct irq_data *d)
{
	struct vf610_gpio_port *port = irq_data_get_irq_chip_data(d);
	void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq);

	vf610_gpio_writel(0, pcr_base);
}

static void vf610_gpio_irq_unmask(struct irq_data *d)
{
	struct vf610_gpio_port *port = irq_data_get_irq_chip_data(d);
	void __iomem *pcr_base = port->base + PORT_PCR(d->hwirq);

	vf610_gpio_writel(port->irqc[d->hwirq] << PORT_PCR_IRQC_OFFSET,
			  pcr_base);
}

static int vf610_gpio_irq_set_wake(struct irq_data *d, u32 enable)
{
	struct vf610_gpio_port *port = irq_data_get_irq_chip_data(d);
	s8 wkpu_gpio = port->fsl_wakeup[d->hwirq];

	if (wkpu_gpio >= 0) {
		void __iomem *base = NULL;
		u32 wrer, irer;

		base = platform_get_drvdata(port->pdev_wkpu);

		/* WKPU wakeup flag for LPSTOPx modes...  */
		wrer = vf610_gpio_readl(base + WKPU_WRER);
		irer = vf610_gpio_readl(base + WKPU_IRER);

		if (enable) {
			wrer |= 1 << wkpu_gpio;
			irer |= 1 << wkpu_gpio;
		} else {
			wrer &= ~(1 << wkpu_gpio);
			irer &= ~(1 << wkpu_gpio);
		}

		vf610_gpio_writel(wrer, base + WKPU_WRER);
		vf610_gpio_writel(irer, base + WKPU_IRER);
	}

	if (enable)
		enable_irq_wake(port->irq);
	else
		disable_irq_wake(port->irq);

	return 0;
}

static struct irq_chip vf610_gpio_irq_chip = {
	.name		= "gpio-vf610",
	.irq_ack	= vf610_gpio_irq_ack,
	.irq_mask	= vf610_gpio_irq_mask,
	.irq_unmask	= vf610_gpio_irq_unmask,
	.irq_set_type	= vf610_gpio_irq_set_type,
	.irq_set_wake	= vf610_gpio_irq_set_wake,
};

static int __maybe_unused vf610_gpio_suspend(struct device *dev)
{
	struct vf610_gpio_port *port = dev_get_drvdata(dev);

	port->state = vf610_gpio_readl(port->gpio_base + GPIO_PDOR);

	/*
	 * There is no need to store Port state since we maintain the state
	 * alread in the irqc array
	 */

	return 0;
}

static int __maybe_unused vf610_gpio_resume(struct device *dev)
{
	struct vf610_gpio_port *port = dev_get_drvdata(dev);
	int i;

	vf610_gpio_writel(port->state, port->gpio_base + GPIO_PDOR);

	for (i = 0; i < port->gc.ngpio; i++) {
		u32 irqc = port->irqc[i] << PORT_PCR_IRQC_OFFSET;

		vf610_gpio_writel(irqc, port->base + PORT_PCR(i));
	}

	return 0;
}

static const struct dev_pm_ops vf610_gpio_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(vf610_gpio_suspend, vf610_gpio_resume)
};

static int vf610_gpio_wkpu(struct device_node *np, struct vf610_gpio_port *port)
{
	struct platform_device *pdev = NULL;
	struct of_phandle_args arg;
	int i, ret;

	for (i = 0; i < VF610_GPIO_PER_PORT; i++)
		port->fsl_wakeup[i] = -1;

	for (i = 0;;i++) {
		int gpioid, wakeupid, cnt;

		ret = of_parse_phandle_with_fixed_args(np, "fsl,gpio-wakeup",
							3, i, &arg);

		if (ret == -ENOENT)
			break;

		if (!pdev)
			pdev = of_find_device_by_node(arg.np);
		of_node_put(arg.np);
		if (!pdev)
			return -EPROBE_DEFER;

		gpioid = arg.args[0];
		wakeupid = arg.args[1];
		cnt = arg.args[2];

		while (cnt-- && gpioid < VF610_GPIO_PER_PORT)
			port->fsl_wakeup[gpioid++] = wakeupid++;
	}

	port->pdev_wkpu = pdev;

	return 0;
}

static int vf610_gpio_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct device_node *np = dev->of_node;
	struct vf610_gpio_port *port;
	struct resource *iores;
	struct gpio_chip *gc;
	int ret;

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

	iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	port->base = devm_ioremap_resource(dev, iores);
	if (IS_ERR(port->base))
		return PTR_ERR(port->base);

	iores = platform_get_resource(pdev, IORESOURCE_MEM, 1);
	port->gpio_base = devm_ioremap_resource(dev, iores);
	if (IS_ERR(port->gpio_base))
		return PTR_ERR(port->gpio_base);

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

	ret = vf610_gpio_wkpu(np, port);
	if (ret < 0)
		return ret;

	gc = &port->gc;
	gc->of_node = np;
	gc->dev = dev;
	gc->label = "vf610-gpio";
	gc->ngpio = VF610_GPIO_PER_PORT;
	gc->base = of_alias_get_id(np, "gpio") * VF610_GPIO_PER_PORT;

	gc->request = vf610_gpio_request;
	gc->free = vf610_gpio_free;
	gc->direction_input = vf610_gpio_direction_input;
	gc->get = vf610_gpio_get;
	gc->direction_output = vf610_gpio_direction_output;
	gc->set = vf610_gpio_set;

	ret = gpiochip_add(gc);
	if (ret < 0)
		return ret;

	/* Clear the interrupt status register for all GPIO's */
	vf610_gpio_writel(~0, port->base + PORT_ISFR);

	ret = gpiochip_irqchip_add(gc, &vf610_gpio_irq_chip, 0,
				   handle_edge_irq, IRQ_TYPE_NONE);
	if (ret) {
		dev_err(dev, "failed to add irqchip\n");
		gpiochip_remove(gc);
		return ret;
	}
	gpiochip_set_chained_irqchip(gc, &vf610_gpio_irq_chip, port->irq,
				     vf610_gpio_irq_handler);
	platform_set_drvdata(pdev, port);

	return 0;
}

static struct platform_driver vf610_gpio_driver = {
	.driver		= {
		.name	= "gpio-vf610",
		.pm = &vf610_gpio_pm_ops,
		.of_match_table = vf610_gpio_dt_ids,
	},
	.probe		= vf610_gpio_probe,
};

static int __init gpio_vf610_init(void)
{
	return platform_driver_register(&vf610_gpio_driver);
}
device_initcall(gpio_vf610_init);

static irqreturn_t vf610_wkpu_irq(int irq, void *data)
{
	void __iomem *base = data;
	u32 wisr;

	wisr = vf610_gpio_readl(base + WKPU_WISR);
	vf610_gpio_writel(wisr, base + WKPU_WISR);
	pr_debug("%s, WKPU interrupt received, flags %08x\n", __func__, wisr);

	return 0;
}

static int vf610_wkpu_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct resource *iores;
	void __iomem *base;
	int irq, err;

	iores = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	base = devm_ioremap_resource(dev, iores);

	if (IS_ERR(base))
		return PTR_ERR(base);

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

	err = devm_request_irq(dev, irq, vf610_wkpu_irq, 0, "wkpu-vf610", base);
	if (err) {
		dev_err(dev, "Error requesting IRQ!\n");
		return err;
	}

	platform_set_drvdata(pdev, base);

	return 0;
}

static struct platform_driver vf610_wkpu_driver = {
	.driver		= {
		.name	= "wkpu-vf610",
		.of_match_table = vf610_wkpu_dt_ids,
	},
	.probe		= vf610_wkpu_probe,
};

static int __init vf610_wkpu_init(void)
{
	return platform_driver_register(&vf610_wkpu_driver);
}
device_initcall(vf610_wkpu_init);

MODULE_AUTHOR("Stefan Agner <stefan@agner.ch>");
MODULE_DESCRIPTION("Freescale VF610 GPIO");
MODULE_LICENSE("GPL v2");