Merge tag 'virtio-next-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / input / serio / arc_ps2.c
blobb571eb3e4efc92c5a2aef1d0ce665cb06038d85d
1 /*
2 * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Driver is originally developed by Pavel Sokolov <psokolov@synopsys.com>
9 */
11 #include <linux/module.h>
12 #include <linux/interrupt.h>
13 #include <linux/input.h>
14 #include <linux/serio.h>
15 #include <linux/platform_device.h>
16 #include <linux/io.h>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
20 #define ARC_PS2_PORTS 2
22 #define ARC_ARC_PS2_ID 0x0001f609
24 #define STAT_TIMEOUT 128
26 #define PS2_STAT_RX_FRM_ERR (1)
27 #define PS2_STAT_RX_BUF_OVER (1 << 1)
28 #define PS2_STAT_RX_INT_EN (1 << 2)
29 #define PS2_STAT_RX_VAL (1 << 3)
30 #define PS2_STAT_TX_ISNOT_FUL (1 << 4)
31 #define PS2_STAT_TX_INT_EN (1 << 5)
33 struct arc_ps2_port {
34 void __iomem *data_addr;
35 void __iomem *status_addr;
36 struct serio *io;
39 struct arc_ps2_data {
40 struct arc_ps2_port port[ARC_PS2_PORTS];
41 void __iomem *addr;
42 unsigned int frame_error;
43 unsigned int buf_overflow;
44 unsigned int total_int;
47 static void arc_ps2_check_rx(struct arc_ps2_data *arc_ps2,
48 struct arc_ps2_port *port)
50 unsigned int timeout = 1000;
51 unsigned int flag, status;
52 unsigned char data;
54 do {
55 status = ioread32(port->status_addr);
56 if (!(status & PS2_STAT_RX_VAL))
57 return;
59 data = ioread32(port->data_addr) & 0xff;
61 flag = 0;
62 arc_ps2->total_int++;
63 if (status & PS2_STAT_RX_FRM_ERR) {
64 arc_ps2->frame_error++;
65 flag |= SERIO_PARITY;
66 } else if (status & PS2_STAT_RX_BUF_OVER) {
67 arc_ps2->buf_overflow++;
68 flag |= SERIO_FRAME;
71 serio_interrupt(port->io, data, flag);
72 } while (--timeout);
74 dev_err(&port->io->dev, "PS/2 hardware stuck\n");
77 static irqreturn_t arc_ps2_interrupt(int irq, void *dev)
79 struct arc_ps2_data *arc_ps2 = dev;
80 int i;
82 for (i = 0; i < ARC_PS2_PORTS; i++)
83 arc_ps2_check_rx(arc_ps2, &arc_ps2->port[i]);
85 return IRQ_HANDLED;
88 static int arc_ps2_write(struct serio *io, unsigned char val)
90 unsigned status;
91 struct arc_ps2_port *port = io->port_data;
92 int timeout = STAT_TIMEOUT;
94 do {
95 status = ioread32(port->status_addr);
96 cpu_relax();
98 if (status & PS2_STAT_TX_ISNOT_FUL) {
99 iowrite32(val & 0xff, port->data_addr);
100 return 0;
103 } while (--timeout);
105 dev_err(&io->dev, "write timeout\n");
106 return -ETIMEDOUT;
109 static int arc_ps2_open(struct serio *io)
111 struct arc_ps2_port *port = io->port_data;
113 iowrite32(PS2_STAT_RX_INT_EN, port->status_addr);
115 return 0;
118 static void arc_ps2_close(struct serio *io)
120 struct arc_ps2_port *port = io->port_data;
122 iowrite32(ioread32(port->status_addr) & ~PS2_STAT_RX_INT_EN,
123 port->status_addr);
126 static void __iomem *arc_ps2_calc_addr(struct arc_ps2_data *arc_ps2,
127 int index, bool status)
129 void __iomem *addr;
131 addr = arc_ps2->addr + 4 + 4 * index;
132 if (status)
133 addr += ARC_PS2_PORTS * 4;
135 return addr;
138 static void arc_ps2_inhibit_ports(struct arc_ps2_data *arc_ps2)
140 void __iomem *addr;
141 u32 val;
142 int i;
144 for (i = 0; i < ARC_PS2_PORTS; i++) {
145 addr = arc_ps2_calc_addr(arc_ps2, i, true);
146 val = ioread32(addr);
147 val &= ~(PS2_STAT_RX_INT_EN | PS2_STAT_TX_INT_EN);
148 iowrite32(val, addr);
152 static int arc_ps2_create_port(struct platform_device *pdev,
153 struct arc_ps2_data *arc_ps2,
154 int index)
156 struct arc_ps2_port *port = &arc_ps2->port[index];
157 struct serio *io;
159 io = kzalloc(sizeof(struct serio), GFP_KERNEL);
160 if (!io)
161 return -ENOMEM;
163 io->id.type = SERIO_8042;
164 io->write = arc_ps2_write;
165 io->open = arc_ps2_open;
166 io->close = arc_ps2_close;
167 snprintf(io->name, sizeof(io->name), "ARC PS/2 port%d", index);
168 snprintf(io->phys, sizeof(io->phys), "arc/serio%d", index);
169 io->port_data = port;
171 port->io = io;
173 port->data_addr = arc_ps2_calc_addr(arc_ps2, index, false);
174 port->status_addr = arc_ps2_calc_addr(arc_ps2, index, true);
176 dev_dbg(&pdev->dev, "port%d is allocated (data = 0x%p, status = 0x%p)\n",
177 index, port->data_addr, port->status_addr);
179 serio_register_port(port->io);
180 return 0;
183 static int arc_ps2_probe(struct platform_device *pdev)
185 struct arc_ps2_data *arc_ps2;
186 struct resource *res;
187 int irq;
188 int error, id, i;
190 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
191 if (!res) {
192 dev_err(&pdev->dev, "no IO memory defined\n");
193 return -EINVAL;
196 irq = platform_get_irq_byname(pdev, "arc_ps2_irq");
197 if (irq < 0) {
198 dev_err(&pdev->dev, "no IRQ defined\n");
199 return -EINVAL;
202 arc_ps2 = devm_kzalloc(&pdev->dev, sizeof(struct arc_ps2_data),
203 GFP_KERNEL);
204 if (!arc_ps2) {
205 dev_err(&pdev->dev, "out of memory\n");
206 return -ENOMEM;
209 arc_ps2->addr = devm_request_and_ioremap(&pdev->dev, res);
210 if (!arc_ps2->addr)
211 return -EBUSY;
213 dev_info(&pdev->dev, "irq = %d, address = 0x%p, ports = %i\n",
214 irq, arc_ps2->addr, ARC_PS2_PORTS);
216 id = ioread32(arc_ps2->addr);
217 if (id != ARC_ARC_PS2_ID) {
218 dev_err(&pdev->dev, "device id does not match\n");
219 return -ENXIO;
222 arc_ps2_inhibit_ports(arc_ps2);
224 error = devm_request_irq(&pdev->dev, irq, arc_ps2_interrupt,
225 0, "arc_ps2", arc_ps2);
226 if (error) {
227 dev_err(&pdev->dev, "Could not allocate IRQ\n");
228 return error;
231 for (i = 0; i < ARC_PS2_PORTS; i++) {
232 error = arc_ps2_create_port(pdev, arc_ps2, i);
233 if (error) {
234 while (--i >= 0)
235 serio_unregister_port(arc_ps2->port[i].io);
236 return error;
240 platform_set_drvdata(pdev, arc_ps2);
242 return 0;
245 static int arc_ps2_remove(struct platform_device *pdev)
247 struct arc_ps2_data *arc_ps2 = platform_get_drvdata(pdev);
248 int i;
250 for (i = 0; i < ARC_PS2_PORTS; i++)
251 serio_unregister_port(arc_ps2->port[i].io);
253 dev_dbg(&pdev->dev, "interrupt count = %i\n", arc_ps2->total_int);
254 dev_dbg(&pdev->dev, "frame error count = %i\n", arc_ps2->frame_error);
255 dev_dbg(&pdev->dev, "buffer overflow count = %i\n",
256 arc_ps2->buf_overflow);
258 return 0;
261 static struct platform_driver arc_ps2_driver = {
262 .driver = {
263 .name = "arc_ps2",
264 .owner = THIS_MODULE,
266 .probe = arc_ps2_probe,
267 .remove = arc_ps2_remove,
270 module_platform_driver(arc_ps2_driver);
272 MODULE_LICENSE("GPL");
273 MODULE_AUTHOR("Pavel Sokolov <psokolov@synopsys.com>");
274 MODULE_DESCRIPTION("ARC PS/2 Driver");