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/input.h>
16 #include <linux/serio.h>
17 #include <linux/interrupt.h>
18 #include <linux/platform_device.h>
20 #include <linux/slab.h>
23 #define DRV_NAME "altera_ps2"
27 struct resource
*iomem_res
;
33 * Read all bytes waiting in the PS2 port. There should be
34 * at the most one, but we loop for safety.
36 static irqreturn_t
altera_ps2_rxint(int irq
, void *dev_id
)
38 struct ps2if
*ps2if
= dev_id
;
40 int handled
= IRQ_NONE
;
42 while ((status
= readl(ps2if
->base
)) & 0xffff0000) {
43 serio_interrupt(ps2if
->io
, status
& 0xff, 0);
44 handled
= IRQ_HANDLED
;
51 * Write a byte to the PS2 port.
53 static int altera_ps2_write(struct serio
*io
, unsigned char val
)
55 struct ps2if
*ps2if
= io
->port_data
;
57 writel(val
, ps2if
->base
);
61 static int altera_ps2_open(struct serio
*io
)
63 struct ps2if
*ps2if
= io
->port_data
;
66 while (readl(ps2if
->base
) & 0xffff0000)
69 writel(1, ps2if
->base
+ 4); /* enable rx irq */
73 static void altera_ps2_close(struct serio
*io
)
75 struct ps2if
*ps2if
= io
->port_data
;
77 writel(0, ps2if
->base
); /* disable rx irq */
81 * Add one device to this driver.
83 static int altera_ps2_probe(struct platform_device
*pdev
)
89 ps2if
= kzalloc(sizeof(struct ps2if
), GFP_KERNEL
);
90 serio
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
91 if (!ps2if
|| !serio
) {
96 serio
->id
.type
= SERIO_8042
;
97 serio
->write
= altera_ps2_write
;
98 serio
->open
= altera_ps2_open
;
99 serio
->close
= altera_ps2_close
;
100 strlcpy(serio
->name
, dev_name(&pdev
->dev
), sizeof(serio
->name
));
101 strlcpy(serio
->phys
, dev_name(&pdev
->dev
), sizeof(serio
->phys
));
102 serio
->port_data
= ps2if
;
103 serio
->dev
.parent
= &pdev
->dev
;
106 ps2if
->iomem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
107 if (ps2if
->iomem_res
== NULL
) {
113 irq
= platform_get_irq(pdev
, 0);
120 if (!request_mem_region(ps2if
->iomem_res
->start
,
121 resource_size(ps2if
->iomem_res
), pdev
->name
)) {
126 ps2if
->base
= ioremap(ps2if
->iomem_res
->start
,
127 resource_size(ps2if
->iomem_res
));
133 error
= request_irq(ps2if
->irq
, altera_ps2_rxint
, 0, pdev
->name
, ps2if
);
135 dev_err(&pdev
->dev
, "could not allocate IRQ %d: %d\n",
140 dev_info(&pdev
->dev
, "base %p, irq %d\n", ps2if
->base
, ps2if
->irq
);
142 serio_register_port(ps2if
->io
);
143 platform_set_drvdata(pdev
, ps2if
);
148 iounmap(ps2if
->base
);
150 release_mem_region(ps2if
->iomem_res
->start
,
151 resource_size(ps2if
->iomem_res
));
159 * Remove one device from this driver.
161 static int altera_ps2_remove(struct platform_device
*pdev
)
163 struct ps2if
*ps2if
= platform_get_drvdata(pdev
);
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
));
176 static const struct of_device_id altera_ps2_match
[] = {
177 { .compatible
= "ALTR,ps2-1.0", },
178 { .compatible
= "altr,ps2-1.0", },
181 MODULE_DEVICE_TABLE(of
, altera_ps2_match
);
182 #endif /* CONFIG_OF */
185 * Our device driver structure
187 static struct platform_driver altera_ps2_driver
= {
188 .probe
= altera_ps2_probe
,
189 .remove
= altera_ps2_remove
,
192 .owner
= THIS_MODULE
,
193 .of_match_table
= of_match_ptr(altera_ps2_match
),
196 module_platform_driver(altera_ps2_driver
);
198 MODULE_DESCRIPTION("Altera University Program PS2 controller driver");
199 MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
200 MODULE_LICENSE("GPL");
201 MODULE_ALIAS("platform:" DRV_NAME
);