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