summaryrefslogtreecommitdiffstats
path: root/configs/platform-energymicro-efm32gg-dk3750/patches/linux-3.12-rc4/0015-gpio-new-driver-for-Energy-Micro-s-GPIO-component.patch
blob: a33add0e9986e2903df3e65c8ad9829488536f7b (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
From: =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= <u.kleine-koenig@pengutronix.de>
Date: Tue, 12 Feb 2013 23:31:27 +0100
Subject: [PATCH] gpio: new driver for Energy Micro's GPIO component

---
 drivers/gpio/Kconfig      |   4 +
 drivers/gpio/Makefile     |   1 +
 drivers/gpio/gpio-efm32.c | 366 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 371 insertions(+)
 create mode 100644 drivers/gpio/gpio-efm32.c

diff --git a/drivers/gpio/Kconfig b/drivers/gpio/Kconfig
index b6ed304..2a3c5af 100644
--- a/drivers/gpio/Kconfig
+++ b/drivers/gpio/Kconfig
@@ -127,6 +127,10 @@ config GPIO_IT8761E
 	help
 	  Say yes here to support GPIO functionality of IT8761E super I/O chip.
 
+config GPIO_EFM32
+	tristate "Energy Micro EFM32 GPIO support"
+	depends on ARCH_EFM32
+
 config GPIO_EM
 	tristate "Emma Mobile GPIO"
 	depends on ARM
diff --git a/drivers/gpio/Makefile b/drivers/gpio/Makefile
index 98e23eb..9b66f5f 100644
--- a/drivers/gpio/Makefile
+++ b/drivers/gpio/Makefile
@@ -22,6 +22,7 @@ obj-$(CONFIG_GPIO_CS5535)	+= gpio-cs5535.o
 obj-$(CONFIG_GPIO_DA9052)	+= gpio-da9052.o
 obj-$(CONFIG_GPIO_DA9055)	+= gpio-da9055.o
 obj-$(CONFIG_ARCH_DAVINCI)	+= gpio-davinci.o
+obj-$(CONFIG_GPIO_EFM32)	+= gpio-efm32.o
 obj-$(CONFIG_GPIO_EM)		+= gpio-em.o
 obj-$(CONFIG_GPIO_EP93XX)	+= gpio-ep93xx.o
 obj-$(CONFIG_GPIO_F7188X)	+= gpio-f7188x.o
diff --git a/drivers/gpio/gpio-efm32.c b/drivers/gpio/gpio-efm32.c
new file mode 100644
index 0000000..01addc7
--- /dev/null
+++ b/drivers/gpio/gpio-efm32.c
@@ -0,0 +1,366 @@
+/*
+ * Copyright (C) 2013 Pengutronix
+ * Uwe Kleine-Koenig <u.kleine-koenig@pengutronix.de>
+ *
+ * This program is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License version 2 as published by the
+ * Free Software Foundation.
+ *
+ * TODO:
+ *  - disable clk in suspend iff no irq is enabled to wake the system
+ */
+#include <linux/platform_device.h>
+#include <linux/device.h>
+#include <linux/of.h>
+#include <linux/of_device.h>
+#include <linux/of_address.h>
+#include <linux/irqdomain.h>
+#include <linux/interrupt.h>
+#include <linux/gfp.h>
+#include <linux/module.h>
+#include <linux/irq.h>
+#include <linux/clk.h>
+
+#include <asm/gpio.h>
+#include <asm/io.h>
+
+#define DRIVER_NAME	"efm32-gpio"
+
+#define GPIO_Px_MODEL(p)	(0x024 * (p) + 0x004)
+#define GPIO_Px_MODEH(p)	(0x024 * (p) + 0x008)
+#define GPIO_Px_DOUT(p)		(0x024 * (p) + 0x00c)
+#define GPIO_Px_DOUTSET(p)	(0x024 * (p) + 0x010)
+#define GPIO_Px_DOUTCLR(p)	(0x024 * (p) + 0x014)
+#define GPIO_Px_DIN(p)		(0x024 * (p) + 0x01c)
+#define GPIO_EXTIPSELL		0x100
+#define GPIO_EXTIPSELH		0x104
+#define GPIO_IEN		0x110
+#define GPIO_IF			0x114
+#define GPIO_IFC		0x11c
+
+struct efm32_gpio_ddata {
+	void __iomem *base;
+	spinlock_t lock;
+	struct clk *clk;
+	unsigned int irq_even, irq_odd;
+	struct irq_domain *irq_domain;
+	struct gpio_chip chip;
+	unsigned assigned_irqpins;
+};
+
+#define to_ddata(chip)	container_of(chip, struct efm32_gpio_ddata, chip)
+
+static unsigned efm32_gpio_get_mode(struct efm32_gpio_ddata *ddata,
+	       	unsigned pin, unsigned port)
+{
+	return (readl(ddata->base + (pin < 8 ? GPIO_Px_MODEL(port) : GPIO_Px_MODEH(port))) >> (4 * (pin & 7))) & 0xf;
+
+}
+static int efm32_gpio_direction_input(struct gpio_chip *chip, unsigned offset)
+{
+	struct efm32_gpio_ddata *ddata = to_ddata(chip);
+	unsigned pin = offset % 16;
+	unsigned port = offset / 16;
+	unsigned mode;
+	int ret;
+
+	mode = efm32_gpio_get_mode(ddata, pin, port);
+
+	/*
+	 * XXX: don't reconfigure, needs to be resolved in combination with a
+	 * pinmux driver
+	 */
+	if (mode > 0 && mode < 4)
+		ret = 0;
+	else
+		ret = -EIO;
+
+	return ret;
+}
+
+static int efm32_gpio_get(struct gpio_chip *chip, unsigned offset)
+{
+	struct efm32_gpio_ddata *ddata = to_ddata(chip);
+	unsigned pin = offset % 16;
+	unsigned port = offset / 16;
+
+	/* XXX use bitband to simplify? */
+	return readl(ddata->base + GPIO_Px_DIN(port)) & (1 << pin);
+}
+
+static int efm32_gpio_direction_output(struct gpio_chip *chip,
+		unsigned offset, int value)
+{
+	struct efm32_gpio_ddata *ddata = to_ddata(chip);
+	unsigned pin = offset % 16;
+	unsigned port = offset / 16;
+	unsigned mode;
+	int ret;
+
+	mode = efm32_gpio_get_mode(ddata, pin, port);
+	/*
+	 * XXX: don't reconfigure, needs to be resolved in combination with a
+	 * pinmux driver
+	 */
+	if (mode >= 4)
+		ret = 0;
+	else
+		ret = -EIO;
+
+	return ret;
+}
+
+static void efm32_gpio_set(struct gpio_chip *chip,
+		unsigned offset, int value)
+{
+	struct efm32_gpio_ddata *ddata = to_ddata(chip);
+	unsigned pin = offset % 16;
+	unsigned port = offset / 16;
+
+	writel(1 << pin, ddata->base +
+			(value ? GPIO_Px_DOUTSET(port) : GPIO_Px_DOUTCLR(port)));
+}
+
+static int efm32_gpio_to_irq(struct gpio_chip *chip, unsigned offset)
+{
+	struct efm32_gpio_ddata *ddata = to_ddata(chip);
+	unsigned pin = offset % 16;
+	unsigned port = offset / 16;
+	unsigned extipsel_offset = pin < 8 ? GPIO_EXTIPSELL : GPIO_EXTIPSELH;
+	unsigned extipsel;
+	unsigned extipsel_shift = (pin % 8) * 4;
+
+	spin_lock(&ddata->lock);
+
+	extipsel = readl(ddata->base + extipsel_offset);
+
+	if (ddata->assigned_irqpins & (1 << pin)) {
+		if (((extipsel >> extipsel_shift) & 0x7) != port) {
+			spin_unlock(&ddata->lock);
+			clk_disable(ddata->clk);
+			return -EBUSY;
+		}
+	}
+
+	extipsel &= ~(0x7 << extipsel_shift);
+	extipsel |= port << extipsel_shift;
+	ddata->assigned_irqpins |= 1 << pin;
+	writel(extipsel, ddata->base + extipsel_offset);
+
+	spin_unlock(&ddata->lock);
+
+	return irq_create_mapping(ddata->irq_domain, offset % 16);
+}
+
+static irqreturn_t efm32_gpio_handler(struct efm32_gpio_ddata *ddata, unsigned mask)
+{
+	unsigned flag, init_flag;
+	irqreturn_t iret = IRQ_NONE;
+
+	init_flag = flag = readl(ddata->base + GPIO_IF) & mask;
+
+	while (flag) {
+		int line = __fls(flag);
+
+		writel(1 << line, ddata->base + GPIO_IFC);
+		if (generic_handle_irq(irq_create_mapping(ddata->irq_domain, line)))
+			iret = IRQ_HANDLED;
+
+		flag &= ~(1 << line);
+	}
+	return iret;
+}
+
+static irqreturn_t efm32_gpio_handler_even(int irq, void *data)
+{
+	struct efm32_gpio_ddata *ddata = data;
+	return efm32_gpio_handler(ddata, 0x55555555U);
+}
+
+static irqreturn_t efm32_gpio_handler_odd(int irq, void *data)
+{
+	struct efm32_gpio_ddata *ddata = data;
+	return efm32_gpio_handler(ddata, 0xaaaaaaaaU);
+}
+
+static void efm32_gpio_irq_ack(struct irq_data *data)
+{
+	struct efm32_gpio_ddata *ddata = irq_get_chip_data(data->irq);
+
+	writel(1 << data->hwirq, ddata->base + GPIO_IFC);
+}
+
+static void efm32_gpio_irq_mask(struct irq_data *data)
+{
+	struct efm32_gpio_ddata *ddata = irq_get_chip_data(data->irq);
+	unsigned ien;
+
+	spin_lock(&ddata->lock);
+       	ien = readl(ddata->base + GPIO_IEN);
+	ien &= ~(1 << data->hwirq);
+	writel(ien, ddata->base + GPIO_IEN);
+	spin_unlock(&ddata->lock);
+}
+
+static void efm32_gpio_irq_unmask(struct irq_data *data)
+{
+	struct efm32_gpio_ddata *ddata = irq_get_chip_data(data->irq);
+	unsigned ien;
+
+	spin_lock(&ddata->lock);
+	ien = readl(ddata->base + GPIO_IEN);
+	ien |= 1 << data->hwirq;
+	writel(ien, ddata->base + GPIO_IEN);
+	spin_unlock(&ddata->lock);
+}
+
+static struct irq_chip efm32_gpio_irqchip = {
+	.irq_ack = efm32_gpio_irq_ack,
+	.irq_mask = efm32_gpio_irq_mask,
+	.irq_unmask = efm32_gpio_irq_unmask,
+};
+
+static int efm32_gpio_irq_map(struct irq_domain *d, unsigned int virq,
+		irq_hw_number_t hw)
+{
+	struct efm32_gpio_ddata *ddata = d->host_data;
+
+	irq_set_chip_data(virq, ddata);
+	irq_set_chip_and_handler(virq, &efm32_gpio_irqchip, handle_edge_irq);
+
+	set_irq_flags(virq, IRQF_VALID | IRQF_PROBE);
+
+	return 0;
+}
+
+static const struct irq_domain_ops efm32_gpio_irq_domain_ops = {
+	.map = efm32_gpio_irq_map,
+};
+
+static int efm32_gpio_probe(struct platform_device *pdev)
+{
+	struct efm32_gpio_ddata *ddata;
+	struct resource *res;
+	int ret = -ENOMEM;
+	int irq_even, irq_odd;
+
+	ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
+	if (!ddata) {
+		dev_err(&pdev->dev, "cannot allocate driver data");
+		return -ENOMEM;
+	}
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res) {
+		dev_err(&pdev->dev, "can't get device resources\n");
+		return -ENOENT;
+	}
+
+	ddata->clk = devm_clk_get(&pdev->dev, NULL);
+	if (IS_ERR(ddata->clk)) {
+		ret = PTR_ERR(ddata->clk);
+		dev_err(&pdev->dev, "can't get clock (%d)\n", ret);
+		return ret;
+	}
+
+	irq_even = platform_get_irq(pdev, 0);
+	irq_odd = platform_get_irq(pdev, 1);
+	if (irq_even <= 0 || irq_odd <= 0) {
+		dev_err(&pdev->dev, "can't get irq numbers (%d, %d)\n",
+				irq_even, irq_odd);
+		return -ENOENT;
+	}
+	ddata->irq_even = irq_even;
+	ddata->irq_odd = irq_odd;
+
+	ddata->base = devm_request_and_ioremap(&pdev->dev, res);
+	if (!ddata->base) {
+		dev_err(&pdev->dev,
+				"cannot request and ioremap register set\n");
+		return -EADDRNOTAVAIL;
+	}
+
+	spin_lock_init(&ddata->lock);
+
+	ret = clk_prepare_enable(ddata->clk);
+	if (ret < 0) {
+		dev_err(&pdev->dev, "cannot enable clock (%d)\n", ret);
+		return ret;
+	}
+
+	/* disable and clear irqs */
+	writel(0, ddata->base + GPIO_IEN);
+	writel(0xffff, ddata->base + GPIO_IFC);
+
+	ddata->irq_domain = irq_domain_add_linear(pdev->dev.of_node,
+			16, &efm32_gpio_irq_domain_ops, ddata);
+	if (!ddata->irq_domain) {
+		dev_err(&pdev->dev, "failed to add irq_domain\n");
+		goto err_add_irq_domain;
+	}
+
+	ddata->chip.label = DRIVER_NAME;
+	ddata->chip.dev = &pdev->dev;
+	ddata->chip.owner = THIS_MODULE;
+
+	//ddata->chip.get_direction
+	ddata->chip.direction_input = efm32_gpio_direction_input;
+	ddata->chip.get = efm32_gpio_get;
+	ddata->chip.direction_output = efm32_gpio_direction_output;
+	ddata->chip.set = efm32_gpio_set;
+	ddata->chip.to_irq = efm32_gpio_to_irq;
+	ddata->chip.base = -1;
+	ddata->chip.ngpio = 96;
+	ddata->chip.can_sleep = 0;
+
+	ret = request_irq(ddata->irq_even, efm32_gpio_handler_even,
+			0, DRIVER_NAME, ddata);
+	if (ret)
+		goto err_request_even_irq;
+
+	ret = request_irq(ddata->irq_odd, efm32_gpio_handler_odd,
+			0, DRIVER_NAME, ddata);
+	if (ret)
+		goto err_request_odd_irq;
+
+	ret = gpiochip_add(&ddata->chip);
+	if (ret) {
+		free_irq(ddata->irq_odd, ddata);
+err_request_odd_irq:
+
+		free_irq(ddata->irq_even, ddata);
+err_request_even_irq:
+
+		irq_domain_remove(ddata->irq_domain);
+	}
+
+err_add_irq_domain:
+	return ret;
+}
+
+static const struct of_device_id efm32_gpio_dt_ids[] = {
+	{
+		.compatible = "efm32,gpio",
+	}, {
+		/* sentinel */
+	}
+};
+MODULE_DEVICE_TABLE(of, efm32_gpio_dt_ids);
+
+static struct platform_driver efm32_gpio_driver = {
+	.probe = efm32_gpio_probe,
+
+	.driver = {
+		.name = DRIVER_NAME,
+		.owner = THIS_MODULE,
+		.of_match_table = efm32_gpio_dt_ids,
+	},
+};
+
+static int __init efm32_gpio_init(void)
+{
+	return platform_driver_register(&efm32_gpio_driver);
+}
+postcore_initcall(efm32_gpio_init);
+
+MODULE_LICENSE("GPL v2");