blob: 61e569df2bba60cabc46a6e85d5f33007fd58a6c [file] [log] [blame]
Magnus Dammc767db02008-07-11 18:55:27 +09001/*
2 * drivers/uio/uio_pdrv_genirq.c
3 *
4 * Userspace I/O platform driver with generic IRQ handling code.
5 *
6 * Copyright (C) 2008 Magnus Damm
7 *
8 * Based on uio_pdrv.c by Uwe Kleine-Koenig,
9 * Copyright (C) 2008 by Digi International Inc.
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published by
14 * the Free Software Foundation.
15 */
16
17#include <linux/platform_device.h>
18#include <linux/uio_driver.h>
19#include <linux/spinlock.h>
20#include <linux/bitops.h>
21#include <linux/interrupt.h>
22#include <linux/stringify.h>
Magnus Dammaf76756e2009-08-14 10:49:38 +000023#include <linux/pm_runtime.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Magnus Dammc767db02008-07-11 18:55:27 +090025
26#define DRIVER_NAME "uio_pdrv_genirq"
27
28struct uio_pdrv_genirq_platdata {
29 struct uio_info *uioinfo;
30 spinlock_t lock;
31 unsigned long flags;
Magnus Dammaf76756e2009-08-14 10:49:38 +000032 struct platform_device *pdev;
Magnus Dammc767db02008-07-11 18:55:27 +090033};
34
Magnus Dammaf76756e2009-08-14 10:49:38 +000035static int uio_pdrv_genirq_open(struct uio_info *info, struct inode *inode)
36{
37 struct uio_pdrv_genirq_platdata *priv = info->priv;
38
39 /* Wait until the Runtime PM code has woken up the device */
40 pm_runtime_get_sync(&priv->pdev->dev);
41 return 0;
42}
43
44static int uio_pdrv_genirq_release(struct uio_info *info, struct inode *inode)
45{
46 struct uio_pdrv_genirq_platdata *priv = info->priv;
47
48 /* Tell the Runtime PM code that the device has become idle */
49 pm_runtime_put_sync(&priv->pdev->dev);
50 return 0;
51}
52
Magnus Dammc767db02008-07-11 18:55:27 +090053static irqreturn_t uio_pdrv_genirq_handler(int irq, struct uio_info *dev_info)
54{
55 struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
56
57 /* Just disable the interrupt in the interrupt controller, and
58 * remember the state so we can allow user space to enable it later.
59 */
60
61 if (!test_and_set_bit(0, &priv->flags))
62 disable_irq_nosync(irq);
63
64 return IRQ_HANDLED;
65}
66
67static int uio_pdrv_genirq_irqcontrol(struct uio_info *dev_info, s32 irq_on)
68{
69 struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
70 unsigned long flags;
71
72 /* Allow user space to enable and disable the interrupt
73 * in the interrupt controller, but keep track of the
74 * state to prevent per-irq depth damage.
75 *
76 * Serialize this operation to support multiple tasks.
77 */
78
79 spin_lock_irqsave(&priv->lock, flags);
80 if (irq_on) {
81 if (test_and_clear_bit(0, &priv->flags))
82 enable_irq(dev_info->irq);
83 } else {
84 if (!test_and_set_bit(0, &priv->flags))
85 disable_irq(dev_info->irq);
86 }
87 spin_unlock_irqrestore(&priv->lock, flags);
88
89 return 0;
90}
91
92static int uio_pdrv_genirq_probe(struct platform_device *pdev)
93{
94 struct uio_info *uioinfo = pdev->dev.platform_data;
95 struct uio_pdrv_genirq_platdata *priv;
96 struct uio_mem *uiomem;
97 int ret = -EINVAL;
98 int i;
99
100 if (!uioinfo || !uioinfo->name || !uioinfo->version) {
101 dev_err(&pdev->dev, "missing platform_data\n");
102 goto bad0;
103 }
104
Mike Frysingere543ae82008-10-29 18:35:52 -0400105 if (uioinfo->handler || uioinfo->irqcontrol ||
106 uioinfo->irq_flags & IRQF_SHARED) {
Magnus Dammc767db02008-07-11 18:55:27 +0900107 dev_err(&pdev->dev, "interrupt configuration error\n");
108 goto bad0;
109 }
110
111 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
112 if (!priv) {
113 ret = -ENOMEM;
114 dev_err(&pdev->dev, "unable to kmalloc\n");
115 goto bad0;
116 }
117
118 priv->uioinfo = uioinfo;
119 spin_lock_init(&priv->lock);
120 priv->flags = 0; /* interrupt is enabled to begin with */
Magnus Dammaf76756e2009-08-14 10:49:38 +0000121 priv->pdev = pdev;
Magnus Dammc767db02008-07-11 18:55:27 +0900122
123 uiomem = &uioinfo->mem[0];
124
125 for (i = 0; i < pdev->num_resources; ++i) {
126 struct resource *r = &pdev->resource[i];
127
128 if (r->flags != IORESOURCE_MEM)
129 continue;
130
131 if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
132 dev_warn(&pdev->dev, "device has more than "
133 __stringify(MAX_UIO_MAPS)
134 " I/O memory resources.\n");
135 break;
136 }
137
138 uiomem->memtype = UIO_MEM_PHYS;
139 uiomem->addr = r->start;
140 uiomem->size = r->end - r->start + 1;
141 ++uiomem;
142 }
143
144 while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
145 uiomem->size = 0;
146 ++uiomem;
147 }
148
149 /* This driver requires no hardware specific kernel code to handle
150 * interrupts. Instead, the interrupt handler simply disables the
151 * interrupt in the interrupt controller. User space is responsible
152 * for performing hardware specific acknowledge and re-enabling of
153 * the interrupt in the interrupt controller.
154 *
155 * Interrupt sharing is not supported.
156 */
157
Mike Frysingere543ae82008-10-29 18:35:52 -0400158 uioinfo->irq_flags |= IRQF_DISABLED;
Magnus Dammc767db02008-07-11 18:55:27 +0900159 uioinfo->handler = uio_pdrv_genirq_handler;
160 uioinfo->irqcontrol = uio_pdrv_genirq_irqcontrol;
Magnus Dammaf76756e2009-08-14 10:49:38 +0000161 uioinfo->open = uio_pdrv_genirq_open;
162 uioinfo->release = uio_pdrv_genirq_release;
Magnus Dammc767db02008-07-11 18:55:27 +0900163 uioinfo->priv = priv;
164
Magnus Dammaf76756e2009-08-14 10:49:38 +0000165 /* Enable Runtime PM for this device:
166 * The device starts in suspended state to allow the hardware to be
167 * turned off by default. The Runtime PM bus code should power on the
168 * hardware and enable clocks at open().
169 */
170 pm_runtime_enable(&pdev->dev);
171
Magnus Dammc767db02008-07-11 18:55:27 +0900172 ret = uio_register_device(&pdev->dev, priv->uioinfo);
173 if (ret) {
174 dev_err(&pdev->dev, "unable to register uio device\n");
175 goto bad1;
176 }
177
178 platform_set_drvdata(pdev, priv);
179 return 0;
180 bad1:
181 kfree(priv);
Kuninori Morimoto68fb2e42009-11-09 02:42:47 +0000182 pm_runtime_disable(&pdev->dev);
Magnus Dammc767db02008-07-11 18:55:27 +0900183 bad0:
184 return ret;
185}
186
187static int uio_pdrv_genirq_remove(struct platform_device *pdev)
188{
189 struct uio_pdrv_genirq_platdata *priv = platform_get_drvdata(pdev);
190
191 uio_unregister_device(priv->uioinfo);
Magnus Dammaf76756e2009-08-14 10:49:38 +0000192 pm_runtime_disable(&pdev->dev);
Magnus Dammc767db02008-07-11 18:55:27 +0900193 kfree(priv);
194 return 0;
195}
196
Magnus Dammaf76756e2009-08-14 10:49:38 +0000197static int uio_pdrv_genirq_runtime_nop(struct device *dev)
198{
199 /* Runtime PM callback shared between ->runtime_suspend()
200 * and ->runtime_resume(). Simply returns success.
201 *
202 * In this driver pm_runtime_get_sync() and pm_runtime_put_sync()
203 * are used at open() and release() time. This allows the
204 * Runtime PM code to turn off power to the device while the
205 * device is unused, ie before open() and after release().
206 *
207 * This Runtime PM callback does not need to save or restore
208 * any registers since user space is responsbile for hardware
209 * register reinitialization after open().
210 */
211 return 0;
212}
213
Alexey Dobriyan47145212009-12-14 18:00:08 -0800214static const struct dev_pm_ops uio_pdrv_genirq_dev_pm_ops = {
Magnus Dammaf76756e2009-08-14 10:49:38 +0000215 .runtime_suspend = uio_pdrv_genirq_runtime_nop,
216 .runtime_resume = uio_pdrv_genirq_runtime_nop,
217};
218
Magnus Dammc767db02008-07-11 18:55:27 +0900219static struct platform_driver uio_pdrv_genirq = {
220 .probe = uio_pdrv_genirq_probe,
221 .remove = uio_pdrv_genirq_remove,
222 .driver = {
223 .name = DRIVER_NAME,
224 .owner = THIS_MODULE,
Magnus Dammaf76756e2009-08-14 10:49:38 +0000225 .pm = &uio_pdrv_genirq_dev_pm_ops,
Magnus Dammc767db02008-07-11 18:55:27 +0900226 },
227};
228
229static int __init uio_pdrv_genirq_init(void)
230{
231 return platform_driver_register(&uio_pdrv_genirq);
232}
233
234static void __exit uio_pdrv_genirq_exit(void)
235{
236 platform_driver_unregister(&uio_pdrv_genirq);
237}
238
239module_init(uio_pdrv_genirq_init);
240module_exit(uio_pdrv_genirq_exit);
241
242MODULE_AUTHOR("Magnus Damm");
243MODULE_DESCRIPTION("Userspace I/O platform driver with generic IRQ handling");
244MODULE_LICENSE("GPL v2");
245MODULE_ALIAS("platform:" DRIVER_NAME);