2 * PXA930 track ball mouse driver
4 * Copyright (C) 2007 Marvell International Ltd.
5 * 2008-02-28: Yong Yao <yaoyong@marvell.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/input.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/delay.h>
19 #include <linux/slab.h>
21 #include <mach/hardware.h>
22 #include <linux/platform_data/mouse-pxa930_trkball.h>
24 /* Trackball Controller Register Definitions */
26 #define TBCNTR (0x0010)
27 #define TBSBC (0x0014)
29 #define TBCR_TBRST (1 << 1)
30 #define TBCR_TBSB (1 << 10)
32 #define TBCR_Y_FLT(n) (((n) & 0xf) << 6)
33 #define TBCR_X_FLT(n) (((n) & 0xf) << 2)
35 #define TBCNTR_YM(n) (((n) >> 24) & 0xff)
36 #define TBCNTR_YP(n) (((n) >> 16) & 0xff)
37 #define TBCNTR_XM(n) (((n) >> 8) & 0xff)
38 #define TBCNTR_XP(n) ((n) & 0xff)
40 #define TBSBC_TBSBC (0x1)
42 struct pxa930_trkball {
43 struct pxa930_trkball_platform_data *pdata;
45 /* Memory Mapped Register */
47 void __iomem *mmio_base;
49 struct input_dev *input;
52 static irqreturn_t pxa930_trkball_interrupt(int irq, void *dev_id)
54 struct pxa930_trkball *trkball = dev_id;
55 struct input_dev *input = trkball->input;
58 /* According to the spec software must read TBCNTR twice:
59 * if the read value is the same, the reading is valid
61 tbcntr = __raw_readl(trkball->mmio_base + TBCNTR);
63 if (tbcntr == __raw_readl(trkball->mmio_base + TBCNTR)) {
64 x = (TBCNTR_XP(tbcntr) - TBCNTR_XM(tbcntr)) / 2;
65 y = (TBCNTR_YP(tbcntr) - TBCNTR_YM(tbcntr)) / 2;
67 input_report_rel(input, REL_X, x);
68 input_report_rel(input, REL_Y, y);
72 __raw_writel(TBSBC_TBSBC, trkball->mmio_base + TBSBC);
73 __raw_writel(0, trkball->mmio_base + TBSBC);
78 /* For TBCR, we need to wait for a while to make sure it has been modified. */
79 static int write_tbcr(struct pxa930_trkball *trkball, int v)
83 __raw_writel(v, trkball->mmio_base + TBCR);
86 if (__raw_readl(trkball->mmio_base + TBCR) == v)
92 pr_err("%s: timed out writing TBCR(%x)!\n", __func__, v);
99 static void pxa930_trkball_config(struct pxa930_trkball *trkball)
103 /* According to spec, need to write the filters of x,y to 0xf first! */
104 tbcr = __raw_readl(trkball->mmio_base + TBCR);
105 write_tbcr(trkball, tbcr | TBCR_X_FLT(0xf) | TBCR_Y_FLT(0xf));
106 write_tbcr(trkball, TBCR_X_FLT(trkball->pdata->x_filter) |
107 TBCR_Y_FLT(trkball->pdata->y_filter));
109 /* According to spec, set TBCR_TBRST first, before clearing it! */
110 tbcr = __raw_readl(trkball->mmio_base + TBCR);
111 write_tbcr(trkball, tbcr | TBCR_TBRST);
112 write_tbcr(trkball, tbcr & ~TBCR_TBRST);
114 __raw_writel(TBSBC_TBSBC, trkball->mmio_base + TBSBC);
115 __raw_writel(0, trkball->mmio_base + TBSBC);
117 pr_debug("%s: final TBCR=%x!\n", __func__,
118 __raw_readl(trkball->mmio_base + TBCR));
121 static int pxa930_trkball_open(struct input_dev *dev)
123 struct pxa930_trkball *trkball = input_get_drvdata(dev);
125 pxa930_trkball_config(trkball);
130 static void pxa930_trkball_disable(struct pxa930_trkball *trkball)
132 uint32_t tbcr = __raw_readl(trkball->mmio_base + TBCR);
134 /* Held in reset, gate the 32-KHz input clock off */
135 write_tbcr(trkball, tbcr | TBCR_TBRST);
138 static void pxa930_trkball_close(struct input_dev *dev)
140 struct pxa930_trkball *trkball = input_get_drvdata(dev);
142 pxa930_trkball_disable(trkball);
145 static int pxa930_trkball_probe(struct platform_device *pdev)
147 struct pxa930_trkball *trkball;
148 struct input_dev *input;
149 struct resource *res;
152 irq = platform_get_irq(pdev, 0);
154 dev_err(&pdev->dev, "failed to get trkball irq\n");
158 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
160 dev_err(&pdev->dev, "failed to get register memory\n");
164 trkball = kzalloc(sizeof(struct pxa930_trkball), GFP_KERNEL);
168 trkball->pdata = dev_get_platdata(&pdev->dev);
169 if (!trkball->pdata) {
170 dev_err(&pdev->dev, "no platform data defined\n");
175 trkball->mmio_base = ioremap_nocache(res->start, resource_size(res));
176 if (!trkball->mmio_base) {
177 dev_err(&pdev->dev, "failed to ioremap registers\n");
182 /* held the module in reset, will be enabled in open() */
183 pxa930_trkball_disable(trkball);
185 error = request_irq(irq, pxa930_trkball_interrupt, 0,
186 pdev->name, trkball);
188 dev_err(&pdev->dev, "failed to request irq: %d\n", error);
192 platform_set_drvdata(pdev, trkball);
194 input = input_allocate_device();
196 dev_err(&pdev->dev, "failed to allocate input device\n");
198 goto failed_free_irq;
201 input->name = pdev->name;
202 input->id.bustype = BUS_HOST;
203 input->open = pxa930_trkball_open;
204 input->close = pxa930_trkball_close;
205 input->dev.parent = &pdev->dev;
206 input_set_drvdata(input, trkball);
208 trkball->input = input;
210 input_set_capability(input, EV_REL, REL_X);
211 input_set_capability(input, EV_REL, REL_Y);
213 error = input_register_device(input);
215 dev_err(&pdev->dev, "unable to register input device\n");
216 goto failed_free_input;
222 input_free_device(input);
224 free_irq(irq, trkball);
226 iounmap(trkball->mmio_base);
232 static int pxa930_trkball_remove(struct platform_device *pdev)
234 struct pxa930_trkball *trkball = platform_get_drvdata(pdev);
235 int irq = platform_get_irq(pdev, 0);
237 input_unregister_device(trkball->input);
238 free_irq(irq, trkball);
239 iounmap(trkball->mmio_base);
245 static struct platform_driver pxa930_trkball_driver = {
247 .name = "pxa930-trkball",
249 .probe = pxa930_trkball_probe,
250 .remove = pxa930_trkball_remove,
252 module_platform_driver(pxa930_trkball_driver);
254 MODULE_AUTHOR("Yong Yao <yaoyong@marvell.com>");
255 MODULE_DESCRIPTION("PXA930 Trackball Mouse Driver");
256 MODULE_LICENSE("GPL");