Linux 2.6.33-rc6
[cris-mirror.git] / drivers / input / serio / altera_ps2.c
blob320b7ca48bf8567a5c1bbdbb114ce971dcb8a960
1 /*
2 * Altera University Program PS2 controller driver
4 * Copyright (C) 2008 Thomas Chou <thomas@wytron.com.tw>
6 * Based on sa1111ps2.c, which is:
7 * Copyright (C) 2002 Russell King
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/input.h>
17 #include <linux/serio.h>
18 #include <linux/interrupt.h>
19 #include <linux/platform_device.h>
20 #include <linux/io.h>
22 #define DRV_NAME "altera_ps2"
24 struct ps2if {
25 struct serio *io;
26 struct resource *iomem_res;
27 void __iomem *base;
28 unsigned irq;
32 * Read all bytes waiting in the PS2 port. There should be
33 * at the most one, but we loop for safety.
35 static irqreturn_t altera_ps2_rxint(int irq, void *dev_id)
37 struct ps2if *ps2if = dev_id;
38 unsigned int status;
39 int handled = IRQ_NONE;
41 while ((status = readl(ps2if->base)) & 0xffff0000) {
42 serio_interrupt(ps2if->io, status & 0xff, 0);
43 handled = IRQ_HANDLED;
46 return handled;
50 * Write a byte to the PS2 port.
52 static int altera_ps2_write(struct serio *io, unsigned char val)
54 struct ps2if *ps2if = io->port_data;
56 writel(val, ps2if->base);
57 return 0;
60 static int altera_ps2_open(struct serio *io)
62 struct ps2if *ps2if = io->port_data;
64 /* clear fifo */
65 while (readl(ps2if->base) & 0xffff0000)
66 /* empty */;
68 writel(1, ps2if->base + 4); /* enable rx irq */
69 return 0;
72 static void altera_ps2_close(struct serio *io)
74 struct ps2if *ps2if = io->port_data;
76 writel(0, ps2if->base); /* disable rx irq */
80 * Add one device to this driver.
82 static int __devinit altera_ps2_probe(struct platform_device *pdev)
84 struct ps2if *ps2if;
85 struct serio *serio;
86 int error, irq;
88 ps2if = kzalloc(sizeof(struct ps2if), GFP_KERNEL);
89 serio = kzalloc(sizeof(struct serio), GFP_KERNEL);
90 if (!ps2if || !serio) {
91 error = -ENOMEM;
92 goto err_free_mem;
95 serio->id.type = SERIO_8042;
96 serio->write = altera_ps2_write;
97 serio->open = altera_ps2_open;
98 serio->close = altera_ps2_close;
99 strlcpy(serio->name, dev_name(&pdev->dev), sizeof(serio->name));
100 strlcpy(serio->phys, dev_name(&pdev->dev), sizeof(serio->phys));
101 serio->port_data = ps2if;
102 serio->dev.parent = &pdev->dev;
103 ps2if->io = serio;
105 ps2if->iomem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
106 if (ps2if->iomem_res == NULL) {
107 error = -ENOENT;
108 goto err_free_mem;
112 irq = platform_get_irq(pdev, 0);
113 if (irq < 0) {
114 error = -ENXIO;
115 goto err_free_mem;
117 ps2if->irq = irq;
119 if (!request_mem_region(ps2if->iomem_res->start,
120 resource_size(ps2if->iomem_res), pdev->name)) {
121 error = -EBUSY;
122 goto err_free_mem;
125 ps2if->base = ioremap(ps2if->iomem_res->start,
126 resource_size(ps2if->iomem_res));
127 if (!ps2if->base) {
128 error = -ENOMEM;
129 goto err_free_res;
132 error = request_irq(ps2if->irq, altera_ps2_rxint, 0, pdev->name, ps2if);
133 if (error) {
134 dev_err(&pdev->dev, "could not allocate IRQ %d: %d\n",
135 ps2if->irq, error);
136 goto err_unmap;
139 dev_info(&pdev->dev, "base %p, irq %d\n", ps2if->base, ps2if->irq);
141 serio_register_port(ps2if->io);
142 platform_set_drvdata(pdev, ps2if);
144 return 0;
146 err_unmap:
147 iounmap(ps2if->base);
148 err_free_res:
149 release_mem_region(ps2if->iomem_res->start,
150 resource_size(ps2if->iomem_res));
151 err_free_mem:
152 kfree(ps2if);
153 kfree(serio);
154 return error;
158 * Remove one device from this driver.
160 static int __devexit altera_ps2_remove(struct platform_device *pdev)
162 struct ps2if *ps2if = platform_get_drvdata(pdev);
164 platform_set_drvdata(pdev, NULL);
165 serio_unregister_port(ps2if->io);
166 free_irq(ps2if->irq, ps2if);
167 iounmap(ps2if->base);
168 release_mem_region(ps2if->iomem_res->start,
169 resource_size(ps2if->iomem_res));
170 kfree(ps2if);
172 return 0;
176 * Our device driver structure
178 static struct platform_driver altera_ps2_driver = {
179 .probe = altera_ps2_probe,
180 .remove = __devexit_p(altera_ps2_remove),
181 .driver = {
182 .name = DRV_NAME,
183 .owner = THIS_MODULE,
187 static int __init altera_ps2_init(void)
189 return platform_driver_register(&altera_ps2_driver);
192 static void __exit altera_ps2_exit(void)
194 platform_driver_unregister(&altera_ps2_driver);
197 module_init(altera_ps2_init);
198 module_exit(altera_ps2_exit);
200 MODULE_DESCRIPTION("Altera University Program PS2 controller driver");
201 MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
202 MODULE_LICENSE("GPL");
203 MODULE_ALIAS("platform:" DRV_NAME);