2 * Copyright (C) 2007 Atmel Corporation
4 * Driver for the AT32AP700X PS/2 controller (PSIF).
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/init.h>
14 #include <linux/serio.h>
15 #include <linux/interrupt.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
19 #include <linux/clk.h>
20 #include <linux/platform_device.h>
22 /* PSIF register offsets */
32 /* Bitfields in control register. */
33 #define PSIF_CR_RXDIS_OFFSET 1
34 #define PSIF_CR_RXDIS_SIZE 1
35 #define PSIF_CR_RXEN_OFFSET 0
36 #define PSIF_CR_RXEN_SIZE 1
37 #define PSIF_CR_SWRST_OFFSET 15
38 #define PSIF_CR_SWRST_SIZE 1
39 #define PSIF_CR_TXDIS_OFFSET 9
40 #define PSIF_CR_TXDIS_SIZE 1
41 #define PSIF_CR_TXEN_OFFSET 8
42 #define PSIF_CR_TXEN_SIZE 1
44 /* Bitfields in interrupt disable, enable, mask and status register. */
45 #define PSIF_NACK_OFFSET 8
46 #define PSIF_NACK_SIZE 1
47 #define PSIF_OVRUN_OFFSET 5
48 #define PSIF_OVRUN_SIZE 1
49 #define PSIF_PARITY_OFFSET 9
50 #define PSIF_PARITY_SIZE 1
51 #define PSIF_RXRDY_OFFSET 4
52 #define PSIF_RXRDY_SIZE 1
53 #define PSIF_TXEMPTY_OFFSET 1
54 #define PSIF_TXEMPTY_SIZE 1
55 #define PSIF_TXRDY_OFFSET 0
56 #define PSIF_TXRDY_SIZE 1
58 /* Bitfields in prescale register. */
59 #define PSIF_PSR_PRSCV_OFFSET 0
60 #define PSIF_PSR_PRSCV_SIZE 12
62 /* Bitfields in receive hold register. */
63 #define PSIF_RHR_RXDATA_OFFSET 0
64 #define PSIF_RHR_RXDATA_SIZE 8
66 /* Bitfields in transmit hold register. */
67 #define PSIF_THR_TXDATA_OFFSET 0
68 #define PSIF_THR_TXDATA_SIZE 8
70 /* Bit manipulation macros */
71 #define PSIF_BIT(name) \
72 (1 << PSIF_##name##_OFFSET)
74 #define PSIF_BF(name, value) \
75 (((value) & ((1 << PSIF_##name##_SIZE) - 1)) \
76 << PSIF_##name##_OFFSET)
78 #define PSIF_BFEXT(name, value) \
79 (((value) >> PSIF_##name##_OFFSET) \
80 & ((1 << PSIF_##name##_SIZE) - 1))
82 #define PSIF_BFINS(name, value, old) \
83 (((old) & ~(((1 << PSIF_##name##_SIZE) - 1) \
84 << PSIF_##name##_OFFSET)) \
85 | PSIF_BF(name, value))
87 /* Register access macros */
88 #define psif_readl(port, reg) \
89 __raw_readl((port)->regs + PSIF_##reg)
91 #define psif_writel(port, reg, value) \
92 __raw_writel((value), (port)->regs + PSIF_##reg)
95 struct platform_device
*pdev
;
101 /* Prevent concurrent writes to PSIF THR. */
105 static irqreturn_t
psif_interrupt(int irq
, void *_ptr
)
107 struct psif
*psif
= _ptr
;
108 int retval
= IRQ_NONE
;
109 unsigned int io_flags
= 0;
110 unsigned long status
;
112 status
= psif_readl(psif
, SR
);
114 if (status
& PSIF_BIT(RXRDY
)) {
115 unsigned char val
= (unsigned char) psif_readl(psif
, RHR
);
117 if (status
& PSIF_BIT(PARITY
))
118 io_flags
|= SERIO_PARITY
;
119 if (status
& PSIF_BIT(OVRUN
))
120 dev_err(&psif
->pdev
->dev
, "overrun read error\n");
122 serio_interrupt(psif
->io
, val
, io_flags
);
124 retval
= IRQ_HANDLED
;
130 static int psif_write(struct serio
*io
, unsigned char val
)
132 struct psif
*psif
= io
->port_data
;
137 spin_lock_irqsave(&psif
->lock
, flags
);
139 while (!(psif_readl(psif
, SR
) & PSIF_BIT(TXEMPTY
)) && timeout
--)
143 psif_writel(psif
, THR
, val
);
145 dev_dbg(&psif
->pdev
->dev
, "timeout writing to THR\n");
149 spin_unlock_irqrestore(&psif
->lock
, flags
);
154 static int psif_open(struct serio
*io
)
156 struct psif
*psif
= io
->port_data
;
159 retval
= clk_enable(psif
->pclk
);
163 psif_writel(psif
, CR
, PSIF_BIT(CR_TXEN
) | PSIF_BIT(CR_RXEN
));
164 psif_writel(psif
, IER
, PSIF_BIT(RXRDY
));
171 static void psif_close(struct serio
*io
)
173 struct psif
*psif
= io
->port_data
;
177 psif_writel(psif
, IDR
, ~0UL);
178 psif_writel(psif
, CR
, PSIF_BIT(CR_TXDIS
) | PSIF_BIT(CR_RXDIS
));
180 clk_disable(psif
->pclk
);
183 static void psif_set_prescaler(struct psif
*psif
)
186 unsigned long rate
= clk_get_rate(psif
->pclk
);
188 /* PRSCV = Pulse length (100 us) * PSIF module frequency. */
189 prscv
= 100 * (rate
/ 1000000UL);
191 if (prscv
> ((1<<PSIF_PSR_PRSCV_SIZE
) - 1)) {
192 prscv
= (1<<PSIF_PSR_PRSCV_SIZE
) - 1;
193 dev_dbg(&psif
->pdev
->dev
, "pclk too fast, "
194 "prescaler set to max\n");
197 clk_enable(psif
->pclk
);
198 psif_writel(psif
, PSR
, prscv
);
199 clk_disable(psif
->pclk
);
202 static int __init
psif_probe(struct platform_device
*pdev
)
204 struct resource
*regs
;
211 psif
= kzalloc(sizeof(struct psif
), GFP_KERNEL
);
213 dev_dbg(&pdev
->dev
, "out of memory\n");
219 io
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
221 dev_dbg(&pdev
->dev
, "out of memory\n");
227 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
229 dev_dbg(&pdev
->dev
, "no mmio resources defined\n");
234 psif
->regs
= ioremap(regs
->start
, resource_size(regs
));
237 dev_dbg(&pdev
->dev
, "could not map I/O memory\n");
241 pclk
= clk_get(&pdev
->dev
, "pclk");
243 dev_dbg(&pdev
->dev
, "could not get peripheral clock\n");
249 /* Reset the PSIF to enter at a known state. */
250 ret
= clk_enable(pclk
);
252 dev_dbg(&pdev
->dev
, "could not enable pclk\n");
255 psif_writel(psif
, CR
, PSIF_BIT(CR_SWRST
));
258 irq
= platform_get_irq(pdev
, 0);
260 dev_dbg(&pdev
->dev
, "could not get irq\n");
264 ret
= request_irq(irq
, psif_interrupt
, IRQF_SHARED
, "at32psif", psif
);
266 dev_dbg(&pdev
->dev
, "could not request irq %d\n", irq
);
271 io
->id
.type
= SERIO_8042
;
272 io
->write
= psif_write
;
273 io
->open
= psif_open
;
274 io
->close
= psif_close
;
275 snprintf(io
->name
, sizeof(io
->name
), "AVR32 PS/2 port%d", pdev
->id
);
276 snprintf(io
->phys
, sizeof(io
->phys
), "at32psif/serio%d", pdev
->id
);
277 io
->port_data
= psif
;
278 io
->dev
.parent
= &pdev
->dev
;
280 psif_set_prescaler(psif
);
282 spin_lock_init(&psif
->lock
);
283 serio_register_port(psif
->io
);
284 platform_set_drvdata(pdev
, psif
);
286 dev_info(&pdev
->dev
, "Atmel AVR32 PSIF PS/2 driver on 0x%08x irq %d\n",
287 (int)psif
->regs
, psif
->irq
);
303 static int __exit
psif_remove(struct platform_device
*pdev
)
305 struct psif
*psif
= platform_get_drvdata(pdev
);
307 psif_writel(psif
, IDR
, ~0UL);
308 psif_writel(psif
, CR
, PSIF_BIT(CR_TXDIS
) | PSIF_BIT(CR_RXDIS
));
310 serio_unregister_port(psif
->io
);
312 free_irq(psif
->irq
, psif
);
316 platform_set_drvdata(pdev
, NULL
);
322 static int psif_suspend(struct platform_device
*pdev
, pm_message_t state
)
324 struct psif
*psif
= platform_get_drvdata(pdev
);
327 psif_writel(psif
, CR
, PSIF_BIT(CR_RXDIS
) | PSIF_BIT(CR_TXDIS
));
328 clk_disable(psif
->pclk
);
334 static int psif_resume(struct platform_device
*pdev
)
336 struct psif
*psif
= platform_get_drvdata(pdev
);
339 clk_enable(psif
->pclk
);
340 psif_set_prescaler(psif
);
341 psif_writel(psif
, CR
, PSIF_BIT(CR_RXEN
) | PSIF_BIT(CR_TXEN
));
347 #define psif_suspend NULL
348 #define psif_resume NULL
351 static struct platform_driver psif_driver
= {
352 .remove
= __exit_p(psif_remove
),
354 .name
= "atmel_psif",
356 .suspend
= psif_suspend
,
357 .resume
= psif_resume
,
360 static int __init
psif_init(void)
362 return platform_driver_probe(&psif_driver
, psif_probe
);
365 static void __exit
psif_exit(void)
367 platform_driver_unregister(&psif_driver
);
370 module_init(psif_init
);
371 module_exit(psif_exit
);
373 MODULE_AUTHOR("Hans-Christian Egtvedt <hans-christian.egtvedt@atmel.com>");
374 MODULE_DESCRIPTION("Atmel AVR32 PSIF PS/2 driver");
375 MODULE_LICENSE("GPL");