1/*
2 * Synaptics touchpad with I2C interface
3 *
4 * Copyright (C) 2009 Compulab, Ltd.
5 * Mike Rapoport <mike@compulab.co.il>
6 * Igor Grinberg <grinberg@compulab.co.il>
7 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file COPYING in the main directory of this archive for
10 * more details.
11 */
12
13#include <linux/module.h>
14#include <linux/i2c.h>
15#include <linux/irq.h>
16#include <linux/interrupt.h>
17#include <linux/input.h>
18#include <linux/delay.h>
19#include <linux/workqueue.h>
20#include <linux/slab.h>
21#include <linux/pm.h>
22
23#define DRIVER_NAME "synaptics_i2c"
24/* maximum product id is 15 characters */
25#define PRODUCT_ID_LENGTH 15
26#define REGISTER_LENGTH 8
27
28/*
29 * after soft reset, we should wait for 1 ms
30 * before the device becomes operational
31 */
32#define SOFT_RESET_DELAY_US 3000
33/* and after hard reset, we should wait for max 500ms */
34#define HARD_RESET_DELAY_MS 500
35
36/* Registers by SMBus address */
37#define PAGE_SEL_REG 0xff
38#define DEVICE_STATUS_REG 0x09
39
40/* Registers by RMI address */
41#define DEV_CONTROL_REG 0x0000
42#define INTERRUPT_EN_REG 0x0001
43#define ERR_STAT_REG 0x0002
44#define INT_REQ_STAT_REG 0x0003
45#define DEV_COMMAND_REG 0x0004
46
47#define RMI_PROT_VER_REG 0x0200
48#define MANUFACT_ID_REG 0x0201
49#define PHYS_INT_VER_REG 0x0202
50#define PROD_PROPERTY_REG 0x0203
51#define INFO_QUERY_REG0 0x0204
52#define INFO_QUERY_REG1 (INFO_QUERY_REG0 + 1)
53#define INFO_QUERY_REG2 (INFO_QUERY_REG0 + 2)
54#define INFO_QUERY_REG3 (INFO_QUERY_REG0 + 3)
55
56#define PRODUCT_ID_REG0 0x0210
57#define PRODUCT_ID_REG1 (PRODUCT_ID_REG0 + 1)
58#define PRODUCT_ID_REG2 (PRODUCT_ID_REG0 + 2)
59#define PRODUCT_ID_REG3 (PRODUCT_ID_REG0 + 3)
60#define PRODUCT_ID_REG4 (PRODUCT_ID_REG0 + 4)
61#define PRODUCT_ID_REG5 (PRODUCT_ID_REG0 + 5)
62#define PRODUCT_ID_REG6 (PRODUCT_ID_REG0 + 6)
63#define PRODUCT_ID_REG7 (PRODUCT_ID_REG0 + 7)
64#define PRODUCT_ID_REG8 (PRODUCT_ID_REG0 + 8)
65#define PRODUCT_ID_REG9 (PRODUCT_ID_REG0 + 9)
66#define PRODUCT_ID_REG10 (PRODUCT_ID_REG0 + 10)
67#define PRODUCT_ID_REG11 (PRODUCT_ID_REG0 + 11)
68#define PRODUCT_ID_REG12 (PRODUCT_ID_REG0 + 12)
69#define PRODUCT_ID_REG13 (PRODUCT_ID_REG0 + 13)
70#define PRODUCT_ID_REG14 (PRODUCT_ID_REG0 + 14)
71#define PRODUCT_ID_REG15 (PRODUCT_ID_REG0 + 15)
72
73#define DATA_REG0 0x0400
74#define ABS_PRESSURE_REG 0x0401
75#define ABS_MSB_X_REG 0x0402
76#define ABS_LSB_X_REG (ABS_MSB_X_REG + 1)
77#define ABS_MSB_Y_REG 0x0404
78#define ABS_LSB_Y_REG (ABS_MSB_Y_REG + 1)
79#define REL_X_REG 0x0406
80#define REL_Y_REG 0x0407
81
82#define DEV_QUERY_REG0 0x1000
83#define DEV_QUERY_REG1 (DEV_QUERY_REG0 + 1)
84#define DEV_QUERY_REG2 (DEV_QUERY_REG0 + 2)
85#define DEV_QUERY_REG3 (DEV_QUERY_REG0 + 3)
86#define DEV_QUERY_REG4 (DEV_QUERY_REG0 + 4)
87#define DEV_QUERY_REG5 (DEV_QUERY_REG0 + 5)
88#define DEV_QUERY_REG6 (DEV_QUERY_REG0 + 6)
89#define DEV_QUERY_REG7 (DEV_QUERY_REG0 + 7)
90#define DEV_QUERY_REG8 (DEV_QUERY_REG0 + 8)
91
92#define GENERAL_2D_CONTROL_REG 0x1041
93#define SENSOR_SENSITIVITY_REG 0x1044
94#define SENS_MAX_POS_MSB_REG 0x1046
95#define SENS_MAX_POS_LSB_REG (SENS_MAX_POS_UPPER_REG + 1)
96
97/* Register bits */
98/* Device Control Register Bits */
99#define REPORT_RATE_1ST_BIT 6
100
101/* Interrupt Enable Register Bits (INTERRUPT_EN_REG) */
102#define F10_ABS_INT_ENA 0
103#define F10_REL_INT_ENA 1
104#define F20_INT_ENA 2
105
106/* Interrupt Request Register Bits (INT_REQ_STAT_REG | DEVICE_STATUS_REG) */
107#define F10_ABS_INT_REQ 0
108#define F10_REL_INT_REQ 1
109#define F20_INT_REQ 2
110/* Device Status Register Bits (DEVICE_STATUS_REG) */
111#define STAT_CONFIGURED 6
112#define STAT_ERROR 7
113
114/* Device Command Register Bits (DEV_COMMAND_REG) */
115#define RESET_COMMAND 0x01
116#define REZERO_COMMAND 0x02
117
118/* Data Register 0 Bits (DATA_REG0) */
119#define GESTURE 3
120
121/* Device Query Registers Bits */
122/* DEV_QUERY_REG3 */
123#define HAS_PALM_DETECT 1
124#define HAS_MULTI_FING 2
125#define HAS_SCROLLER 4
126#define HAS_2D_SCROLL 5
127
128/* General 2D Control Register Bits (GENERAL_2D_CONTROL_REG) */
129#define NO_DECELERATION 1
130#define REDUCE_REPORTING 3
131#define NO_FILTER 5
132
133/* Function Masks */
134/* Device Control Register Masks (DEV_CONTROL_REG) */
135#define REPORT_RATE_MSK 0xc0
136#define SLEEP_MODE_MSK 0x07
137
138/* Device Sleep Modes */
139#define FULL_AWAKE 0x0
140#define NORMAL_OP 0x1
141#define LOW_PWR_OP 0x2
142#define VERY_LOW_PWR_OP 0x3
143#define SENS_SLEEP 0x4
144#define SLEEP_MOD 0x5
145#define DEEP_SLEEP 0x6
146#define HIBERNATE 0x7
147
148/* Interrupt Register Mask */
149/* (INT_REQ_STAT_REG | DEVICE_STATUS_REG | INTERRUPT_EN_REG) */
150#define INT_ENA_REQ_MSK 0x07
151#define INT_ENA_ABS_MSK 0x01
152#define INT_ENA_REL_MSK 0x02
153#define INT_ENA_F20_MSK 0x04
154
155/* Device Status Register Masks (DEVICE_STATUS_REG) */
156#define CONFIGURED_MSK 0x40
157#define ERROR_MSK 0x80
158
159/* Data Register 0 Masks */
160#define FINGER_WIDTH_MSK 0xf0
161#define GESTURE_MSK 0x08
162#define SENSOR_STATUS_MSK 0x07
163
164/*
165 * MSB Position Register Masks
166 * ABS_MSB_X_REG | ABS_MSB_Y_REG | SENS_MAX_POS_MSB_REG |
167 * DEV_QUERY_REG3 | DEV_QUERY_REG5
168 */
169#define MSB_POSITION_MSK 0x1f
170
171/* Device Query Registers Masks */
172
173/* DEV_QUERY_REG2 */
174#define NUM_EXTRA_POS_MSK 0x07
175
176/* When in IRQ mode read the device every THREAD_IRQ_SLEEP_SECS */
177#define THREAD_IRQ_SLEEP_SECS 2
178#define THREAD_IRQ_SLEEP_MSECS (THREAD_IRQ_SLEEP_SECS * MSEC_PER_SEC)
179
180/*
181 * When in Polling mode and no data received for NO_DATA_THRES msecs
182 * reduce the polling rate to NO_DATA_SLEEP_MSECS
183 */
184#define NO_DATA_THRES (MSEC_PER_SEC)
185#define NO_DATA_SLEEP_MSECS (MSEC_PER_SEC / 4)
186
187/* Control touchpad's No Deceleration option */
188static bool no_decel = true;
189module_param(no_decel, bool, 0644);
190MODULE_PARM_DESC(no_decel, "No Deceleration. Default = 1 (on)");
191
192/* Control touchpad's Reduced Reporting option */
193static bool reduce_report;
194module_param(reduce_report, bool, 0644);
195MODULE_PARM_DESC(reduce_report, "Reduced Reporting. Default = 0 (off)");
196
197/* Control touchpad's No Filter option */
198static bool no_filter;
199module_param(no_filter, bool, 0644);
200MODULE_PARM_DESC(no_filter, "No Filter. Default = 0 (off)");
201
202/*
203 * touchpad Attention line is Active Low and Open Drain,
204 * therefore should be connected to pulled up line
205 * and the irq configuration should be set to Falling Edge Trigger
206 */
207/* Control IRQ / Polling option */
208static bool polling_req;
209module_param(polling_req, bool, 0444);
210MODULE_PARM_DESC(polling_req, "Request Polling. Default = 0 (use irq)");
211
212/* Control Polling Rate */
213static int scan_rate = 80;
214module_param(scan_rate, int, 0644);
215MODULE_PARM_DESC(scan_rate, "Polling rate in times/sec. Default = 80");
216
217/* The main device structure */
218struct synaptics_i2c {
219 struct i2c_client *client;
220 struct input_dev *input;
221 struct delayed_work dwork;
222 int no_data_count;
223 int no_decel_param;
224 int reduce_report_param;
225 int no_filter_param;
226 int scan_rate_param;
227 int scan_ms;
228};
229
230static inline void set_scan_rate(struct synaptics_i2c *touch, int scan_rate)
231{
232 touch->scan_ms = MSEC_PER_SEC / scan_rate;
233 touch->scan_rate_param = scan_rate;
234}
235
236/*
237 * Driver's initial design makes no race condition possible on i2c bus,
238 * so there is no need in any locking.
239 * Keep it in mind, while playing with the code.
240 */
241static s32 synaptics_i2c_reg_get(struct i2c_client *client, u16 reg)
242{
243 int ret;
244
245 ret = i2c_smbus_write_byte_data(client, PAGE_SEL_REG, reg >> 8);
246 if (ret == 0)
247 ret = i2c_smbus_read_byte_data(client, reg & 0xff);
248
249 return ret;
250}
251
252static s32 synaptics_i2c_reg_set(struct i2c_client *client, u16 reg, u8 val)
253{
254 int ret;
255
256 ret = i2c_smbus_write_byte_data(client, PAGE_SEL_REG, reg >> 8);
257 if (ret == 0)
258 ret = i2c_smbus_write_byte_data(client, reg & 0xff, val);
259
260 return ret;
261}
262
263static s32 synaptics_i2c_word_get(struct i2c_client *client, u16 reg)
264{
265 int ret;
266
267 ret = i2c_smbus_write_byte_data(client, PAGE_SEL_REG, reg >> 8);
268 if (ret == 0)
269 ret = i2c_smbus_read_word_data(client, reg & 0xff);
270
271 return ret;
272}
273
274static int synaptics_i2c_config(struct i2c_client *client)
275{
276 int ret, control;
277 u8 int_en;
278
279 /* set Report Rate to Device Highest (>=80) and Sleep to normal */
280 ret = synaptics_i2c_reg_set(client, DEV_CONTROL_REG, 0xc1);
281 if (ret)
282 return ret;
283
284 /* set Interrupt Disable to Func20 / Enable to Func10) */
285 int_en = (polling_req) ? 0 : INT_ENA_ABS_MSK | INT_ENA_REL_MSK;
286 ret = synaptics_i2c_reg_set(client, INTERRUPT_EN_REG, int_en);
287 if (ret)
288 return ret;
289
290 control = synaptics_i2c_reg_get(client, GENERAL_2D_CONTROL_REG);
291 /* No Deceleration */
292 control |= no_decel ? 1 << NO_DECELERATION : 0;
293 /* Reduced Reporting */
294 control |= reduce_report ? 1 << REDUCE_REPORTING : 0;
295 /* No Filter */
296 control |= no_filter ? 1 << NO_FILTER : 0;
297 ret = synaptics_i2c_reg_set(client, GENERAL_2D_CONTROL_REG, control);
298 if (ret)
299 return ret;
300
301 return 0;
302}
303
304static int synaptics_i2c_reset_config(struct i2c_client *client)
305{
306 int ret;
307
308 /* Reset the Touchpad */
309 ret = synaptics_i2c_reg_set(client, DEV_COMMAND_REG, RESET_COMMAND);
310 if (ret) {
311 dev_err(&client->dev, "Unable to reset device\n");
312 } else {
313 usleep_range(SOFT_RESET_DELAY_US, SOFT_RESET_DELAY_US + 100);
314 ret = synaptics_i2c_config(client);
315 if (ret)
316 dev_err(&client->dev, "Unable to config device\n");
317 }
318
319 return ret;
320}
321
322static int synaptics_i2c_check_error(struct i2c_client *client)
323{
324 int status, ret = 0;
325
326 status = i2c_smbus_read_byte_data(client, DEVICE_STATUS_REG) &
327 (CONFIGURED_MSK | ERROR_MSK);
328
329 if (status != CONFIGURED_MSK)
330 ret = synaptics_i2c_reset_config(client);
331
332 return ret;
333}
334
335static bool synaptics_i2c_get_input(struct synaptics_i2c *touch)
336{
337 struct input_dev *input = touch->input;
338 int xy_delta, gesture;
339 s32 data;
340 s8 x_delta, y_delta;
341
342 /* Deal with spontaneous resets and errors */
343 if (synaptics_i2c_check_error(touch->client))
344 return false;
345
346 /* Get Gesture Bit */
347 data = synaptics_i2c_reg_get(touch->client, DATA_REG0);
348 gesture = (data >> GESTURE) & 0x1;
349
350 /*
351 * Get Relative axes. we have to get them in one shot,
352 * so we get 2 bytes starting from REL_X_REG.
353 */
354 xy_delta = synaptics_i2c_word_get(touch->client, REL_X_REG) & 0xffff;
355
356 /* Separate X from Y */
357 x_delta = xy_delta & 0xff;
358 y_delta = (xy_delta >> REGISTER_LENGTH) & 0xff;
359
360 /* Report the button event */
361 input_report_key(input, BTN_LEFT, gesture);
362
363 /* Report the deltas */
364 input_report_rel(input, REL_X, x_delta);
365 input_report_rel(input, REL_Y, -y_delta);
366 input_sync(input);
367
368 return xy_delta || gesture;
369}
370
371static irqreturn_t synaptics_i2c_irq(int irq, void *dev_id)
372{
373 struct synaptics_i2c *touch = dev_id;
374
375 mod_delayed_work(system_wq, &touch->dwork, 0);
376
377 return IRQ_HANDLED;
378}
379
380static void synaptics_i2c_check_params(struct synaptics_i2c *touch)
381{
382 bool reset = false;
383
384 if (scan_rate != touch->scan_rate_param)
385 set_scan_rate(touch, scan_rate);
386
387 if (no_decel != touch->no_decel_param) {
388 touch->no_decel_param = no_decel;
389 reset = true;
390 }
391
392 if (no_filter != touch->no_filter_param) {
393 touch->no_filter_param = no_filter;
394 reset = true;
395 }
396
397 if (reduce_report != touch->reduce_report_param) {
398 touch->reduce_report_param = reduce_report;
399 reset = true;
400 }
401
402 if (reset)
403 synaptics_i2c_reset_config(touch->client);
404}
405
406/* Control the Device polling rate / Work Handler sleep time */
407static unsigned long synaptics_i2c_adjust_delay(struct synaptics_i2c *touch,
408 bool have_data)
409{
410 unsigned long delay, nodata_count_thres;
411
412 if (polling_req) {
413 delay = touch->scan_ms;
414 if (have_data) {
415 touch->no_data_count = 0;
416 } else {
417 nodata_count_thres = NO_DATA_THRES / touch->scan_ms;
418 if (touch->no_data_count < nodata_count_thres)
419 touch->no_data_count++;
420 else
421 delay = NO_DATA_SLEEP_MSECS;
422 }
423 return msecs_to_jiffies(delay);
424 } else {
425 delay = msecs_to_jiffies(THREAD_IRQ_SLEEP_MSECS);
426 return round_jiffies_relative(delay);
427 }
428}
429
430/* Work Handler */
431static void synaptics_i2c_work_handler(struct work_struct *work)
432{
433 bool have_data;
434 struct synaptics_i2c *touch =
435 container_of(work, struct synaptics_i2c, dwork.work);
436 unsigned long delay;
437
438 synaptics_i2c_check_params(touch);
439
440 have_data = synaptics_i2c_get_input(touch);
441 delay = synaptics_i2c_adjust_delay(touch, have_data);
442
443 /*
444 * While interrupt driven, there is no real need to poll the device.
445 * But touchpads are very sensitive, so there could be errors
446 * related to physical environment and the attention line isn't
447 * necessarily asserted. In such case we can lose the touchpad.
448 * We poll the device once in THREAD_IRQ_SLEEP_SECS and
449 * if error is detected, we try to reset and reconfigure the touchpad.
450 */
451 mod_delayed_work(system_wq, &touch->dwork, delay);
452}
453
454static int synaptics_i2c_open(struct input_dev *input)
455{
456 struct synaptics_i2c *touch = input_get_drvdata(input);
457 int ret;
458
459 ret = synaptics_i2c_reset_config(touch->client);
460 if (ret)
461 return ret;
462
463 if (polling_req)
464 mod_delayed_work(system_wq, &touch->dwork,
465 msecs_to_jiffies(NO_DATA_SLEEP_MSECS));
466
467 return 0;
468}
469
470static void synaptics_i2c_close(struct input_dev *input)
471{
472 struct synaptics_i2c *touch = input_get_drvdata(input);
473
474 if (!polling_req)
475 synaptics_i2c_reg_set(touch->client, INTERRUPT_EN_REG, 0);
476
477 cancel_delayed_work_sync(&touch->dwork);
478
479 /* Save some power */
480 synaptics_i2c_reg_set(touch->client, DEV_CONTROL_REG, DEEP_SLEEP);
481}
482
483static void synaptics_i2c_set_input_params(struct synaptics_i2c *touch)
484{
485 struct input_dev *input = touch->input;
486
487 input->name = touch->client->name;
488 input->phys = touch->client->adapter->name;
489 input->id.bustype = BUS_I2C;
490 input->id.version = synaptics_i2c_word_get(touch->client,
491 INFO_QUERY_REG0);
492 input->dev.parent = &touch->client->dev;
493 input->open = synaptics_i2c_open;
494 input->close = synaptics_i2c_close;
495 input_set_drvdata(input, touch);
496
497 /* Register the device as mouse */
498 __set_bit(EV_REL, input->evbit);
499 __set_bit(REL_X, input->relbit);
500 __set_bit(REL_Y, input->relbit);
501
502 /* Register device's buttons and keys */
503 __set_bit(EV_KEY, input->evbit);
504 __set_bit(BTN_LEFT, input->keybit);
505}
506
507static struct synaptics_i2c *synaptics_i2c_touch_create(struct i2c_client *client)
508{
509 struct synaptics_i2c *touch;
510
511 touch = kzalloc(sizeof(struct synaptics_i2c), GFP_KERNEL);
512 if (!touch)
513 return NULL;
514
515 touch->client = client;
516 touch->no_decel_param = no_decel;
517 touch->scan_rate_param = scan_rate;
518 set_scan_rate(touch, scan_rate);
519 INIT_DELAYED_WORK(&touch->dwork, synaptics_i2c_work_handler);
520
521 return touch;
522}
523
524static int synaptics_i2c_probe(struct i2c_client *client,
525 const struct i2c_device_id *dev_id)
526{
527 int ret;
528 struct synaptics_i2c *touch;
529
530 touch = synaptics_i2c_touch_create(client);
531 if (!touch)
532 return -ENOMEM;
533
534 ret = synaptics_i2c_reset_config(client);
535 if (ret)
536 goto err_mem_free;
537
538 if (client->irq < 1)
539 polling_req = true;
540
541 touch->input = input_allocate_device();
542 if (!touch->input) {
543 ret = -ENOMEM;
544 goto err_mem_free;
545 }
546
547 synaptics_i2c_set_input_params(touch);
548
549 if (!polling_req) {
550 dev_dbg(&touch->client->dev,
551 "Requesting IRQ: %d\n", touch->client->irq);
552
553 ret = request_irq(touch->client->irq, synaptics_i2c_irq,
554 IRQ_TYPE_EDGE_FALLING,
555 DRIVER_NAME, touch);
556 if (ret) {
557 dev_warn(&touch->client->dev,
558 "IRQ request failed: %d, "
559 "falling back to polling\n", ret);
560 polling_req = true;
561 synaptics_i2c_reg_set(touch->client,
562 INTERRUPT_EN_REG, 0);
563 }
564 }
565
566 if (polling_req)
567 dev_dbg(&touch->client->dev,
568 "Using polling at rate: %d times/sec\n", scan_rate);
569
570 /* Register the device in input subsystem */
571 ret = input_register_device(touch->input);
572 if (ret) {
573 dev_err(&client->dev,
574 "Input device register failed: %d\n", ret);
575 goto err_input_free;
576 }
577
578 i2c_set_clientdata(client, touch);
579
580 return 0;
581
582err_input_free:
583 input_free_device(touch->input);
584err_mem_free:
585 kfree(touch);
586
587 return ret;
588}
589
590static int synaptics_i2c_remove(struct i2c_client *client)
591{
592 struct synaptics_i2c *touch = i2c_get_clientdata(client);
593
594 if (!polling_req)
595 free_irq(client->irq, touch);
596
597 input_unregister_device(touch->input);
598 kfree(touch);
599
600 return 0;
601}
602
603static int __maybe_unused synaptics_i2c_suspend(struct device *dev)
604{
605 struct i2c_client *client = to_i2c_client(dev);
606 struct synaptics_i2c *touch = i2c_get_clientdata(client);
607
608 cancel_delayed_work_sync(&touch->dwork);
609
610 /* Save some power */
611 synaptics_i2c_reg_set(touch->client, DEV_CONTROL_REG, DEEP_SLEEP);
612
613 return 0;
614}
615
616static int __maybe_unused synaptics_i2c_resume(struct device *dev)
617{
618 int ret;
619 struct i2c_client *client = to_i2c_client(dev);
620 struct synaptics_i2c *touch = i2c_get_clientdata(client);
621
622 ret = synaptics_i2c_reset_config(client);
623 if (ret)
624 return ret;
625
626 mod_delayed_work(system_wq, &touch->dwork,
627 msecs_to_jiffies(NO_DATA_SLEEP_MSECS));
628
629 return 0;
630}
631
632static SIMPLE_DEV_PM_OPS(synaptics_i2c_pm, synaptics_i2c_suspend,
633 synaptics_i2c_resume);
634
635static const struct i2c_device_id synaptics_i2c_id_table[] = {
636 { "synaptics_i2c", 0 },
637 { },
638};
639MODULE_DEVICE_TABLE(i2c, synaptics_i2c_id_table);
640
641#ifdef CONFIG_OF
642static const struct of_device_id synaptics_i2c_of_match[] = {
643 { .compatible = "synaptics,synaptics_i2c", },
644 { },
645};
646MODULE_DEVICE_TABLE(of, synaptics_i2c_of_match);
647#endif
648
649static struct i2c_driver synaptics_i2c_driver = {
650 .driver = {
651 .name = DRIVER_NAME,
652 .of_match_table = of_match_ptr(synaptics_i2c_of_match),
653 .pm = &synaptics_i2c_pm,
654 },
655
656 .probe = synaptics_i2c_probe,
657 .remove = synaptics_i2c_remove,
658
659 .id_table = synaptics_i2c_id_table,
660};
661
662module_i2c_driver(synaptics_i2c_driver);
663
664MODULE_DESCRIPTION("Synaptics I2C touchpad driver");
665MODULE_AUTHOR("Mike Rapoport, Igor Grinberg, Compulab");
666MODULE_LICENSE("GPL");
667
668