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