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 <linux/platform_data/mouse-pxa930_trkball.h>
20 /* Trackball Controller Register Definitions */
22 #define TBCNTR (0x0010)
23 #define TBSBC (0x0014)
25 #define TBCR_TBRST (1 << 1)
26 #define TBCR_TBSB (1 << 10)
28 #define TBCR_Y_FLT(n) (((n) & 0xf) << 6)
29 #define TBCR_X_FLT(n) (((n) & 0xf) << 2)
31 #define TBCNTR_YM(n) (((n) >> 24) & 0xff)
32 #define TBCNTR_YP(n) (((n) >> 16) & 0xff)
33 #define TBCNTR_XM(n) (((n) >> 8) & 0xff)
34 #define TBCNTR_XP(n) ((n) & 0xff)
36 #define TBSBC_TBSBC (0x1)
38 struct pxa930_trkball {
39 struct pxa930_trkball_platform_data *pdata;
41 /* Memory Mapped Register */
43 void __iomem *mmio_base;
45 struct input_dev *input;
48 static irqreturn_t pxa930_trkball_interrupt(int irq, void *dev_id)
50 struct pxa930_trkball *trkball = dev_id;
51 struct input_dev *input = trkball->input;
54 /* According to the spec software must read TBCNTR twice:
55 * if the read value is the same, the reading is valid
57 tbcntr = __raw_readl(trkball->mmio_base + TBCNTR);
59 if (tbcntr == __raw_readl(trkball->mmio_base + TBCNTR)) {
60 x = (TBCNTR_XP(tbcntr) - TBCNTR_XM(tbcntr)) / 2;
61 y = (TBCNTR_YP(tbcntr) - TBCNTR_YM(tbcntr)) / 2;
63 input_report_rel(input, REL_X, x);
64 input_report_rel(input, REL_Y, y);
68 __raw_writel(TBSBC_TBSBC, trkball->mmio_base + TBSBC);
69 __raw_writel(0, trkball->mmio_base + TBSBC);
74 /* For TBCR, we need to wait for a while to make sure it has been modified. */
75 static int write_tbcr(struct pxa930_trkball *trkball, int v)
79 __raw_writel(v, trkball->mmio_base + TBCR);
82 if (__raw_readl(trkball->mmio_base + TBCR) == v)
88 pr_err("%s: timed out writing TBCR(%x)!\n", __func__, v);
95 static void pxa930_trkball_config(struct pxa930_trkball *trkball)
99 /* According to spec, need to write the filters of x,y to 0xf first! */
100 tbcr = __raw_readl(trkball->mmio_base + TBCR);
101 write_tbcr(trkball, tbcr | TBCR_X_FLT(0xf) | TBCR_Y_FLT(0xf));
102 write_tbcr(trkball, TBCR_X_FLT(trkball->pdata->x_filter) |
103 TBCR_Y_FLT(trkball->pdata->y_filter));
105 /* According to spec, set TBCR_TBRST first, before clearing it! */
106 tbcr = __raw_readl(trkball->mmio_base + TBCR);
107 write_tbcr(trkball, tbcr | TBCR_TBRST);
108 write_tbcr(trkball, tbcr & ~TBCR_TBRST);
110 __raw_writel(TBSBC_TBSBC, trkball->mmio_base + TBSBC);
111 __raw_writel(0, trkball->mmio_base + TBSBC);
113 pr_debug("%s: final TBCR=%x!\n", __func__,
114 __raw_readl(trkball->mmio_base + TBCR));
117 static int pxa930_trkball_open(struct input_dev *dev)
119 struct pxa930_trkball *trkball = input_get_drvdata(dev);
121 pxa930_trkball_config(trkball);
126 static void pxa930_trkball_disable(struct pxa930_trkball *trkball)
128 uint32_t tbcr = __raw_readl(trkball->mmio_base + TBCR);
130 /* Held in reset, gate the 32-KHz input clock off */
131 write_tbcr(trkball, tbcr | TBCR_TBRST);
134 static void pxa930_trkball_close(struct input_dev *dev)
136 struct pxa930_trkball *trkball = input_get_drvdata(dev);
138 pxa930_trkball_disable(trkball);
141 static int pxa930_trkball_probe(struct platform_device *pdev)
143 struct pxa930_trkball *trkball;
144 struct input_dev *input;
145 struct resource *res;
148 irq = platform_get_irq(pdev, 0);
152 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
154 dev_err(&pdev->dev, "failed to get register memory\n");
158 trkball = kzalloc(sizeof(struct pxa930_trkball), GFP_KERNEL);
162 trkball->pdata = dev_get_platdata(&pdev->dev);
163 if (!trkball->pdata) {
164 dev_err(&pdev->dev, "no platform data defined\n");
169 trkball->mmio_base = ioremap(res->start, resource_size(res));
170 if (!trkball->mmio_base) {
171 dev_err(&pdev->dev, "failed to ioremap registers\n");
176 /* held the module in reset, will be enabled in open() */
177 pxa930_trkball_disable(trkball);
179 error = request_irq(irq, pxa930_trkball_interrupt, 0,
180 pdev->name, trkball);
182 dev_err(&pdev->dev, "failed to request irq: %d\n", error);
186 platform_set_drvdata(pdev, trkball);
188 input = input_allocate_device();
190 dev_err(&pdev->dev, "failed to allocate input device\n");
192 goto failed_free_irq;
195 input->name = pdev->name;
196 input->id.bustype = BUS_HOST;
197 input->open = pxa930_trkball_open;
198 input->close = pxa930_trkball_close;
199 input->dev.parent = &pdev->dev;
200 input_set_drvdata(input, trkball);
202 trkball->input = input;
204 input_set_capability(input, EV_REL, REL_X);
205 input_set_capability(input, EV_REL, REL_Y);
207 error = input_register_device(input);
209 dev_err(&pdev->dev, "unable to register input device\n");
210 goto failed_free_input;
216 input_free_device(input);
218 free_irq(irq, trkball);
220 iounmap(trkball->mmio_base);
226 static int pxa930_trkball_remove(struct platform_device *pdev)
228 struct pxa930_trkball *trkball = platform_get_drvdata(pdev);
229 int irq = platform_get_irq(pdev, 0);
231 input_unregister_device(trkball->input);
232 free_irq(irq, trkball);
233 iounmap(trkball->mmio_base);
239 static struct platform_driver pxa930_trkball_driver = {
241 .name = "pxa930-trkball",
243 .probe = pxa930_trkball_probe,
244 .remove = pxa930_trkball_remove,
246 module_platform_driver(pxa930_trkball_driver);
248 MODULE_AUTHOR("Yong Yao <yaoyong@marvell.com>");
249 MODULE_DESCRIPTION("PXA930 Trackball Mouse Driver");
250 MODULE_LICENSE("GPL");