1 // SPDX-License-Identifier: GPL-2.0-only
3 * PXA930 track ball mouse driver
5 * Copyright (C) 2007 Marvell International Ltd.
6 * 2008-02-28: Yong Yao <yaoyong@marvell.com>
10 #include <linux/input.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/delay.h>
16 #include <linux/slab.h>
18 #include <mach/hardware.h>
19 #include <linux/platform_data/mouse-pxa930_trkball.h>
21 /* Trackball Controller Register Definitions */
23 #define TBCNTR (0x0010)
24 #define TBSBC (0x0014)
26 #define TBCR_TBRST (1 << 1)
27 #define TBCR_TBSB (1 << 10)
29 #define TBCR_Y_FLT(n) (((n) & 0xf) << 6)
30 #define TBCR_X_FLT(n) (((n) & 0xf) << 2)
32 #define TBCNTR_YM(n) (((n) >> 24) & 0xff)
33 #define TBCNTR_YP(n) (((n) >> 16) & 0xff)
34 #define TBCNTR_XM(n) (((n) >> 8) & 0xff)
35 #define TBCNTR_XP(n) ((n) & 0xff)
37 #define TBSBC_TBSBC (0x1)
39 struct pxa930_trkball {
40 struct pxa930_trkball_platform_data *pdata;
42 /* Memory Mapped Register */
44 void __iomem *mmio_base;
46 struct input_dev *input;
49 static irqreturn_t pxa930_trkball_interrupt(int irq, void *dev_id)
51 struct pxa930_trkball *trkball = dev_id;
52 struct input_dev *input = trkball->input;
55 /* According to the spec software must read TBCNTR twice:
56 * if the read value is the same, the reading is valid
58 tbcntr = __raw_readl(trkball->mmio_base + TBCNTR);
60 if (tbcntr == __raw_readl(trkball->mmio_base + TBCNTR)) {
61 x = (TBCNTR_XP(tbcntr) - TBCNTR_XM(tbcntr)) / 2;
62 y = (TBCNTR_YP(tbcntr) - TBCNTR_YM(tbcntr)) / 2;
64 input_report_rel(input, REL_X, x);
65 input_report_rel(input, REL_Y, y);
69 __raw_writel(TBSBC_TBSBC, trkball->mmio_base + TBSBC);
70 __raw_writel(0, trkball->mmio_base + TBSBC);
75 /* For TBCR, we need to wait for a while to make sure it has been modified. */
76 static int write_tbcr(struct pxa930_trkball *trkball, int v)
80 __raw_writel(v, trkball->mmio_base + TBCR);
83 if (__raw_readl(trkball->mmio_base + TBCR) == v)
89 pr_err("%s: timed out writing TBCR(%x)!\n", __func__, v);
96 static void pxa930_trkball_config(struct pxa930_trkball *trkball)
100 /* According to spec, need to write the filters of x,y to 0xf first! */
101 tbcr = __raw_readl(trkball->mmio_base + TBCR);
102 write_tbcr(trkball, tbcr | TBCR_X_FLT(0xf) | TBCR_Y_FLT(0xf));
103 write_tbcr(trkball, TBCR_X_FLT(trkball->pdata->x_filter) |
104 TBCR_Y_FLT(trkball->pdata->y_filter));
106 /* According to spec, set TBCR_TBRST first, before clearing it! */
107 tbcr = __raw_readl(trkball->mmio_base + TBCR);
108 write_tbcr(trkball, tbcr | TBCR_TBRST);
109 write_tbcr(trkball, tbcr & ~TBCR_TBRST);
111 __raw_writel(TBSBC_TBSBC, trkball->mmio_base + TBSBC);
112 __raw_writel(0, trkball->mmio_base + TBSBC);
114 pr_debug("%s: final TBCR=%x!\n", __func__,
115 __raw_readl(trkball->mmio_base + TBCR));
118 static int pxa930_trkball_open(struct input_dev *dev)
120 struct pxa930_trkball *trkball = input_get_drvdata(dev);
122 pxa930_trkball_config(trkball);
127 static void pxa930_trkball_disable(struct pxa930_trkball *trkball)
129 uint32_t tbcr = __raw_readl(trkball->mmio_base + TBCR);
131 /* Held in reset, gate the 32-KHz input clock off */
132 write_tbcr(trkball, tbcr | TBCR_TBRST);
135 static void pxa930_trkball_close(struct input_dev *dev)
137 struct pxa930_trkball *trkball = input_get_drvdata(dev);
139 pxa930_trkball_disable(trkball);
142 static int pxa930_trkball_probe(struct platform_device *pdev)
144 struct pxa930_trkball *trkball;
145 struct input_dev *input;
146 struct resource *res;
149 irq = platform_get_irq(pdev, 0);
153 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
155 dev_err(&pdev->dev, "failed to get register memory\n");
159 trkball = kzalloc(sizeof(struct pxa930_trkball), GFP_KERNEL);
163 trkball->pdata = dev_get_platdata(&pdev->dev);
164 if (!trkball->pdata) {
165 dev_err(&pdev->dev, "no platform data defined\n");
170 trkball->mmio_base = ioremap(res->start, resource_size(res));
171 if (!trkball->mmio_base) {
172 dev_err(&pdev->dev, "failed to ioremap registers\n");
177 /* held the module in reset, will be enabled in open() */
178 pxa930_trkball_disable(trkball);
180 error = request_irq(irq, pxa930_trkball_interrupt, 0,
181 pdev->name, trkball);
183 dev_err(&pdev->dev, "failed to request irq: %d\n", error);
187 platform_set_drvdata(pdev, trkball);
189 input = input_allocate_device();
191 dev_err(&pdev->dev, "failed to allocate input device\n");
193 goto failed_free_irq;
196 input->name = pdev->name;
197 input->id.bustype = BUS_HOST;
198 input->open = pxa930_trkball_open;
199 input->close = pxa930_trkball_close;
200 input->dev.parent = &pdev->dev;
201 input_set_drvdata(input, trkball);
203 trkball->input = input;
205 input_set_capability(input, EV_REL, REL_X);
206 input_set_capability(input, EV_REL, REL_Y);
208 error = input_register_device(input);
210 dev_err(&pdev->dev, "unable to register input device\n");
211 goto failed_free_input;
217 input_free_device(input);
219 free_irq(irq, trkball);
221 iounmap(trkball->mmio_base);
227 static int pxa930_trkball_remove(struct platform_device *pdev)
229 struct pxa930_trkball *trkball = platform_get_drvdata(pdev);
230 int irq = platform_get_irq(pdev, 0);
232 input_unregister_device(trkball->input);
233 free_irq(irq, trkball);
234 iounmap(trkball->mmio_base);
240 static struct platform_driver pxa930_trkball_driver = {
242 .name = "pxa930-trkball",
244 .probe = pxa930_trkball_probe,
245 .remove = pxa930_trkball_remove,
247 module_platform_driver(pxa930_trkball_driver);
249 MODULE_AUTHOR("Yong Yao <yaoyong@marvell.com>");
250 MODULE_DESCRIPTION("PXA930 Trackball Mouse Driver");
251 MODULE_LICENSE("GPL");