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"
31 * Read all bytes waiting in the PS2 port. There should be
32 * at the most one, but we loop for safety.
34 static irqreturn_t
altera_ps2_rxint(int irq
, void *dev_id
)
36 struct ps2if
*ps2if
= dev_id
;
38 irqreturn_t handled
= IRQ_NONE
;
40 while ((status
= readl(ps2if
->base
)) & 0xffff0000) {
41 serio_interrupt(ps2if
->io
, status
& 0xff, 0);
42 handled
= IRQ_HANDLED
;
49 * Write a byte to the PS2 port.
51 static int altera_ps2_write(struct serio
*io
, unsigned char val
)
53 struct ps2if
*ps2if
= io
->port_data
;
55 writel(val
, ps2if
->base
);
59 static int altera_ps2_open(struct serio
*io
)
61 struct ps2if
*ps2if
= io
->port_data
;
64 while (readl(ps2if
->base
) & 0xffff0000)
67 writel(1, ps2if
->base
+ 4); /* enable rx irq */
71 static void altera_ps2_close(struct serio
*io
)
73 struct ps2if
*ps2if
= io
->port_data
;
75 writel(0, ps2if
->base
+ 4); /* disable rx irq */
79 * Add one device to this driver.
81 static int altera_ps2_probe(struct platform_device
*pdev
)
88 ps2if
= devm_kzalloc(&pdev
->dev
, sizeof(struct ps2if
), GFP_KERNEL
);
92 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
93 ps2if
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
94 if (IS_ERR(ps2if
->base
))
95 return PTR_ERR(ps2if
->base
);
97 irq
= platform_get_irq(pdev
, 0);
101 error
= devm_request_irq(&pdev
->dev
, irq
, altera_ps2_rxint
, 0,
104 dev_err(&pdev
->dev
, "could not request IRQ %d\n", irq
);
108 serio
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
112 serio
->id
.type
= SERIO_8042
;
113 serio
->write
= altera_ps2_write
;
114 serio
->open
= altera_ps2_open
;
115 serio
->close
= altera_ps2_close
;
116 strlcpy(serio
->name
, dev_name(&pdev
->dev
), sizeof(serio
->name
));
117 strlcpy(serio
->phys
, dev_name(&pdev
->dev
), sizeof(serio
->phys
));
118 serio
->port_data
= ps2if
;
119 serio
->dev
.parent
= &pdev
->dev
;
122 dev_info(&pdev
->dev
, "base %p, irq %d\n", ps2if
->base
, irq
);
124 serio_register_port(ps2if
->io
);
125 platform_set_drvdata(pdev
, ps2if
);
131 * Remove one device from this driver.
133 static int altera_ps2_remove(struct platform_device
*pdev
)
135 struct ps2if
*ps2if
= platform_get_drvdata(pdev
);
137 serio_unregister_port(ps2if
->io
);
143 static const struct of_device_id altera_ps2_match
[] = {
144 { .compatible
= "ALTR,ps2-1.0", },
145 { .compatible
= "altr,ps2-1.0", },
148 MODULE_DEVICE_TABLE(of
, altera_ps2_match
);
149 #endif /* CONFIG_OF */
152 * Our device driver structure
154 static struct platform_driver altera_ps2_driver
= {
155 .probe
= altera_ps2_probe
,
156 .remove
= altera_ps2_remove
,
159 .of_match_table
= of_match_ptr(altera_ps2_match
),
162 module_platform_driver(altera_ps2_driver
);
164 MODULE_DESCRIPTION("Altera University Program PS2 controller driver");
165 MODULE_AUTHOR("Thomas Chou <thomas@wytron.com.tw>");
166 MODULE_LICENSE("GPL");
167 MODULE_ALIAS("platform:" DRV_NAME
);