2 * Synaptics NavPoint (PXA27x SSP/SPI) driver.
4 * Copyright (C) 2012 Paul Parsons <lost.distance@yahoo.com>
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.
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>
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
30 #define HEADER_LENGTH(byte) ((byte) & 0x07)
31 #define HEADER_CONTROL(byte) (((byte) >> 3) & 0x03)
32 #define HEADER_ADDRESS(byte) ((byte) >> 5)
35 struct ssp_device *ssp;
36 struct input_dev *input;
40 u8 data[1 + HEADER_LENGTH(0xff)];
44 * Initialization values for SSCR0_x, SSCR1_x, SSSR_x.
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 */
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 */
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 */
72 * MEP Query $22: Touchpad Coordinate Range Query is not supported by
73 * the NavPoint module, so sampled values provide the default limits.
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
82 static void navpoint_packet(struct navpoint *navpoint)
88 switch (navpoint->data[0]) {
89 case 0xff: /* Garbage (packet?) between reset and Hello packet */
90 case 0x00: /* Module 0, NULL packet */
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);
108 case 0x19: /* Module 0, Hello packet */
109 if ((navpoint->data[1] & 0xf0) == 0x10)
113 dev_warn(navpoint->dev,
114 "spurious packet: data=0x%02x,0x%02x,...\n",
115 navpoint->data[0], navpoint->data[1]);
120 static irqreturn_t navpoint_irq(int irq, void *dev_id)
122 struct navpoint *navpoint = dev_id;
123 struct ssp_device *ssp = navpoint->ssp;
124 irqreturn_t ret = IRQ_NONE;
127 status = pxa_ssp_read_reg(ssp, SSSR);
129 dev_warn(navpoint->dev,
130 "unexpected interrupt: status=0x%08x\n", status);
131 pxa_ssp_write_reg(ssp, SSSR, (status & sssr));
135 while (status & SSSR_RNE) {
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);
146 status = pxa_ssp_read_reg(ssp, SSSR);
153 static void navpoint_up(struct navpoint *navpoint)
155 struct ssp_device *ssp = navpoint->ssp;
158 clk_prepare_enable(ssp->clk);
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 */
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))
173 dev_err(navpoint->dev,
174 "timeout waiting for SSSR[CSS] to clear\n");
176 if (gpio_is_valid(navpoint->gpio))
177 gpio_set_value(navpoint->gpio, 1);
180 static void navpoint_down(struct navpoint *navpoint)
182 struct ssp_device *ssp = navpoint->ssp;
184 if (gpio_is_valid(navpoint->gpio))
185 gpio_set_value(navpoint->gpio, 0);
187 pxa_ssp_write_reg(ssp, SSCR0, 0);
189 clk_disable_unprepare(ssp->clk);
192 static int navpoint_open(struct input_dev *input)
194 struct navpoint *navpoint = input_get_drvdata(input);
196 navpoint_up(navpoint);
201 static void navpoint_close(struct input_dev *input)
203 struct navpoint *navpoint = input_get_drvdata(input);
205 navpoint_down(navpoint);
208 static int navpoint_probe(struct platform_device *pdev)
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;
218 dev_err(&pdev->dev, "no platform data\n");
222 if (gpio_is_valid(pdata->gpio)) {
223 error = gpio_request_one(pdata->gpio, GPIOF_OUT_INIT_LOW,
229 ssp = pxa_ssp_request(pdata->port, pdev->name);
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);
241 navpoint = kzalloc(sizeof(*navpoint), GFP_KERNEL);
242 input = input_allocate_device();
243 if (!navpoint || !input) {
249 navpoint->input = input;
250 navpoint->dev = &pdev->dev;
251 navpoint->gpio = pdata->gpio;
253 input->name = pdev->name;
254 input->dev.parent = &pdev->dev;
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);
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,
270 input->open = navpoint_open;
271 input->close = navpoint_close;
273 input_set_drvdata(input, navpoint);
275 error = request_irq(ssp->irq, navpoint_irq, 0, pdev->name, navpoint);
279 error = input_register_device(input);
283 platform_set_drvdata(pdev, navpoint);
284 dev_dbg(&pdev->dev, "ssp%d, irq %d\n", pdata->port, ssp->irq);
289 free_irq(ssp->irq, navpoint);
291 input_free_device(input);
295 if (gpio_is_valid(pdata->gpio))
296 gpio_free(pdata->gpio);
301 static int navpoint_remove(struct platform_device *pdev)
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;
308 free_irq(ssp->irq, navpoint);
310 input_unregister_device(navpoint->input);
315 if (gpio_is_valid(pdata->gpio))
316 gpio_free(pdata->gpio);
321 static int __maybe_unused navpoint_suspend(struct device *dev)
323 struct platform_device *pdev = to_platform_device(dev);
324 struct navpoint *navpoint = platform_get_drvdata(pdev);
325 struct input_dev *input = navpoint->input;
327 mutex_lock(&input->mutex);
329 navpoint_down(navpoint);
330 mutex_unlock(&input->mutex);
335 static int __maybe_unused navpoint_resume(struct device *dev)
337 struct platform_device *pdev = to_platform_device(dev);
338 struct navpoint *navpoint = platform_get_drvdata(pdev);
339 struct input_dev *input = navpoint->input;
341 mutex_lock(&input->mutex);
343 navpoint_up(navpoint);
344 mutex_unlock(&input->mutex);
349 static SIMPLE_DEV_PM_OPS(navpoint_pm_ops, navpoint_suspend, navpoint_resume);
351 static struct platform_driver navpoint_driver = {
352 .probe = navpoint_probe,
353 .remove = navpoint_remove,
356 .pm = &navpoint_pm_ops,
360 module_platform_driver(navpoint_driver);
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");