blob: 4e2b1a4c569862554c3b4a8c121952eeea24c56c [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>
Amy Malocheecfb4892013-02-06 13:30:36 -080031#include <linux/of_gpio.h>
Alexandra Chin669d27c2012-12-24 15:42:30 +080032#include "synaptics_i2c_rmi4.h"
Alexandra Chin669d27c2012-12-24 15:42:30 +080033#include <linux/input/mt.h>
Alexandra Chin669d27c2012-12-24 15:42:30 +080034
35#define DRIVER_NAME "synaptics_rmi4_i2c"
36#define INPUT_PHYS_NAME "synaptics_rmi4_i2c/input0"
Alexandra Chin3a747522013-04-15 12:13:38 -070037
38#define RESET_DELAY 100
39
Alexandra Chin669d27c2012-12-24 15:42:30 +080040#define TYPE_B_PROTOCOL
Alexandra Chin669d27c2012-12-24 15:42:30 +080041
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)
Alexandra Chinfb798fa2013-03-26 17:15:38 -070071#define NO_SLEEP_OFF (0 << 2)
72#define NO_SLEEP_ON (1 << 2)
Alexandra Chin669d27c2012-12-24 15:42:30 +080073
Alexandra Chin3a747522013-04-15 12:13:38 -070074enum device_status {
75 STATUS_NO_ERROR = 0x00,
76 STATUS_RESET_OCCURED = 0x01,
77 STATUS_INVALID_CONFIG = 0x02,
78 STATUS_DEVICE_FAILURE = 0x03,
79 STATUS_CONFIG_CRC_FAILURE = 0x04,
80 STATUS_FIRMWARE_CRC_FAILURE = 0x05,
81 STATUS_CRC_IN_PROGRESS = 0x06
82};
83
Amy Maloche1a53b612013-01-18 15:25:15 -080084#define RMI4_VTG_MIN_UV 2700000
85#define RMI4_VTG_MAX_UV 3300000
86#define RMI4_ACTIVE_LOAD_UA 15000
87#define RMI4_LPM_LOAD_UA 10
88
89#define RMI4_I2C_VTG_MIN_UV 1800000
90#define RMI4_I2C_VTG_MAX_UV 1800000
91#define RMI4_I2C_LOAD_UA 10000
92#define RMI4_I2C_LPM_LOAD_UA 10
93
Amy Maloche946da662013-01-18 16:27:11 -080094#define RMI4_GPIO_SLEEP_LOW_US 10000
Amy Maloche1a53b612013-01-18 15:25:15 -080095
Alexandra Chin669d27c2012-12-24 15:42:30 +080096static int synaptics_rmi4_i2c_read(struct synaptics_rmi4_data *rmi4_data,
97 unsigned short addr, unsigned char *data,
98 unsigned short length);
99
100static int synaptics_rmi4_i2c_write(struct synaptics_rmi4_data *rmi4_data,
101 unsigned short addr, unsigned char *data,
102 unsigned short length);
103
104static int synaptics_rmi4_reset_device(struct synaptics_rmi4_data *rmi4_data);
105
106#ifdef CONFIG_HAS_EARLYSUSPEND
107static ssize_t synaptics_rmi4_full_pm_cycle_show(struct device *dev,
108 struct device_attribute *attr, char *buf);
109
110static ssize_t synaptics_rmi4_full_pm_cycle_store(struct device *dev,
111 struct device_attribute *attr, const char *buf, size_t count);
112
113static void synaptics_rmi4_early_suspend(struct early_suspend *h);
114
115static void synaptics_rmi4_late_resume(struct early_suspend *h);
116
117static int synaptics_rmi4_suspend(struct device *dev);
118
119static int synaptics_rmi4_resume(struct device *dev);
120#endif
121
122static ssize_t synaptics_rmi4_f01_reset_store(struct device *dev,
123 struct device_attribute *attr, const char *buf, size_t count);
124
125static ssize_t synaptics_rmi4_f01_productinfo_show(struct device *dev,
126 struct device_attribute *attr, char *buf);
127
128static ssize_t synaptics_rmi4_f01_buildid_show(struct device *dev,
129 struct device_attribute *attr, char *buf);
130
131static ssize_t synaptics_rmi4_f01_flashprog_show(struct device *dev,
132 struct device_attribute *attr, char *buf);
133
134static ssize_t synaptics_rmi4_0dbutton_show(struct device *dev,
135 struct device_attribute *attr, char *buf);
136
137static ssize_t synaptics_rmi4_0dbutton_store(struct device *dev,
138 struct device_attribute *attr, const char *buf, size_t count);
139
Alexandra Chinfb798fa2013-03-26 17:15:38 -0700140static ssize_t synaptics_rmi4_flipx_show(struct device *dev,
141 struct device_attribute *attr, char *buf);
142
143static ssize_t synaptics_rmi4_flipx_store(struct device *dev,
144 struct device_attribute *attr, const char *buf, size_t count);
145
146static ssize_t synaptics_rmi4_flipy_show(struct device *dev,
147 struct device_attribute *attr, char *buf);
148
149static ssize_t synaptics_rmi4_flipy_store(struct device *dev,
150 struct device_attribute *attr, const char *buf, size_t count);
151
152
Alexandra Chin669d27c2012-12-24 15:42:30 +0800153struct synaptics_rmi4_f01_device_status {
154 union {
155 struct {
156 unsigned char status_code:4;
157 unsigned char reserved:2;
158 unsigned char flash_prog:1;
159 unsigned char unconfigured:1;
160 } __packed;
161 unsigned char data[1];
162 };
163};
164
165struct synaptics_rmi4_f1a_query {
166 union {
167 struct {
168 unsigned char max_button_count:3;
169 unsigned char reserved:5;
170 unsigned char has_general_control:1;
171 unsigned char has_interrupt_enable:1;
172 unsigned char has_multibutton_select:1;
173 unsigned char has_tx_rx_map:1;
174 unsigned char has_perbutton_threshold:1;
175 unsigned char has_release_threshold:1;
176 unsigned char has_strongestbtn_hysteresis:1;
177 unsigned char has_filter_strength:1;
178 } __packed;
179 unsigned char data[2];
180 };
181};
182
183struct synaptics_rmi4_f1a_control_0 {
184 union {
185 struct {
186 unsigned char multibutton_report:2;
187 unsigned char filter_mode:2;
188 unsigned char reserved:4;
189 } __packed;
190 unsigned char data[1];
191 };
192};
193
194struct synaptics_rmi4_f1a_control_3_4 {
195 unsigned char transmitterbutton;
196 unsigned char receiverbutton;
197};
198
199struct synaptics_rmi4_f1a_control {
200 struct synaptics_rmi4_f1a_control_0 general_control;
201 unsigned char *button_int_enable;
202 unsigned char *multi_button;
203 struct synaptics_rmi4_f1a_control_3_4 *electrode_map;
204 unsigned char *button_threshold;
205 unsigned char button_release_threshold;
206 unsigned char strongest_button_hysteresis;
207 unsigned char filter_strength;
208};
209
210struct synaptics_rmi4_f1a_handle {
211 int button_bitmask_size;
212 unsigned char button_count;
213 unsigned char valid_button_count;
214 unsigned char *button_data_buffer;
215 unsigned char *button_map;
216 struct synaptics_rmi4_f1a_query button_query;
217 struct synaptics_rmi4_f1a_control button_control;
218};
219
220struct synaptics_rmi4_exp_fn {
221 enum exp_fn fn_type;
222 bool inserted;
223 int (*func_init)(struct synaptics_rmi4_data *rmi4_data);
224 void (*func_remove)(struct synaptics_rmi4_data *rmi4_data);
225 void (*func_attn)(struct synaptics_rmi4_data *rmi4_data,
226 unsigned char intr_mask);
227 struct list_head link;
228};
229
230static struct device_attribute attrs[] = {
231#ifdef CONFIG_HAS_EARLYSUSPEND
232 __ATTR(full_pm_cycle, (S_IRUGO | S_IWUGO),
233 synaptics_rmi4_full_pm_cycle_show,
234 synaptics_rmi4_full_pm_cycle_store),
235#endif
236 __ATTR(reset, S_IWUGO,
237 synaptics_rmi4_show_error,
238 synaptics_rmi4_f01_reset_store),
239 __ATTR(productinfo, S_IRUGO,
240 synaptics_rmi4_f01_productinfo_show,
241 synaptics_rmi4_store_error),
242 __ATTR(buildid, S_IRUGO,
243 synaptics_rmi4_f01_buildid_show,
244 synaptics_rmi4_store_error),
245 __ATTR(flashprog, S_IRUGO,
246 synaptics_rmi4_f01_flashprog_show,
247 synaptics_rmi4_store_error),
248 __ATTR(0dbutton, (S_IRUGO | S_IWUGO),
249 synaptics_rmi4_0dbutton_show,
250 synaptics_rmi4_0dbutton_store),
Alexandra Chinfb798fa2013-03-26 17:15:38 -0700251 __ATTR(flipx, (S_IRUGO | S_IWUGO),
252 synaptics_rmi4_flipx_show,
253 synaptics_rmi4_flipx_store),
254 __ATTR(flipy, (S_IRUGO | S_IWUGO),
255 synaptics_rmi4_flipy_show,
256 synaptics_rmi4_flipy_store),
Alexandra Chin669d27c2012-12-24 15:42:30 +0800257};
258
259static bool exp_fn_inited;
260static struct mutex exp_fn_list_mutex;
261static struct list_head exp_fn_list;
262
263#ifdef CONFIG_HAS_EARLYSUSPEND
264static ssize_t synaptics_rmi4_full_pm_cycle_show(struct device *dev,
265 struct device_attribute *attr, char *buf)
266{
267 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
268
269 return snprintf(buf, PAGE_SIZE, "%u\n",
270 rmi4_data->full_pm_cycle);
271}
272
273static ssize_t synaptics_rmi4_full_pm_cycle_store(struct device *dev,
274 struct device_attribute *attr, const char *buf, size_t count)
275{
276 unsigned int input;
277 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
278
279 if (sscanf(buf, "%u", &input) != 1)
280 return -EINVAL;
281
282 rmi4_data->full_pm_cycle = input > 0 ? 1 : 0;
283
284 return count;
285}
286#endif
287
288static ssize_t synaptics_rmi4_f01_reset_store(struct device *dev,
289 struct device_attribute *attr, const char *buf, size_t count)
290{
291 int retval;
292 unsigned int reset;
293 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
294
295 if (sscanf(buf, "%u", &reset) != 1)
296 return -EINVAL;
297
298 if (reset != 1)
299 return -EINVAL;
300
301 retval = synaptics_rmi4_reset_device(rmi4_data);
302 if (retval < 0) {
303 dev_err(dev,
304 "%s: Failed to issue reset command, error = %d\n",
305 __func__, retval);
306 return retval;
307 }
308
309 return count;
310}
311
312static ssize_t synaptics_rmi4_f01_productinfo_show(struct device *dev,
313 struct device_attribute *attr, char *buf)
314{
315 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
316
317 return snprintf(buf, PAGE_SIZE, "0x%02x 0x%02x\n",
318 (rmi4_data->rmi4_mod_info.product_info[0]),
319 (rmi4_data->rmi4_mod_info.product_info[1]));
320}
321
322static ssize_t synaptics_rmi4_f01_buildid_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
324{
325 unsigned int build_id;
326 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
327 struct synaptics_rmi4_device_info *rmi;
328
329 rmi = &(rmi4_data->rmi4_mod_info);
330
331 build_id = (unsigned int)rmi->build_id[0] +
332 (unsigned int)rmi->build_id[1] * 0x100 +
333 (unsigned int)rmi->build_id[2] * 0x10000;
334
335 return snprintf(buf, PAGE_SIZE, "%u\n",
336 build_id);
337}
338
339static ssize_t synaptics_rmi4_f01_flashprog_show(struct device *dev,
340 struct device_attribute *attr, char *buf)
341{
342 int retval;
343 struct synaptics_rmi4_f01_device_status device_status;
344 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
345
346 retval = synaptics_rmi4_i2c_read(rmi4_data,
347 rmi4_data->f01_data_base_addr,
348 device_status.data,
349 sizeof(device_status.data));
350 if (retval < 0) {
351 dev_err(dev,
352 "%s: Failed to read device status, error = %d\n",
353 __func__, retval);
354 return retval;
355 }
356
357 return snprintf(buf, PAGE_SIZE, "%u\n",
358 device_status.flash_prog);
359}
360
361static ssize_t synaptics_rmi4_0dbutton_show(struct device *dev,
362 struct device_attribute *attr, char *buf)
363{
364 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
365
366 return snprintf(buf, PAGE_SIZE, "%u\n",
367 rmi4_data->button_0d_enabled);
368}
369
370static ssize_t synaptics_rmi4_0dbutton_store(struct device *dev,
371 struct device_attribute *attr, const char *buf, size_t count)
372{
373 int retval;
374 unsigned int input;
375 unsigned char ii;
376 unsigned char intr_enable;
377 struct synaptics_rmi4_fn *fhandler;
378 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
379 struct synaptics_rmi4_device_info *rmi;
380
381 rmi = &(rmi4_data->rmi4_mod_info);
382
383 if (sscanf(buf, "%u", &input) != 1)
384 return -EINVAL;
385
386 input = input > 0 ? 1 : 0;
387
388 if (rmi4_data->button_0d_enabled == input)
389 return count;
390
Alexandra Chind5591a62013-02-07 12:59:15 -0800391 if (!list_empty(&rmi->support_fn_list)) {
392 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
393 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A) {
394 ii = fhandler->intr_reg_num;
Alexandra Chin669d27c2012-12-24 15:42:30 +0800395
Alexandra Chind5591a62013-02-07 12:59:15 -0800396 retval = synaptics_rmi4_i2c_read(rmi4_data,
397 rmi4_data->f01_ctrl_base_addr +
398 1 + ii,
399 &intr_enable,
400 sizeof(intr_enable));
401 if (retval < 0)
402 return retval;
Alexandra Chin669d27c2012-12-24 15:42:30 +0800403
Alexandra Chind5591a62013-02-07 12:59:15 -0800404 if (input == 1)
405 intr_enable |= fhandler->intr_mask;
406 else
407 intr_enable &= ~fhandler->intr_mask;
Alexandra Chin669d27c2012-12-24 15:42:30 +0800408
Alexandra Chind5591a62013-02-07 12:59:15 -0800409 retval = synaptics_rmi4_i2c_write(rmi4_data,
410 rmi4_data->f01_ctrl_base_addr +
411 1 + ii,
412 &intr_enable,
413 sizeof(intr_enable));
414 if (retval < 0)
415 return retval;
416 }
Alexandra Chin669d27c2012-12-24 15:42:30 +0800417 }
418 }
419
420 rmi4_data->button_0d_enabled = input;
421
422 return count;
423}
424
Alexandra Chinfb798fa2013-03-26 17:15:38 -0700425static ssize_t synaptics_rmi4_flipx_show(struct device *dev,
426 struct device_attribute *attr, char *buf)
427{
428 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
429
430 return snprintf(buf, PAGE_SIZE, "%u\n",
431 rmi4_data->flip_x);
432}
433
434static ssize_t synaptics_rmi4_flipx_store(struct device *dev,
435 struct device_attribute *attr, const char *buf, size_t count)
436{
437 unsigned int input;
438 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
439
440 if (sscanf(buf, "%u", &input) != 1)
441 return -EINVAL;
442
443 rmi4_data->flip_x = input > 0 ? 1 : 0;
444
445 return count;
446}
447
448static ssize_t synaptics_rmi4_flipy_show(struct device *dev,
449 struct device_attribute *attr, char *buf)
450{
451 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
452
453 return snprintf(buf, PAGE_SIZE, "%u\n",
454 rmi4_data->flip_y);
455}
456
457static ssize_t synaptics_rmi4_flipy_store(struct device *dev,
458 struct device_attribute *attr, const char *buf, size_t count)
459{
460 unsigned int input;
461 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
462
463 if (sscanf(buf, "%u", &input) != 1)
464 return -EINVAL;
465
466 rmi4_data->flip_y = input > 0 ? 1 : 0;
467
468 return count;
469}
470
Alexandra Chin669d27c2012-12-24 15:42:30 +0800471 /**
472 * synaptics_rmi4_set_page()
473 *
474 * Called by synaptics_rmi4_i2c_read() and synaptics_rmi4_i2c_write().
475 *
476 * This function writes to the page select register to switch to the
477 * assigned page.
478 */
479static int synaptics_rmi4_set_page(struct synaptics_rmi4_data *rmi4_data,
480 unsigned int address)
481{
482 int retval = 0;
483 unsigned char retry;
484 unsigned char buf[PAGE_SELECT_LEN];
485 unsigned char page;
486 struct i2c_client *i2c = rmi4_data->i2c_client;
487
488 page = ((address >> 8) & MASK_8BIT);
489 if (page != rmi4_data->current_page) {
490 buf[0] = MASK_8BIT;
491 buf[1] = page;
492 for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
493 retval = i2c_master_send(i2c, buf, PAGE_SELECT_LEN);
494 if (retval != PAGE_SELECT_LEN) {
495 dev_err(&i2c->dev,
496 "%s: I2C retry %d\n",
497 __func__, retry + 1);
498 msleep(20);
499 } else {
500 rmi4_data->current_page = page;
501 break;
502 }
503 }
504 } else
505 return PAGE_SELECT_LEN;
506 return (retval == PAGE_SELECT_LEN) ? retval : -EIO;
507}
508
509 /**
510 * synaptics_rmi4_i2c_read()
511 *
512 * Called by various functions in this driver, and also exported to
513 * other expansion Function modules such as rmi_dev.
514 *
515 * This function reads data of an arbitrary length from the sensor,
516 * starting from an assigned register address of the sensor, via I2C
517 * with a retry mechanism.
518 */
519static int synaptics_rmi4_i2c_read(struct synaptics_rmi4_data *rmi4_data,
520 unsigned short addr, unsigned char *data, unsigned short length)
521{
522 int retval;
523 unsigned char retry;
524 unsigned char buf;
525 struct i2c_msg msg[] = {
526 {
527 .addr = rmi4_data->i2c_client->addr,
528 .flags = 0,
529 .len = 1,
530 .buf = &buf,
531 },
532 {
533 .addr = rmi4_data->i2c_client->addr,
534 .flags = I2C_M_RD,
535 .len = length,
536 .buf = data,
537 },
538 };
539
540 buf = addr & MASK_8BIT;
541
542 mutex_lock(&(rmi4_data->rmi4_io_ctrl_mutex));
543
544 retval = synaptics_rmi4_set_page(rmi4_data, addr);
545 if (retval != PAGE_SELECT_LEN)
546 goto exit;
547
548 for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
549 if (i2c_transfer(rmi4_data->i2c_client->adapter, msg, 2) == 2) {
550 retval = length;
551 break;
552 }
553 dev_err(&rmi4_data->i2c_client->dev,
554 "%s: I2C retry %d\n",
555 __func__, retry + 1);
556 msleep(20);
557 }
558
559 if (retry == SYN_I2C_RETRY_TIMES) {
560 dev_err(&rmi4_data->i2c_client->dev,
561 "%s: I2C read over retry limit\n",
562 __func__);
563 retval = -EIO;
564 }
565
566exit:
567 mutex_unlock(&(rmi4_data->rmi4_io_ctrl_mutex));
568
569 return retval;
570}
571
572 /**
573 * synaptics_rmi4_i2c_write()
574 *
575 * Called by various functions in this driver, and also exported to
576 * other expansion Function modules such as rmi_dev.
577 *
578 * This function writes data of an arbitrary length to the sensor,
579 * starting from an assigned register address of the sensor, via I2C with
580 * a retry mechanism.
581 */
582static int synaptics_rmi4_i2c_write(struct synaptics_rmi4_data *rmi4_data,
583 unsigned short addr, unsigned char *data, unsigned short length)
584{
585 int retval;
586 unsigned char retry;
587 unsigned char buf[length + 1];
588 struct i2c_msg msg[] = {
589 {
590 .addr = rmi4_data->i2c_client->addr,
591 .flags = 0,
592 .len = length + 1,
593 .buf = buf,
594 }
595 };
596
597 mutex_lock(&(rmi4_data->rmi4_io_ctrl_mutex));
598
599 retval = synaptics_rmi4_set_page(rmi4_data, addr);
600 if (retval != PAGE_SELECT_LEN)
601 goto exit;
602
603 buf[0] = addr & MASK_8BIT;
604 memcpy(&buf[1], &data[0], length);
605
606 for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
607 if (i2c_transfer(rmi4_data->i2c_client->adapter, msg, 1) == 1) {
608 retval = length;
609 break;
610 }
611 dev_err(&rmi4_data->i2c_client->dev,
612 "%s: I2C retry %d\n",
613 __func__, retry + 1);
614 msleep(20);
615 }
616
617 if (retry == SYN_I2C_RETRY_TIMES) {
618 dev_err(&rmi4_data->i2c_client->dev,
619 "%s: I2C write over retry limit\n",
620 __func__);
621 retval = -EIO;
622 }
623
624exit:
625 mutex_unlock(&(rmi4_data->rmi4_io_ctrl_mutex));
626
627 return retval;
628}
629
630 /**
631 * synaptics_rmi4_f11_abs_report()
632 *
633 * Called by synaptics_rmi4_report_touch() when valid Function $11
634 * finger data has been detected.
635 *
636 * This function reads the Function $11 data registers, determines the
637 * status of each finger supported by the Function, processes any
638 * necessary coordinate manipulation, reports the finger data to
639 * the input subsystem, and returns the number of fingers detected.
640 */
641static int synaptics_rmi4_f11_abs_report(struct synaptics_rmi4_data *rmi4_data,
642 struct synaptics_rmi4_fn *fhandler)
643{
644 int retval;
645 unsigned char touch_count = 0; /* number of touch points */
646 unsigned char reg_index;
647 unsigned char finger;
648 unsigned char fingers_supported;
649 unsigned char num_of_finger_status_regs;
650 unsigned char finger_shift;
651 unsigned char finger_status;
652 unsigned char data_reg_blk_size;
653 unsigned char finger_status_reg[3];
654 unsigned char data[F11_STD_DATA_LEN];
655 unsigned short data_addr;
656 unsigned short data_offset;
657 int x;
658 int y;
659 int wx;
660 int wy;
Alexandra Chinfb798fa2013-03-26 17:15:38 -0700661 int z;
Alexandra Chin669d27c2012-12-24 15:42:30 +0800662
663 /*
664 * The number of finger status registers is determined by the
665 * maximum number of fingers supported - 2 bits per finger. So
666 * the number of finger status registers to read is:
667 * register_count = ceil(max_num_of_fingers / 4)
668 */
669 fingers_supported = fhandler->num_of_data_points;
670 num_of_finger_status_regs = (fingers_supported + 3) / 4;
671 data_addr = fhandler->full_addr.data_base;
672 data_reg_blk_size = fhandler->size_of_data_register_block;
673
674 retval = synaptics_rmi4_i2c_read(rmi4_data,
675 data_addr,
676 finger_status_reg,
677 num_of_finger_status_regs);
678 if (retval < 0)
679 return 0;
680
681 for (finger = 0; finger < fingers_supported; finger++) {
682 reg_index = finger / 4;
683 finger_shift = (finger % 4) * 2;
684 finger_status = (finger_status_reg[reg_index] >> finger_shift)
685 & MASK_2BIT;
686
687 /*
688 * Each 2-bit finger status field represents the following:
689 * 00 = finger not present
690 * 01 = finger present and data accurate
691 * 10 = finger present but data may be inaccurate
692 * 11 = reserved
693 */
694#ifdef TYPE_B_PROTOCOL
695 input_mt_slot(rmi4_data->input_dev, finger);
696 input_mt_report_slot_state(rmi4_data->input_dev,
697 MT_TOOL_FINGER, finger_status != 0);
698#endif
699
700 if (finger_status) {
701 data_offset = data_addr +
702 num_of_finger_status_regs +
703 (finger * data_reg_blk_size);
704 retval = synaptics_rmi4_i2c_read(rmi4_data,
705 data_offset,
706 data,
707 data_reg_blk_size);
708 if (retval < 0)
709 return 0;
710
711 x = (data[0] << 4) | (data[2] & MASK_4BIT);
712 y = (data[1] << 4) | ((data[2] >> 4) & MASK_4BIT);
713 wx = (data[3] & MASK_4BIT);
714 wy = (data[3] >> 4) & MASK_4BIT;
Alexandra Chinfb798fa2013-03-26 17:15:38 -0700715 z = data[4];
Alexandra Chin669d27c2012-12-24 15:42:30 +0800716
Alexandra Chinfb798fa2013-03-26 17:15:38 -0700717 if (rmi4_data->flip_x)
Alexandra Chin669d27c2012-12-24 15:42:30 +0800718 x = rmi4_data->sensor_max_x - x;
Alexandra Chinfb798fa2013-03-26 17:15:38 -0700719 if (rmi4_data->flip_y)
Alexandra Chin669d27c2012-12-24 15:42:30 +0800720 y = rmi4_data->sensor_max_y - y;
721
722 dev_dbg(&rmi4_data->i2c_client->dev,
723 "%s: Finger %d:\n"
724 "status = 0x%02x\n"
725 "x = %d\n"
726 "y = %d\n"
727 "wx = %d\n"
728 "wy = %d\n",
729 __func__, finger,
730 finger_status,
731 x, y, wx, wy);
732
Alexandra Chin669d27c2012-12-24 15:42:30 +0800733 input_report_abs(rmi4_data->input_dev,
734 ABS_MT_POSITION_X, x);
735 input_report_abs(rmi4_data->input_dev,
736 ABS_MT_POSITION_Y, y);
Alexandra Chinfb798fa2013-03-26 17:15:38 -0700737 input_report_abs(rmi4_data->input_dev,
738 ABS_MT_PRESSURE, z);
Alexandra Chind5591a62013-02-07 12:59:15 -0800739
Alexandra Chin669d27c2012-12-24 15:42:30 +0800740#ifdef REPORT_2D_W
741 input_report_abs(rmi4_data->input_dev,
742 ABS_MT_TOUCH_MAJOR, max(wx, wy));
743 input_report_abs(rmi4_data->input_dev,
744 ABS_MT_TOUCH_MINOR, min(wx, wy));
745#endif
Alexandra Chind5591a62013-02-07 12:59:15 -0800746#ifndef TYPE_B_PROTOCOL
Alexandra Chin669d27c2012-12-24 15:42:30 +0800747 input_mt_sync(rmi4_data->input_dev);
748#endif
749 touch_count++;
750 }
751 }
752
Amy Malocheecfb4892013-02-06 13:30:36 -0800753 input_report_key(rmi4_data->input_dev, BTN_TOUCH, touch_count > 0);
754 input_report_key(rmi4_data->input_dev,
755 BTN_TOOL_FINGER, touch_count > 0);
756
Alexandra Chin669d27c2012-12-24 15:42:30 +0800757#ifndef TYPE_B_PROTOCOL
758 if (!touch_count)
759 input_mt_sync(rmi4_data->input_dev);
760#else
761 /* sync after groups of events */
762 #ifdef KERNEL_ABOVE_3_7
763 input_mt_sync_frame(rmi4_data->input_dev);
764 #endif
765#endif
766
767 input_sync(rmi4_data->input_dev);
768
769 return touch_count;
770}
771
772static void synaptics_rmi4_f1a_report(struct synaptics_rmi4_data *rmi4_data,
773 struct synaptics_rmi4_fn *fhandler)
774{
775 int retval;
776 unsigned char button;
777 unsigned char index;
778 unsigned char shift;
779 unsigned char status;
780 unsigned char *data;
781 unsigned short data_addr = fhandler->full_addr.data_base;
782 struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
783 static unsigned char do_once = 1;
784 static bool current_status[MAX_NUMBER_OF_BUTTONS];
785#ifdef NO_0D_WHILE_2D
786 static bool before_2d_status[MAX_NUMBER_OF_BUTTONS];
787 static bool while_2d_status[MAX_NUMBER_OF_BUTTONS];
788#endif
789
790 if (do_once) {
791 memset(current_status, 0, sizeof(current_status));
792#ifdef NO_0D_WHILE_2D
793 memset(before_2d_status, 0, sizeof(before_2d_status));
794 memset(while_2d_status, 0, sizeof(while_2d_status));
795#endif
796 do_once = 0;
797 }
798
799 retval = synaptics_rmi4_i2c_read(rmi4_data,
800 data_addr,
801 f1a->button_data_buffer,
802 f1a->button_bitmask_size);
803 if (retval < 0) {
804 dev_err(&rmi4_data->i2c_client->dev,
805 "%s: Failed to read button data registers\n",
806 __func__);
807 return;
808 }
809
810 data = f1a->button_data_buffer;
811
812 for (button = 0; button < f1a->valid_button_count; button++) {
813 index = button / 8;
814 shift = button % 8;
815 status = ((data[index] >> shift) & MASK_1BIT);
816
817 if (current_status[button] == status)
818 continue;
819 else
820 current_status[button] = status;
821
822 dev_dbg(&rmi4_data->i2c_client->dev,
823 "%s: Button %d (code %d) ->%d\n",
824 __func__, button,
825 f1a->button_map[button],
826 status);
827#ifdef NO_0D_WHILE_2D
828 if (rmi4_data->fingers_on_2d == false) {
829 if (status == 1) {
830 before_2d_status[button] = 1;
831 } else {
832 if (while_2d_status[button] == 1) {
833 while_2d_status[button] = 0;
834 continue;
835 } else {
836 before_2d_status[button] = 0;
837 }
838 }
839 input_report_key(rmi4_data->input_dev,
840 f1a->button_map[button],
841 status);
842 } else {
843 if (before_2d_status[button] == 1) {
844 before_2d_status[button] = 0;
845 input_report_key(rmi4_data->input_dev,
846 f1a->button_map[button],
847 status);
848 } else {
849 if (status == 1)
850 while_2d_status[button] = 1;
851 else
852 while_2d_status[button] = 0;
853 }
854 }
855#else
856 input_report_key(rmi4_data->input_dev,
857 f1a->button_map[button],
858 status);
859#endif
860 }
861
862 input_sync(rmi4_data->input_dev);
863
864 return;
865}
866
867 /**
868 * synaptics_rmi4_report_touch()
869 *
870 * Called by synaptics_rmi4_sensor_report().
871 *
872 * This function calls the appropriate finger data reporting function
873 * based on the function handler it receives and returns the number of
874 * fingers detected.
875 */
876static void synaptics_rmi4_report_touch(struct synaptics_rmi4_data *rmi4_data,
877 struct synaptics_rmi4_fn *fhandler,
878 unsigned char *touch_count)
879{
880 unsigned char touch_count_2d;
881
882 dev_dbg(&rmi4_data->i2c_client->dev,
883 "%s: Function %02x reporting\n",
884 __func__, fhandler->fn_number);
885
886 switch (fhandler->fn_number) {
887 case SYNAPTICS_RMI4_F11:
888 touch_count_2d = synaptics_rmi4_f11_abs_report(rmi4_data,
889 fhandler);
890
891 *touch_count += touch_count_2d;
892
893 if (touch_count_2d)
894 rmi4_data->fingers_on_2d = true;
895 else
896 rmi4_data->fingers_on_2d = false;
897 break;
898
899 case SYNAPTICS_RMI4_F1A:
900 synaptics_rmi4_f1a_report(rmi4_data, fhandler);
901 break;
902
903 default:
904 break;
905 }
906
907 return;
908}
909
910 /**
911 * synaptics_rmi4_sensor_report()
912 *
913 * Called by synaptics_rmi4_irq().
914 *
915 * This function determines the interrupt source(s) from the sensor
916 * and calls synaptics_rmi4_report_touch() with the appropriate
917 * function handler for each function with valid data inputs.
918 */
919static int synaptics_rmi4_sensor_report(struct synaptics_rmi4_data *rmi4_data)
920{
921 int retval;
922 unsigned char touch_count = 0;
923 unsigned char intr[MAX_INTR_REGISTERS];
924 struct synaptics_rmi4_fn *fhandler;
925 struct synaptics_rmi4_exp_fn *exp_fhandler;
926 struct synaptics_rmi4_device_info *rmi;
927
928 rmi = &(rmi4_data->rmi4_mod_info);
929
930 /*
931 * Get interrupt status information from F01 Data1 register to
932 * determine the source(s) that are flagging the interrupt.
933 */
934 retval = synaptics_rmi4_i2c_read(rmi4_data,
935 rmi4_data->f01_data_base_addr + 1,
936 intr,
937 rmi4_data->num_of_intr_regs);
938 if (retval < 0)
939 return retval;
940
941 /*
942 * Traverse the function handler list and service the source(s)
943 * of the interrupt accordingly.
944 */
Alexandra Chind5591a62013-02-07 12:59:15 -0800945 if (!list_empty(&rmi->support_fn_list)) {
946 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
947 if (fhandler->num_of_data_sources) {
948 if (fhandler->intr_mask &
949 intr[fhandler->intr_reg_num]) {
950 synaptics_rmi4_report_touch(rmi4_data,
951 fhandler, &touch_count);
952 }
Alexandra Chin669d27c2012-12-24 15:42:30 +0800953 }
954 }
955 }
956
957 mutex_lock(&exp_fn_list_mutex);
958 if (!list_empty(&exp_fn_list)) {
959 list_for_each_entry(exp_fhandler, &exp_fn_list, link) {
960 if (exp_fhandler->inserted &&
961 (exp_fhandler->func_attn != NULL))
962 exp_fhandler->func_attn(rmi4_data, intr[0]);
963 }
964 }
965 mutex_unlock(&exp_fn_list_mutex);
966
967 return touch_count;
968}
969
970 /**
971 * synaptics_rmi4_irq()
972 *
973 * Called by the kernel when an interrupt occurs (when the sensor
974 * asserts the attention irq).
975 *
976 * This function is the ISR thread and handles the acquisition
977 * and the reporting of finger data when the presence of fingers
978 * is detected.
979 */
980static irqreturn_t synaptics_rmi4_irq(int irq, void *data)
981{
982 struct synaptics_rmi4_data *rmi4_data = data;
983
984 synaptics_rmi4_sensor_report(rmi4_data);
985
986 return IRQ_HANDLED;
987}
988
Amy Malocheecfb4892013-02-06 13:30:36 -0800989static int synaptics_rmi4_parse_dt(struct device *dev,
990 struct synaptics_rmi4_platform_data *rmi4_pdata)
991{
992 struct device_node *np = dev->of_node;
993 struct property *prop;
994 u32 temp_val, num_buttons;
995 u32 button_map[MAX_NUMBER_OF_BUTTONS];
996 int rc, i;
997
998 rmi4_pdata->i2c_pull_up = of_property_read_bool(np,
999 "synaptics,i2c-pull-up");
1000 rmi4_pdata->regulator_en = of_property_read_bool(np,
1001 "synaptics,reg-en");
1002 rmi4_pdata->x_flip = of_property_read_bool(np, "synaptics,x-flip");
1003 rmi4_pdata->y_flip = of_property_read_bool(np, "synaptics,y-flip");
1004
1005 rc = of_property_read_u32(np, "synaptics,panel-x", &temp_val);
1006 if (rc && (rc != -EINVAL)) {
1007 dev_err(dev, "Unable to read panel X dimension\n");
1008 return rc;
1009 } else {
1010 rmi4_pdata->panel_x = temp_val;
1011 }
1012
1013 rc = of_property_read_u32(np, "synaptics,panel-y", &temp_val);
1014 if (rc && (rc != -EINVAL)) {
1015 dev_err(dev, "Unable to read panel Y dimension\n");
1016 return rc;
1017 } else {
1018 rmi4_pdata->panel_y = temp_val;
1019 }
1020
Amy Malochecb835832013-03-26 18:06:05 -07001021 rc = of_property_read_string(np, "synaptics,fw-image-name",
1022 &rmi4_pdata->fw_image_name);
1023 if (rc && (rc != -EINVAL)) {
1024 dev_err(dev, "Unable to read fw image name\n");
1025 return rc;
1026 }
1027
Amy Malocheecfb4892013-02-06 13:30:36 -08001028 /* reset, irq gpio info */
1029 rmi4_pdata->reset_gpio = of_get_named_gpio_flags(np,
1030 "synaptics,reset-gpio", 0, &rmi4_pdata->reset_flags);
1031 rmi4_pdata->irq_gpio = of_get_named_gpio_flags(np,
1032 "synaptics,irq-gpio", 0, &rmi4_pdata->irq_flags);
1033
1034 prop = of_find_property(np, "synaptics,button-map", NULL);
1035 if (prop) {
1036 num_buttons = prop->length / sizeof(temp_val);
1037
1038 rmi4_pdata->capacitance_button_map = devm_kzalloc(dev,
1039 sizeof(*rmi4_pdata->capacitance_button_map),
1040 GFP_KERNEL);
1041 if (!rmi4_pdata->capacitance_button_map)
1042 return -ENOMEM;
1043
1044 rmi4_pdata->capacitance_button_map->map = devm_kzalloc(dev,
1045 sizeof(*rmi4_pdata->capacitance_button_map->map) *
1046 MAX_NUMBER_OF_BUTTONS, GFP_KERNEL);
1047 if (!rmi4_pdata->capacitance_button_map->map)
1048 return -ENOMEM;
1049
1050 if (num_buttons <= MAX_NUMBER_OF_BUTTONS) {
1051 rc = of_property_read_u32_array(np,
1052 "synaptics,button-map", button_map,
1053 num_buttons);
1054 if (rc) {
1055 dev_err(dev, "Unable to read key codes\n");
1056 return rc;
1057 }
1058 for (i = 0; i < num_buttons; i++)
1059 rmi4_pdata->capacitance_button_map->map[i] =
1060 button_map[i];
1061 rmi4_pdata->capacitance_button_map->nbuttons =
1062 num_buttons;
1063 } else {
1064 return -EINVAL;
1065 }
1066 }
1067 return 0;
1068}
1069
Alexandra Chin669d27c2012-12-24 15:42:30 +08001070 /**
1071 * synaptics_rmi4_irq_enable()
1072 *
1073 * Called by synaptics_rmi4_probe() and the power management functions
1074 * in this driver and also exported to other expansion Function modules
1075 * such as rmi_dev.
1076 *
1077 * This function handles the enabling and disabling of the attention
1078 * irq including the setting up of the ISR thread.
1079 */
1080static int synaptics_rmi4_irq_enable(struct synaptics_rmi4_data *rmi4_data,
1081 bool enable)
1082{
1083 int retval = 0;
1084 unsigned char intr_status;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001085
1086 if (enable) {
1087 if (rmi4_data->irq_enabled)
1088 return retval;
1089
1090 /* Clear interrupts first */
1091 retval = synaptics_rmi4_i2c_read(rmi4_data,
1092 rmi4_data->f01_data_base_addr + 1,
1093 &intr_status,
1094 rmi4_data->num_of_intr_regs);
1095 if (retval < 0)
1096 return retval;
1097
Alexandra Chinfb798fa2013-03-26 17:15:38 -07001098 enable_irq(rmi4_data->irq);
Alexandra Chin669d27c2012-12-24 15:42:30 +08001099
1100 rmi4_data->irq_enabled = true;
1101 } else {
1102 if (rmi4_data->irq_enabled) {
1103 disable_irq(rmi4_data->irq);
Alexandra Chin669d27c2012-12-24 15:42:30 +08001104 rmi4_data->irq_enabled = false;
1105 }
1106 }
1107
1108 return retval;
1109}
1110
1111 /**
1112 * synaptics_rmi4_f11_init()
1113 *
1114 * Called by synaptics_rmi4_query_device().
1115 *
1116 * This funtion parses information from the Function 11 registers
1117 * and determines the number of fingers supported, x and y data ranges,
1118 * offset to the associated interrupt status register, interrupt bit
1119 * mask, and gathers finger data acquisition capabilities from the query
1120 * registers.
1121 */
1122static int synaptics_rmi4_f11_init(struct synaptics_rmi4_data *rmi4_data,
1123 struct synaptics_rmi4_fn *fhandler,
1124 struct synaptics_rmi4_fn_desc *fd,
1125 unsigned int intr_count)
1126{
1127 int retval;
1128 unsigned char ii;
1129 unsigned char intr_offset;
1130 unsigned char abs_data_size;
1131 unsigned char abs_data_blk_size;
1132 unsigned char query[F11_STD_QUERY_LEN];
1133 unsigned char control[F11_STD_CTRL_LEN];
1134
1135 fhandler->fn_number = fd->fn_number;
1136 fhandler->num_of_data_sources = fd->intr_src_count;
1137
1138 retval = synaptics_rmi4_i2c_read(rmi4_data,
1139 fhandler->full_addr.query_base,
1140 query,
1141 sizeof(query));
1142 if (retval < 0)
1143 return retval;
1144
1145 /* Maximum number of fingers supported */
1146 if ((query[1] & MASK_3BIT) <= 4)
1147 fhandler->num_of_data_points = (query[1] & MASK_3BIT) + 1;
1148 else if ((query[1] & MASK_3BIT) == 5)
1149 fhandler->num_of_data_points = 10;
1150
1151 rmi4_data->num_of_fingers = fhandler->num_of_data_points;
1152
1153 retval = synaptics_rmi4_i2c_read(rmi4_data,
1154 fhandler->full_addr.ctrl_base,
1155 control,
1156 sizeof(control));
1157 if (retval < 0)
1158 return retval;
1159
1160 /* Maximum x and y */
1161 rmi4_data->sensor_max_x = ((control[6] & MASK_8BIT) << 0) |
1162 ((control[7] & MASK_4BIT) << 8);
1163 rmi4_data->sensor_max_y = ((control[8] & MASK_8BIT) << 0) |
1164 ((control[9] & MASK_4BIT) << 8);
1165 dev_dbg(&rmi4_data->i2c_client->dev,
1166 "%s: Function %02x max x = %d max y = %d\n",
1167 __func__, fhandler->fn_number,
1168 rmi4_data->sensor_max_x,
1169 rmi4_data->sensor_max_y);
1170
1171 fhandler->intr_reg_num = (intr_count + 7) / 8;
1172 if (fhandler->intr_reg_num != 0)
1173 fhandler->intr_reg_num -= 1;
1174
1175 /* Set an enable bit for each data source */
1176 intr_offset = intr_count % 8;
1177 fhandler->intr_mask = 0;
1178 for (ii = intr_offset;
1179 ii < ((fd->intr_src_count & MASK_3BIT) +
1180 intr_offset);
1181 ii++)
1182 fhandler->intr_mask |= 1 << ii;
1183
1184 abs_data_size = query[5] & MASK_2BIT;
1185 abs_data_blk_size = 3 + (2 * (abs_data_size == 0 ? 1 : 0));
1186 fhandler->size_of_data_register_block = abs_data_blk_size;
1187
1188 return retval;
1189}
1190
1191static int synaptics_rmi4_f1a_alloc_mem(struct synaptics_rmi4_data *rmi4_data,
1192 struct synaptics_rmi4_fn *fhandler)
1193{
1194 int retval;
1195 struct synaptics_rmi4_f1a_handle *f1a;
1196
1197 f1a = kzalloc(sizeof(*f1a), GFP_KERNEL);
1198 if (!f1a) {
1199 dev_err(&rmi4_data->i2c_client->dev,
1200 "%s: Failed to alloc mem for function handle\n",
1201 __func__);
1202 return -ENOMEM;
1203 }
1204
1205 fhandler->data = (void *)f1a;
1206
1207 retval = synaptics_rmi4_i2c_read(rmi4_data,
1208 fhandler->full_addr.query_base,
1209 f1a->button_query.data,
1210 sizeof(f1a->button_query.data));
1211 if (retval < 0) {
1212 dev_err(&rmi4_data->i2c_client->dev,
1213 "%s: Failed to read query registers\n",
1214 __func__);
1215 return retval;
1216 }
1217
1218 f1a->button_count = f1a->button_query.max_button_count + 1;
1219 f1a->button_bitmask_size = (f1a->button_count + 7) / 8;
1220
1221 f1a->button_data_buffer = kcalloc(f1a->button_bitmask_size,
1222 sizeof(*(f1a->button_data_buffer)), GFP_KERNEL);
1223 if (!f1a->button_data_buffer) {
1224 dev_err(&rmi4_data->i2c_client->dev,
1225 "%s: Failed to alloc mem for data buffer\n",
1226 __func__);
1227 return -ENOMEM;
1228 }
1229
1230 f1a->button_map = kcalloc(f1a->button_count,
1231 sizeof(*(f1a->button_map)), GFP_KERNEL);
1232 if (!f1a->button_map) {
1233 dev_err(&rmi4_data->i2c_client->dev,
1234 "%s: Failed to alloc mem for button map\n",
1235 __func__);
1236 return -ENOMEM;
1237 }
1238
1239 return 0;
1240}
1241
1242static int synaptics_rmi4_capacitance_button_map(
1243 struct synaptics_rmi4_data *rmi4_data,
1244 struct synaptics_rmi4_fn *fhandler)
1245{
1246 unsigned char ii;
1247 struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
1248 const struct synaptics_rmi4_platform_data *pdata = rmi4_data->board;
1249
1250 if (!pdata->capacitance_button_map) {
1251 dev_err(&rmi4_data->i2c_client->dev,
Alexandra Chind5591a62013-02-07 12:59:15 -08001252 "%s: capacitance_button_map is" \
1253 "NULL in board file\n",
Alexandra Chin669d27c2012-12-24 15:42:30 +08001254 __func__);
1255 return -ENODEV;
1256 } else if (!pdata->capacitance_button_map->map) {
1257 dev_err(&rmi4_data->i2c_client->dev,
1258 "%s: Button map is missing in board file\n",
1259 __func__);
1260 return -ENODEV;
1261 } else {
1262 if (pdata->capacitance_button_map->nbuttons !=
1263 f1a->button_count) {
1264 f1a->valid_button_count = min(f1a->button_count,
1265 pdata->capacitance_button_map->nbuttons);
1266 } else {
1267 f1a->valid_button_count = f1a->button_count;
1268 }
1269
1270 for (ii = 0; ii < f1a->valid_button_count; ii++)
1271 f1a->button_map[ii] =
1272 pdata->capacitance_button_map->map[ii];
1273 }
1274
1275 return 0;
1276}
1277
1278static void synaptics_rmi4_f1a_kfree(struct synaptics_rmi4_fn *fhandler)
1279{
1280 struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
1281
1282 if (f1a) {
1283 kfree(f1a->button_data_buffer);
1284 kfree(f1a->button_map);
1285 kfree(f1a);
1286 fhandler->data = NULL;
1287 }
1288
1289 return;
1290}
1291
1292static int synaptics_rmi4_f1a_init(struct synaptics_rmi4_data *rmi4_data,
1293 struct synaptics_rmi4_fn *fhandler,
1294 struct synaptics_rmi4_fn_desc *fd,
1295 unsigned int intr_count)
1296{
1297 int retval;
1298 unsigned char ii;
1299 unsigned short intr_offset;
1300
1301 fhandler->fn_number = fd->fn_number;
1302 fhandler->num_of_data_sources = fd->intr_src_count;
1303
1304 fhandler->intr_reg_num = (intr_count + 7) / 8;
1305 if (fhandler->intr_reg_num != 0)
1306 fhandler->intr_reg_num -= 1;
1307
1308 /* Set an enable bit for each data source */
1309 intr_offset = intr_count % 8;
1310 fhandler->intr_mask = 0;
1311 for (ii = intr_offset;
1312 ii < ((fd->intr_src_count & MASK_3BIT) +
1313 intr_offset);
1314 ii++)
1315 fhandler->intr_mask |= 1 << ii;
1316
1317 retval = synaptics_rmi4_f1a_alloc_mem(rmi4_data, fhandler);
1318 if (retval < 0)
1319 goto error_exit;
1320
1321 retval = synaptics_rmi4_capacitance_button_map(rmi4_data, fhandler);
1322 if (retval < 0)
1323 goto error_exit;
1324
1325 rmi4_data->button_0d_enabled = 1;
1326
1327 return 0;
1328
1329error_exit:
1330 synaptics_rmi4_f1a_kfree(fhandler);
1331
1332 return retval;
1333}
1334
1335static int synaptics_rmi4_alloc_fh(struct synaptics_rmi4_fn **fhandler,
1336 struct synaptics_rmi4_fn_desc *rmi_fd, int page_number)
1337{
Alexandra Chinfb798fa2013-03-26 17:15:38 -07001338 *fhandler = kzalloc(sizeof(**fhandler), GFP_KERNEL);
Alexandra Chin669d27c2012-12-24 15:42:30 +08001339 if (!(*fhandler))
1340 return -ENOMEM;
1341
1342 (*fhandler)->full_addr.data_base =
1343 (rmi_fd->data_base_addr |
1344 (page_number << 8));
1345 (*fhandler)->full_addr.ctrl_base =
1346 (rmi_fd->ctrl_base_addr |
1347 (page_number << 8));
1348 (*fhandler)->full_addr.cmd_base =
1349 (rmi_fd->cmd_base_addr |
1350 (page_number << 8));
1351 (*fhandler)->full_addr.query_base =
1352 (rmi_fd->query_base_addr |
1353 (page_number << 8));
Alexandra Chinfb798fa2013-03-26 17:15:38 -07001354 (*fhandler)->fn_number = rmi_fd->fn_number;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001355
1356 return 0;
1357}
1358
Alexandra Chind5591a62013-02-07 12:59:15 -08001359
1360 /**
1361 * synaptics_rmi4_query_device_info()
1362 *
1363 * Called by synaptics_rmi4_query_device().
1364 *
1365 */
1366static int synaptics_rmi4_query_device_info(
1367 struct synaptics_rmi4_data *rmi4_data)
1368{
1369 int retval;
1370 unsigned char f01_query[F01_STD_QUERY_LEN];
1371 struct synaptics_rmi4_device_info *rmi = &(rmi4_data->rmi4_mod_info);
1372
1373 retval = synaptics_rmi4_i2c_read(rmi4_data,
1374 rmi4_data->f01_query_base_addr,
1375 f01_query,
1376 sizeof(f01_query));
1377 if (retval < 0)
1378 return retval;
1379
1380 /* RMI Version 4.0 currently supported */
1381 rmi->version_major = 4;
1382 rmi->version_minor = 0;
1383
1384 rmi->manufacturer_id = f01_query[0];
1385 rmi->product_props = f01_query[1];
1386 rmi->product_info[0] = f01_query[2] & MASK_7BIT;
1387 rmi->product_info[1] = f01_query[3] & MASK_7BIT;
1388 rmi->date_code[0] = f01_query[4] & MASK_5BIT;
1389 rmi->date_code[1] = f01_query[5] & MASK_4BIT;
1390 rmi->date_code[2] = f01_query[6] & MASK_5BIT;
1391 rmi->tester_id = ((f01_query[7] & MASK_7BIT) << 8) |
1392 (f01_query[8] & MASK_7BIT);
1393 rmi->serial_number = ((f01_query[9] & MASK_7BIT) << 8) |
1394 (f01_query[10] & MASK_7BIT);
1395 memcpy(rmi->product_id_string, &f01_query[11], 10);
1396
1397 if (rmi->manufacturer_id != 1) {
1398 dev_err(&rmi4_data->i2c_client->dev,
1399 "%s: Non-Synaptics device found, manufacturer ID = %d\n",
1400 __func__, rmi->manufacturer_id);
1401 }
1402
1403 retval = synaptics_rmi4_i2c_read(rmi4_data,
1404 rmi4_data->f01_query_base_addr + F01_BUID_ID_OFFSET,
1405 rmi->build_id,
1406 sizeof(rmi->build_id));
1407 if (retval < 0) {
1408 dev_err(&rmi4_data->i2c_client->dev,
1409 "%s: Failed to read firmware build id (code %d)\n",
1410 __func__, retval);
1411 return retval;
1412 }
Alexandra Chinfb798fa2013-03-26 17:15:38 -07001413 return 0;
Alexandra Chind5591a62013-02-07 12:59:15 -08001414}
1415
Alexandra Chin669d27c2012-12-24 15:42:30 +08001416 /**
1417 * synaptics_rmi4_query_device()
1418 *
1419 * Called by synaptics_rmi4_probe().
1420 *
1421 * This funtion scans the page description table, records the offsets
1422 * to the register types of Function $01, sets up the function handlers
1423 * for Function $11 and Function $12, determines the number of interrupt
1424 * sources from the sensor, adds valid Functions with data inputs to the
1425 * Function linked list, parses information from the query registers of
1426 * Function $01, and enables the interrupt sources from the valid Functions
1427 * with data inputs.
1428 */
1429static int synaptics_rmi4_query_device(struct synaptics_rmi4_data *rmi4_data)
1430{
1431 int retval;
1432 unsigned char ii;
1433 unsigned char page_number;
1434 unsigned char intr_count = 0;
1435 unsigned char data_sources = 0;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001436 unsigned short pdt_entry_addr;
1437 unsigned short intr_addr;
1438 struct synaptics_rmi4_f01_device_status status;
1439 struct synaptics_rmi4_fn_desc rmi_fd;
1440 struct synaptics_rmi4_fn *fhandler;
1441 struct synaptics_rmi4_device_info *rmi;
1442
1443 rmi = &(rmi4_data->rmi4_mod_info);
1444
1445 INIT_LIST_HEAD(&rmi->support_fn_list);
1446
1447 /* Scan the page description tables of the pages to service */
1448 for (page_number = 0; page_number < PAGES_TO_SERVICE; page_number++) {
1449 for (pdt_entry_addr = PDT_START; pdt_entry_addr > PDT_END;
1450 pdt_entry_addr -= PDT_ENTRY_SIZE) {
1451 pdt_entry_addr |= (page_number << 8);
1452
1453 retval = synaptics_rmi4_i2c_read(rmi4_data,
1454 pdt_entry_addr,
1455 (unsigned char *)&rmi_fd,
1456 sizeof(rmi_fd));
1457 if (retval < 0)
1458 return retval;
1459
1460 fhandler = NULL;
1461
1462 if (rmi_fd.fn_number == 0) {
1463 dev_dbg(&rmi4_data->i2c_client->dev,
1464 "%s: Reached end of PDT\n",
1465 __func__);
1466 break;
1467 }
1468
1469 dev_dbg(&rmi4_data->i2c_client->dev,
1470 "%s: F%02x found (page %d)\n",
1471 __func__, rmi_fd.fn_number,
1472 page_number);
1473
1474 switch (rmi_fd.fn_number) {
1475 case SYNAPTICS_RMI4_F01:
1476 rmi4_data->f01_query_base_addr =
1477 rmi_fd.query_base_addr;
1478 rmi4_data->f01_ctrl_base_addr =
1479 rmi_fd.ctrl_base_addr;
1480 rmi4_data->f01_data_base_addr =
1481 rmi_fd.data_base_addr;
1482 rmi4_data->f01_cmd_base_addr =
1483 rmi_fd.cmd_base_addr;
1484
Alexandra Chind5591a62013-02-07 12:59:15 -08001485 retval =
1486 synaptics_rmi4_query_device_info(rmi4_data);
1487 if (retval < 0)
1488 return retval;
1489
Alexandra Chin669d27c2012-12-24 15:42:30 +08001490 retval = synaptics_rmi4_i2c_read(rmi4_data,
1491 rmi4_data->f01_data_base_addr,
1492 status.data,
1493 sizeof(status.data));
1494 if (retval < 0)
1495 return retval;
1496
Alexandra Chin3a747522013-04-15 12:13:38 -07001497 while (status.status_code == STATUS_CRC_IN_PROGRESS) {
1498 msleep(1);
1499 retval = synaptics_rmi4_i2c_read(rmi4_data,
1500 rmi4_data->f01_data_base_addr,
1501 status.data,
1502 sizeof(status.data));
1503 if (retval < 0)
1504 return retval;
1505 }
1506
Alexandra Chin669d27c2012-12-24 15:42:30 +08001507 if (status.flash_prog == 1) {
1508 pr_notice("%s: In flash prog mode, status = 0x%02x\n",
1509 __func__,
1510 status.status_code);
1511 goto flash_prog_mode;
1512 }
Alexandra Chind5591a62013-02-07 12:59:15 -08001513 break;
1514
Alexandra Chin669d27c2012-12-24 15:42:30 +08001515 case SYNAPTICS_RMI4_F11:
1516 if (rmi_fd.intr_src_count == 0)
1517 break;
1518
1519 retval = synaptics_rmi4_alloc_fh(&fhandler,
1520 &rmi_fd, page_number);
1521 if (retval < 0) {
1522 dev_err(&rmi4_data->i2c_client->dev,
1523 "%s: Failed to alloc for F%d\n",
1524 __func__,
1525 rmi_fd.fn_number);
1526 return retval;
1527 }
1528
1529 retval = synaptics_rmi4_f11_init(rmi4_data,
1530 fhandler, &rmi_fd, intr_count);
1531 if (retval < 0)
1532 return retval;
1533 break;
1534
1535 case SYNAPTICS_RMI4_F1A:
1536 if (rmi_fd.intr_src_count == 0)
1537 break;
1538
1539 retval = synaptics_rmi4_alloc_fh(&fhandler,
1540 &rmi_fd, page_number);
1541 if (retval < 0) {
1542 dev_err(&rmi4_data->i2c_client->dev,
1543 "%s: Failed to alloc for F%d\n",
1544 __func__,
1545 rmi_fd.fn_number);
1546 return retval;
1547 }
1548
1549 retval = synaptics_rmi4_f1a_init(rmi4_data,
1550 fhandler, &rmi_fd, intr_count);
1551 if (retval < 0)
1552 return retval;
1553 break;
1554 }
1555
1556 /* Accumulate the interrupt count */
1557 intr_count += (rmi_fd.intr_src_count & MASK_3BIT);
1558
1559 if (fhandler && rmi_fd.intr_src_count) {
1560 list_add_tail(&fhandler->link,
1561 &rmi->support_fn_list);
1562 }
1563 }
1564 }
1565
1566flash_prog_mode:
1567 rmi4_data->num_of_intr_regs = (intr_count + 7) / 8;
1568 dev_dbg(&rmi4_data->i2c_client->dev,
1569 "%s: Number of interrupt registers = %d\n",
1570 __func__, rmi4_data->num_of_intr_regs);
1571
Alexandra Chin669d27c2012-12-24 15:42:30 +08001572 memset(rmi4_data->intr_mask, 0x00, sizeof(rmi4_data->intr_mask));
1573
1574 /*
1575 * Map out the interrupt bit masks for the interrupt sources
1576 * from the registered function handlers.
1577 */
Alexandra Chind5591a62013-02-07 12:59:15 -08001578 if (!list_empty(&rmi->support_fn_list)) {
1579 list_for_each_entry(fhandler, &rmi->support_fn_list, link)
1580 data_sources += fhandler->num_of_data_sources;
1581 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001582 if (data_sources) {
Alexandra Chind5591a62013-02-07 12:59:15 -08001583 if (!list_empty(&rmi->support_fn_list)) {
1584 list_for_each_entry(fhandler,
1585 &rmi->support_fn_list, link) {
1586 if (fhandler->num_of_data_sources) {
1587 rmi4_data->intr_mask[fhandler->intr_reg_num] |=
1588 fhandler->intr_mask;
1589 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001590 }
1591 }
1592 }
1593
1594 /* Enable the interrupt sources */
1595 for (ii = 0; ii < rmi4_data->num_of_intr_regs; ii++) {
1596 if (rmi4_data->intr_mask[ii] != 0x00) {
1597 dev_dbg(&rmi4_data->i2c_client->dev,
1598 "%s: Interrupt enable mask %d = 0x%02x\n",
1599 __func__, ii, rmi4_data->intr_mask[ii]);
1600 intr_addr = rmi4_data->f01_ctrl_base_addr + 1 + ii;
1601 retval = synaptics_rmi4_i2c_write(rmi4_data,
1602 intr_addr,
1603 &(rmi4_data->intr_mask[ii]),
1604 sizeof(rmi4_data->intr_mask[ii]));
1605 if (retval < 0)
1606 return retval;
1607 }
1608 }
1609
1610 return 0;
1611}
1612
Alexandra Chinc556cf02013-03-19 17:46:05 -07001613static int synaptics_rmi4_reset_command(struct synaptics_rmi4_data *rmi4_data)
Alexandra Chin669d27c2012-12-24 15:42:30 +08001614{
1615 int retval;
Alexandra Chinc556cf02013-03-19 17:46:05 -07001616 int page_number;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001617 unsigned char command = 0x01;
Alexandra Chinc556cf02013-03-19 17:46:05 -07001618 unsigned short pdt_entry_addr;
1619 struct synaptics_rmi4_fn_desc rmi_fd;
1620 bool done = false;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001621
Alexandra Chinc556cf02013-03-19 17:46:05 -07001622 /* Scan the page description tables of the pages to service */
1623 for (page_number = 0; page_number < PAGES_TO_SERVICE; page_number++) {
1624 for (pdt_entry_addr = PDT_START; pdt_entry_addr > PDT_END;
1625 pdt_entry_addr -= PDT_ENTRY_SIZE) {
1626 retval = synaptics_rmi4_i2c_read(rmi4_data,
1627 pdt_entry_addr,
1628 (unsigned char *)&rmi_fd,
1629 sizeof(rmi_fd));
1630 if (retval < 0)
1631 return retval;
1632
1633 if (rmi_fd.fn_number == 0)
1634 break;
1635
1636 switch (rmi_fd.fn_number) {
1637 case SYNAPTICS_RMI4_F01:
1638 rmi4_data->f01_cmd_base_addr =
1639 rmi_fd.cmd_base_addr;
1640 done = true;
1641 break;
1642 }
1643 }
1644 if (done) {
1645 dev_info(&rmi4_data->i2c_client->dev,
1646 "%s: Find F01 in page description table 0x%x\n",
1647 __func__, rmi4_data->f01_cmd_base_addr);
1648 break;
1649 }
1650 }
1651
1652 if (!done) {
1653 dev_err(&rmi4_data->i2c_client->dev,
1654 "%s: Cannot find F01 in page description table\n",
1655 __func__);
1656 return -EINVAL;
1657 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001658
1659 retval = synaptics_rmi4_i2c_write(rmi4_data,
1660 rmi4_data->f01_cmd_base_addr,
1661 &command,
1662 sizeof(command));
1663 if (retval < 0) {
1664 dev_err(&rmi4_data->i2c_client->dev,
1665 "%s: Failed to issue reset command, error = %d\n",
1666 __func__, retval);
1667 return retval;
1668 }
1669
Alexandra Chin3a747522013-04-15 12:13:38 -07001670 msleep(RESET_DELAY);
Alexandra Chinc556cf02013-03-19 17:46:05 -07001671 return retval;
1672};
1673
1674static int synaptics_rmi4_reset_device(struct synaptics_rmi4_data *rmi4_data)
1675{
1676 int retval;
1677 struct synaptics_rmi4_fn *fhandler;
1678 struct synaptics_rmi4_device_info *rmi;
1679
1680 rmi = &(rmi4_data->rmi4_mod_info);
1681
1682 retval = synaptics_rmi4_reset_command(rmi4_data);
1683 if (retval < 0) {
1684 dev_err(&rmi4_data->i2c_client->dev,
1685 "%s: Failed to send command reset\n",
1686 __func__);
1687 return retval;
1688 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001689
Alexandra Chind5591a62013-02-07 12:59:15 -08001690 if (!list_empty(&rmi->support_fn_list)) {
1691 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
1692 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
1693 synaptics_rmi4_f1a_kfree(fhandler);
1694 else
1695 kfree(fhandler->data);
1696 kfree(fhandler);
1697 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001698 }
1699
1700 retval = synaptics_rmi4_query_device(rmi4_data);
1701 if (retval < 0) {
1702 dev_err(&rmi4_data->i2c_client->dev,
1703 "%s: Failed to query device\n",
1704 __func__);
1705 return retval;
1706 }
1707
1708 return 0;
1709}
1710
1711/**
1712* synaptics_rmi4_detection_work()
1713*
1714* Called by the kernel at the scheduled time.
1715*
1716* This function is a self-rearming work thread that checks for the
1717* insertion and removal of other expansion Function modules such as
1718* rmi_dev and calls their initialization and removal callback functions
1719* accordingly.
1720*/
1721static void synaptics_rmi4_detection_work(struct work_struct *work)
1722{
1723 struct synaptics_rmi4_exp_fn *exp_fhandler, *next_list_entry;
1724 struct synaptics_rmi4_data *rmi4_data =
1725 container_of(work, struct synaptics_rmi4_data,
1726 det_work.work);
1727
Alexandra Chin669d27c2012-12-24 15:42:30 +08001728 mutex_lock(&exp_fn_list_mutex);
1729 if (!list_empty(&exp_fn_list)) {
1730 list_for_each_entry_safe(exp_fhandler,
1731 next_list_entry,
1732 &exp_fn_list,
1733 link) {
1734 if ((exp_fhandler->func_init != NULL) &&
1735 (exp_fhandler->inserted == false)) {
1736 exp_fhandler->func_init(rmi4_data);
1737 exp_fhandler->inserted = true;
1738 } else if ((exp_fhandler->func_init == NULL) &&
1739 (exp_fhandler->inserted == true)) {
1740 exp_fhandler->func_remove(rmi4_data);
1741 list_del(&exp_fhandler->link);
1742 kfree(exp_fhandler);
1743 }
1744 }
1745 }
1746 mutex_unlock(&exp_fn_list_mutex);
1747
1748 return;
1749}
1750
1751/**
1752* synaptics_rmi4_new_function()
1753*
1754* Called by other expansion Function modules in their module init and
1755* module exit functions.
1756*
1757* This function is used by other expansion Function modules such as
1758* rmi_dev to register themselves with the driver by providing their
1759* initialization and removal callback function pointers so that they
1760* can be inserted or removed dynamically at module init and exit times,
1761* respectively.
1762*/
1763void synaptics_rmi4_new_function(enum exp_fn fn_type, bool insert,
1764 int (*func_init)(struct synaptics_rmi4_data *rmi4_data),
1765 void (*func_remove)(struct synaptics_rmi4_data *rmi4_data),
1766 void (*func_attn)(struct synaptics_rmi4_data *rmi4_data,
1767 unsigned char intr_mask))
1768{
1769 struct synaptics_rmi4_exp_fn *exp_fhandler;
1770
1771 if (!exp_fn_inited) {
1772 mutex_init(&exp_fn_list_mutex);
1773 INIT_LIST_HEAD(&exp_fn_list);
1774 exp_fn_inited = 1;
1775 }
1776
1777 mutex_lock(&exp_fn_list_mutex);
1778 if (insert) {
1779 exp_fhandler = kzalloc(sizeof(*exp_fhandler), GFP_KERNEL);
1780 if (!exp_fhandler) {
1781 pr_err("%s: Failed to alloc mem for expansion function\n",
1782 __func__);
1783 goto exit;
1784 }
1785 exp_fhandler->fn_type = fn_type;
1786 exp_fhandler->func_init = func_init;
1787 exp_fhandler->func_attn = func_attn;
1788 exp_fhandler->func_remove = func_remove;
1789 exp_fhandler->inserted = false;
1790 list_add_tail(&exp_fhandler->link, &exp_fn_list);
1791 } else {
Alexandra Chind5591a62013-02-07 12:59:15 -08001792 if (!list_empty(&exp_fn_list)) {
1793 list_for_each_entry(exp_fhandler, &exp_fn_list, link) {
1794 if (exp_fhandler->func_init == func_init) {
1795 exp_fhandler->inserted = false;
1796 exp_fhandler->func_init = NULL;
1797 exp_fhandler->func_attn = NULL;
1798 goto exit;
1799 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001800 }
1801 }
1802 }
1803
1804exit:
1805 mutex_unlock(&exp_fn_list_mutex);
1806
1807 return;
1808}
1809EXPORT_SYMBOL(synaptics_rmi4_new_function);
1810
Amy Maloche1a53b612013-01-18 15:25:15 -08001811
1812static int reg_set_optimum_mode_check(struct regulator *reg, int load_uA)
1813{
1814 return (regulator_count_voltages(reg) > 0) ?
1815 regulator_set_optimum_mode(reg, load_uA) : 0;
1816}
1817
1818static int synaptics_rmi4_regulator_configure(struct synaptics_rmi4_data
1819 *rmi4_data, bool on)
1820{
1821 int retval;
1822
1823 if (on == false)
1824 goto hw_shutdown;
1825
1826 if (rmi4_data->board->regulator_en) {
1827 rmi4_data->vdd = regulator_get(&rmi4_data->i2c_client->dev,
1828 "vdd");
1829 if (IS_ERR(rmi4_data->vdd)) {
1830 dev_err(&rmi4_data->i2c_client->dev,
1831 "%s: Failed to get vdd regulator\n",
1832 __func__);
1833 return PTR_ERR(rmi4_data->vdd);
1834 }
1835
1836 if (regulator_count_voltages(rmi4_data->vdd) > 0) {
1837 retval = regulator_set_voltage(rmi4_data->vdd,
1838 RMI4_VTG_MIN_UV, RMI4_VTG_MAX_UV);
1839 if (retval) {
1840 dev_err(&rmi4_data->i2c_client->dev,
1841 "regulator set_vtg failed retval=%d\n",
1842 retval);
1843 goto err_set_vtg_vdd;
1844 }
1845 }
1846 }
1847
1848 if (rmi4_data->board->i2c_pull_up) {
1849 rmi4_data->vcc_i2c = regulator_get(&rmi4_data->i2c_client->dev,
1850 "vcc_i2c");
1851 if (IS_ERR(rmi4_data->vcc_i2c)) {
1852 dev_err(&rmi4_data->i2c_client->dev,
1853 "%s: Failed to get i2c regulator\n",
1854 __func__);
1855 retval = PTR_ERR(rmi4_data->vcc_i2c);
1856 goto err_get_vtg_i2c;
1857 }
1858
1859 if (regulator_count_voltages(rmi4_data->vcc_i2c) > 0) {
1860 retval = regulator_set_voltage(rmi4_data->vcc_i2c,
1861 RMI4_I2C_VTG_MIN_UV, RMI4_I2C_VTG_MAX_UV);
1862 if (retval) {
1863 dev_err(&rmi4_data->i2c_client->dev,
1864 "reg set i2c vtg failed retval=%d\n",
1865 retval);
1866 goto err_set_vtg_i2c;
1867 }
1868 }
1869 }
Amy Malocheecfb4892013-02-06 13:30:36 -08001870 return 0;
Amy Maloche1a53b612013-01-18 15:25:15 -08001871
1872err_set_vtg_i2c:
1873 if (rmi4_data->board->i2c_pull_up)
1874 regulator_put(rmi4_data->vcc_i2c);
1875err_get_vtg_i2c:
1876 if (rmi4_data->board->regulator_en)
1877 if (regulator_count_voltages(rmi4_data->vdd) > 0)
1878 regulator_set_voltage(rmi4_data->vdd, 0,
1879 RMI4_VTG_MAX_UV);
1880err_set_vtg_vdd:
1881 if (rmi4_data->board->regulator_en)
1882 regulator_put(rmi4_data->vdd);
1883 return retval;
1884
1885hw_shutdown:
1886 if (rmi4_data->board->regulator_en) {
1887 if (regulator_count_voltages(rmi4_data->vdd) > 0)
1888 regulator_set_voltage(rmi4_data->vdd, 0,
1889 RMI4_VTG_MAX_UV);
1890 regulator_put(rmi4_data->vdd);
1891 }
1892 if (rmi4_data->board->i2c_pull_up) {
1893 if (regulator_count_voltages(rmi4_data->vcc_i2c) > 0)
1894 regulator_set_voltage(rmi4_data->vcc_i2c, 0,
1895 RMI4_I2C_VTG_MAX_UV);
1896 regulator_put(rmi4_data->vcc_i2c);
1897 }
1898 return 0;
1899};
1900
1901static int synaptics_rmi4_power_on(struct synaptics_rmi4_data *rmi4_data,
1902 bool on) {
1903 int retval;
1904
1905 if (on == false)
1906 goto power_off;
1907
1908 if (rmi4_data->board->regulator_en) {
1909 retval = reg_set_optimum_mode_check(rmi4_data->vdd,
1910 RMI4_ACTIVE_LOAD_UA);
1911 if (retval < 0) {
1912 dev_err(&rmi4_data->i2c_client->dev,
1913 "Regulator vdd set_opt failed rc=%d\n",
1914 retval);
1915 return retval;
1916 }
1917
1918 retval = regulator_enable(rmi4_data->vdd);
1919 if (retval) {
1920 dev_err(&rmi4_data->i2c_client->dev,
1921 "Regulator vdd enable failed rc=%d\n",
1922 retval);
1923 goto error_reg_en_vdd;
1924 }
1925 }
1926
1927 if (rmi4_data->board->i2c_pull_up) {
1928 retval = reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
1929 RMI4_I2C_LOAD_UA);
1930 if (retval < 0) {
1931 dev_err(&rmi4_data->i2c_client->dev,
1932 "Regulator vcc_i2c set_opt failed rc=%d\n",
1933 retval);
1934 goto error_reg_opt_i2c;
1935 }
1936
1937 retval = regulator_enable(rmi4_data->vcc_i2c);
1938 if (retval) {
1939 dev_err(&rmi4_data->i2c_client->dev,
1940 "Regulator vcc_i2c enable failed rc=%d\n",
1941 retval);
1942 goto error_reg_en_vcc_i2c;
1943 }
1944 }
1945 return 0;
1946
1947error_reg_en_vcc_i2c:
1948 if (rmi4_data->board->i2c_pull_up)
1949 reg_set_optimum_mode_check(rmi4_data->vdd, 0);
1950error_reg_opt_i2c:
1951 if (rmi4_data->board->regulator_en)
1952 regulator_disable(rmi4_data->vdd);
1953error_reg_en_vdd:
1954 if (rmi4_data->board->regulator_en)
1955 reg_set_optimum_mode_check(rmi4_data->vdd, 0);
1956 return retval;
1957
1958power_off:
1959 if (rmi4_data->board->regulator_en) {
1960 reg_set_optimum_mode_check(rmi4_data->vdd, 0);
1961 regulator_disable(rmi4_data->vdd);
1962 }
1963 if (rmi4_data->board->i2c_pull_up) {
1964 reg_set_optimum_mode_check(rmi4_data->vcc_i2c, 0);
1965 regulator_disable(rmi4_data->vcc_i2c);
1966 }
1967 return 0;
1968}
1969
Alexandra Chin669d27c2012-12-24 15:42:30 +08001970 /**
1971 * synaptics_rmi4_probe()
1972 *
1973 * Called by the kernel when an association with an I2C device of the
1974 * same name is made (after doing i2c_add_driver).
1975 *
1976 * This funtion allocates and initializes the resources for the driver
1977 * as an input driver, turns on the power to the sensor, queries the
1978 * sensor for its supported Functions and characteristics, registers
1979 * the driver to the input subsystem, sets up the interrupt, handles
1980 * the registration of the early_suspend and late_resume functions,
1981 * and creates a work queue for detection of other expansion Function
1982 * modules.
1983 */
1984static int __devinit synaptics_rmi4_probe(struct i2c_client *client,
1985 const struct i2c_device_id *dev_id)
1986{
Amy Maloche1a53b612013-01-18 15:25:15 -08001987 int retval = 0;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001988 unsigned char ii;
1989 unsigned char attr_count;
1990 struct synaptics_rmi4_f1a_handle *f1a;
1991 struct synaptics_rmi4_fn *fhandler;
1992 struct synaptics_rmi4_data *rmi4_data;
1993 struct synaptics_rmi4_device_info *rmi;
Amy Malocheecfb4892013-02-06 13:30:36 -08001994 struct synaptics_rmi4_platform_data *platform_data =
Alexandra Chin669d27c2012-12-24 15:42:30 +08001995 client->dev.platform_data;
1996
1997 if (!i2c_check_functionality(client->adapter,
1998 I2C_FUNC_SMBUS_BYTE_DATA)) {
1999 dev_err(&client->dev,
2000 "%s: SMBus byte data not supported\n",
2001 __func__);
2002 return -EIO;
2003 }
2004
Amy Malocheecfb4892013-02-06 13:30:36 -08002005 if (client->dev.of_node) {
2006 platform_data = devm_kzalloc(&client->dev,
2007 sizeof(*platform_data),
2008 GFP_KERNEL);
2009 if (!platform_data) {
2010 dev_err(&client->dev, "Failed to allocate memory\n");
2011 return -ENOMEM;
2012 }
2013
2014 retval = synaptics_rmi4_parse_dt(&client->dev, platform_data);
2015 if (retval)
2016 return retval;
2017 } else {
2018 platform_data = client->dev.platform_data;
2019 }
2020
Alexandra Chin669d27c2012-12-24 15:42:30 +08002021 if (!platform_data) {
2022 dev_err(&client->dev,
2023 "%s: No platform data found\n",
2024 __func__);
2025 return -EINVAL;
2026 }
2027
2028 rmi4_data = kzalloc(sizeof(*rmi4_data) * 2, GFP_KERNEL);
2029 if (!rmi4_data) {
2030 dev_err(&client->dev,
2031 "%s: Failed to alloc mem for rmi4_data\n",
2032 __func__);
2033 return -ENOMEM;
2034 }
2035
2036 rmi = &(rmi4_data->rmi4_mod_info);
2037
2038 rmi4_data->input_dev = input_allocate_device();
2039 if (rmi4_data->input_dev == NULL) {
2040 dev_err(&client->dev,
2041 "%s: Failed to allocate input device\n",
2042 __func__);
2043 retval = -ENOMEM;
2044 goto err_input_device;
2045 }
Alexandra Chind5591a62013-02-07 12:59:15 -08002046
Alexandra Chin669d27c2012-12-24 15:42:30 +08002047 rmi4_data->i2c_client = client;
2048 rmi4_data->current_page = MASK_8BIT;
2049 rmi4_data->board = platform_data;
2050 rmi4_data->touch_stopped = false;
2051 rmi4_data->sensor_sleep = false;
2052 rmi4_data->irq_enabled = false;
2053
2054 rmi4_data->i2c_read = synaptics_rmi4_i2c_read;
2055 rmi4_data->i2c_write = synaptics_rmi4_i2c_write;
2056 rmi4_data->irq_enable = synaptics_rmi4_irq_enable;
2057 rmi4_data->reset_device = synaptics_rmi4_reset_device;
2058
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002059 rmi4_data->flip_x = rmi4_data->board->x_flip;
2060 rmi4_data->flip_y = rmi4_data->board->y_flip;
2061
Amy Malochecb835832013-03-26 18:06:05 -07002062 rmi4_data->fw_image_name = rmi4_data->board->fw_image_name;
2063
Alexandra Chin669d27c2012-12-24 15:42:30 +08002064 rmi4_data->input_dev->name = DRIVER_NAME;
2065 rmi4_data->input_dev->phys = INPUT_PHYS_NAME;
2066 rmi4_data->input_dev->id.bustype = BUS_I2C;
Alexandra Chin5d2999d2013-02-22 15:09:29 -08002067 rmi4_data->input_dev->id.product = SYNAPTICS_DSX_DRIVER_PRODUCT;
2068 rmi4_data->input_dev->id.version = SYNAPTICS_DSX_DRIVER_VERSION;
Alexandra Chin669d27c2012-12-24 15:42:30 +08002069 rmi4_data->input_dev->dev.parent = &client->dev;
2070 input_set_drvdata(rmi4_data->input_dev, rmi4_data);
2071
2072 set_bit(EV_SYN, rmi4_data->input_dev->evbit);
2073 set_bit(EV_KEY, rmi4_data->input_dev->evbit);
2074 set_bit(EV_ABS, rmi4_data->input_dev->evbit);
Alexandra Chind5591a62013-02-07 12:59:15 -08002075 set_bit(BTN_TOUCH, rmi4_data->input_dev->keybit);
2076 set_bit(BTN_TOOL_FINGER, rmi4_data->input_dev->keybit);
Alexandra Chin669d27c2012-12-24 15:42:30 +08002077
2078#ifdef INPUT_PROP_DIRECT
2079 set_bit(INPUT_PROP_DIRECT, rmi4_data->input_dev->propbit);
2080#endif
2081
Amy Maloche1a53b612013-01-18 15:25:15 -08002082 retval = synaptics_rmi4_regulator_configure(rmi4_data, true);
2083 if (retval < 0) {
2084 dev_err(&client->dev, "Failed to configure regulators\n");
Amy Maloche946da662013-01-18 16:27:11 -08002085 goto err_reg_configure;
Amy Maloche1a53b612013-01-18 15:25:15 -08002086 }
2087
2088 retval = synaptics_rmi4_power_on(rmi4_data, true);
2089 if (retval < 0) {
2090 dev_err(&client->dev, "Failed to power on\n");
Amy Maloche946da662013-01-18 16:27:11 -08002091 goto err_power_device;
Amy Maloche1a53b612013-01-18 15:25:15 -08002092 }
2093
Amy Maloche946da662013-01-18 16:27:11 -08002094 if (gpio_is_valid(platform_data->irq_gpio)) {
2095 /* configure touchscreen irq gpio */
2096 retval = gpio_request(platform_data->irq_gpio, "rmi4_irq_gpio");
2097 if (retval) {
2098 dev_err(&client->dev, "unable to request gpio [%d]\n",
2099 platform_data->irq_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002100 goto err_irq_gpio_req;
Amy Maloche946da662013-01-18 16:27:11 -08002101 }
2102 retval = gpio_direction_input(platform_data->irq_gpio);
2103 if (retval) {
2104 dev_err(&client->dev,
2105 "unable to set direction for gpio [%d]\n",
2106 platform_data->irq_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002107 goto err_irq_gpio_dir;
Amy Maloche946da662013-01-18 16:27:11 -08002108 }
2109 } else {
2110 dev_err(&client->dev, "irq gpio not provided\n");
Amy Malocheecfb4892013-02-06 13:30:36 -08002111 goto err_irq_gpio_req;
Amy Maloche946da662013-01-18 16:27:11 -08002112 }
2113
2114 if (gpio_is_valid(platform_data->reset_gpio)) {
2115 /* configure touchscreen reset out gpio */
2116 retval = gpio_request(platform_data->reset_gpio,
2117 "rmi4_reset_gpio");
2118 if (retval) {
2119 dev_err(&client->dev, "unable to request gpio [%d]\n",
2120 platform_data->reset_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002121 goto err_irq_gpio_dir;
Amy Maloche946da662013-01-18 16:27:11 -08002122 }
2123
2124 retval = gpio_direction_output(platform_data->reset_gpio, 1);
2125 if (retval) {
2126 dev_err(&client->dev,
2127 "unable to set direction for gpio [%d]\n",
2128 platform_data->reset_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002129 goto err_reset_gpio_dir;
Amy Maloche946da662013-01-18 16:27:11 -08002130 }
2131
2132 gpio_set_value(platform_data->reset_gpio, 0);
2133 usleep(RMI4_GPIO_SLEEP_LOW_US);
2134 gpio_set_value(platform_data->reset_gpio, 1);
Alexandra Chin3a747522013-04-15 12:13:38 -07002135 msleep(RESET_DELAY);
Alexandra Chinc556cf02013-03-19 17:46:05 -07002136 } else
2137 synaptics_rmi4_reset_command(rmi4_data);
Amy Maloche946da662013-01-18 16:27:11 -08002138
2139
Amy Maloche1a53b612013-01-18 15:25:15 -08002140 init_waitqueue_head(&rmi4_data->wait);
2141 mutex_init(&(rmi4_data->rmi4_io_ctrl_mutex));
2142
2143 retval = synaptics_rmi4_query_device(rmi4_data);
2144 if (retval < 0) {
2145 dev_err(&client->dev,
2146 "%s: Failed to query device\n",
2147 __func__);
Amy Malocheecfb4892013-02-06 13:30:36 -08002148 goto err_reset_gpio_dir;
Amy Maloche1a53b612013-01-18 15:25:15 -08002149 }
2150
Amy Malocheecfb4892013-02-06 13:30:36 -08002151 input_set_abs_params(rmi4_data->input_dev,
2152 ABS_MT_POSITION_X, 0,
2153 rmi4_data->sensor_max_x, 0, 0);
2154 input_set_abs_params(rmi4_data->input_dev,
2155 ABS_MT_POSITION_Y, 0,
2156 rmi4_data->sensor_max_y, 0, 0);
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002157 input_set_abs_params(rmi4_data->input_dev,
2158 ABS_PRESSURE, 0, 255, 0, 0);
Amy Malocheecfb4892013-02-06 13:30:36 -08002159#ifdef REPORT_2D_W
2160 input_set_abs_params(rmi4_data->input_dev,
2161 ABS_MT_TOUCH_MAJOR, 0,
2162 MAX_ABS_MT_TOUCH_MAJOR, 0, 0);
2163#endif
2164
2165#ifdef TYPE_B_PROTOCOL
2166 input_mt_init_slots(rmi4_data->input_dev,
2167 rmi4_data->num_of_fingers);
2168#endif
2169
Amy Maloche1a53b612013-01-18 15:25:15 -08002170 i2c_set_clientdata(client, rmi4_data);
2171
Alexandra Chin669d27c2012-12-24 15:42:30 +08002172 f1a = NULL;
Alexandra Chind5591a62013-02-07 12:59:15 -08002173 if (!list_empty(&rmi->support_fn_list)) {
2174 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
2175 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
2176 f1a = fhandler->data;
2177 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08002178 }
2179
2180 if (f1a) {
2181 for (ii = 0; ii < f1a->valid_button_count; ii++) {
2182 set_bit(f1a->button_map[ii],
2183 rmi4_data->input_dev->keybit);
2184 input_set_capability(rmi4_data->input_dev,
2185 EV_KEY, f1a->button_map[ii]);
2186 }
2187 }
2188
2189 retval = input_register_device(rmi4_data->input_dev);
2190 if (retval) {
2191 dev_err(&client->dev,
2192 "%s: Failed to register input device\n",
2193 __func__);
2194 goto err_register_input;
2195 }
2196
2197#ifdef CONFIG_HAS_EARLYSUSPEND
2198 rmi4_data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
2199 rmi4_data->early_suspend.suspend = synaptics_rmi4_early_suspend;
2200 rmi4_data->early_suspend.resume = synaptics_rmi4_late_resume;
2201 register_early_suspend(&rmi4_data->early_suspend);
2202#endif
2203
2204 if (!exp_fn_inited) {
2205 mutex_init(&exp_fn_list_mutex);
2206 INIT_LIST_HEAD(&exp_fn_list);
2207 exp_fn_inited = 1;
2208 }
2209
2210 rmi4_data->det_workqueue =
2211 create_singlethread_workqueue("rmi_det_workqueue");
2212 INIT_DELAYED_WORK(&rmi4_data->det_work,
2213 synaptics_rmi4_detection_work);
2214 queue_delayed_work(rmi4_data->det_workqueue,
2215 &rmi4_data->det_work,
2216 msecs_to_jiffies(EXP_FN_DET_INTERVAL));
2217
Alexandra Chin669d27c2012-12-24 15:42:30 +08002218 rmi4_data->irq = gpio_to_irq(platform_data->irq_gpio);
2219
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002220 retval = request_threaded_irq(rmi4_data->irq, NULL,
2221 synaptics_rmi4_irq, platform_data->irq_flags,
2222 DRIVER_NAME, rmi4_data);
2223 rmi4_data->irq_enabled = true;
2224
Alexandra Chin669d27c2012-12-24 15:42:30 +08002225 if (retval < 0) {
2226 dev_err(&client->dev,
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002227 "%s: Failed to create irq thread\n",
Alexandra Chin669d27c2012-12-24 15:42:30 +08002228 __func__);
2229 goto err_enable_irq;
2230 }
2231
2232 for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
2233 retval = sysfs_create_file(&rmi4_data->input_dev->dev.kobj,
2234 &attrs[attr_count].attr);
2235 if (retval < 0) {
2236 dev_err(&client->dev,
2237 "%s: Failed to create sysfs attributes\n",
2238 __func__);
2239 goto err_sysfs;
2240 }
2241 }
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002242 retval = synaptics_rmi4_irq_enable(rmi4_data, true);
2243 if (retval < 0) {
2244 dev_err(&client->dev,
2245 "%s: Failed to enable attention interrupt\n",
2246 __func__);
2247 goto err_sysfs;
2248 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08002249
2250 return retval;
2251
2252err_sysfs:
2253 for (attr_count--; attr_count >= 0; attr_count--) {
2254 sysfs_remove_file(&rmi4_data->input_dev->dev.kobj,
2255 &attrs[attr_count].attr);
2256 }
2257
2258err_enable_irq:
Amy Malocheecfb4892013-02-06 13:30:36 -08002259 cancel_delayed_work_sync(&rmi4_data->det_work);
2260 flush_workqueue(rmi4_data->det_workqueue);
2261 destroy_workqueue(rmi4_data->det_workqueue);
Alexandra Chin669d27c2012-12-24 15:42:30 +08002262 input_unregister_device(rmi4_data->input_dev);
2263
2264err_register_input:
Alexandra Chind5591a62013-02-07 12:59:15 -08002265 if (!list_empty(&rmi->support_fn_list)) {
2266 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
2267 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
2268 synaptics_rmi4_f1a_kfree(fhandler);
2269 else
2270 kfree(fhandler->data);
2271 kfree(fhandler);
2272 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08002273 }
Amy Malocheecfb4892013-02-06 13:30:36 -08002274err_reset_gpio_dir:
Amy Maloche946da662013-01-18 16:27:11 -08002275 if (gpio_is_valid(platform_data->reset_gpio))
2276 gpio_free(platform_data->reset_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002277err_irq_gpio_dir:
Amy Maloche946da662013-01-18 16:27:11 -08002278 if (gpio_is_valid(platform_data->irq_gpio))
2279 gpio_free(platform_data->irq_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002280err_irq_gpio_req:
Amy Maloche946da662013-01-18 16:27:11 -08002281 synaptics_rmi4_power_on(rmi4_data, false);
2282err_power_device:
2283 synaptics_rmi4_regulator_configure(rmi4_data, false);
2284err_reg_configure:
Alexandra Chin669d27c2012-12-24 15:42:30 +08002285 input_free_device(rmi4_data->input_dev);
2286 rmi4_data->input_dev = NULL;
Alexandra Chin669d27c2012-12-24 15:42:30 +08002287err_input_device:
2288 kfree(rmi4_data);
2289
2290 return retval;
2291}
2292
2293 /**
2294 * synaptics_rmi4_remove()
2295 *
2296 * Called by the kernel when the association with an I2C device of the
2297 * same name is broken (when the driver is unloaded).
2298 *
2299 * This funtion terminates the work queue, stops sensor data acquisition,
2300 * frees the interrupt, unregisters the driver from the input subsystem,
2301 * turns off the power to the sensor, and frees other allocated resources.
2302 */
2303static int __devexit synaptics_rmi4_remove(struct i2c_client *client)
2304{
2305 unsigned char attr_count;
2306 struct synaptics_rmi4_fn *fhandler;
2307 struct synaptics_rmi4_data *rmi4_data = i2c_get_clientdata(client);
2308 struct synaptics_rmi4_device_info *rmi;
Alexandra Chin669d27c2012-12-24 15:42:30 +08002309
2310 rmi = &(rmi4_data->rmi4_mod_info);
2311
2312 cancel_delayed_work_sync(&rmi4_data->det_work);
2313 flush_workqueue(rmi4_data->det_workqueue);
2314 destroy_workqueue(rmi4_data->det_workqueue);
2315
2316 rmi4_data->touch_stopped = true;
2317 wake_up(&rmi4_data->wait);
2318
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002319 free_irq(rmi4_data->irq, rmi4_data);
Alexandra Chin669d27c2012-12-24 15:42:30 +08002320
2321 for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
2322 sysfs_remove_file(&rmi4_data->input_dev->dev.kobj,
2323 &attrs[attr_count].attr);
2324 }
2325
2326 input_unregister_device(rmi4_data->input_dev);
2327
Alexandra Chind5591a62013-02-07 12:59:15 -08002328 if (!list_empty(&rmi->support_fn_list)) {
2329 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
2330 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
2331 synaptics_rmi4_f1a_kfree(fhandler);
2332 else
2333 kfree(fhandler->data);
2334 kfree(fhandler);
2335 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08002336 }
Amy Maloche946da662013-01-18 16:27:11 -08002337
2338 if (gpio_is_valid(rmi4_data->board->reset_gpio))
2339 gpio_free(rmi4_data->board->reset_gpio);
2340 if (gpio_is_valid(rmi4_data->board->irq_gpio))
2341 gpio_free(rmi4_data->board->irq_gpio);
Alexandra Chin669d27c2012-12-24 15:42:30 +08002342
Amy Maloche1a53b612013-01-18 15:25:15 -08002343 synaptics_rmi4_power_on(rmi4_data, false);
2344 synaptics_rmi4_regulator_configure(rmi4_data, false);
2345
Alexandra Chin669d27c2012-12-24 15:42:30 +08002346 kfree(rmi4_data);
2347
2348 return 0;
2349}
2350
2351#ifdef CONFIG_PM
2352 /**
2353 * synaptics_rmi4_sensor_sleep()
2354 *
2355 * Called by synaptics_rmi4_early_suspend() and synaptics_rmi4_suspend().
2356 *
2357 * This function stops finger data acquisition and puts the sensor to sleep.
2358 */
2359static void synaptics_rmi4_sensor_sleep(struct synaptics_rmi4_data *rmi4_data)
2360{
2361 int retval;
2362 unsigned char device_ctrl;
2363
2364 retval = synaptics_rmi4_i2c_read(rmi4_data,
2365 rmi4_data->f01_ctrl_base_addr,
2366 &device_ctrl,
2367 sizeof(device_ctrl));
2368 if (retval < 0) {
2369 dev_err(&(rmi4_data->input_dev->dev),
2370 "%s: Failed to enter sleep mode\n",
2371 __func__);
2372 rmi4_data->sensor_sleep = false;
2373 return;
2374 }
2375
2376 device_ctrl = (device_ctrl & ~MASK_3BIT);
2377 device_ctrl = (device_ctrl | NO_SLEEP_OFF | SENSOR_SLEEP);
2378
2379 retval = synaptics_rmi4_i2c_write(rmi4_data,
2380 rmi4_data->f01_ctrl_base_addr,
2381 &device_ctrl,
2382 sizeof(device_ctrl));
2383 if (retval < 0) {
2384 dev_err(&(rmi4_data->input_dev->dev),
2385 "%s: Failed to enter sleep mode\n",
2386 __func__);
2387 rmi4_data->sensor_sleep = false;
2388 return;
2389 } else {
2390 rmi4_data->sensor_sleep = true;
2391 }
2392
2393 return;
2394}
2395
2396 /**
2397 * synaptics_rmi4_sensor_wake()
2398 *
2399 * Called by synaptics_rmi4_resume() and synaptics_rmi4_late_resume().
2400 *
2401 * This function wakes the sensor from sleep.
2402 */
2403static void synaptics_rmi4_sensor_wake(struct synaptics_rmi4_data *rmi4_data)
2404{
2405 int retval;
2406 unsigned char device_ctrl;
2407
2408 retval = synaptics_rmi4_i2c_read(rmi4_data,
2409 rmi4_data->f01_ctrl_base_addr,
2410 &device_ctrl,
2411 sizeof(device_ctrl));
2412 if (retval < 0) {
2413 dev_err(&(rmi4_data->input_dev->dev),
2414 "%s: Failed to wake from sleep mode\n",
2415 __func__);
2416 rmi4_data->sensor_sleep = true;
2417 return;
2418 }
2419
2420 device_ctrl = (device_ctrl & ~MASK_3BIT);
2421 device_ctrl = (device_ctrl | NO_SLEEP_OFF | NORMAL_OPERATION);
2422
2423 retval = synaptics_rmi4_i2c_write(rmi4_data,
2424 rmi4_data->f01_ctrl_base_addr,
2425 &device_ctrl,
2426 sizeof(device_ctrl));
2427 if (retval < 0) {
2428 dev_err(&(rmi4_data->input_dev->dev),
2429 "%s: Failed to wake from sleep mode\n",
2430 __func__);
2431 rmi4_data->sensor_sleep = true;
2432 return;
2433 } else {
2434 rmi4_data->sensor_sleep = false;
2435 }
2436
2437 return;
2438}
2439
2440#ifdef CONFIG_HAS_EARLYSUSPEND
2441 /**
2442 * synaptics_rmi4_early_suspend()
2443 *
2444 * Called by the kernel during the early suspend phase when the system
2445 * enters suspend.
2446 *
2447 * This function calls synaptics_rmi4_sensor_sleep() to stop finger
2448 * data acquisition and put the sensor to sleep.
2449 */
2450static void synaptics_rmi4_early_suspend(struct early_suspend *h)
2451{
2452 struct synaptics_rmi4_data *rmi4_data =
2453 container_of(h, struct synaptics_rmi4_data,
2454 early_suspend);
2455
2456 rmi4_data->touch_stopped = true;
2457 wake_up(&rmi4_data->wait);
2458 synaptics_rmi4_irq_enable(rmi4_data, false);
2459 synaptics_rmi4_sensor_sleep(rmi4_data);
2460
2461 if (rmi4_data->full_pm_cycle)
2462 synaptics_rmi4_suspend(&(rmi4_data->input_dev->dev));
2463
2464 return;
2465}
2466
2467 /**
2468 * synaptics_rmi4_late_resume()
2469 *
2470 * Called by the kernel during the late resume phase when the system
2471 * wakes up from suspend.
2472 *
2473 * This function goes through the sensor wake process if the system wakes
2474 * up from early suspend (without going into suspend).
2475 */
2476static void synaptics_rmi4_late_resume(struct early_suspend *h)
2477{
2478 struct synaptics_rmi4_data *rmi4_data =
2479 container_of(h, struct synaptics_rmi4_data,
2480 early_suspend);
2481
2482 if (rmi4_data->full_pm_cycle)
2483 synaptics_rmi4_resume(&(rmi4_data->input_dev->dev));
2484
2485 if (rmi4_data->sensor_sleep == true) {
2486 synaptics_rmi4_sensor_wake(rmi4_data);
2487 rmi4_data->touch_stopped = false;
2488 synaptics_rmi4_irq_enable(rmi4_data, true);
2489 }
2490
2491 return;
2492}
2493#endif
2494
2495 /**
2496 * synaptics_rmi4_suspend()
2497 *
2498 * Called by the kernel during the suspend phase when the system
2499 * enters suspend.
2500 *
2501 * This function stops finger data acquisition and puts the sensor to
2502 * sleep (if not already done so during the early suspend phase),
2503 * disables the interrupt, and turns off the power to the sensor.
2504 */
2505static int synaptics_rmi4_suspend(struct device *dev)
2506{
2507 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
Alexandra Chin669d27c2012-12-24 15:42:30 +08002508
2509 if (!rmi4_data->sensor_sleep) {
2510 rmi4_data->touch_stopped = true;
2511 wake_up(&rmi4_data->wait);
2512 synaptics_rmi4_irq_enable(rmi4_data, false);
2513 synaptics_rmi4_sensor_sleep(rmi4_data);
2514 }
2515
Alexandra Chin669d27c2012-12-24 15:42:30 +08002516 return 0;
2517}
2518
2519 /**
2520 * synaptics_rmi4_resume()
2521 *
2522 * Called by the kernel during the resume phase when the system
2523 * wakes up from suspend.
2524 *
2525 * This function turns on the power to the sensor, wakes the sensor
2526 * from sleep, enables the interrupt, and starts finger data
2527 * acquisition.
2528 */
2529static int synaptics_rmi4_resume(struct device *dev)
2530{
2531 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
Alexandra Chin669d27c2012-12-24 15:42:30 +08002532
2533 synaptics_rmi4_sensor_wake(rmi4_data);
2534 rmi4_data->touch_stopped = false;
2535 synaptics_rmi4_irq_enable(rmi4_data, true);
2536
2537 return 0;
2538}
2539
2540static const struct dev_pm_ops synaptics_rmi4_dev_pm_ops = {
2541 .suspend = synaptics_rmi4_suspend,
2542 .resume = synaptics_rmi4_resume,
2543};
2544#endif
2545
2546static const struct i2c_device_id synaptics_rmi4_id_table[] = {
2547 {DRIVER_NAME, 0},
2548 {},
2549};
2550MODULE_DEVICE_TABLE(i2c, synaptics_rmi4_id_table);
2551
Amy Malocheecfb4892013-02-06 13:30:36 -08002552#ifdef CONFIG_OF
2553static struct of_device_id rmi4_match_table[] = {
2554 { .compatible = "synaptics,rmi4",},
2555 { },
2556};
2557#else
2558#define rmi4_match_table NULL
2559#endif
2560
Alexandra Chin669d27c2012-12-24 15:42:30 +08002561static struct i2c_driver synaptics_rmi4_driver = {
2562 .driver = {
2563 .name = DRIVER_NAME,
2564 .owner = THIS_MODULE,
Amy Malocheecfb4892013-02-06 13:30:36 -08002565 .of_match_table = rmi4_match_table,
Alexandra Chin669d27c2012-12-24 15:42:30 +08002566#ifdef CONFIG_PM
2567 .pm = &synaptics_rmi4_dev_pm_ops,
2568#endif
2569 },
2570 .probe = synaptics_rmi4_probe,
2571 .remove = __devexit_p(synaptics_rmi4_remove),
2572 .id_table = synaptics_rmi4_id_table,
2573};
2574
2575 /**
2576 * synaptics_rmi4_init()
2577 *
2578 * Called by the kernel during do_initcalls (if built-in)
2579 * or when the driver is loaded (if a module).
2580 *
2581 * This function registers the driver to the I2C subsystem.
2582 *
2583 */
2584static int __init synaptics_rmi4_init(void)
2585{
2586 return i2c_add_driver(&synaptics_rmi4_driver);
2587}
2588
2589 /**
2590 * synaptics_rmi4_exit()
2591 *
2592 * Called by the kernel when the driver is unloaded.
2593 *
2594 * This funtion unregisters the driver from the I2C subsystem.
2595 *
2596 */
2597static void __exit synaptics_rmi4_exit(void)
2598{
2599 i2c_del_driver(&synaptics_rmi4_driver);
2600}
2601
2602module_init(synaptics_rmi4_init);
2603module_exit(synaptics_rmi4_exit);
2604
2605MODULE_AUTHOR("Synaptics, Inc.");
2606MODULE_DESCRIPTION("Synaptics RMI4 I2C Touch Driver");
2607MODULE_LICENSE("GPL v2");