1 /*
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/module.h>
22 #include <linux/interrupt.h>
23 #include <linux/stringify.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/slab.h>
26 
27 #include <linux/of.h>
28 #include <linux/of_platform.h>
29 #include <linux/of_address.h>
30 
31 #define DRIVER_NAME "uio_pdrv_genirq"
32 
33 struct uio_pdrv_genirq_platdata {
34 	struct uio_info *uioinfo;
35 	spinlock_t lock;
36 	unsigned long flags;
37 	struct platform_device *pdev;
38 };
39 
uio_pdrv_genirq_open(struct uio_info * info,struct inode * inode)40 static int uio_pdrv_genirq_open(struct uio_info *info, struct inode *inode)
41 {
42 	struct uio_pdrv_genirq_platdata *priv = info->priv;
43 
44 	/* Wait until the Runtime PM code has woken up the device */
45 	pm_runtime_get_sync(&priv->pdev->dev);
46 	return 0;
47 }
48 
uio_pdrv_genirq_release(struct uio_info * info,struct inode * inode)49 static int uio_pdrv_genirq_release(struct uio_info *info, struct inode *inode)
50 {
51 	struct uio_pdrv_genirq_platdata *priv = info->priv;
52 
53 	/* Tell the Runtime PM code that the device has become idle */
54 	pm_runtime_put_sync(&priv->pdev->dev);
55 	return 0;
56 }
57 
uio_pdrv_genirq_handler(int irq,struct uio_info * dev_info)58 static irqreturn_t uio_pdrv_genirq_handler(int irq, struct uio_info *dev_info)
59 {
60 	struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
61 
62 	/* Just disable the interrupt in the interrupt controller, and
63 	 * remember the state so we can allow user space to enable it later.
64 	 */
65 
66 	if (!test_and_set_bit(0, &priv->flags))
67 		disable_irq_nosync(irq);
68 
69 	return IRQ_HANDLED;
70 }
71 
uio_pdrv_genirq_irqcontrol(struct uio_info * dev_info,s32 irq_on)72 static int uio_pdrv_genirq_irqcontrol(struct uio_info *dev_info, s32 irq_on)
73 {
74 	struct uio_pdrv_genirq_platdata *priv = dev_info->priv;
75 	unsigned long flags;
76 
77 	/* Allow user space to enable and disable the interrupt
78 	 * in the interrupt controller, but keep track of the
79 	 * state to prevent per-irq depth damage.
80 	 *
81 	 * Serialize this operation to support multiple tasks.
82 	 */
83 
84 	spin_lock_irqsave(&priv->lock, flags);
85 	if (irq_on) {
86 		if (test_and_clear_bit(0, &priv->flags))
87 			enable_irq(dev_info->irq);
88 	} else {
89 		if (!test_and_set_bit(0, &priv->flags))
90 			disable_irq(dev_info->irq);
91 	}
92 	spin_unlock_irqrestore(&priv->lock, flags);
93 
94 	return 0;
95 }
96 
uio_pdrv_genirq_probe(struct platform_device * pdev)97 static int uio_pdrv_genirq_probe(struct platform_device *pdev)
98 {
99 	struct uio_info *uioinfo = pdev->dev.platform_data;
100 	struct uio_pdrv_genirq_platdata *priv;
101 	struct uio_mem *uiomem;
102 	int ret = -EINVAL;
103 	int i;
104 
105 	if (!uioinfo) {
106 		int irq;
107 
108 		/* alloc uioinfo for one device */
109 		uioinfo = kzalloc(sizeof(*uioinfo), GFP_KERNEL);
110 		if (!uioinfo) {
111 			ret = -ENOMEM;
112 			dev_err(&pdev->dev, "unable to kmalloc\n");
113 			goto bad2;
114 		}
115 		uioinfo->name = pdev->dev.of_node->name;
116 		uioinfo->version = "devicetree";
117 
118 		/* Multiple IRQs are not supported */
119 		irq = platform_get_irq(pdev, 0);
120 		if (irq == -ENXIO)
121 			uioinfo->irq = UIO_IRQ_NONE;
122 		else
123 			uioinfo->irq = irq;
124 	}
125 
126 	if (!uioinfo || !uioinfo->name || !uioinfo->version) {
127 		dev_err(&pdev->dev, "missing platform_data\n");
128 		goto bad0;
129 	}
130 
131 	if (uioinfo->handler || uioinfo->irqcontrol ||
132 	    uioinfo->irq_flags & IRQF_SHARED) {
133 		dev_err(&pdev->dev, "interrupt configuration error\n");
134 		goto bad0;
135 	}
136 
137 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
138 	if (!priv) {
139 		ret = -ENOMEM;
140 		dev_err(&pdev->dev, "unable to kmalloc\n");
141 		goto bad0;
142 	}
143 
144 	priv->uioinfo = uioinfo;
145 	spin_lock_init(&priv->lock);
146 	priv->flags = 0; /* interrupt is enabled to begin with */
147 	priv->pdev = pdev;
148 
149 	uiomem = &uioinfo->mem[0];
150 
151 	for (i = 0; i < pdev->num_resources; ++i) {
152 		struct resource *r = &pdev->resource[i];
153 
154 		if (r->flags != IORESOURCE_MEM)
155 			continue;
156 
157 		if (uiomem >= &uioinfo->mem[MAX_UIO_MAPS]) {
158 			dev_warn(&pdev->dev, "device has more than "
159 					__stringify(MAX_UIO_MAPS)
160 					" I/O memory resources.\n");
161 			break;
162 		}
163 
164 		uiomem->memtype = UIO_MEM_PHYS;
165 		uiomem->addr = r->start;
166 		uiomem->size = resource_size(r);
167 		++uiomem;
168 	}
169 
170 	while (uiomem < &uioinfo->mem[MAX_UIO_MAPS]) {
171 		uiomem->size = 0;
172 		++uiomem;
173 	}
174 
175 	/* This driver requires no hardware specific kernel code to handle
176 	 * interrupts. Instead, the interrupt handler simply disables the
177 	 * interrupt in the interrupt controller. User space is responsible
178 	 * for performing hardware specific acknowledge and re-enabling of
179 	 * the interrupt in the interrupt controller.
180 	 *
181 	 * Interrupt sharing is not supported.
182 	 */
183 
184 	uioinfo->handler = uio_pdrv_genirq_handler;
185 	uioinfo->irqcontrol = uio_pdrv_genirq_irqcontrol;
186 	uioinfo->open = uio_pdrv_genirq_open;
187 	uioinfo->release = uio_pdrv_genirq_release;
188 	uioinfo->priv = priv;
189 
190 	/* Enable Runtime PM for this device:
191 	 * The device starts in suspended state to allow the hardware to be
192 	 * turned off by default. The Runtime PM bus code should power on the
193 	 * hardware and enable clocks at open().
194 	 */
195 	pm_runtime_enable(&pdev->dev);
196 
197 	ret = uio_register_device(&pdev->dev, priv->uioinfo);
198 	if (ret) {
199 		dev_err(&pdev->dev, "unable to register uio device\n");
200 		goto bad1;
201 	}
202 
203 	platform_set_drvdata(pdev, priv);
204 	return 0;
205  bad1:
206 	kfree(priv);
207 	pm_runtime_disable(&pdev->dev);
208  bad0:
209 	/* kfree uioinfo for OF */
210 	if (pdev->dev.of_node)
211 		kfree(uioinfo);
212  bad2:
213 	return ret;
214 }
215 
uio_pdrv_genirq_remove(struct platform_device * pdev)216 static int uio_pdrv_genirq_remove(struct platform_device *pdev)
217 {
218 	struct uio_pdrv_genirq_platdata *priv = platform_get_drvdata(pdev);
219 
220 	uio_unregister_device(priv->uioinfo);
221 	pm_runtime_disable(&pdev->dev);
222 
223 	priv->uioinfo->handler = NULL;
224 	priv->uioinfo->irqcontrol = NULL;
225 
226 	/* kfree uioinfo for OF */
227 	if (pdev->dev.of_node)
228 		kfree(priv->uioinfo);
229 
230 	kfree(priv);
231 	return 0;
232 }
233 
uio_pdrv_genirq_runtime_nop(struct device * dev)234 static int uio_pdrv_genirq_runtime_nop(struct device *dev)
235 {
236 	/* Runtime PM callback shared between ->runtime_suspend()
237 	 * and ->runtime_resume(). Simply returns success.
238 	 *
239 	 * In this driver pm_runtime_get_sync() and pm_runtime_put_sync()
240 	 * are used at open() and release() time. This allows the
241 	 * Runtime PM code to turn off power to the device while the
242 	 * device is unused, ie before open() and after release().
243 	 *
244 	 * This Runtime PM callback does not need to save or restore
245 	 * any registers since user space is responsbile for hardware
246 	 * register reinitialization after open().
247 	 */
248 	return 0;
249 }
250 
251 static const struct dev_pm_ops uio_pdrv_genirq_dev_pm_ops = {
252 	.runtime_suspend = uio_pdrv_genirq_runtime_nop,
253 	.runtime_resume = uio_pdrv_genirq_runtime_nop,
254 };
255 
256 #ifdef CONFIG_OF
257 static const struct of_device_id uio_of_genirq_match[] = {
258 	{ /* empty for now */ },
259 };
260 MODULE_DEVICE_TABLE(of, uio_of_genirq_match);
261 #else
262 # define uio_of_genirq_match NULL
263 #endif
264 
265 static struct platform_driver uio_pdrv_genirq = {
266 	.probe = uio_pdrv_genirq_probe,
267 	.remove = uio_pdrv_genirq_remove,
268 	.driver = {
269 		.name = DRIVER_NAME,
270 		.owner = THIS_MODULE,
271 		.pm = &uio_pdrv_genirq_dev_pm_ops,
272 		.of_match_table = uio_of_genirq_match,
273 	},
274 };
275 
276 module_platform_driver(uio_pdrv_genirq);
277 
278 MODULE_AUTHOR("Magnus Damm");
279 MODULE_DESCRIPTION("Userspace I/O platform driver with generic IRQ handling");
280 MODULE_LICENSE("GPL v2");
281 MODULE_ALIAS("platform:" DRIVER_NAME);
282