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