xref: /openbmc/linux/drivers/input/mouse/navpoint.c (revision 94cdda6b)
1 /*
2  * Synaptics NavPoint (PXA27x SSP/SPI) driver.
3  *
4  * Copyright (C) 2012 Paul Parsons <lost.distance@yahoo.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/gpio.h>
17 #include <linux/input.h>
18 #include <linux/input/navpoint.h>
19 #include <linux/interrupt.h>
20 #include <linux/mutex.h>
21 #include <linux/pxa2xx_ssp.h>
22 #include <linux/slab.h>
23 
24 /*
25  * Synaptics Modular Embedded Protocol: Module Packet Format.
26  * Module header byte 2:0 = Length (# bytes that follow)
27  * Module header byte 4:3 = Control
28  * Module header byte 7:5 = Module Address
29  */
30 #define HEADER_LENGTH(byte)	((byte) & 0x07)
31 #define HEADER_CONTROL(byte)	(((byte) >> 3) & 0x03)
32 #define HEADER_ADDRESS(byte)	((byte) >> 5)
33 
34 struct navpoint {
35 	struct ssp_device	*ssp;
36 	struct input_dev	*input;
37 	struct device		*dev;
38 	int			gpio;
39 	int			index;
40 	u8			data[1 + HEADER_LENGTH(0xff)];
41 };
42 
43 /*
44  * Initialization values for SSCR0_x, SSCR1_x, SSSR_x.
45  */
46 static const u32 sscr0 = 0
47 	| SSCR0_TUM		/* TIM = 1; No TUR interrupts */
48 	| SSCR0_RIM		/* RIM = 1; No ROR interrupts */
49 	| SSCR0_SSE		/* SSE = 1; SSP enabled */
50 	| SSCR0_Motorola	/* FRF = 0; Motorola SPI */
51 	| SSCR0_DataSize(16)	/* DSS = 15; Data size = 16-bit */
52 	;
53 static const u32 sscr1 = 0
54 	| SSCR1_SCFR		/* SCFR = 1; SSPSCLK only during transfers */
55 	| SSCR1_SCLKDIR		/* SCLKDIR = 1; Slave mode */
56 	| SSCR1_SFRMDIR		/* SFRMDIR = 1; Slave mode */
57 	| SSCR1_RWOT		/* RWOT = 1; Receive without transmit mode */
58 	| SSCR1_RxTresh(1)	/* RFT = 0; Receive FIFO threshold = 1 */
59 	| SSCR1_SPH		/* SPH = 1; SSPSCLK inactive 0.5 + 1 cycles */
60 	| SSCR1_RIE		/* RIE = 1; Receive FIFO interrupt enabled */
61 	;
62 static const u32 sssr = 0
63 	| SSSR_BCE		/* BCE = 1; Clear BCE */
64 	| SSSR_TUR		/* TUR = 1; Clear TUR */
65 	| SSSR_EOC		/* EOC = 1; Clear EOC */
66 	| SSSR_TINT		/* TINT = 1; Clear TINT */
67 	| SSSR_PINT		/* PINT = 1; Clear PINT */
68 	| SSSR_ROR		/* ROR = 1; Clear ROR */
69 	;
70 
71 /*
72  * MEP Query $22: Touchpad Coordinate Range Query is not supported by
73  * the NavPoint module, so sampled values provide the default limits.
74  */
75 #define NAVPOINT_X_MIN		1278
76 #define NAVPOINT_X_MAX		5340
77 #define NAVPOINT_Y_MIN		1572
78 #define NAVPOINT_Y_MAX		4396
79 #define NAVPOINT_PRESSURE_MIN	0
80 #define NAVPOINT_PRESSURE_MAX	255
81 
82 static void navpoint_packet(struct navpoint *navpoint)
83 {
84 	int finger;
85 	int gesture;
86 	int x, y, z;
87 
88 	switch (navpoint->data[0]) {
89 	case 0xff:	/* Garbage (packet?) between reset and Hello packet */
90 	case 0x00:	/* Module 0, NULL packet */
91 		break;
92 
93 	case 0x0e:	/* Module 0, Absolute packet */
94 		finger = (navpoint->data[1] & 0x01);
95 		gesture = (navpoint->data[1] & 0x02);
96 		x = ((navpoint->data[2] & 0x1f) << 8) | navpoint->data[3];
97 		y = ((navpoint->data[4] & 0x1f) << 8) | navpoint->data[5];
98 		z = navpoint->data[6];
99 		input_report_key(navpoint->input, BTN_TOUCH, finger);
100 		input_report_abs(navpoint->input, ABS_X, x);
101 		input_report_abs(navpoint->input, ABS_Y, y);
102 		input_report_abs(navpoint->input, ABS_PRESSURE, z);
103 		input_report_key(navpoint->input, BTN_TOOL_FINGER, finger);
104 		input_report_key(navpoint->input, BTN_LEFT, gesture);
105 		input_sync(navpoint->input);
106 		break;
107 
108 	case 0x19:	/* Module 0, Hello packet */
109 		if ((navpoint->data[1] & 0xf0) == 0x10)
110 			break;
111 		/* FALLTHROUGH */
112 	default:
113 		dev_warn(navpoint->dev,
114 			 "spurious packet: data=0x%02x,0x%02x,...\n",
115 			 navpoint->data[0], navpoint->data[1]);
116 		break;
117 	}
118 }
119 
120 static irqreturn_t navpoint_irq(int irq, void *dev_id)
121 {
122 	struct navpoint *navpoint = dev_id;
123 	struct ssp_device *ssp = navpoint->ssp;
124 	irqreturn_t ret = IRQ_NONE;
125 	u32 status;
126 
127 	status = pxa_ssp_read_reg(ssp, SSSR);
128 	if (status & sssr) {
129 		dev_warn(navpoint->dev,
130 			 "unexpected interrupt: status=0x%08x\n", status);
131 		pxa_ssp_write_reg(ssp, SSSR, (status & sssr));
132 		ret = IRQ_HANDLED;
133 	}
134 
135 	while (status & SSSR_RNE) {
136 		u32 data;
137 
138 		data = pxa_ssp_read_reg(ssp, SSDR);
139 		navpoint->data[navpoint->index + 0] = (data >> 8);
140 		navpoint->data[navpoint->index + 1] = data;
141 		navpoint->index += 2;
142 		if (HEADER_LENGTH(navpoint->data[0]) < navpoint->index) {
143 			navpoint_packet(navpoint);
144 			navpoint->index = 0;
145 		}
146 		status = pxa_ssp_read_reg(ssp, SSSR);
147 		ret = IRQ_HANDLED;
148 	}
149 
150 	return ret;
151 }
152 
153 static void navpoint_up(struct navpoint *navpoint)
154 {
155 	struct ssp_device *ssp = navpoint->ssp;
156 	int timeout;
157 
158 	clk_prepare_enable(ssp->clk);
159 
160 	pxa_ssp_write_reg(ssp, SSCR1, sscr1);
161 	pxa_ssp_write_reg(ssp, SSSR, sssr);
162 	pxa_ssp_write_reg(ssp, SSTO, 0);
163 	pxa_ssp_write_reg(ssp, SSCR0, sscr0);	/* SSCR0_SSE written last */
164 
165 	/* Wait until SSP port is ready for slave clock operations */
166 	for (timeout = 100; timeout != 0; --timeout) {
167 		if (!(pxa_ssp_read_reg(ssp, SSSR) & SSSR_CSS))
168 			break;
169 		msleep(1);
170 	}
171 
172 	if (timeout == 0)
173 		dev_err(navpoint->dev,
174 			"timeout waiting for SSSR[CSS] to clear\n");
175 
176 	if (gpio_is_valid(navpoint->gpio))
177 		gpio_set_value(navpoint->gpio, 1);
178 }
179 
180 static void navpoint_down(struct navpoint *navpoint)
181 {
182 	struct ssp_device *ssp = navpoint->ssp;
183 
184 	if (gpio_is_valid(navpoint->gpio))
185 		gpio_set_value(navpoint->gpio, 0);
186 
187 	pxa_ssp_write_reg(ssp, SSCR0, 0);
188 
189 	clk_disable_unprepare(ssp->clk);
190 }
191 
192 static int navpoint_open(struct input_dev *input)
193 {
194 	struct navpoint *navpoint = input_get_drvdata(input);
195 
196 	navpoint_up(navpoint);
197 
198 	return 0;
199 }
200 
201 static void navpoint_close(struct input_dev *input)
202 {
203 	struct navpoint *navpoint = input_get_drvdata(input);
204 
205 	navpoint_down(navpoint);
206 }
207 
208 static int navpoint_probe(struct platform_device *pdev)
209 {
210 	const struct navpoint_platform_data *pdata =
211 					dev_get_platdata(&pdev->dev);
212 	struct ssp_device *ssp;
213 	struct input_dev *input;
214 	struct navpoint *navpoint;
215 	int error;
216 
217 	if (!pdata) {
218 		dev_err(&pdev->dev, "no platform data\n");
219 		return -EINVAL;
220 	}
221 
222 	if (gpio_is_valid(pdata->gpio)) {
223 		error = gpio_request_one(pdata->gpio, GPIOF_OUT_INIT_LOW,
224 					 "SYNAPTICS_ON");
225 		if (error)
226 			return error;
227 	}
228 
229 	ssp = pxa_ssp_request(pdata->port, pdev->name);
230 	if (!ssp) {
231 		error = -ENODEV;
232 		goto err_free_gpio;
233 	}
234 
235 	/* HaRET does not disable devices before jumping into Linux */
236 	if (pxa_ssp_read_reg(ssp, SSCR0) & SSCR0_SSE) {
237 		pxa_ssp_write_reg(ssp, SSCR0, 0);
238 		dev_warn(&pdev->dev, "ssp%d already enabled\n", pdata->port);
239 	}
240 
241 	navpoint = kzalloc(sizeof(*navpoint), GFP_KERNEL);
242 	input = input_allocate_device();
243 	if (!navpoint || !input) {
244 		error = -ENOMEM;
245 		goto err_free_mem;
246 	}
247 
248 	navpoint->ssp = ssp;
249 	navpoint->input = input;
250 	navpoint->dev = &pdev->dev;
251 	navpoint->gpio = pdata->gpio;
252 
253 	input->name = pdev->name;
254 	input->dev.parent = &pdev->dev;
255 
256 	__set_bit(EV_KEY, input->evbit);
257 	__set_bit(EV_ABS, input->evbit);
258 	__set_bit(BTN_LEFT, input->keybit);
259 	__set_bit(BTN_TOUCH, input->keybit);
260 	__set_bit(BTN_TOOL_FINGER, input->keybit);
261 
262 	input_set_abs_params(input, ABS_X,
263 			     NAVPOINT_X_MIN, NAVPOINT_X_MAX, 0, 0);
264 	input_set_abs_params(input, ABS_Y,
265 			     NAVPOINT_Y_MIN, NAVPOINT_Y_MAX, 0, 0);
266 	input_set_abs_params(input, ABS_PRESSURE,
267 			     NAVPOINT_PRESSURE_MIN, NAVPOINT_PRESSURE_MAX,
268 			     0, 0);
269 
270 	input->open = navpoint_open;
271 	input->close = navpoint_close;
272 
273 	input_set_drvdata(input, navpoint);
274 
275 	error = request_irq(ssp->irq, navpoint_irq, 0, pdev->name, navpoint);
276 	if (error)
277 		goto err_free_mem;
278 
279 	error = input_register_device(input);
280 	if (error)
281 		goto err_free_irq;
282 
283 	platform_set_drvdata(pdev, navpoint);
284 	dev_dbg(&pdev->dev, "ssp%d, irq %d\n", pdata->port, ssp->irq);
285 
286 	return 0;
287 
288 err_free_irq:
289 	free_irq(ssp->irq, navpoint);
290 err_free_mem:
291 	input_free_device(input);
292 	kfree(navpoint);
293 	pxa_ssp_free(ssp);
294 err_free_gpio:
295 	if (gpio_is_valid(pdata->gpio))
296 		gpio_free(pdata->gpio);
297 
298 	return error;
299 }
300 
301 static int navpoint_remove(struct platform_device *pdev)
302 {
303 	const struct navpoint_platform_data *pdata =
304 					dev_get_platdata(&pdev->dev);
305 	struct navpoint *navpoint = platform_get_drvdata(pdev);
306 	struct ssp_device *ssp = navpoint->ssp;
307 
308 	free_irq(ssp->irq, navpoint);
309 
310 	input_unregister_device(navpoint->input);
311 	kfree(navpoint);
312 
313 	pxa_ssp_free(ssp);
314 
315 	if (gpio_is_valid(pdata->gpio))
316 		gpio_free(pdata->gpio);
317 
318 	return 0;
319 }
320 
321 static int __maybe_unused navpoint_suspend(struct device *dev)
322 {
323 	struct platform_device *pdev = to_platform_device(dev);
324 	struct navpoint *navpoint = platform_get_drvdata(pdev);
325 	struct input_dev *input = navpoint->input;
326 
327 	mutex_lock(&input->mutex);
328 	if (input->users)
329 		navpoint_down(navpoint);
330 	mutex_unlock(&input->mutex);
331 
332 	return 0;
333 }
334 
335 static int __maybe_unused navpoint_resume(struct device *dev)
336 {
337 	struct platform_device *pdev = to_platform_device(dev);
338 	struct navpoint *navpoint = platform_get_drvdata(pdev);
339 	struct input_dev *input = navpoint->input;
340 
341 	mutex_lock(&input->mutex);
342 	if (input->users)
343 		navpoint_up(navpoint);
344 	mutex_unlock(&input->mutex);
345 
346 	return 0;
347 }
348 
349 static SIMPLE_DEV_PM_OPS(navpoint_pm_ops, navpoint_suspend, navpoint_resume);
350 
351 static struct platform_driver navpoint_driver = {
352 	.probe		= navpoint_probe,
353 	.remove		= navpoint_remove,
354 	.driver = {
355 		.name	= "navpoint",
356 		.pm	= &navpoint_pm_ops,
357 	},
358 };
359 
360 module_platform_driver(navpoint_driver);
361 
362 MODULE_AUTHOR("Paul Parsons <lost.distance@yahoo.com>");
363 MODULE_DESCRIPTION("Synaptics NavPoint (PXA27x SSP/SPI) driver");
364 MODULE_LICENSE("GPL");
365 MODULE_ALIAS("platform:navpoint");
366