1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Atmel maXTouch Touchscreen driver
5 * Copyright (C) 2010 Samsung Electronics Co.Ltd
6 * Copyright (C) 2011-2014 Atmel Corporation
7 * Copyright (C) 2012 Google, Inc.
8 * Copyright (C) 2016 Zodiac Inflight Innovations
10 * Author: Joonyoung Shim <jy0922.shim@samsung.com>
13 #include <linux/acpi.h>
14 #include <linux/dmi.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/completion.h>
18 #include <linux/delay.h>
19 #include <linux/firmware.h>
20 #include <linux/i2c.h>
21 #include <linux/input/mt.h>
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
25 #include <linux/property.h>
26 #include <linux/slab.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/gpio/consumer.h>
29 #include <linux/unaligned.h>
30 #include <media/v4l2-device.h>
31 #include <media/v4l2-ioctl.h>
32 #include <media/videobuf2-v4l2.h>
33 #include <media/videobuf2-vmalloc.h>
34 #include <dt-bindings/input/atmel-maxtouch.h>
37 #define MXT_FW_NAME "maxtouch.fw"
38 #define MXT_CFG_NAME "maxtouch.cfg"
39 #define MXT_CFG_MAGIC "OBP_RAW V1"
42 #define MXT_OBJECT_START 0x07
43 #define MXT_OBJECT_SIZE 6
44 #define MXT_INFO_CHECKSUM_SIZE 3
45 #define MXT_MAX_BLOCK_WRITE 256
48 #define MXT_DEBUG_DIAGNOSTIC_T37 37
49 #define MXT_GEN_MESSAGE_T5 5
50 #define MXT_GEN_COMMAND_T6 6
51 #define MXT_GEN_POWER_T7 7
52 #define MXT_GEN_ACQUIRE_T8 8
53 #define MXT_GEN_DATASOURCE_T53 53
54 #define MXT_TOUCH_MULTI_T9 9
55 #define MXT_TOUCH_KEYARRAY_T15 15
56 #define MXT_TOUCH_PROXIMITY_T23 23
57 #define MXT_TOUCH_PROXKEY_T52 52
58 #define MXT_TOUCH_PTC_KEYS_T97 97
59 #define MXT_PROCI_GRIPFACE_T20 20
60 #define MXT_PROCG_NOISE_T22 22
61 #define MXT_PROCI_ONETOUCH_T24 24
62 #define MXT_PROCI_TWOTOUCH_T27 27
63 #define MXT_PROCI_GRIP_T40 40
64 #define MXT_PROCI_PALM_T41 41
65 #define MXT_PROCI_TOUCHSUPPRESSION_T42 42
66 #define MXT_PROCI_STYLUS_T47 47
67 #define MXT_PROCG_NOISESUPPRESSION_T48 48
68 #define MXT_SPT_COMMSCONFIG_T18 18
69 #define MXT_SPT_GPIOPWM_T19 19
70 #define MXT_SPT_SELFTEST_T25 25
71 #define MXT_SPT_CTECONFIG_T28 28
72 #define MXT_SPT_USERDATA_T38 38
73 #define MXT_SPT_DIGITIZER_T43 43
74 #define MXT_SPT_MESSAGECOUNT_T44 44
75 #define MXT_SPT_CTECONFIG_T46 46
76 #define MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71 71
77 #define MXT_TOUCH_MULTITOUCHSCREEN_T100 100
79 /* MXT_GEN_MESSAGE_T5 object */
80 #define MXT_RPTID_NOMSG 0xff
82 /* MXT_GEN_COMMAND_T6 field */
83 #define MXT_COMMAND_RESET 0
84 #define MXT_COMMAND_BACKUPNV 1
85 #define MXT_COMMAND_CALIBRATE 2
86 #define MXT_COMMAND_REPORTALL 3
87 #define MXT_COMMAND_DIAGNOSTIC 5
89 /* Define for T6 status byte */
90 #define MXT_T6_STATUS_RESET BIT(7)
91 #define MXT_T6_STATUS_OFL BIT(6)
92 #define MXT_T6_STATUS_SIGERR BIT(5)
93 #define MXT_T6_STATUS_CAL BIT(4)
94 #define MXT_T6_STATUS_CFGERR BIT(3)
95 #define MXT_T6_STATUS_COMSERR BIT(2)
97 /* MXT_GEN_POWER_T7 field */
103 #define MXT_POWER_CFG_RUN 0
104 #define MXT_POWER_CFG_DEEPSLEEP 1
106 /* MXT_TOUCH_MULTI_T9 field */
107 #define MXT_T9_CTRL 0
108 #define MXT_T9_XSIZE 3
109 #define MXT_T9_YSIZE 4
110 #define MXT_T9_ORIENT 9
111 #define MXT_T9_RANGE 18
113 /* MXT_TOUCH_MULTI_T9 status */
114 #define MXT_T9_UNGRIP BIT(0)
115 #define MXT_T9_SUPPRESS BIT(1)
116 #define MXT_T9_AMP BIT(2)
117 #define MXT_T9_VECTOR BIT(3)
118 #define MXT_T9_MOVE BIT(4)
119 #define MXT_T9_RELEASE BIT(5)
120 #define MXT_T9_PRESS BIT(6)
121 #define MXT_T9_DETECT BIT(7)
128 /* MXT_TOUCH_MULTI_T9 orient */
129 #define MXT_T9_ORIENT_SWITCH BIT(0)
130 #define MXT_T9_ORIENT_INVERTX BIT(1)
131 #define MXT_T9_ORIENT_INVERTY BIT(2)
133 /* MXT_SPT_COMMSCONFIG_T18 */
134 #define MXT_COMMS_CTRL 0
135 #define MXT_COMMS_CMD 1
136 #define MXT_COMMS_RETRIGEN BIT(6)
138 /* MXT_DEBUG_DIAGNOSTIC_T37 */
139 #define MXT_DIAGNOSTIC_PAGEUP 0x01
140 #define MXT_DIAGNOSTIC_DELTAS 0x10
141 #define MXT_DIAGNOSTIC_REFS 0x11
142 #define MXT_DIAGNOSTIC_SIZE 128
144 #define MXT_FAMILY_1386 160
145 #define MXT1386_COLUMNS 3
146 #define MXT1386_PAGES_PER_COLUMN 8
149 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
152 u8 data
[MXT_DIAGNOSTIC_SIZE
];
156 /* Define for MXT_GEN_COMMAND_T6 */
157 #define MXT_BOOT_VALUE 0xa5
158 #define MXT_RESET_VALUE 0x01
159 #define MXT_BACKUP_VALUE 0x55
161 /* T100 Multiple Touch Touchscreen */
162 #define MXT_T100_CTRL 0
163 #define MXT_T100_CFG1 1
164 #define MXT_T100_TCHAUX 3
165 #define MXT_T100_XSIZE 9
166 #define MXT_T100_XRANGE 13
167 #define MXT_T100_YSIZE 20
168 #define MXT_T100_YRANGE 24
170 #define MXT_T100_CFG_SWITCHXY BIT(5)
171 #define MXT_T100_CFG_INVERTY BIT(6)
172 #define MXT_T100_CFG_INVERTX BIT(7)
174 #define MXT_T100_TCHAUX_VECT BIT(0)
175 #define MXT_T100_TCHAUX_AMPL BIT(1)
176 #define MXT_T100_TCHAUX_AREA BIT(2)
178 #define MXT_T100_DETECT BIT(7)
179 #define MXT_T100_TYPE_MASK 0x70
182 MXT_T100_TYPE_FINGER
= 1,
183 MXT_T100_TYPE_PASSIVE_STYLUS
= 2,
184 MXT_T100_TYPE_HOVERING_FINGER
= 4,
185 MXT_T100_TYPE_GLOVE
= 5,
186 MXT_T100_TYPE_LARGE_TOUCH
= 6,
189 #define MXT_DISTANCE_ACTIVE_TOUCH 0
190 #define MXT_DISTANCE_HOVERING 1
192 #define MXT_TOUCH_MAJOR_DEFAULT 1
193 #define MXT_PRESSURE_DEFAULT 1
196 #define MXT_BACKUP_TIME 50 /* msec */
197 #define MXT_RESET_GPIO_TIME 20 /* msec */
198 #define MXT_RESET_INVALID_CHG 100 /* msec */
199 #define MXT_RESET_TIME 200 /* msec */
200 #define MXT_RESET_TIMEOUT 3000 /* msec */
201 #define MXT_CRC_TIMEOUT 1000 /* msec */
202 #define MXT_FW_RESET_TIME 3000 /* msec */
203 #define MXT_FW_CHG_TIMEOUT 300 /* msec */
204 #define MXT_WAKEUP_TIME 25 /* msec */
206 /* Command to unlock bootloader */
207 #define MXT_UNLOCK_CMD_MSB 0xaa
208 #define MXT_UNLOCK_CMD_LSB 0xdc
210 /* Bootloader mode status */
211 #define MXT_WAITING_BOOTLOAD_CMD 0xc0 /* valid 7 6 bit only */
212 #define MXT_WAITING_FRAME_DATA 0x80 /* valid 7 6 bit only */
213 #define MXT_FRAME_CRC_CHECK 0x02
214 #define MXT_FRAME_CRC_FAIL 0x03
215 #define MXT_FRAME_CRC_PASS 0x04
216 #define MXT_APP_CRC_FAIL 0x40 /* valid 7 8 bit only */
217 #define MXT_BOOT_STATUS_MASK 0x3f
218 #define MXT_BOOT_EXTENDED_ID BIT(5)
219 #define MXT_BOOT_ID_MASK 0x1f
221 /* Touchscreen absolute values */
222 #define MXT_MAX_AREA 0xff
224 #define MXT_PIXELS_PER_MM 20
240 u8 instances_minus_one
;
246 u16 diag_cmd_address
;
247 struct t37_debug
*t37_buf
;
248 unsigned int t37_pages
;
249 unsigned int t37_nodes
;
251 struct v4l2_device v4l2
;
252 struct v4l2_pix_format format
;
253 struct video_device vdev
;
254 struct vb2_queue queue
;
259 enum v4l_dbg_inputs
{
260 MXT_V4L_INPUT_DELTAS
,
265 enum mxt_suspend_mode
{
266 MXT_SUSPEND_DEEP_SLEEP
= 0,
267 MXT_SUSPEND_T9_CTRL
= 1,
270 /* Config update context */
280 struct mxt_info info
;
283 /* Each client has this additional data */
285 struct i2c_client
*client
;
286 struct input_dev
*input_dev
;
287 char phys
[64]; /* device physical location */
288 struct mxt_object
*object_table
;
289 struct mxt_info
*info
;
290 void *raw_info_block
;
311 u8 last_message_count
;
314 struct t7_config t7_cfg
;
316 struct regulator_bulk_data regulators
[2];
317 struct gpio_desc
*reset_gpio
;
318 struct gpio_desc
*wake_gpio
;
319 bool use_retrigen_workaround
;
321 /* Cached parameters from object table */
337 u8 T100_reportid_min
;
338 u8 T100_reportid_max
;
340 /* for fw update in bootloader */
341 struct completion bl_completion
;
343 /* for reset handling */
344 struct completion reset_completion
;
346 /* for config update handling */
347 struct completion crc_completion
;
350 unsigned int t19_num_keys
;
353 unsigned int t15_num_keys
;
355 enum mxt_suspend_mode suspend_mode
;
360 struct mxt_vb2_buffer
{
361 struct vb2_buffer vb
;
362 struct list_head list
;
365 static size_t mxt_obj_size(const struct mxt_object
*obj
)
367 return obj
->size_minus_one
+ 1;
370 static size_t mxt_obj_instances(const struct mxt_object
*obj
)
372 return obj
->instances_minus_one
+ 1;
375 static bool mxt_object_readable(unsigned int type
)
378 case MXT_GEN_COMMAND_T6
:
379 case MXT_GEN_POWER_T7
:
380 case MXT_GEN_ACQUIRE_T8
:
381 case MXT_GEN_DATASOURCE_T53
:
382 case MXT_TOUCH_MULTI_T9
:
383 case MXT_TOUCH_KEYARRAY_T15
:
384 case MXT_TOUCH_PROXIMITY_T23
:
385 case MXT_TOUCH_PROXKEY_T52
:
386 case MXT_TOUCH_PTC_KEYS_T97
:
387 case MXT_TOUCH_MULTITOUCHSCREEN_T100
:
388 case MXT_PROCI_GRIPFACE_T20
:
389 case MXT_PROCG_NOISE_T22
:
390 case MXT_PROCI_ONETOUCH_T24
:
391 case MXT_PROCI_TWOTOUCH_T27
:
392 case MXT_PROCI_GRIP_T40
:
393 case MXT_PROCI_PALM_T41
:
394 case MXT_PROCI_TOUCHSUPPRESSION_T42
:
395 case MXT_PROCI_STYLUS_T47
:
396 case MXT_PROCG_NOISESUPPRESSION_T48
:
397 case MXT_SPT_COMMSCONFIG_T18
:
398 case MXT_SPT_GPIOPWM_T19
:
399 case MXT_SPT_SELFTEST_T25
:
400 case MXT_SPT_CTECONFIG_T28
:
401 case MXT_SPT_USERDATA_T38
:
402 case MXT_SPT_DIGITIZER_T43
:
403 case MXT_SPT_CTECONFIG_T46
:
404 case MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71
:
411 static void mxt_dump_message(struct mxt_data
*data
, u8
*message
)
413 dev_dbg(&data
->client
->dev
, "message: %*ph\n",
414 data
->T5_msg_size
, message
);
417 static int mxt_wait_for_completion(struct mxt_data
*data
,
418 struct completion
*comp
,
419 unsigned int timeout_ms
)
421 struct device
*dev
= &data
->client
->dev
;
422 unsigned long timeout
= msecs_to_jiffies(timeout_ms
);
425 ret
= wait_for_completion_interruptible_timeout(comp
, timeout
);
428 } else if (ret
== 0) {
429 dev_err(dev
, "Wait for completion timed out.\n");
435 static int mxt_bootloader_read(struct mxt_data
*data
,
436 u8
*val
, unsigned int count
)
441 msg
.addr
= data
->bootloader_addr
;
442 msg
.flags
= data
->client
->flags
& I2C_M_TEN
;
443 msg
.flags
|= I2C_M_RD
;
447 ret
= i2c_transfer(data
->client
->adapter
, &msg
, 1);
451 ret
= ret
< 0 ? ret
: -EIO
;
452 dev_err(&data
->client
->dev
, "%s: i2c recv failed (%d)\n",
459 static int mxt_bootloader_write(struct mxt_data
*data
,
460 const u8
* const val
, unsigned int count
)
465 msg
.addr
= data
->bootloader_addr
;
466 msg
.flags
= data
->client
->flags
& I2C_M_TEN
;
470 ret
= i2c_transfer(data
->client
->adapter
, &msg
, 1);
474 ret
= ret
< 0 ? ret
: -EIO
;
475 dev_err(&data
->client
->dev
, "%s: i2c send failed (%d)\n",
482 static int mxt_lookup_bootloader_address(struct mxt_data
*data
, bool retry
)
484 u8 appmode
= data
->client
->addr
;
486 u8 family_id
= data
->info
? data
->info
->family_id
: 0;
491 /* Chips after 1664S use different scheme */
492 if (retry
|| family_id
>= 0xa2) {
493 bootloader
= appmode
- 0x24;
496 fallthrough
; /* for normal case */
501 bootloader
= appmode
- 0x26;
505 dev_err(&data
->client
->dev
,
506 "Appmode i2c address 0x%02x not found\n",
511 data
->bootloader_addr
= bootloader
;
515 static int mxt_probe_bootloader(struct mxt_data
*data
, bool alt_address
)
517 struct device
*dev
= &data
->client
->dev
;
522 error
= mxt_lookup_bootloader_address(data
, alt_address
);
526 error
= mxt_bootloader_read(data
, &val
, 1);
530 /* Check app crc fail mode */
531 crc_failure
= (val
& ~MXT_BOOT_STATUS_MASK
) == MXT_APP_CRC_FAIL
;
533 dev_err(dev
, "Detected bootloader, status:%02X%s\n",
534 val
, crc_failure
? ", APP_CRC_FAIL" : "");
539 static u8
mxt_get_bootloader_version(struct mxt_data
*data
, u8 val
)
541 struct device
*dev
= &data
->client
->dev
;
544 if (val
& MXT_BOOT_EXTENDED_ID
) {
545 if (mxt_bootloader_read(data
, &buf
[0], 3) != 0) {
546 dev_err(dev
, "%s: i2c failure\n", __func__
);
550 dev_dbg(dev
, "Bootloader ID:%d Version:%d\n", buf
[1], buf
[2]);
554 dev_dbg(dev
, "Bootloader ID:%d\n", val
& MXT_BOOT_ID_MASK
);
560 static int mxt_check_bootloader(struct mxt_data
*data
, unsigned int state
,
563 struct device
*dev
= &data
->client
->dev
;
570 * In application update mode, the interrupt
571 * line signals state transitions. We must wait for the
572 * CHG assertion before reading the status byte.
573 * Once the status byte has been read, the line is deasserted.
575 ret
= mxt_wait_for_completion(data
, &data
->bl_completion
,
579 * TODO: handle -ERESTARTSYS better by terminating
580 * fw update process before returning to userspace
581 * by writing length 0x000 to device (iff we are in
582 * WAITING_FRAME_DATA state).
584 dev_err(dev
, "Update wait error %d\n", ret
);
589 ret
= mxt_bootloader_read(data
, &val
, 1);
593 if (state
== MXT_WAITING_BOOTLOAD_CMD
)
594 val
= mxt_get_bootloader_version(data
, val
);
597 case MXT_WAITING_BOOTLOAD_CMD
:
598 case MXT_WAITING_FRAME_DATA
:
599 case MXT_APP_CRC_FAIL
:
600 val
&= ~MXT_BOOT_STATUS_MASK
;
602 case MXT_FRAME_CRC_PASS
:
603 if (val
== MXT_FRAME_CRC_CHECK
) {
605 } else if (val
== MXT_FRAME_CRC_FAIL
) {
606 dev_err(dev
, "Bootloader CRC fail\n");
615 dev_err(dev
, "Invalid bootloader state %02X != %02X\n",
623 static int mxt_send_bootloader_cmd(struct mxt_data
*data
, bool unlock
)
628 buf
[0] = MXT_UNLOCK_CMD_LSB
;
629 buf
[1] = MXT_UNLOCK_CMD_MSB
;
635 return mxt_bootloader_write(data
, buf
, sizeof(buf
));
638 static bool mxt_wakeup_toggle(struct i2c_client
*client
,
639 bool wake_up
, bool in_i2c
)
641 struct mxt_data
*data
= i2c_get_clientdata(client
);
643 switch (data
->wakeup_method
) {
644 case ATMEL_MXT_WAKEUP_I2C_SCL
:
649 case ATMEL_MXT_WAKEUP_GPIO
:
653 gpiod_set_value(data
->wake_gpio
, wake_up
);
661 dev_dbg(&client
->dev
, "waking up controller\n");
663 msleep(MXT_WAKEUP_TIME
);
669 static int __mxt_read_reg(struct i2c_client
*client
,
670 u16 reg
, u16 len
, void *val
)
672 struct i2c_msg xfer
[2];
673 bool retried
= false;
678 buf
[1] = (reg
>> 8) & 0xff;
681 xfer
[0].addr
= client
->addr
;
687 xfer
[1].addr
= client
->addr
;
688 xfer
[1].flags
= I2C_M_RD
;
693 ret
= i2c_transfer(client
->adapter
, xfer
, 2);
696 } else if (!retried
&& mxt_wakeup_toggle(client
, true, true)) {
702 dev_err(&client
->dev
, "%s: i2c transfer failed (%d)\n",
709 static int __mxt_write_reg(struct i2c_client
*client
, u16 reg
, u16 len
,
712 bool retried
= false;
718 buf
= kmalloc(count
, GFP_KERNEL
);
723 buf
[1] = (reg
>> 8) & 0xff;
724 memcpy(&buf
[2], val
, len
);
727 ret
= i2c_master_send(client
, buf
, count
);
730 } else if (!retried
&& mxt_wakeup_toggle(client
, true, true)) {
736 dev_err(&client
->dev
, "%s: i2c send failed (%d)\n",
744 static int mxt_write_reg(struct i2c_client
*client
, u16 reg
, u8 val
)
746 return __mxt_write_reg(client
, reg
, 1, &val
);
749 static struct mxt_object
*
750 mxt_get_object(struct mxt_data
*data
, u8 type
)
752 struct mxt_object
*object
;
755 for (i
= 0; i
< data
->info
->object_num
; i
++) {
756 object
= data
->object_table
+ i
;
757 if (object
->type
== type
)
761 dev_warn(&data
->client
->dev
, "Invalid object type T%u\n", type
);
765 static void mxt_proc_t6_messages(struct mxt_data
*data
, u8
*msg
)
767 struct device
*dev
= &data
->client
->dev
;
769 u32 crc
= msg
[2] | (msg
[3] << 8) | (msg
[4] << 16);
771 if (crc
!= data
->config_crc
) {
772 data
->config_crc
= crc
;
773 dev_dbg(dev
, "T6 Config Checksum: 0x%06X\n", crc
);
776 complete(&data
->crc_completion
);
779 if (status
& MXT_T6_STATUS_RESET
)
780 complete(&data
->reset_completion
);
782 /* Output debug if status has changed */
783 if (status
!= data
->t6_status
)
784 dev_dbg(dev
, "T6 Status 0x%02X%s%s%s%s%s%s%s\n",
786 status
== 0 ? " OK" : "",
787 status
& MXT_T6_STATUS_RESET
? " RESET" : "",
788 status
& MXT_T6_STATUS_OFL
? " OFL" : "",
789 status
& MXT_T6_STATUS_SIGERR
? " SIGERR" : "",
790 status
& MXT_T6_STATUS_CAL
? " CAL" : "",
791 status
& MXT_T6_STATUS_CFGERR
? " CFGERR" : "",
792 status
& MXT_T6_STATUS_COMSERR
? " COMSERR" : "");
794 /* Save current status */
795 data
->t6_status
= status
;
798 static int mxt_write_object(struct mxt_data
*data
,
799 u8 type
, u8 offset
, u8 val
)
801 struct mxt_object
*object
;
804 object
= mxt_get_object(data
, type
);
805 if (!object
|| offset
>= mxt_obj_size(object
))
808 reg
= object
->start_address
;
809 return mxt_write_reg(data
->client
, reg
+ offset
, val
);
812 static void mxt_input_button(struct mxt_data
*data
, u8
*message
)
814 struct input_dev
*input
= data
->input_dev
;
817 for (i
= 0; i
< data
->t19_num_keys
; i
++) {
818 if (data
->t19_keymap
[i
] == KEY_RESERVED
)
821 /* Active-low switch */
822 input_report_key(input
, data
->t19_keymap
[i
],
823 !(message
[1] & BIT(i
)));
827 static void mxt_input_sync(struct mxt_data
*data
)
829 input_mt_report_pointer_emulation(data
->input_dev
,
831 input_sync(data
->input_dev
);
834 static void mxt_proc_t9_message(struct mxt_data
*data
, u8
*message
)
836 struct device
*dev
= &data
->client
->dev
;
837 struct input_dev
*input_dev
= data
->input_dev
;
845 id
= message
[0] - data
->T9_reportid_min
;
847 x
= (message
[2] << 4) | ((message
[4] >> 4) & 0xf);
848 y
= (message
[3] << 4) | ((message
[4] & 0xf));
850 /* Handle 10/12 bit switching */
851 if (data
->max_x
< 1024)
853 if (data
->max_y
< 1024)
857 amplitude
= message
[6];
860 "[%u] %c%c%c%c%c%c%c%c x: %5u y: %5u area: %3u amp: %3u\n",
862 (status
& MXT_T9_DETECT
) ? 'D' : '.',
863 (status
& MXT_T9_PRESS
) ? 'P' : '.',
864 (status
& MXT_T9_RELEASE
) ? 'R' : '.',
865 (status
& MXT_T9_MOVE
) ? 'M' : '.',
866 (status
& MXT_T9_VECTOR
) ? 'V' : '.',
867 (status
& MXT_T9_AMP
) ? 'A' : '.',
868 (status
& MXT_T9_SUPPRESS
) ? 'S' : '.',
869 (status
& MXT_T9_UNGRIP
) ? 'U' : '.',
870 x
, y
, area
, amplitude
);
872 input_mt_slot(input_dev
, id
);
874 if (status
& MXT_T9_DETECT
) {
876 * Multiple bits may be set if the host is slow to read
877 * the status messages, indicating all the events that
880 if (status
& MXT_T9_RELEASE
) {
881 input_mt_report_slot_inactive(input_dev
);
882 mxt_input_sync(data
);
885 /* if active, pressure must be non-zero */
887 amplitude
= MXT_PRESSURE_DEFAULT
;
890 input_mt_report_slot_state(input_dev
, MT_TOOL_FINGER
, 1);
891 input_report_abs(input_dev
, ABS_MT_POSITION_X
, x
);
892 input_report_abs(input_dev
, ABS_MT_POSITION_Y
, y
);
893 input_report_abs(input_dev
, ABS_MT_PRESSURE
, amplitude
);
894 input_report_abs(input_dev
, ABS_MT_TOUCH_MAJOR
, area
);
896 /* Touch no longer active, close out slot */
897 input_mt_report_slot_inactive(input_dev
);
900 data
->update_input
= true;
903 static void mxt_proc_t15_messages(struct mxt_data
*data
, u8
*message
)
905 struct input_dev
*input_dev
= data
->input_dev
;
906 unsigned long keystates
= get_unaligned_le32(&message
[2]);
909 for (key
= 0; key
< data
->t15_num_keys
; key
++)
910 input_report_key(input_dev
, data
->t15_keymap
[key
],
911 keystates
& BIT(key
));
913 data
->update_input
= true;
916 static void mxt_proc_t97_messages(struct mxt_data
*data
, u8
*message
)
918 mxt_proc_t15_messages(data
, message
);
921 static void mxt_proc_t100_message(struct mxt_data
*data
, u8
*message
)
923 struct device
*dev
= &data
->client
->dev
;
924 struct input_dev
*input_dev
= data
->input_dev
;
936 id
= message
[0] - data
->T100_reportid_min
- 2;
938 /* ignore SCRSTATUS events */
943 x
= get_unaligned_le16(&message
[2]);
944 y
= get_unaligned_le16(&message
[4]);
946 if (status
& MXT_T100_DETECT
) {
947 type
= (status
& MXT_T100_TYPE_MASK
) >> 4;
950 case MXT_T100_TYPE_HOVERING_FINGER
:
951 tool
= MT_TOOL_FINGER
;
952 distance
= MXT_DISTANCE_HOVERING
;
954 if (data
->t100_aux_vect
)
955 orientation
= message
[data
->t100_aux_vect
];
959 case MXT_T100_TYPE_FINGER
:
960 case MXT_T100_TYPE_GLOVE
:
961 tool
= MT_TOOL_FINGER
;
962 distance
= MXT_DISTANCE_ACTIVE_TOUCH
;
964 if (data
->t100_aux_area
)
965 major
= message
[data
->t100_aux_area
];
967 if (data
->t100_aux_ampl
)
968 pressure
= message
[data
->t100_aux_ampl
];
970 if (data
->t100_aux_vect
)
971 orientation
= message
[data
->t100_aux_vect
];
975 case MXT_T100_TYPE_PASSIVE_STYLUS
:
979 * Passive stylus is reported with size zero so
982 major
= MXT_TOUCH_MAJOR_DEFAULT
;
984 if (data
->t100_aux_ampl
)
985 pressure
= message
[data
->t100_aux_ampl
];
989 case MXT_T100_TYPE_LARGE_TOUCH
:
990 /* Ignore suppressed touch */
994 dev_dbg(dev
, "Unexpected T100 type\n");
1000 * Values reported should be non-zero if tool is touching the
1003 if (!pressure
&& type
!= MXT_T100_TYPE_HOVERING_FINGER
)
1004 pressure
= MXT_PRESSURE_DEFAULT
;
1006 input_mt_slot(input_dev
, id
);
1008 if (status
& MXT_T100_DETECT
) {
1009 dev_dbg(dev
, "[%u] type:%u x:%u y:%u a:%02X p:%02X v:%02X\n",
1010 id
, type
, x
, y
, major
, pressure
, orientation
);
1012 input_mt_report_slot_state(input_dev
, tool
, 1);
1013 input_report_abs(input_dev
, ABS_MT_POSITION_X
, x
);
1014 input_report_abs(input_dev
, ABS_MT_POSITION_Y
, y
);
1015 input_report_abs(input_dev
, ABS_MT_TOUCH_MAJOR
, major
);
1016 input_report_abs(input_dev
, ABS_MT_PRESSURE
, pressure
);
1017 input_report_abs(input_dev
, ABS_MT_DISTANCE
, distance
);
1018 input_report_abs(input_dev
, ABS_MT_ORIENTATION
, orientation
);
1020 dev_dbg(dev
, "[%u] release\n", id
);
1022 /* close out slot */
1023 input_mt_report_slot_inactive(input_dev
);
1026 data
->update_input
= true;
1029 static int mxt_proc_message(struct mxt_data
*data
, u8
*message
)
1031 u8 report_id
= message
[0];
1033 if (report_id
== MXT_RPTID_NOMSG
)
1036 if (report_id
== data
->T6_reportid
) {
1037 mxt_proc_t6_messages(data
, message
);
1038 } else if (!data
->input_dev
) {
1040 * Do not report events if input device
1041 * is not yet registered.
1043 mxt_dump_message(data
, message
);
1044 } else if (report_id
>= data
->T9_reportid_min
&&
1045 report_id
<= data
->T9_reportid_max
) {
1046 mxt_proc_t9_message(data
, message
);
1047 } else if (report_id
>= data
->T15_reportid_min
&&
1048 report_id
<= data
->T15_reportid_max
) {
1049 mxt_proc_t15_messages(data
, message
);
1050 } else if (report_id
>= data
->T97_reportid_min
&&
1051 report_id
<= data
->T97_reportid_max
) {
1052 mxt_proc_t97_messages(data
, message
);
1053 } else if (report_id
>= data
->T100_reportid_min
&&
1054 report_id
<= data
->T100_reportid_max
) {
1055 mxt_proc_t100_message(data
, message
);
1056 } else if (report_id
== data
->T19_reportid
) {
1057 mxt_input_button(data
, message
);
1058 data
->update_input
= true;
1060 mxt_dump_message(data
, message
);
1066 static int mxt_read_and_process_messages(struct mxt_data
*data
, u8 count
)
1068 struct device
*dev
= &data
->client
->dev
;
1073 /* Safety check for msg_buf */
1074 if (count
> data
->max_reportid
)
1077 /* Process remaining messages if necessary */
1078 ret
= __mxt_read_reg(data
->client
, data
->T5_address
,
1079 data
->T5_msg_size
* count
, data
->msg_buf
);
1081 dev_err(dev
, "Failed to read %u messages (%d)\n", count
, ret
);
1085 for (i
= 0; i
< count
; i
++) {
1086 ret
= mxt_proc_message(data
,
1087 data
->msg_buf
+ data
->T5_msg_size
* i
);
1093 /* return number of messages read */
1097 static irqreturn_t
mxt_process_messages_t44(struct mxt_data
*data
)
1099 struct device
*dev
= &data
->client
->dev
;
1103 /* Read T44 and T5 together */
1104 ret
= __mxt_read_reg(data
->client
, data
->T44_address
,
1105 data
->T5_msg_size
+ 1, data
->msg_buf
);
1107 dev_err(dev
, "Failed to read T44 and T5 (%d)\n", ret
);
1111 count
= data
->msg_buf
[0];
1114 * This condition may be caused by the CHG line being configured in
1115 * Mode 0. It results in unnecessary I2C operations but it is benign.
1120 if (count
> data
->max_reportid
) {
1121 dev_warn(dev
, "T44 count %d exceeded max report id\n", count
);
1122 count
= data
->max_reportid
;
1125 /* Process first message */
1126 ret
= mxt_proc_message(data
, data
->msg_buf
+ 1);
1128 dev_warn(dev
, "Unexpected invalid message\n");
1132 num_left
= count
- 1;
1134 /* Process remaining messages if necessary */
1136 ret
= mxt_read_and_process_messages(data
, num_left
);
1139 else if (ret
!= num_left
)
1140 dev_warn(dev
, "Unexpected invalid message\n");
1144 if (data
->update_input
) {
1145 mxt_input_sync(data
);
1146 data
->update_input
= false;
1152 static int mxt_process_messages_until_invalid(struct mxt_data
*data
)
1154 struct device
*dev
= &data
->client
->dev
;
1158 count
= data
->max_reportid
;
1160 /* Read messages until we force an invalid */
1162 read
= mxt_read_and_process_messages(data
, count
);
1167 if (data
->update_input
) {
1168 mxt_input_sync(data
);
1169 data
->update_input
= false;
1172 dev_err(dev
, "CHG pin isn't cleared\n");
1176 static irqreturn_t
mxt_process_messages(struct mxt_data
*data
)
1178 int total_handled
, num_handled
;
1179 u8 count
= data
->last_message_count
;
1181 if (count
< 1 || count
> data
->max_reportid
)
1184 /* include final invalid message */
1185 total_handled
= mxt_read_and_process_messages(data
, count
+ 1);
1186 if (total_handled
< 0)
1188 /* if there were invalid messages, then we are done */
1189 else if (total_handled
<= count
)
1192 /* keep reading two msgs until one is invalid or reportid limit */
1194 num_handled
= mxt_read_and_process_messages(data
, 2);
1195 if (num_handled
< 0)
1198 total_handled
+= num_handled
;
1200 if (num_handled
< 2)
1202 } while (total_handled
< data
->num_touchids
);
1205 data
->last_message_count
= total_handled
;
1207 if (data
->update_input
) {
1208 mxt_input_sync(data
);
1209 data
->update_input
= false;
1215 static irqreturn_t
mxt_interrupt(int irq
, void *dev_id
)
1217 struct mxt_data
*data
= dev_id
;
1219 if (data
->in_bootloader
) {
1220 /* bootloader state transition completion */
1221 complete(&data
->bl_completion
);
1225 if (!data
->object_table
)
1228 if (data
->T44_address
) {
1229 return mxt_process_messages_t44(data
);
1231 return mxt_process_messages(data
);
1235 static int mxt_t6_command(struct mxt_data
*data
, u16 cmd_offset
,
1236 u8 value
, bool wait
)
1239 u8 command_register
;
1240 int timeout_counter
= 0;
1243 reg
= data
->T6_address
+ cmd_offset
;
1245 ret
= mxt_write_reg(data
->client
, reg
, value
);
1254 ret
= __mxt_read_reg(data
->client
, reg
, 1, &command_register
);
1257 } while (command_register
!= 0 && timeout_counter
++ <= 100);
1259 if (timeout_counter
> 100) {
1260 dev_err(&data
->client
->dev
, "Command failed!\n");
1267 static int mxt_acquire_irq(struct mxt_data
*data
)
1271 enable_irq(data
->irq
);
1273 if (data
->use_retrigen_workaround
) {
1274 error
= mxt_process_messages_until_invalid(data
);
1282 static int mxt_soft_reset(struct mxt_data
*data
)
1284 struct device
*dev
= &data
->client
->dev
;
1287 dev_info(dev
, "Resetting device\n");
1289 disable_irq(data
->irq
);
1291 reinit_completion(&data
->reset_completion
);
1293 ret
= mxt_t6_command(data
, MXT_COMMAND_RESET
, MXT_RESET_VALUE
, false);
1297 /* Ignore CHG line for 100ms after reset */
1298 msleep(MXT_RESET_INVALID_CHG
);
1300 mxt_acquire_irq(data
);
1302 ret
= mxt_wait_for_completion(data
, &data
->reset_completion
,
1310 static void mxt_update_crc(struct mxt_data
*data
, u8 cmd
, u8 value
)
1313 * On failure, CRC is set to 0 and config will always be
1316 data
->config_crc
= 0;
1317 reinit_completion(&data
->crc_completion
);
1319 mxt_t6_command(data
, cmd
, value
, true);
1322 * Wait for crc message. On failure, CRC is set to 0 and config will
1323 * always be downloaded.
1325 mxt_wait_for_completion(data
, &data
->crc_completion
, MXT_CRC_TIMEOUT
);
1328 static void mxt_calc_crc24(u32
*crc
, u8 firstbyte
, u8 secondbyte
)
1330 static const unsigned int crcpoly
= 0x80001B;
1334 data_word
= (secondbyte
<< 8) | firstbyte
;
1335 result
= ((*crc
<< 1) ^ data_word
);
1337 if (result
& 0x1000000)
1343 static u32
mxt_calculate_crc(u8
*base
, off_t start_off
, off_t end_off
)
1346 u8
*ptr
= base
+ start_off
;
1347 u8
*last_val
= base
+ end_off
- 1;
1349 if (end_off
< start_off
)
1352 while (ptr
< last_val
) {
1353 mxt_calc_crc24(&crc
, *ptr
, *(ptr
+ 1));
1357 /* if len is odd, fill the last byte with 0 */
1358 if (ptr
== last_val
)
1359 mxt_calc_crc24(&crc
, *ptr
, 0);
1361 /* Mask to 24-bit */
1367 static int mxt_check_retrigen(struct mxt_data
*data
)
1369 struct i2c_client
*client
= data
->client
;
1372 struct irq_data
*irqd
;
1374 data
->use_retrigen_workaround
= false;
1376 irqd
= irq_get_irq_data(data
->irq
);
1380 if (irqd_is_level_type(irqd
))
1383 if (data
->T18_address
) {
1384 error
= __mxt_read_reg(client
,
1385 data
->T18_address
+ MXT_COMMS_CTRL
,
1390 if (val
& MXT_COMMS_RETRIGEN
)
1394 dev_warn(&client
->dev
, "Enabling RETRIGEN workaround\n");
1395 data
->use_retrigen_workaround
= true;
1399 static int mxt_prepare_cfg_mem(struct mxt_data
*data
, struct mxt_cfg
*cfg
)
1401 struct device
*dev
= &data
->client
->dev
;
1402 struct mxt_object
*object
;
1403 unsigned int type
, instance
, size
, byte_offset
;
1410 while (cfg
->raw_pos
< cfg
->raw_size
) {
1411 /* Read type, instance, length */
1412 ret
= sscanf(cfg
->raw
+ cfg
->raw_pos
, "%x %x %x%n",
1413 &type
, &instance
, &size
, &offset
);
1417 } else if (ret
!= 3) {
1418 dev_err(dev
, "Bad format: failed to parse object\n");
1421 cfg
->raw_pos
+= offset
;
1423 object
= mxt_get_object(data
, type
);
1426 for (i
= 0; i
< size
; i
++) {
1427 ret
= sscanf(cfg
->raw
+ cfg
->raw_pos
, "%hhx%n",
1430 dev_err(dev
, "Bad format in T%d at %d\n",
1434 cfg
->raw_pos
+= offset
;
1439 if (size
> mxt_obj_size(object
)) {
1441 * Either we are in fallback mode due to wrong
1442 * config or config from a later fw version,
1443 * or the file is corrupt or hand-edited.
1445 dev_warn(dev
, "Discarding %zu byte(s) in T%u\n",
1446 size
- mxt_obj_size(object
), type
);
1447 } else if (mxt_obj_size(object
) > size
) {
1449 * If firmware is upgraded, new bytes may be added to
1450 * end of objects. It is generally forward compatible
1451 * to zero these bytes - previous behaviour will be
1452 * retained. However this does invalidate the CRC and
1453 * will force fallback mode until the configuration is
1454 * updated. We warn here but do nothing else - the
1455 * malloc has zeroed the entire configuration.
1457 dev_warn(dev
, "Zeroing %zu byte(s) in T%d\n",
1458 mxt_obj_size(object
) - size
, type
);
1461 if (instance
>= mxt_obj_instances(object
)) {
1462 dev_err(dev
, "Object instances exceeded!\n");
1466 reg
= object
->start_address
+ mxt_obj_size(object
) * instance
;
1468 for (i
= 0; i
< size
; i
++) {
1469 ret
= sscanf(cfg
->raw
+ cfg
->raw_pos
, "%hhx%n",
1473 dev_err(dev
, "Bad format in T%d at %d\n",
1477 cfg
->raw_pos
+= offset
;
1479 if (i
> mxt_obj_size(object
))
1482 byte_offset
= reg
+ i
- cfg
->start_ofs
;
1484 if (byte_offset
>= 0 && byte_offset
< cfg
->mem_size
) {
1485 *(cfg
->mem
+ byte_offset
) = val
;
1487 dev_err(dev
, "Bad object: reg:%d, T%d, ofs=%d\n",
1488 reg
, object
->type
, byte_offset
);
1497 static int mxt_upload_cfg_mem(struct mxt_data
*data
, struct mxt_cfg
*cfg
)
1499 unsigned int byte_offset
= 0;
1502 /* Write configuration as blocks */
1503 while (byte_offset
< cfg
->mem_size
) {
1504 unsigned int size
= cfg
->mem_size
- byte_offset
;
1506 if (size
> MXT_MAX_BLOCK_WRITE
)
1507 size
= MXT_MAX_BLOCK_WRITE
;
1509 error
= __mxt_write_reg(data
->client
,
1510 cfg
->start_ofs
+ byte_offset
,
1511 size
, cfg
->mem
+ byte_offset
);
1513 dev_err(&data
->client
->dev
,
1514 "Config write error, ret=%d\n", error
);
1518 byte_offset
+= size
;
1524 static int mxt_init_t7_power_cfg(struct mxt_data
*data
);
1527 * mxt_update_cfg - download configuration to chip
1529 * Atmel Raw Config File Format
1531 * The first four lines of the raw config file contain:
1533 * 2) Chip ID Information (first 7 bytes of device memory)
1534 * 3) Chip Information Block 24-bit CRC Checksum
1535 * 4) Chip Configuration 24-bit CRC Checksum
1537 * The rest of the file consists of one line per object instance:
1538 * <TYPE> <INSTANCE> <SIZE> <CONTENTS>
1540 * <TYPE> - 2-byte object type as hex
1541 * <INSTANCE> - 2-byte object instance number as hex
1542 * <SIZE> - 2-byte object size as hex
1543 * <CONTENTS> - array of <SIZE> 1-byte hex values
1545 static int mxt_update_cfg(struct mxt_data
*data
, const struct firmware
*fw
)
1547 struct device
*dev
= &data
->client
->dev
;
1552 u32 info_crc
, config_crc
, calculated_crc
;
1555 /* Make zero terminated copy of the OBP_RAW file */
1556 cfg
.raw
= kmemdup_nul(fw
->data
, fw
->size
, GFP_KERNEL
);
1560 cfg
.raw_size
= fw
->size
;
1562 mxt_update_crc(data
, MXT_COMMAND_REPORTALL
, 1);
1564 if (strncmp(cfg
.raw
, MXT_CFG_MAGIC
, strlen(MXT_CFG_MAGIC
))) {
1565 dev_err(dev
, "Unrecognised config file\n");
1570 cfg
.raw_pos
= strlen(MXT_CFG_MAGIC
);
1572 /* Load information block and check */
1573 for (i
= 0; i
< sizeof(struct mxt_info
); i
++) {
1574 ret
= sscanf(cfg
.raw
+ cfg
.raw_pos
, "%hhx%n",
1575 (unsigned char *)&cfg
.info
+ i
,
1578 dev_err(dev
, "Bad format\n");
1583 cfg
.raw_pos
+= offset
;
1586 if (cfg
.info
.family_id
!= data
->info
->family_id
) {
1587 dev_err(dev
, "Family ID mismatch!\n");
1592 if (cfg
.info
.variant_id
!= data
->info
->variant_id
) {
1593 dev_err(dev
, "Variant ID mismatch!\n");
1599 ret
= sscanf(cfg
.raw
+ cfg
.raw_pos
, "%x%n", &info_crc
, &offset
);
1601 dev_err(dev
, "Bad format: failed to parse Info CRC\n");
1605 cfg
.raw_pos
+= offset
;
1607 ret
= sscanf(cfg
.raw
+ cfg
.raw_pos
, "%x%n", &config_crc
, &offset
);
1609 dev_err(dev
, "Bad format: failed to parse Config CRC\n");
1613 cfg
.raw_pos
+= offset
;
1616 * The Info Block CRC is calculated over mxt_info and the object
1617 * table. If it does not match then we are trying to load the
1618 * configuration from a different chip or firmware version, so
1619 * the configuration CRC is invalid anyway.
1621 if (info_crc
== data
->info_crc
) {
1622 if (config_crc
== 0 || data
->config_crc
== 0) {
1623 dev_info(dev
, "CRC zero, attempting to apply config\n");
1624 } else if (config_crc
== data
->config_crc
) {
1625 dev_dbg(dev
, "Config CRC 0x%06X: OK\n",
1630 dev_info(dev
, "Config CRC 0x%06X: does not match file 0x%06X\n",
1631 data
->config_crc
, config_crc
);
1635 "Warning: Info CRC error - device=0x%06X file=0x%06X\n",
1636 data
->info_crc
, info_crc
);
1639 /* Malloc memory to store configuration */
1640 cfg
.start_ofs
= MXT_OBJECT_START
+
1641 data
->info
->object_num
* sizeof(struct mxt_object
) +
1642 MXT_INFO_CHECKSUM_SIZE
;
1643 cfg
.mem_size
= data
->mem_size
- cfg
.start_ofs
;
1644 cfg
.mem
= kzalloc(cfg
.mem_size
, GFP_KERNEL
);
1650 ret
= mxt_prepare_cfg_mem(data
, &cfg
);
1654 /* Calculate crc of the received configs (not the raw config file) */
1655 if (data
->T71_address
)
1656 crc_start
= data
->T71_address
;
1657 else if (data
->T7_address
)
1658 crc_start
= data
->T7_address
;
1660 dev_warn(dev
, "Could not find CRC start\n");
1662 if (crc_start
> cfg
.start_ofs
) {
1663 calculated_crc
= mxt_calculate_crc(cfg
.mem
,
1664 crc_start
- cfg
.start_ofs
,
1667 if (config_crc
> 0 && config_crc
!= calculated_crc
)
1668 dev_warn(dev
, "Config CRC in file inconsistent, calculated=%06X, file=%06X\n",
1669 calculated_crc
, config_crc
);
1672 ret
= mxt_upload_cfg_mem(data
, &cfg
);
1676 mxt_update_crc(data
, MXT_COMMAND_BACKUPNV
, MXT_BACKUP_VALUE
);
1678 ret
= mxt_check_retrigen(data
);
1682 ret
= mxt_soft_reset(data
);
1686 dev_info(dev
, "Config successfully updated\n");
1688 /* T7 config may have changed */
1689 mxt_init_t7_power_cfg(data
);
1698 static void mxt_free_input_device(struct mxt_data
*data
)
1700 if (data
->input_dev
) {
1701 input_unregister_device(data
->input_dev
);
1702 data
->input_dev
= NULL
;
1706 static void mxt_free_object_table(struct mxt_data
*data
)
1708 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
1709 video_unregister_device(&data
->dbg
.vdev
);
1710 v4l2_device_unregister(&data
->dbg
.v4l2
);
1712 data
->object_table
= NULL
;
1714 kfree(data
->raw_info_block
);
1715 data
->raw_info_block
= NULL
;
1716 kfree(data
->msg_buf
);
1717 data
->msg_buf
= NULL
;
1718 data
->T5_address
= 0;
1719 data
->T5_msg_size
= 0;
1720 data
->T6_reportid
= 0;
1721 data
->T7_address
= 0;
1722 data
->T71_address
= 0;
1723 data
->T9_reportid_min
= 0;
1724 data
->T9_reportid_max
= 0;
1725 data
->T15_reportid_min
= 0;
1726 data
->T15_reportid_max
= 0;
1727 data
->T18_address
= 0;
1728 data
->T19_reportid
= 0;
1729 data
->T44_address
= 0;
1730 data
->T97_reportid_min
= 0;
1731 data
->T97_reportid_max
= 0;
1732 data
->T100_reportid_min
= 0;
1733 data
->T100_reportid_max
= 0;
1734 data
->max_reportid
= 0;
1737 static int mxt_parse_object_table(struct mxt_data
*data
,
1738 struct mxt_object
*object_table
)
1740 struct i2c_client
*client
= data
->client
;
1745 /* Valid Report IDs start counting from 1 */
1748 for (i
= 0; i
< data
->info
->object_num
; i
++) {
1749 struct mxt_object
*object
= object_table
+ i
;
1752 le16_to_cpus(&object
->start_address
);
1754 if (object
->num_report_ids
) {
1756 reportid
+= object
->num_report_ids
*
1757 mxt_obj_instances(object
);
1758 max_id
= reportid
- 1;
1764 dev_dbg(&data
->client
->dev
,
1765 "T%u Start:%u Size:%zu Instances:%zu Report IDs:%u-%u\n",
1766 object
->type
, object
->start_address
,
1767 mxt_obj_size(object
), mxt_obj_instances(object
),
1770 switch (object
->type
) {
1771 case MXT_GEN_MESSAGE_T5
:
1772 if (data
->info
->family_id
== 0x80 &&
1773 data
->info
->version
< 0x20) {
1775 * On mXT224 firmware versions prior to V2.0
1776 * read and discard unused CRC byte otherwise
1777 * DMA reads are misaligned.
1779 data
->T5_msg_size
= mxt_obj_size(object
);
1781 /* CRC not enabled, so skip last byte */
1782 data
->T5_msg_size
= mxt_obj_size(object
) - 1;
1784 data
->T5_address
= object
->start_address
;
1786 case MXT_GEN_COMMAND_T6
:
1787 data
->T6_reportid
= min_id
;
1788 data
->T6_address
= object
->start_address
;
1790 case MXT_GEN_POWER_T7
:
1791 data
->T7_address
= object
->start_address
;
1793 case MXT_SPT_DYNAMICCONFIGURATIONCONTAINER_T71
:
1794 data
->T71_address
= object
->start_address
;
1796 case MXT_TOUCH_MULTI_T9
:
1797 data
->multitouch
= MXT_TOUCH_MULTI_T9
;
1798 /* Only handle messages from first T9 instance */
1799 data
->T9_reportid_min
= min_id
;
1800 data
->T9_reportid_max
= min_id
+
1801 object
->num_report_ids
- 1;
1802 data
->num_touchids
= object
->num_report_ids
;
1804 case MXT_TOUCH_KEYARRAY_T15
:
1805 data
->T15_reportid_min
= min_id
;
1806 data
->T15_reportid_max
= max_id
;
1808 case MXT_SPT_COMMSCONFIG_T18
:
1809 data
->T18_address
= object
->start_address
;
1811 case MXT_SPT_MESSAGECOUNT_T44
:
1812 data
->T44_address
= object
->start_address
;
1814 case MXT_SPT_GPIOPWM_T19
:
1815 data
->T19_reportid
= min_id
;
1817 case MXT_TOUCH_PTC_KEYS_T97
:
1818 data
->T97_reportid_min
= min_id
;
1819 data
->T97_reportid_max
= max_id
;
1821 case MXT_TOUCH_MULTITOUCHSCREEN_T100
:
1822 data
->multitouch
= MXT_TOUCH_MULTITOUCHSCREEN_T100
;
1823 data
->T100_reportid_min
= min_id
;
1824 data
->T100_reportid_max
= max_id
;
1825 /* first two report IDs reserved */
1826 data
->num_touchids
= object
->num_report_ids
- 2;
1830 end_address
= object
->start_address
1831 + mxt_obj_size(object
) * mxt_obj_instances(object
) - 1;
1833 if (end_address
>= data
->mem_size
)
1834 data
->mem_size
= end_address
+ 1;
1837 /* Store maximum reportid */
1838 data
->max_reportid
= reportid
;
1840 /* If T44 exists, T5 position has to be directly after */
1841 if (data
->T44_address
&& (data
->T5_address
!= data
->T44_address
+ 1)) {
1842 dev_err(&client
->dev
, "Invalid T44 position\n");
1846 data
->msg_buf
= kcalloc(data
->max_reportid
,
1847 data
->T5_msg_size
, GFP_KERNEL
);
1854 static int mxt_read_info_block(struct mxt_data
*data
)
1856 struct i2c_client
*client
= data
->client
;
1860 uint8_t num_objects
;
1864 /* If info block already allocated, free it */
1865 if (data
->raw_info_block
)
1866 mxt_free_object_table(data
);
1868 /* Read 7-byte ID information block starting at address 0 */
1869 size
= sizeof(struct mxt_info
);
1870 id_buf
= kzalloc(size
, GFP_KERNEL
);
1874 error
= __mxt_read_reg(client
, 0, size
, id_buf
);
1878 /* Resize buffer to give space for rest of info block */
1879 num_objects
= ((struct mxt_info
*)id_buf
)->object_num
;
1880 size
+= (num_objects
* sizeof(struct mxt_object
))
1881 + MXT_INFO_CHECKSUM_SIZE
;
1883 buf
= krealloc(id_buf
, size
, GFP_KERNEL
);
1890 /* Read rest of info block */
1891 error
= __mxt_read_reg(client
, MXT_OBJECT_START
,
1892 size
- MXT_OBJECT_START
,
1893 id_buf
+ MXT_OBJECT_START
);
1897 /* Extract & calculate checksum */
1898 crc_ptr
= id_buf
+ size
- MXT_INFO_CHECKSUM_SIZE
;
1899 data
->info_crc
= crc_ptr
[0] | (crc_ptr
[1] << 8) | (crc_ptr
[2] << 16);
1901 calculated_crc
= mxt_calculate_crc(id_buf
, 0,
1902 size
- MXT_INFO_CHECKSUM_SIZE
);
1905 * CRC mismatch can be caused by data corruption due to I2C comms
1906 * issue or else device is not using Object Based Protocol (eg i2c-hid)
1908 if ((data
->info_crc
== 0) || (data
->info_crc
!= calculated_crc
)) {
1909 dev_err(&client
->dev
,
1910 "Info Block CRC error calculated=0x%06X read=0x%06X\n",
1911 calculated_crc
, data
->info_crc
);
1916 data
->raw_info_block
= id_buf
;
1917 data
->info
= (struct mxt_info
*)id_buf
;
1919 dev_info(&client
->dev
,
1920 "Family: %u Variant: %u Firmware V%u.%u.%02X Objects: %u\n",
1921 data
->info
->family_id
, data
->info
->variant_id
,
1922 data
->info
->version
>> 4, data
->info
->version
& 0xf,
1923 data
->info
->build
, data
->info
->object_num
);
1925 /* Parse object table information */
1926 error
= mxt_parse_object_table(data
, id_buf
+ MXT_OBJECT_START
);
1928 dev_err(&client
->dev
, "Error %d parsing object table\n", error
);
1929 mxt_free_object_table(data
);
1933 data
->object_table
= (struct mxt_object
*)(id_buf
+ MXT_OBJECT_START
);
1942 static int mxt_read_t9_resolution(struct mxt_data
*data
)
1944 struct i2c_client
*client
= data
->client
;
1946 struct t9_range range
;
1947 unsigned char orient
;
1948 struct mxt_object
*object
;
1950 object
= mxt_get_object(data
, MXT_TOUCH_MULTI_T9
);
1954 error
= __mxt_read_reg(client
,
1955 object
->start_address
+ MXT_T9_XSIZE
,
1956 sizeof(data
->xsize
), &data
->xsize
);
1960 error
= __mxt_read_reg(client
,
1961 object
->start_address
+ MXT_T9_YSIZE
,
1962 sizeof(data
->ysize
), &data
->ysize
);
1966 error
= __mxt_read_reg(client
,
1967 object
->start_address
+ MXT_T9_RANGE
,
1968 sizeof(range
), &range
);
1972 data
->max_x
= get_unaligned_le16(&range
.x
);
1973 data
->max_y
= get_unaligned_le16(&range
.y
);
1975 error
= __mxt_read_reg(client
,
1976 object
->start_address
+ MXT_T9_ORIENT
,
1981 data
->xy_switch
= orient
& MXT_T9_ORIENT_SWITCH
;
1982 data
->invertx
= orient
& MXT_T9_ORIENT_INVERTX
;
1983 data
->inverty
= orient
& MXT_T9_ORIENT_INVERTY
;
1988 static int mxt_read_t100_config(struct mxt_data
*data
)
1990 struct i2c_client
*client
= data
->client
;
1992 struct mxt_object
*object
;
1993 u16 range_x
, range_y
;
1997 object
= mxt_get_object(data
, MXT_TOUCH_MULTITOUCHSCREEN_T100
);
2001 /* read touchscreen dimensions */
2002 error
= __mxt_read_reg(client
,
2003 object
->start_address
+ MXT_T100_XRANGE
,
2004 sizeof(range_x
), &range_x
);
2008 data
->max_x
= get_unaligned_le16(&range_x
);
2010 error
= __mxt_read_reg(client
,
2011 object
->start_address
+ MXT_T100_YRANGE
,
2012 sizeof(range_y
), &range_y
);
2016 data
->max_y
= get_unaligned_le16(&range_y
);
2018 error
= __mxt_read_reg(client
,
2019 object
->start_address
+ MXT_T100_XSIZE
,
2020 sizeof(data
->xsize
), &data
->xsize
);
2024 error
= __mxt_read_reg(client
,
2025 object
->start_address
+ MXT_T100_YSIZE
,
2026 sizeof(data
->ysize
), &data
->ysize
);
2030 /* read orientation config */
2031 error
= __mxt_read_reg(client
,
2032 object
->start_address
+ MXT_T100_CFG1
,
2037 data
->xy_switch
= cfg
& MXT_T100_CFG_SWITCHXY
;
2038 data
->invertx
= cfg
& MXT_T100_CFG_INVERTX
;
2039 data
->inverty
= cfg
& MXT_T100_CFG_INVERTY
;
2041 /* allocate aux bytes */
2042 error
= __mxt_read_reg(client
,
2043 object
->start_address
+ MXT_T100_TCHAUX
,
2050 if (tchaux
& MXT_T100_TCHAUX_VECT
)
2051 data
->t100_aux_vect
= aux
++;
2053 if (tchaux
& MXT_T100_TCHAUX_AMPL
)
2054 data
->t100_aux_ampl
= aux
++;
2056 if (tchaux
& MXT_T100_TCHAUX_AREA
)
2057 data
->t100_aux_area
= aux
++;
2059 dev_dbg(&client
->dev
,
2060 "T100 aux mappings vect:%u ampl:%u area:%u\n",
2061 data
->t100_aux_vect
, data
->t100_aux_ampl
, data
->t100_aux_area
);
2066 static int mxt_input_open(struct input_dev
*dev
);
2067 static void mxt_input_close(struct input_dev
*dev
);
2069 static void mxt_set_up_as_touchpad(struct input_dev
*input_dev
,
2070 struct mxt_data
*data
)
2074 input_dev
->name
= "Atmel maXTouch Touchpad";
2076 __set_bit(INPUT_PROP_BUTTONPAD
, input_dev
->propbit
);
2078 input_abs_set_res(input_dev
, ABS_X
, MXT_PIXELS_PER_MM
);
2079 input_abs_set_res(input_dev
, ABS_Y
, MXT_PIXELS_PER_MM
);
2080 input_abs_set_res(input_dev
, ABS_MT_POSITION_X
,
2082 input_abs_set_res(input_dev
, ABS_MT_POSITION_Y
,
2085 for (i
= 0; i
< data
->t19_num_keys
; i
++)
2086 if (data
->t19_keymap
[i
] != KEY_RESERVED
)
2087 input_set_capability(input_dev
, EV_KEY
,
2088 data
->t19_keymap
[i
]);
2091 static int mxt_initialize_input_device(struct mxt_data
*data
)
2093 struct device
*dev
= &data
->client
->dev
;
2094 struct input_dev
*input_dev
;
2096 unsigned int num_mt_slots
;
2097 unsigned int mt_flags
= 0;
2100 switch (data
->multitouch
) {
2101 case MXT_TOUCH_MULTI_T9
:
2102 num_mt_slots
= data
->T9_reportid_max
- data
->T9_reportid_min
+ 1;
2103 error
= mxt_read_t9_resolution(data
);
2105 dev_warn(dev
, "Failed to initialize T9 resolution\n");
2108 case MXT_TOUCH_MULTITOUCHSCREEN_T100
:
2109 num_mt_slots
= data
->num_touchids
;
2110 error
= mxt_read_t100_config(data
);
2112 dev_warn(dev
, "Failed to read T100 config\n");
2116 dev_err(dev
, "Invalid multitouch object\n");
2120 /* Handle default values and orientation switch */
2121 if (data
->max_x
== 0)
2124 if (data
->max_y
== 0)
2127 if (data
->xy_switch
)
2128 swap(data
->max_x
, data
->max_y
);
2130 dev_info(dev
, "Touchscreen size X%uY%u\n", data
->max_x
, data
->max_y
);
2132 /* Register input device */
2133 input_dev
= input_allocate_device();
2137 input_dev
->name
= "Atmel maXTouch Touchscreen";
2138 input_dev
->phys
= data
->phys
;
2139 input_dev
->id
.bustype
= BUS_I2C
;
2140 input_dev
->dev
.parent
= dev
;
2141 input_dev
->open
= mxt_input_open
;
2142 input_dev
->close
= mxt_input_close
;
2144 input_dev
->keycode
= data
->t15_keymap
;
2145 input_dev
->keycodemax
= data
->t15_num_keys
;
2146 input_dev
->keycodesize
= sizeof(data
->t15_keymap
[0]);
2148 input_set_capability(input_dev
, EV_KEY
, BTN_TOUCH
);
2150 /* For single touch */
2151 input_set_abs_params(input_dev
, ABS_X
, 0, data
->max_x
, 0, 0);
2152 input_set_abs_params(input_dev
, ABS_Y
, 0, data
->max_y
, 0, 0);
2154 if (data
->multitouch
== MXT_TOUCH_MULTI_T9
||
2155 (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
2156 data
->t100_aux_ampl
)) {
2157 input_set_abs_params(input_dev
, ABS_PRESSURE
, 0, 255, 0, 0);
2160 /* If device has buttons we assume it is a touchpad */
2161 if (data
->t19_num_keys
) {
2162 mxt_set_up_as_touchpad(input_dev
, data
);
2163 mt_flags
|= INPUT_MT_POINTER
;
2165 mt_flags
|= INPUT_MT_DIRECT
;
2168 /* For multi touch */
2169 error
= input_mt_init_slots(input_dev
, num_mt_slots
, mt_flags
);
2171 dev_err(dev
, "Error %d initialising slots\n", error
);
2175 if (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
) {
2176 input_set_abs_params(input_dev
, ABS_MT_TOOL_TYPE
,
2177 0, MT_TOOL_MAX
, 0, 0);
2178 input_set_abs_params(input_dev
, ABS_MT_DISTANCE
,
2179 MXT_DISTANCE_ACTIVE_TOUCH
,
2180 MXT_DISTANCE_HOVERING
,
2184 input_set_abs_params(input_dev
, ABS_MT_POSITION_X
,
2185 0, data
->max_x
, 0, 0);
2186 input_set_abs_params(input_dev
, ABS_MT_POSITION_Y
,
2187 0, data
->max_y
, 0, 0);
2189 if (data
->multitouch
== MXT_TOUCH_MULTI_T9
||
2190 (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
2191 data
->t100_aux_area
)) {
2192 input_set_abs_params(input_dev
, ABS_MT_TOUCH_MAJOR
,
2193 0, MXT_MAX_AREA
, 0, 0);
2196 if (data
->multitouch
== MXT_TOUCH_MULTI_T9
||
2197 (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
2198 data
->t100_aux_ampl
)) {
2199 input_set_abs_params(input_dev
, ABS_MT_PRESSURE
,
2203 if (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
2204 data
->t100_aux_vect
) {
2205 input_set_abs_params(input_dev
, ABS_MT_ORIENTATION
,
2209 if (data
->multitouch
== MXT_TOUCH_MULTITOUCHSCREEN_T100
&&
2210 data
->t100_aux_vect
) {
2211 input_set_abs_params(input_dev
, ABS_MT_ORIENTATION
,
2215 /* For T15 and T97 Key Array */
2216 if (data
->T15_reportid_min
|| data
->T97_reportid_min
) {
2217 for (i
= 0; i
< data
->t15_num_keys
; i
++)
2218 input_set_capability(input_dev
,
2219 EV_KEY
, data
->t15_keymap
[i
]);
2222 input_set_drvdata(input_dev
, data
);
2224 error
= input_register_device(input_dev
);
2226 dev_err(dev
, "Error %d registering input device\n", error
);
2230 data
->input_dev
= input_dev
;
2235 input_free_device(input_dev
);
2239 static int mxt_configure_objects(struct mxt_data
*data
,
2240 const struct firmware
*cfg
);
2242 static void mxt_config_cb(const struct firmware
*cfg
, void *ctx
)
2244 mxt_configure_objects(ctx
, cfg
);
2245 release_firmware(cfg
);
2248 static int mxt_initialize(struct mxt_data
*data
)
2250 struct i2c_client
*client
= data
->client
;
2251 int recovery_attempts
= 0;
2255 error
= mxt_read_info_block(data
);
2259 /* Check bootloader state */
2260 error
= mxt_probe_bootloader(data
, false);
2262 dev_info(&client
->dev
, "Trying alternate bootloader address\n");
2263 error
= mxt_probe_bootloader(data
, true);
2265 /* Chip is not in appmode or bootloader mode */
2270 /* OK, we are in bootloader, see if we can recover */
2271 if (++recovery_attempts
> 1) {
2272 dev_err(&client
->dev
, "Could not recover from bootloader mode\n");
2274 * We can reflash from this state, so do not
2275 * abort initialization.
2277 data
->in_bootloader
= true;
2281 /* Attempt to exit bootloader into app mode */
2282 mxt_send_bootloader_cmd(data
, false);
2283 msleep(MXT_FW_RESET_TIME
);
2286 error
= mxt_check_retrigen(data
);
2290 error
= mxt_acquire_irq(data
);
2294 error
= request_firmware_nowait(THIS_MODULE
, true, MXT_CFG_NAME
,
2295 &client
->dev
, GFP_KERNEL
, data
,
2298 dev_err(&client
->dev
, "Failed to invoke firmware loader: %d\n",
2306 static int mxt_set_t7_power_cfg(struct mxt_data
*data
, u8 sleep
)
2308 struct device
*dev
= &data
->client
->dev
;
2310 struct t7_config
*new_config
;
2311 struct t7_config deepsleep
= { .active
= 0, .idle
= 0 };
2313 if (sleep
== MXT_POWER_CFG_DEEPSLEEP
)
2314 new_config
= &deepsleep
;
2316 new_config
= &data
->t7_cfg
;
2318 error
= __mxt_write_reg(data
->client
, data
->T7_address
,
2319 sizeof(data
->t7_cfg
), new_config
);
2323 dev_dbg(dev
, "Set T7 ACTV:%d IDLE:%d\n",
2324 new_config
->active
, new_config
->idle
);
2329 static int mxt_init_t7_power_cfg(struct mxt_data
*data
)
2331 struct device
*dev
= &data
->client
->dev
;
2336 error
= __mxt_read_reg(data
->client
, data
->T7_address
,
2337 sizeof(data
->t7_cfg
), &data
->t7_cfg
);
2341 if (data
->t7_cfg
.active
== 0 || data
->t7_cfg
.idle
== 0) {
2343 dev_dbg(dev
, "T7 cfg zero, resetting\n");
2344 mxt_soft_reset(data
);
2348 dev_dbg(dev
, "T7 cfg zero after reset, overriding\n");
2349 data
->t7_cfg
.active
= 20;
2350 data
->t7_cfg
.idle
= 100;
2351 return mxt_set_t7_power_cfg(data
, MXT_POWER_CFG_RUN
);
2355 dev_dbg(dev
, "Initialized power cfg: ACTV %d, IDLE %d\n",
2356 data
->t7_cfg
.active
, data
->t7_cfg
.idle
);
2360 #ifdef CONFIG_TOUCHSCREEN_ATMEL_MXT_T37
2361 static const struct v4l2_file_operations mxt_video_fops
= {
2362 .owner
= THIS_MODULE
,
2363 .open
= v4l2_fh_open
,
2364 .release
= vb2_fop_release
,
2365 .unlocked_ioctl
= video_ioctl2
,
2366 .read
= vb2_fop_read
,
2367 .mmap
= vb2_fop_mmap
,
2368 .poll
= vb2_fop_poll
,
2371 static u16
mxt_get_debug_value(struct mxt_data
*data
, unsigned int x
,
2374 struct mxt_info
*info
= data
->info
;
2375 struct mxt_dbg
*dbg
= &data
->dbg
;
2376 unsigned int ofs
, page
;
2377 unsigned int col
= 0;
2378 unsigned int col_width
;
2380 if (info
->family_id
== MXT_FAMILY_1386
) {
2381 col_width
= info
->matrix_ysize
/ MXT1386_COLUMNS
;
2382 col
= y
/ col_width
;
2385 col_width
= info
->matrix_ysize
;
2388 ofs
= (y
+ (x
* col_width
)) * sizeof(u16
);
2389 page
= ofs
/ MXT_DIAGNOSTIC_SIZE
;
2390 ofs
%= MXT_DIAGNOSTIC_SIZE
;
2392 if (info
->family_id
== MXT_FAMILY_1386
)
2393 page
+= col
* MXT1386_PAGES_PER_COLUMN
;
2395 return get_unaligned_le16(&dbg
->t37_buf
[page
].data
[ofs
]);
2398 static int mxt_convert_debug_pages(struct mxt_data
*data
, u16
*outbuf
)
2400 struct mxt_dbg
*dbg
= &data
->dbg
;
2403 unsigned int i
, rx
, ry
;
2405 for (i
= 0; i
< dbg
->t37_nodes
; i
++) {
2406 /* Handle orientation */
2407 rx
= data
->xy_switch
? y
: x
;
2408 ry
= data
->xy_switch
? x
: y
;
2409 rx
= data
->invertx
? (data
->xsize
- 1 - rx
) : rx
;
2410 ry
= data
->inverty
? (data
->ysize
- 1 - ry
) : ry
;
2412 outbuf
[i
] = mxt_get_debug_value(data
, rx
, ry
);
2415 if (++x
>= (data
->xy_switch
? data
->ysize
: data
->xsize
)) {
2424 static int mxt_read_diagnostic_debug(struct mxt_data
*data
, u8 mode
,
2427 struct mxt_dbg
*dbg
= &data
->dbg
;
2432 struct t37_debug
*p
;
2435 for (page
= 0; page
< dbg
->t37_pages
; page
++) {
2436 p
= dbg
->t37_buf
+ page
;
2438 ret
= mxt_write_reg(data
->client
, dbg
->diag_cmd_address
,
2446 /* Read back command byte */
2447 ret
= __mxt_read_reg(data
->client
, dbg
->diag_cmd_address
,
2448 sizeof(cmd_poll
), &cmd_poll
);
2452 /* Field is cleared once the command has been processed */
2454 if (retries
++ > 100)
2462 ret
= __mxt_read_reg(data
->client
, dbg
->t37_address
,
2463 sizeof(struct t37_debug
), p
);
2467 if (p
->mode
!= mode
|| p
->page
!= page
) {
2468 dev_err(&data
->client
->dev
, "T37 page mismatch\n");
2472 dev_dbg(&data
->client
->dev
, "%s page:%d retries:%d\n",
2473 __func__
, page
, retries
);
2475 /* For remaining pages, write PAGEUP rather than mode */
2476 cmd
= MXT_DIAGNOSTIC_PAGEUP
;
2479 return mxt_convert_debug_pages(data
, outbuf
);
2482 static int mxt_queue_setup(struct vb2_queue
*q
,
2483 unsigned int *nbuffers
, unsigned int *nplanes
,
2484 unsigned int sizes
[], struct device
*alloc_devs
[])
2486 struct mxt_data
*data
= q
->drv_priv
;
2487 size_t size
= data
->dbg
.t37_nodes
* sizeof(u16
);
2490 return sizes
[0] < size
? -EINVAL
: 0;
2498 static void mxt_buffer_queue(struct vb2_buffer
*vb
)
2500 struct mxt_data
*data
= vb2_get_drv_priv(vb
->vb2_queue
);
2505 ptr
= vb2_plane_vaddr(vb
, 0);
2507 dev_err(&data
->client
->dev
, "Error acquiring frame ptr\n");
2511 switch (data
->dbg
.input
) {
2512 case MXT_V4L_INPUT_DELTAS
:
2514 mode
= MXT_DIAGNOSTIC_DELTAS
;
2517 case MXT_V4L_INPUT_REFS
:
2518 mode
= MXT_DIAGNOSTIC_REFS
;
2522 ret
= mxt_read_diagnostic_debug(data
, mode
, ptr
);
2526 vb2_set_plane_payload(vb
, 0, data
->dbg
.t37_nodes
* sizeof(u16
));
2527 vb2_buffer_done(vb
, VB2_BUF_STATE_DONE
);
2531 vb2_buffer_done(vb
, VB2_BUF_STATE_ERROR
);
2534 /* V4L2 structures */
2535 static const struct vb2_ops mxt_queue_ops
= {
2536 .queue_setup
= mxt_queue_setup
,
2537 .buf_queue
= mxt_buffer_queue
,
2538 .wait_prepare
= vb2_ops_wait_prepare
,
2539 .wait_finish
= vb2_ops_wait_finish
,
2542 static const struct vb2_queue mxt_queue
= {
2543 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
2544 .io_modes
= VB2_MMAP
| VB2_USERPTR
| VB2_DMABUF
| VB2_READ
,
2545 .buf_struct_size
= sizeof(struct mxt_vb2_buffer
),
2546 .ops
= &mxt_queue_ops
,
2547 .mem_ops
= &vb2_vmalloc_memops
,
2548 .timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
,
2549 .min_queued_buffers
= 1,
2552 static int mxt_vidioc_querycap(struct file
*file
, void *priv
,
2553 struct v4l2_capability
*cap
)
2555 struct mxt_data
*data
= video_drvdata(file
);
2557 strscpy(cap
->driver
, "atmel_mxt_ts", sizeof(cap
->driver
));
2558 strscpy(cap
->card
, "atmel_mxt_ts touch", sizeof(cap
->card
));
2559 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
2560 "I2C:%s", dev_name(&data
->client
->dev
));
2564 static int mxt_vidioc_enum_input(struct file
*file
, void *priv
,
2565 struct v4l2_input
*i
)
2567 if (i
->index
>= MXT_V4L_INPUT_MAX
)
2570 i
->type
= V4L2_INPUT_TYPE_TOUCH
;
2573 case MXT_V4L_INPUT_REFS
:
2574 strscpy(i
->name
, "Mutual Capacitance References",
2577 case MXT_V4L_INPUT_DELTAS
:
2578 strscpy(i
->name
, "Mutual Capacitance Deltas", sizeof(i
->name
));
2585 static int mxt_set_input(struct mxt_data
*data
, unsigned int i
)
2587 struct v4l2_pix_format
*f
= &data
->dbg
.format
;
2589 if (i
>= MXT_V4L_INPUT_MAX
)
2592 if (i
== MXT_V4L_INPUT_DELTAS
)
2593 f
->pixelformat
= V4L2_TCH_FMT_DELTA_TD16
;
2595 f
->pixelformat
= V4L2_TCH_FMT_TU16
;
2597 f
->width
= data
->xy_switch
? data
->ysize
: data
->xsize
;
2598 f
->height
= data
->xy_switch
? data
->xsize
: data
->ysize
;
2599 f
->field
= V4L2_FIELD_NONE
;
2600 f
->colorspace
= V4L2_COLORSPACE_RAW
;
2601 f
->bytesperline
= f
->width
* sizeof(u16
);
2602 f
->sizeimage
= f
->width
* f
->height
* sizeof(u16
);
2604 data
->dbg
.input
= i
;
2609 static int mxt_vidioc_s_input(struct file
*file
, void *priv
, unsigned int i
)
2611 return mxt_set_input(video_drvdata(file
), i
);
2614 static int mxt_vidioc_g_input(struct file
*file
, void *priv
, unsigned int *i
)
2616 struct mxt_data
*data
= video_drvdata(file
);
2618 *i
= data
->dbg
.input
;
2623 static int mxt_vidioc_fmt(struct file
*file
, void *priv
, struct v4l2_format
*f
)
2625 struct mxt_data
*data
= video_drvdata(file
);
2627 f
->type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
2628 f
->fmt
.pix
= data
->dbg
.format
;
2633 static int mxt_vidioc_enum_fmt(struct file
*file
, void *priv
,
2634 struct v4l2_fmtdesc
*fmt
)
2636 if (fmt
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2639 switch (fmt
->index
) {
2641 fmt
->pixelformat
= V4L2_TCH_FMT_TU16
;
2645 fmt
->pixelformat
= V4L2_TCH_FMT_DELTA_TD16
;
2655 static int mxt_vidioc_g_parm(struct file
*file
, void *fh
,
2656 struct v4l2_streamparm
*a
)
2658 if (a
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
2661 a
->parm
.capture
.readbuffers
= 1;
2662 a
->parm
.capture
.timeperframe
.numerator
= 1;
2663 a
->parm
.capture
.timeperframe
.denominator
= 10;
2667 static const struct v4l2_ioctl_ops mxt_video_ioctl_ops
= {
2668 .vidioc_querycap
= mxt_vidioc_querycap
,
2670 .vidioc_enum_fmt_vid_cap
= mxt_vidioc_enum_fmt
,
2671 .vidioc_s_fmt_vid_cap
= mxt_vidioc_fmt
,
2672 .vidioc_g_fmt_vid_cap
= mxt_vidioc_fmt
,
2673 .vidioc_try_fmt_vid_cap
= mxt_vidioc_fmt
,
2674 .vidioc_g_parm
= mxt_vidioc_g_parm
,
2676 .vidioc_enum_input
= mxt_vidioc_enum_input
,
2677 .vidioc_g_input
= mxt_vidioc_g_input
,
2678 .vidioc_s_input
= mxt_vidioc_s_input
,
2680 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
2681 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
2682 .vidioc_querybuf
= vb2_ioctl_querybuf
,
2683 .vidioc_qbuf
= vb2_ioctl_qbuf
,
2684 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
2685 .vidioc_expbuf
= vb2_ioctl_expbuf
,
2687 .vidioc_streamon
= vb2_ioctl_streamon
,
2688 .vidioc_streamoff
= vb2_ioctl_streamoff
,
2691 static const struct video_device mxt_video_device
= {
2692 .name
= "Atmel maxTouch",
2693 .fops
= &mxt_video_fops
,
2694 .ioctl_ops
= &mxt_video_ioctl_ops
,
2695 .release
= video_device_release_empty
,
2696 .device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_TOUCH
|
2697 V4L2_CAP_READWRITE
| V4L2_CAP_STREAMING
,
2700 static void mxt_debug_init(struct mxt_data
*data
)
2702 struct mxt_info
*info
= data
->info
;
2703 struct mxt_dbg
*dbg
= &data
->dbg
;
2704 struct mxt_object
*object
;
2707 object
= mxt_get_object(data
, MXT_GEN_COMMAND_T6
);
2711 dbg
->diag_cmd_address
= object
->start_address
+ MXT_COMMAND_DIAGNOSTIC
;
2713 object
= mxt_get_object(data
, MXT_DEBUG_DIAGNOSTIC_T37
);
2717 if (mxt_obj_size(object
) != sizeof(struct t37_debug
)) {
2718 dev_warn(&data
->client
->dev
, "Bad T37 size");
2722 dbg
->t37_address
= object
->start_address
;
2724 /* Calculate size of data and allocate buffer */
2725 dbg
->t37_nodes
= data
->xsize
* data
->ysize
;
2727 if (info
->family_id
== MXT_FAMILY_1386
)
2728 dbg
->t37_pages
= MXT1386_COLUMNS
* MXT1386_PAGES_PER_COLUMN
;
2730 dbg
->t37_pages
= DIV_ROUND_UP(data
->xsize
*
2731 info
->matrix_ysize
*
2733 sizeof(dbg
->t37_buf
->data
));
2735 dbg
->t37_buf
= devm_kmalloc_array(&data
->client
->dev
, dbg
->t37_pages
,
2736 sizeof(struct t37_debug
), GFP_KERNEL
);
2740 /* init channel to zero */
2741 mxt_set_input(data
, 0);
2743 /* register video device */
2744 snprintf(dbg
->v4l2
.name
, sizeof(dbg
->v4l2
.name
), "%s", "atmel_mxt_ts");
2745 error
= v4l2_device_register(&data
->client
->dev
, &dbg
->v4l2
);
2749 /* initialize the queue */
2750 mutex_init(&dbg
->lock
);
2751 dbg
->queue
= mxt_queue
;
2752 dbg
->queue
.drv_priv
= data
;
2753 dbg
->queue
.lock
= &dbg
->lock
;
2754 dbg
->queue
.dev
= &data
->client
->dev
;
2756 error
= vb2_queue_init(&dbg
->queue
);
2758 goto error_unreg_v4l2
;
2760 dbg
->vdev
= mxt_video_device
;
2761 dbg
->vdev
.v4l2_dev
= &dbg
->v4l2
;
2762 dbg
->vdev
.lock
= &dbg
->lock
;
2763 dbg
->vdev
.vfl_dir
= VFL_DIR_RX
;
2764 dbg
->vdev
.queue
= &dbg
->queue
;
2765 video_set_drvdata(&dbg
->vdev
, data
);
2767 error
= video_register_device(&dbg
->vdev
, VFL_TYPE_TOUCH
, -1);
2769 goto error_unreg_v4l2
;
2774 v4l2_device_unregister(&dbg
->v4l2
);
2776 dev_warn(&data
->client
->dev
, "Error initializing T37\n");
2779 static void mxt_debug_init(struct mxt_data
*data
)
2784 static int mxt_configure_objects(struct mxt_data
*data
,
2785 const struct firmware
*cfg
)
2787 struct device
*dev
= &data
->client
->dev
;
2790 error
= mxt_init_t7_power_cfg(data
);
2792 dev_err(dev
, "Failed to initialize power cfg\n");
2797 error
= mxt_update_cfg(data
, cfg
);
2799 dev_warn(dev
, "Error %d updating config\n", error
);
2802 if (data
->multitouch
) {
2803 error
= mxt_initialize_input_device(data
);
2807 dev_warn(dev
, "No touch object detected\n");
2810 mxt_debug_init(data
);
2815 /* Firmware Version is returned as Major.Minor.Build */
2816 static ssize_t
mxt_fw_version_show(struct device
*dev
,
2817 struct device_attribute
*attr
, char *buf
)
2819 struct mxt_data
*data
= dev_get_drvdata(dev
);
2820 struct mxt_info
*info
= data
->info
;
2821 return sysfs_emit(buf
, "%u.%u.%02X\n",
2822 info
->version
>> 4, info
->version
& 0xf, info
->build
);
2825 /* Hardware Version is returned as FamilyID.VariantID */
2826 static ssize_t
mxt_hw_version_show(struct device
*dev
,
2827 struct device_attribute
*attr
, char *buf
)
2829 struct mxt_data
*data
= dev_get_drvdata(dev
);
2830 struct mxt_info
*info
= data
->info
;
2831 return sysfs_emit(buf
, "%u.%u\n", info
->family_id
, info
->variant_id
);
2834 static ssize_t
mxt_show_instance(char *buf
, int count
,
2835 struct mxt_object
*object
, int instance
,
2840 if (mxt_obj_instances(object
) > 1)
2841 count
+= sysfs_emit_at(buf
, count
, "Instance %u\n", instance
);
2843 for (i
= 0; i
< mxt_obj_size(object
); i
++)
2844 count
+= sysfs_emit_at(buf
, count
, "\t[%2u]: %02x (%d)\n",
2846 count
+= sysfs_emit_at(buf
, count
, "\n");
2851 static ssize_t
mxt_object_show(struct device
*dev
,
2852 struct device_attribute
*attr
, char *buf
)
2854 struct mxt_data
*data
= dev_get_drvdata(dev
);
2855 struct mxt_object
*object
;
2861 /* Pre-allocate buffer large enough to hold max sized object. */
2862 obuf
= kmalloc(256, GFP_KERNEL
);
2867 for (i
= 0; i
< data
->info
->object_num
; i
++) {
2868 object
= data
->object_table
+ i
;
2870 if (!mxt_object_readable(object
->type
))
2873 count
+= sysfs_emit_at(buf
, count
, "T%u:\n", object
->type
);
2875 for (j
= 0; j
< mxt_obj_instances(object
); j
++) {
2876 u16 size
= mxt_obj_size(object
);
2877 u16 addr
= object
->start_address
+ j
* size
;
2879 error
= __mxt_read_reg(data
->client
, addr
, size
, obuf
);
2883 count
= mxt_show_instance(buf
, count
, object
, j
, obuf
);
2889 return error
?: count
;
2892 static int mxt_check_firmware_format(struct device
*dev
,
2893 const struct firmware
*fw
)
2895 unsigned int pos
= 0;
2898 while (pos
< fw
->size
) {
2899 c
= *(fw
->data
+ pos
);
2901 if (c
< '0' || (c
> '9' && c
< 'A') || c
> 'F')
2908 * To convert file try:
2909 * xxd -r -p mXTXXX__APP_VX-X-XX.enc > maxtouch.fw
2911 dev_err(dev
, "Aborting: firmware file must be in binary format\n");
2916 static int mxt_load_fw(struct device
*dev
, const char *fn
)
2918 struct mxt_data
*data
= dev_get_drvdata(dev
);
2919 const struct firmware
*fw
= NULL
;
2920 unsigned int frame_size
;
2921 unsigned int pos
= 0;
2922 unsigned int retry
= 0;
2923 unsigned int frame
= 0;
2926 ret
= request_firmware(&fw
, fn
, dev
);
2928 dev_err(dev
, "Unable to open firmware %s\n", fn
);
2932 /* Check for incorrect enc file */
2933 ret
= mxt_check_firmware_format(dev
, fw
);
2935 goto release_firmware
;
2937 if (!data
->in_bootloader
) {
2938 /* Change to the bootloader mode */
2939 data
->in_bootloader
= true;
2941 ret
= mxt_t6_command(data
, MXT_COMMAND_RESET
,
2942 MXT_BOOT_VALUE
, false);
2944 goto release_firmware
;
2946 msleep(MXT_RESET_TIME
);
2948 /* Do not need to scan since we know family ID */
2949 ret
= mxt_lookup_bootloader_address(data
, 0);
2951 goto release_firmware
;
2953 mxt_free_input_device(data
);
2954 mxt_free_object_table(data
);
2956 enable_irq(data
->irq
);
2959 reinit_completion(&data
->bl_completion
);
2961 ret
= mxt_check_bootloader(data
, MXT_WAITING_BOOTLOAD_CMD
, false);
2963 /* Bootloader may still be unlocked from previous attempt */
2964 ret
= mxt_check_bootloader(data
, MXT_WAITING_FRAME_DATA
, false);
2968 dev_info(dev
, "Unlocking bootloader\n");
2970 /* Unlock bootloader */
2971 ret
= mxt_send_bootloader_cmd(data
, true);
2976 while (pos
< fw
->size
) {
2977 ret
= mxt_check_bootloader(data
, MXT_WAITING_FRAME_DATA
, true);
2981 frame_size
= ((*(fw
->data
+ pos
) << 8) | *(fw
->data
+ pos
+ 1));
2983 /* Take account of CRC bytes */
2986 /* Write one frame to device */
2987 ret
= mxt_bootloader_write(data
, fw
->data
+ pos
, frame_size
);
2991 ret
= mxt_check_bootloader(data
, MXT_FRAME_CRC_PASS
, true);
2995 /* Back off by 20ms per retry */
2999 dev_err(dev
, "Retry count exceeded\n");
3008 if (frame
% 50 == 0)
3009 dev_dbg(dev
, "Sent %d frames, %d/%zd bytes\n",
3010 frame
, pos
, fw
->size
);
3013 /* Wait for flash. */
3014 ret
= mxt_wait_for_completion(data
, &data
->bl_completion
,
3019 dev_dbg(dev
, "Sent %d frames, %d bytes\n", frame
, pos
);
3022 * Wait for device to reset. Some bootloader versions do not assert
3023 * the CHG line after bootloading has finished, so ignore potential
3026 mxt_wait_for_completion(data
, &data
->bl_completion
, MXT_FW_RESET_TIME
);
3028 data
->in_bootloader
= false;
3031 disable_irq(data
->irq
);
3033 release_firmware(fw
);
3037 static ssize_t
mxt_update_fw_store(struct device
*dev
,
3038 struct device_attribute
*attr
,
3039 const char *buf
, size_t count
)
3041 struct mxt_data
*data
= dev_get_drvdata(dev
);
3044 error
= mxt_load_fw(dev
, MXT_FW_NAME
);
3046 dev_err(dev
, "The firmware update failed(%d)\n", error
);
3049 dev_info(dev
, "The firmware update succeeded\n");
3051 error
= mxt_initialize(data
);
3059 static DEVICE_ATTR(fw_version
, S_IRUGO
, mxt_fw_version_show
, NULL
);
3060 static DEVICE_ATTR(hw_version
, S_IRUGO
, mxt_hw_version_show
, NULL
);
3061 static DEVICE_ATTR(object
, S_IRUGO
, mxt_object_show
, NULL
);
3062 static DEVICE_ATTR(update_fw
, S_IWUSR
, NULL
, mxt_update_fw_store
);
3064 static struct attribute
*mxt_attrs
[] = {
3065 &dev_attr_fw_version
.attr
,
3066 &dev_attr_hw_version
.attr
,
3067 &dev_attr_object
.attr
,
3068 &dev_attr_update_fw
.attr
,
3072 ATTRIBUTE_GROUPS(mxt
);
3074 static void mxt_start(struct mxt_data
*data
)
3076 mxt_wakeup_toggle(data
->client
, true, false);
3078 switch (data
->suspend_mode
) {
3079 case MXT_SUSPEND_T9_CTRL
:
3080 mxt_soft_reset(data
);
3083 /* 0x83 = SCANEN | RPTEN | ENABLE */
3084 mxt_write_object(data
,
3085 MXT_TOUCH_MULTI_T9
, MXT_T9_CTRL
, 0x83);
3088 case MXT_SUSPEND_DEEP_SLEEP
:
3090 mxt_set_t7_power_cfg(data
, MXT_POWER_CFG_RUN
);
3092 /* Recalibrate since chip has been in deep sleep */
3093 mxt_t6_command(data
, MXT_COMMAND_CALIBRATE
, 1, false);
3098 static void mxt_stop(struct mxt_data
*data
)
3100 switch (data
->suspend_mode
) {
3101 case MXT_SUSPEND_T9_CTRL
:
3103 mxt_write_object(data
,
3104 MXT_TOUCH_MULTI_T9
, MXT_T9_CTRL
, 0);
3107 case MXT_SUSPEND_DEEP_SLEEP
:
3109 mxt_set_t7_power_cfg(data
, MXT_POWER_CFG_DEEPSLEEP
);
3113 mxt_wakeup_toggle(data
->client
, false, false);
3116 static int mxt_input_open(struct input_dev
*dev
)
3118 struct mxt_data
*data
= input_get_drvdata(dev
);
3125 static void mxt_input_close(struct input_dev
*dev
)
3127 struct mxt_data
*data
= input_get_drvdata(dev
);
3132 static int mxt_parse_device_properties(struct mxt_data
*data
)
3134 static const char keymap_property
[] = "linux,gpio-keymap";
3135 static const char buttons_property
[] = "linux,keycodes";
3136 struct device
*dev
= &data
->client
->dev
;
3142 if (device_property_present(dev
, keymap_property
)) {
3143 n_keys
= device_property_count_u32(dev
, keymap_property
);
3145 error
= n_keys
< 0 ? n_keys
: -EINVAL
;
3146 dev_err(dev
, "invalid/malformed '%s' property: %d\n",
3147 keymap_property
, error
);
3151 keymap
= devm_kmalloc_array(dev
, n_keys
, sizeof(*keymap
),
3156 error
= device_property_read_u32_array(dev
, keymap_property
,
3159 dev_err(dev
, "failed to parse '%s' property: %d\n",
3160 keymap_property
, error
);
3164 data
->t19_keymap
= keymap
;
3165 data
->t19_num_keys
= n_keys
;
3168 if (device_property_present(dev
, buttons_property
)) {
3169 n_keys
= device_property_count_u32(dev
, buttons_property
);
3171 error
= n_keys
< 0 ? n_keys
: -EINVAL
;
3172 dev_err(dev
, "invalid/malformed '%s' property: %d\n",
3173 buttons_property
, error
);
3177 buttonmap
= devm_kmalloc_array(dev
, n_keys
, sizeof(*buttonmap
),
3182 error
= device_property_read_u32_array(dev
, buttons_property
,
3185 dev_err(dev
, "failed to parse '%s' property: %d\n",
3186 buttons_property
, error
);
3190 data
->t15_keymap
= buttonmap
;
3191 data
->t15_num_keys
= n_keys
;
3197 static const struct dmi_system_id chromebook_T9_suspend_dmi
[] = {
3200 DMI_MATCH(DMI_SYS_VENDOR
, "GOOGLE"),
3201 DMI_MATCH(DMI_PRODUCT_NAME
, "Link"),
3206 DMI_MATCH(DMI_PRODUCT_NAME
, "Peppy"),
3212 static int mxt_probe(struct i2c_client
*client
)
3214 struct mxt_data
*data
;
3218 * Ignore devices that do not have device properties attached to
3219 * them, as we need help determining whether we are dealing with
3220 * touch screen or touchpad.
3222 * So far on x86 the only users of Atmel touch controllers are
3223 * Chromebooks, and chromeos_laptop driver will ensure that
3224 * necessary properties are provided (if firmware does not do that).
3226 if (!device_property_present(&client
->dev
, "compatible"))
3230 * Ignore ACPI devices representing bootloader mode.
3232 * This is a bit of a hack: Google Chromebook BIOS creates ACPI
3233 * devices for both application and bootloader modes, but we are
3234 * interested in application mode only (if device is in bootloader
3235 * mode we'll end up switching into application anyway). So far
3236 * application mode addresses were all above 0x40, so we'll use it
3239 if (ACPI_COMPANION(&client
->dev
) && client
->addr
< 0x40)
3242 data
= devm_kzalloc(&client
->dev
, sizeof(struct mxt_data
), GFP_KERNEL
);
3246 snprintf(data
->phys
, sizeof(data
->phys
), "i2c-%u-%04x/input0",
3247 client
->adapter
->nr
, client
->addr
);
3249 data
->client
= client
;
3250 data
->irq
= client
->irq
;
3251 i2c_set_clientdata(client
, data
);
3253 init_completion(&data
->bl_completion
);
3254 init_completion(&data
->reset_completion
);
3255 init_completion(&data
->crc_completion
);
3257 data
->suspend_mode
= dmi_check_system(chromebook_T9_suspend_dmi
) ?
3258 MXT_SUSPEND_T9_CTRL
: MXT_SUSPEND_DEEP_SLEEP
;
3260 error
= mxt_parse_device_properties(data
);
3265 * VDDA is the analog voltage supply 2.57..3.47 V
3266 * VDD is the digital voltage supply 1.71..3.47 V
3268 data
->regulators
[0].supply
= "vdda";
3269 data
->regulators
[1].supply
= "vdd";
3270 error
= devm_regulator_bulk_get(&client
->dev
, ARRAY_SIZE(data
->regulators
),
3273 if (error
!= -EPROBE_DEFER
)
3274 dev_err(&client
->dev
, "Failed to get regulators %d\n",
3279 /* Request the RESET line as asserted so we go into reset */
3280 data
->reset_gpio
= devm_gpiod_get_optional(&client
->dev
,
3281 "reset", GPIOD_OUT_HIGH
);
3282 if (IS_ERR(data
->reset_gpio
)) {
3283 error
= PTR_ERR(data
->reset_gpio
);
3284 dev_err(&client
->dev
, "Failed to get reset gpio: %d\n", error
);
3288 /* Request the WAKE line as asserted so we go out of sleep */
3289 data
->wake_gpio
= devm_gpiod_get_optional(&client
->dev
,
3290 "wake", GPIOD_OUT_HIGH
);
3291 if (IS_ERR(data
->wake_gpio
)) {
3292 error
= PTR_ERR(data
->wake_gpio
);
3293 dev_err(&client
->dev
, "Failed to get wake gpio: %d\n", error
);
3297 error
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
3298 NULL
, mxt_interrupt
,
3299 IRQF_ONESHOT
| IRQF_NO_AUTOEN
,
3300 client
->name
, data
);
3302 dev_err(&client
->dev
, "Failed to register interrupt\n");
3306 error
= regulator_bulk_enable(ARRAY_SIZE(data
->regulators
),
3309 dev_err(&client
->dev
, "failed to enable regulators: %d\n",
3314 * The device takes 40ms to come up after power-on according
3315 * to the mXT224 datasheet, page 13.
3317 msleep(MXT_BACKUP_TIME
);
3319 if (data
->reset_gpio
) {
3320 /* Wait a while and then de-assert the RESET GPIO line */
3321 msleep(MXT_RESET_GPIO_TIME
);
3322 gpiod_set_value(data
->reset_gpio
, 0);
3323 msleep(MXT_RESET_INVALID_CHG
);
3327 * Controllers like mXT1386 have a dedicated WAKE line that could be
3328 * connected to a GPIO or to I2C SCL pin, or permanently asserted low.
3330 * This WAKE line is used for waking controller from a deep-sleep and
3331 * it needs to be asserted low for 25 milliseconds before I2C transfers
3332 * could be accepted by controller if it was in a deep-sleep mode.
3333 * Controller will go into sleep automatically after 2 seconds of
3334 * inactivity if WAKE line is deasserted and deep sleep is activated.
3336 * If WAKE line is connected to I2C SCL pin, then the first I2C transfer
3337 * will get an instant NAK and transfer needs to be retried after 25ms.
3339 * If WAKE line is connected to a GPIO line, the line must be asserted
3340 * 25ms before the host attempts to communicate with the controller.
3342 device_property_read_u32(&client
->dev
, "atmel,wakeup-method",
3343 &data
->wakeup_method
);
3345 error
= mxt_initialize(data
);
3347 goto err_disable_regulators
;
3351 err_disable_regulators
:
3352 regulator_bulk_disable(ARRAY_SIZE(data
->regulators
),
3357 static void mxt_remove(struct i2c_client
*client
)
3359 struct mxt_data
*data
= i2c_get_clientdata(client
);
3361 disable_irq(data
->irq
);
3362 mxt_free_input_device(data
);
3363 mxt_free_object_table(data
);
3364 regulator_bulk_disable(ARRAY_SIZE(data
->regulators
),
3368 static int mxt_suspend(struct device
*dev
)
3370 struct i2c_client
*client
= to_i2c_client(dev
);
3371 struct mxt_data
*data
= i2c_get_clientdata(client
);
3372 struct input_dev
*input_dev
= data
->input_dev
;
3377 mutex_lock(&input_dev
->mutex
);
3379 if (input_device_enabled(input_dev
))
3382 mutex_unlock(&input_dev
->mutex
);
3384 disable_irq(data
->irq
);
3389 static int mxt_resume(struct device
*dev
)
3391 struct i2c_client
*client
= to_i2c_client(dev
);
3392 struct mxt_data
*data
= i2c_get_clientdata(client
);
3393 struct input_dev
*input_dev
= data
->input_dev
;
3398 enable_irq(data
->irq
);
3400 mutex_lock(&input_dev
->mutex
);
3402 if (input_device_enabled(input_dev
))
3405 mutex_unlock(&input_dev
->mutex
);
3410 static DEFINE_SIMPLE_DEV_PM_OPS(mxt_pm_ops
, mxt_suspend
, mxt_resume
);
3412 static const struct of_device_id mxt_of_match
[] = {
3413 { .compatible
= "atmel,maxtouch", },
3414 /* Compatibles listed below are deprecated */
3415 { .compatible
= "atmel,qt602240_ts", },
3416 { .compatible
= "atmel,atmel_mxt_ts", },
3417 { .compatible
= "atmel,atmel_mxt_tp", },
3418 { .compatible
= "atmel,mXT224", },
3421 MODULE_DEVICE_TABLE(of
, mxt_of_match
);
3424 static const struct acpi_device_id mxt_acpi_id
[] = {
3425 { "ATML0000", 0 }, /* Touchpad */
3426 { "ATML0001", 0 }, /* Touchscreen */
3429 MODULE_DEVICE_TABLE(acpi
, mxt_acpi_id
);
3432 static const struct i2c_device_id mxt_id
[] = {
3440 MODULE_DEVICE_TABLE(i2c
, mxt_id
);
3442 static struct i2c_driver mxt_driver
= {
3444 .name
= "atmel_mxt_ts",
3445 .dev_groups
= mxt_groups
,
3446 .of_match_table
= mxt_of_match
,
3447 .acpi_match_table
= ACPI_PTR(mxt_acpi_id
),
3448 .pm
= pm_sleep_ptr(&mxt_pm_ops
),
3451 .remove
= mxt_remove
,
3455 module_i2c_driver(mxt_driver
);
3457 /* Module information */
3458 MODULE_AUTHOR("Joonyoung Shim <jy0922.shim@samsung.com>");
3459 MODULE_DESCRIPTION("Atmel maXTouch Touchscreen driver");
3460 MODULE_LICENSE("GPL");