blob: 410ece3bd50e2afc656d7ead9164f44aab848eb5 [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 Maloche5210eeb2013-06-04 16:05:27 -0700117static ssize_t synaptics_rmi4_mode_suspend_store(struct device *dev,
118 struct device_attribute *attr, const char *buf, size_t count);
119
120static ssize_t synaptics_rmi4_mode_resume_store(struct device *dev,
121 struct device_attribute *attr, const char *buf, size_t count);
122
Amy Malochef46540d2013-02-15 17:09:43 -0800123#if defined(CONFIG_FB)
124static int fb_notifier_callback(struct notifier_block *self,
125 unsigned long event, void *data);
126#elif defined(CONFIG_HAS_EARLYSUSPEND)
Alexandra Chin669d27c2012-12-24 15:42:30 +0800127static void synaptics_rmi4_early_suspend(struct early_suspend *h);
128
129static void synaptics_rmi4_late_resume(struct early_suspend *h);
Amy Maloche827e8b02013-02-27 15:25:14 -0800130#endif
Alexandra Chin669d27c2012-12-24 15:42:30 +0800131#endif
132
133static ssize_t synaptics_rmi4_f01_reset_store(struct device *dev,
134 struct device_attribute *attr, const char *buf, size_t count);
135
136static ssize_t synaptics_rmi4_f01_productinfo_show(struct device *dev,
137 struct device_attribute *attr, char *buf);
138
139static ssize_t synaptics_rmi4_f01_buildid_show(struct device *dev,
140 struct device_attribute *attr, char *buf);
141
142static ssize_t synaptics_rmi4_f01_flashprog_show(struct device *dev,
143 struct device_attribute *attr, char *buf);
144
145static ssize_t synaptics_rmi4_0dbutton_show(struct device *dev,
146 struct device_attribute *attr, char *buf);
147
148static ssize_t synaptics_rmi4_0dbutton_store(struct device *dev,
149 struct device_attribute *attr, const char *buf, size_t count);
150
Alexandra Chinfb798fa2013-03-26 17:15:38 -0700151static ssize_t synaptics_rmi4_flipx_show(struct device *dev,
152 struct device_attribute *attr, char *buf);
153
154static ssize_t synaptics_rmi4_flipx_store(struct device *dev,
155 struct device_attribute *attr, const char *buf, size_t count);
156
157static ssize_t synaptics_rmi4_flipy_show(struct device *dev,
158 struct device_attribute *attr, char *buf);
159
160static ssize_t synaptics_rmi4_flipy_store(struct device *dev,
161 struct device_attribute *attr, const char *buf, size_t count);
162
163
Alexandra Chin669d27c2012-12-24 15:42:30 +0800164struct synaptics_rmi4_f01_device_status {
165 union {
166 struct {
167 unsigned char status_code:4;
168 unsigned char reserved:2;
169 unsigned char flash_prog:1;
170 unsigned char unconfigured:1;
171 } __packed;
172 unsigned char data[1];
173 };
174};
175
176struct synaptics_rmi4_f1a_query {
177 union {
178 struct {
179 unsigned char max_button_count:3;
180 unsigned char reserved:5;
181 unsigned char has_general_control:1;
182 unsigned char has_interrupt_enable:1;
183 unsigned char has_multibutton_select:1;
184 unsigned char has_tx_rx_map:1;
185 unsigned char has_perbutton_threshold:1;
186 unsigned char has_release_threshold:1;
187 unsigned char has_strongestbtn_hysteresis:1;
188 unsigned char has_filter_strength:1;
189 } __packed;
190 unsigned char data[2];
191 };
192};
193
194struct synaptics_rmi4_f1a_control_0 {
195 union {
196 struct {
197 unsigned char multibutton_report:2;
198 unsigned char filter_mode:2;
199 unsigned char reserved:4;
200 } __packed;
201 unsigned char data[1];
202 };
203};
204
205struct synaptics_rmi4_f1a_control_3_4 {
206 unsigned char transmitterbutton;
207 unsigned char receiverbutton;
208};
209
210struct synaptics_rmi4_f1a_control {
211 struct synaptics_rmi4_f1a_control_0 general_control;
212 unsigned char *button_int_enable;
213 unsigned char *multi_button;
214 struct synaptics_rmi4_f1a_control_3_4 *electrode_map;
215 unsigned char *button_threshold;
216 unsigned char button_release_threshold;
217 unsigned char strongest_button_hysteresis;
218 unsigned char filter_strength;
219};
220
221struct synaptics_rmi4_f1a_handle {
222 int button_bitmask_size;
223 unsigned char button_count;
224 unsigned char valid_button_count;
225 unsigned char *button_data_buffer;
226 unsigned char *button_map;
227 struct synaptics_rmi4_f1a_query button_query;
228 struct synaptics_rmi4_f1a_control button_control;
229};
230
231struct synaptics_rmi4_exp_fn {
232 enum exp_fn fn_type;
233 bool inserted;
234 int (*func_init)(struct synaptics_rmi4_data *rmi4_data);
235 void (*func_remove)(struct synaptics_rmi4_data *rmi4_data);
236 void (*func_attn)(struct synaptics_rmi4_data *rmi4_data,
237 unsigned char intr_mask);
238 struct list_head link;
239};
240
241static struct device_attribute attrs[] = {
Amy Malochef46540d2013-02-15 17:09:43 -0800242#ifdef CONFIG_PM
Alexandra Chin669d27c2012-12-24 15:42:30 +0800243 __ATTR(full_pm_cycle, (S_IRUGO | S_IWUGO),
244 synaptics_rmi4_full_pm_cycle_show,
245 synaptics_rmi4_full_pm_cycle_store),
Amy Maloche5210eeb2013-06-04 16:05:27 -0700246 __ATTR(mode_suspend, S_IWUGO,
247 synaptics_rmi4_show_error,
248 synaptics_rmi4_mode_suspend_store),
249 __ATTR(mode_resume, S_IWUGO,
250 synaptics_rmi4_show_error,
251 synaptics_rmi4_mode_resume_store),
Alexandra Chin669d27c2012-12-24 15:42:30 +0800252#endif
253 __ATTR(reset, S_IWUGO,
254 synaptics_rmi4_show_error,
255 synaptics_rmi4_f01_reset_store),
256 __ATTR(productinfo, S_IRUGO,
257 synaptics_rmi4_f01_productinfo_show,
258 synaptics_rmi4_store_error),
259 __ATTR(buildid, S_IRUGO,
260 synaptics_rmi4_f01_buildid_show,
261 synaptics_rmi4_store_error),
262 __ATTR(flashprog, S_IRUGO,
263 synaptics_rmi4_f01_flashprog_show,
264 synaptics_rmi4_store_error),
265 __ATTR(0dbutton, (S_IRUGO | S_IWUGO),
266 synaptics_rmi4_0dbutton_show,
267 synaptics_rmi4_0dbutton_store),
Alexandra Chinfb798fa2013-03-26 17:15:38 -0700268 __ATTR(flipx, (S_IRUGO | S_IWUGO),
269 synaptics_rmi4_flipx_show,
270 synaptics_rmi4_flipx_store),
271 __ATTR(flipy, (S_IRUGO | S_IWUGO),
272 synaptics_rmi4_flipy_show,
273 synaptics_rmi4_flipy_store),
Alexandra Chin669d27c2012-12-24 15:42:30 +0800274};
275
276static bool exp_fn_inited;
277static struct mutex exp_fn_list_mutex;
278static struct list_head exp_fn_list;
Amy Malochef46540d2013-02-15 17:09:43 -0800279#ifdef CONFIG_PM
Alexandra Chin669d27c2012-12-24 15:42:30 +0800280static ssize_t synaptics_rmi4_full_pm_cycle_show(struct device *dev,
281 struct device_attribute *attr, char *buf)
282{
283 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
284
285 return snprintf(buf, PAGE_SIZE, "%u\n",
286 rmi4_data->full_pm_cycle);
287}
288
289static ssize_t synaptics_rmi4_full_pm_cycle_store(struct device *dev,
290 struct device_attribute *attr, const char *buf, size_t count)
291{
292 unsigned int input;
293 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
294
295 if (sscanf(buf, "%u", &input) != 1)
296 return -EINVAL;
297
298 rmi4_data->full_pm_cycle = input > 0 ? 1 : 0;
299
300 return count;
301}
Amy Malochef46540d2013-02-15 17:09:43 -0800302
Amy Maloche5210eeb2013-06-04 16:05:27 -0700303static ssize_t synaptics_rmi4_mode_suspend_store(struct device *dev,
304 struct device_attribute *attr, const char *buf, size_t count)
305{
306 unsigned int input;
307 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
308
309 if (sscanf(buf, "%u", &input) != 1)
310 return -EINVAL;
311
312 synaptics_rmi4_suspend(&(rmi4_data->input_dev->dev));
313
314 return count;
315}
316
317static ssize_t synaptics_rmi4_mode_resume_store(struct device *dev,
318 struct device_attribute *attr, const char *buf, size_t count)
319{
320 unsigned int input;
321 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
322
323 if (sscanf(buf, "%u", &input) != 1)
324 return -EINVAL;
325
326 synaptics_rmi4_resume(&(rmi4_data->input_dev->dev));
327
328 return count;
329}
330
Amy Malochef46540d2013-02-15 17:09:43 -0800331#ifdef CONFIG_FB
332static void configure_sleep(struct synaptics_rmi4_data *rmi4_data)
333{
334 int retval = 0;
335
336 rmi4_data->fb_notif.notifier_call = fb_notifier_callback;
337
338 retval = fb_register_client(&rmi4_data->fb_notif);
339 if (retval)
340 dev_err(&rmi4_data->i2c_client->dev,
341 "Unable to register fb_notifier: %d\n", retval);
342 return;
343}
344#elif defined CONFIG_HAS_EARLYSUSPEND
345static void configure_sleep(struct synaptics_rmi4_data *rmi4_data)
346{
347 rmi4_data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
348 rmi4_data->early_suspend.suspend = synaptics_rmi4_early_suspend;
349 rmi4_data->early_suspend.resume = synaptics_rmi4_late_resume;
350 register_early_suspend(&rmi4_data->early_suspend);
351
352 return;
353}
354#else
355static void configure_sleep(struct synaptics_rmi4_data *rmi4_data)
356{
357 return;
358}
359#endif
Alexandra Chin669d27c2012-12-24 15:42:30 +0800360#endif
361
362static ssize_t synaptics_rmi4_f01_reset_store(struct device *dev,
363 struct device_attribute *attr, const char *buf, size_t count)
364{
365 int retval;
366 unsigned int reset;
367 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
368
369 if (sscanf(buf, "%u", &reset) != 1)
370 return -EINVAL;
371
372 if (reset != 1)
373 return -EINVAL;
374
375 retval = synaptics_rmi4_reset_device(rmi4_data);
376 if (retval < 0) {
377 dev_err(dev,
378 "%s: Failed to issue reset command, error = %d\n",
379 __func__, retval);
380 return retval;
381 }
382
383 return count;
384}
385
386static ssize_t synaptics_rmi4_f01_productinfo_show(struct device *dev,
387 struct device_attribute *attr, char *buf)
388{
389 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
390
391 return snprintf(buf, PAGE_SIZE, "0x%02x 0x%02x\n",
392 (rmi4_data->rmi4_mod_info.product_info[0]),
393 (rmi4_data->rmi4_mod_info.product_info[1]));
394}
395
396static ssize_t synaptics_rmi4_f01_buildid_show(struct device *dev,
397 struct device_attribute *attr, char *buf)
398{
399 unsigned int build_id;
400 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
401 struct synaptics_rmi4_device_info *rmi;
402
403 rmi = &(rmi4_data->rmi4_mod_info);
404
405 build_id = (unsigned int)rmi->build_id[0] +
406 (unsigned int)rmi->build_id[1] * 0x100 +
407 (unsigned int)rmi->build_id[2] * 0x10000;
408
409 return snprintf(buf, PAGE_SIZE, "%u\n",
410 build_id);
411}
412
413static ssize_t synaptics_rmi4_f01_flashprog_show(struct device *dev,
414 struct device_attribute *attr, char *buf)
415{
416 int retval;
417 struct synaptics_rmi4_f01_device_status device_status;
418 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
419
420 retval = synaptics_rmi4_i2c_read(rmi4_data,
421 rmi4_data->f01_data_base_addr,
422 device_status.data,
423 sizeof(device_status.data));
424 if (retval < 0) {
425 dev_err(dev,
426 "%s: Failed to read device status, error = %d\n",
427 __func__, retval);
428 return retval;
429 }
430
431 return snprintf(buf, PAGE_SIZE, "%u\n",
432 device_status.flash_prog);
433}
434
435static ssize_t synaptics_rmi4_0dbutton_show(struct device *dev,
436 struct device_attribute *attr, char *buf)
437{
438 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
439
440 return snprintf(buf, PAGE_SIZE, "%u\n",
441 rmi4_data->button_0d_enabled);
442}
443
444static ssize_t synaptics_rmi4_0dbutton_store(struct device *dev,
445 struct device_attribute *attr, const char *buf, size_t count)
446{
447 int retval;
448 unsigned int input;
449 unsigned char ii;
450 unsigned char intr_enable;
451 struct synaptics_rmi4_fn *fhandler;
452 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
453 struct synaptics_rmi4_device_info *rmi;
454
455 rmi = &(rmi4_data->rmi4_mod_info);
456
457 if (sscanf(buf, "%u", &input) != 1)
458 return -EINVAL;
459
460 input = input > 0 ? 1 : 0;
461
462 if (rmi4_data->button_0d_enabled == input)
463 return count;
464
Alexandra Chind5591a62013-02-07 12:59:15 -0800465 if (!list_empty(&rmi->support_fn_list)) {
466 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
467 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A) {
468 ii = fhandler->intr_reg_num;
Alexandra Chin669d27c2012-12-24 15:42:30 +0800469
Alexandra Chind5591a62013-02-07 12:59:15 -0800470 retval = synaptics_rmi4_i2c_read(rmi4_data,
471 rmi4_data->f01_ctrl_base_addr +
472 1 + ii,
473 &intr_enable,
474 sizeof(intr_enable));
475 if (retval < 0)
476 return retval;
Alexandra Chin669d27c2012-12-24 15:42:30 +0800477
Alexandra Chind5591a62013-02-07 12:59:15 -0800478 if (input == 1)
479 intr_enable |= fhandler->intr_mask;
480 else
481 intr_enable &= ~fhandler->intr_mask;
Alexandra Chin669d27c2012-12-24 15:42:30 +0800482
Alexandra Chind5591a62013-02-07 12:59:15 -0800483 retval = synaptics_rmi4_i2c_write(rmi4_data,
484 rmi4_data->f01_ctrl_base_addr +
485 1 + ii,
486 &intr_enable,
487 sizeof(intr_enable));
488 if (retval < 0)
489 return retval;
490 }
Alexandra Chin669d27c2012-12-24 15:42:30 +0800491 }
492 }
493
494 rmi4_data->button_0d_enabled = input;
495
496 return count;
497}
498
Alexandra Chinfb798fa2013-03-26 17:15:38 -0700499static ssize_t synaptics_rmi4_flipx_show(struct device *dev,
500 struct device_attribute *attr, char *buf)
501{
502 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
503
504 return snprintf(buf, PAGE_SIZE, "%u\n",
505 rmi4_data->flip_x);
506}
507
508static ssize_t synaptics_rmi4_flipx_store(struct device *dev,
509 struct device_attribute *attr, const char *buf, size_t count)
510{
511 unsigned int input;
512 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
513
514 if (sscanf(buf, "%u", &input) != 1)
515 return -EINVAL;
516
517 rmi4_data->flip_x = input > 0 ? 1 : 0;
518
519 return count;
520}
521
522static ssize_t synaptics_rmi4_flipy_show(struct device *dev,
523 struct device_attribute *attr, char *buf)
524{
525 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
526
527 return snprintf(buf, PAGE_SIZE, "%u\n",
528 rmi4_data->flip_y);
529}
530
531static ssize_t synaptics_rmi4_flipy_store(struct device *dev,
532 struct device_attribute *attr, const char *buf, size_t count)
533{
534 unsigned int input;
535 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
536
537 if (sscanf(buf, "%u", &input) != 1)
538 return -EINVAL;
539
540 rmi4_data->flip_y = input > 0 ? 1 : 0;
541
542 return count;
543}
544
Alexandra Chin669d27c2012-12-24 15:42:30 +0800545 /**
546 * synaptics_rmi4_set_page()
547 *
548 * Called by synaptics_rmi4_i2c_read() and synaptics_rmi4_i2c_write().
549 *
550 * This function writes to the page select register to switch to the
551 * assigned page.
552 */
553static int synaptics_rmi4_set_page(struct synaptics_rmi4_data *rmi4_data,
554 unsigned int address)
555{
556 int retval = 0;
557 unsigned char retry;
558 unsigned char buf[PAGE_SELECT_LEN];
559 unsigned char page;
560 struct i2c_client *i2c = rmi4_data->i2c_client;
561
562 page = ((address >> 8) & MASK_8BIT);
563 if (page != rmi4_data->current_page) {
564 buf[0] = MASK_8BIT;
565 buf[1] = page;
566 for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
567 retval = i2c_master_send(i2c, buf, PAGE_SELECT_LEN);
568 if (retval != PAGE_SELECT_LEN) {
569 dev_err(&i2c->dev,
570 "%s: I2C retry %d\n",
571 __func__, retry + 1);
572 msleep(20);
573 } else {
574 rmi4_data->current_page = page;
575 break;
576 }
577 }
578 } else
579 return PAGE_SELECT_LEN;
580 return (retval == PAGE_SELECT_LEN) ? retval : -EIO;
581}
582
583 /**
584 * synaptics_rmi4_i2c_read()
585 *
586 * Called by various functions in this driver, and also exported to
587 * other expansion Function modules such as rmi_dev.
588 *
589 * This function reads data of an arbitrary length from the sensor,
590 * starting from an assigned register address of the sensor, via I2C
591 * with a retry mechanism.
592 */
593static int synaptics_rmi4_i2c_read(struct synaptics_rmi4_data *rmi4_data,
594 unsigned short addr, unsigned char *data, unsigned short length)
595{
596 int retval;
597 unsigned char retry;
598 unsigned char buf;
599 struct i2c_msg msg[] = {
600 {
601 .addr = rmi4_data->i2c_client->addr,
602 .flags = 0,
603 .len = 1,
604 .buf = &buf,
605 },
606 {
607 .addr = rmi4_data->i2c_client->addr,
608 .flags = I2C_M_RD,
609 .len = length,
610 .buf = data,
611 },
612 };
613
614 buf = addr & MASK_8BIT;
615
616 mutex_lock(&(rmi4_data->rmi4_io_ctrl_mutex));
617
618 retval = synaptics_rmi4_set_page(rmi4_data, addr);
619 if (retval != PAGE_SELECT_LEN)
620 goto exit;
621
622 for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
623 if (i2c_transfer(rmi4_data->i2c_client->adapter, msg, 2) == 2) {
624 retval = length;
625 break;
626 }
627 dev_err(&rmi4_data->i2c_client->dev,
628 "%s: I2C retry %d\n",
629 __func__, retry + 1);
630 msleep(20);
631 }
632
633 if (retry == SYN_I2C_RETRY_TIMES) {
634 dev_err(&rmi4_data->i2c_client->dev,
635 "%s: I2C read over retry limit\n",
636 __func__);
637 retval = -EIO;
638 }
639
640exit:
641 mutex_unlock(&(rmi4_data->rmi4_io_ctrl_mutex));
642
643 return retval;
644}
645
646 /**
647 * synaptics_rmi4_i2c_write()
648 *
649 * Called by various functions in this driver, and also exported to
650 * other expansion Function modules such as rmi_dev.
651 *
652 * This function writes data of an arbitrary length to the sensor,
653 * starting from an assigned register address of the sensor, via I2C with
654 * a retry mechanism.
655 */
656static int synaptics_rmi4_i2c_write(struct synaptics_rmi4_data *rmi4_data,
657 unsigned short addr, unsigned char *data, unsigned short length)
658{
659 int retval;
660 unsigned char retry;
661 unsigned char buf[length + 1];
662 struct i2c_msg msg[] = {
663 {
664 .addr = rmi4_data->i2c_client->addr,
665 .flags = 0,
666 .len = length + 1,
667 .buf = buf,
668 }
669 };
670
671 mutex_lock(&(rmi4_data->rmi4_io_ctrl_mutex));
672
673 retval = synaptics_rmi4_set_page(rmi4_data, addr);
674 if (retval != PAGE_SELECT_LEN)
675 goto exit;
676
677 buf[0] = addr & MASK_8BIT;
678 memcpy(&buf[1], &data[0], length);
679
680 for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
681 if (i2c_transfer(rmi4_data->i2c_client->adapter, msg, 1) == 1) {
682 retval = length;
683 break;
684 }
685 dev_err(&rmi4_data->i2c_client->dev,
686 "%s: I2C retry %d\n",
687 __func__, retry + 1);
688 msleep(20);
689 }
690
691 if (retry == SYN_I2C_RETRY_TIMES) {
692 dev_err(&rmi4_data->i2c_client->dev,
693 "%s: I2C write over retry limit\n",
694 __func__);
695 retval = -EIO;
696 }
697
698exit:
699 mutex_unlock(&(rmi4_data->rmi4_io_ctrl_mutex));
700
701 return retval;
702}
703
704 /**
705 * synaptics_rmi4_f11_abs_report()
706 *
707 * Called by synaptics_rmi4_report_touch() when valid Function $11
708 * finger data has been detected.
709 *
710 * This function reads the Function $11 data registers, determines the
711 * status of each finger supported by the Function, processes any
712 * necessary coordinate manipulation, reports the finger data to
713 * the input subsystem, and returns the number of fingers detected.
714 */
715static int synaptics_rmi4_f11_abs_report(struct synaptics_rmi4_data *rmi4_data,
716 struct synaptics_rmi4_fn *fhandler)
717{
718 int retval;
719 unsigned char touch_count = 0; /* number of touch points */
720 unsigned char reg_index;
721 unsigned char finger;
722 unsigned char fingers_supported;
723 unsigned char num_of_finger_status_regs;
724 unsigned char finger_shift;
725 unsigned char finger_status;
726 unsigned char data_reg_blk_size;
727 unsigned char finger_status_reg[3];
728 unsigned char data[F11_STD_DATA_LEN];
729 unsigned short data_addr;
730 unsigned short data_offset;
731 int x;
732 int y;
733 int wx;
734 int wy;
Alexandra Chinfb798fa2013-03-26 17:15:38 -0700735 int z;
Alexandra Chin669d27c2012-12-24 15:42:30 +0800736
737 /*
738 * The number of finger status registers is determined by the
739 * maximum number of fingers supported - 2 bits per finger. So
740 * the number of finger status registers to read is:
741 * register_count = ceil(max_num_of_fingers / 4)
742 */
743 fingers_supported = fhandler->num_of_data_points;
744 num_of_finger_status_regs = (fingers_supported + 3) / 4;
745 data_addr = fhandler->full_addr.data_base;
746 data_reg_blk_size = fhandler->size_of_data_register_block;
747
748 retval = synaptics_rmi4_i2c_read(rmi4_data,
749 data_addr,
750 finger_status_reg,
751 num_of_finger_status_regs);
752 if (retval < 0)
753 return 0;
754
755 for (finger = 0; finger < fingers_supported; finger++) {
756 reg_index = finger / 4;
757 finger_shift = (finger % 4) * 2;
758 finger_status = (finger_status_reg[reg_index] >> finger_shift)
759 & MASK_2BIT;
760
761 /*
762 * Each 2-bit finger status field represents the following:
763 * 00 = finger not present
764 * 01 = finger present and data accurate
765 * 10 = finger present but data may be inaccurate
766 * 11 = reserved
767 */
768#ifdef TYPE_B_PROTOCOL
769 input_mt_slot(rmi4_data->input_dev, finger);
770 input_mt_report_slot_state(rmi4_data->input_dev,
771 MT_TOOL_FINGER, finger_status != 0);
772#endif
773
774 if (finger_status) {
775 data_offset = data_addr +
776 num_of_finger_status_regs +
777 (finger * data_reg_blk_size);
778 retval = synaptics_rmi4_i2c_read(rmi4_data,
779 data_offset,
780 data,
781 data_reg_blk_size);
782 if (retval < 0)
783 return 0;
784
785 x = (data[0] << 4) | (data[2] & MASK_4BIT);
786 y = (data[1] << 4) | ((data[2] >> 4) & MASK_4BIT);
787 wx = (data[3] & MASK_4BIT);
788 wy = (data[3] >> 4) & MASK_4BIT;
Alexandra Chinfb798fa2013-03-26 17:15:38 -0700789 z = data[4];
Alexandra Chin669d27c2012-12-24 15:42:30 +0800790
Alexandra Chinfb798fa2013-03-26 17:15:38 -0700791 if (rmi4_data->flip_x)
Alexandra Chin669d27c2012-12-24 15:42:30 +0800792 x = rmi4_data->sensor_max_x - x;
Alexandra Chinfb798fa2013-03-26 17:15:38 -0700793 if (rmi4_data->flip_y)
Alexandra Chin669d27c2012-12-24 15:42:30 +0800794 y = rmi4_data->sensor_max_y - y;
795
796 dev_dbg(&rmi4_data->i2c_client->dev,
797 "%s: Finger %d:\n"
798 "status = 0x%02x\n"
799 "x = %d\n"
800 "y = %d\n"
801 "wx = %d\n"
802 "wy = %d\n",
803 __func__, finger,
804 finger_status,
805 x, y, wx, wy);
806
Alexandra Chin669d27c2012-12-24 15:42:30 +0800807 input_report_abs(rmi4_data->input_dev,
808 ABS_MT_POSITION_X, x);
809 input_report_abs(rmi4_data->input_dev,
810 ABS_MT_POSITION_Y, y);
Alexandra Chinfb798fa2013-03-26 17:15:38 -0700811 input_report_abs(rmi4_data->input_dev,
812 ABS_MT_PRESSURE, z);
Alexandra Chind5591a62013-02-07 12:59:15 -0800813
Alexandra Chin669d27c2012-12-24 15:42:30 +0800814#ifdef REPORT_2D_W
815 input_report_abs(rmi4_data->input_dev,
816 ABS_MT_TOUCH_MAJOR, max(wx, wy));
817 input_report_abs(rmi4_data->input_dev,
818 ABS_MT_TOUCH_MINOR, min(wx, wy));
819#endif
Alexandra Chind5591a62013-02-07 12:59:15 -0800820#ifndef TYPE_B_PROTOCOL
Alexandra Chin669d27c2012-12-24 15:42:30 +0800821 input_mt_sync(rmi4_data->input_dev);
822#endif
823 touch_count++;
824 }
825 }
826
Amy Malocheecfb4892013-02-06 13:30:36 -0800827 input_report_key(rmi4_data->input_dev, BTN_TOUCH, touch_count > 0);
828 input_report_key(rmi4_data->input_dev,
829 BTN_TOOL_FINGER, touch_count > 0);
830
Alexandra Chin669d27c2012-12-24 15:42:30 +0800831#ifndef TYPE_B_PROTOCOL
832 if (!touch_count)
833 input_mt_sync(rmi4_data->input_dev);
834#else
835 /* sync after groups of events */
836 #ifdef KERNEL_ABOVE_3_7
837 input_mt_sync_frame(rmi4_data->input_dev);
838 #endif
839#endif
840
841 input_sync(rmi4_data->input_dev);
842
843 return touch_count;
844}
845
846static void synaptics_rmi4_f1a_report(struct synaptics_rmi4_data *rmi4_data,
847 struct synaptics_rmi4_fn *fhandler)
848{
849 int retval;
850 unsigned char button;
851 unsigned char index;
852 unsigned char shift;
853 unsigned char status;
854 unsigned char *data;
855 unsigned short data_addr = fhandler->full_addr.data_base;
856 struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
857 static unsigned char do_once = 1;
858 static bool current_status[MAX_NUMBER_OF_BUTTONS];
859#ifdef NO_0D_WHILE_2D
860 static bool before_2d_status[MAX_NUMBER_OF_BUTTONS];
861 static bool while_2d_status[MAX_NUMBER_OF_BUTTONS];
862#endif
863
864 if (do_once) {
865 memset(current_status, 0, sizeof(current_status));
866#ifdef NO_0D_WHILE_2D
867 memset(before_2d_status, 0, sizeof(before_2d_status));
868 memset(while_2d_status, 0, sizeof(while_2d_status));
869#endif
870 do_once = 0;
871 }
872
873 retval = synaptics_rmi4_i2c_read(rmi4_data,
874 data_addr,
875 f1a->button_data_buffer,
876 f1a->button_bitmask_size);
877 if (retval < 0) {
878 dev_err(&rmi4_data->i2c_client->dev,
879 "%s: Failed to read button data registers\n",
880 __func__);
881 return;
882 }
883
884 data = f1a->button_data_buffer;
885
886 for (button = 0; button < f1a->valid_button_count; button++) {
887 index = button / 8;
888 shift = button % 8;
889 status = ((data[index] >> shift) & MASK_1BIT);
890
891 if (current_status[button] == status)
892 continue;
893 else
894 current_status[button] = status;
895
896 dev_dbg(&rmi4_data->i2c_client->dev,
897 "%s: Button %d (code %d) ->%d\n",
898 __func__, button,
899 f1a->button_map[button],
900 status);
901#ifdef NO_0D_WHILE_2D
902 if (rmi4_data->fingers_on_2d == false) {
903 if (status == 1) {
904 before_2d_status[button] = 1;
905 } else {
906 if (while_2d_status[button] == 1) {
907 while_2d_status[button] = 0;
908 continue;
909 } else {
910 before_2d_status[button] = 0;
911 }
912 }
913 input_report_key(rmi4_data->input_dev,
914 f1a->button_map[button],
915 status);
916 } else {
917 if (before_2d_status[button] == 1) {
918 before_2d_status[button] = 0;
919 input_report_key(rmi4_data->input_dev,
920 f1a->button_map[button],
921 status);
922 } else {
923 if (status == 1)
924 while_2d_status[button] = 1;
925 else
926 while_2d_status[button] = 0;
927 }
928 }
929#else
930 input_report_key(rmi4_data->input_dev,
931 f1a->button_map[button],
932 status);
933#endif
934 }
935
936 input_sync(rmi4_data->input_dev);
937
938 return;
939}
940
941 /**
942 * synaptics_rmi4_report_touch()
943 *
944 * Called by synaptics_rmi4_sensor_report().
945 *
946 * This function calls the appropriate finger data reporting function
947 * based on the function handler it receives and returns the number of
948 * fingers detected.
949 */
950static void synaptics_rmi4_report_touch(struct synaptics_rmi4_data *rmi4_data,
951 struct synaptics_rmi4_fn *fhandler,
952 unsigned char *touch_count)
953{
954 unsigned char touch_count_2d;
955
956 dev_dbg(&rmi4_data->i2c_client->dev,
957 "%s: Function %02x reporting\n",
958 __func__, fhandler->fn_number);
959
960 switch (fhandler->fn_number) {
961 case SYNAPTICS_RMI4_F11:
962 touch_count_2d = synaptics_rmi4_f11_abs_report(rmi4_data,
963 fhandler);
964
965 *touch_count += touch_count_2d;
966
967 if (touch_count_2d)
968 rmi4_data->fingers_on_2d = true;
969 else
970 rmi4_data->fingers_on_2d = false;
971 break;
972
973 case SYNAPTICS_RMI4_F1A:
974 synaptics_rmi4_f1a_report(rmi4_data, fhandler);
975 break;
976
977 default:
978 break;
979 }
980
981 return;
982}
983
984 /**
985 * synaptics_rmi4_sensor_report()
986 *
987 * Called by synaptics_rmi4_irq().
988 *
989 * This function determines the interrupt source(s) from the sensor
990 * and calls synaptics_rmi4_report_touch() with the appropriate
991 * function handler for each function with valid data inputs.
992 */
993static int synaptics_rmi4_sensor_report(struct synaptics_rmi4_data *rmi4_data)
994{
995 int retval;
996 unsigned char touch_count = 0;
997 unsigned char intr[MAX_INTR_REGISTERS];
998 struct synaptics_rmi4_fn *fhandler;
999 struct synaptics_rmi4_exp_fn *exp_fhandler;
1000 struct synaptics_rmi4_device_info *rmi;
1001
1002 rmi = &(rmi4_data->rmi4_mod_info);
1003
1004 /*
1005 * Get interrupt status information from F01 Data1 register to
1006 * determine the source(s) that are flagging the interrupt.
1007 */
1008 retval = synaptics_rmi4_i2c_read(rmi4_data,
1009 rmi4_data->f01_data_base_addr + 1,
1010 intr,
1011 rmi4_data->num_of_intr_regs);
1012 if (retval < 0)
1013 return retval;
1014
1015 /*
1016 * Traverse the function handler list and service the source(s)
1017 * of the interrupt accordingly.
1018 */
Alexandra Chind5591a62013-02-07 12:59:15 -08001019 if (!list_empty(&rmi->support_fn_list)) {
1020 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
1021 if (fhandler->num_of_data_sources) {
1022 if (fhandler->intr_mask &
1023 intr[fhandler->intr_reg_num]) {
1024 synaptics_rmi4_report_touch(rmi4_data,
1025 fhandler, &touch_count);
1026 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001027 }
1028 }
1029 }
1030
1031 mutex_lock(&exp_fn_list_mutex);
1032 if (!list_empty(&exp_fn_list)) {
1033 list_for_each_entry(exp_fhandler, &exp_fn_list, link) {
1034 if (exp_fhandler->inserted &&
1035 (exp_fhandler->func_attn != NULL))
1036 exp_fhandler->func_attn(rmi4_data, intr[0]);
1037 }
1038 }
1039 mutex_unlock(&exp_fn_list_mutex);
1040
1041 return touch_count;
1042}
1043
1044 /**
1045 * synaptics_rmi4_irq()
1046 *
1047 * Called by the kernel when an interrupt occurs (when the sensor
1048 * asserts the attention irq).
1049 *
1050 * This function is the ISR thread and handles the acquisition
1051 * and the reporting of finger data when the presence of fingers
1052 * is detected.
1053 */
1054static irqreturn_t synaptics_rmi4_irq(int irq, void *data)
1055{
1056 struct synaptics_rmi4_data *rmi4_data = data;
1057
1058 synaptics_rmi4_sensor_report(rmi4_data);
1059
1060 return IRQ_HANDLED;
1061}
1062
Amy Malocheecfb4892013-02-06 13:30:36 -08001063static int synaptics_rmi4_parse_dt(struct device *dev,
1064 struct synaptics_rmi4_platform_data *rmi4_pdata)
1065{
1066 struct device_node *np = dev->of_node;
1067 struct property *prop;
1068 u32 temp_val, num_buttons;
1069 u32 button_map[MAX_NUMBER_OF_BUTTONS];
1070 int rc, i;
1071
1072 rmi4_pdata->i2c_pull_up = of_property_read_bool(np,
1073 "synaptics,i2c-pull-up");
Amy Malocheecfb4892013-02-06 13:30:36 -08001074 rmi4_pdata->x_flip = of_property_read_bool(np, "synaptics,x-flip");
1075 rmi4_pdata->y_flip = of_property_read_bool(np, "synaptics,y-flip");
1076
1077 rc = of_property_read_u32(np, "synaptics,panel-x", &temp_val);
1078 if (rc && (rc != -EINVAL)) {
1079 dev_err(dev, "Unable to read panel X dimension\n");
1080 return rc;
1081 } else {
1082 rmi4_pdata->panel_x = temp_val;
1083 }
1084
1085 rc = of_property_read_u32(np, "synaptics,panel-y", &temp_val);
1086 if (rc && (rc != -EINVAL)) {
1087 dev_err(dev, "Unable to read panel Y dimension\n");
1088 return rc;
1089 } else {
1090 rmi4_pdata->panel_y = temp_val;
1091 }
1092
Amy Malochecb835832013-03-26 18:06:05 -07001093 rc = of_property_read_string(np, "synaptics,fw-image-name",
1094 &rmi4_pdata->fw_image_name);
1095 if (rc && (rc != -EINVAL)) {
1096 dev_err(dev, "Unable to read fw image name\n");
1097 return rc;
1098 }
1099
Amy Malocheecfb4892013-02-06 13:30:36 -08001100 /* reset, irq gpio info */
1101 rmi4_pdata->reset_gpio = of_get_named_gpio_flags(np,
1102 "synaptics,reset-gpio", 0, &rmi4_pdata->reset_flags);
1103 rmi4_pdata->irq_gpio = of_get_named_gpio_flags(np,
1104 "synaptics,irq-gpio", 0, &rmi4_pdata->irq_flags);
1105
1106 prop = of_find_property(np, "synaptics,button-map", NULL);
1107 if (prop) {
1108 num_buttons = prop->length / sizeof(temp_val);
1109
1110 rmi4_pdata->capacitance_button_map = devm_kzalloc(dev,
1111 sizeof(*rmi4_pdata->capacitance_button_map),
1112 GFP_KERNEL);
1113 if (!rmi4_pdata->capacitance_button_map)
1114 return -ENOMEM;
1115
1116 rmi4_pdata->capacitance_button_map->map = devm_kzalloc(dev,
1117 sizeof(*rmi4_pdata->capacitance_button_map->map) *
1118 MAX_NUMBER_OF_BUTTONS, GFP_KERNEL);
1119 if (!rmi4_pdata->capacitance_button_map->map)
1120 return -ENOMEM;
1121
1122 if (num_buttons <= MAX_NUMBER_OF_BUTTONS) {
1123 rc = of_property_read_u32_array(np,
1124 "synaptics,button-map", button_map,
1125 num_buttons);
1126 if (rc) {
1127 dev_err(dev, "Unable to read key codes\n");
1128 return rc;
1129 }
1130 for (i = 0; i < num_buttons; i++)
1131 rmi4_pdata->capacitance_button_map->map[i] =
1132 button_map[i];
1133 rmi4_pdata->capacitance_button_map->nbuttons =
1134 num_buttons;
1135 } else {
1136 return -EINVAL;
1137 }
1138 }
1139 return 0;
1140}
1141
Alexandra Chin669d27c2012-12-24 15:42:30 +08001142 /**
1143 * synaptics_rmi4_irq_enable()
1144 *
1145 * Called by synaptics_rmi4_probe() and the power management functions
1146 * in this driver and also exported to other expansion Function modules
1147 * such as rmi_dev.
1148 *
1149 * This function handles the enabling and disabling of the attention
1150 * irq including the setting up of the ISR thread.
1151 */
1152static int synaptics_rmi4_irq_enable(struct synaptics_rmi4_data *rmi4_data,
1153 bool enable)
1154{
1155 int retval = 0;
Amy Maloche5c7b5d52013-04-29 10:40:41 -07001156 unsigned char *intr_status;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001157
1158 if (enable) {
1159 if (rmi4_data->irq_enabled)
1160 return retval;
1161
Amy Maloche5c7b5d52013-04-29 10:40:41 -07001162 intr_status = kzalloc(rmi4_data->num_of_intr_regs, GFP_KERNEL);
1163 if (!intr_status) {
1164 dev_err(&rmi4_data->i2c_client->dev,
1165 "%s: Failed to alloc memory\n",
1166 __func__);
1167 return -ENOMEM;
1168 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001169 /* Clear interrupts first */
1170 retval = synaptics_rmi4_i2c_read(rmi4_data,
1171 rmi4_data->f01_data_base_addr + 1,
Amy Maloche5c7b5d52013-04-29 10:40:41 -07001172 intr_status,
Alexandra Chin669d27c2012-12-24 15:42:30 +08001173 rmi4_data->num_of_intr_regs);
Amy Maloche5c7b5d52013-04-29 10:40:41 -07001174 kfree(intr_status);
Alexandra Chin669d27c2012-12-24 15:42:30 +08001175 if (retval < 0)
1176 return retval;
1177
Alexandra Chinfb798fa2013-03-26 17:15:38 -07001178 enable_irq(rmi4_data->irq);
Alexandra Chin669d27c2012-12-24 15:42:30 +08001179
1180 rmi4_data->irq_enabled = true;
1181 } else {
1182 if (rmi4_data->irq_enabled) {
1183 disable_irq(rmi4_data->irq);
Alexandra Chin669d27c2012-12-24 15:42:30 +08001184 rmi4_data->irq_enabled = false;
1185 }
1186 }
1187
1188 return retval;
1189}
1190
1191 /**
1192 * synaptics_rmi4_f11_init()
1193 *
1194 * Called by synaptics_rmi4_query_device().
1195 *
1196 * This funtion parses information from the Function 11 registers
1197 * and determines the number of fingers supported, x and y data ranges,
1198 * offset to the associated interrupt status register, interrupt bit
1199 * mask, and gathers finger data acquisition capabilities from the query
1200 * registers.
1201 */
1202static int synaptics_rmi4_f11_init(struct synaptics_rmi4_data *rmi4_data,
1203 struct synaptics_rmi4_fn *fhandler,
1204 struct synaptics_rmi4_fn_desc *fd,
1205 unsigned int intr_count)
1206{
1207 int retval;
1208 unsigned char ii;
1209 unsigned char intr_offset;
1210 unsigned char abs_data_size;
1211 unsigned char abs_data_blk_size;
1212 unsigned char query[F11_STD_QUERY_LEN];
1213 unsigned char control[F11_STD_CTRL_LEN];
1214
1215 fhandler->fn_number = fd->fn_number;
1216 fhandler->num_of_data_sources = fd->intr_src_count;
1217
1218 retval = synaptics_rmi4_i2c_read(rmi4_data,
1219 fhandler->full_addr.query_base,
1220 query,
1221 sizeof(query));
1222 if (retval < 0)
1223 return retval;
1224
1225 /* Maximum number of fingers supported */
1226 if ((query[1] & MASK_3BIT) <= 4)
1227 fhandler->num_of_data_points = (query[1] & MASK_3BIT) + 1;
1228 else if ((query[1] & MASK_3BIT) == 5)
1229 fhandler->num_of_data_points = 10;
1230
1231 rmi4_data->num_of_fingers = fhandler->num_of_data_points;
1232
1233 retval = synaptics_rmi4_i2c_read(rmi4_data,
1234 fhandler->full_addr.ctrl_base,
1235 control,
1236 sizeof(control));
1237 if (retval < 0)
1238 return retval;
1239
1240 /* Maximum x and y */
1241 rmi4_data->sensor_max_x = ((control[6] & MASK_8BIT) << 0) |
1242 ((control[7] & MASK_4BIT) << 8);
1243 rmi4_data->sensor_max_y = ((control[8] & MASK_8BIT) << 0) |
1244 ((control[9] & MASK_4BIT) << 8);
1245 dev_dbg(&rmi4_data->i2c_client->dev,
1246 "%s: Function %02x max x = %d max y = %d\n",
1247 __func__, fhandler->fn_number,
1248 rmi4_data->sensor_max_x,
1249 rmi4_data->sensor_max_y);
1250
1251 fhandler->intr_reg_num = (intr_count + 7) / 8;
1252 if (fhandler->intr_reg_num != 0)
1253 fhandler->intr_reg_num -= 1;
1254
1255 /* Set an enable bit for each data source */
1256 intr_offset = intr_count % 8;
1257 fhandler->intr_mask = 0;
1258 for (ii = intr_offset;
1259 ii < ((fd->intr_src_count & MASK_3BIT) +
1260 intr_offset);
1261 ii++)
1262 fhandler->intr_mask |= 1 << ii;
1263
1264 abs_data_size = query[5] & MASK_2BIT;
1265 abs_data_blk_size = 3 + (2 * (abs_data_size == 0 ? 1 : 0));
1266 fhandler->size_of_data_register_block = abs_data_blk_size;
1267
1268 return retval;
1269}
1270
1271static int synaptics_rmi4_f1a_alloc_mem(struct synaptics_rmi4_data *rmi4_data,
1272 struct synaptics_rmi4_fn *fhandler)
1273{
1274 int retval;
1275 struct synaptics_rmi4_f1a_handle *f1a;
1276
1277 f1a = kzalloc(sizeof(*f1a), GFP_KERNEL);
1278 if (!f1a) {
1279 dev_err(&rmi4_data->i2c_client->dev,
1280 "%s: Failed to alloc mem for function handle\n",
1281 __func__);
1282 return -ENOMEM;
1283 }
1284
1285 fhandler->data = (void *)f1a;
1286
1287 retval = synaptics_rmi4_i2c_read(rmi4_data,
1288 fhandler->full_addr.query_base,
1289 f1a->button_query.data,
1290 sizeof(f1a->button_query.data));
1291 if (retval < 0) {
1292 dev_err(&rmi4_data->i2c_client->dev,
1293 "%s: Failed to read query registers\n",
1294 __func__);
1295 return retval;
1296 }
1297
1298 f1a->button_count = f1a->button_query.max_button_count + 1;
1299 f1a->button_bitmask_size = (f1a->button_count + 7) / 8;
1300
1301 f1a->button_data_buffer = kcalloc(f1a->button_bitmask_size,
1302 sizeof(*(f1a->button_data_buffer)), GFP_KERNEL);
1303 if (!f1a->button_data_buffer) {
1304 dev_err(&rmi4_data->i2c_client->dev,
1305 "%s: Failed to alloc mem for data buffer\n",
1306 __func__);
1307 return -ENOMEM;
1308 }
1309
1310 f1a->button_map = kcalloc(f1a->button_count,
1311 sizeof(*(f1a->button_map)), GFP_KERNEL);
1312 if (!f1a->button_map) {
1313 dev_err(&rmi4_data->i2c_client->dev,
1314 "%s: Failed to alloc mem for button map\n",
1315 __func__);
1316 return -ENOMEM;
1317 }
1318
1319 return 0;
1320}
1321
1322static int synaptics_rmi4_capacitance_button_map(
1323 struct synaptics_rmi4_data *rmi4_data,
1324 struct synaptics_rmi4_fn *fhandler)
1325{
1326 unsigned char ii;
1327 struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
1328 const struct synaptics_rmi4_platform_data *pdata = rmi4_data->board;
1329
1330 if (!pdata->capacitance_button_map) {
1331 dev_err(&rmi4_data->i2c_client->dev,
Alexandra Chind5591a62013-02-07 12:59:15 -08001332 "%s: capacitance_button_map is" \
1333 "NULL in board file\n",
Alexandra Chin669d27c2012-12-24 15:42:30 +08001334 __func__);
1335 return -ENODEV;
1336 } else if (!pdata->capacitance_button_map->map) {
1337 dev_err(&rmi4_data->i2c_client->dev,
1338 "%s: Button map is missing in board file\n",
1339 __func__);
1340 return -ENODEV;
1341 } else {
1342 if (pdata->capacitance_button_map->nbuttons !=
1343 f1a->button_count) {
1344 f1a->valid_button_count = min(f1a->button_count,
1345 pdata->capacitance_button_map->nbuttons);
1346 } else {
1347 f1a->valid_button_count = f1a->button_count;
1348 }
1349
1350 for (ii = 0; ii < f1a->valid_button_count; ii++)
1351 f1a->button_map[ii] =
1352 pdata->capacitance_button_map->map[ii];
1353 }
1354
1355 return 0;
1356}
1357
1358static void synaptics_rmi4_f1a_kfree(struct synaptics_rmi4_fn *fhandler)
1359{
1360 struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
1361
1362 if (f1a) {
1363 kfree(f1a->button_data_buffer);
1364 kfree(f1a->button_map);
1365 kfree(f1a);
1366 fhandler->data = NULL;
1367 }
1368
1369 return;
1370}
1371
1372static int synaptics_rmi4_f1a_init(struct synaptics_rmi4_data *rmi4_data,
1373 struct synaptics_rmi4_fn *fhandler,
1374 struct synaptics_rmi4_fn_desc *fd,
1375 unsigned int intr_count)
1376{
1377 int retval;
1378 unsigned char ii;
1379 unsigned short intr_offset;
1380
1381 fhandler->fn_number = fd->fn_number;
1382 fhandler->num_of_data_sources = fd->intr_src_count;
1383
1384 fhandler->intr_reg_num = (intr_count + 7) / 8;
1385 if (fhandler->intr_reg_num != 0)
1386 fhandler->intr_reg_num -= 1;
1387
1388 /* Set an enable bit for each data source */
1389 intr_offset = intr_count % 8;
1390 fhandler->intr_mask = 0;
1391 for (ii = intr_offset;
1392 ii < ((fd->intr_src_count & MASK_3BIT) +
1393 intr_offset);
1394 ii++)
1395 fhandler->intr_mask |= 1 << ii;
1396
1397 retval = synaptics_rmi4_f1a_alloc_mem(rmi4_data, fhandler);
1398 if (retval < 0)
1399 goto error_exit;
1400
1401 retval = synaptics_rmi4_capacitance_button_map(rmi4_data, fhandler);
1402 if (retval < 0)
1403 goto error_exit;
1404
1405 rmi4_data->button_0d_enabled = 1;
1406
1407 return 0;
1408
1409error_exit:
1410 synaptics_rmi4_f1a_kfree(fhandler);
1411
1412 return retval;
1413}
1414
1415static int synaptics_rmi4_alloc_fh(struct synaptics_rmi4_fn **fhandler,
1416 struct synaptics_rmi4_fn_desc *rmi_fd, int page_number)
1417{
Alexandra Chinfb798fa2013-03-26 17:15:38 -07001418 *fhandler = kzalloc(sizeof(**fhandler), GFP_KERNEL);
Alexandra Chin669d27c2012-12-24 15:42:30 +08001419 if (!(*fhandler))
1420 return -ENOMEM;
1421
1422 (*fhandler)->full_addr.data_base =
1423 (rmi_fd->data_base_addr |
1424 (page_number << 8));
1425 (*fhandler)->full_addr.ctrl_base =
1426 (rmi_fd->ctrl_base_addr |
1427 (page_number << 8));
1428 (*fhandler)->full_addr.cmd_base =
1429 (rmi_fd->cmd_base_addr |
1430 (page_number << 8));
1431 (*fhandler)->full_addr.query_base =
1432 (rmi_fd->query_base_addr |
1433 (page_number << 8));
Alexandra Chinfb798fa2013-03-26 17:15:38 -07001434 (*fhandler)->fn_number = rmi_fd->fn_number;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001435
1436 return 0;
1437}
1438
Alexandra Chind5591a62013-02-07 12:59:15 -08001439
1440 /**
1441 * synaptics_rmi4_query_device_info()
1442 *
1443 * Called by synaptics_rmi4_query_device().
1444 *
1445 */
1446static int synaptics_rmi4_query_device_info(
1447 struct synaptics_rmi4_data *rmi4_data)
1448{
1449 int retval;
1450 unsigned char f01_query[F01_STD_QUERY_LEN];
1451 struct synaptics_rmi4_device_info *rmi = &(rmi4_data->rmi4_mod_info);
1452
1453 retval = synaptics_rmi4_i2c_read(rmi4_data,
1454 rmi4_data->f01_query_base_addr,
1455 f01_query,
1456 sizeof(f01_query));
1457 if (retval < 0)
1458 return retval;
1459
1460 /* RMI Version 4.0 currently supported */
1461 rmi->version_major = 4;
1462 rmi->version_minor = 0;
1463
1464 rmi->manufacturer_id = f01_query[0];
1465 rmi->product_props = f01_query[1];
1466 rmi->product_info[0] = f01_query[2] & MASK_7BIT;
1467 rmi->product_info[1] = f01_query[3] & MASK_7BIT;
1468 rmi->date_code[0] = f01_query[4] & MASK_5BIT;
1469 rmi->date_code[1] = f01_query[5] & MASK_4BIT;
1470 rmi->date_code[2] = f01_query[6] & MASK_5BIT;
1471 rmi->tester_id = ((f01_query[7] & MASK_7BIT) << 8) |
1472 (f01_query[8] & MASK_7BIT);
1473 rmi->serial_number = ((f01_query[9] & MASK_7BIT) << 8) |
1474 (f01_query[10] & MASK_7BIT);
1475 memcpy(rmi->product_id_string, &f01_query[11], 10);
1476
1477 if (rmi->manufacturer_id != 1) {
1478 dev_err(&rmi4_data->i2c_client->dev,
1479 "%s: Non-Synaptics device found, manufacturer ID = %d\n",
1480 __func__, rmi->manufacturer_id);
1481 }
1482
1483 retval = synaptics_rmi4_i2c_read(rmi4_data,
1484 rmi4_data->f01_query_base_addr + F01_BUID_ID_OFFSET,
1485 rmi->build_id,
1486 sizeof(rmi->build_id));
1487 if (retval < 0) {
1488 dev_err(&rmi4_data->i2c_client->dev,
1489 "%s: Failed to read firmware build id (code %d)\n",
1490 __func__, retval);
1491 return retval;
1492 }
Alexandra Chinfb798fa2013-03-26 17:15:38 -07001493 return 0;
Alexandra Chind5591a62013-02-07 12:59:15 -08001494}
1495
Alexandra Chin669d27c2012-12-24 15:42:30 +08001496 /**
1497 * synaptics_rmi4_query_device()
1498 *
1499 * Called by synaptics_rmi4_probe().
1500 *
1501 * This funtion scans the page description table, records the offsets
1502 * to the register types of Function $01, sets up the function handlers
1503 * for Function $11 and Function $12, determines the number of interrupt
1504 * sources from the sensor, adds valid Functions with data inputs to the
1505 * Function linked list, parses information from the query registers of
1506 * Function $01, and enables the interrupt sources from the valid Functions
1507 * with data inputs.
1508 */
1509static int synaptics_rmi4_query_device(struct synaptics_rmi4_data *rmi4_data)
1510{
1511 int retval;
1512 unsigned char ii;
1513 unsigned char page_number;
1514 unsigned char intr_count = 0;
1515 unsigned char data_sources = 0;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001516 unsigned short pdt_entry_addr;
1517 unsigned short intr_addr;
1518 struct synaptics_rmi4_f01_device_status status;
1519 struct synaptics_rmi4_fn_desc rmi_fd;
1520 struct synaptics_rmi4_fn *fhandler;
1521 struct synaptics_rmi4_device_info *rmi;
1522
1523 rmi = &(rmi4_data->rmi4_mod_info);
1524
1525 INIT_LIST_HEAD(&rmi->support_fn_list);
1526
1527 /* Scan the page description tables of the pages to service */
1528 for (page_number = 0; page_number < PAGES_TO_SERVICE; page_number++) {
1529 for (pdt_entry_addr = PDT_START; pdt_entry_addr > PDT_END;
1530 pdt_entry_addr -= PDT_ENTRY_SIZE) {
1531 pdt_entry_addr |= (page_number << 8);
1532
1533 retval = synaptics_rmi4_i2c_read(rmi4_data,
1534 pdt_entry_addr,
1535 (unsigned char *)&rmi_fd,
1536 sizeof(rmi_fd));
1537 if (retval < 0)
1538 return retval;
1539
1540 fhandler = NULL;
1541
1542 if (rmi_fd.fn_number == 0) {
1543 dev_dbg(&rmi4_data->i2c_client->dev,
1544 "%s: Reached end of PDT\n",
1545 __func__);
1546 break;
1547 }
1548
1549 dev_dbg(&rmi4_data->i2c_client->dev,
1550 "%s: F%02x found (page %d)\n",
1551 __func__, rmi_fd.fn_number,
1552 page_number);
1553
1554 switch (rmi_fd.fn_number) {
1555 case SYNAPTICS_RMI4_F01:
1556 rmi4_data->f01_query_base_addr =
1557 rmi_fd.query_base_addr;
1558 rmi4_data->f01_ctrl_base_addr =
1559 rmi_fd.ctrl_base_addr;
1560 rmi4_data->f01_data_base_addr =
1561 rmi_fd.data_base_addr;
1562 rmi4_data->f01_cmd_base_addr =
1563 rmi_fd.cmd_base_addr;
1564
Alexandra Chind5591a62013-02-07 12:59:15 -08001565 retval =
1566 synaptics_rmi4_query_device_info(rmi4_data);
1567 if (retval < 0)
1568 return retval;
1569
Alexandra Chin669d27c2012-12-24 15:42:30 +08001570 retval = synaptics_rmi4_i2c_read(rmi4_data,
1571 rmi4_data->f01_data_base_addr,
1572 status.data,
1573 sizeof(status.data));
1574 if (retval < 0)
1575 return retval;
1576
Alexandra Chin3a747522013-04-15 12:13:38 -07001577 while (status.status_code == STATUS_CRC_IN_PROGRESS) {
1578 msleep(1);
1579 retval = synaptics_rmi4_i2c_read(rmi4_data,
1580 rmi4_data->f01_data_base_addr,
1581 status.data,
1582 sizeof(status.data));
1583 if (retval < 0)
1584 return retval;
1585 }
1586
Alexandra Chin669d27c2012-12-24 15:42:30 +08001587 if (status.flash_prog == 1) {
1588 pr_notice("%s: In flash prog mode, status = 0x%02x\n",
1589 __func__,
1590 status.status_code);
1591 goto flash_prog_mode;
1592 }
Alexandra Chind5591a62013-02-07 12:59:15 -08001593 break;
1594
Alexandra Chin669d27c2012-12-24 15:42:30 +08001595 case SYNAPTICS_RMI4_F11:
1596 if (rmi_fd.intr_src_count == 0)
1597 break;
1598
1599 retval = synaptics_rmi4_alloc_fh(&fhandler,
1600 &rmi_fd, page_number);
1601 if (retval < 0) {
1602 dev_err(&rmi4_data->i2c_client->dev,
1603 "%s: Failed to alloc for F%d\n",
1604 __func__,
1605 rmi_fd.fn_number);
1606 return retval;
1607 }
1608
1609 retval = synaptics_rmi4_f11_init(rmi4_data,
1610 fhandler, &rmi_fd, intr_count);
1611 if (retval < 0)
1612 return retval;
1613 break;
1614
1615 case SYNAPTICS_RMI4_F1A:
1616 if (rmi_fd.intr_src_count == 0)
1617 break;
1618
1619 retval = synaptics_rmi4_alloc_fh(&fhandler,
1620 &rmi_fd, page_number);
1621 if (retval < 0) {
1622 dev_err(&rmi4_data->i2c_client->dev,
1623 "%s: Failed to alloc for F%d\n",
1624 __func__,
1625 rmi_fd.fn_number);
1626 return retval;
1627 }
1628
1629 retval = synaptics_rmi4_f1a_init(rmi4_data,
1630 fhandler, &rmi_fd, intr_count);
1631 if (retval < 0)
1632 return retval;
1633 break;
1634 }
1635
1636 /* Accumulate the interrupt count */
1637 intr_count += (rmi_fd.intr_src_count & MASK_3BIT);
1638
1639 if (fhandler && rmi_fd.intr_src_count) {
1640 list_add_tail(&fhandler->link,
1641 &rmi->support_fn_list);
1642 }
1643 }
1644 }
1645
1646flash_prog_mode:
1647 rmi4_data->num_of_intr_regs = (intr_count + 7) / 8;
1648 dev_dbg(&rmi4_data->i2c_client->dev,
1649 "%s: Number of interrupt registers = %d\n",
1650 __func__, rmi4_data->num_of_intr_regs);
1651
Alexandra Chin669d27c2012-12-24 15:42:30 +08001652 memset(rmi4_data->intr_mask, 0x00, sizeof(rmi4_data->intr_mask));
1653
1654 /*
1655 * Map out the interrupt bit masks for the interrupt sources
1656 * from the registered function handlers.
1657 */
Alexandra Chind5591a62013-02-07 12:59:15 -08001658 if (!list_empty(&rmi->support_fn_list)) {
1659 list_for_each_entry(fhandler, &rmi->support_fn_list, link)
1660 data_sources += fhandler->num_of_data_sources;
1661 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001662 if (data_sources) {
Alexandra Chind5591a62013-02-07 12:59:15 -08001663 if (!list_empty(&rmi->support_fn_list)) {
1664 list_for_each_entry(fhandler,
1665 &rmi->support_fn_list, link) {
1666 if (fhandler->num_of_data_sources) {
1667 rmi4_data->intr_mask[fhandler->intr_reg_num] |=
1668 fhandler->intr_mask;
1669 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001670 }
1671 }
1672 }
1673
1674 /* Enable the interrupt sources */
1675 for (ii = 0; ii < rmi4_data->num_of_intr_regs; ii++) {
1676 if (rmi4_data->intr_mask[ii] != 0x00) {
1677 dev_dbg(&rmi4_data->i2c_client->dev,
1678 "%s: Interrupt enable mask %d = 0x%02x\n",
1679 __func__, ii, rmi4_data->intr_mask[ii]);
1680 intr_addr = rmi4_data->f01_ctrl_base_addr + 1 + ii;
1681 retval = synaptics_rmi4_i2c_write(rmi4_data,
1682 intr_addr,
1683 &(rmi4_data->intr_mask[ii]),
1684 sizeof(rmi4_data->intr_mask[ii]));
1685 if (retval < 0)
1686 return retval;
1687 }
1688 }
1689
1690 return 0;
1691}
1692
Alexandra Chinc556cf02013-03-19 17:46:05 -07001693static int synaptics_rmi4_reset_command(struct synaptics_rmi4_data *rmi4_data)
Alexandra Chin669d27c2012-12-24 15:42:30 +08001694{
1695 int retval;
Alexandra Chinc556cf02013-03-19 17:46:05 -07001696 int page_number;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001697 unsigned char command = 0x01;
Alexandra Chinc556cf02013-03-19 17:46:05 -07001698 unsigned short pdt_entry_addr;
1699 struct synaptics_rmi4_fn_desc rmi_fd;
1700 bool done = false;
Alexandra Chin669d27c2012-12-24 15:42:30 +08001701
Alexandra Chinc556cf02013-03-19 17:46:05 -07001702 /* Scan the page description tables of the pages to service */
1703 for (page_number = 0; page_number < PAGES_TO_SERVICE; page_number++) {
1704 for (pdt_entry_addr = PDT_START; pdt_entry_addr > PDT_END;
1705 pdt_entry_addr -= PDT_ENTRY_SIZE) {
1706 retval = synaptics_rmi4_i2c_read(rmi4_data,
1707 pdt_entry_addr,
1708 (unsigned char *)&rmi_fd,
1709 sizeof(rmi_fd));
1710 if (retval < 0)
1711 return retval;
1712
1713 if (rmi_fd.fn_number == 0)
1714 break;
1715
1716 switch (rmi_fd.fn_number) {
1717 case SYNAPTICS_RMI4_F01:
1718 rmi4_data->f01_cmd_base_addr =
1719 rmi_fd.cmd_base_addr;
1720 done = true;
1721 break;
1722 }
1723 }
1724 if (done) {
1725 dev_info(&rmi4_data->i2c_client->dev,
1726 "%s: Find F01 in page description table 0x%x\n",
1727 __func__, rmi4_data->f01_cmd_base_addr);
1728 break;
1729 }
1730 }
1731
1732 if (!done) {
1733 dev_err(&rmi4_data->i2c_client->dev,
1734 "%s: Cannot find F01 in page description table\n",
1735 __func__);
1736 return -EINVAL;
1737 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001738
1739 retval = synaptics_rmi4_i2c_write(rmi4_data,
1740 rmi4_data->f01_cmd_base_addr,
1741 &command,
1742 sizeof(command));
1743 if (retval < 0) {
1744 dev_err(&rmi4_data->i2c_client->dev,
1745 "%s: Failed to issue reset command, error = %d\n",
1746 __func__, retval);
1747 return retval;
1748 }
1749
Alexandra Chin3a747522013-04-15 12:13:38 -07001750 msleep(RESET_DELAY);
Alexandra Chinc556cf02013-03-19 17:46:05 -07001751 return retval;
1752};
1753
1754static int synaptics_rmi4_reset_device(struct synaptics_rmi4_data *rmi4_data)
1755{
1756 int retval;
1757 struct synaptics_rmi4_fn *fhandler;
1758 struct synaptics_rmi4_device_info *rmi;
1759
1760 rmi = &(rmi4_data->rmi4_mod_info);
1761
1762 retval = synaptics_rmi4_reset_command(rmi4_data);
1763 if (retval < 0) {
1764 dev_err(&rmi4_data->i2c_client->dev,
1765 "%s: Failed to send command reset\n",
1766 __func__);
1767 return retval;
1768 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001769
Alexandra Chind5591a62013-02-07 12:59:15 -08001770 if (!list_empty(&rmi->support_fn_list)) {
1771 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
1772 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
1773 synaptics_rmi4_f1a_kfree(fhandler);
1774 else
1775 kfree(fhandler->data);
1776 kfree(fhandler);
1777 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001778 }
1779
1780 retval = synaptics_rmi4_query_device(rmi4_data);
1781 if (retval < 0) {
1782 dev_err(&rmi4_data->i2c_client->dev,
1783 "%s: Failed to query device\n",
1784 __func__);
1785 return retval;
1786 }
1787
1788 return 0;
1789}
1790
1791/**
1792* synaptics_rmi4_detection_work()
1793*
1794* Called by the kernel at the scheduled time.
1795*
1796* This function is a self-rearming work thread that checks for the
1797* insertion and removal of other expansion Function modules such as
1798* rmi_dev and calls their initialization and removal callback functions
1799* accordingly.
1800*/
1801static void synaptics_rmi4_detection_work(struct work_struct *work)
1802{
1803 struct synaptics_rmi4_exp_fn *exp_fhandler, *next_list_entry;
1804 struct synaptics_rmi4_data *rmi4_data =
1805 container_of(work, struct synaptics_rmi4_data,
1806 det_work.work);
1807
Alexandra Chin669d27c2012-12-24 15:42:30 +08001808 mutex_lock(&exp_fn_list_mutex);
1809 if (!list_empty(&exp_fn_list)) {
1810 list_for_each_entry_safe(exp_fhandler,
1811 next_list_entry,
1812 &exp_fn_list,
1813 link) {
1814 if ((exp_fhandler->func_init != NULL) &&
1815 (exp_fhandler->inserted == false)) {
1816 exp_fhandler->func_init(rmi4_data);
1817 exp_fhandler->inserted = true;
1818 } else if ((exp_fhandler->func_init == NULL) &&
1819 (exp_fhandler->inserted == true)) {
1820 exp_fhandler->func_remove(rmi4_data);
1821 list_del(&exp_fhandler->link);
1822 kfree(exp_fhandler);
1823 }
1824 }
1825 }
1826 mutex_unlock(&exp_fn_list_mutex);
1827
1828 return;
1829}
1830
1831/**
1832* synaptics_rmi4_new_function()
1833*
1834* Called by other expansion Function modules in their module init and
1835* module exit functions.
1836*
1837* This function is used by other expansion Function modules such as
1838* rmi_dev to register themselves with the driver by providing their
1839* initialization and removal callback function pointers so that they
1840* can be inserted or removed dynamically at module init and exit times,
1841* respectively.
1842*/
1843void synaptics_rmi4_new_function(enum exp_fn fn_type, bool insert,
1844 int (*func_init)(struct synaptics_rmi4_data *rmi4_data),
1845 void (*func_remove)(struct synaptics_rmi4_data *rmi4_data),
1846 void (*func_attn)(struct synaptics_rmi4_data *rmi4_data,
1847 unsigned char intr_mask))
1848{
1849 struct synaptics_rmi4_exp_fn *exp_fhandler;
1850
1851 if (!exp_fn_inited) {
1852 mutex_init(&exp_fn_list_mutex);
1853 INIT_LIST_HEAD(&exp_fn_list);
1854 exp_fn_inited = 1;
1855 }
1856
1857 mutex_lock(&exp_fn_list_mutex);
1858 if (insert) {
1859 exp_fhandler = kzalloc(sizeof(*exp_fhandler), GFP_KERNEL);
1860 if (!exp_fhandler) {
1861 pr_err("%s: Failed to alloc mem for expansion function\n",
1862 __func__);
1863 goto exit;
1864 }
1865 exp_fhandler->fn_type = fn_type;
1866 exp_fhandler->func_init = func_init;
1867 exp_fhandler->func_attn = func_attn;
1868 exp_fhandler->func_remove = func_remove;
1869 exp_fhandler->inserted = false;
1870 list_add_tail(&exp_fhandler->link, &exp_fn_list);
1871 } else {
Alexandra Chind5591a62013-02-07 12:59:15 -08001872 if (!list_empty(&exp_fn_list)) {
1873 list_for_each_entry(exp_fhandler, &exp_fn_list, link) {
1874 if (exp_fhandler->func_init == func_init) {
1875 exp_fhandler->inserted = false;
1876 exp_fhandler->func_init = NULL;
1877 exp_fhandler->func_attn = NULL;
1878 goto exit;
1879 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08001880 }
1881 }
1882 }
1883
1884exit:
1885 mutex_unlock(&exp_fn_list_mutex);
1886
1887 return;
1888}
1889EXPORT_SYMBOL(synaptics_rmi4_new_function);
1890
Amy Maloche1a53b612013-01-18 15:25:15 -08001891
1892static int reg_set_optimum_mode_check(struct regulator *reg, int load_uA)
1893{
1894 return (regulator_count_voltages(reg) > 0) ?
1895 regulator_set_optimum_mode(reg, load_uA) : 0;
1896}
1897
1898static int synaptics_rmi4_regulator_configure(struct synaptics_rmi4_data
1899 *rmi4_data, bool on)
1900{
1901 int retval;
1902
1903 if (on == false)
1904 goto hw_shutdown;
1905
Amy Maloche836c0b02013-06-04 15:49:02 -07001906 rmi4_data->vdd = regulator_get(&rmi4_data->i2c_client->dev,
1907 "vdd");
1908 if (IS_ERR(rmi4_data->vdd)) {
1909 dev_err(&rmi4_data->i2c_client->dev,
1910 "%s: Failed to get vdd regulator\n",
1911 __func__);
1912 return PTR_ERR(rmi4_data->vdd);
1913 }
Amy Maloche1a53b612013-01-18 15:25:15 -08001914
Amy Maloche836c0b02013-06-04 15:49:02 -07001915 if (regulator_count_voltages(rmi4_data->vdd) > 0) {
1916 retval = regulator_set_voltage(rmi4_data->vdd,
1917 RMI4_VTG_MIN_UV, RMI4_VTG_MAX_UV);
1918 if (retval) {
1919 dev_err(&rmi4_data->i2c_client->dev,
1920 "regulator set_vtg failed retval =%d\n",
1921 retval);
1922 goto err_set_vtg_vdd;
Amy Maloche1a53b612013-01-18 15:25:15 -08001923 }
1924 }
1925
1926 if (rmi4_data->board->i2c_pull_up) {
1927 rmi4_data->vcc_i2c = regulator_get(&rmi4_data->i2c_client->dev,
1928 "vcc_i2c");
1929 if (IS_ERR(rmi4_data->vcc_i2c)) {
1930 dev_err(&rmi4_data->i2c_client->dev,
1931 "%s: Failed to get i2c regulator\n",
1932 __func__);
1933 retval = PTR_ERR(rmi4_data->vcc_i2c);
1934 goto err_get_vtg_i2c;
1935 }
1936
1937 if (regulator_count_voltages(rmi4_data->vcc_i2c) > 0) {
1938 retval = regulator_set_voltage(rmi4_data->vcc_i2c,
1939 RMI4_I2C_VTG_MIN_UV, RMI4_I2C_VTG_MAX_UV);
1940 if (retval) {
1941 dev_err(&rmi4_data->i2c_client->dev,
Amy Maloche827e8b02013-02-27 15:25:14 -08001942 "reg set i2c vtg failed retval =%d\n",
Amy Maloche1a53b612013-01-18 15:25:15 -08001943 retval);
1944 goto err_set_vtg_i2c;
1945 }
1946 }
1947 }
Amy Malocheecfb4892013-02-06 13:30:36 -08001948 return 0;
Amy Maloche1a53b612013-01-18 15:25:15 -08001949
1950err_set_vtg_i2c:
1951 if (rmi4_data->board->i2c_pull_up)
1952 regulator_put(rmi4_data->vcc_i2c);
1953err_get_vtg_i2c:
Amy Maloche836c0b02013-06-04 15:49:02 -07001954 if (regulator_count_voltages(rmi4_data->vdd) > 0)
1955 regulator_set_voltage(rmi4_data->vdd, 0,
1956 RMI4_VTG_MAX_UV);
Amy Maloche1a53b612013-01-18 15:25:15 -08001957err_set_vtg_vdd:
Amy Maloche836c0b02013-06-04 15:49:02 -07001958 regulator_put(rmi4_data->vdd);
Amy Maloche1a53b612013-01-18 15:25:15 -08001959 return retval;
1960
1961hw_shutdown:
Amy Maloche836c0b02013-06-04 15:49:02 -07001962 if (regulator_count_voltages(rmi4_data->vdd) > 0)
1963 regulator_set_voltage(rmi4_data->vdd, 0,
1964 RMI4_VTG_MAX_UV);
1965 regulator_put(rmi4_data->vdd);
Amy Maloche1a53b612013-01-18 15:25:15 -08001966 if (rmi4_data->board->i2c_pull_up) {
1967 if (regulator_count_voltages(rmi4_data->vcc_i2c) > 0)
1968 regulator_set_voltage(rmi4_data->vcc_i2c, 0,
1969 RMI4_I2C_VTG_MAX_UV);
1970 regulator_put(rmi4_data->vcc_i2c);
1971 }
1972 return 0;
1973};
1974
1975static int synaptics_rmi4_power_on(struct synaptics_rmi4_data *rmi4_data,
1976 bool on) {
1977 int retval;
1978
1979 if (on == false)
1980 goto power_off;
1981
Amy Maloche836c0b02013-06-04 15:49:02 -07001982 retval = reg_set_optimum_mode_check(rmi4_data->vdd,
1983 RMI4_ACTIVE_LOAD_UA);
1984 if (retval < 0) {
1985 dev_err(&rmi4_data->i2c_client->dev,
1986 "Regulator vdd set_opt failed rc=%d\n",
1987 retval);
1988 return retval;
1989 }
Amy Maloche1a53b612013-01-18 15:25:15 -08001990
Amy Maloche836c0b02013-06-04 15:49:02 -07001991 retval = regulator_enable(rmi4_data->vdd);
1992 if (retval) {
1993 dev_err(&rmi4_data->i2c_client->dev,
1994 "Regulator vdd enable failed rc=%d\n",
1995 retval);
1996 goto error_reg_en_vdd;
Amy Maloche1a53b612013-01-18 15:25:15 -08001997 }
1998
1999 if (rmi4_data->board->i2c_pull_up) {
2000 retval = reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
2001 RMI4_I2C_LOAD_UA);
2002 if (retval < 0) {
2003 dev_err(&rmi4_data->i2c_client->dev,
2004 "Regulator vcc_i2c set_opt failed rc=%d\n",
2005 retval);
2006 goto error_reg_opt_i2c;
2007 }
2008
2009 retval = regulator_enable(rmi4_data->vcc_i2c);
2010 if (retval) {
2011 dev_err(&rmi4_data->i2c_client->dev,
2012 "Regulator vcc_i2c enable failed rc=%d\n",
2013 retval);
2014 goto error_reg_en_vcc_i2c;
2015 }
2016 }
2017 return 0;
2018
2019error_reg_en_vcc_i2c:
2020 if (rmi4_data->board->i2c_pull_up)
2021 reg_set_optimum_mode_check(rmi4_data->vdd, 0);
2022error_reg_opt_i2c:
Amy Maloche836c0b02013-06-04 15:49:02 -07002023 regulator_disable(rmi4_data->vdd);
Amy Maloche1a53b612013-01-18 15:25:15 -08002024error_reg_en_vdd:
Amy Maloche836c0b02013-06-04 15:49:02 -07002025 reg_set_optimum_mode_check(rmi4_data->vdd, 0);
Amy Maloche1a53b612013-01-18 15:25:15 -08002026 return retval;
2027
2028power_off:
Amy Maloche836c0b02013-06-04 15:49:02 -07002029 reg_set_optimum_mode_check(rmi4_data->vdd, 0);
2030 regulator_disable(rmi4_data->vdd);
Amy Maloche1a53b612013-01-18 15:25:15 -08002031 if (rmi4_data->board->i2c_pull_up) {
2032 reg_set_optimum_mode_check(rmi4_data->vcc_i2c, 0);
2033 regulator_disable(rmi4_data->vcc_i2c);
2034 }
2035 return 0;
2036}
2037
Alexandra Chin669d27c2012-12-24 15:42:30 +08002038 /**
2039 * synaptics_rmi4_probe()
2040 *
2041 * Called by the kernel when an association with an I2C device of the
2042 * same name is made (after doing i2c_add_driver).
2043 *
2044 * This funtion allocates and initializes the resources for the driver
2045 * as an input driver, turns on the power to the sensor, queries the
2046 * sensor for its supported Functions and characteristics, registers
2047 * the driver to the input subsystem, sets up the interrupt, handles
2048 * the registration of the early_suspend and late_resume functions,
2049 * and creates a work queue for detection of other expansion Function
2050 * modules.
2051 */
2052static int __devinit synaptics_rmi4_probe(struct i2c_client *client,
2053 const struct i2c_device_id *dev_id)
2054{
Amy Maloche1a53b612013-01-18 15:25:15 -08002055 int retval = 0;
Alexandra Chin669d27c2012-12-24 15:42:30 +08002056 unsigned char ii;
2057 unsigned char attr_count;
2058 struct synaptics_rmi4_f1a_handle *f1a;
2059 struct synaptics_rmi4_fn *fhandler;
2060 struct synaptics_rmi4_data *rmi4_data;
2061 struct synaptics_rmi4_device_info *rmi;
Amy Malocheecfb4892013-02-06 13:30:36 -08002062 struct synaptics_rmi4_platform_data *platform_data =
Alexandra Chin669d27c2012-12-24 15:42:30 +08002063 client->dev.platform_data;
2064
2065 if (!i2c_check_functionality(client->adapter,
2066 I2C_FUNC_SMBUS_BYTE_DATA)) {
2067 dev_err(&client->dev,
2068 "%s: SMBus byte data not supported\n",
2069 __func__);
2070 return -EIO;
2071 }
2072
Amy Malocheecfb4892013-02-06 13:30:36 -08002073 if (client->dev.of_node) {
2074 platform_data = devm_kzalloc(&client->dev,
2075 sizeof(*platform_data),
2076 GFP_KERNEL);
2077 if (!platform_data) {
2078 dev_err(&client->dev, "Failed to allocate memory\n");
2079 return -ENOMEM;
2080 }
2081
2082 retval = synaptics_rmi4_parse_dt(&client->dev, platform_data);
2083 if (retval)
2084 return retval;
2085 } else {
2086 platform_data = client->dev.platform_data;
2087 }
2088
Alexandra Chin669d27c2012-12-24 15:42:30 +08002089 if (!platform_data) {
2090 dev_err(&client->dev,
2091 "%s: No platform data found\n",
2092 __func__);
2093 return -EINVAL;
2094 }
2095
2096 rmi4_data = kzalloc(sizeof(*rmi4_data) * 2, GFP_KERNEL);
2097 if (!rmi4_data) {
2098 dev_err(&client->dev,
2099 "%s: Failed to alloc mem for rmi4_data\n",
2100 __func__);
2101 return -ENOMEM;
2102 }
2103
2104 rmi = &(rmi4_data->rmi4_mod_info);
2105
2106 rmi4_data->input_dev = input_allocate_device();
2107 if (rmi4_data->input_dev == NULL) {
2108 dev_err(&client->dev,
2109 "%s: Failed to allocate input device\n",
2110 __func__);
2111 retval = -ENOMEM;
2112 goto err_input_device;
2113 }
Alexandra Chind5591a62013-02-07 12:59:15 -08002114
Alexandra Chin669d27c2012-12-24 15:42:30 +08002115 rmi4_data->i2c_client = client;
2116 rmi4_data->current_page = MASK_8BIT;
2117 rmi4_data->board = platform_data;
2118 rmi4_data->touch_stopped = false;
2119 rmi4_data->sensor_sleep = false;
2120 rmi4_data->irq_enabled = false;
Amy Malocheb494b9a2013-05-13 18:01:53 -07002121 rmi4_data->fw_updating = false;
Alexandra Chin669d27c2012-12-24 15:42:30 +08002122
2123 rmi4_data->i2c_read = synaptics_rmi4_i2c_read;
2124 rmi4_data->i2c_write = synaptics_rmi4_i2c_write;
2125 rmi4_data->irq_enable = synaptics_rmi4_irq_enable;
2126 rmi4_data->reset_device = synaptics_rmi4_reset_device;
2127
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002128 rmi4_data->flip_x = rmi4_data->board->x_flip;
2129 rmi4_data->flip_y = rmi4_data->board->y_flip;
2130
Amy Malocheb494b9a2013-05-13 18:01:53 -07002131 if (rmi4_data->board->fw_image_name)
2132 snprintf(rmi4_data->fw_image_name, NAME_BUFFER_SIZE, "%s",
2133 rmi4_data->board->fw_image_name);
Amy Malochecb835832013-03-26 18:06:05 -07002134
Alexandra Chin669d27c2012-12-24 15:42:30 +08002135 rmi4_data->input_dev->name = DRIVER_NAME;
2136 rmi4_data->input_dev->phys = INPUT_PHYS_NAME;
2137 rmi4_data->input_dev->id.bustype = BUS_I2C;
Alexandra Chin5d2999d2013-02-22 15:09:29 -08002138 rmi4_data->input_dev->id.product = SYNAPTICS_DSX_DRIVER_PRODUCT;
2139 rmi4_data->input_dev->id.version = SYNAPTICS_DSX_DRIVER_VERSION;
Alexandra Chin669d27c2012-12-24 15:42:30 +08002140 rmi4_data->input_dev->dev.parent = &client->dev;
2141 input_set_drvdata(rmi4_data->input_dev, rmi4_data);
2142
2143 set_bit(EV_SYN, rmi4_data->input_dev->evbit);
2144 set_bit(EV_KEY, rmi4_data->input_dev->evbit);
2145 set_bit(EV_ABS, rmi4_data->input_dev->evbit);
Alexandra Chind5591a62013-02-07 12:59:15 -08002146 set_bit(BTN_TOUCH, rmi4_data->input_dev->keybit);
2147 set_bit(BTN_TOOL_FINGER, rmi4_data->input_dev->keybit);
Alexandra Chin669d27c2012-12-24 15:42:30 +08002148
2149#ifdef INPUT_PROP_DIRECT
2150 set_bit(INPUT_PROP_DIRECT, rmi4_data->input_dev->propbit);
2151#endif
2152
Amy Maloche1a53b612013-01-18 15:25:15 -08002153 retval = synaptics_rmi4_regulator_configure(rmi4_data, true);
2154 if (retval < 0) {
2155 dev_err(&client->dev, "Failed to configure regulators\n");
Amy Maloche946da662013-01-18 16:27:11 -08002156 goto err_reg_configure;
Amy Maloche1a53b612013-01-18 15:25:15 -08002157 }
2158
2159 retval = synaptics_rmi4_power_on(rmi4_data, true);
2160 if (retval < 0) {
2161 dev_err(&client->dev, "Failed to power on\n");
Amy Maloche946da662013-01-18 16:27:11 -08002162 goto err_power_device;
Amy Maloche1a53b612013-01-18 15:25:15 -08002163 }
2164
Amy Maloche946da662013-01-18 16:27:11 -08002165 if (gpio_is_valid(platform_data->irq_gpio)) {
2166 /* configure touchscreen irq gpio */
2167 retval = gpio_request(platform_data->irq_gpio, "rmi4_irq_gpio");
2168 if (retval) {
2169 dev_err(&client->dev, "unable to request gpio [%d]\n",
2170 platform_data->irq_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002171 goto err_irq_gpio_req;
Amy Maloche946da662013-01-18 16:27:11 -08002172 }
2173 retval = gpio_direction_input(platform_data->irq_gpio);
2174 if (retval) {
2175 dev_err(&client->dev,
2176 "unable to set direction for gpio [%d]\n",
2177 platform_data->irq_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002178 goto err_irq_gpio_dir;
Amy Maloche946da662013-01-18 16:27:11 -08002179 }
2180 } else {
2181 dev_err(&client->dev, "irq gpio not provided\n");
Amy Malocheecfb4892013-02-06 13:30:36 -08002182 goto err_irq_gpio_req;
Amy Maloche946da662013-01-18 16:27:11 -08002183 }
2184
2185 if (gpio_is_valid(platform_data->reset_gpio)) {
2186 /* configure touchscreen reset out gpio */
2187 retval = gpio_request(platform_data->reset_gpio,
2188 "rmi4_reset_gpio");
2189 if (retval) {
2190 dev_err(&client->dev, "unable to request gpio [%d]\n",
2191 platform_data->reset_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002192 goto err_irq_gpio_dir;
Amy Maloche946da662013-01-18 16:27:11 -08002193 }
2194
2195 retval = gpio_direction_output(platform_data->reset_gpio, 1);
2196 if (retval) {
2197 dev_err(&client->dev,
2198 "unable to set direction for gpio [%d]\n",
2199 platform_data->reset_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002200 goto err_reset_gpio_dir;
Amy Maloche946da662013-01-18 16:27:11 -08002201 }
2202
2203 gpio_set_value(platform_data->reset_gpio, 0);
2204 usleep(RMI4_GPIO_SLEEP_LOW_US);
2205 gpio_set_value(platform_data->reset_gpio, 1);
Alexandra Chin3a747522013-04-15 12:13:38 -07002206 msleep(RESET_DELAY);
Alexandra Chinc556cf02013-03-19 17:46:05 -07002207 } else
2208 synaptics_rmi4_reset_command(rmi4_data);
Amy Maloche946da662013-01-18 16:27:11 -08002209
2210
Amy Maloche1a53b612013-01-18 15:25:15 -08002211 init_waitqueue_head(&rmi4_data->wait);
2212 mutex_init(&(rmi4_data->rmi4_io_ctrl_mutex));
2213
2214 retval = synaptics_rmi4_query_device(rmi4_data);
2215 if (retval < 0) {
2216 dev_err(&client->dev,
2217 "%s: Failed to query device\n",
2218 __func__);
Amy Malocheecfb4892013-02-06 13:30:36 -08002219 goto err_reset_gpio_dir;
Amy Maloche1a53b612013-01-18 15:25:15 -08002220 }
2221
Amy Malocheecfb4892013-02-06 13:30:36 -08002222 input_set_abs_params(rmi4_data->input_dev,
2223 ABS_MT_POSITION_X, 0,
2224 rmi4_data->sensor_max_x, 0, 0);
2225 input_set_abs_params(rmi4_data->input_dev,
2226 ABS_MT_POSITION_Y, 0,
2227 rmi4_data->sensor_max_y, 0, 0);
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002228 input_set_abs_params(rmi4_data->input_dev,
2229 ABS_PRESSURE, 0, 255, 0, 0);
Amy Malocheecfb4892013-02-06 13:30:36 -08002230#ifdef REPORT_2D_W
2231 input_set_abs_params(rmi4_data->input_dev,
2232 ABS_MT_TOUCH_MAJOR, 0,
2233 MAX_ABS_MT_TOUCH_MAJOR, 0, 0);
2234#endif
2235
2236#ifdef TYPE_B_PROTOCOL
2237 input_mt_init_slots(rmi4_data->input_dev,
2238 rmi4_data->num_of_fingers);
2239#endif
2240
Amy Maloche1a53b612013-01-18 15:25:15 -08002241 i2c_set_clientdata(client, rmi4_data);
2242
Alexandra Chin669d27c2012-12-24 15:42:30 +08002243 f1a = NULL;
Alexandra Chind5591a62013-02-07 12:59:15 -08002244 if (!list_empty(&rmi->support_fn_list)) {
2245 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
2246 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
2247 f1a = fhandler->data;
2248 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08002249 }
2250
2251 if (f1a) {
2252 for (ii = 0; ii < f1a->valid_button_count; ii++) {
2253 set_bit(f1a->button_map[ii],
2254 rmi4_data->input_dev->keybit);
2255 input_set_capability(rmi4_data->input_dev,
2256 EV_KEY, f1a->button_map[ii]);
2257 }
2258 }
2259
2260 retval = input_register_device(rmi4_data->input_dev);
2261 if (retval) {
2262 dev_err(&client->dev,
2263 "%s: Failed to register input device\n",
2264 __func__);
2265 goto err_register_input;
2266 }
2267
Amy Malochef46540d2013-02-15 17:09:43 -08002268 configure_sleep(rmi4_data);
Alexandra Chin669d27c2012-12-24 15:42:30 +08002269
2270 if (!exp_fn_inited) {
2271 mutex_init(&exp_fn_list_mutex);
2272 INIT_LIST_HEAD(&exp_fn_list);
2273 exp_fn_inited = 1;
2274 }
2275
2276 rmi4_data->det_workqueue =
2277 create_singlethread_workqueue("rmi_det_workqueue");
2278 INIT_DELAYED_WORK(&rmi4_data->det_work,
2279 synaptics_rmi4_detection_work);
2280 queue_delayed_work(rmi4_data->det_workqueue,
2281 &rmi4_data->det_work,
2282 msecs_to_jiffies(EXP_FN_DET_INTERVAL));
2283
Alexandra Chin669d27c2012-12-24 15:42:30 +08002284 rmi4_data->irq = gpio_to_irq(platform_data->irq_gpio);
2285
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002286 retval = request_threaded_irq(rmi4_data->irq, NULL,
2287 synaptics_rmi4_irq, platform_data->irq_flags,
2288 DRIVER_NAME, rmi4_data);
2289 rmi4_data->irq_enabled = true;
2290
Alexandra Chin669d27c2012-12-24 15:42:30 +08002291 if (retval < 0) {
2292 dev_err(&client->dev,
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002293 "%s: Failed to create irq thread\n",
Alexandra Chin669d27c2012-12-24 15:42:30 +08002294 __func__);
2295 goto err_enable_irq;
2296 }
2297
2298 for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
2299 retval = sysfs_create_file(&rmi4_data->input_dev->dev.kobj,
2300 &attrs[attr_count].attr);
2301 if (retval < 0) {
2302 dev_err(&client->dev,
2303 "%s: Failed to create sysfs attributes\n",
2304 __func__);
2305 goto err_sysfs;
2306 }
2307 }
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002308 retval = synaptics_rmi4_irq_enable(rmi4_data, true);
2309 if (retval < 0) {
2310 dev_err(&client->dev,
2311 "%s: Failed to enable attention interrupt\n",
2312 __func__);
2313 goto err_sysfs;
2314 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08002315
2316 return retval;
2317
2318err_sysfs:
2319 for (attr_count--; attr_count >= 0; attr_count--) {
2320 sysfs_remove_file(&rmi4_data->input_dev->dev.kobj,
2321 &attrs[attr_count].attr);
2322 }
2323
2324err_enable_irq:
Amy Malocheecfb4892013-02-06 13:30:36 -08002325 cancel_delayed_work_sync(&rmi4_data->det_work);
2326 flush_workqueue(rmi4_data->det_workqueue);
2327 destroy_workqueue(rmi4_data->det_workqueue);
Alexandra Chin669d27c2012-12-24 15:42:30 +08002328 input_unregister_device(rmi4_data->input_dev);
2329
2330err_register_input:
Alexandra Chind5591a62013-02-07 12:59:15 -08002331 if (!list_empty(&rmi->support_fn_list)) {
2332 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
2333 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
2334 synaptics_rmi4_f1a_kfree(fhandler);
2335 else
2336 kfree(fhandler->data);
2337 kfree(fhandler);
2338 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08002339 }
Amy Malocheecfb4892013-02-06 13:30:36 -08002340err_reset_gpio_dir:
Amy Maloche946da662013-01-18 16:27:11 -08002341 if (gpio_is_valid(platform_data->reset_gpio))
2342 gpio_free(platform_data->reset_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002343err_irq_gpio_dir:
Amy Maloche946da662013-01-18 16:27:11 -08002344 if (gpio_is_valid(platform_data->irq_gpio))
2345 gpio_free(platform_data->irq_gpio);
Amy Malocheecfb4892013-02-06 13:30:36 -08002346err_irq_gpio_req:
Amy Maloche946da662013-01-18 16:27:11 -08002347 synaptics_rmi4_power_on(rmi4_data, false);
2348err_power_device:
2349 synaptics_rmi4_regulator_configure(rmi4_data, false);
2350err_reg_configure:
Alexandra Chin669d27c2012-12-24 15:42:30 +08002351 input_free_device(rmi4_data->input_dev);
2352 rmi4_data->input_dev = NULL;
Alexandra Chin669d27c2012-12-24 15:42:30 +08002353err_input_device:
2354 kfree(rmi4_data);
2355
2356 return retval;
2357}
2358
2359 /**
2360 * synaptics_rmi4_remove()
2361 *
2362 * Called by the kernel when the association with an I2C device of the
2363 * same name is broken (when the driver is unloaded).
2364 *
2365 * This funtion terminates the work queue, stops sensor data acquisition,
2366 * frees the interrupt, unregisters the driver from the input subsystem,
2367 * turns off the power to the sensor, and frees other allocated resources.
2368 */
2369static int __devexit synaptics_rmi4_remove(struct i2c_client *client)
2370{
2371 unsigned char attr_count;
2372 struct synaptics_rmi4_fn *fhandler;
2373 struct synaptics_rmi4_data *rmi4_data = i2c_get_clientdata(client);
2374 struct synaptics_rmi4_device_info *rmi;
Alexandra Chin669d27c2012-12-24 15:42:30 +08002375
2376 rmi = &(rmi4_data->rmi4_mod_info);
2377
2378 cancel_delayed_work_sync(&rmi4_data->det_work);
2379 flush_workqueue(rmi4_data->det_workqueue);
2380 destroy_workqueue(rmi4_data->det_workqueue);
2381
2382 rmi4_data->touch_stopped = true;
2383 wake_up(&rmi4_data->wait);
2384
Alexandra Chinfb798fa2013-03-26 17:15:38 -07002385 free_irq(rmi4_data->irq, rmi4_data);
Alexandra Chin669d27c2012-12-24 15:42:30 +08002386
2387 for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
2388 sysfs_remove_file(&rmi4_data->input_dev->dev.kobj,
2389 &attrs[attr_count].attr);
2390 }
2391
2392 input_unregister_device(rmi4_data->input_dev);
2393
Alexandra Chind5591a62013-02-07 12:59:15 -08002394 if (!list_empty(&rmi->support_fn_list)) {
2395 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
2396 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
2397 synaptics_rmi4_f1a_kfree(fhandler);
2398 else
2399 kfree(fhandler->data);
2400 kfree(fhandler);
2401 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08002402 }
Amy Maloche946da662013-01-18 16:27:11 -08002403
2404 if (gpio_is_valid(rmi4_data->board->reset_gpio))
2405 gpio_free(rmi4_data->board->reset_gpio);
2406 if (gpio_is_valid(rmi4_data->board->irq_gpio))
2407 gpio_free(rmi4_data->board->irq_gpio);
Alexandra Chin669d27c2012-12-24 15:42:30 +08002408
Amy Maloche1a53b612013-01-18 15:25:15 -08002409 synaptics_rmi4_power_on(rmi4_data, false);
2410 synaptics_rmi4_regulator_configure(rmi4_data, false);
2411
Alexandra Chin669d27c2012-12-24 15:42:30 +08002412 kfree(rmi4_data);
2413
2414 return 0;
2415}
2416
2417#ifdef CONFIG_PM
2418 /**
2419 * synaptics_rmi4_sensor_sleep()
2420 *
2421 * Called by synaptics_rmi4_early_suspend() and synaptics_rmi4_suspend().
2422 *
2423 * This function stops finger data acquisition and puts the sensor to sleep.
2424 */
2425static void synaptics_rmi4_sensor_sleep(struct synaptics_rmi4_data *rmi4_data)
2426{
2427 int retval;
2428 unsigned char device_ctrl;
2429
2430 retval = synaptics_rmi4_i2c_read(rmi4_data,
2431 rmi4_data->f01_ctrl_base_addr,
2432 &device_ctrl,
2433 sizeof(device_ctrl));
2434 if (retval < 0) {
2435 dev_err(&(rmi4_data->input_dev->dev),
2436 "%s: Failed to enter sleep mode\n",
2437 __func__);
2438 rmi4_data->sensor_sleep = false;
2439 return;
2440 }
2441
2442 device_ctrl = (device_ctrl & ~MASK_3BIT);
2443 device_ctrl = (device_ctrl | NO_SLEEP_OFF | SENSOR_SLEEP);
2444
2445 retval = synaptics_rmi4_i2c_write(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 enter sleep mode\n",
2452 __func__);
2453 rmi4_data->sensor_sleep = false;
2454 return;
2455 } else {
2456 rmi4_data->sensor_sleep = true;
2457 }
2458
2459 return;
2460}
2461
2462 /**
2463 * synaptics_rmi4_sensor_wake()
2464 *
2465 * Called by synaptics_rmi4_resume() and synaptics_rmi4_late_resume().
2466 *
2467 * This function wakes the sensor from sleep.
2468 */
2469static void synaptics_rmi4_sensor_wake(struct synaptics_rmi4_data *rmi4_data)
2470{
2471 int retval;
2472 unsigned char device_ctrl;
2473
2474 retval = synaptics_rmi4_i2c_read(rmi4_data,
2475 rmi4_data->f01_ctrl_base_addr,
2476 &device_ctrl,
2477 sizeof(device_ctrl));
2478 if (retval < 0) {
2479 dev_err(&(rmi4_data->input_dev->dev),
2480 "%s: Failed to wake from sleep mode\n",
2481 __func__);
2482 rmi4_data->sensor_sleep = true;
2483 return;
2484 }
2485
2486 device_ctrl = (device_ctrl & ~MASK_3BIT);
2487 device_ctrl = (device_ctrl | NO_SLEEP_OFF | NORMAL_OPERATION);
2488
2489 retval = synaptics_rmi4_i2c_write(rmi4_data,
2490 rmi4_data->f01_ctrl_base_addr,
2491 &device_ctrl,
2492 sizeof(device_ctrl));
2493 if (retval < 0) {
2494 dev_err(&(rmi4_data->input_dev->dev),
2495 "%s: Failed to wake from sleep mode\n",
2496 __func__);
2497 rmi4_data->sensor_sleep = true;
2498 return;
2499 } else {
2500 rmi4_data->sensor_sleep = false;
2501 }
2502
2503 return;
2504}
2505
Amy Malochef46540d2013-02-15 17:09:43 -08002506#if defined(CONFIG_FB)
2507static int fb_notifier_callback(struct notifier_block *self,
2508 unsigned long event, void *data)
2509{
2510 struct fb_event *evdata = data;
2511 int *blank;
2512 struct synaptics_rmi4_data *rmi4_data =
2513 container_of(self, struct synaptics_rmi4_data, fb_notif);
2514
2515 if (evdata && evdata->data && event == FB_EVENT_BLANK &&
2516 rmi4_data && rmi4_data->i2c_client) {
2517 blank = evdata->data;
2518 if (*blank == FB_BLANK_UNBLANK)
2519 synaptics_rmi4_resume(&(rmi4_data->input_dev->dev));
2520 else if (*blank == FB_BLANK_POWERDOWN)
2521 synaptics_rmi4_suspend(&(rmi4_data->input_dev->dev));
2522 }
2523
2524 return 0;
2525}
2526#elif defined(CONFIG_HAS_EARLYSUSPEND)
Alexandra Chin669d27c2012-12-24 15:42:30 +08002527 /**
2528 * synaptics_rmi4_early_suspend()
2529 *
2530 * Called by the kernel during the early suspend phase when the system
2531 * enters suspend.
2532 *
2533 * This function calls synaptics_rmi4_sensor_sleep() to stop finger
2534 * data acquisition and put the sensor to sleep.
2535 */
2536static void synaptics_rmi4_early_suspend(struct early_suspend *h)
2537{
2538 struct synaptics_rmi4_data *rmi4_data =
2539 container_of(h, struct synaptics_rmi4_data,
2540 early_suspend);
2541
2542 rmi4_data->touch_stopped = true;
2543 wake_up(&rmi4_data->wait);
2544 synaptics_rmi4_irq_enable(rmi4_data, false);
2545 synaptics_rmi4_sensor_sleep(rmi4_data);
2546
2547 if (rmi4_data->full_pm_cycle)
2548 synaptics_rmi4_suspend(&(rmi4_data->input_dev->dev));
2549
2550 return;
2551}
2552
2553 /**
2554 * synaptics_rmi4_late_resume()
2555 *
2556 * Called by the kernel during the late resume phase when the system
2557 * wakes up from suspend.
2558 *
2559 * This function goes through the sensor wake process if the system wakes
2560 * up from early suspend (without going into suspend).
2561 */
2562static void synaptics_rmi4_late_resume(struct early_suspend *h)
2563{
2564 struct synaptics_rmi4_data *rmi4_data =
2565 container_of(h, struct synaptics_rmi4_data,
2566 early_suspend);
2567
2568 if (rmi4_data->full_pm_cycle)
2569 synaptics_rmi4_resume(&(rmi4_data->input_dev->dev));
2570
2571 if (rmi4_data->sensor_sleep == true) {
2572 synaptics_rmi4_sensor_wake(rmi4_data);
2573 rmi4_data->touch_stopped = false;
2574 synaptics_rmi4_irq_enable(rmi4_data, true);
2575 }
2576
2577 return;
2578}
2579#endif
2580
Amy Maloche827e8b02013-02-27 15:25:14 -08002581static int synaptics_rmi4_regulator_lpm(struct synaptics_rmi4_data *rmi4_data,
2582 bool on)
2583{
2584 int retval;
2585
2586 if (on == false)
2587 goto regulator_hpm;
2588
2589 retval = reg_set_optimum_mode_check(rmi4_data->vdd, RMI4_LPM_LOAD_UA);
2590 if (retval < 0) {
2591 dev_err(&rmi4_data->i2c_client->dev,
2592 "Regulator vcc_ana set_opt failed rc=%d\n",
2593 retval);
2594 goto fail_regulator_lpm;
2595 }
2596
2597 if (rmi4_data->board->i2c_pull_up) {
2598 retval = reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
Amy Malochecd38a572013-05-16 10:58:32 -07002599 RMI4_I2C_LPM_LOAD_UA);
Amy Maloche827e8b02013-02-27 15:25:14 -08002600 if (retval < 0) {
2601 dev_err(&rmi4_data->i2c_client->dev,
2602 "Regulator vcc_i2c set_opt failed rc=%d\n",
2603 retval);
2604 goto fail_regulator_lpm;
2605 }
2606 }
2607
2608 return 0;
2609
2610regulator_hpm:
2611
2612 retval = reg_set_optimum_mode_check(rmi4_data->vdd,
2613 RMI4_ACTIVE_LOAD_UA);
2614 if (retval < 0) {
2615 dev_err(&rmi4_data->i2c_client->dev,
2616 "Regulator vcc_ana set_opt failed rc=%d\n",
2617 retval);
2618 goto fail_regulator_hpm;
2619 }
2620
2621 if (rmi4_data->board->i2c_pull_up) {
2622 retval = reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
2623 RMI4_I2C_LOAD_UA);
2624 if (retval < 0) {
2625 dev_err(&rmi4_data->i2c_client->dev,
2626 "Regulator vcc_i2c set_opt failed rc=%d\n",
2627 retval);
2628 goto fail_regulator_hpm;
2629 }
2630 }
2631
2632 return 0;
2633
2634fail_regulator_lpm:
2635 reg_set_optimum_mode_check(rmi4_data->vdd, RMI4_ACTIVE_LOAD_UA);
2636 if (rmi4_data->board->i2c_pull_up)
2637 reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
2638 RMI4_I2C_LOAD_UA);
2639
2640 return retval;
2641
2642fail_regulator_hpm:
2643 reg_set_optimum_mode_check(rmi4_data->vdd, RMI4_LPM_LOAD_UA);
2644 if (rmi4_data->board->i2c_pull_up)
2645 reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
2646 RMI4_I2C_LPM_LOAD_UA);
2647 return retval;
2648}
2649
Alexandra Chin669d27c2012-12-24 15:42:30 +08002650 /**
2651 * synaptics_rmi4_suspend()
2652 *
2653 * Called by the kernel during the suspend phase when the system
2654 * enters suspend.
2655 *
2656 * This function stops finger data acquisition and puts the sensor to
2657 * sleep (if not already done so during the early suspend phase),
2658 * disables the interrupt, and turns off the power to the sensor.
2659 */
2660static int synaptics_rmi4_suspend(struct device *dev)
2661{
2662 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
Amy Maloche827e8b02013-02-27 15:25:14 -08002663 int retval;
Alexandra Chin669d27c2012-12-24 15:42:30 +08002664
Amy Malocheb494b9a2013-05-13 18:01:53 -07002665 if (!rmi4_data->fw_updating) {
2666 if (!rmi4_data->sensor_sleep) {
2667 rmi4_data->touch_stopped = true;
2668 wake_up(&rmi4_data->wait);
2669 synaptics_rmi4_irq_enable(rmi4_data, false);
2670 synaptics_rmi4_sensor_sleep(rmi4_data);
2671 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08002672
Amy Malocheb494b9a2013-05-13 18:01:53 -07002673 retval = synaptics_rmi4_regulator_lpm(rmi4_data, true);
2674 if (retval < 0) {
2675 dev_err(dev, "failed to enter low power mode\n");
2676 return retval;
2677 }
2678 } else
2679 dev_err(dev,
2680 "Firmware updating, cannot go into suspend mode\n");
Alexandra Chin669d27c2012-12-24 15:42:30 +08002681 return 0;
2682}
2683
2684 /**
2685 * synaptics_rmi4_resume()
2686 *
2687 * Called by the kernel during the resume phase when the system
2688 * wakes up from suspend.
2689 *
2690 * This function turns on the power to the sensor, wakes the sensor
2691 * from sleep, enables the interrupt, and starts finger data
2692 * acquisition.
2693 */
2694static int synaptics_rmi4_resume(struct device *dev)
2695{
2696 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
Amy Maloche827e8b02013-02-27 15:25:14 -08002697 int retval;
2698
2699 retval = synaptics_rmi4_regulator_lpm(rmi4_data, false);
2700 if (retval < 0) {
2701 dev_err(dev, "failed to enter active power mode\n");
2702 return retval;
2703 }
Alexandra Chin669d27c2012-12-24 15:42:30 +08002704
2705 synaptics_rmi4_sensor_wake(rmi4_data);
2706 rmi4_data->touch_stopped = false;
2707 synaptics_rmi4_irq_enable(rmi4_data, true);
2708
2709 return 0;
2710}
2711
Amy Malochef46540d2013-02-15 17:09:43 -08002712#if (!defined(CONFIG_FB) && !defined(CONFIG_HAS_EARLYSUSPEND))
Alexandra Chin669d27c2012-12-24 15:42:30 +08002713static const struct dev_pm_ops synaptics_rmi4_dev_pm_ops = {
2714 .suspend = synaptics_rmi4_suspend,
2715 .resume = synaptics_rmi4_resume,
2716};
Amy Malochef46540d2013-02-15 17:09:43 -08002717#else
2718static const struct dev_pm_ops synaptics_rmi4_dev_pm_ops = {
2719};
2720#endif
Alexandra Chin669d27c2012-12-24 15:42:30 +08002721#endif
2722
2723static const struct i2c_device_id synaptics_rmi4_id_table[] = {
2724 {DRIVER_NAME, 0},
2725 {},
2726};
2727MODULE_DEVICE_TABLE(i2c, synaptics_rmi4_id_table);
2728
Amy Malocheecfb4892013-02-06 13:30:36 -08002729#ifdef CONFIG_OF
2730static struct of_device_id rmi4_match_table[] = {
2731 { .compatible = "synaptics,rmi4",},
2732 { },
2733};
2734#else
2735#define rmi4_match_table NULL
2736#endif
2737
Alexandra Chin669d27c2012-12-24 15:42:30 +08002738static struct i2c_driver synaptics_rmi4_driver = {
2739 .driver = {
2740 .name = DRIVER_NAME,
2741 .owner = THIS_MODULE,
Amy Malocheecfb4892013-02-06 13:30:36 -08002742 .of_match_table = rmi4_match_table,
Alexandra Chin669d27c2012-12-24 15:42:30 +08002743#ifdef CONFIG_PM
2744 .pm = &synaptics_rmi4_dev_pm_ops,
2745#endif
2746 },
2747 .probe = synaptics_rmi4_probe,
2748 .remove = __devexit_p(synaptics_rmi4_remove),
2749 .id_table = synaptics_rmi4_id_table,
2750};
2751
2752 /**
2753 * synaptics_rmi4_init()
2754 *
2755 * Called by the kernel during do_initcalls (if built-in)
2756 * or when the driver is loaded (if a module).
2757 *
2758 * This function registers the driver to the I2C subsystem.
2759 *
2760 */
2761static int __init synaptics_rmi4_init(void)
2762{
2763 return i2c_add_driver(&synaptics_rmi4_driver);
2764}
2765
2766 /**
2767 * synaptics_rmi4_exit()
2768 *
2769 * Called by the kernel when the driver is unloaded.
2770 *
2771 * This funtion unregisters the driver from the I2C subsystem.
2772 *
2773 */
2774static void __exit synaptics_rmi4_exit(void)
2775{
2776 i2c_del_driver(&synaptics_rmi4_driver);
2777}
2778
2779module_init(synaptics_rmi4_init);
2780module_exit(synaptics_rmi4_exit);
2781
2782MODULE_AUTHOR("Synaptics, Inc.");
2783MODULE_DESCRIPTION("Synaptics RMI4 I2C Touch Driver");
2784MODULE_LICENSE("GPL v2");