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