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>
21 #include <linux/slab.h>
23 /* PSIF register offsets */
33 /* Bitfields in control register. */
34 #define PSIF_CR_RXDIS_OFFSET 1
35 #define PSIF_CR_RXDIS_SIZE 1
36 #define PSIF_CR_RXEN_OFFSET 0
37 #define PSIF_CR_RXEN_SIZE 1
38 #define PSIF_CR_SWRST_OFFSET 15
39 #define PSIF_CR_SWRST_SIZE 1
40 #define PSIF_CR_TXDIS_OFFSET 9
41 #define PSIF_CR_TXDIS_SIZE 1
42 #define PSIF_CR_TXEN_OFFSET 8
43 #define PSIF_CR_TXEN_SIZE 1
45 /* Bitfields in interrupt disable, enable, mask and status register. */
46 #define PSIF_NACK_OFFSET 8
47 #define PSIF_NACK_SIZE 1
48 #define PSIF_OVRUN_OFFSET 5
49 #define PSIF_OVRUN_SIZE 1
50 #define PSIF_PARITY_OFFSET 9
51 #define PSIF_PARITY_SIZE 1
52 #define PSIF_RXRDY_OFFSET 4
53 #define PSIF_RXRDY_SIZE 1
54 #define PSIF_TXEMPTY_OFFSET 1
55 #define PSIF_TXEMPTY_SIZE 1
56 #define PSIF_TXRDY_OFFSET 0
57 #define PSIF_TXRDY_SIZE 1
59 /* Bitfields in prescale register. */
60 #define PSIF_PSR_PRSCV_OFFSET 0
61 #define PSIF_PSR_PRSCV_SIZE 12
63 /* Bitfields in receive hold register. */
64 #define PSIF_RHR_RXDATA_OFFSET 0
65 #define PSIF_RHR_RXDATA_SIZE 8
67 /* Bitfields in transmit hold register. */
68 #define PSIF_THR_TXDATA_OFFSET 0
69 #define PSIF_THR_TXDATA_SIZE 8
71 /* Bit manipulation macros */
72 #define PSIF_BIT(name) \
73 (1 << PSIF_##name##_OFFSET)
75 #define PSIF_BF(name, value) \
76 (((value) & ((1 << PSIF_##name##_SIZE) - 1)) \
77 << PSIF_##name##_OFFSET)
79 #define PSIF_BFEXT(name, value) \
80 (((value) >> PSIF_##name##_OFFSET) \
81 & ((1 << PSIF_##name##_SIZE) - 1))
83 #define PSIF_BFINS(name, value, old) \
84 (((old) & ~(((1 << PSIF_##name##_SIZE) - 1) \
85 << PSIF_##name##_OFFSET)) \
86 | PSIF_BF(name, value))
88 /* Register access macros */
89 #define psif_readl(port, reg) \
90 __raw_readl((port)->regs + PSIF_##reg)
92 #define psif_writel(port, reg, value) \
93 __raw_writel((value), (port)->regs + PSIF_##reg)
96 struct platform_device
*pdev
;
101 /* Prevent concurrent writes to PSIF THR. */
106 static irqreturn_t
psif_interrupt(int irq
, void *_ptr
)
108 struct psif
*psif
= _ptr
;
109 int retval
= IRQ_NONE
;
110 unsigned int io_flags
= 0;
111 unsigned long status
;
113 status
= psif_readl(psif
, SR
);
115 if (status
& PSIF_BIT(RXRDY
)) {
116 unsigned char val
= (unsigned char) psif_readl(psif
, RHR
);
118 if (status
& PSIF_BIT(PARITY
))
119 io_flags
|= SERIO_PARITY
;
120 if (status
& PSIF_BIT(OVRUN
))
121 dev_err(&psif
->pdev
->dev
, "overrun read error\n");
123 serio_interrupt(psif
->io
, val
, io_flags
);
125 retval
= IRQ_HANDLED
;
131 static int psif_write(struct serio
*io
, unsigned char val
)
133 struct psif
*psif
= io
->port_data
;
138 spin_lock_irqsave(&psif
->lock
, flags
);
140 while (!(psif_readl(psif
, SR
) & PSIF_BIT(TXEMPTY
)) && timeout
--)
144 psif_writel(psif
, THR
, val
);
146 dev_dbg(&psif
->pdev
->dev
, "timeout writing to THR\n");
150 spin_unlock_irqrestore(&psif
->lock
, flags
);
155 static int psif_open(struct serio
*io
)
157 struct psif
*psif
= io
->port_data
;
160 retval
= clk_enable(psif
->pclk
);
164 psif_writel(psif
, CR
, PSIF_BIT(CR_TXEN
) | PSIF_BIT(CR_RXEN
));
165 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
);
216 io
= kzalloc(sizeof(struct serio
), GFP_KERNEL
);
223 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
225 dev_dbg(&pdev
->dev
, "no mmio resources defined\n");
230 psif
->regs
= ioremap(regs
->start
, resource_size(regs
));
233 dev_dbg(&pdev
->dev
, "could not map I/O memory\n");
237 pclk
= clk_get(&pdev
->dev
, "pclk");
239 dev_dbg(&pdev
->dev
, "could not get peripheral clock\n");
245 /* Reset the PSIF to enter at a known state. */
246 ret
= clk_enable(pclk
);
248 dev_dbg(&pdev
->dev
, "could not enable pclk\n");
251 psif_writel(psif
, CR
, PSIF_BIT(CR_SWRST
));
254 irq
= platform_get_irq(pdev
, 0);
256 dev_dbg(&pdev
->dev
, "could not get irq\n");
260 ret
= request_irq(irq
, psif_interrupt
, IRQF_SHARED
, "at32psif", psif
);
262 dev_dbg(&pdev
->dev
, "could not request irq %d\n", irq
);
267 io
->id
.type
= SERIO_8042
;
268 io
->write
= psif_write
;
269 io
->open
= psif_open
;
270 io
->close
= psif_close
;
271 snprintf(io
->name
, sizeof(io
->name
), "AVR32 PS/2 port%d", pdev
->id
);
272 snprintf(io
->phys
, sizeof(io
->phys
), "at32psif/serio%d", pdev
->id
);
273 io
->port_data
= psif
;
274 io
->dev
.parent
= &pdev
->dev
;
276 psif_set_prescaler(psif
);
278 spin_lock_init(&psif
->lock
);
279 serio_register_port(psif
->io
);
280 platform_set_drvdata(pdev
, psif
);
282 dev_info(&pdev
->dev
, "Atmel AVR32 PSIF PS/2 driver on 0x%08x irq %d\n",
283 (int)psif
->regs
, psif
->irq
);
298 static int __exit
psif_remove(struct platform_device
*pdev
)
300 struct psif
*psif
= platform_get_drvdata(pdev
);
302 psif_writel(psif
, IDR
, ~0UL);
303 psif_writel(psif
, CR
, PSIF_BIT(CR_TXDIS
) | PSIF_BIT(CR_RXDIS
));
305 serio_unregister_port(psif
->io
);
307 free_irq(psif
->irq
, psif
);
314 #ifdef CONFIG_PM_SLEEP
315 static int psif_suspend(struct device
*dev
)
317 struct platform_device
*pdev
= to_platform_device(dev
);
318 struct psif
*psif
= platform_get_drvdata(pdev
);
321 psif_writel(psif
, CR
, PSIF_BIT(CR_RXDIS
) | PSIF_BIT(CR_TXDIS
));
322 clk_disable(psif
->pclk
);
328 static int psif_resume(struct device
*dev
)
330 struct platform_device
*pdev
= to_platform_device(dev
);
331 struct psif
*psif
= platform_get_drvdata(pdev
);
334 clk_enable(psif
->pclk
);
335 psif_set_prescaler(psif
);
336 psif_writel(psif
, CR
, PSIF_BIT(CR_RXEN
) | PSIF_BIT(CR_TXEN
));
343 static SIMPLE_DEV_PM_OPS(psif_pm_ops
, psif_suspend
, psif_resume
);
345 static struct platform_driver psif_driver
= {
346 .remove
= __exit_p(psif_remove
),
348 .name
= "atmel_psif",
353 module_platform_driver_probe(psif_driver
, psif_probe
);
355 MODULE_AUTHOR("Hans-Christian Egtvedt <egtvedt@samfundet.no>");
356 MODULE_DESCRIPTION("Atmel AVR32 PSIF PS/2 driver");
357 MODULE_LICENSE("GPL");