blob: 7d6f3dd48572f9259837546feb2dc0c1d3f09e18 [file] [log] [blame]
Alexandra Chin669d27c2012-12-24 15:42:30 +08001/*
2 * Synaptics RMI4 touchscreen driver
3 *
4 * Copyright (C) 2012 Synaptics Incorporated
5 *
6 * Copyright (C) 2012 Alexandra Chin <alexandra.chin@tw.synaptics.com>
7 * Copyright (C) 2012 Scott Lin <scott.lin@tw.synaptics.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/i2c.h>
24#include <linux/interrupt.h>
25#include <linux/delay.h>
26#include <linux/input.h>
27#include <linux/gpio.h>
28#include <linux/regulator/consumer.h>
29#include <linux/input/synaptics_dsx.h>
30#include "synaptics_i2c_rmi4.h"
31#ifdef KERNEL_ABOVE_2_6_38
32#include <linux/input/mt.h>
33#endif
34
35#define DRIVER_NAME "synaptics_rmi4_i2c"
36#define INPUT_PHYS_NAME "synaptics_rmi4_i2c/input0"
37
38#ifdef KERNEL_ABOVE_2_6_38
39#define TYPE_B_PROTOCOL
40#endif
41
42#define NO_0D_WHILE_2D
43/*
44#define REPORT_2D_Z
45*/
46#define REPORT_2D_W
47
48#define RPT_TYPE (1 << 0)
49#define RPT_X_LSB (1 << 1)
50#define RPT_X_MSB (1 << 2)
51#define RPT_Y_LSB (1 << 3)
52#define RPT_Y_MSB (1 << 4)
53#define RPT_Z (1 << 5)
54#define RPT_WX (1 << 6)
55#define RPT_WY (1 << 7)
56#define RPT_DEFAULT (RPT_TYPE | RPT_X_LSB | RPT_X_MSB | RPT_Y_LSB | RPT_Y_MSB)
57
58#define EXP_FN_DET_INTERVAL 1000 /* ms */
59#define POLLING_PERIOD 1 /* ms */
60#define SYN_I2C_RETRY_TIMES 10
61#define MAX_ABS_MT_TOUCH_MAJOR 15
62
63#define F01_STD_QUERY_LEN 21
64#define F01_BUID_ID_OFFSET 18
65#define F11_STD_QUERY_LEN 9
66#define F11_STD_CTRL_LEN 10
67#define F11_STD_DATA_LEN 12
68
69#define NORMAL_OPERATION (0 << 0)
70#define SENSOR_SLEEP (1 << 0)
71#define NO_SLEEP_OFF (0 << 3)
72#define NO_SLEEP_ON (1 << 3)
73
74static int synaptics_rmi4_i2c_read(struct synaptics_rmi4_data *rmi4_data,
75 unsigned short addr, unsigned char *data,
76 unsigned short length);
77
78static int synaptics_rmi4_i2c_write(struct synaptics_rmi4_data *rmi4_data,
79 unsigned short addr, unsigned char *data,
80 unsigned short length);
81
82static int synaptics_rmi4_reset_device(struct synaptics_rmi4_data *rmi4_data);
83
84#ifdef CONFIG_HAS_EARLYSUSPEND
85static ssize_t synaptics_rmi4_full_pm_cycle_show(struct device *dev,
86 struct device_attribute *attr, char *buf);
87
88static ssize_t synaptics_rmi4_full_pm_cycle_store(struct device *dev,
89 struct device_attribute *attr, const char *buf, size_t count);
90
91static void synaptics_rmi4_early_suspend(struct early_suspend *h);
92
93static void synaptics_rmi4_late_resume(struct early_suspend *h);
94
95static int synaptics_rmi4_suspend(struct device *dev);
96
97static int synaptics_rmi4_resume(struct device *dev);
98#endif
99
100static ssize_t synaptics_rmi4_f01_reset_store(struct device *dev,
101 struct device_attribute *attr, const char *buf, size_t count);
102
103static ssize_t synaptics_rmi4_f01_productinfo_show(struct device *dev,
104 struct device_attribute *attr, char *buf);
105
106static ssize_t synaptics_rmi4_f01_buildid_show(struct device *dev,
107 struct device_attribute *attr, char *buf);
108
109static ssize_t synaptics_rmi4_f01_flashprog_show(struct device *dev,
110 struct device_attribute *attr, char *buf);
111
112static ssize_t synaptics_rmi4_0dbutton_show(struct device *dev,
113 struct device_attribute *attr, char *buf);
114
115static ssize_t synaptics_rmi4_0dbutton_store(struct device *dev,
116 struct device_attribute *attr, const char *buf, size_t count);
117
118struct synaptics_rmi4_f01_device_status {
119 union {
120 struct {
121 unsigned char status_code:4;
122 unsigned char reserved:2;
123 unsigned char flash_prog:1;
124 unsigned char unconfigured:1;
125 } __packed;
126 unsigned char data[1];
127 };
128};
129
130struct synaptics_rmi4_f1a_query {
131 union {
132 struct {
133 unsigned char max_button_count:3;
134 unsigned char reserved:5;
135 unsigned char has_general_control:1;
136 unsigned char has_interrupt_enable:1;
137 unsigned char has_multibutton_select:1;
138 unsigned char has_tx_rx_map:1;
139 unsigned char has_perbutton_threshold:1;
140 unsigned char has_release_threshold:1;
141 unsigned char has_strongestbtn_hysteresis:1;
142 unsigned char has_filter_strength:1;
143 } __packed;
144 unsigned char data[2];
145 };
146};
147
148struct synaptics_rmi4_f1a_control_0 {
149 union {
150 struct {
151 unsigned char multibutton_report:2;
152 unsigned char filter_mode:2;
153 unsigned char reserved:4;
154 } __packed;
155 unsigned char data[1];
156 };
157};
158
159struct synaptics_rmi4_f1a_control_3_4 {
160 unsigned char transmitterbutton;
161 unsigned char receiverbutton;
162};
163
164struct synaptics_rmi4_f1a_control {
165 struct synaptics_rmi4_f1a_control_0 general_control;
166 unsigned char *button_int_enable;
167 unsigned char *multi_button;
168 struct synaptics_rmi4_f1a_control_3_4 *electrode_map;
169 unsigned char *button_threshold;
170 unsigned char button_release_threshold;
171 unsigned char strongest_button_hysteresis;
172 unsigned char filter_strength;
173};
174
175struct synaptics_rmi4_f1a_handle {
176 int button_bitmask_size;
177 unsigned char button_count;
178 unsigned char valid_button_count;
179 unsigned char *button_data_buffer;
180 unsigned char *button_map;
181 struct synaptics_rmi4_f1a_query button_query;
182 struct synaptics_rmi4_f1a_control button_control;
183};
184
185struct synaptics_rmi4_exp_fn {
186 enum exp_fn fn_type;
187 bool inserted;
188 int (*func_init)(struct synaptics_rmi4_data *rmi4_data);
189 void (*func_remove)(struct synaptics_rmi4_data *rmi4_data);
190 void (*func_attn)(struct synaptics_rmi4_data *rmi4_data,
191 unsigned char intr_mask);
192 struct list_head link;
193};
194
195static struct device_attribute attrs[] = {
196#ifdef CONFIG_HAS_EARLYSUSPEND
197 __ATTR(full_pm_cycle, (S_IRUGO | S_IWUGO),
198 synaptics_rmi4_full_pm_cycle_show,
199 synaptics_rmi4_full_pm_cycle_store),
200#endif
201 __ATTR(reset, S_IWUGO,
202 synaptics_rmi4_show_error,
203 synaptics_rmi4_f01_reset_store),
204 __ATTR(productinfo, S_IRUGO,
205 synaptics_rmi4_f01_productinfo_show,
206 synaptics_rmi4_store_error),
207 __ATTR(buildid, S_IRUGO,
208 synaptics_rmi4_f01_buildid_show,
209 synaptics_rmi4_store_error),
210 __ATTR(flashprog, S_IRUGO,
211 synaptics_rmi4_f01_flashprog_show,
212 synaptics_rmi4_store_error),
213 __ATTR(0dbutton, (S_IRUGO | S_IWUGO),
214 synaptics_rmi4_0dbutton_show,
215 synaptics_rmi4_0dbutton_store),
216};
217
218static bool exp_fn_inited;
219static struct mutex exp_fn_list_mutex;
220static struct list_head exp_fn_list;
221
222#ifdef CONFIG_HAS_EARLYSUSPEND
223static ssize_t synaptics_rmi4_full_pm_cycle_show(struct device *dev,
224 struct device_attribute *attr, char *buf)
225{
226 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
227
228 return snprintf(buf, PAGE_SIZE, "%u\n",
229 rmi4_data->full_pm_cycle);
230}
231
232static ssize_t synaptics_rmi4_full_pm_cycle_store(struct device *dev,
233 struct device_attribute *attr, const char *buf, size_t count)
234{
235 unsigned int input;
236 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
237
238 if (sscanf(buf, "%u", &input) != 1)
239 return -EINVAL;
240
241 rmi4_data->full_pm_cycle = input > 0 ? 1 : 0;
242
243 return count;
244}
245#endif
246
247static ssize_t synaptics_rmi4_f01_reset_store(struct device *dev,
248 struct device_attribute *attr, const char *buf, size_t count)
249{
250 int retval;
251 unsigned int reset;
252 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
253
254 if (sscanf(buf, "%u", &reset) != 1)
255 return -EINVAL;
256
257 if (reset != 1)
258 return -EINVAL;
259
260 retval = synaptics_rmi4_reset_device(rmi4_data);
261 if (retval < 0) {
262 dev_err(dev,
263 "%s: Failed to issue reset command, error = %d\n",
264 __func__, retval);
265 return retval;
266 }
267
268 return count;
269}
270
271static ssize_t synaptics_rmi4_f01_productinfo_show(struct device *dev,
272 struct device_attribute *attr, char *buf)
273{
274 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
275
276 return snprintf(buf, PAGE_SIZE, "0x%02x 0x%02x\n",
277 (rmi4_data->rmi4_mod_info.product_info[0]),
278 (rmi4_data->rmi4_mod_info.product_info[1]));
279}
280
281static ssize_t synaptics_rmi4_f01_buildid_show(struct device *dev,
282 struct device_attribute *attr, char *buf)
283{
284 unsigned int build_id;
285 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
286 struct synaptics_rmi4_device_info *rmi;
287
288 rmi = &(rmi4_data->rmi4_mod_info);
289
290 build_id = (unsigned int)rmi->build_id[0] +
291 (unsigned int)rmi->build_id[1] * 0x100 +
292 (unsigned int)rmi->build_id[2] * 0x10000;
293
294 return snprintf(buf, PAGE_SIZE, "%u\n",
295 build_id);
296}
297
298static ssize_t synaptics_rmi4_f01_flashprog_show(struct device *dev,
299 struct device_attribute *attr, char *buf)
300{
301 int retval;
302 struct synaptics_rmi4_f01_device_status device_status;
303 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
304
305 retval = synaptics_rmi4_i2c_read(rmi4_data,
306 rmi4_data->f01_data_base_addr,
307 device_status.data,
308 sizeof(device_status.data));
309 if (retval < 0) {
310 dev_err(dev,
311 "%s: Failed to read device status, error = %d\n",
312 __func__, retval);
313 return retval;
314 }
315
316 return snprintf(buf, PAGE_SIZE, "%u\n",
317 device_status.flash_prog);
318}
319
320static ssize_t synaptics_rmi4_0dbutton_show(struct device *dev,
321 struct device_attribute *attr, char *buf)
322{
323 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
324
325 return snprintf(buf, PAGE_SIZE, "%u\n",
326 rmi4_data->button_0d_enabled);
327}
328
329static ssize_t synaptics_rmi4_0dbutton_store(struct device *dev,
330 struct device_attribute *attr, const char *buf, size_t count)
331{
332 int retval;
333 unsigned int input;
334 unsigned char ii;
335 unsigned char intr_enable;
336 struct synaptics_rmi4_fn *fhandler;
337 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
338 struct synaptics_rmi4_device_info *rmi;
339
340 rmi = &(rmi4_data->rmi4_mod_info);
341
342 if (sscanf(buf, "%u", &input) != 1)
343 return -EINVAL;
344
345 input = input > 0 ? 1 : 0;
346
347 if (rmi4_data->button_0d_enabled == input)
348 return count;
349
Alexandra Chind5591a62013-02-07 12:59:15 -0800350 if (!list_empty(&rmi->support_fn_list)) {
351 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
352 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A) {
353 ii = fhandler->intr_reg_num;
Alexandra Chin669d27c2012-12-24 15:42:30 +0800354
Alexandra Chind5591a62013-02-07 12:59:15 -0800355 retval = synaptics_rmi4_i2c_read(rmi4_data,
356 rmi4_data->f01_ctrl_base_addr +
357 1 + ii,
358 &intr_enable,
359 sizeof(intr_enable));
360 if (retval < 0)
361 return retval;
Alexandra Chin669d27c2012-12-24 15:42:30 +0800362
Alexandra Chind5591a62013-02-07 12:59:15 -0800363 if (input == 1)
364 intr_enable |= fhandler->intr_mask;
365 else
366 intr_enable &= ~fhandler->intr_mask;
Alexandra Chin669d27c2012-12-24 15:42:30 +0800367
Alexandra Chind5591a62013-02-07 12:59:15 -0800368 retval = synaptics_rmi4_i2c_write(rmi4_data,
369 rmi4_data->f01_ctrl_base_addr +
370 1 + ii,
371 &intr_enable,
372 sizeof(intr_enable));
373 if (retval < 0)
374 return retval;
375 }
Alexandra Chin669d27c2012-12-24 15:42:30 +0800376 }
377 }
378
379 rmi4_data->button_0d_enabled = input;
380
381 return count;
382}
383
384 /**
385 * synaptics_rmi4_set_page()
386 *
387 * Called by synaptics_rmi4_i2c_read() and synaptics_rmi4_i2c_write().
388 *
389 * This function writes to the page select register to switch to the
390 * assigned page.
391 */
392static int synaptics_rmi4_set_page(struct synaptics_rmi4_data *rmi4_data,
393 unsigned int address)
394{
395 int retval = 0;
396 unsigned char retry;
397 unsigned char buf[PAGE_SELECT_LEN];
398 unsigned char page;
399 struct i2c_client *i2c = rmi4_data->i2c_client;
400
401 page = ((address >> 8) & MASK_8BIT);
402 if (page != rmi4_data->current_page) {
403 buf[0] = MASK_8BIT;
404 buf[1] = page;
405 for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
406 retval = i2c_master_send(i2c, buf, PAGE_SELECT_LEN);
407 if (retval != PAGE_SELECT_LEN) {
408 dev_err(&i2c->dev,
409 "%s: I2C retry %d\n",
410 __func__, retry + 1);
411 msleep(20);
412 } else {
413 rmi4_data->current_page = page;
414 break;
415 }
416 }
417 } else
418 return PAGE_SELECT_LEN;
419 return (retval == PAGE_SELECT_LEN) ? retval : -EIO;
420}
421
422 /**
423 * synaptics_rmi4_i2c_read()
424 *
425 * Called by various functions in this driver, and also exported to
426 * other expansion Function modules such as rmi_dev.
427 *
428 * This function reads data of an arbitrary length from the sensor,
429 * starting from an assigned register address of the sensor, via I2C
430 * with a retry mechanism.
431 */
432static int synaptics_rmi4_i2c_read(struct synaptics_rmi4_data *rmi4_data,
433 unsigned short addr, unsigned char *data, unsigned short length)
434{
435 int retval;
436 unsigned char retry;
437 unsigned char buf;
438 struct i2c_msg msg[] = {
439 {
440 .addr = rmi4_data->i2c_client->addr,
441 .flags = 0,
442 .len = 1,
443 .buf = &buf,
444 },
445 {
446 .addr = rmi4_data->i2c_client->addr,
447 .flags = I2C_M_RD,
448 .len = length,
449 .buf = data,
450 },
451 };
452
453 buf = addr & MASK_8BIT;
454
455 mutex_lock(&(rmi4_data->rmi4_io_ctrl_mutex));
456
457 retval = synaptics_rmi4_set_page(rmi4_data, addr);
458 if (retval != PAGE_SELECT_LEN)
459 goto exit;
460
461 for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
462 if (i2c_transfer(rmi4_data->i2c_client->adapter, msg, 2) == 2) {
463 retval = length;
464 break;
465 }
466 dev_err(&rmi4_data->i2c_client->dev,
467 "%s: I2C retry %d\n",
468 __func__, retry + 1);
469 msleep(20);
470 }
471
472 if (retry == SYN_I2C_RETRY_TIMES) {
473 dev_err(&rmi4_data->i2c_client->dev,
474 "%s: I2C read over retry limit\n",
475 __func__);
476 retval = -EIO;
477 }
478
479exit:
480 mutex_unlock(&(rmi4_data->rmi4_io_ctrl_mutex));
481
482 return retval;
483}
484
485 /**
486 * synaptics_rmi4_i2c_write()
487 *
488 * Called by various functions in this driver, and also exported to
489 * other expansion Function modules such as rmi_dev.
490 *
491 * This function writes data of an arbitrary length to the sensor,
492 * starting from an assigned register address of the sensor, via I2C with
493 * a retry mechanism.
494 */
495static int synaptics_rmi4_i2c_write(struct synaptics_rmi4_data *rmi4_data,
496 unsigned short addr, unsigned char *data, unsigned short length)
497{
498 int retval;
499 unsigned char retry;
500 unsigned char buf[length + 1];
501 struct i2c_msg msg[] = {
502 {
503 .addr = rmi4_data->i2c_client->addr,
504 .flags = 0,
505 .len = length + 1,
506 .buf = buf,
507 }
508 };
509
510 mutex_lock(&(rmi4_data->rmi4_io_ctrl_mutex));
511
512 retval = synaptics_rmi4_set_page(rmi4_data, addr);
513 if (retval != PAGE_SELECT_LEN)
514 goto exit;
515
516 buf[0] = addr & MASK_8BIT;
517 memcpy(&buf[1], &data[0], length);
518
519 for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
520 if (i2c_transfer(rmi4_data->i2c_client->adapter, msg, 1) == 1) {
521 retval = length;
522 break;
523 }
524 dev_err(&rmi4_data->i2c_client->dev,
525 "%s: I2C retry %d\n",
526 __func__, retry + 1);
527 msleep(20);
528 }
529
530 if (retry == SYN_I2C_RETRY_TIMES) {
531 dev_err(&rmi4_data->i2c_client->dev,
532 "%s: I2C write over retry limit\n",
533 __func__);
534 retval = -EIO;
535 }
536
537exit:
538 mutex_unlock(&(rmi4_data->rmi4_io_ctrl_mutex));
539
540 return retval;
541}
542
543 /**
544 * synaptics_rmi4_f11_abs_report()
545 *
546 * Called by synaptics_rmi4_report_touch() when valid Function $11
547 * finger data has been detected.
548 *
549 * This function reads the Function $11 data registers, determines the
550 * status of each finger supported by the Function, processes any
551 * necessary coordinate manipulation, reports the finger data to
552 * the input subsystem, and returns the number of fingers detected.
553 */
554static int synaptics_rmi4_f11_abs_report(struct synaptics_rmi4_data *rmi4_data,
555 struct synaptics_rmi4_fn *fhandler)
556{
557 int retval;
558 unsigned char touch_count = 0; /* number of touch points */
559 unsigned char reg_index;
560 unsigned char finger;
561 unsigned char fingers_supported;
562 unsigned char num_of_finger_status_regs;
563 unsigned char finger_shift;
564 unsigned char finger_status;
565 unsigned char data_reg_blk_size;
566 unsigned char finger_status_reg[3];
567 unsigned char data[F11_STD_DATA_LEN];
568 unsigned short data_addr;
569 unsigned short data_offset;
570 int x;
571 int y;
572 int wx;
573 int wy;
574
575 /*
576 * The number of finger status registers is determined by the
577 * maximum number of fingers supported - 2 bits per finger. So
578 * the number of finger status registers to read is:
579 * register_count = ceil(max_num_of_fingers / 4)
580 */
581 fingers_supported = fhandler->num_of_data_points;
582 num_of_finger_status_regs = (fingers_supported + 3) / 4;
583 data_addr = fhandler->full_addr.data_base;
584 data_reg_blk_size = fhandler->size_of_data_register_block;
585
586 retval = synaptics_rmi4_i2c_read(rmi4_data,
587 data_addr,
588 finger_status_reg,
589 num_of_finger_status_regs);
590 if (retval < 0)
591 return 0;
592
593 for (finger = 0; finger < fingers_supported; finger++) {
594 reg_index = finger / 4;
595 finger_shift = (finger % 4) * 2;
596 finger_status = (finger_status_reg[reg_index] >> finger_shift)
597 & MASK_2BIT;
598
599 /*
600 * Each 2-bit finger status field represents the following:
601 * 00 = finger not present
602 * 01 = finger present and data accurate
603 * 10 = finger present but data may be inaccurate
604 * 11 = reserved
605 */
606#ifdef TYPE_B_PROTOCOL
607 input_mt_slot(rmi4_data->input_dev, finger);
608 input_mt_report_slot_state(rmi4_data->input_dev,
609 MT_TOOL_FINGER, finger_status != 0);
610#endif
611
612 if (finger_status) {
613 data_offset = data_addr +
614 num_of_finger_status_regs +
615 (finger * data_reg_blk_size);
616 retval = synaptics_rmi4_i2c_read(rmi4_data,
617 data_offset,
618 data,
619 data_reg_blk_size);
620 if (retval < 0)
621 return 0;
622
623 x = (data[0] << 4) | (data[2] & MASK_4BIT);
624 y = (data[1] << 4) | ((data[2] >> 4) & MASK_4BIT);
625 wx = (data[3] & MASK_4BIT);
626 wy = (data[3] >> 4) & MASK_4BIT;
627
628 if (rmi4_data->board->x_flip)
629 x = rmi4_data->sensor_max_x - x;
630 if (rmi4_data->board->y_flip)
631 y = rmi4_data->sensor_max_y - y;
632
633 dev_dbg(&rmi4_data->i2c_client->dev,
634 "%s: Finger %d:\n"
635 "status = 0x%02x\n"
636 "x = %d\n"
637 "y = %d\n"
638 "wx = %d\n"
639 "wy = %d\n",
640 __func__, finger,
641 finger_status,
642 x, y, wx, wy);
643
Alexandra Chind5591a62013-02-07 12:59:15 -0800644 input_report_key(rmi4_data->input_dev,
645 BTN_TOUCH, 1);
646 input_report_key(rmi4_data->input_dev,
647 BTN_TOOL_FINGER, 1);
Alexandra Chin669d27c2012-12-24 15:42:30 +0800648 input_report_abs(rmi4_data->input_dev,
649 ABS_MT_POSITION_X, x);
650 input_report_abs(rmi4_data->input_dev,
651 ABS_MT_POSITION_Y, y);
Alexandra Chind5591a62013-02-07 12:59:15 -0800652
Alexandra Chin669d27c2012-12-24 15:42:30 +0800653#ifdef REPORT_2D_W
654 input_report_abs(rmi4_data->input_dev,
655 ABS_MT_TOUCH_MAJOR, max(wx, wy));
656 input_report_abs(rmi4_data->input_dev,
657 ABS_MT_TOUCH_MINOR, min(wx, wy));
658#endif
Alexandra Chind5591a62013-02-07 12:59:15 -0800659#ifndef TYPE_B_PROTOCOL
Alexandra Chin669d27c2012-12-24 15:42:30 +0800660 input_mt_sync(rmi4_data->input_dev);
661#endif
662 touch_count++;
663 }
664 }
665
666#ifndef TYPE_B_PROTOCOL
667 if (!touch_count)
668 input_mt_sync(rmi4_data->input_dev);
669#else
670 /* sync after groups of events */
671 #ifdef KERNEL_ABOVE_3_7
672 input_mt_sync_frame(rmi4_data->input_dev);
673 #endif
674#endif
675
676 input_sync(rmi4_data->input_dev);
677
678 return touch_count;
679}
680
681static void synaptics_rmi4_f1a_report(struct synaptics_rmi4_data *rmi4_data,
682 struct synaptics_rmi4_fn *fhandler)
683{
684 int retval;
685 unsigned char button;
686 unsigned char index;
687 unsigned char shift;
688 unsigned char status;
689 unsigned char *data;
690 unsigned short data_addr = fhandler->full_addr.data_base;
691 struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
692 static unsigned char do_once = 1;
693 static bool current_status[MAX_NUMBER_OF_BUTTONS];
694#ifdef NO_0D_WHILE_2D
695 static bool before_2d_status[MAX_NUMBER_OF_BUTTONS];
696 static bool while_2d_status[MAX_NUMBER_OF_BUTTONS];
697#endif
698
699 if (do_once) {
700 memset(current_status, 0, sizeof(current_status));
701#ifdef NO_0D_WHILE_2D
702 memset(before_2d_status, 0, sizeof(before_2d_status));
703 memset(while_2d_status, 0, sizeof(while_2d_status));
704#endif
705 do_once = 0;
706 }
707
708 retval = synaptics_rmi4_i2c_read(rmi4_data,
709 data_addr,
710 f1a->button_data_buffer,
711 f1a->button_bitmask_size);
712 if (retval < 0) {
713 dev_err(&rmi4_data->i2c_client->dev,
714 "%s: Failed to read button data registers\n",
715 __func__);
716 return;
717 }
718
719 data = f1a->button_data_buffer;
720
721 for (button = 0; button < f1a->valid_button_count; button++) {
722 index = button / 8;
723 shift = button % 8;
724 status = ((data[index] >> shift) & MASK_1BIT);
725
726 if (current_status[button] == status)
727 continue;
728 else
729 current_status[button] = status;
730
731 dev_dbg(&rmi4_data->i2c_client->dev,
732 "%s: Button %d (code %d) ->%d\n",
733 __func__, button,
734 f1a->button_map[button],
735 status);
736#ifdef NO_0D_WHILE_2D
737 if (rmi4_data->fingers_on_2d == false) {
738 if (status == 1) {
739 before_2d_status[button] = 1;
740 } else {
741 if (while_2d_status[button] == 1) {
742 while_2d_status[button] = 0;
743 continue;
744 } else {
745 before_2d_status[button] = 0;
746 }
747 }
748 input_report_key(rmi4_data->input_dev,
749 f1a->button_map[button],
750 status);
751 } else {
752 if (before_2d_status[button] == 1) {
753 before_2d_status[button] = 0;
754 input_report_key(rmi4_data->input_dev,
755 f1a->button_map[button],
756 status);
757 } else {
758 if (status == 1)
759 while_2d_status[button] = 1;
760 else
761 while_2d_status[button] = 0;
762 }
763 }
764#else
765 input_report_key(rmi4_data->input_dev,
766 f1a->button_map[button],
767 status);
768#endif
769 }
770
771 input_sync(rmi4_data->input_dev);
772
773 return;
774}
775
776 /**
777 * synaptics_rmi4_report_touch()
778 *
779 * Called by synaptics_rmi4_sensor_report().
780 *
781 * This function calls the appropriate finger data reporting function
782 * based on the function handler it receives and returns the number of
783 * fingers detected.
784 */
785static void synaptics_rmi4_report_touch(struct synaptics_rmi4_data *rmi4_data,
786 struct synaptics_rmi4_fn *fhandler,
787 unsigned char *touch_count)
788{
789 unsigned char touch_count_2d;
790
791 dev_dbg(&rmi4_data->i2c_client->dev,
792 "%s: Function %02x reporting\n",
793 __func__, fhandler->fn_number);
794
795 switch (fhandler->fn_number) {
796 case SYNAPTICS_RMI4_F11:
797 touch_count_2d = synaptics_rmi4_f11_abs_report(rmi4_data,
798 fhandler);
799
800 *touch_count += touch_count_2d;
801
802 if (touch_count_2d)
803 rmi4_data->fingers_on_2d = true;
804 else
805 rmi4_data->fingers_on_2d = false;
806 break;
807
808 case SYNAPTICS_RMI4_F1A:
809 synaptics_rmi4_f1a_report(rmi4_data, fhandler);
810 break;
811
812 default:
813 break;
814 }
815
816 return;
817}
818
819 /**
820 * synaptics_rmi4_sensor_report()
821 *
822 * Called by synaptics_rmi4_irq().
823 *
824 * This function determines the interrupt source(s) from the sensor
825 * and calls synaptics_rmi4_report_touch() with the appropriate
826 * function handler for each function with valid data inputs.
827 */
828static int synaptics_rmi4_sensor_report(struct synaptics_rmi4_data *rmi4_data)
829{
830 int retval;
831 unsigned char touch_count = 0;
832 unsigned char intr[MAX_INTR_REGISTERS];
833 struct synaptics_rmi4_fn *fhandler;
834 struct synaptics_rmi4_exp_fn *exp_fhandler;
835 struct synaptics_rmi4_device_info *rmi;
836
837 rmi = &(rmi4_data->rmi4_mod_info);
838
839 /*
840 * Get interrupt status information from F01 Data1 register to
841 * determine the source(s) that are flagging the interrupt.
842 */
843 retval = synaptics_rmi4_i2c_read(rmi4_data,
844 rmi4_data->f01_data_base_addr + 1,
845 intr,
846 rmi4_data->num_of_intr_regs);
847 if (retval < 0)
848 return retval;
849
850 /*
851 * Traverse the function handler list and service the source(s)
852 * of the interrupt accordingly.
853 */
Alexandra Chind5591a62013-02-07 12:59:15 -0800854 if (!list_empty(&rmi->support_fn_list)) {
855 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
856 if (fhandler->num_of_data_sources) {
857 if (fhandler->intr_mask &
858 intr[fhandler->intr_reg_num]) {
859 synaptics_rmi4_report_touch(rmi4_data,
860 fhandler, &touch_count);
861 }
Alexandra Chin669d27c2012-12-24 15:42:30 +0800862 }
863 }
864 }
865
866 mutex_lock(&exp_fn_list_mutex);
867 if (!list_empty(&exp_fn_list)) {
868 list_for_each_entry(exp_fhandler, &exp_fn_list, link) {
869 if (exp_fhandler->inserted &&
870 (exp_fhandler->func_attn != NULL))
871 exp_fhandler->func_attn(rmi4_data, intr[0]);
872 }
873 }
874 mutex_unlock(&exp_fn_list_mutex);
875
876 return touch_count;
877}
878
879 /**
880 * synaptics_rmi4_irq()
881 *
882 * Called by the kernel when an interrupt occurs (when the sensor
883 * asserts the attention irq).
884 *
885 * This function is the ISR thread and handles the acquisition
886 * and the reporting of finger data when the presence of fingers
887 * is detected.
888 */
889static irqreturn_t synaptics_rmi4_irq(int irq, void *data)
890{
891 struct synaptics_rmi4_data *rmi4_data = data;
892
893 synaptics_rmi4_sensor_report(rmi4_data);
894
895 return IRQ_HANDLED;
896}
897
898 /**
899 * synaptics_rmi4_irq_enable()
900 *
901 * Called by synaptics_rmi4_probe() and the power management functions
902 * in this driver and also exported to other expansion Function modules
903 * such as rmi_dev.
904 *
905 * This function handles the enabling and disabling of the attention
906 * irq including the setting up of the ISR thread.
907 */
908static int synaptics_rmi4_irq_enable(struct synaptics_rmi4_data *rmi4_data,
909 bool enable)
910{
911 int retval = 0;
912 unsigned char intr_status;
913 const struct synaptics_rmi4_platform_data *platform_data =
914 rmi4_data->i2c_client->dev.platform_data;
915
916 if (enable) {
917 if (rmi4_data->irq_enabled)
918 return retval;
919
920 /* Clear interrupts first */
921 retval = synaptics_rmi4_i2c_read(rmi4_data,
922 rmi4_data->f01_data_base_addr + 1,
923 &intr_status,
924 rmi4_data->num_of_intr_regs);
925 if (retval < 0)
926 return retval;
927
928 retval = request_threaded_irq(rmi4_data->irq, NULL,
929 synaptics_rmi4_irq, platform_data->irq_flags,
930 DRIVER_NAME, rmi4_data);
931 if (retval < 0) {
932 dev_err(&rmi4_data->i2c_client->dev,
933 "%s: Failed to create irq thread\n",
934 __func__);
935 return retval;
936 }
937
938 rmi4_data->irq_enabled = true;
939 } else {
940 if (rmi4_data->irq_enabled) {
941 disable_irq(rmi4_data->irq);
942 free_irq(rmi4_data->irq, rmi4_data);
943 rmi4_data->irq_enabled = false;
944 }
945 }
946
947 return retval;
948}
949
950 /**
951 * synaptics_rmi4_f11_init()
952 *
953 * Called by synaptics_rmi4_query_device().
954 *
955 * This funtion parses information from the Function 11 registers
956 * and determines the number of fingers supported, x and y data ranges,
957 * offset to the associated interrupt status register, interrupt bit
958 * mask, and gathers finger data acquisition capabilities from the query
959 * registers.
960 */
961static int synaptics_rmi4_f11_init(struct synaptics_rmi4_data *rmi4_data,
962 struct synaptics_rmi4_fn *fhandler,
963 struct synaptics_rmi4_fn_desc *fd,
964 unsigned int intr_count)
965{
966 int retval;
967 unsigned char ii;
968 unsigned char intr_offset;
969 unsigned char abs_data_size;
970 unsigned char abs_data_blk_size;
971 unsigned char query[F11_STD_QUERY_LEN];
972 unsigned char control[F11_STD_CTRL_LEN];
973
974 fhandler->fn_number = fd->fn_number;
975 fhandler->num_of_data_sources = fd->intr_src_count;
976
977 retval = synaptics_rmi4_i2c_read(rmi4_data,
978 fhandler->full_addr.query_base,
979 query,
980 sizeof(query));
981 if (retval < 0)
982 return retval;
983
984 /* Maximum number of fingers supported */
985 if ((query[1] & MASK_3BIT) <= 4)
986 fhandler->num_of_data_points = (query[1] & MASK_3BIT) + 1;
987 else if ((query[1] & MASK_3BIT) == 5)
988 fhandler->num_of_data_points = 10;
989
990 rmi4_data->num_of_fingers = fhandler->num_of_data_points;
991
992 retval = synaptics_rmi4_i2c_read(rmi4_data,
993 fhandler->full_addr.ctrl_base,
994 control,
995 sizeof(control));
996 if (retval < 0)
997 return retval;
998
999 /* Maximum x and y */
1000 rmi4_data->sensor_max_x = ((control[6] & MASK_8BIT) << 0) |
1001 ((control[7] & MASK_4BIT) << 8);
1002 rmi4_data->sensor_max_y = ((control[8] & MASK_8BIT) << 0) |
1003 ((control[9] & MASK_4BIT) << 8);
1004 dev_dbg(&rmi4_data->i2c_client->dev,
1005 "%s: Function %02x max x = %d max y = %d\n",
1006 __func__, fhandler->fn_number,
1007 rmi4_data->sensor_max_x,
1008 rmi4_data->sensor_max_y);
1009
1010 fhandler->intr_reg_num = (intr_count + 7) / 8;
1011 if (fhandler->intr_reg_num != 0)
1012 fhandler->intr_reg_num -= 1;
1013
1014 /* Set an enable bit for each data source */
1015 intr_offset = intr_count % 8;
1016 fhandler->intr_mask = 0;
1017 for (ii = intr_offset;
1018 ii < ((fd->intr_src_count & MASK_3BIT) +
1019 intr_offset);
1020 ii++)
1021 fhandler->intr_mask |= 1 << ii;
1022
1023 abs_data_size = query[5] & MASK_2BIT;
1024 abs_data_blk_size = 3 + (2 * (abs_data_size == 0 ? 1 : 0));
1025 fhandler->size_of_data_register_block = abs_data_blk_size;
1026
1027 return retval;
1028}
1029
1030static int synaptics_rmi4_f1a_alloc_mem(struct synaptics_rmi4_data *rmi4_data,
1031 struct synaptics_rmi4_fn *fhandler)
1032{
1033 int retval;
1034 struct synaptics_rmi4_f1a_handle *f1a;
1035
1036 f1a = kzalloc(sizeof(*f1a), GFP_KERNEL);
1037 if (!f1a) {
1038 dev_err(&rmi4_data->i2c_client->dev,
1039 "%s: Failed to alloc mem for function handle\n",
1040 __func__);
1041 return -ENOMEM;
1042 }
1043
1044 fhandler->data = (void *)f1a;
1045
1046 retval = synaptics_rmi4_i2c_read(rmi4_data,
1047 fhandler->full_addr.query_base,
1048 f1a->button_query.data,
1049 sizeof(f1a->button_query.data));
1050 if (retval < 0) {
1051 dev_err(&rmi4_data->i2c_client->dev,
1052 "%s: Failed to read query registers\n",
1053 __func__);
1054 return retval;
1055 }
1056
1057 f1a->button_count = f1a->button_query.max_button_count + 1;
1058 f1a->button_bitmask_size = (f1a->button_count + 7) / 8;
1059
1060 f1a->button_data_buffer = kcalloc(f1a->button_bitmask_size,
1061 sizeof(*(f1a->button_data_buffer)), GFP_KERNEL);
1062 if (!f1a->button_data_buffer) {
1063 dev_err(&rmi4_data->i2c_client->dev,
1064 "%s: Failed to alloc mem for data buffer\n",
1065 __func__);
1066 return -ENOMEM;
1067 }
1068
1069 f1a->button_map = kcalloc(f1a->button_count,
1070 sizeof(*(f1a->button_map)), GFP_KERNEL);
1071 if (!f1a->button_map) {
1072 dev_err(&rmi4_data->i2c_client->dev,
1073 "%s: Failed to alloc mem for button map\n",
1074 __func__);
1075 return -ENOMEM;
1076 }
1077
1078 return 0;
1079}
1080
1081static int synaptics_rmi4_capacitance_button_map(
1082 struct synaptics_rmi4_data *rmi4_data,
1083 struct synaptics_rmi4_fn *fhandler)
1084{
1085 unsigned char ii;
1086 struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
1087 const struct synaptics_rmi4_platform_data *pdata = rmi4_data->board;
1088
1089 if (!pdata->capacitance_button_map) {
1090 dev_err(&rmi4_data->i2c_client->dev,
Alexandra Chind5591a62013-02-07 12:59:15 -08001091 "%s: capacitance_button_map is" \
1092 "NULL in board file\n",
Alexandra Chin669d27c2012-12-24 15:42:30 +08001093 __func__);
1094 return -ENODEV;
1095 } else if (!pdata->capacitance_button_map->map) {
1096 dev_err(&rmi4_data->i2c_client->dev,
1097 "%s: Button map is missing in board file\n",
1098 __func__);
1099 return -ENODEV;
1100 } else {
1101 if (pdata->capacitance_button_map->nbuttons !=
1102 f1a->button_count) {
1103 f1a->valid_button_count = min(f1a->button_count,
1104 pdata->capacitance_button_map->nbuttons);
1105 } else {
1106 f1a->valid_button_count = f1a->button_count;
1107 }
1108
1109 for (ii = 0; ii < f1a->valid_button_count; ii++)
1110 f1a->button_map[ii] =
1111 pdata->capacitance_button_map->map[ii];
1112 }
1113
1114 return 0;
1115}
1116
1117static void synaptics_rmi4_f1a_kfree(struct synaptics_rmi4_fn *fhandler)
1118{
1119 struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
1120
1121 if (f1a) {
1122 kfree(f1a->button_data_buffer);
1123 kfree(f1a->button_map);
1124 kfree(f1a);
1125 fhandler->data = NULL;
1126 }
1127
1128 return;
1129}
1130
1131static int synaptics_rmi4_f1a_init(struct synaptics_rmi4_data *rmi4_data,
1132 struct synaptics_rmi4_fn *fhandler,
1133 struct synaptics_rmi4_fn_desc *fd,
1134 unsigned int intr_count)
1135{
1136 int retval;
1137 unsigned char ii;
1138 unsigned short intr_offset;
1139
1140 fhandler->fn_number = fd->fn_number;
1141 fhandler->num_of_data_sources = fd->intr_src_count;
1142
1143 fhandler->intr_reg_num = (intr_count + 7) / 8;
1144 if (fhandler->intr_reg_num != 0)
1145 fhandler->intr_reg_num -= 1;
1146
1147 /* Set an enable bit for each data source */
1148 intr_offset = intr_count % 8;
1149 fhandler->intr_mask = 0;
1150 for (ii = intr_offset;
1151 ii < ((fd->intr_src_count & MASK_3BIT) +
1152 intr_offset);
1153 ii++)
1154 fhandler->intr_mask |= 1 << ii;
1155
1156 retval = synaptics_rmi4_f1a_alloc_mem(rmi4_data, fhandler);
1157 if (retval < 0)
1158 goto error_exit;
1159
1160 retval = synaptics_rmi4_capacitance_button_map(rmi4_data, fhandler);
1161 if (retval < 0)
1162 goto error_exit;
1163
1164 rmi4_data->button_0d_enabled = 1;
1165
1166 return 0;
1167
1168error_exit:
1169 synaptics_rmi4_f1a_kfree(fhandler);
1170
1171 return retval;
1172}
1173
1174static int synaptics_rmi4_alloc_fh(struct synaptics_rmi4_fn **fhandler,
1175 struct synaptics_rmi4_fn_desc *rmi_fd, int page_number)
1176{
1177 *fhandler = kmalloc(sizeof(**fhandler), GFP_KERNEL);
1178 if (!(*fhandler))
1179 return -ENOMEM;
1180
1181 (*fhandler)->full_addr.data_base =
1182 (rmi_fd->data_base_addr |
1183 (page_number << 8));
1184 (*fhandler)->full_addr.ctrl_base =
1185 (rmi_fd->ctrl_base_addr |
1186 (page_number << 8));
1187 (*fhandler)->full_addr.cmd_base =
1188 (rmi_fd->cmd_base_addr |
1189 (page_number << 8));
1190 (*fhandler)->full_addr.query_base =
1191 (rmi_fd->query_base_addr |
1192 (page_number << 8));
1193
1194 return 0;
1195}
1196
Alexandra Chind5591a62013-02-07 12:59:15 -08001197
1198 /**
1199 * synaptics_rmi4_query_device_info()
1200 *
1201 * Called by synaptics_rmi4_query_device().
1202 *
1203 */
1204static int synaptics_rmi4_query_device_info(
1205 struct synaptics_rmi4_data *rmi4_data)
1206{
1207 int retval;
1208 unsigned char f01_query[F01_STD_QUERY_LEN];
1209 struct synaptics_rmi4_device_info *rmi = &(rmi4_data->rmi4_mod_info);
1210
1211 retval = synaptics_rmi4_i2c_read(rmi4_data,
1212 rmi4_data->f01_query_base_addr,
1213 f01_query,
1214 sizeof(f01_query));
1215 if (retval < 0)
1216 return retval;
1217
1218 /* RMI Version 4.0 currently supported */
1219 rmi->version_major = 4;
1220 rmi->version_minor = 0;
1221
1222 rmi->manufacturer_id = f01_query[0];
1223 rmi->product_props = f01_query[1];
1224 rmi->product_info[0] = f01_query[2] & MASK_7BIT;
1225 rmi->product_info[1] = f01_query[3] & MASK_7BIT;
1226 rmi->date_code[0] = f01_query[4] & MASK_5BIT;
1227 rmi->date_code[1] = f01_query[5] & MASK_4BIT;
1228 rmi->date_code[2] = f01_query[6] & MASK_5BIT;
1229 rmi->tester_id = ((f01_query[7] & MASK_7BIT) << 8) |
1230 (f01_query[8] & MASK_7BIT);
1231 rmi->serial_number = ((f01_query[9] & MASK_7BIT) << 8) |
1232 (f01_query[10] & MASK_7BIT);
1233 memcpy(rmi->product_id_string, &f01_query[11], 10);
1234
1235 if (rmi->manufacturer_id != 1) {
1236 dev_err(&rmi4_data->i2c_client->dev,
1237 "%s: Non-Synaptics device found, manufacturer ID = %d\n",
1238 __func__, rmi->manufacturer_id);
1239 }
1240
1241 retval = synaptics_rmi4_i2c_read(rmi4_data,
1242 rmi4_data->f01_query_base_addr + F01_BUID_ID_OFFSET,
1243 rmi->build_id,
1244 sizeof(rmi->build_id));
1245 if (retval < 0) {
1246 dev_err(&rmi4_data->i2c_client->dev,
1247 "%s: Failed to read firmware build id (code %d)\n",
1248 __func__, retval);
1249 return retval;
1250 }
1251 return retval;
1252}
1253
Alexandra Chin669d27c2012-12-24 15:42:30 +08001254 /**
1255 * synaptics_rmi4_query_device()
1256 *
1257 * Called by synaptics_rmi4_probe().
1258 *
1259 * This funtion scans the page description table, records the offsets
1260 * to the register types of Function $01, sets up the function handlers
1261 * for Function $11 and Function $12, determines the number of interrupt
1262 * sources from the sensor, adds valid Functions with data inputs to the
1263 * Function linked list, parses information from the query registers of
1264 * Function $01, and enables the interrupt sources from the valid Functions
1265 * with data inputs.
1266 */
1267static int synaptics_rmi4_query_device(struct synaptics_rmi4_data *rmi4_data)
1268{
1269 int retval;
1270 unsigned char ii;
1271 unsigned char page_number;
1272 unsigned char intr_count = 0;
1273 unsigned char data_sources = 0;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001274 unsigned short pdt_entry_addr;
1275 unsigned short intr_addr;
1276 struct synaptics_rmi4_f01_device_status status;
1277 struct synaptics_rmi4_fn_desc rmi_fd;
1278 struct synaptics_rmi4_fn *fhandler;
1279 struct synaptics_rmi4_device_info *rmi;
1280
1281 rmi = &(rmi4_data->rmi4_mod_info);
1282
1283 INIT_LIST_HEAD(&rmi->support_fn_list);
1284
1285 /* Scan the page description tables of the pages to service */
1286 for (page_number = 0; page_number < PAGES_TO_SERVICE; page_number++) {
1287 for (pdt_entry_addr = PDT_START; pdt_entry_addr > PDT_END;
1288 pdt_entry_addr -= PDT_ENTRY_SIZE) {
1289 pdt_entry_addr |= (page_number << 8);
1290
1291 retval = synaptics_rmi4_i2c_read(rmi4_data,
1292 pdt_entry_addr,
1293 (unsigned char *)&rmi_fd,
1294 sizeof(rmi_fd));
1295 if (retval < 0)
1296 return retval;
1297
1298 fhandler = NULL;
1299
1300 if (rmi_fd.fn_number == 0) {
1301 dev_dbg(&rmi4_data->i2c_client->dev,
1302 "%s: Reached end of PDT\n",
1303 __func__);
1304 break;
1305 }
1306
1307 dev_dbg(&rmi4_data->i2c_client->dev,
1308 "%s: F%02x found (page %d)\n",
1309 __func__, rmi_fd.fn_number,
1310 page_number);
1311
1312 switch (rmi_fd.fn_number) {
1313 case SYNAPTICS_RMI4_F01:
1314 rmi4_data->f01_query_base_addr =
1315 rmi_fd.query_base_addr;
1316 rmi4_data->f01_ctrl_base_addr =
1317 rmi_fd.ctrl_base_addr;
1318 rmi4_data->f01_data_base_addr =
1319 rmi_fd.data_base_addr;
1320 rmi4_data->f01_cmd_base_addr =
1321 rmi_fd.cmd_base_addr;
1322
Alexandra Chind5591a62013-02-07 12:59:15 -08001323 retval =
1324 synaptics_rmi4_query_device_info(rmi4_data);
1325 if (retval < 0)
1326 return retval;
1327
Alexandra Chin669d27c2012-12-24 15:42:30 +08001328 retval = synaptics_rmi4_i2c_read(rmi4_data,
1329 rmi4_data->f01_data_base_addr,
1330 status.data,
1331 sizeof(status.data));
1332 if (retval < 0)
1333 return retval;
1334
1335 if (status.flash_prog == 1) {
1336 pr_notice("%s: In flash prog mode, status = 0x%02x\n",
1337 __func__,
1338 status.status_code);
1339 goto flash_prog_mode;
1340 }
Alexandra Chind5591a62013-02-07 12:59:15 -08001341 break;
1342
1343 case SYNAPTICS_RMI4_F34:
1344 retval = synaptics_rmi4_i2c_read(rmi4_data,
1345 rmi_fd.ctrl_base_addr,
1346 rmi->config_id,
1347 sizeof(rmi->config_id));
1348 if (retval < 0)
1349 return retval;
1350 break;
1351
Alexandra Chin669d27c2012-12-24 15:42:30 +08001352 case SYNAPTICS_RMI4_F11:
1353 if (rmi_fd.intr_src_count == 0)
1354 break;
1355
1356 retval = synaptics_rmi4_alloc_fh(&fhandler,
1357 &rmi_fd, page_number);
1358 if (retval < 0) {
1359 dev_err(&rmi4_data->i2c_client->dev,
1360 "%s: Failed to alloc for F%d\n",
1361 __func__,
1362 rmi_fd.fn_number);
1363 return retval;
1364 }
1365
1366 retval = synaptics_rmi4_f11_init(rmi4_data,
1367 fhandler, &rmi_fd, intr_count);
1368 if (retval < 0)
1369 return retval;
1370 break;
1371
1372 case SYNAPTICS_RMI4_F1A:
1373 if (rmi_fd.intr_src_count == 0)
1374 break;
1375
1376 retval = synaptics_rmi4_alloc_fh(&fhandler,
1377 &rmi_fd, page_number);
1378 if (retval < 0) {
1379 dev_err(&rmi4_data->i2c_client->dev,
1380 "%s: Failed to alloc for F%d\n",
1381 __func__,
1382 rmi_fd.fn_number);
1383 return retval;
1384 }
1385
1386 retval = synaptics_rmi4_f1a_init(rmi4_data,
1387 fhandler, &rmi_fd, intr_count);
1388 if (retval < 0)
1389 return retval;
1390 break;
1391 }
1392
1393 /* Accumulate the interrupt count */
1394 intr_count += (rmi_fd.intr_src_count & MASK_3BIT);
1395
1396 if (fhandler && rmi_fd.intr_src_count) {
1397 list_add_tail(&fhandler->link,
1398 &rmi->support_fn_list);
1399 }
1400 }
1401 }
1402
1403flash_prog_mode:
1404 rmi4_data->num_of_intr_regs = (intr_count + 7) / 8;
1405 dev_dbg(&rmi4_data->i2c_client->dev,
1406 "%s: Number of interrupt registers = %d\n",
1407 __func__, rmi4_data->num_of_intr_regs);
1408
Alexandra Chin669d27c2012-12-24 15:42:30 +08001409 memset(rmi4_data->intr_mask, 0x00, sizeof(rmi4_data->intr_mask));
1410
1411 /*
1412 * Map out the interrupt bit masks for the interrupt sources
1413 * from the registered function handlers.
1414 */
Alexandra Chind5591a62013-02-07 12:59:15 -08001415 if (!list_empty(&rmi->support_fn_list)) {
1416 list_for_each_entry(fhandler, &rmi->support_fn_list, link)
1417 data_sources += fhandler->num_of_data_sources;
1418 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001419 if (data_sources) {
Alexandra Chind5591a62013-02-07 12:59:15 -08001420 if (!list_empty(&rmi->support_fn_list)) {
1421 list_for_each_entry(fhandler,
1422 &rmi->support_fn_list, link) {
1423 if (fhandler->num_of_data_sources) {
1424 rmi4_data->intr_mask[fhandler->intr_reg_num] |=
1425 fhandler->intr_mask;
1426 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001427 }
1428 }
1429 }
1430
1431 /* Enable the interrupt sources */
1432 for (ii = 0; ii < rmi4_data->num_of_intr_regs; ii++) {
1433 if (rmi4_data->intr_mask[ii] != 0x00) {
1434 dev_dbg(&rmi4_data->i2c_client->dev,
1435 "%s: Interrupt enable mask %d = 0x%02x\n",
1436 __func__, ii, rmi4_data->intr_mask[ii]);
1437 intr_addr = rmi4_data->f01_ctrl_base_addr + 1 + ii;
1438 retval = synaptics_rmi4_i2c_write(rmi4_data,
1439 intr_addr,
1440 &(rmi4_data->intr_mask[ii]),
1441 sizeof(rmi4_data->intr_mask[ii]));
1442 if (retval < 0)
1443 return retval;
1444 }
1445 }
1446
1447 return 0;
1448}
1449
1450static int synaptics_rmi4_reset_device(struct synaptics_rmi4_data *rmi4_data)
1451{
1452 int retval;
1453 unsigned char command = 0x01;
1454 struct synaptics_rmi4_fn *fhandler;
1455 struct synaptics_rmi4_device_info *rmi;
1456
1457 rmi = &(rmi4_data->rmi4_mod_info);
1458
1459 retval = synaptics_rmi4_i2c_write(rmi4_data,
1460 rmi4_data->f01_cmd_base_addr,
1461 &command,
1462 sizeof(command));
1463 if (retval < 0) {
1464 dev_err(&rmi4_data->i2c_client->dev,
1465 "%s: Failed to issue reset command, error = %d\n",
1466 __func__, retval);
1467 return retval;
1468 }
1469
1470 msleep(100);
1471
Alexandra Chind5591a62013-02-07 12:59:15 -08001472 if (!list_empty(&rmi->support_fn_list)) {
1473 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
1474 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
1475 synaptics_rmi4_f1a_kfree(fhandler);
1476 else
1477 kfree(fhandler->data);
1478 kfree(fhandler);
1479 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001480 }
1481
1482 retval = synaptics_rmi4_query_device(rmi4_data);
1483 if (retval < 0) {
1484 dev_err(&rmi4_data->i2c_client->dev,
1485 "%s: Failed to query device\n",
1486 __func__);
1487 return retval;
1488 }
1489
1490 return 0;
1491}
1492
1493/**
1494* synaptics_rmi4_detection_work()
1495*
1496* Called by the kernel at the scheduled time.
1497*
1498* This function is a self-rearming work thread that checks for the
1499* insertion and removal of other expansion Function modules such as
1500* rmi_dev and calls their initialization and removal callback functions
1501* accordingly.
1502*/
1503static void synaptics_rmi4_detection_work(struct work_struct *work)
1504{
1505 struct synaptics_rmi4_exp_fn *exp_fhandler, *next_list_entry;
1506 struct synaptics_rmi4_data *rmi4_data =
1507 container_of(work, struct synaptics_rmi4_data,
1508 det_work.work);
1509
Alexandra Chin669d27c2012-12-24 15:42:30 +08001510 mutex_lock(&exp_fn_list_mutex);
1511 if (!list_empty(&exp_fn_list)) {
1512 list_for_each_entry_safe(exp_fhandler,
1513 next_list_entry,
1514 &exp_fn_list,
1515 link) {
1516 if ((exp_fhandler->func_init != NULL) &&
1517 (exp_fhandler->inserted == false)) {
1518 exp_fhandler->func_init(rmi4_data);
1519 exp_fhandler->inserted = true;
1520 } else if ((exp_fhandler->func_init == NULL) &&
1521 (exp_fhandler->inserted == true)) {
1522 exp_fhandler->func_remove(rmi4_data);
1523 list_del(&exp_fhandler->link);
1524 kfree(exp_fhandler);
1525 }
1526 }
1527 }
1528 mutex_unlock(&exp_fn_list_mutex);
1529
1530 return;
1531}
1532
1533/**
1534* synaptics_rmi4_new_function()
1535*
1536* Called by other expansion Function modules in their module init and
1537* module exit functions.
1538*
1539* This function is used by other expansion Function modules such as
1540* rmi_dev to register themselves with the driver by providing their
1541* initialization and removal callback function pointers so that they
1542* can be inserted or removed dynamically at module init and exit times,
1543* respectively.
1544*/
1545void synaptics_rmi4_new_function(enum exp_fn fn_type, bool insert,
1546 int (*func_init)(struct synaptics_rmi4_data *rmi4_data),
1547 void (*func_remove)(struct synaptics_rmi4_data *rmi4_data),
1548 void (*func_attn)(struct synaptics_rmi4_data *rmi4_data,
1549 unsigned char intr_mask))
1550{
1551 struct synaptics_rmi4_exp_fn *exp_fhandler;
1552
1553 if (!exp_fn_inited) {
1554 mutex_init(&exp_fn_list_mutex);
1555 INIT_LIST_HEAD(&exp_fn_list);
1556 exp_fn_inited = 1;
1557 }
1558
1559 mutex_lock(&exp_fn_list_mutex);
1560 if (insert) {
1561 exp_fhandler = kzalloc(sizeof(*exp_fhandler), GFP_KERNEL);
1562 if (!exp_fhandler) {
1563 pr_err("%s: Failed to alloc mem for expansion function\n",
1564 __func__);
1565 goto exit;
1566 }
1567 exp_fhandler->fn_type = fn_type;
1568 exp_fhandler->func_init = func_init;
1569 exp_fhandler->func_attn = func_attn;
1570 exp_fhandler->func_remove = func_remove;
1571 exp_fhandler->inserted = false;
1572 list_add_tail(&exp_fhandler->link, &exp_fn_list);
1573 } else {
Alexandra Chind5591a62013-02-07 12:59:15 -08001574 if (!list_empty(&exp_fn_list)) {
1575 list_for_each_entry(exp_fhandler, &exp_fn_list, link) {
1576 if (exp_fhandler->func_init == func_init) {
1577 exp_fhandler->inserted = false;
1578 exp_fhandler->func_init = NULL;
1579 exp_fhandler->func_attn = NULL;
1580 goto exit;
1581 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001582 }
1583 }
1584 }
1585
1586exit:
1587 mutex_unlock(&exp_fn_list_mutex);
1588
1589 return;
1590}
1591EXPORT_SYMBOL(synaptics_rmi4_new_function);
1592
1593 /**
1594 * synaptics_rmi4_probe()
1595 *
1596 * Called by the kernel when an association with an I2C device of the
1597 * same name is made (after doing i2c_add_driver).
1598 *
1599 * This funtion allocates and initializes the resources for the driver
1600 * as an input driver, turns on the power to the sensor, queries the
1601 * sensor for its supported Functions and characteristics, registers
1602 * the driver to the input subsystem, sets up the interrupt, handles
1603 * the registration of the early_suspend and late_resume functions,
1604 * and creates a work queue for detection of other expansion Function
1605 * modules.
1606 */
1607static int __devinit synaptics_rmi4_probe(struct i2c_client *client,
1608 const struct i2c_device_id *dev_id)
1609{
1610 int retval;
1611 unsigned char ii;
1612 unsigned char attr_count;
1613 struct synaptics_rmi4_f1a_handle *f1a;
1614 struct synaptics_rmi4_fn *fhandler;
1615 struct synaptics_rmi4_data *rmi4_data;
1616 struct synaptics_rmi4_device_info *rmi;
1617 const struct synaptics_rmi4_platform_data *platform_data =
1618 client->dev.platform_data;
1619
1620 if (!i2c_check_functionality(client->adapter,
1621 I2C_FUNC_SMBUS_BYTE_DATA)) {
1622 dev_err(&client->dev,
1623 "%s: SMBus byte data not supported\n",
1624 __func__);
1625 return -EIO;
1626 }
1627
1628 if (!platform_data) {
1629 dev_err(&client->dev,
1630 "%s: No platform data found\n",
1631 __func__);
1632 return -EINVAL;
1633 }
1634
1635 rmi4_data = kzalloc(sizeof(*rmi4_data) * 2, GFP_KERNEL);
1636 if (!rmi4_data) {
1637 dev_err(&client->dev,
1638 "%s: Failed to alloc mem for rmi4_data\n",
1639 __func__);
1640 return -ENOMEM;
1641 }
1642
1643 rmi = &(rmi4_data->rmi4_mod_info);
1644
1645 rmi4_data->input_dev = input_allocate_device();
1646 if (rmi4_data->input_dev == NULL) {
1647 dev_err(&client->dev,
1648 "%s: Failed to allocate input device\n",
1649 __func__);
1650 retval = -ENOMEM;
1651 goto err_input_device;
1652 }
Alexandra Chind5591a62013-02-07 12:59:15 -08001653
Alexandra Chin669d27c2012-12-24 15:42:30 +08001654 if (platform_data->regulator_en) {
1655 rmi4_data->regulator = regulator_get(&client->dev, "vdd");
1656 if (IS_ERR(rmi4_data->regulator)) {
1657 dev_err(&client->dev,
1658 "%s: Failed to get regulator\n",
1659 __func__);
1660 retval = PTR_ERR(rmi4_data->regulator);
1661 goto err_regulator;
1662 }
1663 regulator_enable(rmi4_data->regulator);
1664 }
Alexandra Chind5591a62013-02-07 12:59:15 -08001665
Alexandra Chin669d27c2012-12-24 15:42:30 +08001666 rmi4_data->i2c_client = client;
1667 rmi4_data->current_page = MASK_8BIT;
1668 rmi4_data->board = platform_data;
1669 rmi4_data->touch_stopped = false;
1670 rmi4_data->sensor_sleep = false;
1671 rmi4_data->irq_enabled = false;
1672
1673 rmi4_data->i2c_read = synaptics_rmi4_i2c_read;
1674 rmi4_data->i2c_write = synaptics_rmi4_i2c_write;
1675 rmi4_data->irq_enable = synaptics_rmi4_irq_enable;
1676 rmi4_data->reset_device = synaptics_rmi4_reset_device;
1677
1678 init_waitqueue_head(&rmi4_data->wait);
1679 mutex_init(&(rmi4_data->rmi4_io_ctrl_mutex));
1680
1681 retval = synaptics_rmi4_query_device(rmi4_data);
1682 if (retval < 0) {
1683 dev_err(&client->dev,
1684 "%s: Failed to query device\n",
1685 __func__);
1686 goto err_query_device;
1687 }
1688
1689 i2c_set_clientdata(client, rmi4_data);
1690
1691 rmi4_data->input_dev->name = DRIVER_NAME;
1692 rmi4_data->input_dev->phys = INPUT_PHYS_NAME;
1693 rmi4_data->input_dev->id.bustype = BUS_I2C;
Alexandra Chin5d2999d2013-02-22 15:09:29 -08001694 rmi4_data->input_dev->id.product = SYNAPTICS_DSX_DRIVER_PRODUCT;
1695 rmi4_data->input_dev->id.version = SYNAPTICS_DSX_DRIVER_VERSION;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001696 rmi4_data->input_dev->dev.parent = &client->dev;
1697 input_set_drvdata(rmi4_data->input_dev, rmi4_data);
1698
1699 set_bit(EV_SYN, rmi4_data->input_dev->evbit);
1700 set_bit(EV_KEY, rmi4_data->input_dev->evbit);
1701 set_bit(EV_ABS, rmi4_data->input_dev->evbit);
Alexandra Chind5591a62013-02-07 12:59:15 -08001702 set_bit(BTN_TOUCH, rmi4_data->input_dev->keybit);
1703 set_bit(BTN_TOOL_FINGER, rmi4_data->input_dev->keybit);
Alexandra Chin669d27c2012-12-24 15:42:30 +08001704
1705#ifdef INPUT_PROP_DIRECT
1706 set_bit(INPUT_PROP_DIRECT, rmi4_data->input_dev->propbit);
1707#endif
1708
1709 input_set_abs_params(rmi4_data->input_dev,
1710 ABS_MT_POSITION_X, 0,
1711 rmi4_data->sensor_max_x, 0, 0);
1712 input_set_abs_params(rmi4_data->input_dev,
1713 ABS_MT_POSITION_Y, 0,
1714 rmi4_data->sensor_max_y, 0, 0);
1715#ifdef REPORT_2D_W
1716 input_set_abs_params(rmi4_data->input_dev,
1717 ABS_MT_TOUCH_MAJOR, 0,
1718 MAX_ABS_MT_TOUCH_MAJOR, 0, 0);
1719#endif
1720
1721#ifdef TYPE_B_PROTOCOL
1722 input_mt_init_slots(rmi4_data->input_dev,
1723 rmi4_data->num_of_fingers);
1724#endif
1725
1726 f1a = NULL;
Alexandra Chind5591a62013-02-07 12:59:15 -08001727 if (!list_empty(&rmi->support_fn_list)) {
1728 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
1729 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
1730 f1a = fhandler->data;
1731 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001732 }
1733
1734 if (f1a) {
1735 for (ii = 0; ii < f1a->valid_button_count; ii++) {
1736 set_bit(f1a->button_map[ii],
1737 rmi4_data->input_dev->keybit);
1738 input_set_capability(rmi4_data->input_dev,
1739 EV_KEY, f1a->button_map[ii]);
1740 }
1741 }
1742
1743 retval = input_register_device(rmi4_data->input_dev);
1744 if (retval) {
1745 dev_err(&client->dev,
1746 "%s: Failed to register input device\n",
1747 __func__);
1748 goto err_register_input;
1749 }
1750
1751#ifdef CONFIG_HAS_EARLYSUSPEND
1752 rmi4_data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
1753 rmi4_data->early_suspend.suspend = synaptics_rmi4_early_suspend;
1754 rmi4_data->early_suspend.resume = synaptics_rmi4_late_resume;
1755 register_early_suspend(&rmi4_data->early_suspend);
1756#endif
1757
1758 if (!exp_fn_inited) {
1759 mutex_init(&exp_fn_list_mutex);
1760 INIT_LIST_HEAD(&exp_fn_list);
1761 exp_fn_inited = 1;
1762 }
1763
1764 rmi4_data->det_workqueue =
1765 create_singlethread_workqueue("rmi_det_workqueue");
1766 INIT_DELAYED_WORK(&rmi4_data->det_work,
1767 synaptics_rmi4_detection_work);
1768 queue_delayed_work(rmi4_data->det_workqueue,
1769 &rmi4_data->det_work,
1770 msecs_to_jiffies(EXP_FN_DET_INTERVAL));
1771
Alexandra Chin669d27c2012-12-24 15:42:30 +08001772 rmi4_data->irq = gpio_to_irq(platform_data->irq_gpio);
1773
1774 retval = synaptics_rmi4_irq_enable(rmi4_data, true);
1775 if (retval < 0) {
1776 dev_err(&client->dev,
1777 "%s: Failed to enable attention interrupt\n",
1778 __func__);
1779 goto err_enable_irq;
1780 }
1781
1782 for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
1783 retval = sysfs_create_file(&rmi4_data->input_dev->dev.kobj,
1784 &attrs[attr_count].attr);
1785 if (retval < 0) {
1786 dev_err(&client->dev,
1787 "%s: Failed to create sysfs attributes\n",
1788 __func__);
1789 goto err_sysfs;
1790 }
1791 }
1792
1793 return retval;
1794
1795err_sysfs:
1796 for (attr_count--; attr_count >= 0; attr_count--) {
1797 sysfs_remove_file(&rmi4_data->input_dev->dev.kobj,
1798 &attrs[attr_count].attr);
1799 }
1800
1801err_enable_irq:
Alexandra Chin669d27c2012-12-24 15:42:30 +08001802 input_unregister_device(rmi4_data->input_dev);
1803
1804err_register_input:
1805err_query_device:
1806 if (platform_data->regulator_en) {
1807 regulator_disable(rmi4_data->regulator);
1808 regulator_put(rmi4_data->regulator);
1809 }
1810
Alexandra Chind5591a62013-02-07 12:59:15 -08001811 if (!list_empty(&rmi->support_fn_list)) {
1812 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
1813 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
1814 synaptics_rmi4_f1a_kfree(fhandler);
1815 else
1816 kfree(fhandler->data);
1817 kfree(fhandler);
1818 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001819 }
Alexandra Chind5591a62013-02-07 12:59:15 -08001820
Alexandra Chin669d27c2012-12-24 15:42:30 +08001821err_regulator:
Alexandra Chin669d27c2012-12-24 15:42:30 +08001822 input_free_device(rmi4_data->input_dev);
1823 rmi4_data->input_dev = NULL;
1824
1825err_input_device:
1826 kfree(rmi4_data);
1827
1828 return retval;
1829}
1830
1831 /**
1832 * synaptics_rmi4_remove()
1833 *
1834 * Called by the kernel when the association with an I2C device of the
1835 * same name is broken (when the driver is unloaded).
1836 *
1837 * This funtion terminates the work queue, stops sensor data acquisition,
1838 * frees the interrupt, unregisters the driver from the input subsystem,
1839 * turns off the power to the sensor, and frees other allocated resources.
1840 */
1841static int __devexit synaptics_rmi4_remove(struct i2c_client *client)
1842{
1843 unsigned char attr_count;
1844 struct synaptics_rmi4_fn *fhandler;
1845 struct synaptics_rmi4_data *rmi4_data = i2c_get_clientdata(client);
1846 struct synaptics_rmi4_device_info *rmi;
1847 const struct synaptics_rmi4_platform_data *platform_data =
1848 rmi4_data->board;
1849
1850 rmi = &(rmi4_data->rmi4_mod_info);
1851
1852 cancel_delayed_work_sync(&rmi4_data->det_work);
1853 flush_workqueue(rmi4_data->det_workqueue);
1854 destroy_workqueue(rmi4_data->det_workqueue);
1855
1856 rmi4_data->touch_stopped = true;
1857 wake_up(&rmi4_data->wait);
1858
1859 synaptics_rmi4_irq_enable(rmi4_data, false);
1860
1861 for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
1862 sysfs_remove_file(&rmi4_data->input_dev->dev.kobj,
1863 &attrs[attr_count].attr);
1864 }
1865
1866 input_unregister_device(rmi4_data->input_dev);
1867
1868 if (platform_data->regulator_en) {
1869 regulator_disable(rmi4_data->regulator);
1870 regulator_put(rmi4_data->regulator);
1871 }
1872
Alexandra Chind5591a62013-02-07 12:59:15 -08001873 if (!list_empty(&rmi->support_fn_list)) {
1874 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
1875 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
1876 synaptics_rmi4_f1a_kfree(fhandler);
1877 else
1878 kfree(fhandler->data);
1879 kfree(fhandler);
1880 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001881 }
1882 input_free_device(rmi4_data->input_dev);
1883
1884 kfree(rmi4_data);
1885
1886 return 0;
1887}
1888
1889#ifdef CONFIG_PM
1890 /**
1891 * synaptics_rmi4_sensor_sleep()
1892 *
1893 * Called by synaptics_rmi4_early_suspend() and synaptics_rmi4_suspend().
1894 *
1895 * This function stops finger data acquisition and puts the sensor to sleep.
1896 */
1897static void synaptics_rmi4_sensor_sleep(struct synaptics_rmi4_data *rmi4_data)
1898{
1899 int retval;
1900 unsigned char device_ctrl;
1901
1902 retval = synaptics_rmi4_i2c_read(rmi4_data,
1903 rmi4_data->f01_ctrl_base_addr,
1904 &device_ctrl,
1905 sizeof(device_ctrl));
1906 if (retval < 0) {
1907 dev_err(&(rmi4_data->input_dev->dev),
1908 "%s: Failed to enter sleep mode\n",
1909 __func__);
1910 rmi4_data->sensor_sleep = false;
1911 return;
1912 }
1913
1914 device_ctrl = (device_ctrl & ~MASK_3BIT);
1915 device_ctrl = (device_ctrl | NO_SLEEP_OFF | SENSOR_SLEEP);
1916
1917 retval = synaptics_rmi4_i2c_write(rmi4_data,
1918 rmi4_data->f01_ctrl_base_addr,
1919 &device_ctrl,
1920 sizeof(device_ctrl));
1921 if (retval < 0) {
1922 dev_err(&(rmi4_data->input_dev->dev),
1923 "%s: Failed to enter sleep mode\n",
1924 __func__);
1925 rmi4_data->sensor_sleep = false;
1926 return;
1927 } else {
1928 rmi4_data->sensor_sleep = true;
1929 }
1930
1931 return;
1932}
1933
1934 /**
1935 * synaptics_rmi4_sensor_wake()
1936 *
1937 * Called by synaptics_rmi4_resume() and synaptics_rmi4_late_resume().
1938 *
1939 * This function wakes the sensor from sleep.
1940 */
1941static void synaptics_rmi4_sensor_wake(struct synaptics_rmi4_data *rmi4_data)
1942{
1943 int retval;
1944 unsigned char device_ctrl;
1945
1946 retval = synaptics_rmi4_i2c_read(rmi4_data,
1947 rmi4_data->f01_ctrl_base_addr,
1948 &device_ctrl,
1949 sizeof(device_ctrl));
1950 if (retval < 0) {
1951 dev_err(&(rmi4_data->input_dev->dev),
1952 "%s: Failed to wake from sleep mode\n",
1953 __func__);
1954 rmi4_data->sensor_sleep = true;
1955 return;
1956 }
1957
1958 device_ctrl = (device_ctrl & ~MASK_3BIT);
1959 device_ctrl = (device_ctrl | NO_SLEEP_OFF | NORMAL_OPERATION);
1960
1961 retval = synaptics_rmi4_i2c_write(rmi4_data,
1962 rmi4_data->f01_ctrl_base_addr,
1963 &device_ctrl,
1964 sizeof(device_ctrl));
1965 if (retval < 0) {
1966 dev_err(&(rmi4_data->input_dev->dev),
1967 "%s: Failed to wake from sleep mode\n",
1968 __func__);
1969 rmi4_data->sensor_sleep = true;
1970 return;
1971 } else {
1972 rmi4_data->sensor_sleep = false;
1973 }
1974
1975 return;
1976}
1977
1978#ifdef CONFIG_HAS_EARLYSUSPEND
1979 /**
1980 * synaptics_rmi4_early_suspend()
1981 *
1982 * Called by the kernel during the early suspend phase when the system
1983 * enters suspend.
1984 *
1985 * This function calls synaptics_rmi4_sensor_sleep() to stop finger
1986 * data acquisition and put the sensor to sleep.
1987 */
1988static void synaptics_rmi4_early_suspend(struct early_suspend *h)
1989{
1990 struct synaptics_rmi4_data *rmi4_data =
1991 container_of(h, struct synaptics_rmi4_data,
1992 early_suspend);
1993
1994 rmi4_data->touch_stopped = true;
1995 wake_up(&rmi4_data->wait);
1996 synaptics_rmi4_irq_enable(rmi4_data, false);
1997 synaptics_rmi4_sensor_sleep(rmi4_data);
1998
1999 if (rmi4_data->full_pm_cycle)
2000 synaptics_rmi4_suspend(&(rmi4_data->input_dev->dev));
2001
2002 return;
2003}
2004
2005 /**
2006 * synaptics_rmi4_late_resume()
2007 *
2008 * Called by the kernel during the late resume phase when the system
2009 * wakes up from suspend.
2010 *
2011 * This function goes through the sensor wake process if the system wakes
2012 * up from early suspend (without going into suspend).
2013 */
2014static void synaptics_rmi4_late_resume(struct early_suspend *h)
2015{
2016 struct synaptics_rmi4_data *rmi4_data =
2017 container_of(h, struct synaptics_rmi4_data,
2018 early_suspend);
2019
2020 if (rmi4_data->full_pm_cycle)
2021 synaptics_rmi4_resume(&(rmi4_data->input_dev->dev));
2022
2023 if (rmi4_data->sensor_sleep == true) {
2024 synaptics_rmi4_sensor_wake(rmi4_data);
2025 rmi4_data->touch_stopped = false;
2026 synaptics_rmi4_irq_enable(rmi4_data, true);
2027 }
2028
2029 return;
2030}
2031#endif
2032
2033 /**
2034 * synaptics_rmi4_suspend()
2035 *
2036 * Called by the kernel during the suspend phase when the system
2037 * enters suspend.
2038 *
2039 * This function stops finger data acquisition and puts the sensor to
2040 * sleep (if not already done so during the early suspend phase),
2041 * disables the interrupt, and turns off the power to the sensor.
2042 */
2043static int synaptics_rmi4_suspend(struct device *dev)
2044{
2045 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
2046 const struct synaptics_rmi4_platform_data *platform_data =
2047 rmi4_data->board;
2048
2049 if (!rmi4_data->sensor_sleep) {
2050 rmi4_data->touch_stopped = true;
2051 wake_up(&rmi4_data->wait);
2052 synaptics_rmi4_irq_enable(rmi4_data, false);
2053 synaptics_rmi4_sensor_sleep(rmi4_data);
2054 }
2055
2056 if (platform_data->regulator_en)
2057 regulator_disable(rmi4_data->regulator);
2058
2059 return 0;
2060}
2061
2062 /**
2063 * synaptics_rmi4_resume()
2064 *
2065 * Called by the kernel during the resume phase when the system
2066 * wakes up from suspend.
2067 *
2068 * This function turns on the power to the sensor, wakes the sensor
2069 * from sleep, enables the interrupt, and starts finger data
2070 * acquisition.
2071 */
2072static int synaptics_rmi4_resume(struct device *dev)
2073{
2074 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
2075 const struct synaptics_rmi4_platform_data *platform_data =
2076 rmi4_data->board;
2077
2078 if (platform_data->regulator_en)
2079 regulator_enable(rmi4_data->regulator);
2080
2081 synaptics_rmi4_sensor_wake(rmi4_data);
2082 rmi4_data->touch_stopped = false;
2083 synaptics_rmi4_irq_enable(rmi4_data, true);
2084
2085 return 0;
2086}
2087
2088static const struct dev_pm_ops synaptics_rmi4_dev_pm_ops = {
2089 .suspend = synaptics_rmi4_suspend,
2090 .resume = synaptics_rmi4_resume,
2091};
2092#endif
2093
2094static const struct i2c_device_id synaptics_rmi4_id_table[] = {
2095 {DRIVER_NAME, 0},
2096 {},
2097};
2098MODULE_DEVICE_TABLE(i2c, synaptics_rmi4_id_table);
2099
2100static struct i2c_driver synaptics_rmi4_driver = {
2101 .driver = {
2102 .name = DRIVER_NAME,
2103 .owner = THIS_MODULE,
2104#ifdef CONFIG_PM
2105 .pm = &synaptics_rmi4_dev_pm_ops,
2106#endif
2107 },
2108 .probe = synaptics_rmi4_probe,
2109 .remove = __devexit_p(synaptics_rmi4_remove),
2110 .id_table = synaptics_rmi4_id_table,
2111};
2112
2113 /**
2114 * synaptics_rmi4_init()
2115 *
2116 * Called by the kernel during do_initcalls (if built-in)
2117 * or when the driver is loaded (if a module).
2118 *
2119 * This function registers the driver to the I2C subsystem.
2120 *
2121 */
2122static int __init synaptics_rmi4_init(void)
2123{
2124 return i2c_add_driver(&synaptics_rmi4_driver);
2125}
2126
2127 /**
2128 * synaptics_rmi4_exit()
2129 *
2130 * Called by the kernel when the driver is unloaded.
2131 *
2132 * This funtion unregisters the driver from the I2C subsystem.
2133 *
2134 */
2135static void __exit synaptics_rmi4_exit(void)
2136{
2137 i2c_del_driver(&synaptics_rmi4_driver);
2138}
2139
2140module_init(synaptics_rmi4_init);
2141module_exit(synaptics_rmi4_exit);
2142
2143MODULE_AUTHOR("Synaptics, Inc.");
2144MODULE_DESCRIPTION("Synaptics RMI4 I2C Touch Driver");
2145MODULE_LICENSE("GPL v2");