blob: 426c7e76a1854c49f990633ea0ee374c6fbea7dd [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;
Amy Maloche5c7b5d52013-04-29 10:40:41 -07001085 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
Amy Maloche5c7b5d52013-04-29 10:40:41 -07001091 intr_status = kzalloc(rmi4_data->num_of_intr_regs, GFP_KERNEL);
1092 if (!intr_status) {
1093 dev_err(&rmi4_data->i2c_client->dev,
1094 "%s: Failed to alloc memory\n",
1095 __func__);
1096 return -ENOMEM;
1097 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001098 /* Clear interrupts first */
1099 retval = synaptics_rmi4_i2c_read(rmi4_data,
1100 rmi4_data->f01_data_base_addr + 1,
Amy Maloche5c7b5d52013-04-29 10:40:41 -07001101 intr_status,
Alexandra Chin669d27c2012-12-24 15:42:30 +08001102 rmi4_data->num_of_intr_regs);
Amy Maloche5c7b5d52013-04-29 10:40:41 -07001103 kfree(intr_status);
Alexandra Chin669d27c2012-12-24 15:42:30 +08001104 if (retval < 0)
1105 return retval;
1106
Alexandra Chinfb798fa2013-03-26 17:15:38 -07001107 enable_irq(rmi4_data->irq);
Alexandra Chin669d27c2012-12-24 15:42:30 +08001108
1109 rmi4_data->irq_enabled = true;
1110 } else {
1111 if (rmi4_data->irq_enabled) {
1112 disable_irq(rmi4_data->irq);
Alexandra Chin669d27c2012-12-24 15:42:30 +08001113 rmi4_data->irq_enabled = false;
1114 }
1115 }
1116
1117 return retval;
1118}
1119
1120 /**
1121 * synaptics_rmi4_f11_init()
1122 *
1123 * Called by synaptics_rmi4_query_device().
1124 *
1125 * This funtion parses information from the Function 11 registers
1126 * and determines the number of fingers supported, x and y data ranges,
1127 * offset to the associated interrupt status register, interrupt bit
1128 * mask, and gathers finger data acquisition capabilities from the query
1129 * registers.
1130 */
1131static int synaptics_rmi4_f11_init(struct synaptics_rmi4_data *rmi4_data,
1132 struct synaptics_rmi4_fn *fhandler,
1133 struct synaptics_rmi4_fn_desc *fd,
1134 unsigned int intr_count)
1135{
1136 int retval;
1137 unsigned char ii;
1138 unsigned char intr_offset;
1139 unsigned char abs_data_size;
1140 unsigned char abs_data_blk_size;
1141 unsigned char query[F11_STD_QUERY_LEN];
1142 unsigned char control[F11_STD_CTRL_LEN];
1143
1144 fhandler->fn_number = fd->fn_number;
1145 fhandler->num_of_data_sources = fd->intr_src_count;
1146
1147 retval = synaptics_rmi4_i2c_read(rmi4_data,
1148 fhandler->full_addr.query_base,
1149 query,
1150 sizeof(query));
1151 if (retval < 0)
1152 return retval;
1153
1154 /* Maximum number of fingers supported */
1155 if ((query[1] & MASK_3BIT) <= 4)
1156 fhandler->num_of_data_points = (query[1] & MASK_3BIT) + 1;
1157 else if ((query[1] & MASK_3BIT) == 5)
1158 fhandler->num_of_data_points = 10;
1159
1160 rmi4_data->num_of_fingers = fhandler->num_of_data_points;
1161
1162 retval = synaptics_rmi4_i2c_read(rmi4_data,
1163 fhandler->full_addr.ctrl_base,
1164 control,
1165 sizeof(control));
1166 if (retval < 0)
1167 return retval;
1168
1169 /* Maximum x and y */
1170 rmi4_data->sensor_max_x = ((control[6] & MASK_8BIT) << 0) |
1171 ((control[7] & MASK_4BIT) << 8);
1172 rmi4_data->sensor_max_y = ((control[8] & MASK_8BIT) << 0) |
1173 ((control[9] & MASK_4BIT) << 8);
1174 dev_dbg(&rmi4_data->i2c_client->dev,
1175 "%s: Function %02x max x = %d max y = %d\n",
1176 __func__, fhandler->fn_number,
1177 rmi4_data->sensor_max_x,
1178 rmi4_data->sensor_max_y);
1179
1180 fhandler->intr_reg_num = (intr_count + 7) / 8;
1181 if (fhandler->intr_reg_num != 0)
1182 fhandler->intr_reg_num -= 1;
1183
1184 /* Set an enable bit for each data source */
1185 intr_offset = intr_count % 8;
1186 fhandler->intr_mask = 0;
1187 for (ii = intr_offset;
1188 ii < ((fd->intr_src_count & MASK_3BIT) +
1189 intr_offset);
1190 ii++)
1191 fhandler->intr_mask |= 1 << ii;
1192
1193 abs_data_size = query[5] & MASK_2BIT;
1194 abs_data_blk_size = 3 + (2 * (abs_data_size == 0 ? 1 : 0));
1195 fhandler->size_of_data_register_block = abs_data_blk_size;
1196
1197 return retval;
1198}
1199
1200static int synaptics_rmi4_f1a_alloc_mem(struct synaptics_rmi4_data *rmi4_data,
1201 struct synaptics_rmi4_fn *fhandler)
1202{
1203 int retval;
1204 struct synaptics_rmi4_f1a_handle *f1a;
1205
1206 f1a = kzalloc(sizeof(*f1a), GFP_KERNEL);
1207 if (!f1a) {
1208 dev_err(&rmi4_data->i2c_client->dev,
1209 "%s: Failed to alloc mem for function handle\n",
1210 __func__);
1211 return -ENOMEM;
1212 }
1213
1214 fhandler->data = (void *)f1a;
1215
1216 retval = synaptics_rmi4_i2c_read(rmi4_data,
1217 fhandler->full_addr.query_base,
1218 f1a->button_query.data,
1219 sizeof(f1a->button_query.data));
1220 if (retval < 0) {
1221 dev_err(&rmi4_data->i2c_client->dev,
1222 "%s: Failed to read query registers\n",
1223 __func__);
1224 return retval;
1225 }
1226
1227 f1a->button_count = f1a->button_query.max_button_count + 1;
1228 f1a->button_bitmask_size = (f1a->button_count + 7) / 8;
1229
1230 f1a->button_data_buffer = kcalloc(f1a->button_bitmask_size,
1231 sizeof(*(f1a->button_data_buffer)), GFP_KERNEL);
1232 if (!f1a->button_data_buffer) {
1233 dev_err(&rmi4_data->i2c_client->dev,
1234 "%s: Failed to alloc mem for data buffer\n",
1235 __func__);
1236 return -ENOMEM;
1237 }
1238
1239 f1a->button_map = kcalloc(f1a->button_count,
1240 sizeof(*(f1a->button_map)), GFP_KERNEL);
1241 if (!f1a->button_map) {
1242 dev_err(&rmi4_data->i2c_client->dev,
1243 "%s: Failed to alloc mem for button map\n",
1244 __func__);
1245 return -ENOMEM;
1246 }
1247
1248 return 0;
1249}
1250
1251static int synaptics_rmi4_capacitance_button_map(
1252 struct synaptics_rmi4_data *rmi4_data,
1253 struct synaptics_rmi4_fn *fhandler)
1254{
1255 unsigned char ii;
1256 struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
1257 const struct synaptics_rmi4_platform_data *pdata = rmi4_data->board;
1258
1259 if (!pdata->capacitance_button_map) {
1260 dev_err(&rmi4_data->i2c_client->dev,
Alexandra Chind5591a62013-02-07 12:59:15 -08001261 "%s: capacitance_button_map is" \
1262 "NULL in board file\n",
Alexandra Chin669d27c2012-12-24 15:42:30 +08001263 __func__);
1264 return -ENODEV;
1265 } else if (!pdata->capacitance_button_map->map) {
1266 dev_err(&rmi4_data->i2c_client->dev,
1267 "%s: Button map is missing in board file\n",
1268 __func__);
1269 return -ENODEV;
1270 } else {
1271 if (pdata->capacitance_button_map->nbuttons !=
1272 f1a->button_count) {
1273 f1a->valid_button_count = min(f1a->button_count,
1274 pdata->capacitance_button_map->nbuttons);
1275 } else {
1276 f1a->valid_button_count = f1a->button_count;
1277 }
1278
1279 for (ii = 0; ii < f1a->valid_button_count; ii++)
1280 f1a->button_map[ii] =
1281 pdata->capacitance_button_map->map[ii];
1282 }
1283
1284 return 0;
1285}
1286
1287static void synaptics_rmi4_f1a_kfree(struct synaptics_rmi4_fn *fhandler)
1288{
1289 struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
1290
1291 if (f1a) {
1292 kfree(f1a->button_data_buffer);
1293 kfree(f1a->button_map);
1294 kfree(f1a);
1295 fhandler->data = NULL;
1296 }
1297
1298 return;
1299}
1300
1301static int synaptics_rmi4_f1a_init(struct synaptics_rmi4_data *rmi4_data,
1302 struct synaptics_rmi4_fn *fhandler,
1303 struct synaptics_rmi4_fn_desc *fd,
1304 unsigned int intr_count)
1305{
1306 int retval;
1307 unsigned char ii;
1308 unsigned short intr_offset;
1309
1310 fhandler->fn_number = fd->fn_number;
1311 fhandler->num_of_data_sources = fd->intr_src_count;
1312
1313 fhandler->intr_reg_num = (intr_count + 7) / 8;
1314 if (fhandler->intr_reg_num != 0)
1315 fhandler->intr_reg_num -= 1;
1316
1317 /* Set an enable bit for each data source */
1318 intr_offset = intr_count % 8;
1319 fhandler->intr_mask = 0;
1320 for (ii = intr_offset;
1321 ii < ((fd->intr_src_count & MASK_3BIT) +
1322 intr_offset);
1323 ii++)
1324 fhandler->intr_mask |= 1 << ii;
1325
1326 retval = synaptics_rmi4_f1a_alloc_mem(rmi4_data, fhandler);
1327 if (retval < 0)
1328 goto error_exit;
1329
1330 retval = synaptics_rmi4_capacitance_button_map(rmi4_data, fhandler);
1331 if (retval < 0)
1332 goto error_exit;
1333
1334 rmi4_data->button_0d_enabled = 1;
1335
1336 return 0;
1337
1338error_exit:
1339 synaptics_rmi4_f1a_kfree(fhandler);
1340
1341 return retval;
1342}
1343
1344static int synaptics_rmi4_alloc_fh(struct synaptics_rmi4_fn **fhandler,
1345 struct synaptics_rmi4_fn_desc *rmi_fd, int page_number)
1346{
Alexandra Chinfb798fa2013-03-26 17:15:38 -07001347 *fhandler = kzalloc(sizeof(**fhandler), GFP_KERNEL);
Alexandra Chin669d27c2012-12-24 15:42:30 +08001348 if (!(*fhandler))
1349 return -ENOMEM;
1350
1351 (*fhandler)->full_addr.data_base =
1352 (rmi_fd->data_base_addr |
1353 (page_number << 8));
1354 (*fhandler)->full_addr.ctrl_base =
1355 (rmi_fd->ctrl_base_addr |
1356 (page_number << 8));
1357 (*fhandler)->full_addr.cmd_base =
1358 (rmi_fd->cmd_base_addr |
1359 (page_number << 8));
1360 (*fhandler)->full_addr.query_base =
1361 (rmi_fd->query_base_addr |
1362 (page_number << 8));
Alexandra Chinfb798fa2013-03-26 17:15:38 -07001363 (*fhandler)->fn_number = rmi_fd->fn_number;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001364
1365 return 0;
1366}
1367
Alexandra Chind5591a62013-02-07 12:59:15 -08001368
1369 /**
1370 * synaptics_rmi4_query_device_info()
1371 *
1372 * Called by synaptics_rmi4_query_device().
1373 *
1374 */
1375static int synaptics_rmi4_query_device_info(
1376 struct synaptics_rmi4_data *rmi4_data)
1377{
1378 int retval;
1379 unsigned char f01_query[F01_STD_QUERY_LEN];
1380 struct synaptics_rmi4_device_info *rmi = &(rmi4_data->rmi4_mod_info);
1381
1382 retval = synaptics_rmi4_i2c_read(rmi4_data,
1383 rmi4_data->f01_query_base_addr,
1384 f01_query,
1385 sizeof(f01_query));
1386 if (retval < 0)
1387 return retval;
1388
1389 /* RMI Version 4.0 currently supported */
1390 rmi->version_major = 4;
1391 rmi->version_minor = 0;
1392
1393 rmi->manufacturer_id = f01_query[0];
1394 rmi->product_props = f01_query[1];
1395 rmi->product_info[0] = f01_query[2] & MASK_7BIT;
1396 rmi->product_info[1] = f01_query[3] & MASK_7BIT;
1397 rmi->date_code[0] = f01_query[4] & MASK_5BIT;
1398 rmi->date_code[1] = f01_query[5] & MASK_4BIT;
1399 rmi->date_code[2] = f01_query[6] & MASK_5BIT;
1400 rmi->tester_id = ((f01_query[7] & MASK_7BIT) << 8) |
1401 (f01_query[8] & MASK_7BIT);
1402 rmi->serial_number = ((f01_query[9] & MASK_7BIT) << 8) |
1403 (f01_query[10] & MASK_7BIT);
1404 memcpy(rmi->product_id_string, &f01_query[11], 10);
1405
1406 if (rmi->manufacturer_id != 1) {
1407 dev_err(&rmi4_data->i2c_client->dev,
1408 "%s: Non-Synaptics device found, manufacturer ID = %d\n",
1409 __func__, rmi->manufacturer_id);
1410 }
1411
1412 retval = synaptics_rmi4_i2c_read(rmi4_data,
1413 rmi4_data->f01_query_base_addr + F01_BUID_ID_OFFSET,
1414 rmi->build_id,
1415 sizeof(rmi->build_id));
1416 if (retval < 0) {
1417 dev_err(&rmi4_data->i2c_client->dev,
1418 "%s: Failed to read firmware build id (code %d)\n",
1419 __func__, retval);
1420 return retval;
1421 }
Alexandra Chinfb798fa2013-03-26 17:15:38 -07001422 return 0;
Alexandra Chind5591a62013-02-07 12:59:15 -08001423}
1424
Alexandra Chin669d27c2012-12-24 15:42:30 +08001425 /**
1426 * synaptics_rmi4_query_device()
1427 *
1428 * Called by synaptics_rmi4_probe().
1429 *
1430 * This funtion scans the page description table, records the offsets
1431 * to the register types of Function $01, sets up the function handlers
1432 * for Function $11 and Function $12, determines the number of interrupt
1433 * sources from the sensor, adds valid Functions with data inputs to the
1434 * Function linked list, parses information from the query registers of
1435 * Function $01, and enables the interrupt sources from the valid Functions
1436 * with data inputs.
1437 */
1438static int synaptics_rmi4_query_device(struct synaptics_rmi4_data *rmi4_data)
1439{
1440 int retval;
1441 unsigned char ii;
1442 unsigned char page_number;
1443 unsigned char intr_count = 0;
1444 unsigned char data_sources = 0;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001445 unsigned short pdt_entry_addr;
1446 unsigned short intr_addr;
1447 struct synaptics_rmi4_f01_device_status status;
1448 struct synaptics_rmi4_fn_desc rmi_fd;
1449 struct synaptics_rmi4_fn *fhandler;
1450 struct synaptics_rmi4_device_info *rmi;
1451
1452 rmi = &(rmi4_data->rmi4_mod_info);
1453
1454 INIT_LIST_HEAD(&rmi->support_fn_list);
1455
1456 /* Scan the page description tables of the pages to service */
1457 for (page_number = 0; page_number < PAGES_TO_SERVICE; page_number++) {
1458 for (pdt_entry_addr = PDT_START; pdt_entry_addr > PDT_END;
1459 pdt_entry_addr -= PDT_ENTRY_SIZE) {
1460 pdt_entry_addr |= (page_number << 8);
1461
1462 retval = synaptics_rmi4_i2c_read(rmi4_data,
1463 pdt_entry_addr,
1464 (unsigned char *)&rmi_fd,
1465 sizeof(rmi_fd));
1466 if (retval < 0)
1467 return retval;
1468
1469 fhandler = NULL;
1470
1471 if (rmi_fd.fn_number == 0) {
1472 dev_dbg(&rmi4_data->i2c_client->dev,
1473 "%s: Reached end of PDT\n",
1474 __func__);
1475 break;
1476 }
1477
1478 dev_dbg(&rmi4_data->i2c_client->dev,
1479 "%s: F%02x found (page %d)\n",
1480 __func__, rmi_fd.fn_number,
1481 page_number);
1482
1483 switch (rmi_fd.fn_number) {
1484 case SYNAPTICS_RMI4_F01:
1485 rmi4_data->f01_query_base_addr =
1486 rmi_fd.query_base_addr;
1487 rmi4_data->f01_ctrl_base_addr =
1488 rmi_fd.ctrl_base_addr;
1489 rmi4_data->f01_data_base_addr =
1490 rmi_fd.data_base_addr;
1491 rmi4_data->f01_cmd_base_addr =
1492 rmi_fd.cmd_base_addr;
1493
Alexandra Chind5591a62013-02-07 12:59:15 -08001494 retval =
1495 synaptics_rmi4_query_device_info(rmi4_data);
1496 if (retval < 0)
1497 return retval;
1498
Alexandra Chin669d27c2012-12-24 15:42:30 +08001499 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
Alexandra Chin3a747522013-04-15 12:13:38 -07001506 while (status.status_code == STATUS_CRC_IN_PROGRESS) {
1507 msleep(1);
1508 retval = synaptics_rmi4_i2c_read(rmi4_data,
1509 rmi4_data->f01_data_base_addr,
1510 status.data,
1511 sizeof(status.data));
1512 if (retval < 0)
1513 return retval;
1514 }
1515
Alexandra Chin669d27c2012-12-24 15:42:30 +08001516 if (status.flash_prog == 1) {
1517 pr_notice("%s: In flash prog mode, status = 0x%02x\n",
1518 __func__,
1519 status.status_code);
1520 goto flash_prog_mode;
1521 }
Alexandra Chind5591a62013-02-07 12:59:15 -08001522 break;
1523
Alexandra Chin669d27c2012-12-24 15:42:30 +08001524 case SYNAPTICS_RMI4_F11:
1525 if (rmi_fd.intr_src_count == 0)
1526 break;
1527
1528 retval = synaptics_rmi4_alloc_fh(&fhandler,
1529 &rmi_fd, page_number);
1530 if (retval < 0) {
1531 dev_err(&rmi4_data->i2c_client->dev,
1532 "%s: Failed to alloc for F%d\n",
1533 __func__,
1534 rmi_fd.fn_number);
1535 return retval;
1536 }
1537
1538 retval = synaptics_rmi4_f11_init(rmi4_data,
1539 fhandler, &rmi_fd, intr_count);
1540 if (retval < 0)
1541 return retval;
1542 break;
1543
1544 case SYNAPTICS_RMI4_F1A:
1545 if (rmi_fd.intr_src_count == 0)
1546 break;
1547
1548 retval = synaptics_rmi4_alloc_fh(&fhandler,
1549 &rmi_fd, page_number);
1550 if (retval < 0) {
1551 dev_err(&rmi4_data->i2c_client->dev,
1552 "%s: Failed to alloc for F%d\n",
1553 __func__,
1554 rmi_fd.fn_number);
1555 return retval;
1556 }
1557
1558 retval = synaptics_rmi4_f1a_init(rmi4_data,
1559 fhandler, &rmi_fd, intr_count);
1560 if (retval < 0)
1561 return retval;
1562 break;
1563 }
1564
1565 /* Accumulate the interrupt count */
1566 intr_count += (rmi_fd.intr_src_count & MASK_3BIT);
1567
1568 if (fhandler && rmi_fd.intr_src_count) {
1569 list_add_tail(&fhandler->link,
1570 &rmi->support_fn_list);
1571 }
1572 }
1573 }
1574
1575flash_prog_mode:
1576 rmi4_data->num_of_intr_regs = (intr_count + 7) / 8;
1577 dev_dbg(&rmi4_data->i2c_client->dev,
1578 "%s: Number of interrupt registers = %d\n",
1579 __func__, rmi4_data->num_of_intr_regs);
1580
Alexandra Chin669d27c2012-12-24 15:42:30 +08001581 memset(rmi4_data->intr_mask, 0x00, sizeof(rmi4_data->intr_mask));
1582
1583 /*
1584 * Map out the interrupt bit masks for the interrupt sources
1585 * from the registered function handlers.
1586 */
Alexandra Chind5591a62013-02-07 12:59:15 -08001587 if (!list_empty(&rmi->support_fn_list)) {
1588 list_for_each_entry(fhandler, &rmi->support_fn_list, link)
1589 data_sources += fhandler->num_of_data_sources;
1590 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001591 if (data_sources) {
Alexandra Chind5591a62013-02-07 12:59:15 -08001592 if (!list_empty(&rmi->support_fn_list)) {
1593 list_for_each_entry(fhandler,
1594 &rmi->support_fn_list, link) {
1595 if (fhandler->num_of_data_sources) {
1596 rmi4_data->intr_mask[fhandler->intr_reg_num] |=
1597 fhandler->intr_mask;
1598 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001599 }
1600 }
1601 }
1602
1603 /* Enable the interrupt sources */
1604 for (ii = 0; ii < rmi4_data->num_of_intr_regs; ii++) {
1605 if (rmi4_data->intr_mask[ii] != 0x00) {
1606 dev_dbg(&rmi4_data->i2c_client->dev,
1607 "%s: Interrupt enable mask %d = 0x%02x\n",
1608 __func__, ii, rmi4_data->intr_mask[ii]);
1609 intr_addr = rmi4_data->f01_ctrl_base_addr + 1 + ii;
1610 retval = synaptics_rmi4_i2c_write(rmi4_data,
1611 intr_addr,
1612 &(rmi4_data->intr_mask[ii]),
1613 sizeof(rmi4_data->intr_mask[ii]));
1614 if (retval < 0)
1615 return retval;
1616 }
1617 }
1618
1619 return 0;
1620}
1621
Alexandra Chinc556cf02013-03-19 17:46:05 -07001622static int synaptics_rmi4_reset_command(struct synaptics_rmi4_data *rmi4_data)
Alexandra Chin669d27c2012-12-24 15:42:30 +08001623{
1624 int retval;
Alexandra Chinc556cf02013-03-19 17:46:05 -07001625 int page_number;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001626 unsigned char command = 0x01;
Alexandra Chinc556cf02013-03-19 17:46:05 -07001627 unsigned short pdt_entry_addr;
1628 struct synaptics_rmi4_fn_desc rmi_fd;
1629 bool done = false;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001630
Alexandra Chinc556cf02013-03-19 17:46:05 -07001631 /* Scan the page description tables of the pages to service */
1632 for (page_number = 0; page_number < PAGES_TO_SERVICE; page_number++) {
1633 for (pdt_entry_addr = PDT_START; pdt_entry_addr > PDT_END;
1634 pdt_entry_addr -= PDT_ENTRY_SIZE) {
1635 retval = synaptics_rmi4_i2c_read(rmi4_data,
1636 pdt_entry_addr,
1637 (unsigned char *)&rmi_fd,
1638 sizeof(rmi_fd));
1639 if (retval < 0)
1640 return retval;
1641
1642 if (rmi_fd.fn_number == 0)
1643 break;
1644
1645 switch (rmi_fd.fn_number) {
1646 case SYNAPTICS_RMI4_F01:
1647 rmi4_data->f01_cmd_base_addr =
1648 rmi_fd.cmd_base_addr;
1649 done = true;
1650 break;
1651 }
1652 }
1653 if (done) {
1654 dev_info(&rmi4_data->i2c_client->dev,
1655 "%s: Find F01 in page description table 0x%x\n",
1656 __func__, rmi4_data->f01_cmd_base_addr);
1657 break;
1658 }
1659 }
1660
1661 if (!done) {
1662 dev_err(&rmi4_data->i2c_client->dev,
1663 "%s: Cannot find F01 in page description table\n",
1664 __func__);
1665 return -EINVAL;
1666 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001667
1668 retval = synaptics_rmi4_i2c_write(rmi4_data,
1669 rmi4_data->f01_cmd_base_addr,
1670 &command,
1671 sizeof(command));
1672 if (retval < 0) {
1673 dev_err(&rmi4_data->i2c_client->dev,
1674 "%s: Failed to issue reset command, error = %d\n",
1675 __func__, retval);
1676 return retval;
1677 }
1678
Alexandra Chin3a747522013-04-15 12:13:38 -07001679 msleep(RESET_DELAY);
Alexandra Chinc556cf02013-03-19 17:46:05 -07001680 return retval;
1681};
1682
1683static int synaptics_rmi4_reset_device(struct synaptics_rmi4_data *rmi4_data)
1684{
1685 int retval;
1686 struct synaptics_rmi4_fn *fhandler;
1687 struct synaptics_rmi4_device_info *rmi;
1688
1689 rmi = &(rmi4_data->rmi4_mod_info);
1690
1691 retval = synaptics_rmi4_reset_command(rmi4_data);
1692 if (retval < 0) {
1693 dev_err(&rmi4_data->i2c_client->dev,
1694 "%s: Failed to send command reset\n",
1695 __func__);
1696 return retval;
1697 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001698
Alexandra Chind5591a62013-02-07 12:59:15 -08001699 if (!list_empty(&rmi->support_fn_list)) {
1700 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
1701 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
1702 synaptics_rmi4_f1a_kfree(fhandler);
1703 else
1704 kfree(fhandler->data);
1705 kfree(fhandler);
1706 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001707 }
1708
1709 retval = synaptics_rmi4_query_device(rmi4_data);
1710 if (retval < 0) {
1711 dev_err(&rmi4_data->i2c_client->dev,
1712 "%s: Failed to query device\n",
1713 __func__);
1714 return retval;
1715 }
1716
1717 return 0;
1718}
1719
1720/**
1721* synaptics_rmi4_detection_work()
1722*
1723* Called by the kernel at the scheduled time.
1724*
1725* This function is a self-rearming work thread that checks for the
1726* insertion and removal of other expansion Function modules such as
1727* rmi_dev and calls their initialization and removal callback functions
1728* accordingly.
1729*/
1730static void synaptics_rmi4_detection_work(struct work_struct *work)
1731{
1732 struct synaptics_rmi4_exp_fn *exp_fhandler, *next_list_entry;
1733 struct synaptics_rmi4_data *rmi4_data =
1734 container_of(work, struct synaptics_rmi4_data,
1735 det_work.work);
1736
Alexandra Chin669d27c2012-12-24 15:42:30 +08001737 mutex_lock(&exp_fn_list_mutex);
1738 if (!list_empty(&exp_fn_list)) {
1739 list_for_each_entry_safe(exp_fhandler,
1740 next_list_entry,
1741 &exp_fn_list,
1742 link) {
1743 if ((exp_fhandler->func_init != NULL) &&
1744 (exp_fhandler->inserted == false)) {
1745 exp_fhandler->func_init(rmi4_data);
1746 exp_fhandler->inserted = true;
1747 } else if ((exp_fhandler->func_init == NULL) &&
1748 (exp_fhandler->inserted == true)) {
1749 exp_fhandler->func_remove(rmi4_data);
1750 list_del(&exp_fhandler->link);
1751 kfree(exp_fhandler);
1752 }
1753 }
1754 }
1755 mutex_unlock(&exp_fn_list_mutex);
1756
1757 return;
1758}
1759
1760/**
1761* synaptics_rmi4_new_function()
1762*
1763* Called by other expansion Function modules in their module init and
1764* module exit functions.
1765*
1766* This function is used by other expansion Function modules such as
1767* rmi_dev to register themselves with the driver by providing their
1768* initialization and removal callback function pointers so that they
1769* can be inserted or removed dynamically at module init and exit times,
1770* respectively.
1771*/
1772void synaptics_rmi4_new_function(enum exp_fn fn_type, bool insert,
1773 int (*func_init)(struct synaptics_rmi4_data *rmi4_data),
1774 void (*func_remove)(struct synaptics_rmi4_data *rmi4_data),
1775 void (*func_attn)(struct synaptics_rmi4_data *rmi4_data,
1776 unsigned char intr_mask))
1777{
1778 struct synaptics_rmi4_exp_fn *exp_fhandler;
1779
1780 if (!exp_fn_inited) {
1781 mutex_init(&exp_fn_list_mutex);
1782 INIT_LIST_HEAD(&exp_fn_list);
1783 exp_fn_inited = 1;
1784 }
1785
1786 mutex_lock(&exp_fn_list_mutex);
1787 if (insert) {
1788 exp_fhandler = kzalloc(sizeof(*exp_fhandler), GFP_KERNEL);
1789 if (!exp_fhandler) {
1790 pr_err("%s: Failed to alloc mem for expansion function\n",
1791 __func__);
1792 goto exit;
1793 }
1794 exp_fhandler->fn_type = fn_type;
1795 exp_fhandler->func_init = func_init;
1796 exp_fhandler->func_attn = func_attn;
1797 exp_fhandler->func_remove = func_remove;
1798 exp_fhandler->inserted = false;
1799 list_add_tail(&exp_fhandler->link, &exp_fn_list);
1800 } else {
Alexandra Chind5591a62013-02-07 12:59:15 -08001801 if (!list_empty(&exp_fn_list)) {
1802 list_for_each_entry(exp_fhandler, &exp_fn_list, link) {
1803 if (exp_fhandler->func_init == func_init) {
1804 exp_fhandler->inserted = false;
1805 exp_fhandler->func_init = NULL;
1806 exp_fhandler->func_attn = NULL;
1807 goto exit;
1808 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001809 }
1810 }
1811 }
1812
1813exit:
1814 mutex_unlock(&exp_fn_list_mutex);
1815
1816 return;
1817}
1818EXPORT_SYMBOL(synaptics_rmi4_new_function);
1819
Amy Maloche1a53b612013-01-18 15:25:15 -08001820
1821static int reg_set_optimum_mode_check(struct regulator *reg, int load_uA)
1822{
1823 return (regulator_count_voltages(reg) > 0) ?
1824 regulator_set_optimum_mode(reg, load_uA) : 0;
1825}
1826
1827static int synaptics_rmi4_regulator_configure(struct synaptics_rmi4_data
1828 *rmi4_data, bool on)
1829{
1830 int retval;
1831
1832 if (on == false)
1833 goto hw_shutdown;
1834
1835 if (rmi4_data->board->regulator_en) {
1836 rmi4_data->vdd = regulator_get(&rmi4_data->i2c_client->dev,
1837 "vdd");
1838 if (IS_ERR(rmi4_data->vdd)) {
1839 dev_err(&rmi4_data->i2c_client->dev,
1840 "%s: Failed to get vdd regulator\n",
1841 __func__);
1842 return PTR_ERR(rmi4_data->vdd);
1843 }
1844
1845 if (regulator_count_voltages(rmi4_data->vdd) > 0) {
1846 retval = regulator_set_voltage(rmi4_data->vdd,
1847 RMI4_VTG_MIN_UV, RMI4_VTG_MAX_UV);
1848 if (retval) {
1849 dev_err(&rmi4_data->i2c_client->dev,
Amy Maloche827e8b02013-02-27 15:25:14 -08001850 "regulator set_vtg failed retval =%d\n",
Amy Maloche1a53b612013-01-18 15:25:15 -08001851 retval);
1852 goto err_set_vtg_vdd;
1853 }
1854 }
1855 }
1856
1857 if (rmi4_data->board->i2c_pull_up) {
1858 rmi4_data->vcc_i2c = regulator_get(&rmi4_data->i2c_client->dev,
1859 "vcc_i2c");
1860 if (IS_ERR(rmi4_data->vcc_i2c)) {
1861 dev_err(&rmi4_data->i2c_client->dev,
1862 "%s: Failed to get i2c regulator\n",
1863 __func__);
1864 retval = PTR_ERR(rmi4_data->vcc_i2c);
1865 goto err_get_vtg_i2c;
1866 }
1867
1868 if (regulator_count_voltages(rmi4_data->vcc_i2c) > 0) {
1869 retval = regulator_set_voltage(rmi4_data->vcc_i2c,
1870 RMI4_I2C_VTG_MIN_UV, RMI4_I2C_VTG_MAX_UV);
1871 if (retval) {
1872 dev_err(&rmi4_data->i2c_client->dev,
Amy Maloche827e8b02013-02-27 15:25:14 -08001873 "reg set i2c vtg failed retval =%d\n",
Amy Maloche1a53b612013-01-18 15:25:15 -08001874 retval);
1875 goto err_set_vtg_i2c;
1876 }
1877 }
1878 }
Amy Malocheecfb4892013-02-06 13:30:36 -08001879 return 0;
Amy Maloche1a53b612013-01-18 15:25:15 -08001880
1881err_set_vtg_i2c:
1882 if (rmi4_data->board->i2c_pull_up)
1883 regulator_put(rmi4_data->vcc_i2c);
1884err_get_vtg_i2c:
1885 if (rmi4_data->board->regulator_en)
1886 if (regulator_count_voltages(rmi4_data->vdd) > 0)
1887 regulator_set_voltage(rmi4_data->vdd, 0,
1888 RMI4_VTG_MAX_UV);
1889err_set_vtg_vdd:
1890 if (rmi4_data->board->regulator_en)
1891 regulator_put(rmi4_data->vdd);
1892 return retval;
1893
1894hw_shutdown:
1895 if (rmi4_data->board->regulator_en) {
1896 if (regulator_count_voltages(rmi4_data->vdd) > 0)
1897 regulator_set_voltage(rmi4_data->vdd, 0,
1898 RMI4_VTG_MAX_UV);
1899 regulator_put(rmi4_data->vdd);
1900 }
1901 if (rmi4_data->board->i2c_pull_up) {
1902 if (regulator_count_voltages(rmi4_data->vcc_i2c) > 0)
1903 regulator_set_voltage(rmi4_data->vcc_i2c, 0,
1904 RMI4_I2C_VTG_MAX_UV);
1905 regulator_put(rmi4_data->vcc_i2c);
1906 }
1907 return 0;
1908};
1909
1910static int synaptics_rmi4_power_on(struct synaptics_rmi4_data *rmi4_data,
1911 bool on) {
1912 int retval;
1913
1914 if (on == false)
1915 goto power_off;
1916
1917 if (rmi4_data->board->regulator_en) {
1918 retval = reg_set_optimum_mode_check(rmi4_data->vdd,
1919 RMI4_ACTIVE_LOAD_UA);
1920 if (retval < 0) {
1921 dev_err(&rmi4_data->i2c_client->dev,
1922 "Regulator vdd set_opt failed rc=%d\n",
1923 retval);
1924 return retval;
1925 }
1926
1927 retval = regulator_enable(rmi4_data->vdd);
1928 if (retval) {
1929 dev_err(&rmi4_data->i2c_client->dev,
1930 "Regulator vdd enable failed rc=%d\n",
1931 retval);
1932 goto error_reg_en_vdd;
1933 }
1934 }
1935
1936 if (rmi4_data->board->i2c_pull_up) {
1937 retval = reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
1938 RMI4_I2C_LOAD_UA);
1939 if (retval < 0) {
1940 dev_err(&rmi4_data->i2c_client->dev,
1941 "Regulator vcc_i2c set_opt failed rc=%d\n",
1942 retval);
1943 goto error_reg_opt_i2c;
1944 }
1945
1946 retval = regulator_enable(rmi4_data->vcc_i2c);
1947 if (retval) {
1948 dev_err(&rmi4_data->i2c_client->dev,
1949 "Regulator vcc_i2c enable failed rc=%d\n",
1950 retval);
1951 goto error_reg_en_vcc_i2c;
1952 }
1953 }
1954 return 0;
1955
1956error_reg_en_vcc_i2c:
1957 if (rmi4_data->board->i2c_pull_up)
1958 reg_set_optimum_mode_check(rmi4_data->vdd, 0);
1959error_reg_opt_i2c:
1960 if (rmi4_data->board->regulator_en)
1961 regulator_disable(rmi4_data->vdd);
1962error_reg_en_vdd:
1963 if (rmi4_data->board->regulator_en)
1964 reg_set_optimum_mode_check(rmi4_data->vdd, 0);
1965 return retval;
1966
1967power_off:
1968 if (rmi4_data->board->regulator_en) {
1969 reg_set_optimum_mode_check(rmi4_data->vdd, 0);
1970 regulator_disable(rmi4_data->vdd);
1971 }
1972 if (rmi4_data->board->i2c_pull_up) {
1973 reg_set_optimum_mode_check(rmi4_data->vcc_i2c, 0);
1974 regulator_disable(rmi4_data->vcc_i2c);
1975 }
1976 return 0;
1977}
1978
Alexandra Chin669d27c2012-12-24 15:42:30 +08001979 /**
1980 * synaptics_rmi4_probe()
1981 *
1982 * Called by the kernel when an association with an I2C device of the
1983 * same name is made (after doing i2c_add_driver).
1984 *
1985 * This funtion allocates and initializes the resources for the driver
1986 * as an input driver, turns on the power to the sensor, queries the
1987 * sensor for its supported Functions and characteristics, registers
1988 * the driver to the input subsystem, sets up the interrupt, handles
1989 * the registration of the early_suspend and late_resume functions,
1990 * and creates a work queue for detection of other expansion Function
1991 * modules.
1992 */
1993static int __devinit synaptics_rmi4_probe(struct i2c_client *client,
1994 const struct i2c_device_id *dev_id)
1995{
Amy Maloche1a53b612013-01-18 15:25:15 -08001996 int retval = 0;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001997 unsigned char ii;
1998 unsigned char attr_count;
1999 struct synaptics_rmi4_f1a_handle *f1a;
2000 struct synaptics_rmi4_fn *fhandler;
2001 struct synaptics_rmi4_data *rmi4_data;
2002 struct synaptics_rmi4_device_info *rmi;
Amy Malocheecfb4892013-02-06 13:30:36 -08002003 struct synaptics_rmi4_platform_data *platform_data =
Alexandra Chin669d27c2012-12-24 15:42:30 +08002004 client->dev.platform_data;
2005
2006 if (!i2c_check_functionality(client->adapter,
2007 I2C_FUNC_SMBUS_BYTE_DATA)) {
2008 dev_err(&client->dev,
2009 "%s: SMBus byte data not supported\n",
2010 __func__);
2011 return -EIO;
2012 }
2013
Amy Malocheecfb4892013-02-06 13:30:36 -08002014 if (client->dev.of_node) {
2015 platform_data = devm_kzalloc(&client->dev,
2016 sizeof(*platform_data),
2017 GFP_KERNEL);
2018 if (!platform_data) {
2019 dev_err(&client->dev, "Failed to allocate memory\n");
2020 return -ENOMEM;
2021 }
2022
2023 retval = synaptics_rmi4_parse_dt(&client->dev, platform_data);
2024 if (retval)
2025 return retval;
2026 } else {
2027 platform_data = client->dev.platform_data;
2028 }
2029
Alexandra Chin669d27c2012-12-24 15:42:30 +08002030 if (!platform_data) {
2031 dev_err(&client->dev,
2032 "%s: No platform data found\n",
2033 __func__);
2034 return -EINVAL;
2035 }
2036
2037 rmi4_data = kzalloc(sizeof(*rmi4_data) * 2, GFP_KERNEL);
2038 if (!rmi4_data) {
2039 dev_err(&client->dev,
2040 "%s: Failed to alloc mem for rmi4_data\n",
2041 __func__);
2042 return -ENOMEM;
2043 }
2044
2045 rmi = &(rmi4_data->rmi4_mod_info);
2046
2047 rmi4_data->input_dev = input_allocate_device();
2048 if (rmi4_data->input_dev == NULL) {
2049 dev_err(&client->dev,
2050 "%s: Failed to allocate input device\n",
2051 __func__);
2052 retval = -ENOMEM;
2053 goto err_input_device;
2054 }
Alexandra Chind5591a62013-02-07 12:59:15 -08002055
Alexandra Chin669d27c2012-12-24 15:42:30 +08002056 rmi4_data->i2c_client = client;
2057 rmi4_data->current_page = MASK_8BIT;
2058 rmi4_data->board = platform_data;
2059 rmi4_data->touch_stopped = false;
2060 rmi4_data->sensor_sleep = false;
2061 rmi4_data->irq_enabled = false;
2062
2063 rmi4_data->i2c_read = synaptics_rmi4_i2c_read;
2064 rmi4_data->i2c_write = synaptics_rmi4_i2c_write;
2065 rmi4_data->irq_enable = synaptics_rmi4_irq_enable;
2066 rmi4_data->reset_device = synaptics_rmi4_reset_device;
2067
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002068 rmi4_data->flip_x = rmi4_data->board->x_flip;
2069 rmi4_data->flip_y = rmi4_data->board->y_flip;
2070
Amy Malochecb835832013-03-26 18:06:05 -07002071 rmi4_data->fw_image_name = rmi4_data->board->fw_image_name;
2072
Alexandra Chin669d27c2012-12-24 15:42:30 +08002073 rmi4_data->input_dev->name = DRIVER_NAME;
2074 rmi4_data->input_dev->phys = INPUT_PHYS_NAME;
2075 rmi4_data->input_dev->id.bustype = BUS_I2C;
Alexandra Chin5d2999d2013-02-22 15:09:29 -08002076 rmi4_data->input_dev->id.product = SYNAPTICS_DSX_DRIVER_PRODUCT;
2077 rmi4_data->input_dev->id.version = SYNAPTICS_DSX_DRIVER_VERSION;
Alexandra Chin669d27c2012-12-24 15:42:30 +08002078 rmi4_data->input_dev->dev.parent = &client->dev;
2079 input_set_drvdata(rmi4_data->input_dev, rmi4_data);
2080
2081 set_bit(EV_SYN, rmi4_data->input_dev->evbit);
2082 set_bit(EV_KEY, rmi4_data->input_dev->evbit);
2083 set_bit(EV_ABS, rmi4_data->input_dev->evbit);
Alexandra Chind5591a62013-02-07 12:59:15 -08002084 set_bit(BTN_TOUCH, rmi4_data->input_dev->keybit);
2085 set_bit(BTN_TOOL_FINGER, rmi4_data->input_dev->keybit);
Alexandra Chin669d27c2012-12-24 15:42:30 +08002086
2087#ifdef INPUT_PROP_DIRECT
2088 set_bit(INPUT_PROP_DIRECT, rmi4_data->input_dev->propbit);
2089#endif
2090
Amy Maloche1a53b612013-01-18 15:25:15 -08002091 retval = synaptics_rmi4_regulator_configure(rmi4_data, true);
2092 if (retval < 0) {
2093 dev_err(&client->dev, "Failed to configure regulators\n");
Amy Maloche946da662013-01-18 16:27:11 -08002094 goto err_reg_configure;
Amy Maloche1a53b612013-01-18 15:25:15 -08002095 }
2096
2097 retval = synaptics_rmi4_power_on(rmi4_data, true);
2098 if (retval < 0) {
2099 dev_err(&client->dev, "Failed to power on\n");
Amy Maloche946da662013-01-18 16:27:11 -08002100 goto err_power_device;
Amy Maloche1a53b612013-01-18 15:25:15 -08002101 }
2102
Amy Maloche946da662013-01-18 16:27:11 -08002103 if (gpio_is_valid(platform_data->irq_gpio)) {
2104 /* configure touchscreen irq gpio */
2105 retval = gpio_request(platform_data->irq_gpio, "rmi4_irq_gpio");
2106 if (retval) {
2107 dev_err(&client->dev, "unable to request gpio [%d]\n",
2108 platform_data->irq_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002109 goto err_irq_gpio_req;
Amy Maloche946da662013-01-18 16:27:11 -08002110 }
2111 retval = gpio_direction_input(platform_data->irq_gpio);
2112 if (retval) {
2113 dev_err(&client->dev,
2114 "unable to set direction for gpio [%d]\n",
2115 platform_data->irq_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002116 goto err_irq_gpio_dir;
Amy Maloche946da662013-01-18 16:27:11 -08002117 }
2118 } else {
2119 dev_err(&client->dev, "irq gpio not provided\n");
Amy Malocheecfb4892013-02-06 13:30:36 -08002120 goto err_irq_gpio_req;
Amy Maloche946da662013-01-18 16:27:11 -08002121 }
2122
2123 if (gpio_is_valid(platform_data->reset_gpio)) {
2124 /* configure touchscreen reset out gpio */
2125 retval = gpio_request(platform_data->reset_gpio,
2126 "rmi4_reset_gpio");
2127 if (retval) {
2128 dev_err(&client->dev, "unable to request gpio [%d]\n",
2129 platform_data->reset_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002130 goto err_irq_gpio_dir;
Amy Maloche946da662013-01-18 16:27:11 -08002131 }
2132
2133 retval = gpio_direction_output(platform_data->reset_gpio, 1);
2134 if (retval) {
2135 dev_err(&client->dev,
2136 "unable to set direction for gpio [%d]\n",
2137 platform_data->reset_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002138 goto err_reset_gpio_dir;
Amy Maloche946da662013-01-18 16:27:11 -08002139 }
2140
2141 gpio_set_value(platform_data->reset_gpio, 0);
2142 usleep(RMI4_GPIO_SLEEP_LOW_US);
2143 gpio_set_value(platform_data->reset_gpio, 1);
Alexandra Chin3a747522013-04-15 12:13:38 -07002144 msleep(RESET_DELAY);
Alexandra Chinc556cf02013-03-19 17:46:05 -07002145 } else
2146 synaptics_rmi4_reset_command(rmi4_data);
Amy Maloche946da662013-01-18 16:27:11 -08002147
2148
Amy Maloche1a53b612013-01-18 15:25:15 -08002149 init_waitqueue_head(&rmi4_data->wait);
2150 mutex_init(&(rmi4_data->rmi4_io_ctrl_mutex));
2151
2152 retval = synaptics_rmi4_query_device(rmi4_data);
2153 if (retval < 0) {
2154 dev_err(&client->dev,
2155 "%s: Failed to query device\n",
2156 __func__);
Amy Malocheecfb4892013-02-06 13:30:36 -08002157 goto err_reset_gpio_dir;
Amy Maloche1a53b612013-01-18 15:25:15 -08002158 }
2159
Amy Malocheecfb4892013-02-06 13:30:36 -08002160 input_set_abs_params(rmi4_data->input_dev,
2161 ABS_MT_POSITION_X, 0,
2162 rmi4_data->sensor_max_x, 0, 0);
2163 input_set_abs_params(rmi4_data->input_dev,
2164 ABS_MT_POSITION_Y, 0,
2165 rmi4_data->sensor_max_y, 0, 0);
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002166 input_set_abs_params(rmi4_data->input_dev,
2167 ABS_PRESSURE, 0, 255, 0, 0);
Amy Malocheecfb4892013-02-06 13:30:36 -08002168#ifdef REPORT_2D_W
2169 input_set_abs_params(rmi4_data->input_dev,
2170 ABS_MT_TOUCH_MAJOR, 0,
2171 MAX_ABS_MT_TOUCH_MAJOR, 0, 0);
2172#endif
2173
2174#ifdef TYPE_B_PROTOCOL
2175 input_mt_init_slots(rmi4_data->input_dev,
2176 rmi4_data->num_of_fingers);
2177#endif
2178
Amy Maloche1a53b612013-01-18 15:25:15 -08002179 i2c_set_clientdata(client, rmi4_data);
2180
Alexandra Chin669d27c2012-12-24 15:42:30 +08002181 f1a = NULL;
Alexandra Chind5591a62013-02-07 12:59:15 -08002182 if (!list_empty(&rmi->support_fn_list)) {
2183 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
2184 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
2185 f1a = fhandler->data;
2186 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08002187 }
2188
2189 if (f1a) {
2190 for (ii = 0; ii < f1a->valid_button_count; ii++) {
2191 set_bit(f1a->button_map[ii],
2192 rmi4_data->input_dev->keybit);
2193 input_set_capability(rmi4_data->input_dev,
2194 EV_KEY, f1a->button_map[ii]);
2195 }
2196 }
2197
2198 retval = input_register_device(rmi4_data->input_dev);
2199 if (retval) {
2200 dev_err(&client->dev,
2201 "%s: Failed to register input device\n",
2202 __func__);
2203 goto err_register_input;
2204 }
2205
2206#ifdef CONFIG_HAS_EARLYSUSPEND
2207 rmi4_data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
2208 rmi4_data->early_suspend.suspend = synaptics_rmi4_early_suspend;
2209 rmi4_data->early_suspend.resume = synaptics_rmi4_late_resume;
2210 register_early_suspend(&rmi4_data->early_suspend);
2211#endif
2212
2213 if (!exp_fn_inited) {
2214 mutex_init(&exp_fn_list_mutex);
2215 INIT_LIST_HEAD(&exp_fn_list);
2216 exp_fn_inited = 1;
2217 }
2218
2219 rmi4_data->det_workqueue =
2220 create_singlethread_workqueue("rmi_det_workqueue");
2221 INIT_DELAYED_WORK(&rmi4_data->det_work,
2222 synaptics_rmi4_detection_work);
2223 queue_delayed_work(rmi4_data->det_workqueue,
2224 &rmi4_data->det_work,
2225 msecs_to_jiffies(EXP_FN_DET_INTERVAL));
2226
Alexandra Chin669d27c2012-12-24 15:42:30 +08002227 rmi4_data->irq = gpio_to_irq(platform_data->irq_gpio);
2228
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002229 retval = request_threaded_irq(rmi4_data->irq, NULL,
2230 synaptics_rmi4_irq, platform_data->irq_flags,
2231 DRIVER_NAME, rmi4_data);
2232 rmi4_data->irq_enabled = true;
2233
Alexandra Chin669d27c2012-12-24 15:42:30 +08002234 if (retval < 0) {
2235 dev_err(&client->dev,
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002236 "%s: Failed to create irq thread\n",
Alexandra Chin669d27c2012-12-24 15:42:30 +08002237 __func__);
2238 goto err_enable_irq;
2239 }
2240
2241 for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
2242 retval = sysfs_create_file(&rmi4_data->input_dev->dev.kobj,
2243 &attrs[attr_count].attr);
2244 if (retval < 0) {
2245 dev_err(&client->dev,
2246 "%s: Failed to create sysfs attributes\n",
2247 __func__);
2248 goto err_sysfs;
2249 }
2250 }
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002251 retval = synaptics_rmi4_irq_enable(rmi4_data, true);
2252 if (retval < 0) {
2253 dev_err(&client->dev,
2254 "%s: Failed to enable attention interrupt\n",
2255 __func__);
2256 goto err_sysfs;
2257 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08002258
2259 return retval;
2260
2261err_sysfs:
2262 for (attr_count--; attr_count >= 0; attr_count--) {
2263 sysfs_remove_file(&rmi4_data->input_dev->dev.kobj,
2264 &attrs[attr_count].attr);
2265 }
2266
2267err_enable_irq:
Amy Malocheecfb4892013-02-06 13:30:36 -08002268 cancel_delayed_work_sync(&rmi4_data->det_work);
2269 flush_workqueue(rmi4_data->det_workqueue);
2270 destroy_workqueue(rmi4_data->det_workqueue);
Alexandra Chin669d27c2012-12-24 15:42:30 +08002271 input_unregister_device(rmi4_data->input_dev);
2272
2273err_register_input:
Alexandra Chind5591a62013-02-07 12:59:15 -08002274 if (!list_empty(&rmi->support_fn_list)) {
2275 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
2276 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
2277 synaptics_rmi4_f1a_kfree(fhandler);
2278 else
2279 kfree(fhandler->data);
2280 kfree(fhandler);
2281 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08002282 }
Amy Malocheecfb4892013-02-06 13:30:36 -08002283err_reset_gpio_dir:
Amy Maloche946da662013-01-18 16:27:11 -08002284 if (gpio_is_valid(platform_data->reset_gpio))
2285 gpio_free(platform_data->reset_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002286err_irq_gpio_dir:
Amy Maloche946da662013-01-18 16:27:11 -08002287 if (gpio_is_valid(platform_data->irq_gpio))
2288 gpio_free(platform_data->irq_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002289err_irq_gpio_req:
Amy Maloche946da662013-01-18 16:27:11 -08002290 synaptics_rmi4_power_on(rmi4_data, false);
2291err_power_device:
2292 synaptics_rmi4_regulator_configure(rmi4_data, false);
2293err_reg_configure:
Alexandra Chin669d27c2012-12-24 15:42:30 +08002294 input_free_device(rmi4_data->input_dev);
2295 rmi4_data->input_dev = NULL;
Alexandra Chin669d27c2012-12-24 15:42:30 +08002296err_input_device:
2297 kfree(rmi4_data);
2298
2299 return retval;
2300}
2301
2302 /**
2303 * synaptics_rmi4_remove()
2304 *
2305 * Called by the kernel when the association with an I2C device of the
2306 * same name is broken (when the driver is unloaded).
2307 *
2308 * This funtion terminates the work queue, stops sensor data acquisition,
2309 * frees the interrupt, unregisters the driver from the input subsystem,
2310 * turns off the power to the sensor, and frees other allocated resources.
2311 */
2312static int __devexit synaptics_rmi4_remove(struct i2c_client *client)
2313{
2314 unsigned char attr_count;
2315 struct synaptics_rmi4_fn *fhandler;
2316 struct synaptics_rmi4_data *rmi4_data = i2c_get_clientdata(client);
2317 struct synaptics_rmi4_device_info *rmi;
Alexandra Chin669d27c2012-12-24 15:42:30 +08002318
2319 rmi = &(rmi4_data->rmi4_mod_info);
2320
2321 cancel_delayed_work_sync(&rmi4_data->det_work);
2322 flush_workqueue(rmi4_data->det_workqueue);
2323 destroy_workqueue(rmi4_data->det_workqueue);
2324
2325 rmi4_data->touch_stopped = true;
2326 wake_up(&rmi4_data->wait);
2327
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002328 free_irq(rmi4_data->irq, rmi4_data);
Alexandra Chin669d27c2012-12-24 15:42:30 +08002329
2330 for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
2331 sysfs_remove_file(&rmi4_data->input_dev->dev.kobj,
2332 &attrs[attr_count].attr);
2333 }
2334
2335 input_unregister_device(rmi4_data->input_dev);
2336
Alexandra Chind5591a62013-02-07 12:59:15 -08002337 if (!list_empty(&rmi->support_fn_list)) {
2338 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
2339 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
2340 synaptics_rmi4_f1a_kfree(fhandler);
2341 else
2342 kfree(fhandler->data);
2343 kfree(fhandler);
2344 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08002345 }
Amy Maloche946da662013-01-18 16:27:11 -08002346
2347 if (gpio_is_valid(rmi4_data->board->reset_gpio))
2348 gpio_free(rmi4_data->board->reset_gpio);
2349 if (gpio_is_valid(rmi4_data->board->irq_gpio))
2350 gpio_free(rmi4_data->board->irq_gpio);
Alexandra Chin669d27c2012-12-24 15:42:30 +08002351
Amy Maloche1a53b612013-01-18 15:25:15 -08002352 synaptics_rmi4_power_on(rmi4_data, false);
2353 synaptics_rmi4_regulator_configure(rmi4_data, false);
2354
Alexandra Chin669d27c2012-12-24 15:42:30 +08002355 kfree(rmi4_data);
2356
2357 return 0;
2358}
2359
2360#ifdef CONFIG_PM
2361 /**
2362 * synaptics_rmi4_sensor_sleep()
2363 *
2364 * Called by synaptics_rmi4_early_suspend() and synaptics_rmi4_suspend().
2365 *
2366 * This function stops finger data acquisition and puts the sensor to sleep.
2367 */
2368static void synaptics_rmi4_sensor_sleep(struct synaptics_rmi4_data *rmi4_data)
2369{
2370 int retval;
2371 unsigned char device_ctrl;
2372
2373 retval = synaptics_rmi4_i2c_read(rmi4_data,
2374 rmi4_data->f01_ctrl_base_addr,
2375 &device_ctrl,
2376 sizeof(device_ctrl));
2377 if (retval < 0) {
2378 dev_err(&(rmi4_data->input_dev->dev),
2379 "%s: Failed to enter sleep mode\n",
2380 __func__);
2381 rmi4_data->sensor_sleep = false;
2382 return;
2383 }
2384
2385 device_ctrl = (device_ctrl & ~MASK_3BIT);
2386 device_ctrl = (device_ctrl | NO_SLEEP_OFF | SENSOR_SLEEP);
2387
2388 retval = synaptics_rmi4_i2c_write(rmi4_data,
2389 rmi4_data->f01_ctrl_base_addr,
2390 &device_ctrl,
2391 sizeof(device_ctrl));
2392 if (retval < 0) {
2393 dev_err(&(rmi4_data->input_dev->dev),
2394 "%s: Failed to enter sleep mode\n",
2395 __func__);
2396 rmi4_data->sensor_sleep = false;
2397 return;
2398 } else {
2399 rmi4_data->sensor_sleep = true;
2400 }
2401
2402 return;
2403}
2404
2405 /**
2406 * synaptics_rmi4_sensor_wake()
2407 *
2408 * Called by synaptics_rmi4_resume() and synaptics_rmi4_late_resume().
2409 *
2410 * This function wakes the sensor from sleep.
2411 */
2412static void synaptics_rmi4_sensor_wake(struct synaptics_rmi4_data *rmi4_data)
2413{
2414 int retval;
2415 unsigned char device_ctrl;
2416
2417 retval = synaptics_rmi4_i2c_read(rmi4_data,
2418 rmi4_data->f01_ctrl_base_addr,
2419 &device_ctrl,
2420 sizeof(device_ctrl));
2421 if (retval < 0) {
2422 dev_err(&(rmi4_data->input_dev->dev),
2423 "%s: Failed to wake from sleep mode\n",
2424 __func__);
2425 rmi4_data->sensor_sleep = true;
2426 return;
2427 }
2428
2429 device_ctrl = (device_ctrl & ~MASK_3BIT);
2430 device_ctrl = (device_ctrl | NO_SLEEP_OFF | NORMAL_OPERATION);
2431
2432 retval = synaptics_rmi4_i2c_write(rmi4_data,
2433 rmi4_data->f01_ctrl_base_addr,
2434 &device_ctrl,
2435 sizeof(device_ctrl));
2436 if (retval < 0) {
2437 dev_err(&(rmi4_data->input_dev->dev),
2438 "%s: Failed to wake from sleep mode\n",
2439 __func__);
2440 rmi4_data->sensor_sleep = true;
2441 return;
2442 } else {
2443 rmi4_data->sensor_sleep = false;
2444 }
2445
2446 return;
2447}
2448
2449#ifdef CONFIG_HAS_EARLYSUSPEND
2450 /**
2451 * synaptics_rmi4_early_suspend()
2452 *
2453 * Called by the kernel during the early suspend phase when the system
2454 * enters suspend.
2455 *
2456 * This function calls synaptics_rmi4_sensor_sleep() to stop finger
2457 * data acquisition and put the sensor to sleep.
2458 */
2459static void synaptics_rmi4_early_suspend(struct early_suspend *h)
2460{
2461 struct synaptics_rmi4_data *rmi4_data =
2462 container_of(h, struct synaptics_rmi4_data,
2463 early_suspend);
2464
2465 rmi4_data->touch_stopped = true;
2466 wake_up(&rmi4_data->wait);
2467 synaptics_rmi4_irq_enable(rmi4_data, false);
2468 synaptics_rmi4_sensor_sleep(rmi4_data);
2469
2470 if (rmi4_data->full_pm_cycle)
2471 synaptics_rmi4_suspend(&(rmi4_data->input_dev->dev));
2472
2473 return;
2474}
2475
2476 /**
2477 * synaptics_rmi4_late_resume()
2478 *
2479 * Called by the kernel during the late resume phase when the system
2480 * wakes up from suspend.
2481 *
2482 * This function goes through the sensor wake process if the system wakes
2483 * up from early suspend (without going into suspend).
2484 */
2485static void synaptics_rmi4_late_resume(struct early_suspend *h)
2486{
2487 struct synaptics_rmi4_data *rmi4_data =
2488 container_of(h, struct synaptics_rmi4_data,
2489 early_suspend);
2490
2491 if (rmi4_data->full_pm_cycle)
2492 synaptics_rmi4_resume(&(rmi4_data->input_dev->dev));
2493
2494 if (rmi4_data->sensor_sleep == true) {
2495 synaptics_rmi4_sensor_wake(rmi4_data);
2496 rmi4_data->touch_stopped = false;
2497 synaptics_rmi4_irq_enable(rmi4_data, true);
2498 }
2499
2500 return;
2501}
2502#endif
2503
Amy Maloche827e8b02013-02-27 15:25:14 -08002504static int synaptics_rmi4_regulator_lpm(struct synaptics_rmi4_data *rmi4_data,
2505 bool on)
2506{
2507 int retval;
2508
2509 if (on == false)
2510 goto regulator_hpm;
2511
2512 retval = reg_set_optimum_mode_check(rmi4_data->vdd, RMI4_LPM_LOAD_UA);
2513 if (retval < 0) {
2514 dev_err(&rmi4_data->i2c_client->dev,
2515 "Regulator vcc_ana set_opt failed rc=%d\n",
2516 retval);
2517 goto fail_regulator_lpm;
2518 }
2519
2520 if (rmi4_data->board->i2c_pull_up) {
2521 retval = reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
2522 RMI4_I2C_LOAD_UA);
2523 if (retval < 0) {
2524 dev_err(&rmi4_data->i2c_client->dev,
2525 "Regulator vcc_i2c set_opt failed rc=%d\n",
2526 retval);
2527 goto fail_regulator_lpm;
2528 }
2529 }
2530
2531 return 0;
2532
2533regulator_hpm:
2534
2535 retval = reg_set_optimum_mode_check(rmi4_data->vdd,
2536 RMI4_ACTIVE_LOAD_UA);
2537 if (retval < 0) {
2538 dev_err(&rmi4_data->i2c_client->dev,
2539 "Regulator vcc_ana set_opt failed rc=%d\n",
2540 retval);
2541 goto fail_regulator_hpm;
2542 }
2543
2544 if (rmi4_data->board->i2c_pull_up) {
2545 retval = reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
2546 RMI4_I2C_LOAD_UA);
2547 if (retval < 0) {
2548 dev_err(&rmi4_data->i2c_client->dev,
2549 "Regulator vcc_i2c set_opt failed rc=%d\n",
2550 retval);
2551 goto fail_regulator_hpm;
2552 }
2553 }
2554
2555 return 0;
2556
2557fail_regulator_lpm:
2558 reg_set_optimum_mode_check(rmi4_data->vdd, RMI4_ACTIVE_LOAD_UA);
2559 if (rmi4_data->board->i2c_pull_up)
2560 reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
2561 RMI4_I2C_LOAD_UA);
2562
2563 return retval;
2564
2565fail_regulator_hpm:
2566 reg_set_optimum_mode_check(rmi4_data->vdd, RMI4_LPM_LOAD_UA);
2567 if (rmi4_data->board->i2c_pull_up)
2568 reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
2569 RMI4_I2C_LPM_LOAD_UA);
2570 return retval;
2571}
2572
Alexandra Chin669d27c2012-12-24 15:42:30 +08002573 /**
2574 * synaptics_rmi4_suspend()
2575 *
2576 * Called by the kernel during the suspend phase when the system
2577 * enters suspend.
2578 *
2579 * This function stops finger data acquisition and puts the sensor to
2580 * sleep (if not already done so during the early suspend phase),
2581 * disables the interrupt, and turns off the power to the sensor.
2582 */
2583static int synaptics_rmi4_suspend(struct device *dev)
2584{
2585 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
Amy Maloche827e8b02013-02-27 15:25:14 -08002586 int retval;
Alexandra Chin669d27c2012-12-24 15:42:30 +08002587
2588 if (!rmi4_data->sensor_sleep) {
2589 rmi4_data->touch_stopped = true;
2590 wake_up(&rmi4_data->wait);
2591 synaptics_rmi4_irq_enable(rmi4_data, false);
2592 synaptics_rmi4_sensor_sleep(rmi4_data);
2593 }
2594
Amy Maloche827e8b02013-02-27 15:25:14 -08002595 retval = synaptics_rmi4_regulator_lpm(rmi4_data, true);
2596 if (retval < 0) {
2597 dev_err(dev, "failed to enter low power mode\n");
2598 return retval;
2599 }
2600
Alexandra Chin669d27c2012-12-24 15:42:30 +08002601 return 0;
2602}
2603
2604 /**
2605 * synaptics_rmi4_resume()
2606 *
2607 * Called by the kernel during the resume phase when the system
2608 * wakes up from suspend.
2609 *
2610 * This function turns on the power to the sensor, wakes the sensor
2611 * from sleep, enables the interrupt, and starts finger data
2612 * acquisition.
2613 */
2614static int synaptics_rmi4_resume(struct device *dev)
2615{
2616 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
Amy Maloche827e8b02013-02-27 15:25:14 -08002617 int retval;
2618
2619 retval = synaptics_rmi4_regulator_lpm(rmi4_data, false);
2620 if (retval < 0) {
2621 dev_err(dev, "failed to enter active power mode\n");
2622 return retval;
2623 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08002624
2625 synaptics_rmi4_sensor_wake(rmi4_data);
2626 rmi4_data->touch_stopped = false;
2627 synaptics_rmi4_irq_enable(rmi4_data, true);
2628
2629 return 0;
2630}
2631
2632static const struct dev_pm_ops synaptics_rmi4_dev_pm_ops = {
2633 .suspend = synaptics_rmi4_suspend,
2634 .resume = synaptics_rmi4_resume,
2635};
2636#endif
2637
2638static const struct i2c_device_id synaptics_rmi4_id_table[] = {
2639 {DRIVER_NAME, 0},
2640 {},
2641};
2642MODULE_DEVICE_TABLE(i2c, synaptics_rmi4_id_table);
2643
Amy Malocheecfb4892013-02-06 13:30:36 -08002644#ifdef CONFIG_OF
2645static struct of_device_id rmi4_match_table[] = {
2646 { .compatible = "synaptics,rmi4",},
2647 { },
2648};
2649#else
2650#define rmi4_match_table NULL
2651#endif
2652
Alexandra Chin669d27c2012-12-24 15:42:30 +08002653static struct i2c_driver synaptics_rmi4_driver = {
2654 .driver = {
2655 .name = DRIVER_NAME,
2656 .owner = THIS_MODULE,
Amy Malocheecfb4892013-02-06 13:30:36 -08002657 .of_match_table = rmi4_match_table,
Alexandra Chin669d27c2012-12-24 15:42:30 +08002658#ifdef CONFIG_PM
2659 .pm = &synaptics_rmi4_dev_pm_ops,
2660#endif
2661 },
2662 .probe = synaptics_rmi4_probe,
2663 .remove = __devexit_p(synaptics_rmi4_remove),
2664 .id_table = synaptics_rmi4_id_table,
2665};
2666
2667 /**
2668 * synaptics_rmi4_init()
2669 *
2670 * Called by the kernel during do_initcalls (if built-in)
2671 * or when the driver is loaded (if a module).
2672 *
2673 * This function registers the driver to the I2C subsystem.
2674 *
2675 */
2676static int __init synaptics_rmi4_init(void)
2677{
2678 return i2c_add_driver(&synaptics_rmi4_driver);
2679}
2680
2681 /**
2682 * synaptics_rmi4_exit()
2683 *
2684 * Called by the kernel when the driver is unloaded.
2685 *
2686 * This funtion unregisters the driver from the I2C subsystem.
2687 *
2688 */
2689static void __exit synaptics_rmi4_exit(void)
2690{
2691 i2c_del_driver(&synaptics_rmi4_driver);
2692}
2693
2694module_init(synaptics_rmi4_init);
2695module_exit(synaptics_rmi4_exit);
2696
2697MODULE_AUTHOR("Synaptics, Inc.");
2698MODULE_DESCRIPTION("Synaptics RMI4 I2C Touch Driver");
2699MODULE_LICENSE("GPL v2");