· 9 years ago · Jan 27, 2017, 02:08 PM
1/*
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>
8 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
9 *
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/pinctrl/consumer.h>
31#include <linux/input/synaptics_dsx.h>
32#include <linux/of_gpio.h>
33
34#if defined(CONFIG_SECURE_TOUCH)
35#include <linux/pm_runtime.h>
36#include <linux/errno.h>
37#endif
38
39#include "synaptics_i2c_rmi4.h"
40#include <linux/input/mt.h>
41
42#define DRIVER_NAME "synaptics_rmi4_i2c"
43#define INPUT_PHYS_NAME "synaptics_rmi4_i2c/input0"
44#define DEBUGFS_DIR_NAME "ts_debug"
45
46#define RESET_DELAY 100
47
48#define TYPE_B_PROTOCOL
49
50#define NO_0D_WHILE_2D
51/*
52#define REPORT_2D_Z
53*/
54#define REPORT_2D_W
55
56#define RPT_TYPE (1 << 0)
57#define RPT_X_LSB (1 << 1)
58#define RPT_X_MSB (1 << 2)
59#define RPT_Y_LSB (1 << 3)
60#define RPT_Y_MSB (1 << 4)
61#define RPT_Z (1 << 5)
62#define RPT_WX (1 << 6)
63#define RPT_WY (1 << 7)
64#define RPT_DEFAULT (RPT_TYPE | RPT_X_LSB | RPT_X_MSB | RPT_Y_LSB | RPT_Y_MSB)
65
66#define EXP_FN_DET_INTERVAL 1000 /* ms */
67#define POLLING_PERIOD 1 /* ms */
68#define SYN_I2C_RETRY_TIMES 10
69#define MAX_ABS_MT_TOUCH_MAJOR 15
70
71#define F01_STD_QUERY_LEN 21
72#define F01_PACKAGE_ID_OFFSET 17
73#define F01_BUID_ID_OFFSET 18
74#define F11_STD_QUERY_LEN 9
75#define F11_STD_CTRL_LEN 10
76#define F11_STD_DATA_LEN 12
77
78#define NORMAL_OPERATION 0
79#define SENSOR_SLEEP 1
80#define NO_SLEEP_OFF 0
81#define NO_SLEEP_ON 1
82
83enum device_status {
84 STATUS_NO_ERROR = 0x00,
85 STATUS_RESET_OCCURED = 0x01,
86 STATUS_INVALID_CONFIG = 0x02,
87 STATUS_DEVICE_FAILURE = 0x03,
88 STATUS_CONFIG_CRC_FAILURE = 0x04,
89 STATUS_FIRMWARE_CRC_FAILURE = 0x05,
90 STATUS_CRC_IN_PROGRESS = 0x06,
91 STATUS_UNCONFIGURED = 0x80
92};
93
94#define DEVICE_CONFIGURED 0x1
95
96#define RMI4_VTG_MIN_UV 2700000
97#define RMI4_VTG_MAX_UV 3300000
98#define RMI4_ACTIVE_LOAD_UA 15000
99#define RMI4_LPM_LOAD_UA 10
100
101#define RMI4_I2C_VTG_MIN_UV 1800000
102#define RMI4_I2C_VTG_MAX_UV 1800000
103#define RMI4_I2C_LOAD_UA 10000
104#define RMI4_I2C_LPM_LOAD_UA 10
105
106#define RMI4_GPIO_SLEEP_LOW_US 10000
107#define F12_FINGERS_TO_SUPPORT 10
108#define MAX_F11_TOUCH_WIDTH 15
109
110#define RMI4_COORDS_ARR_SIZE 4
111
112#define F11_MAX_X 4096
113#define F11_MAX_Y 4096
114#define F12_MAX_X 65536
115#define F12_MAX_Y 65536
116
117static int synaptics_rmi4_i2c_read(struct synaptics_rmi4_data *rmi4_data,
118 unsigned short addr, unsigned char *data,
119 unsigned short length);
120
121static int synaptics_rmi4_i2c_write(struct synaptics_rmi4_data *rmi4_data,
122 unsigned short addr, unsigned char *data,
123 unsigned short length);
124
125static int synaptics_rmi4_reset_device(struct synaptics_rmi4_data *rmi4_data);
126
127static void synaptics_rmi4_sensor_wake(struct synaptics_rmi4_data *rmi4_data);
128
129static void __maybe_unused synaptics_rmi4_sensor_sleep(
130 struct synaptics_rmi4_data *rmi4_data);
131
132static int __maybe_unused synaptics_rmi4_regulator_lpm(
133 struct synaptics_rmi4_data *rmi4_data, bool on);
134
135static void __maybe_unused synaptics_rmi4_release_all(
136 struct synaptics_rmi4_data *rmi4_data);
137
138static int synaptics_rmi4_check_configuration(struct synaptics_rmi4_data
139 *rmi4_data);
140
141static int synaptics_rmi4_suspend(struct device *dev);
142
143static int synaptics_rmi4_resume(struct device *dev);
144
145static ssize_t synaptics_rmi4_full_pm_cycle_show(struct device *dev,
146 struct device_attribute *attr, char *buf);
147
148static ssize_t synaptics_rmi4_full_pm_cycle_store(struct device *dev,
149 struct device_attribute *attr, const char *buf, size_t count);
150
151#if defined(CONFIG_FB)
152static int fb_notifier_callback(struct notifier_block *self,
153 unsigned long event, void *data);
154#elif defined(CONFIG_HAS_EARLYSUSPEND)
155static void synaptics_rmi4_early_suspend(struct early_suspend *h);
156
157static void synaptics_rmi4_late_resume(struct early_suspend *h);
158#endif
159
160static ssize_t synaptics_rmi4_f01_reset_store(struct device *dev,
161 struct device_attribute *attr, const char *buf, size_t count);
162
163static ssize_t synaptics_rmi4_f01_productinfo_show(struct device *dev,
164 struct device_attribute *attr, char *buf);
165
166static ssize_t synaptics_rmi4_f01_buildid_show(struct device *dev,
167 struct device_attribute *attr, char *buf);
168
169static ssize_t synaptics_rmi4_f01_flashprog_show(struct device *dev,
170 struct device_attribute *attr, char *buf);
171
172static ssize_t synaptics_rmi4_0dbutton_show(struct device *dev,
173 struct device_attribute *attr, char *buf);
174
175static ssize_t synaptics_rmi4_0dbutton_store(struct device *dev,
176 struct device_attribute *attr, const char *buf, size_t count);
177
178static ssize_t synaptics_rmi4_flipx_show(struct device *dev,
179 struct device_attribute *attr, char *buf);
180
181static ssize_t synaptics_rmi4_flipx_store(struct device *dev,
182 struct device_attribute *attr, const char *buf, size_t count);
183
184static ssize_t synaptics_rmi4_flipy_show(struct device *dev,
185 struct device_attribute *attr, char *buf);
186
187static ssize_t synaptics_rmi4_flipy_store(struct device *dev,
188 struct device_attribute *attr, const char *buf, size_t count);
189
190static int synaptics_rmi4_capacitance_button_map(
191 struct synaptics_rmi4_data *rmi4_data,
192 struct synaptics_rmi4_fn *fhandler);
193
194static irqreturn_t synaptics_rmi4_irq(int irq, void *data);
195
196#if defined(CONFIG_SECURE_TOUCH)
197static ssize_t synaptics_secure_touch_enable_show(struct device *dev,
198 struct device_attribute *attr, char *buf);
199
200static ssize_t synaptics_secure_touch_enable_store(struct device *dev,
201 struct device_attribute *attr, const char *buf, size_t count);
202
203static ssize_t synaptics_secure_touch_show(struct device *dev,
204 struct device_attribute *attr, char *buf);
205#endif
206
207struct synaptics_rmi4_f01_device_status {
208 union {
209 struct {
210 unsigned char status_code:4;
211 unsigned char reserved:2;
212 unsigned char flash_prog:1;
213 unsigned char unconfigured:1;
214 } __packed;
215 unsigned char data[1];
216 };
217};
218
219struct synaptics_rmi4_f01_device_control_0 {
220 union {
221 struct {
222 unsigned char sleep_mode:2;
223 unsigned char nosleep:1;
224 unsigned char reserved:2;
225 unsigned char charger_input:1;
226 unsigned char report_rate:1;
227 unsigned char configured:1;
228 } __packed;
229 unsigned char data[1];
230 };
231};
232
233struct synaptics_rmi4_f12_query_5 {
234 union {
235 struct {
236 unsigned char size_of_query6;
237 struct {
238 unsigned char ctrl0_is_present:1;
239 unsigned char ctrl1_is_present:1;
240 unsigned char ctrl2_is_present:1;
241 unsigned char ctrl3_is_present:1;
242 unsigned char ctrl4_is_present:1;
243 unsigned char ctrl5_is_present:1;
244 unsigned char ctrl6_is_present:1;
245 unsigned char ctrl7_is_present:1;
246 } __packed;
247 struct {
248 unsigned char ctrl8_is_present:1;
249 unsigned char ctrl9_is_present:1;
250 unsigned char ctrl10_is_present:1;
251 unsigned char ctrl11_is_present:1;
252 unsigned char ctrl12_is_present:1;
253 unsigned char ctrl13_is_present:1;
254 unsigned char ctrl14_is_present:1;
255 unsigned char ctrl15_is_present:1;
256 } __packed;
257 struct {
258 unsigned char ctrl16_is_present:1;
259 unsigned char ctrl17_is_present:1;
260 unsigned char ctrl18_is_present:1;
261 unsigned char ctrl19_is_present:1;
262 unsigned char ctrl20_is_present:1;
263 unsigned char ctrl21_is_present:1;
264 unsigned char ctrl22_is_present:1;
265 unsigned char ctrl23_is_present:1;
266 } __packed;
267 struct {
268 unsigned char ctrl24_is_present:1;
269 unsigned char ctrl25_is_present:1;
270 unsigned char ctrl26_is_present:1;
271 unsigned char ctrl27_is_present:1;
272 unsigned char ctrl28_is_present:1;
273 unsigned char ctrl29_is_present:1;
274 unsigned char ctrl30_is_present:1;
275 unsigned char ctrl31_is_present:1;
276 } __packed;
277 };
278 unsigned char data[5];
279 };
280};
281
282struct synaptics_rmi4_f12_query_8 {
283 union {
284 struct {
285 unsigned char size_of_query9;
286 struct {
287 unsigned char data0_is_present:1;
288 unsigned char data1_is_present:1;
289 unsigned char data2_is_present:1;
290 unsigned char data3_is_present:1;
291 unsigned char data4_is_present:1;
292 unsigned char data5_is_present:1;
293 unsigned char data6_is_present:1;
294 unsigned char data7_is_present:1;
295 } __packed;
296 struct {
297 unsigned char data8_is_present:1;
298 unsigned char data9_is_present:1;
299 unsigned char data10_is_present:1;
300 unsigned char data11_is_present:1;
301 unsigned char data12_is_present:1;
302 unsigned char data13_is_present:1;
303 unsigned char data14_is_present:1;
304 unsigned char data15_is_present:1;
305 } __packed;
306 };
307 unsigned char data[3];
308 };
309};
310
311struct synaptics_rmi4_f12_ctrl_8 {
312 union {
313 struct {
314 unsigned char max_x_coord_lsb;
315 unsigned char max_x_coord_msb;
316 unsigned char max_y_coord_lsb;
317 unsigned char max_y_coord_msb;
318 unsigned char rx_pitch_lsb;
319 unsigned char rx_pitch_msb;
320 unsigned char tx_pitch_lsb;
321 unsigned char tx_pitch_msb;
322 unsigned char low_rx_clip;
323 unsigned char high_rx_clip;
324 unsigned char low_tx_clip;
325 unsigned char high_tx_clip;
326 unsigned char num_of_rx;
327 unsigned char num_of_tx;
328 };
329 unsigned char data[14];
330 };
331};
332
333struct synaptics_rmi4_f12_ctrl_23 {
334 union {
335 struct {
336 unsigned char obj_type_enable;
337 unsigned char max_reported_objects;
338 };
339 unsigned char data[2];
340 };
341};
342
343struct synaptics_rmi4_f12_finger_data {
344 unsigned char object_type_and_status;
345 unsigned char x_lsb;
346 unsigned char x_msb;
347 unsigned char y_lsb;
348 unsigned char y_msb;
349#ifdef REPORT_2D_Z
350 unsigned char z;
351#endif
352#ifdef REPORT_2D_W
353 unsigned char wx;
354 unsigned char wy;
355#endif
356};
357
358struct synaptics_rmi4_f1a_query {
359 union {
360 struct {
361 unsigned char max_button_count:3;
362 unsigned char reserved:5;
363 unsigned char has_general_control:1;
364 unsigned char has_interrupt_enable:1;
365 unsigned char has_multibutton_select:1;
366 unsigned char has_tx_rx_map:1;
367 unsigned char has_perbutton_threshold:1;
368 unsigned char has_release_threshold:1;
369 unsigned char has_strongestbtn_hysteresis:1;
370 unsigned char has_filter_strength:1;
371 } __packed;
372 unsigned char data[2];
373 };
374};
375
376struct synaptics_rmi4_f1a_control_0 {
377 union {
378 struct {
379 unsigned char multibutton_report:2;
380 unsigned char filter_mode:2;
381 unsigned char reserved:4;
382 } __packed;
383 unsigned char data[1];
384 };
385};
386
387struct synaptics_rmi4_f1a_control_3_4 {
388 unsigned char transmitterbutton;
389 unsigned char receiverbutton;
390};
391
392struct synaptics_rmi4_f1a_control {
393 struct synaptics_rmi4_f1a_control_0 general_control;
394 unsigned char *button_int_enable;
395 unsigned char *multi_button;
396 struct synaptics_rmi4_f1a_control_3_4 *electrode_map;
397 unsigned char *button_threshold;
398 unsigned char button_release_threshold;
399 unsigned char strongest_button_hysteresis;
400 unsigned char filter_strength;
401};
402
403struct synaptics_rmi4_f1a_handle {
404 int button_bitmask_size;
405 unsigned char button_count;
406 unsigned char valid_button_count;
407 unsigned char *button_data_buffer;
408 unsigned char *button_map;
409 struct synaptics_rmi4_f1a_query button_query;
410 struct synaptics_rmi4_f1a_control button_control;
411};
412
413struct synaptics_rmi4_f12_extra_data {
414 unsigned char data1_offset;
415 unsigned char data15_offset;
416 unsigned char data15_size;
417 unsigned char data15_data[(F12_FINGERS_TO_SUPPORT + 7) / 8];
418};
419
420struct synaptics_rmi4_exp_fn {
421 enum exp_fn fn_type;
422 bool inserted;
423 int (*func_init)(struct synaptics_rmi4_data *rmi4_data);
424 void (*func_remove)(struct synaptics_rmi4_data *rmi4_data);
425 void (*func_attn)(struct synaptics_rmi4_data *rmi4_data,
426 unsigned char intr_mask);
427 struct list_head link;
428};
429
430static struct device_attribute attrs[] = {
431 __ATTR(full_pm_cycle, (S_IRUGO | S_IWUSR | S_IWGRP),
432 synaptics_rmi4_full_pm_cycle_show,
433 synaptics_rmi4_full_pm_cycle_store),
434 __ATTR(reset, S_IWUSR | S_IWGRP,
435 NULL,
436 synaptics_rmi4_f01_reset_store),
437 __ATTR(productinfo, S_IRUGO,
438 synaptics_rmi4_f01_productinfo_show,
439 synaptics_rmi4_store_error),
440 __ATTR(buildid, S_IRUGO,
441 synaptics_rmi4_f01_buildid_show,
442 synaptics_rmi4_store_error),
443 __ATTR(flashprog, S_IRUGO,
444 synaptics_rmi4_f01_flashprog_show,
445 synaptics_rmi4_store_error),
446 __ATTR(0dbutton, (S_IRUGO | S_IWUSR | S_IWGRP),
447 synaptics_rmi4_0dbutton_show,
448 synaptics_rmi4_0dbutton_store),
449 __ATTR(flipx, (S_IRUGO | S_IWUSR | S_IWGRP),
450 synaptics_rmi4_flipx_show,
451 synaptics_rmi4_flipx_store),
452 __ATTR(flipy, (S_IRUGO | S_IWUSR | S_IWGRP),
453 synaptics_rmi4_flipy_show,
454 synaptics_rmi4_flipy_store),
455#if defined(CONFIG_SECURE_TOUCH)
456 __ATTR(secure_touch_enable, (S_IRUGO | S_IWUSR | S_IWGRP),
457 synaptics_secure_touch_enable_show,
458 synaptics_secure_touch_enable_store),
459 __ATTR(secure_touch, S_IRUGO ,
460 synaptics_secure_touch_show,
461 NULL),
462#endif
463};
464
465static bool exp_fn_inited;
466static struct mutex exp_fn_list_mutex;
467static struct list_head exp_fn_list;
468
469#if defined(CONFIG_SECURE_TOUCH)
470static int synaptics_secure_touch_clk_prepare_enable(
471 struct synaptics_rmi4_data *rmi4_data)
472{
473 int ret;
474 ret = clk_prepare_enable(rmi4_data->iface_clk);
475 if (ret) {
476 dev_err(&rmi4_data->i2c_client->dev,
477 "error on clk_prepare_enable(iface_clk):%d\n", ret);
478 return ret;
479 }
480
481 ret = clk_prepare_enable(rmi4_data->core_clk);
482 if (ret) {
483 clk_disable_unprepare(rmi4_data->iface_clk);
484 dev_err(&rmi4_data->i2c_client->dev,
485 "error clk_prepare_enable(core_clk):%d\n", ret);
486 }
487 return ret;
488}
489
490static void synaptics_secure_touch_clk_disable_unprepare(
491 struct synaptics_rmi4_data *rmi4_data)
492{
493 clk_disable_unprepare(rmi4_data->core_clk);
494 clk_disable_unprepare(rmi4_data->iface_clk);
495}
496
497static void synaptics_secure_touch_init(struct synaptics_rmi4_data *data)
498{
499 int ret = 0;
500 data->st_initialized = 0;
501 init_completion(&data->st_powerdown);
502 init_completion(&data->st_irq_processed);
503 /* Get clocks */
504 data->core_clk = clk_get(&data->i2c_client->dev, "core_clk");
505 if (IS_ERR(data->core_clk)) {
506 ret = PTR_ERR(data->core_clk);
507 dev_err(&data->i2c_client->dev,
508 "%s: error on clk_get(core_clk):%d\n", __func__, ret);
509 return;
510 }
511
512 data->iface_clk = clk_get(&data->i2c_client->dev, "iface_clk");
513 if (IS_ERR(data->iface_clk)) {
514 ret = PTR_ERR(data->iface_clk);
515 dev_err(&data->i2c_client->dev,
516 "%s: error on clk_get(iface_clk)\n", __func__);
517 goto err_iface_clk;
518 }
519
520 data->st_initialized = 1;
521 return;
522
523err_iface_clk:
524 clk_put(data->core_clk);
525 data->core_clk = NULL;
526}
527static void synaptics_secure_touch_notify(struct synaptics_rmi4_data *data)
528{
529 sysfs_notify(&data->i2c_client->dev.kobj, NULL, "secure_touch");
530}
531static irqreturn_t synaptics_filter_interrupt(struct synaptics_rmi4_data *data)
532{
533 if (atomic_read(&data->st_enabled)) {
534 if (atomic_cmpxchg(&data->st_pending_irqs, 0, 1) == 0) {
535 synaptics_secure_touch_notify(data);
536 wait_for_completion_interruptible(
537 &data->st_irq_processed);
538 }
539 return IRQ_HANDLED;
540 }
541 return IRQ_NONE;
542}
543static void synaptics_secure_touch_stop(
544 struct synaptics_rmi4_data *data,
545 int blocking)
546{
547 if (atomic_read(&data->st_enabled)) {
548 atomic_set(&data->st_pending_irqs, -1);
549 synaptics_secure_touch_notify(data);
550 if (blocking)
551 wait_for_completion_interruptible(&data->st_powerdown);
552 }
553}
554#else
555static void synaptics_secure_touch_init(struct synaptics_rmi4_data *data)
556{
557}
558static irqreturn_t synaptics_filter_interrupt(struct synaptics_rmi4_data *data)
559{
560 return IRQ_NONE;
561}
562static void synaptics_secure_touch_stop(
563 struct synaptics_rmi4_data *data,
564 int blocking)
565{
566}
567#endif
568
569#if defined(CONFIG_SECURE_TOUCH)
570static ssize_t synaptics_secure_touch_enable_show(struct device *dev,
571 struct device_attribute *attr, char *buf)
572{
573 struct synaptics_rmi4_data *data = dev_get_drvdata(dev);
574 return scnprintf(buf, PAGE_SIZE, "%d", atomic_read(&data->st_enabled));
575}
576/*
577 * Accept only "0" and "1" valid values.
578 * "0" will reset the st_enabled flag, then wake up the reading process and
579 * the interrupt handler.
580 * The bus driver is notified via pm_runtime that it is not required to stay
581 * awake anymore.
582 * It will also make sure the queue of events is emptied in the controller,
583 * in case a touch happened in between the secure touch being disabled and
584 * the local ISR being ungated.
585 * "1" will set the st_enabled flag and clear the st_pending_irqs flag.
586 * The bus driver is requested via pm_runtime to stay awake.
587 */
588static ssize_t synaptics_secure_touch_enable_store(struct device *dev,
589 struct device_attribute *attr,
590 const char *buf, size_t count)
591{
592 struct synaptics_rmi4_data *data = dev_get_drvdata(dev);
593 struct device *adapter = data->i2c_client->adapter->dev.parent;
594 unsigned long value;
595 int err = 0;
596
597 if (count > 2)
598 return -EINVAL;
599
600 err = kstrtoul(buf, 10, &value);
601 if (err != 0)
602 return err;
603
604 if (!data->st_initialized)
605 return -EIO;
606
607 err = count;
608
609 switch (value) {
610 case 0:
611 if (atomic_read(&data->st_enabled) == 0)
612 break;
613
614 synaptics_secure_touch_clk_disable_unprepare(data);
615 pm_runtime_put_sync(adapter);
616 atomic_set(&data->st_enabled, 0);
617 synaptics_secure_touch_notify(data);
618 complete(&data->st_irq_processed);
619 synaptics_rmi4_irq(data->irq, data);
620 complete(&data->st_powerdown);
621
622 break;
623 case 1:
624 if (atomic_read(&data->st_enabled)) {
625 err = -EBUSY;
626 break;
627 }
628
629 synchronize_irq(data->irq);
630 if (pm_runtime_get_sync(adapter) < 0) {
631 dev_err(&data->i2c_client->dev, "pm_runtime_get_sync failed\n");
632 err = -EIO;
633 break;
634 }
635
636 if (synaptics_secure_touch_clk_prepare_enable(data) < 0) {
637 pm_runtime_put_sync(adapter);
638 err = -EIO;
639 break;
640 }
641 INIT_COMPLETION(data->st_powerdown);
642 INIT_COMPLETION(data->st_irq_processed);
643 atomic_set(&data->st_enabled, 1);
644 atomic_set(&data->st_pending_irqs, 0);
645 break;
646 default:
647 dev_err(&data->i2c_client->dev,
648 "unsupported value: %lu\n", value);
649 err = -EINVAL;
650 break;
651 }
652 return err;
653}
654
655/*
656 * This function returns whether there are pending interrupts, or
657 * other error conditions that need to be signaled to the userspace library,
658 * according tot he following logic:
659 * - st_enabled is 0 if secure touch is not enabled, returning -EBADF
660 * - st_pending_irqs is -1 to signal that secure touch is in being stopped,
661 * returning -EINVAL
662 * - st_pending_irqs is 1 to signal that there is a pending irq, returning
663 * the value "1" to the sysfs read operation
664 * - st_pending_irqs is 0 (only remaining case left) if the pending interrupt
665 * has been processed, so the interrupt handler can be allowed to continue.
666 */
667static ssize_t synaptics_secure_touch_show(struct device *dev,
668 struct device_attribute *attr, char *buf)
669{
670 struct synaptics_rmi4_data *data = dev_get_drvdata(dev);
671 int val = 0;
672 if (atomic_read(&data->st_enabled) == 0)
673 return -EBADF;
674
675 if (atomic_cmpxchg(&data->st_pending_irqs, -1, 0) == -1)
676 return -EINVAL;
677
678 if (atomic_cmpxchg(&data->st_pending_irqs, 1, 0) == 1)
679 val = 1;
680 else
681 complete(&data->st_irq_processed);
682
683 return scnprintf(buf, PAGE_SIZE, "%u", val);
684
685}
686#endif
687static int synaptics_rmi4_debug_suspend_set(void *_data, u64 val)
688{
689 struct synaptics_rmi4_data *rmi4_data = _data;
690
691 if (val)
692 synaptics_rmi4_suspend(&rmi4_data->input_dev->dev);
693 else
694 synaptics_rmi4_resume(&rmi4_data->input_dev->dev);
695
696 return 0;
697}
698
699static int synaptics_rmi4_debug_suspend_get(void *_data, u64 *val)
700{
701 struct synaptics_rmi4_data *rmi4_data = _data;
702
703 *val = rmi4_data->suspended;
704
705 return 0;
706}
707
708DEFINE_SIMPLE_ATTRIBUTE(debug_suspend_fops, synaptics_rmi4_debug_suspend_get,
709 synaptics_rmi4_debug_suspend_set, "%lld\n");
710
711static ssize_t synaptics_rmi4_full_pm_cycle_show(struct device *dev,
712 struct device_attribute *attr, char *buf)
713{
714 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
715
716 return snprintf(buf, PAGE_SIZE, "%u\n",
717 rmi4_data->full_pm_cycle);
718}
719
720static ssize_t synaptics_rmi4_full_pm_cycle_store(struct device *dev,
721 struct device_attribute *attr, const char *buf, size_t count)
722{
723 unsigned int input;
724 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
725
726 if (sscanf(buf, "%u", &input) != 1)
727 return -EINVAL;
728
729 rmi4_data->full_pm_cycle = input > 0 ? 1 : 0;
730
731 return count;
732}
733
734#ifdef CONFIG_FB
735static void configure_sleep(struct synaptics_rmi4_data *rmi4_data)
736{
737 int retval = 0;
738
739 rmi4_data->fb_notif.notifier_call = fb_notifier_callback;
740
741 retval = fb_register_client(&rmi4_data->fb_notif);
742 if (retval)
743 dev_err(&rmi4_data->i2c_client->dev,
744 "Unable to register fb_notifier: %d\n", retval);
745 return;
746}
747#elif defined CONFIG_HAS_EARLYSUSPEND
748static void configure_sleep(struct synaptics_rmi4_data *rmi4_data)
749{
750 rmi4_data->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN + 1;
751 rmi4_data->early_suspend.suspend = synaptics_rmi4_early_suspend;
752 rmi4_data->early_suspend.resume = synaptics_rmi4_late_resume;
753 register_early_suspend(&rmi4_data->early_suspend);
754
755 return;
756}
757#else
758static void configure_sleep(struct synaptics_rmi4_data *rmi4_data)
759{
760 return;
761}
762#endif
763
764static ssize_t synaptics_rmi4_f01_reset_store(struct device *dev,
765 struct device_attribute *attr, const char *buf, size_t count)
766{
767 int retval;
768 unsigned int reset;
769 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
770
771 if (sscanf(buf, "%u", &reset) != 1)
772 return -EINVAL;
773
774 if (reset != 1)
775 return -EINVAL;
776
777 retval = synaptics_rmi4_reset_device(rmi4_data);
778 if (retval < 0) {
779 dev_err(dev,
780 "%s: Failed to issue reset command, error = %d\n",
781 __func__, retval);
782 return retval;
783 }
784
785 return count;
786}
787
788static ssize_t synaptics_rmi4_f01_productinfo_show(struct device *dev,
789 struct device_attribute *attr, char *buf)
790{
791 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
792
793 return snprintf(buf, PAGE_SIZE, "0x%02x 0x%02x\n",
794 (rmi4_data->rmi4_mod_info.product_info[0]),
795 (rmi4_data->rmi4_mod_info.product_info[1]));
796}
797
798static ssize_t synaptics_rmi4_f01_buildid_show(struct device *dev,
799 struct device_attribute *attr, char *buf)
800{
801 unsigned int build_id;
802 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
803 struct synaptics_rmi4_device_info *rmi;
804
805 rmi = &(rmi4_data->rmi4_mod_info);
806
807 build_id = (unsigned int)rmi->build_id[0] +
808 (unsigned int)rmi->build_id[1] * 0x100 +
809 (unsigned int)rmi->build_id[2] * 0x10000;
810
811 return snprintf(buf, PAGE_SIZE, "%u\n",
812 build_id);
813}
814
815static ssize_t synaptics_rmi4_f01_flashprog_show(struct device *dev,
816 struct device_attribute *attr, char *buf)
817{
818 int retval;
819 struct synaptics_rmi4_f01_device_status device_status;
820 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
821
822 retval = synaptics_rmi4_i2c_read(rmi4_data,
823 rmi4_data->f01_data_base_addr,
824 device_status.data,
825 sizeof(device_status.data));
826 if (retval < 0) {
827 dev_err(dev,
828 "%s: Failed to read device status, error = %d\n",
829 __func__, retval);
830 return retval;
831 }
832
833 return snprintf(buf, PAGE_SIZE, "%u\n",
834 device_status.flash_prog);
835}
836
837static ssize_t synaptics_rmi4_0dbutton_show(struct device *dev,
838 struct device_attribute *attr, char *buf)
839{
840 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
841
842 return snprintf(buf, PAGE_SIZE, "%u\n",
843 rmi4_data->button_0d_enabled);
844}
845
846static ssize_t synaptics_rmi4_0dbutton_store(struct device *dev,
847 struct device_attribute *attr, const char *buf, size_t count)
848{
849 int retval;
850 unsigned int input;
851 unsigned char ii;
852 unsigned char intr_enable;
853 struct synaptics_rmi4_fn *fhandler;
854 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
855 struct synaptics_rmi4_device_info *rmi;
856
857 rmi = &(rmi4_data->rmi4_mod_info);
858
859 if (sscanf(buf, "%u", &input) != 1)
860 return -EINVAL;
861
862 input = input > 0 ? 1 : 0;
863
864 if (rmi4_data->button_0d_enabled == input)
865 return count;
866
867 mutex_lock(&rmi->support_fn_list_mutex);
868 if (!list_empty(&rmi->support_fn_list)) {
869 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
870 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A) {
871 ii = fhandler->intr_reg_num;
872
873 retval = synaptics_rmi4_i2c_read(rmi4_data,
874 rmi4_data->f01_ctrl_base_addr +
875 1 + ii,
876 &intr_enable,
877 sizeof(intr_enable));
878 if (retval < 0)
879 goto exit;
880
881 if (input == 1)
882 intr_enable |= fhandler->intr_mask;
883 else
884 intr_enable &= ~fhandler->intr_mask;
885
886 retval = synaptics_rmi4_i2c_write(rmi4_data,
887 rmi4_data->f01_ctrl_base_addr +
888 1 + ii,
889 &intr_enable,
890 sizeof(intr_enable));
891 if (retval < 0)
892 goto exit;
893 }
894 }
895 }
896 mutex_unlock(&rmi->support_fn_list_mutex);
897 rmi4_data->button_0d_enabled = input;
898
899 return count;
900exit:
901 mutex_unlock(&rmi->support_fn_list_mutex);
902 return retval;
903}
904
905static ssize_t synaptics_rmi4_flipx_show(struct device *dev,
906 struct device_attribute *attr, char *buf)
907{
908 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
909
910 return snprintf(buf, PAGE_SIZE, "%u\n",
911 rmi4_data->flip_x);
912}
913
914static ssize_t synaptics_rmi4_flipx_store(struct device *dev,
915 struct device_attribute *attr, const char *buf, size_t count)
916{
917 unsigned int input;
918 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
919
920 if (sscanf(buf, "%u", &input) != 1)
921 return -EINVAL;
922
923 rmi4_data->flip_x = input > 0 ? 1 : 0;
924
925 return count;
926}
927
928static ssize_t synaptics_rmi4_flipy_show(struct device *dev,
929 struct device_attribute *attr, char *buf)
930{
931 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
932
933 return snprintf(buf, PAGE_SIZE, "%u\n",
934 rmi4_data->flip_y);
935}
936
937static ssize_t synaptics_rmi4_flipy_store(struct device *dev,
938 struct device_attribute *attr, const char *buf, size_t count)
939{
940 unsigned int input;
941 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
942
943 if (sscanf(buf, "%u", &input) != 1)
944 return -EINVAL;
945
946 rmi4_data->flip_y = input > 0 ? 1 : 0;
947
948 return count;
949}
950
951 /**
952 * synaptics_rmi4_set_page()
953 *
954 * Called by synaptics_rmi4_i2c_read() and synaptics_rmi4_i2c_write().
955 *
956 * This function writes to the page select register to switch to the
957 * assigned page.
958 */
959static int synaptics_rmi4_set_page(struct synaptics_rmi4_data *rmi4_data,
960 unsigned int address)
961{
962 int retval = 0;
963 unsigned char retry;
964 unsigned char buf[PAGE_SELECT_LEN];
965 unsigned char page;
966 struct i2c_client *i2c = rmi4_data->i2c_client;
967
968 page = ((address >> 8) & MASK_8BIT);
969 if (page != rmi4_data->current_page) {
970 buf[0] = MASK_8BIT;
971 buf[1] = page;
972 for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
973 retval = i2c_master_send(i2c, buf, PAGE_SELECT_LEN);
974 if (retval != PAGE_SELECT_LEN) {
975 dev_err(&i2c->dev,
976 "%s: I2C retry %d\n",
977 __func__, retry + 1);
978 msleep(20);
979 } else {
980 rmi4_data->current_page = page;
981 break;
982 }
983 }
984 } else
985 return PAGE_SELECT_LEN;
986 return (retval == PAGE_SELECT_LEN) ? retval : -EIO;
987}
988
989 /**
990 * synaptics_rmi4_i2c_read()
991 *
992 * Called by various functions in this driver, and also exported to
993 * other expansion Function modules such as rmi_dev.
994 *
995 * This function reads data of an arbitrary length from the sensor,
996 * starting from an assigned register address of the sensor, via I2C
997 * with a retry mechanism.
998 */
999static int synaptics_rmi4_i2c_read(struct synaptics_rmi4_data *rmi4_data,
1000 unsigned short addr, unsigned char *data, unsigned short length)
1001{
1002 int retval;
1003 unsigned char retry;
1004 unsigned char buf;
1005 struct i2c_msg msg[] = {
1006 {
1007 .addr = rmi4_data->i2c_client->addr,
1008 .flags = 0,
1009 .len = 1,
1010 .buf = &buf,
1011 },
1012 {
1013 .addr = rmi4_data->i2c_client->addr,
1014 .flags = I2C_M_RD,
1015 .len = length,
1016 .buf = data,
1017 },
1018 };
1019
1020 buf = addr & MASK_8BIT;
1021
1022 mutex_lock(&(rmi4_data->rmi4_io_ctrl_mutex));
1023
1024 retval = synaptics_rmi4_set_page(rmi4_data, addr);
1025 if (retval != PAGE_SELECT_LEN)
1026 goto exit;
1027
1028 for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
1029 if (i2c_transfer(rmi4_data->i2c_client->adapter, msg, 2) == 2) {
1030 retval = length;
1031 break;
1032 }
1033 dev_err(&rmi4_data->i2c_client->dev,
1034 "%s: I2C retry %d\n",
1035 __func__, retry + 1);
1036 msleep(20);
1037 }
1038
1039 if (retry == SYN_I2C_RETRY_TIMES) {
1040 dev_err(&rmi4_data->i2c_client->dev,
1041 "%s: I2C read over retry limit\n",
1042 __func__);
1043 retval = -EIO;
1044 }
1045
1046exit:
1047 mutex_unlock(&(rmi4_data->rmi4_io_ctrl_mutex));
1048
1049 return retval;
1050}
1051
1052 /**
1053 * synaptics_rmi4_i2c_write()
1054 *
1055 * Called by various functions in this driver, and also exported to
1056 * other expansion Function modules such as rmi_dev.
1057 *
1058 * This function writes data of an arbitrary length to the sensor,
1059 * starting from an assigned register address of the sensor, via I2C with
1060 * a retry mechanism.
1061 */
1062static int synaptics_rmi4_i2c_write(struct synaptics_rmi4_data *rmi4_data,
1063 unsigned short addr, unsigned char *data, unsigned short length)
1064{
1065 int retval;
1066 unsigned char retry;
1067 unsigned char buf[length + 1];
1068 struct i2c_msg msg[] = {
1069 {
1070 .addr = rmi4_data->i2c_client->addr,
1071 .flags = 0,
1072 .len = length + 1,
1073 .buf = buf,
1074 }
1075 };
1076
1077 mutex_lock(&(rmi4_data->rmi4_io_ctrl_mutex));
1078
1079 retval = synaptics_rmi4_set_page(rmi4_data, addr);
1080 if (retval != PAGE_SELECT_LEN)
1081 goto exit;
1082
1083 buf[0] = addr & MASK_8BIT;
1084 memcpy(&buf[1], &data[0], length);
1085
1086 for (retry = 0; retry < SYN_I2C_RETRY_TIMES; retry++) {
1087 if (i2c_transfer(rmi4_data->i2c_client->adapter, msg, 1) == 1) {
1088 retval = length;
1089 break;
1090 }
1091 dev_err(&rmi4_data->i2c_client->dev,
1092 "%s: I2C retry %d\n",
1093 __func__, retry + 1);
1094 msleep(20);
1095 }
1096
1097 if (retry == SYN_I2C_RETRY_TIMES) {
1098 dev_err(&rmi4_data->i2c_client->dev,
1099 "%s: I2C write over retry limit\n",
1100 __func__);
1101 retval = -EIO;
1102 }
1103
1104exit:
1105 mutex_unlock(&(rmi4_data->rmi4_io_ctrl_mutex));
1106
1107 return retval;
1108}
1109
1110/**
1111 * synaptics_rmi4_release_all()
1112 *
1113 * Called by synaptics_rmi4_suspend()
1114 *
1115 * Release all touch data during the touch device switch to suspend state.
1116 */
1117
1118static void synaptics_rmi4_release_all(struct synaptics_rmi4_data *rmi4_data)
1119{
1120 int finger;
1121 int max_num_fingers = rmi4_data->num_of_fingers;
1122
1123 for (finger = 0; finger < max_num_fingers; finger++) {
1124 input_mt_slot(rmi4_data->input_dev, finger);
1125 input_mt_report_slot_state(rmi4_data->input_dev,
1126 MT_TOOL_FINGER, 0);
1127 }
1128
1129 input_report_key(rmi4_data->input_dev, BTN_TOUCH, 0);
1130 input_report_key(rmi4_data->input_dev,
1131 BTN_TOOL_FINGER, 0);
1132
1133 input_sync(rmi4_data->input_dev);
1134}
1135
1136 /**
1137 * synaptics_rmi4_f11_abs_report()
1138 *
1139 * Called by synaptics_rmi4_report_touch() when valid Function $11
1140 * finger data has been detected.
1141 *
1142 * This function reads the Function $11 data registers, determines the
1143 * status of each finger supported by the Function, processes any
1144 * necessary coordinate manipulation, reports the finger data to
1145 * the input subsystem, and returns the number of fingers detected.
1146 */
1147static int synaptics_rmi4_f11_abs_report(struct synaptics_rmi4_data *rmi4_data,
1148 struct synaptics_rmi4_fn *fhandler)
1149{
1150 int retval;
1151 unsigned char touch_count = 0; /* number of touch points */
1152 unsigned char reg_index;
1153 unsigned char finger;
1154 unsigned char fingers_supported;
1155 unsigned char num_of_finger_status_regs;
1156 unsigned char finger_shift;
1157 unsigned char finger_status;
1158 unsigned char data_reg_blk_size;
1159 unsigned char finger_status_reg[3];
1160 unsigned char data[F11_STD_DATA_LEN];
1161 unsigned short data_addr;
1162 unsigned short data_offset;
1163 int x;
1164 int y;
1165 int wx;
1166 int wy;
1167 int z;
1168
1169 /*
1170 * The number of finger status registers is determined by the
1171 * maximum number of fingers supported - 2 bits per finger. So
1172 * the number of finger status registers to read is:
1173 * register_count = ceil(max_num_of_fingers / 4)
1174 */
1175 fingers_supported = fhandler->num_of_data_points;
1176 num_of_finger_status_regs = (fingers_supported + 3) / 4;
1177 data_addr = fhandler->full_addr.data_base;
1178 data_reg_blk_size = fhandler->size_of_data_register_block;
1179
1180 retval = synaptics_rmi4_i2c_read(rmi4_data,
1181 data_addr,
1182 finger_status_reg,
1183 num_of_finger_status_regs);
1184 if (retval < 0)
1185 return 0;
1186
1187 for (finger = 0; finger < fingers_supported; finger++) {
1188 reg_index = finger / 4;
1189 finger_shift = (finger % 4) * 2;
1190 finger_status = (finger_status_reg[reg_index] >> finger_shift)
1191 & MASK_2BIT;
1192
1193 /*
1194 * Each 2-bit finger status field represents the following:
1195 * 00 = finger not present
1196 * 01 = finger present and data accurate
1197 * 10 = finger present but data may be inaccurate
1198 * 11 = reserved
1199 */
1200#ifdef TYPE_B_PROTOCOL
1201 input_mt_slot(rmi4_data->input_dev, finger);
1202 input_mt_report_slot_state(rmi4_data->input_dev,
1203 MT_TOOL_FINGER, finger_status != 0);
1204#endif
1205
1206 if (finger_status) {
1207 data_offset = data_addr +
1208 num_of_finger_status_regs +
1209 (finger * data_reg_blk_size);
1210 retval = synaptics_rmi4_i2c_read(rmi4_data,
1211 data_offset,
1212 data,
1213 data_reg_blk_size);
1214 if (retval < 0)
1215 return 0;
1216
1217 x = (data[0] << 4) | (data[2] & MASK_4BIT);
1218 y = (data[1] << 4) | ((data[2] >> 4) & MASK_4BIT);
1219 wx = (data[3] & MASK_4BIT);
1220 wy = (data[3] >> 4) & MASK_4BIT;
1221 z = data[4];
1222
1223 if (rmi4_data->flip_x)
1224 x = rmi4_data->sensor_max_x - x;
1225 if (rmi4_data->flip_y)
1226 y = rmi4_data->sensor_max_y - y;
1227
1228 dev_dbg(&rmi4_data->i2c_client->dev,
1229 "%s: Finger %d:\n"
1230 "status = 0x%02x\n"
1231 "x = %d\n"
1232 "y = %d\n"
1233 "wx = %d\n"
1234 "wy = %d\n",
1235 __func__, finger,
1236 finger_status,
1237 x, y, wx, wy);
1238
1239 input_report_abs(rmi4_data->input_dev,
1240 ABS_MT_POSITION_X, x);
1241 input_report_abs(rmi4_data->input_dev,
1242 ABS_MT_POSITION_Y, y);
1243 input_report_abs(rmi4_data->input_dev,
1244 ABS_MT_PRESSURE, z);
1245
1246#ifdef REPORT_2D_W
1247 input_report_abs(rmi4_data->input_dev,
1248 ABS_MT_TOUCH_MAJOR, max(wx, wy));
1249 input_report_abs(rmi4_data->input_dev,
1250 ABS_MT_TOUCH_MINOR, min(wx, wy));
1251#endif
1252#ifndef TYPE_B_PROTOCOL
1253 input_mt_sync(rmi4_data->input_dev);
1254#endif
1255 touch_count++;
1256 }
1257 }
1258
1259 input_report_key(rmi4_data->input_dev, BTN_TOUCH, touch_count > 0);
1260 input_report_key(rmi4_data->input_dev,
1261 BTN_TOOL_FINGER, touch_count > 0);
1262
1263#ifndef TYPE_B_PROTOCOL
1264 if (!touch_count)
1265 input_mt_sync(rmi4_data->input_dev);
1266#else
1267 input_mt_report_pointer_emulation(rmi4_data->input_dev, false);
1268#endif
1269
1270 input_sync(rmi4_data->input_dev);
1271
1272 return touch_count;
1273}
1274
1275 /**
1276 * synaptics_rmi4_f12_abs_report()
1277 *
1278 * Called by synaptics_rmi4_report_touch() when valid Function $12
1279 * finger data has been detected.
1280 *
1281 * This function reads the Function $12 data registers, determines the
1282 * status of each finger supported by the Function, processes any
1283 * necessary coordinate manipulation, reports the finger data to
1284 * the input subsystem, and returns the number of fingers detected.
1285 */
1286static int synaptics_rmi4_f12_abs_report(struct synaptics_rmi4_data *rmi4_data,
1287 struct synaptics_rmi4_fn *fhandler)
1288{
1289 int retval;
1290 unsigned char touch_count = 0; /* number of touch points */
1291 unsigned char finger;
1292 unsigned char fingers_to_process;
1293 unsigned char finger_status;
1294 unsigned char size_of_2d_data;
1295 unsigned short data_addr;
1296 int x;
1297 int y;
1298 int wx;
1299 int wy;
1300 struct synaptics_rmi4_f12_extra_data *extra_data;
1301 struct synaptics_rmi4_f12_finger_data *data;
1302 struct synaptics_rmi4_f12_finger_data *finger_data;
1303
1304 fingers_to_process = fhandler->num_of_data_points;
1305 data_addr = fhandler->full_addr.data_base;
1306 extra_data = (struct synaptics_rmi4_f12_extra_data *)fhandler->extra;
1307 size_of_2d_data = sizeof(struct synaptics_rmi4_f12_finger_data);
1308
1309 retval = synaptics_rmi4_i2c_read(rmi4_data,
1310 data_addr + extra_data->data1_offset,
1311 (unsigned char *)fhandler->data,
1312 fingers_to_process * size_of_2d_data);
1313 if (retval < 0)
1314 return 0;
1315
1316 data = (struct synaptics_rmi4_f12_finger_data *)fhandler->data;
1317
1318 for (finger = 0; finger < fingers_to_process; finger++) {
1319 finger_data = data + finger;
1320 finger_status = finger_data->object_type_and_status & MASK_2BIT;
1321
1322 /*
1323 * Each 2-bit finger status field represents the following:
1324 * 00 = finger not present
1325 * 01 = finger present and data accurate
1326 * 10 = finger present but data may be inaccurate
1327 * 11 = reserved
1328 */
1329#ifdef TYPE_B_PROTOCOL
1330 input_mt_slot(rmi4_data->input_dev, finger);
1331 input_mt_report_slot_state(rmi4_data->input_dev,
1332 MT_TOOL_FINGER, finger_status != 0);
1333#endif
1334
1335 if (finger_status) {
1336 x = (finger_data->x_msb << 8) | (finger_data->x_lsb);
1337 y = (finger_data->y_msb << 8) | (finger_data->y_lsb);
1338#ifdef REPORT_2D_W
1339 wx = finger_data->wx;
1340 wy = finger_data->wy;
1341#endif
1342
1343 if (rmi4_data->flip_x)
1344 x = rmi4_data->sensor_max_x - x;
1345 if (rmi4_data->flip_y)
1346 y = rmi4_data->sensor_max_y - y;
1347
1348 dev_dbg(&rmi4_data->i2c_client->dev,
1349 "%s: Finger %d:\n"
1350 "status = 0x%02x\n"
1351 "x = %d\n"
1352 "y = %d\n"
1353 "wx = %d\n"
1354 "wy = %d\n",
1355 __func__, finger,
1356 finger_status,
1357 x, y, wx, wy);
1358
1359 input_report_key(rmi4_data->input_dev,
1360 BTN_TOUCH, 1);
1361 input_report_key(rmi4_data->input_dev,
1362 BTN_TOOL_FINGER, 1);
1363 input_report_abs(rmi4_data->input_dev,
1364 ABS_MT_POSITION_X, x);
1365 input_report_abs(rmi4_data->input_dev,
1366 ABS_MT_POSITION_Y, y);
1367#ifdef REPORT_2D_W
1368 input_report_abs(rmi4_data->input_dev,
1369 ABS_MT_TOUCH_MAJOR, max(wx, wy));
1370 input_report_abs(rmi4_data->input_dev,
1371 ABS_MT_TOUCH_MINOR, min(wx, wy));
1372#endif
1373#ifndef TYPE_B_PROTOCOL
1374 input_mt_sync(rmi4_data->input_dev);
1375#endif
1376 touch_count++;
1377 }
1378 }
1379
1380 input_report_key(rmi4_data->input_dev,
1381 BTN_TOUCH, touch_count > 0);
1382 input_report_key(rmi4_data->input_dev,
1383 BTN_TOOL_FINGER, touch_count > 0);
1384#ifndef TYPE_B_PROTOCOL
1385 if (!touch_count)
1386 input_mt_sync(rmi4_data->input_dev);
1387#endif
1388 input_mt_report_pointer_emulation(rmi4_data->input_dev, false);
1389 input_sync(rmi4_data->input_dev);
1390
1391 return touch_count;
1392}
1393
1394static void synaptics_rmi4_f1a_report(struct synaptics_rmi4_data *rmi4_data,
1395 struct synaptics_rmi4_fn *fhandler)
1396{
1397 int retval;
1398 unsigned char button;
1399 unsigned char index;
1400 unsigned char shift;
1401 unsigned char status;
1402 unsigned char *data;
1403 unsigned short data_addr = fhandler->full_addr.data_base;
1404 struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
1405 static unsigned char do_once = 1;
1406 static bool current_status[MAX_NUMBER_OF_BUTTONS];
1407#ifdef NO_0D_WHILE_2D
1408 static bool before_2d_status[MAX_NUMBER_OF_BUTTONS];
1409 static bool while_2d_status[MAX_NUMBER_OF_BUTTONS];
1410#endif
1411
1412 if (do_once) {
1413 memset(current_status, 0, sizeof(current_status));
1414#ifdef NO_0D_WHILE_2D
1415 memset(before_2d_status, 0, sizeof(before_2d_status));
1416 memset(while_2d_status, 0, sizeof(while_2d_status));
1417#endif
1418 do_once = 0;
1419 }
1420
1421 retval = synaptics_rmi4_i2c_read(rmi4_data,
1422 data_addr,
1423 f1a->button_data_buffer,
1424 f1a->button_bitmask_size);
1425 if (retval < 0) {
1426 dev_err(&rmi4_data->i2c_client->dev,
1427 "%s: Failed to read button data registers\n",
1428 __func__);
1429 return;
1430 }
1431
1432 data = f1a->button_data_buffer;
1433
1434 for (button = 0; button < f1a->valid_button_count; button++) {
1435 index = button / 8;
1436 shift = button % 8;
1437 status = ((data[index] >> shift) & MASK_1BIT);
1438
1439 if (current_status[button] == status)
1440 continue;
1441 else
1442 current_status[button] = status;
1443
1444 dev_dbg(&rmi4_data->i2c_client->dev,
1445 "%s: Button %d (code %d) ->%d\n",
1446 __func__, button,
1447 f1a->button_map[button],
1448 status);
1449#ifdef NO_0D_WHILE_2D
1450 if (rmi4_data->fingers_on_2d == false) {
1451 if (status == 1) {
1452 before_2d_status[button] = 1;
1453 } else {
1454 if (while_2d_status[button] == 1) {
1455 while_2d_status[button] = 0;
1456 continue;
1457 } else {
1458 before_2d_status[button] = 0;
1459 }
1460 }
1461 input_report_key(rmi4_data->input_dev,
1462 f1a->button_map[button],
1463 status);
1464 } else {
1465 if (before_2d_status[button] == 1) {
1466 before_2d_status[button] = 0;
1467 input_report_key(rmi4_data->input_dev,
1468 f1a->button_map[button],
1469 status);
1470 } else {
1471 if (status == 1)
1472 while_2d_status[button] = 1;
1473 else
1474 while_2d_status[button] = 0;
1475 }
1476 }
1477#else
1478 input_report_key(rmi4_data->input_dev,
1479 f1a->button_map[button],
1480 status);
1481#endif
1482 }
1483
1484 input_sync(rmi4_data->input_dev);
1485
1486 return;
1487}
1488
1489 /**
1490 * synaptics_rmi4_report_touch()
1491 *
1492 * Called by synaptics_rmi4_sensor_report().
1493 *
1494 * This function calls the appropriate finger data reporting function
1495 * based on the function handler it receives and returns the number of
1496 * fingers detected.
1497 */
1498static void synaptics_rmi4_report_touch(struct synaptics_rmi4_data *rmi4_data,
1499 struct synaptics_rmi4_fn *fhandler,
1500 unsigned char *touch_count)
1501{
1502 unsigned char touch_count_2d;
1503
1504 dev_dbg(&rmi4_data->i2c_client->dev,
1505 "%s: Function %02x reporting\n",
1506 __func__, fhandler->fn_number);
1507
1508 switch (fhandler->fn_number) {
1509 case SYNAPTICS_RMI4_F11:
1510 touch_count_2d = synaptics_rmi4_f11_abs_report(rmi4_data,
1511 fhandler);
1512
1513 *touch_count += touch_count_2d;
1514
1515 if (touch_count_2d)
1516 rmi4_data->fingers_on_2d = true;
1517 else
1518 rmi4_data->fingers_on_2d = false;
1519 break;
1520
1521 case SYNAPTICS_RMI4_F12:
1522 touch_count_2d = synaptics_rmi4_f12_abs_report(rmi4_data,
1523 fhandler);
1524
1525 if (touch_count_2d)
1526 rmi4_data->fingers_on_2d = true;
1527 else
1528 rmi4_data->fingers_on_2d = false;
1529 break;
1530
1531 case SYNAPTICS_RMI4_F1A:
1532 synaptics_rmi4_f1a_report(rmi4_data, fhandler);
1533 break;
1534
1535 default:
1536 break;
1537 }
1538
1539 return;
1540}
1541
1542 /**
1543 * synaptics_rmi4_sensor_report()
1544 *
1545 * Called by synaptics_rmi4_irq().
1546 *
1547 * This function determines the interrupt source(s) from the sensor
1548 * and calls synaptics_rmi4_report_touch() with the appropriate
1549 * function handler for each function with valid data inputs.
1550 */
1551static int synaptics_rmi4_sensor_report(struct synaptics_rmi4_data *rmi4_data)
1552{
1553 int retval;
1554 unsigned char touch_count = 0;
1555 unsigned char intr[MAX_INTR_REGISTERS];
1556 struct synaptics_rmi4_fn *fhandler;
1557 struct synaptics_rmi4_exp_fn *exp_fhandler;
1558 struct synaptics_rmi4_device_info *rmi;
1559
1560 rmi = &(rmi4_data->rmi4_mod_info);
1561
1562 /*
1563 * Get interrupt status information from F01 Data1 register to
1564 * determine the source(s) that are flagging the interrupt.
1565 */
1566 retval = synaptics_rmi4_i2c_read(rmi4_data,
1567 rmi4_data->f01_data_base_addr + 1,
1568 intr,
1569 rmi4_data->num_of_intr_regs);
1570 if (retval < 0)
1571 return retval;
1572
1573 /*
1574 * Traverse the function handler list and service the source(s)
1575 * of the interrupt accordingly.
1576 */
1577 mutex_lock(&rmi->support_fn_list_mutex);
1578 if (!list_empty(&rmi->support_fn_list)) {
1579 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
1580 if (fhandler->num_of_data_sources) {
1581 if (fhandler->intr_mask &
1582 intr[fhandler->intr_reg_num]) {
1583 synaptics_rmi4_report_touch(rmi4_data,
1584 fhandler, &touch_count);
1585 }
1586 }
1587 }
1588 }
1589 mutex_unlock(&rmi->support_fn_list_mutex);
1590
1591 mutex_lock(&exp_fn_list_mutex);
1592 if (!list_empty(&exp_fn_list)) {
1593 list_for_each_entry(exp_fhandler, &exp_fn_list, link) {
1594 if (exp_fhandler->inserted &&
1595 (exp_fhandler->func_attn != NULL))
1596 exp_fhandler->func_attn(rmi4_data, intr[0]);
1597 }
1598 }
1599 mutex_unlock(&exp_fn_list_mutex);
1600
1601 return touch_count;
1602}
1603
1604 /**
1605 * synaptics_rmi4_irq()
1606 *
1607 * Called by the kernel when an interrupt occurs (when the sensor
1608 * asserts the attention irq).
1609 *
1610 * This function is the ISR thread and handles the acquisition
1611 * and the reporting of finger data when the presence of fingers
1612 * is detected.
1613 */
1614static irqreturn_t synaptics_rmi4_irq(int irq, void *data)
1615{
1616 struct synaptics_rmi4_data *rmi4_data = data;
1617
1618 if (IRQ_HANDLED == synaptics_filter_interrupt(data))
1619 return IRQ_HANDLED;
1620
1621 synaptics_rmi4_sensor_report(rmi4_data);
1622
1623 return IRQ_HANDLED;
1624}
1625
1626#ifdef CONFIG_OF
1627static int synaptics_rmi4_get_button_map(struct device *dev, char *name,
1628 struct synaptics_rmi4_platform_data *rmi4_pdata,
1629 struct device_node *np)
1630{
1631 struct property *prop;
1632 int rc, i;
1633 u32 temp_val, num_buttons;
1634 u32 button_map[MAX_NUMBER_OF_BUTTONS];
1635
1636 prop = of_find_property(np, "synaptics,button-map", NULL);
1637 if (prop) {
1638 num_buttons = prop->length / sizeof(temp_val);
1639
1640 rmi4_pdata->capacitance_button_map = devm_kzalloc(dev,
1641 sizeof(*rmi4_pdata->capacitance_button_map),
1642 GFP_KERNEL);
1643 if (!rmi4_pdata->capacitance_button_map)
1644 return -ENOMEM;
1645
1646 rmi4_pdata->capacitance_button_map->map = devm_kzalloc(dev,
1647 sizeof(*rmi4_pdata->capacitance_button_map->map) *
1648 MAX_NUMBER_OF_BUTTONS, GFP_KERNEL);
1649 if (!rmi4_pdata->capacitance_button_map->map)
1650 return -ENOMEM;
1651
1652 if (num_buttons <= MAX_NUMBER_OF_BUTTONS) {
1653 rc = of_property_read_u32_array(np,
1654 "synaptics,button-map", button_map,
1655 num_buttons);
1656 if (rc) {
1657 dev_err(dev, "Unable to read key codes\n");
1658 return rc;
1659 }
1660 for (i = 0; i < num_buttons; i++)
1661 rmi4_pdata->capacitance_button_map->map[i] =
1662 button_map[i];
1663 rmi4_pdata->capacitance_button_map->nbuttons =
1664 num_buttons;
1665 } else {
1666 return -EINVAL;
1667 }
1668 }
1669 return 0;
1670}
1671
1672static int synaptics_rmi4_get_dt_coords(struct device *dev, char *name,
1673 struct synaptics_rmi4_platform_data *pdata,
1674 struct device_node *node)
1675{
1676 u32 coords[RMI4_COORDS_ARR_SIZE];
1677 struct property *prop;
1678 struct device_node *np = (node == NULL) ? (dev->of_node) : (node);
1679 int coords_size, rc;
1680
1681 prop = of_find_property(np, name, NULL);
1682 if (!prop)
1683 return -EINVAL;
1684 if (!prop->value)
1685 return -ENODATA;
1686
1687 coords_size = prop->length / sizeof(u32);
1688 if (coords_size != RMI4_COORDS_ARR_SIZE) {
1689 dev_err(dev, "invalid %s\n", name);
1690 return -EINVAL;
1691 }
1692
1693 rc = of_property_read_u32_array(np, name, coords, coords_size);
1694 if (rc && (rc != -EINVAL)) {
1695 dev_err(dev, "Unable to read %s\n", name);
1696 return rc;
1697 }
1698
1699 if (strcmp(name, "synaptics,panel-coords") == 0) {
1700 pdata->panel_minx = coords[0];
1701 pdata->panel_miny = coords[1];
1702 pdata->panel_maxx = coords[2];
1703 pdata->panel_maxy = coords[3];
1704
1705 if (pdata->panel_maxx == 0 || pdata->panel_minx > 0)
1706 rc = -EINVAL;
1707 else if (pdata->panel_maxy == 0 || pdata->panel_miny > 0)
1708 rc = -EINVAL;
1709
1710 if (rc) {
1711 dev_err(dev, "Invalid panel resolution %d\n", rc);
1712 return rc;
1713 }
1714 } else if (strcmp(name, "synaptics,display-coords") == 0) {
1715 pdata->disp_minx = coords[0];
1716 pdata->disp_miny = coords[1];
1717 pdata->disp_maxx = coords[2];
1718 pdata->disp_maxy = coords[3];
1719 } else {
1720 dev_err(dev, "unsupported property %s\n", name);
1721 return -EINVAL;
1722 }
1723
1724 return 0;
1725}
1726
1727static int synaptics_rmi4_parse_dt_children(struct device *dev,
1728 struct synaptics_rmi4_platform_data *rmi4_pdata,
1729 struct synaptics_rmi4_data *rmi4_data)
1730{
1731 struct synaptics_rmi4_device_info *rmi = &(rmi4_data->rmi4_mod_info);
1732 struct device_node *node = dev->of_node, *child;
1733 int rc = 0;
1734 struct synaptics_rmi4_fn *fhandler = NULL;
1735
1736 for_each_child_of_node(node, child) {
1737 rc = of_property_read_u32(child, "synaptics,package-id",
1738 &rmi4_pdata->package_id);
1739 if (rc && (rc != -EINVAL)) {
1740 dev_err(dev, "Unable to read package_id\n");
1741 return rc;
1742 } else if (rc == -EINVAL) {
1743 rmi4_pdata->package_id = 0x00;
1744 }
1745
1746 if (rmi4_pdata->package_id) {
1747 if (rmi4_pdata->package_id != rmi->package_id) {
1748 dev_err(dev,
1749 "%s: Synaptics package id don't match %d %d\n",
1750 __func__,
1751 rmi4_pdata->package_id,
1752 rmi->package_id);
1753
1754 continue;
1755 } else {
1756 /*
1757 * If package id read from DT matches the
1758 * package id value read from touch controller,
1759 * also check if sensor dimensions read from DT
1760 * match those read from controller, before
1761 * moving further. For this first check if touch
1762 * panel coordinates are defined in DT or not.
1763 */
1764 if (of_find_property(child,
1765 "synaptics,panel-coords", NULL)) {
1766 synaptics_rmi4_get_dt_coords(dev,
1767 "synaptics,panel-coords",
1768 rmi4_pdata, child);
1769 dev_info(dev, "Pmax_x Pmax_y = %d:%d\n",
1770 rmi4_pdata->panel_maxx,
1771 rmi4_pdata->panel_maxy);
1772 dev_info(dev, "Smax_x Smax_y = %d:%d\n",
1773 rmi4_data->sensor_max_x,
1774 rmi4_data->sensor_max_y);
1775 if ((rmi4_pdata->panel_maxx !=
1776 rmi4_data->sensor_max_x) ||
1777 (rmi4_pdata->panel_maxy !=
1778 rmi4_data->sensor_max_y))
1779 continue;
1780 }
1781 }
1782 }
1783
1784 rc = synaptics_rmi4_get_dt_coords(dev,
1785 "synaptics,display-coords",
1786 rmi4_pdata,
1787 child);
1788 if (rc && (rc != -EINVAL))
1789 return rc;
1790
1791 rc = synaptics_rmi4_get_button_map(dev, "synaptics,button-map",
1792 rmi4_pdata, child);
1793 if (rc < 0) {
1794 dev_err(dev, "Unable to read key codes\n");
1795 return rc;
1796 }
1797
1798 mutex_lock(&rmi->support_fn_list_mutex);
1799 if (!list_empty(&rmi->support_fn_list)) {
1800 list_for_each_entry(fhandler,
1801 &rmi->support_fn_list, link) {
1802 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
1803 break;
1804 }
1805 }
1806 mutex_unlock(&rmi->support_fn_list_mutex);
1807
1808 if (fhandler != NULL && fhandler->fn_number ==
1809 SYNAPTICS_RMI4_F1A) {
1810 rc = synaptics_rmi4_capacitance_button_map(rmi4_data,
1811 fhandler);
1812 if (rc < 0) {
1813 dev_err(dev, "Fail to register F1A %d\n", rc);
1814 return rc;
1815 }
1816 }
1817 break;
1818 }
1819
1820 return 0;
1821}
1822
1823static int synaptics_rmi4_parse_dt(struct device *dev,
1824 struct synaptics_rmi4_platform_data *rmi4_pdata)
1825{
1826 struct device_node *np = dev->of_node;
1827 struct property *prop;
1828 u32 temp_val, num_buttons;
1829 u32 button_map[MAX_NUMBER_OF_BUTTONS];
1830 int rc, i;
1831
1832 rmi4_pdata->i2c_pull_up = of_property_read_bool(np,
1833 "synaptics,i2c-pull-up");
1834 rmi4_pdata->power_down_enable = of_property_read_bool(np,
1835 "synaptics,power-down");
1836 rmi4_pdata->disable_gpios = of_property_read_bool(np,
1837 "synaptics,disable-gpios");
1838 rmi4_pdata->modify_reso = of_property_read_bool(np,
1839 "synaptics,modify-reso");
1840 rmi4_pdata->x_flip = of_property_read_bool(np, "synaptics,x-flip");
1841 rmi4_pdata->y_flip = of_property_read_bool(np, "synaptics,y-flip");
1842 rmi4_pdata->do_lockdown = of_property_read_bool(np,
1843 "synaptics,do-lockdown");
1844
1845 rc = synaptics_rmi4_get_dt_coords(dev, "synaptics,display-coords",
1846 rmi4_pdata, NULL);
1847 if (rc && (rc != -EINVAL))
1848 return rc;
1849
1850 rc = synaptics_rmi4_get_dt_coords(dev, "synaptics,panel-coords",
1851 rmi4_pdata, NULL);
1852 if (rc && (rc != -EINVAL))
1853 return rc;
1854
1855 rmi4_pdata->reset_delay = RESET_DELAY;
1856 rc = of_property_read_u32(np, "synaptics,reset-delay", &temp_val);
1857 if (!rc)
1858 rmi4_pdata->reset_delay = temp_val;
1859 else if (rc != -EINVAL) {
1860 dev_err(dev, "Unable to read reset delay\n");
1861 return rc;
1862 }
1863
1864 rc = of_property_read_string(np, "synaptics,fw-image-name",
1865 &rmi4_pdata->fw_image_name);
1866 if (rc && (rc != -EINVAL)) {
1867 dev_err(dev, "Unable to read fw image name\n");
1868 return rc;
1869 }
1870
1871 /* reset, irq gpio info */
1872 rmi4_pdata->reset_gpio = of_get_named_gpio_flags(np,
1873 "synaptics,reset-gpio", 0, &rmi4_pdata->reset_flags);
1874 rmi4_pdata->irq_gpio = of_get_named_gpio_flags(np,
1875 "synaptics,irq-gpio", 0, &rmi4_pdata->irq_flags);
1876
1877 rmi4_pdata->detect_device = of_property_read_bool(np,
1878 "synaptics,detect-device");
1879
1880 if (rmi4_pdata->detect_device)
1881 return 0;
1882
1883 prop = of_find_property(np, "synaptics,button-map", NULL);
1884 if (prop) {
1885 num_buttons = prop->length / sizeof(temp_val);
1886
1887 rmi4_pdata->capacitance_button_map = devm_kzalloc(dev,
1888 sizeof(*rmi4_pdata->capacitance_button_map),
1889 GFP_KERNEL);
1890 if (!rmi4_pdata->capacitance_button_map)
1891 return -ENOMEM;
1892
1893 rmi4_pdata->capacitance_button_map->map = devm_kzalloc(dev,
1894 sizeof(*rmi4_pdata->capacitance_button_map->map) *
1895 MAX_NUMBER_OF_BUTTONS, GFP_KERNEL);
1896 if (!rmi4_pdata->capacitance_button_map->map)
1897 return -ENOMEM;
1898
1899 if (num_buttons <= MAX_NUMBER_OF_BUTTONS) {
1900 rc = of_property_read_u32_array(np,
1901 "synaptics,button-map", button_map,
1902 num_buttons);
1903 if (rc) {
1904 dev_err(dev, "Unable to read key codes\n");
1905 return rc;
1906 }
1907 for (i = 0; i < num_buttons; i++)
1908 rmi4_pdata->capacitance_button_map->map[i] =
1909 button_map[i];
1910 rmi4_pdata->capacitance_button_map->nbuttons =
1911 num_buttons;
1912 } else {
1913 return -EINVAL;
1914 }
1915 }
1916
1917 return 0;
1918}
1919#else
1920static inline int synaptics_rmi4_parse_dt(struct device *dev,
1921 struct synaptics_rmi4_platform_data *rmi4_pdata)
1922{
1923 return 0;
1924}
1925#endif
1926
1927 /**
1928 * synaptics_rmi4_irq_enable()
1929 *
1930 * Called by synaptics_rmi4_probe() and the power management functions
1931 * in this driver and also exported to other expansion Function modules
1932 * such as rmi_dev.
1933 *
1934 * This function handles the enabling and disabling of the attention
1935 * irq including the setting up of the ISR thread.
1936 */
1937static int synaptics_rmi4_irq_enable(struct synaptics_rmi4_data *rmi4_data,
1938 bool enable)
1939{
1940 int retval = 0;
1941 unsigned char *intr_status;
1942
1943 if (enable) {
1944 if (rmi4_data->irq_enabled)
1945 return retval;
1946
1947 intr_status = kzalloc(rmi4_data->num_of_intr_regs, GFP_KERNEL);
1948 if (!intr_status) {
1949 dev_err(&rmi4_data->i2c_client->dev,
1950 "%s: Failed to alloc memory\n",
1951 __func__);
1952 return -ENOMEM;
1953 }
1954 /* Clear interrupts first */
1955 retval = synaptics_rmi4_i2c_read(rmi4_data,
1956 rmi4_data->f01_data_base_addr + 1,
1957 intr_status,
1958 rmi4_data->num_of_intr_regs);
1959 kfree(intr_status);
1960 if (retval < 0)
1961 return retval;
1962
1963 enable_irq(rmi4_data->irq);
1964
1965 rmi4_data->irq_enabled = true;
1966 } else {
1967 if (rmi4_data->irq_enabled) {
1968 disable_irq(rmi4_data->irq);
1969 rmi4_data->irq_enabled = false;
1970 }
1971 }
1972
1973 return retval;
1974}
1975
1976 /**
1977 * synaptics_rmi4_f11_init()
1978 *
1979 * Called by synaptics_rmi4_query_device().
1980 *
1981 * This funtion parses information from the Function 11 registers
1982 * and determines the number of fingers supported, x and y data ranges,
1983 * offset to the associated interrupt status register, interrupt bit
1984 * mask, and gathers finger data acquisition capabilities from the query
1985 * registers.
1986 */
1987static int synaptics_rmi4_f11_init(struct synaptics_rmi4_data *rmi4_data,
1988 struct synaptics_rmi4_fn *fhandler,
1989 struct synaptics_rmi4_fn_desc *fd,
1990 unsigned int intr_count)
1991{
1992 int retval;
1993 unsigned char ii;
1994 unsigned char intr_offset;
1995 unsigned char abs_data_size;
1996 unsigned char abs_data_blk_size;
1997 unsigned char query[F11_STD_QUERY_LEN];
1998 unsigned char control[F11_STD_CTRL_LEN];
1999
2000 fhandler->fn_number = fd->fn_number;
2001 fhandler->num_of_data_sources = fd->intr_src_count;
2002
2003 retval = synaptics_rmi4_i2c_read(rmi4_data,
2004 fhandler->full_addr.query_base,
2005 query,
2006 sizeof(query));
2007 if (retval < 0)
2008 return retval;
2009
2010 /* Maximum number of fingers supported */
2011 if ((query[1] & MASK_3BIT) <= 4)
2012 fhandler->num_of_data_points = (query[1] & MASK_3BIT) + 1;
2013 else if ((query[1] & MASK_3BIT) == 5)
2014 fhandler->num_of_data_points = 10;
2015
2016 rmi4_data->num_of_fingers = fhandler->num_of_data_points;
2017
2018 retval = synaptics_rmi4_i2c_read(rmi4_data,
2019 fhandler->full_addr.ctrl_base,
2020 control,
2021 sizeof(control));
2022 if (retval < 0)
2023 return retval;
2024
2025 /* Maximum x */
2026 rmi4_data->sensor_max_x = ((control[6] & MASK_8BIT) << 0) |
2027 ((control[7] & MASK_4BIT) << 8);
2028
2029 if (rmi4_data->board->modify_reso) {
2030 if (rmi4_data->board->panel_maxx) {
2031 if (rmi4_data->board->panel_maxx >= F11_MAX_X) {
2032 dev_err(&rmi4_data->i2c_client->dev,
2033 "F11 max_x value out of bound.");
2034 return -EINVAL;
2035 }
2036 if (rmi4_data->sensor_max_x !=
2037 rmi4_data->board->panel_maxx) {
2038 rmi4_data->sensor_max_x =
2039 rmi4_data->board->panel_maxx;
2040 control[6] = rmi4_data->board->panel_maxx
2041 & MASK_8BIT;
2042 control[7] = (rmi4_data->board->panel_maxx >> 8)
2043 & MASK_4BIT;
2044 retval = synaptics_rmi4_i2c_write(rmi4_data,
2045 fhandler->full_addr.ctrl_base,
2046 control,
2047 sizeof(control));
2048 if (retval < 0)
2049 return retval;
2050 }
2051 }
2052 }
2053
2054 /* Maximum y */
2055 rmi4_data->sensor_max_y = ((control[8] & MASK_8BIT) << 0) |
2056 ((control[9] & MASK_4BIT) << 8);
2057
2058 if (rmi4_data->board->modify_reso) {
2059 if (rmi4_data->board->panel_maxy) {
2060 if (rmi4_data->board->panel_maxy >= F11_MAX_Y) {
2061 dev_err(&rmi4_data->i2c_client->dev,
2062 "F11 max_y value out of bound.");
2063 return -EINVAL;
2064 }
2065 if (rmi4_data->sensor_max_y !=
2066 rmi4_data->board->panel_maxy) {
2067 rmi4_data->sensor_max_y =
2068 rmi4_data->board->panel_maxy;
2069 control[8] = rmi4_data->board->panel_maxy
2070 & MASK_8BIT;
2071 control[9] = (rmi4_data->board->panel_maxy >> 8)
2072 & MASK_4BIT;
2073 retval = synaptics_rmi4_i2c_write(rmi4_data,
2074 fhandler->full_addr.ctrl_base,
2075 control,
2076 sizeof(control));
2077 if (retval < 0)
2078 return retval;
2079 }
2080 }
2081 }
2082
2083 dev_dbg(&rmi4_data->i2c_client->dev,
2084 "%s: Function %02x max x = %d max y = %d\n",
2085 __func__, fhandler->fn_number,
2086 rmi4_data->sensor_max_x,
2087 rmi4_data->sensor_max_y);
2088
2089 rmi4_data->max_touch_width = MAX_F11_TOUCH_WIDTH;
2090
2091 fhandler->intr_reg_num = (intr_count + 7) / 8;
2092 if (fhandler->intr_reg_num != 0)
2093 fhandler->intr_reg_num -= 1;
2094
2095 /* Set an enable bit for each data source */
2096 intr_offset = intr_count % 8;
2097 fhandler->intr_mask = 0;
2098 for (ii = intr_offset;
2099 ii < ((fd->intr_src_count & MASK_3BIT) +
2100 intr_offset);
2101 ii++)
2102 fhandler->intr_mask |= 1 << ii;
2103
2104 abs_data_size = query[5] & MASK_2BIT;
2105 abs_data_blk_size = 3 + (2 * (abs_data_size == 0 ? 1 : 0));
2106 fhandler->size_of_data_register_block = abs_data_blk_size;
2107
2108 return retval;
2109}
2110
2111static int synaptics_rmi4_f12_set_enables(struct synaptics_rmi4_data *rmi4_data,
2112 unsigned short ctrl28)
2113{
2114 int retval;
2115 static unsigned short ctrl_28_address;
2116
2117 if (ctrl28)
2118 ctrl_28_address = ctrl28;
2119
2120 retval = synaptics_rmi4_i2c_write(rmi4_data,
2121 ctrl_28_address,
2122 &rmi4_data->report_enable,
2123 sizeof(rmi4_data->report_enable));
2124 if (retval < 0)
2125 return retval;
2126
2127 return retval;
2128}
2129
2130 /**
2131 * synaptics_rmi4_f12_init()
2132 *
2133 * Called by synaptics_rmi4_query_device().
2134 *
2135 * This funtion parses information from the Function 12 registers and
2136 * determines the number of fingers supported, offset to the data1
2137 * register, x and y data ranges, offset to the associated interrupt
2138 * status register, interrupt bit mask, and allocates memory resources
2139 * for finger data acquisition.
2140 */
2141static int synaptics_rmi4_f12_init(struct synaptics_rmi4_data *rmi4_data,
2142 struct synaptics_rmi4_fn *fhandler,
2143 struct synaptics_rmi4_fn_desc *fd,
2144 unsigned int intr_count)
2145{
2146 int retval;
2147 unsigned char ii;
2148 unsigned char intr_offset;
2149 unsigned char size_of_2d_data;
2150 unsigned char size_of_query8;
2151 unsigned char ctrl_8_offset;
2152 unsigned char ctrl_23_offset;
2153 unsigned char ctrl_28_offset;
2154 unsigned char num_of_fingers;
2155 struct synaptics_rmi4_f12_extra_data *extra_data;
2156 struct synaptics_rmi4_f12_query_5 query_5;
2157 struct synaptics_rmi4_f12_query_8 query_8;
2158 struct synaptics_rmi4_f12_ctrl_8 ctrl_8;
2159 struct synaptics_rmi4_f12_ctrl_23 ctrl_23;
2160
2161 fhandler->fn_number = fd->fn_number;
2162 fhandler->num_of_data_sources = fd->intr_src_count;
2163 size_of_2d_data = sizeof(struct synaptics_rmi4_f12_finger_data);
2164
2165 fhandler->extra = kmalloc(sizeof(*extra_data), GFP_KERNEL);
2166 if (!fhandler->extra) {
2167 dev_err(&rmi4_data->i2c_client->dev,
2168 "%s: Failed to alloc mem for function handler\n",
2169 __func__);
2170 return -ENOMEM;
2171 }
2172 extra_data = (struct synaptics_rmi4_f12_extra_data *)fhandler->extra;
2173
2174 retval = synaptics_rmi4_i2c_read(rmi4_data,
2175 fhandler->full_addr.query_base + 5,
2176 query_5.data,
2177 sizeof(query_5.data));
2178 if (retval < 0)
2179 goto free_function_handler_mem;
2180
2181 ctrl_8_offset = query_5.ctrl0_is_present +
2182 query_5.ctrl1_is_present +
2183 query_5.ctrl2_is_present +
2184 query_5.ctrl3_is_present +
2185 query_5.ctrl4_is_present +
2186 query_5.ctrl5_is_present +
2187 query_5.ctrl6_is_present +
2188 query_5.ctrl7_is_present;
2189
2190 ctrl_23_offset = ctrl_8_offset +
2191 query_5.ctrl8_is_present +
2192 query_5.ctrl9_is_present +
2193 query_5.ctrl10_is_present +
2194 query_5.ctrl11_is_present +
2195 query_5.ctrl12_is_present +
2196 query_5.ctrl13_is_present +
2197 query_5.ctrl14_is_present +
2198 query_5.ctrl15_is_present +
2199 query_5.ctrl16_is_present +
2200 query_5.ctrl17_is_present +
2201 query_5.ctrl18_is_present +
2202 query_5.ctrl19_is_present +
2203 query_5.ctrl20_is_present +
2204 query_5.ctrl21_is_present +
2205 query_5.ctrl22_is_present;
2206
2207 ctrl_28_offset = ctrl_23_offset +
2208 query_5.ctrl23_is_present +
2209 query_5.ctrl24_is_present +
2210 query_5.ctrl25_is_present +
2211 query_5.ctrl26_is_present +
2212 query_5.ctrl27_is_present;
2213
2214 retval = synaptics_rmi4_i2c_read(rmi4_data,
2215 fhandler->full_addr.ctrl_base + ctrl_23_offset,
2216 ctrl_23.data,
2217 sizeof(ctrl_23.data));
2218 if (retval < 0)
2219 goto free_function_handler_mem;
2220
2221 /* Maximum number of fingers supported */
2222 fhandler->num_of_data_points = min(ctrl_23.max_reported_objects,
2223 (unsigned char)F12_FINGERS_TO_SUPPORT);
2224
2225 num_of_fingers = fhandler->num_of_data_points;
2226 rmi4_data->num_of_fingers = num_of_fingers;
2227
2228 retval = synaptics_rmi4_i2c_read(rmi4_data,
2229 fhandler->full_addr.query_base + 7,
2230 &size_of_query8,
2231 sizeof(size_of_query8));
2232 if (retval < 0)
2233 goto free_function_handler_mem;
2234
2235 retval = synaptics_rmi4_i2c_read(rmi4_data,
2236 fhandler->full_addr.query_base + 8,
2237 query_8.data,
2238 size_of_query8);
2239 if (retval < 0)
2240 goto free_function_handler_mem;
2241
2242 /* Determine the presence of the Data0 register */
2243 extra_data->data1_offset = query_8.data0_is_present;
2244
2245 if ((size_of_query8 >= 3) && (query_8.data15_is_present)) {
2246 extra_data->data15_offset = query_8.data0_is_present +
2247 query_8.data1_is_present +
2248 query_8.data2_is_present +
2249 query_8.data3_is_present +
2250 query_8.data4_is_present +
2251 query_8.data5_is_present +
2252 query_8.data6_is_present +
2253 query_8.data7_is_present +
2254 query_8.data8_is_present +
2255 query_8.data9_is_present +
2256 query_8.data10_is_present +
2257 query_8.data11_is_present +
2258 query_8.data12_is_present +
2259 query_8.data13_is_present +
2260 query_8.data14_is_present;
2261 extra_data->data15_size = (num_of_fingers + 7) / 8;
2262 } else {
2263 extra_data->data15_size = 0;
2264 }
2265
2266 rmi4_data->report_enable = RPT_DEFAULT;
2267#ifdef REPORT_2D_Z
2268 rmi4_data->report_enable |= RPT_Z;
2269#endif
2270#ifdef REPORT_2D_W
2271 rmi4_data->report_enable |= (RPT_WX | RPT_WY);
2272#endif
2273
2274 retval = synaptics_rmi4_f12_set_enables(rmi4_data,
2275 fhandler->full_addr.ctrl_base + ctrl_28_offset);
2276 if (retval < 0)
2277 goto free_function_handler_mem;
2278
2279 retval = synaptics_rmi4_i2c_read(rmi4_data,
2280 fhandler->full_addr.ctrl_base + ctrl_8_offset,
2281 ctrl_8.data,
2282 sizeof(ctrl_8.data));
2283 if (retval < 0)
2284 goto free_function_handler_mem;
2285
2286 /* Maximum x */
2287 rmi4_data->sensor_max_x =
2288 ((unsigned short)ctrl_8.max_x_coord_lsb << 0) |
2289 ((unsigned short)ctrl_8.max_x_coord_msb << 8);
2290
2291 if (rmi4_data->board->modify_reso) {
2292 if (rmi4_data->board->panel_maxx) {
2293 if (rmi4_data->board->panel_maxx >= F12_MAX_X) {
2294 dev_err(&rmi4_data->i2c_client->dev,
2295 "F12 max_x value out of bound.");
2296 retval = -EINVAL;
2297 goto free_function_handler_mem;
2298 }
2299 if (rmi4_data->sensor_max_x !=
2300 rmi4_data->board->panel_maxx) {
2301 rmi4_data->sensor_max_x =
2302 rmi4_data->board->panel_maxx;
2303 ctrl_8.max_x_coord_lsb = (unsigned char)
2304 (rmi4_data->board->panel_maxx
2305 & MASK_8BIT);
2306 ctrl_8.max_x_coord_msb = (unsigned char)
2307 ((rmi4_data->board->panel_maxx >> 8)
2308 & MASK_8BIT);
2309 retval = synaptics_rmi4_i2c_write(rmi4_data,
2310 fhandler->full_addr.ctrl_base
2311 + ctrl_8_offset,
2312 ctrl_8.data,
2313 sizeof(ctrl_8.data));
2314 if (retval < 0)
2315 goto free_function_handler_mem;
2316 }
2317 }
2318 }
2319
2320 /* Maximum y */
2321 rmi4_data->sensor_max_y =
2322 ((unsigned short)ctrl_8.max_y_coord_lsb << 0) |
2323 ((unsigned short)ctrl_8.max_y_coord_msb << 8);
2324
2325 if (rmi4_data->board->modify_reso) {
2326 if (rmi4_data->board->panel_maxy) {
2327 if (rmi4_data->board->panel_maxy >= F12_MAX_Y) {
2328 dev_err(&rmi4_data->i2c_client->dev,
2329 "F12 max_y value out of bound.");
2330 retval = -EINVAL;
2331 goto free_function_handler_mem;
2332 }
2333 if (rmi4_data->sensor_max_y !=
2334 rmi4_data->board->panel_maxy) {
2335 rmi4_data->sensor_max_y =
2336 rmi4_data->board->panel_maxy;
2337 ctrl_8.max_y_coord_lsb = (unsigned char)
2338 (rmi4_data->board->panel_maxy
2339 & MASK_8BIT);
2340 ctrl_8.max_y_coord_msb = (unsigned char)
2341 ((rmi4_data->board->panel_maxy >> 8)
2342 & MASK_8BIT);
2343 retval = synaptics_rmi4_i2c_write(rmi4_data,
2344 fhandler->full_addr.ctrl_base
2345 + ctrl_8_offset,
2346 ctrl_8.data,
2347 sizeof(ctrl_8.data));
2348 if (retval < 0)
2349 goto free_function_handler_mem;
2350 }
2351 }
2352 }
2353
2354 dev_dbg(&rmi4_data->i2c_client->dev,
2355 "%s: Function %02x max x = %d max y = %d\n",
2356 __func__, fhandler->fn_number,
2357 rmi4_data->sensor_max_x,
2358 rmi4_data->sensor_max_y);
2359
2360 rmi4_data->num_of_rx = ctrl_8.num_of_rx;
2361 rmi4_data->num_of_tx = ctrl_8.num_of_tx;
2362 rmi4_data->max_touch_width = max(rmi4_data->num_of_rx,
2363 rmi4_data->num_of_tx);
2364
2365 fhandler->intr_reg_num = (intr_count + 7) / 8;
2366 if (fhandler->intr_reg_num != 0)
2367 fhandler->intr_reg_num -= 1;
2368
2369 /* Set an enable bit for each data source */
2370 intr_offset = intr_count % 8;
2371 fhandler->intr_mask = 0;
2372 for (ii = intr_offset;
2373 ii < ((fd->intr_src_count & MASK_3BIT) +
2374 intr_offset);
2375 ii++)
2376 fhandler->intr_mask |= 1 << ii;
2377
2378 /* Allocate memory for finger data storage space */
2379 fhandler->data_size = num_of_fingers * size_of_2d_data;
2380 fhandler->data = kmalloc(fhandler->data_size, GFP_KERNEL);
2381 if (!fhandler->data) {
2382 dev_err(&rmi4_data->i2c_client->dev,
2383 "%s: Failed to alloc mem for function handler data\n",
2384 __func__);
2385 retval = -ENOMEM;
2386 goto free_function_handler_mem;
2387 }
2388
2389 return retval;
2390
2391free_function_handler_mem:
2392 kfree(fhandler->extra);
2393 return retval;
2394}
2395
2396static int synaptics_rmi4_f1a_alloc_mem(struct synaptics_rmi4_data *rmi4_data,
2397 struct synaptics_rmi4_fn *fhandler)
2398{
2399 int retval;
2400 struct synaptics_rmi4_f1a_handle *f1a;
2401
2402 f1a = kzalloc(sizeof(*f1a), GFP_KERNEL);
2403 if (!f1a) {
2404 dev_err(&rmi4_data->i2c_client->dev,
2405 "%s: Failed to alloc mem for function handle\n",
2406 __func__);
2407 return -ENOMEM;
2408 }
2409
2410 fhandler->data = (void *)f1a;
2411
2412 retval = synaptics_rmi4_i2c_read(rmi4_data,
2413 fhandler->full_addr.query_base,
2414 f1a->button_query.data,
2415 sizeof(f1a->button_query.data));
2416 if (retval < 0) {
2417 dev_err(&rmi4_data->i2c_client->dev,
2418 "%s: Failed to read query registers\n",
2419 __func__);
2420 return retval;
2421 }
2422
2423 f1a->button_count = f1a->button_query.max_button_count + 1;
2424 f1a->button_bitmask_size = (f1a->button_count + 7) / 8;
2425
2426 f1a->button_data_buffer = kcalloc(f1a->button_bitmask_size,
2427 sizeof(*(f1a->button_data_buffer)), GFP_KERNEL);
2428 if (!f1a->button_data_buffer) {
2429 dev_err(&rmi4_data->i2c_client->dev,
2430 "%s: Failed to alloc mem for data buffer\n",
2431 __func__);
2432 return -ENOMEM;
2433 }
2434
2435 f1a->button_map = kcalloc(f1a->button_count,
2436 sizeof(*(f1a->button_map)), GFP_KERNEL);
2437 if (!f1a->button_map) {
2438 dev_err(&rmi4_data->i2c_client->dev,
2439 "%s: Failed to alloc mem for button map\n",
2440 __func__);
2441 return -ENOMEM;
2442 }
2443
2444 return 0;
2445}
2446
2447static int synaptics_rmi4_capacitance_button_map(
2448 struct synaptics_rmi4_data *rmi4_data,
2449 struct synaptics_rmi4_fn *fhandler)
2450{
2451 unsigned char ii;
2452 struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
2453 const struct synaptics_rmi4_platform_data *pdata = rmi4_data->board;
2454
2455 if (!pdata->capacitance_button_map) {
2456 dev_info(&rmi4_data->i2c_client->dev,
2457 "%s: capacitance_button_map not in use\n",
2458 __func__);
2459 return 0;
2460 } else if (!pdata->capacitance_button_map->map) {
2461 dev_err(&rmi4_data->i2c_client->dev,
2462 "%s: Button map is missing in board file\n",
2463 __func__);
2464 return -ENODEV;
2465 } else {
2466 if (pdata->capacitance_button_map->nbuttons !=
2467 f1a->button_count) {
2468 f1a->valid_button_count = min(f1a->button_count,
2469 pdata->capacitance_button_map->nbuttons);
2470 } else {
2471 f1a->valid_button_count = f1a->button_count;
2472 }
2473
2474 for (ii = 0; ii < f1a->valid_button_count; ii++)
2475 f1a->button_map[ii] =
2476 pdata->capacitance_button_map->map[ii];
2477 }
2478
2479 return 0;
2480}
2481
2482static void synaptics_rmi4_f1a_kfree(struct synaptics_rmi4_fn *fhandler)
2483{
2484 struct synaptics_rmi4_f1a_handle *f1a = fhandler->data;
2485
2486 if (f1a) {
2487 kfree(f1a->button_data_buffer);
2488 kfree(f1a->button_map);
2489 kfree(f1a);
2490 fhandler->data = NULL;
2491 }
2492
2493 return;
2494}
2495
2496static int synaptics_rmi4_f1a_init(struct synaptics_rmi4_data *rmi4_data,
2497 struct synaptics_rmi4_fn *fhandler,
2498 struct synaptics_rmi4_fn_desc *fd,
2499 unsigned int intr_count)
2500{
2501 int retval;
2502 unsigned char ii;
2503 unsigned short intr_offset;
2504
2505 fhandler->fn_number = fd->fn_number;
2506 fhandler->num_of_data_sources = fd->intr_src_count;
2507
2508 fhandler->intr_reg_num = (intr_count + 7) / 8;
2509 if (fhandler->intr_reg_num != 0)
2510 fhandler->intr_reg_num -= 1;
2511
2512 /* Set an enable bit for each data source */
2513 intr_offset = intr_count % 8;
2514 fhandler->intr_mask = 0;
2515 for (ii = intr_offset;
2516 ii < ((fd->intr_src_count & MASK_3BIT) +
2517 intr_offset);
2518 ii++)
2519 fhandler->intr_mask |= 1 << ii;
2520
2521 retval = synaptics_rmi4_f1a_alloc_mem(rmi4_data, fhandler);
2522 if (retval < 0)
2523 goto error_exit;
2524
2525 retval = synaptics_rmi4_capacitance_button_map(rmi4_data, fhandler);
2526 if (retval < 0)
2527 goto error_exit;
2528
2529 rmi4_data->button_0d_enabled = 1;
2530
2531 return 0;
2532
2533error_exit:
2534 synaptics_rmi4_f1a_kfree(fhandler);
2535
2536 return retval;
2537}
2538
2539static int synaptics_rmi4_alloc_fh(struct synaptics_rmi4_fn **fhandler,
2540 struct synaptics_rmi4_fn_desc *rmi_fd, int page_number)
2541{
2542 *fhandler = kzalloc(sizeof(**fhandler), GFP_KERNEL);
2543 if (!(*fhandler))
2544 return -ENOMEM;
2545
2546 (*fhandler)->full_addr.data_base =
2547 (rmi_fd->data_base_addr |
2548 (page_number << 8));
2549 (*fhandler)->full_addr.ctrl_base =
2550 (rmi_fd->ctrl_base_addr |
2551 (page_number << 8));
2552 (*fhandler)->full_addr.cmd_base =
2553 (rmi_fd->cmd_base_addr |
2554 (page_number << 8));
2555 (*fhandler)->full_addr.query_base =
2556 (rmi_fd->query_base_addr |
2557 (page_number << 8));
2558 (*fhandler)->fn_number = rmi_fd->fn_number;
2559
2560 return 0;
2561}
2562
2563
2564 /**
2565 * synaptics_rmi4_query_device_info()
2566 *
2567 * Called by synaptics_rmi4_query_device().
2568 *
2569 */
2570static int synaptics_rmi4_query_device_info(
2571 struct synaptics_rmi4_data *rmi4_data)
2572{
2573 int retval;
2574 unsigned char f01_query[F01_STD_QUERY_LEN];
2575 struct synaptics_rmi4_device_info *rmi = &(rmi4_data->rmi4_mod_info);
2576 unsigned char pkg_id[4];
2577
2578 retval = synaptics_rmi4_i2c_read(rmi4_data,
2579 rmi4_data->f01_query_base_addr,
2580 f01_query,
2581 sizeof(f01_query));
2582 if (retval < 0)
2583 return retval;
2584
2585 /* RMI Version 4.0 currently supported */
2586 rmi->version_major = 4;
2587 rmi->version_minor = 0;
2588
2589 rmi->manufacturer_id = f01_query[0];
2590 rmi->product_props = f01_query[1];
2591 rmi->product_info[0] = f01_query[2] & MASK_7BIT;
2592 rmi->product_info[1] = f01_query[3] & MASK_7BIT;
2593 rmi->date_code[0] = f01_query[4] & MASK_5BIT;
2594 rmi->date_code[1] = f01_query[5] & MASK_4BIT;
2595 rmi->date_code[2] = f01_query[6] & MASK_5BIT;
2596 rmi->tester_id = ((f01_query[7] & MASK_7BIT) << 8) |
2597 (f01_query[8] & MASK_7BIT);
2598 rmi->serial_number = ((f01_query[9] & MASK_7BIT) << 8) |
2599 (f01_query[10] & MASK_7BIT);
2600 memcpy(rmi->product_id_string, &f01_query[11], 10);
2601
2602 if (rmi->manufacturer_id != 1) {
2603 dev_err(&rmi4_data->i2c_client->dev,
2604 "%s: Non-Synaptics device found, manufacturer ID = %d\n",
2605 __func__, rmi->manufacturer_id);
2606 }
2607
2608 retval = synaptics_rmi4_i2c_read(rmi4_data,
2609 rmi4_data->f01_query_base_addr + F01_PACKAGE_ID_OFFSET,
2610 pkg_id,
2611 sizeof(pkg_id));
2612 if (retval < 0) {
2613 dev_err(&rmi4_data->i2c_client->dev,
2614 "%s: Failed to read device package id (code %d)\n",
2615 __func__, retval);
2616 return retval;
2617 }
2618
2619 rmi->package_id = (pkg_id[1] << 8) | pkg_id[0];
2620 rmi->package_id_rev = (pkg_id[3] << 8) | pkg_id[2];
2621
2622 retval = synaptics_rmi4_i2c_read(rmi4_data,
2623 rmi4_data->f01_query_base_addr + F01_BUID_ID_OFFSET,
2624 rmi->build_id,
2625 sizeof(rmi->build_id));
2626 if (retval < 0) {
2627 dev_err(&rmi4_data->i2c_client->dev,
2628 "%s: Failed to read firmware build id (code %d)\n",
2629 __func__, retval);
2630 return retval;
2631 }
2632 return 0;
2633}
2634
2635/*
2636* This function checks whether the fhandler already existis in the
2637* support_fn_list or not.
2638* If it exists then return 1 as found or return 0 as not found.
2639*
2640* Called by synaptics_rmi4_query_device().
2641*/
2642static int synaptics_rmi4_check_fn_list(struct synaptics_rmi4_data *rmi4_data,
2643 struct synaptics_rmi4_fn *fhandler)
2644{
2645 int found = 0;
2646 struct synaptics_rmi4_fn *new_fhandler;
2647 struct synaptics_rmi4_device_info *rmi;
2648
2649 rmi = &(rmi4_data->rmi4_mod_info);
2650
2651 mutex_lock(&rmi->support_fn_list_mutex);
2652 if (!list_empty(&rmi->support_fn_list))
2653 list_for_each_entry(new_fhandler, &rmi->support_fn_list, link)
2654 if (new_fhandler->fn_number == fhandler->fn_number)
2655 found = 1;
2656 mutex_unlock(&rmi->support_fn_list_mutex);
2657
2658 return found;
2659}
2660
2661 /**
2662 * synaptics_rmi4_query_device()
2663 *
2664 * Called by synaptics_rmi4_probe().
2665 *
2666 * This funtion scans the page description table, records the offsets
2667 * to the register types of Function $01, sets up the function handlers
2668 * for Function $11 and Function $12, determines the number of interrupt
2669 * sources from the sensor, adds valid Functions with data inputs to the
2670 * Function linked list, parses information from the query registers of
2671 * Function $01, and enables the interrupt sources from the valid Functions
2672 * with data inputs.
2673 */
2674static int synaptics_rmi4_query_device(struct synaptics_rmi4_data *rmi4_data)
2675{
2676 int retval, found;
2677 unsigned char ii;
2678 unsigned char page_number;
2679 unsigned char intr_count = 0;
2680 unsigned char data_sources = 0;
2681 unsigned short pdt_entry_addr;
2682 unsigned short intr_addr;
2683 struct synaptics_rmi4_f01_device_status status;
2684 struct synaptics_rmi4_fn_desc rmi_fd;
2685 struct synaptics_rmi4_fn *fhandler;
2686 struct synaptics_rmi4_device_info *rmi;
2687
2688 rmi = &(rmi4_data->rmi4_mod_info);
2689
2690 /* Scan the page description tables of the pages to service */
2691 for (page_number = 0; page_number < PAGES_TO_SERVICE; page_number++) {
2692 for (pdt_entry_addr = PDT_START; pdt_entry_addr > PDT_END;
2693 pdt_entry_addr -= PDT_ENTRY_SIZE) {
2694 pdt_entry_addr |= (page_number << 8);
2695
2696 retval = synaptics_rmi4_i2c_read(rmi4_data,
2697 pdt_entry_addr,
2698 (unsigned char *)&rmi_fd,
2699 sizeof(rmi_fd));
2700 if (retval < 0)
2701 return retval;
2702
2703 fhandler = NULL;
2704 found = 0;
2705 if (rmi_fd.fn_number == 0) {
2706 dev_dbg(&rmi4_data->i2c_client->dev,
2707 "%s: Reached end of PDT\n",
2708 __func__);
2709 break;
2710 }
2711
2712 dev_dbg(&rmi4_data->i2c_client->dev,
2713 "%s: F%02x found (page %d)\n",
2714 __func__, rmi_fd.fn_number,
2715 page_number);
2716
2717 switch (rmi_fd.fn_number) {
2718 case SYNAPTICS_RMI4_F01:
2719 rmi4_data->f01_query_base_addr =
2720 rmi_fd.query_base_addr;
2721 rmi4_data->f01_ctrl_base_addr =
2722 rmi_fd.ctrl_base_addr;
2723 rmi4_data->f01_data_base_addr =
2724 rmi_fd.data_base_addr;
2725 rmi4_data->f01_cmd_base_addr =
2726 rmi_fd.cmd_base_addr;
2727
2728 retval =
2729 synaptics_rmi4_query_device_info(rmi4_data);
2730 if (retval < 0)
2731 return retval;
2732
2733 retval = synaptics_rmi4_i2c_read(rmi4_data,
2734 rmi4_data->f01_data_base_addr,
2735 status.data,
2736 sizeof(status.data));
2737 if (retval < 0)
2738 return retval;
2739
2740 while (status.status_code == STATUS_CRC_IN_PROGRESS) {
2741 msleep(1);
2742 retval = synaptics_rmi4_i2c_read(rmi4_data,
2743 rmi4_data->f01_data_base_addr,
2744 status.data,
2745 sizeof(status.data));
2746 if (retval < 0)
2747 return retval;
2748 }
2749
2750 if (status.flash_prog == 1) {
2751 pr_notice("%s: In flash prog mode, status = 0x%02x\n",
2752 __func__,
2753 status.status_code);
2754 goto flash_prog_mode;
2755 }
2756 break;
2757
2758 case SYNAPTICS_RMI4_F11:
2759 if (rmi_fd.intr_src_count == 0)
2760 break;
2761
2762 retval = synaptics_rmi4_alloc_fh(&fhandler,
2763 &rmi_fd, page_number);
2764 if (retval < 0) {
2765 dev_err(&rmi4_data->i2c_client->dev,
2766 "%s: Failed to alloc for F%d\n",
2767 __func__,
2768 rmi_fd.fn_number);
2769 return retval;
2770 }
2771
2772 retval = synaptics_rmi4_f11_init(rmi4_data,
2773 fhandler, &rmi_fd, intr_count);
2774 if (retval < 0)
2775 return retval;
2776 break;
2777
2778 case SYNAPTICS_RMI4_F12:
2779 if (rmi_fd.intr_src_count == 0)
2780 break;
2781
2782 retval = synaptics_rmi4_alloc_fh(&fhandler,
2783 &rmi_fd, page_number);
2784 if (retval < 0) {
2785 dev_err(&rmi4_data->i2c_client->dev,
2786 "%s: Failed to alloc for F%d\n",
2787 __func__,
2788 rmi_fd.fn_number);
2789 return retval;
2790 }
2791
2792 retval = synaptics_rmi4_f12_init(rmi4_data,
2793 fhandler, &rmi_fd, intr_count);
2794 if (retval < 0)
2795 return retval;
2796 break;
2797
2798 case SYNAPTICS_RMI4_F1A:
2799 if (rmi_fd.intr_src_count == 0)
2800 break;
2801
2802 retval = synaptics_rmi4_alloc_fh(&fhandler,
2803 &rmi_fd, page_number);
2804 if (retval < 0) {
2805 dev_err(&rmi4_data->i2c_client->dev,
2806 "%s: Failed to alloc for F%d\n",
2807 __func__,
2808 rmi_fd.fn_number);
2809 return retval;
2810 }
2811
2812 retval = synaptics_rmi4_f1a_init(rmi4_data,
2813 fhandler, &rmi_fd, intr_count);
2814 if (retval < 0)
2815 return retval;
2816 break;
2817 }
2818
2819 /* Accumulate the interrupt count */
2820 intr_count += (rmi_fd.intr_src_count & MASK_3BIT);
2821
2822 if (fhandler && rmi_fd.intr_src_count) {
2823 /* Want to check whether the fhandler already
2824 exists in the support_fn_list or not.
2825 If not found then add it to the list, otherwise
2826 free the memory allocated to it.
2827 */
2828 found = synaptics_rmi4_check_fn_list(rmi4_data,
2829 fhandler);
2830
2831 if (!found) {
2832 mutex_lock(&rmi->support_fn_list_mutex);
2833 list_add_tail(&fhandler->link,
2834 &rmi->support_fn_list);
2835 mutex_unlock(
2836 &rmi->support_fn_list_mutex);
2837 } else {
2838 if (fhandler->fn_number ==
2839 SYNAPTICS_RMI4_F1A) {
2840 synaptics_rmi4_f1a_kfree(
2841 fhandler);
2842 } else {
2843 kfree(fhandler->data);
2844 kfree(fhandler->extra);
2845 }
2846 kfree(fhandler);
2847 }
2848 }
2849 }
2850 }
2851
2852flash_prog_mode:
2853 rmi4_data->num_of_intr_regs = (intr_count + 7) / 8;
2854 dev_dbg(&rmi4_data->i2c_client->dev,
2855 "%s: Number of interrupt registers = %d\n",
2856 __func__, rmi4_data->num_of_intr_regs);
2857
2858 memset(rmi4_data->intr_mask, 0x00, sizeof(rmi4_data->intr_mask));
2859
2860 /*
2861 * Map out the interrupt bit masks for the interrupt sources
2862 * from the registered function handlers.
2863 */
2864 mutex_lock(&rmi->support_fn_list_mutex);
2865 if (!list_empty(&rmi->support_fn_list)) {
2866 list_for_each_entry(fhandler, &rmi->support_fn_list, link)
2867 data_sources += fhandler->num_of_data_sources;
2868 }
2869 mutex_unlock(&rmi->support_fn_list_mutex);
2870
2871 if (data_sources) {
2872 mutex_lock(&rmi->support_fn_list_mutex);
2873 if (!list_empty(&rmi->support_fn_list)) {
2874 list_for_each_entry(fhandler,
2875 &rmi->support_fn_list, link) {
2876 if (fhandler->num_of_data_sources) {
2877 rmi4_data->intr_mask[fhandler->intr_reg_num] |=
2878 fhandler->intr_mask;
2879 }
2880 }
2881 }
2882 mutex_unlock(&rmi->support_fn_list_mutex);
2883 }
2884
2885 /* Enable the interrupt sources */
2886 for (ii = 0; ii < rmi4_data->num_of_intr_regs; ii++) {
2887 if (rmi4_data->intr_mask[ii] != 0x00) {
2888 dev_dbg(&rmi4_data->i2c_client->dev,
2889 "%s: Interrupt enable mask %d = 0x%02x\n",
2890 __func__, ii, rmi4_data->intr_mask[ii]);
2891 intr_addr = rmi4_data->f01_ctrl_base_addr + 1 + ii;
2892 retval = synaptics_rmi4_i2c_write(rmi4_data,
2893 intr_addr,
2894 &(rmi4_data->intr_mask[ii]),
2895 sizeof(rmi4_data->intr_mask[ii]));
2896 if (retval < 0)
2897 return retval;
2898 }
2899 }
2900
2901 return 0;
2902}
2903
2904static int synaptics_rmi4_reset_command(struct synaptics_rmi4_data *rmi4_data)
2905{
2906 int retval;
2907 int page_number;
2908 unsigned char command = 0x01;
2909 unsigned short pdt_entry_addr;
2910 struct synaptics_rmi4_fn_desc rmi_fd;
2911 bool done = false;
2912
2913 /* Scan the page description tables of the pages to service */
2914 for (page_number = 0; page_number < PAGES_TO_SERVICE; page_number++) {
2915 for (pdt_entry_addr = PDT_START; pdt_entry_addr > PDT_END;
2916 pdt_entry_addr -= PDT_ENTRY_SIZE) {
2917 retval = synaptics_rmi4_i2c_read(rmi4_data,
2918 pdt_entry_addr,
2919 (unsigned char *)&rmi_fd,
2920 sizeof(rmi_fd));
2921 if (retval < 0)
2922 return retval;
2923
2924 if (rmi_fd.fn_number == 0)
2925 break;
2926
2927 switch (rmi_fd.fn_number) {
2928 case SYNAPTICS_RMI4_F01:
2929 rmi4_data->f01_cmd_base_addr =
2930 rmi_fd.cmd_base_addr;
2931 done = true;
2932 break;
2933 }
2934 }
2935 if (done) {
2936 dev_info(&rmi4_data->i2c_client->dev,
2937 "%s: Find F01 in page description table 0x%x\n",
2938 __func__, rmi4_data->f01_cmd_base_addr);
2939 break;
2940 }
2941 }
2942
2943 if (!done) {
2944 dev_err(&rmi4_data->i2c_client->dev,
2945 "%s: Cannot find F01 in page description table\n",
2946 __func__);
2947 return -EINVAL;
2948 }
2949
2950 retval = synaptics_rmi4_i2c_write(rmi4_data,
2951 rmi4_data->f01_cmd_base_addr,
2952 &command,
2953 sizeof(command));
2954 if (retval < 0) {
2955 dev_err(&rmi4_data->i2c_client->dev,
2956 "%s: Failed to issue reset command, error = %d\n",
2957 __func__, retval);
2958 return retval;
2959 }
2960
2961 msleep(rmi4_data->board->reset_delay);
2962 return retval;
2963};
2964
2965static int synaptics_rmi4_reset_device(struct synaptics_rmi4_data *rmi4_data)
2966{
2967 int retval;
2968 struct synaptics_rmi4_fn *fhandler;
2969 struct synaptics_rmi4_fn *next_fhandler;
2970 struct synaptics_rmi4_device_info *rmi;
2971
2972 rmi = &(rmi4_data->rmi4_mod_info);
2973
2974 retval = synaptics_rmi4_reset_command(rmi4_data);
2975 if (retval < 0) {
2976 dev_err(&rmi4_data->i2c_client->dev,
2977 "%s: Failed to send command reset\n",
2978 __func__);
2979 return retval;
2980 }
2981
2982 if (!list_empty(&rmi->support_fn_list)) {
2983 list_for_each_entry_safe(fhandler, next_fhandler,
2984 &rmi->support_fn_list, link) {
2985 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
2986 synaptics_rmi4_f1a_kfree(fhandler);
2987 else {
2988 kfree(fhandler->data);
2989 kfree(fhandler->extra);
2990 }
2991 kfree(fhandler);
2992 }
2993 }
2994
2995 INIT_LIST_HEAD(&rmi->support_fn_list);
2996
2997 retval = synaptics_rmi4_query_device(rmi4_data);
2998 if (retval < 0) {
2999 dev_err(&rmi4_data->i2c_client->dev,
3000 "%s: Failed to query device\n",
3001 __func__);
3002 return retval;
3003 }
3004
3005 return 0;
3006}
3007
3008/**
3009* synaptics_rmi4_detection_work()
3010*
3011* Called by the kernel at the scheduled time.
3012*
3013* This function is a self-rearming work thread that checks for the
3014* insertion and removal of other expansion Function modules such as
3015* rmi_dev and calls their initialization and removal callback functions
3016* accordingly.
3017*/
3018static void synaptics_rmi4_detection_work(struct work_struct *work)
3019{
3020 struct synaptics_rmi4_exp_fn *exp_fhandler, *next_list_entry;
3021 struct synaptics_rmi4_data *rmi4_data =
3022 container_of(work, struct synaptics_rmi4_data,
3023 det_work.work);
3024
3025 mutex_lock(&exp_fn_list_mutex);
3026 if (!list_empty(&exp_fn_list)) {
3027 list_for_each_entry_safe(exp_fhandler,
3028 next_list_entry,
3029 &exp_fn_list,
3030 link) {
3031 if ((exp_fhandler->func_init != NULL) &&
3032 (exp_fhandler->inserted == false)) {
3033 if (exp_fhandler->func_init(rmi4_data) < 0) {
3034 list_del(&exp_fhandler->link);
3035 kfree(exp_fhandler);
3036 } else {
3037 exp_fhandler->inserted = true;
3038 }
3039 } else if ((exp_fhandler->func_init == NULL) &&
3040 (exp_fhandler->inserted == true)) {
3041 exp_fhandler->func_remove(rmi4_data);
3042 list_del(&exp_fhandler->link);
3043 kfree(exp_fhandler);
3044 }
3045 }
3046 }
3047 mutex_unlock(&exp_fn_list_mutex);
3048
3049 return;
3050}
3051
3052/**
3053* synaptics_rmi4_new_function()
3054*
3055* Called by other expansion Function modules in their module init and
3056* module exit functions.
3057*
3058* This function is used by other expansion Function modules such as
3059* rmi_dev to register themselves with the driver by providing their
3060* initialization and removal callback function pointers so that they
3061* can be inserted or removed dynamically at module init and exit times,
3062* respectively.
3063*/
3064void synaptics_rmi4_new_function(enum exp_fn fn_type, bool insert,
3065 int (*func_init)(struct synaptics_rmi4_data *rmi4_data),
3066 void (*func_remove)(struct synaptics_rmi4_data *rmi4_data),
3067 void (*func_attn)(struct synaptics_rmi4_data *rmi4_data,
3068 unsigned char intr_mask))
3069{
3070 struct synaptics_rmi4_exp_fn *exp_fhandler;
3071
3072 if (!exp_fn_inited) {
3073 mutex_init(&exp_fn_list_mutex);
3074 INIT_LIST_HEAD(&exp_fn_list);
3075 exp_fn_inited = 1;
3076 }
3077
3078 mutex_lock(&exp_fn_list_mutex);
3079 if (insert) {
3080 exp_fhandler = kzalloc(sizeof(*exp_fhandler), GFP_KERNEL);
3081 if (!exp_fhandler) {
3082 pr_err("%s: Failed to alloc mem for expansion function\n",
3083 __func__);
3084 goto exit;
3085 }
3086 exp_fhandler->fn_type = fn_type;
3087 exp_fhandler->func_init = func_init;
3088 exp_fhandler->func_attn = func_attn;
3089 exp_fhandler->func_remove = func_remove;
3090 exp_fhandler->inserted = false;
3091 list_add_tail(&exp_fhandler->link, &exp_fn_list);
3092 } else {
3093 if (!list_empty(&exp_fn_list)) {
3094 list_for_each_entry(exp_fhandler, &exp_fn_list, link) {
3095 if (exp_fhandler->func_init == func_init) {
3096 exp_fhandler->inserted = false;
3097 exp_fhandler->func_init = NULL;
3098 exp_fhandler->func_attn = NULL;
3099 goto exit;
3100 }
3101 }
3102 }
3103 }
3104
3105exit:
3106 mutex_unlock(&exp_fn_list_mutex);
3107
3108 return;
3109}
3110EXPORT_SYMBOL(synaptics_rmi4_new_function);
3111
3112
3113static int reg_set_optimum_mode_check(struct regulator *reg, int load_uA)
3114{
3115 return (regulator_count_voltages(reg) > 0) ?
3116 regulator_set_optimum_mode(reg, load_uA) : 0;
3117}
3118
3119static int synaptics_rmi4_regulator_configure(struct synaptics_rmi4_data
3120 *rmi4_data, bool on)
3121{
3122 int retval;
3123
3124 if (on == false)
3125 goto hw_shutdown;
3126
3127 rmi4_data->vdd = regulator_get(&rmi4_data->i2c_client->dev,
3128 "vdd");
3129 if (IS_ERR(rmi4_data->vdd)) {
3130 dev_err(&rmi4_data->i2c_client->dev,
3131 "%s: Failed to get vdd regulator\n",
3132 __func__);
3133 return PTR_ERR(rmi4_data->vdd);
3134 }
3135
3136 if (regulator_count_voltages(rmi4_data->vdd) > 0) {
3137 retval = regulator_set_voltage(rmi4_data->vdd,
3138 RMI4_VTG_MIN_UV, RMI4_VTG_MAX_UV);
3139 if (retval) {
3140 dev_err(&rmi4_data->i2c_client->dev,
3141 "regulator set_vtg failed retval =%d\n",
3142 retval);
3143 goto err_set_vtg_vdd;
3144 }
3145 }
3146
3147 if (rmi4_data->board->i2c_pull_up) {
3148 rmi4_data->vcc_i2c = regulator_get(&rmi4_data->i2c_client->dev,
3149 "vcc_i2c");
3150 if (IS_ERR(rmi4_data->vcc_i2c)) {
3151 dev_err(&rmi4_data->i2c_client->dev,
3152 "%s: Failed to get i2c regulator\n",
3153 __func__);
3154 retval = PTR_ERR(rmi4_data->vcc_i2c);
3155 goto err_get_vtg_i2c;
3156 }
3157
3158 if (regulator_count_voltages(rmi4_data->vcc_i2c) > 0) {
3159 retval = regulator_set_voltage(rmi4_data->vcc_i2c,
3160 RMI4_I2C_VTG_MIN_UV, RMI4_I2C_VTG_MAX_UV);
3161 if (retval) {
3162 dev_err(&rmi4_data->i2c_client->dev,
3163 "reg set i2c vtg failed retval =%d\n",
3164 retval);
3165 goto err_set_vtg_i2c;
3166 }
3167 }
3168 }
3169 return 0;
3170
3171err_set_vtg_i2c:
3172 if (rmi4_data->board->i2c_pull_up)
3173 regulator_put(rmi4_data->vcc_i2c);
3174err_get_vtg_i2c:
3175 if (regulator_count_voltages(rmi4_data->vdd) > 0)
3176 regulator_set_voltage(rmi4_data->vdd, 0,
3177 RMI4_VTG_MAX_UV);
3178err_set_vtg_vdd:
3179 regulator_put(rmi4_data->vdd);
3180 return retval;
3181
3182hw_shutdown:
3183 if (regulator_count_voltages(rmi4_data->vdd) > 0)
3184 regulator_set_voltage(rmi4_data->vdd, 0,
3185 RMI4_VTG_MAX_UV);
3186 regulator_put(rmi4_data->vdd);
3187 if (rmi4_data->board->i2c_pull_up) {
3188 if (regulator_count_voltages(rmi4_data->vcc_i2c) > 0)
3189 regulator_set_voltage(rmi4_data->vcc_i2c, 0,
3190 RMI4_I2C_VTG_MAX_UV);
3191 regulator_put(rmi4_data->vcc_i2c);
3192 }
3193 return 0;
3194};
3195
3196static int synaptics_rmi4_power_on(struct synaptics_rmi4_data *rmi4_data,
3197 bool on) {
3198 int retval;
3199
3200 if (on == false)
3201 goto power_off;
3202
3203 retval = reg_set_optimum_mode_check(rmi4_data->vdd,
3204 RMI4_ACTIVE_LOAD_UA);
3205 if (retval < 0) {
3206 dev_err(&rmi4_data->i2c_client->dev,
3207 "Regulator vdd set_opt failed rc=%d\n",
3208 retval);
3209 return retval;
3210 }
3211
3212 retval = regulator_enable(rmi4_data->vdd);
3213 if (retval) {
3214 dev_err(&rmi4_data->i2c_client->dev,
3215 "Regulator vdd enable failed rc=%d\n",
3216 retval);
3217 goto error_reg_en_vdd;
3218 }
3219
3220 if (rmi4_data->board->i2c_pull_up) {
3221 retval = reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
3222 RMI4_I2C_LOAD_UA);
3223 if (retval < 0) {
3224 dev_err(&rmi4_data->i2c_client->dev,
3225 "Regulator vcc_i2c set_opt failed rc=%d\n",
3226 retval);
3227 goto error_reg_opt_i2c;
3228 }
3229
3230 retval = regulator_enable(rmi4_data->vcc_i2c);
3231 if (retval) {
3232 dev_err(&rmi4_data->i2c_client->dev,
3233 "Regulator vcc_i2c enable failed rc=%d\n",
3234 retval);
3235 goto error_reg_en_vcc_i2c;
3236 }
3237 }
3238 return 0;
3239
3240error_reg_en_vcc_i2c:
3241 if (rmi4_data->board->i2c_pull_up)
3242 reg_set_optimum_mode_check(rmi4_data->vcc_i2c, 0);
3243error_reg_opt_i2c:
3244 regulator_disable(rmi4_data->vdd);
3245error_reg_en_vdd:
3246 reg_set_optimum_mode_check(rmi4_data->vdd, 0);
3247 return retval;
3248
3249power_off:
3250 reg_set_optimum_mode_check(rmi4_data->vdd, 0);
3251 regulator_disable(rmi4_data->vdd);
3252 if (rmi4_data->board->i2c_pull_up) {
3253 reg_set_optimum_mode_check(rmi4_data->vcc_i2c, 0);
3254 regulator_disable(rmi4_data->vcc_i2c);
3255 }
3256 return 0;
3257}
3258
3259static int synaptics_rmi4_pinctrl_init(struct synaptics_rmi4_data *rmi4_data)
3260{
3261 int retval;
3262
3263 /* Get pinctrl if target uses pinctrl */
3264 rmi4_data->ts_pinctrl = devm_pinctrl_get(&(rmi4_data->i2c_client->dev));
3265 if (IS_ERR_OR_NULL(rmi4_data->ts_pinctrl)) {
3266 retval = PTR_ERR(rmi4_data->ts_pinctrl);
3267 dev_dbg(&rmi4_data->i2c_client->dev,
3268 "Target does not use pinctrl %d\n", retval);
3269 goto err_pinctrl_get;
3270 }
3271
3272 rmi4_data->pinctrl_state_active
3273 = pinctrl_lookup_state(rmi4_data->ts_pinctrl,
3274 PINCTRL_STATE_ACTIVE);
3275 if (IS_ERR_OR_NULL(rmi4_data->pinctrl_state_active)) {
3276 retval = PTR_ERR(rmi4_data->pinctrl_state_active);
3277 dev_err(&rmi4_data->i2c_client->dev,
3278 "Can not lookup %s pinstate %d\n",
3279 PINCTRL_STATE_ACTIVE, retval);
3280 goto err_pinctrl_lookup;
3281 }
3282
3283 rmi4_data->pinctrl_state_suspend
3284 = pinctrl_lookup_state(rmi4_data->ts_pinctrl,
3285 PINCTRL_STATE_SUSPEND);
3286 if (IS_ERR_OR_NULL(rmi4_data->pinctrl_state_suspend)) {
3287 retval = PTR_ERR(rmi4_data->pinctrl_state_suspend);
3288 dev_err(&rmi4_data->i2c_client->dev,
3289 "Can not lookup %s pinstate %d\n",
3290 PINCTRL_STATE_SUSPEND, retval);
3291 goto err_pinctrl_lookup;
3292 }
3293
3294 rmi4_data->pinctrl_state_release
3295 = pinctrl_lookup_state(rmi4_data->ts_pinctrl,
3296 PINCTRL_STATE_RELEASE);
3297 if (IS_ERR_OR_NULL(rmi4_data->pinctrl_state_release)) {
3298 retval = PTR_ERR(rmi4_data->pinctrl_state_release);
3299 dev_dbg(&rmi4_data->i2c_client->dev,
3300 "Can not lookup %s pinstate %d\n",
3301 PINCTRL_STATE_RELEASE, retval);
3302 }
3303
3304 return 0;
3305
3306err_pinctrl_lookup:
3307 devm_pinctrl_put(rmi4_data->ts_pinctrl);
3308err_pinctrl_get:
3309 rmi4_data->ts_pinctrl = NULL;
3310 return retval;
3311}
3312
3313static int synaptics_rmi4_gpio_configure(struct synaptics_rmi4_data *rmi4_data,
3314 bool on)
3315{
3316 int retval = 0;
3317
3318 if (on) {
3319 if (gpio_is_valid(rmi4_data->board->irq_gpio)) {
3320 /* configure touchscreen irq gpio */
3321 retval = gpio_request(rmi4_data->board->irq_gpio,
3322 "rmi4_irq_gpio");
3323 if (retval) {
3324 dev_err(&rmi4_data->i2c_client->dev,
3325 "unable to request gpio [%d]\n",
3326 rmi4_data->board->irq_gpio);
3327 goto err_irq_gpio_req;
3328 }
3329 retval = gpio_direction_input(rmi4_data->board->\
3330 irq_gpio);
3331 if (retval) {
3332 dev_err(&rmi4_data->i2c_client->dev,
3333 "unable to set direction for gpio " \
3334 "[%d]\n", rmi4_data->board->irq_gpio);
3335 goto err_irq_gpio_dir;
3336 }
3337 } else {
3338 dev_err(&rmi4_data->i2c_client->dev,
3339 "irq gpio not provided\n");
3340 goto err_irq_gpio_req;
3341 }
3342
3343 if (gpio_is_valid(rmi4_data->board->reset_gpio)) {
3344 /* configure touchscreen reset out gpio */
3345 retval = gpio_request(rmi4_data->board->reset_gpio,
3346 "rmi4_reset_gpio");
3347 if (retval) {
3348 dev_err(&rmi4_data->i2c_client->dev,
3349 "unable to request gpio [%d]\n",
3350 rmi4_data->board->reset_gpio);
3351 goto err_irq_gpio_dir;
3352 }
3353
3354 retval = gpio_direction_output(rmi4_data->board->\
3355 reset_gpio, 1);
3356 if (retval) {
3357 dev_err(&rmi4_data->i2c_client->dev,
3358 "unable to set direction for gpio " \
3359 "[%d]\n", rmi4_data->board->reset_gpio);
3360 goto err_reset_gpio_dir;
3361 }
3362
3363 gpio_set_value(rmi4_data->board->reset_gpio, 1);
3364 msleep(rmi4_data->board->reset_delay);
3365 } else
3366 synaptics_rmi4_reset_command(rmi4_data);
3367
3368 return 0;
3369 } else {
3370 if (rmi4_data->board->disable_gpios) {
3371 if (gpio_is_valid(rmi4_data->board->irq_gpio))
3372 gpio_free(rmi4_data->board->irq_gpio);
3373 if (gpio_is_valid(rmi4_data->board->reset_gpio)) {
3374 /*
3375 * This is intended to save leakage current
3376 * only. Even if the call(gpio_direction_input)
3377 * fails, only leakage current will be more but
3378 * functionality will not be affected.
3379 */
3380 retval = gpio_direction_input(rmi4_data->
3381 board->reset_gpio);
3382 if (retval) {
3383 dev_err(&rmi4_data->i2c_client->dev,
3384 "unable to set direction for gpio "
3385 "[%d]\n", rmi4_data->board->irq_gpio);
3386 }
3387 gpio_free(rmi4_data->board->reset_gpio);
3388 }
3389 }
3390
3391 return 0;
3392 }
3393
3394err_reset_gpio_dir:
3395 if (gpio_is_valid(rmi4_data->board->reset_gpio))
3396 gpio_free(rmi4_data->board->reset_gpio);
3397err_irq_gpio_dir:
3398 if (gpio_is_valid(rmi4_data->board->irq_gpio))
3399 gpio_free(rmi4_data->board->irq_gpio);
3400err_irq_gpio_req:
3401 return retval;
3402}
3403
3404 /**
3405 * synaptics_rmi4_probe()
3406 *
3407 * Called by the kernel when an association with an I2C device of the
3408 * same name is made (after doing i2c_add_driver).
3409 *
3410 * This funtion allocates and initializes the resources for the driver
3411 * as an input driver, turns on the power to the sensor, queries the
3412 * sensor for its supported Functions and characteristics, registers
3413 * the driver to the input subsystem, sets up the interrupt, handles
3414 * the registration of the early_suspend and late_resume functions,
3415 * and creates a work queue for detection of other expansion Function
3416 * modules.
3417 */
3418static int synaptics_rmi4_probe(struct i2c_client *client,
3419 const struct i2c_device_id *dev_id)
3420{
3421 int retval = 0;
3422 unsigned char ii;
3423 unsigned char attr_count;
3424 struct synaptics_rmi4_f1a_handle *f1a;
3425 struct synaptics_rmi4_fn *fhandler;
3426 struct synaptics_rmi4_fn *next_fhandler;
3427 struct synaptics_rmi4_data *rmi4_data;
3428 struct synaptics_rmi4_device_info *rmi;
3429 struct synaptics_rmi4_platform_data *platform_data =
3430 client->dev.platform_data;
3431 struct dentry *temp;
3432
3433 if (!i2c_check_functionality(client->adapter,
3434 I2C_FUNC_SMBUS_BYTE_DATA)) {
3435 dev_err(&client->dev,
3436 "%s: SMBus byte data not supported\n",
3437 __func__);
3438 return -EIO;
3439 }
3440
3441 if (client->dev.of_node) {
3442 platform_data = devm_kzalloc(&client->dev,
3443 sizeof(*platform_data),
3444 GFP_KERNEL);
3445 if (!platform_data) {
3446 dev_err(&client->dev, "Failed to allocate memory\n");
3447 return -ENOMEM;
3448 }
3449
3450 retval = synaptics_rmi4_parse_dt(&client->dev, platform_data);
3451 if (retval)
3452 return retval;
3453 } else {
3454 platform_data = client->dev.platform_data;
3455 }
3456
3457 if (!platform_data) {
3458 dev_err(&client->dev,
3459 "%s: No platform data found\n",
3460 __func__);
3461 return -EINVAL;
3462 }
3463
3464 rmi4_data = kzalloc(sizeof(*rmi4_data) * 2, GFP_KERNEL);
3465 if (!rmi4_data) {
3466 dev_err(&client->dev,
3467 "%s: Failed to alloc mem for rmi4_data\n",
3468 __func__);
3469 return -ENOMEM;
3470 }
3471
3472 rmi = &(rmi4_data->rmi4_mod_info);
3473
3474 rmi4_data->input_dev = input_allocate_device();
3475 if (rmi4_data->input_dev == NULL) {
3476 dev_err(&client->dev,
3477 "%s: Failed to allocate input device\n",
3478 __func__);
3479 retval = -ENOMEM;
3480 goto err_input_device;
3481 }
3482
3483 rmi4_data->i2c_client = client;
3484 rmi4_data->current_page = MASK_8BIT;
3485 rmi4_data->board = platform_data;
3486 rmi4_data->touch_stopped = false;
3487 rmi4_data->sensor_sleep = false;
3488 rmi4_data->irq_enabled = false;
3489 rmi4_data->fw_updating = false;
3490 rmi4_data->suspended = false;
3491
3492 rmi4_data->i2c_read = synaptics_rmi4_i2c_read;
3493 rmi4_data->i2c_write = synaptics_rmi4_i2c_write;
3494 rmi4_data->irq_enable = synaptics_rmi4_irq_enable;
3495 rmi4_data->reset_device = synaptics_rmi4_reset_device;
3496
3497 rmi4_data->flip_x = rmi4_data->board->x_flip;
3498 rmi4_data->flip_y = rmi4_data->board->y_flip;
3499
3500 if (rmi4_data->board->fw_image_name)
3501 snprintf(rmi4_data->fw_image_name, NAME_BUFFER_SIZE, "%s",
3502 rmi4_data->board->fw_image_name);
3503
3504 rmi4_data->input_dev->name = DRIVER_NAME;
3505 rmi4_data->input_dev->phys = INPUT_PHYS_NAME;
3506 rmi4_data->input_dev->id.bustype = BUS_I2C;
3507 rmi4_data->input_dev->id.product = SYNAPTICS_DSX_DRIVER_PRODUCT;
3508 rmi4_data->input_dev->id.version = SYNAPTICS_DSX_DRIVER_VERSION;
3509 rmi4_data->input_dev->dev.parent = &client->dev;
3510 input_set_drvdata(rmi4_data->input_dev, rmi4_data);
3511
3512 set_bit(EV_SYN, rmi4_data->input_dev->evbit);
3513 set_bit(EV_KEY, rmi4_data->input_dev->evbit);
3514 set_bit(EV_ABS, rmi4_data->input_dev->evbit);
3515 set_bit(BTN_TOUCH, rmi4_data->input_dev->keybit);
3516 set_bit(BTN_TOOL_FINGER, rmi4_data->input_dev->keybit);
3517
3518#ifdef INPUT_PROP_DIRECT
3519 set_bit(INPUT_PROP_DIRECT, rmi4_data->input_dev->propbit);
3520#endif
3521
3522 retval = synaptics_rmi4_regulator_configure(rmi4_data, true);
3523 if (retval < 0) {
3524 dev_err(&client->dev, "Failed to configure regulators\n");
3525 goto err_reg_configure;
3526 }
3527
3528 retval = synaptics_rmi4_power_on(rmi4_data, true);
3529 if (retval < 0) {
3530 dev_err(&client->dev, "Failed to power on\n");
3531 goto err_power_device;
3532 }
3533
3534 retval = synaptics_rmi4_pinctrl_init(rmi4_data);
3535 if (!retval && rmi4_data->ts_pinctrl) {
3536 /*
3537 * Pinctrl handle is optional. If pinctrl handle is found
3538 * let pins to be configured in active state. If not found
3539 * continue further without error
3540 */
3541 if (pinctrl_select_state(rmi4_data->ts_pinctrl,
3542 rmi4_data->pinctrl_state_active))
3543 dev_err(&rmi4_data->i2c_client->dev,
3544 "Can not select %s pinstate\n",
3545 PINCTRL_STATE_ACTIVE);
3546 }
3547
3548 retval = synaptics_rmi4_gpio_configure(rmi4_data, true);
3549 if (retval < 0) {
3550 dev_err(&client->dev, "Failed to configure gpios\n");
3551 goto err_gpio_config;
3552 }
3553
3554 init_waitqueue_head(&rmi4_data->wait);
3555 mutex_init(&(rmi4_data->rmi4_io_ctrl_mutex));
3556
3557 INIT_LIST_HEAD(&rmi->support_fn_list);
3558 mutex_init(&rmi->support_fn_list_mutex);
3559
3560 retval = synaptics_rmi4_query_device(rmi4_data);
3561 if (retval < 0) {
3562 dev_err(&client->dev,
3563 "%s: Failed to query device\n",
3564 __func__);
3565 goto err_free_gpios;
3566 }
3567
3568 if (platform_data->detect_device) {
3569 retval = synaptics_rmi4_parse_dt_children(&client->dev,
3570 platform_data, rmi4_data);
3571 if (retval < 0)
3572 dev_err(&client->dev,
3573 "%s: Failed to parse device tree property\n",
3574 __func__);
3575 }
3576
3577 if (rmi4_data->board->disp_maxx)
3578 rmi4_data->disp_maxx = rmi4_data->board->disp_maxx;
3579 else
3580 rmi4_data->disp_maxx = rmi4_data->sensor_max_x;
3581
3582 if (rmi4_data->board->disp_maxy)
3583 rmi4_data->disp_maxy = rmi4_data->board->disp_maxy;
3584 else
3585 rmi4_data->disp_maxy = rmi4_data->sensor_max_y;
3586
3587 if (rmi4_data->board->disp_minx)
3588 rmi4_data->disp_minx = rmi4_data->board->disp_minx;
3589 else
3590 rmi4_data->disp_minx = 0;
3591
3592 if (rmi4_data->board->disp_miny)
3593 rmi4_data->disp_miny = rmi4_data->board->disp_miny;
3594 else
3595 rmi4_data->disp_miny = 0;
3596
3597 input_set_abs_params(rmi4_data->input_dev,
3598 ABS_MT_POSITION_X, rmi4_data->disp_minx,
3599 rmi4_data->disp_maxx, 0, 0);
3600 input_set_abs_params(rmi4_data->input_dev,
3601 ABS_MT_POSITION_Y, rmi4_data->disp_miny,
3602 rmi4_data->disp_maxy, 0, 0);
3603 input_set_abs_params(rmi4_data->input_dev,
3604 ABS_PRESSURE, 0, 255, 0, 0);
3605#ifdef REPORT_2D_W
3606 input_set_abs_params(rmi4_data->input_dev,
3607 ABS_MT_TOUCH_MAJOR, 0,
3608 rmi4_data->max_touch_width, 0, 0);
3609 input_set_abs_params(rmi4_data->input_dev,
3610 ABS_MT_TOUCH_MINOR, 0,
3611 rmi4_data->max_touch_width, 0, 0);
3612#endif
3613
3614#ifdef TYPE_B_PROTOCOL
3615 input_mt_init_slots(rmi4_data->input_dev,
3616 rmi4_data->num_of_fingers, 0);
3617#endif
3618
3619 i2c_set_clientdata(client, rmi4_data);
3620
3621 f1a = NULL;
3622 mutex_lock(&rmi->support_fn_list_mutex);
3623 if (!list_empty(&rmi->support_fn_list)) {
3624 list_for_each_entry(fhandler, &rmi->support_fn_list, link) {
3625 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
3626 f1a = fhandler->data;
3627 }
3628 }
3629 mutex_unlock(&rmi->support_fn_list_mutex);
3630
3631 if (f1a) {
3632 for (ii = 0; ii < f1a->valid_button_count; ii++) {
3633 set_bit(f1a->button_map[ii],
3634 rmi4_data->input_dev->keybit);
3635 input_set_capability(rmi4_data->input_dev,
3636 EV_KEY, f1a->button_map[ii]);
3637 }
3638 }
3639
3640 retval = input_register_device(rmi4_data->input_dev);
3641 if (retval) {
3642 dev_err(&client->dev,
3643 "%s: Failed to register input device\n",
3644 __func__);
3645 goto err_register_input;
3646 }
3647
3648 configure_sleep(rmi4_data);
3649
3650 if (!exp_fn_inited) {
3651 mutex_init(&exp_fn_list_mutex);
3652 INIT_LIST_HEAD(&exp_fn_list);
3653 exp_fn_inited = 1;
3654 }
3655
3656 rmi4_data->det_workqueue =
3657 create_singlethread_workqueue("rmi_det_workqueue");
3658 INIT_DELAYED_WORK(&rmi4_data->det_work,
3659 synaptics_rmi4_detection_work);
3660 queue_delayed_work(rmi4_data->det_workqueue,
3661 &rmi4_data->det_work,
3662 msecs_to_jiffies(EXP_FN_DET_INTERVAL));
3663
3664 rmi4_data->irq = gpio_to_irq(platform_data->irq_gpio);
3665
3666 retval = request_threaded_irq(rmi4_data->irq, NULL,
3667 synaptics_rmi4_irq, platform_data->irq_flags,
3668 DRIVER_NAME, rmi4_data);
3669 rmi4_data->irq_enabled = true;
3670
3671 if (retval < 0) {
3672 dev_err(&client->dev,
3673 "%s: Failed to create irq thread\n",
3674 __func__);
3675 goto err_enable_irq;
3676 }
3677
3678 rmi4_data->dir = debugfs_create_dir(DEBUGFS_DIR_NAME, NULL);
3679 if (rmi4_data->dir == NULL || IS_ERR(rmi4_data->dir)) {
3680 dev_err(&client->dev,
3681 "%s: Failed to create debugfs directory, rc = %ld\n",
3682 __func__, PTR_ERR(rmi4_data->dir));
3683 retval = PTR_ERR(rmi4_data->dir);
3684 goto err_create_debugfs_dir;
3685 }
3686
3687 temp = debugfs_create_file("suspend", S_IRUSR | S_IWUSR, rmi4_data->dir,
3688 rmi4_data, &debug_suspend_fops);
3689 if (temp == NULL || IS_ERR(temp)) {
3690 dev_err(&client->dev,
3691 "%s: Failed to create suspend debugfs file, rc = %ld\n",
3692 __func__, PTR_ERR(temp));
3693 retval = PTR_ERR(temp);
3694 goto err_create_debugfs_file;
3695 }
3696
3697 for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
3698 retval = sysfs_create_file(&client->dev.kobj,
3699 &attrs[attr_count].attr);
3700 if (retval < 0) {
3701 dev_err(&client->dev,
3702 "%s: Failed to create sysfs attributes\n",
3703 __func__);
3704 goto err_sysfs;
3705 }
3706 }
3707
3708 synaptics_rmi4_sensor_wake(rmi4_data);
3709
3710 retval = synaptics_rmi4_irq_enable(rmi4_data, true);
3711 if (retval < 0) {
3712 dev_err(&client->dev,
3713 "%s: Failed to enable attention interrupt\n",
3714 __func__);
3715 goto err_sysfs;
3716 }
3717
3718 synaptics_secure_touch_init(rmi4_data);
3719 synaptics_secure_touch_stop(rmi4_data, 1);
3720
3721 retval = synaptics_rmi4_check_configuration(rmi4_data);
3722 if (retval < 0) {
3723 dev_err(&client->dev, "Failed to check configuration\n");
3724 return retval;
3725 }
3726
3727 return retval;
3728
3729err_sysfs:
3730 for (attr_count--; attr_count >= 0; attr_count--) {
3731 sysfs_remove_file(&rmi4_data->input_dev->dev.kobj,
3732 &attrs[attr_count].attr);
3733 }
3734err_create_debugfs_file:
3735 debugfs_remove_recursive(rmi4_data->dir);
3736err_create_debugfs_dir:
3737 free_irq(rmi4_data->irq, rmi4_data);
3738err_enable_irq:
3739 cancel_delayed_work_sync(&rmi4_data->det_work);
3740 flush_workqueue(rmi4_data->det_workqueue);
3741 destroy_workqueue(rmi4_data->det_workqueue);
3742 input_unregister_device(rmi4_data->input_dev);
3743
3744err_register_input:
3745 mutex_lock(&rmi->support_fn_list_mutex);
3746 if (!list_empty(&rmi->support_fn_list)) {
3747 list_for_each_entry_safe(fhandler, next_fhandler,
3748 &rmi->support_fn_list, link) {
3749 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
3750 synaptics_rmi4_f1a_kfree(fhandler);
3751 else {
3752 kfree(fhandler->data);
3753 kfree(fhandler->extra);
3754 }
3755 kfree(fhandler);
3756 }
3757 }
3758 mutex_unlock(&rmi->support_fn_list_mutex);
3759err_free_gpios:
3760 if (gpio_is_valid(rmi4_data->board->reset_gpio))
3761 gpio_free(rmi4_data->board->reset_gpio);
3762 if (gpio_is_valid(rmi4_data->board->irq_gpio))
3763 gpio_free(rmi4_data->board->irq_gpio);
3764err_gpio_config:
3765 if (rmi4_data->ts_pinctrl) {
3766 if (IS_ERR_OR_NULL(rmi4_data->pinctrl_state_release)) {
3767 devm_pinctrl_put(rmi4_data->ts_pinctrl);
3768 rmi4_data->ts_pinctrl = NULL;
3769 } else {
3770 retval = pinctrl_select_state(rmi4_data->ts_pinctrl,
3771 rmi4_data->pinctrl_state_release);
3772 if (retval)
3773 pr_err("failed to select release pinctrl state\n");
3774 }
3775 }
3776 synaptics_rmi4_power_on(rmi4_data, false);
3777err_power_device:
3778 synaptics_rmi4_regulator_configure(rmi4_data, false);
3779err_reg_configure:
3780 input_free_device(rmi4_data->input_dev);
3781 rmi4_data->input_dev = NULL;
3782err_input_device:
3783 kfree(rmi4_data);
3784
3785 return retval;
3786}
3787
3788 /**
3789 * synaptics_rmi4_remove()
3790 *
3791 * Called by the kernel when the association with an I2C device of the
3792 * same name is broken (when the driver is unloaded).
3793 *
3794 * This funtion terminates the work queue, stops sensor data acquisition,
3795 * frees the interrupt, unregisters the driver from the input subsystem,
3796 * turns off the power to the sensor, and frees other allocated resources.
3797 */
3798static int synaptics_rmi4_remove(struct i2c_client *client)
3799{
3800 unsigned char attr_count;
3801 struct synaptics_rmi4_fn *fhandler;
3802 struct synaptics_rmi4_fn *next_fhandler;
3803 struct synaptics_rmi4_data *rmi4_data = i2c_get_clientdata(client);
3804 struct synaptics_rmi4_device_info *rmi;
3805 int retval;
3806
3807 rmi = &(rmi4_data->rmi4_mod_info);
3808
3809 debugfs_remove_recursive(rmi4_data->dir);
3810 cancel_delayed_work_sync(&rmi4_data->det_work);
3811 flush_workqueue(rmi4_data->det_workqueue);
3812 destroy_workqueue(rmi4_data->det_workqueue);
3813
3814 rmi4_data->touch_stopped = true;
3815 wake_up(&rmi4_data->wait);
3816
3817 free_irq(rmi4_data->irq, rmi4_data);
3818
3819 for (attr_count = 0; attr_count < ARRAY_SIZE(attrs); attr_count++) {
3820 sysfs_remove_file(&rmi4_data->input_dev->dev.kobj,
3821 &attrs[attr_count].attr);
3822 }
3823
3824 input_unregister_device(rmi4_data->input_dev);
3825
3826 mutex_lock(&rmi->support_fn_list_mutex);
3827 if (!list_empty(&rmi->support_fn_list)) {
3828 list_for_each_entry_safe(fhandler, next_fhandler,
3829 &rmi->support_fn_list, link) {
3830 if (fhandler->fn_number == SYNAPTICS_RMI4_F1A)
3831 synaptics_rmi4_f1a_kfree(fhandler);
3832 else {
3833 kfree(fhandler->data);
3834 kfree(fhandler->extra);
3835 }
3836 kfree(fhandler);
3837 }
3838 }
3839 mutex_unlock(&rmi->support_fn_list_mutex);
3840
3841 if (gpio_is_valid(rmi4_data->board->reset_gpio))
3842 gpio_free(rmi4_data->board->reset_gpio);
3843 if (gpio_is_valid(rmi4_data->board->irq_gpio))
3844 gpio_free(rmi4_data->board->irq_gpio);
3845
3846 if (rmi4_data->ts_pinctrl) {
3847 if (IS_ERR_OR_NULL(rmi4_data->pinctrl_state_release)) {
3848 devm_pinctrl_put(rmi4_data->ts_pinctrl);
3849 rmi4_data->ts_pinctrl = NULL;
3850 } else {
3851 retval = pinctrl_select_state(rmi4_data->ts_pinctrl,
3852 rmi4_data->pinctrl_state_release);
3853 if (retval < 0)
3854 pr_err("failed to select release pinctrl state\n");
3855 }
3856 }
3857
3858 synaptics_rmi4_power_on(rmi4_data, false);
3859 synaptics_rmi4_regulator_configure(rmi4_data, false);
3860
3861 kfree(rmi4_data);
3862
3863 return 0;
3864}
3865
3866 /**
3867 * synaptics_rmi4_sensor_sleep()
3868 *
3869 * Called by synaptics_rmi4_early_suspend() and synaptics_rmi4_suspend().
3870 *
3871 * This function stops finger data acquisition and puts the sensor to sleep.
3872 */
3873static void synaptics_rmi4_sensor_sleep(struct synaptics_rmi4_data *rmi4_data)
3874{
3875 int retval;
3876 struct synaptics_rmi4_f01_device_control_0 device_ctrl;
3877
3878 retval = synaptics_rmi4_i2c_read(rmi4_data,
3879 rmi4_data->f01_ctrl_base_addr,
3880 device_ctrl.data,
3881 sizeof(device_ctrl.data));
3882 if (retval < 0) {
3883 dev_err(&(rmi4_data->input_dev->dev),
3884 "%s: Failed to enter sleep mode\n",
3885 __func__);
3886 rmi4_data->sensor_sleep = false;
3887 return;
3888 }
3889
3890 device_ctrl.sleep_mode = SENSOR_SLEEP;
3891 device_ctrl.nosleep = NO_SLEEP_OFF;
3892
3893 retval = synaptics_rmi4_i2c_write(rmi4_data,
3894 rmi4_data->f01_ctrl_base_addr,
3895 device_ctrl.data,
3896 sizeof(device_ctrl.data));
3897 if (retval < 0) {
3898 dev_err(&(rmi4_data->input_dev->dev),
3899 "%s: Failed to enter sleep mode\n",
3900 __func__);
3901 rmi4_data->sensor_sleep = false;
3902 return;
3903 } else {
3904 rmi4_data->sensor_sleep = true;
3905 }
3906
3907 return;
3908}
3909
3910 /**
3911 * synaptics_rmi4_sensor_wake()
3912 *
3913 * Called by synaptics_rmi4_resume() and synaptics_rmi4_late_resume().
3914 *
3915 * This function wakes the sensor from sleep.
3916 */
3917static void synaptics_rmi4_sensor_wake(struct synaptics_rmi4_data *rmi4_data)
3918{
3919 int retval;
3920 struct synaptics_rmi4_f01_device_control_0 device_ctrl;
3921
3922 retval = synaptics_rmi4_i2c_read(rmi4_data,
3923 rmi4_data->f01_ctrl_base_addr,
3924 device_ctrl.data,
3925 sizeof(device_ctrl.data));
3926 if (retval < 0) {
3927 dev_err(&(rmi4_data->input_dev->dev),
3928 "%s: Failed to wake from sleep mode\n",
3929 __func__);
3930 rmi4_data->sensor_sleep = true;
3931 return;
3932 }
3933
3934 if (device_ctrl.nosleep == NO_SLEEP_OFF &&
3935 device_ctrl.sleep_mode == NORMAL_OPERATION) {
3936 rmi4_data->sensor_sleep = false;
3937 return;
3938 }
3939
3940 device_ctrl.sleep_mode = NORMAL_OPERATION;
3941 device_ctrl.nosleep = NO_SLEEP_OFF;
3942
3943 retval = synaptics_rmi4_i2c_write(rmi4_data,
3944 rmi4_data->f01_ctrl_base_addr,
3945 device_ctrl.data,
3946 sizeof(device_ctrl.data));
3947 if (retval < 0) {
3948 dev_err(&(rmi4_data->input_dev->dev),
3949 "%s: Failed to wake from sleep mode\n",
3950 __func__);
3951 rmi4_data->sensor_sleep = true;
3952 return;
3953 } else {
3954 rmi4_data->sensor_sleep = false;
3955 }
3956
3957 return;
3958}
3959
3960#if defined(CONFIG_FB)
3961static int fb_notifier_callback(struct notifier_block *self,
3962 unsigned long event, void *data)
3963{
3964 struct fb_event *evdata = data;
3965 int *blank;
3966 struct synaptics_rmi4_data *rmi4_data =
3967 container_of(self, struct synaptics_rmi4_data, fb_notif);
3968
3969 if (evdata && evdata->data && rmi4_data && rmi4_data->i2c_client) {
3970 if (event == FB_EARLY_EVENT_BLANK)
3971 synaptics_secure_touch_stop(rmi4_data, 0);
3972 else if (event == FB_EVENT_BLANK) {
3973 blank = evdata->data;
3974 if (*blank == FB_BLANK_UNBLANK)
3975 synaptics_rmi4_resume(
3976 &(rmi4_data->input_dev->dev));
3977 else if (*blank == FB_BLANK_POWERDOWN)
3978 synaptics_rmi4_suspend(
3979 &(rmi4_data->input_dev->dev));
3980 }
3981 }
3982
3983 return 0;
3984}
3985#elif defined(CONFIG_HAS_EARLYSUSPEND)
3986 /**
3987 * synaptics_rmi4_early_suspend()
3988 *
3989 * Called by the kernel during the early suspend phase when the system
3990 * enters suspend.
3991 *
3992 * This function calls synaptics_rmi4_sensor_sleep() to stop finger
3993 * data acquisition and put the sensor to sleep.
3994 */
3995static void synaptics_rmi4_early_suspend(struct early_suspend *h)
3996{
3997 struct synaptics_rmi4_data *rmi4_data =
3998 container_of(h, struct synaptics_rmi4_data,
3999 early_suspend);
4000
4001 if (rmi4_data->stay_awake)
4002 rmi4_data->staying_awake = true;
4003 else
4004 rmi4_data->staying_awake = false;
4005
4006 synaptics_secure_touch_stop(rmi4_data, 0);
4007
4008 rmi4_data->touch_stopped = true;
4009 wake_up(&rmi4_data->wait);
4010 synaptics_rmi4_irq_enable(rmi4_data, false);
4011 synaptics_rmi4_sensor_sleep(rmi4_data);
4012
4013 if (rmi4_data->full_pm_cycle)
4014 synaptics_rmi4_suspend(&(rmi4_data->input_dev->dev));
4015
4016 return;
4017}
4018
4019 /**
4020 * synaptics_rmi4_late_resume()
4021 *
4022 * Called by the kernel during the late resume phase when the system
4023 * wakes up from suspend.
4024 *
4025 * This function goes through the sensor wake process if the system wakes
4026 * up from early suspend (without going into suspend).
4027 */
4028static void synaptics_rmi4_late_resume(struct early_suspend *h)
4029{
4030 struct synaptics_rmi4_data *rmi4_data =
4031 container_of(h, struct synaptics_rmi4_data,
4032 early_suspend);
4033
4034 if (rmi4_data->staying_awake)
4035 return;
4036
4037 synaptics_secure_touch_stop(rmi4_data, 0);
4038
4039 if (rmi4_data->full_pm_cycle)
4040 synaptics_rmi4_resume(&(rmi4_data->input_dev->dev));
4041
4042 if (rmi4_data->sensor_sleep == true) {
4043 synaptics_rmi4_sensor_wake(rmi4_data);
4044 rmi4_data->touch_stopped = false;
4045 synaptics_rmi4_irq_enable(rmi4_data, true);
4046 }
4047
4048 return;
4049}
4050#endif
4051
4052static int synaptics_rmi4_regulator_lpm(struct synaptics_rmi4_data *rmi4_data,
4053 bool on)
4054{
4055 int retval;
4056 int load_ua;
4057
4058 if (on == false)
4059 goto regulator_hpm;
4060
4061 if (rmi4_data->board->i2c_pull_up) {
4062 load_ua = rmi4_data->board->power_down_enable ?
4063 0 : RMI4_I2C_LPM_LOAD_UA;
4064 retval = reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
4065 load_ua);
4066 if (retval < 0) {
4067 dev_err(&rmi4_data->i2c_client->dev,
4068 "Regulator vcc_i2c set_opt failed " \
4069 "rc=%d\n", retval);
4070 goto fail_regulator_lpm;
4071 }
4072
4073 if (rmi4_data->board->power_down_enable) {
4074 retval = regulator_disable(rmi4_data->vcc_i2c);
4075 if (retval) {
4076 dev_err(&rmi4_data->i2c_client->dev,
4077 "Regulator vcc_i2c disable failed " \
4078 "rc=%d\n", retval);
4079 goto fail_regulator_lpm;
4080 }
4081 }
4082 }
4083
4084 load_ua = rmi4_data->board->power_down_enable ? 0 : RMI4_LPM_LOAD_UA;
4085 retval = reg_set_optimum_mode_check(rmi4_data->vdd, load_ua);
4086 if (retval < 0) {
4087 dev_err(&rmi4_data->i2c_client->dev,
4088 "Regulator vdd_ana set_opt failed rc=%d\n",
4089 retval);
4090 goto fail_regulator_lpm;
4091 }
4092
4093 if (rmi4_data->board->power_down_enable) {
4094 retval = regulator_disable(rmi4_data->vdd);
4095 if (retval) {
4096 dev_err(&rmi4_data->i2c_client->dev,
4097 "Regulator vdd disable failed rc=%d\n",
4098 retval);
4099 goto fail_regulator_lpm;
4100 }
4101 }
4102
4103 return 0;
4104
4105regulator_hpm:
4106
4107 retval = reg_set_optimum_mode_check(rmi4_data->vdd,
4108 RMI4_ACTIVE_LOAD_UA);
4109 if (retval < 0) {
4110 dev_err(&rmi4_data->i2c_client->dev,
4111 "Regulator vcc_ana set_opt failed rc=%d\n",
4112 retval);
4113 goto fail_regulator_hpm;
4114 }
4115
4116 if (rmi4_data->board->power_down_enable) {
4117 retval = regulator_enable(rmi4_data->vdd);
4118 if (retval) {
4119 dev_err(&rmi4_data->i2c_client->dev,
4120 "Regulator vdd enable failed rc=%d\n",
4121 retval);
4122 goto fail_regulator_hpm;
4123 }
4124 }
4125
4126 if (rmi4_data->board->i2c_pull_up) {
4127 retval = reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
4128 RMI4_I2C_LOAD_UA);
4129 if (retval < 0) {
4130 dev_err(&rmi4_data->i2c_client->dev,
4131 "Regulator vcc_i2c set_opt failed rc=%d\n",
4132 retval);
4133 goto fail_regulator_hpm;
4134 }
4135
4136 if (rmi4_data->board->power_down_enable) {
4137 retval = regulator_enable(rmi4_data->vcc_i2c);
4138 if (retval) {
4139 dev_err(&rmi4_data->i2c_client->dev,
4140 "Regulator vcc_i2c enable failed " \
4141 "rc=%d\n", retval);
4142 goto fail_regulator_hpm;
4143 }
4144 }
4145 }
4146
4147 return 0;
4148
4149fail_regulator_lpm:
4150 reg_set_optimum_mode_check(rmi4_data->vdd, RMI4_ACTIVE_LOAD_UA);
4151 if (rmi4_data->board->i2c_pull_up)
4152 reg_set_optimum_mode_check(rmi4_data->vcc_i2c,
4153 RMI4_I2C_LOAD_UA);
4154
4155 return retval;
4156
4157fail_regulator_hpm:
4158 load_ua = rmi4_data->board->power_down_enable ? 0 : RMI4_LPM_LOAD_UA;
4159 reg_set_optimum_mode_check(rmi4_data->vdd, load_ua);
4160 if (rmi4_data->board->i2c_pull_up) {
4161 load_ua = rmi4_data->board->power_down_enable ?
4162 0 : RMI4_I2C_LPM_LOAD_UA;
4163 reg_set_optimum_mode_check(rmi4_data->vcc_i2c, load_ua);
4164 }
4165 return retval;
4166}
4167
4168static int synaptics_rmi4_check_configuration(struct synaptics_rmi4_data
4169 *rmi4_data)
4170{
4171 int retval;
4172 struct synaptics_rmi4_f01_device_control_0 device_control;
4173 struct synaptics_rmi4_f01_device_status device_status;
4174
4175 retval = synaptics_rmi4_i2c_read(rmi4_data,
4176 rmi4_data->f01_data_base_addr,
4177 device_status.data,
4178 sizeof(device_status.data));
4179 if (retval < 0) {
4180 dev_err(&rmi4_data->i2c_client->dev,
4181 "Failed to read device status, rc=%d\n", retval);
4182 return retval;
4183 }
4184
4185 if (device_status.unconfigured) {
4186 retval = synaptics_rmi4_query_device(rmi4_data);
4187 if (retval < 0) {
4188 dev_err(&rmi4_data->i2c_client->dev,
4189 "Failed to query device, rc=%d\n", retval);
4190 return retval;
4191 }
4192
4193 retval = synaptics_rmi4_i2c_read(rmi4_data,
4194 rmi4_data->f01_ctrl_base_addr,
4195 device_control.data,
4196 sizeof(device_control.data));
4197 if (retval < 0)
4198 return retval;
4199
4200 device_control.configured = DEVICE_CONFIGURED;
4201
4202 retval = synaptics_rmi4_i2c_write(rmi4_data,
4203 rmi4_data->f01_ctrl_base_addr,
4204 device_control.data,
4205 sizeof(device_control.data));
4206 if (retval < 0)
4207 return retval;
4208 }
4209
4210 return 0;
4211}
4212
4213 /**
4214 * synaptics_rmi4_suspend()
4215 *
4216 * Called by the kernel during the suspend phase when the system
4217 * enters suspend.
4218 *
4219 * This function stops finger data acquisition and puts the sensor to
4220 * sleep (if not already done so during the early suspend phase),
4221 * disables the interrupt, and turns off the power to the sensor.
4222 */
4223#ifdef CONFIG_PM
4224static int synaptics_rmi4_suspend(struct device *dev)
4225{
4226 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
4227 int retval;
4228
4229 if (rmi4_data->stay_awake) {
4230 rmi4_data->staying_awake = true;
4231 return 0;
4232 } else
4233 rmi4_data->staying_awake = false;
4234
4235 if (rmi4_data->suspended) {
4236 dev_info(dev, "Already in suspend state\n");
4237 return 0;
4238 }
4239
4240 synaptics_secure_touch_stop(rmi4_data, 1);
4241
4242 if (!rmi4_data->fw_updating) {
4243 if (!rmi4_data->sensor_sleep) {
4244 rmi4_data->touch_stopped = true;
4245 wake_up(&rmi4_data->wait);
4246 synaptics_rmi4_irq_enable(rmi4_data, false);
4247 synaptics_rmi4_sensor_sleep(rmi4_data);
4248 }
4249
4250 synaptics_rmi4_release_all(rmi4_data);
4251
4252 retval = synaptics_rmi4_regulator_lpm(rmi4_data, true);
4253 if (retval < 0) {
4254 dev_err(dev, "failed to enter low power mode\n");
4255 goto err_lpm_regulator;
4256 }
4257 } else {
4258 dev_err(dev,
4259 "Firmware updating, cannot go into suspend mode\n");
4260 return 0;
4261 }
4262
4263 if (rmi4_data->board->disable_gpios) {
4264 if (rmi4_data->ts_pinctrl) {
4265 retval = pinctrl_select_state(rmi4_data->ts_pinctrl,
4266 rmi4_data->pinctrl_state_suspend);
4267 if (retval < 0)
4268 dev_err(dev, "failed to select idle pinctrl state\n");
4269 }
4270
4271 retval = synaptics_rmi4_gpio_configure(rmi4_data, false);
4272 if (retval < 0) {
4273 dev_err(dev, "failed to put gpios in suspend state\n");
4274 goto err_gpio_configure;
4275 }
4276 }
4277 rmi4_data->suspended = true;
4278
4279 return 0;
4280
4281err_gpio_configure:
4282 if (rmi4_data->ts_pinctrl) {
4283 retval = pinctrl_select_state(rmi4_data->ts_pinctrl,
4284 rmi4_data->pinctrl_state_active);
4285 if (retval < 0)
4286 dev_err(dev, "failed to select get default pinctrl state\n");
4287 }
4288 synaptics_rmi4_regulator_lpm(rmi4_data, false);
4289
4290err_lpm_regulator:
4291 if (rmi4_data->sensor_sleep) {
4292 synaptics_rmi4_sensor_wake(rmi4_data);
4293 synaptics_rmi4_irq_enable(rmi4_data, true);
4294 rmi4_data->touch_stopped = false;
4295 }
4296
4297 return retval;
4298}
4299
4300 /**
4301 * synaptics_rmi4_resume()
4302 *
4303 * Called by the kernel during the resume phase when the system
4304 * wakes up from suspend.
4305 *
4306 * This function turns on the power to the sensor, wakes the sensor
4307 * from sleep, enables the interrupt, and starts finger data
4308 * acquisition.
4309 */
4310static int synaptics_rmi4_resume(struct device *dev)
4311{
4312 struct synaptics_rmi4_data *rmi4_data = dev_get_drvdata(dev);
4313 int retval;
4314
4315 if (rmi4_data->staying_awake)
4316 return 0;
4317
4318 if (!rmi4_data->suspended) {
4319 dev_info(dev, "Already in awake state\n");
4320 return 0;
4321 }
4322
4323 synaptics_secure_touch_stop(rmi4_data, 1);
4324
4325 retval = synaptics_rmi4_regulator_lpm(rmi4_data, false);
4326 if (retval < 0) {
4327 dev_err(dev, "Failed to enter active power mode\n");
4328 return retval;
4329 }
4330
4331 if (rmi4_data->board->disable_gpios) {
4332 if (rmi4_data->ts_pinctrl) {
4333 retval = pinctrl_select_state(rmi4_data->ts_pinctrl,
4334 rmi4_data->pinctrl_state_active);
4335 if (retval < 0)
4336 dev_err(dev, "failed to select default pinctrl state\n");
4337 }
4338
4339 retval = synaptics_rmi4_gpio_configure(rmi4_data, true);
4340 if (retval < 0) {
4341 dev_err(dev, "Failed to put gpios in active state\n");
4342 goto err_gpio_configure;
4343 }
4344 }
4345
4346 synaptics_rmi4_sensor_wake(rmi4_data);
4347 rmi4_data->touch_stopped = false;
4348 synaptics_rmi4_irq_enable(rmi4_data, true);
4349
4350 retval = synaptics_rmi4_check_configuration(rmi4_data);
4351 if (retval < 0) {
4352 dev_err(dev, "Failed to check configuration\n");
4353 goto err_check_configuration;
4354 }
4355 rmi4_data->suspended = false;
4356
4357 return 0;
4358
4359err_check_configuration:
4360 synaptics_rmi4_irq_enable(rmi4_data, false);
4361 rmi4_data->touch_stopped = true;
4362 synaptics_rmi4_sensor_sleep(rmi4_data);
4363
4364 if (rmi4_data->board->disable_gpios) {
4365 if (rmi4_data->ts_pinctrl) {
4366 retval = pinctrl_select_state(rmi4_data->ts_pinctrl,
4367 rmi4_data->pinctrl_state_suspend);
4368 if (retval < 0)
4369 dev_err(dev, "failed to select idle pinctrl state\n");
4370 }
4371
4372 synaptics_rmi4_gpio_configure(rmi4_data, false);
4373 }
4374 synaptics_rmi4_regulator_lpm(rmi4_data, true);
4375 wake_up(&rmi4_data->wait);
4376
4377 return retval;
4378
4379err_gpio_configure:
4380 if (rmi4_data->ts_pinctrl) {
4381 retval = pinctrl_select_state(rmi4_data->ts_pinctrl,
4382 rmi4_data->pinctrl_state_suspend);
4383 if (retval < 0)
4384 pr_err("failed to select idle pinctrl state\n");
4385 }
4386 synaptics_rmi4_regulator_lpm(rmi4_data, true);
4387 wake_up(&rmi4_data->wait);
4388
4389 return retval;
4390}
4391
4392#if (!defined(CONFIG_FB) && !defined(CONFIG_HAS_EARLYSUSPEND))
4393static const struct dev_pm_ops synaptics_rmi4_dev_pm_ops = {
4394 .suspend = synaptics_rmi4_suspend,
4395 .resume = synaptics_rmi4_resume,
4396};
4397#else
4398static const struct dev_pm_ops synaptics_rmi4_dev_pm_ops = {
4399};
4400#endif
4401#else
4402static int synaptics_rmi4_suspend(struct device *dev)
4403{
4404 return 0;
4405}
4406
4407static int synaptics_rmi4_resume(struct device *dev)
4408{
4409 return 0;
4410}
4411#endif
4412
4413static const struct i2c_device_id synaptics_rmi4_id_table[] = {
4414 {DRIVER_NAME, 0},
4415 {},
4416};
4417MODULE_DEVICE_TABLE(i2c, synaptics_rmi4_id_table);
4418
4419#ifdef CONFIG_OF
4420static struct of_device_id rmi4_match_table[] = {
4421 { .compatible = "synaptics,rmi4",},
4422 { },
4423};
4424#else
4425#define rmi4_match_table NULL
4426#endif
4427
4428static struct i2c_driver synaptics_rmi4_driver = {
4429 .driver = {
4430 .name = DRIVER_NAME,
4431 .owner = THIS_MODULE,
4432 .of_match_table = rmi4_match_table,
4433#ifdef CONFIG_PM
4434 .pm = &synaptics_rmi4_dev_pm_ops,
4435#endif
4436 },
4437 .probe = synaptics_rmi4_probe,
4438 .remove = synaptics_rmi4_remove,
4439 .id_table = synaptics_rmi4_id_table,
4440};
4441
4442 /**
4443 * synaptics_rmi4_init()
4444 *
4445 * Called by the kernel during do_initcalls (if built-in)
4446 * or when the driver is loaded (if a module).
4447 *
4448 * This function registers the driver to the I2C subsystem.
4449 *
4450 */
4451static int __init synaptics_rmi4_init(void)
4452{
4453 return i2c_add_driver(&synaptics_rmi4_driver);
4454}
4455
4456 /**
4457 * synaptics_rmi4_exit()
4458 *
4459 * Called by the kernel when the driver is unloaded.
4460 *
4461 * This funtion unregisters the driver from the I2C subsystem.
4462 *
4463 */
4464static void __exit synaptics_rmi4_exit(void)
4465{
4466 i2c_del_driver(&synaptics_rmi4_driver);
4467}
4468
4469module_init(synaptics_rmi4_init);
4470module_exit(synaptics_rmi4_exit);
4471
4472MODULE_AUTHOR("Synaptics, Inc.");
4473MODULE_DESCRIPTION("Synaptics RMI4 I2C Touch Driver");
4474MODULE_LICENSE("GPL v2");