Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / input / mouse / pxa930_trkball.c
blob3332b77eef2aecbe5748722d6ea40eae42677649
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * PXA930 track ball mouse driver
5 * Copyright (C) 2007 Marvell International Ltd.
6 * 2008-02-28: Yong Yao <yaoyong@marvell.com>
7 * initial version
8 */
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>
15 #include <linux/io.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 */
22 #define TBCR (0x000C)
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 */
43 struct resource *mem;
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;
53 int tbcntr, x, y;
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);
66 input_sync(input);
69 __raw_writel(TBSBC_TBSBC, trkball->mmio_base + TBSBC);
70 __raw_writel(0, trkball->mmio_base + TBSBC);
72 return IRQ_HANDLED;
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)
78 int i = 100;
80 __raw_writel(v, trkball->mmio_base + TBCR);
82 while (--i) {
83 if (__raw_readl(trkball->mmio_base + TBCR) == v)
84 break;
85 msleep(1);
88 if (i == 0) {
89 pr_err("%s: timed out writing TBCR(%x)!\n", __func__, v);
90 return -ETIMEDOUT;
93 return 0;
96 static void pxa930_trkball_config(struct pxa930_trkball *trkball)
98 uint32_t tbcr;
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);
124 return 0;
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;
147 int irq, error;
149 irq = platform_get_irq(pdev, 0);
150 if (irq < 0)
151 return -ENXIO;
153 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
154 if (!res) {
155 dev_err(&pdev->dev, "failed to get register memory\n");
156 return -ENXIO;
159 trkball = kzalloc(sizeof(struct pxa930_trkball), GFP_KERNEL);
160 if (!trkball)
161 return -ENOMEM;
163 trkball->pdata = dev_get_platdata(&pdev->dev);
164 if (!trkball->pdata) {
165 dev_err(&pdev->dev, "no platform data defined\n");
166 error = -EINVAL;
167 goto failed;
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");
173 error = -ENXIO;
174 goto failed;
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);
182 if (error) {
183 dev_err(&pdev->dev, "failed to request irq: %d\n", error);
184 goto failed_free_io;
187 platform_set_drvdata(pdev, trkball);
189 input = input_allocate_device();
190 if (!input) {
191 dev_err(&pdev->dev, "failed to allocate input device\n");
192 error = -ENOMEM;
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);
209 if (error) {
210 dev_err(&pdev->dev, "unable to register input device\n");
211 goto failed_free_input;
214 return 0;
216 failed_free_input:
217 input_free_device(input);
218 failed_free_irq:
219 free_irq(irq, trkball);
220 failed_free_io:
221 iounmap(trkball->mmio_base);
222 failed:
223 kfree(trkball);
224 return error;
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);
235 kfree(trkball);
237 return 0;
240 static struct platform_driver pxa930_trkball_driver = {
241 .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");